Hi Brian, why not take it the next step and
> for key in metadata_dict:
> if data.startswith(key):
> exec('''self.%s = """%s"""''' %(metadata_dict[key],
> data[len(key):]))
> # triple quotes as there may be quotes in metadata
> # values
> break
self.foo = {}
for key in metadata_dict.keys(): #? I got confused, so I guessed.
if data.startswith(key):
self.foo[metadata_dict[key]]=data[len(key):]
And then instead of self.x (if metadata_dict[key] = x] You just call
self.foo['x']
A bit more obfuscated, but it would seem to remove the exec, although
I'm not sure how else it impacts your class.
I came across something similar when using Pythoncard, because
Pythoncard stores widgets as a dictionary.
To crate a widget via a method in your card, you'd use -
self.components.myButton = {'properties'}
But to generate widgets based on data input, I'd use a dictionary
generated by the data -
a = raw_input('name? ')
widgDict = {a: {dict of properties}}
for (key, val) in widgDict.items():
self.components[key]=val
(Which incidentally is why Pythoncard rocks)
So yeah, hope that helps a wee bit.
Regards,
Liam Clarke
On Tue, 15 Feb 2005 17:19:37 -0500, Brian van den Broek
<[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I'm still plugging away at my project of writing code to process
> treepad files. (This was the task which I posted about in the recent
> "help with refactoring needed -- which approach is more Pythonic?"
> thread.)
>
> My present problem is how best to reorganize a long (20 elements) elif
> chain. The file format I am dealing with organizes itself with a file
> header, and then a series of nodes. Each node has a header filled with
> up to 20 different metadata elements, followed by the node content
> proper. These metadata elements can be in arbitrary order, and need
> not all be present.
>
> My Node class defines a _parse method which separates out the node
> header, and sends those lines to a _parse_metadata method. This is
> where the elif chain occurs -- each line of the metadata starts with a
> tag like "dt=" and I need to recognize each tag and set the
> appropriate Node object attribute, such as .document_type. (I do not
> want to rely on the unhelpful names for the tags in the file format,
> preferring more self-documenting attribute names.)
>
> I've come up with *a* way to use a dictionary dispatch, but I'll wager
> a great deal it isn't the *best* way.
>
> Here is a minimal illustration of what I have come up with:
>
> <code>
> class A:
>
> def __init__(self):
>
> self.something = None
> self.something_else = None
> self.still_another_thing = None
>
> def update(self, data):
>
>
> metadata_dict = {'something_tag=': 'something',
> '2nd_tag=': 'something_else',
> 'last=': 'still_another_thing'}
>
> a = A()
> print a.still_another_thing
> a.update('last=the metadata value for the "last=" metadata tag')
> print a.still_another_thing
> </code>
>
> <output>
> >>>
> None
> the metadata value for the "last=" metadata tag
> </output>
>
> So, it works. Yay :-)
>
> But, should I be doing it another way?
>
> Also, I know the general security concerns about things like exec.
> They make me nervous in using it, even though I am (as yet) the sole
> user. Am I right in thinking that the constrained way I am using it
> here protects me? My code uses most of the attributes as a simple
> storage container for later rewriting of the file, but in a few cases
> they enter into (safe seeming) conditionals like:
>
> if 'text' == self.document_type:
> self.do_text_stuff()
> if 'RTF' == self.document_type:
> self.do_RTF_stuff()
>
> Thanks and best to all,
>
> Brian vdB
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor