xdev.format_quotes module

Modifies Python code to roughly following quote scheme described in [1].
  • Single quotes (’) are used for code

  • Double quotes (”) are used for documentation

  • Don’t touch any string that has an internal quote.

References

CommandLine

# See it in action
FPATH=$(python -c "import six; print(six.__file__)")
python -m xdev.format_quotes $FPATH --diff=True
xdev.format_quotes.format_quotes_in_text(text, backend='parso')[source]

Reformat text according to formatting rules

Parameters:

text (str) – python source code

Returns:

modified text

Return type:

str

Example

>>> # xdoctest: +REQUIRES(module:parso)
>>> from xdev.format_quotes import *  # NOQA
>>> text = ub.codeblock(
...     f'''
...     def func1():
...         {TRIPLE_SINGLE_QUOTE}
...         Fix this.
...         {TRIPLE_SINGLE_QUOTE}
...
...     def func2():
...         {TRIPLE_DOUBLE_QUOTE}
...         Leave the doctests alone!
...         {TRIPLE_DOUBLE_QUOTE}
...
...     string1 = "fix these"
...     string2 = "don't fix these"
...     string3 = {TRIPLE_SINGLE_QUOTE}this is ok{TRIPLE_SINGLE_QUOTE}
...     string4 = {TRIPLE_DOUBLE_QUOTE}fix this{TRIPLE_DOUBLE_QUOTE}
...
...     def func3():
...         inside_string1 = "fix these"
...         inside_string2 = "don't fix these"
...         inside_string3 = {TRIPLE_SINGLE_QUOTE}this is ok{TRIPLE_SINGLE_QUOTE}
...         inside_string4 = {TRIPLE_DOUBLE_QUOTE}fix this{TRIPLE_DOUBLE_QUOTE}
...     ''')
>>> print(text)
>>> fixed = format_quotes_in_text(text)
>>> print(fixed)
>>> import xdev
>>> fixed = format_quotes_in_text(text, backend='parso')
>>> print('----')
>>> print(xdev.difftext(text, fixed, colored=True))
>>> # xdoctest: +REQUIRES(module:redbaron)
>>> print('----')
>>> fixed = format_quotes_in_text(text, backend='redbaron')
>>> print('----')
>>> print(xdev.difftext(text, fixed, colored=True))
>>> print('----')
xdev.format_quotes.format_quotes_in_file(fpath, diff=True, write=False, verbose=3)[source]

Autoformat quotation marks in Python files

Parameters:
  • fpath (str) – The file to format

  • diff (bool) – if True write the diff between old and new to stdout

  • write (bool) – if True write the modifications to disk

  • verbose (int) – verbosity level

xdev.format_quotes.format_quotes(path, diff=True, write=False, verbose=3, recursive=True)[source]