Skip to content

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:

  • general (Dict[str, NESTED_DICT]) –

    General-purpose metadata category.

general property

general: Dict[str, NESTED_DICT]

General-purpose metadata category.

__setitem__

__setitem__(key: str, value: NESTED_DICT) -> None

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__

__getattr__(name: str) -> Dict[str, NESTED_DICT]

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:

  • name

    (str) –

    The name of the category to retrieve.

Returns:

  • Dict[str, NESTED_DICT]

    Dict[str, NESTED_DICT]: The dictionary of metadata for the specified category.

Raises:

__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

has_category(category: str) -> bool

Check if a category exists in the metadata.

Parameters:

  • category

    (str) –

    The name of the category to check.

Returns:

  • bool ( bool ) –

    True if the category exists, False otherwise.

add_category

add_category(category: str) -> bool

Add a new category to the metadata if it does not already exist.

Parameters:

  • category

    (str) –

    The name of the category to add.

Returns:

  • bool ( bool ) –

    True if the category was added, False if it already existed.

add

add(key: str, value: NESTED_DICT, category: str = 'general') -> bool

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(key: str, value: NESTED_DICT, category: str = 'general') -> None

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:

  • key

    (str) –

    The key to set.

  • 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'.

get

get(
    key: str, default: Optional[NESTED_DICT] = None, category: str = "general"
) -> NESTED_DICT

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.