Updates from Python SVN, Part 6
urllib2 does not raise exceptions if the HTTP Status Code is a 2xx one. Previously it checked only for response code 200 and 206.
urllib2 does not raise exceptions if the HTTP Status Code is a 2xx one. Previously it checked only for response code 200 and 206.
This is not about who’s right or wrong, it’s about me still laughing
This is an addition of the post Producer/Consumer in Python I wrote earlier.
It’s just the same example rewritten with py.magic.greenlet.
Greenlets, tasklets, micro-threads, coroutines are basically the same thing: suspending and resuming a function in multiple points is the core feature we need.
In the previous example we did it with plain old generators, now we try to use the greenlets in py library.
I won’t comment the code line by line because nothing really changes except the API of the library involved. In our case the key feature is greenlet.switch() which switches from one greenlet to another. That’s really it.
You can find the full example here: prodcons-greenlet.py
The list of picklable objects has a new citizen: slices.
There’s also a new method, chgat() in the curses module to change the font style of certain characters.
You can use the string’s translate() method to delete a series of char in one shot without translating, just pass None to the table argument:
Last but not least there’s a change in how __ slots __ works. I think it’s better to cite directly the documentation and the code instead of inferring in this case:
When __slots__ are set to a unicode string, make it work the same as setting a plain string, ie don’t expand to single letter identifiers.
That was not really clear to me at the first glance so I think it’s safer to report some code from the tests also to explain it better:
Like Hello World in imperative programming the Producer/Consumer problem is one of kind of program everybody has wrote once in a lifetime.
It’s one of the toy examples in concurrent programming demonstrating the basics of synchronization.
Basically there’s a shared buffer, a producer entity and a consumer one. The producer generates some data and puts it in the buffer (which can have one or multiple slots), then the consumer gets the data from the same buffer and does something with it.
Now we’re going to see a couple (and half) implementations in Python. I bet there are tons of ways to solve this little problem and hence I’ll show you a threaded implementation with locking around around data structures which are not thread safe, a slight modification using Queue objects and an alternative implementation using just simple generators.
I just released another version of Pinder, my Campfire API.
The major news is the ability to read the online transcripts like this:
See Campfire.transcripts(), Room.transcripts() and Room.transcript() for the details.
Here what’s new:
Campfire objects now have rooms() and rooms_names() methods to get the list of the associated room objects and the names of all the rooms
Campfire objects also have find_or_create_room_by_name(), an helper method which combine find_room_by_name() and create_room()
The whole library has been updated to httlibp2 0.3.0
A proper user agent is sent during the requests
Room objects now have guest_access_enabled() to know if the guests can enter that room
The support for transcripts has been added throughout the library. See the changelog for details.
I decided to write this post just to get things straight and recall some memories of the past.
Since I still get emails about Atom.NET I think it’s time to say it out loud and clear: I don’t care about it anymore but I still think Atom is a good idea, though
I repeat: I don’t have anything, whatsoever, to do with the library anymore. I sold it years ago to ToolButton Inc. so please just ask them for support or if they’re gonna release something anytime soon.
The only reason why I’m still listed in the SourceForge project page is because I can’t remove myself.
Now let’s recall those memories I was talking about.
I started Atom.NET at the end of 2003. At that time I was foolishly hooked up by things like Microsoft .NET and XML. Now I think that’s one of the worst matches ever but let’s not digress…
In 2003 MS .NET was two years old I think and I was on my way to change my desktop operating system from Windows to Debian Linux but one day the hype got me and I gave C# a try and soon after that I postponed my switch.
Maybe I was searching for a reason to stick with Windows (which I didn’t really like anyway) and since the unborn Ubuntu or Apple’s OSX were not on my radar yet, I stopped the clock and gave the new language a try.
I soon became one of the most enthusiastic contributor in the Italian .NET user group jumping from one conference to another, from one workshop to another and post to newsgroups or forums and post and post and learn and post.
I even happened to receive a kind letter from Microsoft Italy for my contribution with a nice book bundled to drain the thirst of knowledge I had.
.NET was all I was searching for to develop applications on Windows. But some things were thankfully going to come in the way: Python, OpenSource and the healthy part of my brain.
In the second quarter of the year 2003 I did find a job as a .NET developer in a Windows oriented Web firm (I said it all) and although I wasn’t really shining as a .NET coder due to the newbieness on the field they hired me right away to create ASP.NET custom controls and something else.
The people there were mostly great and young but I soon realized my way of thinking was miles away theirs. They didn’t really care about cross platform, standards support, Linux, OpenSource, Python and I started feeling bad with myself so I guess my performances at work dropped down and after a while I resigned. It was a summer job anyway so no one complained.
That experience taught me something: never, never ever use only proprietary technology that can lock yourself in somewhere unless you have no other choice. At least this is Lawrence’s opinion.
I know there’s gazillions of people perfectly happy with proprietary stuff and I’m too because I use a Macintosh now and I’m neither a Microsoft zealot nor a FOSS one. I choose technologies according to my personal tastes, technical reasons and adherence to public domain standards.
I didn’t, by the way, stop to code in C# but I had been hired in a FOSS based startup to work with Python, Linux and all the things I technically enjoyed the most. So my language for fun became the one to work with and C# replaced it (with not the same fun ratio) because of what’s going to become the first library to handle Atom in the .NET world.
Atom.NET was already in my mind and on paper but I honestly admit I don’t remember the exact moment when I started coding it.
The reason why I did that was to learn something more from .NET, make use of the many books I had (most of them not purchased by me) and bring Atom to the .NET masses. They were the days of the rising of the Atom syndication format so I joined the bandwagon and created a library to handle Atom 0.3 (1.0 came later in time).
I think the first implementation was based on .NET’s XmlReader, a pull parser, but later I switched to XPath and a little plugin architecture.
I made the first public release in January 2004 with only a feed generator, then after a month I wrote the first parser, then after 20 days I rewrote it basically from scratch introducing a plugin architecture, using XPath and something else I don’t remember. I also enhanced my library with some custom XML stuff made by MS MVPs sending a couple of patches.
The latest public release was in July 2004 (I was already caught up in the Python world).
I really felt better after leaving the dotnetty-we-all-think-xml-is-the-cure world.
A year and a half later a Canadian private company, Toolbutton Inc., offered me money to sell them the library and the intellectual property. Since I had no use of it or .NET anymore I soon agreed and in December 2005 Atom.NET was no longer mine.
I could have sacrificed a couple of lambs to the gods but I thought it was too much
The library before the acquisition ended up in various places: an Xml SDK, FeedJumbler, inside a feed reader and various articles. It was quite a success to be the first project of a .NET newbie. It lasted one year and a half.
One year and half without any kind of community feedback. That was absurd because I had tons of download and usage testimonies. But hey, I didn’t care and I surely do not care now. The MS .NET community didn’t get the OpenSource spirit and it seems they still don’t get it very well.
I’m now happily using other technologies and you all know what I’m talking about.
My blog has been linked from Reddit. I definitely had an unusual peak in the logs
I collected the posts about what’s new in Python-SVN in a single category. This way they can be found more easily.
I’m planning on doing regular updates, once a week or so, until I’ll fed up with Python and change main language but that’s not going to happen anytime soon (at least I think, who knows).
The most relevant change since the last update is the new function socket.create_connect(). It’s an helper function easing the creation of a connected socket to a given address (the usual host+port tuple) with an optional timeout.
The function is currently being used throughout some network related modules in the standard library.
Its implementation is very straightforward:
loop over getaddrinfo's sequences: create the object socket set the timeout if any connect to the remote address return the connected socket if everything went well
At this time it’s being used in ftplib.py, httplib.py, telnetlib.py, poplib.py and smtplib.py.
Thanks to that you can set a timeout directly during the instantiation of these classes: HTTPConnection, FTP, POP3, SMTP and Telnet.
A new kind of temp file in tempfile.py also appeared in the stdlib: SpooledTemporaryFile.