Skip to content

Instantly share code, notes, and snippets.

@brunobord
Created November 2, 2011 13:56
Show Gist options
  • Save brunobord/1333685 to your computer and use it in GitHub Desktop.
Save brunobord/1333685 to your computer and use it in GitHub Desktop.
Chained string transformations
### 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
@revolunet
Copy link

@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()

@davidbgk
Copy link

davidbgk commented Nov 3, 2011

@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