fully-qualified namespaces?

L

Lenny G.

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.
 
G

George Sakkis

Lenny G. said:
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
 
L

Lenny G.

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.
 
M

Michael Hoffman

Lenny said:
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.
 
L

Lenny G.

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.
 
M

Michael Hoffman

Lenny said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top