Skip to content

ysskrishna/quotes-convert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

quotes-convert

License Tests Python PyPI PyPI Downloads Documentation Interactive Playground

Convert matching double-quotes to single-quotes or vice versa in strings and streams. Inspired by the popular to-single-quotes npm package.

πŸš€ Try it interactively in your browser! Test the library with our Interactive Playground - no installation required.

Features

  • Multiple input types: Convert quotes in strings and streams
  • Command-line interface: Optional CLI tool for file and stream processing
  • Proper escaping: Automatically handles quote escaping and unescaping
  • Memory efficient: Process large texts with streaming without loading everything into memory
  • Zero dependencies: Lightweight core library with no external dependencies
  • Type safe: Full type hints for excellent IDE support

Why use this library?

Why not just use .replace('"', "'")? Because simply replacing quotes breaks strings that contain nested quotes.

# The problem with simple replace
original = '"result = \'test\'"'
broken   = original.replace('"', "'")
# Result: 'result = 'test'' -> Broken! Unmatched quotes

# The solution: quotes-convert handles escaping correctly
from quotes_convert import single_quotes
fixed    = single_quotes(original)
# Result: 'result = \'test\'' -> Fixed! Properly escaped

Installation

pip install quotes-convert

Usage Examples

Basic Usage

from quotes_convert import single_quotes, double_quotes

result = single_quotes('x = "hello"; y = "world"')
print(result)  # x = 'hello'; y = 'world'


result = double_quotes('x = "hello"; y = "world"')
print(result)  # x = "hello"; y = "world"

Handling Mixed Quotes

from quotes_convert import single_quotes, double_quotes

# Automatically escapes inner quotes
result = single_quotes('"it\'s working"')
print(result)  # 'it\'s working'

result = double_quotes("'say \"hi\"'")
print(result)  # "say \"hi\""

Processing JSON-like Strings

Useful for normalizing JSON strings or Python dict definitions.

from quotes_convert import double_quotes

json_str = "{'key': 'value', 'nested': {'inner': 'data'}}"
result = double_quotes(json_str) # {"key": "value", "nested": {"inner": "data"}}

Shell Script Processing

from quotes_convert import single_quotes

script = 'echo "Hello $USER"; grep "pattern" file.txt'
result = single_quotes(script) # echo 'Hello $USER'; grep 'pattern' file.txt

Streaming Large Texts

Process large files or streams efficiently without loading the entire content into memory.

from quotes_convert import single_quotes_stream

def line_generator():
    yield 'line 1: "hello"\n'
    yield 'line 2: "world"\n'

# Process the stream chunk by chunk
for chunk in single_quotes_stream(line_generator()):
    print(chunk, end='')

# Output:
# line 1: 'hello'
# line 2: 'world'

Command-Line Interface

The CLI tool allows you to convert quotes in files and streams directly from the command line.

Installation

pip install quotes-convert[cli]

Quick Examples

# Convert to single quotes
quotes-convert -s file.py
echo 'x = "hello"' | quotes-convert -s

# Convert to double quotes
quotes-convert -d file.py

# Edit file in place
quotes-convert -s -i file.py

# Check version
quotes-convert --version

For complete documentation including batch processing, shell integration, and advanced usage, see the CLI Guide.

API Reference

Function Description
single_quotes(text: str) -> str Convert matching double-quotes to single-quotes.
double_quotes(text: str) -> str Convert matching single-quotes to double-quotes.
single_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] Convert matching double-quotes to single-quotes in a stream, yielding chunks.
double_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] Convert matching single-quotes to double-quotes in a stream, yielding chunks.

Acknowledgments

Inspired by Sindre Sorhus's to-single-quotes npm package.

Changelog

See CHANGELOG.md for a detailed list of changes and version history.

Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, development setup, and the process for submitting pull requests.

Support

If you find this library helpful:

  • ⭐ Star the repository
  • πŸ› Report issues
  • πŸ”€ Submit pull requests
  • πŸ’ Sponsor on GitHub

License

MIT Β© Y. Siva Sai Krishna - see LICENSE file for details.


Author's GitHub β€’ Author's LinkedIn β€’ Report Issues β€’ Package on PyPI β€’ Package Documentation β€’ Package Playground

About

Convert matching double-quotes to single-quotes or vice versa in strings and streams. Inspired by the popular to-single-quotes npm package

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors

Languages