標籤:
記錄Tornado-4.0.2源碼的閱讀,學習,分析
options.py1. imports 部分1.1 __future__
1 from __future__ import absolute_import, division, print_function, with_statement
future_statement 即:from __futuren import XXX. 參見 XiaoKL學Python(C)__future__
1.2 other imports from Python Library
1 import datetime2 import numbers3 import re4 import sys5 import os6 import textwrap
1.3 other imports from tornado
1 from tornado.escape import _unicode2 from tornado.log import define_logging_options3 from tornado import stack_context4 from tornado.util import basestring_type, exec_in
2. 定義的類2.1 OptionParser類
該類負責解析命令列的Option, 每個option被抽象為類_Option的對象。
2.2 _Option類
代表命令列上的每個option,該類為內部類。
2.3 全域的OptionParser對象
定義了一個全域的OptionParser對象。options.py提供的介面都是在該對象上進行操作的。
末尾調用了以下介面來將log相關的option添加到options.py模組中。
1 define_logging_options(options)
3. options.py對外提供的介面
[Todo]
3.1 define
1 def define(name, default=None, type=None, help=None, metavar=None,2 multiple=False, group=None, callback=None)
3.2 parse_command_line
1 def parse_config_file(path, final=True)
Learn From options.py1. sys模組
import sys
https://docs.python.org/2/library/sys.html?highlight=sys#module-sys
sys._getframe([depth])
"Return a frame object from the call stack. If optional integer depth is given, return the frame
object that many calls below the top of the stack. If that is deeper than the call stack, ValueError
is raised. The default fordepth is zero, returning the frame at the top of the call stack."
在options.py中的使用,在OptionParser類的define方法的實現中:
1 frame = sys._getframe(0)
2. inspect 模組
https://docs.python.org/2/library/inspect.html
"The inspect module provides several useful functions to help get information about live
objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. "
該模組中包含 frame 類型(即: sys._getframe() 傳回值的類型 )的說明。
3. unittest.mock.patch
mock 模組
Python中的單元測試。
4. 不瞭解的文法4.1 return xxxx if yyyy else zzzz
1 def value(self):2 return self.default if self._value is _Option.UNSET else self._value
4.2 xxx for yyy, zzz in ttt
1 _TIMEDELTA_ABBREV_DICT = dict(2 (abbrev, full) for full, abbrevs in _TIMEDELTA_ABBREVS3 for abbrev in abbrevs)
4.3 Regex
1 _FLOAT_PATTERN = r‘[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?‘
Reference
1. http://www.tornadoweb.org/en/stable/options.html
Python.tornado.2-tornado.options