python代码规范¶
代码基本规范:¶
代码风格的规范,需要符合PEP8规范。范例:
#!/user/bin/env python3
# -*- coding: utf-8 -*-
#
# 此文件包含“框架代码”,请确保已获得本公司的许可。
#
# Copyright (c) 2021, 上海掌签信息科技有限公司. 保留所有权.
#
import argparse
import os
import shutil
import yaml2rst
def parse_args() -> None:
"""
获取启动程序时的命令行参数。
"""
parser = argparse.ArgumentParser()
parser.add_argument('--configs_path',
help="Please enter config, example:CONFIGSDIR")
args = parser.parse_args()
return args
def schema2rst() -> None:
configs_path = parse_args().configs_path
if configs_path is None:
configs_path = "configs"
source_config_path = os.path.join(os.getcwd(), "source")
rst_config_path = os.path.join(source_config_path, configs_path)
if os.path.exists(rst_config_path):
shutil.rmtree(rst_config_path)
os.mkdir(rst_config_path)
else:
os.mkdir(rst_config_path)
configs_path = os.path.join(os.path.dirname(os.path.dirname((os.path.abspath(__file__)))), 'configs')
base_path = os.path.join(configs_path, 'base')
for schema in os.listdir(base_path):
if schema.split('.')[-1] == 'schema':
schema_name = f"{schema.split('.')[0]}_schema"
rst_name = f"{schema_name}.rst"
yaml2rst.convert_file(os.path.join(base_path, schema), os.path.join(rst_config_path, rst_name))
if __name__ == '__main__':
schema2rst()
配置文件规范:公司推荐使用yaml和ini文件格式做为配置文件格式,如下:
flak8:
title:palmsion
pylint:
time:night
版本规范:¶
安装包的规范,只能使用公司规定的第三方包。
公司规定只能采用python3.8.8版本的python版本。
只能使用公司规定的第三方包和对应的版本。超出范围的需要邮件申请,邮箱地址为xeky@163.com 申请时写明原因。
flask项目目录结构要求:¶
公司对项目目录结构有一定的要求,需要遵照以下要求:
只有在上述目录结构不能囊括的情况才可以添加相应的目录。比如需要写一个相对独立的工具模块,就可以自行添加一个tools目录。
解析请求须采用RequestParser()方法,因为自动化生成单元测试用例需要解析此处的校验参数。返回参数只能是封装好的api_result(),返回值只有3个key:data, status_code, message。
一个符合标准的接口应该是如下形式的:
class AddInterfaceApi(Resource):
# method_decorators = [jwt_required()]
def post(self):
# username = get_jwt_identity()
parser = RequestParser()
parser.add_argument('name', type=str, required=True, nullable=False,location='json') # 记录route
parser.add_argument('route', type=str, required=True, nullable=False, location='json') # 记录route
parser.add_argument('rule', type=str, required=True, location='json')
parser.add_argument('method', type=str, location='json')
args = parser.parse_args()
interface = InterFace(id=rand_id(), name=args["name"], route=args["route"], rule=args["rule"],
method=args["method"])
db.session.add(interface)
db.session.commit()
return jsonify(api_result("添加接口成功"))
公司可用的包清单:可以直接复制到虚拟环境:¶
alabaster==0.7.12alembic==1.4.2aliyun-python-sdk-core-v3==2.13.11aliyun-python-sdk-kms==2.11.0aliyun-python-sdk-rds==2.5.1aniso8601==8.0.0anyio==3.4.0APScheduler==3.6.3attrs==21.2.0azt==0.0.7Babel==2.8.0beautifulsoup4==4.10.0blinker==1.4bs4==0.0.1certifi==2020.6.20cffi==1.14.2chardet==3.0.4charset-normalizer==2.0.10click==8.0.3cloud-init==19.1.9colorama==0.4.4command-not-found==0.3configobj==5.0.6crcmod==1.7cryptography==3.1Cython==0.29.21dbus-python==1.2.16Deprecated==1.2.13dnspython==2.0.0docutils==0.17.1email-validator==1.1.3fastapi==0.71.0filelock==3.0.12flake8==4.0.1Flask==2.0.2Flask-Admin==1.5.6Flask-APScheduler==1.11.0Flask-BabelEx==0.9.4Flask-CKEditor==0.4.4.1Flask-Cors==3.0.10Flask-Docs==0.1.3Flask-Excel==0.0.7Flask-JWT-Extended==4.3.1Flask-LogConfig==0.4.2Flask-Login==0.5.0Flask-Mail==0.9.1flask-marshmallow==0.14.0Flask-Migrate==2.5.3Flask-Principal==0.4.0flask-redis==0.4.0Flask-RESTful==0.3.9Flask-Script==2.0.6Flask-Security==3.0.0flask-sqlacodegen==1.1.8Flask-SQLAlchemy==2.5.1Flask-WTF==0.14.3greenlet==1.1.2gunicorn==20.1.0h11==0.9.0httptools==0.1.2idna==2.10imagesize==1.3.0inflect==5.3.0iniconfig==1.1.1itsdangerous==2.0.1Jinja2==3.0.3jmespath==0.10.0jsonpatch==1.32jsonpointer==2.1jsonschema==3.2.0jwcrypto==0.6.0language-selector==0.1lml==0.0.9ogconfig==0.4.0logutils==0.3.5Mako==1.1.3MarkupSafe==2.0.1marshmallow==3.14.0marshmallow-sqlalchemy==0.26.1mccabe==0.6.1motor==2.5.1Naked==0.1.31netifaces==0.10.4oauthlib==3.1.1oss2==2.15.0packaging==21.0passlib==1.7.4Pillow==7.2.0platformdirs==2.2.0pluggy==0.13.1pycodestyle==2.8.0pycparser==2.20pycryptodome==3.12.0pydantic==1.9.0pyexcel==0.6.4pyexcel-io==0.5.20pyexcel-webio==0.1.4pyfiglet==0.8.post1pyflakes==2.4.0Pygments==2.11.2PyGObject==3.36.0PyJWT==2.3.0pymacaroons==0.13.0pymongo==3.12.3PyMySQL==1.0.2PyNaCl==1.3.0pyparsing==2.4.7pyrsistent==0.17.3pytest==6.2.4pandas==1.4.1python-dateutil==2.8.1python-editor==1.0.4pytz==2020.1PyYAML==5.3.1pyzt==1.0.1redis==4.0.2requests==2.26.0shellescape==3.8.1simplejson==3.17.2six==1.16.0sniffio==1.2.0snowballstemmer==2.2.0soupsieve==2.2.1speaklater==1.3Sphinx==4.3.0sphinxcontrib-applehelp==1.0.2sphinxcontrib-devhelp==1.0.2sphinxcontrib-htmlhelp==2.0.0sphinxcontrib-jsmath==1.0.1sphinxcontrib-qthelp==1.0.3sphinxcontrib-serializinghtml==1.1.5SQLAlchemy==1.3.24SQLAlchemy-Utils==0.38.2ssh-import-id==5.10starlette==0.17.1termcolor==1.1.0texttable==1.6.3tornado==6.0.4typing-extensions==3.10.0.2tzlocal==2.1urllib3==1.25.10uvicorn==0.11.3uvloop==0.16.0virtualenv==20.7.2websockets==8.1Werkzeug==2.0.2wrapt==1.13.3wsproto==0.15.0WTForms==2.3.3xmltodict==0.12.0zipp==3.6.0
其他规范:¶
每一个项目都要写好readme.md文件,写不同环境切换时需要修改的事项和文件目录,比如:不可或缺的基础数据,切换时需要更改的域名。需要依赖的第三方包或者插件等。
公司的接口请求方式只有两种:get和post。