list和dict的子类,递归地提供属性访问和帮助函数
PyBoa的Python项目详细描述
Python
list和dict的子类,递归地提供属性访问和帮助函数
导入boa
>>>fromboaimportboa^{pr2}$
做你平时做的事
boa
返回dict
和list
的子类,让你做你想做的一切
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'
标签:
- 项目
>>>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'去月球!
标签: