ipython / ipython/ipython

Show any same-line comment in %whos information after data

Open
#13,810 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
16.8k
Forks
4.5k
Avg merge
1d 2h
Merged PRs (30d)
6

Description

I do data science, and need to be precise. But it's a lot of 'write once' linear script code.

So, I make a concerted effort to comment on the same line as each assignment: typically I'll do this first. The line will have a short phrase saying what the thing is, and will include what units it is in.

Then, I'll typically just name the variable with a three or four letter acronynym formed from that phrase, and the units (slashes replaced with p for 'per'), with an underscore to separate them.

I know this is not 'pythonic', but I don't want long lines full_of_long_variable_names, the equations / calculations are long enough already.

We often end up needing to change units for the same variable, so it's easier and clearer to read that code this way, with relatively terse variables as mnemonics, but including the units.

If there's any confusion about what a variable is, jumping to where it was defined and reading the comment clears it right up.

The feature request is this: Could the output of `%whos` be made to include an optional column after the name of the varible, to be populated by whatever string is after a # on the same source code line?

This allows an arbitrary short string to serve as documentation for the variable - a good place to put additional details.

Also, if one accidentally overwrites a variable, that info will vanish from the table, which, unlike just a different value appearing, is likely to be noticed immediately. Putting it to the left of the data makes it easier to read, and is closer to the format of most spreadsheet 'engineering' calculations.

I recently translated one of those type 'quick and dirty' spreadsheet calcs to python code, and caught an error that was not a calculation error, but a thinko due to excel's somewhat opaque handling of certain formats.

My boss wrote the spreadsheet, and I couldn't see the error until I did this.

Boss is very business-minded, and an excel die-hard. I am certain I am not the only one with a boss like this.
He doesn't want to have to look at encoded python variable names, he just wants to see a line of explanatory text, and the numbers.

Now, I could use pandas to add that, but it doesn't really sell ipython as a reasonable way to work to him: he already sees python as an unnecessary overcomplicated waste of time and excel as a perfectly good, simple standard.

the `%whos` magic is good: but this feature I am requesting seems low-hanging fruit towards making it a lot better.

These sorts of important (yet often hurried) engineering calculations are always just a long list of extremely simple calculations.

So it would also be good if `%whos` listed variables in the order they were defined, not just alphabetically.

Every engineer is going to want to see what the numbers are doing going down the page of such calculations: We grow a certain familarity or instinct for what range each number should fall into, and looking is a good cross-check that nothing's hilariously off.

But this is a little harder to do if constrained to python's varible_naming_conventions.

Also, for such a 'quick' calc, I don't want to have to think hard about naming these references, yet the mnemonic naming convention above that I use does impose an additional level of interpretation to translate back into what the thing was.

I know that, using a setup having proper completion (like ipython, but also jupyter, or vim with ycm) that long_variable_names are not really a problem:

However, it *looks* like it is. And certainly so to the boss who thinks immediately that it is, and that more typing will be involved.

(For truely quick and dirty calcs, I use `_` in jupyter / ipython a lot... which I am sure would please boss to learn of... if he hadn't already totally discarded the notion of even taking a look).

So it seems valuable to keep both a short python variable codename, as well as a 'pretty' name, easily available for displays such as `%whos`, and having that just be a line-comment on the same line as the defining `=` for variables seems very pythonic to me. It's just unfortunate that using `?` or even `??` with a variable fails to show such definitions. However, having `%whos` do so would be far better anyway.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.