jeudi 17 septembre 2020

checking for a variable multiple times

I am writing some analysis scripts to in which there are a lot of conditions I need to satisfy to implement the code correctly. The code looks very messy because I have to keep checking for certain things. Here is a simplified version of my problem.

I have a variable name 'measurement_type'.

measurement_type can be 3 values, "transmission", "reflection", "both". I need to perform 2 different set of instructions depending on whether measurement_type is in ("transmission" or "reflection") or measurement_type == "both"

I going to simplify my problem here because I don't think that the actual code really matters.

If measurement_type in ("transmission","reflection") I only have to run this:

table = pd.DataFrame(stuff)
arr = []
for num in table:
    arr.append(num)
plt.plot(stuff)

Since I have to account for measurement_type == 'both', I have to run this to check for that as well:

table1 = pd.DataFrame(stuff)
arr1 = []
if measurement_type == 'both': 
    table2 =  pd.DataFrame(stuff) #always same dimensions as table1
    arr2 = []
for j in range(len(table)):
    arr1.append(table['data'][j])
    if measurement_type == 'both':
        arr2.append(table2['data'][j]
plt.plot(stuff)
if measurement_type == 'both':
    plt.plot(more stuff)

I am having to run the latter because I need to take care of the possibility of measurement_type = 'both' This is getting out of hand because I am having to perform this check of whether or not "both" exists.

Is there a better way of doing this? I would rather not have to type in if measurement_type == 'both' over and over again as my analysis scripts get longer and more complicated.

Aucun commentaire:

Enregistrer un commentaire