fully-qualified namespaces?

Discussion in 'Python' started by Lenny G., Sep 12, 2005.

  1. Lenny G.

    Lenny G. Guest

    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.
     
    Lenny G., Sep 12, 2005
    #1
    1. Advertising

  2. "Lenny G." <> 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
     
    George Sakkis, Sep 12, 2005
    #2
    1. Advertising

  3. Lenny G.

    Lenny G. Guest

    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.
     
    Lenny G., Sep 12, 2005
    #3
  4. 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
     
    Michael Hoffman, Sep 12, 2005
    #4
  5. Lenny G.

    Lenny G. Guest

    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.
     
    Lenny G., Sep 12, 2005
    #5
  6. 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
     
    Michael Hoffman, Sep 12, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Drebin

    Re: fully qualified domain name

    Frank Drebin, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    574
    Frank Drebin
    Aug 26, 2003
  2. Jacob Crossley
    Replies:
    0
    Views:
    407
    Jacob Crossley
    Apr 2, 2004
  3. Jacob Crossley
    Replies:
    0
    Views:
    336
    Jacob Crossley
    Apr 6, 2004
  4. S.Marion
    Replies:
    2
    Views:
    491
    Roedy Green
    Mar 3, 2006
  5. Szabolcs Borsanyi

    Pointer to qualified poitner to qualified object

    Szabolcs Borsanyi, May 30, 2008, in forum: C Programming
    Replies:
    13
    Views:
    563
    Keith Thompson
    Jun 8, 2008
Loading...

Share This Page