一个小的python模块,用于在python函数和文本输入之间提供方便的映射

funcmap的Python项目详细描述


Latest PyPI versionLatest Travis CI build status

一个小的python模块,用于在python函数和文本输入之间提供方便的映射。

用法

你知道Flask,对吧?你喜欢他们使用的装修路线,对吧?-这是完全相同的,但对于任何文本和 不是url和regex!

以下是使用方法:

首先需要创建funcmapper的实例:

fromfuncmapimportFuncMappermapper=FuncMapper()

现在您可以映射,映射,映射:

有两种方法支持将正则表达式映射到函数。 首先使用地图装饰符:

@mapper.map(r'call my_func')defmy_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+)')defadder(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))

…然后使用map作为函数:

defmy_func():"""We can map simple functions.

    Just pass a phrase to the map decorator.
    """return'I, my_func, have been called'defadder(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)

映射不够?-比画更多的地图!双倍地图时间!

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

不,我们有自尊的功能:

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

愚蠢的计算器:

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

这个精神分裂症患者:

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

有关更多指导,请查看examples文件夹和docstrings方法。

为什么我需要那个?

当然,因为它使用了一些很棒的python工具!-但是说真的,我们的想法是把它作为一个小的辅助框架 当需要一些简单的语言逻辑时。一个典型的例子是聊天机器人或某种人工智能助手。

我开发了这个小东西,因为我正计划建造一个非常愚蠢的语言快捷引擎 简单任务语音或短信/信息可控。我现在没有时间,但我想从某个地方开始。 如果你有时间,偷我的主意,让它,分享它,并成为可怕的!

它能做…?

可能还没有…还没有!目前我只实现了我所需要的最低限度。如果我能在这个图书馆做更多的工作 很可能会找到更多我需要的东西。在那之前,轮到你了!如果你缺少一个功能,请告诉我!我会的 尽我所能让它为你工作!

你在说什么正则表达式?

正则表达式(regex)是超级酷的语言处理魔法,如果你理解它们的话。我确实…有时候。如果你想的话 掌握正则表达式learn heretest your skills here

安装

pip install funcmap

或者如果你想开发一个很棒的新功能(是的,我知道你想!):

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

我需要什么?!(要求)

不,不,不,不,

但是,它有效吗?!(测试)

是的!希望如此。如果有疑问,请运行

python setup.py test

兼容性

该模块仅与Python3兼容。它使用不向后兼容的re.fullmatch。如果你真的 需要一个与Python2.7兼容的版本,让我知道。我想应该可以解决这个问题。

许可证

此软件包是根据麻省理工学院的许可证(版权所有(C)2017 Arne Küderle)

作者

funcmap是由Arne Küderle编写的。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
IntelliJ中的java默认Maven项目结构不一致   java我希望链接(在帖子和页面上)在一些访问者加载时被自动点击   java如何使用单独的方法隐藏JButton并在新类中调用   java KStream leftJoin KStream具有相同的密钥   java图像在垂直滚动窗格视图端口中消失   java从指定的起始点开始以n的增量填充数组   java JLabel和JTextField不在swing表单中应用   java springboot mongo如果没有映像,请使用现有映像   类似C++映射的java容器   java如何在没有Valgrind错误的情况下调用JNI_CreateJavaVM?   java如何在安卓中运行后台服务   java onPostExecute不运行