Trên GitHub, sanic-org/sanic-routing đã đạt 16 sao, nhóm Backend, ngôn ngữ Python. Internal handler routing for Sanic beginning with v21.3.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
We can can see the code that the router has generated for us. It is available as a string at router.find_route_src.
def find_route(path, method, router, basket, extra):
parts = tuple(path[1:].split(router.delimiter))
num = len(parts)
# node=1 // part=__dynamic__:str
if num == 1: # CHECK 1
try:
basket['__matches__'][0] = str(parts[0])
except ValueError:
pass
else:
# Return 1
return router.dynamic_routes[('<__dynamic__:str>',)][0], basket
raise NotFound
FYI: If you are on Python 3.9, you can see a representation of the source after compilation at router.find_route_src_compiled
What's it doing?
Therefore, in general implementation requires you to:
Define a router with a get method;
Add one or more routes;
Finalize the router (router.finalize()); and
Call the router's get method.
NOTE: You can call router.finalize(False) if you do not want to compile the source code into executable form. This is useful if you only intend to review the generated output.
Every time you call router.add you create one (1) new Route instance. Even if that one route is created with multiple methods, it generates a single instance. If you add() another Route that has a similar path structure (but, perhaps has differen methods) they will be grouped together into a RouteGroup. It is worth also noting that a RouteGroup is created the first time you call add(), but subsequent similar routes will reuse the existing grouping instance.
When you call finalize(), it is taking the defined route groups and arranging them into "nodes" in a hierarchical tree. A single node is a path segment. A Node instance can have one or more RouteGroup on it where the Node is the termination point for that path.
def find_route(path, method, router, basket, extra):
parts = tuple(path[1:].split(router.delimiter))
num = len(parts)
# node=1 // part=path
if num > 1: # CHECK 1
if parts[0] == "path": # CHECK 4
# node=1.1 // part=to
if num > 2: # CHECK 1
if parts[1] == "to": # CHECK 4
# node=1.1.1 // part=different
if num > 3: # CHECK 1
if parts[2] == "different": # CHECK 4
# node=1.1.1.1 // part=__dynamic__:str
if num == 4: # CHECK 1
try:
basket['__matches__'][3] = str(parts[3])
except ValueError:
pass
else:
if method in frozenset({'one', 'two'}):
route_idx = 0
elif method in frozenset({'BASE'}):
route_idx = 1
else:
raise NoMethod
# Return 1.1.1.1
return router.dynamic_routes[('path', 'to', 'different', '<__dynamic__:str>')][route_idx], basket
# node=1.1.2 // part=__dynamic__:int
if num >= 3: # CHECK 1
try:
basket['__matches__'][2] = int(parts[2])
except ValueError:
pass
else:
if num == 3: # CHECK 5
# Return 1.1.2
return router.dynamic_routes[('path', 'to', '<__dynamic__:int>')][0], basket
# node=1.1.3 // part=__dynamic__:str
if num >= 3: # CHECK 1
try:
basket['__matches__'][2] = str(parts[2])
except ValueError:
pass
else:
if num == 3: # CHECK 5
# Return 1.1.3
return router.dynamic_routes[('path', 'to', '<__dynamic__:str>')][0], basket
raise NotFound
Special cases
The above example only shows routes that have a dynamic path segment in them (example: <foo>). But, there are other use cases that are covered differently:
fully static paths - These are paths with no parameters (example: /user/login). These are basically matched against a key/value store.
regex paths - If a route as a single regular expression match, then the whole route will be matched via regex. In general, this happens inline not too dissimilar than what we see in the above example.
special regex paths - The router comes with a special path type (example: <foo:path>) that can match on an expanded delimiter. This is also true for any regex that uses the path delimiter in it. These cannot be matched in the normal course since they are of unknown length.
sanic-org/sanic-routing thuộc nhóm Backend trên TopGit, cùng 4 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về sanic-org/sanic-routing ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/sanic-org/sanic-routing là nguồn chính thức.
sanic-org/sanic-routing có bao nhiêu sao?
sanic-org/sanic-routing có 16 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/sanic-org/sanic-routing. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
sanic-org/sanic-routing có những chủ đề gì?
GitHub topics của sanic-org/sanic-routing: "python", "routing", "sanic", "webframework". TopGit xếp repo vào nhóm Backend.
sanic-org/sanic-routing có phải mã nguồn mở không?
Có — sanic-org/sanic-routing phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/sanic-org/sanic-routing.
sanic-org/sanic-routing có trang demo không?
Dự án có trang chủ ở https://sanicframework.org/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
sanic-org/sanic-routing còn đang phát triển không?
Commit gần nhất trên sanic-org/sanic-routing là 2.6 năm trước (theo timestamp GitHub). Repo có 10 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
sanic-routing có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về sanic-routing.