Created
November 20, 2023 01:49
-
-
Save devnull255/8ff8e873e08f348dd63596445f7b0df4 to your computer and use it in GitHub Desktop.
Parse_keywords_decorator (py2)
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
from functools import wraps | |
def parse_keywords(func): | |
""" | |
decorates a python 2 function with the ability to parse list of key=value pair strings into a dict. | |
Dict is accessible through wrapper.arg_d attribute. | |
""" | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
wrapper.arg_d.update(dict([arg.split('=') for arg in args[0] if '=' in arg])) | |
result = func(*args, **kwargs) | |
return result | |
wrapper.arg_d = {} | |
return wrapper | |
@parse_keywords | |
def print_arg_d(args): | |
print(print_arg_d.arg_d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment