CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyparsing

A Python parsing module providing an alternative approach to creating and executing simple grammars

Pending
Overview
Eval results
Files

common-expressions.mddocs/

Common Parser Expressions

Pre-built parser expressions for frequently used patterns including numeric types, identifiers, network addresses, dates, and parse actions for data conversion. The pyparsing_common class provides a comprehensive collection of ready-to-use parsers that handle common data formats.

Capabilities

Numeric Parsers

Parser expressions for various numeric formats with automatic type conversion.

class pyparsing_common:
    """Namespace class containing common parser expressions."""
    
    # Integer parsers
    integer: ParserElement           # Unsigned integer
    signed_integer: ParserElement    # Signed integer (+/-)
    hex_integer: ParserElement       # Hexadecimal integer (0x...)
    
    # Floating point parsers
    real: ParserElement             # Floating point number
    sci_real: ParserElement         # Scientific notation (1.23e-4)
    number: ParserElement           # Any number (int or float)
    fnumber: ParserElement          # Any number returned as float
    ieee_float: ParserElement       # IEEE float including NaN, inf
    
    # Fraction parsers
    fraction: ParserElement         # Fraction (n/d)
    mixed_integer: ParserElement    # Mixed number (1-1/2)

Usage examples:

from pyparsing import pyparsing_common as ppc

# Parse integers
result = ppc.integer.parse_string("12345")  # -> [12345]

# Parse signed numbers  
result = ppc.signed_integer.parse_string("-42")  # -> [-42]

# Parse hexadecimal
result = ppc.hex_integer.parse_string("0xFF")  # -> [255]

# Parse floating point
result = ppc.real.parse_string("3.14159")  # -> [3.14159]

# Parse scientific notation
result = ppc.sci_real.parse_string("6.02e23")  # -> [6.02e+23]

# Parse fractions
result = ppc.fraction.parse_string("3/4")  # -> [[3, '/', 4]]

# Parse mixed numbers
result = ppc.mixed_integer.parse_string("2-1/3")  # -> [[2, [1, '/', 3]]]

String and Identifier Parsers

Parsers for common string patterns and programming language constructs.

class pyparsing_common:
    # Programming identifiers
    identifier: ParserElement        # Programming language identifier
    
    # List parsers
    comma_separated_list: ParserElement  # Comma-separated values

Usage examples:

# Parse identifiers
result = ppc.identifier.parse_string("variable_name")  # -> ['variable_name']
result = ppc.identifier.parse_string("_private")      # -> ['_private']

# Parse CSV data
csv_data = ppc.comma_separated_list.parse_string("apple,banana,cherry")
# -> [['apple', 'banana', 'cherry']]

Network Address Parsers

Parsers for various network address formats.

class pyparsing_common:
    # IP addresses
    ipv4_address: ParserElement     # IPv4 address (192.168.1.1)
    ipv6_address: ParserElement     # IPv6 address  
    
    # Other network formats
    mac_address: ParserElement      # MAC address (AA:BB:CC:DD:EE:FF)
    url: ParserElement             # HTTP/HTTPS/FTP URLs

Usage examples:

# Parse IPv4 addresses  
result = ppc.ipv4_address.parse_string("192.168.1.1")
# -> [['192', '.', '168', '.', '1', '.', '1']]

# Parse URLs
result = ppc.url.parse_string("https://www.example.com/path")
# -> ['https://www.example.com/path']

# Parse MAC addresses
result = ppc.mac_address.parse_string("AA:BB:CC:DD:EE:FF")
# -> [['AA', ':', 'BB', ':', 'CC', ':', 'DD', ':', 'EE', ':', 'FF']]

Date and Time Parsers

Parsers for ISO8601 date and datetime formats.

class pyparsing_common:
    # Date/time parsers
    iso8601_date: ParserElement      # ISO8601 date (YYYY-MM-DD)
    iso8601_datetime: ParserElement  # ISO8601 datetime

Usage examples:

# Parse ISO8601 dates
result = ppc.iso8601_date.parse_string("2023-12-25")
# -> [['2023', '-', '12', '-', '25']]

# Parse ISO8601 datetime
result = ppc.iso8601_datetime.parse_string("2023-12-25T10:30:00Z")
# -> datetime parsing result

UUID Parser

Parser for Universally Unique Identifiers.

class pyparsing_common:
    # UUID parser
    uuid: ParserElement             # UUID format

Usage example:

# Parse UUIDs
import uuid
ppc.uuid.set_parse_action(lambda t: uuid.UUID(t[0]))
result = ppc.uuid.parse_string("12345678-1234-5678-1234-567812345678")
# -> [UUID('12345678-1234-5678-1234-567812345678')]

Parse Actions for Data Conversion

Static methods that create parse actions for converting parsed tokens to specific data types.

class pyparsing_common:
    @staticmethod
    def convert_to_integer() -> callable:
        """Create parse action to convert tokens to integers."""
    
    @staticmethod  
    def convert_to_float() -> callable:
        """Create parse action to convert tokens to floats."""
    
    @staticmethod
    def convert_to_date(fmt: str = "%Y-%m-%d") -> callable:
        """Create parse action to convert tokens to date objects."""
    
    @staticmethod
    def convert_to_datetime(fmt: str = None) -> callable:
        """Create parse action to convert tokens to datetime objects."""

Usage examples:

# Convert to integers
int_parser = Word(nums).set_parse_action(ppc.convert_to_integer())
result = int_parser.parse_string("42")  # -> [42] (int, not string)

# Convert to floats  
float_parser = Regex(r'\d+\.\d+').set_parse_action(ppc.convert_to_float())
result = float_parser.parse_string("3.14")  # -> [3.14] (float)

# Convert to date objects
date_parser = ppc.iso8601_date.set_parse_action(ppc.convert_to_date())
result = date_parser.parse_string("2023-12-25")  # -> [datetime.date(2023, 12, 25)]

# Convert to datetime objects  
datetime_parser = ppc.iso8601_datetime.set_parse_action(ppc.convert_to_datetime())

Text Processing Actions

Static methods for common text processing operations.

class pyparsing_common:
    @staticmethod
    def strip_html_tags() -> callable:
        """Create parse action to remove HTML tags from text."""
    
    @staticmethod
    def upcase_tokens() -> callable:
        """Create parse action to convert tokens to uppercase."""
    
    @staticmethod  
    def downcase_tokens() -> callable:
        """Create parse action to convert tokens to lowercase."""

Usage examples:

# Strip HTML tags
html_stripper = Regex(r'<[^>]*>').set_parse_action(ppc.strip_html_tags())

# Convert to uppercase
upper_parser = Word(alphas).set_parse_action(ppc.upcase_tokens())
result = upper_parser.parse_string("hello")  # -> ['HELLO']

# Convert to lowercase  
lower_parser = Word(alphas).set_parse_action(ppc.downcase_tokens())
result = lower_parser.parse_string("WORLD")  # -> ['world']

Usage Patterns

Common patterns for using pyparsing_common expressions.

Complete number parsing:

# Parse any numeric format
any_number = (ppc.sci_real | ppc.real | ppc.signed_integer | ppc.integer)

# Parse with automatic conversion
typed_number = any_number.copy().set_parse_action(
    lambda t: float(t[0]) if '.' in t[0] or 'e' in t[0].lower() else int(t[0])
)

Configuration file parsing:

# Parse configuration entries
config_value = (ppc.number | ppc.uuid | QuotedString('"') | Word(alphanums))
config_entry = ppc.identifier + "=" + config_value
config_file = OneOrMore(config_entry)

Network log parsing:

# Parse access log entries
log_entry = (ppc.ipv4_address + 
            QuotedString('"') +  # User agent
            ppc.iso8601_datetime +
            ppc.integer)  # Response code

Data validation with parse actions:

# Validate and convert email-like patterns
email_pattern = (Word(alphanums + "._") + "@" + 
                Word(alphanums + ".-") + "." + 
                Word(alphas, min=2, max=4))

validated_email = email_pattern.set_parse_action(
    lambda t: "".join(t) if "@" in "".join(t) else None
)

Install with Tessl CLI

npx tessl i tessl/pypi-pyparsing

docs

common-expressions.md

core-elements.md

enhancement.md

exceptions.md

helpers.md

index.md

testing-debugging.md

tile.json