Creating different classes dynamically?

Discussion in 'Python' started by Victor Hooi, Dec 10, 2012.

  1. Victor Hooi

    Victor Hooi Guest

    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
    Victor Hooi, Dec 10, 2012
    #1
    1. Advertising

  2. Victor Hooi

    Dave Angel Guest

    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
    Dave Angel, Dec 10, 2012
    #2
    1. Advertising

  3. Victor Hooi

    Victor Hooi Guest

    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
    Victor Hooi, Dec 10, 2012
    #3
  4. Victor Hooi

    Victor Hooi Guest

    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
    Victor Hooi, Dec 10, 2012
    #4
    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. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,924
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,192
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,392
    Andrew Thompson
    May 31, 2004
  4. Jonas
    Replies:
    0
    Views:
    159
    Jonas
    Sep 23, 2003
  5. T H
    Replies:
    2
    Views:
    137
    Marco Nawijn
    Jan 24, 2012
Loading...

Share This Page