Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: How much sanity checking is required for function inputs? Date: Sun, 17 Apr 2016 22:18:47 +0000 Lines: 47 Message-ID: References: <5713E52D.3060407@icloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de gJZsq//Fy4vyUB7BccaGOQMCNtZXd1zBvlzypoRBkI9Q== Return-Path: 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; 'skip:[ 20': 0.03; 'value,': 0.03; 'classes,': 0.05; 'key.': 0.07; 'sanity': 0.07; 'strings.': 0.07; 'subject:How': 0.09; 'python.': 0.11; 'def': 0.13; 'explicitly': 0.15; '4:35': 0.16; 'constants': 0.16; 'coordinates': 0.16; 'degree.': 0.16; 'dictionary,': 0.16; 'finer': 0.16; 'flavors': 0.16; 'java.': 0.16; 'keyerror': 0.16; 'len': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.16; 'python?': 0.18; 'input': 0.18; 'to:2**1': 0.21; 'fairly': 0.22; 'java': 0.22; 'constant': 0.22; 'code.': 0.23; 'wrote': 0.23; 'specified': 0.23; 'header:In-Reply-To:1': 0.24; 'checking': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'pieces': 0.27; 'skip:i 40': 0.27; 'function': 0.28; 'values': 0.28; 'raise': 0.29; "i'm": 0.30; 'code': 0.30; 'checks': 0.30; 'compared': 0.30; 'position.': 0.30; "i'd": 0.31; 'builds': 0.33; 'hell': 0.33; 'items.': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'community': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'skip:& 10': 0.37; 'turn': 0.37; 'doing': 0.38; 'skip:v 20': 0.38; 'building': 0.38; 'why': 0.39; 'does': 0.39; 'rather': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'determine': 0.61; "you'll": 0.61; 'greetings,': 0.61; 'engine': 0.62; 'details': 0.62; 'positions': 0.64; 'college': 0.67; 'color': 0.67; 'forward.': 0.84; 'rap': 0.84; 'subject:much': 0.91; 'instructors': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=IpJQrFNu8IQMOpSFdQje5Jnubb/JanFKrgAKPx4tMbQ=; b=NR4vySsFfR42kl51+lDnl4E9KJ4oovfs0ixmz0MnLC2KSws4IcA0HwjXNU6RRVFyly sPDyGmqb2fdzYn7GE4wkA/13Qqh7WyacQgc0k6PV+7gn5jFrrO15t4T9U34hTHsCNFW9 klJI8LNAP2rbC1r09I3PbqYW9R7GCg77amdF9+RezpplBXuzi8lh/AmiZjSp+OGlSJ0X +SrieoijinkWo3a1dQOD7D/KBiC8bgk1S4t8/hhckEVGBtixAd/3dvLjnSCRYVQIjjny qN6CkN6ChVDDOJnQ+Q3JaelbrDUJou63PWTdc4hV8P6HRVDhX412M05P3ytPbWwGj/T3 LNGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=IpJQrFNu8IQMOpSFdQje5Jnubb/JanFKrgAKPx4tMbQ=; b=UBod+SFnQYKR3PcMFs3bOxpBipom21YLd+Xlpdmc7ZORSGi3BjBu1Cb9Tu4Qj540CJ 49qOdYmcM1k2NPlIHl290SDv90SBP5fEfGPkmnu61qVSgS4MZMDkkw4faA/NlpbXRaUy ubAAn37fRe8RaVE4PpasTY48jD4qoxGxbHRUC8hN5Oj1kKOr63TlpppykDytQh76Q6wE t02GgAiKljikzWz/qCi3J1JwjcQWVXI+CO2ef+/wR1YLvd3XZPIQz8u5ha28wemoZ0LW H//m9+Ghh6FmXYcFvq5C5m44cA32VYQ+VNy0rFXf9a8K78QwZbiXzeZo4nhHeLGbbCI6 aekw== X-Gm-Message-State: AOPr4FUUY96dPSVALBnOW/qnESWVPsR0JR3ZkFV8d/7ha1k2hCsBfhl3ROycadZo4LaPYDtY88lO8BEv1enrJQ== X-Received: by 10.28.20.198 with SMTP id 189mr15483473wmu.103.1460931537146; Sun, 17 Apr 2016 15:18:57 -0700 (PDT) In-Reply-To: <5713E52D.3060407@icloud.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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: X-Mailman-Original-References: <5713E52D.3060407@icloud.com> Xref: csiph.com comp.lang.python:107202 On Sun, Apr 17, 2016, 4:35 PM Christopher Reimer < christopher_reimer@icloud.com> wrote: > Greetings, > > I'm currently building a chess engine to learn the finer details of > Python. When I learned all flavors of Java in community college a decade > ago, we had to sanity check the hell out of the input values for every > function and wrote a lot of redundant code in addition to the > getters/setters code. > > Here's the input sanity checking I got for a generator function to > return a set of chess pieces for a specified color and position. > > > def generate_set(color, positions): > > if color not in [VARS['COLOR_BLACK'], VARS['COLOR_WHITE']]: > raise Exception("Require \'{}\' or \'{}\' for input value, got > \'{}\' instead.".format(VARS['COLOR_BLACK'], VARS['COLOR_WHITE'], color)) > > if len(positions) != 16: > raise Exception("Require 16 positions for input value, got {} > instead.".format(len(positions))) > > > The two sanity checks are fairly straight forward. Color input has to > match the color constant strings. Positions input has to have 16 items. > > I *could* sanity check the positions input to determine if it had a list > of 16 valid coordinates. The reason I don't is because the code that > calls this function builds the coordinates from constants with valid > coordinates. However, if I was doing this in my Java classes, one of my > instructors rap me on the knuckles for not sanity checking to nth degree. > > How much sanity checking is too much in Python? > I'd rather turn the question around: how much sanity checking is necessary or useful? You'll find the answer is "surprisingly little" compared to your experience in Java. For example, you don't need to explicitly check whether the color is present in your dictionary, because it'll give you a KeyError if you look up a bad key. Why does the len of positions need to be 16? >