Skip to main content

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

### 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


Download files

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

Source Distribution

relattrs-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

relattrs-0.0.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: relattrs-0.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for relattrs-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e171ee4044b3cf23781df684b901132d366daf6a9b7e93e95644bbfaaba1033c
MD5 8375a98cb9c2b508ba54327a69eaafa3
BLAKE2b-256 a4771483f45d1dfe6b389d32854f2bdf2c515a4b0033f03434b7e9f3fba55336

See more details on using hashes here.

File details

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

File metadata

  • Download URL: relattrs-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for relattrs-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eb812e37fb40e296c03611e930f4679575a0a07af09a54ec8912df2940af54e7
MD5 f55e3b7714471bc4d6291b1af76dbcf2
BLAKE2b-256 bb943f55b629110ef0606e520e3da589b739ab13262f30ed28f56b645382c128

See more details on using hashes here.

Supported by

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