coding convention conversion

Garry Knight garryknight at bigfoot.com
Sun Dec 24 02:29:59 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

Well, this turned out to be fairly trivial for functions with names
like fooBarBaz, but became a little less trivial for cases where the
author has defined functions with names like FooBarBaz. However, this
should do the job:

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

while (<>) {
  push(@lines, $_);
  if (/^\s*def\s+(\w+)\s*\(/) {
  $defs{$1} = "\l$1";
  }
}
@list = keys(%defs);
foreach $line (@lines) {
  foreach $def (@list) {
    if ($line =~ /$def/) {
      $line =~ s/$def/$defs{$def}/;
      $line =~ s/([A-Z])/_\l\1/g;
      last;
    }
  }
  print $line;
}
--------8X-cut here-----------------------------------------------

As before, copy and paste the text between the cut lines and save it
as, say, /usr/local/bin/redef (or the equivalent location on your
system) and use it as follows:
  redef <infile >outfile
Of course, I'm assuming you're running a *nix system with Perl
installed. If not, hopefully someone else will come up with a solution.

Thanks to David Porter and Ben Wolfson for helpful suggestions.

-- 
Garry Knight
garryknight at bigfoot.com



More information about the Python-list mailing list