zoukankan      html  css  js  c++  java
  • 自定义内建模块

     1 在 python 中, 用户可以通过 py 文件创建自定义的 module, 也可以通过 C 创建 dll, 扩展 python module.
     2 当用户在一个正在编辑的模块 module 中, 引入(import)另一个已经编辑好的 module 的时候,
     3 需要名字指明另一个 module 的所在位置,python 才能成功 import 该模块.
     4     例如,
     5         在 A.py 中 import abc 文件夹下的 123 module,
     6             A.py,
     7                 import abc/123
     8 
     9             目录结构如下,
    10             A.py
    11             - abc
    12                 - 123.py
    13 
    14 然而当引入 built in module 的时候, 只需要以 import + name of built in module 的形式即可,
    15import sys . 这是为什么呢?为什么 python 会知道所要 import 的 built in module 所在的位置呢?
    16 毫无疑问, 一定是有这么一个机制 - 能否告诉 python 所引用的 built in module 在什么地方儿呢,
    17 所以对与 built in module 的 import 来说,在 import 的时候不需要显示的指出 module 所在位置.
    18 
    19 下面来具体看一下儿,
    20     在 python 启动之后, python 已经创建了一个 名字空间 namespace,
    21     在这个 namespace 中的符号和值来至于系统 module.而这些系统 module 中,
    22     __builtin__ module 就是其中一个被创建的 module .
    23     而 python 会创建一个 PyDictObject 对象,来维护系统所有 modules.
    24 
    25         static PyMethodDef builtin_methods[] = {
    26         {"__built_class__", (PyCFunction)builtin___built_class__,METH_VARARGS | METH_KEYWORDS, built_class_doc},
    27         {"__import__",      (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
    28         {"dir",             builtin_dir,     METH_VARARGS | METH_KEYWORDS, built_class_doc},   METH_VARARGS, dir_doc},
    29         {"getattr",         builtin_getattr,    METH_VARARGS, getattr_doc},
    30         {"iter",            builtin_iter,       METH_VARARGS, iter_doc},
    31         {"max",             (PyCFunction)builtin_max,        METH_VARARGS | METH_KEYWORDS, max_doc},
    32         {"min",             (PyCFunction)builtin_min,        METH_VARARGS | METH_KEYWORDS, min_doc},
    33         {"next",            (PyCFunction)builtin_next,       METH_VARARGS, next_doc},
    34         {"print",           (PyCFunction)builtin_print,      METH_VARARGS | METH_KEYWORDS, print_doc},
    35         {"round",           (PyCFunction)builtin_round,      METH_VARARGS | METH_KEYWORDS, round_doc},
    36         {"vars",            builtin_vars,       METH_VARARGS, vars_doc},};
    37 
    38     设置完 builtin methods 的属性, 接下就需要这个一个函数, 可以告诉 python 所引入 builtin module 的搜索路径了.
    39     这个函数就是,
    40         void Py_SetPath(const wchar_t *)
    41 
    42         python docs org 上面的解释如下,
    43             Set the default module search path. If this function is called before 
    44             Py_Initialize(),then Py_GetPath() won’t attempt to compute a default 
    45             search path but uses the one provided instead.This is useful if Python
    46             is embedded by an application that has full knowledge of the location
    47             of all modules.The path components should be separated by the platform
    48             dependent delimiter character,which is ':' on Unix and Mac OS X, 
    49             ';' on Windows.
    50             This also causes sys.executable to be set only to the raw program name 
    51             (see Py_SetProgramName()) and for sys.prefix and sys.exec_prefix to be
    52             empty. It is up to the caller to modify these if required after calling
    53             Py_Initialize().Use Py_DecodeLocale() to decode a bytes string to get 
    54             a wchar_* string.The path argument is copied internally, so the caller
    55             may free it after the call completes.
    56 
    57     最后,来一起看一个添加自定义 builtin module 的示例,
    58         例子,
    59             其实很简单, 值需要将自定义模块(zzyzz.py), 放在路径 "PythonPython36-32lib" 下就可以了,
    60                 zzyzz.py
    61                     + import datetime
    62                       def weeknumber():
    63                           print  ("Week - %s" % datetime.date.today().isocalendar()[1])
    64                           print ("Date - %s" % str(datetime.date.today().isocalendar()))
    65                           print ("Date - %s" % str(datetime.date.today()))
    66 
    67                 Output,
    68                     >>>import zzyzz
    69                     >>>zzyzz.weeknumber()
    70                     Week - 43
    71                     Date - (2017, 43, 1)
    72                     Date - 2017-10-23
    73                     >>>sys.modules['zzyzz']
    74                     <module 'zzyzz' from '...\Python\Python36-32\lib\zzyzz.py'>
  • 相关阅读:
    如何通过Restful API的方式读取SAP Commerce Cloud的Product图片
    Permute Digits
    Browser
    Jamie and Alarm Snooze
    Perfect Squares
    Color the ball
    Stars
    1086. Tree Traversals Again (25)
    1085. Perfect Sequence (25)
    1083. List Grades (25)
  • 原文地址:https://www.cnblogs.com/zzyzz/p/7716465.html
Copyright © 2011-2022 走看看