Skip to content

Instantly share code, notes, and snippets.

@devnull255
Created November 20, 2023 01:49
Show Gist options
  • Save devnull255/8ff8e873e08f348dd63596445f7b0df4 to your computer and use it in GitHub Desktop.
Save devnull255/8ff8e873e08f348dd63596445f7b0df4 to your computer and use it in GitHub Desktop.
Parse_keywords_decorator (py2)
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