Skip to content

Nibblex/python-relattrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyPI - Python Version PyPI - Version Ruff pre-commit.ci status https://codecov.io/gh/Nibblex/python-relattrs/graph/badge.svg?token=JL8M6865RB PyPI - License

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.

About

Toolkit for working with nth order relational attributes in Python classes.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages