python基础
Python安装
PEP:源文件中声明编码
python解释器在解释文件时用到这些编码信息。
python标准编码方式:ASCII编码
python3.x所有字符串都被视为unicode
# -- coding: utf-8 --:把文件编码强制转换为utf-8
"%^.\n.∗?#.coding[:=]\s[0-9A-Za-z-_.]+.$ "
#coding=<encoding name>
#!/usr/bin/python
# -*- coding: <encoding name> -*-
# 第一行:为告诉Linux/OS X系统,是一个Python可执行程序,Windows忽略;
#!/usr/bin/python :是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器;
#!/usr/bin/env python :这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。
#当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
#!/usr/bin/python :相当于python的绝对路径;
#!/usr/bin/env python :会去环境设置寻找python目录(事先需要配置好环境变量)
# 第二行:告诉Python解释器,按照UTF-8编码读取源代码(中文)
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
__future__模块
把下一新版本的特性导入到当前版本,在当前版本中测试一些新版本的特性。
从Python 2.7到Python 3.x就有不兼容的一些改动,比如2.x里的字符串用’xxx’表示str,Unicode字符串用u’xxx’表示unicode,而在3.x中,所有字符串都被视为unicode,因此,写u’xxx’和’xxx’是完全一致的,而在2.x中以’xxx’表示的str就必须写成b’xxx’,以此表示“二进制字符串”。
print_function
division
with_statement
absolute_import
绝对引入主要是针对python2.4及之前的版本的,在引入某一个.py文件时,会首先从当前目录下查找是否有该文件。如果有,则优先引用当前包内的文件。而如果我们想引用python自带的.py文件时,则需要使用。
unicode_literals
使得所有的字符串文本成为Unicode字符串。这就意味着\u转义序列可以用于包含Unicode字符。
isinstance(s, unicode)
tf.app.run()
tf.flags.
- define your flags like tf.flags.DEFINE_integer(‘batch_size’, 128, ‘Number of images to process in a batch.’) and tf.app.run() will set things up so that you can globally access the passed values of the flags you defined, like tf.flags.FLAGS.batch_size from wherever you need it in your code.
- then run your custom main function with a set of arguments.
if name == “main”:
current file is executed under a shell instead of imported as a module.
如果当前是从其它模块调用的该模块程序,则不会运行main函数!
而如果就是直接运行的该模块程序,则会运行main函数。
app.py
def run(main=None, argv=None):
"""Runs the program with an optional 'main' function and 'argv' list."""
f = flags.FLAGS
# Extract the args from the optional `argv` list.
args = argv[1:] if argv else None
# Parse the known flags from that list, or from the command
# line otherwise.
# pylint: disable=protected-access
flags_passthrough = f._parse_flags(args=args) # ensures that the argument you pass through command line is valid
# pylint: enable=protected-access
main = main or sys.modules['__main__'].main # The first main in right side of '=' :the first argument of current function run(main=None, argv=None); sys.modules['__main__'] :current running file(e.g.my_model.py).
# Call the main function, passing through any arguments
# to the final program.
sys.exit(main(sys.argv[:1] + flags_passthrough)) # ensures your 'main(argv)' or 'my_main_running_function(argv)' function is called with parsed arguments properly
cases
- You don’t have a main function in my_model.py, Then you have to call tf.app.run(my_main_running_function) .
if there’s no main in the file, it instead uses whatever’s in sys.modules[‘main’].main. The sys.exit : to run the main command thus found using the args and any flags passed through, and to exit with the return value of main. - you have a main function in my_model.py. (This is mostly the case.)
enumerate(sequence, [start=0])
用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。