Skip to main content

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

Project description

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:

@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))

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.

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

funcmap-1.0.1.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

funcmap-1.0.1-py3-none-any.whl (10.4 kB view hashes)

Uploaded Python 3

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