Live data from Hacker News

How to Write the Worst Possible Python Code (Humor)

effective-programmer.com

21–30 of 31 posts

Re: How to Write the Worst Possible Python Code (Humor)

#21

I once was in the position of becoming pr approver[1] for a team of outsourced python programmers who were under some pretty extreme deadline pressure. Anyway one day a PR comes in and I can’t help but notice it was doing a string eval. Weird. You almost never need to do string eval in python, and whenever there is something where you think you need eval there is a better and safer way to achieve the same result. Als…

I saw plenty of similar things from outsourced teams.

For instance: An e-commerce API that used JSON. Not only did the spec. tell them to use integers with pence for prices, but it explicitly called out that it MUST NOT use floating points with pounds. Sure enough, they implemented it as floating point pounds. So we asked them to fix this.

The underlying datatype in the database was pounds in a decimal type. You would think that they would multiply this by 100 and call it a day. What they actually did was: a) render it as a string, b) strip the period character, and c) parse the resulting string as an integer. They didn’t test this properly before deploying, which resulted in us charging the correct price for things that cost £xxx.x5 but undercharging by a factor of ten for things that cost £xxx.x0 and undercharging by a factor of a hundred for things that cost £xxx.00.

Re: How to Write the Worst Possible Python Code (Humor)

#22
post #11

I once was in the position of becoming pr approver[1] for a team of outsourced python programmers who were under some pretty extreme deadline pressure. Anyway one day a PR comes in and I can’t help but notice it was doing a string eval. Weird. You almost never need to do string eval in python, and whenever there is something where you think you need eval there is a better and safer way to achieve the same result. Als…

I came across something quite similar. Using eval to convert a string to a dict. The string potentially included user input.

I wonder whether my dudes cut and pasted from the same cursed stack overflow snippet as your dudes had.

The strings here included user input too. Worse still, the situation was the company was offering a b2b service and the string didn’t just come from user input by an employee of the company they came from arbitrary customers of the customers of the company.

Re: How to Write the Worst Possible Python Code (Humor)

#23
post #3

Disappointed that my favorite pythonic pattern of "here's a dict with unknown contents, do the right thing with it" is not listed.

What do you mean, "unknown contents"?

Just trace back the dict to it's origin, through all the places it can be modified, and you know what is (or could be) inside...

/s

Re: How to Write the Worst Possible Python Code (Humor)

#24
post #13

> Rule #13: AI-Assisted Coding is for the Weak Me reading this article: yup, yup, yup, yup, wait a minute! This part feels a bit snuck in, in a "leading and pacing" kind of way: All the other points are long-established no brainers, but this one is still controversially discussed and I'd say - in the general form it's presented here - wrong. The author is still right that it's wrong to categorically dismiss AI tools…

Jokes on you, the article is ai written

busted

Re: How to Write the Worst Possible Python Code (Humor)

#26
post #3

Disappointed that my favorite pythonic pattern of "here's a dict with unknown contents, do the right thing with it" is not listed.

Hey now, at least the dictionary has keys that _could_ hint at the contents (or be completely misleading). What about the tuple with just positions? image.size ndarray.shape Are the image sizes (width, height) or (height, width)? Trick question of course, it's (height, width, channels) for numpy. numpy is fairly well known though and sort of gets away with it, but when your never-seen-before internal company starts d…

Serious question, but what is the best practice for keeping n-dimensional arrays organized/labeled? Pandas? Xarray? Converting everything to netcdf before using it?

Re: How to Write the Worst Possible Python Code (Humor)

#29
post #11

Earlier quoted context omitted.

I came across something quite similar. Using eval to convert a string to a dict. The string potentially included user input.

I wonder whether my dudes cut and pasted from the same cursed stack overflow snippet as your dudes had. The strings here included user input too. Worse still, the situation was the company was offering a b2b service and the string didn’t just come from user input by an employee of the company they came from arbitrary customers of the customers of the company.

In my case the data was visible in the URL - they had chosen to not store use session specific data in the DB or cookie or anything sane like that, but to pass it to the page in the URL path by converting a dict to a string/

Git blame shows the same thing done in two different places and the line edited by at least two different people.

Re: How to Write the Worst Possible Python Code (Humor)

#30
post #3

Disappointed that my favorite pythonic pattern of "here's a dict with unknown contents, do the right thing with it" is not listed.

Seen (and fixed): A function returns some “struct” (as a dict), with a couple of fields, mostly stringly typed, and one field for accumulating error messages called “errors”. The dict was created as a defaultdict(list) (probably) to make it easier to append messages.
Post reply on HN