Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Date: Sun, 31 Jan 2016 20:40:48 +1100 Lines: 35 Message-ID: References: <56add21a$0$1593$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de nvjSFhEApOlNcf+vWlZ/GgMMUB6N9wTDIVd9svu+DVqg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'json': 0.05; 'semantic': 0.07; 'cc:addr:python-list': 0.09; 'lines:': 0.09; 'substitution': 0.09; 'python': 0.10; 'jan': 0.11; 'subject: \n ': 0.15; '2016': 0.16; 'accesses': 0.16; 'chained': 0.16; 'dog': 0.16; 'dot,': 0.16; 'dots': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subscripting': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'nested': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '31,': 0.22; 'parsing': 0.22; 'trying': 0.22; '(or': 0.23; 'skip:l 40': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'depth': 0.29; 'document,': 0.29; 'code': 0.30; 'generally': 0.32; 'subject:all': 0.32; 'problem': 0.33; 'call,': 0.33; "d'aprano": 0.33; 'loading': 0.33; 'steven': 0.33; 'case,': 0.34; 'structure': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'represent': 0.38; 'data': 0.39; 'easily': 0.39; 'software': 0.40; 'your': 0.60; 'relationship': 0.61; 'more': 0.63; 'series': 0.65; 'numerous': 0.66; 'levels': 0.70; 'obvious': 0.76; 'blob': 0.84; 'chrisa': 0.84; 'inherent': 0.84; 'legs,': 0.84; 'to:none': 0.91; '"one': 0.91; 'dog.': 0.91; 'imagine': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=mgvxjzAudNRJPPQnYn/TeYPu+xlxgE+/wvO61nkG2+Q=; b=hTMpj4dXczTXqute/PayhOIIzPrL9yYrCQukSRuGuyEWlVGDixp09C3vJb+U9qdzO6 9nPVhpQt787imNUf9sSX5IRMhMlIi+4eoFsSbAXD4Bnb1LEYf0TrtePcBRDR2iBCdtwz Nl4PxDjDLL33AUbx18aG18NWHSra6QCFi1LqesZvr53YfYAi8xtbmoikTRfFd7WoG0oZ vRFqSh+SZjuu4GBsdfJC1JlBdvmBp/Tk0I3sBuE2z102thkMEsa7g1Xk14OjTx5FVdIi 8hDKFsyIeiQB/FCkOtmnk3mqIfJufcVH/qFnnLBfWQL726Wb9K/VOcV9viXQYC28czv9 r6qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=mgvxjzAudNRJPPQnYn/TeYPu+xlxgE+/wvO61nkG2+Q=; b=hwVHg7dyJH3wN3tcFlbb4elMy9CPRpx8tmZlsFglt5BnQcaM+gra5dXl/9QSDREeC6 +zb5Ohd5cTVEAPraC/LpvhO3tzNDr1rSk/MZPnL/K+aO10ZEl3bwOV1DGcv9hs8ADtnA rDIhOOook6CFi0FRaNZ8VyrC/owHCDNeU4/cBpfXEVlVqtIqk7E34WS/krxfvNf9FxKO ZXVxdHA+ful8hLOMxaI2nQSctn1JylDUZplPNi/BuT1QfjiFr4QKaDLu7eaJT6UHgZex mDn0/JecWdftqgzAYqGs97WAaLp4bHSkeHH63dhRrjPwiJ7iU6DA7yfgZUuSkhr0qTCs CRmg== X-Gm-Message-State: AG10YORrvGRpe+NTIWbRMIfgzUl6paMzMwLQWE+2qaSN6szRW4QpOanUrRBfuiFEzgbLrQ3VgfY3OSB21a6utw== X-Received: by 10.107.47.162 with SMTP id v34mr16665771iov.19.1454233248926; Sun, 31 Jan 2016 01:40:48 -0800 (PST) In-Reply-To: <56add21a$0$1593$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102340 On Sun, Jan 31, 2016 at 8:21 PM, Steven D'Aprano wrote: > Hmmm. Well, I've never used lxml, but the first obvious problem I see is > that your lines: > > description = li_item.find_class('vip')[0].text_content() > > link = li_item.find_class('vip')[0].get('href') > > price_dollar = li_item.find_class('lvprice prc')[0].xpath('span')[0].text > > bids = li_item.find_class('lvformat')[0].xpath('span')[0].text > > > look suspiciously like a violation of the Liskov Substitution Principle. > ("Talk to your dog, not to the dog's legs!") A long series of chained dot > accesses (or equivalent getitem, call, getitem, dot, etc) is a code-smell > suggesting that you are trying to control your dog's individual legs, > instead of just calling the dog. (Isn't that the Law of Demeter, not LSP?) The principle of "one dot maximum" is fine when dots represent a form of ownership. The dog owns his legs; you own (or, have a relationship with) the dog. But in this case, the depth of subscripting is more about the inherent depth of the document, and it's more of a data thing than a code one. Imagine taking a large and complex JSON blob and loading it into a Python structure with nested lists and dicts - it wouldn't violate software design principles to call up info["records"][3]["name"], even though that's three indirections in a row. Parsing HTML is even worse, as there's generally going to be numerous levels of structure that have no semantic meaning (they're there for layout) - so instead of three levels, you might easily have a dozen. ChrisA