netlist_carpentry.core.netlist_elements.mixins.metadata
¶
Mixin for metadata, generally used by netlist elements.
Classes:
-
MetadataMixin–A mixin class that provides a structured way to manage metadata categorized by keys.
MetadataMixin
¶
Bases: METADATA_DICT
A mixin class that provides a structured way to manage metadata categorized by keys.
This class extends a dictionary structure where each key maps to another dictionary of metadata entries. It ensures that values added to categories are dictionaries, and provides convenient methods for adding, setting, and retrieving metadata.
Methods:
-
__setitem__–Set an item in the metadata dictionary.
-
__getattr__–Retrieve a category from the metadata by attribute access.
-
__get_pydantic_core_schema__–Generate a Pydantic core schema for this class.
-
has_category–Check if a category exists in the metadata.
-
add_category–Add a new category to the metadata if it does not already exist.
-
add–Add a new key-value pair to a specified category.
-
set–Set a key-value pair in a specified category.
-
get–Retrieve a value from a specified category by key.
Attributes:
__setitem__
¶
Set an item in the metadata dictionary.
Parameters:
-
(key¶str) –The category name to set.
-
(value¶NESTED_DICT) –The value to assign. Must be a dictionary.
Raises:
-
ValueError–If the value is not a dictionary.
__getattr__
¶
Retrieve a category from the metadata by attribute access.
In particular, if the metadata object has a category "some_cat", it can be accessed
directly via metadata["some_cat"] but also via metadata.some_cat.
Calling metadata.nonexisting_category will raise an AttributeError.
Parameters:
Returns:
-
Dict[str, NESTED_DICT]–Dict[str, NESTED_DICT]: The dictionary of metadata for the specified category.
Raises:
-
AttributeError–If the category does not exist.
__get_pydantic_core_schema__
classmethod
¶
__get_pydantic_core_schema__(
source_type: object, handler: GetCoreSchemaHandler
) -> CoreSchema
Generate a Pydantic core schema for this class.
This is required if the class using this mixin extends pydantic.BaseModel.
Parameters:
-
(source_type¶object) –The source type.
-
(handler¶GetCoreSchemaHandler) –The handler to use for generating the schema.
Returns:
-
CoreSchema(CoreSchema) –The generated schema.
has_category
¶
add_category
¶
add
¶
Add a new key-value pair to a specified category.
If the category does not exist, it is created.
Parameters:
-
(key¶str) –The key to add.
-
(value¶NESTED_DICT) –The value to assign to the key.
-
(category¶str, default:'general') –The category in which to store the key-value pair. Defaults to 'general', which means that if no category is specified, the key-value pair will be added to the 'general' category.
Returns:
-
bool(bool) –True if the key was added, False if it already existed.
set
¶
Set a key-value pair in a specified category.
If the category does not exist, it is created. Any previously assigned key-value pairs are overwritten.
Parameters:
get
¶
Retrieve a value from a specified category by key.
MetaDataMixin.get('key', category='cat') is equivalent to MetaDataMixin.cat.get('key'),
(given that the category cat exists).
Also, MetaDataMixin.get('key', 'default', category='cat') is equivalent to MetaDataMixin.cat.get('key', 'default'),
(given that the category cat exists).
Parameters:
-
(key¶str) –The key to retrieve.
-
(default¶Optional[NESTED_DICT], default:None) –The default value if the key is not found. Defaults to None.
-
(category¶str, default:'general') –The category from which to retrieve the key. Defaults to 'general'.
Returns:
-
NESTED_DICT(NESTED_DICT) –The value associated with the key, or the default if not found.