V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Prodesire
V2EX  ›  Python

pydu: Python 常用数据结构和工具库,欢迎你的加入!!!

  •  2
     
  •   Prodesire ·
    Prodesire · 2017-12-28 18:39:24 +08:00 · 5177 次点击
    这是一个创建于 2282 天前的主题,其中的信息可能已经有所发展或是发生改变。
    项目名称:
    pydu

    项目地址:
    https://github.com/Prodesire/pydu

    项目描述:
    pydu ( python data structures and utils —— python 数据结构和工具)是一个面向 Python 2 和 3 的库。它收集自开源项目,也由贡献者创建。

    示例代码:
    英文版: http://pydu.readthedocs.io/en/latest/
    中文版: http://pydu.readthedocs.io/zh/latest/

    pydu 将平时常用的数据结构和工具都收录其中,可供日常开发使用,也可供学习借鉴。相比于 GitHub 上现存的 pyutils 等项目,其优势是积极维护,并有丰富文档。

    现在,我们非常欢迎大家一起来提出想法、参与讨论、贡献代码,将平时常用的功能抽象为通用的工具集和数据结构,供大家学习或使用!
    第 1 条附言  ·  2017-12-30 11:18:22 +08:00
    更多关于 pydu 的思考: https://zhuanlan.zhihu.com/p/32482132
    43 条回复    2018-01-06 23:33:25 +08:00
    agnewee
        1
    agnewee  
       2017-12-28 19:02:37 +08:00
    支持
    Kylin30
        2
    Kylin30  
       2017-12-28 22:18:47 +08:00
    先 star 为敬
    Prodesire
        3
    Prodesire  
    OP
       2017-12-28 22:37:29 +08:00
    @Kylin30 thanks
    Prodesire
        4
    Prodesire  
    OP
       2017-12-28 22:38:27 +08:00
    @agnewee 一起加油
    mingyun
        5
    mingyun  
       2017-12-28 23:22:51 +08:00
    不支持 win 7
    $ pip install pydu
    Collecting pydu
    Using cached pydu-0.3.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-4i3oi1nh\pydu\setup.p
    y", line 41, in <module>
    long_description=open('README.rst').read(),
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 1162: ill
    egal multibyte sequence

    ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1
    \AppData\Local\Temp\pip-build-4i3oi1nh\pydu\
    TuringGunner
        6
    TuringGunner  
       2017-12-28 23:39:18 +08:00
    支持一波,其实我觉得可以抛弃 python2 了
    haiyangcn
        7
    haiyangcn  
       2017-12-28 23:41:33 +08:00
    stared
    Prodesire
        8
    Prodesire  
    OP
       2017-12-28 23:50:02 +08:00
    @mingyun 应该是 readme 中的中文引起的。请问是使用 Python2 还是 3 ?
    Prodesire
        9
    Prodesire  
    OP
       2017-12-28 23:51:26 +08:00
    @TuringGunner 主要是由于工作项目原因,还必须得支持 Python 2。 (逃
    mingyun
        10
    mingyun  
       2017-12-29 00:41:59 +08:00
    lxml
        11
    lxml  
       2017-12-29 00:45:44 +08:00
    建议使用 Gitbook 维护,readthedocs.io 阅读体验略逊
    diggerdu
        12
    diggerdu  
       2017-12-29 03:03:27 +08:00 via iPhone
    pythonds
    264768502
        13
    264768502  
       2017-12-29 08:17:59 +08:00 via Android
    est
        14
    est  
       2017-12-29 09:11:37 +08:00
    https://github.com/Prodesire/pydu/blob/master/pydu/list.py

    这个在 py3 里貌似就一句话

    >>> list(dict.fromkeys('3938475638391'))
    ['3', '9', '8', '4', '7', '5', '6', '1']
    DeTamble
        15
    DeTamble  
       2017-12-29 09:14:47 +08:00
    Win 10, Python 3.6.3 安装失败。
    pip install 错误信息和上面那位一样。
    用本地文件安装同样失败:
    [py36] PS E:\Download\pydu-master> python setup.py install
    Traceback (most recent call last):
    File "setup.py", line 41, in <module>
    long_description=open('README.rst').read(),
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 1162: illegal multibyte sequence

    另外文档这里有三个 ipv6 写成 ipv4 了: https://pydu.readthedocs.io/zh/latest/network.html#pydu.network.is_ipv6
    xpresslink
        16
    xpresslink  
       2017-12-29 09:19:08 +08:00
    @est
    如果要保证元素出现次序稳定,应该用
    >>> from collections import OrderedDict
    >>> OrderedDict.fromkeys('3938475638391').keys()
    heaton_nobu
        17
    heaton_nobu  
       2017-12-29 09:48:19 +08:00
    Network 里的 is_ipv6 部分没有改过来
    swulling
        18
    swulling  
       2017-12-29 09:48:21 +08:00 via iPhone
    基础库,尤其是数据结构,基础算法之类
    没有一定的本事就别乱造轮子了……
    est
        19
    est  
       2017-12-29 10:06:49 +08:00
    @xpresslink python 3.6 以后的 dict 不会改变插入顺序了。

    python2 的结果:

    >>> list(dict.fromkeys('3938475638391'))
    ['1', '3', '5', '4', '7', '6', '9', '8']
    est
        20
    est  
       2017-12-29 10:10:52 +08:00   ❤️ 1
    @xpresslink .keys() 返回也不是 list 了。而是一个可以迭代的 view

    https://www.python.org/dev/peps/pep-3106/

    所以最后还是得 list() 一下。
    xpresslink
        21
    xpresslink  
       2017-12-29 10:55:17 +08:00
    支持一下,我用 smallcrocodile 提交了一个更新是关于 uniq list 的。
    在概浏览了一下,总体上来说代码质量没有到能实用的程度。
    比如 AttrDict

    def __getattr__(self, key):
    try:
    return self[key]
    except KeyError as k:
    raise AttributeError(k)

    这个异常处理明显有问题会抛出异常两次。
    return self[key] 如果 key 不存在直接就会抛异常了,后面又人工抛一次,体验不太好。
    还不如下面的方案,
    value = self.get(key, KeyError(k))
    if isinstance(value, KeyError):
    raise AttributeError(k)

    应该有更好的方案我只是举例说一下。
    另外就是是不是考虑多层字典 AttrDict 嵌套的方案。

    d = AttrDict(abc={'a': 1}, d=True)
    d.abc.a 怎么样?

    if isinstance(value, dict):
    value = AttrDict(value)
    return value
    xpresslink
        22
    xpresslink  
       2017-12-29 11:16:48 +08:00
    @est 在 python2 里返回就是 list,python3 改成的 iterator
    实际上造轮子是很难的,要解决众多版本特性差异问题。
    官方说了不要把 3.6 的字典有序当成可以确保的事儿。
    docs.python.org/3.6/whatsnew/3.6.html#new-dict-implementation
    > The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).

    另外就是在 key 出现哈希碰撞和字典发生扩容后 key 顺序是会改变的。
    wueizzz
        23
    wueizzz  
       2017-12-29 11:19:41 +08:00
    先 star 为敬
    est
        24
    est  
       2017-12-29 11:51:46 +08:00
    @xpresslink 官方的意思是,现在不能确保,因为将来才会变成强制标准。

    但是 Raymond Hettinger 这种核心人物搞的东西,一般不会有人去动。

    哈希碰撞和扩容发生顺序改变我倒是很感兴趣,有 case 么?
    est
        25
    est  
       2017-12-29 11:52:52 +08:00   ❤️ 1
    实际上我就是从 https://twitter.com/raymondh/status/944125570534621185 这里偷学到的技巧。python core developer 都这样说了。我觉得应该没啥坑吧。
    mailto1587
        26
    mailto1587  
       2017-12-29 11:58:18 +08:00
    公司项目也在用 py2,支持
    Prodesire
        27
    Prodesire  
    OP
       2017-12-29 12:06:54 +08:00
    @lxml 个人感觉 gitbook 体验也不太好。大家提提意见,看哪个更好?
    Prodesire
        28
    Prodesire  
    OP
       2017-12-29 12:08:07 +08:00
    @diggerdu 关注点不太相同,pythonds 关注的是算法上的数据结构。pydu 关注的是实际业务领域(运维、web 等)方面的数据结构和工具集。
    Prodesire
        29
    Prodesire  
    OP
       2017-12-29 12:08:30 +08:00
    @264768502 不错不错,纳入计划!
    Prodesire
        30
    Prodesire  
    OP
       2017-12-29 12:09:34 +08:00
    @est 不能保证有序
    Prodesire
        31
    Prodesire  
    OP
       2017-12-29 12:10:21 +08:00
    @DeTamble 今天尽快修复,感谢提出
    Prodesire
        32
    Prodesire  
    OP
       2017-12-29 12:11:27 +08:00
    @xpresslink 这个方案是可行的,接下来我做个 benchmark,看现有的和这个方案哪个更快,占用更低
    est
        33
    est  
       2017-12-29 12:12:03 +08:00
    @Prodesire 求 CPython 3.6 里的反例。
    Prodesire
        34
    Prodesire  
    OP
       2017-12-29 12:12:48 +08:00
    @swulling 集思广益
    Prodesire
        35
    Prodesire  
    OP
       2017-12-29 12:14:57 +08:00
    @xpresslink 这个是 AttriDict,概念上如果取值错误,应该抛出属性异常,所以才把 KeyError 改成 AttributeError
    Arnie97
        36
    Arnie97  
       2017-12-29 13:49:21 +08:00 via Android
    粗略看了下,不少功能在 py3 标准库里有实现😂

    pydu.cmd.terminate→os.kill
    pydu.console.console_size→shutil.get_terminal_size
    pydu.exception.ignore→contextlib.suppress
    pydu.misc.copy→shutil.copytree
    pydu.network.dotted_netmask→ipaddress

    当然 py2 用户不妨用楼主的实现,并且像 AttrDict 这种 JS 画风的语法糖官方肯定不会写的
    Prodesire
        37
    Prodesire  
    OP
       2017-12-29 20:15:36 +08:00
    @xpresslink 这点赞同!
    Prodesire
        38
    Prodesire  
    OP
       2017-12-29 20:16:37 +08:00
    @mingyun @DeTamble 已修复在 Windows 上使用 Python 3 安装的问题,见 pydu 0.3.1 版本
    Prodesire
        39
    Prodesire  
    OP
       2017-12-30 11:33:28 +08:00
    @est 3.6 中倒没有反例。如 @xpresslink 所说,官方并不推荐这么做。

    有一点你可能没有注意到,pydu 中的 uniq 还有一个参数是 key,举个例子:
    class A(object):
    def __init__(self, v):
    self.v = v
    def __repr__(self):
    return 'A({})'.format(self.v)

    l = [A(2), A(1), A(2)]
    uniq(l, key=lambda o: o.v) # 结果是 [A(2), A(1)]
    list(dict.fromkeys(l)) # 结果是 [A(2), A(1), A(2)],原因大家都知道,列表中都是不同对象,我们需要对比的是 v,但是没办法做到
    Prodesire
        40
    Prodesire  
    OP
       2017-12-30 11:42:50 +08:00
    @Arnie97 感谢斧正!
    pydu.cmd.terminate→os.kill #terminiate 在 windows 上的处理调用了 Win32API,会更加靠谱
    pydu.console.console_size→shutil.get_terminal_size #这个还真是
    pydu.exception.ignore→contextlib.suppress #这个还真是,Py2 上倒是可以参考实现了
    pydu.misc.copy→shutil.copytree #pydu 的 copy 更加上层,不用区分拷贝对象是文件还是文件夹,类似 Linux 上的 cp
    pydu.network.dotted_netmask→ipaddress # 确实是这样,Python3 新增的 ipaddress 解决了相关问题,dotted_netmask 算是 py2 上的补充吧。这段代码更大的意义是学习背后的实现,来自 requests 库。
    xpresslink
        41
    xpresslink  
       2017-12-31 15:02:41 +08:00
    @Prodesire
    你那个算法上有个歧义的地方,set([1, 2, 1.0]) -> set([1,2])
    用 dict.fromkeys 有相同的问题,因为 dict 的 key 都是调用的 objcet.__hash__()生成的,以前 python 就没有 set,都是用 dict 模拟。
    qsnow6
        42
    qsnow6  
       2018-01-02 10:59:32 +08:00
    可以的,作为脚手架的补充
    Prodesire
        43
    Prodesire  
    OP
       2018-01-06 23:33:25 +08:00
    @xpresslink 我觉得类似 1 和 1.0 我们通常可以认为是一样的,那么不论是 set 还是 dic.fromkeys 的结果都可以接受。
    如果说非要区分的话,倒是可以对 int, float, decimal.Decimal 做特殊处理。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3534 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 10:46 · PVG 18:46 · LAX 03:46 · JFK 06:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.