Live data from Hacker News

Ask HN: Good Python codebases to read?

news.ycombinator.com

111–120 of 153 posts

Re: Ask HN: Good Python codebases to read?

#111

Jumping on the Kenneth Reitz train, you might check out The Hitchhiker's Guide to Python: http://docs.python-guide.org/en/latest/ He recommends the following Python projects for reading: * Howdoi ( https://github.com/gleitz/howdoi ) * Flask ( https://github.com/mitsuhiko/flask ) * Werkzeug ( https://github.com/mitsuhiko/werkzeug ) * Requests ( https://github.com/kennethreitz/requests ) * Tablib ( https://github.com/k…

I would add the Django project to that list as it's a very large, mature, and successful open source python project - https://github.com/django/django

Re: Ask HN: Good Python codebases to read?

#113

Earlier quoted context omitted.

> The former is absolutely identical to the latter Oh, really? Let's compare: >>> absolute_0 = lambda path: path if path.startswith('/') else '/' + path >>> def absolute_1(path): ... '''Return the absolute unix path from a given path name''' ... if not path.startswith('/'): ... path = '/' + path ... return path >>> import dis >>> dis.dis(absolute_0) 2 0 LOAD_FAST 0 (path) 3 LOAD_ATTR 0 (startswith) 6 LOAD_CONST 1 ('/…

Spare us the half-baked hackery. List comprehensions and generator expressions have replaced all need for map, filter, and lambdas, and are far more readable. For someone new to Python, halfway through LPTHW, they don't need those things. Hey, maybe you can help me decipher this, I've always wondered exactly what's going on here: https://docs.python.org/2/faq/programming.html#is-it-possibl... # Mandelbrot set print (…

> List comprehensions and generator expressions have replaced all need for map, filter, and lambdas, and are far more readable.

I would say that is debatable. Coming from an FP background, I use map, filter and lambdas nearly all of the time because I find it more readable and can easily reason about the code. I have seen some two line list comprehensions and they are far harder for me to read and understand.

Re: Ask HN: Good Python codebases to read?

#114

Earlier quoted context omitted.

Spare us the half-baked hackery. List comprehensions and generator expressions have replaced all need for map, filter, and lambdas, and are far more readable. For someone new to Python, halfway through LPTHW, they don't need those things. Hey, maybe you can help me decipher this, I've always wondered exactly what's going on here: https://docs.python.org/2/faq/programming.html#is-it-possibl... # Mandelbrot set print (…

> Spare us the half-baked hackery. Ad hominem? I guess I win. > List comprehensions and generator expressions have replaced all need for map, filter, and lambdas Please explain how list comprehensions and generators have replaced the need for lambdas. >>> sorted(((x, -(x**2)) for x in xrange(10) if 0 == x % 2), key=lambda item: item[1]) [(8, -64), (6, -36), (4, -16), (2, -4), (0, 0)] Your "distaste" of functional pro…

A more idiomatic way to handle this is to arrange the items in the tuple by sort precedence, then restructure the data in the tuple after the sort.

>>> [(b, a) for a, b in sorted(((-(x2), x) for x in xrange(10) if 0 == x % 2))]

[(8, -64), (6, -36), (4, -16), (2, -4), (0, 0)]

However, ocasionally you still need a more complex sorting function. So lambdas are still handy, IMO.

Re: Ask HN: Good Python codebases to read?

#115
post #81

Earlier quoted context omitted.

requests is very useful, but it always gets mentioned as a good python codebase and I'm not sure I agree. One example: The first thing many users will do is requests.get? Which tells them that it takes some kwargs, but doesn't tell them what those kwargs are. It's easy, especially for a newcomer, to read "optional arguments that `request` takes" and fail to understand that they should look up the docs on (not-really-…

> requests.get? I was using Python for 8 years and IPython for somewhat 5 years if my memory serves me right but today I have learned that you can invoke help on an object by appending '?'. I guess I might delve into IPython documentation sometime. Thank you for lengthy and detailed explanation.

Ipython is amazing. "requests.get??" will show you the source (works on modules too). "%pdb on" is another fave to drop straight into the debugger with uncaught exceptions.

Re: Ask HN: Good Python codebases to read?

#117

Peter Norvig's examples. They are quite short and include much explanation in addition to code. They also include tests and benchmarking code. http://norvig.com/lispy.html http://norvig.com/lispy2.html (Lisp interpreter) http://www.norvig.com/spell-correct.html (Spelling corrector) http://norvig.com/sudoku.html (Sudoku solver) Also his online course Design of Computer programs includes many short, well-explained Pyth…

His coding style is not common to most writers of Python. He's using overly terse, poorly descriptive variable names, not using multiline strings for docstrings, not indenting where he should, and one-lining if/elif statements and function definitions. This style does not contribute to readability. This is not how I would want someone just learning Python to learn it.

I'm personally a fan of conditional expressions, they're easy to read IMO as long as you're not nesting them.

For instance, I find this unnecessarily difficult to read.

>>> 0 if True else 1 if True else 2

0

But breaking it up like this keeps it reasonably concise, while retaining readability.

    if True:
        a = 0 if True else 1
    else:
        a = 2

    print(a)
They can also be combined with comprehensions, which can be useful.

>>> import random

>>> ['a' if random.choice((True, False)) else 'b' for _ in range(6)]

['a', 'a', 'a', 'a', 'a', 'b']

Re: Ask HN: Good Python codebases to read?

#119

Jumping on the Kenneth Reitz train, you might check out The Hitchhiker's Guide to Python: http://docs.python-guide.org/en/latest/ He recommends the following Python projects for reading: * Howdoi ( https://github.com/gleitz/howdoi ) * Flask ( https://github.com/mitsuhiko/flask ) * Werkzeug ( https://github.com/mitsuhiko/werkzeug ) * Requests ( https://github.com/kennethreitz/requests ) * Tablib ( https://github.com/k…

I would add the Django project to that list as it's a very large, mature, and successful open source python project - https://github.com/django/django

Django's source is very high quality. Though due to the large scope of the project, there are necessarily many layers of indirection, which may be a bit daunting for someone who is just starting out.

However reading the less abstract parts may help. For instance, the paginator is pretty self contained. https://github.com/django/django/blob/master/django/core/pag...

Post reply on HN