Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'builtin': 0.07; 'except:': 0.07; 'exception.': 0.07; 'raises': 0.07; 'skip:% 20': 0.07; 'try:': 0.07; 'called.': 0.09; 'subject:skip:a 10': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "'%d": 0.16; '(typeerror,': 0.16; '(usually)': 0.16; 'above?': 0.16; 'emanuele': 0.16; 'valueerror):': 0.16; 'helper': 0.17; 'string,': 0.17; 'trying': 0.21; "i'd": 0.22; 'cc:2**0': 0.23; 'errors': 0.23; 'task': 0.23; 'this:': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'directly,': 0.29; 'exposed': 0.29; 'writes:': 0.29; 'point': 0.31; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'entered': 0.34; 'false': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; "wasn't": 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'two': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'instead': 0.39; '8bit%:95': 0.61; 'price': 0.66; '8bit%:100': 0.70; '8bit%:92': 0.70; '2013': 0.84; 'quando': 0.84; 'surround': 0.84 X-Received: by 10.50.163.41 with SMTP id yf9mr118507igb.3.1361539460650; Fri, 22 Feb 2013 05:24:20 -0800 (PST) Newsgroups: comp.lang.python Date: Fri, 22 Feb 2013 05:24:19 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.68.75.81; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4 References: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> <49de0696-b6f6-42ce-9272-79dac37c9605@googlegroups.com> <0065de90-75e4-4a50-b084-be6c8c7e5f23@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.68.75.81 MIME-Version: 1.0 Subject: Re: Checking for valid date input and convert appropriately From: Ferrous Cranus To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 108 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361540139 news.xs4all.nl 6949 [2001:888:2000:d::a6]:44285 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39562 =CE=A4=CE=B7 =CE=A0=CE=B1=CF=81=CE=B1=CF=83=CE=BA=CE=B5=CF=85=CE=AE, 22 =CE= =A6=CE=B5=CE=B2=CF=81=CE=BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 2013 2:03:3= 9 =CE=BC.=CE=BC. UTC+2, =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE=B7=CF=82 L= ele Gaifax =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > Ferrous Cranus writes: >=20 >=20 >=20 > > I'am thinking if somehting like the follwoing work: >=20 > > >=20 > > if( task and ( price and price.isdigit() and price.__len__() <=3D 3 ) a= nd ( date and eval( datetime.strptime(date, '%d %m %Y').strftime('%Y-%m-%d'= ) ) ) ): >=20 >=20 >=20 > a) you should not (usually) call =E2=80=9Cdunder methods=E2=80=9D directl= y, as they are >=20 > (usually) exposed by builtin functions:: >=20 >=20 >=20 > obj.__len__() =3D> len(obj) >=20 >=20 >=20 > b) what's the point of the eval() call above? When the strptime() >=20 > succeds, the following strftime() call always returns a string, >=20 > otherwise it will raise an exception and both the strftime() and the >=20 > outer eval() won't be called. >=20 >=20 >=20 > Should I write the above, I'd go for having two helper functions, for >=20 > example::=20 >=20 >=20 >=20 > def price_is_valid(price): >=20 > return price and price.isdigit() and len(price) <=3D 3 >=20 >=20 >=20 > def date_is_valid(date): >=20 > try: >=20 > datetime.strptime(date, '%d %m %Y') >=20 > except (TypeError, ValueError): >=20 > return False >=20 > else: >=20 > return True >=20 >=20 >=20 > ... >=20 >=20 >=20 > if task and price_is_valid(price) and date_is_valid(date): >=20 > ... >=20 >=20 >=20 > hth, >=20 > ciao, lele. >=20 > --=20 >=20 > nickname: Lele Gaifax | Quando vivr=C3=B2 di quello che ho pensato ieri >=20 > real: Emanuele Gaifas | comincer=C3=B2 ad aver paura di chi mi copia. >=20 > 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 =3D 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.=20 If i surround it with eval() its still raises an excpetion. if ( datetime.strptime(date, '%d %m %Y') ): date =3D 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?