Toolkit for working with nth order relational attributes in Python classes.
Project description
python-relattrs
relattrs is a small utility library for recursively getting, setting, checking, and deleting attributes of an object using a dotted string representation. This can be particularly useful when dealing with nested objects.
The library provides four main functions:
rgetattr: Recursively gets an attribute from an object.
rhasattr: Recursively checks if an attribute exists on an object.
rsetattr: Recursively sets an attribute on an object.
rdelattr: Recursively deletes an attribute from an object.
Installation
You can install relattrs via pip:
pip install relattrs
Usage
rgetattr
Recursively gets an attribute from an object based on a dotted string representation.
Example:
from relattrs import rgetattr
class A:
class B:
class C:
value = 1
obj = A()
print(rgetattr(obj, "B.C.value")) # Output: 1
Example with *sep* parameter:
from relattrs import rgetattr
class A:
class B:
class C:
value = 1
obj = A()
print(rgetattr(obj, "B|C|value", sep="|")) # Output: 1
Example with *default* parameter:
from relattrs import rgetattr
class A:
class B:
class C:
value = 1
obj = A()
print(rgetattr(obj, "B.C.val", "Not found")) # Output: Not found
rhasattr
Recursively checks if an object has an attribute based on a dotted string representation.
Example:
from relattrs import rhasattr
class A:
class B:
class C:
value = 1
obj = A()
print(rhasattr(obj, "B.C.value")) # Output: True
print(rhasattr(obj, "B.C.val")) # Output: False
Example with *sep* parameter:
from relattrs import rhasattr
class A:
class B:
class C:
value = 1
obj = A()
print(rhasattr(obj, "B|C|value", sep="|")) # Output: True
print(rhasattr(obj, "B|C|val", sep="|")) # Output: False
rsetattr
Recursively sets an attribute on an object based on a dotted string representation.
Example:
from relattrs import rsetattr
class A:
class B:
class C:
value = 1
obj = A()
rsetattr(obj, "B.C.value", 2)
print(obj.B.C.value) # Output: 2
Example with *sep* parameter:
from relattrs import rsetattr
class A:
class B:
class C:
value = 1
obj = A()
rsetattr(obj, "B|C|value", 2, sep="|")
print(obj.B.C.value) # Output: 2
rdelattr
Recursively deletes an attribute from an object based on a dotted string representation.
Example:
from relattrs import rdelattr, rhasattr
class A:
class B:
class C:
value = 1
obj = A()
rdelattr(obj, "B.C.value")
print(rhasattr(obj, "B.C.value")) # Output: False
Example with *sep* parameter:
from relattrs import rdelattr, rhasattr
class A:
class B:
class C:
value = 1
obj = A()
rdelattr(obj, "B|C|value", sep="|")
print(rhasattr(obj, "B|C|value", sep="|")) # Output: False
License
This project is licensed under the MIT License.
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 relattrs-1.0.0.tar.gz.
File metadata
- Download URL: relattrs-1.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e6d7c0943d26d36264632c2444d6359bc256aaa0855b5b1954c679ec39b3a16
|
|
| MD5 |
0422cc6bfc3f723fc0fcf3fb8e06a0a6
|
|
| BLAKE2b-256 |
00297e5824aae1ddaf502666d19b4567674fbfde9a4fac5ea680747d31a38314
|
File details
Details for the file relattrs-1.0.0-py3-none-any.whl.
File metadata
- Download URL: relattrs-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
927582377aa5f4856c429baaf20fe070d67829ea0d0a08777d7403679f0c20d6
|
|
| MD5 |
19b5fa4f2e2c17bfb80db98b77fab95f
|
|
| BLAKE2b-256 |
a1689d7611e61731f3e49da41b7d172b83612498072634212210d8cc9c8efca2
|