coding convention conversion

Bruce Dodson bruce_dodson at bigfoot.com
Fri Dec 22 23:29:08 EST 2000


Better yet, import the file as a module, and scan through its public symbols
(via dir()) to find out which names need to be changed, and then use that
information to build a sed script which can be applied globally.  You can
easily take advantage of other meta-information such as the type, e.g. limit
it to just functions if you want.  This approach is good because the parser
will recognize more variations than a regexp.


Something like this is what I had in mind:

from types import ModuleType, ClassType

def print_symbols(symbols, prefix=''):
    for sym in dir(symbols):
        if sym[:2] == '__': continue

        v = getattr(symbols, sym)
        if type(v) is ModuleType: continue

        print prefix + sym

        if type(v) is ClassType:
            print_symbols(v, prefix + sym + '.')


See also the pyclbr module for a way to do something similar without
executing the module.

Bruce



"D-Man" <dsh8290 at rit.edu> wrote in message
news:mailman.977522226.5104.python-list at python.org...
>
> This shouldn't be too hard with a python script and some regexes with
> some more string processing in between (or maybe multiple pass).
>
> Everything you want to change is bounded by "def" on one side and "("
> on the other.  Then replace the capitals with _ followed by the lower
> case version.
>
> A quick (partial) solution:
>
> re = re.compile( "def ([a-z]+)([A-Z])([a-z]*)\(" )
>
> for line in source.readlines() :
> match = re.match( line )
> if ( match ) :
> res = "def " + re.group( 0 ) + "_" + \
> string.lower( re.group( 1 ) + re.group( 2 ) + "("
>
>
> This isn't complete and won't have the arguments in the result (nor
> preserve indentation before the "def").
>
> I think the re.sub() function will do more of what you want it to.
>
>
> HTH,
> -D
>
>
> On Fri, Dec 22, 2000 at 12:20:15PM -0800, Issac Trotts 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.
> >
> > Thanks,
> > Issac
>




More information about the Python-list mailing list