Skip to main content

An alternative API for creating attrs fields

Project description

Elysia

PyPI - Python Version PyPI GitHub release (latest SemVer) PyPI - License Code style: Black

Elysia is an addon for attrs that provides an alternative API for creating fields on @defined classes.

Installation

pip install elysia

Usage

Elysia's sole export is the Fields class, which wraps attrs.field, attrs.setters, and attrs.validators to provide a more concise API for defining instance attributes.

Here's a brief example of a class created with attrs and Elysia:

from datetime import datetime

from attrs import define
from elysia import Fields


@define
class User:
    name: str = Fields.field()
    password: str = Fields.field(
        on_setattr=Fields.setters.validate,
        validator=Fields.validators.min_len(8)
    )

    created_at: datetime = Fields.attr(factory=datetime.utcnow, frozen=True)

The User class has two __init__ arguments: name and password. Whenever set, password is validated to ensure it's at least 8 characters long.

User also has a created_at attribute that can't set via an __init__ argument. When a User object is instantiated, created_at is set to the current time and cannot be changed afterwards.

So...how does all that work, exactly?

Glad you asked.

There are two ways to define an attribute with Elysia: Fields.field() and Fields.attr(). Fields.field() defines attributes that map to __init__ arguments; Fields.attr() defines attributes that do not. Both are wrappers around attrs.field and accept all the same arguments. Like attrs.field, all arguments to Fields.field() and Fields.attr() are keyword-only.

Both methods also accept an optional, boolean, frozen argument. Setting it to True is a shortcut for on_setattr=attrs.setters.frozen — that is, it freezes the attribute, raising an exception if you try to set it after initialization.

Warning

Elysia is happy to combine frozen=True with anything else you pass to on_setattr, but attrs.setters.frozen will be applied first, which may not be what you expect.

Fields also provides access to attrs' setters and validators via Fields.setters and Fields.validators, respectively. It makes no difference whether setters and validators are accessed through Fields or attrs. Do what you like.

License

Elysia is licensed under the MIT License.

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

elysia-1.1.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

elysia-1.1.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file elysia-1.1.1.tar.gz.

File metadata

  • Download URL: elysia-1.1.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.1 Linux/5.15.0-1034-azure

File hashes

Hashes for elysia-1.1.1.tar.gz
Algorithm Hash digest
SHA256 1f45374f994ae86833d59200c960bd0aaccdb5ccc65f5d7a6bbe3fc8376f7976
MD5 f8b9d463b6b2db98781ad9097959d210
BLAKE2b-256 abde67b9761b33016a3b3ef06bd385fe8446f4e8b2ea3ad972f52d5010678c93

See more details on using hashes here.

File details

Details for the file elysia-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: elysia-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.1 Linux/5.15.0-1034-azure

File hashes

Hashes for elysia-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3fc07c5c4e42c3e32fbc3b830a379fc7edea03c825627c1c46a98660fa88fd1d
MD5 d201f21fa64fd5f51903ea389b82e806
BLAKE2b-256 c3da1e089a52feecb5e5b776e06afce515eedff616469a88837a76e9c7de49b5

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