Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'root': 0.04; 'base.': 0.05; 'arguments': 0.07; 'classes.': 0.07; 'dynamically': 0.07; 'parser': 0.07; 'root,': 0.07; 'try:': 0.07; 'type,': 0.07; '__init__': 0.09; 'brackets': 0.09; 'dict': 0.09; 'files:': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'files.': 0.13; "'r')": 0.16; 'dirs,': 0.16; 'instantiate': 0.16; 'pythonic': 0.16; '{0}': 0.16; 'wrote:': 0.17; 'appropriate': 0.20; 'file:': 0.22; 'parse': 0.22; "i'd": 0.22; 'cheers,': 0.23; 'monday,': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'leave': 0.26; 'cc:addr:gmail.com': 0.27; 'right.': 0.27; 'separate': 0.27; 'cc:2**2': 0.27; 'tree': 0.27; 'this?': 0.28; 'decide': 0.28; 'objects': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; "skip:' 10": 0.30; 'worked': 0.30; 'e.g.': 0.30; 'code': 0.31; 'december': 0.32; 'file': 0.32; 'says': 0.33; 'done,': 0.33; 'like:': 0.33; 'hi,': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'whatever': 0.35; 'mapping': 0.35; 'doing': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'xml': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'received:209.85.214': 0.39; 'easily': 0.39; 'where': 0.40; 'your': 0.60; 'identify': 0.61; 'different': 0.63; 'more': 0.63; 'subject': 0.66; 'skip:c 50': 0.66; 'walk': 0.71; 'off,': 0.84; 'received:209.85.214.184': 0.84; 'received:mail- ob0-f184.google.com': 0.84; 'tree,': 0.84; 'victor': 0.84; 'angel': 0.93 Newsgroups: comp.lang.python Date: Sun, 9 Dec 2012 17:34:16 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=202.147.46.147; posting-account=LMx4fAoAAAAL3v616YFvkt1ueXc1H63- References: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 202.147.46.147 MIME-Version: 1.0 Subject: Re: Creating different classes dynamically? From: Victor Hooi To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: Victor Hooi , d@davea.name, python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 134 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355105479 news.xs4all.nl 6860 [2001:888:2000:d::a6]:58936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34536 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