vcorelib.dict package#
Submodules#
vcorelib.dict.cache module#
A module implementing a JSON-file database.
- class vcorelib.dict.cache.DirectoryCache(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>)[source]#
Bases:
JsonCache
A class implementing a JSON cache based on a directory.
- class vcorelib.dict.cache.FileCache(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>)[source]#
Bases:
JsonCache
A class implementing a JSON cache based on a file.
- class vcorelib.dict.cache.JsonCache(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>)[source]#
Bases:
ABC
A JSON cach interface.
vcorelib.dict.codec module#
A module for implementing classes that can be loaded from dictionaries and exported as dictionaries.
- class vcorelib.dict.codec.BasicDictCodec(data: Dict[str, str | int | float | bool | None | Dict[str, str | int | float | bool | None] | List[str | int | float | bool | None]] = None, schemas: SchemaMap = None, dest_attr: str = 'data', verify: bool = True)[source]#
Bases:
DictCodec
The simplest possible dictionary codec implementation.
- class vcorelib.dict.codec.DictCodec(data: Dict[str, str | int | float | bool | None | Dict[str, str | int | float | bool | None] | List[str | int | float | bool | None]] = None, schemas: SchemaMap = None, dest_attr: str = 'data', verify: bool = True)[source]#
Bases:
JsonCodec
A class implementing an interface for objects that can be decoded from disk and encoded back to disk.
- class vcorelib.dict.codec.JsonCodec(schemas: SchemaMap, valid_attr: str = 'data')[source]#
Bases:
ABC
,SchemaMixin
A simple JSON codec interface.
- abstract asdict() Dict[str, str | int | float | bool | None | Dict[str, str | int | float | bool | None] | List[str | int | float | bool | None]] [source]#
Obtain a dictionary representing this instance.
- abstract classmethod create(data: Dict[str, str | int | float | bool | None | Dict[str, str | int | float | bool | None] | List[str | int | float | bool | None]] = None, schemas: SchemaMap = None, dest_attr: str = 'data', verify: bool = True) T [source]#
A factory method to create an instance.
- classmethod decode(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>, schemas: ~vcorelib.schemas.base.SchemaMap = None, dest_attr: str = 'data', verify: bool = True, require_success: bool = True, default_data: ~typing.Dict[str, str | int | float | bool | None | ~typing.Dict[str, str | int | float | bool | None] | ~typing.List[str | int | float | bool | None]] = None, **kwargs) T [source]#
Decode an object instance from data loaded from a file.
- encode(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>, **kwargs) Tuple[bool, int] [source]#
Encode this object instance to a file.
- classmethod file_cache(pathlike: ~pathlib.Path | str | None, arbiter: ~vcorelib.io.arbiter.DataArbiter = <vcorelib.io.arbiter.DataArbiter object>, encode_kwargs: ~typing.Dict[str, ~typing.Any] = None, require_success: bool = False, **kwargs) Iterator[T] [source]#
Manage an instance of this class as a disk-backed file.
vcorelib.dict.config module#
A module for implementing a configuration data manager with a dictionary.
- class vcorelib.dict.config.Config(dict=None, /, **kwargs)[source]#
Bases:
UserDict
,MutableMapping
[str
,Any
]A dictionary that allows access to data only once via each key.
- static from_directory(pathlike: Path | str | None, *args, **kwargs) Config [source]#
Load a configuration from a directory on disk.
- static from_file(pathlike: Path | str | None, *args, **kwargs) Config [source]#
Load a configuration from a file on disk.
- static from_path(pathlike: Path | str | None, *args, **kwargs) Config [source]#
Load a configuration from an arbitrary path.
vcorelib.dict.env module#
Utilities for working with environment-variable data contained inside various data structures.
- vcorelib.dict.env.dict_resolve_env_vars(data: Dict[Any, Any], keys: bool = True, values: bool = True, lists: bool = True, env: Mapping[str, str] = None) Dict[Any, Any] [source]#
Recursively resolve dictionary data that may contain strings that should be treated as environment-variable substitutions. The data is updated in-place.
- vcorelib.dict.env.list_resolve_env_vars(data: List[Any], keys: bool = True, values: bool = True, lists: bool = True, env: Mapping[str, str] = None) List[Any] [source]#
Recursively resolve list data that may contain strings that should be treated as environment-variable substitutions. The data is updated in-place.
Module contents#
Dictionary manipulation utilities.
- class vcorelib.dict.MergeStrategy(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
Enum
An enumeration describing strategies for combining various data structures.
- RECURSIVE = 1#
- UPDATE = 2#
- vcorelib.dict.consume(data: Dict[Any, Any], key: Any, default: Any = None) Any [source]#
Attempt to obtain dictionary data via key, removing the data if it was present.
- vcorelib.dict.limited(data: Dict[Any, Any], key: Any, value: Any = None) Iterator[None] [source]#
Ensure that dictionary data is only temporarily added.
- vcorelib.dict.merge(dict_a: Dict[Any, Any], dict_b: Dict[Any, Any], path: List[str] = None, expect_overwrite: bool = False, logger: Logger | LoggerAdapter[Any] = None, strategy: MergeStrategy = MergeStrategy.RECURSIVE) Dict[Any, Any] [source]#
Combine two dictionaries based on a provided merge strategy.
- vcorelib.dict.merge_dicts(dicts: List[Dict[Any, Any]], expect_overwrite: bool = False, logger: Logger | LoggerAdapter[Any] = None, strategy: MergeStrategy = MergeStrategy.RECURSIVE) Dict[Any, Any] [source]#
Merge a list of dictionary data into a single set (mutates the first element).
- vcorelib.dict.merge_recursive(dict_a: Dict[Any, Any], dict_b: Dict[Any, Any], path: List[str] = None, expect_overwrite: bool = False, logger: Logger | LoggerAdapter[Any] = None, strategy: MergeStrategy = MergeStrategy.RECURSIVE) Dict[Any, Any] [source]#
Combine two dictionaries recursively, prefers dict_a in a conflict. For values of the same key that are lists, the lists are combined. Otherwise the resulting dictionary is cleanly merged.