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 free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. See the LICENSE file for details.

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-2.0.0.tar.gz (16.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-2.0.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file relattrs-2.0.0.tar.gz.

File metadata

  • Download URL: relattrs-2.0.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for relattrs-2.0.0.tar.gz
Algorithm Hash digest
SHA256 57672c6656d55f00c90bc07f1c5e54a8ecf078ddbae7c94f667be430438e1fca
MD5 c7b24b726d21f2e52d5647bc4c54d326
BLAKE2b-256 f971e3100d326ac0969d2f15b9af7092722e0ddeda16df505d76a5108c9bd1e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for relattrs-2.0.0.tar.gz:

Publisher: publish.yml on Nibblex/python-relattrs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file relattrs-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: relattrs-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for relattrs-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7af305ebdc87ae7ffe39c1b164bb48ad05fff2b29240a9e2ba4ddaf2c6a539e3
MD5 2c2feb86ecee0a6c1477bdc9c1159c92
BLAKE2b-256 b9dc2f311c2a3e5bcfa1a0bde0e4605c5a3fb8056d4896e9865f1c2d0040297c

See more details on using hashes here.

Provenance

The following attestation bundles were made for relattrs-2.0.0-py3-none-any.whl:

Publisher: publish.yml on Nibblex/python-relattrs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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