Striving for PEP-8 compliance

Robert Kern robert.kern at gmail.com
Wed Apr 7 12:11:38 EDT 2010


On 2010-04-07 11:06 AM, Tom Evans wrote:
> On Wed, Apr 7, 2010 at 4:10 PM, geremy condra<debatem1 at gmail.com>  wrote:
>> On Wed, Apr 7, 2010 at 10:53 AM, Tom Evans<tevans.uk at googlemail.com>  wrote:
>>> [ Please keep me cc'ed, I'm not subscribed ]
>>>
>>> Hi all
>>>
>>> I've written a bunch of internal libraries for my company, and they
>>> all use two space indents, and I'd like to be more consistent and
>>> conform to PEP-8 as much as I can.
>>>
>>> My problem is I would like to be certain that any changes do not alter
>>> the logic of the libraries. When doing this in C, I would simply
>>> compile each module to an object file, calculate the MD5 of the object
>>> file, then make the whitespace changes, recompile the object file and
>>> compare the checksums. If the checksums match, then the files are
>>> equivalent.
>>>
>>> Is there any way to do something semantically the same as this with python?
>>
>> Probably the logical thing would be to run your test suite against
>> it, but assuming that's not an option, you could run the whole
>> thing through dis and check that the bytecode is identical. There's
>> probably an easier way to do this though.
>>
>> Geremy Condra
>>
>
> dis looks like it may be interesting.
>
> I had looked a little at the bytecode, but only enough to rule out md5
> sums as a solution. Looking closer at the bytecode for a simple
> module, it seems like only a few bytes change (see below for hexdumps
> of the pyc).
>
> So in this case, only bytes 5 and 6 changed, the rest of the file
> remains exactly the same. Looks like I need to do some digging to find
> out what those bytes mean.

You will also have to be careful about docstrings. If you are cleaning up for 
style reasons, you will also end up indenting the triple-quoted docstrings and 
thus change their contents. This will be reflected in the bytecode.

In [1]: def f():
    ...:     """This is
    ...:     a docstring.
    ...:     """
    ...:
    ...:

In [2]: def g():
    ...:   """This is
    ...:   a docstring.
    ...:   """
    ...:
    ...:

In [3]: f.__doc__
Out[3]: 'This is \n    a docstring.\n    '

In [4]: g.__doc__
Out[4]: 'This is\n  a docstring.\n  '


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list