2Miscellaneous decorators that can be useful
5from .debugging
import get_metadata, called_as_decorator, print_debug_count, get_context, print_context
6from .misc
import interpret_percentage
7from warnings
import warn
15 """ This is a joke/pet peeve of mine for people who say they're 100% confident in something """
18def todo(feature:str=
None, enabled:bool=
True, blocking:bool=
False, limit:bool=
True):
20 """ Leave reminders for yourself to finish parts of your code.
21 Can also be used as a decorator (
for functions
or classes) to
print a reminder
and optionally
22 throw a NotImplemented error on being called/constructed.
24 enabled: enable
or disable printed warnings
25 blocking:
raise an error, instead of just warning you
26 limit:
if called multiple times,
and this
is true, only
print once
32 metadata = get_metadata(2)
33 situation = called_as_decorator(
'todo', metadata)
35 uniqueID = (metadata.lineno, metadata.filename)
36 if limit
and uniqueID
in _todoCalls:
39 _todoCalls.add(uniqueID)
41 def printTodo(disableFunc):
42 if not hide_todo
and enabled:
44 print(get_context(metadata, path=
True, func=
False if disableFunc
else None), end=
'', style=color)
46 print(f
'TODO: {feature.__name__ if disableFunc else feature}')
48 raise NotImplementedError(feature)
51 if situation
in (1, 3):
53 def innerWrap(*funcArgs, **funcKwArgs):
56 raise NotImplementedError(feature)
57 return feature(*funcArgs, **funcKwArgs)
63 def raiseErr(*_, **kw_):
64 raise NotImplementedError(feature)
67 feature.__init__ = raiseErr
72def confidence(level:float):
76 level = interpret_percentage(level) * 100
79 def innerWrap(*funcArgs, **funcKwArgs):
83 raise TypeError(f
"You can't be {level}% confident, that's not how it works.")
88 raise UserWarning(f
"{func.__name__} will almost certainly going to fail. ({level}% confidence)")
91 print_context(3, func=
False, color=
'warn')
92 print(f
"Warning: {func.__name__} will probably fail. ({level}% confidence)", style=
'warn')
96 print(f
"Warning: {func.__name__} might not work. ({level}% confidence)", style=
'confidence_warning')
98 return func(*funcArgs, **funcKwArgs)
102def depricated(why=''):
107 def innerWrap(*funcArgs, **funcKwArgs):
109 print_context(2, color=
'warn')
110 warn(f
"{func.__name__} is Depricated{': ' if len(why) else '.'}{why}")
112 return func(*funcArgs, **funcKwArgs)
116def reprise(obj: type, *args, **kwargs) -> type:
118 A class decorator that sets the __repr__ member to the __str__ member.
119 Not super useful, but nice
for quick custom classes.
121 obj.__repr__ = obj.__str__
125confident = confidence
126untested = confidence(51)
127tested = confidence(95)
This is a joke/pet peeve of mine for people who say they're 100% confident in something.
type reprise(type obj, *args, **kwargs)
A class decorator that sets the repr member to the str member.
def todo(str feature=None, bool enabled=True, bool blocking=False, bool limit=True)
Leave reminders for yourself to finish parts of your code.