jztools.autonamed_pattern#

Classes

AutonamedPattern(pattern, nested_patterns, ...)

Represents a pattern with named groups that have sequential numbers automatically attached to them as suffixes.

pxs()

Contains base regex patterns.

class jztools.autonamed_pattern.AutonamedPattern(pattern: str, nested_patterns: ~typing.Dict[str, ~jztools.autonamed_pattern.AutonamedPattern] = <factory>, names: ~typing.Tuple[str] | None = None, _k: int = 0, _lock: ~threading.RLock = <factory>, _frozen: bool = False)#

Bases: object

Represents a pattern with named groups that have sequential numbers automatically attached to them as suffixes. The numbers are guaranteed to be the same for tags that appear at the same nesting level. Other than that, no guarantees are provided about their order, except that it should be sequential. Example:

from soleil.solconf.autonamed_pattern import AutonamedPattern
import re

NESTED = AutonamedPattern('(?P<addend>[0-9])')
str(NESTED)  # Advance the counter for illustration purposes.

# 'my_letter' and 'my_value' are at the same nesting level; 'addend' is one level down.
ap = AutonamedPattern(
    r'(?P<my_letter>[a-z]) \= (?P<my_value>[0-9]) \+ {NESTED}', vars())
match = re.match(str(ap), 'a = 1 + 2')

# Tags at the same nesting level have the same suffix identifier
assert match.groupdict() == {'my_letter_0': 'a', 'my_value_0': '1', 'addend_1': '2'}

The pattern can also contain placeholders for other AutonamedPattern patterns using a syntax similar to the str.format syntax.

Warning

Calling the AutonamedPattern.__str__() method (even implicitly through print(obj)) will modify the object by advancing the counter. This enables support for situations where the same nested pattern is used more than once in the same expression, e.g., '{pattern}{pattern}'.

Use AutonamedPattern.view() instead if you want to view the rendered auto-named pattern string without modifying the object.

# Simple auto-named pattern
sp = AutonamedPattern('(?P<htag>Hello)')

# Match simple pattern
assert sp.view() == '(?P<htag_0>Hello)' # View the pattern w/o modifying the object
assert re.match(str(sp), 'Hello') # Modifies the object
assert sp.view() == '(?P<htag_1>Hello)'

# Composite auto-named pattern
cp = AutonamedPattern('{x} {x} {x} (?P<wtag>World)', {'x': sp})

# Match composite pattern
assert(
    cp.view() ==
    '(?P<htag_1>Hello) (?P<htag_2>Hello) (?P<htag_3>Hello) (?P<wtag_0>World)')
assert re.match(
    str(cp), 'Hello Hello Hello World')
assert (
    cp.view() ==
    '(?P<htag_4>Hello) (?P<htag_5>Hello) (?P<htag_6>Hello) (?P<wtag_1>World)')
next_name(name)#

Generates the next auto-numbered name derived from name.

classmethod name_builder(name, identifier)#

Generates the name derived from name for the given identifier. Derived classes wishing to modify the auto-named string format should overload this method.

classmethod get_single(base_tag: str, match: Match)#

Returns the value of id-suffixed version of base_tag, checking first that a single such tag exists in match.

classmethod get_single_tag(base_tag: str, match: Match)#

Returns the id-suffixed version of base_tag, and checks that a single such tag exists in match.

view()#

Compiles the pattern to a string and without advancing the counters.

class jztools.autonamed_pattern.pxs#

Bases: object

Contains base regex patterns.