我主要从事数据科学/分析工作。通常,我需要测试带有和不带随机次抽样的代码(如果原始数据太大),有时显示或隐藏结果,这取决于我是在测试代码还是生成最终结果,以及在.csv中保存最终结果/df等等。
我一直在使用这种策略,即在代码的开头创建一个称为“控制面板”的部分,并在其中设置了许多“开关”等以及用户定义的参数,如下所示
# Control panel
save_file_switch = False # WARNING: will overwrite existing when == True
edge_date_inclusion = True # whether to include the last date in the range of inclusion criteria
testing_printout_switch = False
result_printout_switch = True
df_subsampling_switch = False # WARNING: make to sure turn off for final results
df_subsampling_n = 15000
random_seed = 888稍后在我的代码中,要打开或关闭的代码块将由开关的值决定。
if testing_printout_switch == True:
print (df_results) # print some results到目前为止,我发现它是一种有用的组织工具,但我不知道是否有更好和更多的pythonic方法来解决这个问题。
发布于 2018-12-06 16:48:09
我会将所有的标志、开关和变量存储在字典中。
control_panel = {
'save_file_switch': False,
'edge_date_inclusion': True,
'testing_printout_switch': False,
'result_printout_switch': True,
'df_subsampling_switch': False
'df_subsampling_n': 15000
'random_seed': 888
}
if control_panel['testing_printout_switch']:
print(df_results)虽然不完全确定这在多大程度上是"Pythonic“,但它为您提供了一个集中存储您的价值观的地方,同时又保持了它们的独特性和独特性。
https://stackoverflow.com/questions/53655960
复制相似问题