[Chicago] create all classes

Kumar McMillan kumar.mcmillan at gmail.com
Fri Feb 1 18:06:33 CET 2008


On Feb 1, 2008 10:32 AM, Kumar McMillan <kumar.mcmillan at gmail.com> wrote:
> you want
>
> import blocks, types
> for name in dir(blocks):
>     TheClass = getattr(blocks, name)
>     if not type(TheClass)==types.ClassType:
>         continue
>     c = TheClass()

mm, coffee, much better.

I meant:

if not type(TheClass)==type:
    continue

(to detect new-style classes) but you're probably better off giving
all your 30 classes a common base class, i.e. MessageControl, and
checking like this:

if not type(TheClass)==type:
    continue
if not issubclass(TheClass, MessageControl):
    continue
c = TheClass()
# more stuff

>
> you might also need weed out any classes that are not the ones you are
> trying to instantiate
>
>
> On Feb 1, 2008 10:26 AM, Lukasz Szybalski <szybalski at gmail.com> wrote:
> > Hello,
> > I just converted big txt file definition to classes in .py file.
> >
> > There are around 30 classes in the file. How can I iterate through
> > that file and initiate each class to some object in a list?
> >
> > ---blocks.py
> >
> > class MSG_CTRL_BLK(object):
> >     def __init__(self):
> >         self.block='025A'
> >         self.GMSLOC=Element(self.block,'GLOC','MESSAGE HEADER','AN',26, 1)
> >
> > so instead of typing :
> > a=MSG_CTRL_BLK()
> > ....
> > ...
> > 30 times
> > ....
> >
> > I want:
> > Outside of the file I would like to :
> > import blocks
> > myblocks=[]
> > for i in blocks.xx???Xx:
> >     myblocks.append( i() )
> >
> > what would be the function to get me list of classes?
> > How can I iterate and create them on a fly.
> >
> > Lucas
> > _______________________________________________
> > Chicago mailing list
> > Chicago at python.org
> > http://mail.python.org/mailman/listinfo/chicago
> >
>


More information about the Chicago mailing list