在 python 中使用不带正则表达式的给定字符串从定义的模式列表中提取模式
Extraction pattern from a defined pattern list using a given string without regex in python
我目前正在创建一个支持 RESTful 的 HTTP 服务器。
为了支持像 /user/:user/:id 这样的 url,我创建了一种从 url.
中查找端点的机制
我当前的代码:
self.index_array= [
"POST-/foo/__",
"GET-/user/__/__"
]
self.index = {
"/foo/__": ["POST"],
"/user/__/__": ["GET"]
}
self.endpoints = [EndPointClass(...), EndPointClass(...)]
def get(self, method, path):
# Static
if path in self.index and method in self.index[path]:
return self.endpoints[self.index_array.index(method + "-" + path)]
# Dynamic
parts = path.split("/")
for a in parts:
test = path.replace(a, "__", 1)
if method + "-" + test in self.index_array:
return self.endpoints[self.index_array.index(method + "-" + test)]
上述代码可以像 /foo/bar 那样路由路径,但不能像 /user/username/1234 那样路由具有多个路径参数的路径。
我正在考虑将其拆分为方法并递归处理,但是随着路径嵌套的加深,这种想法会减慢执行速度。
此外,我不使用正则表达式的原因是因为我认为使用正则表达式会导致可怕的回溯并减慢进程。
谁能告诉我如何路由包含多个路径参数的端点?
我正在使用翻译器,所以如果您有任何问题,请发表评论,我会尽力回答。如果您有任何问题或意见,请给我留言。
谢谢。
有一百万种方法可以做到这一点,只要让你自己简单一点。您可以使用字典将 index_array 变成 index_tree,例如:
index_tree = {
"POST":{"foo":{"endpoint":1}}
"GET": {
"user":{
"username": {"endpoint":2},
"email":{"endpoint":5}
},
"bar":{
"DirtyGoose": {"endpoint":3},
"3HornUnicorn": {"endpoint":4}
}
}
}
然后在拆分后简单地导航它们:
nodes = path.split()
cur = index_tree
for i, node in enumerate(nodes):
if "endpoint" in cur:
if i == len(nodes) - 1:
return cur["endpoint"], None
return cur["endpoint"], nodes[i+1:] # arguments for your path
if node not in cur:
return None, None
cur = cur[node] # this will go as deep as you want until "endpoint" or incorrect node
但您也可以不拆分来完成:然后保存为所有可能路径的排序列表,然后平分找到一个看起来像您的路径。所有这些都不在路径中 - 扔掉或用作参数。
我目前正在创建一个支持 RESTful 的 HTTP 服务器。 为了支持像 /user/:user/:id 这样的 url,我创建了一种从 url.
中查找端点的机制我当前的代码:
self.index_array= [
"POST-/foo/__",
"GET-/user/__/__"
]
self.index = {
"/foo/__": ["POST"],
"/user/__/__": ["GET"]
}
self.endpoints = [EndPointClass(...), EndPointClass(...)]
def get(self, method, path):
# Static
if path in self.index and method in self.index[path]:
return self.endpoints[self.index_array.index(method + "-" + path)]
# Dynamic
parts = path.split("/")
for a in parts:
test = path.replace(a, "__", 1)
if method + "-" + test in self.index_array:
return self.endpoints[self.index_array.index(method + "-" + test)]
上述代码可以像 /foo/bar 那样路由路径,但不能像 /user/username/1234 那样路由具有多个路径参数的路径。 我正在考虑将其拆分为方法并递归处理,但是随着路径嵌套的加深,这种想法会减慢执行速度。
此外,我不使用正则表达式的原因是因为我认为使用正则表达式会导致可怕的回溯并减慢进程。
谁能告诉我如何路由包含多个路径参数的端点?
我正在使用翻译器,所以如果您有任何问题,请发表评论,我会尽力回答。如果您有任何问题或意见,请给我留言。
谢谢。
有一百万种方法可以做到这一点,只要让你自己简单一点。您可以使用字典将 index_array 变成 index_tree,例如:
index_tree = {
"POST":{"foo":{"endpoint":1}}
"GET": {
"user":{
"username": {"endpoint":2},
"email":{"endpoint":5}
},
"bar":{
"DirtyGoose": {"endpoint":3},
"3HornUnicorn": {"endpoint":4}
}
}
}
然后在拆分后简单地导航它们:
nodes = path.split()
cur = index_tree
for i, node in enumerate(nodes):
if "endpoint" in cur:
if i == len(nodes) - 1:
return cur["endpoint"], None
return cur["endpoint"], nodes[i+1:] # arguments for your path
if node not in cur:
return None, None
cur = cur[node] # this will go as deep as you want until "endpoint" or incorrect node
但您也可以不拆分来完成:然后保存为所有可能路径的排序列表,然后平分找到一个看起来像您的路径。所有这些都不在路径中 - 扔掉或用作参数。