推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
openroc
V2EX  ›  Python

用 python 做了一个简单 dict 封装成 document,大家帮助 review 一下。:)(修改成 gist)

  •  
  •   openroc · May 28, 2014 · 3855 views
    This topic created in 4382 days ago, the information mentioned may be changed or developed.
    主要目的针对dict少写[]和“”,如 a["test"]["aaa"], 可以简化成a.test.aaa

    8 replies    2014-05-29 13:16:43 +08:00
    openroc
        1
    openroc  
    OP
       May 28, 2014
    binux
        2
    binux  
       May 28, 2014
    class Obict(dict):
    ....__getattr__ = dict.__getitem__
    ....__setattr__ = dict.__setitem__
    openroc
        3
    openroc  
    OP
       May 28, 2014
    @blnux, 要支持list和dict,嵌套。:)
    lifemaxer
        4
    lifemaxer  
       May 28, 2014
    @binux 又见牛人!上次的“uuid.uuid1()”
    binux
        5
    binux  
       May 28, 2014   ❤️ 1
    @openroc 嵌套就是 return 的时候 Obict 包一下就完了,list 同理
    而且,建议扩展基础类型的时候从 dict, 或者 UserDict 继承,保证类型兼容性。不然你让 keys, items, values, for loop 怎么办?
    openroc
        6
    openroc  
    OP
       May 28, 2014
    @binux, 嗯,类型兼容的确没有考虑,谢过。
    clino
        7
    clino  
       May 28, 2014
    这个有类似的作用:
    https://code.google.com/p/ulipad/source/browse/trunk/modules/dict4ini.py
    不过是用在ini文件上

    d = DictIni('t2.ini', format="%s:%s", hideData=True)
    d.a.a = 'mama'
    d.a.b = 'lubs me!'

    d.b.a = 'i'
    d.b.b = 'lub bosunmi!'

    d.c.a = 'dada'
    d.c.b = 'lubs me too!'
    d.save()
    chuangbo
        8
    chuangbo  
       May 29, 2014
    赞同 @binux,继承 dict 是必须的。
    web.py 的 Storage 是一个比较完整的实现。
    https://github.com/webpy/webpy/blob/master/web/utils.py#L52
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    ♥ Do have faith in what you're doing.