Don't understand a probleme of module time not defined when calling a foo func
Ni Va
nivaemail at gmail.com
Tue May 31 03:51:30 EDT 2016
On Monday, May 30, 2016 at 11:10:33 PM UTC+2, Matt Wheeler wrote:
> On Mon, 30 May 2016, 21:08 Ni Va, <nivaemail at gmail.com> wrote:
>
> >
> > _____________
> > Output:
> > Traceback (most recent call last):
> > File "<string>", line 1, in <module>
> > File "<string>", line 16, in PyExecReplace
> > File "<string>", line 22, in <module>
> > File "<string>", line 11, in foo
> > NameError: global name 'time' is not defined
> >
>
> Based on your traceback it looks like the problem is actually not in your
> code, but in the way you're running it.
>
> PyExecReplace appears to be part of a vim plugin for running snippets of
> Python within vim.
> It looks like it uses exec() to actually run the code. A search online for
> something like "python exec import" might help explain why you're having
> problems with that (or add to the confusion... I had a quick look for a
> difinitive answer but have given up for now).
>
> Try running your script directly from the command line using python instead
> and you may get better results :)
>
> >
Hi Matt!
Reading some web articles, I have solved the problem doing correction in PyReplace that I should submit.
My initial goal is to do abstraction from OS. Than I set PYTHONPATH from within Vim and also interprete python script inside.
Then this is the vimscript plugin modified only for PyReplace :
_________________________________
python << EOL
import vim, StringIO, string, sys
def PyExecAppend(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
exec('\n'.join(r[:]) + '\n')
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
r.append(output[:-1]) # the -1 is to remove the final blank line
redirected.close()
def PyExecReplace(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
# exec('\n'.join(r[:]) + '\n') 2016 05 31 NiVa : resolve module call from def func
exec('\n'.join(r[:]) + '\n') in globals(),globals()
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
r[:] = output[:-1] # the -1 is to remove the final blank line
redirected.close()
def PyExecOut(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
exec('\n'.join(r[:]) + '\n')
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
redirected.close()
return output
EOL
command! -range Pyeo python PyExecOut(<f-line1>,<f-line2>)
command! -range Pyer python PyExecReplace(<f-line1>,<f-line2>)
command! -range Pyea python PyExecAppend(<f-line1>,<f-line2>)
_________________________________
And the test code working now:
# -*- coding: utf-8 -*-
import time
import sys
# from timeout import *
# print help('modules')
print "Starting Test at " + time.ctime()
# @timeout(3)
def foo():
time.sleep(1)
res = 0
x = 0.2
y = 0.3
i = 03
# for i in range(1,15):
# time.sleep(i)
# print "%d seconds have passed" % i
# vim.command( "echo " + "%d seconds have passed" % i
return
# main
foo()
print "->Ending Test at " + time.ctime()
More information about the Python-list
mailing list