Python的import嵌套
[root@fuel ~]# vi /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/models/node.py
88
89 class Node(Base):
304 @classmethod
305 def delete_by_ids(cls, ids):
306 db.query(Node).filter(Node.id.in_(ids)).delete('fetch')
32 from nailgun.db import db
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db.py中的db变量。
发现db.py不存在:
[root@fuel ~]# ls -l /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db.py
进入nailgun目录:
[root@fuel ~]# cd /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun
[root@fuel nailgun]# ls
发现nailgun目录下还有一个db目录:
[root@fuel nailgun]# cd db
[root@fuel db]# ls
deadlock_detector.py deadlock_detector.pyo __init__.pyc migration
deadlock_detector.pyc __init__.py __init__.pyo sqlalchemy
从网络了解到:“__init__.py”会在被导入时执行,因此这是Python另一种导入方式。
[root@fuel db]# vi __init__.py
17 from nailgun.db.sqlalchemy import db
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy.py中的db变量。
同样不存在sqlalchemy.py,但存在sqlalchemy目录:
[root@fuel sqlalchemy]# vi __init__.py
80 db = scoped_session(
81 sessionmaker(
82 autoflush=True,
83 autocommit=False,
84 bind=engine,
85 query_cls=query_class,
86 class_=session_class
87 )
88 )
24 from sqlalchemy.orm import scoped_session
26 from sqlalchemy.orm import sessionmaker
可看出引用了/usr/lib/python2.6/site-packages/sqlalchemy/orm.py中的scoped_session、sessionmaker变量。不存在orm.py,且不存在orm目录。
19 from sqlalchemy import create_engine
31 from nailgun.db.sqlalchemy import utils
35 db_str = utils.make_dsn(**settings.DATABASE)
36 engine = create_engine(db_str, client_encoding='utf8')
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy.py中的utils对象或/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/__init__.py中引用了utils对象。
db_str明显是数据库连接字符串,内容是“postgresql://nailgun:dvbbYDuU@168.5.21.2:5432/nailgun”。
72 if settings.DEVELOPMENT:
73 query_class = DeadlockDetectingQuery
74 session_class = DeadlockDetectingSession
75 else:
76 query_class = NoCacheQuery
77 session_class = Session
32 from nailgun.settings import settings
进入docker:
[root@fuel sqlalchemy]# docker exec -it fuel-core-6.1-nailgun /bin/sh
sh-4.1# cd /usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy
sh-4.1# vi __init__.py
33 #zzy added
34 from nailgun.logger import logger
35 import sys
82 #zzy added
83 logger.warning('____ffff____')
84 logger.warning(sys.path)
sh-4.1# python __init__.py
sh-4.1# tail -n 20 /tmp/nailgun.log |grep WARNING
2016-01-27 06:24:20.257 WARNING [7f92c5287700] (__init__) ____ffff____
2016-01-27 06:24:20.257 WARNING [7f92c5287700] (__init__) ['/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages']
orm.py或orm目录使用以下命令查找:
ls /usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/sqlalchemy/orm*
ls /usr/lib64/python26.zip/sqlalchemy/orm*
ls /usr/lib64/python2.6/sqlalchemy/orm*
ls /usr/lib64/python2.6/plat-linux2/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-tk/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-old/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-dynload/sqlalchemy/orm*
ls /usr/lib64/python2.6/site-packages/sqlalchemy/orm*
ls /usr/lib/python2.6/site-packages/sqlalchemy/orm*
最终发现:
sh-4.1# cd /usr/lib64/python2.6/site-packages/sqlalchemy/orm
sh-4.1# vi __init__.py
63 from .scoping import (
64 scoped_session
65 )
56 from .session import (
57 Session,
58 object_session,
59 sessionmaker,
60 make_transient,
61 make_transient_to_detached
62 )
从网络了解到,“.scoping”指“__init__.py”同一级目录的“scoping.py”:
sh-4.1# vi scoping.py
14 __all__ = ['scoped_session']
15
16
17 class scoped_session(object):
18 """Provides scoped management of :class:`.Session` objects.
“__all__”定义只导出scoped_session类(当scoping.py被import的时候)。
sh-4.1# vi session.py
27 __all__ = ['Session', 'SessionTransaction',
28 'SessionExtension', 'sessionmaker']
2253 class sessionmaker(_SessionClassMethods):
2254
2255 """A configurable :class:`.Session` factory.
2256
2257 The :class:`.sessionmaker` factory generates new
2258 :class:`.Session` objects when called, creating them given
2259 the configurational arguments established here.
/usr/lib64/python2.6/site-packages/sqlalchemy/orm/scoping.py
105 def query_property(self, query_cls=None):
106 """return a class property which produces a :class:`.Query` object
107 against the class and the current :class:`.Session` when called.
108
109 e.g.::
110
111 Session = scoped_session(sessionmaker())
112
113 class MyClass(object):
114 query = Session.query_property()
115
116 # after mappers are defined
117 result = MyClass.query.filter(MyClass.name=='foo').all()
118
119 Produces instances of the session's configured query class by
120 default. To override and use a custom implementation, provide
121 a ``query_cls`` callable. The callable will be invoked with
122 the class's mapper as a positional argument and a session
123 keyword argument.
124
125 There is no limit to the number of query properties placed on
126 a class.
127
128 """
129 class query(object):
130 def __get__(s, instance, owner):
131 try:
132 mapper = class_mapper(owner)
133 if mapper:
134 if query_cls:
135 # custom query class
136 return query_cls(mapper, session=self.registry() )
137 else:
138 # session's configured query class
139 return self.registry().query(mapper)
140 except orm_exc.UnmappedClassError:
141 return None
142 return query()
143
Python的import嵌套的更多相关文章
- python之import机制
1. 标准 import Python 中所有加载到内存的模块都放在 sys.modules .当 import 一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将 ...
- 浅析python 的import 模块(转)
摘要: 学习python有几天了,对import一直不是很清楚,和C里面的include是否一样,重复引入有问题么?搜索路径是怎样的?整理解决下我的疑问. 一 模块的搜索路径 模块的搜索路径都放在了s ...
- python基础—函数嵌套与闭包
python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间 内置在解释器中的名称 2 全局名称空间 顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...
- python中的嵌套类
python中的嵌套类 在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问 ...
- python中import和from...import区别
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
- Python中import的使用
python中的import语句是用来导入模块的,在python模块库中有着大量的模块可供使用,要想使用这些文件需要用import语句把指定模块导入到当前程序中. import语句的作用 import ...
- python 的import机制2
http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块, ...
- python中import和from...import...的区别
python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...
- python的import与from...import的不同之处
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...
随机推荐
- elasticsearch快照和恢复
摘要:es可以通过简单的命令对索引或者整个集群进行快照和恢复 快照和恢复 Snapshot and restore 模块允许创建单个索引或者整个集群的快照到远程仓库. 在初始版本里只支持共享文件系统的 ...
- caffe的matlab接口一览表
blob 简述 方法: shape reshape get_diff set_diff 私有方法: check_and_preprocess_shape check_and_preprocess_da ...
- Inno Setup入门(十六)——Inno Setup类参考(2)
分类: Install Setup 2013-02-02 11:28 815人阅读 评论(0) 收藏 举报 这里将接着在前面的基础上介绍如何在自定义页面上添加按钮.按钮属于Tbutton类,该类继承自 ...
- Java 中的四种引用及垃圾回收策略
Java 中有四种引用:强引用.软引用.弱引用.虚引用: 其主要区别在于垃圾回收时是否进行回收: 1.强引用 使用最普遍的引用.如果一个对象具有强引用,那就 类似于必不可少的生活用品,垃圾回收器绝不会 ...
- 【转】RestQL:现代化的 API 开发方式
原文:http://tech.meituan.com/koa-restql.html RestQL:现代化的 API 开发方式 李鑫 ·2016-08-12 11:26 koa-restql 已经在 ...
- C++ 字符串字面值
C++ 字符串字面值 C++ 基本字符串类型 C++ 字符串类型 char 和 wchar_t c11 新增了 char16_t 和 char32_t 例子: wchat_t title[] = L& ...
- iOS UIScrollView偏移量属性
contentSize: The size of the content view. 其实就是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentS ...
- jQuery仿百度帖吧头部固定不随滚动条滚动效果
<style> *{margin:0px;padding:0px;} div.nav{background:#000000;height:57px;line-height:57px;col ...
- Python 3.5.1 Syntax & APIs(Continue Updating..
print(x, end=' ') instead of print(x) to escape the default line-changing-output. print(str.ljust(si ...
- C --> OC with RunTime
前言 本来打算写一篇关于runtime的学习总结,无奈长篇大论不是我的风格,就像写申论一样痛苦,加之网上关于tuntime的文章多如牛毛,应该也够童子们学习的了,今天就随便聊聊我的理解吧. runti ...