Created
January 3, 2019 22:15
-
-
Save pedro2555/dfa14b0d132d8255b6dd8418620310a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ptype(regex): | |
"""Searches a given regex parameterized query into a dict | |
>>> @search('(?P<letter>[A-Z])?') | |
... def getletter(string): | |
... return string['letter'] | |
... | |
>>> getletter('ABC') | |
('A', 'BC') | |
>>> getletter('0BC') | |
(None, '0BC') | |
""" | |
def decorator(parse_func): | |
"""Returns the search decorator""" | |
def func_wrapper(tail): | |
"""Returns the decorated search wrapper""" | |
match = re.search('^' + regex, tail.strip(), re.I | re.X) | |
if match is None: | |
return None, tail | |
# item = parse_func(match.groupdict()) | |
item = match.groupdict()['type'] | |
if item is not None: | |
tail = tail.strip()[match.end():] | |
return item, tail | |
return func_wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment