Skip to main content

Contextfun

license pypi

The contextfun python package provides functions for context-based filtering and mapping.

We can filter a list with the Python's own filter() 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 own map() 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 their contextual_filter() and contextual_map() functions.

1 Install

You can install contextfun easily from PyPI:

pip install contextfun

2 Usage

2.1 Filtering

contextual_filter(iterable, predicate, before=0, after=0, quantifier=all)

With contextual_filter we 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 the iterable, its output is Boolean (predicate(obj: object) -> bool).

The context can be defined with the parameters before and after. The context of an item consists of the before items standing in the iterable before it, and the after items 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] are

1: (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 the predicate should be true. Default is Python's own all function. In this case, the predicate should be true for all items of the context. You can also choose the any function from the built-in functions. According to this, the predicate must 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 as lambda context: sum((1 for x in context if x)) <= 2.

Remark: The default behavior (quantifier=all) may seem strange for empty contexts, e.g.

>>> for i in contextual_filter([1], lambda x: x%5==0, after=2):
...     print(i)
1

This is not a bug but follows the principles of predicate logic (see vacuous truth).

2.2 Mapping

contextual_map(iterable, mapping, predicate, before=0, after=0, quantifier=all)

With contextual_map you 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 of contextual_filter() except the mapping parameter. It should be callable and will be applied for the items of the iterable if its context fulfils the conditions represented by predicate and quantifier.

2.3 Examples

Look for words in a text after 'the':

>>> from contextfun import contextual_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 = lambda word: word == 'the'
>>> for word in contextual_filter(text, pred, before=1, quantifier=any):
...     print(word)
sanitation
medicine
fresh-water
Romans

Highlight the words after the word 'the' in a text:

>>> from contextfun import contextual_map
>>> highlighter = lambda word: 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 setup

Requirements:

Install other development dependencies:

make dev

Run automated test-suite:

make test

4 Release history

  • 0.5.0
    • Work in progress
  • 0.6.0
    • initial release
  • 0.6.1
    • updated readme
  • 0.7.0
    • performance improvement

5 Acknowledgments

Release files for contextfun 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextfun 0.7.0
File Size Uploaded
contextfun-0.7.0.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contextfun 0.7.0
File Interpreter ABI Platform
contextfun-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.5 kB

Release files / contextfun-0.7.0.tar.gz

Download URL contextfun-0.7.0.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b3ef455362fb567b977bc976bb7a22f28b2c34736fa6f4c005c31c736c2ac132
BLAKE2b-256 checksum
How to use checksums
5fa3ac13d99b85ec3dcda15a4c967bd66c1ae4d85bf90c651fd025dfc279680b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / contextfun-0.7.0-py3-none-any.whl

Download URL contextfun-0.7.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e87362d849de51ed6b7653491bd1e19e1c88fe1ba46a24565235dcaa9e62034a
BLAKE2b-256 checksum
How to use checksums
f5cd2e1236bc3233f9d1148abba60c4a4057ff049a7ac9160dd6409fdf7fdba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page