Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old February 22nd, 2013, 07:27 AM
Nik's Avatar
Nik Nik is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Location: Thessaloniki
Posts: 1,004 Nik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 52 m 42 sec
Reputation Power: 11
Send a message via Google Talk to 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:

Code:
if ( datetime.strptime(date, '%d %m %Y') ): 
     date = datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') 
else: 
     print( "Date wasn't entered properly" ) 


I'am trying this but if user entered date is noit on the acceptible format it raises an exception.
If i surround it with eval() its still raises an excpetion.

Code:
if ( eval( datetime.strptime(date, '%d %m %Y')  ) ): 
     date = datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') 
else: 
     print( "Date wasn't entered properly" ) 


How can i write that with an if suppresing the errors if any?
__________________
What is now proved was once only imagined!

Reply With Quote
  #17  
Old February 22nd, 2013, 08:53 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 56 m 16 sec
Reputation Power: 65
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)

Reply With Quote
  #18  
Old February 22nd, 2013, 09:07 AM
Nik's Avatar
Nik Nik is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Location: Thessaloniki
Posts: 1,004 Nik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 52 m 42 sec
Reputation Power: 11
Send a message via Google Talk to Nik
ok, i'll just use the try: except: i was just thinking putting them all in the same if() statemt but apparently it can't be done without creatign extra functions!

thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Checking for valid date input and convert appropriately

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap