Skip to main content

Another Uncompromising Code Formatter for Python.

Project description

logo

StraitJacket: Another Uncompromising Code Formatter for Python

StraitJacket is a wrapper around black which implements post processing to perform automatic code alignment.

Project/Repo:

MIT License Supported Python Versions CalVer v202106.1019 PyPI Version PyPI Downloads

Code Quality/CI:

GitHub Build Status GitLab Build Status Type Checked with mypy Code Coverage Code Style: sjfmt

Name role since until
Manuel Barkhau (mbarkhau@gmail.com) author/maintainer 2018-10 -

Alignment

Example of automatic alignment.

class TokenType(enum.Enum):           # class TokenType(enum.Enum):

    INDENT    = 0                     #     INDENT = 0
    SEPARATOR = 1                     #     SEPARATOR = 1
    CODE      = 2                     #     CODE = 2


Indent      = str                     # Indent = str
RowIndex    = int                     # RowIndex = int
ColIndex    = int                     # ColIndex = int
OffsetWidth = int                     # OffsetWidth = int
TokenTable  = typ.List[TokenRow]      # TokenTable = typ.List[TokenRow]

Usage

Usage is exactly the same as for black, except that the command is named sjfmt.

$ pip install straitjacket
$ sjfmt --help
Usage: sjfmt [OPTIONS] [SRC]...

  Another uncompromising code formatter.

Options:
  -l, --line-length INTEGER       How many characters per line to allow.
                                  [default: 88]
  --py36                          Allow using Python 3.6-only syntax on all

Editor/Tooling Integration

Plugins for your editor usually support setting a custom path to black. You can simply point to sjfmt instead.

Unix

$ which sjfmt
/home/user/miniconda3/envs/py36/bin/sjfmt
$ which sjfmtd
/home/user/miniconda3/envs/py36/bin/sjfmtd

Windows

C:\Users\Username>where sjfmt
C:\Python37\Scripts\sjfmt.exe

or

PS C:\Users\Username> (gcm sjfmt).Path
C:\Python37\Scripts\sjfmt.exe

sublack:

{

    "black_command": "C:/Python37/Scripts/sjfmt.exe",
    "black_line_length": 100,
    // ...
}

Document formatting can be triggered with Ctrl+Alt+F.

vscode python extension

{
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "C:\\Python37\\Scripts\\sjfmt.exe",
    "python.formatting.blackArgs": [
        "--line-length", "100",
        "--py36",
        "--skip-string-normalization"
    ],
}

Document formatting can be triggered with Shift+Alt+F.

BlackPycharm

Install the plugin black-pycharm, which can be found in Settings > Plugins > Brows Repositories. You may have to restart PyCharm for the plugin to load.

To configure the path, go to Settings > Tools > BlackPycharm Configuration and set Path to Black executable to the location of the sjfmt binary.

You can reformat your code using Ctrl + Shift + A to access the Find Action panel. The name of the action to format your code is Reformat code (BLACK). You may want to rebind this action, at least in my setup the default binding didn't seem to work.

Flake8

By the nature of this plugin, certain flake8 codes will be violated. This is an excerpt from what you might put in your setup.cfg to ignore these:

[flake8]
ignore =
    # No whitespace after paren open "("
    E201
    # No whitespace before paren ")"
    E202
    # Whitespace before ":"
    E203
    # Multiple spaces before operator
    E221
    # Multiple spaces after operand
    E222
    # Multiple spaces after ':'
    E241
    # Multiple spaces before keyword
    E272
    # Spaces around keyword/parameter equals
    E251

Changelog for straitjacket

v202106.1019

  • Update vendored black to 21.6b0

v202104.1018

  • Update vendored black to 21.4b2

v202104.1017

  • Update vendored black to 21.4b1

v202008.1016

  • Bugfix: Bad path handling at root level.

v202008.1015

  • Update vendored black to 20.8b1

v202008.1014

  • Vendor black from master
  • Fix issue with multiprocessing/fork on MacOS

v201910.0011-beta

  • Update for release of black==19.10b0

v201904.0010-beta

  • Update for release of black==19.3b0

v201812.0007-alpha

  • TLDR: more bugfixes and updated documentation

v201812.0006-alpha

TLDR: no major changes, just bug fixes.

  • Fixed #1: Disable right alignment of numbers when not preceded by a number.
  • Fixed #3: Disable alignment when preceded by a multiline string.
  • Fixed #4: Excessive whitespace in non aligned context.
  • Fixed #8: Double and single quotes around strings not consistent

v201810.0004-alpha

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

straitjacket-202106.1019.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

straitjacket-202106.1019-py3-none-any.whl (80.2 kB view details)

Uploaded Python 3

File details

Details for the file straitjacket-202106.1019.tar.gz.

File metadata

  • Download URL: straitjacket-202106.1019.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for straitjacket-202106.1019.tar.gz
Algorithm Hash digest
SHA256 e7ce869d045577d5152d8f87f1f857992ba04e81e1b0c10afea98309ad267696
MD5 6bfb097e65bf2991bcb942abd93d4793
BLAKE2b-256 99d9d7a2c7b1c43f18d86ffe748b169cefff50e993ecfe6c59acd6de6682638c

See more details on using hashes here.

File details

Details for the file straitjacket-202106.1019-py3-none-any.whl.

File metadata

  • Download URL: straitjacket-202106.1019-py3-none-any.whl
  • Upload date:
  • Size: 80.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for straitjacket-202106.1019-py3-none-any.whl
Algorithm Hash digest
SHA256 ded495cf6afc63134352d1e51d52133889000da382ec12789a554b605dad3e57
MD5 8589f53f1546515a4ebaddc90e73be40
BLAKE2b-256 8ef2840a002362c6e4f89615feed85f0744ea530bacc1a75ae240611bdc07e7b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page