Re: Writing a string.ishex function

2010-01-22 Thread Albert van der Horst
In article mailman.924.1263494171.28905.python-l...@python.org,
MRAB  pyt...@mrabarnett.plus.com wrote:
D'Arcy J.M. Cain wrote:
 On Thu, 14 Jan 2010 07:52:58 -0800 (PST)
 chandra chyav...@gmail.com 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 am unsure how I should go about it. Specifically, I have attempted
 this much:

 You should always test code before posting and post the exact code that
 you tested.

 ---cut---
 #! /usr/bin/python
 # -*- coding: utf-8 -*-

 import string

 def ishex(string):

 Bad idea to name your variable after a module.  This function fails
 because of that.

 ishex = False
 for i in strdef ishex(sing:
 if i in string.hexdigits:
 ishex = True
 else:
 ishex = False
 break
 return ishex

 After renaming the variable this works but you can simplify it.


 ---cut---

 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
 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 '=='.

BTW, ishex('') should return False.

You are very wrong. Not with the above statement, but
the very act of issuing a statement like that is wrong.

The OP didn't specify ishex().
In absence of a specification, border cases are not defined.

If the specification was:
   any character of string s must be a hex character

then ishex('') should return True.

If the specification was:
   the string must represent a hex number

then ishex('') should probably return False.

I can imagine a specification where it is appropriate to
throw an exception for an empty string.
This is also the safest thing to do, if there is the slightest
hesitation.

So the correct behaviour is:
Please mister customer, what exactly did you have in mind?

Groetjes Albert



--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
alb...@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-15 Thread Duncan Booth
MRAB pyt...@mrabarnett.plus.com wrote:

 Duncan Booth wrote:
 MRAB pyt...@mrabarnett.plus.com 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 your own ishex('') should return False test.
 
 Neither do the others!

That's true, but since you were the one that pointed out they were all 
broken I would have thought your solution should actually work.

I'm sure you'll agree that a longer solution that works trumps any short 
but broken solution.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-15 Thread Vlastimil Brom
2010/1/15 Duncan Booth duncan.bo...@invalid.invalid:
 MRAB pyt...@mrabarnett.plus.com wrote:

 Duncan Booth wrote:
 MRAB pyt...@mrabarnett.plus.com 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 your own ishex('') should return False test.

 Neither do the others!

 That's true, but since you were the one that pointed out they were all
 broken I would have thought your solution should actually work.

 I'm sure you'll agree that a longer solution that works trumps any short
 but broken solution.
 --
 http://mail.python.org/mailman/listinfo/python-list


ishex=lambda s:bool(re.match([a-fA-F0-9]+$,s))
?
vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-15 Thread MRAB

Duncan Booth wrote:

MRAB pyt...@mrabarnett.plus.com wrote:


Duncan Booth wrote:

MRAB pyt...@mrabarnett.plus.com 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 your own ishex('') should return False test.

Neither do the others!


That's true, but since you were the one that pointed out they were all 
broken I would have thought your solution should actually work.


I'm sure you'll agree that a longer solution that works trumps any short 
but broken solution.


I believe that the rules of the character-counting game don't require
correctness, only that it's no worse.

A real solution would use 'def', have a docstring, etc, of course.
--
http://mail.python.org/mailman/listinfo/python-list


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 there was obvious nonsense. I should be agreeing 
with you, not disagreeing!



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Iain King
On Jan 14, 3:52 pm, chandra chyav...@gmail.com 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. Specifically, I have attempted
 this much:
 ---cut---
 #! /usr/bin/python
 # -*- coding: utf-8 -*-

 import string

 def ishex(string):
     ishex = False
     for i in string:
         if i in string.hexdigits:
             ishex = True
         else:
             ishex = False
             break
     return ishex
 ---cut---

 Can someone help me get further along please?

 Thanks.

better would be:
def ishex(s):
for c in s:
if c not in string.hexdigits:
return False
return True

Iain
-- 
http://mail.python.org/mailman/listinfo/python-list


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 chyav...@gmail.com 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 am unsure how I should go about it. Specifically, I have attempted
 this much:

You should always test code before posting and post the exact code that
you tested.

 ---cut---
 #! /usr/bin/python
 # -*- coding: utf-8 -*-
 
 import string
 
 def ishex(string):

Bad idea to name your variable after a module.  This function fails
because of that.

 ishex = False
 for i in strdef ishex(sing:
 if i in string.hexdigits:
 ishex = True
 else:
 ishex = False
 break
 return ishex

After renaming the variable this works but you can simplify it.


 ---cut---

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
print ishex('abc') is True
print ishex('xyz') is False
print ishex('0123456789abcdefABCDEF') is True
print ishex('0123456789abcdefABCDEFG') is False

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread chandra
On Jan 15, 12:22 am, D'Arcy J.M. Cain da...@druid.net 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
 print ishex('abc') is True
 print ishex('xyz') is False
 print ishex('0123456789abcdefABCDEF') is True
 print ishex('0123456789abcdefABCDEFG') is False


Thanks to Iain and to you.

One further question: even though it is a single function, is there
any way to convert it into a module? Can existing modules be enhanced
with functions such as these? If not, how may I make my own module,
called, say mystring? At present, I am saving the file as ishex.py and
calling it after declaring

from ishex import ishex

Is there a more elegant way to integrate this function and make it
available to other python scripts using the import mechanism?

Thanks.

Chandra
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Chris Rebert
On Thu, Jan 14, 2010 at 8:14 AM, Iain King iaink...@gmail.com wrote:
 On Jan 14, 3:52 pm, chandra chyav...@gmail.com 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. Specifically, I have attempted
 this much:
 ---cut---
 #! /usr/bin/python
 # -*- coding: utf-8 -*-

 import string

 def ishex(string):
     ishex = False
     for i in string:
         if i in string.hexdigits:
             ishex = True
         else:
             ishex = False
             break
     return ishex
 ---cut---

 Can someone help me get further along please?

 Thanks.

 better would be:
 def ishex(s):
    for c in s:
        if c not in string.hexdigits:
            return False
    return True

Even more succinctly:

def ishex(s):
return all(c in string.hexdigits for c in s)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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 da...@druid.net 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
 print ishex('abc') is True
 print ishex('xyz') is False
 print ishex('0123456789abcdefABCDEF') is True
 print ishex('0123456789abcdefABCDEFG') is False

 
 Thanks to Iain and to you.
 
 One further question: even though it is a single function, is there
 any way to convert it into a module? Can existing modules be enhanced
 with functions such as these? If not, how may I make my own module,
 called, say mystring? At present, I am saving the file as ishex.py and
 calling it after declaring
 
 from ishex import ishex
 
 Is there a more elegant way to integrate this function and make it
 available to other python scripts using the import mechanism?
 
Nope, that's about as elegant as it gets.

You can, of course, include it in a generic utility module and import
several things from that module - you aren't limited to defining a
single object in a module.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


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 c...@rebertia.com 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 da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 set(s).issubset(hexdigits)
---

If you are also interested in the integer value of a hex string:

---
def gethex(s):
try:
return int(s, 16)
except ValueError:
return None
---

Check for gethex(s) is not None to see if s is a hex string.

Christian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Arnaud Delobelle
D'Arcy J.M. Cain da...@druid.net writes:

 On Thu, 14 Jan 2010 09:07:47 -0800
 Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)

ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Mine

Notice that ishex2 is more accurate than ishex1.  E.g.

 ishex1(['abc', '123'])
True
 ishex2(['abc', '123'])
False

-- 
Arnaud
-- 
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 chyav...@gmail.com 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 am unsure how I should go about it. Specifically, I have attempted
this much:


You should always test code before posting and post the exact code that
you tested.


---cut---
#! /usr/bin/python
# -*- coding: utf-8 -*-

import string

def ishex(string):


Bad idea to name your variable after a module.  This function fails
because of that.


ishex = False
for i in strdef ishex(sing:
if i in string.hexdigits:
ishex = True
else:
ishex = False
break
return ishex


After renaming the variable this works but you can simplify it.



---cut---


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
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 '=='.

BTW, ishex('') should return False.
--
http://mail.python.org/mailman/listinfo/python-list


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

Arnaud Delobelle wrote:

D'Arcy J.M. Cain da...@druid.net writes:


On Thu, 14 Jan 2010 09:07:47 -0800
Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)

ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Mine


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)

:-)


Notice that ishex2 is more accurate than ishex1.  E.g.


ishex1(['abc', '123'])

True

ishex2(['abc', '123'])

False



--
http://mail.python.org/mailman/listinfo/python-list


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 Duncan Booth
MRAB pyt...@mrabarnett.plus.com 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 your own ishex('') should return False test.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 MRAB

Duncan Booth wrote:

MRAB pyt...@mrabarnett.plus.com 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 your own ishex('') should return False test.


Neither do the others!
--
http://mail.python.org/mailman/listinfo/python-list


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 duncan.bo...@invalid.invalid 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 none of those pass your own ishex('') should return False test.

Good point.  Obviously a unit test was missing.

Of course, all this is good clean fun but I wonder how useful an ishex
method really is.  Personally I would tend to do this instead.

try: x = isinstance(s, int) and s or int(s, 0)
except ValueError: [handle invalid input]

IOW return the value whether it is a decimal string (e.g. 12), a hex
string (e.g. 0xaf) or even if it is already an integer.  Of course,
you still have to test for '' and None.

Naturally this all depends heavily on the actual requirements.  Perhaps
that's why there is no ishex method in the first place.  Such a method
could wind up with more options than ls(1)

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread D'Arcy J.M. Cain
On Thu, 14 Jan 2010 18:36:12 +
MRAB pyt...@mrabarnett.plus.com 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 one True and one False in Python.  Is it a style
issue for you?  If so then say shouldn't not don't.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 duncan.bo...@invalid.invalid 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 none of those pass your own ishex('') should return False test.


Good point.  Obviously a unit test was missing.

Of course, all this is good clean fun but I wonder how useful an ishex
method really is.  Personally I would tend to do this instead.

try: x = isinstance(s, int) and s or int(s, 0)
except ValueError: [handle invalid input]

IOW return the value whether it is a decimal string (e.g. 12), a hex
string (e.g. 0xaf) or even if it is already an integer.  Of course,
you still have to test for '' and None.


Still missing some unit tests.  This one fails for 0.  Spend a few more 
lines and save yourself some bugs. :)


Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Arnaud Delobelle
MRAB pyt...@mrabarnett.plus.com writes:

 Arnaud Delobelle wrote:
 D'Arcy J.M. Cain da...@druid.net writes:

 On Thu, 14 Jan 2010 09:07:47 -0800
 Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)

 ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
 ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Mine

 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

Aha! The Operator Precedence trick! I fold.

 I could actually go three better:

 ishex3=lambda s:not set(s)-set(string.hexdigits)

In this case, I'll go five better:

h=lambda s:not set(s)-set(string.hexdigits)

;-)

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


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 Rebertc...@rebertia.com  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)


This is inferior because the resulting function object has generic name 
attribute 'lambda' instead of the specific name 'ishax'.


Please do not push such inferior code on new programmers.

Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread MRAB

Arnaud Delobelle wrote:

MRAB pyt...@mrabarnett.plus.com writes:


Arnaud Delobelle wrote:

D'Arcy J.M. Cain da...@druid.net writes:


On Thu, 14 Jan 2010 09:07:47 -0800
Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)

ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Mine


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


Aha! The Operator Precedence trick! I fold.


I could actually go three better:

ishex3=lambda s:not set(s)-set(string.hexdigits)


In this case, I'll go five better:

h=lambda s:not set(s)-set(string.hexdigits)

;-)


Touché!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Aahz
In article mailman.935.1263499719.28905.python-l...@python.org,
D'Arcy J.M. Cain da...@druid.net 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 professional to do the job, wait
until you hire an amateur.  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


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') is False
 
 Don't use 'is', use '=='.

I beg to differ. In this case, since ishex is intended to return True or 
False and not 1.0 or Decimal(0), the correct test for the purposes of 
unit-testing is the identity test, not equality.

However, for the purposes of *displaying* the result, rather than saying:

print ishex('123') is True

one should obviously say:

print ishex('123') is True is True

No, wait, that should be:

print ishex('123') is True is True is True

No wait, this is better:

print ishex('123') is True is True is True is True

*wink*

Seriously, just say print ishex('123'). It already returns True or False, 
you don't need to compare it to True or False to get a True or False 
answer.


 BTW, ishex('') should return False.


Only if you want to be inconsistent with other isFoo string functions:

 ''.isalpha()
False
 ''.isupper()
False
 ''.islower()
False
 ''.isspace()
False


Also, if ishex(s) returns True, then a reasonable person will expect that 
calling int(s, 16) should succeed. But if s is the empty string, it will 
fail.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread Wolfram Hinderer
On 14 Jan., 19:48, MRAB pyt...@mrabarnett.plus.com wrote:
 Arnaud Delobelle wrote:
  D'Arcy J.M. Cain da...@druid.net writes:

  On Thu, 14 Jan 2010 09:07:47 -0800
  Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)

  ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
  ishex2 = lambda s: not(set(s)-set(string.hexdigits))     # Mine

 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)

 :-)


ishex4=lambda s:not s.strip(string.hexdigits)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a string.ishex function

2010-01-14 Thread MRAB

Wolfram Hinderer wrote:

On 14 Jan., 19:48, MRAB pyt...@mrabarnett.plus.com wrote:

Arnaud Delobelle wrote:

D'Arcy J.M. Cain da...@druid.net writes:

On Thu, 14 Jan 2010 09:07:47 -0800
Chris Rebert c...@rebertia.com 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'see your one-liner and raise you by four characters :o)
ishex1 = lambda s: all(c in string.hexdigits for c in s) # Yours
ishex2 = lambda s: not(set(s)-set(string.hexdigits)) # Mine

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)

:-)



ishex4=lambda s:not s.strip(string.hexdigits)


Doh!
--
http://mail.python.org/mailman/listinfo/python-list