Re: mocking for get method in requests

2019-03-26 Thread Toni Sissala
On 18.1.2019 19:16, Shakti Kumar wrote: Hello people, I noticed something weird (weird as per my current knowledge, though I know its subjective) today. Hi Kumar, mock_req.get('').return_value = 'Hello' Here you are calling mock_req -MagicMocks get-method with paramet

Re: mocking for get method in requests

2019-03-16 Thread George Fischhof
Shakti Kumar ezt írta (időpont: 2019. jan. 18., P, 18:18): > Hello people, > I noticed something weird (weird as per my current knowledge, though I know > its subjective) today. > > sample.py file > > -- > > import requests > def random_testing(): > out = requests.get('www.cisco.com') > a

mocking for get method in requests

2019-01-18 Thread Shakti Kumar
Hello people, I noticed something weird (weird as per my current knowledge, though I know its subjective) today. sample.py file -- import requests def random_testing(): out = requests.get('www.cisco.com') a = out.json() return a testing.py file -- @patch(*’*sample.requests') def

Re: Simple question: how do I print output from .get() method

2018-05-30 Thread Rhodri James
On 30/05/18 06:51, Chris Angelico wrote: On Wed, May 30, 2018 at 3:44 PM, MrMagoo2018 wrote: Hello folks, imagine I have the code below and I am getting the "error" message when attempting to print() the output of 'sw_report'. Can you suggest which method I should use to retrieve this? Is that

Re: Simple question: how do I print output from .get() method

2018-05-29 Thread Chris Angelico
On Wed, May 30, 2018 at 3:44 PM, MrMagoo2018 wrote: > Hello folks, imagine I have the code below and I am getting the "error" > message when attempting to print() the output of 'sw_report'. > Can you suggest which method I should use to retrieve this? Is that a > dictionary maybe? > > from arist

Simple question: how do I print output from .get() method

2018-05-29 Thread MrMagoo2018
Hello folks, imagine I have the code below and I am getting the "error" message when attempting to print() the output of 'sw_report'. Can you suggest which method I should use to retrieve this? Is that a dictionary maybe? from arista import arista m = arista() m.authenticate ("user","password")

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:54 AM, MRAB wrote: > Occasionally someone posts here wanting to count items and solutions > involving dict or defaultdict are suggested, and I think that a 'bag' class > would be useful. The 'set' class was introduced first in a module, but it > soon became a builtin. My

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:49 AM, Steven D'Aprano wrote: > What set module? Sorry I must have meant the collections module :) > Adding a multi-set or bag class to the collections module would be a good > idea though. Perhaps you should put in a feature request? :) Perhaps I will. cheers James

Re: get method

2008-12-30 Thread MRAB
James Mills wrote: On Wed, Dec 31, 2008 at 10:22 AM, John Machin wrote: (snip) The "crawl through the shrubbery looking for evidence" approach stumbles on the actual code: Yes I found his implementation soon after :) Not bad actually... I wonder why bag() isn't shipped with the std lib - per

Re: get method

2008-12-30 Thread Steven D'Aprano
On Wed, 31 Dec 2008 10:29:14 +1000, James Mills wrote: > On Wed, Dec 31, 2008 at 10:22 AM, John Machin > wrote: (snip) > >> The "crawl through the shrubbery looking for evidence" approach >> stumbles on the actual code: > > Yes I found his implementation soon after :) Not bad actually... I > wo

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:22 AM, John Machin wrote: (snip) > The "crawl through the shrubbery looking for evidence" approach > stumbles on the actual code: Yes I found his implementation soon after :) Not bad actually... I wonder why bag() isn't shipped with the std lib - perhaps in teh set mod

Re: get method

2008-12-30 Thread John Machin
On Dec 31, 10:58 am, "James Mills" wrote: > On Wed, Dec 31, 2008 at 9:15 AM, MRAB wrote: > > (snip) > > > A while back I posted a Python implementation of 'bag' (also called a > > multiset). The code would then become something like: > > What complexity is this ? The "armchair philosopher" appro

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 9:15 AM, MRAB wrote: (snip) > A while back I posted a Python implementation of 'bag' (also called a > multiset). The code would then become something like: What complexity is this ? cheers James -- http://mail.python.org/mailman/listinfo/python-list

Re: get method

2008-12-30 Thread MRAB
James Mills wrote: On Tue, Dec 30, 2008 at 7:10 PM, Roel Schroeven wrote: Hm, you just changed an O(n) algorithm to an O(n**2) algorithm. No big deal for short strings, but try your solution on a string with length 1 and see the difference. On my computer the O(n) version takes 0.008 second

Re: get method

2008-12-30 Thread James Mills
On Tue, Dec 30, 2008 at 7:10 PM, Roel Schroeven wrote: > Hm, you just changed an O(n) algorithm to an O(n**2) algorithm. No big > deal for short strings, but try your solution on a string with length > 1 and see the difference. On my computer the O(n) version takes > 0.008 seconds, while your

Re: get method

2008-12-30 Thread Roel Schroeven
James Mills schreef: > Ross, the others have informed you that you are not > actually incrementing the count. I'll assume you've > fixed your function now :) ... I want to show you a far > simpler way to do this which takes advantage of > Python's list comprehensions and mappings (which are > reall

Re: get method

2008-12-29 Thread Aaron Brady
On Dec 29, 8:02 pm, Steven D'Aprano wrote: > On Mon, 29 Dec 2008 17:38:36 -0800, Ross wrote: > > On Dec 29, 8:07 pm, Scott David Daniels wrote: > >> Ross wrote: > >> > ... Use get to write histogram more concisely. You should be able to > >> > eliminate the if statement. > > >> > def histogram(s)

Re: get method

2008-12-29 Thread Steven D'Aprano
On Mon, 29 Dec 2008 17:38:36 -0800, Ross wrote: > On Dec 29, 8:07 pm, Scott David Daniels wrote: >> Ross wrote: >> > ... Use get to write histogram more concisely. You should be able to >> > eliminate the if statement. >> >> > def histogram(s): >> >    d = dict() >> >    for c in s: >> >        

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:43 AM, James Mills wrote: > On Tue, Dec 30, 2008 at 11:38 AM, Ross wrote: >> I realize the code isn't counting, but how am I to do this without >> using an if statement as the problem instructs? > > I just gave you a hint :) Ross: This exercise is a simple exercise de

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:38 AM, Ross wrote: > I realize the code isn't counting, but how am I to do this without > using an if statement as the problem instructs? I just gave you a hint :) cheers James -- http://mail.python.org/mailman/listinfo/python-list

Re: get method

2008-12-29 Thread Ross
On Dec 29, 8:07 pm, Scott David Daniels wrote: > Ross wrote: > > ... Use get to write histogram more concisely. You should be able to > > eliminate the if statement. > > > def histogram(s): > >    d = dict() > >    for c in s: > >            d[c]= d.get(c,0) > >    return d > > > This code returns

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:32 AM, James Mills wrote: > Ross, the others have informed you that you are not > actually incrementing the count. I'll assume you've > fixed your function now :) ... I want to show you a far > simpler way to do this which takes advantage of > Python's list comprehension

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:00 AM, Ross wrote: > I am teaching myself Python by going through Allen Downing's "Think > Python." I have come across what should be a simple exercise, but I am > not getting the correct answer. Here's the exercise: > > Given: > > def histogram(s): >d = dict() >

Re: get method

2008-12-29 Thread Scott David Daniels
Ross wrote: ... Use get to write histogram more concisely. You should be able to eliminate the if statement. def histogram(s): d = dict() for c in s: d[c]= d.get(c,0) return d This code returns a dictionary of all the letters to any string s I give it but

Re: get method

2008-12-29 Thread Steven D'Aprano
On Mon, 29 Dec 2008 17:00:31 -0800, Ross wrote: > Here's my code: > > def histogram(s): > d = dict() > for c in s: > d[c]= d.get(c,0) > return d > > This code returns a dictionary of all the letters to any string s I give > it but each corresponding value is incor

get method

2008-12-29 Thread Ross
I am teaching myself Python by going through Allen Downing's "Think Python." I have come across what should be a simple exercise, but I am not getting the correct answer. Here's the exercise: Given: def histogram(s): d = dict() for c in s: if c not in d: d[c] = 1

Re: Why does list have no 'get' method?

2008-02-11 Thread grflanagan
On Feb 8, 4:24 pm, [EMAIL PROTECTED] wrote: > > > val = BETTER foo THAN bar > > > > ;-) > > > > Cobol-strikes-back-ly yours, > > > > George > > > I use a ETL language/tool that actually has a function for this kind > > of thing: > > > NulltoValue(value,defaultValue) > > > it returns defaultValue if

Re: Why does list have no 'get' method?

2008-02-08 Thread Ben Finney
[EMAIL PROTECTED] writes: > (would it help if I put lots of blank lines after this, so the disclaimer > is out-of-sight/out-of-mind?) Not really. It would help far more if you could stop having this disclaimer appear at all on this list (or any other list, really, but that's a matter for those l

Re: Why does list have no 'get' method?

2008-02-08 Thread bearophileHUGS
Carl Banks: > I think booleans should be completely disjoint from all other types, > as they are in Java. "and", "or", and "not" should accept only boolean > operands and return only boolean results. I'd like "or" and "and" (and "not") to return booleans only, to avoid bugs and make their meanin

Re: Why does list have no 'get' method?

2008-02-08 Thread Gabriel Genellina
En Fri, 08 Feb 2008 13:24:19 -0200, <[EMAIL PROTECTED]> escribió: > > Matt. > (would it help if I put lots of blank lines after this, so the disclaimer > is out-of-sight/out-of-mind?) > -- > ie, from here on? No, what would help is a line containing *exactly* (dash)(dash)(space)(newline) That

Re: Why does list have no 'get' method?

2008-02-08 Thread Carl Banks
On Feb 8, 3:38 am, [EMAIL PROTECTED] wrote: > Can't you just make a subclass of Boolean and add methods to the true/ > false, or am i running up a tree with a cat? I'm not sure what point you were trying to make, but no you can't subclass bool in Python as it is now; certainly it wouldn't be possi

Re: Why does list have no 'get' method?

2008-02-08 Thread Matthew_WARREN
> > val = BETTER foo THAN bar > > > > ;-) > > > > Cobol-strikes-back-ly yours, > > > > George > > I use a ETL language/tool that actually has a function for this kind > of thing: > > NulltoValue(value,defaultValue) > > it returns defaultValue if value is null otherwise value. even Ksh has one

Re: Why does list have no 'get' method?

2008-02-08 Thread pruebauno
On Feb 7, 4:36 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Feb 7, 4:25 pm, Steven D'Aprano <[EMAIL PROTECTED] > > > > cybersource.com.au> wrote: > > On Thu, 07 Feb 2008 15:59:13 +0100, Wildemar Wildenburger wrote: > > > Arnaud Delobelle wrote: > > >> Personally, between > > > >> * foo if foo

Re: Why does list have no 'get' method?

2008-02-08 Thread cokofreedom
On Feb 8, 8:23 am, Carl Banks <[EMAIL PROTECTED]> wrote: > On Feb 7, 8:44 am, Steve Holden <[EMAIL PROTECTED]> wrote: > > > [EMAIL PROTECTED] wrote: > > > I'd like to know what others think about it, about this anti-feature. > > > What I can say is that other computer languages too think that boole

Re: Why does list have no 'get' method?

2008-02-07 Thread Carl Banks
On Feb 7, 8:44 am, Steve Holden <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I'd like to know what others think about it, about this anti-feature. > > What I can say is that other computer languages too think that boolean > > operations must return boolean values only, so I am not alon

Re: Why does list have no 'get' method?

2008-02-07 Thread Gabriel Genellina
En Thu, 07 Feb 2008 19:36:57 -0200, George Sakkis <[EMAIL PROTECTED]> escribió: > On Feb 7, 4:25 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> On Thu, 07 Feb 2008 15:59:13 +0100, Wildemar Wildenburger wrote: >> >> > val = foo rather than bar >> >> > If that is not clear

Re: Why does list have no 'get' method?

2008-02-07 Thread George Sakkis
On Feb 7, 4:25 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Thu, 07 Feb 2008 15:59:13 +0100, Wildemar Wildenburger wrote: > > Arnaud Delobelle wrote: > >> Personally, between > > >> * foo if foo else bar > >> * foo or bar > > >> I prefer the second. Maybe it could be spelt

Re: Why does list have no 'get' method?

2008-02-07 Thread Paul Rubin
"Denis Bilenko" <[EMAIL PROTECTED]> writes: > I convinced that this > > line = self._buffer.get(self._bufindex) > if line is None: > self._bufindex += 1 > self._lineno += 1 > self._filelineno += 1 > return line > line = self.readline() I haven't looked

Re: Why does list have no 'get' method?

2008-02-07 Thread Steven D'Aprano
On Thu, 07 Feb 2008 15:59:13 +0100, Wildemar Wildenburger wrote: > Arnaud Delobelle wrote: >> Personally, between >> >> * foo if foo else bar >> * foo or bar >> >> I prefer the second. Maybe it could be spelt >> >> * foo else bar ? >> > How about > > val = foo rather than bar > > If that is

Re: Why does list have no 'get' method?

2008-02-07 Thread pruebauno
On Feb 7, 12:15 pm, [EMAIL PROTECTED] wrote: > On Feb 7, 11:01 am, "Denis Bilenko" <[EMAIL PROTECTED]> wrote: > > > > > Steve Holden wrote: > > > These versions differ with respect to treatment of blank lines, which > > > indicates how easy it is to go astray in this kind of semantic > > > optimiz

Re: Why does list have no 'get' method?

2008-02-07 Thread Michael Spencer
Wildemar Wildenburger wrote: > Arnaud Delobelle wrote: >> Personally, between >> >> * foo if foo else bar >> * foo or bar >> >> I prefer the second. Maybe it could be spelt >> >> * foo else bar ? >> > How about > > val = foo rather than bar > > If that is not clear and obvios, I don't know what i

Re: Why does list have no 'get' method?

2008-02-07 Thread pruebauno
On Feb 7, 11:01 am, "Denis Bilenko" <[EMAIL PROTECTED]> wrote: > Steve Holden wrote: > > These versions differ with respect to treatment of blank lines, which > > indicates how easy it is to go astray in this kind of semantic > > optimization. Your example simply wouldn't work (though you could pa

Re: Why does list have no 'get' method?

2008-02-07 Thread Denis Bilenko
Steve Holden wrote: > These versions differ with respect to treatment of blank lines, which > indicates how easy it is to go astray in this kind of semantic > optimization. Your example simply wouldn't work (though you could patch > it up using "if line is None". (despite the use of short-circuiti

Re: Why does list have no 'get' method?

2008-02-07 Thread Steve Holden
Wildemar Wildenburger wrote: > Arnaud Delobelle wrote: >> Personally, between >> >> * foo if foo else bar >> * foo or bar >> >> I prefer the second. Maybe it could be spelt >> >> * foo else bar ? >> > How about > > val = foo rather than bar > > If that is not clear and obvios, I don't know what i

Re: Why does list have no 'get' method?

2008-02-07 Thread Wildemar Wildenburger
Arnaud Delobelle wrote: > Personally, between > > * foo if foo else bar > * foo or bar > > I prefer the second. Maybe it could be spelt > > * foo else bar ? > How about val = foo rather than bar If that is not clear and obvios, I don't know what is. ;) /W -- http://mail.python.org/mailman/

Re: Why does list have no 'get' method?

2008-02-07 Thread Bruno Desthuilliers
Thomas Bellman a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > (snip) >> In Lisp (and IIRC), an empty list is false, anything else >> is true. > > There seems to be a language name missing from the parenthesis. Yes, sorry - I was thinking of OCaml and/or Haskell, but

Re: Why does list have no 'get' method?

2008-02-07 Thread Steve Holden
Denis Bilenko wrote: > Tim Golden wrote: > >> Dodging your question slightly (and at the risk of teaching >> my grandmother to suck eggs) I sometimes use this idiom for >> checking params. Obviously it only goes so far, but it's >> fairly compact: > >> >> import os, sys > >> if __name__ == '__m

Re: Why does list have no 'get' method?

2008-02-07 Thread Steve Holden
[EMAIL PROTECTED] wrote: > Steven D'Aprano: >> With the greatest respect, I think that if you think the second example >> "is more clear", you're completely bonkers. *grins* > It's amusing how often "with the greatest respect" is used to preface a statement that clearly implies very little respec

Re: Why does list have no 'get' method?

2008-02-07 Thread Thomas Bellman
Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Not quite. In C and a couple other langages, int 0 is false, anything > else is true. Not just int, but all kinds of integers, as well as all kinds of floating point types and all kinds of pointers, with the value 0 are considered false. And stru

Re: Why does list have no 'get' method?

2008-02-07 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > Steven D'Aprano: >> With the greatest respect, I think that if you think the second example >> "is more clear", you're completely bonkers. *grins* > > No one is completely normal, I presume :-) > I'd like to know what others think about it, about this anti-feature. s

Re: Why does list have no 'get' method?

2008-02-07 Thread Stefan Behnel
Denis Bilenko wrote: > Raymond Hettinger wrote: >> If positions 0 and 1 are optional, how do you expect to know whether >> "path" is going to be at position 2? This problem doesn't exist with >> dictionaries because the presence or absence of optional entries does >> not affect the key reference t

Re: Why does list have no 'get' method?

2008-02-07 Thread Arnaud Delobelle
On Feb 7, 8:20 am, [EMAIL PROTECTED] wrote: > Paul Rubin: > > > I like the suggestion, except it should be > >      port = int(sys.argv.get(1, '8000')) > > one could imagine your example going wrong in a protocol where 0 is > > a valid port number. > > I think a high-level language like Python must

Re: Why does list have no 'get' method?

2008-02-07 Thread bearophileHUGS
Steven D'Aprano: > With the greatest respect, I think that if you think the second example > "is more clear", you're completely bonkers. *grins* No one is completely normal, I presume :-) I'd like to know what others think about it, about this anti-feature. What I can say is that other computer la

Re: Why does list have no 'get' method?

2008-02-07 Thread Denis Bilenko
Tim Golden wrote: > Dodging your question slightly (and at the risk of teaching > my grandmother to suck eggs) I sometimes use this idiom for > checking params. Obviously it only goes so far, but it's > fairly compact: > > import os, sys > if __name__ == '__main__': > ARGS = None, "DEV" > f

Re: Why does list have no 'get' method?

2008-02-07 Thread Steven D'Aprano
On Thu, 07 Feb 2008 00:20:52 -0800, bearophileHUGS wrote: > I think a high-level language like Python must have boolean operators > (or and not) that behave in a much more clean way, with a simpler > semantics. That is they have to return only true or false. Otherwise > they are just a source of b

Re: Why does list have no 'get' method?

2008-02-07 Thread Tim Golden
Denis Bilenko wrote: > Why does list have no 'get' method with exactly the same semantics as > dict's get, > that is "return an element if there is one, but do NOT raise > an exception if there is not.": > > def get(self, item, default = None): &g

Re: Why does list have no 'get' method?

2008-02-07 Thread Stefan Behnel
[EMAIL PROTECTED] wrote: > Paul Rubin: >> I like the suggestion, except it should be >> port = int(sys.argv.get(1, '8000')) >> one could imagine your example going wrong in a protocol where 0 is >> a valid port number. > > I think a high-level language like Python must have boolean operators

Re: Why does list have no 'get' method?

2008-02-07 Thread bearophileHUGS
Paul Rubin: > I like the suggestion, except it should be > port = int(sys.argv.get(1, '8000')) > one could imagine your example going wrong in a protocol where 0 is > a valid port number. I think a high-level language like Python must have boolean operators (or and not) that behave in a much

Re: Why does list have no 'get' method?

2008-02-06 Thread Raymond Hettinger
[Denis Bilenko] > Why does list have no 'get' method with exactly the same semantics as > dict's get, > that is "return an element if there is one, but do NOT raise > an exception if there is not.": . . . > It is often desirable, for example, when one

Re: Why does list have no 'get' method?

2008-02-06 Thread Paul Rubin
"Denis Bilenko" <[EMAIL PROTECTED]> writes: > port = sys.argv.get(1) or 8000 I like the suggestion, except it should be port = int(sys.argv.get(1, '8000')) one could imagine your example going wrong in a protocol where 0 is a valid port number. -- http://mail.python.org/mailman/listinf

Why does list have no 'get' method?

2008-02-06 Thread Denis Bilenko
Why does list have no 'get' method with exactly the same semantics as dict's get, that is "return an element if there is one, but do NOT raise an exception if there is not.": def get(self, item, default = None): try: return self[item]

Re: Need help with the get() method of a Text entry

2007-04-13 Thread Steve Holden
Chad wrote: > On Apr 12, 5:03 pm, James Stroud <[EMAIL PROTECTED]> wrote: >> James Stroud wrote: >>> Chad wrote: I have a simple little program that brings up asks the user to enter a note, then is supposed to place that note into a text file when the user hits the submit button. Ho

Re: Need help with the get() method of a Text entry

2007-04-12 Thread Chad
On Apr 12, 5:03 pm, James Stroud <[EMAIL PROTECTED]> wrote: > James Stroud wrote: > > Chad wrote: > > >> I have a simple little program that brings up asks the user to enter a > >> note, then is supposed to place that note into a text file when the > >> user hits the submit button. However, when t

Re: Need help with the get() method of a Text entry

2007-04-12 Thread James Stroud
James Stroud wrote: > Chad wrote: > >> I have a simple little program that brings up asks the user to enter a >> note, then is supposed to place that note into a text file when the >> user hits the submit button. However, when the user hits the submit >> button, absolutely nothing happens. IDLE

Re: Need help with the get() method of a Text entry

2007-04-12 Thread James Stroud
Chad wrote: > I have a simple little program that brings up asks the user to enter a > note, then is supposed to place that note into a text file when the > user hits the submit button. However, when the user hits the submit > button, absolutely nothing happens. IDLE doesn't give an error > messa

Re: Need help with the get() method of a Text entry

2007-04-12 Thread James Stroud
Chad wrote: > I have a simple little program that brings up asks the user to enter a > note, then is supposed to place that note into a text file when the > user hits the submit button. However, when the user hits the submit > button, absolutely nothing happens. IDLE doesn't give an error > messa

Need help with the get() method of a Text entry

2007-04-12 Thread Chad
I have a simple little program that brings up asks the user to enter a note, then is supposed to place that note into a text file when the user hits the submit button. However, when the user hits the submit button, absolutely nothing happens. IDLE doesn't give an error message and the note is not

Re: Is there a short-circuiting dictionary "get" method?

2005-03-11 Thread Duncan Booth
Bengt Richter wrote: > then there's always > >if 'x' in d: value = d['x'] >else: value = bsf() > > or > >try: value = d['x'] >except KeyError: value = bsf() > Its worth remembering that the first of those two suggestions is also faster than using get, so you aren't losing on

Re: Is there a short-circuiting dictionary "get" method?

2005-03-11 Thread Bengt Richter
On Fri, 11 Mar 2005 04:12:19 -0500, Steve Holden <[EMAIL PROTECTED]> wrote: >Terry Reedy wrote: >> "Skip Montanaro" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >> >>> value = d.get('x') or bsf() >>> >>>Of course, this bsf() will get called if d['x'] evaluates to false, not

Re: Is there a short-circuiting dictionary "get" method?

2005-03-11 Thread Steve Holden
Terry Reedy wrote: "Skip Montanaro" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] value = d.get('x') or bsf() Of course, this bsf() will get called if d['x'] evaluates to false, not just None, value = (d.get('x') is not None) or bsf() #?? Unfortunately this will set value to Tru

Re: Is there a short-circuiting dictionary "get" method?

2005-03-10 Thread Terry Reedy
"Skip Montanaro" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >value = d.get('x') or bsf() > > Of course, this bsf() will get called if d['x'] evaluates to false, not > just > None, value = (d.get('x') is not None) or bsf() #?? tjr -- http://mail.python.org/mailman/listi

Re: Is there a short-circuiting dictionary "get" method?

2005-03-10 Thread Skip Montanaro
Dave> In this snippet: Dave> d = {'x': 1} Dave> value = d.get('x', bigscaryfunction()) Dave> the bigscaryfunction is always called, even though 'x' is a valid Dave> key. I sometimes use value = d.get('x') or bsf() Of course, this bsf() will get called if d['x'] evaluat

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Jeff Epler
untested def my_getter(m, i, f): try: return m[i] except (KeyError, IndexError): return f() my_getter(d, 'x', bigscaryfunction) my_getter(d, 'y', lambda: scaryinlineexpresion) pgp04VRKFqQL1.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Michael Spencer
Dave Opstad wrote: In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Steven Bethard
Bill Mill wrote: On 9 Mar 2005 10:05:21 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: Maybe this can help: value = d.get('x', lambda: bigscaryfunction()) def test(): print 'gbye' ... d = {} z = d.get('x', lambda: test()) z at 0x008D6870> So this seems to be merely an obfuscation of: z = d.g

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Reinhold Birkenfeld
Dave Opstad wrote: > In this snippet: > > d = {'x': 1} > value = d.get('x', bigscaryfunction()) > > the bigscaryfunction is always called, even though 'x' is a valid key. > Is there a "short-circuit" version of get that doesn't evaluate the > second argument if the first is a valid key? For now

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Kent Johnson
F. Petitjean wrote: Le Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad a écrit : Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around it, but this behavior surprised me a bit... def scary(): print "scary called"

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Bill Mill
On 09 Mar 2005 18:13:01 GMT, F. Petitjean <[EMAIL PROTECTED]> wrote: > Le Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad a écrit : > > In this snippet: > > > > d = {'x': 1} > > value = d.get('x', bigscaryfunction()) > > > > the bigscaryfunction is always called, even though 'x' is a valid key. > > Is

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Bill Mill
On 9 Mar 2005 10:05:21 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Maybe this can help: > > value = d.get('x', lambda: bigscaryfunction()) >>> def test(): print 'gbye' ... >>> d = {} >>> z = d.get('x', lambda: test()) >>> z at 0x008D6870> So this seems to be merely an obfuscation of:

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread F. Petitjean
Le Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad a écrit : > In this snippet: > > d = {'x': 1} > value = d.get('x', bigscaryfunction()) > > the bigscaryfunction is always called, even though 'x' is a valid key. > Is there a "short-circuit" version of get that doesn't evaluate the > second argume

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread bearophileHUGS
Maybe this can help: value = d.get('x', lambda: bigscaryfunction()) Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Bill Mill
Dave, On Wed, 09 Mar 2005 09:45:41 -0800, Dave Opstad <[EMAIL PROTECTED]> wrote: > In this snippet: > > d = {'x': 1} > value = d.get('x', bigscaryfunction()) > > the bigscaryfunction is always called, even though 'x' is a valid key. > Is there a "short-circuit" version of get that doesn't evalua

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Peter Hansen
Dave Opstad wrote: In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around

Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Dave Opstad
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around it, but this beh