clean up triple quotes
Project description
triple-quote-clean
TripleQuoteCleaner is a Python class that can be used to clean triple-quoted strings in a variety of ways. It's designed to be used in cases where you want to remove extraneous whitespace and/or add indentation to a triple-quoted string. This is useful when dealing with extensions/code that require some extraneous metadata in the string.
To use TripleQuoteCleaner, simply import it into your Python script:
from triple_quote_clean import TripleQuoteCleaner
Usage
Basic Usage
TripleQuoteCleaner is designed to be used with the >>
, <<
, <
>
, **
operators in Python. All the different operators were selected to allow for
flexibility when considering the operator order, i.e. **
takes the highest
priority and should be used when adding strings
Here's an example:
query = """--sql
select *
from some_database
"""
tqc = TripleQuoteCleaner()
tqc.skip_top_lines = 1
output = query >> tqc
In this example, the >> operator is used to "pipe" the query string into tqc. skip_top_lines is set to 1 so that the first line of the query (the "--sql" line) is skipped. The output of this code will be:
select *
from some_database
Indentation
TripleQuoteCleaner can also be used to add indentation to a triple-quoted string. Here's an example:
input_string = """hello world"""
tqc = TripleQuoteCleaner()
output = '\n' + input_string >> tqc.tab
In this example, tqc.tab is used to add one level of indentation to the output string. The output of this code will be:
hello world
Guide Characters
Indentation can also be specified using a "guide character". This is useful if you want to maintain a certain level of tabs in all your paragraphs in your triple-quoted string. Here's an example:
query = """--sql
$$
select *
from some_database
"""
tqc = TripleQuoteCleaner()
tqc.skip_top_lines = 1
output = query >> tqc
In this example, tqc.guide_character is set to "$$" so that the 4 beginning spaces will be maintained . The output of this code will be:
select *
from some_database
Jinja Pipe
A jinja pipe is a simple wrapper over a jinja string render. For example
tqc = TripleQuoteCleaner(skip_top_lines=1)
(
JinjaPipe(columns=["hello", "these", "are", "some", "columns"])
<< tqc
** """--sql
select
{% for item in columns %}
{% if loop.last%}
nvl({{item}}, -1)
{% else %}
nvl({{item}}, -1),
{% endif %}
{% endfor %}
from
some.table
"""
)
will produce
select
hello,
these,
are,
some,
columns,
world
from
some.table
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file triple-quote-clean-1.1.0.tar.gz
.
File metadata
- Download URL: triple-quote-clean-1.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5113a42665f71877689f9be59199d0d7a3d04b30fba6ff62d4debc69c382b2be |
|
MD5 | 27755dba5d8b16126a946186ee4564ea |
|
BLAKE2b-256 | 7816fcb292e18789943a396a0c3413ffab9469ce13667df11894b48406597730 |
File details
Details for the file triple_quote_clean-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: triple_quote_clean-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4da847e4c3d3f9c7e37e99d0e2abcb063fe01e373589f24a5ea57e5cc1b10e1e |
|
MD5 | e865eb55372324f955f4421d3ae9b40c |
|
BLAKE2b-256 | a69cbe97c1906ba149732a27889e72e776d5bfb590f44c12e20f15d15d75d052 |