Osmose

I make websites and chiptunes!

  • he/him

AKAs:
Lapsed Neurotypical
JavaScript's Strongest Warrior
Fake Podcast Host
Thotleader
Vertically Integrated Boyfriend
Your Fave's Mutual
Certified 5x Severals by the RIAA
Inconsistently Medicated
The Source That Views Back
Carnally Known
The Alternative


Homepage
osmose.ceo/

lexi
@lexi

javascript is one of the languages of all time


Osmose
@Osmose

Let me introduce you to the debugging library called q:

import q

foo = 7
q(foo) # Logs foo=7 to /tmp/q

What is the type of q? If you guessed "callable module", you'd be correct.

Callable modules are not a thing in Python.

By the way, you can also do q/expr or q|expr to log expr. You can also decorate a function with @q to log it's arguments, runtime, and return value. Also, q is able to log the name of the variable you pass as an argument.

I wrote a post a while ago detailing exactly how it works, but the tl;dr is:

  • q modifies the value in sys.modules['q'] to make the q module a value that isn't a module.
  • It uses sys._getframe to get a reference to the stack frame that called q().
  • It then parses the source code of the function that called it with the ast module to figure out whether it's being used as a decorator or function, and to extract the variable names of any arguments being passed.

You must log in to comment.

in reply to @lexi's post:

I'm pretty sure I've seen this being used as an actual feature in a COBOL interpreter in JS.
I think the reason might have been to attach the code (in comments) to functions, without doing any API calls.