473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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':'bldSection','COMPOSITION ':'bldComposition','OBSERVATION':'bldObservation', 'ITEM_TREE':'bldItemTree'}

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':bldSection,
'COMPOSITION':bldComposition,
'OBSERVATION':bldObservation,
'ITEM_TREE':bldItemTree, # trailing comma allows easy additions
}

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

tjr
Jul 13 '08 #1
1 885
On Jul 13, 7:00*pm, Terry Reedy <tjre...@udel.eduwrote:
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':'bldSection','COMPOSITION ':'bldComposition','OBSER VATION':'bldObservation','ITEM_TREE':'bldItemTree' }
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':bldSection,
* *'COMPOSITION':bldComposition,
* *'OBSERVATION':bldObservation,
* *'ITEM_TREE':bldItemTree, # 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(self):
...
def OBSERVATION(self):
...
def ITEM_TREE(self):
...

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

--
Paul Hankin
Jul 13 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Esmeralda Weatherwax | last post by:
For 10 days now, a new online judge (a system for the verification of the correctness of submitted programs, which solve problems selected from a repository) has been available to the public. ...
0
by: Maurizio Colucci | last post by:
Hello, I am just beginning with python on suse 9.1. The package python-devel is installed. I am writing from functional import * and I get "no module named functional".
8
by: beza1e1 | last post by:
I see myself shifting more and more over to the functional kind of coding. Could be related to the Haskell, we had to learn in CS. Now i was wondering, how other people use Python? With...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
17
by: Jon Harrop | last post by:
Read our free introduction to the F# programming language from Microsoft Research: http://www.ffconsultancy.com/products/fsharp_journal/free/introduction.html?u4 Register your interest in our...
15
by: Lorenzo Stella | last post by:
Hi all, I haven't experienced functional programming very much, but now I'm trying to learn Haskell and I've learned that: 1) in functional programming LISTS are fundmental; 2) any "cycle" in FP...
5
by: Keith Thompson | last post by:
Ertugrul Söylemez <es@ertes.dewrites: Of the four programs you posted, three of them depend on a non-standard extension, namely nested function definitions. As such, they are, in my opinion,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.