Skip to main content

A descriptor for instance attributes whose values are determined on demand on first access.

Project description

aod

AttrOnDemand — a descriptor for instance attributes whose values are determined on demand on first access.

Installation

pip install aod

Requires Python 3.12 or later (uses PEP 695 generic syntax).

Usage

The name of the method that will set the attribute values can be passed to the constructor. Several attributes can share the same fetch method, so a single call can populate them all at once:

from aod import AttrOnDemand

class Person:
	firstname = AttrOnDemand[str]("fetch")
	lastname = AttrOnDemand[str]("fetch")

	def __init__(self, dbid):
		self.dbid = dbid

	def fetch(self):
		cursor = db.cursor()
		cursor.execute("select firstname, lastname from person where id=:id", id=self.dbid)
		row = cursor.fetchone()
		self.firstname = row.firstname
		self.lastname = row.lastname

p = Person(42)
print(p.firstname)   # triggers fetch, sets both firstname and lastname
print(p.lastname)    # already known, no fetch

Internally the value of an attribute foo will be stored in the instance dictionary as _foo. As long as the value hasn't been set yet, _foo will not be in the instance dictionary, and accessing the attribute will call the fetch method. The setter simply sets _foo in the instance dictionary, so the fetch method should set the attribute values via the setter.

Deleting an attribute forgets its cached value; the next access will fetch it again:

del p.firstname
print(p.firstname)   # fetches again (and refreshes lastname too)

Use AttrOnDemand.known(instance) to check whether a value has already been computed without triggering a fetch.

Note that using AttrOnDemand descriptors only works for instances that have instance dictionaries.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aod-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aod-0.1.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file aod-0.1.1.tar.gz.

File metadata

  • Download URL: aod-0.1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for aod-0.1.1.tar.gz
Algorithm Hash digest
SHA256 47b01ecc1e5637623901f72ec284c6c4e15f98ba65fd76fa3bf8e5cf6b98c875
MD5 a11f68570d9ec3ecc4e82e7a88016fe2
BLAKE2b-256 6afef68e5963cd55ad4d215fe89c3eaa1328cf721be19a60ebbafd0c9f65a9dd

See more details on using hashes here.

File details

Details for the file aod-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: aod-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for aod-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45bc68e1d54a8bc153c3ad5832f9047a4de82f6c91827f1e90911b307b440543
MD5 cc728e9c28c015532c11d4fdb6e01b3c
BLAKE2b-256 aa6fc51a4a93b915abc61df24289c66dd9bb97a0789341eb425c3cf6cf9cabda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page