Toolkit for working with nth order relational attributes in Python classes.
Project description
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.
Installation
You can install relattrs via pip:
pip install relattrs
Functions
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.
Usage
Here’s how you can use each function:
### 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.
MIT License
Copyright (c) 2024 Sergio Rodríguez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
File details
Details for the file relattrs-0.0.4.tar.gz
.
File metadata
- Download URL: relattrs-0.0.4.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 423f47082002771430667768a103b19b4997149bb1db258656442831f5603ac7 |
|
MD5 | 5db34d9f959a74e6a58a6c0bc5108475 |
|
BLAKE2b-256 | d700c7d61d93710fe209ebf3361f7f762ab883945572be375fbe9d74610941f4 |
File details
Details for the file relattrs-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: relattrs-0.0.4-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 329084fd479b8255c08f290928fd6d474b932e9de4bbaebe91dc93fceceb7d15 |
|
MD5 | 1c95e85d6673a0814f4d2bdc2c67f9cd |
|
BLAKE2b-256 | 8cd44df0a8fd1a04c2c4b836d383b7c1260ea9fa43044dea82f632e9a04dbb81 |