Home > Software design >  Almost same parameters for different function python
Almost same parameters for different function python

Time:01-28

for example I have code as below:

if env = 'staging':
    db_cluster = rds.DatabaseCluster(self, 
                                     instances=2,
                                     identifier='ksdfjdsk', 
                                     region='string', 
                                     password='string',
                                     removal_policy='string'
                                     ... 
                                     ...
                                     iam='iam role name')

else:
    db_cluster = rds.DatabaseClusterFromSnapshot(self, 
                                                 instances=2,
                                                 snapshot_identifier="arn"
                                                 identifier='ksdfjdsk', 
                                                 region='string', 
                                                 password='string value',
                                                 removal_policy='stringsdfdd'
                                                 ... 
                                                 ...
                                                 iam='iam role name')

Here, DatabaseCluster and DatabaseClusterFromSnapshot are was-cdk APIs. Both take almost the same parameters and the only difference is snapshot_identifier extra parameter on second one.

Now the question is, Is there any method or logic so that I could reduce code blocks of if-else. (cause both have huge no.of parameters)

CodePudding user response:

One way you could do it is build a dictionary of the common arguments common_args like:

common_args = dict(
    instances=2,
    region="string",
    identifier="ksdfjdsk",
    password="string",
    removal_policy="string",
    iam="iam role name",
)

Then use dictionary unpacking and dict.update (|) to add any required extra arguments to the specific call:

if env == "staging":
    db_cluster = rds.DatabaseCluster(self, **common_args)
else:
    extra_args = dict(snapshot_identifier="arn")
    db_cluster = rds.DatabaseClusterFromSnapshot(self, **(common_args | extra_args))

CodePudding user response:

You can use dict-unpacking to solve the problem, and both make the code more easy to read, but also make it easier to change values.

Take this simple example:

class fake_api:
    
    def print_values(self, value1, value2):
        print(value1)
        print(value2)
    
    def prefix_print_values(self, value1, value2, prefix):
        print(f'{prefix}_{value1}')
        print(f'{prefix}_{value2}')
        

value_dict = {
    "value1": "Volvo",
    "value2": "Saab"
}

fake_object = fake_api()

fake_object.print_values(**value_dict)

value_dict["prefix"] = "car"
fake_object.prefix_print_values(**value_dict)

The output of which is:

Volvo
Saab
car_Volvo
car_Saab

Now you can just change the dictionary in your code when the if-statement is false, and run the other method.

  •  Tags:  
  • Related