Ask HN: How do you structure your Python programs?

7 points by augustflanagan ↗ HN
I've been teaching myself to code for about 6 months now, and am finally starting to see the efforts pay off (I can actually do stuff). I first started learning to code with Python, and then switched to Ruby on Rails for a project I am working on about 3 months ago. The thing that I, as a noob, love most about Rails is the rigid structure that it provides. It totally makes sense to me, and I feel like it keeps my code organized very well.

However, I had an idea for a small coding project the other day, and for a couple of reasons it really made sense to build it on Google App Engine. So...I dove back into Python after several months of being away. It's amazing how much better I understand the fundamentals now, but I'm looking at my code base (<500 lines) and realizing that I have a mess on my hands.

So, my questions is: How do you structure your code in Python? Do you have any recommendations for tutorials or suggested readings? Any quick tips to help me clean up a mess?

Thanks.

7 comments

[ 2.9 ms ] story [ 30.9 ms ] thread
Thanks! That looks like a good place to start.
PEP 8 is great, but (unless I've forgotten . . .) it doesn't really help w/ directory structure and so on.

I'd say take a look at some of the code in the standard library and in some other packages you've used before, then read this:

http://jcalderone.livejournal.com/39794.html

Then just go for it.

And keep refactoring, when you get into a mess.
I guess that what you miss about rails is the MVC architectural layout, but that's not specific to RoR and you can use it wherever you want, even in the webapp microframework for google app engine: have a file or module for models, another for controllers and another for views and get them together in a main module; you can also use django (I've used django 1.1 in gae with no problem) or use Jruby on appengine (there's a whole project for that: http://code.google.com/p/appengine-jruby/) to run ruby on rails, padrino or sinatra, if you really miss ruby. I've also run ruby projects on app-engine (in sinatra, which is the coolest microframework out there, IMO) and it's been easy, I actually felt it easier to set up that a django project, as it was hack free.
Thanks, I'd never heard of Jruby before, that looks really interesting.
Try a Python framework - there's enough of them to work with. The good ones (like Django) have a structure and standards that make sense - and have great tutorials, for the most part.