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
ObjectProxy
Create a weak-referenceable proxy to an object that supports the same operations:
from objecttools import ObjectProxy
ls = [1, 2, 3]
proxy = ObjectProxy(ls)
proxy.append(4)
proxy += [5, 6]
proxy[2:4] = [7, 8, 9]
print(ls) # [1, 2, 7, 8, 9, 5, 6]
serializable
Create serializable forms of objects (For pickling)
from objecttools import SerializableFunction, SerializableConstant
import pickle
file = open('file.txt', 'w')
f = lambda file, a: file.write(a)
try:
# Cannot pickle files, even though it is a global constant
gile = pickle.loads(pickle.dumps(file))
except TypeError:
gile = pickle.loads(pickle.dumps(SerializableConstant('file', __name__))).value
try:
# Cannot pickle functions if they are lambdas
# Or are inner functions
# Or are deleted after creation
# Or are methods
g = pickle.loads(pickle.dumps(f))
except pickle.PicklingError:
g = pickle.loads(pickle.dumps(SerializableFunction(f)))
g(gile, 'data') # Works
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 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| objecttools-1.0.1.tar.gz | 11.4 kB | Without provenance | Details for objecttools-1.0.1.tar.gz |
Release files / objecttools-1.0.1.tar.gz
| Download URL | objecttools-1.0.1.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba27b70c5bf9ef479d60dd6f79f60dc274e2134bb9cd35fafaf5477f28e9d37e
|
|
BLAKE2b-256 checksum How to use checksums |
f15f25089ad4fea2a9de63b09f66a55236f808e8667d31a3ea38a965fb9ca47b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for objecttools 1.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.