将功能分解为更易于管理的细分市场

时间:2013-05-07 09:35:28

标签: python python-3.x

我现在有一个程序,我正在尝试分解为更小,更易于管理的作品。但问题是,我在遵循指南集方面遇到了一些困难。这尤其是函数不能包含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')

任何帮助都会受到赞赏,干杯!

3 个答案:

答案 0 :(得分:1)

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')

也许是这些方面的东西。 显然这可能不起作用..有点乱,但我会继续编辑它,希望你能得到这个想法。

优点:

  • 没有文件句柄打开
  • 被叫顺序的功能
  • 删除了过多的代码

答案 1 :(得分:1)

这个怎么样:

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')

这听起来非常接近你想要的。 希望它有所帮助。

答案 2 :(得分:0)

此操作还说明了如何使用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')