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 requested object does not exist.

exception libvcs._internal.query_list.ObjectDoesNotExist[source]#

Bases: Exception

The query returned multiple objects when only one was expected.

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) –

exception libvcs._internal.query_list.OpNotFound(op, *args)[source]#

Bases: ValueError

Parameters:
class libvcs._internal.query_list.QueryList(iterable=(), /)[source]#

Bases: list[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")
[]
data: Sequence[TypeVar(T, Any, Any)]#
pk_key: Optional[str]#
items()[source]#
Return type:

list[TypeVar(T, Any, Any)]

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

QueryList[TypeVar(T, Any, Any)]

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

Optional[TypeVar(T, Any, Any)]

Parameters:
  • matcher (Callable[[T], bool] | T | None) –

  • default (Any | None) –

  • kwargs (Any) –