Skip to main content
Latest PyPI version Latest Travis CI build status

A small Python module to provide convenient mapping between Python functions and text input.

Usage

You know Flask, right? You love the decorator-routing they use, right? - This is exactly the same, but for any text and not URLs and with REGEX!

Here is how you use it:

First you need to create an instance of the FuncMapper:

from funcmap import FuncMapper

mapper = FuncMapper()

And now you map, map, map:

There are two ways supported to map a regex to a function. First using the map decorator:

@mapper.map(r'call my_func')
def my_func():
    """We can map simple functions.

    Just pass a phrase to the map decorator.
    """
    return 'I, my_func, have been called'


@mapper.map(r'(?P<first>\d+)\+(?P<second>\d+)')
def adder(first, second):
    """We can map functions with arguments.

    All named capture groups (this is a capture group named first: (?P<first>\d+)) are passed as keyword-arguments to
    function. So make sure the names of your capture groups fit the names of your function arguments.
    Unnamed capture groups as positional arguments are NOT supported to avoid some edge-cases!
    """
    return '{} + {} = {}'.format(first, second, int(first) + int(second))

… and second using map as a function:

def my_func():
    """We can map simple functions.

    Just pass a phrase to the map decorator.
    """
    return 'I, my_func, have been called'


def adder(first, second):
    """We can map functions with arguments.

    All named capture groups (this is a capture group named first: (?P<first>\d+)) are passed as keyword-arguments to
    function. So make sure the names of your capture groups fit the names of your function arguments.
    Unnamed capture groups as positional arguments are NOT supported to avoid some edge-cases!
    """
    return '{} + {} = {}'.format(first, second, int(first) + int(second))

mapper.map(r'call my_func', my_func)
mapper.map(r'(?P<first>\d+)\+(?P<second>\d+)', adder)

Not enough mapping? - Than map some more! Double-Map-Time!

@mapper.map(r'I am Fred')
@mapper.map(r'No, I am Joe')
def multi_name():
    """We can map multiple Regex-expressions to the same function."""
    return 'I have multiple names!'

And no we have a function with some self esteem:

>>> print(mapper('call my_func'))
'I, my_func, have been called'

A silly calculator:

>>> print(mapper('3+5'))
'3 + 5 = 8'
>>> print(mapper('10+2'))
'10 + 2 = 12'

And this schizophrenic guy:

>>> print('I am Fred')
'I have multiple names!'
>>> print('No, I am Joe')
'I have multiple names!'

For more guidance check the examples folder and the method docstrings.

Why the heck do I need that?

Because it uses some awesome Python stuff, of course! - But seriously, the idea is to use it as tiny helper-framework when some simple language logic is needed. A prime example would be something like a chatbot or some kind of AI-Assistant.

I developed this little thing, because I am planning on building like a very dumb language-shortcut engine to make some simple tasks voice or SMS/Message controllable. I currently don’t have time for that, but I wanted to start somewhere. If you have the time, STEAL MY IDEA, MAKE IT, SHARE IT, AND BE AWESOME!

Can it do …?

Probably not… yet! Currently I only implemented the bare minimum I needed. If I will do more work with this library I will very likely find more stuff I need. Till then, it is your turn! If you are missing a feature let me know! I will try my best to make it work for you!

What is this RegEx you are talking about?

Regular Expressions (RegEx) are super cool language-processing-magic, if one understands them. I do… sometimes. If you want to master the RegEx learn here and test your skills here.

Installation

pip install funcmap

or if you want to develop an awesome new feature (yes, I know you want to!):

git clone https://github.com/AKuederle/funcmap.git
cd funcmap
pip install -e .

What do I need?! (Requirements)

Na Na Na Nothing

But, does it work?! (Tests)

YES! I hope. If in doubt, run

python setup.py test

Compatibility

The module is compatible with Python 3 only. It uses re.fullmatch, which is not backwards compatible. If you really need a version that is compatible with Python 2.7, let me know. I guess it should be possible to fix that.

Licence

This package is licenced under a MIT licence (Copyright (c) 2017 Arne Küderle)

Authors

funcmap was written by Arne Küderle.

Release files for funcmap 1.0.3

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

Source distribution (sdist)

Source distribution for funcmap 1.0.3
File Size Uploaded
funcmap-1.0.3.tar.gz 7.0 kB Details

Built distribution (wheel)

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

Total release size: 19.0 kB

Release files / funcmap-1.0.3.tar.gz

Download URL funcmap-1.0.3.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f6135fe1bb2d72ddfd2e03091d65a7ee49f0579a256a916b36d1b5971a621fe5
BLAKE2b-256 checksum
How to use checksums
76edbd40fb98fb3a783b51d452cac3d1a1a84750d458351700846581fb0e561c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / funcmap-1.0.3-py3-none-any.whl

Download URL funcmap-1.0.3-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5677024c8c518faf72da7dee7f2950d5225d1bed02bc42a923ed0ba1695fee59
BLAKE2b-256 checksum
How to use checksums
ac6427a005b945de0961927ec4d62af7e0d5a6dac7438de9a3114ee9e280d95b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.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