Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

relattrs-1.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

relattrs-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

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

Hashes for relattrs-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2e6d7c0943d26d36264632c2444d6359bc256aaa0855b5b1954c679ec39b3a16
MD5 0422cc6bfc3f723fc0fcf3fb8e06a0a6
BLAKE2b-256 00297e5824aae1ddaf502666d19b4567674fbfde9a4fac5ea680747d31a38314

See more details on using hashes here.

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

Hashes for relattrs-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 927582377aa5f4856c429baaf20fe070d67829ea0d0a08777d7403679f0c20d6
MD5 19b5fa4f2e2c17bfb80db98b77fab95f
BLAKE2b-256 a1689d7611e61731f3e49da41b7d172b83612498072634212210d8cc9c8efca2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page