1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? return values from @python

Discussion in 'Support' started by Stefano Piccardi, Apr 22, 2012.

  1. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    What techniques are available to return values from %@python[] to TCC ?
    I have been exploring some straightforward ways, but I can't see how to get something out of @python except for console output. Note that I am not using the supported ActivePython but another distribution, so my issue could be all there. Anyways, this is what I tried:
    1. Capture standard output: set x=%@python[print 1+1] >out
      This sets x=0 (python's exit code?), creates an empty out file and prints 2 to the console.
    2. Set a TCC variable from python: how can I do that?
      Incidentally I discovered that the persistent python interpreter gets its own frozen environment block, so setting environment variables, like $PATH, from python is pointless because changes aren't reflected in TCC's environment block.
    3. Write to a file from python. Here is a working example, but I would like to find a memory based solution as well, like #2.
    Code:
    set tmpf=%@unique["%[TEMP]"]
    echo>nul %@python[with open(r'%[tmpf]','w') as tmpf: print >>tmpf, 1+1]
    type "%[tmpf]"
    del "%[tmpf]"
    
     
  2. Steve Pitts

    Joined:
    Jul 7, 2008
    Messages:
    158
    Likes Received:
    0
    I can't speak for the Python support Stefano, but the equivalent @REXX simply allows you to return a numeric value valid as an exit code (REXX has both EXIT and RETURN 'statements' that can be used to achieve that). Any attempt to return a value that cannot be interpreted as a number (and this might differ for python, REXX has no data typing so all data are effectively strings) causes the TCC instance to crash.
     
  3. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    Thanks Steve. I read in an older thread of this forum that performing an exit(n) in python closes the TCC session as well because the python interpreter is persistent.
     

Share This Page