Relative import bug or not?

2006-10-14 Thread Alexey Borzenkov
After reading PEP-0328 I wanted to give relative imports a try:

# somepkg/__init__.py


# somepkg/test1.py
from __future__ import absolute_import
from . import test2

if __name__ == "__main__":
print "Test"

# somepkg/test2.py


But it complaints:
C:\1\somepkg>test1.py
Traceback (most recent call last):
  File "C:\1\somepkg\test1.py", line 1, in 
from . import test2
ValueError: Attempted relative import in non-package

Does this mean that packages that implement self tests are not allowed
to use relative import? Or is it just a bug? I can understand that I
can use "import test2" when it's __main__, but when it's not now it
complains about no module test2 with absolute_import on.

PEP-0328 also has this phrase: "Relative imports use a module's
__name__ attribute to determine that module's position in the package
hierarchy. If the module's name does not contain any package
information (e.g. it is set to '__main__') then relative imports are
resolved as if the module were a top level module, regardless of where
the module is actually located on the file system.", but maybe my
english knowledge is not really good, because I can't understand what
should actually happen here ("relative imports are resolved as if the
module were a top level module")... :-/

So is it a bug, or am I doing something wrong?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A friendlier, sugarier lambda -- a proposal for Ruby-like blocks in python

2006-10-14 Thread Alexey Borzenkov
[EMAIL PROTECTED] wrote:
> Compared to the Python I know and love, Ruby isn't quite the same.
> However, it has at least one terrific feature: "blocks".

Well, I particularly like how Boo (http://boo.codehaus.org) has done
it:

  func(a, b, c) def(p1, p2, p3):
stmts

I was so attached to these "nameless" def-forms that I was even shocked
when I found that this doesn't work in python:

  f = def(a, b):
return a*b

Another good feature of Boo, btw.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A friendlier, sugarier lambda -- a proposal for Ruby-like blocks in python

2006-10-14 Thread Alexey Borzenkov
[EMAIL PROTECTED] wrote:
> but maybe it reduces code readabilty a bit for people
> that have just started to program:
>
> mul2 = def(a, b):
> return a * b
>
> Instead of:
>
> def mul2(a, b):
> return a * b

For such simple cases, yes. What about:

  button.click += def(obj):
# do stuff

You obviously can't:

  def button.click(obj):
# do stuff

:-) And if you make intermediate function and then assign it somewhere,
it "pollutes namespace": it's still left there, unneeded.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: chained attrgetter

2006-10-25 Thread Alexey Borzenkov
On Oct 25, 10:00 pm, "David S." <[EMAIL PROTECTED]> wrote:
> Does something like operator.getattr exist to perform a chained attr
> lookup?

Do you mean something like

class cattrgetter:
def __init__(self, name):
self.names = name.split('.')
def __call__(self, obj):
for name in self.names:
obj = getattr(obj, name)
return obj

?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python assignment loop

2007-05-21 Thread Alexey Borzenkov
On May 21, 8:12 am, "Silver Rock" <[EMAIL PROTECTED]> wrote:
> yes, that is the way I a solving the problem. using lists. so it seems
> that there is no way around it then..

There's at least one way to do it that I can think of straight away:

selfmodule = __import__(__name__, None, None, (None,))
setattr(selfmodule, "varname", value)

But I can't say it's anywhere near elegant.

-- 
http://mail.python.org/mailman/listinfo/python-list