插件扩展了attrs库的功能。

attrs-mate的Python项目详细描述


https://travis-ci.org/MacHu-GWU/attrs_mate-project.svg?branch=masterhttps://codecov.io/gh/MacHu-GWU/attrs_mate-project/branch/master/graph/badge.svghttps://img.shields.io/pypi/v/attrs_mate.svghttps://img.shields.io/pypi/l/attrs_mate.svghttps://img.shields.io/pypi/pyversions/attrs_mate.svghttps://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Document-blue.svghttps://img.shields.io/badge/Link-API-blue.svghttps://img.shields.io/badge/Link-Source_Code-blue.svghttps://img.shields.io/badge/Link-Install-blue.svghttps://img.shields.io/badge/Link-GitHub-blue.svghttps://img.shields.io/badge/Link-Submit_Issue-blue.svghttps://img.shields.io/badge/Link-Request_Feature-blue.svghttps://img.shields.io/badge/Link-Download-blue.svg

欢迎使用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

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

推荐PyPI第三方库


热门话题
java与Groovy正则表达式匹配混淆   java控制台未定义为JSNI   主目录中的java指针异常   java如何获取hashmap中的arrayList的大小   ApachePOI用于文本,Java代码中的word doc(.docx)中没有进行追加   Ruby对象到Java对象   JavaSpringJPA哈希集只返回一个值   Java在执行由配置了精确类路径参数的mavenjarpluin生成的可执行jar时无法找到依赖项   java我试图在画布上写一个文本,但什么也没发生   java HTTP 404源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。不起作用   java提取小数点后的最后一个数字   oop Java:无法访问对象的元素   PHP URL中RecyclerView中的java句柄空异常   不调用OnCreateViewHolder的java Update RecyclerView适配器项