KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
The reason to do animation is caricature. Good caricature picks out the essense of the statement and removes everything else.... – Brad Bird, writer and director, The Incredibles (The Incredibles DVD, behind the scenes, character design)

Archive: November 22, 2003

← November 21, 2003November 23, 2003 →

Daily link icon Saturday, November 22, 2003

Bush turns Europe's consensus on its head

I thought this short opinion piece at the Telegraph was very good: Bush turns Europe's consensus on its head. Via Leslie.

How to convert PHP code to Python

Here's a set of find/replaces I'm doing to convert large blocks of PHP code to Python:

remove "<?php" or "?>" anywhere they appear
s/;$// #remove trailing semicolons
s/\$// #remove dollar signs
s/.=/+=/ #change concatenation to Python syntax
s/array\(/[/ #though, that's only right in some cases
s/list\(/\(/
s/\s*=>/:/
s/->/./
s/if\((.*)\){/if $1:/ #remove any parentheses around ifs
s/}// # Smiley (big smile)
s/elseif/elif/
s/else{/else:/
s/echo/print/
s/NULL/None/i
s/++/+=1/
s/is_numeric\(([^ ]*)\)/$1.isdigit()/
s/strlen/len/

And seriously, that's most of the work involved... at least, most of the tedious work. I could automate more things, but at this point you start to get diminishing returns. For instance, I could write a regular expression to change "array_key_exists(key, arr)" into Python's "key in arr", but it starts being more trouble than it's worth. Ooh, I could even change expressions using PHP's ternary operator into Python expressions, which goes something like:

foo ? bar : baz => foo and bar or baz

Although if bar's false you'll still get baz, though that's often what you want anyway. I often use PHP ternary operators to do something like isset($foo[$bar]) ? $foo[$bar] : false to avoid getting PHP's notices, for instance.

By the way, these regular expressions aren't exactly what I'm using. I'm just doing find/replaces in EditPlus; I'm not parsing the file with Perl or anything. I just used Perl syntax to explain the find/replaces I'm doing in the most concise way possible. Sorry if there are any errors. And I know, I need the "g" modifier on all of those.

CMS progress

Well, my CMS in Python is well underway. I'm now going through and converting my PHP code to Python (pretty much any other language to Python is a pleasant transition). I'm finally having to learn some of Python's libraries for doing what's built in in PHP, such as unescaping a URL, figuring out where to get the request path from, etc.

I spent the first span of time while I was working in mod_python + Cheetah to learn the system and figure out how to do away with Cheetah's errorCatcher system, which is kind of a mess. I think I have something very good that should be able to replace the existing errorCatcher system, and that should even have low enough overhead to use in production, simply because there is almost no overhead involved, in sharp contrast to using errorCatcher.

Eventually I'd like to contribute the code I've written to the Cheetah project, assuming they'll take it. Before I do that, I'll want to make sure it works fine "for real" and not just in my tests -- meaning that I run it live on my site, that the Cheetah test cases pass with it, and that I've made the necessary modifications to the Cheetah compiler.

My biggest unknown right now is how to struture my CMS under mod_python, since I'd like to persist a lot of data between requests, and some of what my CMS object does wouldn't be thread safe between requests.

Finally, boy am I glad I used my programming language neutral data markup language for the configuration in my CMS, since, as I already have a parser written in Python for it (and Perl, and Java, and C++, in addition to PHP, by the way), I was able to use the configuration data in Python without missing a beat. The configuration file parser would have been the biggest annoyance to have to rewrite out of everything in my CMS.

Update: The other thing that's a minor pain is that I have to write a lot of my own "infrastructure", like cookies, sessions, etc. At least I think... I'll have to check if Python has anything for this. Not that it's that big of a deal anyway, but it takes time.

← November 21, 2003November 23, 2003 →
October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 7 posts

Recent comments XML

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.044s.

(Used 7 db queries)

mobile phone