Python,如何在for循环之外移动if语句而不重复循环

时间:2017-03-16 14:52:15

标签: python loops optimization

我可以用两种方式写我的函数。

def output_ip_hist(target, final, stats, table_name, bulk_qty, type = "sql"):
    if(type == "sql"):
        field_names = ",".join(get_field_names(final, table_name))
        count = 0
        stats[table_name] = 0
        values = []
        for comp_name, row in final.items():
            for ip_address, sub_row in row.items():
                for index, ip_hist in enumerate(sub_row):
                    hist_item = ip_hist.replace('"', "'")
                    values.append('("' + comp_name + '", "' + ip_address + '", ' + str(index) + ',"' + hist_item + '")')
                    count += 1
                    if(count == bulk_qty):
                        insert_sql_many(target, count, table_name, field_names, values, stats)
                        count = 0
                        values = []
        if(count != 0):
            insert_sql_many(target, count, table_name, field_names, values, stats)
    elif(type == "csv"):
        for comp_name, row in final.items():
            for ip_address, sub_row in row.items():
                for index, ip_hist in enumerate(sub_row):
                    insert_csv(target, { "computer_name": comp_name, "id": str(index), "ip_address": ip_address, "hist_item": ip_hist.replace('"', "'") }, stats, table_name)

这是第一种方式。这种方式的缺点是循环被写入两次,造成一些重复。

第二种方法是在循环中移动最外面的if语句,这样循环只完成一次,但缺点是if语句在每一个循环上执行,从而减慢循环循环到400万条记录。

我想知道是否有可能充分利用两个世界,减少重复并尽可能快地保持循环。

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以定义两个功能:process_sqlprocess_csv。根据{{​​1}},您可以将data_type设置为第一个函数或第二个函数。

在循环内部,您可以使用process_data

process_data

答案 1 :(得分:0)

您可以尝试编写生成器以从嵌套循环中生成值流。这也允许您使用itertools.islice来简化SQL批处理代码。

def my_generator(final):
    for comp_name, row in final.items():
        for ip_address, sub_row in row.items():
            for index, ip_hist in enumerate(sub_row):
               yield comp_name, ip_address, index, ip_hist.replace('"', "'")

def output_ip_hist(target, final, stats, table_name, bulk_qty, type = "sql"):
    items = my_generator(final)
    if type == "sql":
        field_names = ",".join(get_field_names(final, table_name))
        stats[table_name] = 0
        while True:
            values = ['("%s", "%s", "%s", "%s")' % i for i in islice(items, bulk_qty)]
            if not values:
                break                
            insert_sql_many(target, len(values), table_name, field_names, values, stats)
    elif type == "csv":
        for comp_name, ip_address, index, hist_item in items:
            blob = {
                "computer_name": comp_name,
                "id": str(index),
                "ip_address": ip_address,
                "hist_item": hist_item
            }
            insert_csv(target, blob, stats, table_name)

但是,它有点像反模式,将有效的两个不同函数合并到一个包含if语句的函数中。

def output_ip_hist_sql(target, final, stats, table_name, bulk_qty):
    field_names = ",".join(get_field_names(final, table_name))
    stats[table_name] = 0

    items = my_generator(final)
    while True:
        values = ['("%s", "%s", "%s", "%s")' % i for i in islice(items, bulk_qty)]
        if not values:
            break                
        insert_sql_many(target, len(values), table_name, field_names, values, stats)

def output_ip_hist_csv(target, final, stats, table_name):
    items = my_generator(final)
    for comp_name, ip_address, index, hist_item in items:
        blob = {
            "computer_name": comp_name,
            "id": str(index),
            "ip_address": ip_address,
            "hist_item": hist_item
        }
        insert_csv(target, blob, stats, table_name)