Archive for the 'Python' Category

02/07 w00t!

root@fic-gta01:~$ python hangul.py
Python 2.5.1 (OpenEmbedded build) sitecustomize.py active.

가와세 나쓰나(川瀬夏菜,9월 8일생)는 일본의 만화가이다. 히로시마현 출신, O형. '미아의 행방(迷子の行方)' (「LaLa DX」2000년 11월호)으로 데뷔하였다. 다네무라 아리나의 어시스턴트로 일한 경력이 있다고 한다.

KA-WA-SEY- NA-SSU-NA-(川瀬夏菜,9WEL- 8IL-SAYNG-)NUN- IL-PON-YI- MAN-HWA-KA-I-TA-. HI-LO-SI-MA-HYEN- CHWUL-SIN-, OHYENG-. 'MI-A-YI- HAYNG-PANG-(迷子の行方)' (「LaLa DX」2000NYEN- 11WEL-HO-)U-LO- TEY-PWI-HA-YESS-TA-. TA-NEY-MWU-LA- A-LI-NA-YI- E-SI-SU-THEN-THU-LO- IL-HAN- KYENG-LYEK-I- ISS-TA-KO- HAN-TA-.

Doesn’t look like much, but this is some Python code I wrote for someone, running *on* my Neo1973. Sweet…

01/23 identicons

   

I am adding my entry to identicon third party implementations. I followed more or less Don’s algorithm, and, using the PIL, I have now a functioning identicon lib in Python. The results are not as good as the originals, but it should give you an idea. If you perfect it, I’ll welcome back changes.

Download the source code.

identicon Python

03/31 Tomabaem online light

On the side bar you’ll find a small form that accepts one chinese character, aka sinogram, and will return in the box below that the readings [Cantonese, Mandarin, Japanese, Korean and Viêtnamese] and meanings for this sinogram. This stuff was pulled from the UniHan database, as explained at the Tomabaem main page. Tomabaem Online is a derivate of the desktop app, and is built on indexes of the database that are made offline. Then, a little Python [thanks to 2.4’s unicode codecs, conversion from UTF-16 to UTF-8 is seamless] and grep later, and we have a winner! Which reminds me, I haven’t updated the indexes for a while, I shall do that now. I wrote an RB app that runs 11 threads [must. resist. temptation. to. rewrite. it. in. Erlang.] and the main routine reads the UniHan db and sends text to each thread depending on its content. Here’s a sample:

U+3400 kCantonese jau1
U+3400 kDefinition (same as U+4E18 丘) hillock or mound
U+3400 kHanYu 10015.030
U+3400 kIRGHanyuDaZidian 10015.030
U+3400 kIRGKangXi 0078.010
U+3400 kIRG_GSource KX
U+3400 kIRG_JSource A-2121
U+3400 kIRG_TSource 6-222C
U+3400 kMandarin QIU1
U+3400 kRSUnicode 1.4
U+3400 kSemanticVariant U+4E18
U+3400 kTotalStrokes 5

U+3400 gives me the sinogram’s codepoint, and depending on the kXXXXX tag, I pass each line to a thread storing the data in a separate file. The whole process takes two and a half minutes, and I guess I could do faster. But 160 seconds for 25MB of data is fast enough. So now the indexes are up to date :-) I need to update Tomabaem’s own db, will do that later.

dda> time ./threadIndexer/threadIndexer
Thread kSimplifiedVariant starting...
Thread kTraditionalVariant starting...
Thread kCantonese starting...
Thread kJapaneseOn starting...
Thread kJapaneseKun starting...
Thread kKorean starting...
Thread kMandarin starting...
Thread kVietnamese starting...
Thread kRSKangXi starting...
Thread kTotalStrokes starting...
Thread kDefinition starting...
Done dispatching in 150,204,884µs
Saving kSimplifiedVariant.idx
Saving [...]
Thread kSimplifiedVariant finished...
Thread [...] finished...

real    2m39.733s
user    1m54.760s
sys     0m6.600s

So. Okay, most of the time is spent dispatching, I could improve that…

03/09 Programming, 빨리빨리, and Translation

Appart from a couple of inaccuracies, this is a good translation of Teach Yourself Programming in Ten Years by Peter Norvig. That a document which starts with the question Why is everyone in such a rush? was translated into Korean – and the link to that translation is at the top of the list… – is definitely amusing, since we know the maladive penchant our [ex-]hosts have for speed over accuracy. If you’re learning Korean, it’s a good sample to work from.

Learn at least a half dozen programming languages. Include one language that supports class abstractions (like Java or C++), one that supports functional abstraction (like Lisp or ML), one that supports syntactic abstraction (like Lisp), one that supports declarative specifications (like Prolog or C++ templates), one that supports coroutines (like Icon or Scheme), and one that supports parallelism (like Sisal).

Hmmph. I guess that list could use some upgrading, since the hype languages of the 21st Century are mostly not in there, and at least one language quoted here [Icon] is not developed any longer – which is a shame, I like Icon… As for Prolog, was it ever alive? ;-)

Python will match a few categories here, my favorite, RB, too, Erlang will cover a lot of ground for you here – not that I am conversant [yet!] in Erlang, but I’m learning! – and while C may come up short, most of these languages were/are written in C. Of course, if you aim to achieve stable and well-performing concurrent programming in C, you’ll end up with Erlang, as they say… :D I don’t think I’ll ever wrap my head around Lisp/Scheme – not that I didn’t try… – and I find Java and C++ repulsive and offending, but it’s probably just a matter of taste and education [or lack thereof…?].

But in the end, be it computer languages or human ones, 10 years is a minimum. Practice makes good, and education alone won’t take you very far [case in point: the profs I had at the Uni. You could tell those who had practiced their language skills and those who had kept their knowledge at the academic level…].

Anyway, this is an attempt at tagging

12/04 import in Python

Seen this on a French Linux mailing list:

marque.py

class Marque:
  def nom(self):
    nom="une marque"
    return nom

voiture.py
class Voiture:
  import marque

  def presentation(self):
    informations = "Voiture:n"
    mamarque = marque.Marque()
    informations = informations+mamarque.nom()+"n"
    return informations

mavoiture = Voiture()
print mavoiture.presentation()

Running voiture.py gives this:
$ python voiture.py

Traceback (most recent call last):
  File "voiture.py", line 11, in ?
    print mavoiture.presentation()
  File "voiture.py", line 6, in presentation
    mamarque = marque.Marque()
NameError: global name 'marque' is not defined

D’oh. The culprit is here:

class Voiture:
  import marque

Doing the import inside the class definition makes marque local to Voiture, whch means that

mamarque = marque.Marque()

should be rewritten as

mamarque = self.marque.Marque()

Or better, move that import at the top of the script…

Just a little Sunday Python gotcha…