Python Formatter¶
Contents:
Readme¶
Introduction¶


formatter2 is a Python formatter based on the tokenize library in Python. Due to a bug with line continuations we are currently running a fork of the tokenize library however.
During every format sequence the compile method is used to check if no code has been broken, but backing up is still a good idea.
The library is made to be very extendable and configurable but it’s still in the beginning phase. Eventually this will become a fully featured formatting tool for Python.
Install¶
To install simply execute python setup.py install or pip install formatter2. If you want to run the tests first, run nosetests
Usage¶
To format all of your code recursively (MAKE BACKUPS!):
python-formatter -r DIRECTORY
Or:
format-python -r DIRECTORY
Formatting stdin is also possible:
cat some_python_file.py | format-python - > some_formatted_file.py
formatter Package¶
formatter
Package¶
-
class
formatter2.
Token
(offsets, tok_type, token, begin, end, line)[source]¶ Bases:
object
-
begin
¶ The begin coordinates.
Settings this to a tuple will set the first to the row and the latter to the col.
This returns a
SmartList
with the row and the column.
-
begin_col
¶ The begin column.
-
begin_row
¶ The begin row.
-
col
¶ The column.
Setting this to a tuple will set the first to the begin and the latter to the end.
This returns a
SmartList
with the begin and end column.
-
end
¶ The end.
Settings this to a tuple will set the first to the row and the latter to the col.
This returns a
SmartList
with the row and column.
-
end_col
¶ The end column.
-
end_row
¶ The end row.
-
line
¶ The line.
Setting the line automatically updates the column as well.
Returns a
str()
object with the line.
-
row
¶ The row.
Setting this to a tuple will set the first to the begin and the latter to the end.
This returns a
SmartList
with the begin and end row.
-
type
¶
-
-
class
formatter2.
TokenOffset
(parent, type_, token, pre=0, post=0, children=None, end=None, pre_collapse=True, post_collapse=True)[source]¶ Bases:
object
-
end
¶ Set the end token in case of children.
Should either be a token or a tuple with
TokenType
and token which will be a string.
-
parent
¶ The parent.
TokenOffsets
-
surround
¶ Surround the token with this amount of space. Setting will set the pre and post when given a tuple or
int()
.
-
type
¶ The type.
TokenType
-
offsets
Module¶
-
class
formatter2.offsets.
TokenOffset
(parent, type_, token, pre=0, post=0, children=None, end=None, pre_collapse=True, post_collapse=True)[source]¶ Bases:
object
-
end
¶ Set the end token in case of children.
Should either be a token or a tuple with
TokenType
and token which will be a string.
-
parent
¶ The parent.
TokenOffsets
-
surround
¶ Surround the token with this amount of space. Setting will set the pre and post when given a tuple or
int()
.
-
type
¶ The type.
TokenType
-
tokens
Module¶
-
class
formatter2.tokens.
Token
(offsets, tok_type, token, begin, end, line)[source]¶ Bases:
object
-
begin
¶ The begin coordinates.
Settings this to a tuple will set the first to the row and the latter to the col.
This returns a
SmartList
with the row and the column.
-
begin_col
¶ The begin column.
-
begin_row
¶ The begin row.
-
col
¶ The column.
Setting this to a tuple will set the first to the begin and the latter to the end.
This returns a
SmartList
with the begin and end column.
-
end
¶ The end.
Settings this to a tuple will set the first to the row and the latter to the col.
This returns a
SmartList
with the row and column.
-
end_col
¶ The end column.
-
end_row
¶ The end row.
-
line
¶ The line.
Setting the line automatically updates the column as well.
Returns a
str()
object with the line.
-
row
¶ The row.
Setting this to a tuple will set the first to the begin and the latter to the end.
This returns a
SmartList
with the begin and end row.
-
type
¶
-
types
Module¶
-
class
formatter2.types.
CommentTokenType
(id_, name)[source]¶ Bases:
formatter2.types.TokenType
-
name
= 'COMMENT'¶
-
-
class
formatter2.types.
DefaultTokenType
[source]¶ Bases:
formatter2.types.TokenType
-
class
formatter2.types.
IndentTokenType
(id_, name)[source]¶ Bases:
formatter2.types.TokenType
-
name
= 'INDENT'¶
-
-
class
formatter2.types.
StringTokenType
(id_, name)[source]¶ Bases:
formatter2.types.TokenType
-
name
= 'STRING'¶
-