|
| 1 | +from collections.abc import MutableMapping |
| 2 | +from typing import Iterable, Iterator, TypeVar |
| 3 | + |
| 4 | +from .value_list import AttributeValueList |
| 5 | + |
| 6 | +__all__ = ("AttributeMap",) |
| 7 | + |
| 8 | + |
| 9 | +T = TypeVar("T") |
| 10 | +C = TypeVar("C", bound="AttributeMap") |
| 11 | + |
| 12 | + |
| 13 | +class AttributeMap(MutableMapping[str, AttributeValueList[T]]): |
| 14 | + """Helper object that provides a mutable mapping-like interface to access |
| 15 | + the vertex or edge attributes of a graph. |
| 16 | +
|
| 17 | + Enforces that values assigned to keys in the map are of type |
| 18 | + AttributeValueList_ and that they have the right length. |
| 19 | + """ |
| 20 | + |
| 21 | + _items: dict[str, AttributeValueList[T]] |
| 22 | + _common_length_of_values: int = 0 |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def wrap_empty_dict(cls, length: int = 0): |
| 26 | + return cls({}, length) |
| 27 | + |
| 28 | + def __init__(self, items: dict[str, AttributeValueList[T]], length: int) -> None: |
| 29 | + self._common_length_of_values = length |
| 30 | + self._items = items |
| 31 | + |
| 32 | + assert all(v.fixed_length and len(v) == length for v in items.values()) |
| 33 | + |
| 34 | + def copy(self: C) -> C: |
| 35 | + """Returns a shallow copy of the attribute map. |
| 36 | +
|
| 37 | + Note that unlike with regular dictionaries, this function makes a |
| 38 | + shallow copy of the _values_ as well. |
| 39 | + """ |
| 40 | + return self.__class__( |
| 41 | + {k: v.copy() for k, v in self._items.items()}, |
| 42 | + self._common_length_of_values, |
| 43 | + ) |
| 44 | + |
| 45 | + def copy_empty(self: C) -> C: |
| 46 | + """Returns another, empty attribute map with the same expected length |
| 47 | + for any items being assigned in the future. |
| 48 | + """ |
| 49 | + return self.__class__.wrap_empty_dict(self._common_length_of_values) |
| 50 | + |
| 51 | + def remove(self, key: str) -> None: |
| 52 | + del self._items[key] |
| 53 | + |
| 54 | + def set(self, key: str, value: T | Iterable[T]) -> None: |
| 55 | + """Assigns a value to _all_ the attribute values corresponding to the |
| 56 | + given attribute. |
| 57 | +
|
| 58 | + This function is also available as the ``__setitem__`` magic method, |
| 59 | + making it possible to use the class as if it was a dictionary. |
| 60 | +
|
| 61 | + Args: |
| 62 | + key: the name of the attribute to set |
| 63 | + value: the new value of the attribute. When it is an iterable |
| 64 | + that is not a string or a bytes object, it is assumed to |
| 65 | + contain the values for all items (vertices or edges) |
| 66 | + individually, and its length will be checked against the |
| 67 | + number of vertices or edges. WHen it is not an iterable, or |
| 68 | + it is a string or bytes object, it is assumed to be a common |
| 69 | + value for all vertices or edges. |
| 70 | + """ |
| 71 | + length = self._common_length_of_values |
| 72 | + |
| 73 | + if isinstance(value, (bytes, str)): |
| 74 | + # strings and bytes are iterable but they are treated as if not |
| 75 | + avl = AttributeValueList( |
| 76 | + [value] * length, fixed_length=True |
| 77 | + ) # type: ignore |
| 78 | + elif isinstance(value, Iterable): |
| 79 | + # iterables are mapped to an AttributeValueList. Note that this |
| 80 | + # also takes care of copying existing AttributeValueList instances |
| 81 | + avl = AttributeValueList(value, fixed_length=True) # type: ignore |
| 82 | + if len(avl) != length: |
| 83 | + raise RuntimeError( |
| 84 | + f"attribute value list length must be {length}, got {len(avl)}" |
| 85 | + ) |
| 86 | + else: |
| 87 | + # all other values are assumed to be a common value for all |
| 88 | + # vertices or edges |
| 89 | + avl = AttributeValueList( |
| 90 | + [value] * length, fixed_length=True |
| 91 | + ) # type: ignore |
| 92 | + |
| 93 | + assert avl.fixed_length and len(avl) == length |
| 94 | + self._items[key] = avl |
| 95 | + |
| 96 | + def _extend_common_length(self, n: int) -> None: |
| 97 | + self._common_length_of_values += n |
| 98 | + for value_list in self._items.values(): |
| 99 | + value_list._extend_length(n) |
| 100 | + |
| 101 | + def __getitem__(self, key: str) -> AttributeValueList[T]: |
| 102 | + return self._items[key] |
| 103 | + |
| 104 | + def __iter__(self) -> Iterator[str]: |
| 105 | + return self._items.__iter__() |
| 106 | + |
| 107 | + def __len__(self) -> int: |
| 108 | + return len(self._items) |
| 109 | + |
| 110 | + __delitem__ = remove |
| 111 | + __setitem__ = set # type: ignore |
0 commit comments