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


Groups > comp.lang.python > #107469

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Christopher Reimer <christopher_reimer@icloud.com>
Newsgroups comp.lang.python
Subject Re: How much sanity checking is required for function inputs?
Date Thu, 21 Apr 2016 18:34:59 -0700
Lines 55
Message-ID <mailman.0.1461288908.2861.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>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de E2ixGLTLJSkOvMVFf2e3zgm5cAnx7IE8iezjxwfkzxhw==
Return-Path <christopher_reimer@icloud.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.059
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; 'method.': 0.05; 'exception.': 0.07; 'sanity': 0.07; 'subject:How': 0.09; 'def': 0.13; 'feedback.': 0.15; 'value.': 0.15; 'knight': 0.16; 'position)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'input': 0.18; 'code,': 0.23; 'thanks,': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'finally,': 0.27; 'received:17': 0.27; 'yield': 0.27; "skip:' 10": 0.28; 'values': 0.28; 'initial': 0.28; 'raise': 0.29; 'code': 0.30; 'everyone': 0.31; 'received:10.0.0': 0.32; 'class': 0.33; 'received:10.0': 0.34; 'lists': 0.34; 'could': 0.35; 'skip:i 20': 0.36; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'method': 0.37; 'thanks': 0.37; 'skip:v 20': 0.38; 'skip:p 20': 0.38; 'test': 0.39; 'url:en': 0.39; 'to:addr:python.org': 0.40; 'header:MIME-version:1': 0.60; 'greetings,': 0.61; 'providing': 0.62; 'charset:windows-1252': 0.62; 'here:': 0.63; 'better.': 0.66; 'incorporate': 0.66; 'validate': 0.76; 'checker': 0.84; 'header:In-reply-to:1': 0.84; 'positions.': 0.84; 'url:readthedocs': 0.84; 'subject:much': 0.91; 'url:latest': 0.91
X-Proofpoint-Virus-Version vendor=fsecure engine=2.50.10432:,, definitions=2016-04-22_02:,, signatures=0
X-Proofpoint-Spam-Details rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1604220022
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2
In-reply-to <57185E88.30201@yahoo.com>
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1461288899; bh=tn+8JH61cL1kUlJgG0B7sUMXKvkJPQyJliSQeiUnyCk=; h=Subject:To:From:Message-id:Date:MIME-version:Content-type; b=jS7b5UTrRvNGYJl3ppJNI09BhNhNiypUqcV2peBP1TX7fzPZZ8XrgBrcGk2/boWjG jPBqW3NG+kdTYVeSSbUPf5emSh0jNchql0WoJiFOpgZFiGPspJ8PHBpeAVlN5Pga0M VKSc1KyMWdOcp65puLotlmt/gADVn7HzNQZ7Kellrm66Ee1bfAcoI3oBKajxi0BEhY pO5LffEYfCe2TERbnfk8CBfC4wXcPp0oaXv0vwgJpNBipdctyE8u+kP9ghopUoDaAe y5UR38OjYiZatZOs298Z9+qlRPbfzBS7x9UrjSFV6NyuUcwunu9ax/mUUZWKsctM1G tapLkjSfp2yoA==
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 <57197FC3.4030701@icloud.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>
Xref csiph.com comp.lang.python:107469

Show key headers only | View raw


Greetings,

Thanks to everyone for providing feedback. Here's my revised code to 
generate a set of chess pieces.


class PieceFactory(object):

         def factory(color, piece, position):
             if piece == 'Bishop':
                 return Bishop(color, position)
             if piece == 'King':
                 return King(color, position)
             if piece == 'Knight':
                 return Knight(color, position)
             if piece == 'Pawn':
                 return Pawn(color, position)
             if piece == 'Queen':
                 return Queen(color, position)
             if piece == 'Rook':
                 return Rook(color, position)

             raise PieceException('No valid Piece object for factory, 
got {}'
                                  ' instead'.format(piece))

         factory = staticmethod(factory)


def generate_set(color, pieces, positions):

     for piece, position in zip(pieces, positions):
         yield getattr(PieceFactory, 'factory')(color, piece, position)


The input values for 'pieces' and 'positions' are 16-item lists zipped 
together to produce a piece name and a position coordinate for the 
factory method. With slight modifications to the code, the factory 
method could also return checker pieces.

I got the factory method from here: 
http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Factory.html

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.

Finally, VARS['VARIABLE_NAME'] got change to const['variable_name']. 
Should smell better.

Thanks,

Chris R

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


Thread

Re: How much sanity checking is required for function inputs? Christopher Reimer <christopher_reimer@icloud.com> - 2016-04-21 18:34 -0700
  Re: How much sanity checking is required for function inputs? Steven D'Aprano <steve@pearwood.info> - 2016-04-22 12:22 +1000

csiph-web