list和dict的子类,递归地提供属性访问和帮助函数

PyBoa的Python项目详细描述


Python

Latest VersionSupported Python versions

list和dict的子类,递归地提供属性访问和帮助函数

导入boa

>>>fromboaimportboa
^{pr2}$

做你平时做的事

boa返回dictlist的子类,让你做你想做的一切

doc: list, dict中的所有内容仍然可以完成

除了过多的方法:

列表.反转
>>>li=boa([1,2])>>>li.reverse()[2,1]>>>li# no change in the original list[1,2]# behaviour of list.reverse:>>>li.reverse(side_effect=True)>>>li[2,1]# or use list.reverse directly>>>li=boa([1,2])>>>list.reverse(li)>>>li[2,1]
列表.复制
>>>b=boa([1,{'d':3}])>>>c=b.copy()>>>b[0]=100# c is a copy of b, and still a boa object:>>>c[0]1>>>c[1].d3

改进:

列表.索引
>>>li=boa([1,2])>>>li.index(4)# same behaviour as the originalValueError:4isnotinlist>>>li.index(4,raise_exception=False)# improvementNone

也转换插入的数据:

>>>a=boa({})>>>a.x={'y':{'z':3}}>>>a.x.y.z3>>>a.li=[]>>>a.li.append({'b':2})>>>a.li[0].b2>>>a['b']=[{'li':[1,2]}]>>>a.b[0].li.map(lambdax:x+1)[2,3]

增加一些有用的功能

名单上:

映射

>>>li=boa([1,2])>>>li.map(lambdax:x+1)[2,3]>>>li=boa([xforxinrange(10)])>>>li.filter(lambdax:1<x<=4)[2,3,4]# .filter & .map return boa list>>>li=boa([{'x':x}forxinrange(10)])>>>li.filter(lambdaobj:obj.x<2).map(lambdaobj:obj.x)[0,1]

。减少

>>>li=boa([2,3])>>>li.reduce(lambdaacc,x:acc*x)6

随机使用.shuffle&randomChoice
>>>arr=boa([xforxinrange(10)])>>>arr.shuffle()[3,1,5,8,6,2,0,7,9,4]>>>arr.randomChoice()4# ``one of the element at random``

返回正常dict和list:

>>>a=boa({'li':[1,2,{'k':'v'}]})>>>b=a.toPython()>>>b.liAttributeError:'dict'objecthasnoattribute'li'>>>b['li'].map()AttributeError:'list'objecthasnoattribute'map'>>>b['li'][2].kAttributeError:'dict'objecthasnoattribute'k'

覆盖属性:

# can overide dictionary attributes, if you really want it:>>>d=boa({'items':1})>>>d.keys=2>>>d.values()dict_values([1,2])

包装函数

>>>@boa>>>deffun(x):>>>returnx>>>fun({'a':1}).a1

包裹物体?

classA:"""simple doc"""d={'key':'value'}deffun(self,data):returndata
>>>obj=boa(A())# keep the class name and doc informations:>>>obj.__class__.__name__'A'>>>obj.__doc__'simple doc'>>>obj.fun({'a':{'b':4}}).a.b4>>>obj.d.key'value'# no side effect on class A:>>>obj_2=A()>>>obj_2.fun({'b':2}).bAttributeError:'dict'objecthasnoattribute'b'>>>obj_2.d.keyAttributeError:'dict'objecthasnoattribute'key'
去月球!
classB:defget_a(self):returnA()defget_b(self):returnB()
>>>b=boa(B())>>>b.get_a().d.key'value'>>>b.get_b().get_b().get_a().d.key'value'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java广播接收器未检测到按键事件   java不为特定列更改创建修订   java采用了更实用的方法   java桌面窗格HTML呈现   java处理internet连接丢失   java在调整JFrame的大小时,如何防止JTable的列调整大小?   如何用Java中的“Scanner.hasNext”完成程序   具有模块名称的java停止IntelliJ项目工具窗口?   执行已编译的Rails-Warbler JAR文件时发生java加载错误   java Spring数据redis存储库不支持集合查询?   每个客户端的java队列请求   获取Java中的JSON嵌套数组元素   java GWT,Vaadin ConcurrentModificationException   firebase存储中的java在保存照片之前,我需要发送两次照片   JavaSpringEleaf如何在容器外使用变量