unify - Python Quote Formatter

0.5 · active · verified Thu Apr 16

Unify is a Python command-line tool designed to standardize string literal quoting within Python source files. It modifies strings to consistently use either single or double quotes where possible, configurable by the user. The current version is 0.5, released on August 7, 2019, and its release cadence is infrequent as it's a mature, focused utility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `unify` CLI tool programmatically via Python's `subprocess` module to standardize string quotes in a file. It first creates a sample Python file, applies `unify` to enforce single quotes, then reapplies it to enforce double quotes, and finally cleans up the temporary file.

import subprocess
import os

# Create a dummy Python file for demonstration
file_content = """x = \"abc\"
y = 'hello'
z = \"world\""""
with open('example.py', 'w') as f:
    f.write(file_content)

print('Original content of example.py:')
with open('example.py', 'r') as f:
    print(f.read())

# Run unify to enforce single quotes in-place
try:
    # Using --quote ' to enforce single quotes
    result = subprocess.run(['unify', '--in-place', '--quote', "'", 'example.py'], capture_output=True, text=True, check=True)
    print('\nUnify output:', result.stdout)
    if result.stderr:
        print('Unify errors:', result.stderr)

    print('\nContent of example.py after unify (single quotes):')
    with open('example.py', 'r') as f:
        print(f.read())

    # Run unify again to enforce double quotes in-place
    result = subprocess.run(['unify', '--in-place', '--quote', '"', 'example.py'], capture_output=True, text=True, check=True)
    print('\nUnify output:', result.stdout)
    if result.stderr:
        print('Unify errors:', result.stderr)

    print('\nContent of example.py after unify (double quotes):')
    with open('example.py', 'r') as f:
        print(f.read())

except subprocess.CalledProcessError as e:
    print(f"Error running unify: {e.stderr}")
except FileNotFoundError:
    print("Error: 'unify' command not found. Make sure it's installed and in your PATH.")
finally:
    # Clean up the dummy file
    if os.path.exists('example.py'):
        os.remove('example.py')
    print('\nCleaned up example.py')

view raw JSON →