Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34527 > unrolled thread

Creating different classes dynamically?

Started byVictor Hooi <victorhooi@gmail.com>
First post2012-12-09 16:35 -0800
Last post2012-12-09 17:34 -0800
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Creating different classes dynamically? Victor Hooi <victorhooi@gmail.com> - 2012-12-09 16:35 -0800
    Re: Creating different classes dynamically? Dave Angel <d@davea.name> - 2012-12-09 19:53 -0500
      Re: Creating different classes dynamically? Victor Hooi <victorhooi@gmail.com> - 2012-12-09 17:34 -0800
      Re: Creating different classes dynamically? Victor Hooi <victorhooi@gmail.com> - 2012-12-09 17:34 -0800

#34527 — Creating different classes dynamically?

FromVictor Hooi <victorhooi@gmail.com>
Date2012-12-09 16:35 -0800
SubjectCreating different classes dynamically?
Message-ID<6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com>
Hi,

I have a directory tree with various XML configuration files.

I then have separate classes for each type, which all inherit from a base. E.g.

    class AnimalConfigurationParser:
        ...

    class DogConfigurationParser(AnimalConfigurationParser):
        ...

    class CatConfigurationParser(AnimalConfigurationParser):
        ....

I can identify the type of configuration file from the root XML tag.

I'd like to walk through the directory tree, and create different objects based on the type of configuration file:

    for root, dirs, files in os.walk('./'):
        for file in files:
            if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file:
                with open(os.path.join(root, file), 'r') as f:
                    try:
                        tree = etree.parse(f)
                        root = tree.getroot()
                        print(f.name)
                        print(root.tag)
                        # Do something to create the appropriate type of parser
                    except xml.parsers.expat.ExpatError as e:
                        print('Unable to parse file {0} - {1}'.format(f.name, e.message))

I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this?

    root_tags = {
                   'DogRootTag': DogConfigurationParser(),
                    'CatRootTag': CatConfigurationParser(),
    }

Cheers,
Victor

[toc] | [next] | [standalone]


#34530

FromDave Angel <d@davea.name>
Date2012-12-09 19:53 -0500
Message-ID<mailman.665.1355100892.29569.python-list@python.org>
In reply to#34527
On 12/09/2012 07:35 PM, Victor Hooi wrote:
> Hi,
>
> I have a directory tree with various XML configuration files.
>
> I then have separate classes for each type, which all inherit from a base. E.g.
>
>     class AnimalConfigurationParser:
>         ...
>
>     class DogConfigurationParser(AnimalConfigurationParser):
>         ...
>
>     class CatConfigurationParser(AnimalConfigurationParser):
>         ....
>
> I can identify the type of configuration file from the root XML tag.
>
> I'd like to walk through the directory tree, and create different objects based on the type of configuration file:
>
>     for root, dirs, files in os.walk('./'):
>         for file in files:
>             if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file:
>                 with open(os.path.join(root, file), 'r') as f:
>                     try:
>                         tree = etree.parse(f)
>                         root = tree.getroot()
>                         print(f.name)
>                         print(root.tag)
>                         # Do something to create the appropriate type of parser
>                     except xml.parsers.expat.ExpatError as e:
>                         print('Unable to parse file {0} - {1}'.format(f.name, e.message))
>
> I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this?
>
>     root_tags = {
>                    'DogRootTag': DogConfigurationParser(),
>                     'CatRootTag': CatConfigurationParser(),
>     }
>
> Cheers,
> Victor

Your subject line says you want to create the classes dynamically, but
that's not what your code implies.  if you just want to decide which
class to INSTANTIATE dynamically, that's easily done, and you have it
almost right.  In your dict you should leave off those parentheses.



Then the parser creation looks something like:
               parser_instance = root_tags[root.tag] (arg1, arg2)

where the arg1, arg2 are whatever arguments the __init__ of these
classes expects.

(untested)

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#34535

FromVictor Hooi <victorhooi@gmail.com>
Date2012-12-09 17:34 -0800
Message-ID<bae8f804-35c4-4857-9ecc-53a9bd2bb128@googlegroups.com>
In reply to#34530
heya,

Dave: Ahah, thanks =).

You're right, my terminology was off, I want to dynamically *instantiate*, not create new classes.

And yes, removing the brackets worked =).

Cheers,
Victor

On Monday, 10 December 2012 11:53:30 UTC+11, Dave Angel  wrote:
> On 12/09/2012 07:35 PM, Victor Hooi wrote:
> 
> > Hi,
> 
> >
> 
> > I have a directory tree with various XML configuration files.
> 
> >
> 
> > I then have separate classes for each type, which all inherit from a base. E.g.
> 
> >
> 
> >     class AnimalConfigurationParser:
> 
> >         ...
> 
> >
> 
> >     class DogConfigurationParser(AnimalConfigurationParser):
> 
> >         ...
> 
> >
> 
> >     class CatConfigurationParser(AnimalConfigurationParser):
> 
> >         ....
> 
> >
> 
> > I can identify the type of configuration file from the root XML tag.
> 
> >
> 
> > I'd like to walk through the directory tree, and create different objects based on the type of configuration file:
> 
> >
> 
> >     for root, dirs, files in os.walk('./'):
> 
> >         for file in files:
> 
> >             if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file:
> 
> >                 with open(os.path.join(root, file), 'r') as f:
> 
> >                     try:
> 
> >                         tree = etree.parse(f)
> 
> >                         root = tree.getroot()
> 
> >                         print(f.name)
> 
> >                         print(root.tag)
> 
> >                         # Do something to create the appropriate type of parser
> 
> >                     except xml.parsers.expat.ExpatError as e:
> 
> >                         print('Unable to parse file {0} - {1}'.format(f.name, e.message))
> 
> >
> 
> > I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this?
> 
> >
> 
> >     root_tags = {
> 
> >                    'DogRootTag': DogConfigurationParser(),
> 
> >                     'CatRootTag': CatConfigurationParser(),
> 
> >     }
> 
> >
> 
> > Cheers,
> 
> > Victor
> 
> 
> 
> Your subject line says you want to create the classes dynamically, but
> 
> that's not what your code implies.  if you just want to decide which
> 
> class to INSTANTIATE dynamically, that's easily done, and you have it
> 
> almost right.  In your dict you should leave off those parentheses.
> 
> 
> 
> 
> 
> 
> 
> Then the parser creation looks something like:
> 
>                parser_instance = root_tags[root.tag] (arg1, arg2)
> 
> 
> 
> where the arg1, arg2 are whatever arguments the __init__ of these
> 
> classes expects.
> 
> 
> 
> (untested)
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

[toc] | [prev] | [next] | [standalone]


#34536

FromVictor Hooi <victorhooi@gmail.com>
Date2012-12-09 17:34 -0800
Message-ID<mailman.668.1355105479.29569.python-list@python.org>
In reply to#34530
heya,

Dave: Ahah, thanks =).

You're right, my terminology was off, I want to dynamically *instantiate*, not create new classes.

And yes, removing the brackets worked =).

Cheers,
Victor

On Monday, 10 December 2012 11:53:30 UTC+11, Dave Angel  wrote:
> On 12/09/2012 07:35 PM, Victor Hooi wrote:
> 
> > Hi,
> 
> >
> 
> > I have a directory tree with various XML configuration files.
> 
> >
> 
> > I then have separate classes for each type, which all inherit from a base. E.g.
> 
> >
> 
> >     class AnimalConfigurationParser:
> 
> >         ...
> 
> >
> 
> >     class DogConfigurationParser(AnimalConfigurationParser):
> 
> >         ...
> 
> >
> 
> >     class CatConfigurationParser(AnimalConfigurationParser):
> 
> >         ....
> 
> >
> 
> > I can identify the type of configuration file from the root XML tag.
> 
> >
> 
> > I'd like to walk through the directory tree, and create different objects based on the type of configuration file:
> 
> >
> 
> >     for root, dirs, files in os.walk('./'):
> 
> >         for file in files:
> 
> >             if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file:
> 
> >                 with open(os.path.join(root, file), 'r') as f:
> 
> >                     try:
> 
> >                         tree = etree.parse(f)
> 
> >                         root = tree.getroot()
> 
> >                         print(f.name)
> 
> >                         print(root.tag)
> 
> >                         # Do something to create the appropriate type of parser
> 
> >                     except xml.parsers.expat.ExpatError as e:
> 
> >                         print('Unable to parse file {0} - {1}'.format(f.name, e.message))
> 
> >
> 
> > I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this?
> 
> >
> 
> >     root_tags = {
> 
> >                    'DogRootTag': DogConfigurationParser(),
> 
> >                     'CatRootTag': CatConfigurationParser(),
> 
> >     }
> 
> >
> 
> > Cheers,
> 
> > Victor
> 
> 
> 
> Your subject line says you want to create the classes dynamically, but
> 
> that's not what your code implies.  if you just want to decide which
> 
> class to INSTANTIATE dynamically, that's easily done, and you have it
> 
> almost right.  In your dict you should leave off those parentheses.
> 
> 
> 
> 
> 
> 
> 
> Then the parser creation looks something like:
> 
>                parser_instance = root_tags[root.tag] (arg1, arg2)
> 
> 
> 
> where the arg1, arg2 are whatever arguments the __init__ of these
> 
> classes expects.
> 
> 
> 
> (untested)
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web