-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.py
More file actions
36 lines (28 loc) · 1.34 KB
/
Copy pathdecorators.py
File metadata and controls
36 lines (28 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import copy
def obtuse_demutable(func):
"""
This decorator is used as a bugfix for functions with mutable values in their default values from the function definition
This will not save the initial values from the first time it is called, but it will set them to a blank list or dict
:param func:
:return:
"""
def wrapper(*args, **kwargs):
func.__defaults__ = tuple([[arg, type(arg)()][isinstance(arg, (list, dict))] for arg in func.__defaults__])
func(*args, **kwargs)
return wrapper
def demutable(func):
"""
This decorator is used as a bugfix for functions with mutable values in their default values from the function definition
This saves the initial values of the function instead of being obtuse and setting them to blank lists or dicts
:param func:
:return:
"""
def wrapper(*args, **kwargs):
# Save the initial state of the function's default arguments
default_storage, kwdefault_storage = copy.deepcopy(func.__defaults__), copy.deepcopy(func.__kwdefaults__)
# 'Run' the Function and store the results in a return value
ret_func = func(*args, **kwargs)
# Set the functions arguments back to their default state
func.__defaults__, func.__kwdefaults__ = default_storage, kwdefault_storage
return ret_func
return wrapper