-
-
Save brunobord/1333685 to your computer and use it in GitHub Desktop.
### My question is: is this solution elegant enough? | |
### I mean: if I'm adding several other functions to "clean" my "cell", will it still be "Pythonic"? | |
### e.g.: for f in (func1, func2, func3, func..): stuff = f(stuff) | |
def strip(cell): | |
return cell.strip() | |
def removedblspaces(cell): | |
return u' '.join(cell.split()) | |
def clean(cell): | |
if isinstance(cell, unicode): | |
for f in (removedblspaces, strip): | |
cell = f(cell) | |
return cell | |
return cell |
What about:
def clean(cell):
if isinstance(cell, unicode):
return ' '.join(part.strip() for part in cell.split())
return cell
To me this one-liner is more readable than understanding that it iterates through functions and verifying which function is doing what, in which order and so on.
@revolunet: never put a mutable as a default argument, please ;-)
@davidbgk: yes thanks, i changed list to tuple ;)
the oneliner is readable but doesnt provide the chained function calls as requested, or did i miss something ?
@revolunet is right. @davidbgk has missed the point.
@davidbgk pointed something important (and weird) about mutable functions arguments.
check this example :
def test1(a=[1,2,3]):
a.append(4)
return a
print test1()
print test1(a=[5,6,7])
print test1()
@brunobord if you want some kind of pipeline to generate a workflow, I advise you to use generators. I even wonder if you can use decorators in your case:
@remove_blank_spaces
@strip
def clean_cell(data):
return data
...
But it really depends on your data, as always!
yes, right. both solutions (yours and mine) are quite similar: iteration over a list of functions. that conforts me in my position, thx.