From: Karol L. <kar...@kn...> - 2007-02-03 09:27:31
|
On Friday 02 of February 2007 20:35, Noel O'Boyle wrote: > Just been reading today that instead of a massive switch statement > it's better to use a dictionary of functions. It's not exactly the > same, but it makes me think of something like... > > for line in file: > for keyphrase,blockname in keyphrases: > if line.find(keyphrase)>=0: > extract[blockname]() > > e.g. keyphrase("Number of MOs") and corresponding blockname is 'MO section' I thought about this also - extracting a new attribute is then just adding a new entry to that dict, very conscise - but this would take some more work from where we are (which I'm willign to do). One question remains, where would the functions be defined? As methods of the class? As locals in the module? Directly inside the dictionary? There's one more problem, the "keyphrase conditions" are various, some conditions check more than just the current line - some have more than one possible keyphrase, some have additional conditions. So there would have to be another dict of functions that check the conditions specific to the parser and attribute. So I thought that that was too much change at one time. > I'm not sure why you think it should return a tuple though (this makes > things more difficult). Surely it can just set the attributes from > instead the extract function. Not necessarily a tuple, but subscriptable, because some conditional blocks extract more than one attribute at a time. Now that I look at it, seems superfluous, since the type checks can go after the extracting, and progress isn't ever updated twice in the same block. Karol -- written by Karol Langner Sat Feb 3 10:08:29 CET 2007 |