Idea with decorators - well maybe
Please don't flame me about this post :) I have just learnt enough about decorators to be dangerous.
I have put a very silly example here of using a decorator to help to debug, but using dialogs. In this case a list dialog. It's super simple, it's more the idea of it. With introspection and more complex ui.View it seems like you could make some pretty cool debug tools rather than just printing to the console all the time.
Maybe I have had too many whiskeys again, however I must admit I haven't had that many yet :).
I would be interested just hearing people's comments, even the super bad ones :). But ok, it's food for thought.
import ui from functools import wraps import dialogs def debug_ui(func): @wraps(func) def wrapper(*args, **kwargs): lst = sorted([d for d in vars(func).keys()]) result = dialogs.list_dialog(title=func.__qualname__, items=lst) return func(*args, **kwargs) return wrapper @debug_ui def make_button(*args, **kwargs): pass @debug_ui class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.tb = None self.make_view() def make_view(self): make_button() pass def __repr__(self): return('Stupid Test Class') if __name__ == '__main__': f = (0, 0, 300, 400) v = MyClass(frame = f) v.present(style='sheet', animated=False)
Just when you thought it was safe :) I had another crazy idea. Using line annotations in the editor to drop in some info using a decorator. I couldn't get the code right, but it's not important. It's again just the idea. I have had quite a few more whiskeys, but I think this could be pretty cool or variations of it.
I have always though the annotations were powerful. Just needed a use case, this could be one. If you haven't used annotations from the editor module before, it's worth while running the script below. If you do run it, click the file title name also. You will see the annotations listed so you can jump to them and you can clear them all.
You can see in the script I was experimenting a little. But with enough time, I am sure could be very interesting
import ui, editor import inspect from functools import wraps #from inspect import formatargspec, getfullargspec def args_annotation_decorator(func): @wraps(func) def wrapper(*args, **kwargs): #ln = inspect.currentframe().f_back.f_lineno #ln = inspect.currentframe().f_lineno ''' i tried to get the right line number...it didnt work it out. i am sure its possible, i hard coded in just for the sake of showing the raw idea ''' editor.annotate_line(22, str(inspect.currentframe().f_locals)) return func(*args, **kwargs) return wrapper @args_annotation_decorator class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.make_view() def make_view(self): pass if __name__ == '__main__': f = (0, 0, 300, 400) v = MyClass(frame = f) v.present(style='sheet', animated=False)
@Phuket2 This is quite an interesting idea!
@omz , great. I hope it gives some people some fun ideas to do some stuff. I really like the annotation decorator idea. You could get some nice info, like passed args, timings etc... I am pretty sure ppl with exp with inspect would have no problem getting the right line number. But what makes it so nice is your implementation of the annotations. I don't feel like that it would feel like a kludge. Of course the other interesting things with the annotations is running scripts from the wrench over your code to mark items that make be of interest, same as you do with pep8 and pyflakes. What i also did not show in my example is using the style and expanded paramsof annotate_line. I am not sure if the filename param would be needed. If done as a decorator, it would seem not to me. But maybe some more complex decorators might need it.
Anyway, i got excited about it. Even the first post, about showing a ui.View during execution. Maybe a popover style would make more sense so you can position it.
If anyone could give me the line of code that would get the right line number in the annotation example, that would be great. With a few mods I would use that decorator. Shorter name for one. And maybe use formatargspec, getfullargspec to get more specific info.
@omz, while writing this I just went and tested and can see that happens when you add annotations to the same line number with a different style. I was able to get 3 annotations for a single line number by using 'warning', 'error', 'success'. If you are in that module doing work again, might be an idea to have a 'user' style. Maybe....
@Phuket2 At some point, I got similarly excited about highlighting the line that is currently being executed, while running a script, but that never really went anywhere...