准备自定义模块
先来看一个自定义模块示例:
模块名为my_package,其中包含两个文件:
__init__.py,表明这是一个模块;
# 1 使用 . 表示相对路径
from . import run
def compute(host, database, username, password):
try:
data = run.fetch_data(host, database, username, password)
res = run.do_something(data)
print({"status": res, "msg": "ok"})
except Exception as e:
print(e)
print({"status": "error", "msg": "error info: %s" % e})
# 2 将必要的接口暴露,内部细节隐藏
__all__ = ["compute"]
run.py 这是代码块;
def fetch_data(host, database, username, password):
print("fetch data from database with params:")
print("host : %s, datab