Re: writing Python in Emacs
Rob Wolfe wrote: > The good news is that I managed to configure completion for Python > in Emacs using pymacs, python-mode.el, pycomplete.el and pycomplete.py. > For contents of my pycomplete.el, pycomplete.py and necessary > settings in .emacs see below. Thanks for that! I've been hoping something like this landed on my lap for years. -- http://mail.python.org/mailman/listinfo/python-list
Re: exec and closures
On Feb 22, 3:18 am, Peter Otten <[EMAIL PROTECTED]> wrote:
> Alejandro Dubrovsky wrote:
> > def autoassign(_init_):
> > import inspect
> > import functools
>
> > argnames, _, _, defaults = inspect.getargspec(_init_)
> > argnames = argnames[1:]
>
> > indentation = ''
> > settings = ['self.%s = %s' % (arg[1:], arg) for arg in argnames
> > if arg[0] == '_']
>
> > if len(settings) <= 0:
> > return _init_
>
> > if defaults is None:
> > args = argnames[:]
> > else:
> > args = argnames[:-len(defaults)]
> > for key, value in zip(argnames[-len(defaults):],defaults):
> > args.append('%s=%s' % (key, repr(value)))
>
> > template = """def _autoassign(self, %(args)s):
> > %(setting)s
> > _init_(self, %(argnames)s)
> > """ % {'args' : ", ".join(args), 'setting' : "\n".join(['%s%s' %
> > (indentation, setting) for setting
> > in settings]), 'argnames' : ', '.join(argnames)}
>
> > try:
> > exec template
> > except SyntaxError, e:
> > raise SyntaxError('%s. line: %s. offset %s:\n%s' %
> > (e.msg, e.lineno, e.offset, template))
> > return _autoassign
>
>
[snip]
> > Is there a way to bind the _init_ name at exec time?
>
> Use a dedicated namespace:
>
> namespace = dict(_init_=_init_)
> exec template in namespace
> return namespace["_autoassign"]
>
> Peter
That works! Excellent, thanks.
I still don't understand why the original doesn't work. I thought
exec with no namespace specified used the current context, in which
_init_ would be _init_ already. But understanding can wait.
ale
--
http://mail.python.org/mailman/listinfo/python-list
Re: Problem with exec
On Mar 14, 9:47 am, Justus Schwabedal <[EMAIL PROTECTED]>
wrote:
[snipped]
> However when I do this:
>
> bash-3.2$ cat execBug2.py
> #! /usr/bin/python
> header="""
> from scipy import randn
> def f():
> return randn()
> """
> def g():
> exec header
> return f()
> print "g() =",g()
> bash-3.2$ ./execBug2.py
> g() =
> Traceback (most recent call last):
>File "./execBug2.py", line 10, in
> print "g() =",g()
>File "./execBug2.py", line 9, in g
> return f()
>File "", line 4, in f
> NameError: global name 'randn' is not defined
> bash-3.2$ ???
>
> I get this global name error. I can fix it with adding some line like
> "global randn" but I don't want to do this. I want to do exactly what I
> wrote: Import the function scipy.randn in the local namespace of the
>
> function "g" so that "return f()" makes sense. Can anybody help me out?
> Yours Justus
Maybe using an explicit namespace is good enough for your needs:
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
n = {}
exec header in n
return n['f']()
print "g() =",g()
(i don't understand the issues, but I can cut-and-paste with the best
of them)
--
http://mail.python.org/mailman/listinfo/python-list
