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:

Union[None, Any, str, list[str], Mapping[str, str]]

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.

Return type:

Optional[Any]

Parameters:
>>> 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ΒΆ
libvcs._internal.query_list.lookup_exact(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_iexact(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_contains(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_icontains(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_startswith(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_istartswith(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_endswith(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_iendswith(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_in(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_nin(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_regex(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
libvcs._internal.query_list.lookup_iregex(data, rhs)[source]ΒΆ
Return type:

bool

Parameters:
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

Parameters:
Return type:

None

class libvcs._internal.query_list.QueryList(items=None)[source]ΒΆ

Bases: list[T], Generic[T]

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)

data: Sequence[TypeVar(T)]ΒΆ
pk_key: Optional[str]ΒΆ
items()[source]ΒΆ
Return type:

list[tuple[str, TypeVar(T)]]

filter(matcher=None, **kwargs)[source]ΒΆ
Return type:

QueryList[TypeVar(T)]

Parameters:
get(matcher=None, default=<object object>, **kwargs)[source]ΒΆ
Return type:

Optional[TypeVar(T)]

Parameters: