Archive

Posts Tagged ‘python’

One Honking Great Idea

June 8th, 2010

I really enjoyed From Python Import Podcast’s two-parter on the Zen of Python, but I’ve been thinking that they may have missed something really interesting in the last item in the Zen:

Namespaces are one honking great idea — let’s do more of those!

For a long time, I had largely overlooked this one myself, because on its face it seems frivolous and perhaps too specific. Up to that point, the Zen is all broad strokes and elegant generalizations, and suddenly BAM! here comes this thing about namespaces that’s full of colloquialisms and exclamation points, and it feels very tacked on and strange and arbitrary, like a sort of gangly teenager trying to fit into a more mature crowd.

Naturally, the conversation in the podcast went straight down the nerd rabbit hole to talk about the meaning of namespaces, and completely neglected the real meat here, which is a pity because this might actually be one of the most important aspects of the Zen.

This is an expression of the enthusiasm and joy that pervade the Python community.

This is Python’s “Get Excited and Make Things.” This is a formal declaration that it’s all right to get excited about stuff that we make. This is a mission statement to go forth and create new delights, so that those who encounter them may be similarly inspired. That above all, this stuff should be fun.

Think about that the next time you’re crafting some Python code and see if it doesn’t change how you approach things.

python, thoughts , ,

Atomisator Configuration for Unified Python Planet

May 9th, 2009

Robert Kern asked me to share my Atomisator configuration for the Unified Python Planet. So, here it is:

[atomisator]
 
# sources
sources = 
    rss http://www.planetpython.org/rss20.xml 
    rss http://planet.python.org/rss10.xml
 
# filters
# "doublons" is French for "duplicates" --  this is what de-dups the feed
filters =
	doublons
 
# enhancers
# I'm not using any, just leave this blank (default)
enhancers =
 
# outputs
outputs =
    rss /home2/mpirnat/webapps/pirnatwp/static/unified_python_planet.xml "http://feeds2.feedburner.com/UnifiedPythonPlanet" "Unified Python Planet" "A uniqued union of the Official and Unofficial Python planet feeds.  Generated by Atomisator FTW!" 
 
# database
database = sqlite:///atomisator/unified_python_planet.db

The example configuration files that come with Atomisator are really good at illustrating how you would use them; I’ve omitted the explanatory comments from my example, but they’re in the examples and will point you in the right direction. Thanks again to Tarek for this very handy tool!

geekery, python , , , , ,

The Diaper Pattern Stinks

May 9th, 2009

I mentioned the “Diaper Pattern” in a recent post and got some comments asking what the term meant. I had hoped to just link to an explanation, thinking it was a well-known antipattern, but several minutes of frustrated Googling have left me convinced that there’s actually a tiny knowledge gap to be filled here.

“Diaper Pattern” is a term that I picked up from working with Matt Wilson and David Stanek. It’s an anti-pattern about naively over-broad exception catching. A very basic example in Python might be:

try:
    do_something_that_might_throw_various_exceptions()
except Exception:
    pass

If anything at all goes wrong in do_something, the except will catch and silence it–but we know that errors should never pass silently (unless explicitly silenced). It’s called a Diaper because it catches all the shit.

In practice, unless you have a good reason to catch everything, it’s a lot better to only catch the specific exceptions that need special handling, so that you’ll know right away about any runtime surprises and where they came from:

try:
    do_something_that_might_throw_various_exceptions()
except IOError, e:
    # handle just IOErrors; let everything else make noise

Maybe it’s just a coincidence, but I’ve heard the term used more by developers who have young children than by those without kids.

geekery, naughty-words, python , , , ,

The Zen of Doing It Wrong

May 6th, 2009

A coworker unearthed this little treasure today… I think it’s a vestigial structure to assist Diaper (anti)Pattern treatment during testing or debugging, but it’s still gross to see it in real, live production code. (Oddly enough, I couldn’t find a good “Diaper Pattern” link whilst Googling about–surely I’m not the only one who uses this term?) Anyway, without further ado:

        try:
            os.remove(filename)
        except:
            raise
            pass
        try:
            os.remove(os.path.join(TMP,'out-%s' % base_filename))
        except:
            raise
            pass
        try:
            os.remove(os.path.join(TMP,'properties_%s.lock' % brandid_human))
        except:
            raise
            pass

If an exception is raised, raise an exception… It has a certain zenlike beauty to its awfulness. The use of the Diaper Pattern is bad enough, but this guarantees blowouts!

geekery, python, wtf , , , ,

Return to Clepy; Possible End of the Universe

April 13th, 2009

Forget the LHC–a far surer sign that the end times are nigh is that I’ve finally been able to attend Clepy for the first time in about 15 months. (In case I do actually cause the implosion of the universe, let me take this opportunity to apologize in advance. Sorry about that; my bad!)

We had an extra-long social period at the start of this month’s meeting (while there were some misadventures with pizza delivery), but the bonus casual time was great for meeting new people and getting caught up with people I hadn’t seen in a long time.

The main focus of the meeting tonight was a live demo of Gary Bernhardt’s TDD tools, Dingus and Mote. I’d seen a little bit of Dingus from following Gary on Twitter, but had not ever seen it in action, and Mote was brand new to me. Dingus is a mocking library that attempts to aggressively and magically isolate the code under test from everything external to it. Mote is a lightweight spec-runner (not “test-runner!”) that wants to remove as much verbosity as possible. Thanks to some nifty decorator-fu that makes it super-easy to rig up automatic Dingus isolation in your Mote specs, the two combine to form some kind of TDD Voltron. Mote’s a little rough around the edges, but it’s brand new and under heavy development, and shows a ton of potential. If you’re interested in testing or TDD, check out these tools!

The unsung star of tonight’s presentation was Gary’s .vimrc, which contains powerful magics for quickly running tests from within vim and displaying their success or failure as a green or red line at the bottom of the buffer. There’s plenty more fascinating arcana in there, so it’s nice to know that his dot files are on bitbucket too. I’ll eventually peruse and dissect them.

This was Clepy’s first meeting at LeanDog’s floating headquarters; I was really pleased with it as a venue. It’s centrally located, easy to get to and from, is highly geek-friendly, and is quite novel as a meeting space. (It’s on a boat! Next to a submarine! How great is that?!) I hope we can continue to meet here as it really seems like a positive change. It was also great to see new faces and meet new people tonight; it seems like the group has really started to thrive independently of AGI.

Above all, I really hope to be able to start attending regularly again, even if it does trigger the apocalypse.

clepy, python, testing , , , , , ,

A More Excellent Unified Python Planet

March 28th, 2009

Good news, everyone!

After a couple days of playing Twitter tag with Tarek Ziade, we finally met up for lunch at PyCon, where, in addition to having a lovely conversation, he was kind enough to give me a personal tour of his Atomisator framework for data aggregation.

Atomisator is much savvier than Yahoo Pipes at things like removing duplicates and not mangling content, so I’ve kicked Pipes to the curb and hooked up the Unified Python Planet feed to my Atomisator output.

What this means for you, lucky reader, is that everyone who subscribes to the Unified Python Planet should not only stop getting duplicates, but, more importantly, have legible code samples directly in the feed. (It’s okay if you need to take a moment to process this joyous news. It makes me pretty damn happy.)

Please let me know if you experience any flakiness with the new, improved feed.

I want to give major, major thanks to Tarek! Atomisator is really well suited to this purpose, as well as some other stuff I’m now curious to tinker with.

geekery, pycon, python , , ,

Python: You’re Doing It Wrong

November 25th, 2008

A coworker just showed me this little gem–a maintenance script that’s got no author attribution and isn’t in source control, so whoever has perpetrated this crime against all that’s good and holy remains anonymous and (for the moment) safe from our wrath. I’m so completely taken aback by this–I… I don’t know what to say. I just have to share.

#!/usr/bin/env python
import os,sys
C=os.chdir
S=os.system
M=os.mkdir
J=os.path.join
A=os.path.abspath
D=os.path.dirname
E=os.path.exists
W=sys.stdout.write
V=sys.argv
X=sys.exit
ERR=lambda m:W(m+"\n")
PRNT=lambda m:W(m+"\n")
assert len(V)==2,"you must provide a sandbox name"
SB=V[1]
H=A(D(__file__))
SBD=J(D(H),SB)
C(SBD)
PST=J(SBD,'bin/paster')
VAR=J(SBD,'var')
ETC=J(SBD,'etc')
S("mkdir -p "+VAR)
PRNT("restarting "+SB)
CMD=";".join(['source %s'%J(SBD,'bin/activate'),PST+" serve --daemon --pid-file=%s/sandbox.pid --log-file=%s/sandbox.log %s/sandbox.ini start"%(VAR,VAR,ETC)])
PRNT(CMD)
S(CMD)
PRNT("All done!")
X(0)

Weep for us.

geekery, python, wtf , , ,

A Unified Python Planet

November 2nd, 2008

I subscribe to both the official and unofficial Python planet feeds because they’ve each got some content that’s unique to one or the other, but the overlap and duplication has really been bugging me lately. So I suddenly remembered the joy that is Yahoo Pipes and glued both of them together with a unique-ifying filter to eliminate duplicates, and, feeling generous, I stuck a friendlier Feedburner URL around it–you can get it at http://feeds.feedburner.com/UnifiedPythonPlanet.

It makes me pretty happy so far, except that somewhere in Pipes land I seem to be losing newlines in pre elements (which appears to be a bug in Pipes itself, argh), which is a little (very) annoying when trying to read people’s code samples. I may end up replacing the Pipes side with a little bit of Python (a little Feedparser and PyRSS2Gen ought to do the trick) if I get the time later today.

Anyway. I figured I should share it, on the offchance it makes anyone else’s life better. (Or if such a creature already exists, let me know and I’ll gladly use that instead.)

geekery, python, rss , , ,