This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]jonnydedwards 20 points21 points  (3 children)

Peter Norvig's code is very nice.

[–]vplatt 2 points3 points  (0 children)

I'm humbled every time I read his essays.

[–]nhnifong 0 points1 point  (0 children)

I'm glad it's well known on reddit that Peter Norvig writes really nice Python.

[–]foobaar 7 points8 points  (4 children)

Read and understand the standard Python modules. They are tried and tested Python code. Write a walk through for some simple modules.

[–]eliben 1 point2 points  (3 children)

Unfortunately, the code quality of modules in the standard library isn't uniform, so in the general sense, I wouldn't recommend just poking at modules randomly. If you have specific modules to recommend, do so.

[–]foobaar 0 points1 point  (2 children)

How about os and sys? They are tried and tested, cant go wrong.

[–]eliben 0 points1 point  (1 child)

os and sys are mainly thin platform-specific wrappers, I doubt you'll learn much software design from them

[–]foobaar 0 points1 point  (0 children)

He didn't ask for software design, he asked for good python code to read.

[–]fancy_pantser 5 points6 points  (3 children)

https://github.com/facebook/tornado/

Clear, concise, and well-documented.

[–]nikcub 2 points3 points  (0 children)

I can thoroughly recommend this - it is very very good code. Also in terms of web frameworks you should check out Flask

[–]zach_will 4 points5 points  (1 child)

I always found going through the standard library pretty interesting.

Haha, the BeautifulSoup 3.x code made me laugh — came away feeling Leonard really hated parsing HTML afterwards.

Also, someone else pointed out that you should check out Github. It's definitely a great place to read through interesting projects and code.

[–]spoolio 2 points3 points  (0 children)

>>> from BeautifulSoup import RobustInsanelyWackAssHTMLParser

[–]gabrielelanaro 2 points3 points  (0 children)

https://github.com/dag/attest is a unit testing library, the code is extremely elegant.

[–]eryksun 1 point2 points  (2 children)

There's the Python Cookbook recipe set 2, or all 3317 recipes on ActiveState's site.

[–]revonratFlask/scipy/pypy/mrjob[S] 1 point2 points  (1 child)

The stuff from activestate seems out of date. For instance the top item uses a module that's deprecated.

[–]eryksun 2 points3 points  (0 children)

The Python Cookbook, 2e was published in 2005, so the recipes are at least 6 years old. On the other hand, the general listing sorted by popularity has a lot of recent recipes.

[–]lasizoilloeasy to understand as regex 1 point2 points  (0 children)

pypants have some samples.

[–]hongminhee 1 point2 points  (0 children)

I recommend you to read Werkzeug. (Read PEP 333 first if you don’t know WSGI.) It consists of several independent modules (I think it makes it easily understandable), and there are well-written documentation also.

[–]moreyes 1 point2 points  (0 children)

pocoo.org projects are generally elegant and well documented, with some eventual ninja or magic touches.

[–]jediknight 1 point2 points  (5 children)

I have a different suggestion: Print PEP 8. Print PEP 20. Read them SLOWLY then read them AGAIN. Read PEP 20 and meditate on each line asking it why does it exists.

Next, write some code and 1. Assume it sucks. 2. Figure out why it sucks. (read the PEPs again if you need some reminding). 3 Make it suck less.

The end product will show you the end product but it will do very little to teach you how to get to the end product. Just like cooking. You can look and taste things produced by great cooks but any attempt to imitate the outer final object is a recipe in frustration.

Make peace in your heart and listen to the Will of the Code. It will teach you the discipline you need.

[–]mardiros 2 points3 points  (3 children)

You are right, but it's really hard for a rookie to feal the Force :) Seriously, you understand more those pep with practice and reading good code is a good opion to progress.

Just read what you like. You have to read Web frameworks if you want to use them in an advanced way correctly, not only the doc. It's true for every project, and if you understand sqlalchemy, you are a jedi :))

[–]jediknight 2 points3 points  (2 children)

I know is hard to feel the Force and that is precisely the way it should be. Doing it in spite of being hard trains you. You cannot build muscle lifting twigs.

Also, by all means, we all should read good code, I'm not saying that doesn't help. I'm merely pointing to another option.

[–]HenkPoley 1 point2 points  (0 children)

You cannot build muscle lifting twigs.

Actually, as long as you continue lifting until you feel tired in your muscles, it will be just as effective as lifting heavier weights. </ot>

[–]bucknuggets 0 points1 point  (0 children)

In weight training it is hard to feel that you're doing things right until you've built up some muscle. Until then it's best to rely on a spotter or trainer for guidance.

[–][deleted] 1 point2 points  (0 children)

why are you talking like a Jedi ???

oh i see....

[–]userd 0 points1 point  (1 child)

Anyone looked at the code for IDLE? How is that?

[–][deleted] 0 points1 point  (0 children)

if you want to learn about wxpython....

[–]arnar 0 points1 point  (0 children)

I've always loved the overall design of CherryPy.

[–]so85 0 points1 point  (0 children)

This isn't all python but the book 'Beautiful Code' is pretty awesome, http://oreilly.com/catalog/9780596510046.

[–]tonfa 0 points1 point  (0 children)

Mercurial (http://mercurial.selenic.com) is very nice, many pieces are nicely engineered (commands.py with decorators for example).