Welcome to AnyField’s documentation!

Note, this module is in experimental stage

This module provides SField class which is ised to avaoid lambdas there where function of one argument is required to be applied to multiple items Examples of such cases could be functions like: - sorted - filter - map - etc

Also this module provides shortcuts (already built SField instances), that could be starting point of SField expressions. They are: SF, F Both are same.

For example:

import requests
from anyfield import F, SView
data = requests.get('https://api.github.com/repos/vmg/redcarpet/issues?state=closed')
data = data.json()
view = SView(F['id'],
             F['state'],
             F['user']['login'],
             F['title'][:40],
)
for row in view(data):
    print(row)

Will result in:

[121393880, u'closed', u'fusion809', u'Rendering of markdown in HTML tags']
[120824892, u'closed', u'nitoyon', u'Fix bufprintf for Windows MinGW-w64']
[118147051, u'closed', u'clemensg', u'Fix header anchor normalization']
[115033701, u'closed', u'mitchelltd', u'Unicode headers produce invalid anchors']
[113887752, u'closed', u'Stemby', u'Definition lists']
[113740700, u'closed', u'Stemby', u'Multiline tables']
[112952970, u'closed', u'im-kulikov', u"recipe for target 'redcarpet.so' failed"]
[112494169, u'closed', u'mstahl', u'Unable to compile native extensions in O']
[111961692, u'closed', u'reiz', u'Adding dependency badge to README']
[111582314, u'closed', u'jamesaduke', u'Pre tags on code are not added when you ']
[108204636, u'closed', u'shaneog', u'Push 3.3.3 to Rubygems']

Function Reference

anyfield.toFn(fn)[source]

Simple wrapper to adapt SField instances to callables, that usualy used in .filter(), .sort() and other methods.

If some part of Your code may accept SField instances or callable of one arg as parametrs, use this function to adapt argument for example:

>>> def my_super_filter_func(my_sequence, filter_fn):
...    filter_fn = toFn(filter_fn)
...    return list(filter(filter_fn, my_sequence))
>>> my_super_filter_func(range(15), F % 2 == 0)
[0, 2, 4, 6, 8, 10, 12, 14]

This little line of code makes your function be able to use SField instances as filter_fn filter functions.

Parameters:fn – callable or SField instance
Return type:callable
Returns:if fn is instance of SField, then it’s method .__claculate__ will be returned, otherwise ‘fn’ will be returned unchanged
anyfield.toSField(field)[source]

Reverse of toFn. if field is not SField instance, attempts to convert it to SField

Returns:field converted to SField instance

Class Reference

class anyfield.SField(name=None, dummy=False)[source]

Bases: object

Class that allows to build simple expressions. For example, instead of writing something like:

>>> l = [{'a': -30, 'b': {'c': 5}, 'd': 4},
...      {'a': 2, 'b': {'c': 15}, 'd': 3}]
>>> l.sort(key=lambda x: x['a'] + x['b']['c'] - x['d'])
>>> [i['a'] for i in l]  # just print first el from dict
[-30, 2]

With this class it is possible to write folowing:

>>> from anyfield import SField
>>> l = [{'a': -30, 'b': {'c': 5}, 'd': 4},
...      {'a': 2, 'b': {'c': 15}, 'd': 3}]
>>> SF = SField(dummy=True)
>>> l.sort(key=(SF['a'] + SF['b']['c'] - SF['d'])._F)
>>> [i['a'] for i in l]  # just print first el from dict
[-30, 2]

Or using SF shortcut and F wrapper defined in this module:

>>> from anyfield import SField, F
>>> l = [{'a': -30, 'b': {'c': 5}, 'd': 4},
...      {'a': 2, 'b': {'c': 15}, 'd': 3}]
>>> l.sort(key=(F['a'] + F['b']['c'] - F['d'])._F)
>>> [i['a'] for i in l]  # just print first el from dict
[-30, 2]
Parameters:
  • name (str) – name of field
  • dummy (bool) – if set to True, on next operation new SField instance will be created
_F(record)[source]

Shortcut for __calculate__ method

If you need callable of one arg ot be passed for example to filter function Just finishe your expression with ._F and You will get it

_A(fn, *args, **kwargs)[source]

Shortcut for ‘__apply_fn__’ method.

__apply_fn__(fn, *args, **kwargs)[source]

Adds ability to apply specified function to record in expression

Parameters:fn (callable) – function to apply to expression result
Returns:SField instance

For example:

>>> data = ['10', '23', '1', '21', '53', '3', '4', '16']
>>> expr = SField().__apply_fn__(int)
>>> data.sort(key=expr._F)
>>> print (data)
['1', '3', '4', '10', '16', '21', '23', '53']
__calculate__(record)[source]

Do final calculation of this SField instances for specified record

q_contains

Check if record contains argument.Used instead arg in F expression

q_in

Check if argument contains recordUsed instead F in arg expression

class anyfield.SView(*fields)[source]

Bases: object

Just a simple view to work with SField.

This class allows to build table-like representation of data.

For example:

view = SView(F.name, F.user.name, F.user.name.startswith('~'))
data = requests.get('<data url>').json()
for name, username, umark in view(data):
    print name, username, umark
headers

List of field names

Shortcuts

anyfield.SF = <SField (dummy)>

Shortcut for SField(dummy=True). Can be used as starting point of SField expression.

anyfield.F = <SField (dummy)>

Shortcut for SField(dummy=True). Can be used as starting point of SField expression.