Python Forum
Unpacking dictionary from .xlsx as Kwargs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking dictionary from .xlsx as Kwargs
#1
Summary: I'm looking to make my code more Pythonic. It works, but I'm just uneasy knowing that there's probably a better solution elsewhere

I've tried creating a web automation script with Selenium, that allows users to choose which fields to change. The Excel file which the script reads from, is as follows

[Image: iTHUqB2.png]

Meaning first row, col_72708 will have 4 fields edited. Second row will have 2 fields edited, and the last row will have only 1 field edited.

I then read this file as multiple dictionaries. Key being the column header, and Value being the actual value (in the table).

I then unpack this dictionary into my function (with **), which has kwargs of default value 0. My function has multiple if statements, so if the kwarg passed in is blank, it skips the function that makes the amendment in the script.

This code is functionally sufficient, but I'm confident that there must be a better way to do this.

I hope I'm sufficiently clear to facilitate anyone giving me advice on this.

Many thanks!
Reply
#2
bump (am I allowed to do this?)
Reply
#3
bumping from time to time when reasonable amount of time has passed is OK
It's difficult to tell what can be improved without seeing any code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
As Raymond Hettinger uses to say: "ThereĀ“s always a better way".
I recommend watching his tutorials on Youtube.
Reply
#5
I agree that bumping is fine, but I want to point out that the same people that see your bump are the ones that overlooked your question already. Instead of just bumping, you should try to improve your question. As mentioned, providing your Python code is essential to finding what's not Pythonic about it - we could go back and forth in natural language about it, but to truly get us all on the same page, you should provide the code.
Reply
#6
Thank you for the clarification.

For the first part of my question, I'm having the below function. The kwargs have default value of 0, and the if clause is skipped if the user does not assign a value to the kwarg. Is this conceptually the way to do so?

def master_update(subproj_n, p_BaseSynergy=0, p_synergyproject=0, p_impact_date=0, p_new_name=0, p_totalValue=0):
    
    opening_subproject(subproj_n)
    
    if p_BaseSynergy:
        f_BaseSynergy(subproj_n, p_BaseSynergy)
    if p_synergyproject:
        f_synergyproject(subproj_n, p_synergyproject)
    if p_impact_date:
        f_impact_date(subproj_n, p_impact_date)
    if p_new_name:
        f_update_name(subproj_n, p_new_name)
    if p_totalValue:
        f_update_values(subproj_n, p_totalValue)
    
    closing_subproject()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 982 Dec-02-2023, 11:50 PM
Last Post: msrk
  How do I instantiate a class with **kwargs? palladium 6 7,313 Feb-16-2023, 06:10 PM
Last Post: deanhystad
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,293 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  Unpacking zip object Mark17 12 3,214 Mar-28-2022, 04:59 PM
Last Post: deanhystad
  kwargs question Jeff_t 8 2,955 Feb-16-2022, 04:33 PM
Last Post: Jeff_t
  unpacking list wardancer84 2 1,874 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,484 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,605 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 11,297 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Misunderstanding kwargs? Donovan 2 2,270 Aug-04-2020, 08:09 PM
Last Post: Donovan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020