coding convention conversion

David Porter jcm at bigskytel.com
Sat Dec 23 00:04:14 EST 2000


* Garry Knight <garryknight at bigfoot.com>:
> In article <mailman.977516469.1157.python-list at python.org> "Issac
> Trotts" <trotts at llnl.gov> wrote:
> 
> > Does anyone know of a utility to change between the coding conventions
> >  of
> > 
> > def fooBarBaz(): pass
> > 
> > and 
> > 
> > def foo_bar_baz(): pass
> > 
> > It is a pain to do this by hand.
> 
> Much though I hate to do this in a Python newsgroup, here's a Perl
> script that should do the job. It reads lines from STDIN and copies
> them to STDOUT. If a line starts with optional whitespace followed by
> the word "def" followed by more whitespace followed by a name, then if
> that name contains capital letters, each capital is lower-cased and has
> a "_" prepended.
> 
> Copy and paste the text between the cut marks and save it as, say,
> /usr/local/bin/redef (or the equivalent location on your system) and
> use it as follows:
>   redef <infile >outfile
> It's about as simple as it can be and hasn't been tested exhaustively.
> If there are errors, or if you need it enhanced, please let me know.

It only changes the def lines. It should also alter the lines that call the
function (but not imported functions):

from x import importedFunc

def someFunc(x):
    pass

someFunc("what about me?")    

importedFunc(DontAlter)

> --------8X-cut here-----------------------------------------------
> #!/usr/bin/perl
> 
> while (<>) {
>   if (/^\s*def\s+\w+/) {
>     s/([A-Z])/_\l\1/g;
>   }
>   print;
> }
> --------8X-cut here-----------------------------------------------






More information about the Python-list mailing list