Lazy
lazyfields has utilities to allow for lazily defined attributes so that you can avoid some patterns like the one below or avoid calculating a property twice.
class MyClass:
def __init__(self):
self._val = None
def get_val(self):
if val is None:
self._do_something()
return self._val
lazyfield
The lazyfields.lazyfield descriptor streamlines the process of implementing lazy attributes. Here's how you can use it:
from lazyfields import lazyfield
class MyClass:
@lazyfield
def expensive_value(self):
return do_expensive_operation()
instance = MyClass()
instance.expensive_value # Does the expensive operation, saves the result and returns the value
instance.expensive_value # Returns directly the cached value
del instance.expensive_value # Cleans the cache
instance.expensive_value # redo the expensive operation
instance.expensive_value = "Other" # Overwrites the cached value with the value assigned
lazyfieldsaves the value directly in the class as a hidden attribute so you don't have to worry about garbage collection or weakrefs
asynclazyfield
The lazyfields.asynclazyfield descriptor tackles the same issue as lazyfield, but while preserving the asynchronous API
from lazyfields import asynclazyfield
class MyClass:
@asynclazyfield
async def expensive_value(self):
return await do_expensive_operation()
instance = MyClass()
await instance.expensive_value() # you still call it as a function, but it will do the same thing, call the function, store the result and return the value
await instance.expensive_value() # now it only returns the value
dellazy(instance, "expensive_value") # clear the stored value
await instance.expensive_value() # here the calculation is done again
setlazy(instance, "expensive_value", "Other") # overwrite the stored value with "Other"
Helpers
lazyfields provides helpers to work with frozen classes or when using the asynclazyfield and need to set or reset the field manually
setlazy(instance: Any, attribute: str, value: Any, bypass_setattr: bool = False)setlazy allows you to directly change the hidden attribute behind any lazy field, and the bypass_setattr parameter will instead of using thesetattrfunction, useobject.__setattr__.dellazy(instance: Any, attribute: str, bypass_delattr: bool = False)dellazy allows you to clear the value stored in the hidden attribute to allow for recalculation. Similar to setlazy the bypass_delattr parameter will useobject.__delattr__instead ofdelattrforce_set(instance: Any, attribute: str, value: Any)force_set is just a shortcut for setlazy(..., bypass_setattr=True)force_del(instance: Any, attribute: str)force_del is just a shortcut for dellazy(..., bypass_delattr=True)is_initialized(instance: Any, attribute: str)Returns whether the lazyfield has stored a value yet, without triggering the routine inadvertently.
Metadata
Release files for lazy-fields 1.1.2
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 |
|---|---|---|---|---|
| lazy_fields-1.1.2.tar.gz | 9.3 kB | Without provenance | Details for lazy_fields-1.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lazy_fields-1.1.2-py3-none-any.whl | Python 3 | none | any | Details for lazy_fields-1.1.2-py3-none-any.whl |
Total release size: 19.0 kB
Release files / lazy_fields-1.1.2.tar.gz
| Download URL | lazy_fields-1.1.2.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a3ab320ac218ba52a627ed80e7bf0c72ba49ba3b785c4bb952609630d7c2d3f
|
|
BLAKE2b-256 checksum How to use checksums |
af30dce61f498cd4832d2ed7691d1318cf6dc45fb2d08a6fe3599b188f37259e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.8 Linux/6.12.6-100.fc40.x86_64
|
Release files / lazy_fields-1.1.2-py3-none-any.whl
| Download URL | lazy_fields-1.1.2-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b8ed38780ad646b137600b158223e4e421e577cfe74199d42817d00ac7ef25c
|
|
BLAKE2b-256 checksum How to use checksums |
7cc705837a23e68241874a1f610dcd0a230e66adc555b19d5e1dd2e8902ca113
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.8 Linux/6.12.6-100.fc40.x86_64
|
Security for lazy-fields 1.1.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.