473,831 Members | 2,263 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 2113
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
7036
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
1983
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
2954
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
1761
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
1412
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
6962
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
6926
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
5758
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
4499
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
9642
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
10777
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
10494
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
10534
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
10208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7748
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3076
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.