Learning Python

---------------------------------------------------------------------------------------------

解决循环引用:

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)


---------------------------------------------------------------------------------------------



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值