473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help converting some perl code to python

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

Nov 4 '05 #1
4 2327
ei***********@yahoo.com 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
Nov 4 '05 #2
ei***********@yahoo.com 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.

Nov 4 '05 #3
ei***********@yahoo.com 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

Nov 4 '05 #4
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

Nov 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
5
by: Mothra | last post by:
Hi All, I am the current author of the Astro-Sunrise perl module http://search.cpan.org/~rkhill/Astro-Sunrise-0.91/Sunrise.pm and was wondering if it would be worth while to convert it to...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
7
by: ChiTownBob | last post by:
I'm trying to convert our documentation over to the Wikipedia format (MediaWiki), and about ready to give up on the Perl-based method I found (because Perl just sucks, as all good Python hackers...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
4
by: eight02645999 | last post by:
hi i have some regular exp code in perl that i want to convert to python. if $line =~ m#<(tag1)>(.*)</\1># { $variable = $2; }
12
by: rurpy | last post by:
Is there an effcient way (more so than cgi) of using Python with Microsoft IIS? Something equivalent to Perl-ISAPI?
4
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.