negmas.generics¶
A set of generic classes and corresponding functions to iterate and index them.
The main goal of having this module is to allow for flexible modeling of collections with most python builtin
collections (e.g. list, tuple, generator) or even Callables without awkward isinstance calls in
other modules of the library.
- negmas.generics.IterableMapping[source]¶
Something that can be iterated upon with Key-value pairs (e.g. list, dict, tuple).
- negmas.generics.gget(x: Callable[[Any], Any] | Mapping | Sequence, _key: Any, default=None) Any[source]¶
Get an item from an IterableMapping
- Parameters:
x – the generic mapping
_key – key (must be immutable)
default – default value if no value attached with the key is found
Examples
Example with a list
>>> [gget([10, 20, 30], _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Example with a dictionary
>>> [gget({"a": 10, "b": 20, "c": 30}, _) for _ in ("a", "c", "b", -1, "d")] [10, 30, 20, None, None]
Example with a tuple
>>> [gget((10, 20, 30), _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Example with a generator
>>> [gget(range(10, 40, 10), _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Returns:
- negmas.generics.gmap(group: Callable[[Any], Any] | Mapping | Sequence, param: Any) Any[source]¶
Calls or indexes the group by the param
- Parameters:
group – Either a Callable or a Mapping
param – The parameters to use for mapping
Examples
>>> gmap([1, 23, 44], 1) 23 >>> gmap({"a": 3, "b": 5, "c": 4}, "c") 4 >>> gmap(lambda x: 3 * x, 20) 60
Returns:
- negmas.generics.ienumerate(x: Mapping | Sequence) Iterable[tuple[Any, Any]][source]¶
Enumerates a GenericMapping.
- Parameters:
x (IterableMapping) – A generic mapping (see
GenericMapping)
Examples
Example with a list
>>> for k, cutoff_utility in ienumerate([10, 20, 30]): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
Example with a dictionary
>>> for k, cutoff_utility in ienumerate({"a": 10, "b": 20, "c": 30}): ... print(k, cutoff_utility, end="-") a 10-b 20-c 30-
Example with a tuple
>>> for k, cutoff_utility in ienumerate((10, 20, 30)): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
Example with a generator
>>> for k, cutoff_utility in ienumerate(range(10, 40, 10)): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
- Returns:
a generator/iterator with tuples of key-value pairs.
- negmas.generics.iget(x: Mapping | Sequence, _key: Any, default=None) Any[source]¶
Get an item from an IterableMapping
- Parameters:
x – the generic mapping
_key – key (must be immutable)
default – default value if no value attached with the key is found
Examples
Example with a list
>>> [iget([10, 20, 30], _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Example with a dictionary
>>> [iget({"a": 10, "b": 20, "c": 30}, _) for _ in ("a", "c", "b", -1, "d")] [10, 30, 20, None, None]
Example with a tuple
>>> [iget((10, 20, 30), _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Example with a generator
>>> [iget(range(10, 40, 10), _) for _ in (0, 2, 1, -1, 4)] [10, 30, 20, 30, None]
Returns:
- negmas.generics.iitems(x: Mapping | Sequence) Iterable[tuple[Any, Any]][source]¶
Enumerates a GenericMapping.
- Parameters:
x (IterableMapping) – A generic mapping (see
GenericMapping)
Examples
Example with a list
>>> for k, cutoff_utility in ienumerate([10, 20, 30]): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
Example with a dictionary
>>> for k, cutoff_utility in ienumerate({"a": 10, "b": 20, "c": 30}): ... print(k, cutoff_utility, end="-") a 10-b 20-c 30-
Example with a tuple
>>> for k, cutoff_utility in ienumerate((10, 20, 30)): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
Example with a generator
>>> for k, cutoff_utility in ienumerate(range(10, 40, 10)): ... print(k, cutoff_utility, end="-") 0 10-1 20-2 30-
- Returns:
a generator/iterator with tuples of key-value pairs.
- negmas.generics.ikeys(x: Mapping | Sequence) Iterable[Any][source]¶
Returns all keys of the iterable.
- Parameters:
x (IterableMapping) – A generic mapping (see
GenericMapping)
Examples
Example with a list
>>> for k in ikeys([10, 20, 30]): ... print(k, end="-") 0-1-2-
Example with a dictionary
>>> for k in ikeys({"a": 10, "b": 20, "c": 30}): ... print(k, end="-") a-b-c-
Example with a tuple
>>> for k in ikeys((10, 20, 30)): ... print(k, end="-") 0-1-2-
Example with a generator
>>> for k in ikeys(range(10, 40, 10)): ... print(k, end="-") 0-1-2-
- Returns:
a generator/iterator with tuples of key-value pairs.
- negmas.generics.ivalues(x: Mapping | Sequence) Iterable[Any][source]¶
Returns all keys of the iterable.
- Parameters:
x (IterableMapping) – A generic mapping (see
GenericMapping)
Examples
Example with a list
>>> for k in ivalues([10, 20, 30]): ... print(k, end="-") 10-20-30-
Example with a dictionary
>>> for k in ivalues({"a": 10, "b": 20, "c": 30}): ... print(k, end="-") 10-20-30-
Example with a tuple
>>> for k in ivalues((10, 20, 30)): ... print(k, end="-") 10-20-30-
Example with a generator
>>> for k in ivalues(range(10, 40, 10)): ... print(k, end="-") 10-20-30-
- Returns:
a generator/iterator with tuples of key-value pairs.