Skip to main content

context based filtering and mapping

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextfun-0.7.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

contextfun-0.7.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file contextfun-0.7.0.tar.gz.

File metadata

  • Download URL: contextfun-0.7.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for contextfun-0.7.0.tar.gz
Algorithm Hash digest
SHA256 b3ef455362fb567b977bc976bb7a22f28b2c34736fa6f4c005c31c736c2ac132
MD5 54ef8bb25bd666f152b4f25fd7127c87
BLAKE2b-256 5fa3ac13d99b85ec3dcda15a4c967bd66c1ae4d85bf90c651fd025dfc279680b

See more details on using hashes here.

File details

Details for the file contextfun-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: contextfun-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for contextfun-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e87362d849de51ed6b7653491bd1e19e1c88fe1ba46a24565235dcaa9e62034a
MD5 7a8105d06f37c89d67137e4c7694d216
BLAKE2b-256 f5cd2e1236bc3233f9d1148abba60c4a4057ff049a7ac9160dd6409fdf7fdba1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page