Re: Writing a string.ishex function

2010-01-22 Thread Albert van der Horst
In article , MRAB wrote: >D'Arcy J.M. Cain wrote: >> On Thu, 14 Jan 2010 07:52:58 -0800 (PST) >> chandra wrote: >>> Folks, >>> >>> I am new to Python and could not find a function along the lines of >> >> Welcome. >> >>> string.ishex in Python. There is however, a string.hexdigits constant >>> i

Re: Writing a string.ishex function

2010-01-15 Thread Steven D'Aprano
On Fri, 15 Jan 2010 00:53:51 +, Steven D'Aprano wrote: > On Thu, 14 Jan 2010 18:36:12 +, MRAB wrote: >> BTW, ishex('') should return False. > > > Only if you want to be inconsistent with other isFoo string functions: > ''.isalpha() > False I said what??? Sorry MRAB, what I wrote

Re: Writing a string.ishex function

2010-01-15 Thread MRAB
Duncan Booth wrote: MRAB wrote: Duncan Booth wrote: MRAB wrote: I raise you one character: ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine I could actually go three better: ishex3=lambda s:not set(s)-set(st

Re: Writing a string.ishex function

2010-01-15 Thread Vlastimil Brom
2010/1/15 Duncan Booth : > MRAB wrote: > >> Duncan Booth wrote: >>> MRAB wrote: >>> I raise you one character: ishex2 = lambda s: not(set(s)-set(string.hexdigits))     # Yours ishex3 = lambda s: not set(s)-set(string.hexdigits)      # Mine I could actually go three b

Re: Writing a string.ishex function

2010-01-15 Thread Duncan Booth
MRAB wrote: > Duncan Booth wrote: >> MRAB wrote: >> >>> I raise you one character: >>> >>> ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours >>> ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine >>> >>> I could actually go three better: >>> >>> ishex3=lambda s:not s

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
Wolfram Hinderer wrote: On 14 Jan., 19:48, MRAB wrote: Arnaud Delobelle wrote: "D'Arcy J.M. Cain" writes: On Thu, 14 Jan 2010 09:07:47 -0800 Chris Rebert wrote: Even more succinctly: def ishex(s): return all(c in string.hexdigits for c in s) I'll see your two-liner and raise you. :-)

Re: Writing a string.ishex function

2010-01-14 Thread Wolfram Hinderer
On 14 Jan., 19:48, MRAB wrote: > Arnaud Delobelle wrote: > > "D'Arcy J.M. Cain" writes: > > >> On Thu, 14 Jan 2010 09:07:47 -0800 > >> Chris Rebert wrote: > >>> Even more succinctly: > > >>> def ishex(s): > >>>     return all(c in string.hexdigits for c in s) > >> I'll see your two-liner and rai

Re: Writing a string.ishex function

2010-01-14 Thread Steven D'Aprano
On Thu, 14 Jan 2010 18:36:12 +, MRAB wrote: >> And here are your unit tests. Every line should print "True". >> >> print ishex('123') is True >> print ishex('abc') is True >> print ishex('xyz') is False >> print ishex('0123456789abcdefABCDEF') is True >> print ishex('0123456789abcdefABCDEFG

Re: Writing a string.ishex function

2010-01-14 Thread Aahz
In article , D'Arcy J.M. Cain wrote: > >try: x = isinstance(s, int) and s or int(s, 0) >except ValueError: [handle invalid input] Why aren't you using the ternary? -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professiona

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
Arnaud Delobelle wrote: MRAB writes: Arnaud Delobelle wrote: "D'Arcy J.M. Cain" writes: On Thu, 14 Jan 2010 09:07:47 -0800 Chris Rebert wrote: Even more succinctly: def ishex(s): return all(c in string.hexdigits for c in s) I'll see your two-liner and raise you. :-) ishex = lambd

Re: Writing a string.ishex function

2010-01-14 Thread Terry Reedy
On 1/14/2010 12:44 PM, D'Arcy J.M. Cain wrote: On Thu, 14 Jan 2010 09:07:47 -0800 Chris Rebert wrote: Even more succinctly: def ishex(s): return all(c in string.hexdigits for c in s) I'll see your two-liner and raise you. :-) ishex = lambda s: all(c in string.hexdigits for c in s) T

Re: Writing a string.ishex function

2010-01-14 Thread Arnaud Delobelle
MRAB writes: > Arnaud Delobelle wrote: >> "D'Arcy J.M. Cain" writes: >> >>> On Thu, 14 Jan 2010 09:07:47 -0800 >>> Chris Rebert wrote: Even more succinctly: def ishex(s): return all(c in string.hexdigits for c in s) >>> I'll see your two-liner and raise you. :-) >>> >>>

Re: Writing a string.ishex function

2010-01-14 Thread exarkun
On 08:15 pm, da...@druid.net wrote: On 14 Jan 2010 19:19:53 GMT Duncan Booth wrote: > ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours > ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine > > I could actually go three better: > > ishex3=lambda s:not set(s)-set(strin

Re: Writing a string.ishex function

2010-01-14 Thread D'Arcy J.M. Cain
On Thu, 14 Jan 2010 18:36:12 + MRAB wrote: > > print ishex('123') is True > > print ishex('abc') is True > > print ishex('xyz') is False > > print ishex('0123456789abcdefABCDEF') is True > > print ishex('0123456789abcdefABCDEFG') is False > > > Don't use 'is', use '=='. Why? There is only o

Re: Writing a string.ishex function

2010-01-14 Thread D'Arcy J.M. Cain
On 14 Jan 2010 19:19:53 GMT Duncan Booth wrote: > > ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours > > ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine > > > > I could actually go three better: > > > > ishex3=lambda s:not set(s)-set(string.hexdigits) > > But non

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
Duncan Booth wrote: MRAB wrote: I raise you one character: ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine I could actually go three better: ishex3=lambda s:not set(s)-set(string.hexdigits) But none of those

Re: Writing a string.ishex function

2010-01-14 Thread Jean-Michel Pichavant
Phlip wrote: MRAB wrote: BTW, ishex('') should return False. So should int('')! Did you mean isint('') ? JM -- http://mail.python.org/mailman/listinfo/python-list

Re: Writing a string.ishex function

2010-01-14 Thread Duncan Booth
MRAB wrote: > I raise you one character: > > ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Yours > ishex3 = lambda s: not set(s)-set(string.hexdigits) # Mine > > I could actually go three better: > > ishex3=lambda s:not set(s)-set(string.hexdigits) But none of those pass you

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
Phlip wrote: MRAB wrote: BTW, ishex('') should return False. So should int('')! Why? -- http://mail.python.org/mailman/listinfo/python-list

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
Arnaud Delobelle wrote: "D'Arcy J.M. Cain" writes: On Thu, 14 Jan 2010 09:07:47 -0800 Chris Rebert wrote: Even more succinctly: def ishex(s): return all(c in string.hexdigits for c in s) I'll see your two-liner and raise you. :-) ishex = lambda s: all(c in string.hexdigits for c in s

Re: Writing a string.ishex function

2010-01-14 Thread Phlip
MRAB wrote: BTW, ishex('') should return False. So should int('')! -- http://mail.python.org/mailman/listinfo/python-list

Re: Writing a string.ishex function

2010-01-14 Thread MRAB
D'Arcy J.M. Cain wrote: On Thu, 14 Jan 2010 07:52:58 -0800 (PST) chandra wrote: Folks, I am new to Python and could not find a function along the lines of Welcome. string.ishex in Python. There is however, a string.hexdigits constant in the string module. I thought I would enhance the exis

Re: Writing a string.ishex function

2010-01-14 Thread Arnaud Delobelle
"D'Arcy J.M. Cain" writes: > On Thu, 14 Jan 2010 09:07:47 -0800 > Chris Rebert wrote: >> Even more succinctly: >> >> def ishex(s): >> return all(c in string.hexdigits for c in s) > > I'll see your two-liner and raise you. :-) > > ishex = lambda s: all(c in string.hexdigits for c in s) I's

Re: Writing a string.ishex function

2010-01-14 Thread Christian Heimes
Iain King wrote: > better would be: > def ishex(s): > for c in s: > if c not in string.hexdigits: > return False > return True Even more elegant and probably a faster solutions: --- from string import hexdigits hexdigits = frozenset(hexdigits) def ishex(s): return

Re: Writing a string.ishex function

2010-01-14 Thread D'Arcy J.M. Cain
On Thu, 14 Jan 2010 09:07:47 -0800 Chris Rebert wrote: > Even more succinctly: > > def ishex(s): > return all(c in string.hexdigits for c in s) I'll see your two-liner and raise you. :-) ishex = lambda s: all(c in string.hexdigits for c in s) -- D'Arcy J.M. Cain | Democracy is

Re: Writing a string.ishex function

2010-01-14 Thread Steve Holden
chandra wrote: > On Jan 15, 12:22 am, "D'Arcy J.M. Cain" wrote: > >> Just return False once you find a non-hex digit. >> >> def ishex(s): >> for c in s: >> if not c in string.hexdigits: return False >> >> return True >> >> And here are your unit tests. Every line should print "True". >>

Re: Writing a string.ishex function

2010-01-14 Thread Chris Rebert
On Thu, Jan 14, 2010 at 8:14 AM, Iain King wrote: > On Jan 14, 3:52 pm, chandra wrote: >> Folks, >> >> I am new to Python and could not find a function along the lines of >> string.ishex in Python. There is however, a string.hexdigits constant >> in the string module. I thought I would enhance th

Re: Writing a string.ishex function

2010-01-14 Thread chandra
On Jan 15, 12:22 am, "D'Arcy J.M. Cain" wrote: > Just return False once you find a non-hex digit. > > def ishex(s): >   for c in s: >     if not c in string.hexdigits: return False > >   return True > > And here are your unit tests.  Every line should print "True". > > print ishex('123') is True

Re: Writing a string.ishex function

2010-01-14 Thread D'Arcy J.M. Cain
On Thu, 14 Jan 2010 07:52:58 -0800 (PST) chandra wrote: > Folks, > > I am new to Python and could not find a function along the lines of Welcome. > string.ishex in Python. There is however, a string.hexdigits constant > in the string module. I thought I would enhance the existing modlue > but a

Re: Writing a string.ishex function

2010-01-14 Thread Iain King
On Jan 14, 3:52 pm, chandra wrote: > Folks, > > I am new to Python and could not find a function along the lines of > string.ishex in Python. There is however, a string.hexdigits constant > in the string module. I thought I would enhance the existing modlue > but am unsure how I should go about it

Writing a string.ishex function

2010-01-14 Thread chandra
Folks, I am new to Python and could not find a function along the lines of string.ishex in Python. There is however, a string.hexdigits constant in the string module. I thought I would enhance the existing modlue but am unsure how I should go about it. Specifically, I have attempted this much: ---