Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102340
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up |
| Date | 2016-01-31 20:40 +1100 |
| Message-ID | <mailman.153.1454233251.2338.python-list@python.org> (permalink) |
| References | <n8k448$3pd$1@dont-email.me> <mailman.152.1454217800.2338.python-list@python.org> <n8k9f4$gjf$1@dont-email.me> <56add21a$0$1593$c3e8da3$5496439d@news.astraweb.com> |
On Sun, Jan 31, 2016 at 8:21 PM, Steven D'Aprano <steve@pearwood.info> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up "Veek. M" <vek.m1234@gmail.com> - 2016-01-31 10:28 +0530
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Chris Angelico <rosuav@gmail.com> - 2016-01-31 16:23 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up "Veek. M" <vek.m1234@gmail.com> - 2016-01-31 11:59 +0530
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up "Veek. M" <vek.m1234@gmail.com> - 2016-01-31 12:01 +0530
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Steven D'Aprano <steve@pearwood.info> - 2016-01-31 20:21 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Chris Angelico <rosuav@gmail.com> - 2016-01-31 20:40 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Steven D'Aprano <steve@pearwood.info> - 2016-01-31 21:14 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Chris Angelico <rosuav@gmail.com> - 2016-02-01 00:27 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Peter Otten <__peter__@web.de> - 2016-01-31 11:40 +0100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Larry Hudson <orgnut@yahoo.com> - 2016-01-31 13:27 -0800
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Steven D'Aprano <steve@pearwood.info> - 2016-01-31 18:22 +1100
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up "Veek. M" <vek.m1234@gmail.com> - 2016-01-31 20:55 +0530
Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Vincent Davis <vincent@vincentdavis.net> - 2016-02-01 11:40 -0700
csiph-web