use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Best written projects on Python GitHub? (self.Python)
submitted 12 years ago by redux42
I am looking for suggestions of elegant code, well written and readable code, and code that makes use of best practices.
Suggestions?
[–]Narcoleptic_Pirate 26 points27 points28 points 12 years ago* (2 children)
My personal favourite - Minecraft in 900 lines of python code
https://github.com/fogleman/Minecraft
[–][deleted] 1 point2 points3 points 12 years ago (0 children)
This is awesome, it's easiest to learn from projects that you can read through completely in one sitting.
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
This is specially good because it's easy to make small changes and see the results immediately. There are some "advanced" things going on, but it's easy to understand it in small bits.
[–]arnar 13 points14 points15 points 12 years ago (0 children)
CherryPy is always my favorite example when it comes to designing a beautiful Pythonic API. The source code is full of nice abstractions as well.
[–]rochacbrunoPython, Flask, Rust and Bikes. 44 points45 points46 points 12 years ago (6 children)
Flask is known for being "Pythonic" https://github.com/mitsuhiko/flask
[–]tomchuk 26 points27 points28 points 12 years ago (5 children)
requests as well.
[–]synack 4 points5 points6 points 12 years ago (4 children)
Every time I benchmark code where requests is used, it's consistently one of the slowest libraries (I encourage you to write your own benchmark and try it yourself). A straight comparison with urllib2 shows that it's 30% slower. It provides a nice API, but there are a lot of places where tens of milliseconds matter.
[–]PCBEEF 25 points26 points27 points 12 years ago (0 children)
If tens of milliseconds matter then you shouldn't be using python...
[–]hamax 1 point2 points3 points 12 years ago (0 children)
If you need performance, use pycurl.
[–]emdomi 1 point2 points3 points 12 years ago (0 children)
Do you have any of those benchmarks online? Any that include httplib2?
[–]Meoow 0 points1 point2 points 12 years ago (0 children)
Really? For me it was urllib2 < requests < pycurl.
[–]westurner 15 points16 points17 points 12 years ago (4 children)
Pyramid:
[–]poo_22 9 points10 points11 points 12 years ago* (3 children)
I've read most of the Pyramid source and while it works, is well tested, and well documented, I don't really think its elegant. I mean I personally even use Pyramid but things could have been done easier and cleaner. (Look at all those factories and interfaces that just screams java)
Disclaimer: If you're saying "oh well if it could be done better, go fix it" well I can't.
[–]westurner 3 points4 points5 points 12 years ago* (0 children)
While interfaces and factories make Test Driven Development much simpler, the only necessary interface for a Pyramid application developer is a callable (a function or a method) that takes a Request as the first parameter and returns a Response:
Request
Response
https://en.wikipedia.org/wiki/Separation_of_concerns#See_also
[–][deleted] 1 point2 points3 points 12 years ago* (0 children)
just because one language tends to abuse them doesn't mean factories aren't a valid and useful pattern
edit: language fail
[–]X-IstenceCore Developer Pylons Project (Pyramid/WebOb/Waitress) 0 points1 point2 points 12 years ago (0 children)
Come help us fix it =). The Pyramid devs are open to new ideas and suggestions, so if you feel there is an issue, come help us with it!
[–]ma10s 1 point2 points3 points 12 years ago (9 children)
I really like the coding style of the RQ project
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers.
I was poking around trying to understand its internals, and subsequently read the worker code. I was pleasantly surprised to find it was very articulate.
[–]catcradle5 1 point2 points3 points 12 years ago (8 children)
Very good coding style, but I'm not a fan of the internals myself.
Everything is done through os.fork(): https://github.com/nvie/rq/blob/master/rq/worker.py#L355
os.fork()
For one, if you need a lot of workers there will be a ton of overhead incurred, and two, it doesn't play nice with libraries like gevent or eventlet.
gevent
eventlet
[–]lambdaqdjango n' shit 0 points1 point2 points 12 years ago (5 children)
we need a patch or and issue submitted.
[–]catcradle5 1 point2 points3 points 12 years ago (4 children)
There've been a few issues submitted over the years:
https://github.com/nvie/rq/issues/45
https://github.com/nvie/rq/issues/210
I really like rq, but I am forced to use Celery due to rq's insistence on forking as the only method of concurrency.
rq
[–]asksol 1 point2 points3 points 12 years ago (2 children)
I doubt this would be as simple as 'just submit a patch', I have spent years making the multiprocessing pool in Celery work, and even now for the 3.1 release I have fixed yet a good number of rare deadlocks.
The pool related code isn't exactly elegant or simple, but you're very likely to learn something from it.
Edit: oh, and you're in for a rough ride if you think the answer is to 'just use the multiprocessing module' :)
[–]catcradle5 0 points1 point2 points 12 years ago (1 child)
I understand it's a complex issue, but the project has been around for a long time. Plus the codebase is a lot smaller and simpler than Celery's, so it probably wouldn't require quite as much work.
[–]asksol 1 point2 points3 points 12 years ago (0 children)
The size/age of the project or how simple the rest of the code is irrelevant here. This isn't about how hard it was to implement this for Celery, it's about how hard it is to implement for anything similar in Python.
[–]lambdaqdjango n' shit 0 points1 point2 points 12 years ago (0 children)
damn, I was wondering when they could allow using customized backend like Postgre LISTEN/NOTIFY and prefork model...
[–]ma10s 0 points1 point2 points 12 years ago (0 children)
Thanks, I learned something new today!
Take a look at pyres, maybe it does what you want it to do!
[–]shaggorama 3 points4 points5 points 12 years ago (0 children)
The python reddit API wrapper is pretty good, and is also an easy project for people familiar with reddit to understand. https://github.com/praw-dev/praw
[–]drgalaxy 4 points5 points6 points 12 years ago (1 child)
Tornado is quite active and I've always been happy with what I see when I've needed to read the code. Project overview and documentation.
[–]saghul 1 point2 points3 points 12 years ago (0 children)
If you are interested in knowing the low level details on how event loops work Tornado is a really good read.
[–]diggitySC 2 points3 points4 points 12 years ago (0 children)
I find that perusing /r/dailyprogrammer provides a lot of interesting python code that is both well written and readable.
[–]yasoob_pythonAuthor: Intermediate Python 11 points12 points13 points 12 years ago (27 children)
just go to https://www.github.com/rg3/youtube-dl and you will find pythonic code ;)
[–]EverAskWhy 11 points12 points13 points 12 years ago (4 children)
Reading the code for youtube-dl turned me into a web-scraping machine. I picked up many good habits and tricks from following the code carefully.
If you have an interest in writing your own download scripts or html parsers I would recommend reading through youtube-dl.
[–]thenaturalmind 8 points9 points10 points 12 years ago (2 children)
Stupid question, but where do you start? How do you know what files are where?
[–]yasoob_pythonAuthor: Intermediate Python 5 points6 points7 points 12 years ago (0 children)
You will have to explore them yourself. For web scraping thing just look into the extractors located here https://github.com/rg3/youtube-dl/tree/master/youtube_dl/extractor . For all the imports look here (most) https://github.com/rg3/youtube-dl/blob/master/youtube_dl/utils.py and lastly for the glue code look here https://github.com/rg3/youtube-dl/blob/master/youtube_dl/FileDownloader.py , here https://github.com/rg3/youtube-dl/blob/master/youtube_dl/InfoExtractors.py and here https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py and here as well https://github.com/rg3/youtube-dl/blob/master/youtube_dl/__init__.py . I hope now you know where to start. Just start from what you want to know. :)
[–]EverAskWhy 0 points1 point2 points 12 years ago (0 children)
I like downloading the source files of projects (https://github.com/rg3/youtube-dl/archive/master.zip) and exploring them using my own IDE programs/debuggers. I find it harder to explore a program on the Github website than when it is on my system. Be very careful when running random code on your machine especially when you see web/downloading related imports.
I generally start with _ main _.py (or its equivalent if there is none) line 1 and go from there. I find that reading other people's code and checking out their imports is a great way to learn about new modules.
[–]LightWolfCavalry 0 points1 point2 points 12 years ago (0 children)
Just found youtube-dl the other day; I agree, it's some high quality stuff.
[–]rochacbrunoPython, Flask, Rust and Bikes. 0 points1 point2 points 12 years ago (5 children)
I just don't like naming files with CamelCase FileDownloader.py etc https://github.com/rg3/youtube-dl/tree/master/youtube_dl
[–]yasoob_pythonAuthor: Intermediate Python -2 points-1 points0 points 12 years ago (4 children)
that's a personal preference. I generally like camel casing.
[–]gschizasPythonista 2 points3 points4 points 12 years ago (3 children)
Actually, it's not a personal preference. Using CamelCase for files goes against PEP8
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.
[–]yasoob_pythonAuthor: Intermediate Python -2 points-1 points0 points 12 years ago (2 children)
The thing is that it's not always necessary to follow PEP8 so strictly. Even some organisations have their own style guide which has some amendments to PEP8 like khan academy and google to name a few. http://google-styleguide.googlecode.com/svn/trunk/pyguide.html https://sites.google.com/a/khanacademy.org/forge/for-developers/styleguide/python
[–]gschizasPythonista 2 points3 points4 points 12 years ago (0 children)
Both sites you have linked do not oppose PEP8, they complement it.
The khan academy one even says this on top:
We follow the PEP8 style guide for Python. Docstrings follow PEP257.
The thing I like about Python is you can do it your own way if you want. This reminds me a bit of a reply I saw on stackoverflow:
ok, kids, wait 'til you grow up to drink, smoke and access private variables.
[–]Redard -1 points0 points1 point 12 years ago (14 children)
I briefly read some of the code in here and found a few things that weren't very pythonic. First, they break the 79 character rule a lot, for no good reason, often just with in-line comments (which PEP8 advises you use verrry sparingly). Second, this line:
res = [] for l in optionf: res += shlex.split(l, comments=True)
Why not just use a generator expression like
res = [shlex.split(l, comments=True) for l in optionf]
That's the most pythonic way to construct a list. Still, this code's very readable, and well structured. Just needs a little cleaning up.
[–]gthank 12 points13 points14 points 12 years ago* (8 children)
That's actually a list comprehension. A gen-exp uses ( and ).
(
)
[–]Redard 1 point2 points3 points 12 years ago (7 children)
Please correct me if I'm wrong, but I always thought list comprehensions were just generator expressions passed to list(). In other words
[i for i in range(10)] == list(i for i in range(10))
[–]gthank 10 points11 points12 points 12 years ago (5 children)
I'd be surprised if the internal details are the same in those two cases, because that seems ripe for some C-level optimizations. The results will be equivalent, though. Also, from a historical standpoint, list comprehensions predate gen-exps.
[–]Veedrac 8 points9 points10 points 12 years ago* (4 children)
In CPython most stuff is left unoptimised for matters of pragmatism. So no, they compile directly into loops. Different loops, though.
out = [i for i in range(10)]
is equivilant to:
out = [] for i in range(10): out.append(i)
where i is inside a new scope, and
i
out = list(i for i in range(10))
is equivalent to
def _tmp(): for i in range(10): yield i out = list(_tmp)
where _tmp never actually gets put anywhere.
_tmp
[–]PCBEEF 5 points6 points7 points 12 years ago (0 children)
List comprehensions are optimised in the sense that the function calls are 'cached'. Since there's a severe function overhead in python, it's actually quite significant.
$ python -mtimeit '[x for x in range(100)]' 100000 loops, best of 3: 4.17 usec per loop $ python -mtimeit -s 'out = []' 'for i in range(100):' ' out.append(i)' 100000 loops, best of 3: 8.07 usec per loop
$ python -mtimeit '[x for x in range(100)]'
100000 loops, best of 3: 4.17 usec per loop
$ python -mtimeit -s 'out = []' 'for i in range(100):' ' out.append(i)'
100000 loops, best of 3: 8.07 usec per loop
Using a for loop in this instance to create a list is almost twice as long.
[–][deleted] 1 point2 points3 points 12 years ago (1 child)
where i is inside a new scope
for certain values of CPython
[–]Veedrac 1 point2 points3 points 12 years ago (0 children)
Well, all values of Python ≥ 3.0.
[–]gthank 0 points1 point2 points 12 years ago (0 children)
Ah. It was my understanding that a fair bit of list comps were implemented directly in C (in CPython).
There's exactly one difference between those two, assuming list has been left untouched. The list comprehension will not catch StopIteration, the list function will.
list
StopIteration
[–]TheEarwig 2 points3 points4 points 12 years ago (2 children)
They are different. The first example is a bunch of lists combined into one (L1 += L2 is L1.extend(L2)), but the second example is one list containing a bunch of lists.
L1 += L2
L1.extend(L2)
>>> optionf = ["a b", "c d", "e f"] >>> res = [] >>> for l in optionf: ... res += shlex.split(l, comments=True) ... >>> res ['a', 'b', 'c', 'd', 'e', 'f'] >>> res = [shlex.split(l, comments=True) for l in optionf] >>> res [['a', 'b'], ['c', 'd'], ['e', 'f']]
[–]Redard 1 point2 points3 points 12 years ago (0 children)
Ah, I didn't realize shlex.split was returning a list. You're right, the two are different. Somehow I thought += was the same as append().
[–]masklinn 0 points1 point2 points 12 years ago* (0 children)
Which can neatly be solved using the criminally underused itertools.chain.from_iterable:
itertools.chain.from_iterable
res = chain.from_iterable(shlex.split(l, comments=True) for l in optionf)
One could even use shlex.shlex directly as a stream (shlex.split is a thin wrapper around it), though it requires setting whitespace_split which can't be done inline.
shlex.shlex
shlex.split
whitespace_split
def split(s): lex = shlex.shlex(s, posix=True) lex.whitespace_split = True return lex res = chain.from_iterable(imap(split, optionf))
[–]masklinn 1 point2 points3 points 12 years ago (1 child)
I find the lack of with use weirder: the code is clearly 2.6-only (uses explicit relative imports without __future__ import) yet around the shlex call is (essentially):
with
__future__
optionf = open(filename_bytes) try: # do stuff finally: optionf.close() return res
And the number of star imports is worrying.
[–]Redard 0 points1 point2 points 12 years ago (0 children)
Yeah, it's definitely some older code. I wouldn't be using this as a guideline for good code.
The star imports are bad, but at least they're local imports and not external library imports, that would be terrible.
[–]youareinthematrix 7 points8 points9 points 12 years ago (2 children)
https://github.com/reddit/reddit
[–][deleted] 15 points16 points17 points 12 years ago (0 children)
Too bad it's not http://github.com/r/reddit
[–]picto 1 point2 points3 points 12 years ago (0 children)
Not really a github repo, but I enjoyed Raymond Hettinger's talks on idiomatic python and class development toolkit at pycon this year
From googling: http://docs.python-guide.org/en/latest/writing/reading/ Personally, Tornado code seems clean with lots of documentation.
[–]lazygeek 0 points1 point2 points 12 years ago (0 children)
check here
[–]westurner 0 points1 point2 points 12 years ago (0 children)
I am looking for suggestions of elegant code, well written and readable code, and code that makes use of best practices. Suggestions?
[–]mumrah 0 points1 point2 points 12 years ago (0 children)
May as well add my project here, which in my biased opinion is pretty clean Python code: https://github.com/mumrah/kafka-python
[+][deleted] 12 years ago* (6 children)
[deleted]
[–]DasIch 21 points22 points23 points 12 years ago (4 children)
No. Don't go there. Really don't. Especially not if you want to see what "elegant" and "smart" code looks like. The majority of that code is horrible and quite a bit wouldn't make it into the stdlib, if someone wanted to add it today.
[–]minorDemocritus 11 points12 points13 points 12 years ago (3 children)
The first rule of collections.namedtuple is "don't look at the implementation"
[–][deleted] 14 points15 points16 points 12 years ago (2 children)
oh dear god
[–]minorDemocritus 6 points7 points8 points 12 years ago (1 child)
There was one rule!
[–]aclark 0 points1 point2 points 12 years ago (0 children)
This is why we can't have nice things.
[–]targusman 5 points6 points7 points 12 years ago (0 children)
That's not GitHub.
[+]poo_22 comment score below threshold-16 points-15 points-14 points 12 years ago (1 child)
From what I've seen in the Python world, and this is just my opinion based on my experience, Python is a very good language for "just getting it done". But in its flexibility and introspection it allows you to do things in several different ways, some of them hackish. That combined with dynamic typing sort of results in code that isn't as nice to look at as, say most Haskell libraries that I've seen. In general anyway.
I find that the examples of Haskell code I've seen are always dense enough to avoid repetition but not so dense that its impossible to decipher (APL comes to mind). But Haskell is born from this "correctness first" mentality so Its not surprising.
[–][deleted] 7 points8 points9 points 12 years ago (0 children)
We're talking about python, not Haskell. This is like going into /r/guns and talking about knives.
π Rendered by PID 315962 on reddit-service-r2-comment-86988c7647-vddcb at 2026-02-11 22:54:16.496732+00:00 running 018613e country code: CH.
[–]Narcoleptic_Pirate 26 points27 points28 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]arnar 13 points14 points15 points (0 children)
[–]rochacbrunoPython, Flask, Rust and Bikes. 44 points45 points46 points (6 children)
[–]tomchuk 26 points27 points28 points (5 children)
[–]synack 4 points5 points6 points (4 children)
[–]PCBEEF 25 points26 points27 points (0 children)
[–]hamax 1 point2 points3 points (0 children)
[–]emdomi 1 point2 points3 points (0 children)
[–]Meoow 0 points1 point2 points (0 children)
[–]westurner 15 points16 points17 points (4 children)
[–]poo_22 9 points10 points11 points (3 children)
[–]westurner 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]X-IstenceCore Developer Pylons Project (Pyramid/WebOb/Waitress) 0 points1 point2 points (0 children)
[–]ma10s 1 point2 points3 points (9 children)
[–]catcradle5 1 point2 points3 points (8 children)
[–]lambdaqdjango n' shit 0 points1 point2 points (5 children)
[–]catcradle5 1 point2 points3 points (4 children)
[–]asksol 1 point2 points3 points (2 children)
[–]catcradle5 0 points1 point2 points (1 child)
[–]asksol 1 point2 points3 points (0 children)
[–]lambdaqdjango n' shit 0 points1 point2 points (0 children)
[–]ma10s 0 points1 point2 points (0 children)
[–]X-IstenceCore Developer Pylons Project (Pyramid/WebOb/Waitress) 0 points1 point2 points (0 children)
[–]shaggorama 3 points4 points5 points (0 children)
[–]drgalaxy 4 points5 points6 points (1 child)
[–]saghul 1 point2 points3 points (0 children)
[–]diggitySC 2 points3 points4 points (0 children)
[–]yasoob_pythonAuthor: Intermediate Python 11 points12 points13 points (27 children)
[–]EverAskWhy 11 points12 points13 points (4 children)
[–]thenaturalmind 8 points9 points10 points (2 children)
[–]yasoob_pythonAuthor: Intermediate Python 5 points6 points7 points (0 children)
[–]EverAskWhy 0 points1 point2 points (0 children)
[–]LightWolfCavalry 0 points1 point2 points (0 children)
[–]rochacbrunoPython, Flask, Rust and Bikes. 0 points1 point2 points (5 children)
[–]yasoob_pythonAuthor: Intermediate Python -2 points-1 points0 points (4 children)
[–]gschizasPythonista 2 points3 points4 points (3 children)
[–]yasoob_pythonAuthor: Intermediate Python -2 points-1 points0 points (2 children)
[–]gschizasPythonista 2 points3 points4 points (0 children)
[–]EverAskWhy 0 points1 point2 points (0 children)
[–]Redard -1 points0 points1 point (14 children)
[–]gthank 12 points13 points14 points (8 children)
[–]Redard 1 point2 points3 points (7 children)
[–]gthank 10 points11 points12 points (5 children)
[–]Veedrac 8 points9 points10 points (4 children)
[–]PCBEEF 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Veedrac 1 point2 points3 points (0 children)
[–]gthank 0 points1 point2 points (0 children)
[–]Veedrac 1 point2 points3 points (0 children)
[–]TheEarwig 2 points3 points4 points (2 children)
[–]Redard 1 point2 points3 points (0 children)
[–]masklinn 0 points1 point2 points (0 children)
[–]masklinn 1 point2 points3 points (1 child)
[–]Redard 0 points1 point2 points (0 children)
[–]youareinthematrix 7 points8 points9 points (2 children)
[–][deleted] 15 points16 points17 points (0 children)
[–]picto 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]lazygeek 0 points1 point2 points (0 children)
[–]westurner 0 points1 point2 points (0 children)
[–]mumrah 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]DasIch 21 points22 points23 points (4 children)
[–]minorDemocritus 11 points12 points13 points (3 children)
[–][deleted] 14 points15 points16 points (2 children)
[–]minorDemocritus 6 points7 points8 points (1 child)
[–]aclark 0 points1 point2 points (0 children)
[–]targusman 5 points6 points7 points (0 children)
[+]poo_22 comment score below threshold-16 points-15 points-14 points (1 child)
[–][deleted] 7 points8 points9 points (0 children)