Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34530
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!news2.arglkargh.de!diablo1.news.osn.de!news.osn.de!diablo2.news.osn.de!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 | <d@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'root': 0.04; 'base.': 0.05; 'arguments': 0.07; 'parser': 0.07; 'root,': 0.07; 'try:': 0.07; 'type,': 0.07; '__init__': 0.09; 'dict': 0.09; 'files:': 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; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; '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; 'right.': 0.27; 'separate': 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; 'e.g.': 0.30; 'code': 0.31; 'file': 0.32; 'says': 0.33; 'done,': 0.33; 'like:': 0.33; 'hi,': 0.33; 'skip:d 20': 0.34; 'whatever': 0.35; 'mapping': 0.35; 'doing': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'xml': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'sure': 0.38; 'received:192': 0.39; 'easily': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'identify': 0.61; 'different': 0.63; 'more': 0.63; 'subject': 0.66; 'skip:c 50': 0.66; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'walk': 0.71; 'reply-to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84; 'tree,': 0.84; 'victor': 0.84 |
| Date | Sun, 09 Dec 2012 19:53:30 -0500 |
| From | Dave Angel <d@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 |
| MIME-Version | 1.0 |
| To | Victor Hooi <victorhooi@gmail.com> |
| Subject | Re: Creating different classes dynamically? |
| References | <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> |
| In-Reply-To | <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:6QVXyxymRcQHndD8MJ6C5tg/hyh/iMMsJ5bKQ7+V6ab rXgICVz4RDWf4hdNAA5ZkXn3OhLicYXlFtfjihlJzN/ULNQLnu dgD8/JhzwmBgOIdfxy0qtLssgJjPu1p97aWuLq9CuQ56Io1zXp kPNeeB7NhNy6pk/OjjGsDlJ+xGliFVkScx7CzmDdWWQZI2NzzN Ok+IEkb3PA51jjga0FELbPhOw1c9kNtci6e6OjBWvkOZF61pgT WUzFJhnAdhg8BfvbQtb4uQ8VO4j8QVRts5sILGZhCH/VPMqmp+ XWxCYS/nwJFTml8Qm8NaurHVrz8pJvS2+4D1E+p6dSPshYIug= = |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | d@davea.name |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.665.1355100892.29569.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1355100892 news.xs4all.nl 6870 [2001:888:2000:d::a6]:59728 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:34530 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web