Skip to main content

Oba: Converting a Python Iterable Object to an Attribute Class

PyPI GitHub

Introduction

Converting iterable objects (such as dictionaries, tuples, lists, sets, and subclasses) to access values using class attributes instead of square brackets []. Supports recursive operations and supports both getting and setting values.

Installation

pip install oba

Usage

from oba import Obj

# Convert a dictionary to an attribute class
d = dict(a=[1, 2, 3], b=[4, dict(x=1)], c=dict(l='hello'))
o = Obj(d)

# Get values
print(o.a[2])  # => 3
print(o.c.l)  # => hello

# Set values
o.b[1].x = 4
print(o.b[1]())  # => {'x': 4}

points = [dict(x=1, y=2), dict(x=-1, y=0), dict(x=0, y=1)]
o = Obj(points)

o[0].x += 1
print(o[0]())  # => {'x': 2, 'y': 2}

o['c.l'] = 'world'
print(o.c.l)  # => world

Comparison

namedtuple (built-in module)

from collections import namedtuple

o = namedtuple('Struct', d.keys())(*d.values())

print(o)  # => Struct(a=[1, 2, 3], b=[4, {'x': 1}], c={'l': 'hello'})
print(o.a)  # => [1, 2, 3]

o.x = 2  # => AttributeError

bunch (package)

from bunch import Bunch

o = Bunch(d)

print(o.a)  # => [1, 2, 3]
print(o.b['x'])  # => 1
print(o.b.x)  # => KeyError

o.x = 2  # OK, editable

o = Bunch(points)  # => AttributeError

json (built-in module)

import json

class Obj(object):
    def __init__(self, d):
        self.__dict__.update(d)

o = json.loads(json.dumps(d), object_hook=Obj)

print(o.a[2])  # => 3
print(o.c.l)  # => hello

o.x = 2  # OK, editable

o = Obj(points)

o[0].x += 1
print(o[0].x)  # => 2

mock (package)

from mock import Mock

o = Mock(**d)

print(o.a)  # => [1, 2, 3]
print(o.b['x'])  # => 1
print(o.b.x)  # => KeyError

o.x = 2  # OK

o = Mock(*points)  # => TypeError

Summary

Feature namedtuple bunch (2011) json mock (2020) Oba (2022)
built-in ✓ ✗ 11K ✓ ✗ 28K ✗ 3K
recursive ✗ ✗ ✓ ✗ ✓
revert to raw ✗ ✓ (with extra operations) ✗ ✗ ✓
editable ✗ ✓ ✓ ✓ ✓
iterable ✓ (no keys) ✓ ✗ ✗ ✓
support dict ✓ ✓ ✓ ✓ ✓
support list ✓ ✗ ✓ ✗ ✓
support tuple ✗ ✗ ✓ ✗ ✓
tolerable ✗ ✗ ✗ ✗ ✓

Features

Additionally, Oba also has a unique tolerance for unknown attributes. In cases where some attributes do not exist, for example:

d = dict(a=1)
print(d['b'])  # KeyError

Other libraries will immediately raise an error. However, in some scenarios (such as reading configuration files), the absence of sub-attributes is a common problem, and we hope to be able to tolerate and monitor the existence of such errors.

from oba import Obj

d = dict(a=1)
o = Obj(d)

print('x' in o)  # => False
if not o.x.y.z:  # OK
    print('not exist')  # => not exist
print(o.x.y.z)  # => ValueError: NoneObj (x.y.z)  # locating the non-existent attribute chain

Its internal implementation is that when an attribute does not exist, the object automatically switches to the NoneObj class and records the attribute chain.

License

MIT

Metadata

Release files for oba 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oba 0.3.2
File Size Uploaded Provenance
oba-0.3.2.tar.gz 5.4 kB Without provenance Details

Release files / oba-0.3.2.tar.gz

Download URL oba-0.3.2.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bac3287a1bf4dad6f1eea9459dcbcdb861d9a5f664df2f2805f829867606ff4d
BLAKE2b-256 checksum
How to use checksums
bde8e796384f272cbd1b44bcc06a97d26db04b59cc63bf5498adbc3a6e7274a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for oba 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page