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.
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]
a_ Create a selector function which selects an attribute by name. k_ Create a selector function which indexes into the element by key. m_ Create a selector function which calls a named method.
- asq.selectors.a_(name)¶
Create a selector function which selects an attribute by name.
Parameters: name – The name of the attribute which will be retrieved from each element. Returns: A unary selector function which retrieves the named attribute from its only argument and returns the value of that attribute. Longhand equivalent
The selector factory call:
a_(name)is equivalent to the longhand:
lambda element: element.nameExample
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)¶
Create a selector function which indexes into the element by key.
Parameters: key – The key which the generated selector will use to index into elements. Returns: A unary selector function which indexes into its only argument with the key value. 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)¶
Create a selector function which calls a named method.
Parameters:
- name – The name of the method which will be called on each element.
- *args – Any optional positional arguments which will be passed to the called method.
- **kwargs – Any optional named arguments which will be passed to the called method.
Returns: A unary selector function which calls the named method with any optional positional or named arguments and which returns the result of the method call.
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]