kappybara.utils#
Functions
|
Classes
|
|
|
A subclass of the built-in set, with support for indexing by arbitrary properties of set members, as well as integer indexing to allow for random sampling. |
|
|
|
This class should be initialized with a function or lambda which takes a set member as input, and returns a single object which is the corresponding property value of a set member. |
|
This class should be initialized with a function or lambda which takes a set member as input, and returns a collection or iterable of values associated with that member that you want to index by. |
- class kappybara.utils.IndexedSet(iterable=[])[source]#
A subclass of the built-in set, with support for indexing by arbitrary properties of set members, as well as integer indexing to allow for random sampling.
Credit https://stackoverflow.com/a/15993515 for the integer indexing logic.
If you know for some property that you should only get a single set member back when using lookup, mark that property as unique when you create it.
NOTE: although this class is indexable due to the implementation of __getitem__, member ordering is not stable across insertions and deletions.
Example usage: ``` @dataclass class SportsTeam:
name: str jersey_color: str members: list[str]
teams: IndexedSet[SportsTeam] = IndexedSet() teams.create_index(“name”, Property(lambda team: team.name, is_unique=True)) teams.create_index(“color”, Property(lambda team: team.jersey_color))
[…] # populate the set with teams
teams.lookup(“name”, “Manchester”) # Returns the team whose name is “Manchester” teams.lookup(“color”, “blue”) # Returns all teams with blue jerseys ```
- Parameters:
iterable (Iterable[T])
- add(item)[source]#
Add an element to a set.
This has no effect if the element is already present.
- Parameters:
item (T)
- create_index(name, prop)[source]#
Given a function which maps a set member to an Any-typed value, create a reverse-index mapping a property value to the set of the members of self with that property. This index is updated when adding new members or removing existing ones, but please note that if you mutate the internal state of an existing set member, this object will not reflect those updates unless you take the care to update the indices manually.
NOTE: mutating set members outside of interface calls can invalidate indices.
- Parameters:
name (str)
prop (SetProperty)
- indices: dict[str, defaultdict[Hashable, Self]]#
- properties: dict[str, SetProperty]#
- class kappybara.utils.Property(fn, is_unique=False)[source]#
This class should be initialized with a function or lambda which takes a set member as input, and returns a single object which is the corresponding property value of a set member.
An example of initializing a Property ``` @dataclass class Fruit:
color: str
my_property = Property(lambda fruit: fruit.color) # Equivalent using a lambda function ```
- Parameters:
fn (Callable[[T], Iterable[Hashable]])
- class kappybara.utils.SetProperty(fn, is_unique=False)[source]#
This class should be initialized with a function or lambda which takes a set member as input, and returns a collection or iterable of values associated with that member that you want to index by.
Example initialization: ``` @dataclass class SportsTeam:
name: str members: list[str]
members_alt = SetProperty(lambda team: team.members) # If someone can belong to multiple teams (default) ```
- Parameters:
fn (Callable[[T], Iterable[Hashable]])