Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to determine if a line of python code is a continuation of the line above it

Reply
Thread Tools

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

 
 
Sandra-24
Guest
Posts: n/a
 
      04-08-2006
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

 
Reply With Quote
 
 
 
 
Dan Sommers
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
 
 
 
Sandra-24
Guest
Posts: n/a
 
      04-08-2006
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

 
Reply With Quote
 
Michael Spencer
Guest
Posts: n/a
 
      04-08-2006
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

 
Reply With Quote
 
Hans Georg Krauthaeuser
Guest
Posts: n/a
 
      04-09-2006
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[i]
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
 
Reply With Quote
 
Leif K-Brooks
Guest
Posts: n/a
 
      04-09-2006
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)
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
We are currently buying Foundy Networks - primarily NetIron MLX andXMR but are also looking for BigIron RX, ServerIron WMS7, and FastIronFESX's. If you have the above or have access to the above from a clientlooking ro recover value, please sen Network/Software Buyer Cisco 1 07-30-2010 01:25 AM
Confusing SyntaxError while entering non-indented code in interactiveinterpreter on continuation line. Colin Howell Python 2 04-25-2010 10:21 PM
line continuation for lines ending in "and" or "or" Russ P. Python 5 06-05-2008 09:13 PM
wrapping long attribute value (line-continuation for attribute value) lophiomys@gmx.at XML 1 08-02-2006 01:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57