我正在写一个烧瓶应用程序,我发现我有很多通用的实用程序功能.
以下是我认为通用实用程序函数的函数类型的示例:
def make_hash():
return defaultdict(make_hash)
def file_read(filename):
with open(file_name_, 'r') as f:
return f.read()
def file_write(filename, data):
with open(filename, 'w') as f:
f.write(data)
Run Code Online (Sandbox Code Playgroud)
我想把这些功能一起扔到一个单独的模块中.但是,如果我有以下问题,我很好奇:
问题: - 对通用效用函数进行分组的pythonic方法是什么?我应该创建一个单独的模块吗?好奇其他人正在组织这种类型的代码.
以下代码段:
import yaml
import collections
def hasher():
return collections.defaultdict(hasher)
data = hasher()
data['this']['is']['me'] = 'test'
print yaml.dump(data)
Run Code Online (Sandbox Code Playgroud)
返回:
!!python/object/apply:collections.defaultdict
args: [&id001 !!python/name:__main__.hasher '']
dictitems:
this: !!python/object/apply:collections.defaultdict
args: [*id001]
dictitems:
is: !!python/object/apply:collections.defaultdict
args: [*id001]
dictitems: {me: test}
Run Code Online (Sandbox Code Playgroud)
我该如何删除:
!!python/object/apply:collections.defaultdict
[*id001]
Run Code Online (Sandbox Code Playgroud)
最终目标是:
this:
is:
me: "test"
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏!