Skip to main content

objecttools

Various tools for working with objects and classes in Python

Cached properties

Works just like a normal property, but returned values are cached:

from objecttools import CachedProperty

class ExpensiveOperations(object):
    @CachedProperty
    def expensive_attribute(self):
        return self.calculate()

    # To make it settable

    @expensive_attribute.setter
    def expensive_attribute(self, value):
        pass

    # To make it deletable

    @expensive_attribute.deleter
    def expensive_attribute(self):
        pass

e = ExpensiveOperations()
e.other_attribute = 1
print(e.expensive_attribute)  # Takes a long time.
print(e.expensive_attribute)  # Very quick; just retrieve from cached
v = e.other_attribute

e.other_attribute = 2  # expensive_attribute should be different now!
print(e.expensive_attribute)  # Old value that is wrong.
del e.expensive_attribute
print(e.expensive_attribute)  # Takes a long time, but returns new value.
e.other_attribute = 1
# Reset to known value
e.expensive_attribute = v
print(e.expensive_attribute)  # Correct value!

Singletons

from objecttools import Singleton

Sentinel = Singleton.create('Sentinel')

Sentinel() is Sentinel()  # True

d.get('missing_value', Sentinel()) is Sentinel()  # True

class GlobalState(dict, metaclass=Singleton):
    attr = 0


gs = GlobalState()
gs['value'] = 7
gs.attr = 1

print(GlobalState()['value'] + GlobalState().attr)  # 8

Installing

From PyPI

$ pip install objecttools

From source

$ git clone 'https://github.com/MitalAshok/objecttools.git'
$ python ./objecttools/setup.py install

Metadata

Release files for objecttools 0.0.1

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

Source distribution (sdist)

Source distribution for objecttools 0.0.1
File Size Uploaded Provenance Actions
objecttools-0.0.1.tar.gz 4.6 kB Without provenance Details for objecttools-0.0.1.tar.gz

Release files / objecttools-0.0.1.tar.gz

Download URL objecttools-0.0.1.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3ae06eca05d0478e0937425e2708c7d8fb1f475616cb18267a2a235f577cfe77
BLAKE2b-256 checksum
How to use checksums
a45b7e21e9998ec26a7a1e1b11ae6e634bdcb8e8a44760111b127a086a895667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for objecttools 0.0.1

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