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:

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.

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.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

funcmap-1.0.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file funcmap-1.0.3.tar.gz.

File metadata

  • Download URL: funcmap-1.0.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for funcmap-1.0.3.tar.gz
Algorithm Hash digest
SHA256 f6135fe1bb2d72ddfd2e03091d65a7ee49f0579a256a916b36d1b5971a621fe5
MD5 f8dfefb179b2873a3e0a43a04db7c00c
BLAKE2b-256 76edbd40fb98fb3a783b51d452cac3d1a1a84750d458351700846581fb0e561c

See more details on using hashes here.

File details

Details for the file funcmap-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for funcmap-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5677024c8c518faf72da7dee7f2950d5225d1bed02bc42a923ed0ba1695fee59
MD5 afb9fc272ff5a805c5bd3e7030977e38
BLAKE2b-256 ac6427a005b945de0961927ec4d62af7e0d5a6dac7438de9a3114ee9e280d95b

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