1

I have three similarmethods. And I think this can be reduced to one method.

So I have this almost identical methods:

def calulate_totalAnanas_NorthMidSouth():
        
    sheet_factuur = excelWorkbook['Facturen ']
    totalAnanasNorth = sheet_factuur.cell(row=6, column=5).value
    totalAnanasMid = sheet_factuur.cell(row=7, column=5).value
    totalAnanasSouth = sheet_factuur.cell(row=8, column=5).value    
    totalAnanasNoMidSou = totalAnanasNorth + totalAnanasMid + totalAnanasSouth   
    
       
    print(totalAnanasNoMidSou)
    
def calulate_totalApples_NorthMidSouth():
        
    sheet_factuur = excelWorkbook['Facturen ']
    totalAppleNorth = sheet_factuur.cell(row=9, column=5).value
    totalApplesMid = sheet_factuur.cell(row=10, column=5).value
    totalAppleSouth = sheet_factuur.cell(row=11, column=5).value    
    totalAppleNoMidSou = totalAppleNorth + totalApplesMid + totalAppleSouth   
    
      
    print(totalAppleNoMidSou)
    
def calulate_totalWaspeen_NorthMidSouth():
        
    sheet_factuur = excelWorkbook['Facturen ']
    totalWaspeenNorth = sheet_factuur.cell(row=12, column=5).value
    totalWaspeenMid = sheet_factuur.cell(row=13, column=5).value
    totalWaspeenSouth = sheet_factuur.cell(row=14, column=5).value    
    totalWaspeenNoMidSou = totalWaspeenNorth + totalWaspeenMid + totalWaspeenSouth   
    
      
    print(totalWaspeenNoMidSou)

So my question is:how to refactor this?

4
  • You need to work out what the differences are between these functions. And by that I mean, not the variable names you use, but the data you use; ie numbers and strings. Commented Sep 7, 2022 at 11:21
  • 1
    It is a better code style to return solutions and not to print them. If you later want to print them you can still do it by printing the function call. Commented Sep 7, 2022 at 11:22
  • The differeneces are the names and ofcourse the row numbers in the excel sheet. So for every product: apple, waspeen adn ananas. Three numers are added Commented Sep 7, 2022 at 11:23
  • the variable names are internal to the function though, so are not relevant - the only difference between the functions currently is the row numbers Commented Sep 7, 2022 at 11:26

5 Answers 5

2

try this code. Note that i have not tested it but idea is clear:

def calulate_total_fruit_NorthMidSouth(fruit_name: str) -> int:

    sheet_factuur = excelWorkbook['Facturen ']

    fruit_name_rows = {
        'ananas': [6, 7, 8],
        'apple': [9, 10, 11],
        'waspeen': [12, 13, 14],
    }

    total_fruit_counts = [sheet_factuur.cell(
        row=row_num, column=5).value for row_num in fruit_name_rows.get(fruit_name)]

    return sum(total_fruit_counts)


print(calulate_total_fruit_NorthMidSouth('ananas'))
print(calulate_total_fruit_NorthMidSouth('apple'))
print(calulate_total_fruit_NorthMidSouth('waspeen'))

Note!: you should pass rows as parameters to eliminate hardcoding.

Sign up to request clarification or add additional context in comments.

Comments

1

So, it looks like you just need three variables for the rows. Also there is nothing special about these rows, so they might as well be a collection:

def calulate_total_NorthMidSouth(rows):
        
    sheet_factuur = excelWorkbook['Facturen ']
    total = 0
    for row in rows:
        total += sheet_factuur.cell(row=row, column=5).value
    return total

def calulate_totalAnanas_NorthMidSouth():
    totalAnanasNoMidSou = calulate_total_NorthMidSouth((6,7,8))       
       
    print(totalAnanasNoMidSou)

Comments

1

First I'd parametrise the row numbers so they actually make some sense. Also you can use list comprehension and other fun methods.

def calulate_total_NorthMidSouth(rows):
    return sum([excelWorkbook['Facturen '].cell(row=row, column=5).value for row in rows]) 
     
Ananas = [6,7,8]
Apple = [9,10,11]
Waspeen = [12,13,14]
products = [Ananas, Apple, Waspeen]

for product in products: print(calulate_total_NorthMidSouth(product))

Comments

1

Best to use a control dictionary then iterate over its keys/values. Something like this:

control = {'Ananas': [6, 7, 8], 'Apples': [9, 10, 11], 'Waspeen': [12, 13, 14]}

sheet = excelWorkbook['Facturen '] # only do this once

def calculate(sheet, rows):
    return sum(sheet.cell(row=row, column=5).value for row in rows)

for k, rows in control.items():
    print(k, calculate(sheet, rows))

Comments

0

I don't know how is your excel data. But I think these two ways work for you. If you want to do all the job at once:

def calulate_total_NorthMidSouth():
        
    sheet_factuur = excelWorkbook['Facturen ']

    totalAnanasNorth = sheet_factuur.cell(row=6, column=5).value
    totalAnanasMid = sheet_factuur.cell(row=7, column=5).value
    totalAnanasSouth = sheet_factuur.cell(row=8, column=5).value    
    totalAnanasNoMidSou = totalAnanasNorth + totalAnanasMid + totalAnanasSouth   
     
    totalAppleNorth = sheet_factuur.cell(row=9, column=5).value
    totalApplesMid = sheet_factuur.cell(row=10, column=5).value
    totalAppleSouth = sheet_factuur.cell(row=11, column=5).value    
    totalAppleNoMidSou = totalAppleNorth + totalApplesMid + totalAppleSouth   

    totalWaspeenNorth = sheet_factuur.cell(row=12, column=5).value
    totalWaspeenMid = sheet_factuur.cell(row=13, column=5).value
    totalWaspeenSouth = sheet_factuur.cell(row=14, column=5).value    
    totalWaspeenNoMidSou = totalWaspeenNorth + totalWaspeenMid + totalWaspeenSouth  

    total = {'totalAnanasNoMidSou': totalAnanasNoMidSou , 'totalAppleNoMidSou': totalAppleNoMidSou, 'totalWaspeenNoMidSou': totalWaspeenNoMidSou }

If you want to do it in parts:

def calulate_total_NorthMidSouth(rows):
        
    sheet_factuur = excelWorkbook['Facturen ']

    totalNorth = sheet_factuur.cell(row=rows[0], column=5).value
    totalMid = sheet_factuur.cell(row=rows[1], column=5).value
    totalSouth = sheet_factuur.cell(row=rows[2], column=5).value    
    totalNoMidSou = totalNorth + totalMid + totalSouth 

By the way, I think you can construct your excel in a better way and read the excel with pandas (faster, easier). For example, put mid, south, and north in rows and fruits in columns. In that case, you can calculate your goal super faster and more pythonic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.