Package: contextfun
ContextfunThecontextfunpython package provides functions for context-based
filtering and mapping.We can filter a list with the Python's ownfilter()function, based on the properties of items.
For example, we can filter out prime numbers (filter(is_prime, range(10))).
But what if we are interested in numbers standing in the list before prime
numbers?
Or numbers that have at least two prime numbers in their three radius context?Similarly, we can replace items in a list, based on their properties with
Python's ownmap()function. For example, we can replace prime numbers with zeros
(map(lambda x: 0 if is_prime(x) else x, range(10))).
But what if we want replace numbers standing in the list after prime numbers?
Or numbers that have at most three prime numbers in their context?This package can help to resolve this problem with theircontextual_filter()andcontextual_map()functions.1 InstallYou can installcontextfuneasily from PyPI:pipinstallcontextfun2 Usage2.1 Filteringcontextual_filter(iterable, predicate, before=0, after=0, quantifier=all)Withcontextual_filterwe can filter items of iterables based on their
context. The function returns a generator of the filtered items.iterable:
The iterable to filter.predicate:
A function which can be applied for items of the context. Its input is an
item of theiterable, its output is Boolean
(predicate(obj: object) -> bool).The context can be defined with the parametersbeforeandafter.
The context of an item consists of thebeforeitems standing in the
iterable before it, and theafteritems following it.
The current item itself is never a part of its context.
The context is truncated at the beginning and the end of the iterable.
For example, the -1, +2 context (before=1, after=2) of the items in the
list[1, 2, 3, 4, 5]are1: (2, 3)
2: (1, 3, 4)
3: (2, 4, 5)
4: (3, 5)
5: (4)quantifier:
This parameter allows you to specify the part of the context for which thepredicateshould be true.
Default is Python's ownallfunction.
In this case, thepredicateshould be true for all items of the context.
You can also choose theanyfunction from the
built-in functions.
According to this, thepredicatemust be true for at least one item of the
context.You can also write your own custom function with the following restrictions:
(1) The only parameter of the function is the(predicate(x) for x in context)generator, and
(2) the return value should be Boolean.
For example, the 'up to two' can be represented aslambda context: sum((1 for x in context if x)) <= 2.Remark: The default behavior (quantifier=all) may seem strange for empty
contexts, e.g.>>>foriincontextual_filter([1],lambdax:x%5==0,after=2):...print(i)1This is not a bug but follows the principles of predicate logic
(seevacuous truth).2.2 Mappingcontextual_map(iterable, mapping, predicate, before=0, after=0, quantifier=all)Withcontextual_mapyou can map an iterable based on the context of its
items. This function returns a generator of the mapped iterable.The parameters are the same as that ofcontextual_filter()except themappingparameter. It should be callable and will
be applied for the items of the iterable if its context fulfils the conditions
represented bypredicateandquantifier.2.3 ExamplesLook for words in a text after 'the':>>>fromcontextfunimportcontextual_filter>>>text='''Alright but apart from the sanitation the medicine education... wine public order irrigation roads the fresh-water system and public... health what have the Romans ever done for us?'''.split()>>>pred=lambdaword:word=='the'>>>forwordincontextual_filter(text,pred,before=1,quantifier=any):...print(word)sanitationmedicinefresh-waterRomansHighlight the words after the word 'the' in a text:>>>fromcontextfunimportcontextual_map>>>highlighter=lambdaword:f'<b>{word}</b>'>>>words=contextual_map(text,highlighter,pred,before=1,quantifier=any)>>>' '.join(words)'Alright but apart from the <b>sanitation</b> the <b>medicine</b> education wine public order irrigation roads the <b>fresh-water</b> system and public health what have the <b>Romans</b> ever done for us?'3 Development setupRequirements:bashmakepython3.7pipenvInstall other development dependencies:makedevRun automated test-suite:maketest4 Release history0.5.0Work in progress0.6.0initial release0.6.1updated readme0.7.0performance improvement5 Acknowledgments@dlazesz@esztersimon@sassbalint