Replies: 4 comments
-
|
Hi Adrian. Before I get going, let me begin by saying that I really appreciate the time you are putting into all of this, and for sharing your code with me (and the world). There is an old joke which goes something like this: "You have a problem, and you've decided to fix it with a regular expression... You now have two problems!" 😉 To be clear... as an old Perl coder, I love regular expressions. But they do not make much of an appearance in Head First Python as the goal of my book is to teach Python, not to teach regular expressions (although I can appreciate what you are doing here with yours). The goal of the web scraping code in the book is to demonstrate what's possible at the expense of producing code with a level of robustness which rivals that as written by a professional coder. With any webpage (but especially with Wikipedia), the content and structure of the target webpage can change often and sometimes quite dramatically. Rather than try to cover every eventuality, the goal was always to get the concept across with code which should work most of the time. Obviously, if those index values change due to Wikipedia edits, my code breaks, but it's a simple fix to resolve the issue and move on: simply change the index values. I would think your code breaks just as quickly if someone messes with the headings being looked for. I'd also question whether the complexity you've added to the code is worth the perceived robustness that is provided (when a simple edit to the index values was all that's needed here for the original code to work again). Pages 372 through 378 in Chapter 8 somewhat discuss this issue by concentrating on the size of the tables on the page (rather than a table's relative position to anything else). My goal is always to keep things as simple as possible with an eye on future maintenance of my code. As I said, I can appreciate what you are doing, but I'd caution that the added complexity of your code may not be worth it. I'd love to hear your thoughts on this. Regards. --Paul. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Paul, Thanks for the thoughtful reply, and for the context on what you’re optimising for in the book. The “two problems” regex joke definitely landed 😎 I agree with the core goal you stated: teach the concept, keep the code approachable, and accept that Wikipedia is a moving target. I also agree that “change the index values and move on” is often the right call for a learner who just wants to keep progressing. Where I was coming from is slightly different, and it’s mostly about the learner experience when it breaks.
On headings vs indices, I think you’re right in principle: both are brittle. I don’t think headings are magically stable. My (practical) bet was just that Wikipedia editors are more likely to reorder or insert tables than they are to rename “Long course” / “Short course” or swap the Men/Women section headings. If headings change, my approach breaks too, but it should break “loudly” in a way that points at what to update. On complexity, I hear you. Dropping regex into Head First Python is a lot, and it’s not aligned with the teaching objective. I wasn’t suggesting it needs to go into the book verbatim. I mainly wanted to (a) document the root cause for anyone hitting the same issue, and (b) offer an alternative for people who want a slightly more resilient version, even if it sits outside the main learning path. If it helps, there’s probably a middle ground that stays “Head First simple” and avoids regex entirely:
That would preserve the teaching intent and still reduce the “mysterious missing women’s records” problem. Happy to align with whatever approach you prefer in the repo. If you want, I can propose a minimal “defensive check” patch that avoids regex and keeps the narrative of the chapter intact, while making the failure case easier for readers to diagnose. Regards, Adrian |
Beta Was this translation helpful? Give feedback.
-
|
We are on the same page here. 😎 I'm planning to tweak some of the code this afternoon, then push the updates to GitHUB. I'll come up with a defensive check to warn when the indices need to change. Thanks, again, for your work here and your considered discussion. It is all very much appreciated. --Paul. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Paul, Sounds good, and that approach makes a lot of sense for the book. Regards, Adrian |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Paul,
Based on my previous post #17, I updated my version of update_records.py (Chapter 9) by:
This fixed the “missing women’s world records” problem for me and made the script more robust.
See the commented refactored update_records.py below:
Validation
Hope you find useful.
Regards,
Adrian
Beta Was this translation helpful? Give feedback.
All reactions