-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ic to be used as a decorator #32
Comments
Wonderful idea. The space for a decorator is vast, as the decorator can take all manner of A simple Do you have bandwidth to tackle this? If not, I'll get around to it, but it will |
I might do, although I haven't looked at the code and don't know how |
Decorators just takes a function and return another function. So class IceCreamDebugger:
def __call__(self, *args):
if args and callable(args[0]): # Decorator use detected.
# Do decorator magic here.
else:
# Normal behavior, like ic('foo'). |
But if I write |
Excellent catch @alexmojaki! Silly, trivial folly on my end. No morning ☕ The two viable approaches:
@ic(decorator=True)
def foo():
... But this approach is bad; magic values like
from icecream import icDecorator
@icDecorator
def foo():
... This is the most robust, independent approach. But it doesn't reuse |
|
I think |
It should probably have a more descriptive name like |
Yes, I didn't mean "decorate" should be the actual name, just a method on |
Has this issue been solved?Can I use ic as a decorator? |
Obviously this should be called ic.wrap ... |
Not |
I don't see why you would need a separate method to use ic as a decorator. |
I addressed this idea:
Decoration doesn't always use import inspect
from ycecream import y
class C:
# @y
def foo(self):
return 1
def bar(self):
return 2
for name, method in inspect.getmembers(C, inspect.isfunction):
decorated = y(method)
setattr(C, name, decorated)
C().foo() The expected output, which I get if I use
The actual output is just:
Even assuming just from ycecream import y
@y(
prefix="pre",
show_time=True,
show_exit=False
)
def add2(i):
return i + 2
add2(3) works in Python 3.8 and 3.9 but breaks in 3.7: pre#3 @ 19:15:09.745189
Traceback (most recent call last):
File "/home/alex/.config/JetBrains/PyCharm2020.3/scratches/scratch_1218.py", line 6, in <module>
show_exit=False
TypeError: 'NoneType' object is not callable
Because no one has asked for it? There's already plenty of libraries and snippets on StackOverflow and beyond that do this easily in a few lines of code. Sure it might be nice to have in icecream but it doesn't surprise me that there's no demand. If you think icecream should have it, make a PR. |
You are addressing some very edge case that ycecream doesn't address properly. If you would like to have that changed, please make it an issue on the ycecream GitHub page. With repect to PRs for IceCream: I have all the functionality that I need already in ycecrean, so I have absolutely no need for that in IceCream. But maybe your users have? |
I've implemented this in #73 with an upgrade to |
🎉!
does the new
@ic
def foo():
pass
@ic(timer=True)
def foo():
pass and/or 3. @ic.timer(utc=True)
def foo():
pass
@ic.trace
def foo():
pass if all three, we've got maximum surface area to decide the best API @ |
Yes, you can put any expression after |
I find that I want to inspect the arguments and return value of a function whenever it's called, but right now I have to go to every line that calls it and add
ic()
around the call. Moreover, that doesn't even work because it's a Flask function and something messes up.It would be much better if I could do something like:
And get the arguments and return value printed every time the function got called.
The text was updated successfully, but these errors were encountered: