How to determine if a line of python code is a continuation of the line above it

Discussion in 'Python' started by Sandra-24, Apr 8, 2006.

  1. Sandra-24

    Sandra-24 Guest

    I'm not sure how complex this is, I've been brainstorming a little, and
    I've come up with:

    If the previous line ended with a comma or a \ (before an optional
    comment)

    That's easy to cover with a regex

    But that doesn't cover everything, because this is legal:

    l = [
    1,
    2,
    3
    ]

    and with dictionaries and tuples as well.

    Not sure how I would check for that programmatically yet.

    Is there any others I'm missing?

    Thanks,
    -Sandra
    Sandra-24, Apr 8, 2006
    #1
    1. Advertising

  2. Sandra-24

    Dan Sommers Guest

    On 8 Apr 2006 11:24:04 -0700,
    "Sandra-24" <> wrote:

    > I'm not sure how complex this is, I've been brainstorming a little, and
    > I've come up with:


    ["This" meaning how to determine if a line of python code is a
    continuation of the line above it.]

    > If the previous line ended with a comma or a \ (before an optional
    > comment)


    A line ending with a comma does *not* indicate a single statement spread
    out over two lines:

    a = 1,
    print a,
    a = [ ]

    None of those lines is a continuation of the line above it.

    > That's easy to cover with a regex


    > But that doesn't cover everything ...


    I think you'll end up having to parse the code in its entirety to do
    this correctly. Consider triple quoted strings and multiple uses of
    parenthesis, both of which can be nested arbitrarily, including inside
    each other, and arbitrarily nested delimeters are beyond the ability of
    regexen.

    Is this merely an academic exercise, or is there a larger purpose for
    wanting this information?

    Regards,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    "I wish people would die in alphabetical order." -- My wife, the genealogist
    Dan Sommers, Apr 8, 2006
    #2
    1. Advertising

  3. Sandra-24

    Sandra-24 Guest

    No it's not an academic excercise, but your right, the situation is
    more complex than I originally thought. I've got a minor bug in my
    template code, but it'd cause more trouble to fix than to leave in for
    the moment.

    Thanks for your input!
    -Sandra
    Sandra-24, Apr 8, 2006
    #3
  4. Re: How to determine if a line of python code is a continuation ofthe line above it

    Sandra-24 wrote:
    > No it's not an academic excercise, but your right, the situation is
    > more complex than I originally thought. I've got a minor bug in my
    > template code, but it'd cause more trouble to fix than to leave in for
    > the moment.
    >
    > Thanks for your input!
    > -Sandra
    >

    Take a look at the codeop module in the standard library

    Michael
    Michael Spencer, Apr 8, 2006
    #4
  5. Re: How to determine if a line of python code is a continuation ofthe line above it

    Sandra-24 wrote:
    > I'm not sure how complex this is, I've been brainstorming a little, and
    > I've come up with:
    >
    > If the previous line ended with a comma or a \ (before an optional
    > comment)
    >
    > That's easy to cover with a regex
    >
    > But that doesn't cover everything, because this is legal:
    >
    > l = [
    > 1,
    > 2,
    > 3
    > ]
    >
    > and with dictionaries and tuples as well.
    >
    > Not sure how I would check for that programmatically yet.
    >
    > Is there any others I'm missing?
    >
    > Thanks,
    > -Sandra
    >

    Sandra,

    in a similar situation I used 'inspect' and 'compile' like so:


    import inspect

    def func(*arg, **kwarg):
    return get_cmd()

    def get_cmd():
    frame = inspect.currentframe()
    outerframes = inspect.getouterframes(frame)
    caller = outerframes[1][0]
    ccframe = outerframes[2][0]
    ccfname = outerframes[2][1]
    ccmodule = inspect.getmodule(ccframe)
    slines, start = inspect.getsourcelines(ccmodule)
    clen = len(slines)
    finfo = inspect.getframeinfo(ccframe, clen)
    theindex = finfo[4]
    lines = finfo[3]
    theline = lines[theindex]
    cmd = theline
    for i in range(theindex-1, 0, -1):
    line = lines
    try:
    compile (cmd.lstrip(), '<string>', 'exec')
    except SyntaxError:
    cmd = line + cmd
    else:
    break
    return cmd

    if __name__ == '__main__':
    a=0
    b="test"
    c=42

    cmd=func(a)
    print cmd
    cmd=func(a,
    b,
    c)
    print cmd



    output:
    cmd=func(a)

    cmd=func(a,
    b,
    c)

    Regards
    Hans Georg
    Hans Georg Krauthaeuser, Apr 9, 2006
    #5
  6. Re: How to determine if a line of python code is a continuation ofthe line above it

    Sandra-24 wrote:
    > I'm not sure how complex this is, I've been brainstorming a little, and
    > I've come up with:


    from tokenize import generate_tokens, NL, NEWLINE
    from cStringIO import StringIO

    def code_lines(source):
    """Takes Python source code (as either a string or file-like
    object) and yields a tuple of (is_new_logical, code) for each
    physical line of code.
    """

    if isinstance(source, basestring):
    source = StringIO(source)

    buffer = []
    new_logical = True

    for token_type, source, sloc, eloc, line in \
    generate_tokens(source.readline):
    buffer.append(source)
    if token_type == NL:
    yield new_logical, ''.join(buffer)
    buffer = []
    new_logical = False
    elif token_type == NEWLINE:
    yield new_logical, ''.join(buffer)
    buffer = []
    new_logical = True
    if buffer:
    yield new_logical, ''.join(buffer)
    Leif K-Brooks, Apr 9, 2006
    #6
    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. Replies:
    1
    Views:
    4,222
    Joe Kesselman
    Aug 2, 2006
  2. Russ P.
    Replies:
    5
    Views:
    351
    Paul Boddie
    Jun 5, 2008
  3. Neil Cerutti
    Replies:
    4
    Views:
    312
    Neil Cerutti
    Nov 24, 2009
  4. Yingjie Lan
    Replies:
    37
    Views:
    1,324
    Robert Kern
    Nov 10, 2010
  5. Replies:
    10
    Views:
    184
    Curt Sampson
    Jan 19, 2008
Loading...

Share This Page