-
Notifications
You must be signed in to change notification settings - Fork 240
Python Dos and Don'ts
The logger's debug()
, info()
, warn()
, etc. methods are all variadic, and the extra arguments will be interpolated into the log message. IOW,
logger.info('Hello, %s' % world)
is equivalent to
logger.info('Hello, %s', world)
just less efficient if the log level set above info. String interpolation isn't exactly cheap so its desirable to only do it if the log message is actually delivered.
Document the non-obvious. Don't document the how, do document the why. Recently came across this:
# toil imports
from toil.job import Job
# toil scripts imports
from toil_scripts.batch_alignment.bwa_alignment import docker_call
#############################
# Look at me, look at me!!! #
#############################
When I say that writing good software is an art, I don't mean that.
… use single-quoted string literals by default. Use double-quoted string literals if the string value contains single quotes.
Python can infer a line continuation if it occurs within a balanced construct like parentheses, braces and brackets. I sometimes even introduce an otherwise redundant pair of parens if that lets me avoid backslashes. So don't
log( "Bla, bla, bla %s %s %s", foo, \
bar, \
ding )
but do
log( "Bla, bla, bla %s %s %s", foo,
bar,
ding )
And don't
from germany import bmw, \
mercedes, \
audi
but do
from germany import (bmw,
mercedes,
audi)
… don't
for k in d:
stuff(k, d[k])
but do
for k,v in k.iteritems():
stuff(k,v)
I think it is faster.
… unless you intend to call join()
on that queue. Note that threading.Queue.join()
is different from threading.Thread.join()
.
Use threading.Event only if you need to efficiently block on the event to occur. Otherwise use a simple bool variable. The GIL takes care of the rest.