Pyjoyment.Routes.Pattern - Route pattern¶
from Pyjoyment.Routes.Pattern import Pattern
# Create pattern
pattern = Pattern('/test/:name')
# Or import the module, as in Google Python Style Guide
import Pyjoyment.Routes.Pattern
# Create pattern
pattern = Pyjoyment.Routes.Pattern.new('/test/:name')
# Match routes
captures = pattern.match('/test/sebastian')
print(captures['name'])
Pyjoyment.Routes.Pattern is the core of Pyjoyment.Routes.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojolicious::Routes::Pattern of Mojolicious 9.49.
- class Pyjoyment.Routes.Pattern.Pyjoyment_Routes_Pattern(pattern: str | None = None, /, **constraints: Constraint)[source]¶
Pyjoyment.Routes.Patterninherits all methods fromPyjo.Baseand implements the following new ones.- constraints: dict[str, Constraint]¶
constraints = pattern.constraints pattern.constraints = {'foo': re.compile(r'\w+')}
Regular expression constraints.
- defaults: dict[str, Any]¶
defaults = pattern.defaults pattern.defaults = {'foo': 'bar'}
Default parameters.
- match(path: str, detect: bool = False) dict[str, Any] | None[source]¶
captures = pattern.match('/foo/bar') captures = pattern.match('/foo/bar', True)
Match pattern against entire path, format detection is disabled by default.
- match_partial(path: str, detect: bool = False) tuple[dict[str, Any], str] | None[source]¶
captures, path = pattern.match_partial(path) captures, path = pattern.match_partial(path, True)
Match pattern against path and return the captures with the rest of the path, without the matching parts, format detection is disabled by default.
- parse(pattern: str | None = None, /, **constraints: Constraint) Self[source]¶
pattern = pattern.parse('/user/:id') pattern = pattern.parse('/user/:id', id=re.compile(r'\d+')) pattern = pattern.parse(format=['json', 'yaml'])
Parse pattern.
- placeholder_start¶
start = pattern.placeholder_start pattern.placeholder_start = ':'
Character indicating a placeholder, defaults to
:.
- placeholders: list[str]¶
placeholders = pattern.placeholders pattern.placeholders = ['foo', 'bar']
Placeholder names.
- quote_end¶
end = pattern.quote_end pattern.quote_end = '}'
Character indicating the end of a quoted placeholder, defaults to
>.
- quote_start¶
start = pattern.quote_start pattern.quote_start = '{'
Character indicating the start of a quoted placeholder, defaults to
<.
- regex: Pattern[str] | None¶
regex = pattern.regex pattern.regex = regex
Pattern in compiled regular expression form.
- relaxed_start¶
start = pattern.relaxed_start pattern.relaxed_start = '*'
Character indicating a relaxed placeholder, defaults to
#.
- render(values: dict[str, Any] | None = None, endpoint: bool = False) str[source]¶
path = pattern.render({'id': 24}) path = pattern.render({'id': 24}, True)
Render pattern into a path with parameters, format rendering is disabled by default.
- tree: list[Token]¶
tree = pattern.tree pattern.tree = [['text', '/foo']]
Pattern in parsed form. Note that this structure should only be used very carefully since it is very dynamic.
- type_start¶
start = pattern.type_start pattern.type_start = '|'
Character indicating the start of a placeholder type, defaults to
:.
- types: dict[str, Constraint]¶
types = pattern.types pattern.types = {'int': re.compile(r'[0-9]+')}
Placeholder types.
- unparsed: str | None¶
unparsed = pattern.unparsed pattern.unparsed = '/:foo/:bar'
Raw unparsed pattern.
- wildcard_start¶
start = pattern.wildcard_start pattern.wildcard_start = '*'
Character indicating the start of a wildcard placeholder, defaults to
*.