How to Rebuild a Virtualenv in Heroku

Today I managed to get myself a little bit stuck with Heroku’s otherwise seamless Python support. In an earlier revision of my app, I had used -e to install an editable version of a package (the version of the package I needed had not yet been released to PyPI, so I pointed pip at Github). Since then, the version I need has been released.

So, update requirements.txt, then git push heroku master, and all is well, right? Not so fast...

python, heroku, virtualenv

Read More...

A Modest Proposal for Tech Conferences

After more difficulty than expected, I’m at PyOhio, and once again I found myself not taking notes during today’s sessions, but madly scrambling to open browser tabs to each of the open source projects, blog posts, and other online resources the presenters mentioned. It’s a rush to keep up, particularly with laundry-list-of-links slides!

The sessions at PyOhio (and most other conferences I attend) are recorded and posted online, but I never seem to make the time to look for the links after the fact — and you can’t click a link in a video of a projected slide anyway.

So this has gotten me thinking: since presenters typically submit talks through web forms, why not also ask them for a listing of the resources that are a part of their presentation, and preserve this in the online talk notes? A few words on the subject of the link would be nice as well, but with not that much cleverness, this could be extracted from the page. Knowing that this will be available after the fact frees presenters to move fluidly through their presentations, and allows attendees to focus on the content rather than trying to type URLs as quickly as possible.

Perhaps not everyone will be willing to take the time to fill out what, to them, surely seems like a busy-work form, but with the right application of social pressure, and after the expectation has been set that speakers will do this, I suspect that a majority would be willing to make the effort.

conferences

What the heck is an xrange?

Pop quiz: how would you implement Python’s xrange (known in Python 3.x as range) in Python and without making a list or any other sequence type?

If you answered “with a generator,” you’d be wrong! And it’s likely because you’ve only ever used an xrange in code like:

for i in xrange(10):
    # do something 10 times

In this case (where the xrange is only used as an “argument” to a for loop), a generator would probably suffice. But, in fact, xrange is implemented as an object, not a function or generator. Why? Let’s find out.

python, internals

Read More...

A Python "Cast Constructor"

Very occasionally, I write code where I’m given an object of some class (usually from a library call), but I wish to use additional methods on that class as though they had been defined there. In some languages (Objective-C shines in this regard with categories), you can do this very naturally. In Python, most people probably resort to monkey patching to accomplish this.

python, anti-pattern

Read More...

Exploring Python Code Objects

Inspired by David Beazley’s Keynote at PyCon, I’ve been digging around in code objects in Python lately. I don’t have a particular axe to grind, nor some particular task to solve (yet?), so consider this post just some notes and ramblings that might be of interest (and my apologies if not).

Disclaimer: This post is about CPython version 2.7, though much of it is also likely true for other CPython versions (including 3.x). I make no claims to its accuracy or applicability to PyPy, Jython, IronPython, etc.

python, internals, tinkering

Read More...

Hitchhiker's Guide to Python

I first heard about The Hitchhiker’s Guide to Python at PyCodeConf a few months ago. It’s a fantastic idea: open source, community-driven documentation on how to do Python right: everything from how to learn Python, to how to write idiomatic code, to how to distribute your projects, to surveys of best-of-breed open source projects and libraries you can build projects and applications on top of. Many many thanks to Kenneth Reitz for creating and maintaining the project, which is hosted at GitHub.

At this time, the Hitchhiker’s guide is a little rough around the edges: many sections are only outlined, and need content written; other sections may not even exist yet. We can safely consider it a first draft, or, if you prefer, an alpha.

This sort of undertaking is effectively impossible for one person to maintain—one person can’t possibly know of every project, library, and idiom. Moreover, it’s unfair as a user of the Guide to demand that one person must do all the work.

Therefore, a call to action: in order to make the Python community great, everyone should fork the Hitchhiker’s Guide today, find (or add) a section of interest, and submit a pull request. If you’re lucky, Kenneth will give you a sparkly cake.

And a pledge: time allowing (for, sadly, contributing to Python documentation is not my day job), I will make one contribution to the Hitchhiker’s Guide per week until it reaches completion, or until there’s nothing left to which I can conscientiously contribute (I won’t attempt to document things which I know too little about).

Finally, since this is a community effort, I want to give a shout-out to all those who’ve contributed to the Guide so far:

Thanks to everyone who’s helping to make Python a better place!

(To the folks above, if you’d like me to add or correct any of the contact information, please leave a comment.)

python, community