Live data from Hacker News

Think Python, 3rd Edition

allendowney.github.io

51–60 of 126 posts

Re: Think Python, 3rd Edition

#51

What are some books for mid to advanced programming in Python? I already know Puthon and programming in general but want to improve my Python skills. I know only Fluent Python which I'm currently reading, and CPython Internals.

You might want to consider books that show application of techniques in real world practical code.

For example, Effective Pandas 2 illustrates common patterns for dealing with tabular data. Along the way, it uses comprehensions, lambdas, unpacking, etc. Shows how to use pytest to refactor. Leverage visualization to understand data.

(Disclaimer: I'm the author)

Re: Think Python, 3rd Edition

#52
post #10

This book gets overlooked in favor of other ones such as Python Crash Course but I really enjoyed reading through Think Python 2e and will read through this version as well. Check out the rest of his books on Green Tea Press.

I also like PCC. What do you think the pros and cons are of PCC v. TP3e? They are really two different types of books in terms of pedagogy.

Python Crash Course teaches Python while Think Python teachers computer science using Python. You are right that they are two different books but people recommend Python Crash Course for beginners when Think Python is a much better recommendation for them in opinion. If you just need to learn Python then PCC is a great book.

Re: Think Python, 3rd Edition

#53

Apologies for changing the subject, but aside from real world experience (which I have and am getting at work), is there a resource of similar quality for more intermediate/advanced Python programmers? I always feel like there's a big chunk of the language or stdlib I do not know.

It’s a bit older, but I learned a lot from “Writing idiomatic Python”. Honorable mention to “the little book of Python antipatterns” as well.

Re: Think Python, 3rd Edition

#54
post #35

How does one do proper concurrency in Python? As in separate "processes" running with encapsulation, fault tolerance, etc. For example, how does one bundle up a TCP client in a "process" such that the process handles failed connections, broken connections, retrieving data at periodic intervals (relatively fast), receiving "messages" from other "processes", etc.? There's Ray, Pyro, Pykka, Celery, multiprocessing, asyn…

Can you give an example of what you consider a good solution in a different language? That will help us see exactly what you're looking for and how/whether it might be achieved in Python.

Erlang and Elixir. :)

Re: Think Python, 3rd Edition

#55
post #39

Earlier quoted context omitted.

You mean types, right? Typing is what you do on a keyboard. If you want to understand type systems, Types and Programming Languages ( https://www.cis.upenn.edu/~bcpierce/tapl/ ) is the book most people start with. If that is too advanced for you, PLAI ( https://www.plai.org/ ) is a gentle introduction to programming language theory which includes type systems.

Those books are great; though I'm not sure whether they are good for a beginner? Getting your feet wet by programming in a few reasonably typed languages might be better, before you tackle TAPL? (By 'reasonably typed' I mean something like Haskell, OCaml, even TypeScript or Facebook's hack. But not Go, C++ or Java.)

If you want something JavaScript-ish with strong type inference, take a look at ReScript (https://rescript-lang.org/)

Re: Think Python, 3rd Edition

#56
post #48
post #35

How does one do proper concurrency in Python? As in separate "processes" running with encapsulation, fault tolerance, etc. For example, how does one bundle up a TCP client in a "process" such that the process handles failed connections, broken connections, retrieving data at periodic intervals (relatively fast), receiving "messages" from other "processes", etc.? There's Ray, Pyro, Pykka, Celery, multiprocessing, asyn…

If you are I/O bound then asyncio or good old fashioned gevent will do great. If you are CPU bound, then use multiprocessing. If you need to accept "jobs" from elsewhere, use RabbitMQ (with or without Celery). If you have mixed CPU/IO workload that fits the worker pattern, then you would do all 3. At the top level you have a RabbitMQ consumer, fetching jobs from a remote queue and then putting these into a multiproce…

So do you recommend a single Python process running asyncio or multiprocessing or both? Or do people normally split these things amongst several Python processes?

My understanding is that multiprocessing creates multiple interpreters but that it still comes across some GIL issues if all under the same Python process.

I am in general quite comfortable with the actor model, and I would ideally use Erlang/Elixir here, but I can't for various reasons.

Re: Think Python, 3rd Edition

#57
post #48
post #35

How does one do proper concurrency in Python? As in separate "processes" running with encapsulation, fault tolerance, etc. For example, how does one bundle up a TCP client in a "process" such that the process handles failed connections, broken connections, retrieving data at periodic intervals (relatively fast), receiving "messages" from other "processes", etc.? There's Ray, Pyro, Pykka, Celery, multiprocessing, asyn…

If you are I/O bound then asyncio or good old fashioned gevent will do great. If you are CPU bound, then use multiprocessing. If you need to accept "jobs" from elsewhere, use RabbitMQ (with or without Celery). If you have mixed CPU/IO workload that fits the worker pattern, then you would do all 3. At the top level you have a RabbitMQ consumer, fetching jobs from a remote queue and then putting these into a multiproce…

Why some people are extremely averse to RabbitMQ?

I saw that at one company having RabbitMQ / Celery setup - every time a new software engineer comes in, they complain about RabbitMQ and ask why would company use it. The infrastructure was running like this without hiccups for years. At one point company has let go of many experienced engineers and this time one developer found some issue with the code and blamed it on rabbit as it was locking the queue. There were no more senior developers to contest it, so he convinced manager to swap it out for Redis. He took about two months to rewrite it. Surprise, the same issue existed on Redis. The Redis solution works fine, but has its own limitations...

Re: Think Python, 3rd Edition

#60
post #46
post #35

How does one do proper concurrency in Python? As in separate "processes" running with encapsulation, fault tolerance, etc. For example, how does one bundle up a TCP client in a "process" such that the process handles failed connections, broken connections, retrieving data at periodic intervals (relatively fast), receiving "messages" from other "processes", etc.? There's Ray, Pyro, Pykka, Celery, multiprocessing, asyn…

Use asyncio. Bind to a socket, set blocking False, then asyncio.run(on_new_connection(sock)). Inside that coroutine get the loop and await loop.sock_accept(sock) And then asyncio.create_task(on_connection_data(connection)). The only gotcha is you need to keep a reference to that task so it doesn't get garbage collected.

Get garbage collected by what? Doesn’t Python use reference counting?
Post reply on HN