将函数分解为更易于管理的部分



我现在有一个程序,我正试图将其分解成更小、更易于管理的部分,用于锻炼目的。但问题是,我在遵循指导方针方面遇到了一些困难。值得注意的是,函数不能包含6个以上的语句。

以下是程序最初的样子。。

def print_monthly_totals (input_csv_filename):
'''Read the given CSV file and print the totals for each month.
   The input file is assumed to have the month number in column 1,
   the number of days in the month in column 2 and the floating
   point rainfalls (in mm) for each month in the remaining columns
   of the row. 
'''
    data = open(input_csv_filename).readlines()
    print('Rainfall totals for each month')
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])
        total_rainfall = 0
        for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)
        print('Month {:2}: {:.1f}'.format(month, total_rainfall))
print_monthly_totals('rainfalls2011.csv')

我设法把它分解成两个独立的函数,给了我。。

def print_monthly_totals(input_csv_filename):
    data = open(input_csv_filename).readlines()
    print('Rainfall totals for each month')
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])     
        trf = rainfall_total(columns, num_days)  
        print('Month {:2}: {:.1f}'.format(month, trf))  
def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall
print_monthly_totals('rainfalls2011.csv')

但是我的print_monthly_totals函数仍然太大,我需要创建第三个函数来满足每个函数6条语句的规则。我的尝试刚刚导致了一系列全局名称未定义错误。。

def print_monthly_totals(input_csv_filename):
    data = open(input_csv_filename).readlines()      
    print('Rainfall totals for each month')
    trf = rainfall_total(columns, num_days)        
    print('Month {:2}: {:.1f}'.format(month, trf))      
def data():
    for line in data:
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])         
def rainfall_total(columns, num_days):
    '''Guts of the program'''
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall
print_monthly_totals('rainfalls2011.csv')

任何帮助都将不胜感激,干杯!

这个怎么样:

def print_monthly_totals(input_csv_filename):
    with open(input_csv_filename) as csv_file:
        data = csv_file.readlines()
        print('Rainfall totals for each month')
        for line in data:
            manage_line(line)

def manage_line(line):
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])     
        trf = rainfall_total(columns, num_days)  
        print('Month {:2}: {:.1f}'.format(month, trf))  
def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall
print_monthly_totals('rainfalls2011.csv')

这听起来很接近你想要的。希望能有所帮助。

def parse_line(data):
    columns = data.split(',')
    month = int(columns[0])
    num_days = int(columns[1])
    return month, num_days
def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)      
    return total_rainfall
def print_monthly_totals(input_csv_filename):
    with open(input_csv_filename) as fh:
        for line in fh:
            month, days = parse_line(line)
            print('Rainfall totals for each month')
            trf = rainfall_total(columns, num_days)        
            print('Month {:2}: {:.1f}'.format(month, trf))      
print_monthly_totals('rainfalls2011.csv')

也许是类似的东西。显然,这可能不起作用。。有点乱,但我会继续编辑它,希望你能明白。

优点:

  • 没有打开的文件句柄
  • 按调用顺序的函数
  • 删除了多余的代码

这个镜头还展示了如何使用csv模块:

def print_monthly_totals (input_csv_filename):
    '''
    Read the given CSV file and print the totals for each month.
    The input file is assumed to have the month number in column 1,
    the number of days in the month in column 2 and the floating
    point rainfalls (in mm) for each month in the remaining columns
    of the row. 
    '''    
    import csv
    print('Rainfall totals for each month')
    with open(input_csv_filename) as csv_file:
        for row in csv.reader(csv_file):
            print_row(row)
def print_row(columns):
    month = int(columns[0])
    num_days = int(columns[1])
    total_rainfall = sum(float(val) for val in columns[2:2+num_days])
    print('Month {:2}: {:.1f}'.format(month, total_rainfall))
print_monthly_totals('rainfalls2011.csv')

最新更新