Skip to main content

A package to check whether a string is balanced

Project description

strbalance

This small Python library provides functionality to check whether brackets, quotation marks, tags etc. inside the string are balanced, i.e. whether each of them is opened and closed properly with respect to other punctuation.

Installation

Use the package manager pip to install strbalance:

pip install strbalance

Usage

If the punctuation in the string is not balanced, the result is an Unbalanced object, None otherwise:

import strbalance

balance = strbalance.Balance()
print(balance.is_unbalanced('{[]}[()]'))  # outputs None
print(balance.is_unbalanced('abc(def"gh"ijkl)'))  # outputs None

unbalanced = balance.is_unbalanced('abcdefgh(ijkl]mnopqrst')
print(unbalanced.unclosed, unbalanced.short_summary, 
        unbalanced.long_summary)  # outputs ( ...fgh(ijk... ...fgh(ijkl]mno...
print(unbalanced.opening_position, unbalanced.opening_length,
        unbalanced.closing_position, unbalanced.closing_length)  # outputs 8 1 13 1

Istead of

import strbalance
balance = strbalance.Balance()
balance.is_unbalanced('{[]}[()]')

one can simply write

import strbalance
strbalance.is_unbalanced('{[]}[()]')

Apart from the string to check, strbalance.is_unbalanced() accepts all the optional parameters which can be passed to Balance constructor:

  • pairs and symmetrical are lists of additional characters (or lines) to match; they default to None:
    • pairs – a list containing pairs of strings in the following format:

      [['a-opening'], ['a-closing'], ['b-opening', 'b-closing'] ...]

    • symmetrical – a list of strings representing characters (or their sequences) which have identical opening and closing forms.

  • All other parameters are boolean and default to False:
    • tags – match HTML (XML) paired tags (case sensitive).
    • ignore_case – ignore case in tags.
    • cjk – include CJK brackets and quotation signs: 「…」, 「…」, 〈…〉, 《…》, 『…』, (…), […], <…>, {…}, ⦅…⦆, 【…】, 〔…〕, 〖…〗, 〘…〙, 〚…〛.
    • straight – include straight quotation marks (single and double).
    • custom - balance only custom characters and character sequences (listed in pairs and symmetrical and/or added with other parameters).
    • german - use German quoting convention: „…“, ‚…‘, »…«, ›…‹ instead of “…”, ‘…’, «…», ‹…›.
    • math - match parentheses with brackets in order to include mathematical [a,c) notation.

The pairs of characters matched by default:

  • (…) parentheses
  • […] brackets
  • {…} braces
  • “…” double quotes
  • ‘…’ single quotes
  • «…» double angle quotes
  • ‹…› single angle quotes

License

This project is licensed under the MIT License.

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

strbalance-0.3.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

strbalance-0.3.1-py2-none-any.whl (7.6 kB view details)

Uploaded Python 2

File details

Details for the file strbalance-0.3.1.tar.gz.

File metadata

  • Download URL: strbalance-0.3.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.5

File hashes

Hashes for strbalance-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a33e6b088d8109d448e1546ec54562d0ec8732fca49d8aa73716c1ec6f9d4c47
MD5 5e279b118fe4391e57a7759f05100fdc
BLAKE2b-256 112ae3e57601a4f6c35d884512254900273e85cd3b0a4733157204e972705a9f

See more details on using hashes here.

File details

Details for the file strbalance-0.3.1-py2-none-any.whl.

File metadata

  • Download URL: strbalance-0.3.1-py2-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.5

File hashes

Hashes for strbalance-0.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 d9746183ae16c081761e295abe078db8c1a9508f5c0cd92bdc48de890936e001
MD5 f1ebc6497944e7a49d2a6c5e108edf27
BLAKE2b-256 a2356d766ae6b8479c3db444d486711094e4523ea2d059b8d690dbf599ab347f

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