Skip to main content

lazymap

A lazy mapping whose values are evaluated when accessed

Installation

You can install this package with pip.

$ pip install lazymap

Documentation

Source Code - GitHub

PyPI - lazymap

Usage

A LazyMap is a mapping whose values can be evaluated only when they are accessed. This is useful when you need a mapping which might not need to evaluate all of its values, but it is unknown which values will be needed.

Not all values of a LazyMap need to be lazily evaluated. You can also store regular values in a LazyMap.

LazyMap also supports caching of values, so that they are only evaluated once, although this can be disabled on a per-object or per-key basis.

Importing

from lazymap import LazyMap

Creating a LazyMap

You can initialize a LazyMap and provide it with initial static values, initial lazy values, a default factory function for missing keys, and any combination of the above. Additionally you can enable (default) or disable caching for the entire LazyMap.

If a default factory function is provided, it will be called with the key as an argument when a key is accessed which is not in the LazyMap.

static = LazyMap({"a": 1, "b": 2})
lazy = LazyMap({"c": lambda: 3})
default = LazyMap(default=lambda key: key * 2)
uncached = LazyMap({"random": lambda: randint(0, 100)}, cache=False)

Creating a LazyMap from keys and a function

You can initialize a LazyMap from a set of keys and a function which will be used to evaluate the value of each key. This way, the values are only evaluated when they are accessed.

You can also pass allow_missing=True to the fromkeys constructor to also use the function as a default factory function for missing keys.

def expensive(key):
	print(f'Calculating value for key {key}...')
	return key * 2

from_keys = LazyMap.fromkeys((1, 2, 3), expensive)

Adding values to a LazyMap

You can add non-lazy values to a LazyMap just like you would a dictionary.

lazy_map = LazyMap()
lazy_map["a"] = 1 # not lazy

You can also add lazy values to a LazyMap with the lazy method.

def get_b():
	print('Calculating value of x...')
	sleep(1)
	return 42

lazy_map.lazy("x", get_x) # lazy
print(lazy_map["x"]) # Calls get_x

Caching values

By default, the lazy values of a LazyMap are only evaluated once. The value is then cached and returned on subsequent accesses.

You can also pass cache=True or cache=False when adding a lazy key with lazy() to override the default caching behaviour for that key.

lazy_map = LazyMap()

def get_value():
	sleep(1)
	return randint(0, 100)

lazy_map.lazy("cached", get_value)
lazy_map.lazy("uncached", get_value, cache=False)

print(lazy_map["cached"]) # 42
print(lazy_map["cached"]) # 42
print(lazy_map["uncached"]) # 69
print(lazy_map["uncached"]) # 13

Metadata

Release files for lazymap 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lazymap 1.0.1
File Size Uploaded
lazymap-1.0.1.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lazymap 1.0.1
File Interpreter ABI Platform
lazymap-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.0 kB

Release files / lazymap-1.0.1.tar.gz

Download URL lazymap-1.0.1.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3756e22ac7ba8f84b4c0c10d92a14e55385fa05289059958719c67f59464bebf
BLAKE2b-256 checksum
How to use checksums
94f6087385f0a5a4666b6070587d52fed4fb229f6594ed666b282e34024f5519
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.8.18 Linux/6.5.0-1021-azure

Release files / lazymap-1.0.1-py3-none-any.whl

Download URL lazymap-1.0.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7bbea1273409899915e1dc291fc3cbd2e9b9e5e31ccd2bb6a6c8f0b1dec825c
BLAKE2b-256 checksum
How to use checksums
4ff541ed0193b6e7688ee2a1e34454938829f651106f65b661ee4410bb051596
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.8.18 Linux/6.5.0-1021-azure

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page