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

Welcome to attrs_mate Documentation

Usage: more utility methods.

import attr
from attrs_mate import 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")])

Feature: allow attrs to load complex object from dict data.

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(
        convert=Profile.from_dict,
        factory=Profile,
    )
    degrees = attr.ib(
        convert=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()

# {
#     'id': 1,
#     'profile': {
#         'lastname': 'John', 'ssn': '123-45-6789', 'firstname': 'David'
#     },
#     'degrees': [
#         {'name': 'Bachelor', 'year': 2004},
#         {'name': 'Master', 'year': 2006}
#     ]
# }
print(people_data)

people = People.from_dict(people_data)
# People(id=1, profile=Profile(firstname='David', lastname='John', ssn='123-45-6789'), degrees=[Degree(name='Bachelor', year=2004), Degree(name='Master', year=2006)])
print(people)

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.2.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

attrs_mate-0.0.2-py2-none-any.whl (9.9 kB view hashes)

Uploaded Python 2

Supported by

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