unify - Python Quote Formatter
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
-
unify: command not found
cause The `unify` executable is not in your system's PATH, or the package was not installed correctly.fixEnsure `unify` is installed via `pip install unify` and that your Python environment's script directory is included in your system's PATH. If using a virtual environment, activate it before running `unify`. -
SyntaxError: EOL while scanning string literal
cause This error occurs in your Python code, not necessarily from `unify` itself, but can happen if `unify` processes a file that already has unclosed or incorrectly escaped string literals, or if you manually try to construct a string that `unify` would otherwise fix.fixReview the Python file being processed for unclosed or improperly escaped string literals. `unify` attempts to fix quote consistency but relies on valid Python syntax otherwise. Use a linter (e.g., Flake8, Pylint) to catch basic syntax errors before running `unify`. -
ModuleNotFoundError: No module named 'unify'
cause This error occurs if you try to `import unify` in Python without the `unify` package being installed in your environment, or if you're trying to import it as a library when its primary interface is the command line.fixIf you intend to use the `unify` CLI tool, ensure it's installed via `pip install unify` and then invoke it using `subprocess.run(['unify', ...])`. If you genuinely need a Python library for programmatic string quote unification, you might need to look for alternatives or wrap the `unify` CLI tool yourself.
Warnings
- gotcha The `unify` library is primarily a command-line tool. While you can invoke it from Python using `subprocess`, it does not offer a direct, high-level Python API for manipulating strings or files in memory, unlike some other formatting tools.
- breaking When using the `--in-place` flag, `unify` modifies files directly without creating backups. Accidental or unintended changes can lead to data loss if not managed carefully.
- gotcha Due to its generic name, `unify` can be confused with other Python packages such as `unifyai`, `unifi`, `unificontrol`, `python-unifi-client`, or `unifi-python-api`. Ensure you are installing and using `myint/unify` if your intention is string quote unification.
Install
-
pip install unify
Imports
- unify (CLI)
from unify import unify_string
import subprocess subprocess.run(['unify', '--help'])
Quickstart
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')