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


Groups > comp.lang.python > #64039

Re: data validation when creating an object

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.freenet.ag!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; '__init__': 0.09; 'filename': 0.09; 'that).': 0.09; 'types:': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'jan': 0.12; '%r"': 0.16; 'early.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'simpson': 0.16; 'str()': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'supplied': 0.16; 'typeerror:': 0.16; 'utterly': 0.16; 'value;': 0.16; 'exception': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'thu,': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'raise': 0.29; 'errors': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; '"",': 0.31; 'fast.': 0.31; 'values.': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'subject:data': 0.36; 'reports': 0.37; 'wrong': 0.37; 'checks': 0.38; 'pm,': 0.38; 'recent': 0.39; 'itself': 0.39; 'simple': 0.61; 'received:': 0.65; 'receive': 0.70; 'obvious': 0.74; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=ggZ0T1/19p0cw4hBJ0n4o3UG8vyqFk6dzs0jf76XtlA=; b=Ne3LMoDcHl7bmv/2Vpwu14/WVxQrZADssHNNjZgF0Adm/E11xtrqFZJj9we+rmtJgh HbDRF24XshxSe/VYBqLvnJAThOnqFJJRz2NyNalswATjbfnme3TSR8VdAQZufO2lm4Sx qkjJVrvJomy6TBpINbhecY6th/w02KM7E86ZQBp8fAUrms2p1RkCdPb6l6ifTUIK/6V1 yqBpaM8dSf8kn2+u0Ph1rjjq1ON8BiDxg9ipsq70ReER8rEpTZ6M7BMh8IERpFni7Jxb FAKr5+U9t30S+Vo/ZZ/2/kjSnyJoP7uvYKEGOcwLOloqb48WSUcAmgcH0Hi7qYxJ1mcI W3UQ==
MIME-Version 1.0
X-Received by 10.68.201.10 with SMTP id jw10mr6554504pbc.25.1389836812686; Wed, 15 Jan 2014 17:46:52 -0800 (PST)
In-Reply-To <20140116012542.GA5065@cskk.homeip.net>
References <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com> <20140116012542.GA5065@cskk.homeip.net>
Date Thu, 16 Jan 2014 12:46:52 +1100
Subject Re: data validation when creating an object
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5561.1389836816.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389836816 news.xs4all.nl 2845 [2001:888:2000:d::a6]:49505
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64039

Show key headers only | View raw


On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson <cs@zip.com.au> wrote:
> However, I would also have obvious validity checks in __init__
> itself on the supplied values. Eg:
>
>   def __init__(self, size, lifetime):
>     if size < 1:
>       raise ValueError("size must be >= 1, received: %r" % (size,))
>     if lifetime <= 0:
>       raise ValueError("lifetime must be > 0, received: %r" % (lifetime,))
>
> Trivial, fast. Fails early. Note that the exception reports the
> receive value; very handy for simple errors like passing utterly
> the wrong thing (eg a filename when you wanted a counter, or something
> like that).

With code like this, passing a filename as the size will raise TypeError on Py3:

>>> size = "test.txt"
>>> size < 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

Yet another advantage of Py3 :)

ChrisA

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


Thread

Re: data validation when creating an object Chris Angelico <rosuav@gmail.com> - 2014-01-16 12:46 +1100

csiph-web