Reputation: 9986
Newbie question about python syntaxis. I have some function calls like following
summaryA = do_something(lambda info: (info.a, 1)). \
.do_other() \
.do_anoter() \
.saveAsTextFile('/output/path/a.tsv')
summaryJ = do_something(lambda info: (info.j, 1)). \
.do_other() \
.do_anoter() \
.saveAsTextFile('/output/path/j.tsv')
summaryZ = do_something(lambda info: (info.z, 1)). \
.do_other() \
.do_anoter() \
.saveAsTextFile('/output/path/z.tsv')
info
is an instance of class Info
.
These calls are pretty alike. So, I would like to remove copy-paste and get something like following (This is just idea, I am not "native pythonian")
summaryA = super_do(Info.a, '/output/path/a.tsv')
summaryJ = super_do(Info.j, '/output/path/j.tsv')
summaryZ = super_do(Info.z, '/output/path/z.tsv')
How to write function super_do
?
Upvotes: 0
Views: 63
Reputation: 20346
You can do this:
def super_do(attr, path):
return do_something(lambda info, attr=attr: (getattr(info, attr), 1)).\
.do_other() \
.do_another() \
.saveAsTextFile(path)
You can then use it like this:
summaryA = super_do("a", "/output/path/a.tsv")
Upvotes: 2