Skip to content
Advertisement

How to pass 1 argument each from 2 functions into 1 function?

I’ve tried to understand how and why I haven’t seen any answers to this specific question.

I have two functions each has an argument path

def check_sbb_name(path):
    if path is None:
        DeltaZero_Selenium_Manager.test_sbb_name_textfield(path)
        return path
    else:
        call = sbb_manager_sql()
        name = call.get_new_sbb_name()
        DeltaZero_Selenium_Manager.test_sbb_name_textfield(name)
        
        create_new_sbb("", name)
def check_sites_for_sbb(path):
    if path is None:
        DeltaZero_Selenium_Manager.test_site_combo_options_alternative_overview(path)
    else:
        call = sbb_manager_sql()
        site = call.connect_site()
        DeltaZero_Selenium_Manager.test_site_combo_options_alternative_overview(site)
        
        create_new_sbb(site, "")

Just ignore everything these functions actually do, cause everything works just perfectly until I want to pass on name & site to the following create_new_sbb function

def create_new_sbb(site_name, sbb_name):
    mycursor.execute("INSERT INTO sbb_data (site, name, is_deleted) VALUES (%s, %s, %s)",
                         (site_name, sbb_name, 0))
    db.commit()

So I want to pass on these arguments to create_new_sbb at the same time, instead of calling create_new_sbb on each function

Note that create_new_sbb is located in another file

The results in MySql DB:

enter image description here

I want site and name on the same row obviously
So only execute create_new_sbb when both arguments have been passed somehow.

Any help is very appreciated!!!

Advertisement

Answer

I will assume here that you always call check_sbb_name and check_sites_for_sbb in pairs with the same path. I would use:

cur_path = None
cur_name = None
cur_site = None

def check_sbb_name(path):
    ...    
    register_name(path, name)      # only register

def check_sites_for_sbb(path):
    ...
    register_site(path, site)      # only register

def register_name(path, name):
    if (path == cur_path) and (cur_site is not None): # other element already registered
        create_new_sbb(cur_site, name)                # ok, we can insert
    else:
        cur_path = path                               # register for this path
        cur_site = None
    cur_name = name

def register_site(path, site):
    if (path == cur_path) and (cur_name is not None):
        create_new_sbb(site, cur_name)
    else:
        cur_path = path
        cur_name = None
    cur_site = site

The important part is to register elements and only write to the database when you have a pair. Alternative methods (using same principle) could be used if for example you always call check_sbb_name first, then register_name would always register cur_name (no need for path) and register_site would always call create_new_bb. Or…

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement