problem with exec

Discussion in 'Python' started by ...:::JA:::..., Jul 21, 2007.

  1. Hello,

    After my program read and translate this code:

    koristi os,sys;
    ispisi 'bok kaj ima';

    into the:

    import os,sys;
    print 'bok kaj ima';

    and when it run this code with "exec", I always get error like this, but I
    still dont't know what is a problem:

    Traceback (most recent call last):
    File "C:\Python24\Lib\site-packages\VL\__init__.py", line 188, in
    kompajlati
    kompajlati_proces()
    File "C:\Python24\Lib\site-packages\VL\__init__.py", line 183, in
    kompajlati_proces
    h2=Konzola()
    File "C:\Python24\Lib\site-packages\VL\__init__.py", line 158, in __init__
    k=kod(ZTextCtrl.GetLabel())
    File "C:\Python24\Lib\site-packages\VL\__init__.py", line 83, in kod
    exec(str_ngh)
    File "<string>", line 1
    import os ,sys ;
    ^
    SyntaxError: invalid syntax

    PS: How can I change when user write script with my program to he don't need
    aspirate the lines of his source code
    e.g.
    import os,sys
    n=90
    if n==90:print "OK"
    else:print "No"


    Regards,

    Vedran


    __________________________________________________________________ Vedran
    veki ICQ#: 264412055 Current ICQ status: + More ways to contact me Get ICQ!
    __________________________________________________________________
    ...:::JA:::..., Jul 21, 2007
    #1
    1. Advertising

  2. ...:::JA:::...

    Steve Holden Guest

    ....:::JA:::... wrote:
    > Hello,
    >
    > After my program read and translate this code:
    >
    > koristi os,sys;
    > ispisi 'bok kaj ima';
    >
    > into the:
    >
    > import os,sys;
    > print 'bok kaj ima';
    >
    > and when it run this code with "exec", I always get error like this, but I
    > still dont't know what is a problem:
    >
    > Traceback (most recent call last):
    > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 188, in
    > kompajlati
    > kompajlati_proces()
    > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 183, in
    > kompajlati_proces
    > h2=Konzola()
    > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 158, in __init__
    > k=kod(ZTextCtrl.GetLabel())
    > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 83, in kod
    > exec(str_ngh)
    > File "<string>", line 1
    > import os ,sys ;
    > ^
    > SyntaxError: invalid syntax
    >

    This is almost certainly because the code contains embedded carriage
    returns:

    >>> code = """import os,sys;\nprint 'bok kaj ima';"""
    >>> exec code

    bok kaj ima
    >>> code = """import os,sys;\r\nprint 'bok kaj ima';"""
    >>> exec code

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 1
    import os,sys;
    ^
    SyntaxError: invalid syntax
    >>>


    > PS: How can I change when user write script with my program to he don't need
    > aspirate the lines of his source code
    > e.g.
    > import os,sys
    > n=90
    > if n==90:print "OK"
    > else:print "No"
    >
    >

    I'm afraid I don't understand this question. If you are talking about
    the indentation of the code, if you don't want indentation you will have
    to use braces - { and } - to indicate the nesting structure of your program.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Jul 21, 2007
    #2
    1. Advertising

  3. On Sat, 21 Jul 2007 16:31:38 -0400, Steve Holden wrote:

    > I'm afraid I don't understand this question. If you are talking about
    > the indentation of the code, if you don't want indentation you will have
    > to use braces - { and } - to indicate the nesting structure of your program.


    Oh my, teasing the n00bs like that is not nice. You know Python doesn't
    uses braces, it uses BEGIN ... END blocks.


    --
    Steven.
    Steven D'Aprano, Jul 22, 2007
    #3
  4. ...:::JA:::...

    Guest

    On 21 srp, 22:31, Steve Holden <> wrote:
    > ...:::JA:::... wrote:
    > > Hello,

    >
    > > After my program read and translate this code:

    >
    > > koristi os,sys;
    > > ispisi 'bok kaj ima';

    >
    > > into the:

    >
    > > import os,sys;
    > > print 'bok kaj ima';

    >
    > > and when it run this code with "exec", I always get error like this, but I
    > > still dont't know what is a problem:

    >
    > > Traceback (most recent call last):
    > > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 188, in
    > > kompajlati
    > > kompajlati_proces()
    > > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 183, in
    > > kompajlati_proces
    > > h2=Konzola()
    > > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 158, in __init__
    > > k=kod(ZTextCtrl.GetLabel())
    > > File "C:\Python24\Lib\site-packages\VL\__init__.py", line 83, in kod
    > > exec(str_ngh)
    > > File "<string>", line 1
    > > import os ,sys ;
    > > ^
    > > SyntaxError: invalid syntax

    >
    > This is almost certainly because the code contains embedded carriage
    > returns:
    >
    > >>> code = """import os,sys;\nprint 'bok kaj ima';"""
    > >>> exec code

    > bok kaj ima
    > >>> code = """import os,sys;\r\nprint 'bok kaj ima';"""
    > >>> exec code

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "<string>", line 1
    > import os,sys;
    > ^
    > SyntaxError: invalid syntax
    > >>>

    >
    > > PS: How can I change when user write script with my program to he don't need
    > > aspirate the lines of his source code
    > > e.g.
    > > import os,sys
    > > n=90
    > > if n==90:print "OK"
    > > else:print "No"

    >
    > I'm afraid I don't understand this question. If you are talking about
    > the indentation of the code, if you don't want indentation you will have
    > to use braces - { and } - to indicate the nesting structure of your program.
    >
    > regards
    > Steve
    > --
    > Steve Holden +1 571 484 6266 +1 800 494 3119
    > Holden Web LLC/Ltd http://www.holdenweb.com
    > Skype: holdenweb http://del.icio.us/steve.holden
    > --------------- Asciimercial ------------------
    > Get on the web: Blog, lens and tag the Internet
    > Many services currently offer free registration
    > ----------- Thank You for Reading -------------


    Hello,

    Thanks for everything previously, but just to I ask about code
    indentation,this with { and } doesn't
    employed, here is my example how can I solve this about code
    indentation:

    >>> n=90
    >>> if n==90:

    {print "bok kjai ma'}
    File "<input>", line 2
    {print "bok kjai ma'}
    ^
    SyntaxError: invalid syntax


    Thanks!!!!!!!

    Regards,Vedran
    , Jul 22, 2007
    #4
  5. On Sun, 22 Jul 2007 03:23:30 -0700, vedrandekovic wrote:

    > Thanks for everything previously, but just to I ask about code
    > indentation,this with { and } doesn't
    > employed, here is my example how can I solve this about code
    > indentation:
    >
    >>>> n=90
    >>>> if n==90:

    > {print "bok kjai ma'}
    > File "<input>", line 2
    > {print "bok kjai ma'}
    > ^
    > SyntaxError: invalid syntax



    Steve Holden was playing silly games. You can't use { } for indentation.
    You have to use indentation.

    --
    Steven.
    Steven D'Aprano, Jul 22, 2007
    #5
  6. ...:::JA:::...

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Sun, 22 Jul 2007 03:23:30 -0700, vedrandekovic wrote:
    >
    >> Thanks for everything previously, but just to I ask about code
    >> indentation,this with { and } doesn't
    >> employed, here is my example how can I solve this about code
    >> indentation:
    >>
    >>>>> n=90
    >>>>> if n==90:

    >> {print "bok kjai ma'}
    >> File "<input>", line 2
    >> {print "bok kjai ma'}
    >> ^
    >> SyntaxError: invalid syntax

    >
    >
    > Steve Holden was playing silly games. You can't use { } for indentation.
    > You have to use indentation.
    >

    I wasn't playing silly games at all, and I did prefix that part ofmy
    answer with "I'm afraid I don't understand this question". The OP is
    writing a program to "translate" a Python-like language that uses
    non-English keywords into Python. Since the application is transforming
    its input, it could transform braces into indentation. Of course
    *Python* doesn't use braces, but the question was how to write
    "pseudo-Python" without using indentation to indicate grouping.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Jul 22, 2007
    #6
  7. ...:::JA:::...

    Guest

    > I wasn't playing silly games at all, and I did prefix that part ofmy
    > answer with "I'm afraid I don't understand this question". The OP is
    > writing a program to "translate" a Python-like language that uses
    > non-English keywords into Python. Since the application is transforming
    > its input, it could transform braces into indentation. Of course *Python*
    > doesn't use braces, but the question was how to write "pseudo-Python"
    > without using indentation to indicate grouping.
    >
    > regards
    > Steve



    Hi,

    This previously is exactly what I need can you help me somehow about
    this
    code
    indentation, on any way you know.Plese help I will really appreciate
    this!!!!!!!!!!!!!!



    Regards,

    Vedran
    , Jul 22, 2007
    #7
  8. On Sun, 22 Jul 2007 09:12:21 -0400, Steve Holden wrote:


    >> Steve Holden was playing silly games. You can't use { } for indentation.
    >> You have to use indentation.
    >>

    > I wasn't playing silly games at all, and I did prefix that part ofmy
    > answer with "I'm afraid I don't understand this question". The OP is
    > writing a program to "translate" a Python-like language that uses
    > non-English keywords into Python. Since the application is transforming
    > its input, it could transform braces into indentation. Of course
    > *Python* doesn't use braces, but the question was how to write
    > "pseudo-Python" without using indentation to indicate grouping.


    Then I have misunderstood you, and I apologize.



    --
    Steven.
    Steven D'Aprano, Jul 22, 2007
    #8
  9. ...:::JA:::...

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Sun, 22 Jul 2007 09:12:21 -0400, Steve Holden wrote:
    >
    >
    >>> Steve Holden was playing silly games. You can't use { } for indentation.
    >>> You have to use indentation.
    >>>

    >> I wasn't playing silly games at all, and I did prefix that part ofmy
    >> answer with "I'm afraid I don't understand this question". The OP is
    >> writing a program to "translate" a Python-like language that uses
    >> non-English keywords into Python. Since the application is transforming
    >> its input, it could transform braces into indentation. Of course
    >> *Python* doesn't use braces, but the question was how to write
    >> "pseudo-Python" without using indentation to indicate grouping.

    >
    > Then I have misunderstood you, and I apologize.
    >

    Thanks. I was hoping that was the case.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
    Steve Holden, Jul 22, 2007
    #9
  10. En Sun, 22 Jul 2007 10:36:59 -0300, <>
    escribió:

    >> Since the application is transforming
    >> its input, it could transform braces into indentation. Of course
    >> *Python*
    >> doesn't use braces, but the question was how to write "pseudo-Python"
    >> without using indentation to indicate grouping.
    >>

    > This previously is exactly what I need can you help me somehow about
    > this
    > code
    > indentation, on any way you know.Plese help I will really appreciate
    > this!!!!!!!!!!!!!!


    If you are using the tokenize module as suggested some time ago, try to
    analyze the token sequence you get using { } (or perhaps begin/end pairs
    in your own language, that are easier to distinguish from a dictionary
    display) and the sequence you get from the "real" python code. Then write
    a script to transform one into another:

    from tokenize import generate_tokens
    from token import tok_name
    from cStringIO import StringIO

    def analyze(source):
    g = generate_tokens(StringIO(source).readline)
    for toknum, tokval, _, _, _ in g:
    print tok_name[toknum], repr(tokval)

    I think you basically will have to ignore INDENT, DEDENT, and replace
    NAME+"begin" with INDENT, NAME+"end" with DEDENT.

    --
    Gabriel Genellina
    Gabriel Genellina, Jul 23, 2007
    #10
  11. En Sun, 22 Jul 2007 10:36:59 -0300, <>
    escribió:

    >> Since the application is transforming
    >> its input, it could transform braces into indentation. Of course
    >> *Python*
    >> doesn't use braces, but the question was how to write "pseudo-Python"
    >> without using indentation to indicate grouping.
    >>

    > This previously is exactly what I need can you help me somehow about
    > this
    > code
    > indentation, on any way you know.Plese help I will really appreciate
    > this!!!!!!!!!!!!!!


    If you are using the tokenize module as suggested some time ago, try to
    analyze the token sequence you get using { } (or perhaps begin/end pairs
    in your own language, that are easier to distinguish from a dictionary
    display) and the sequence you get from the "real" python code. Then write
    a script to transform one into another:

    from tokenize import generate_tokens
    from token import tok_name
    from cStringIO import StringIO

    def analyze(source):
    g = generate_tokens(StringIO(source).readline)
    for toknum, tokval, _, _, _ in g:
    print tok_name[toknum], repr(tokval)

    I think you basically will have to ignore INDENT, DEDENT, and replace
    NAME+"begin" with INDENT, NAME+"end" with DEDENT.

    --
    Gabriel Genellina
    Gabriel Genellina, Jul 23, 2007
    #11
  12. ...:::JA:::...

    Guest

    On 23 srp, 09:19, "Gabriel Genellina" <> wrote:
    > En Sun, 22 Jul 2007 10:36:59 -0300, <>
    > escribió:
    >
    > >> Since the application is transforming
    > >> its input, it could transform braces into indentation. Of course
    > >> *Python*
    > >> doesn't use braces, but the question was how to write "pseudo-Python"
    > >> without using indentation to indicate grouping.

    >
    > > This previously is exactly what I need can you help me somehow about
    > > this
    > > code
    > > indentation, on any way you know.Plese help I will really appreciate
    > > this!!!!!!!!!!!!!!

    >
    > If you are using the tokenize module as suggested some time ago, try to
    > analyze the token sequence you get using { } (or perhaps begin/end pairs
    > in your own language, that are easier to distinguish from a dictionary
    > display) and the sequence you get from the "real" python code. Then write
    > a script to transform one into another:
    >
    > from tokenize import generate_tokens
    > from token import tok_name
    > from cStringIO import StringIO
    >
    > def analyze(source):
    > g = generate_tokens(StringIO(source).readline)
    > for toknum, tokval, _, _, _ in g:
    > print tok_name[toknum], repr(tokval)
    >
    > I think you basically will have to ignore INDENT, DEDENT, and replace
    > NAME+"begin" with INDENT, NAME+"end" with DEDENT.
    >
    > --
    > Gabriel Genellina


    Hello,

    I know what do you mean and I really need that , but I don't know how
    to I do this. Please help me!!!!! It's really important to me


    Regards,

    Vedran
    , Jul 23, 2007
    #12
    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. Hal Vaughan
    Replies:
    11
    Views:
    1,090
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,061
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    451
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    156
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    633
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page