Skip to main content

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

Project description

aod

Attr — 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:

import aod

class Person:
	firstname = aod.Attr[str]("fetch")
	lastname = aod.Attr[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 Attr.known(instance) to check whether a value has already been computed without triggering a fetch.

Note that using Attr 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.2.0.tar.gz (3.6 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.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aod-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6e292a2ef09e829f2fa652c3c58452b8df608f9d386ed37d7554436b38c7c7bc
MD5 af6b1283cf2ada60f3493adf6b9dc984
BLAKE2b-256 46a6fa980d0a7c1cd3a87c5a70d5e972db083af78d826f0f4a53ab7d4295d1d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aod-0.2.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13b28ef9a819110b3410aee8900a75fd1450d9f05c4998a878d00dca53cfb1ac
MD5 e10b6cab0c5d345db0ce875ca9901b15
BLAKE2b-256 74c746bf334a23bcf9c966d72b92998847c8f6f1c7077871a078224ee0e4a1f7

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