scanning for numerals / letters

2006-04-18 Thread Kun
I have the following if statement that checks if a form is empty:

 if form.has_key("date") and form["date"].value != "":
 date=form['date'].value

 else:
 print "ERROR: No date entered!"
 raise Exception

I would also like to add another if statement checking if 'date' has any 
letters (a-z) in it, and if so, would like to say that "you have to 
enter a date with numbers".  I am not sure how to alter my current if 
statement to do that check so any assistance would be appreciated.


On the flip side, I also have a field called 'purchases' where the user 
must enter non-numerals, thus i would also like to know how to scan to 
see if their entry has numerals and print 'please do not use numbers' if 
they did.

Thanks for your help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: scanning for numerals / letters

2006-04-18 Thread Steve Bergman
Something like this should work:

==
for c in form.get('date'):
  if c in string.letters:
print "ERROR: You have to enter a date with numbers."
==

You have to import the string module.  'letters' is one of the
attributes defined in that module.

Other attributes defined there include 'digits'. (Hint, hint. ;-) )

Also note that if you are not sure if a dictionary has a particular
key, you can use:

form.get('date')

and you will get the value if the key 'date' exists.  If not, you get
None.

It's handier than checking has_key.

You can also say:

form.get('date', '01/01/70')

If the dictionary has a key 'date', you will get the value associated
with it.  Othewise, you will get '01/01/70'.

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


Re: scanning for numerals / letters

2006-04-18 Thread Dale Strickland-Clark
What about this?

import re
if not form.get('date'):
print "Tsk! No date entered."
raise Exception

if re.search('[a-zA-Z]', form.get('date')):
print "Tsk! No fancy date words."
raise Exception

date = form.get('date')

if not form.get('purchases'):
print "Tsk! Are you buying or not?"
raise Exception

if re.search('[0-9]', form.get('purchases')):
print "Tsk! For some reason, you can't buy anything with numbers in it."
raise Exception

purchases = form.get('purchases')


Kun wrote:

> I have the following if statement that checks if a form is empty:
> 
>  if form.has_key("date") and form["date"].value != "":
>  date=form['date'].value
> 
>  else:
>  print "ERROR: No date entered!"
>  raise Exception
> 
> I would also like to add another if statement checking if 'date' has any
> letters (a-z) in it, and if so, would like to say that "you have to
> enter a date with numbers".  I am not sure how to alter my current if
> statement to do that check so any assistance would be appreciated.
> 
> 
> On the flip side, I also have a field called 'purchases' where the user
> must enter non-numerals, thus i would also like to know how to scan to
> see if their entry has numerals and print 'please do not use numbers' if
> they did.
> 
> Thanks for your help.

-- 
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk
We're recruiting programmers. Please see the web site.

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


Re: scanning for numerals / letters

2006-04-19 Thread johnzenger
First, note that form["date"] is all you need.  form["date"].value is
redundant.

I would do this with sets:

import string
if set(form["date"]) & set(string.ascii_letters) != set([]): print "You
have to enter a date with numbers"

if set(form["purchases"]) & set(string.digits) != set([]): print
"Please do not use numbers"

Sets take time to construct, but they test membership faster than
strings.  Plus, the code just reads logically.  (The &, if you couldn't
figure it out, does an intersection of sets.  You could also use the
.intersection method for even better readability).

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


Re: scanning for numerals / letters

2006-04-19 Thread Gerard Flanagan
Kun wrote:
> I have the following if statement that checks if a form is empty:
>
>  if form.has_key("date") and form["date"].value != "":
>  date=form['date'].value
>
>  else:
>  print "ERROR: No date entered!"
>  raise Exception
>
> I would also like to add another if statement checking if 'date' has any
> letters (a-z) in it, and if so, would like to say that "you have to
> enter a date with numbers".  I am not sure how to alter my current if
> statement to do that check so any assistance would be appreciated.
>

Having just attempted a 'string_to_date' function I can see the wisdom
of having separate 'day', 'month' and 'year' input fields on the
client.  If you can't or won't provide separate fields then I suppose
you have to inform users as to what you accept as valid input, eg.
'ddmmyy', or 'month/day/year'.  Here's some code which assumes that you
are providing appropriate formatting hints:

import time
import datetime

DDMMYY = ['%d %m %Y', '%d %m %y', '%d/%m/%Y', '%d/%m/%y', '%d-%m-%Y',
'%d-%m-%y' ]

def yearmonthday(datestring, fmts=DDMMYY):
ymd = tuple()
for f in fmts:
try:
ymd = time.strptime( datestring, f )
break
except ValueError:
continue
if not ymd:
raise ValueError
return ymd[0], ymd[1], ymd[2]

def is_valid_date(datestring, fmts=DDMMYY):
try:
yearmonthday(datestring, fmts)
return True
except ValueError:
return False

def string_to_date(datestring, fmts=DDMMYY):
return datetime.date( *yearmonthday(datestring, fmts) )

assert string_to_date( '1/2/01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '1 2 01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '01/02/01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '1/02/2001', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '29/02/2008', DDMMYY) ==
datetime.date(2008,2,29)
assert string_to_date( '01/2/99', DDMMYY) == datetime.date(1999,2,1)

for d in [ '', '32/1/01', '01/13/01', '29/2/07', '1/2', 'abcdef' ]:
assert not is_valid_date(d, DDMMYY)


Gerard

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