Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: Re: How much sanity checking is required for function inputs? Date: Tue, 19 Apr 2016 19:20:43 -0700 Lines: 53 Message-ID: References: <5713E52D.3060407@icloud.com> <57145CB4.5040100@icloud.com> <5716E77B.6030306@icloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de fvyCpDuLRb92IZnYrBlCtgm++mahvMx7xT9U5EIo4W3A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'sanity': 0.07; 'squares': 0.07; 'subject:How': 0.09; 'skip:[ 40': 0.09; 'type)': 0.09; 'exception': 0.13; 'def': 0.13; '"got': 0.16; 'compares': 0.16; 'position)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reusable': 0.16; 'subject:required': 0.16; 'wrote:': 0.16; 'say,': 0.18; 'input': 0.18; 'occurs': 0.22; 'programming': 0.22; 'am,': 0.23; 'code.': 0.23; 'changes,': 0.23; 'thanks,': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'order.': 0.27; 'pieces': 0.27; 'received:17': 0.27; 'yield': 0.27; 'function': 0.28; 'dictionary': 0.29; 'objects': 0.29; 'raise': 0.29; "i'm": 0.30; 'board': 0.30; 'code': 0.30; 'received:10.0.0': 0.32; 'michael': 0.33; 'traceback': 0.33; 'similar': 0.33; 'file': 0.34; 'received:10.0': 0.34; 'lists': 0.34; 'list': 0.34; 'according': 0.36; 'but': 0.36; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'being': 0.37; 'skip:v 20': 0.38; 'wrong': 0.38; 'why': 0.39; 'goes': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'header:MIME-version:1': 0.60; 'your': 0.60; 'provide': 0.61; 'engine': 0.62; 'side': 0.62; 'different': 0.63; 'positions': 0.64; 'color': 0.67; 'header:In-reply-to:1': 0.84; 'positions.': 0.84; 'positions:': 0.84; 'checks.': 0.91; 'subject:much': 0.91; 'rank': 0.97 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-04-20_01:,, 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-1604200033 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-reply-to: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5716E77B.6030306@icloud.com> X-Mailman-Original-References: <5713E52D.3060407@icloud.com> <57145CB4.5040100@icloud.com> Xref: csiph.com comp.lang.python:107388 On 4/19/2016 1:02 AM, Michael Selik wrote: > Why relocate rather than remove? What message would you provide that's > better than ``KeyError: 42`` with a traceback that shows exactly which > dictionary is being used and how? 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) > I meant, what goes wrong if the number of positions input is other than > 16? Without these "sanity" checks, your functions might be reusable in, > say, a checkers game. Chess has 16 pieces (six types) on each side that are set up on the board in a particular order. Checkers has 12 pieces (one type) on each side that are set up on alternating squares in no particular order. Since I'm writing a chess engine because it has endless supply of programming changes, I have no desire to write reusable code for two similar but different games at the same time. Thanks, Chris R.