"""蓝色天空网 mbsky.com: CPython code object and standard-library example."""
import json,types,dis,sqlite3
from contextlib import closing
source="result = sum([2, 3, 5])"
code=compile(source,"<mbsky-owned>","exec");namespace={};exec(code,namespace)
class LearningPlan:
def __init__(self,values):self.values=list(values)
def total(self):return sum(self.values)
with closing(sqlite3.connect(":memory:"))as connection:
connection.execute("CREATE TABLE learning (name TEXT, amount INTEGER)")
connection.executemany("INSERT INTO learning VALUES (?, ?)",[("HTML5",2),("CSS",3),("JavaScript",5)])
sql_total=connection.execute("SELECT SUM(amount) FROM learning").fetchone()[0]
if __name__=="__main__":
print(json.dumps({"brand":"蓝色天空网 · mbsky.com","code_object":isinstance(code,types.CodeType),"own_compiled_result":namespace["result"],"opcode_count":len(list(dis.get_instructions(code))),"oop_total":LearningPlan([2,3,5]).total(),"function_total":sum([2,3,5]),"sqlite_total":sql_total,"database_scope":"own in-memory only","gui_network_c_extensions":"not tested"},ensure_ascii=False))
本例需在 本地 Python 环境 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地 Python 环境
入口文件:projects/python-lab/python3-intro.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
python3-intro.py,或下载右侧课程工程包。 - 确认本地已准备 本地 Python 环境。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202611a463.zip
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202612a292.zip