- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It probably behaves differently if the file system supports cased files (so ABC.details =/= abc.details and so on).
Admin
Assuming Path.glob does what I expect (because why would I read the docs when I can assume?), this code could raise the InternalError if it's running on a filesystem that allows literal wildcard characters in filenames. If the directory contained files named
helper-for-game.details
andhelp*me.details
, then globbinghelp*me.details
would return both files.Admin
The other point is that it may well be returning the data that corresponds to an identifier that is part of the content of the file rather than part of the name of the file. That doesn't excuse the arse-about-tit way the code goes about scanning the files, but could explain why it scans all of them rather than just the one named by the
identifier
parameter.Admin
That path is the Road of White Nettles. Follow it and horrors will reveal to yourself.
http://thecodelesscode.com/case/33
Admin
Weird code that works despite how it was coded kind of reminds me of RL agents that find some glitch or non-intuitive feature to win the game.
Admin
Is
base_path.glob
a correct recounting of the code?base_path
is a string, not a Path, there's noglob
method. It should beglob.glob(main_file)
.I guess we're globbing because the function allows you to provide a wildcarded filename. It will process the match, but only if it's unique.
Admin
The identifier in the content is the same as the file name. By definition, because the code that creates these files, written by the same author, sets the filename based on the identifier in the contents.
Admin
wait, there's more! if I remember correctly,
dict.update(another_dict)
basically "merges" the two dict, adding or overriding the keys/values of dict with the ones from another_dict (yes, I checked - https://docs.python.org/3/library/stdtypes.html#dict.update). So - ideally and if I read the whole shenanigans right - this will return the identifier value contained in the last file read that had that identifier inside! Of course the order of file read is not guaranteed by listdir, so yay for deterministic results!Admin
Yes, sorry, this is a sanitisation error on my part - base_path has been coerced to a Path object by some of the logic I elided, so the method is present and this call works, it's just absurd.
Admin
That Road of White Nettles story was excellent. It makes a great parable even for non-developers to understand the issues of poor design.