Python 有没有更好的方法将不同的参数输入到带有if语句的函数中?
我自学Python已经有一段时间了,以前从未编程过。我刚刚编写了一个基本的备份程序,它在复制时写出每个文件的进度。我编写了一个函数来确定缓冲区的大小,以便用较小的缓冲区复制较小的文件,用较大的缓冲区复制较大的文件。我现在设置代码的方式似乎不是很有效,因为有一个if循环,然后导致另一个if循环,创建四个选项,它们都只是用不同的参数调用同一个函数Python 有没有更好的方法将不同的参数输入到带有if语句的函数中?,python,Python,我自学Python已经有一段时间了,以前从未编程过。我刚刚编写了一个基本的备份程序,它在复制时写出每个文件的进度。我编写了一个函数来确定缓冲区的大小,以便用较小的缓冲区复制较小的文件,用较大的缓冲区复制较大的文件。我现在设置代码的方式似乎不是很有效,因为有一个if循环,然后导致另一个if循环,创建四个选项,它们都只是用不同的参数调用同一个函数 import os import sys def smartcopy(filestocopy, dest_path, show_progress = F
import os
import sys
def smartcopy(filestocopy, dest_path, show_progress = False):
"""Determines what buffer size to use with copy()
Setting show_progress to True calls back display_progress()"""
#filestocopy is a list of dictionaries for the files needed to be copied
#dictionaries are used as the fullpath, st_mtime, and size are needed
if len(filestocopy.keys()) == 0:
return None
#Determines average file size for which buffer to use
average_size = 0
for key in filestocopy.keys():
average_size += int(filestocopy[key]['size'])
average_size = average_size/len(filestocopy.keys())
#Smaller buffer for smaller files
if average_size < 1024*10000: #Buffer sizes determined by informal tests on my laptop
if show_progress:
for key in filestocopy.keys():
#dest_path+key is the destination path, as the key is the relative path
#and the dest_path is the top level folder
copy(filestocopy[key]['fullpath'], dest_path+key,
callback = lambda pos, total: display_progress(pos, total, key))
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, callback = None)
#Bigger buffer for bigger files
else:
if show_progress:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600,
callback = lambda pos, total: display_progress(pos, total, key))
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600)
def display_progress(pos, total, filename):
percent = round(float(pos)/float(total)*100,2)
if percent <= 100:
sys.stdout.write(filename + ' - ' + str(percent)+'% \r')
else:
percent = 100
sys.stdout.write(filename + ' - Completed \n')
导入操作系统
导入系统
def smartcopy(文件目录、目的地路径、显示进度=False):
“”“确定要与copy()一起使用的缓冲区大小”
将show_progress设置为True将回调display_progress()
#filestocopy是需要复制的文件的字典列表
#使用字典作为完整路径,需要时间和大小
如果len(filestocopy.keys())==0:
一无所获
#确定要使用的缓冲区的平均文件大小
平均大小=0
对于filestocopy.keys()中的键:
平均大小+=int(filestocopy[key]['size'])
平均大小=平均大小/len(filestocopy.keys())
#较小文件的较小缓冲区
如果平均_大小<1024*10000:#缓冲区大小由我的笔记本电脑上的非正式测试确定
如果显示进度:
对于filestocopy.keys()中的键:
#dest_path+key是目标路径,因为key是相对路径
#dest_路径是顶层文件夹
复制(filestocopy[key]['fullpath'],dest_path+key,
回调=lambda pos,总计:显示进度(pos,总计,键))
其他:
对于filestocopy.keys()中的键:
复制(filestocopy[key]['fullpath'],dest_path+key,callback=None)
#更大的文件缓冲区
其他:
如果显示进度:
对于filestocopy.keys()中的键:
复制(filestocopy[key]['fullpath'],dest_path+key,1024*2600,
回调=lambda pos,总计:显示进度(pos,总计,键))
其他:
对于filestocopy.keys()中的键:
复制(filestocopy[key]['fullpath'],dest_path+key,1024*2600)
def显示进度(位置、总数、文件名):
百分比=四舍五入(浮动(位置)/浮动(总计)*100,2)
如果百分比我相信你在正确的轨道上。问题的一个解决方案是将参数保存在变量中
def smartcopy(filestocopy, dest_path, show_progress = False):
"""Determines what buffer size to use with copy()
Setting show_progress to True calls back display_progress()"""
#filestocopy is a list of dictionaries for the files needed to be copied
#dictionaries are used as the fullpath, st_mtime, and size are needed
if len(filestocopy.keys()) == 0:
return None
#Determines average file size for which buffer to use
average_size = 0
for key in filestocopy.keys():
average_size += int(filestocopy[key]['size'])
average_size = average_size/len(filestocopy.keys())
#Smaller buffer for smaller files
if show_progress:
progress_callback = lambda pos, total: display_progress(pos, total, key)
else:
progress_callback = None
#Bigger buffer for bigger files
if average_size < 1024*10000: #Buffer sizes determined by informal tests on my laptop
buffer = None
else:
buffer = 1024 * 2600
for key, value in filestocopy.iteritems():
#dest_path+key is the destination path, as the key is the relative path
#and the dest_path is the top level folder
copy(value['fullpath'], dest_path+key, buffer, callback=progress_callback)
多加一点注意,类似这样:
if len(filestocopy.keys()) == 0:
return None
if not filestocopy:
return
也可以这样写:
if len(filestocopy.keys()) == 0:
return None
if not filestocopy:
return
循环本身可以简化为:
for key, value in filestocopy.iteritems():
#dest_path+key is the destination path, as the key is the relative path
#and the dest_path is the top level folder
copy(value['fullpath'], dest_path+key, **kwargs)
在迭代字典时,从来都不需要keys()
,因为这是默认行为:)
因此,以下几行都将得到相同的结果:
keys = list(some_dict)
keys = some_dict.keys()
keys = list(some_dict.keys())
keys = list(some_dict.iterkeys())
我相信你在正确的轨道上。问题的一个解决方案是将参数保存在变量中
def smartcopy(filestocopy, dest_path, show_progress = False):
"""Determines what buffer size to use with copy()
Setting show_progress to True calls back display_progress()"""
#filestocopy is a list of dictionaries for the files needed to be copied
#dictionaries are used as the fullpath, st_mtime, and size are needed
if len(filestocopy.keys()) == 0:
return None
#Determines average file size for which buffer to use
average_size = 0
for key in filestocopy.keys():
average_size += int(filestocopy[key]['size'])
average_size = average_size/len(filestocopy.keys())
#Smaller buffer for smaller files
if show_progress:
progress_callback = lambda pos, total: display_progress(pos, total, key)
else:
progress_callback = None
#Bigger buffer for bigger files
if average_size < 1024*10000: #Buffer sizes determined by informal tests on my laptop
buffer = None
else:
buffer = 1024 * 2600
for key, value in filestocopy.iteritems():
#dest_path+key is the destination path, as the key is the relative path
#and the dest_path is the top level folder
copy(value['fullpath'], dest_path+key, buffer, callback=progress_callback)
多加一点注意,类似这样:
if len(filestocopy.keys()) == 0:
return None
if not filestocopy:
return
也可以这样写:
if len(filestocopy.keys()) == 0:
return None
if not filestocopy:
return
循环本身可以简化为:
for key, value in filestocopy.iteritems():
#dest_path+key is the destination path, as the key is the relative path
#and the dest_path is the top level folder
copy(value['fullpath'], dest_path+key, **kwargs)
在迭代字典时,从来都不需要keys()
,因为这是默认行为:)
因此,以下几行都将得到相同的结果:
keys = list(some_dict)
keys = some_dict.keys()
keys = list(some_dict.keys())
keys = list(some_dict.iterkeys())
首先,请注意:
- 您不必使用
len(filestocopy.keys())
,只需执行len(filestocopy)
- 首选
键入filestocopy.iterkeys()
而不是键入filestocopy.keys()
您可以尝试以下方法:
callback = None
if show_progress:
callback = lambda pos, total: display_progress(pos, total, key)
if average_size < 1024*10000:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path + key, callback)
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], desth_path + key, 1024 * 2600, callback)
这里,“WHATEVER_SIZE”显然应该替换为较小列表的缓冲区大小,或者默认值应该是什么
基本思想是在循环变量之前初始化函数参数,然后在函数调用中使用这些变量。:) 一些注意事项,首先:
- 您不必使用
len(filestocopy.keys())
,只需执行len(filestocopy)
- 首选
键入filestocopy.iterkeys()
而不是键入filestocopy.keys()
您可以尝试以下方法:
callback = None
if show_progress:
callback = lambda pos, total: display_progress(pos, total, key)
if average_size < 1024*10000:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path + key, callback)
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], desth_path + key, 1024 * 2600, callback)
这里,“WHATEVER_SIZE”显然应该替换为较小列表的缓冲区大小,或者默认值应该是什么
基本思想是在循环变量之前初始化函数参数,然后在函数调用中使用这些变量。:) 看起来您调用的是同一个函数copy
if show_progress:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600,
callback = lambda pos, total: display_progress(pos, total, key))
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600)
具有不同数量的参数。为什么这里需要if/else。不太清楚。看起来您调用的是同一个函数copy
if show_progress:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600,
callback = lambda pos, total: display_progress(pos, total, key))
else:
for key in filestocopy.keys():
copy(filestocopy[key]['fullpath'], dest_path+key, 1024*2600)
具有不同数量的参数。为什么这里需要if/else。不太清楚。沃尔夫和萨夫的答案大致正确。下面是一个随机Python简化,您可以使用以下方法进行平均计算:
沃尔夫和萨夫的回答几乎是对的。下面是一个随机Python简化,您可以使用以下方法进行平均计算:
您给出了一个巨大的示例代码,很难理解。但是您从问题的标题中询问(解释)的是“通常如何将可变数量的参数传递给python的函数调用?”?然后,答案是通过传递列表/元组或字典
def fun(a, *b, **c):
print a
for item in b:
print item
for k,v in c:
print k,v
a = 42
b = [1,2,3]
c = {'a':'1','b':2,'c':True}
fun(a,b,c)
只需注意,当传递容器(列表/元组)和字典时,前者应该在后者之前。您给出了大量示例代码,很难理解。但是您从问题的标题中询问(解释)的是“通常如何将可变数量的参数传递给python的函数调用?”?然后,答案是通过传递列表/元组或字典
def fun(a, *b, **c):
print a
for item in b:
print item
for k,v in c:
print k,v
a = 42
b = [1,2,3]
c = {'a':'1','b':2,'c':True}
fun(a,b,c)
只需注意,当传递容器(列表/元组)和字典时,前者应该在后者之前。不需要列表