By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

fully-qualified namespaces?

P: n/a
Suppose I have a python module named Hippo. In the Hippo module is a
class named Crypto. The Crypto class wants to 'from Crypto.Hash import
SHA' which refers to the module/classes in python-crypto. Other
classes in the Hippo module want to 'import Crypto' referring to
Hippo.Crypto.

How do I do this? For now, I just renamed my Hippo.Crypto to
Hippo.HippoCrypto and everything is okay as long as I use 'HippoCrypto'
to refer to that class. But, this is of course redundant. What I
really want to do is use

'import Crypto' # to refer to python-crypto
'import Hippo.Crypto' # to refer to Hippo.Crypto

but the 2nd item doesn't seem to work from within the Hippo module.
What am I missing?

Thanks,
Lenny G.

Sep 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Lenny G." <al*********@yahoo.com> wrote:
Suppose I have a python module named Hippo. In the Hippo module is a
class named Crypto. The Crypto class wants to 'from Crypto.Hash import
SHA' which refers to the module/classes in python-crypto. Other
classes in the Hippo module want to 'import Crypto' referring to
Hippo.Crypto.

How do I do this? For now, I just renamed my Hippo.Crypto to
Hippo.HippoCrypto and everything is okay as long as I use 'HippoCrypto'
to refer to that class. But, this is of course redundant. What I
really want to do is use

'import Crypto' # to refer to python-crypto
'import Hippo.Crypto' # to refer to Hippo.Crypto

but the 2nd item doesn't seem to work from within the Hippo module.
What am I missing?


The (optional) renaming of the imported modules/classes I would guess:

# Hippo.py

import Crypto as PythonCrypto

class Crypto:
pass

h1 = PythonCrypto.Hash
h2 = Crypto.Hash # refers to Hippo.Crypto
HTH,
George
Sep 12 '05 #2

P: n/a
Thanks George. But I have to apologize -- I think I used the wrong
term in my question. Hippo is actually a package, not a module. So I
have:

Hippo/
__init__.py
Crypto.py
Potamus.py

And inside Crypto.py, I need to access python-crypto's Crypto.Hash
package. Inside Potamus.py, I need to access Hippo.Crypto, e.g.,

Hippo/
__init__.py
Crypto.py # wants to import python-crypto's Crypto.Hash
Potamus.py # wants to import Hippo's Crypto

Can I do this? Crypto.py can't seem to access python-crypto's Crypto
namespace, because it's own namespace takes precendence. I tried using
the renaming trick inside of Crypto.py, but it still can't find the
original python-crypto Crypto. Maybe there's something I can do in the
__init__.py? Maybe something with __path__? Is there a better way to
access namespaces from the module files themselves?

Thanks,
Lenny G.

Sep 12 '05 #3

P: n/a
Lenny G. wrote:
Hippo/
__init__.py
Crypto.py
Potamus.py

And inside Crypto.py, I need to access python-crypto's Crypto.Hash
package. Inside Potamus.py, I need to access Hippo.Crypto, e.g.,

Hippo/
__init__.py
Crypto.py # wants to import python-crypto's Crypto.Hash
Potamus.py # wants to import Hippo's Crypto

Can I do this? Crypto.py can't seem to access python-crypto's Crypto
namespace, because it's own namespace takes precendence. I tried using
the renaming trick inside of Crypto.py, but it still can't find the
original python-crypto Crypto. Maybe there's something I can do in the
__init__.py? Maybe something with __path__? Is there a better way to
access namespaces from the module files themselves?


The *best* way would surely be to rename Hippo/Crypto.py to something
else. This will save you some grief.

If you wanted to load based on filenames, you can use the imp module in
the stdlib.
--
Michael Hoffman
Sep 12 '05 #4

P: n/a
Thanks Michael. That's actually what I already have, e.g.,

Hippo/
__init__.py
HippoCrypto.py
Potamus.py

Of course, this has the disadvantage of not really taking advantage of
the Hippo namespace -- I might as well have:

HippoCrypto.py
Hippo/
__init__.py
Potamus.py

or even get rid of the namespace altogether:

HippoCrypto.py
HippoPotamus.py

Since Hippo.HippoCrypto was already in the Hippo namespace (and clearly
unambiguous with the top-level Crypto namespace from python-crypto), I
thought there might be some way to use this fact to shorten up the name
by removing the redundancy (as can be done in java/C++/etc namespaces).

It sounds like you are saying that there either isn't a way to make the
interpreter utilize this type of namespace difference, or that doing so
is so convoluted that it is certainly worse than just living with
Hippo.HippoCrypto. I can live with these facts (with a little bit of
discomfort ;) ) -- just wanted to make sure that I understood.

Lenny G.

Sep 12 '05 #5

P: n/a
Lenny G. wrote:
It sounds like you are saying that there either isn't a way to make the
interpreter utilize this type of namespace difference, or that doing so
is so convoluted that it is certainly worse than just living with
Hippo.HippoCrypto. I can live with these facts (with a little bit of
discomfort ;) ) -- just wanted to make sure that I understood.


There are some steps to solve this issue outlined here:

http://www.python.org/peps/pep-0328.html

But the fix was not included in Python 2.4 as planned. Maybe in Python 2.5?
--
Michael Hoffman
Sep 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.