API Reference#
Functions#
- renorm.compile(pattern: str, *specs: NormSpec, flags: _FlagsType = 0, **named_specs: NormSpec) Pattern#
Compile a renorm pattern by combining a raw pattern string with positional and named specifications, and return a
Patternobject.- Parameters:
- patternstr
The raw pattern string containing positional (
{@idx}) or named ({@name}) placeholders.- *specsNormSpec[T]
Positional specifications to substitute into the pattern.
- flagsre.RegexFlag, optional
Standard
remodule flags to apply when compiling the underlying regular expression.- **named_specsNormSpec[T]
Named specifications to substitute into the pattern.
- Returns:
- Pattern[T]
A compiled renorm pattern that can be used to search and normalize matches.
Regular expression objects#
- class renorm.pattern.Pattern(*args: Any, **kwargs: Any)#
Bases:
GenericCompiled renorm pattern exposing a minimal API similar to
re.Pattern, but returningrenorm.Matchobjects with normalization semantics.Instances of this class are created via
renorm.compile()and cannot be instantiated directly.- property pattern: str#
The fully expanded regular expression string incorporating renorm specifications and used internally for matching.
- search(text: str) Match | None#
Scan through text looking for the first location where the pattern matches.
- Parameters:
- textstr
The input string to search.
- Returns:
- Match[T] or None
A renorm
Matchobject if a match is found, otherwiseNone.
Match objects#
- class renorm.pattern.Match(*args: Any, **kwargs: Any)#
Bases:
Genericrenormmatch object exposing a minimal API similar tore.Match, but returning normalized groups according to the associatedNormSpecobjects.Instances of this class are created internally by
Patternmethods and cannot be instantiated directly.- groups() tuple[str | T | None, ...]#
Return all captured groups after normalization.
Groups associated with a
NormSpecare normalized using itsnormalize()method. Groups without an associated specification are returned as raw strings. Optional groups that did not participate in the match are returned asNone.A captured substring may be
Nonewhen the corresponding capturing group did not participate in the match. This happens with optional groups. For example, in a pattern liker"(a)?(b)":matching only
"b"produces the groups(None, "b")because the optional group(a)?matched nothing.
- Returns:
- tuple of (str or T or None)
The normalized captured groups.
- group(*idxs: int) str | T | tuple[str | T | None, ...] | None#
Return one or more captured groups.
Behaves like
re.Match.group(), but applies normalization to groups associated with specifications.- Parameters:
- *idxsint
Group indices to retrieve.
0returns the entire match.
- Returns:
- str | T | None or tuple of (str or T or None)
A single normalized group, or a tuple of groups if multiple indices are provided.
Data Specification objects#
renorm composable specifications that pair a regular expression
pattern with a deterministic normalization strategy.
Each specification describes a precise grammar for a class of textual values and guarantees that matched substrings can be transformed into a canonical representation.
- class renorm.specs.NormSpec#
-
Abstract specification pairing a regular expression pattern with a deterministic normalization strategy.
A
NormSpecdefines two things:a regex pattern describing the acceptable textual representations of a value of type
T;a normalization function that converts a captured substring into a canonical value of type
T(orNoneif the substring is invalid).
Subclasses must implement
patternandnormalize().- abstract property pattern: str#
Regular expression pattern describing the acceptable textual representations for this specification.
- Returns:
- str
The regex pattern as a string.
- abstractmethod normalize(group: str | None) T | None#
Normalize a captured substring according to this specification.
A captured substring may be
Nonewhen the corresponding capturing group did not participate in the match. This happens with optional groups. For example, in the patternr"(a)?(b)":matching only
"b"produces the groups(None, "b")because the optional group(a)?matched nothing.
- Parameters:
- groupstr or None
The captured substring to normalize.
Noneindicates that the capturing group did not participate in the match (e.g., optional groups that matched nothing).
- Returns:
- T or None
The canonical value corresponding to the substring, or
Noneif the substring is invalid under this specification.
- class renorm.specs.Num(dec: str = '.', ths: str = '')#
-
Specification for plain (non-scientific) numeric literals with configurable decimal and thousands separators.
The
patternproperty exposes a permissive regex used only for capture: it greedily matches number-like fragments to avoid truncating malformed literals. Normalization then applies a strict pattern that enforces the instance’s numeric specification. If the captured literal does not satisfy the strict rules,normalizereturns None; otherwise it returns a normalizedfloat.Numeric literal semantics
If
thsis an empty string, the integer part must be ungrouped.If
thsis a valid character, the integer part must be grouped in 3-digit blocks from the right, usingthsas the thousands separator.If
decis an empty string, the number must not contain a fractional part.If
decis a valid character, a fractional part is optional and must usedec.An optional sign (
+or-) is allowed, with optional trailing whitespace (not configurable).
- Attributes:
- decstr
Decimal separator:
'.',',', or''for integers only- thsstr
Thousands separator:
'.',',',"'",' '<space>, or''for no grouping
- property pattern: str#
Permissive regular expression used for initial capture of number-like fragments. This pattern is intentionally broad and accepts any substring that resembles a numeric literal, without enforcing strict grouping or decimal rules. Strict validation according to the specified
thsanddecseparators is applied later bynormalize().- Returns:
- str
The permissive regex pattern as a string.
- normalize(group: str | None) float | None#
Normalize a captured numeric literal according to this specification.
This method applies strict validation to a substring previously matched by
pattern. If the substring satisfies the configured grouping rules (ths), decimal separator (dec), and optional sign semantics, it is converted into a canonicalfloat. Otherwise,Noneis returned.- Parameters:
- groupstr or None
The captured substring to normalize.
- Returns:
- float or None
The normalized numeric value, or
Noneif the substring is invalid under this specification.
Exceptions#
Define renorm exceptions and errors.
- exception renorm.exceptions.PatternError#
Bases:
RenormErrorRaised when a renorm pattern violates parser rules.
- exception renorm.exceptions.PatternIndexError#
Bases:
PatternErrorRaised when a positional placeholder index is out of range.
- exception renorm.exceptions.PatternKeyError#
Bases:
PatternErrorRaised when a named placeholder is missing from named_specs.
- exception renorm.exceptions.SpecValueError#
Bases:
RenormErrorRaised when a normalization specification receives an invalid value