Skip to main content

A Python formatter to automatically add blank lines after if/for/while/with/try blocks

Project description

blank-line-after-blocks

A Python formatter to automatically add blank lines after if/for/while/with/try blocks to improve code readability.

Installation

pip install blank-line-after-blocks

Usage

Command Line

# Format Python files
blank-line-after-blocks file1.py file2.py

# Format Jupyter notebooks
blank-line-after-blocks-jupyter notebook1.ipynb notebook2.ipynb

Pre-commit Hook

Add this to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/jsh9/blank-line-after-blocks
    rev: 0.1.0
    hooks:
      - id: blank-line-after-blocks
      - id: blank-line-after-blocks-jupyter

What it does

This tool automatically adds one blank line after the end of:

  • if statements
  • for loops
  • while loops
  • with statements
  • try/except/finally blocks

This improves code readability by providing visual separation between blocks and subsequent code.

Examples

Basic if and for blocks

  if condition:
      do_something()
+
  next_statement()

  for item in items:
      process(item)
+
  final_step()

Try/except blocks with context managers

  def process_files(filenames):
      results = []
      for filename in filenames:
          try:
              with open(filename) as f:
                  data = json.load(f)
+
              results.append(data)
          except FileNotFoundError:
              print(f'File {filename} not found')
          except json.JSONDecodeError:
              print(f'Invalid JSON in {filename}')
+
      return results

Nested blocks in class methods

  class TestClass:
      def method(self):
          try:
              if self.condition():
                  with self.get_context():
                      self.do_work()
+
                  self.cleanup()
+
          except Exception as e:
              self.handle_error(e)
+
          print('method complete')

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

blank_line_after_blocks-0.1.0.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

blank_line_after_blocks-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file blank_line_after_blocks-0.1.0.tar.gz.

File metadata

  • Download URL: blank_line_after_blocks-0.1.0.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for blank_line_after_blocks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fb52d25c21aae85cfc1fc2a884dd69ed9e47f06b54c7e76294423f34a011c6db
MD5 38732da347680d6eeeac418aa8e43c27
BLAKE2b-256 749730aece1a708fbbd748730d16884b01f40b4e625417819551a9db55fc2f35

See more details on using hashes here.

File details

Details for the file blank_line_after_blocks-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for blank_line_after_blocks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 626d87ce515afe55fbd34b86d67b20bae0fdedd7f722e49deaae6991fac5f7fa
MD5 c18529509ad38aa9c8ef9bcee53de40e
BLAKE2b-256 829cf8e520d58500281be86a78e160f608962decfbc5879ad332daea5ca38fc7

See more details on using hashes here.

Supported by

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