[vtk-developers] New vtk packages; suggestions reqd. for other modules

David Gobbi dgobbi at irus.rri.ca
Tue Oct 9 00:49:18 EDT 2001


Hi Prabhu,

There is another alternative for the namespace separation, although it
is a little more complicated.  Also, it would allow 'from vtk import *'
to load everything, but it would not allow 'import vtk' to import
everything.

The 'vtk/__init__.py' would specify the list of packets through its
'__all__' variable as per standard package setup, and each of
vtk/common etc. modules would load all items into both their own
namespace and into the 'vtk' namespace, like so:

--------------------------------------------------
""" This module loads all the classes from the VTK Common library into its
namespace.  This is a required module."""

import os

if os.name == 'posix':
    from libvtkCommonPython import *
    import libvtkCommonPython
    __namespace = libvtkCommonPython
    del libvtkCommonPython
else:
    from vtkCommonPython import *
    import vtkCommonPython
    __namespace = vtkCommonPython
    del vtkCommonPython

del os

# copy all names into the vtk namespace
import vtk

for item in __namespace.__dict__.items():
    key, val = item
    if key[0:2] != '__':
        setattr(vtk, key, val)

del vtk
del __namespace
--------------------------------------------------

Like I said, a little ugly, but it
1) allows each module to be loaded individually, i.e. "import vtk.common"
   will not cause all modules to be silently loaded
2) puts everything in the 'vtk' namespace
3) assuming that the __all__ variable can be constructed properly (maybe
   as part of the build process?), then no special tricks will be needed
   for error reporting for optional modules
4) allows "from vtk import *" to load everything

but it would be necessary to have a 'everything' or 'all' module to load
everthing into the vtk namespace withough specifying individual package
names:

import vtk.everything

so it still doesn't seem ideal.

Thanks a lot for your work on this, btw.  I wish that I had more time to
participate in the discussions.

 - David


--
  David Gobbi, MSc                       dgobbi at irus.rri.ca
  Advanced Imaging Research Group
  Robarts Research Institute, University of Western Ontario

On Tue, 9 Oct 2001, Prabhu Ramachandran wrote:

> hi,
>
> Thanks for the feedback!
>
> >>>>> "DG" == David Gobbi <dgobbi at irus.rri.ca> writes:
>
> <snip>
>     >>>> import vtk.common
>
>     DG> the result is identical to if I do
>
>     >>>> import vtk
>
>     DG> because both cause vtk/__init__.py to be loaded.  The only way
>     DG> to import just the 'common' module is with "from vtk import
>     DG> common".
>
>     DG> The only work-around I can think of is to move the contents of
>     DG> vtk/__init__.py into another file called e.g. vtk/all.py, so
>     DG> that a simple "import vtk" doesn't cause any modules to be
>     DG> loaded.
>
> Yes, I know of this and mentioned it in one of my several mails and it
> bothered me too.  :) The only reason I did this was because it works
> and even though it is a little funny at first - this approach does
> give you the convenience of using just import vtk and also give you
> specific set of classes via vtk.common and friends.  Actually there
> are a couple of alternatives to using all.py.
>
>   (1) We change __import__ - dirty, forget it. :)
>
>   (2) We change the base directory of the package to some other name
>   like vtkpkg (or vtkpy) and stick vtk.py inside it or outside it.
>   The result.
>
>     import vtkpkg.common
>     # gives you *just* common
>     import vtkpkg.vtk
>     # gives you everything
>     # Alternatively if vtk.py is outside the package structure.
>     import vtk
>     # will do what we expect - give us everything.
>
> What do you folks say?
>
>     DG> Also, (also because vtk/__init__.py is being called) when I do
>
>     >>>> import vtk.parallel
>
>     DG> no error is generated even though I don't have "parallel"
>     DG> compiled.
>
> Ahhh I didnt check that case - I kept trying to handle link errors.
> This is easily fixed by doing a del sys.modules['vtk.pkg'] for all
> failed 'pkg's - that way you should see the error.  All it needs is a
> couple of lines in __helper.py.  Can commit it if it will make a
> difference.  Let me know.
>
>     DG> It might, in the end, be easier not use a directory structure,
>     DG> and to instead just have a "vtk.py", a "vtkcommon.py", etc.
>     DG> but only if the directory-structure approach doesn't work out.
>
> Which is why I wanted folks to comment.  Any approach is a compromise.
> Either you have to type more, or deal with some issue or the other
> etc.  So its up to us to see what is the best compromise. :) I know
> this is a pain since lots of us are busy, but I see no choice. :)
>
> Thanks again for the feedback!
>
> prabhu
> _______________________________________________
> vtk-developers mailing list
> vtk-developers at public.kitware.com
> http://public.kitware.com/mailman/listinfo/vtk-developers
>




More information about the vtk-developers mailing list