lazymap


Namelazymap JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/abrahammurciano/python-lazymap
SummaryA lazy mapping whose values are evaluated when accessed
upload_time2024-05-22 08:07:00
maintainerNone
docs_urlNone
authorAbraham Murciano
requires_python>=3.8
licenseGPLv3
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # lazymap

A lazy mapping whose values are evaluated when accessed

## Installation

You can install this package with pip.
```sh
$ pip install lazymap
```

## Links

[![Documentation](https://img.shields.io/badge/Documentation-C61C3E?style=for-the-badge&logo=Read+the+Docs&logoColor=%23FFFFFF)](https://abrahammurciano.github.io/python-lazymap)

[![Source Code - GitHub](https://img.shields.io/badge/Source_Code-GitHub-181717?style=for-the-badge&logo=GitHub&logoColor=%23FFFFFF)](https://github.com/abrahammurciano/python-lazymap.git)

[![PyPI - lazymap](https://img.shields.io/badge/PyPI-lazymap-006DAD?style=for-the-badge&logo=PyPI&logoColor=%23FFD242)](https://pypi.org/project/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

```python
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`.

```python
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.

```python
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.

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

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

```python
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.

```python
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
```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/abrahammurciano/python-lazymap",
    "name": "lazymap",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Abraham Murciano",
    "author_email": "abrahammurciano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/94/f6/087385f0a5a4666b6070587d52fed4fb229f6594ed666b282e34024f5519/lazymap-1.0.1.tar.gz",
    "platform": null,
    "description": "# lazymap\n\nA lazy mapping whose values are evaluated when accessed\n\n## Installation\n\nYou can install this package with pip.\n```sh\n$ pip install lazymap\n```\n\n## Links\n\n[![Documentation](https://img.shields.io/badge/Documentation-C61C3E?style=for-the-badge&logo=Read+the+Docs&logoColor=%23FFFFFF)](https://abrahammurciano.github.io/python-lazymap)\n\n[![Source Code - GitHub](https://img.shields.io/badge/Source_Code-GitHub-181717?style=for-the-badge&logo=GitHub&logoColor=%23FFFFFF)](https://github.com/abrahammurciano/python-lazymap.git)\n\n[![PyPI - lazymap](https://img.shields.io/badge/PyPI-lazymap-006DAD?style=for-the-badge&logo=PyPI&logoColor=%23FFD242)](https://pypi.org/project/lazymap/)\n\n## Usage\n\nA `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.\n\nNot all values of a `LazyMap` need to be lazily evaluated. You can also store regular values in a `LazyMap`.\n\n`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.\n\n### Importing\n\n```python\nfrom lazymap import LazyMap\n```\n\n### Creating a LazyMap\n\nYou 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`.\n\nIf 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`.\n\n```python\nstatic = LazyMap({\"a\": 1, \"b\": 2})\nlazy = LazyMap({\"c\": lambda: 3})\ndefault = LazyMap(default=lambda key: key * 2)\nuncached = LazyMap({\"random\": lambda: randint(0, 100)}, cache=False)\n```\n\n### Creating a LazyMap from keys and a function\n\nYou 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.\n\nYou can also pass `allow_missing=True` to the `fromkeys` constructor to also use the function as a default factory function for missing keys.\n\n```python\ndef expensive(key):\n\tprint(f'Calculating value for key {key}...')\n\treturn key * 2\n\nfrom_keys = LazyMap.fromkeys((1, 2, 3), expensive)\n```\n\n### Adding values to a LazyMap\n\nYou can add **non-lazy** values to a `LazyMap` just like you would a dictionary.\n\n```python\nlazy_map = LazyMap()\nlazy_map[\"a\"] = 1 # not lazy\n```\n\nYou can also add **lazy** values to a `LazyMap` with the `lazy` method.\n\n```python\ndef get_b():\n\tprint('Calculating value of x...')\n\tsleep(1)\n\treturn 42\n\nlazy_map.lazy(\"x\", get_x) # lazy\nprint(lazy_map[\"x\"]) # Calls get_x\n```\n\n### Caching values\n\nBy default, the lazy values of a `LazyMap` are only evaluated once. The value is then cached and returned on subsequent accesses.\n\nYou can also pass `cache=True` or `cache=False` when adding a lazy key with `lazy()` to override the default caching behaviour for that key.\n\n```python\nlazy_map = LazyMap()\n\ndef get_value():\n\tsleep(1)\n\treturn randint(0, 100)\n\nlazy_map.lazy(\"cached\", get_value)\nlazy_map.lazy(\"uncached\", get_value, cache=False)\n\nprint(lazy_map[\"cached\"]) # 42\nprint(lazy_map[\"cached\"]) # 42\nprint(lazy_map[\"uncached\"]) # 69\nprint(lazy_map[\"uncached\"]) # 13\n```",
    "bugtrack_url": null,
    "license": "GPLv3",
    "summary": "A lazy mapping whose values are evaluated when accessed",
    "version": "1.0.1",
    "project_urls": {
        "Documentation": "https://abrahammurciano.github.io/python-lazymap/lazymap",
        "Homepage": "https://github.com/abrahammurciano/python-lazymap",
        "Repository": "https://github.com/abrahammurciano/python-lazymap"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ff541ed0193b6e7688ee2a1e34454938829f651106f65b661ee4410bb051596",
                "md5": "f8ae79066c9172d04f4ab998bdaa97e0",
                "sha256": "e7bbea1273409899915e1dc291fc3cbd2e9b9e5e31ccd2bb6a6c8f0b1dec825c"
            },
            "downloads": -1,
            "filename": "lazymap-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f8ae79066c9172d04f4ab998bdaa97e0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16313,
            "upload_time": "2024-05-22T08:06:59",
            "upload_time_iso_8601": "2024-05-22T08:06:59.285086Z",
            "url": "https://files.pythonhosted.org/packages/4f/f5/41ed0193b6e7688ee2a1e34454938829f651106f65b661ee4410bb051596/lazymap-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "94f6087385f0a5a4666b6070587d52fed4fb229f6594ed666b282e34024f5519",
                "md5": "35c32986856fb7550464113fd1f9adbd",
                "sha256": "3756e22ac7ba8f84b4c0c10d92a14e55385fa05289059958719c67f59464bebf"
            },
            "downloads": -1,
            "filename": "lazymap-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "35c32986856fb7550464113fd1f9adbd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 15721,
            "upload_time": "2024-05-22T08:07:00",
            "upload_time_iso_8601": "2024-05-22T08:07:00.860470Z",
            "url": "https://files.pythonhosted.org/packages/94/f6/087385f0a5a4666b6070587d52fed4fb229f6594ed666b282e34024f5519/lazymap-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-22 08:07:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "abrahammurciano",
    "github_project": "python-lazymap",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "lazymap"
}
        
Elapsed time: 0.97756s