
February 22nd, 2013, 08:53 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by Nik Let me ask it like this:
How can i avoid using try: except: for checkign the date but instead check it with an if statement: |
Well it’s possible but demands lots of extra work. First you create a function to do sanity checks:
Code:
def is_sane_date(date):
parts = [int(part) for part in date.split() if part.isdigit()]
if len(parts) == 3 and \
1 <= parts[0] <= 31 and \
1 <= parts[1] <= 12 and \
1900 <= parts[2] <= 2100:
return True
return False
And then you use this in your if test:
Code:
if is_sane_date(date):
# ...
But exceptions are there for a purpose!
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|