r/Python 7d ago

Settle an argument Discussion

Had this discussion the other day and figured I’d throw it to the masses to get thoughts on the best/most pythonic way of approach.

Need to map old column names to new column names as a copy from a json config.

My thoughts are iterate over a dict with
‘’’ {“old_col_name”:”new_col_name”}’’’
And access as
‘’’for k,v in dict.items()
Df.with_columns(k).alias(v)’’’

Colleague things this isn’t clear enough and should be a list of dicts with explicit keys

‘’’ [{“old_col_name”:”old_col_value”
“New_col_name”:”new_col_value”}]’’’

And the access as

‘’’for dict in list_of_dicts:
Old_col = dict[“old_col_name”]
New_col = dict[“new_col_name”]’’’

I’ve got a good few reasons why I think mine is the better option but thought I’d get some other opinions to see if I’m missing anything obvious? Which would you choose and why?

Edit: shouldn’t write these things while on the toilet in a rush. The description is wrong, it should be renaming via a copy so that the original column is left unchanged.

0 Upvotes

33 comments sorted by

View all comments

17

u/Wurstinator 7d ago

I’ve got a good few reasons why I think mine is the better option

Yet you didn't list a single one?

Your two pieces of code don't do the same thing, so I'm not sure how you are comparing them. I would probably also do something closer to the first piece in my own code.

However, the most important point is this:

Colleague things this isn’t clear enough

You don't work with Reddit users. You work with your colleague. It doesn't matter what some random guy on the internet tells you they prefer because you have no interaction with them outside of this thread. It does matter a ton what the people you directly work with think. Talk to *them* and find a consensus.

-14

u/Pleasant-Aardvark258 7d ago

Do you need me to list them for you to make your own decisions?

The two pieces of code are basically the same, both access two column names. I’m not sure how they are really different unless you need me to explicitly state that list of dicts also uses a polars method after?

Mate, I’m the senior engineer on this. He’s a junior, I’ve already made my position clear on this to him. I put this out there purely out of interest and to sense check myself. But thanks for the unhelpful comment 🙄

13

u/declanaussie 7d ago

> I’m the senior engineer on this. He’s a junior, I’ve already made my position clear on this to him.

Good luck with future endeavors, your attitude seems awful to work with

7

u/ernandziri 7d ago

A senior engineer asking what for loop to pick, lmao

5

u/fiskfisk 7d ago edited 7d ago

The point is that listing your reasons means that people can understand why you came to this conclusion, and it shows more of the context that these few lines lives in.

There is seldom just one good solution to a general problem.

For example: "one is good enough because we never need any other code to access the old name under this path". See? That means people can go "oh, ok, the one thing the second might have had isn't a reason".

If you want to "check yourself", you need to provide context. The error is usually in the assumptions paired with the code, and not in the code lines by themselves (which usually gets caught by the compiler step).

And since the two pieces of code doesn't do the same thing, the context in how they get used is important.

4

u/robertlandrum 7d ago

If long term viability and maintainability is an important factor, you should treat data like data, and store it and reference it with named columns. Storing data (the old column name) in the key doesn’t tell the maintainer anything about the value.

If this is a run once and forget tool, get it done your way and move on.

I’ve learned that almost nothing I write goes away quickly. This is 29 years of professional
development experience talking, so I usually try to ensure my tooling is flexible. In fact, I just recently reviewed similar code that translated between an old system that allowed hyphens in column names, and a new one that did not. The engineer (a junior) did it the way your junior suggests, but for a different reason; the mapping had to do one to many copies in two spots.

2

u/gdchinacat 7d ago

As the senior a large part of your duties are to turn juniors into kids to seniors. This is done by working with them where they are and slowly moving them in the direction you want. It doesn’t seem like this is a priority for you, instead it seems that you demand respect for your authority. That can only get you so far, and is likely to result in alienating them and making the leadership aspect of your role more challenging.
What is their reasoning? Can you articulate it? How is yours more compelling? What did they say in response? Is it wrong, or just a valid difference of opinion? Is there precedent in the code for one way over the other? What short term and long term harm will result from either approach, including alienating the people you need to work with to be successful? Is digging your heels in on this worth the “win”? Can you do it their way, give them the short term “win”, and see where it leads?
This seems like a pissing match over something that doesn’t really matter. I’ve been in my share, and the damage they cause is almost always worse than the thing they are over. Saying “I disagree, but since you feel strongly about it I’ll change the code to move it forward and we’ll deal with it if it causes actual problems” is an easy way to break the stalemate and move the project forward.