aft-may25-2023/util.py

70 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-05-24 19:05:16 -06:00
from functools import wraps
from AST import Set, atom
import more_itertools
2023-05-25 14:37:24 -06:00
2023-05-24 19:05:16 -06:00
def powerset(items):
return map(Set, more_itertools.powerset(items))
def format_set(s: Set[atom]):
return "{" + ", ".join(sorted(s)) + "}"
2023-05-25 14:37:24 -06:00
def textp(*args):
2023-05-24 19:05:16 -06:00
whole = "("
sets = (format_set(arg) for arg in args)
whole += ", ".join(sets)
whole += ")"
2023-05-25 14:37:24 -06:00
return whole
def printp(*args):
print(textp(*args))
2023-05-24 19:05:16 -06:00
def prints_input(*pos):
def wrappest(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
output = fn(*args, **kwargs)
print(
f"Input of {fn.__name__} pos: {pos}:",
" ".join(
(format_set(arg) if isinstance(arg, Set) else str(arg))
for i, arg in enumerate(args)
if i in pos
),
)
return output
return wrapper
return wrappest
def prints_output(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
output = fn(*args, **kwargs)
print(
f"Output of {fn.__name__}:",
format_set(output) if isinstance(output, Set) else output,
)
return output
return wrapper
def disabled_static(return_value):
"Disables a function by making it return a specified value instead"
def wrappest(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
return return_value
return wrapper
return wrappest