Created
November 2, 2011 13:56
-
-
Save brunobord/1333685 to your computer and use it in GitHub Desktop.
Chained string transformations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
@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!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about:
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 ;-)