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


Groups > comp.lang.python > #39555

Re: Checking for valid date input and convert appropriately

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'exception': 0.03; 'try:': 0.07; 'called.': 0.09; 'received:151': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:a 10': 0.09; 'def': 0.10; "'%d": 0.16; '(typeerror,': 0.16; 'above?': 0.16; 'emanuele': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'valueerror):': 0.16; 'helper': 0.17; 'string,': 0.17; "i'd": 0.22; 'task': 0.23; 'raise': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'writes:': 0.29; 'point': 0.31; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'false': 0.35; "won't": 0.35; 'received:org': 0.36; 'except': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'price': 0.66; 'quando': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lele Gaifax <lele@metapensiero.it>
Subject Re: Checking for valid date input and convert appropriately
Date Fri, 22 Feb 2013 13:03:39 +0100
Organization Nautilus Entertainments
References <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> <mailman.2190.1361477655.2939.python-list@python.org> <ca0a0004-2504-486f-9a82-3d1f6234932b@googlegroups.com> <mailman.2206.1361485261.2939.python-list@python.org> <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> <mailman.2216.1361489840.2939.python-list@python.org> <49de0696-b6f6-42ce-9272-79dac37c9605@googlegroups.com> <mailman.2230.1361494634.2939.python-list@python.org> <mailman.2236.1361507671.2939.python-list@python.org> <mailman.2241.1361514692.2939.python-list@python.org> <0065de90-75e4-4a50-b084-be6c8c7e5f23@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 151.62.64.118
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)
Cancel-Lock sha1:YsJJwsxlOllbMrZDmqAI0T7iQfo=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2258.1361534636.2939.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361534636 news.xs4all.nl 6879 [2001:888:2000:d::a6]:43964
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39555

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web