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

2017-12-28 18:39:24 +08:00
 Prodesire
项目名称:
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 等项目,其优势是积极维护,并有丰富文档。

现在,我们非常欢迎大家一起来提出想法、参与讨论、贡献代码,将平时常用的功能抽象为通用的工具集和数据结构,供大家学习或使用!
5235 次点击
所在节点    Python
43 条回复
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
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
2017-12-29 11:19:41 +08:00
先 star 为敬
est
2017-12-29 11:51:46 +08:00
@xpresslink 官方的意思是,现在不能确保,因为将来才会变成强制标准。

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

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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/418389

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX