{"id":2368,"library":"zict","title":"Zict: Composable Mutable Mappings","description":"Zict provides a collection of abstract MutableMapping classes that can be composed to build complex storage systems. It allows developers to create custom dictionaries that manage data across different locations (in-memory, on disk), apply various eviction policies (like LRU), and handle data transformations (compression, serialization). This enables intuitive interfaces over sophisticated storage strategies. The library is currently at version 3.0.0 and appears to be actively maintained.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"https://github.com/dask/zict","tags":["mapping","cache","persistent storage","LRU","mutable mapping","serialization","compression"],"install":[{"cmd":"pip install zict","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"File","correct":"from zict import File"},{"symbol":"Func","correct":"from zict import Func"},{"symbol":"LRU","correct":"from zict import LRU"},{"symbol":"Buffer","correct":"from zict import Buffer"},{"note":"Requires `lmdb` to be installed separately for full functionality.","symbol":"LMDB","correct":"from zict import LMDB"},{"symbol":"Sieve","correct":"from zict import Sieve"}],"quickstart":{"code":"import pickle\nimport zlib\nimport os\nimport shutil\nfrom zict import File, Func, LRU\n\n# Create a temporary directory for file storage\nstorage_dir = 'zict_example_storage'\nif os.path.exists(storage_dir):\n    shutil.rmtree(storage_dir)\n\n# 1. File: A mapping that stores values as files in a directory\na = File(storage_dir, mode='a')\n\n# 2. Func: A mapping that applies functions (e.g., compress/decompress, dumps/loads)\n#    to values as they are set or retrieved from the underlying mapping.\nb = Func(zlib.compress, zlib.decompress, a)\nc = Func(pickle.dumps, pickle.loads, b)\n\n# 3. LRU: A Least Recently Used (LRU) cache that wraps another mapping.\n#    When the LRU overflows, items are evicted to the underlying mapping (c).\nd = LRU(10, c) # Keep 10 items in the LRU cache\n\n# Store and retrieve items\nd['x'] = [1, 2, 3]\nd['y'] = {'a': 1, 'b': 2}\n\nprint(f\"Value for 'x': {d['x']}\")\nprint(f\"Value for 'y': {d['y']}\")\n\n# Populate more items to trigger LRU eviction\nfor i in range(15):\n    d[str(i)] = i * 10\n\n# Accessing 'x' brings it back to the fast cache (LRU)\nprint(f\"Accessing 'x' again: {d['x']}\")\n\n# Clean up resources (important for File, LMDB, etc.)\nd.close()\n# Clean up the temporary storage directory\nshutil.rmtree(storage_dir)\n","lang":"python","description":"This example demonstrates how to compose `zict` mappings to create a multi-layered storage system. It sets up an LRU cache (`LRU`) backed by a function-transforming mapping (`Func`) for serialization and compression, which in turn writes to disk using a file-based mapping (`File`). This allows efficient in-memory caching with persistent, compressed storage."},"warnings":[{"fix":"Implement external locking mechanisms or ensure thread-safe underlying mappings for concurrent `__getitem__` and `__setitem__` operations when using `zict.Sieve`.","message":"When using `zict.Sieve` in a multithreaded environment, be aware that the `__getitem__` and `__setitem__` methods of the *underlying mappings* are not protected by locks. This means if multiple threads access the same underlying mapping concurrently via Sieve, race conditions could occur. Only `__contains__` and `__delitem__` methods of underlying mappings are protected if they are fast.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always call the `.close()` method on `zict` mappings that manage external resources when they are no longer needed. Consider using a `with` statement if the mapping supports the context manager protocol, or a `try...finally` block.","message":"Mappings that interface with system resources (e.g., `zict.File`, `zict.LMDB`) must have their `close()` method called to release those resources. Failure to do so can lead to resource leaks, file locks, or data corruption, especially in long-running applications or when reopening mappings.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If planning to use `zict.LMDB`, ensure `pip install lmdb` is run in your environment.","message":"`zict.LMDB` requires the `lmdb` Python package to be installed separately to function correctly. It is not listed as a direct dependency of `zict` itself. Without it, attempting to use `zict.LMDB` will result in an `ImportError`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}