Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39451 > unrolled thread
| Started by | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-02-21 11:38 -0800 |
| Last post | 2013-02-21 13:22 -0800 |
| Articles | 20 on this page of 41 — 9 participants |
Back to article view | Back to comp.lang.python
Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 11:38 -0800
Re: Checking for valid date input and convert appropriately MRAB <python@mrabarnett.plus.com> - 2013-02-21 20:14 +0000
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 13:22 -0800
Re: Checking for valid date input and convert appropriately MRAB <python@mrabarnett.plus.com> - 2013-02-21 21:46 +0000
Re: Checking for valid date input and convert appropriately "Michael Ross" <gmx@ross.cx> - 2013-02-21 23:03 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 15:08 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 15:18 -0800
Re: Checking for valid date input and convert appropriately Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-21 23:30 +0000
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 15:18 -0800
Re: Checking for valid date input and convert appropriately "Michael Ross" <gmx@ross.cx> - 2013-02-22 00:37 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 16:12 -0800
Re: Checking for valid date input and convert appropriately "Michael Ross" <gmx@ross.cx> - 2013-02-22 01:57 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 20:34 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 20:34 -0800
Re: Checking for valid date input and convert appropriately rob.marshall17@gmail.com - 2013-02-21 22:20 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 02:06 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 02:06 -0800
Re: Checking for valid date input and convert appropriately rob.marshall17@gmail.com - 2013-02-21 22:20 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 03:01 -0800
Re: Checking for valid date input and convert appropriately Lele Gaifax <lele@metapensiero.it> - 2013-02-22 13:03 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 05:24 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 05:28 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 05:28 -0800
Re: Checking for valid date input and convert appropriately Lele Gaifax <lele@metapensiero.it> - 2013-02-22 14:35 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 07:07 -0800
Re: Checking for valid date input and convert appropriately Lele Gaifax <lele@metapensiero.it> - 2013-02-22 16:25 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 09:38 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 09:39 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 09:39 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 09:38 -0800
Re: Checking for valid date input and convert appropriately Tim Chase <python.list@tim.thechases.com> - 2013-02-22 10:18 -0600
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 07:07 -0800
Re: Checking for valid date input and convert appropriately Chris Angelico <rosuav@gmail.com> - 2013-02-23 01:03 +1100
Re: Checking for valid date input and convert appropriately Andreas Perstinger <andipersti@gmail.com> - 2013-02-22 15:10 +0100
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 05:24 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 03:01 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 03:45 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-22 03:45 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 16:12 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 15:08 -0800
Re: Checking for valid date input and convert appropriately Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-02-21 13:22 -0800
Page 1 of 3 [1] 2 3 Next page →
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 11:38 -0800 |
| Subject | Checking for valid date input and convert appropriately |
| Message-ID | <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> |
import datetime from datetime
try:
datetime.strptime( date, '%d %m %Y' )
date = date.strptime( '%Y-%m-%d' )
except ValueError:
print( "<h2><font color=red>H ημερομηνία πρέπει να εισαχθεί στην σωστή μορφή => 21 05 2013!" )
sys.exit(0)
================================
Hello, a have an html that among other thing require the user to insert a valid date.
The user must enter a date like "21 02 2013"
a) first i need to check if the entered date was inserted properly as above
b) convert the entered date to '%Y-%m-%d' which is an appropriate mysql date type format.
Also it would be nice if i could check the user entered date within an if statement like i check all my other fields instead of creating an extra ;try' statemnt just for checking tha date
=================================
if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ):
=================================
I want it to be written like the above, but if the user entered date is not valid somehow i need to catch the exception otherwise the cgi-script displays python errors.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-02-21 20:14 +0000 |
| Message-ID | <mailman.2190.1361477655.2939.python-list@python.org> |
| In reply to | #39451 |
On 2013-02-21 19:38, Ferrous Cranus wrote: > import datetime from datetime Should be: from datetime import datetime > > try: > datetime.strptime( date, '%d %m %Y' ) That parses the date and discards the result. > date = date.strptime( '%Y-%m-%d' ) 'date' is a string, it doesn't have a 'strptime' method. When you parsed the date you got a datetime object; _that_ has the 'strptime' method. > except ValueError: > print( "<h2><font color=red>H ημερομηνία πρέπει να εισαχθεί στην σωστή μορφή => 21 05 2013!" ) > sys.exit(0) > ================================ > > Hello, a have an html that among other thing require the user to insert a valid date. > The user must enter a date like "21 02 2013" > > a) first i need to check if the entered date was inserted properly as above > b) convert the entered date to '%Y-%m-%d' which is an appropriate mysql date type format. > > Also it would be nice if i could check the user entered date within an if statement like i check all my other fields instead of creating an extra ;try' statemnt just for checking tha date > > ================================= > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ): > ================================= > > I want it to be written like the above, but if the user entered date is not valid somehow i need to catch the exception otherwise the cgi-script displays python errors. >
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 13:22 -0800 |
| Message-ID | <ca0a0004-2504-486f-9a82-3d1f6234932b@googlegroups.com> |
| In reply to | #39452 |
Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB έγραψε: > On 2013-02-21 19:38, Ferrous Cranus wrote: > > > import datetime from datetime > > > > Should be: > > > > from datetime import datetime > > > > > > > > try: > > > datetime.strptime( date, '%d %m %Y' ) > > > > That parses the date and discards the result. > > > > > date = date.strptime( '%Y-%m-%d' ) > > > > 'date' is a string, it doesn't have a 'strptime' method. > > > > When you parsed the date you got a datetime object; _that_ has the > > 'strptime' method. I don't need to store the date i just want to make sure is entered correctly. I would like to have the user type the date like 21 02 2013 and then convert it to 2013-02-21 because thats how mysql wants the date in order to store it. Please show me how to write this. Also, can you show me how to write it that if so even if the user entered date is wrong it doesn't just crash the cgi-script?i know i can use tr: expect: but i want to avoid it, somehow i need to check the date in the same if statemnt like i do with the other user defined varibles for validity. if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ): Thanks.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-02-21 21:46 +0000 |
| Message-ID | <mailman.2201.1361483381.2939.python-list@python.org> |
| In reply to | #39460 |
On 2013-02-21 21:22, Ferrous Cranus wrote: > Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB έγραψε: >> On 2013-02-21 19:38, Ferrous Cranus wrote: >> > import datetime from datetime >> >> Should be: >> >> from datetime import datetime >> >> > >> > try: >> > datetime.strptime( date, '%d %m %Y' ) >> >> That parses the date and discards the result. >> >> > date = date.strptime( '%Y-%m-%d' ) >> >> 'date' is a string, it doesn't have a 'strptime' method. >> >> When you parsed the date you got a datetime object; _that_ has the >> 'strptime' method. Correction: the datetime object has the 'strftime' for formatting the date. > > I don't need to store the date i just want to make sure is entered correctly. > I would like to have the user type the date like > > 21 02 2013 > and then convert it to 2013-02-21 because thats how mysql wants the date in order to store it. > Please show me how to write this. > The 'strptime' method parses the string and returns a datetime object, and you can then use that object's 'strftime' method to format it to the desired string. > Also, can you show me how to write it that if so even if the user entered date is wrong it doesn't just crash the cgi-script?i know i can use tr: expect: but i want to avoid it, somehow i need to check the date in the same if statemnt like i do with the other user defined varibles for validity. > It _doesn't_ crash the cgi-script. You're telling it to exit if an exception is raised. If you don't want the cgi-script to exit, don't call 'exit'. > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ): > > Thanks. >
[toc] | [prev] | [next] | [standalone]
| From | "Michael Ross" <gmx@ross.cx> |
|---|---|
| Date | 2013-02-21 23:03 +0100 |
| Message-ID | <mailman.2206.1361485261.2939.python-list@python.org> |
| In reply to | #39460 |
On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus <nikos.gr33k@gmail.com> wrote: > Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB > έγραψε: >> On 2013-02-21 19:38, Ferrous Cranus wrote: >> >> > import datetime from datetime >> >> >> >> Should be: >> >> >> >> from datetime import datetime >> >> >> >> > >> >> > try: >> >> > datetime.strptime( date, '%d %m %Y' ) >> >> >> >> That parses the date and discards the result. >> >> >> >> > date = date.strptime( '%Y-%m-%d' ) >> >> >> >> 'date' is a string, it doesn't have a 'strptime' method. >> >> >> >> When you parsed the date you got a datetime object; _that_ has the >> >> 'strptime' method. > > I don't need to store the date i just want to make sure is entered > correctly. > I would like to have the user type the date like > > 21 02 2013 > and then convert it to 2013-02-21 because thats how mysql wants the date > in order to store it. > Please show me how to write this. > > Also, can you show me how to write it that if so even if the user > entered date is wrong it doesn't just crash the cgi-script?i know i can > use tr: expect: but i want to avoid it, somehow i need to check the date > in the same if statemnt like i do with the other user defined varibles > for validity. > You *have* to try/expect in order to not have the script crash. Think user typo: 21 02 2ß13 Personally, I'd use a javascript on the html so users can't POST invalid dates. I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13 as input, and you end up with 21.02.2013 posted to your cgi in any case. Michael
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 15:08 -0800 |
| Message-ID | <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> |
| In reply to | #39474 |
Τη Παρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael Ross έγραψε: > On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus <nikos.gr33k@gmail.com> > > wrote: > > > > > Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB > > > έγραψε: > > >> On 2013-02-21 19:38, Ferrous Cranus wrote: > > >> > > >> > import datetime from datetime > > >> > > >> > > >> > > >> Should be: > > >> > > >> > > >> > > >> from datetime import datetime > > >> > > >> > > >> > > >> > > > >> > > >> > try: > > >> > > >> > datetime.strptime( date, '%d %m %Y' ) > > >> > > >> > > >> > > >> That parses the date and discards the result. > > >> > > >> > > >> > > >> > date = date.strptime( '%Y-%m-%d' ) > > >> > > >> > > >> > > >> 'date' is a string, it doesn't have a 'strptime' method. > > >> > > >> > > >> > > >> When you parsed the date you got a datetime object; _that_ has the > > >> > > >> 'strptime' method. > > > > > > I don't need to store the date i just want to make sure is entered > > > correctly. > > > I would like to have the user type the date like > > > > > > 21 02 2013 > > > and then convert it to 2013-02-21 because thats how mysql wants the date > > > in order to store it. > > > Please show me how to write this. > > > > > > Also, can you show me how to write it that if so even if the user > > > entered date is wrong it doesn't just crash the cgi-script?i know i can > > > use tr: expect: but i want to avoid it, somehow i need to check the date > > > in the same if statemnt like i do with the other user defined varibles > > > for validity. > > > > > > > You *have* to try/expect in order to not have the script crash. > > Think user typo: 21 02 2ß13 > > > > Personally, I'd use a javascript on the html so users can't POST invalid > > dates. > > I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13 as > > input, > > and you end up with 21.02.2013 posted to your cgi in any case. i just want to check for date validity from within ha same if statemnt like i check the other variables and i tried a regualr expression just now: if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and re.search( r'(\d+) (\d+) (\d+)', date ) ) ): Do i ahve somehting wrong in it? will the if become true if the user enters 21 02 2013 ?
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 15:18 -0800 |
| Message-ID | <125c2ff7-9306-4d8d-955e-aac8b3401d19@googlegroups.com> |
| In reply to | #39482 |
Then i try to save the date as MySQL wants but it just aint happening: date = datetime.strftime(date, '%Y-%m-%d') Can you help me please save the user entered date to MySQL format?
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-02-21 23:30 +0000 |
| Message-ID | <mailman.2215.1361489339.2939.python-list@python.org> |
| In reply to | #39484 |
On 21/02/2013 23:18, Ferrous Cranus wrote: > Then i try to save the date as MySQL wants but it just aint happening: > > date = datetime.strftime(date, '%Y-%m-%d') > > Can you help me please save the user entered date to MySQL format? > I suggest testing your code at the interactive prompt, so something like. c:\Users\Mark>python Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from datetime import datetime >>> date = '21 02 2013' >>> date = datetime.strftime(date, '%Y-%m-%d') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str' -- Cheers. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 15:18 -0800 |
| Message-ID | <mailman.2211.1361488727.2939.python-list@python.org> |
| In reply to | #39482 |
Then i try to save the date as MySQL wants but it just aint happening: date = datetime.strftime(date, '%Y-%m-%d') Can you help me please save the user entered date to MySQL format?
[toc] | [prev] | [next] | [standalone]
| From | "Michael Ross" <gmx@ross.cx> |
|---|---|
| Date | 2013-02-22 00:37 +0100 |
| Message-ID | <mailman.2216.1361489840.2939.python-list@python.org> |
| In reply to | #39482 |
On Fri, 22 Feb 2013 00:08:01 +0100, Ferrous Cranus <nikos.gr33k@gmail.com> wrote: > Τη Παρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael > Ross έγραψε: >> On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus >> <nikos.gr33k@gmail.com> >> >> wrote: >> >> >> >> > Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB >> >> > έγραψε: >> >> >> On 2013-02-21 19:38, Ferrous Cranus wrote: >> >> >> >> >> >> > import datetime from datetime >> >> >> >> >> >> >> >> >> >> >> >> Should be: >> >> >> >> >> >> >> >> >> >> >> >> from datetime import datetime >> >> >> >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> > try: >> >> >> >> >> >> > datetime.strptime( date, '%d %m %Y' ) >> >> >> >> >> >> >> >> >> >> >> >> That parses the date and discards the result. >> >> >> >> >> >> >> >> >> >> >> >> > date = date.strptime( '%Y-%m-%d' ) >> >> >> >> >> >> >> >> >> >> >> >> 'date' is a string, it doesn't have a 'strptime' method. >> >> >> >> >> >> >> >> >> >> >> >> When you parsed the date you got a datetime object; _that_ has the >> >> >> >> >> >> 'strptime' method. >> >> > >> >> > I don't need to store the date i just want to make sure is entered >> >> > correctly. >> >> > I would like to have the user type the date like >> >> > >> >> > 21 02 2013 >> >> > and then convert it to 2013-02-21 because thats how mysql wants the >> date >> >> > in order to store it. >> >> > Please show me how to write this. >> >> > >> >> > Also, can you show me how to write it that if so even if the user >> >> > entered date is wrong it doesn't just crash the cgi-script?i know i >> can >> >> > use tr: expect: but i want to avoid it, somehow i need to check the >> date >> >> > in the same if statemnt like i do with the other user defined varibles >> >> > for validity. >> >> > >> >> >> >> You *have* to try/expect in order to not have the script crash. >> >> Think user typo: 21 02 2ß13 >> >> >> >> Personally, I'd use a javascript on the html so users can't POST invalid >> >> dates. >> >> I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13 >> as >> >> input, >> >> and you end up with 21.02.2013 posted to your cgi in any case. > > i just want to check for date validity from within ha same if statemnt > like i check the other variables and i tried a regualr expression just > now: > > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and > ( date and re.search( r'(\d+) (\d+) (\d+)', date ) ) ): > > Do i ahve somehting wrong in it? > > will the if become true if the user enters 21 02 2013 ? I pretend not to know, try it out? And try with invalid inputs, too: 211 02 2013 21 99 2013 31 02 2013
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 16:12 -0800 |
| Message-ID | <49de0696-b6f6-42ce-9272-79dac37c9605@googlegroups.com> |
| In reply to | #39492 |
Please i have been trying hours for this:
try:
datetime.strptime(date, '%d m% %Y')
date = date.strftime('%Y-%m-%d')
except:
print( "<h2>date not propetly entered." )
sys.exit(0)
===========================
the user enters 21 02 2013
a) i just need to check if its in accepted format
b) then truncate the given date to mysql format
That's all i want to do and i cant make it work. Plese tell me how to write this.
I have tried anything i can think of.
[toc] | [prev] | [next] | [standalone]
| From | "Michael Ross" <gmx@ross.cx> |
|---|---|
| Date | 2013-02-22 01:57 +0100 |
| Message-ID | <mailman.2230.1361494634.2939.python-list@python.org> |
| In reply to | #39500 |
On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <nikos.gr33k@gmail.com>
wrote:
> Please i have been trying hours for this:
Don't do that: Spending hours on being stuck. Take a break. Call it a
night.
Brain needs time to unstick itself.
Besides:
>>> from datetime import date
>>> entry='31 03 2013'
>>> day, month, year = int(entry[:2]), int(entry[3:5]), int(entry[6:])
>>> mydate=date(year,month,day)
>>> mydate.strftime('%Y-%m-%d')
'2013-03-31'
> try:
> datetime.strptime(date, '%d m% %Y')
> date = date.strftime('%Y-%m-%d')
> except:
> print( "<h2>date not propetly entered." )
> sys.exit(0)
> ===========================
>
> the user enters 21 02 2013
>
> a) i just need to check if its in accepted format
> b) then truncate the given date to mysql format
>
> That's all i want to do and i cant make it work. Plese tell me how to
> write this.
>
> I have tried anything i can think of.
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 20:34 -0800 |
| Message-ID | <6811dbe3-8574-4b96-836f-6c9828b06edc@googlegroups.com> |
| In reply to | #39510 |
Τη Παρασκευή, 22 Φεβρουαρίου 2013 2:57:03 π.μ. UTC+2, ο χρήστης Michael Ross έγραψε:
> On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <nikos.gr33k@gmail.com>
> wrote:
> > Please i have been trying hours for this:
> Don't do that: Spending hours on being stuck. Take a break. Call it a
>
> night.
>
> Brain needs time to unstick itself.
> Besides:
> >>> from datetime import date
> >>> entry='31 03 2013'
> >>> day, month, year = int(entry[:2]), int(entry[3:5]), int(entry[6:])
>
>
>
> >>> mydate=date(year,month,day)
> >>> mydate.strftime('%Y-%m-%d')
>
> '2013-03-31'
Tis seems very nice solution but also we need to check the user input for validity. What if the user entered: 29 15 2013 ?
i tried to use your method applying some date validation check but that also failed for me:
try:
if( datetime.strptime(date, '%d m% %Y') ):
day, month, year = int(date[:2]), int(date[3:5]), int(date[6:])
date = date(year, month, day)
date = date.strftime('%Y-%m-%d')
except:
print "....."
========================
Isn't method strptime check the user input for validity?
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-21 20:34 -0800 |
| Message-ID | <mailman.2236.1361507671.2939.python-list@python.org> |
| In reply to | #39510 |
Τη Παρασκευή, 22 Φεβρουαρίου 2013 2:57:03 π.μ. UTC+2, ο χρήστης Michael Ross έγραψε:
> On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <nikos.gr33k@gmail.com>
> wrote:
> > Please i have been trying hours for this:
> Don't do that: Spending hours on being stuck. Take a break. Call it a
>
> night.
>
> Brain needs time to unstick itself.
> Besides:
> >>> from datetime import date
> >>> entry='31 03 2013'
> >>> day, month, year = int(entry[:2]), int(entry[3:5]), int(entry[6:])
>
>
>
> >>> mydate=date(year,month,day)
> >>> mydate.strftime('%Y-%m-%d')
>
> '2013-03-31'
Tis seems very nice solution but also we need to check the user input for validity. What if the user entered: 29 15 2013 ?
i tried to use your method applying some date validation check but that also failed for me:
try:
if( datetime.strptime(date, '%d m% %Y') ):
day, month, year = int(date[:2]), int(date[3:5]), int(date[6:])
date = date(year, month, day)
date = date.strftime('%Y-%m-%d')
except:
print "....."
========================
Isn't method strptime check the user input for validity?
[toc] | [prev] | [next] | [standalone]
| From | rob.marshall17@gmail.com |
|---|---|
| Date | 2013-02-21 22:20 -0800 |
| Message-ID | <ceca99e1-c51a-4b31-8a73-de7f46bf4353@googlegroups.com> |
| In reply to | #39518 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-22 02:06 -0800 |
| Message-ID | <98d8e70a-70b8-40a6-bd4c-f489b14314fb@googlegroups.com> |
| In reply to | #39523 |
Τη Παρασκευή, 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!!
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-22 02:06 -0800 |
| Message-ID | <mailman.2246.1361527594.2939.python-list@python.org> |
| In reply to | #39523 |
Τη Παρασκευή, 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!!
[toc] | [prev] | [next] | [standalone]
| From | rob.marshall17@gmail.com |
|---|---|
| Date | 2013-02-21 22:20 -0800 |
| Message-ID | <mailman.2241.1361514692.2939.python-list@python.org> |
| In reply to | #39518 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-02-22 03:01 -0800 |
| Message-ID | <0065de90-75e4-4a50-b084-be6c8c7e5f23@googlegroups.com> |
| In reply to | #39525 |
Τη Παρασκευή, 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') ) ) ):
[toc] | [prev] | [next] | [standalone]
| From | Lele Gaifax <lele@metapensiero.it> |
|---|---|
| Date | 2013-02-22 13:03 +0100 |
| Message-ID | <mailman.2258.1361534636.2939.python-list@python.org> |
| In reply to | #39541 |
Ferrous Cranus <nikos.gr33k@gmail.com> 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.
lele@metapensiero.it | -- Fortunato Depero, 1929.
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web