Hello All,
Because of my poorly designing a database, I have recently found it necessary
to explore the wonders of the Python pseudo-switch:
do_case = { "A" : lambda x: x["bob"],
"B" : lambda x: x["carol"],
"C" : lambda x: "Ted",
"D" : lambda x: do_something(x) }
my_thing = do_case[get_value_from_ thin_air()](adict)
How to handle this kind of thing when lambda is removed from the language,
beside the obvious def'ing those tiny little functions?
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/ 3 1840
James Stroud wrote: Hello All,
Because of my poorly designing a database, I have recently found it
necessary to explore the wonders of the Python pseudo-switch:
do_case = { "A" : lambda x: x["bob"], "B" : lambda x: x["carol"], "C" : lambda x: "Ted", "D" : lambda x: do_something(x) }
my_thing = do_case[get_value_from_ thin_air()](adict)
How to handle this kind of thing when lambda is removed from the
language, beside the obvious def'ing those tiny little functions?
You can always re-invent lambda. def lambda2(arg_nam es, expression):
.... exec 'def _(%s): return %s' % (','.join(arg_n ames), expression)
.... return _
.... add = lambda2(('x', 'y'), 'x + y') add(3, 4)
7
James Stroud wrote: Hello All,
Because of my poorly designing a database, I have recently found it
necessary to explore the wonders of the Python pseudo-switch:
do_case = { "A" : lambda x: x["bob"], "B" : lambda x: x["carol"], "C" : lambda x: "Ted", "D" : lambda x: do_something(x) }
class CaseThing:
def pref_A (self, x):
return x["bob"]
def pref_B (self, x):
return x["carol"]
def pref_C (self, x);
return "Ted"
def pref_D (self, x)
return do_something(x)
def getThing (self, x):
attr = getattr(self, 'pref_%s' % (x,))
if attr is not None:
return attr
else:
raise SomeError("Thin g %s does not exist" % (x,))
my_thing = CaseThing().get Thing(get_value _from_thin_air) (adict)
You can do something similar with method decorators for more complex
strings than what's allowed for python method names.
my_thing = do_case[get_value_from_ thin_air()](adict)
How to handle this kind of thing when lambda is removed from the
language, beside the obvious def'ing those tiny little functions?
James
-- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095
http://www.jamesstroud.com/
# -*- python -*-
import sys
def switchFor (target):
sw = '__switch_table __'
# please pretend the frame hack is not here,
# it happens to work for classes and messing with
# frame stack in a try/except is probably okay
try:
raise Exception()
except:
defs = sys.exc_info()[2].tb_frame.f_bac k.f_locals
if sw not in defs:
defs[sw] = {}
table = defs[sw]
def _(meth):
table[target] = meth
return meth
return _
class SwitchMixin (object):
def __init__ (self):
self.__switch_t able__ = self.__switch_t able__.copy()
def switchOn (self, target, *args, **kw):
return self.__switch_t able__[target](self, *args, **kw)
if __name__ == '__main__':
class SwitchTest(Swit chMixin):
@switchFor("foo ")
def switch (self, arg):
print arg * 3
@switchFor("bar ")
def switch (self, arg):
print "__%s__" % (arg,)
@switchFor("baz ")
def switch (self, arg):
print arg + ''.join(reverse d(arg))
st = SwitchTest()
st.switchOn("fo o", "oof")
st.switchOn("ba r", "rab")
st.switchOn("ba z", "zab") This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bo Peng |
last post by:
Dear Python group:
I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.
I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can...
|
by: seberino |
last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters".
He claims he developed a web app at light speed using Lisp and lots
of macros.
It got me curious if Lisp
is inherently faster to develop complex apps in. It would seem if you
could create your own language in Lisp using macros that that would be
quite an advantage....
I realize that Python has operator overloading and OOP so I'm not sure.
|
by: Maurice LING |
last post by:
Hi,
I am wondering if Python has services or frameworks that does the same
as Java RMI?
What I am seeking is to do "pseudo-clustering". That is, a server will
contains a program to control what is needed for execution. This will be
pretty much like process management. Call this controller,
HeadControl.py. What HeadControl.py can do is to expose a SOAP interface.
|
by: Tolga |
last post by:
Hi everyone,
I am using Common Lisp for a while and nowadays I've heard so much
about Python that finally I've decided to give it a try becuase Python
is not very far away from Lisp family.
I cannot believe this! This snake is amazing, incredible and so
beautiful! You, Pythonists, why didn't you tell me about this before?
:-)
|
by: Mark |
last post by:
Hi, I'm new to python and looking for a better idiom to use for the
manner I have been organising my python scripts. I've googled all over
the place about this but found absolutely nothing.
I'm a linux/unix command line guy quite experienced in shell scripts
etc. I have a heap of command line utility scripts which I run directly.
What is the best way to create python command line scripts but exploit
the (loadonly) speed-up benefit of...
| |
by: Vicent Giner |
last post by:
Hello.
I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.
However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.
I am working on my PhD Thesis, which is about Operations Research,
|
by: John Dann |
last post by:
A Python newbie, but some basic understanding of how classes, objects
etc work in eg VB.Net. However, I'm struggling a little to translate
this knowledge into the Python context.
I'm trying to teach myself this aspect of Python by working up a trial
project, part of which calls for pulling in data from a serial data
connection at regular intervals. It looked sensible to place all the
comms procedures/functions in their own class and...
|
by: iu2 |
last post by:
Hi,
This is a little bit strange post, but I'm curious...
I learned Python from its tutorial step by step, and practicing
writing small scripts.
I haven't seen a Python program before knowing Python.
I'm curious, what did Python code look like to those of you who have
seen a bunch of Python code for the first time before knowing Python?
|
by: Tyler Breisacher |
last post by:
To paraphrase this video (http://www.youtube.com/watch?v=zJOS0sV2a24)
(which is the Randall Munroe, author of the famous xkcd
(http://www.xkcd.com/), giving a talk at Google), "you just type the
pseudo-code and it runs! And as someone said, if Python is executable
pseudo-code, then Perl is executable line noise." That quote (and the
"import antigravity" comic) were my first exposure to Python and then
when I actually began to use it, I...
|
by: Ivan Reborin |
last post by:
Hello everyone,
I was wondering if anyone here has a moment of time to help me with 2
things that have been bugging me.
1. Multi dimensional arrays - how do you load them in python
For example, if I had:
-------
1 2 3
4 5 6
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |