473,944 Members | 20,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dispatch tables in Python 2.4

<Still experimenting with the new features of Python 2.4 ...>

A thing I always had in my wishlist was the ability to define dispatch
tables in an elegant way. Currently, Python is butt ugly:

def function_with_a _long_name_1():
"do this"

def function_with_a _long_name_2():
"do that"

dispatch_table = dict(
function_with_a _long_name_1 = function_with_a _long_name_1,
function_with_a _long_name_2 = function_with_a _long_name_2,
)

Really horrible and error prone (especially for a poor typist like
myself). With decorators I can do

def add_to(lst):
def closure(func):
lst.append(func )
return func
return closure

lst = []

@add_to(lst)
def function_with_a _long_name_1():
"do this"

@add_to(lst)
def function_with_a _long_name_2():
"do that"

dispatch_table = dict((f.func_na me,f) for f in lst)
#!generator-comprehension!

With the advent of decorators I see myself using more and more
closures.

BTW, what's the status of the so called "functional " module? I think
there
was a rumor of such a module scheduled for Python 2.4 or 2.5. To play
with
decorators we really need some standard way to compose functions and
to do
partial application of arguments.

Just my 2c,
Michele Simionato

P.S. decorators make also easy to abuse classes for making dispatch
tables:

class DispatchTable(o bject):

@staticmethod
def function_with_a _long_name_1():
"do this"

@staticmethod
def function_with_a _long_name_2():
"do that"
But I somewhat prefer dispatch_table[function_name] over
getattr(Dispatc hTable, function_name).
Jul 18 '05 #1
1 2117
mi************* **@gmail.com (Michele Simionato) writes:
BTW, what's the status of the so called "functional " module? I think
there was a rumor of such a module scheduled for Python 2.4 or 2.5.
To play with decorators we really need some standard way to compose
functions and to do partial application of arguments.


PEP 309 is marked as "Accepted", and an implementation patch exists.
The PEP needs updating, and there are possibly a few more procedural
things to do before the code is checked in. I emailed the author of
the PEP on Friday, asking about this. I think it's mainly lack of
tuits.

The PEP only covers partial application, BTW.

Paul.
--
Any sufficiently advanced technology is indistinguishab le from magic.
-- Arthur C. Clarke
Jul 18 '05 #2

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

Similar topics

3
7045
by: RJ | last post by:
Hi, I've been going over the Quick Start to Client side COM and Python and many other sources, but cannot find an example that will get my com/ActiveX .ocx USB device driver imported. The Excel and Word examples run fine. win32com.client.Dispatch("Excel.Application") etc., but how does one know the ("<foo>.<bar>") to type in? Excel looks only vaguely like "Excel.Application" in the com browser.
0
1993
by: Thomas Heller | last post by:
I'm currently reinventing the wheel ;-), implementing 'clones' for the win32com.client.Dispatch and DispatchWithEvents functions using ctypes. It works fairly well, although the more complicated data types like SAFEARRAY or RECORD are not yet implemented. The win32com.client versions of these functions require makepy support (for DispatchWithEvents), and create this on demand. The ctypes versions don't require something similar, they...
31
2966
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would using myclass.len() instead of myclass.__len__() really cause Python considerable harm? As much as I adore Python, I have to admit, I find this to be one of the language's most "unPythonic" features and a key arguing point against Python. I've...
4
1769
by: The Stevemeister | last post by:
Hi, I have an entity class (from a rendering engine) with about 100 functions now that are virtual. So far no slowdowns, but since I've done alot of work with OWL and MFC over the years, and I know that when the virtual functions get too many, OWL and MFC result to dispatch tables. Question is, at what point do I say, "At what point do I switch to dispatch tables?"
3
1420
by: talin at acm dot org | last post by:
Been playing around a bit more with developing my python inference engine, and I thought it might be of general interest (plus, I am finding the criticism useful). My original goal was to brush up on my math skills. Now, I've long felt that the best way to learn a language *thoroughly* is to write a compiler for it. So why not write a compiler for math? However, algebra and calculus aren't just about evaluating an expression and...
3
6981
by: tyler.schlosser | last post by:
Hi there, I am trying to launch a program called AmiBroker using the command: AB = win32com.client.Dispatch("Broker.Application") However, I have a dual-core CPU and would like to launch two instances of AmiBroker. I know it is possible to run two instances simultaneously since it is easy to do manually by double-clicking the AmiBroker.exe file twice. However, when I write two lines of code like this:
4
6955
by: mirandacascade | last post by:
O/S : Win2K vsn of Python: 2.4 Hoping to find information that provide information about error messages being encountered. Pythonwin session: Traceback (most recent call last): File "<interactive input>", line 1, in ?
2
5777
by: lialie | last post by:
Hi, Maybe it 's quite simple, but I can't fix it. Do I make some mistakes in my env setting? My excel version is 2003. any suggestion? Thanks. Traceback (most recent call last): File "testexcel.py", line 3, in ? excel = Dispatch("Excel.Application") File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 95, in
1
4506
by: Cupric | last post by:
I have a python script that runs fine from the command line or from within IDLE, but doesn't work through the Vista Task Scheduler. The script downloads some csv files and then uses pywin32 to combine the csv files into a single document. When I run it through the task scheduler, it downloads the csv files, but then doesn't seem to launch excel. I can't figure out what is wrong or how to add enough logging to tell. I'm using Python...
0
10143
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9971
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11543
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11134
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11306
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9868
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3518
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.