Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > help converting some perl code to python

Reply
Thread Tools

help converting some perl code to python

 
 
eight02645999@yahoo.com
Guest
Posts: n/a
 
      11-04-2005
hi
i need help with converting a piece of perl code to python
......
my $start = '\[start\]';
my $file = '\[files\]';
my $end = '\[end\]';
.....

while(<FILE>) #open a file
{
if ( /$start/ .. /$file/ ) { # if line match [start] till
[files]
do something with $_
}
if (/$file/ .. /$end/ )
{
do something with $_
}
}

The file looks something like:

[start]
....
[files]
....
[end]

the problem is the '..' operator in perl. Is there any equivalent in
python?
any suggestions ?
thanks

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      11-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with:
> the problem is the '..' operator in perl. Is there any equivalent in
> python? any suggestions ?


I have a suggestion: stop assuming we know perl, and explain what this
'..' operator does.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
 
Reply With Quote
 
 
 
 
Ben Sizer
Guest
Posts: n/a
 
      11-04-2005
(E-Mail Removed) wrote:
> the problem is the '..' operator in perl. Is there any equivalent in
> python?


I can't think of anything with a similar operation, to be honest. I'd
try using while loops which look out for the next section delimiter.

--
Ben Sizer.

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      11-04-2005
(E-Mail Removed) wrote:

> i need help with converting a piece of perl code to python


> the problem is the '..' operator in perl. Is there any equivalent in
> python?


Here is a class that emulates the .. operator:

<code>
import sys
import re

start, files, end = map(re.escape, ["[start]", "[files]", "[end]"])

class Section(object):
def __init__(self, start, end):
self.start = re.compile(start).match
self.end = re.compile(end).match
self.inside = False
def __contains__(self, line):
result = self.inside
if result:
if self.end(line):
self.inside = False
else:
if self.start(line):
result = self.inside = True
return result

first = Section(start, files)
second = Section(files, end)
for line in sys.stdin:
line = line[:-1]
if line in first:
# your code
if line in second:
# your code
</code>

However, the simpler

<code>
#untested
import sys

start, files, end = "[start]", "[files]", "[end]"
keys = set([start, files, end])

key = None
for line in sys.stdin:
line = line[:-1]
if line in keys:
key = line
elif key == start:
# your code
elif key == files:
# your code
</code>

might work even better because 'your code' doesn't get to see the sections'
begin/end markers.

Peter

 
Reply With Quote
 
jgardner@jonathangardner.net
Guest
Posts: n/a
 
      11-04-2005
The '..' operator is the flip-flop operator in perl. (It is rarely
used.) It is exactly the same as the 'range' type operator. It returns
false until the first condition is met, then it returns true until the
last condition met, then it returns false.

You could create a flip-flop with a python closure (t_cond and f_cond
are functions that take a value and return True of False)

def make_flip_flop(t_cond, f_cond):
state = [False]
def flip_flop(val):
if state[0] and f_cond(val):
state[0] = False
elif not state[0] and t_cond(val):
state[0] = True
return state[0]
return flip_flop

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting some autogenerated ruby code to C Eric Mahurin Ruby 14 12-20-2007 05:22 PM
I could use some help making this Python code run faster using only Python code. Python Maniac Python 24 09-23-2007 05:48 PM
Can some one help to translate Python code to Perl? wnjayantha@yahoo.com Perl Misc 2 01-23-2007 06:21 PM
help in converting perl re to python re eight02645999@yahoo.com Python 4 03-03-2006 07:17 PM
Need some help for some perl homework.... Perl 0 02-25-2004 01:45 AM



Advertisments