- 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
Admin
I can understand the manual generation of a new filename. It's not uncommon that the filenames from big datasets (e.g. medical exports) are just random and we would like to sort them in a human-readable way. Of course any sane script would not care about filenames, but for development, debugging and archival reorganizing the files comes quite handy, not to mention other "scientific" software that might care about filenames more than metadata, and you get lung sections intermixed with kidney sections. Of course it shouldn't, but...
The enumerate thing is a real WTF. Unfortunately I have the impression that CS is still taught in terms of C-like arrays and integer indexes, so that's what people end up doing in Python as well. See for an example the nice talk "Stop teaching C" from Kate Gregory at CppCon 2015, available on Youtube.
Admin
This line also annoys me:
Ignoring the missing
=
sign there (I assume that's just a typo), this code isn't otherwise wrong, but it's also not good. Older Python programmers might do this instead:And more modern Python programmers might do this:
Admin
Filename generation depends on situation - sometimes you MUST discard the old fname and make something like that, whether because the old fname contains sensitive info, is random and hard to use, comes from a system with a different number offset, or whatever, so it's not a WTF.
Admin
From TFA:
I think that you accidentally a word there :-P
Addendum 2024-09-11 10:57: I agree with @Allexxann - in the absence of further info about the naming spec/requirements for the image files, the generation of the new filename might be situational and required.
Admin
I suspect the missing word is "while":
Admin
To quote Jules Winnfield: "Correctamundo!"
Admin
Actually, this is some of the cleanest scientist-written code I've seen. If only I'd thought to save some of the examples instead of spending the subsequent trying to erase them from my memory...
Admin
Agree. Having worked with a number of scientists in the past there are often valid (if sometimes odd) reasons for renaming files, sometimes due to restrictions or oddities in the programs that consume the files later on
Admin
Frequently in my career doing presales tech support for scientific visualization software I encountered code created by a scientist & programmer pair. One time when the programmer was out of the room the scientist said “he’s an idiot — he doesn’t know anything about the science — he doesn’t understand what this code does.” Then when the other was gone I heard “he’s an idiot — he doesn’t know coding — he doesn’t understand what this code does.”
Admin
I think @Allexxann and @The Beast in Black missed the point: they discard 'fname' and immediately load in 'data' with the same value 'fname' had ('img_list[index]').
At least I believe that's what Remy was pointing at.
Admin
This is why we need a guild, certification and licensing. That way if anyone ever wants to write even a line of code they would have to hire a certified software engineer. Not only would we avoid bad code but we would get full employment, high wages, quality control, full employment, collective bargaining, quality control, the power to rule the world and maybe even some quality.
Admin
wow. this is the first time I have seen someone wrongly use "e.g." instead of "i.e."
Admin
One thing that stood out to me here is the line
I'm going to assume,
data[0]
is annumpy.ndarray
. The[:,:]
indexing creates a newndarray
object, but it is backed by the same actual memory. So effectively the only difference betweendata[0]
anddata[0][:,:]
is that the latter raises an exception, ifdata[0]
isn't an
ndarray` with at least rank 2. (Curiously, it is valid for a rank 3 array.)Admin
Remy pointed at two different things - the wasteful (re)loading(?) and the renaming. I don't speak python, so I don't know how the loading part works, but me and @The Beast in Black are defending the second part.
Admin
Exactly what @Allexxann said above: we're both referring to line 5 of the snippet in TFA, the one which sets
img_title
to be'img'+str(index).zfill(4)+'.jpg'
(albeit with all the other issues pointed out in TFA and herein), not lines 2 and 3.TFA says:
What @Allexxann and I are trying to say is that it may be that the original filename is unsuitable for whatever reason, and that the requirement may be that the final filename be the one generated in line 5 especially if the original filename is different; we have not been given sufficient data to determine that the original filename is indeed a "good" filename which can be used directly.
Admin
Taking out the truly redundant parts:
The weird slicing? Well, it's on a Numpy array so it might do something somewhat unexpected when it interacts with
imwrite
. I'd not be at all keen to alter that without some test cases set up first.