Re: Functional/Best?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Reedy

    Re: Functional/Best?



    Tim Cook wrote:
    I guess I can classify my application(s) as more procedural than
    anything else. But I have a question about the best way to handle
    something in Python.
    >
    When given a mapping of keywords, I want to call a function based on a
    certain keyword found when parsing a text file. The mapping looks like
    this:
    >
    definClassMap={ 'SECTION':'bldS ection','COMPOS ITION':'bldComp osition','OBSER VATION':'bldObs ervation','ITEM _TREE':'bldItem Tree'}
    >
    So if the text file contains 'ITEM_TREE' I want to call bldItemTree
    which creates an instance of the class ItemTree.
    >
    I currently use an if ..., elif ... construct.
    Is there a better, more efficient, more Pythonic way of doing this?
    Yes. Create a mapping of keywords to function objects rather than to
    function names!

    def bldSection(): <whatever>
    ....
    def bldItemTree(): <whatever else>

    class_map={
    'SECTION':bldSe ction,
    'COMPOSITION':b ldComposition,
    'OBSERVATION':b ldObservation,
    'ITEM_TREE':bld ItemTree, # trailing comma allows easy additions
    }

    for word in parselist:
    try;
    class_map[word]()
    except KeyError:
    <whatever for non-keys>

    tjr


  • Paul Hankin

    #2
    Re: Functional/Best?

    On Jul 13, 7:00 pm, Terry Reedy <tjre...@udel.e duwrote:
    Tim Cook wrote:
    I guess I can classify my application(s) as more procedural than
    anything else.  But I have a question about the best way to handle
    something in Python.
    >
    When given a mapping of keywords, I want to call a function based on a
    certain keyword found when parsing a text file.  The mapping looks like
    this:
    >
    definClassMap={ 'SECTION':'bldS ection','COMPOS ITION':'bldComp osition','OBSER VATION':'bldObs ervation','ITEM _TREE':'bldItem Tree'}
    >
    So if the text file contains 'ITEM_TREE'  I want to call bldItemTree
    which creates an instance of the class ItemTree.  
    >
    I currently use an if ..., elif ... construct.
    Is there a better, more efficient, more Pythonic way of doing this?
    >
    Yes. Create a mapping of keywords to function objects rather than to
    function names!
    >
    def bldSection(): <whatever>
    ...
    def bldItemTree(): <whatever else>
    >
    class_map={
       'SECTION':bldSe ction,
       'COMPOSITION':b ldComposition,
       'OBSERVATION':b ldObservation,
       'ITEM_TREE':bld ItemTree, # trailing comma allows easy additions
    >
    }
    >
    for word in parselist:
       try;
         class_map[word]()
       except KeyError:
         <whatever for non-keys>
    A nice variant of this, which minimizes repetition, is to group all
    the factory methods together into a class, naming them the same as the
    keyword...

    class Builder(object) :
    def SECTION(self):
    ...
    def COMPOSITION(sel f):
    ...
    def OBSERVATION(sel f):
    ...
    def ITEM_TREE(self) :
    ...

    builder = Builder()
    for word in parse_list:
    item = getattr(builder , word)()
    ...

    --
    Paul Hankin

    Comment

    Working...