"""蓝色天空网 mbsky.com | Generate an index of existing site-owned practices."""
from html import escape
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent))
from mbsky_pychecks import Checks
PRACTICES = [
("基础语法", "标识符、表达式、字符串和输出", "syntax-quiz.html"),
("运算符", "算术、比较、位运算和逻辑条件", "operators-quiz.html"),
("循环", "遍历、边界、跳过与退出", "loops-quiz.html"),
("函数", "参数、默认值和返回值", "functions-quiz.html"),
("字典", "键的查找、取值和更新", "dictionary-quiz.html"),
("数字与随机", "数值运算、取值边界和随机抽样", "../../partial-preview/fc184ea61e840ca257352792.html"),
("列表与容器", "列表操作、切片、复制和排序", "../../partial-preview/d9cce7a261c66b8e0e72519e.html"),
("文本", "字符、字符串切片与格式化", "../../partial-preview/7494aad7c23d2ffd9d150f49.html"),
("文件和标准流", "读写、文件模式、输入输出", "../../partial-preview/347b97fb88b24d7e69985dc5.html"),
("标准库", "文本匹配、进程、归档与本机协议", "../../partial-preview/97fa701dac14fbda510b7d49.html"),
]
def practice_page():
rows = "".join(
f'<li><a href="{escape(path, quote=True)}">{escape(title)}</a>'
f'<p>{escape(topics)}</p></li>\n' for title, topics, path in PRACTICES
)
return """<!doctype html>
<html lang="zh-CN">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex,nofollow">
<title>Python3 练习入口 · 蓝色天空网</title>
<style>
:root { color-scheme: light; font-family: system-ui, sans-serif; background: #f1f6fb; color: #122b42; }
body { margin: 0; }
main { max-width: 46rem; padding: 2rem; margin: auto; }
h1 { margin: .7rem 0; }
a { color: #155698; text-underline-offset: .2em; }
label { display: block; font-weight: 650; margin: 1.5rem 0 .4rem; }
input, button { padding: .55rem .75rem; font: inherit; border: 1px solid #697f96; border-radius: .4rem; }
input { width: min(20rem, 68%); background: white; }
button { color: #122b42; background: white; cursor: pointer; }
ul { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: .6rem; padding: 0; list-style: none; }
li { padding: .85rem; border: 1px solid #cfdeec; border-radius: .6rem; background: white; }
li p { margin: .3rem 0 0; color: #42596e; }
[hidden] { display: none; }
</style>
<main>
<a href="../../partial-preview/18aeb49b2fd1d034e5f6ed0d.html">返回实例与练习入口章节</a>
<h1>Python3 本站练习入口</h1>
<p>选择已备好的自测或章节练习。筛选只在本页查找,不会发送查询或执行 Python 代码。</p>
<label for="filter">按主题或技能筛选</label>
<input id="filter" type="search" placeholder="例如:循环或排序" autocomplete="off">
<button type="button" id="clear-filter">清除</button>
<p id="count" role="status"></p>
<ul id="practices">
""" + rows + """</ul>
</main>
<script>
"use strict";
const filter = document.getElementById("filter");
const status = document.getElementById("count");
const entries = Array.from(document.querySelectorAll("#practices > li"));
function showMatches() {
const search = filter.value.trim().toLocaleLowerCase();
let visible = 0;
for (const item of entries) {
item.hidden = !item.textContent.toLocaleLowerCase().includes(search);
if (!item.hidden) visible++;
}
status.textContent = visible ? ("可见练习 " + visible + " 项") : "没有匹配的练习";
}
filter.addEventListener("input", showMatches);
document.getElementById("clear-filter").addEventListener("click", () => {
filter.value = "";
showMatches();
filter.focus();
});
showMatches();
</script>
</html>
"""
def main():
checks = Checks()
root = Path(__file__).resolve().parent
checks.eq("practice links unique", len({path for _, _, path in PRACTICES}), len(PRACTICES))
for title, topics, link in PRACTICES:
target = (root / link).resolve()
checks.eq("owned local target " + title, target.is_relative_to(root.parents[1]) and target.is_file(), True)
html = practice_page()
checks.eq("UTF-8 and no external data dependencies",
('charset="utf-8"' in html, "http://" in html or "https://" in html), (True, False))
checks.eq("visible topics are actually authored content", html.count("<li>"), len(PRACTICES))
checks.finish("python3-examples", runtime=sys.version, practice_links=len(PRACTICES),
scope="Existing site-owned practice links only; original linked pages pending")
if __name__ == "__main__":
if sys.argv[1:] == ["--html"]:
print(practice_page(), end="")
else:
main()
本例需在 本地浏览器 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地浏览器
入口文件:projects/python-lab/python3-examples.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
python3-examples.py,或下载右侧课程工程包。 - 确认本地已准备 本地浏览器。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202611a463.zip
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202612a292.zip