Dataclass-like class attributes hierarchical system
Project description
Attrarchy
Introduction
The infuse decorator will install some additional methods to supplied class
and convert class attributes to Attrs.
Attrs are descriptors that live on the enhanced class definition and store information
about the attribute (data type/class, name, position in the hierarchy). Any attribute
can have a leader whose value will be used if its own is not set. Any attribute can have a parent
whose value will be used (or rather the parent's leader's value if it is defined) if attribute
does not have a leader or its leader also does not have a value set. Any top level attributes
have to have a default value set.
When instancing an enhanced class, the whole attribute hierarchy is copied, preserving the relationships between the nested attributes.
The intention here is that attributes on class definition serve as default values, whereas attributes on instances can override them if desired. Attrs have a value change signal. Setting attribute values during initialization is handled. Validation might be added in the future.
Motivation
Yet another "dataclass" clone. Inspired in part by attrs (www.attrs.org), when I wanted to figure out how does it work.
I wanted to see how far I can push this with Python, messing with descriptors and injecting external functionality into existing classes. The concept mostly works, but there are some quirks, especially if mixed with class inheritance. Still work in progress.
Simple Example
import attrarchy as aa
@aa.infuse
class Test:
attr: float = 1.
attr2: int = 2
test = Test(attr=5.)
print(test.attr()) # 5.
print(test.attr2()) # 2
Note that typecheck is likely to protest that Test.attr is defined as float and therefore is not callable.
However, @aa.infuse will replace Test.attr float by a descriptor with enhanced functionality.
Allowing test.attr = 2 assignment would replace the Attr descriptor and break attrarchy.
Structure
Briefly, Attr represents one of two types: 1. leaf attr that represent a single value (can be int, str, dataclass, ...)
and 2. node attr which contain other attributes. The second type is the enhanced class.
What infuse does is to add methods and (hidden) attributes that turn
supplied class into a node attr. In addition, some class attributes are changed into leaf attrs.
Individual attributes can be linked to other attributes (of compatible type) and use their values if their own is not defined. Nested attributes can also ask their parent node attrs for a value. If a value cannot be found for a given instance (or within its network), class hierarchy is queried.
THE RULES
- hierarchy is set on class creation
- leaf with class root (i.e. the top most parent is a user class) has to have value or leader set (or their parent's leader's equivalent)
- Nodes do not have "real" value, they aggregate values of their leaves
- effective value of leaves is self.value, self.leader.value, self.parent.leader.self_equivalent.value (possibly going for its leader, etc), self_class_equivalent.value
- leaders need to have the same structure/datatype as self
- leaders have the same hierarchy root as followers
- Attrs cannot have a value of type "type".
Rules are not enforced for the most part, but things are likely to break if not followed.
Notes
- Classes derived from enhanced classes should be explicitly infused too. Otherwise, they will share its ancestor class attrs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file attrarchy-0.5.0.tar.gz.
File metadata
- Download URL: attrarchy-0.5.0.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
221da0695bd7429093202f6fbfec129a1656aa6f51cac9d794287e32dc0dd8ed
|
|
| MD5 |
7c11cd087489e4017d908166db26d586
|
|
| BLAKE2b-256 |
a828c295526d9bbe3c87203d5295beae2181fb69de2e377dbdb02a23c966b894
|
File details
Details for the file Attrarchy-0.5.0-py3-none-any.whl.
File metadata
- Download URL: Attrarchy-0.5.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
384b6fef8efc71c38090f4c77590268fb307af86187a29d8abcac6291c81ca0d
|
|
| MD5 |
8fdfd32f03e7669fd880d4df7f1f7e28
|
|
| BLAKE2b-256 |
97a68fa76199afc9b6d397adadb27500a5e60227aa22137a872d96d282d0e22c
|