asq.selectors

Selector functions and selector function factories.

Selectors are so-called because they are used to select a value from an element. The selected value is often an attribute or sub-element but could be any computed value. The selectors module provides to standard selectors and also some selector factories.

Selectors

identity The identity function.
asq.selectors.identity(x)

The identity function.

The identity function returns its only argument.

Parameters:x – A value that will be returned.
Returns:The argument x.

Examples

Use the the identity function with the where() query operator, which has the effect that only elements which evaluate to True are present in the result:

>>> from selectors import identity
>>> a = [5, 3, 0, 1, 0, 4, 2, 0, 3]
>>> query(a).where(identity).to_list()
[5, 3, 1, 4, 2, 3]

Selector factories

a_ alias of attrgetter
k_ alias of itemgetter
m_ alias of methodcaller
asq.selectors.a_(name)

attrgetter(attr, ...) –> attrgetter object

Return a callable object that fetches the given attribute(s) from its operand. After f = attrgetter(‘name’), the call f(r) returns r.name. After g = attrgetter(‘name’, ‘date’), the call g(r) returns (r.name, r.date). After h = attrgetter(‘name.first’, ‘name.last’), the call h(r) returns (r.name.first, r.name.last).

Longhand equivalent

The selector factory call:

a_(name)

is equivalent to the longhand:

lambda element: element.name

Example

From a list of spaceship characteristics order the spaceships by length and select the spaceship name:

>>> from asq.selectors import a_
>>> class SpaceShip(object):
...     def __init__(self, name, length, crew):
...         self.name = name
...         self.length = length
...         self.crew = crew
...
>>> spaceships = [SpaceShip("Nebulon-B", 300, 854),
...               SpaceShip("V-19 Torrent", 6, 1),
...               SpaceShip("Venator", 1137, 7400),
...               SpaceShip("Lambda-class T-4a shuttle", 20, 6),
...               SpaceShip("GR-45 medium transport", 90, 6)]
>>> query(spaceships).order_by(a_('length')).select(a_('name')).to_list()
['V-19 Torrent', 'Lambda-class T-4a shuttle', 'GR-45 medium transport',
 'Nebulon-B', 'Venator']

or sort the

asq.selectors.k_(key)

itemgetter(item, ...) –> itemgetter object

Return a callable object that fetches the given item(s) from its operand. After f = itemgetter(2), the call f(r) returns r[2]. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])

Longhand equivalent

The selector factory call:

k_(key)

is equivalent to the longhand:

lambda element: element[name]

Example

From a list of dictionaries containing planetary data, sort the planets by increasing mass and select their distance from the sun:

>>> from asq.selectors import k_
>>> planets = [dict(name='Mercury', mass=0.055, period=88),
...            dict(name='Venus', mass=0.815, period=224.7),
...            dict(name='Earth', mass=1.0, period=365.3),
...            dict(name='Mars', mass=0.532, period=555.3),
...            dict(name='Jupiter', mass=317.8, period=4332),
...            dict(name='Saturn', mass=95.2, period=10761),
...            dict(name='Uranus', mass=14.6, period=30721),
...            dict(name='Neptune', mass=17.2, period=60201)]
>>> query(planets).order_by(k_('mass')).select(k_('period')).to_list()
[88, 555.3, 224.7, 365.3, 30721, 60201, 10761, 4332]
asq.selectors.m_(name, *args, **kwargs)

methodcaller(name, ...) –> methodcaller object

Return a callable object that calls the given method on its operand. After f = methodcaller(‘name’), the call f(r) returns r.name(). After g = methodcaller(‘name’, ‘date’, foo=1), the call g(r) returns r.name(‘date’, foo=1).

Longhand equivalent

The selector factory call:

m_(name, *args, **kwargs)

is equivalent to the longhand:

lambda element: getattr(element, name)(*args, **kwargs)

Example

From a list of SwimmingPool objects compute a list of swimming pool areas by selecting the area() method on each pool:

>>> class SwimmingPool(object):
...     def __init__(self, length, width):
...         self.length = length
...         self.width = width
...     def area(self):
...         return self.width * self.length
...     def volume(self, depth):
...         return self.area() * depth
...
>>> pools = [SwimmingPool(50, 25),
...          SwimmingPool(25, 12.5),
...          SwimmingPool(100, 25),
...          SwimmingPool(10, 10)]
>>> query(pools).select(m_('area')).to_list()
[1250, 312.5, 2500, 100]

Compute volumes of the above pools for a water depth of 2 metres by passing the depth as a positional argument to the m_() selector factory:

>>> query(pools).select(m_('volume', 2)).to_list()
[2500, 625.0, 5000, 200]

Alternatively, we can use a named parameter to make the code clearer:

>>> query(pools).select(m_('volume', depth=1.5)).to_list()
[1875.0, 468.75, 3750.0, 150.0]