Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Checking for valid date input and convert appropriately

Reply
Thread Tools

Checking for valid date input and convert appropriately

 
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 8:20:20 π.μ. UTC+2, ο χρήστης rob.mar...@gmail.com *γραψε:
> The datetime function: strptime() DOES check the date for validity. So try something like:
>
>
>
> from datetime import datetime
>
>
>
> def get_date():
>
> while True:
>
> try:
>
> date_in = raw_input("Enter date (dd mm yyyy): ")
>
> date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
>
> return date_out
>
> except ValueError:
>
> print "Invalid date: {}, try again...".format(date_in)


Thank you very very much!! i cannot beleive that it was so easy, a matter of one line of coding!

date = datetime.strptime(date,"%d %m %Y").strftime("%Y-%m-%d")

Cna you please explain in to me?
This line checks the date variable for valid pattern entry and then also tranforms the date to the othjer pattern?

And if there is a way to embed this line to the existing if() statemtn along with the othwr variables check that would be perfect!!

 
Reply With Quote
 
 
 
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 8:20:20 π.μ. UTC+2, ο χρήστης rob.mar...@gmail.com *γραψε:
> The datetime function: strptime() DOES check the date for validity. So try something like:
>
>
>
> from datetime import datetime
>
>
>
> def get_date():
>
> while True:
>
> try:
>
> date_in = raw_input("Enter date (dd mm yyyy): ")
>
> date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
>
> return date_out
>
> except ValueError:
>
> print "Invalid date: {}, try again...".format(date_in)


I'am thinking if somehting like the follwoing work:

if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and (date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):
 
Reply With Quote
 
 
 
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 8:20:20 π.μ. UTC+2, ο χρήστης rob.mar...@gmail.com *γραψε:
> The datetime function: strptime() DOES check the date for validity. So try something like:
>
>
>
> from datetime import datetime
>
>
>
> def get_date():
>
> while True:
>
> try:
>
> date_in = raw_input("Enter date (dd mm yyyy): ")
>
> date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
>
> return date_out
>
> except ValueError:
>
> print "Invalid date: {}, try again...".format(date_in)


I'am thinking if somehting like the follwoing work:

if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and (date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013

> I'am thinking if somehting like the follwoing work:
>
> if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):


I just tried it out this workaround so to avoid having an extra try: except: but if the user doesnt enter the date properly it still raises a ValueError exception.

Is there a way to still use the if statemnt and somehow supress the message nd let else: handle it somehow?

thank you.
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013

> I'am thinking if somehting like the follwoing work:
>
> if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):


I just tried it out this workaround so to avoid having an extra try: except: but if the user doesnt enter the date properly it still raises a ValueError exception.

Is there a way to still use the if statemnt and somehow supress the message nd let else: handle it somehow?

thank you.
 
Reply With Quote
 
Lele Gaifax
Guest
Posts: n/a
 
      02-22-2013
Ferrous Cranus <> writes:

> I'am thinking if somehting like the follwoing work:
>
> if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):


a) you should not (usually) call “dunder methods” directly, as they are
(usually) exposed by builtin functions::

obj.__len__() => len(obj)

b) what's the point of the eval() call above? When the strptime()
succeds, the following strftime() call always returns a string,
otherwise it will raise an exception and both the strftime() and the
outer eval() won't be called.

Should I write the above, I'd go for having two helper functions, for
example::

def price_is_valid(price):
return price and price.isdigit() and len(price) <= 3

def date_is_valid(date):
try:
datetime.strptime(date, '%d %m %Y')
except (TypeError, ValueError):
return False
else:
return True

...

if task and price_is_valid(price) and date_is_valid(date):
...

hth,
ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
| -- Fortunato Depero, 1929.

 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 2:03:39 μ.μ. UTC+2, ο χρήστης Lele Gaifax *γραψε:
> Ferrous Cranus <> writes:
>
>
>
> > I'am thinking if somehting like the follwoing work:

>
> >

>
> > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):

>
>
>
> a) you should not (usually) call “dunder methods” directly, as they are
>
> (usually) exposed by builtin functions::
>
>
>
> obj.__len__() => len(obj)
>
>
>
> b) what's the point of the eval() call above? When the strptime()
>
> succeds, the following strftime() call always returns a string,
>
> otherwise it will raise an exception and both the strftime() and the
>
> outer eval() won't be called.
>
>
>
> Should I write the above, I'd go for having two helper functions, for
>
> example::
>
>
>
> def price_is_valid(price):
>
> return price and price.isdigit() and len(price) <= 3
>
>
>
> def date_is_valid(date):
>
> try:
>
> datetime.strptime(date, '%d %m %Y')
>
> except (TypeError, ValueError):
>
> return False
>
> else:
>
> return True
>
>
>
> ...
>
>
>
> if task and price_is_valid(price) and date_is_valid(date):
>
> ...
>
>
>
> hth,
>
> ciao, lele.
>
> --
>
> nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
>
> real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
>
> | -- Fortunato Depero, 1929.


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:

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.

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" )

How can i write that with an if suppresing the errors if any?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 2:03:39 μ.μ. UTC+2, ο χρήστης Lele Gaifax *γραψε:
> Ferrous Cranus <> writes:
>
>
>
> > I'am thinking if somehting like the follwoing work:

>
> >

>
> > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d') ) ) ):

>
>
>
> a) you should not (usually) call “dunder methods” directly, as they are
>
> (usually) exposed by builtin functions::
>
>
>
> obj.__len__() => len(obj)
>
>
>
> b) what's the point of the eval() call above? When the strptime()
>
> succeds, the following strftime() call always returns a string,
>
> otherwise it will raise an exception and both the strftime() and the
>
> outer eval() won't be called.
>
>
>
> Should I write the above, I'd go for having two helper functions, for
>
> example::
>
>
>
> def price_is_valid(price):
>
> return price and price.isdigit() and len(price) <= 3
>
>
>
> def date_is_valid(date):
>
> try:
>
> datetime.strptime(date, '%d %m %Y')
>
> except (TypeError, ValueError):
>
> return False
>
> else:
>
> return True
>
>
>
> ...
>
>
>
> if task and price_is_valid(price) and date_is_valid(date):
>
> ...
>
>
>
> hth,
>
> ciao, lele.
>
> --
>
> nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
>
> real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
>
> | -- Fortunato Depero, 1929.


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:

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.

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" )

How can i write that with an if suppresing the errors if any?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
i made a liitle typo at the ned, i meant this:

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" )
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
i made a liitle typo at the ned, i meant this:

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" )
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get DIV height in IE6 to set appropriately (IE7 works fine) Richard.Gohs@pa-tech.com HTML 1 09-24-2008 08:14 AM
How do I tell "imconplete input" from "valid input"? $B$?$+(B Python 2 05-29-2008 10:32 AM
Using size_t clearly (appropriately?) Mark Odell C Programming 40 07-13-2006 06:19 AM
Return appropriately by value, (smart) pointer, or reference Neal Coombes C++ 5 09-22-2005 10:22 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57