From 27bf980036f72a7ed00227c057279aa4728bf915 Mon Sep 17 00:00:00 2001 From: Ahmad Faizal B H Date: Sat, 1 Aug 2020 17:12:41 +0530 Subject: [PATCH] fix register_call function --- README.md | 20 +++++++++++++++++++- chatbot/__init__.py | 8 ++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a12c1a5d..417fcef8 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ In think mode ``` ## Get matched group +for grouping in regex refer [Python regular expression docs](https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups) #### Get Nth matched group of client pattern ``` %N @@ -110,8 +111,16 @@ Example to get first matched ``` %1 ``` +#### Get matching named group of client pattern +``` +%Client_pattern_group_name +``` +Example to get matching named group `person` +``` +%person +``` -#### Get Nth matched group of bots pattern +#### Get Nth matched group of bots message pattern ``` %!N ``` @@ -120,6 +129,15 @@ Example to get first matched %!1 ``` +#### Get matching named group of bots message pattern +``` +%!Bot_pattern_group_name +``` +Example to get matching named group `region` +``` +%!region +``` + ## Recursion Get response as if client said this new statement ``` diff --git a/chatbot/__init__.py b/chatbot/__init__.py index 18f6c6dc..4ab8a699 100644 --- a/chatbot/__init__.py +++ b/chatbot/__init__.py @@ -44,7 +44,7 @@ def call(self, string, session_id): _function_call = MultiFunctionCall() -def register_call(function_name): +def register_call(function_name=None): def wrap(function): if type(function).__name__ != 'function': raise TypeError("function expected found %s" % type(function).__name__) @@ -54,10 +54,14 @@ def wrap(function): mapper[name] = function return function + if function_name is None: + return register_call if type(function_name).__name__ in ('unicode', 'str'): name = function_name return wrap - name = function.__name__ + if type(function_name).__name__ != 'function': + raise TypeError("String is expected for function name found %s" % type(function).__name__) + name = function_name.__name__ return wrap(function_name)