Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   parsing pyton code (http://www.velocityreviews.com/forums/t337837-parsing-pyton-code.html)

Philippe C. Martin 11-06-2004 08:38 PM

parsing pyton code
 
Hi,

I'm looking for a very simple way to figure out if a line of code requires
further lines of code ex: for i in range(10): So far I have found the
following trick that seem dangerous to me since it assumes the string 'EOF'
exists in the exception message.

Do I have the right to do this ?




import compiler
import string
try:
compiler.parse('for i in range(10):')
except SyntaxError, (val1, val2):
print val1
print val2
l_eof = False
l = string.split(val1,' ')
for l_c in l:
if l_c == 'EOF':
l_eof = True
break
if l_eof == True:
print 'YES'

Jean Brouwers 11-06-2004 11:32 PM

Re: parsing pyton code
 

Check the source of the 'code' module, especially the runsource()
method of the InteractiveInterpreter class. Also, see these docs

<http://docs.python.org/lib/module-code.html>


/Jean Brouwers
ProphICy Semiconductor, Inc.


<mailman.6015.1099773489.5135.python-list@python.org>, Philippe C.
Martin <philippecmartin@sbcglobal.net> wrote:

> Hi,
>
> I'm looking for a very simple way to figure out if a line of code requires
> further lines of code ex: for i in range(10): So far I have found the
> following trick that seem dangerous to me since it assumes the string 'EOF'
> exists in the exception message.
>
> Do I have the right to do this ?
>
>
>
>
> import compiler
> import string
> try:
> compiler.parse('for i in range(10):')
> except SyntaxError, (val1, val2):
> print val1
> print val2
> l_eof = False
> l = string.split(val1,' ')
> for l_c in l:
> if l_c == 'EOF':
> l_eof = True
> break
> if l_eof == True:
> print 'YES'



All times are GMT. The time now is 01:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.