http://www.lacusveris.com/PythonTidy/PythonTidy.python
Now, I'm looking for beta-testers. Compensation is a bit on the low
side. In fact it's limited to the satisfaction of helping out.
Thanks.
--
.. Chuck Rhode, Sheboygan, WI, USA
$ cat test.py
#!/usr/bin/env python2.5
# vim: set fileencoding=utf-8 :
for i in xrange ( 3) :
print i
$ python2.5 PythonTidy.python < test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
for i in xrange(3):
print i
About changing the shebang line: I'll take it as a bug.
About changing the encoding declaration from vim-style to emacs-style:
I'll take it as an insult :)
Both are comments, and should be left that way. Besides, there is no
officially preferred way for each of them. BTW, in a recent thread on
this newsgroup, most people said they preferred #!/usr/bin/env python over
#!/usb/bin/python for the shebang line. See http://tinyurl.com/yngmfr .
Best regards.
--
Roberto Bonvallet
This is not "emacs-style", this is Python normalized source encoding
directive for correct interpretation of u"..." strings by Python
interpreter.
See http://www.python.org/dev/peps/pep-0263/
A+
Laurent.
The -*- syntax is emacs-style. The encoding directive is anything that
matches r"coding[=:]\s*([-\w.]+)". The docs recommend to use either
emacs-style or vim-style. See http://docs.python.org/ref/encodings.html
Cheers,
--
Roberto Bonvallet
[Laurent]
> This is not "emacs-style", this is Python normalized source encoding
> directive for correct interpretation of u"..." strings by Python
> interpreter.
So is "# vim: set fileencoding=utf-8". Anything matching
"coding[:=]\s*([-\w.]+)" on the first or second line counts.
> See http://www.python.org/dev/peps/pep-0263/
Yes, you should. 8-)
--
Richie Hindle
ric...@entrian.com
> Roberto Bonvallet a écrit :
>> # vim: set fileencoding=utf-8 :
> ...
>> # -*- coding: utf-8 -*-
> ...
>> About changing the shebang line: I'll take it as a bug.
>> About changing the encoding declaration from vim-style to emacs-style:
>> I'll take it as an insult :)
>
> This is not "emacs-style", this is Python normalized source encoding
> directive for correct interpretation of u"..." strings by Python
> interpreter.
>
> See http://www.python.org/dev/peps/pep-0263/
Where does it say it has to be emacs-style? It just uses that style as
example as far as I can see. The real definition is a regexp:
coding[:=]\s*([-\w.]+)
So the vim-style is fine. No need to replace it.
Ciao,
Marc 'BlackJack' Rintsch
> See http://www.python.org/dev/peps/pep-0263/
Aye, sorry for my missreading...
[seem I hurt the emacs guy]
> About changing the shebang line: I'll take it as a bug.
> About changing the encoding declaration from vim-style to
> emacs-style: I'll take it as an insult :)
Ooh! <wounded>
> Both are comments, and should be left that way. Besides, there is no
> officially preferred way for each of them. BTW, in a recent thread on
> this newsgroup, most people said they preferred #!/usr/bin/env python over
> #!/usb/bin/python for the shebang line. See http://tinyurl.com/yngmfr .
Thanks for the link. I was unaware of the /usr/bin/env technique and
the controversy surrounding it.
Thanks, too, for trying *PythonTidy*.
As you have no doubt perceived, *PythonTidy* is *not* "table driven."
It is a script after all. I decided before writing it that I didn't
really need to externalize all the options; nevertheless, most are
declared near the beginning where they sit just begging for end-user
involvement. See: CODING_SPEC and SHEBANG. *PythonTidy* is all about
consistency, consistency, and consistency. You can use it to
standardize shebangs and coding across a whole library of Python
scripts.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 25° — Wind NW 13 mph
No, thanks you for writing such a tool!
> [...] nevertheless, most [options] are declared near the beginning where
> they sit just begging for end-user involvement. See: CODING_SPEC and
> SHEBANG.
The fact that I immediately noticed them and came up with an example to
raise both issues is a indicator of how easy it would be to customize the
script :)
Cheers!
--
Roberto Bonvallet
Thanks,
Thomas
> The two things that bother me at the moment are how the comments are
> formatted (dunno if that can be customized or changed easily), and
> it would be good if the script took command line args instead of
> working as a filter only.
Thank you for trying PythonTidy.
o Command-line args: Please give an example of a standard command that
I might emulate w.r.t. standard argument use.
o Comments: Input is parsed twice: I use *tokenize.generate_tokens* to
extract the comments and *compiler.parse* to generate the Abstract
Syntax Tree (AST). Other applications usually use the AST to generate
bytecode, so it contains no information about comments. The tokens
list identifies keywords (and comments and some whitespace) but
doesn't group them into statements. I need both: comments *and*
functional grouping. Fortunately both the AST and the tokens list
carry line numbers to reference the source. Unfortunately the AST
line numbers are relative to functional groups and do not necessarily
apply to the particular keywords that introduce each group. This
makes fixing the position of comments relative to reconstructed code a
bit of a challenge. For example, when a comment has a line number in
the beginning/ending range of what would normally be considered one
command, I have to assume it is an inline comment regardless of how it
may have appeared in the original code.
Out-of-line comments should appear pretty much as they did in the
original code, however. Can you provide an example where they do not?
Would you prefer that they be left justified or wrapped? >-~
Doc strings (for modules, class declarations, and functions) are
another matter. PythonTidy should not mess with them (unless
LEFTJUST_DOC_STRINGS is True). They should appear exactly as
originally written.
I was taught that code ought to be self documenting and that comments
more often than not diminish readability by padding the code beyond
what can be comprehended on one page (screen). I use them only
minimally and am not greatly inconvenienced when they are moved around
a little.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 21° — Wind N 8 mph
Well, at least it would be nice if I could call
'PythonTidy.py mymodule.py' to tidy up the mymodule.py file.
> o Comments: Input is parsed twice: I use *tokenize.generate_tokens* to
> extract the comments and *compiler.parse* to generate the Abstract
> Syntax Tree (AST). Other applications usually use the AST to generate
> bytecode, so it contains no information about comments. The tokens
> list identifies keywords (and comments and some whitespace) but
> doesn't group them into statements. I need both: comments *and*
> functional grouping. Fortunately both the AST and the tokens list
> carry line numbers to reference the source. Unfortunately the AST
> line numbers are relative to functional groups and do not necessarily
> apply to the particular keywords that introduce each group. This
> makes fixing the position of comments relative to reconstructed code a
> bit of a challenge. For example, when a comment has a line number in
> the beginning/ending range of what would normally be considered one
> command, I have to assume it is an inline comment regardless of how it
> may have appeared in the original code.
>
> Out-of-line comments should appear pretty much as they did in the
> original code, however. Can you provide an example where they do not?
> Would you prefer that they be left justified or wrapped? >-~
Here is part of a diff before and after running PythonTidy on it:
<start>
- def comptr_setitem(self, index, value):
- # We override the __setitem__ method of the
- # POINTER(POINTER(interface)) type, so that the COM
- # reference count is managed correctly.
- #
- # This is so that we can implement COM methods that have to
- # return COM pointers more easily and consistent. Instead of
- # using CopyComPointer in the method implementation, we can
- # simply do:
- #
- # def GetTypeInfo(self, this, ..., pptinfo):
- # if not pptinfo: return E_POINTER
- # pptinfo[0] = a_com_interface_pointer
- # return S_OK
+ def comptr_setitem(self, index, value): # We override the __setitem__ method of the
+ # POINTER(POINTER(interface)) type, so that the COM
+ # reference count is managed correctly.
+ #
+ # This is so that we can implement COM methods that have to
+ # return COM pointers more easily and consistent. Instead of
+ # using CopyComPointer in the method implementation, we can
+ # simply do:
+ #
+ # def GetTypeInfo(self, this, ..., pptinfo):
+ # if not pptinfo: return E_POINTER
+ # pptinfo[0] = a_com_interface_pointer
+ # return S_OK
<end>
Can this be customized?
> Doc strings (for modules, class declarations, and functions) are
> another matter. PythonTidy should not mess with them (unless
> LEFTJUST_DOC_STRINGS is True). They should appear exactly as
> originally written.
>
> I was taught that code ought to be self documenting and that comments
> more often than not diminish readability by padding the code beyond
> what can be comprehended on one page (screen). I use them only
> minimally and am not greatly inconvenienced when they are moved around
> a little.
>
Thomas
> Here is part of a diff before and after running PythonTidy on it:
>
> <start>
> - def comptr_setitem(self, index, value):
> - # We override the __setitem__ method of the
> - # POINTER(POINTER(interface)) type, so that the COM
> - # reference count is managed correctly.
> + def comptr_setitem(self, index, value): # We override the __setitem__ method of the
> + # POINTER(POINTER(interface)) type, so that the COM
> + # reference count is managed correctly.
>
> Can this be customized?
I am able to rationalize why this happens, but you are correct: This
behavior is not appropriate. I am testing several changes, one of
which should prevent it in all cases, obviating the need for a custom
switch.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. 1979 Honda Goldwing GL1000 (Geraldine)
.. Weather: http://LacusVeris.com/WX
.. 10° — Wind S 5 mph
> Chuck Rhode schrieb:
> > o Command-line args: Please give an example of a standard command that
> > I might emulate w.r.t. standard argument use.
> Well, at least it would be nice if I could call
> 'PythonTidy.py mymodule.py' to tidy up the mymodule.py file.
I've uploaded a new version of PythonTidy:
o http://www.lacusveris.com/PythonTidy/PythonTidy-1.4.python
It fixes several problems. Also it allows file names as arguments.
> Here is part of a diff before and after running PythonTidy on it:
> <start>
> - def comptr_setitem(self, index, value):
> - # We override the __setitem__ method of the
> - # POINTER(POINTER(interface)) type, so that the COM
> - # reference count is managed correctly.
> + def comptr_setitem(self, index, value): # We override the __setitem__ method of the
> + # POINTER(POINTER(interface)) type, so that the COM
> + # reference count is managed correctly.
> Can this be customized?
This problem has been fixed I think. No customization should be
required to keep block comments from being rendered as in-line
comments.
Wolfgang Grafen reported that PythonTidy crashed in Python-2.4 because
new Abstract Syntax Tree node types introduced in Python-2.5 weren't
available. It was trivial to check availability, so PythonTidy should
now run in Python-2.4. However, this has not been thoroughly tested
and is not guaranteed.
PythonTidy can now recode string literals if required, although this
capability is turned off by default. See RECODE_STRINGS.
Docstrings will henceforward be enclosed in double quotes when
feasible.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 30° — Wind WSW 10 mph — Sky overcast.
PythonTidy.py cleans up, regularizes, and reformats the text of
Python scripts:
http://www.LacusVeris.com/PythonTidy/PythonTidy.python
What next? Is it appropriately submitted to the Cheese Shop?
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 3° — Wind W 8 mph
Sure.
There is still one major issue. pythonTidy uses open(input-file, "rb") to
open the Python module to tidy up. That does not work on Windows,
at least if the file has (as it should) "\r\n" newlines.
I suggest you open the file with open(input-file, "rU").
For output, PythonTidy generates "\n" line endings, this should also be changed
on Windows.
And another wish from me:
PythonTidy outputs strings with single quotes, while my own style is to
use double quotes (I don't think that pep8 prefers one over the other).
Is it possible to customize this?
> PythonTidy.py cleans up, regularizes, and reformats the text of
> Python scripts:
>
> http://www.LacusVeris.com/PythonTidy/PythonTidy.python
>
> What next? Is it appropriately submitted to the Cheese Shop?
>
Of course.
Thomas
> That went well. PythonTidy has been looked at at least 10**2
> times, and I have received a couple of complaints, which I hope
> I have addressed satisfactorily -- plenty good enough for a beta
> test. The basic concept stands.
>
> PythonTidy.py cleans up, regularizes, and reformats the text of
> Python scripts:
>
> http://www.LacusVeris.com/PythonTidy/PythonTidy.python
>
> What next? Is it appropriately submitted to the Cheese Shop?
>
I ran PythonTidy on a wxPython sample, and found that wx.CONSTANTS
were being translated to wx.Constants, which won't do at all.
In general, there is very little case manipulation that should be
done in a case-sensitive language; I suppose this is controllable
by one or more of the options, but it wasn't clear on casual
observation what would do the trick.
The idea of PythonTidy is not bad, but there should be a minimal
option that essentially reindents and nothing more. Adding other
style preferences should (in my opinion) be something like
checklist items. For example, I don't necessarily need a shebang
or coding line for my purposes, but without going into the code
and commenting out lines, I can't readily suppress them. As it
stands now, I can't trust PythonTidy to do the right thing on
unfamiliar code, and I can't readily try out various options by
simply activating or deactivating them; if I could, it would be
much more useful to me.
--
rzed
> I ran PythonTidy on a wxPython sample, and found that wx.CONSTANTS
> were being translated to wx.Constants, which won't do at all.
Find the first line in the PythonTidy code where the following global
variables are declared and change them as follows:
PERSONAL = False # This eliminates case manipulation.
> The idea of PythonTidy is not bad, but there should be a minimal
> option that essentially reindents and nothing more. ... For example,
> I don't necessarily need a shebang or coding line for my purposes,
> but without going into the code and commenting out lines, I can't
> readily suppress them.
Yes, it's too bad that PythonTidy doesn't have an *.rc file or a *.cfg
file or an *.ini file or an *.xml file or a Registry entry or a Gnome
configuration or its own graphical front end or a gob of command-line
options, but that's just the way it is.
SHEBANG = '' # This removes the shebang line from the output.
CODING_SPEC = '' # This removes the coding specification from the output.
> As it stands now, I can't trust PythonTidy to do the right thing on
> unfamiliar code, and I can't readily try out various options by
> simply activating or deactivating them; if I could, it would be much
> more useful to me.
PythonTidy should be run only on code that is already familiar or that
will rapidly become so in the near future.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 25° — Wind SSW 20 mph — Sky overcast. Precipitation.
> I suggest you open the file with open(input-file, "rU").
This doesn't work so pretty good while reading from sys.stdin, so I'm
still at the drawing board.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. 1979 Honda Goldwing GL1000 (Geraldine)
.. Weather: http://LacusVeris.com/WX
.. 27° — Wind S 15 mph — Sky overcast.
> There is still one major issue. pythonTidy uses open(input-file,
> "rb") to open the Python module to tidy up. That does not work on
> Windows, at least if the file has (as it should) "\r\n" newlines.
Thank you for challenging my parochial world view.
I have posted yet another version of PythonTidy:
http://www.lacusveris.com/PythonTidy/PythonTidy-1.5.python
This one is omnivorous wrt to newlines.
> For output, PythonTidy generates "\n" line endings, this should also
> be changed on Windows.
When OVERRIDE_NEWLINE = None, the first newline encountered on input
is the one used throughout the output; otherwise, you can set it to
what you want, e.g, OVERRIDE_NEWLINE = '\n'.
> PythonTidy outputs strings with single quotes, while my own style is
> to use double quotes (I don't think that pep8 prefers one over the
> other). Is it possible to customize this?
Here is a new global: DOUBLE_QUOTED_STRINGS = True.
--
.. Chuck Rhode, Sheboygan, WI, USA
.. Weather: http://LacusVeris.com/WX
.. 30° — Wind WNW 15 mph — Sky overcast.
Thanks,
Thomas