---------------------------------------------------------------------------------------------
解决循环引用:
1. 不要import,直接将对象(动态)传过去,直接调用对象的方法;
(适合只有两层循环调用:A->B, B->A)
2. 不要用from,全部直接用import。
(适合多层循环调用:A->B, B->C, C->D, D->A)
dict 与 string 互转:
dict -> string : s = str(d)
string -> dict : d = eval(s)
---------------------------------------------------------------------------------------------
import上层文件:
package1|______init__.py
|______init__.pyc
|____common
| |______init__.py
| |______init__.pyc
| |____tools.py
| |____tools.pyc
|____package2
| |______init__.py
| |______init__.pyc
| |____package3
| | |______init__.py
| | |______init__.pyc
| | |____main.py
| | |____test.py
| | |____test.pyc
run.py
run.py:
from package1.package2.package3.test import test
test()
test.py:
from ...common import tools
def test():
print tools.add(1, 2)
python run.py
关于__init__.py: http://blog.csdn.net/yxmmxy7913/article/details/4233420
python导入同级别模块很方便:
import xxx
要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件
from dirname import xxx
要导入上级目录,可以使用sys.path:
首先sys.path的作用是:当使用import语句导入模块时,解释器会搜索当前模块所在目录以及sys.path指定的路径去找需要import的模块
所以改变思路,直接把上级目录加到sys.path里:sys.path.append('../')
from fatherdirname import xxx
---------------------------------------------------------------------------------------------
disable no-self-use warning:
# pylint: disable=no-self-use
关于pylint报第三方包的错(no method, no attri), 拿lxml为例:
The reason for this is that pylint by default only trusts C extensions from the standard library and will ignore those that aren't.
As lxml isn't part of stdlib, you have to whitelist it manually. To do this, navigate to the directory of your project in a terminal, and generate an rcfile for pylint:
$ pylint --generate-rcfile > .pylintrc
Then, open that file and edit add lxml to the whitelist like so:
extension-pkg-whitelist=lxml
After that, all E1101 errors regarding lxml should vanish.
---------------------------------------------------------------------------------------------
lxml使用:
from lxml import etree
def test():
root = etree.Element('xml')
elem = etree.SubElement(root, 'sender')
elem.text = etree.CDATA('zyd')
print etree.dump(root)
print root.find('sender').text
test()
---------------------------------------------------------------------------------------------
elasticsearch delete scroll
request_addr = es_addr + "/_search/scroll?scroll_id=" + request_json['scroll_id']
requests.delete(request_addr)
---------------------------------------------------------------------------------------------