Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
help converting some perl code to python
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
eight02645...@yahoo.com  
View profile  
 More options Nov 4 2005, 4:01 am
Newsgroups: comp.lang.python
From: eight02645...@yahoo.com
Date: 4 Nov 2005 01:01:50 -0800
Local: Fri, Nov 4 2005 4:01 am
Subject: 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sybren Stuvel  
View profile  
 More options Nov 4 2005, 5:07 am
Newsgroups: comp.lang.python
From: Sybren Stuvel <sybren...@YOURthirdtower.com.imagination>
Date: Fri, 4 Nov 2005 11:07:56 +0100
Local: Fri, Nov 4 2005 5:07 am
Subject: Re: help converting some perl code to python
eight02645...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Sizer  
View profile  
 More options Nov 4 2005, 5:17 am
Newsgroups: comp.lang.python
From: "Ben Sizer" <kylo...@gmail.com>
Date: 4 Nov 2005 02:17:06 -0800
Local: Fri, Nov 4 2005 5:17 am
Subject: Re: help converting some perl code to python

eight02645...@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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Nov 4 2005, 6:47 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 04 Nov 2005 12:47:49 +0100
Local: Fri, Nov 4 2005 6:47 am
Subject: Re: help converting some perl code to python

eight02645...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jgardner@jonathangardner. net  
View profile  
 More options Nov 4 2005, 10:33 am
Newsgroups: comp.lang.python
From: "jgard...@jonathangardner.net" <jgard...@jonathangardner.net>
Date: 4 Nov 2005 07:33:21 -0800
Local: Fri, Nov 4 2005 10:33 am
Subject: Re: help converting some perl code to python
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google