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.0.tar.gz (3.4 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.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aod-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 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.0.tar.gz
Algorithm Hash digest
SHA256 9711bcd5dbff022e63b4e44b320825639b3d415534831caa6a49e4bb6f1beaa2
MD5 847ddf2a853b6476981be15e711e5a0c
BLAKE2b-256 3499399ddabea6a0be9a914941df8e6514c9f2b6c3ff24e6f619bcfafc91e21c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aod-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdf7a9beec5cf404f975c8eba6dc01314f5d8f80b7abe276e604be4e779f98e1
MD5 303df483b5e05a7d86b02e7ed0122f64
BLAKE2b-256 da72db4d1cf61671322f466ea7682e1242eb0f46c2fb7de5659d415b0694a013

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