hundreds-breakfast-49010
11/07/2019, 9:38 PMhundreds-breakfast-49010
11/07/2019, 9:40 PMhundreds-breakfast-49010
11/07/2019, 9:40 PM@rule
and @console_rule
decorators to take an optional name argumenthundreds-breakfast-49010
11/07/2019, 9:40 PMhundreds-breakfast-49010
11/07/2019, 9:40 PMaloof-angle-91616
11/07/2019, 9:41 PMhundreds-father-404
11/07/2019, 9:41 PMaloof-angle-91616
11/07/2019, 9:41 PMhundreds-father-404
11/07/2019, 9:41 PMcreate_pex
. Or you want it in plain English like Create Pex
?aloof-angle-91616
11/07/2019, 9:42 PMhundreds-breakfast-49010
11/07/2019, 9:42 PMconsole_rule
decorator, then I think that blows away the original functionaloof-angle-91616
11/07/2019, 9:42 PMhundreds-breakfast-49010
11/07/2019, 9:42 PMhundreds-father-404
11/07/2019, 9:43 PMif I just pass an argument to thePython strongly recommends usingdecorator, then I think that blows away the original functionconsole_rule
functools.wraps
, in part for this reason. We don't use it muchhundreds-breakfast-49010
11/07/2019, 9:43 PMhundreds-breakfast-49010
11/07/2019, 9:44 PMhundreds-father-404
11/07/2019, 9:45 PMaloof-angle-91616
11/07/2019, 9:48 PMdmcclanahan@tw-mbp-dmcclanahan: ~/workspace/codesing 13:43:57 $? 2
X> python
Python 3.7.4 (default, Oct 12 2019, 18:55:28)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def decorator_maybe_args(*args, **kwargs):
... if not kwargs and len(args) == 1 and hasattr(args[0], '__call__'):
... print('called without any args!!!')
... return args[0]
... else:
... print(f'called with args!! args={args}, kwargs={kwargs}')
... def inner_decorator(func):
... print('we are in the inner decorator now!!!')
... return func
... return inner_decorator
...
>>> @decorator_maybe_args
... def f():
... return 3
...
called without any args!!!
>>> f()
3
>>> @decorator_maybe_args('asdf', some_arg='wow')
... def g():
... return 4
...
called with args!! args=('asdf',), kwargs={'some_arg': 'wow'}
we are in the inner decorator now!!!
>>> g()
4
>>>
hundreds-father-404
11/07/2019, 9:48 PMdef logged(func=None, *, level=logging.DEBUG):
if func is None:
return functools.partial(logged, level=level)
@wraps(func)
def wrapper(*args, **kwargs):
log.log(level, "test")
return func(*args, **kwargs)
return wrapper
aloof-angle-91616
11/07/2019, 9:51 PMaloof-angle-91616
11/07/2019, 9:51 PMaloof-angle-91616
11/07/2019, 9:51 PMaloof-angle-91616
11/07/2019, 9:51 PMhundreds-father-404
11/07/2019, 9:51 PMhundreds-breakfast-49010
11/07/2019, 9:52 PMhundreds-breakfast-49010
11/07/2019, 9:52 PMhundreds-father-404
11/07/2019, 9:53 PMearly-needle-54791
11/07/2019, 10:40 PMaloof-angle-91616
11/08/2019, 2:51 AMaloof-angle-91616
11/08/2019, 2:52 AMaloof-angle-91616
11/08/2019, 2:53 AM*args
and **kwargs
captures after those thoughaloof-angle-91616
11/08/2019, 2:53 AMaloof-angle-91616
11/08/2019, 2:53 AM