List querying - libtmux._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 libtmux._internal.query_list.MultipleObjectsReturned[source]

The query returned multiple objects when only one was expected.

exception libtmux._internal.query_list.ObjectDoesNotExist[source]

The requested object does not exist.

libtmux._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 List, 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'
libtmux._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'
exception libtmux._internal.query_list.PKRequiredException[source]
__init__(*args)[source]
Parameters:

args (object) –

Return type:

None

exception libtmux._internal.query_list.OpNotFound[source]
__init__(op, *args)[source]
Parameters:
Return type:

None

class libtmux._internal.query_list.QueryList[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, Dict
>>> 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 List, 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")
[]
__init__(items=None)[source]
Parameters:

items (Iterable[T] | None) –

Return type:

None

filter(matcher=None, **kwargs)[source]

Filter list of objects.

Return type:

QueryList[TypeVar(T)]

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

Retrieve one object.

Raises MultipleObjectsReturned if multiple objects found.

Raises ObjectDoesNotExist if no object found, unless default stated.

Return type:

Optional[TypeVar(T)]

Parameters: