Skip to main content

framework for in-line functional composition, iterable extensions similar to LINQ, and complex object evaluation

Project description

pip install auttcomp

Guide

Composition with |

g(f(x)) == (f | g)(x)

To achieve inline composition, functions must be wrapped with the Composable object (f)

from auttcomp.composable import Composable as f

square = f(lambda x: x ** 2)
add3 = f(lambda x: x + 3)

comp = square | add3
assert comp(3) == 12

Automatic wrapping

If the composition chain starts with a Composable, the rest of the chain is automatically wrapped

from auttcomp.composable import Composable as f

square = f(lambda x: x ** 2)
add3 = lambda x: x + 3

comp = square | add3 | (lambda x: x + 10)
assert comp(3) == 22

Partial Application with &

Consider python's map function - map(func, data)

In this example, & is used to partially apply the square func to map

from auttcomp.composable import Composable as f

square = lambda x: x ** 2
pmap = f(map) & square

assert list(pmap([1, 2, 3])) == [1, 4, 9]

Extensions Api primer: Identity function and invocation with >

The proceeding examples will import the extensions api as f. The api itself is composable, but also contains many extension methods which are commonly used on iterable data structures.

f.id is used to create a composable identity function. You will soon see that this will be the root of our composition pipeline. Conceptually we can think of this as SQL's "select * from table"

from auttcomp.extensions import Api as f
from auttcomp.testing.base_test import get_hugging_face_sample

data = get_hugging_face_sample()

id_func = f.id(data.models)
just_data_models_again = id_func()

The data in this sample is a search result from the Hugging Face api.

We'll explore the data with a query soon, but first we'd like to know about it's structure. It is difficult to understand the structure of the model just by looking at the raw data, so we'll use the f.shape function to help us understand it.

The f.shape function accepts any data as input, and prints a summary to the console.

When the invocation operator (>) is used, the identity function on the left is invoked first (returning data), and the data is passed as an argument to the next composable function (f.shape)

f.id(data.models) > f.shape

Result:

[ { 'author': 'str',
    'authorData': { '_id': 'str',
                    'avatarUrl': 'str',
                    'followerCount': 'int',
                    'fullname': 'str',
                    'isEnterprise': 'bool',
                    'isHf': 'bool',
                    'isMod': 'bool',
                    'isPro': 'bool',
                    'name': 'str',
                    'type': 'str'},
    'downloads': 'int',
    'gated': 'bool|str',
    'id': 'str',
    'inference': 'str',
    'isLikedByUser': 'bool',
    'lastModified': 'str',
    'likes': 'int',
    'pipeline_tag': 'str',
    'private': 'bool',
    'repoType': 'str',
    'widgetOutputUrls': ['str']}]

Extensions Api

Python already has many common higher order functions (map, filter, reduce, etc). Those functions, and others can be implemented as follows.

#list the author of each model
f.id(data.models) > f(map) & (lambda x: x.authorData) | f(map) & (lambda x: x.name) | list

However, for convenience, many common functions have been curried and attached to f. So the same query could also be described as...

f.id(data.models) > f.map(lambda x: x.authorData) | f.map(lambda x: x.name) | list

or even...

get_author_data = lambda x: x.authorData
get_name = lambda x: x.name
comp = f.map & get_author_data | f.map & get_name | list
f.id(data.models) > comp

Example query

Let's create a list which shows the authors with the most downloads.

f.shape will be used to show the result of the query at each stage.

First, group by author

f.id(data.models) > f.group(lambda x: x.author) | list | f.shape
[ { 'key': 'str',
    'value': [ { 'author': 'str',
                 'authorData': { '_id': 'str',
                                 'avatarUrl': 'str',
                                 'followerCount': 'int',
                                 'fullname': 'str',
                                 'isEnterprise': 'bool',
                                 'isHf': 'bool',
                                 'isMod': 'bool',
                                 'isPro': 'bool',
                                 'name': 'str',
                                 'type': 'str'},
                 'downloads': 'int',
                 'gated': 'bool|str',
                 'id': 'str',
                 'inference': 'str',
                 'isLikedByUser': 'bool',
                 'lastModified': 'str',
                 'likes': 'int',
                 'pipeline_tag': 'str',
                 'private': 'bool',
                 'repoType': 'str',
                 'widgetOutputUrls': ['str']}]}]

Next, map to tuple of (key, sum_downloads)

(
  f.id(data.models)
  > f.group(lambda x: x.author)
  | f.map(lambda g: (
      g.key,
      f.id(g.value) > f.map(lambda x: x.downloads) | sum
  ))
  | list 
  | f.shape
)
[('str', 'int')]

Finally, sort by descending downloads and take the top 5 results

(
  f.id(data.models)
  > f.group(lambda x: x.author)
  | f.map(lambda g: (
    g.key,
    f.id(g.value) > f.map(lambda x: x.downloads) | sum
  ))
  | f.sort_by_desc(lambda x: x[1])
  | f.take(5)
  | list
)
[('black-forest-labs', 1548084),
 ('deepseek-ai', 1448374),
 ('microsoft', 264891),
 ('unsloth', 142908),
 ('openbmb', 135782)]

Testing

pytest 7.4.3

Misc

developed on Python 3.12.8

No dependencies outside of core python

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

auttcomp-2.0.0.tar.gz (66.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auttcomp-2.0.0-py3-none-any.whl (71.8 kB view details)

Uploaded Python 3

File details

Details for the file auttcomp-2.0.0.tar.gz.

File metadata

  • Download URL: auttcomp-2.0.0.tar.gz
  • Upload date:
  • Size: 66.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for auttcomp-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3d7016ae9b0a107b0612812b861db77a614648f4505fc2b60f771e9cbd94e788
MD5 b16e9bda9696242bb668d8316376476a
BLAKE2b-256 ea0ce0e944a91f756bc4bad53ce6a0049d73205637520c5daca3a239e33bc74d

See more details on using hashes here.

File details

Details for the file auttcomp-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: auttcomp-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 71.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for auttcomp-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f9c5ee3bfa0e3f85793eb028d7b3ad9eeaeb089e167a0d356ea12285947036c
MD5 329edf6c35d90221901609ca26b74767
BLAKE2b-256 3dd862b6ed27b5326bd1eefad354e29cfa4b38c4437bc8426ef9a2381eca7fa8

See more details on using hashes here.

Supported by

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