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


Groups > comp.lang.python > #107391

Re: How much sanity checking is required for function inputs?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: How much sanity checking is required for function inputs?
Date Wed, 20 Apr 2016 13:50:11 +1000
Lines 61
Message-ID <mailman.12.1461124214.12923.python-list@python.org> (permalink)
References <5713E52D.3060407@icloud.com> <CAGgTfkOR8e501zFfRXLgv3imVnbL7nBqKNGhc95rJVBPPwEYeg@mail.gmail.com> <57145CB4.5040100@icloud.com> <CAGgTfkM41po9gWS=spn4+imqSczhnJsKdwBjBT0NHYpSW82JoA@mail.gmail.com> <5716E77B.6030306@icloud.com> <CAPTjJmrspoCLdNVRSg0StH2nt_cGO=1JgP2-06OzuFiYo6moVw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de jzqubkBwa+DnjYgv72sIRAbNQAElB9E0kcL6Zn139Ucw==
Return-Path <rosuav@gmail.com>
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; 'received:209.85.223': 0.03; 'elif': 0.04; 'classes,': 0.05; 'definitions': 0.07; 'sanity': 0.07; 'table.': 0.07; 'cc:addr :python-list': 0.09; 'subject:How': 0.09; 'exception,': 0.09; 'skip:[ 40': 0.09; 'subclass': 0.09; 'assume': 0.11; 'exception': 0.13; 'def': 0.13; 'wed,': 0.15; '"got': 0.16; '2016': 0.16; 'compares': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'places.': 0.16; 'position)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'wrote:': 0.16; 'example.': 0.18; 'input': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(by': 0.22; "aren't": 0.22; 'occurs': 0.22; 'code.': 0.23; 'slightly': 0.23; 'split': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'function': 0.28; 'initial': 0.28; 'looks': 0.29; 'btw,': 0.29; 'dictionary': 0.29; 'objects': 0.29; 'raise': 0.29; "i'm": 0.30; 'board': 0.30; 'code': 0.30; "i'd": 0.31; 'rules': 0.31; 'usually': 0.33; 'file': 0.34; 'lists': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'according': 0.36; 'but': 0.36; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'seem': 0.37; 'starting': 0.37; 'doing': 0.38; 'skip:v 20': 0.38; 'received:209': 0.38; 'or,': 0.38; 'sure': 0.39; 'easily': 0.39; 'some': 0.40; 'your': 0.60; 'more': 0.63; 'different': 0.63; 'great': 0.63; 'positions': 0.64; '20,': 0.66; 'else.': 0.66; 'color': 0.67; '12:20': 0.84; 'chrisa': 0.84; 'positions.': 0.84; 'positions:': 0.84; 'tie': 0.84; 'to:none': 0.91; 'checks.': 0.91; 'subject:much': 0.91; 'rank': 0.97
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; bh=ad7Ox40SNw0laxRpGiqhdlSYwLNZA3q1VD7lutZUIlE=; b=a0PvCEymkxlxa2p141n8TRqI+vMdgexkF/ts+g1TVBWjj6cd52SrLZRYBAhLpJe9HB PEmQJ0yjEeMiuXGlxlhgDPjdu6ETiCvZEgNlTcOqU8I0fJDnUQwBGI27vcXX7SeNe7IM au6aEk23HJpBOjamB1RhXo18xLMtGO8UKIdE0vU15DpY55yUMIpp9nCq8bD/bHPc95ie cN0PIlbdzLkTODgfwnwtq4gITbpjcAXjUcILR1+/XvKGf2dzU6vzPcANkU8KaEhC1/v6 XR3yfFLt7xhICHZTN9/opHwKiJCusB+HxnbfQbTWSmUuMaiAd9MapNg7Ze8ZfNnhNRDg qAdA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=ad7Ox40SNw0laxRpGiqhdlSYwLNZA3q1VD7lutZUIlE=; b=LgiATHEzMGSEu8j4mplKeUG8qtQZJnS3ZUVhken3oU5fj42lFzznKDElfyagchB5qh bzMASjXFxzpd3beTDX3HQ/jRrVLF6UJRC78qtYTvzTq+fI8M3GTDEeH+pb6si445xVyF TUaFN1N7WvFZJhT/xFCwmao8OcdF/UcWDG+CBH8koESm4QPFCbxeVn4or/VAcCinpjBJ 202YsHBV9712QdItIGDA0lMFLH6Fx0ECCYRTAmcxdl4swA5MmEqKgwcvfZFZUip9SNgi rWbbsJP9oigMXlqKCtuGL6VA6opNTVWpfIl1MOKixo77vpJBoeDRyf0IQOTacdYYayES dIXg==
X-Gm-Message-State AOPr4FVBULIoxvubB94l9D4dJct6UO1UnFOGzctYrfX94roGxfnc1Eik2/717NvDgQNlzElkCmfoYjiN8oJBhg==
X-Received by 10.107.169.13 with SMTP id s13mr4076429ioe.19.1461124211663; Tue, 19 Apr 2016 20:50:11 -0700 (PDT)
In-Reply-To <5716E77B.6030306@icloud.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CAPTjJmrspoCLdNVRSg0StH2nt_cGO=1JgP2-06OzuFiYo6moVw@mail.gmail.com>
X-Mailman-Original-References <5713E52D.3060407@icloud.com> <CAGgTfkOR8e501zFfRXLgv3imVnbL7nBqKNGhc95rJVBPPwEYeg@mail.gmail.com> <57145CB4.5040100@icloud.com> <CAGgTfkM41po9gWS=spn4+imqSczhnJsKdwBjBT0NHYpSW82JoA@mail.gmail.com> <5716E77B.6030306@icloud.com>
Xref csiph.com comp.lang.python:107391

Show key headers only | View raw


On Wed, Apr 20, 2016 at 12:20 PM, Christopher Reimer
<christopher_reimer@icloud.com> wrote:
> I think you misread my code. No dictionary exception occurs in the sanity
> checks. Below is the full function with the revised sanity check for
> positions that compares the input list with the two valid lists of board
> positions.
>
>
> def generate_set(color, positions):
>
>     if positions not in [VARS['COORDINATES'][VARS['BOARD_BOTTOM']],
>                          VARS['COORDINATES'][VARS['BOARD_TOP']]]:
>         raise Exception("List for positions contains no valid coordinates, "
>                         "got {} instead.".format(positions))
>
>     # generate objects according to color and position
>     for position in positions:
>         rank, file = position
>         if rank in VARS['RANK_NOBILITY']:
>             if file in VARS['FILE_ROOK']:
>                 yield Rook(color, position)
>             elif file in VARS['FILE_BISHOP']:
>                 yield Bishop(color, position)
>             elif file in VARS['FILE_KNIGHT']:
>                 yield Knight(color, position)
>             elif file is VARS['FILE_QUEEN']:
>                 yield Queen(color, position)
>             elif file is VARS['FILE_KING']:
>                 yield King(color, position)
>         else:
>             yield Pawn(color, position)
>

If you nuke the check at the beginning, what happens? (By the way, I
don't like this shouty "VARS" thing you have going on. It makes it
hard to skim the code. I'm not entirely sure what your initial "not
in" check is even doing.) You seem to have two slightly different ways
of checking things: first something about BOARD_TOP and BOARD_BOTTOM,
and then something else about RANK_NOBILITY and... everything else. It
looks to me like all you need is to have a second check for
RANK_PAWNS, and then raise an exception if it's neither of those. Then
you don't need to pre-check, and the rules aren't split into two
places.

But if I were doing this, I'd make it far more data-driven.

starting_positions = {
    VARS['FILE_ROOK']: Rook,
    VARS['FILE_BISHOP']: Bishop,
    ...
}

Or, even better, tie that to the definitions of your classes, in some
way. (Metaprogramming is great here.) Then your code doesn't need an
if/elif tree; you can generate starting positions easily by just
looking up your table.

BTW, it's usually best to not raise Exception, but a custom subclass
of it. But I'm going to assume that this is just an example.

ChrisA

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


Thread

Re: How much sanity checking is required for function inputs? Chris Angelico <rosuav@gmail.com> - 2016-04-20 13:50 +1000

csiph-web