Skip to main content

regexparser

Frequently I have to parse text into float, int and date, for a few examples. The TextParser class to isolates the parsing task, instead of getting the parsing rules (or functions) spread all over the code.

Install

pip install regexparser

pip install from github:

pip install git+https://github.com/wilsonfreitas/regexparser.git

Using

Create a class inheriting TextParser and write methods with names starting with parse. These methods must accept 2 more arguments after self and those arguments are the text that will be parsed and the MatchObject that is returned by applying the regular expression to the text. The parse* methods are called only if its regular expression is matched and their regular expressions are set in the methods' doc string.

textparser provides a compact way of applying transformation rules and that rules don't have to be spread out along the code.

The following code shows how to create text parsing rules for a tew text chunks in portuguese.

class PortugueseRulesParser(TextParser):
    # transform Sim and Não into boolean True and False, ignoring case
    def parseBoolean_ptBR(self, text, match):
        r'^(sim|Sim|SIM|n.o|N.o|N.O)$'
        return text[0].lower() == 's'
    # transform Verdadeiro and Falso into boolean True and False, ignoring case
    def parseBoolean_ptBR2(self, text, match):
        r'^(verdadeiro|VERDADEIRO|falso|FALSO|V|F|v|f)$'
        return text[0].lower() == 'v'
    # parses a decimal number
    def parse_number_decimal_ptBR(self, text, match):
        r'^-?\s*\d+,\d+?$'
        text = text.replace(',', '.')
        return eval(text)
    # parses number with thousands
    def parse_number_with_thousands_ptBR(self, text, match):
        r'^-?\s*(\d+\.)+\d+,\d+?$'
        text = text.replace('.', '')
        text = text.replace(',', '.')
        return eval(text)

parser = PortugueseRulesParser()

assert parser.parse('1,1') == 1.1
assert parser.parse('-1,1') == -1.1
assert parser.parse('- 1,1') == -1.1
assert parser.parse('Wálson') == 'Wálson'
assert parser.parse('1.100,01') == 1100.01

I copied the idea of using a regular expression in __doc__ from PLY.

Metadata

Release files for regexparser 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for regexparser 0.1.0
File Size Uploaded Provenance
regexparser-0.1.0.tar.gz 4.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for regexparser 0.1.0
File Interpreter ABI Platform
regexparser-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / regexparser-0.1.0.tar.gz

Download URL regexparser-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a4f021db06ed3c1aa0d9a97a151be2a116ae0bd6b0001880580c48f3ea4070d0
BLAKE2b-256 checksum
How to use checksums
bf24b2c0ab8ea331145bda3b10c929e8e4aaa808d300480109951da666ca513f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.7.16 Windows/10

Release files / regexparser-0.1.0-py3-none-any.whl

Download URL regexparser-0.1.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ef0094b1fc58379b209c3887c357a931e33488496ae1c1b0ab44822d1995e33
BLAKE2b-256 checksum
How to use checksums
794a9b2219eb2fd0cd8551b3a6c99d2206a006367706aca5b58f64384b83f6b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.7.16 Windows/10

Security for regexparser 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page