jdavisp3/twisted-intro

Many problems with python 3.6

Closed this issue · 13 comments

Besides the print syntax becoming print() there are anyway some problems on python3.6 (or python3.x I guess) for example you need to use .decode() and .encode() to pass bytes to a string and vice versa
In blocking-client.py
poem += data
becomes
poem += data.decode()
In blocking-client.py again
print'Got %d poems in %s' % (len(addresses), elapsed)
object of type 'map' has no len(), maybe it had one in 2.7? I don't know...
i use
print('Got %d poems in %s' % (i+1, elapsed))

But more serious
in get_poetry(sockets) from async.client.py
sock2task = dict([(s, i + 1) for i, s in enumerate(sockets)) # => {}
sockets after sockets = list(sockets) # => []
And of course async-client.py doesn't run!
./async-client/get-poetry.py 43264 43265 43266
return :
Got some poems in 0:00:00.000298
and all the clients return:

blocking-server/slowpoetry.py --port 43264 poetry/ecstasy.txt
Serving poetry/ecstasy.txt on port 43264.
Somebody at ('127.0.0.1', 33832) wants poetry!
Sending 10 bytes
Sending 10 bytes

etc

If I were stronger I would correct all that, but I admit that correcting a program using notions that I try to learn is not easy for me.

Too bad, I thought I had finally found the explanations necessary to understand asynchronous programming and exactly in the field I was looking for: networks.
And it is difficult to work in 2.7, the asyncio library having been largely modified between 2.7 and 3.6

True! I wrote this for Python 2 and adding Python 3 support would be a lot of work. Also, it would probably make more sense to focus on the new asyncio stuff at first rather than Twisted. But, alas, I simply don't have time these days. Hope you find what you are looking for.

Too bad, this series of articles looked super well written...

Thanks, I happen to think it was too 😄

It's never too much!!! :)

kvaml commented

Would you be interested in pulling python3 versions of the programs? I do not guarantee I'll do them all, but I have been changing the .py files to Python3 syntax as I've been going through your code examples. I see no reason to preserve Python2 compatibility.

Would you be interested in pulling python3 versions of the programs? I do not guarantee I'll do them all, but I have been changing the .py files to Python3 syntax as I've been going through your code examples. I see no reason to preserve Python2 compatibility.

For sure! Appreciate any contributions you'd like to make.

kvaml commented

Excellent! If you are interested in learning more about git I highly recommend the free labs at lab.github.com.

kvaml commented

I have converted the first 10 lessons. They are in the py3 branch at
https://github.com/kvaml/twisted-intro/commits/py3
I will send a pull request when I finish.

The conversion to Python3 highlights the switch between bytes on the network and unicode when working with the poems. I tried to put the bytes.decode('utf8') at the transition between twisted code and the application that wants poetry. You might want to review the tutorial where you use "strings" somewhat interchangeably between byte and unicode strings which are now distinct.

The tutorial is fun, engaging, and very well thought out.

Nice, glad you are enjoying it!

I think we can resolve this, would you agree @kvaml ?

kvaml commented

Yes, this is resolved.

Your tutorial is excellent and now you have the Python 3 code to support it.

Thanks @kvaml !