473,320 Members | 1,825 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,320 software developers and data experts.

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

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

Apr 8 '06 #1
5 1553
On 8 Apr 2006 11:24:04 -0700,
"Sandra-24" <sa***********@yahoo.com> 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
Apr 8 '06 #2
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

Apr 8 '06 #3
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

Apr 8 '06 #4
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
Apr 9 '06 #5
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)
Apr 9 '06 #6

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
28
by: Matt Leslie | last post by:
Hi, I'm trying to use microthreads under stackless python, since they sound like exactly what I am after, but I am having very little success. I've got a fresh install of python 2.3.3 from...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
0
by: Robert | last post by:
After failing on a yield/iterator-continuation problem in Python (see below) I tried the Ruby (1.8.2) language first time on that construct: The example tries to convert a block callback interface...
8
by: Steve Bergman | last post by:
As I study Python, I am trying to develop good, Pythonic, habits. For one thing, I am trying to keep Guido's the style guide in mind. And I know that it starts out saying that it should not be...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
4
by: Lawrence D'Oliveiro | last post by:
The Python docs recommend the use of triple-quoted string literals for docstrings, e.g. def Meet(Alice, Bob) : """arranges a meeting between Alice and Bob. Returns a reference to the meeting...
6
by: Russ P. | last post by:
I've always appreciated Python's lack of requirement for a semi-colon at the end of each line. I also appreciate its rules for automatic line continuation. If a statement ends with a "+", for...
3
by: MLH | last post by:
I don't understand why (a) works and compiles but (b) does not... (a) Set qdfTemp = .CreateQueryDef("", _ "SELECT * FROM Employees") (b) Set qdfTemp = .CreateQueryDef("", _ "SELECT...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.