Skip to content

NamedTuple

NamedTuple - Imitates the behavior of the named tuple.

Class NamedTuple contains the following methods:

  • get - Return the value for key if key is in the dictionary, else None.
  • update - Update a value of key.
  • to_dict - Convert to the dictionary.
  • items - Returns a generator of list of NamedTuple elements grouped into tuples.
  • keys - Get a generator of list of keys.
  • values - Get a generator of list of values.
  • has_key - Returns True if the key exists, otherwise False.
  • has_value - Returns True if the value exists, otherwise False.

NamedTuple

This class imitates the behavior of the named tuple.

Source code in src/xloft/types/named_tuple.py
class NamedTuple:
    """This class imitates the behavior of the `named tuple`."""

    def __init__(self, **kwargs: dict[str, Any]) -> None:  # noqa: D107
        self.__dict__["_store"] = copy.deepcopy(kwargs)

    def __repr__(self) -> str:
        """Called by the repr built-in function.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> repr(nt)
            'NamedTuple(x=10, y="Hello")'

        Returns:
            Returns raw data used for internal representation in python.
        """
        args = []
        for key, val in self.__dict__["_store"].items():
            if isinstance(val, str):
                args.append(f'{key}="{val}"')  # pyrefly: ignore[bad-argument-type]
            else:
                args.append(f"{key}={val}")  # pyrefly: ignore[bad-argument-type]

        return f"NamedTuple({', '.join(args)})"

    def __str__(self) -> str:
        """Get a string representation of NamedTuple.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> str(nt)
            ''

        Returns:
            String representation of NamedTuple.
        """
        return str(self.__dict__["_store"])

    def __bool__(self) -> bool:
        """Called when checking for truth.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> bool(nt)
            True

        Returns:
            Boolean value.
        """
        return len(self.__dict__["_store"]) > 0

    def __len__(self) -> int:
        """Get the number of elements in the tuple.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> len(nt)
            2

        Returns:
            The number of elements in the tuple.
        """
        return len(self._store)

    def __getattr__(self, name: str) -> Any:
        """Getter.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt.x
            10

        Args:
            name (str): Key name.

        Returns:
            Value of key.
        """
        value = self._store[name]
        return copy.deepcopy(value)

    def __setattr__(self, name: str, value: Any) -> None:
        """Blocked Setter."""
        raise AttributeDoesNotSetValueError(name)

    def __delattr__(self, name: str) -> None:
        """Blocked Deleter."""
        raise AttributeCannotBeDeleteError(name)

    def __getitem__(self, key: str) -> Any:
        """Get value by [key_name].

        Args:
            key (str): Key name.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt["x"]
            10

        Returns:
            Return the value for key, else KeyError.
        """
        value = self._store[key]
        return copy.deepcopy(value)

    def get(self, key: str, default: Any = None) -> Any:
        """Return the value for key if key is in the NamedTuple, else default.

        Args:
            key (str): Key name.
            default (Any): Returns if the value is missing.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt.get("x")
            10

        Returns:
            Return the value for key if key is in the NamedTuple, else default.
        """
        try:
            value = self._store[key]
            return copy.deepcopy(value)
        except KeyError:
            return default

    def update(self, key: str, value: Any) -> None:
        """Update a value of key.

        Attention: This is an uncharacteristic action for the type `tuple`.

        Args:
            key (str): Key name.
            value (Any): Value of key.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt.update("x", 20)
            >>> nt.x
            20

        Returns:
            `None` or `KeyError` is missing.
        """
        keys: list[str] = self._store.keys()
        if key not in keys:
            err_msg = f"The key `{key}` is missing."
            raise KeyError(err_msg)
        self._store[key] = copy.deepcopy(value)

    def to_dict(self) -> dict[str, Any]:
        """Convert to the dictionary.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> d = nt.to_dict()
            >>> d["x"]
            10

        Returns:
            Dictionary with keys and values of the tuple.
        """
        return copy.deepcopy(self._store)

    def items(self) -> Any:
        """Returns a generator of list containing a tuple for each key-value pair.

        This is convenient for use in a `for` loop.
        If you need to get a list, do it list(instance.items()).

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> for key, val in nt.items():
            ...     print(f"Key: {key}, Value: {val}")
            "Key: x, Value: 10"
            "Key: y, Value: Hello"

        Returns:
            Returns a list containing a tuple for each key-value pair.
            Type: `list[tuple[str, Any]]`.
        """
        return ((key, copy.deepcopy(value)) for key, value in self._store.items())

    def keys(self) -> Any:
        """Get a generator of list of keys.

        If you need to get a list, do it list(instance.items()).

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> list(nt.keys())
            ["x", "y"]

        Returns:
            List of keys.
        """
        return self._store.keys()

    def values(self) -> Any:
        """Get a generator of list of values.

        If you need to get a list, do it list(instance.items()).

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> list(nt.values())
            [10, "Hello"]

        Returns:
            List of values.
        """
        return (copy.deepcopy(value) for value in self._store.values())

    def has_key(self, key: str) -> bool:
        """Check if the key exists.

        Args:
            key (str): Key name.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt.has_key("x")
            True

        Returns:
            True if the key exists, otherwise False.
        """
        key_list = self._store.keys()
        return key in key_list

    def has_value(self, value: Any) -> bool:
        """Check if the value exists.

        Args:
            value (Any): Value of key.

        Examples:
            >>> from xloft import NamedTuple
            >>> nt = NamedTuple(x=10, y="Hello")
            >>> nt.has_value(10)
            True

        Returns:
            True if the value exists, otherwise False.
        """
        value_list = self._store.values()
        return value in value_list

__bool__()

Called when checking for truth.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> bool(nt)
True

Returns:

Type Description
bool

Boolean value.

Source code in src/xloft/types/named_tuple.py
def __bool__(self) -> bool:
    """Called when checking for truth.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> bool(nt)
        True

    Returns:
        Boolean value.
    """
    return len(self.__dict__["_store"]) > 0

__delattr__(name)

Blocked Deleter.

Source code in src/xloft/types/named_tuple.py
def __delattr__(self, name: str) -> None:
    """Blocked Deleter."""
    raise AttributeCannotBeDeleteError(name)

__getattr__(name)

Getter.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.x
10

Parameters:

Name Type Description Default
name str

Key name.

required

Returns:

Type Description
Any

Value of key.

Source code in src/xloft/types/named_tuple.py
def __getattr__(self, name: str) -> Any:
    """Getter.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt.x
        10

    Args:
        name (str): Key name.

    Returns:
        Value of key.
    """
    value = self._store[name]
    return copy.deepcopy(value)

__getitem__(key)

Get value by [key_name].

Parameters:

Name Type Description Default
key str

Key name.

required

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt["x"]
10

Returns:

Type Description
Any

Return the value for key, else KeyError.

Source code in src/xloft/types/named_tuple.py
def __getitem__(self, key: str) -> Any:
    """Get value by [key_name].

    Args:
        key (str): Key name.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt["x"]
        10

    Returns:
        Return the value for key, else KeyError.
    """
    value = self._store[key]
    return copy.deepcopy(value)

__len__()

Get the number of elements in the tuple.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> len(nt)
2

Returns:

Type Description
int

The number of elements in the tuple.

Source code in src/xloft/types/named_tuple.py
def __len__(self) -> int:
    """Get the number of elements in the tuple.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> len(nt)
        2

    Returns:
        The number of elements in the tuple.
    """
    return len(self._store)

__repr__()

Called by the repr built-in function.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> repr(nt)
'NamedTuple(x=10, y="Hello")'

Returns:

Type Description
str

Returns raw data used for internal representation in python.

Source code in src/xloft/types/named_tuple.py
def __repr__(self) -> str:
    """Called by the repr built-in function.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> repr(nt)
        'NamedTuple(x=10, y="Hello")'

    Returns:
        Returns raw data used for internal representation in python.
    """
    args = []
    for key, val in self.__dict__["_store"].items():
        if isinstance(val, str):
            args.append(f'{key}="{val}"')  # pyrefly: ignore[bad-argument-type]
        else:
            args.append(f"{key}={val}")  # pyrefly: ignore[bad-argument-type]

    return f"NamedTuple({', '.join(args)})"

__setattr__(name, value)

Blocked Setter.

Source code in src/xloft/types/named_tuple.py
def __setattr__(self, name: str, value: Any) -> None:
    """Blocked Setter."""
    raise AttributeDoesNotSetValueError(name)

__str__()

Get a string representation of NamedTuple.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> str(nt)
''

Returns:

Type Description
str

String representation of NamedTuple.

Source code in src/xloft/types/named_tuple.py
def __str__(self) -> str:
    """Get a string representation of NamedTuple.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> str(nt)
        ''

    Returns:
        String representation of NamedTuple.
    """
    return str(self.__dict__["_store"])

get(key, default=None)

Return the value for key if key is in the NamedTuple, else default.

Parameters:

Name Type Description Default
key str

Key name.

required
default Any

Returns if the value is missing.

None

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.get("x")
10

Returns:

Type Description
Any

Return the value for key if key is in the NamedTuple, else default.

Source code in src/xloft/types/named_tuple.py
def get(self, key: str, default: Any = None) -> Any:
    """Return the value for key if key is in the NamedTuple, else default.

    Args:
        key (str): Key name.
        default (Any): Returns if the value is missing.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt.get("x")
        10

    Returns:
        Return the value for key if key is in the NamedTuple, else default.
    """
    try:
        value = self._store[key]
        return copy.deepcopy(value)
    except KeyError:
        return default

has_key(key)

Check if the key exists.

Parameters:

Name Type Description Default
key str

Key name.

required

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.has_key("x")
True

Returns:

Type Description
bool

True if the key exists, otherwise False.

Source code in src/xloft/types/named_tuple.py
def has_key(self, key: str) -> bool:
    """Check if the key exists.

    Args:
        key (str): Key name.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt.has_key("x")
        True

    Returns:
        True if the key exists, otherwise False.
    """
    key_list = self._store.keys()
    return key in key_list

has_value(value)

Check if the value exists.

Parameters:

Name Type Description Default
value Any

Value of key.

required

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.has_value(10)
True

Returns:

Type Description
bool

True if the value exists, otherwise False.

Source code in src/xloft/types/named_tuple.py
def has_value(self, value: Any) -> bool:
    """Check if the value exists.

    Args:
        value (Any): Value of key.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt.has_value(10)
        True

    Returns:
        True if the value exists, otherwise False.
    """
    value_list = self._store.values()
    return value in value_list

items()

Returns a generator of list containing a tuple for each key-value pair.

This is convenient for use in a for loop. If you need to get a list, do it list(instance.items()).

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> for key, val in nt.items():
...     print(f"Key: {key}, Value: {val}")
"Key: x, Value: 10"
"Key: y, Value: Hello"

Returns:

Name Type Description
Any

Returns a list containing a tuple for each key-value pair.

Type Any

list[tuple[str, Any]].

Source code in src/xloft/types/named_tuple.py
def items(self) -> Any:
    """Returns a generator of list containing a tuple for each key-value pair.

    This is convenient for use in a `for` loop.
    If you need to get a list, do it list(instance.items()).

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> for key, val in nt.items():
        ...     print(f"Key: {key}, Value: {val}")
        "Key: x, Value: 10"
        "Key: y, Value: Hello"

    Returns:
        Returns a list containing a tuple for each key-value pair.
        Type: `list[tuple[str, Any]]`.
    """
    return ((key, copy.deepcopy(value)) for key, value in self._store.items())

keys()

Get a generator of list of keys.

If you need to get a list, do it list(instance.items()).

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> list(nt.keys())
["x", "y"]

Returns:

Type Description
Any

List of keys.

Source code in src/xloft/types/named_tuple.py
def keys(self) -> Any:
    """Get a generator of list of keys.

    If you need to get a list, do it list(instance.items()).

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> list(nt.keys())
        ["x", "y"]

    Returns:
        List of keys.
    """
    return self._store.keys()

to_dict()

Convert to the dictionary.

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> d = nt.to_dict()
>>> d["x"]
10

Returns:

Type Description
dict[str, Any]

Dictionary with keys and values of the tuple.

Source code in src/xloft/types/named_tuple.py
def to_dict(self) -> dict[str, Any]:
    """Convert to the dictionary.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> d = nt.to_dict()
        >>> d["x"]
        10

    Returns:
        Dictionary with keys and values of the tuple.
    """
    return copy.deepcopy(self._store)

update(key, value)

Update a value of key.

Attention: This is an uncharacteristic action for the type tuple.

Parameters:

Name Type Description Default
key str

Key name.

required
value Any

Value of key.

required

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.update("x", 20)
>>> nt.x
20

Returns:

Type Description
None

None or KeyError is missing.

Source code in src/xloft/types/named_tuple.py
def update(self, key: str, value: Any) -> None:
    """Update a value of key.

    Attention: This is an uncharacteristic action for the type `tuple`.

    Args:
        key (str): Key name.
        value (Any): Value of key.

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> nt.update("x", 20)
        >>> nt.x
        20

    Returns:
        `None` or `KeyError` is missing.
    """
    keys: list[str] = self._store.keys()
    if key not in keys:
        err_msg = f"The key `{key}` is missing."
        raise KeyError(err_msg)
    self._store[key] = copy.deepcopy(value)

values()

Get a generator of list of values.

If you need to get a list, do it list(instance.items()).

Examples:

>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> list(nt.values())
[10, "Hello"]

Returns:

Type Description
Any

List of values.

Source code in src/xloft/types/named_tuple.py
def values(self) -> Any:
    """Get a generator of list of values.

    If you need to get a list, do it list(instance.items()).

    Examples:
        >>> from xloft import NamedTuple
        >>> nt = NamedTuple(x=10, y="Hello")
        >>> list(nt.values())
        [10, "Hello"]

    Returns:
        List of values.
    """
    return (copy.deepcopy(value) for value in self._store.values())