List querying - libvcs._internal.query_list
ΒΆ
Utilities for filtering or searching list
of objects / list data.
Note
This is an internal API not covered by versioning policy.
- exception libvcs._internal.query_list.MultipleObjectsReturned[source]ΒΆ
Bases:
Exception
The query returned multiple objects when only one was expected.
- exception libvcs._internal.query_list.ObjectDoesNotExist[source]ΒΆ
Bases:
Exception
The requested object does not exist.
- libvcs._internal.query_list.keygetter(obj, path)[source]ΒΆ
Fetch values in objects and keys, supported nested data.
With dictionaries:
- Return type:
- Parameters:
>>> keygetter({ "food": { "breakfast": "cereal" } }, "food") {'breakfast': 'cereal'}
>>> keygetter({ "food": { "breakfast": "cereal" } }, "food__breakfast") 'cereal'
With objects:
>>> from typing import Optional >>> from dataclasses import dataclass, field
>>> @dataclass() ... class Food: ... fruit: list[str] = field(default_factory=list) ... breakfast: Optional[str] = None
>>> @dataclass() ... class Restaurant: ... place: str ... city: str ... state: str ... food: Food = field(default_factory=Food)
>>> restaurant = Restaurant( ... place="Largo", ... city="Tampa", ... state="Florida", ... food=Food( ... fruit=["banana", "orange"], breakfast="cereal" ... ) ... )
>>> restaurant Restaurant(place='Largo', city='Tampa', state='Florida', food=Food(fruit=['banana', 'orange'], breakfast='cereal'))
>>> keygetter(restaurant, "food") Food(fruit=['banana', 'orange'], breakfast='cereal')
>>> keygetter(restaurant, "food__breakfast") 'cereal'
- libvcs._internal.query_list.parse_lookup(obj, path, lookup)[source]ΒΆ
Check if field lookup key, e.g. βmy__path__containsβ has comparator, return val.
If comparator not used or value not found, return None.
>>> parse_lookup({ "food": "red apple" }, "food__istartswith", "__istartswith") 'red apple'
It can also look up objects:
>>> from dataclasses import dataclass
>>> @dataclass() ... class Inventory: ... food: str
>>> item = Inventory(food="red apple")
>>> item Inventory(food='red apple')
>>> parse_lookup(item, "food__istartswith", "__istartswith") 'red apple'
- class libvcs._internal.query_list.LookupProtocol(*args, **kwargs)[source]ΒΆ
Bases:
Protocol
Protocol for
QueryList
filtering operators.- _abc_impl = <_abc._abc_data object>ΒΆ
- _is_protocol = TrueΒΆ
- exception libvcs._internal.query_list.PKRequiredException(*args)[source]ΒΆ
Bases:
Exception
- Parameters:
args (object)
- Return type:
None
- exception libvcs._internal.query_list.OpNotFound(op, *args)[source]ΒΆ
Bases:
ValueError
- class libvcs._internal.query_list.QueryList(items=None)[source]ΒΆ
-
Filter list of object/dictionaries. For small, local datasets.
Experimental, unstable.
With dictionaries:
>>> query = QueryList( ... [ ... { ... "place": "Largo", ... "city": "Tampa", ... "state": "Florida", ... "foods": {"fruit": ["banana", "orange"], "breakfast": "cereal"}, ... }, ... { ... "place": "Chicago suburbs", ... "city": "Elmhurst", ... "state": "Illinois", ... "foods": {"fruit": ["apple", "cantelope"], "breakfast": "waffles"}, ... }, ... ] ... )
>>> query.filter(place="Chicago suburbs")[0]['city'] 'Elmhurst' >>> query.filter(place__icontains="chicago")[0]['city'] 'Elmhurst' >>> query.filter(foods__breakfast="waffles")[0]['city'] 'Elmhurst' >>> query.filter(foods__fruit__in="cantelope")[0]['city'] 'Elmhurst' >>> query.filter(foods__fruit__in="orange")[0]['city'] 'Tampa'
>>> query.filter(foods__fruit__in="apple") [{'place': 'Chicago suburbs', 'city': 'Elmhurst', 'state': 'Illinois', 'foods': {'fruit': ['apple', 'cantelope'], 'breakfast': 'waffles'}}]
>>> query.filter(foods__fruit__in="non_existent") []
With objects:
>>> from typing import Any >>> from dataclasses import dataclass, field
>>> @dataclass() ... class Restaurant: ... place: str ... city: str ... state: str ... foods: dict[str, Any]
>>> restaurant = Restaurant( ... place="Largo", ... city="Tampa", ... state="Florida", ... foods={ ... "fruit": ["banana", "orange"], "breakfast": "cereal" ... } ... )
>>> restaurant Restaurant(place='Largo', city='Tampa', state='Florida', foods={'fruit': ['banana', 'orange'], 'breakfast': 'cereal'})
>>> query = QueryList([restaurant])
>>> query.filter(foods__fruit__in="banana") [Restaurant(place='Largo', city='Tampa', state='Florida', foods={'fruit': ['banana', 'orange'], 'breakfast': 'cereal'})]
>>> query.filter(foods__fruit__in="banana")[0].city 'Tampa'
>>> query.get(foods__fruit__in="banana").city 'Tampa'
With objects (nested):
>>> from typing import Optional >>> from dataclasses import dataclass, field
>>> @dataclass() ... class Food: ... fruit: list[str] = field(default_factory=list) ... breakfast: Optional[str] = None
>>> @dataclass() ... class Restaurant: ... place: str ... city: str ... state: str ... food: Food = field(default_factory=Food)
>>> query = QueryList([ ... Restaurant( ... place="Largo", ... city="Tampa", ... state="Florida", ... food=Food( ... fruit=["banana", "orange"], breakfast="cereal" ... ) ... ), ... Restaurant( ... place="Chicago suburbs", ... city="Elmhurst", ... state="Illinois", ... food=Food( ... fruit=["apple", "cantelope"], breakfast="waffles" ... ) ... ) ... ])
>>> query.filter(food__fruit__in="banana") [Restaurant(place='Largo', city='Tampa', state='Florida', food=Food(fruit=['banana', 'orange'], breakfast='cereal'))]
>>> query.filter(food__fruit__in="banana")[0].city 'Tampa'
>>> query.get(food__fruit__in="banana").city 'Tampa'
>>> query.filter(food__breakfast="waffles") [Restaurant(place='Chicago suburbs', city='Elmhurst', state='Illinois', food=Food(fruit=['apple', 'cantelope'], breakfast='waffles'))]
>>> query.filter(food__breakfast="waffles")[0].city 'Elmhurst'
>>> query.filter(food__breakfast="non_existent") []
- Parameters:
items (Iterable[T] | None)