Pyjoyment.Routes.Match - Find routes

import Pyjoyment.Controller
import Pyjoyment.Routes

from Pyjoyment.Routes.Match import Match

# Routes
r = Pyjoyment.Routes.new()
r.get('/user/:id')
r.put('/user/:id')

# Match
c = Pyjoyment.Controller.new()
match = Match(root=r)
match.find(c, method='PUT', path='/user/23')
print(match.stack[0]['id'])

# Or import the module, as in Google Python Style Guide

import Pyjoyment.Controller
import Pyjoyment.Routes
import Pyjoyment.Routes.Match

# Routes
r = Pyjoyment.Routes.new()
r.get('/user/:id')
r.put('/user/:id')

# Match
c = Pyjoyment.Controller.new()
match = Pyjoyment.Routes.Match.new(root=r)
match.find(c, method='PUT', path='/user/23')
print(match.stack[0]['id'])

# Render
print(match.path_for()['path'])
print(match.path_for(id=24)['path'])

Pyjoyment.Routes.Match finds routes in Pyjoyment.Routes structures.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Routes::Match of Mojolicious 9.52.

class Pyjoyment.Routes.Match.Pyjoyment_Routes_Match(endpoint: Pyjoyment_Routes_Route | None = None, position: int = 0, root: Any = None, stack: list[dict[str, Any]] | None = None)[source]

Pyjoyment.Routes.Match inherits all methods from Pyjo.Base and implements the following new ones.

endpoint
route = match.endpoint
match.endpoint = Pyjoyment.Routes.Route.new()

The route endpoint that matched, usually a Pyjoyment.Routes.Route object.

find(c: Pyjoyment_Controller, *, method: str, path: str, websocket: bool = False) → bool[source]
match.find(Pyjoyment.Controller.new(), method='GET', path='/')

Match controller and options against root to find an appropriate endpoint.

path_for(*args: Any, **values: Any) → dict[str, Any][source]
info = match.path_for()
info = match.path_for(foo='bar')
info = match.path_for({'foo': 'bar'})
info = match.path_for('named')
info = match.path_for('named', foo='bar')
info = match.path_for('named', {'foo': 'bar'})

Render matching route with parameters into path.

position
position = match.position
match.position = 2

Current position on the stack, defaults to 0.

root
root = match.root
match.root = Pyjoyment.Routes.new()

The root of the route structure, usually a Pyjoyment.Routes object.

stack: list[dict[str, Any]]
stack = match.stack
match.stack = [{'action': 'foo'}, {'action': 'bar'}]

Captured parameters with nesting history.