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.2.1.tar.gz (6.5 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.2.1-py2-none-any.whl (7.7 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: strbalance-0.2.1.tar.gz
  • Upload date:
  • Size: 6.5 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.2.1.tar.gz
Algorithm Hash digest
SHA256 9c2b1e2b23a1fe4bce7f27387e17af6239ed6531ebf5b546708df3c9ca36e02b
MD5 e0aad67a77c16eb47e0e1f47ebb4f2dd
BLAKE2b-256 6a2c012c6ac7dd87330f0b87fa7c429545748f2d047ac15bf8e165476f23c952

See more details on using hashes here.

File details

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

File metadata

  • Download URL: strbalance-0.2.1-py2-none-any.whl
  • Upload date:
  • Size: 7.7 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.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 46f493061ed19447a63a48c13fa73cd871b8e811a9ba2aa5f178d53b8d49551e
MD5 eeb535645feb46d3971f9bf8af119190
BLAKE2b-256 4bb6bd7be8c5d49cc25ffadd20d8958c0daf10b4231b2d39fd1bb569d747e307

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