Another form of dynamic import

Discussion in 'Python' started by Marco Nawijn, Mar 25, 2009.

  1. Marco Nawijn

    Marco Nawijn Guest

    Hello,

    In short I would like to know if somebody knows if it is possible to
    re-execute a statement that raised an exception? I will explain the
    reason by providing a small introduction on why this might be nice in
    my case
    and some example code.

    I am using the python bindings to a *very* large C++ library. About
    5000 classes divided over approx. 450 different
    packages are exposed through the Python interface. To reduce the
    number of import statements that need to be inserted and to limit the
    number of wildcard imports it would be very helpful if class names
    could be automatically imported from the proper module. There is no
    problem in finding out the proper module given a (valid) class name.

    As an example, look at the following statement

    >> aPoint = gp_Pnt(1.0, 0.0, 0.0) # Oops, this will raise a NameError, since

    # gp_Pnt class
    is unknown

    NameError: name 'gp_Pnt' is not defined

    As indicated, this will raise a NameError exception. What I would like
    to do is something like the following (pseudo-code):

    try:
    ....
    ....
    aPoint = gp_Pnt(1.0, 0.0, 0.0) [1]

    ....
    ....
    except NameError, e:

    name = e.args[0].split[1]

    if isValid(name):
    doImport(name)
    ===> Can I go back to statement [1] from this point?
    else:
    raise e

    There is no problem in catching the exception, finding out which name
    is unknown to python and check if this is a valid name for my library.
    My question is, is there any possibility of going back to the
    statement that raised the error, re-execute the statement and
    continue?

    Thanks for any thoughts and suggestions.

    Marco
    Marco Nawijn, Mar 25, 2009
    #1
    1. Advertising

  2. Marco Nawijn

    Kay Schluehr Guest

    On 25 Mrz., 15:23, Marco Nawijn <> wrote:
    > Hello,
    >
    > In short I would like to know if somebody knows if it is possible to
    > re-execute a statement that raised an exception? I will explain the
    > reason by providing a small introduction on why this might be nice in
    > my case
    > and some example code.
    >
    > I am using the python bindings to a *very* large C++ library. About
    > 5000 classes divided over approx. 450 different
    > packages are exposed through the Python interface. To reduce the
    > number of import statements that need to be inserted and to limit the
    > number of wildcard imports it would be very helpful if class names
    > could be automatically imported from the proper module. There is no
    > problem in finding out the proper module given a (valid) class name.
    >
    > As an example, look at the following statement
    >
    > >> aPoint = gp_Pnt(1.0, 0.0, 0.0) # Oops, this will raise a NameError, since

    >
    > # gp_Pnt class
    > is unknown
    >
    > NameError: name 'gp_Pnt' is not defined
    >
    > As indicated, this will raise a NameError exception. What I would like
    > to do is something like the following (pseudo-code):
    >
    > try:
    > ....
    > ....
    > aPoint = gp_Pnt(1.0, 0.0, 0.0) [1]
    >
    > ....
    > ....
    > except NameError, e:
    >
    > name = e.args[0].split[1]
    >
    > if isValid(name):
    > doImport(name)
    > ===> Can I go back to statement [1] from this point?
    > else:
    > raise e
    >
    > There is no problem in catching the exception, finding out which name
    > is unknown to python and check if this is a valid name for my library.
    > My question is, is there any possibility of going back to the
    > statement that raised the error, re-execute the statement and
    > continue?
    >
    > Thanks for any thoughts and suggestions.
    >
    > Marco


    There is no call/cc continuation in Python when you are asking for
    such a thing.

    I wonder however why you don't try lazy attribute access? Instead of
    making a raw function call like that to gp_Pnt, one can thread all
    calls to the C++ system through an object that implements __getattr__
    and loads new names incrementally if one is missing.
    Kay Schluehr, Mar 25, 2009
    #2
    1. Advertising

  3. Marco Nawijn

    Guest

    On Mar 25, 10:23 am, Marco Nawijn <> wrote:
    > Hello,
    >
    > In short I would like to know if somebody knows if it is possible to
    > re-execute a statement that raised an exception? I will explain the
    > reason by providing a small introduction on why this might be nice in
    > my case
    > and some example code.
    >
    > I am using the python bindings to a *very* large C++ library. About
    > 5000 classes divided over approx. 450 different
    > packages are exposed through the Python interface. To reduce the
    > number of import statements that need to be inserted and to limit the
    > number of wildcard imports it would be very helpful if class names
    > could be automatically imported from the proper module. There is no
    > problem in finding out the proper module given a (valid) class name.
    >
    > As an example, look at the following statement
    >
    > >> aPoint = gp_Pnt(1.0, 0.0, 0.0)     # Oops, this will raise a NameError, since

    >
    >                                                       # gp_Pnt class
    > is unknown
    >
    > NameError: name 'gp_Pnt' is not defined
    >
    > As indicated, this will raise a NameError exception. What I would like
    > to do is something like the following (pseudo-code):
    >
    > try:
    >     ....
    >     ....
    >     aPoint = gp_Pnt(1.0, 0.0, 0.0)    [1]
    >
    >     ....
    >     ....
    > except NameError, e:
    >
    >      name = e.args[0].split[1]
    >
    >      if isValid(name):
    >           doImport(name)
    > ===> Can I go back to statement [1] from this point?
    >      else:
    >          raise e
    >
    > There is no problem in catching the exception, finding out which name
    > is unknown to python and check if this is a valid name for my library.
    > My question is, is there any possibility of going back to the
    > statement that raised the error, re-execute the statement and
    > continue?
    >
    > Thanks for any thoughts and suggestions.
    >
    > Marco


    You can always use a loop:

    recover=True
    while True:
    try:
    ....
    ....
    aPoint = gp_Pnt(1.0, 0.0, 0.0) [1]
    ....
    ....
    except NameError, e:
    if recover:
    recover=False
    name = e.args[0].split[1]
    if isValid(name):
    doImport(name)
    else:
    raise e
    else:
    break
    , Mar 25, 2009
    #3
  4. Marco Nawijn

    Terry Reedy Guest

    Marco Nawijn wrote:

    > In short I would like to know if somebody knows if it is possible to
    > re-execute a statement that raised an exception?


    In short, no.

    > As an example, look at the following statement
    >
    >>> aPoint = gp_Pnt(1.0, 0.0, 0.0) # Oops, this will raise a NameError, since

    > # gp_Pnt class
    > is unknown
    >
    > NameError: name 'gp_Pnt' is not defined


    Either make sure names are defined before you use them (easy) or provide
    alternative code in the except clause.

    If you actually have the name written literally in the code, you should
    simply import it first, so I presume you have a more complicated use
    case where the name to use is dynamically defined in a string. If so,
    do something like

    aPoint = MyImporter(class_name)(1.0, 0.0, 0.0)

    where MyImporter.__new__ imports and returns the class

    Terry Jan Reedy
    Terry Reedy, Mar 25, 2009
    #4
  5. Also, instead of caching exceptions you can do lazy lookups kinda like
    this:

    -------------------------------------------------------------------------------------
    # a.py

    class A:
    pass

    -------------------------------------------------------------------------------------
    # b.py

    class B:
    pass

    -------------------------------------------------------------------------------------
    # c.py

    class D:
    pass

    class E:
    pass

    -------------------------------------------------------------------------------------
    # iface.py

    class LazyInterface(object):
    _attr_dict = dict(A='a', B='b', C='c', D='c')
    _attr_cache = dict()
    _mod_cache = dict()

    def __getattr__(self, name):
    if name in self._attr_cache:
    return self._attr_cache[name]

    elif name in self._attr_dict:
    module_name = self._attr_dict[name]
    self._mod_cache[module_name] =
    self._mod_cache.get(module_name,__import__(module_name))
    self._attr_cache[name] =
    getattr(self._mod_cache[module_name], name)
    return self._attr_cache[name]
    else: raise AttributeError


    >>> from iface import LazyInterface
    >>> i = LazyInterface()
    >>> a = i.A()
    >>> a

    <a.A instance at 0x7fb034317440>
    >>> c = i.C()
    >>> c

    <c.C instance at 0x7fb034317758>
    >>> d = i.D()
    >>> d

    <c.D instance at 0x7fb0343177a0>

    There is probably a cleaner/more robust way of doing the above, but you
    get the idea.
    Albert Hopkins, Mar 25, 2009
    #5
  6. Marco Nawijn

    Anton Hartl Guest

    On 2009-03-25, Marco Nawijn <> wrote:
    > Hello,
    >
    > In short I would like to know if somebody knows if it is possible to
    > re-execute a statement that raised an exception? I will explain the
    > reason by providing a small introduction on why this might be nice in
    > my case
    > and some example code.
    >
    > I am using the python bindings to a *very* large C++ library. About
    > 5000 classes divided over approx. 450 different
    > packages are exposed through the Python interface. To reduce the
    > number of import statements that need to be inserted and to limit the
    > number of wildcard imports it would be very helpful if class names
    > could be automatically imported from the proper module. There is no
    > problem in finding out the proper module given a (valid) class name.


    Maybe this helps:

    http://www.connellybarnes.com/code/autoimp/

    Regards,
    Anton
    Anton Hartl, Mar 25, 2009
    #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. et
    Replies:
    1
    Views:
    521
    Yunus Emre ALPÖZEN [MCSD.NET]
    Jun 29, 2005
  2. et
    Replies:
    0
    Views:
    144
  3. Rob R. Ainscough
    Replies:
    3
    Views:
    322
    Rob R. Ainscough
    Jun 14, 2005
  4. pizzy
    Replies:
    4
    Views:
    140
    pizzy
    Mar 18, 2005
  5. pizzy
    Replies:
    7
    Views:
    155
    pizzy
    Mar 23, 2005
Loading...

Share This Page