Nick Coghlan <ncoghlan <at> gmail.com> writes:

> My rewritten version of PEP 414 is now up
> (http://www.python.org/dev/peps/pep-0414/). It describes in detail a
> lot more of the historical background that was taken as read when
> Guido accepted the PEP.

Nice work - thanks!

I've implemented a first attempt at an import hook as mentioned in the PEP:

https://bitbucket.org/vinay.sajip/uprefix/

It's used as follows: assume you have a simple package hierarchy of code
containing u-prefixed literals:

frob
+-- __init__.py
+-- subwob
|   +-- __init__.py
|   +-- subsubwob.py
+-- wob.py

with the following contents:

# frob/subwob/__init__.py
z = u'def'
#-------------------------
# frob/subwob/subsubwob.py
w = u'tuv'
#-------------------------
# frob/__init__.py
y = u'xyz'
#-------------------------
# frob/wob.py
x = u'abc'
#-------------------------

You can now import these in Python 3.2 using the hook:

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import uprefix; uprefix.register_hook()
>>> import frob.subwob.subsubwob
>>> frob.subwob.subsubwob.w
'tuv'
>>> frob.subwob
<module 'frob.subwob' from 'frob/subwob/__init__.py'>
>>> frob.subwob.z
'def'
>>> import frob.wob
>>> frob.wob.x
'abc'
>>> frob
<module 'frob' from 'frob/__init__.py'>
>>> frob.y
'xyz'
>>> 

The "import uprefix; uprefix.register_hook()" is all that's needed to enable the
hook. You can also unregister the hook by calling "uprefix.unregister_hook()".

The project is set up with a setup.py and (basic) test suite, though it's too
early to put it on PyPI. I have done basic testing with it, and it should also
work as expected in virtual environments.

The implementation uses lib2to3, though that could be changed if needed.

You can also, of course, use it in Python 3.3 right now (before the PEP gets
implemented).

Please take a look at it, try it out, and give some feedback.

Regards,

Vinay Sajip

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to