Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'elif': 0.05; 'subject:Python': 0.06; 'binary': 0.07; 'nested': 0.07; 'lookup': 0.09; 'wrapper': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '70,': 0.16; 'abbreviated': 0.16; 'bisect': 0.16; 'dct': 0.16; 'dictionaries': 0.16; 'res': 0.16; 'search:': 0.16; 'subject:user': 0.16; 'tuple': 0.16; 'weapon': 0.16; 'index': 0.16; 'ignore': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'starts': 0.20; 'import': 0.22; 'handles': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'entries': 0.24; 'instance,': 0.24; 'precise': 0.24; 'string,': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'array': 0.29; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'second,': 0.31; 'class': 0.32; 'lists': 0.32; 'table': 0.34; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'subject:New': 0.37; 'so,': 0.37; 'easily': 0.37; 'nov': 0.38; 'handle': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'ability': 0.39; 'subject: / ': 0.60; 'most': 0.60; 'full': 0.61; 'first': 0.61; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'here': 0.66; 'skip:m 50': 0.68; 'statement,': 0.68; 'obvious': 0.74; 'special': 0.74; 'day': 0.76; '100': 0.79; 'data-driven': 0.84; "it'd": 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=+MlGprPaB6aczOQBa/awmIA3GqmmMuXas/5BPoyNbcc=; b=atxk+fZevy4w6OPbAMVPmJ4+FFo0An8MUK7FHLPDuQDS+OKiqtlql5/tyHSsQtERq1 HfnNlRMRlariNrlybcZNosUMDThNw4y4owJPqlnZOFBSkqwUiVM+KzN1erA1nagMbQeL TSsX2czKEmkyQCx8PE/5YBALGcx9STwUOQTAl8XOd36OVuiqAoGwz0MEWozsrb/GWJwH Abis+KAJH3h1lJJ35MIb3sIaNj35OtqmurVzqTiwhPVzeEX0gDgOCL0UFVPvX2huHm/J bC+1Dqs1Wdz6VFu1g4vnd00pUQuJrdJoSfho2vSFm0f5nhDz1bvOtpHpHH53NolrkaaH S+4A== X-Received: by 10.152.216.167 with SMTP id or7mr24339876lac.10.1384203075227; Mon, 11 Nov 2013 12:51:15 -0800 (PST) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: References: <-JadnUirYuhUruPPnZ2dnUVZ8rSdnZ2d@bt.com> <1c4c0901-f80a-42f3-9df5-7e7431353079@googlegroups.com> From: Joshua Landau Date: Mon, 11 Nov 2013 20:50:35 +0000 X-Google-Sender-Auth: ob5GGBoPhGRjIZOnrJrLTs2J13U Subject: Re: New user's initial thoughts / criticisms of Python To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384203083 news.xs4all.nl 15933 [2001:888:2000:d::a6]:48053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59100 On 11 November 2013 10:39, Chris Angelico wrote: > On Mon, Nov 11, 2013 at 9:09 PM, wrote: >> Regarding the "select" statement, I think the most "Pythonic" approach i= s using dictionaries rather than nested ifs. >> Supposing we want to decode abbreviated day names ("mon") to full names = ("Monday"): > > You can't [normally], for instance, build up a > dictionary that handles inequalities, but you can do that with elif. > [...] Consider the following logic: > > A 'minor weapon' is based on a roll of a 100-sided dice. If it's 01 to > 70, "+1 weapon: 2,000gp [weapon]"; if it's 71 to 85, "+2 weapon: > 8,000gp [weapon]"; if 86 to 90, "Specific weapon [minor specific > weapon]"; and if 91 to 100, "Special ability [minor special weapon] > and roll again". > > My code to handle that starts out with this array: > > "minor weapon":({ > 70,"+1 weapon: 2,000gp [weapon]", > 85,"+2 weapon: 8,000gp [weapon]", > 90,"Specific weapon [minor specific weapon]", > 100,"Special ability [minor special weapon] and roll again", > }), > > (that's Pike; in Python it'd be a list, or maybe a tuple of tuples), > and denormalizes it into a lookup table by creating 70 entries quoting > the first string, 15 quoting the second, 5, and 10, respectively. So, > with a bit of preprocessing, a lookup table (which in this case is an > array (list), but could just as easily be a dict) can be used to > handle inequalities. The obvious way to me is a binary search: from bisect import bisect_left class FloorMap: def __init__(self, dct): self.indexes =3D sorted(list(dct)) self.dct =3D dct def __getitem__(self, itm): index =3D self.indexes[bisect_left(self.indexes, itm)] return self.dct[index] minor_weapon =3D FloorMap({ 70: "+1 weapon: 2,000gp [weapon]", 85: "+2 weapon: 8,000gp [weapon]", 90: "Specific weapon [minor specific weapon]", 100: "Special ability [minor special weapon] and roll again" }) minor_weapon[63] #>>> '+1 weapon: 2,000gp [weapon]' The precise details of the wrapper class here are just to make initialisation pretty; it could be done straight from a pair of lists too: from bisect import bisect_left minor_weapon_indexes =3D 70, 85, 90, 100 minor_weapon_descriptions =3D ( "+1 weapon: 2,000gp [weapon]", "+2 weapon: 8,000gp [weapon]", "Specific weapon [minor specific weapon]", "Special ability [minor special weapon] and roll again" ) minor_weapon_descriptions[bisect_left(minor_weapon_indexes, 80)] #>>> '+2 weapon: 8,000gp [weapon]' Compare to if 80 <=3D 70: res =3D "+1 weapon: 2,000gp [weapon]" elif 80 <=3D 85: res =3D "+2 weapon: 8,000gp [weapon]" elif 80 <=3D 90: res =3D "Specific weapon [minor specific weapon]" elif 80 <=3D 100: res =3D "Special ability [minor special weapon] and roll again" which although shorter=C2=B9 is a lot less data-driven and much less reusab= le. =C2=B9 Longer if you ignore the import and class declaration.