Easyclasses
Lightweight alternative to dataclasses
Quick Example
from easyclasses import EasyClass
class MyClass(EasyClass):
a: str
b: str
c: str = "test"
print(MyClass("a", "b", c="hi")) # MyClass(a='a', b='b', c='hi')
Installation
Linux/Mac
python3 -m pip install -U easyclasses
Windows
py -3 -m pip install -U easyclasses
Performance
Based on benchmarks.py, easyclasses is over 6x faster than the built-in dataclasses module.
On top of that, it's also outperforms NamedTuple.
Usage
Basics
Easyclasses API is pretty similar to dataclasses.
The main difference between dataclasses is that easyclasses uses a subclass instead of a decorator, like so:
from easyclasses import EasyClass
class MyEasyClass(EasyClass):
a: str
You can also provide default arguments the same way:
from easyclasses import EasyClass
class MyEasyClass(EasyClass):
a: str
b: str = "h"
You can pass subclass arguments to enable or disable certain features:
from easyclasses import EasyClass
class MyEasyClass(EasyClass, eq=False, immutable=True):
a: str
MyEasyClass("a") == MyEasyClass("a") # False
MyEasyClass("a").a = "test" # TypeError
If you aren't planning on using features like __eq__, then you can use LightEasyClass, which has features like that removed, allowing it to be faster than the normal EasyClass:
from easyclasses import LightEasyClass
class MyEasyClass(LightEasyClass): # works the same
a: str
Factories
If you need a default argument for a mutable object (such as list or dict), you can use the factory function:
from easyclasses import EasyClass, factory
class MyEasyClass(EasyClass):
a: list = factory(list)
Validators
You can set a validator using the validator function:
from easyclasses import EasyClass, validator
class MyEasyClass(EasyClass):
a: str = validator(str, lambda v: v == "hi")
MyEasyClass("hi") # OK
MyEasyClass("a") # AssertionError
You can even use it with factory:
from easyclasses import EasyClass, validator, factory
class MyEasyClass(EasyClass):
a: list = validator(factory(list), lambda v: v == [1])
MyEasyClass([1, 2]) # AssertionError
Metadata
Release files for easyclasses 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| easyclasses-1.0.1.tar.gz | 3.6 kB | Without provenance | Details for easyclasses-1.0.1.tar.gz |
Release files / easyclasses-1.0.1.tar.gz
| Download URL | easyclasses-1.0.1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f85579ad9e775358370fe7416ce3b2d36bb9fbb619fabba99ea81c19d5e9ebc5
|
|
BLAKE2b-256 checksum How to use checksums |
5cab1c5458b1f8e58ee7d28da0d332c2e43d3351424136376dde0f9be1092121
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Security for easyclasses 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.