我正在编写一个 Django 应用程序,并在 VSCode ( ) 中使用以下配置settings.json来自动格式化我的 Python 代码(我也使用 Django VSCode 扩展):
{
"liveshare.authenticationProvider": "GitHub",
"editor.fontSize": 16,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"html.format.endWithNewline": true,
"files.exclude": {
"**/__pycache__": true
},
"explorer.confirmDragAndDrop": false,
"editor.formatOnSave": true,
"git.confirmSync": false,
"window.zoomLevel": -1,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"python.linting.flake8Args": [
"--ignore=E501,E266,W503"
],
"files.associations": {
"**/*.html": "html",
"**/templates/**/*.html": "django-html",
"**/templates/**/*": "django-txt",
"**/requirements{/**,*}.{txt,in}": "pip-requirements",
"*.html": "django-html"
},
"emmet.includeLanguages": {"django-html": "html"},
}
Run Code Online (Sandbox Code Playgroud)
虽然 Python 文件中的格式按预期工作,但它似乎也会干扰我的 Django 模板并破坏它们。
例如,以下模板...
{% extends "base.html" %}
{% load martortags %}
{% …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下结构的项目:
project-name/
|
|___ __init__.py
|___ main.py
|
|___ toolA/
| |___ __init__.py
| |___ toolA.py
|
|___ toolB/
| |___ __init__.py
| |___ toolB.py
|
|___ common/
|___ __init__.py
|___ utils.py
Run Code Online (Sandbox Code Playgroud)
我需要工具 A 和 B(脚本toolA.py和toolB.py)才能导入位于utils.py.
此外,toolA.py和toolB.py应该是工具链的一部分(由 实现main.py),但它们也需要是独立的工具。这就是为什么我决定将它们设为可执行文件,并在它们中都包含一个 shebang:
#!/usr/bin/env python3.7
Run Code Online (Sandbox Code Playgroud)
-m我在执行时找到了一些带有该标志的解决方案python,但由于 shebang,这不是一个选项。有什么办法可以实现这一点吗?我找到了很多帖子和解决方案,但没有一个对我有用。理想情况下,我需要这样的东西,无需任何进一步的配置(如编辑路径等):
project-name/
|
|___ __init__.py
|___ main.py
|
|___ toolA/
| |___ __init__.py
| |___ toolA.py
|
|___ toolB/
| |___ __init__.py
| …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个 RESTful API 应用程序flask_restful,flask_jwt_extended用于用户授权并将flask_limiter用户的配额限制为 6/分钟。我的玩具/测试代码如下(尚未实施实际的授权方案):
from flask import Flask, make_response
from flask_restful import Api, Resource
from flask_limiter import Limiter
from werkzeug.exceptions import HTTPException
from flask_jwt_extended import jwt_required, create_access_token, JWTManager, get_jwt_identity
# custom HTTP exception
class OutOfQuota(HTTPException):
code = 402
name = 'Out Of Quota'
app = Flask(__name__)
limiter = Limiter(app, key_func=get_jwt_identity)
api = Api(prefix='')
class Token(Resource):
def get(self, user):
return make_response({'token': create_access_token(identity=user)})
class Test(Resource):
@jwt_required
@limiter.limit('6/minute')
def get(self):
return make_response({'message': f'OK {get_jwt_identity()}'})
api.add_resource(Token, '/token/<string:user>') …Run Code Online (Sandbox Code Playgroud)