Skip to main content

A plugin extends power of attrs library.

Project description

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

Welcome to attrs_mate Documentation

attrs might be the second widely used python library for developers (First is requests). It is the ultimate weapon for writing class.

attrs_mate aims to bring more features to attrs, and better code pattern.

Usage1: More Utility Methods

from attrs_mate import attr, AttrsClass

@attr.s
class User(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")])

Usage2: Allow attrs to construct complex object from dict data.

Plus, this is an example of nesting schema.

import attr
from attrs_mate import AttrsClass


@attr.s
class Profile(AttrsClass):
    """
    firstname, lastname, ssn are generic data type field.
    """
    firstname = attr.ib(default=None)
    lastname = attr.ib(default=None)
    ssn = attr.ib(default=None)


@attr.s
class Degree(AttrsClass):
    name = attr.ib(default=None)
    year = attr.ib(default=None)


@attr.s
class People(AttrsClass):
    """
    - ``profile`` is nested field.
    - ``degrees`` is collection type field.
    """
    id = attr.ib(default=None)
    profile = attr.ib(
        converter=Profile.from_dict,
        validator=attr.validators.optional(
            attr.validators.instance_of(Profile)
        ),
        factory=Profile,
    )
    degrees = attr.ib(
        converter=lambda degrees: [
            Degree.from_dict(degree) for degree in degrees],
        factory=list,
    )

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)
>>> 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)])

Usage3: Cached Instance and Property Attribute

from attrs_mate import attr, LazyClass

@attr.s
class User(LazyClass): # instance are cached
    id = attr.ib()
    lastname = attr.ib()
    firstname = attr.ib()
    uuid_called_count = attr.ib(default=0)
    fullname_called_count = attr.ib(default=0)

    @LazyClass.lazyproperty
    def uuid(self):
        self.uuid_called_count += 1
        return self.id

    @LazyClass.lazyproperty
    def fullname(self): # property method are cached
        self.fullname_called_count += 1
        return "{} {}".format(self.lastname, self.firstname)

>>> user1 = User.lazymake(id=1, lastname="David", firstname="John")
>>> user1.fullname_called_count
0 # initially, fullname never been called
>>> user1.fullname
David John
>>> user1.fullname_called_count
1 # called once
>>> user1.fullname
David John
>>> user1.fullname_called_count
1 # 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_count
1 # already been called once, because it is actually user1

Install

attrs_mate is released on PyPI, so all you need is:

$ pip install attrs_mate

To upgrade to latest version:

$ pip install --upgrade attrs_mate

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

attrs_mate-0.0.3.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

attrs_mate-0.0.3-py2.py3-none-any.whl (18.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file attrs_mate-0.0.3.tar.gz.

File metadata

  • Download URL: attrs_mate-0.0.3.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.13

File hashes

Hashes for attrs_mate-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b68031c43aa5e6e6b3b54ac9492d3d566f2c78069d35e10861ce91806c1d31ee
MD5 392976fc5bf0f902c5e1fd81bb8eaea5
BLAKE2b-256 fad37c35c5227fcc92d45764d71715d6cf3d860d2e708dcc8731516e17eeb23a

See more details on using hashes here.

File details

Details for the file attrs_mate-0.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: attrs_mate-0.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.13

File hashes

Hashes for attrs_mate-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2c74315ad1e9aba86cc5290a73887fe16c1ea437173a4d31068fb134408854c9
MD5 7b1c6a3255e80ecd195d5777c78bc4b3
BLAKE2b-256 7dcca85201c761656f208d9098b316a730b356430204877db1445445f4599f29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page