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


Groups > comp.lang.python > #107521

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Matt Wheeler <m@funkyhat.org>
Newsgroups comp.lang.python
Subject Re: How much sanity checking is required for function inputs?
Date Sat, 23 Apr 2016 22:33:36 +0100
Lines 57
Message-ID <mailman.14.1461447652.32212.python-list@python.org> (permalink)
References <5713E52D.3060407@icloud.com> <1461131475.4042173.584042297.03DAB00E@webmail.messagingengine.com> <57171D23.40709@stoneleaf.us> <1461133057.4046578.584060569.2DD34821@webmail.messagingengine.com> <57185E88.30201@yahoo.com> <57197FC3.4030701@icloud.com> <57198827.1020608@stoneleaf.us> <5719964E.2050400@icloud.com> <CAG93HwHmQUABxKwDdWEbdTJ0gDQVcUW52uqjGLC6j8b53MPh4g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de VEIgraFNlDX57GGnG5jqXQO196IYpserFDrLxObosEFQ==
Return-Path <m@funkyhat.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'exception.': 0.07; 'sanity': 0.07; 'subject:How': 0.09; 'backwards': 0.09; 'prevents': 0.09; 'subclass': 0.09; 'stored': 0.10; 'def': 0.13; 'suggest': 0.15; 'value.': 0.15; '2016': 0.16; 'comparison.': 0.16; 'knight': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'this)': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.16; 'skip:` 10': 0.18; 'tests': 0.18; '>>>': 0.20; 'level,': 0.22; 'header:In-Reply- To:1': 0.24; '(which': 0.26; 'sense': 0.26; 'bugs': 0.27; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'initial': 0.28; 'interface': 0.29; 'raise': 0.29; "i'm": 0.30; 'classes': 0.30; 'received:209.85.215.46': 0.30; "i'd": 0.31; 'probably': 0.31; 'implement': 0.32; 'class': 0.33; "i'll": 0.33; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'being': 0.37; 'method': 0.37; 'thought': 0.37; 'starting': 0.37; 'doing': 0.38; "won't": 0.38; 'received:209': 0.38; 'means': 0.39; 'test': 0.39; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'suitable': 0.61; 'positions': 0.64; 'incorporate': 0.66; 'state,': 0.66; 'carried': 0.76; 'validate': 0.76; 'hand': 0.82; 'from:addr:m': 0.84; 'positions.': 0.84; 'ethan': 0.91; 'furman': 0.91; 'subject:much': 0.91
X-Virus-Scanned Debian amavisd-new at membrane.funkyhat.net
X-Gm-Message-State AOPr4FXSAl2I5Yl7FVBP7GErCO7PkeHSR1va6OLR4UJ2KX/VXC9RIXukOHhSxsQ+KcqAhvLcXU12liBlBZfSzw==
X-Received by 10.25.20.155 with SMTP id 27mr8281102lfu.90.1461447236759; Sat, 23 Apr 2016 14:33:56 -0700 (PDT)
In-Reply-To <5719964E.2050400@icloud.com>
X-Gmail-Original-Message-ID <CAG93HwHmQUABxKwDdWEbdTJ0gDQVcUW52uqjGLC6j8b53MPh4g@mail.gmail.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 <CAG93HwHmQUABxKwDdWEbdTJ0gDQVcUW52uqjGLC6j8b53MPh4g@mail.gmail.com>
X-Mailman-Original-References <5713E52D.3060407@icloud.com> <1461131475.4042173.584042297.03DAB00E@webmail.messagingengine.com> <57171D23.40709@stoneleaf.us> <1461133057.4046578.584060569.2DD34821@webmail.messagingengine.com> <57185E88.30201@yahoo.com> <57197FC3.4030701@icloud.com> <57198827.1020608@stoneleaf.us> <5719964E.2050400@icloud.com>
Xref csiph.com comp.lang.python:107521

Show key headers only | View raw


On 22 April 2016 at 04:11, Christopher Reimer
<christopher_reimer@icloud.com> wrote:
> On 4/21/2016 7:10 PM, Ethan Furman wrote:
>>> I do plan to incorporate a sanity test in each Piece class to validate
>>> the initial position value. Pawns have 16 specific positions. Bishop,
>>> Knight and Rook each have four specific positions. King and Queen each
>>> have two specific positions. An invalid value will raise an exception.
>>
>> This will make it so you cannot use your PieceFactory for custom setups.
>
> The sanity check won't be in the PieceFactory, but in the Piece class as an
> interface and each Piece subclass will implement the correct positions for
> comparison.

This is still backwards to me. It prevents your classes from being
suitable for restoring a stored game state, not just custom starting
positions (which I think is what Ethan means by custom setups).

If you need to put sanity checking in your initialisation at all it
should be at some higher level, probably in a method named something
like `start_chess_game`. But really I'd suggest just writing tests for
that method to make sure it's doing the right thing.

On the other hand (and I'm sure you've already thought of this) it
probably does make sense to put piece move validation in to each of
the piece classes: you could even have some fun with it...

def move_straight(old, new):
    if old[0] == new[0] or old[1] == new[1]:
        return True

def move_diag(old, new):
    diff_x = abs(old[0] - new[0])
    diff_y = abs(old[1] - new[1])
    if diff_x == diff_y:
        return True

class Piece:
   ...
    def move(self, new):
        if self.validate_move(new):
            # do the move
            ...
        else: raise IllegalMove('or something')

class Queen(Piece):
    def validate_move(self, new):
        return any((test(self.position, new) for test in
(move_straight, move_diag)))


Ok I'll stop before I get too carried away... This is completely
untested so bugs will abound I'm sure :)

-- 
Matt Wheeler
http://funkyh.at

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


Thread

Re: How much sanity checking is required for function inputs? Matt Wheeler <m@funkyhat.org> - 2016-04-23 22:33 +0100

csiph-web