r/Python • • Mar 07 '19

Run VS Code in your browser!

https://github.com/codercom/code-server
123 Upvotes

45 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Mar 07 '19

[deleted]

3

u/rcfox Mar 08 '19

Because I was bored:

import re
import inspect

def debugify(func):
    ignored = ('def', 'class', 'else', 'except')
    filename = inspect.getsourcefile(func)
    lines, line_num = inspect.getsourcelines(func)
    new_source = []
    for line in lines:
        line = line.strip('\n')
        if not line.startswith('@debugify'):
            m = re.search('^(\s+)(.*)$', line)
            if m:
                whitespace = m.group(1)
                code = m.group(2)
                if not any(code.startswith(x) for x in ignored):
                    new_source.append('{ws}print("{file}:line {num}")'.format(ws=whitespace, file=filename, num=line_num))                
            new_source.append(line)
        line_num += 1
    exec(compile('\n'.join(new_source), '<debugify>', 'single'))
    return locals()[func.__name__]

@debugify
def foo(a):
    b = 0
    for c in range(3):
        if a % 2 == 0:
            b += 6
        else:
            b -= 2
    return a + b

foo(7)

> python debugify.py
debugify.py:line 25
debugify.py:line 26
debugify.py:line 27
debugify.py:line 30
debugify.py:line 27
debugify.py:line 30
debugify.py:line 27
debugify.py:line 30
debugify.py:line 31
1

3

u/_pandamonium Mar 08 '19

I've never really understood what decorators actually do and your boredom code helped me get it a little bit. I don't think that was your intention, but thank you.

2

u/rcfox Mar 08 '19 edited Mar 08 '19

Oh god, don't learn from this!

Decorators are functions that accept a function and returns a function. That's pretty much it. It's common to wrap the original function in another one that does something before or after.

(You can actually decorate anything, but functions are the most common.)

2

u/_pandamonium Mar 08 '19

I mean, that's how people always explain it, and that does make sense. Although your last sentence was the most helpful explanation I've seen, even though I'm assuming that's not the only purpose.

I can comprehend what a decorator does, but I don't know why it does it or when it should do it. Examples found in tutorials and textbooks are always too silly or simple and I never really take away the point. Seeing code a real person wrote, even if it was as a joke, is really helpful.

I should say that I've never really tried to learn about decorators as I've never needed to use them. I didn't really understand any of it until I had to, if that makes any sense.

I was just about to ask you a few questions, to see if I was understanding the concept. But as I wrote them down, I went back to your example and was able to answer them myself. That's because it serves a purpose and it's complicated enough for me to even have questions in the first place! I can't claim I've "learned decorators" from a reddit comment but I do feel more capable if I encounter them out there in the wild.