Archives for the ‘Python’ Category

Guide for Python Newbies

Gleaned from Usenet

Varargs in Python functions
Encoding HTML Entities in Python
True And False
Ten sample simple introductory python programs to get you started

Paraguay TLD for Python?

I didn’t realize that .py TLD is for Paraguay until today.
You know, that could be really cute for Python software repositories, like Cheeseshop.com.py.

A Joke In the Python Interpreter

>>> import this

>>> love = this
>>> this is love
True
>>> love is True
False
>>> love is False
False
>>> [...]

More Warts in Python Exceptions

I’ve previously blogged about a little Python wart in the way exceptions differ from the way it’s raised.
Here’s another one that may surprise you, especially if you are writing long running processes like servers. But I have a real love/hate relationship with it. On one hand, the behavior makes Python really sweet to work with, [...]

Catching Custom Python Exceptions

Python is often described as a language which doesn’t “surprise” people with strange edge cases.
Here’s one for you. Don’t peek at the answer, and try to predict the result.

>>> class NewException(Exception): pass

>>> try:
… raise NewException, ‘Error Message’
… except NewException, message:
… pass
>>> print message
???? you answer [...]

Python Threading Tutorials

(Note: This content was migrated from http://teyc.editthispage.com)
Python has a high level threading module, but there has never been much specific tutorials on how to use it.
Just to get you started, here’s a code example:

import thread

def background(param1, param2, param3):
print “Background thread called with %r %r %r” % (param1, param2, param3)
[...]

Python Tip: Escaping Strings to C-style encodings

Wai Yip Tung contributed a useful snippet in the Python Cookbook for escaping non printable characters in python to C-style equivalents. It uses an undocumented part of the standard python library. Very neat.

>>> a=”ABC\\nDEF”
>>> print a.encode(’string_escape’)
ABC\\nDEF
>>>

PullDOM with Python Tutorial/Documentation or Code Sample

The PullDOM documentation is pretty dismal, especially because it’s a library worthwhile learning. If you have to process a large XML document (having a huge DOM tree can consume a lot of memory), but prefer not to use a SAX style interface, because the code can be difficult to understand, then PullDOM is your [...]

Python in the Enterprise

James Governor points to a report how Twisted Python is used by ITA software in the airline industry.
By the way, if you haven’t added James’ RSS feed to your feed reader, you should. His posts brings a lot of mundane programming tasks back into perspective.

Continuations and BASIC programming

I came across this very nice through the web programming in BASIC (demo made in Wink). Now, hopefully Guido can see why Python needs continuations? The developer has an affinity for Smalltalk, and you can see that he’s really leveraged it well here.
Also check out http://runbasic.com.

The little csv module that could

The CSV module in python can process more than just CSV files. It can also handle tab separated ones.
In fact, it has an in-built sniffer to guess what type of delimiters the file is using. This includes guessing whether strings are quoted or not.
Here’s some sample code:

dialect=csv.Sniffer().sniff(open(’fitted.txt’, ‘r’).read())
for column_1, column_2, column_3 in \
[...]

Cisco and Twisted Python

Cisco acquires IronPort (Source : Network World)

Full Text Indexing in Ruby is Faster than Java

Jim Wiseman John Wiseman, who is porting Ferret to Lisp, reports on the blazing performance of Ferret.
(Ferret was originally a port of Lucene, but recent alteration to the file format has increased performence 5x over GCJ, and then the author ported to C to get an order of magnitude improvement.)
The moral of the story is [...]

Software for Bulgarian Asparagus Farmers

You have to read the story behind this quote about developing software on top of the .Net 3.0 framework:

“I want to develop for the .NET 3.0 framework”
I don’t think you could find a smaller market segment if you tried. Perhaps writing software for Bulgarian asparagus farmers?
Joel On Software Discussion Group

Atom WSSE Profile

It’s been a few years since Mark Pilgrim wrote up the WSSE authentication on XML.com. There has been little exposition of it, bar Ezra from MovableType who explained that the OASIS scheme requires passwords to be stored in clear text on the server. The WSSE scheme doesn’t really define any extensions which specify that a [...]

Beware of Property Descriptor and __getattr__

What’s wrong with this code?

class C:

def __init__(self, id):
self.id = id

def getdata(self):
if not hasattr(self, ‘_data’)
temp = longComputation()
self._data = temp.value()
return self._data

def __getattr__(self, name):
[...]

Profiling Javascript

Why in the world would someone want to profile Javascript? For goodness sake, it’s hard enough trying to debug it. Well, Brendan Gregg gives compelling reason:
But now JavaScript, as part ofAJAX, isdriving new and serious Web 2.0 applications.Google Maps is one of what may be many new (and very cool) applications to embrace these technologies.
Ways [...]

Strongtalk Is Open Sourced

Strongtalk is a version of Smalltalk developed with speed in mind. It was developed by a startup but the company was bought by Sun to work on the JVM before Strongtalk was productized.
Strongtalk allows optional static typic. Interestingly, the static typing information is not used by the optimizer to produce fast code. Instead, Strongtalk [...]

Serving Large Files with Zope

Here are some timings using asyn_http_bench before and after a small change was applied to ZServer/HTTPServer.py:
asyn_http_bench myhost 80 /myurl 3 3
/myurl returns a string of varying length (10Mb, 20Mb, etc)

Size
Before
After

10 Mb
26.5 sec
9.8 sec

20 Mb
88.4 sec
20.47 sec

30 Mb
194.6 sec
30.3 sec

40 Mb
connection errors
40.2 sec

$ diff -c HTTPServer.py.ZopeVendor HTTPServer.py.ZopePatch27
*** HTTPServer.py.ZopeVendor Thu Sep [...]

What it means to be innocent

To be innocent, is to be of clean hands, not in the eyes of the law, or the eyes of the bully, but in the eyes of God.
If your neighbour is a bad man, I can see no justification for you to be punished. The police should take due care when pursuing criminals that your [...]