Re: Switch function

2019-02-03 Thread Dan Sommers
On 2/3/19 9:03 PM, Avi Gross wrote: > The example I show above could in many cases be done as you describe > but what are you gaining? > > I mean if I subtract the integer representation of a keyboard > alphabetic letter (ASCII for the example) from letter 'a' or 'A' then > A maps to 0 and B maps

RE: Switch function

2019-02-03 Thread Avi Gross
list On Behalf Of Dan Sommers Sent: Sunday, February 3, 2019 8:29 PM To: python-list@python.org Subject: Re: Switch function On 2/3/19 5:40 PM, Avi Gross wrote: > Bottom line, does anyone bother using anything like this? It is > actually a bunch of hidden IF statements matched in order bu

Re: Switch function

2019-02-03 Thread Dan Sommers
On 2/3/19 5:40 PM, Avi Gross wrote: Bottom line, does anyone bother using anything like this? It is actually a bunch of hidden IF statements matched in order but may meet many needs. I sure don't. In the rare case that I might use a switch statement in another language, I just use a series of

RE: Switch statement

2013-03-10 Thread Joseph L. Casale
> Or could you do something like: > > arguments_to_pass = [list of some sort] > switch.get(var, default)(*arguments_to_pass) Stevens lambda suggestion was most appropriate. Within the switch, there are functions called with none, or some variation of arguments. It was not easy to pass them in afte

Re: Switch statement

2013-03-10 Thread Terry Reedy
On 3/10/2013 11:18 AM, Steven D'Aprano wrote: On Sun, 10 Mar 2013 14:16:27 +, Joseph L. Casale wrote: I have a switch statement composed using a dict: switch = { 'a': func_a, 'b': func_b, 'c': func_c } switch.get(var, default)() As a result of multiple functions per choic

Re: Switch statement

2013-03-10 Thread Nicholas Cole
On Sun, Mar 10, 2013 at 8:42 PM, Mitya Sirenef wrote: > On 03/10/2013 10:16 AM, Joseph L. Casale wrote: > >> I have a switch statement composed using a dict: >> > > > > > > switch = { > > 'a': func_a, > > 'b': func_b, > > 'c': func_c > > } > > switch.get(var, default)() > > > > > > As a result of

Re: Switch statement

2013-03-10 Thread Mitya Sirenef
On 03/10/2013 10:16 AM, Joseph L. Casale wrote: I have a switch statement composed using a dict: > > > switch = { > 'a': func_a, > 'b': func_b, > 'c': func_c > } > switch.get(var, default)() > > > As a result of multiple functions per choice, it migrated to: > > > > switch = { > 'a': (func_a1,

RE: Switch statement

2013-03-10 Thread Joseph L. Casale
> switch = {  > 'A': functools.partial(spam, a), > 'B': lambda b, c=c: ham(b, c), > 'C': eggs, > } >  > switch[letter](b) That's cool, never even thought to use lambdas. > functools.partial isn't always applicable, but when it is, you should > prefer it over lambda since it will

Re: Switch statement

2013-03-10 Thread Steven D'Aprano
On Sun, 10 Mar 2013 14:16:27 +, Joseph L. Casale wrote: > I have a switch statement composed using a dict: > > > switch = { >     'a': func_a, >     'b': func_b, >     'c': func_c > } > switch.get(var, default)() > > > As a result of multiple functions per choice, it migrated to: > > >

Attributions (was Re: switch)

2009-12-24 Thread Aahz
In article , Tim Chase wrote: >On 12/10/2009 09:22 PM, John Bokma wrote: >> >> Please don't delete attribution line(s), added: >> >> Asun Friere writes: > >I tend to prune them because a good newsreader will thread messages >and put my reply in the context of the message to which I'm repl

Re: switch

2009-12-21 Thread Albert van der Horst
In article , Steven D'Aprano wrote: >On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote: >> >> Compiled languages' switch statements typically require constant labels >> as this enables various optimisations. > >Pascal, for example, can test against either single values, enumerated >values, or a ra

Re: switch

2009-12-11 Thread Asun Friere
On Dec 11, 1:38 am, Tim Chase wrote: > It's clean if it were the solution to my problem Picking out that line first, just to be clear about this. You missed the disclaimer. This was never meant to be a solution to your problem. It was solution to the problem contained in the code you posted.

Re: switch

2009-12-11 Thread Tim Chase
On 12/10/2009 09:22 PM, John Bokma wrote: Tim Chase writes: Please don't delete attribution line(s), added: Asun Friere writes: I tend to prune them because a good newsreader will thread messages and put my reply in the context of the message to which I'm replying. Both Thunderbird an

Re: switch

2009-12-11 Thread Bearophile
Bruno Desthuilliers: > Well, obviously such business rules must by no mean be hardcoded. You > really need a "rule engine", configurable by your domain experts thru a > DSL that we'll design specially for you. The rule engine will generate > an AbstractScoreFactory that will instanciate appropriat

Re: switch

2009-12-10 Thread John Bokma
Tim Chase writes: Please don't delete attribution line(s), added: Asun Friere writes: >> >> phone.update_from_record(record) >> >> This switch statement belongs to one guy. One guy who wants to know >> how to do everything that needs to be done to Phones no matter who >> asks > > T

Re: switch

2009-12-10 Thread MRAB
Steven D'Aprano wrote: On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote: On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote: I string together a bunch of elif statements to simulate a switch if foo == True: blah elif bar == True: blah blah elif bar == False: blarg

Re: switch

2009-12-10 Thread Nobody
On Thu, 10 Dec 2009 05:47:19 +, Steven D'Aprano wrote: >>> I string together a bunch of elif statements to simulate a switch >>> >>> if foo == True: >>> blah >>> elif bar == True: >>> blah blah >>> elif bar == False: >>> blarg >>> elif >> >> This isn't what would normally be

Re: switch

2009-12-10 Thread Ethan Furman
Asun Friere wrote: On Dec 10, 2:00 pm, Carl Banks wrote: On Dec 9, 5:02 pm, Asun Friere wrote: On Dec 9, 7:08 pm, Carl Banks wrote: What if the object is a string you just read from a file? How do you dispatch using polymorphism in that case? This would be a pertinent question, w

Re: switch

2009-12-10 Thread Tim Chase
Great example Tim. This is something that many of us must be dealing with on a daily basis. The problem has enough details (bar one), to allow an answer and not so detailed as to be confusing. And for me it's a particularly good example, because your need accommodate mulitple provider formats

Re: switch

2009-12-10 Thread Carl Banks
On Dec 10, 3:34 am, Asun Friere wrote: > On Dec 10, 2:00 pm, Carl Banks wrote: [snip most of questionable, verly verbose reply] > > You argued that a decent language OO should never > > have a switch statement because polymorphic dispatch is the right way > > to handle it in OO languages, which

Re: switch

2009-12-10 Thread Asun Friere
On Dec 10, 6:57 am, Tim Chase wrote: > Carl Banks wrote: > > What if the object is a string you just read from a file? > > > How do you dispatch using polymorphism in that case? > > This is where I most miss a switch/case statement in Python...I > do lots of text-file processing (cellular provider

Re: switch

2009-12-10 Thread Asun Friere
On Dec 10, 2:00 pm, Carl Banks wrote: > On Dec 9, 5:02 pm, Asun Friere wrote: > > > On Dec 9, 7:08 pm, Carl Banks wrote: > > > > What if the object is a string you just read from a file? > > > > How do you dispatch using polymorphism in that case? > > > This would be a pertinent question, were I

Re: switch

2009-12-09 Thread Steven D'Aprano
On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote: > On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote: > >> I string together a bunch of elif statements to simulate a switch >> >> if foo == True: >> blah >> elif bar == True: >> blah blah >> elif bar == False: >> blarg >> elif

Re: switch

2009-12-09 Thread Carl Banks
On Dec 9, 5:02 pm, Asun Friere wrote: > On Dec 9, 7:08 pm, Carl Banks wrote: > > > What if the object is a string you just read from a file? > > > How do you dispatch using polymorphism in that case? > > This would be a pertinent question, were I advocating that _all_ > switch statements should,

Re: switch

2009-12-09 Thread Asun Friere
On Dec 9, 7:08 pm, Carl Banks wrote: > What if the object is a string you just read from a file? > > How do you dispatch using polymorphism in that case? This would be a pertinent question, were I advocating that _all_ switch statements should, or even can, be replaced with "dispatch using polymo

Re: switch

2009-12-09 Thread Asun Friere
On Dec 9, 5:39 pm, Steven D'Aprano wrote: > On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote: > > On Dec 9, 4:02 pm, Kee Nethery wrote: > >> I string together a bunch of elif statements to simulate a switch > > >> if foo == True: > >>         blah > >> elif bar == True: > >>         blah bl

Re: switch

2009-12-09 Thread Tim Chase
MRAB wrote: Tim Chase wrote: switch row['recordtype']: case '01': phone.international += Decimal(row['internationalcost']) // optionally a "break" here depending on // C/C++/Java/PHP syntax vs. Pascal syntax which // doesn't have fall-through case '02': ph

Re: switch

2009-12-09 Thread MRAB
Tim Chase wrote: Carl Banks wrote: What if the object is a string you just read from a file? How do you dispatch using polymorphism in that case? [snip] which would nicely change into something like switch row['recordtype']: case '01': phone.international += Decimal(row['inter

Re: switch

2009-12-09 Thread Tim Chase
Carl Banks wrote: What if the object is a string you just read from a file? How do you dispatch using polymorphism in that case? This is where I most miss a switch/case statement in Python...I do lots of text-file processing (cellular provider data), so I have lots of code (for each provider

Re: switch

2009-12-09 Thread Nobody
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote: > I string together a bunch of elif statements to simulate a switch > > if foo == True: > blah > elif bar == True: > blah blah > elif bar == False: > blarg > elif This isn't what would normally be considered a switch (

Re: switch

2009-12-09 Thread Matt McCredie
hong zhang yahoo.com> writes: > > List, > > Python does not have switch statement. Any other option does similar work? > Thanks for help. > > --henry > > I see a couple of people have mentioned using a dictionary. If the value that you are switching on is a string, or could be made i

Re: switch

2009-12-09 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote: (snip) It is a principle of OO design that "an object should know what to do itself." Rather running an object though a series of tests, it is better to send the object a message, relying on polymorphism or duck-

Re: switch

2009-12-09 Thread Carl Banks
On Dec 8, 9:36 pm, Asun Friere wrote: > This code is probably symptomatic of poor design. (Not to mention that > your condition tests).  For which reason python has no 'case' > statement and why no decent OO language should. > > It is a principle of OO design that "an object should know what to do

Re: switch

2009-12-08 Thread Asun Friere
On Dec 9, 5:12 pm, Steven D'Aprano wrote: > On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote: > > I string together a bunch of elif statements to simulate a switch > > > if foo == True: > >    blah > > elif bar == True: > >    blah blah > > elif bar == False: > >    blarg > > elif > > Ar

Re: switch

2009-12-08 Thread Steven D'Aprano
On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote: > On Dec 9, 4:02 pm, Kee Nethery wrote: >> I string together a bunch of elif statements to simulate a switch >> >> if foo == True: >>         blah >> elif bar == True: >>         blah blah >> elif bar == False: >>         blarg >> elif >

Re: switch

2009-12-08 Thread Steven D'Aprano
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote: > I string together a bunch of elif statements to simulate a switch > > if foo == True: > blah > elif bar == True: > blah blah > elif bar == False: > blarg > elif Are you sure you want to test for equality with True a

Re: switch

2009-12-08 Thread Asun Friere
On Dec 9, 4:02 pm, Kee Nethery wrote: > I string together a bunch of elif statements to simulate a switch > > if foo == True: >         blah > elif bar == True: >         blah blah > elif bar == False: >         blarg > elif This code is probably symptomatic of poor design. (Not to mention

Re: switch

2009-12-08 Thread Kee Nethery
I string together a bunch of elif statements to simulate a switch if foo == True: blah elif bar == True: blah blah elif bar == False: blarg elif -- http://mail.python.org/mailman/listinfo/python-list

Re: switch

2009-12-08 Thread zeph
> > Even better (well, shorter!): > options = {"a" : do_a, "b",do_b, "c", do_c} > options.get(option, do_default)() > You can also make it something callable like so, which is a little more compact if you need to reuse it a lot: >>> def do_a(x): print "a:", x ... >>> def do_b(x): print "b:", x ..

Re: switch

2009-12-08 Thread rzzzwilson
On Dec 9, 1:00 pm, Benjamin Kaplan wrote: > On Tue, Dec 8, 2009 at 8:53 PM, hong zhang wrote: > > List, > > > Python does not have switch statement. Any other option does similar work? > > Thanks for help. > > Use a dict instead, where the keys are the different cases and the > values are usually

Re: switch

2009-12-08 Thread Benjamin Kaplan
On Tue, Dec 8, 2009 at 8:53 PM, hong zhang wrote: > List, > > Python does not have switch statement. Any other option does similar work? > Thanks for help. > Use a dict instead, where the keys are the different cases and the values are usually callable objects (such as functions) options = {"a"

Re: switch

2009-12-08 Thread Chris Rebert
On Tue, Dec 8, 2009 at 5:53 PM, hong zhang wrote: > Python does not have switch statement. Any other option does similar work? Yes, a dictionary with functions as values: http://simonwillison.net/2004/May/7/switch/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/list

Re: switch to interactive mode

2009-03-16 Thread nntpman68
Hi JBW. code.interact() does what I wanted. Great !!! Thanks N JBW wrote: On Mon, 16 Mar 2009 23:49:34 +0100, nntpman68 wrote: I'd like, that a python script can be started by just calling it (clicking on it), but that the script can decide to enter interactive mode if certain conditio

Re: switch to interactive mode

2009-03-16 Thread Mike Driscoll
On Mar 16, 5:49 pm, nntpman68 wrote: > Hi > > I know about two ways to enter python interactive mode > > 1.) just start python > > 2.) run python -i pythonscript.py > > What I am looking for is slightly different: > > I'd like, that a python script can be started by just calling it > (clicking on

Re: switch to interactive mode

2009-03-16 Thread JBW
On Mon, 16 Mar 2009 23:49:34 +0100, nntpman68 wrote: > I'd like, that a python script can be started by just calling it > (clicking on it), > > but that the script can decide to enter interactive mode if certain > conditions occur. > > Is this possible? Don't know about the clicky-clicky part,

Re: switch user

2006-10-03 Thread Nick Vatamaniuc
Check out the os module, especially the os.getgid(), os.getuid(), os.setgid(), os.getuid(), methods. There are more , take at a look at Python documentation. You can start a script as root then change your priveleges with os.setgid() os.setuid() methods. Note: those methods operate with integer us

Re: Switch statement (was: Lambda going out of fashion)

2005-01-03 Thread TZOTZIOY
On Thu, 23 Dec 2004 19:57:27 GMT, rumours say that rzed <[EMAIL PROTECTED]> might have written: [Why did PEP 275 stall?] >It seems to me that it was regarded as misguidod. QOTPE +1 (PE=Python Era) Oncoming Python book: "Hitchhiker's Guido to the Python Language" -- TZOTZIOY, I speak England v

Re: Switch statement (was: Lambda going out of fashion)

2004-12-23 Thread rzed
Skip Montanaro <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > > Stephen> { > Stephen> 'one': lambda x:x.blat(), > Stephen> 'two': lambda x:x.blah(), > Stephen> }.get(someValue, lambda x:0)(someOtherValue) > > One thing to remember is that function calls in Python are >