← 返回题库
初级

RESTful资源路径规范验证

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    users_csv = open_url("https://data.zuihe.com/dbd/ms-shop/state_00/users.csv").read()
    import re
    bad_paths = ['/getUser', '/createOrder', '/deleteProduct/1', '/user_list', '/GetProducts']
    def is_restful(path):
        return bool(re.match(r'^/[a-z]+s(/\d+)?(/[a-z]+)*$', path))
    def suggest(path):
        resource = re.sub(r'[A-Z]', lambda m: '_'+m.group().lower(), path.lstrip('/'))
        resource = resource.replace('get_','').replace('create_','').replace('delete_','')
        return '/' + resource + 's' if not resource.endswith('s') else '/' + resource
    for p in bad_paths:
        print(f"  {p} -> RESTful: {is_restful(p)}, 建议: {suggest(p)}")

示例

输入
solve()
期望输出
/getUser -> RESTful: False, 建议: /users
  /createOrder -> RESTful: False, 建议: /orders
  /deleteProduct/1 -> RESTful: False, 建议: /product/1s
  /user_list -> RESTful: False, 建议: /user_lists
  /GetProducts -> RESTful: False, 建议: /_products
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码