KBD

Keith Devens .com

Thursday, August 28, 2008 Flag waving
"To know what *would* have happened, child?" said Aslan. "No. Nobody is ever told that." – C.S. Lewis (Aslan, in Prince Caspian, Chapter 10)
← CMS progress Bush turns Europe's consensus on its head →

Daily link icon Saturday, November 22, 2003

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 Bush turns Europe's consensus on its head →

Comments XML gif

Basil Crow wrote:

Don't you think it's time to stop showing "PHP Rocks" on the bottom? :-)

∴ Basil Crow | 22-Nov-2003 9:47pm est | #3328

Keith (http://keithdevens.com/) wrote:

Smiley Could be. I'll replace that button with a Python button once I get my Python site up.

Keith | 22-Nov-2003 9:58pm est | http://keithdevens.com/ | #3330

Mean Dean (http://www.healyourchurchwebsite.com) wrote:

Hmmm ... given some time, I believe I could figure out a single line of Perl to make the convert work ..

... sorry, couldn't resist.

∴ Mean Dean | 22-Nov-2003 10:55pm est | http://www.healyourchurchwebsite.com | #3331

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



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

Recent comments XML

new⇒Girls, please don't get breast implants

http://war-11.ucufyod.net​http://jewelry-26.wohiles.net​http://games-52.ruz...

Randolph: Aug 28, 5:16am

Generated in about 0.137s.

(Used 8 db queries)

mobile phone