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 cache
v = e.expensive_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

For Python 2 and 3 compatibility, use it as a decorator:

@Singleton.as_decorator
class Class(object):
    pass

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.4

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.4
File Size Uploaded
objecttools-0.0.4.tar.gz 5.1 kB Details

Release files / objecttools-0.0.4.tar.gz

Download URL objecttools-0.0.4.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6b182fd994c66eca50cf03ab2a0806bdf3825e65f8aad1c6f7243c81ee4d1c46
BLAKE2b-256 checksum
How to use checksums
c3a70fadb38247b4ba3ead80cb4656d1950b44973905d20bf68cbd52d07cd227
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.0.1

1 release file

1.0.0

1 release file

0.0.6

1 release file

0.0.5

1 release file

This release

0.0.4 This release

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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