Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39451 > unrolled thread

Checking for valid date input and convert appropriately

Started byFerrous Cranus <nikos.gr33k@gmail.com>
First post2013-02-21 11:38 -0800
Last post2013-02-21 13:22 -0800
Articles 20 on this page of 41 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  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 2 of 3 — ← Prev page 1 [2] 3  Next page →


#39559

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 05:24 -0800
Message-ID<edc3161d-d11a-4ec6-97d0-fd917fc10b9d@googlegroups.com>
In reply to#39555
Τη Παρασκευή, 22 Φεβρουαρίου 2013 2:03:39 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> 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.

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?

[toc] | [prev] | [next] | [standalone]


#39560

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 05:28 -0800
Message-ID<d4bc35d5-f0ed-40da-9f07-e878fbae4861@googlegroups.com>
In reply to#39559
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" ) 

[toc] | [prev] | [next] | [standalone]


#39561

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 05:28 -0800
Message-ID<mailman.2262.1361539694.2939.python-list@python.org>
In reply to#39559
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" ) 

[toc] | [prev] | [next] | [standalone]


#39563

FromLele Gaifax <lele@metapensiero.it>
Date2013-02-22 14:35 +0100
Message-ID<mailman.2264.1361540146.2939.python-list@python.org>
In reply to#39559
Ferrous Cranus <nikos.gr33k@gmail.com> writes:

> 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:

Let me answer this way: you can't, without resorting to the simple
helper functions I wrote in my previous message. 

Why do you dislike that solution?

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]


#39576

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 07:07 -0800
Message-ID<7c5dff1b-1dd1-494a-b774-47db1e55e329@googlegroups.com>
In reply to#39563
Τη Παρασκευή, 22 Φεβρουαρίου 2013 3:35:31 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> 
> 
> > 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:
> 
> 
> 
> Let me answer this way: you can't, without resorting to the simple
> 
> helper functions I wrote in my previous message. 
> 
> 
> 
> Why do you dislike that solution?
> 
> 
> 
> 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.

ok, i'll just use the try: except: i was just thinking putting them all in the same if() statemt but apparently it cant be done.

Actually it can, but instead of try: i have to create a function:

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

if is_sane_date(date):
    # ...

but the try: solution is much more less hassle.

[toc] | [prev] | [next] | [standalone]


#39578

FromLele Gaifax <lele@metapensiero.it>
Date2013-02-22 16:25 +0100
Message-ID<mailman.2273.1361546760.2939.python-list@python.org>
In reply to#39576
Ferrous Cranus <nikos.gr33k@gmail.com> writes:

> but the try: solution is much more less hassle.

... not to mention it is more effective than your simplicistic check :-)

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]


#39597

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 09:38 -0800
Message-ID<c9d75913-909d-4d50-a757-4ff1bb5825d9@googlegroups.com>
In reply to#39578
Τη Παρασκευή, 22 Φεβρουαρίου 2013 5:25:41 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> 
> 
> > but the try: solution is much more less hassle.
> 
> 
> 
> ... not to mention it is more effective than your simplicistic check :-)
> 
> 
> 
> 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]


#39599

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 09:39 -0800
Message-ID<02452cda-7f27-4bdf-a354-46ba5436dfab@googlegroups.com>
In reply to#39597
Τη Παρασκευή, 22 Φεβρουαρίου 2013 7:38:47 μ.μ. UTC+2, ο χρήστης Ferrous Cranus έγραψε:
> Τη Παρασκευή, 22 Φεβρουαρίου 2013 5:25:41 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> 
> > Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> > 
> 
> > 
> 
> > 
> 
> > > but the try: solution is much more less hassle.
> 
> > 
> 
> > 
> 
> > 
> 
> > ... not to mention it is more effective than your simplicistic check :-)
> 
> > 
> 
> > 
> 
> > 
> 
> > 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.
> 
> 
> 
> :-)

True :-)

[toc] | [prev] | [next] | [standalone]


#39600

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 09:39 -0800
Message-ID<mailman.2291.1361554749.2939.python-list@python.org>
In reply to#39597
Τη Παρασκευή, 22 Φεβρουαρίου 2013 7:38:47 μ.μ. UTC+2, ο χρήστης Ferrous Cranus έγραψε:
> Τη Παρασκευή, 22 Φεβρουαρίου 2013 5:25:41 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> 
> > Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> > 
> 
> > 
> 
> > 
> 
> > > but the try: solution is much more less hassle.
> 
> > 
> 
> > 
> 
> > 
> 
> > ... not to mention it is more effective than your simplicistic check :-)
> 
> > 
> 
> > 
> 
> > 
> 
> > 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.
> 
> 
> 
> :-)

True :-)

[toc] | [prev] | [next] | [standalone]


#39598

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 09:38 -0800
Message-ID<mailman.2290.1361554735.2939.python-list@python.org>
In reply to#39578
Τη Παρασκευή, 22 Φεβρουαρίου 2013 5:25:41 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> 
> 
> > but the try: solution is much more less hassle.
> 
> 
> 
> ... not to mention it is more effective than your simplicistic check :-)
> 
> 
> 
> 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]


#39585

FromTim Chase <python.list@tim.thechases.com>
Date2013-02-22 10:18 -0600
Message-ID<mailman.2279.1361549847.2939.python-list@python.org>
In reply to#39576
On 2013-02-22 07:07, Ferrous Cranus wrote:
> Actually it can, but instead of try: i have to create a function:
> 
> 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

Then you have things like "31 2 2013" being a valid date.  Also, the
hard caps at 1900 and 2100 are both pretty short-sighted.  To the
best of my knowledge, there are valid dates outside that date
range :-)

-tkc

[toc] | [prev] | [next] | [standalone]


#39584

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 07:07 -0800
Message-ID<mailman.2278.1361548007.2939.python-list@python.org>
In reply to#39563
Τη Παρασκευή, 22 Φεβρουαρίου 2013 3:35:31 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> Ferrous Cranus <nikos.gr33k@gmail.com> writes:
> 
> 
> 
> > 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:
> 
> 
> 
> Let me answer this way: you can't, without resorting to the simple
> 
> helper functions I wrote in my previous message. 
> 
> 
> 
> Why do you dislike that solution?
> 
> 
> 
> 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.

ok, i'll just use the try: except: i was just thinking putting them all in the same if() statemt but apparently it cant be done.

Actually it can, but instead of try: i have to create a function:

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

if is_sane_date(date):
    # ...

but the try: solution is much more less hassle.

[toc] | [prev] | [next] | [standalone]


#39566

FromChris Angelico <rosuav@gmail.com>
Date2013-02-23 01:03 +1100
Message-ID<mailman.2266.1361541794.2939.python-list@python.org>
In reply to#39559
On Sat, Feb 23, 2013 at 12:35 AM, Lele Gaifax <lele@metapensiero.it> wrote:
> Ferrous Cranus <nikos.gr33k@gmail.com> writes:
>
>> 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:
>
> Let me answer this way: you can't, without resorting to the simple
> helper functions I wrote in my previous message.
>
> Why do you dislike that solution?

This is very similar to the previous threads that the OP has engaged
in recently. He asks a question of the form "How can I do X, without
using facility Y" where Y is the obvious way to do X.

http://redwing.hutman.net/~mreed/warriorshtm/ferouscranus.htm

My days of not taking him seriously are certainly coming to a middle.

ChrisA

[toc] | [prev] | [next] | [standalone]


#39568

FromAndreas Perstinger <andipersti@gmail.com>
Date2013-02-22 15:10 +0100
Message-ID<mailman.2269.1361542262.2939.python-list@python.org>
In reply to#39559
Lele Gaifax <lele@metapensiero.it> wrote:
>Ferrous Cranus <nikos.gr33k@gmail.com> writes:
>
>> 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:
>
>Let me answer this way: you can't, without resorting to the simple
>helper functions I wrote in my previous message. 
>
>Why do you dislike that solution?

Ferrous Cranus has proven in previous discussions (search the archives)
that he doesn't like common-sense solutions and insists on doing it his
own way (although his methods are either wrong or impossible).

Don't waste your time.

Bye, Andreas

[toc] | [prev] | [next] | [standalone]


#39562

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 05:24 -0800
Message-ID<mailman.2263.1361540139.2939.python-list@python.org>
In reply to#39555
Τη Παρασκευή, 22 Φεβρουαρίου 2013 2:03:39 μ.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
> 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.

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?

[toc] | [prev] | [next] | [standalone]


#39542

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 03:01 -0800
Message-ID<mailman.2249.1361530885.2939.python-list@python.org>
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]


#39551

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 03:45 -0800
Message-ID<585bb86a-436d-433a-8926-c6e065400b8c@googlegroups.com>
In reply to#39542
> 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.

[toc] | [prev] | [next] | [standalone]


#39553

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-22 03:45 -0800
Message-ID<mailman.2257.1361534127.2939.python-list@python.org>
In reply to#39542
> 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.

[toc] | [prev] | [next] | [standalone]


#39501

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-21 16:12 -0800
Message-ID<mailman.2222.1361491968.2939.python-list@python.org>
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]


#39486

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-02-21 15:08 -0800
Message-ID<mailman.2212.1361488829.2939.python-list@python.org>
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]


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

Back to top | Article view | comp.lang.python


csiph-web