mindstalk: (Default)
[personal profile] mindstalk
Speaking of aggravating values of fun... I foresaw a day of boring editing in my future? Well, I managed to make that half a day. I'd looked for programmatic solutions but they all seemed half-assed in bad ways, especially in breaking the logging information (filename, function name) we want. So I went through by hand... and a good mastery of vim commands and maps. It's funny, I haven't had to make a new vim map in forever, but the skills are there.

Samples: (Edit: all the [] should be angle brackets; I forgot how Dreamwidth swallows them.)

map CP :s/,/+/g #turns commma to plus, down a line
map KE cwkinnlog.error([Esc]A)[ESC]CP
#changes word under cursor to 'kinnlog.error(', appends ')' to line, invokes CP
map KX cwkinnlog.exception([Esc]A)[ESC]CP
map fp /print^M # fast search for 'print', since CP changes the current search to ','
map St istr(^[ #inserts 'str(' and goes back to command more.
map stt a)^[ #inserts ')' and goes back to command more
map kld cf(kinnlog.debug(^[ #changes everything from cursor to a '('

I got to show off more simply last week, too; I was setting up the product for Co-worker, and had to edit the IP address in a bunch of files.
$ vi *.json
/[IP 1]
C[IP2]",
n #search next
. #ditto
:wn #write, move to next file -- which preserves all the state, so I can keep going 'n' and '.'

She was impressed by my speed. (If you're wondering why not regexes, there were only 1 or 2 addresses per file, this seemed as fast or faster as recalling a ':%s/...')

*******

All that said and done... Python makes it easy to write code. Python has an awesomely nice standard library, including a logger module with many handlers like RotatingFileHandler. Python makes it easy to rip out the guts of a stub logging module and replace it with something sophisticated.

Python 2 has a print statement which can take an indefinite list of things, and print them all, without any type information.

Python 2 does not provide any function that does something similar, so converting from those print statements to calls to actual logging functions was terribly painful and/or un-typesafe. I mean, a lot of the time I can't tell easily what the type of the value of a variable is where it's being used. Granted you can wrap almost anything in 'str()', and maybe I should have been more thorough about doing just that. Python doesn't have a compiler, and as long as it's basically syntactically correct, will be happy until it tries to actually execute the code. I really hate this.

Date: 2017-Apr-12, Wednesday 04:13 (UTC)
l33tminion: (Default)
From: [personal profile] l33tminion
Python 2 does not provide any function that does something similar

You can use the Python 3 style print function (instead of print statements) in Python 2.7 with:
from __future__ import print_function

Date: 2017-Apr-12, Wednesday 10:36 (UTC)
l33tminion: (Default)
From: [personal profile] l33tminion
It doesn't, was just a side-note on that one sentence about Python 2 not having a function that behaves like the print statement.

Though, now that I think about it, since 2to3 can replace print statements with the print function (e.g. 2to3 -w -n -f print *.py), one could do that followed by replacing "print(" with something else (presumably a wrapper for the logging function that calls str on its arguments).

Date: 2017-Apr-12, Wednesday 13:13 (UTC)
l33tminion: (Default)
From: [personal profile] l33tminion
Yeah. Sadly, '\t'.join(args) doesn't quite do that, you'd need '\t'.join(str(arg) for arg in args) (or '\t'.join(itertools.imap(str, args))).

Date: 2017-Apr-12, Wednesday 14:21 (UTC)
l33tminion: (Default)
From: [personal profile] l33tminion
Oh, right. I think I ran into that problem, too, the last time I tangled with that area of the code.

It would be great if the Python logging module provided a "ignore this function" decorator. Or at least an "ignore n extra functions" parameter. There doesn't seem to be any easy way to hide a wrapper function from logging currently, and the ways of working around that seem to be excessively hairy.

Page Summary

Most Popular Tags