help converting some perl code to python

Discussion in 'Python' started by eight02645999@yahoo.com, Nov 4, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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
    Sybren Stuvel, Nov 4, 2005
    #2
    1. Advertising

  3. Ben Sizer Guest

    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.
    Ben Sizer, Nov 4, 2005
    #3
  4. Peter Otten Guest

    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
    Peter Otten, Nov 4, 2005
    #4
  5. Guest

    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, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    680
    Guest
    Feb 25, 2004
  2. Replies:
    4
    Views:
    309
    Mitja Trampus
    Mar 3, 2006
  3. Python Maniac
    Replies:
    24
    Views:
    775
    Matt McCredie
    Sep 23, 2007
  4. Eric Mahurin
    Replies:
    14
    Views:
    214
    Fla As
    Dec 20, 2007
  5. Replies:
    2
    Views:
    161
    John Bokma
    Jan 23, 2007
Loading...

Share This Page