Recommendation on building a TopicDefnProvider

18 views
Skip to first unread message

Josh English

unread,
Jun 4, 2013, 6:23:22 PM6/4/13
to pypu...@googlegroups.com
I'm building an XML Topic Defn Provider for PubSub.

Right now I'm searching the stored xml tree with every call to getDefn(). 

Does this really slow down the messaging process all that much? Would it be less memory/time consumptive just to parse the XML at the beginning?

Any thoughts?

Josh

oliver

unread,
Jun 4, 2013, 9:54:29 PM6/4/13
to PyPubSub
Hi Josh, 

I have never benchmarked different representations but I expect that using a tree representation in the language itself (such as I do currently in pubsub using classes and methods) is the fastest of all (pure python) alternatives. So why not create a converter that converts the in-memory XML element tree to the class structure that TopicDefnProvider expects? In fact I believe there is already some scafolding in pubsub to do this: I vaguely remember an ITopicDefnProvider interface (don't have the code handy) in core, your constructor would take the xml and create the class structure. 

To dynamically create classes, take a look at this: http://henry.precheur.org/python/Dynamically%20create%20a%20type%20with%20Python, very easy to do (but I have not tested!). You would create a recursive function, the whole thing would probably take you 20 lines including the additional lines to create the listener functions, docstring, etc from the xml. 

I'm curious to hear how that goes so please keep me posted!



Josh

--
You received this message because you are subscribed to the Google Groups "pypubsub" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pypubsub+u...@googlegroups.com.
To post to this group, send email to pypu...@googlegroups.com.
Visit this group at http://groups.google.com/group/pypubsub?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Josh English

unread,
Jun 6, 2013, 1:05:38 PM6/6/13
to pypu...@googlegroups.com
Here's what I have. (If this double posts, I'm sorry).

I chose to parse the tree during creation. It propagates arguments down the tree, but when I export the topic tree, the required arguments don't show up. That's a bug I haven't figured out yet.
xmltopicdefnprovider.py

oliver

unread,
Jun 10, 2013, 1:37:10 PM6/10/13
to PyPubSub
Hi Josh, looks good. I'd like to suggest the following: make scheme self-documenting and extendable. If I were to add such a class to pubsub core or utils, I would spec it as such: 

xml = """<topicdefntree>
<!-- this is a comment -->
<topic id="parent">
    <description>Parent with a parameter and subtopics</description>
    <listenerspec>
        <arg id="name" optional="true">A name, for some reason</arg>
        <arg id="lastname">last name</arg>
    </listenerspec>
    <topic id="child">
        <description>This is the first child</description>
        <listenerspec>
            <arg id="nick">A nickname</arg>
        </listenerspec>
    </topic>
</topic>
</topicdefntree>
"""

I think you will find you require fewer lines of code too. 
Cheers, 
Oliver

Josh English

unread,
Jun 11, 2013, 3:19:00 PM6/11/13
to pypu...@googlegroups.com
You're right, I should make it clearer. I've been under the influence
of bad XML lately.
Josh English
Joshua.R...@gmail.com
http://www.joshuarenglish.com
Reply all
Reply to author
Forward
0 new messages