utils.perlpie

Module: utils.perlpie

Perform a global search and replace on the current directory recursively.

This a small python wrapper around the perl -p -i -e functionality. I strongly recommend running perlpie on files under source control. In this way it’s easy to track your changes and if you discover your regular expression was wrong you can easily revert. I also recommend using grin to test your regular expressions before running perlpie.

Parameters

regexregular expression

Regular expression matching the string you want to replace

newstringstring

The string you would like to replace the oldstring with. Note this is not a regular expression but the exact string. One exception to this rule is the at symbol @. This has special meaning in perl, so you need an escape character for this. See Examples below.

Requires

perl : The underlying language we’re using to perform the search and replace.

grin : Grin is a tool written by Robert Kern to wrap grep and find with python and easier command line options.

Examples

Replace all occurrences of foo with bar:

perlpie foo bar

Replace numpy.testing with nipy’s testing framework:

perlpie 'from\s+numpy\.testing.*' 'from nipy.testing import *'

Replace all @slow decorators in my code with @dec.super_slow. Here we have to escape the @ symbol which has special meaning in perl:

perlpie '\@slow' '\@dec.super_slow'

Remove all occurrences of importing make_doctest_suite:

perlpie 'from\snipy\.utils\.testutils.*make_doctest_suite'

Functions

nipy.utils.perlpie.check_deps()
nipy.utils.perlpie.main()
nipy.utils.perlpie.perl_dash_pie(oldstr, newstr, dry_run=None)

Use perl to replace the oldstr with the newstr.

Examples

# To replace all occurrences of ‘import numpy as N’ with ‘import numpy as np’ from nipy.utils import perlpie perlpie.perl_dash_pie(r’imports+numpys+ass+N’, ‘import numpy as np’) grind | xargs perl -pi -e ‘s/imports+numpys+ass+N/import numpy as np/g’

nipy.utils.perlpie.print_extended_help(option, opt_str, value, parser, *args, **kwargs)