Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Stephen Hansen Newsgroups: comp.lang.python Subject: Re: How much sanity checking is required for function inputs? Date: Thu, 21 Apr 2016 19:20:13 -0700 Lines: 62 Message-ID: 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> <1461291613.433097.586174185.01BB7E37@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de j23GRrGHrCe7f9kVFXfROwl41j6ieAUOs+igpGXk7ecg== 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; 'none:': 0.05; '21,': 0.07; 'pretend': 0.07; 'semantic': 0.07; 'subject:How': 0.09; 'anymore.': 0.09; 'before.': 0.09; 'received:internal': 0.09; 'def': 0.13; 'thu,': 0.15; '...]': 0.16; 'begging': 0.16; 'dictionary.': 0.16; 'instantiate': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'position)': 0.16; 'reason?': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'wrote:': 0.16; 'changes': 0.20; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; "i've": 0.25; "doesn't": 0.26; 'skip:_ 20': 0.26; 'finally,': 0.27; 'said,': 0.27; 'yield': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'went': 0.28; 'dictionary': 0.29; 'helpful.': 0.29; 'raise': 0.29; 'there.': 0.30; "i'm": 0.30; 'that.': 0.30; "i'd": 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'text': 0.35; 'something': 0.35; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'skip:v 20': 0.38; 'received:66': 0.38; 'wrong': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'sure': 0.39; 'url:en': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'forget': 0.60; 'header :Message-Id:1': 0.61; 'here:': 0.63; 'better.': 0.66; 'dear': 0.67; 'function)': 0.84; 'god,': 0.84; 'url:readthedocs': 0.84; 'subject:much': 0.91; 'url:latest': 0.91 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=1FmYTrnm9H2SNVbsOXvQQBH+EiM=; b=fbjW9H OaKAdw9kY2cS+414oI/DpOf5j4D+pPpj2EWgc4Cn7HE6Mmg0kt3V3JqRNRJfxXYt Aft1mATgzBsLRGKhaY8cxTAtJurtdJmCLG9onqiZvP6FUi44VeDKMbkuhq+1cfYo OcOIh5Vok8+EaQmuhp+fcJZGsHlkhl8XmxLaM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=1FmYTrnm9H2SNVb sOXvQQBH+EiM=; b=BAph/2VBxk82KWPPBmyhlZRehqKrvLTelB3sWUApO0dP5Q3 2kBsrfpy4cWhtvLKHHEiUzqYB6PlfjPSdMJMATUHmng03Dg/43yBHQ9u+YT9EnWq NXU0wLqRvKfcyH9t0Y/lAjy1qh25k4T8E4idupqepNF17V0yO7hM22U0obo4= X-Sasl-Enc: IVRL1eSLlv1aoFCAO4P35iPogw57XTrrTJ7PNQHUrP+r 1461291613 X-Mailer: MessagingEngine.com Webmail Interface - ajax-76f1c811 In-Reply-To: <57197FC3.4030701@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1461291613.433097.586174185.01BB7E37@webmail.messagingengine.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> Xref: csiph.com comp.lang.python:107472 On Thu, Apr 21, 2016, at 06:34 PM, Christopher Reimer wrote: > 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) This whole section is begging for a dictionary. Like... _PIECE_TYPES= {"Bishop": Bishop, "King": King, ...] class PieceFactory(object): def factory(color, piece, position): klass = __PIECE_TYPES.get(piece) if klass is None: raise PieceException("...") return klass(color, position) Or something like that. That said, I'm not sure why its not just a function that does the same thing. Why is it in a class that only does one thing? You never even instantiate it. > def generate_set(color, pieces, positions): > for piece, position in zip(pieces, positions): > yield getattr(PieceFactory, 'factory')(color, piece, position) Whyyy are you using getattr? Something wrong with PieceFactory.factory(color, piece, position)? (Or, better yet, yield piece_factory(color, piece, position) where piece_factory is just a function) > I got the factory method from here: > http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Factory.html I... that... what... I'd forget that link and pretend you never went there. Its not helpful. > Finally, VARS['VARIABLE_NAME'] got change to const['variable_name']. > Should smell better. I don't know that this changes anything about the small at all. What's the contents of this big dictionary that has everything in it for some reason? That said, dear god, 'piece' doesn't look like an english word to me anymore. I've never suffered semantic satiation from text before. --Stephen m e @ i x o k a i . i o