coding convention conversion

Garry Knight garryknight at bigfoot.com
Sat Dec 23 02:29:00 CET 2000


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.

--------8X-cut here-----------------------------------------------
#!/usr/bin/perl

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

-- 
Garry Knight
garryknight at bigfoot.com



More information about the Python-list mailing list