Main      Site Guide    
Message Forum
Re: Python Question
Posted By: Sam, on host 24.61.194.240
Date: Wednesday, July 3, 2002, at 05:06:28
In Reply To: Re: Python Question posted by Don the Monkeyman on Tuesday, July 2, 2002, at 16:52:22:

> A good example/question comes to mind: In your opinion, how easy would it be to write that SSI interpreter script in Python?

Easy. Pretty much all the functionality I think that uses maps directly to functionality available in Python.

Random thing that occurs to me you may find useful which is not necessarily immediately obvious: Perl-style regex processing (anything with the =~ or !~ operators in Perl) is available in Python in the "re" module. Import that, then look at the link below.

Example uses:

import re
str = "This rules."
if re.search("r[aeiou]le",str):
_______ print "Woo-hoo!"
else:
_______ print "Bummer, dude."

The same thing, better:

import re
str = "This rules."
matchobject = re.search("r[aeiou]le",str)
if matchobject:
_______ print "Woo-hoo!"
_______ start = matchobject.start()
_______ end = matchobject.end()
_______ print "The substring matched was: " + str[start:end]
else:
_______ print "Bummer, dude."

The "str[start:end]" bit is a slicing operation, which extracts the substring of "str" that starts at character number "start" and ends just before character number "end".

To mimic Perl's s///g:

replacement = re.sub('rule','stink','This rules!')

...after which "replacement" is set to 'This stinks!'


Regex processing is one of Perl's strongest features, and its counterpart in Python, being stuffed away in a module rather than integrated into the language syntax, isn't always immediately obvious, so I figured I'd direct you to it.


Link: re module API

Replies To This Message

Post a Reply

RinkChat Username:
Password:
Email: (optional)
Subject:
Message:
Link URL: (optional)
Link Title: (optional)

Make sure you read our message forum policy before posting.