插件扩展了attrs库的功能。
attrs-mate的Python项目详细描述
欢迎使用attrs_mate文档
attrs可能是开发人员第二个广泛使用的python库(第一个是requests)。它是写作课堂的终极武器。
attrs_mate旨在为attrs带来更多功能,更少的代码,更好的代码模式。
用法1:更多实用方法
fromattrs_mateimportattr,AttrsClass@attr.sclassUser(AttrsClass):id=attr.ib()name=attr.ib()user=User(id=1,name="Alice")user.keys()# ["id", "name"]user.values()# [1, "Alice"]user.items()# [("id", 1), ("name": "Alice")]user.to_dict()# {"id": 1, "name": "Alice"}user.to_OrderedDict()# OrderedDict([("id", 1), ("name": "Alice")])
用法2:允许属性从dict数据构造复杂对象。
plus,这是嵌套模式的示例。
importattrfromattrs_mateimportAttrsClass@attr.sclassProfile(AttrsClass):""" firstname, lastname, ssn are generic data type field. """firstname=AttrsClass.ib_str()# default String Validatorlastname=AttrsClass.ib_str()ssn=AttrsClass.ib_str()@attr.sclassDegree(AttrsClass):name=AttrsClass.ib_str()year=AttrsClass.ib_int()# default Integer Validator@attr.sclassPeople(AttrsClass):""" - ``profile`` is nested field. - ``degrees`` is collection type field. """id=AttrsClass.ib_int()profile=Profile.ib_nested()# default Nested Schema Validator and Converterdegrees=Degree.ib_list()# default Nested Schema Validator and Converter>>>people=People(id=1,profile=Profile(firstname="David",lastname="John",ssn="123-45-6789",),degrees=[Degree(name="Bachelor",year=2004),Degree(name="Master",year=2006),],)>>>people_data=people.to_dict()>>>people_data{'id':1,'profile':{'lastname':'John','ssn':'123-45-6789','firstname':'David'},'degrees':[{'name':'Bachelor','year':2004},{'name':'Master','year':2006}]}>>>people=People.from_dict(people_data)>>>peoplePeople(id=1,profile=Profile(firstname='David',lastname='John',ssn='123-45-6789'),degrees=[Degree(name='Bachelor',year=2004),Degree(name='Master',year=2006)])
或者您可以在字典中传递嵌套模式,其工作原理相同:
>>>people=People(id=1,profile=dict(firstname="David",lastname="John",ssn="123-45-6789",),degrees=[dict(name="Bachelor",year=2004),dict(name="Master",year=2006),],)
usage3:缓存的实例和属性属性
fromattrs_mateimportattr,LazyClass@attr.sclassUser(LazyClass):# instance are cachedid=attr.ib()lastname=attr.ib()firstname=attr.ib()uuid_called_count=attr.ib(default=0)fullname_called_count=attr.ib(default=0)@LazyClass.lazypropertydefuuid(self):self.uuid_called_count+=1returnself.id@LazyClass.lazypropertydeffullname(self):# property method are cachedself.fullname_called_count+=1return"{} {}".format(self.lastname,self.firstname)>>>user1=User.lazymake(id=1,lastname="David",firstname="John")>>>user1.fullname_called_count0# initially, fullname never been called>>>user1.fullnameDavidJohn>>>user1.fullname_called_count1# called once>>>user1.fullnameDavidJohn>>>user1.fullname_called_count1# User.fullname() not been called# use factory method to create new instance>>>user2=User.lazymake(id=1,lastname="David",firstname="Kim")>>>id(user1)==id(user2)True# since>>>user2.firstname=="John"True>>>user2.fullname_called_count1# already been called once, because it is actually user1
安装
attrs_mate在pypi上发布,所以您只需要:
$ pip install attrs_mate
要升级到最新版本:
$ pip install --upgrade attrs_mate