Questions on importing and exec command.

Sam Tannous stannous at employees.org
Thu Nov 4 15:03:25 EST 1999


I'm writing a program that starts a separate thread
where the user can type python commands (which
are executed with the exec command).  This saves me
the trouble of having to write a parser, etc... I realize
the hazards in doing this ;-)

I'm having several problems:

1.  A minor problem is command retrieval (or
    command history) in the console. If someone types a fairly
    long command, I'm not sure how to make the
    up and down arrows work correctly (as in tcsh)
    from the console I've created.   Some key mapping
    within the program?

2.  I want to allow the user (once she's in the console
    I've provided) to "import" or otherwise run python
    code fragments that have access to the functions I've
    provided globally. Typing the function name in the
    console seems to work just fine.  But "import"ing a small
    chunk of code doesn't seem to work (and I don't want
    to force users to type everything by hand).  The files
    I'm importing don't see the global functions I've declared
   (see the error in the sample session below).   I want to give the
    user complete freedom to write python code fragments and
    save them in files and then just run them from the console
    as easily as possible.

Here is the code:

------------  test2.py ---------
#!/usr/bin/python
import time,thread,string,sys

def main():
    print 'in main'
    thread.start_new(listener,('test',))
    myconsole()

def listener(test):
    global count
    count = 0
    while 1:
        print "in listener",test,count
        time.sleep(30)
        count = count + 1

def myconsole(*x):
    print 'Now in console'
    while 1:
        command = raw_input('console > ')
        command = string.strip(command)

        try:
            exec command in globals()
        except:
            print sys.exc_type,sys.exc_value

def testcall(message='default'):
    print "in testcall",message

def startit(hold_time=10):
    thread.start_new(do_stuff,(hold_time,))

def do_stuff(hold_time=10):
    print 'started do_stuff  hold_time =',hold_time
    time.sleep(hold_time)
    print 'ended do_stuff'

def do_nothing(*x):
    pass


count = 0
my_dict = {'01':('test1',startit),'07':('test2',do_nothing)}

main()
------------------------


-------- call1.py -------
for i in range(5,9):
    print i
    startit(hold_time=i)
---------------------------

Here's a session where I get the error message
when calling function startit from an import statement.
(stuff the program prints has a '****' in front of it)
-------------------------
$test2.py
****in main
****Now in console
****in listener test 0
console >
console >
console > testcall()              #here I call a function without
problems
****in testcall default
console > ****in listener test 1
****in listener test 2
****in listener test 3
****in listener test 4
****in listener test 5
****in listener test 6
****in listener test 7

console > ****in listener test 8

console > count = 666            #changing a global variable is no
problem
console >
console > testcall()
****in testcall default
console > print my_dict          #looking at global variables is no
problem
{'01': ('test1', <function startit at 27bba0>), '07': ('test2',
<function do_nothing at 293688>)}
console > ****in listener test 667

console > startit()
console > ****started do_stuff  hold_time = 10
****ended do_stuff

console > start****in listener test 668
^R

console > startit(hold_time=3)
console > ****started do_stuff  hold_time = 3

console > ****ended do_stuff

console > import call1           # importing a code fragment causes
problems
5
exceptions.NameError startit
---------------------------

Is there a trick to importing code fragments from this python session
I've created?

Thanks,
Sam





More information about the Python-list mailing list