下面给出两段“增量模型”的示例代码,均用 Python 编写,方便直接运行、对比。
- 第一段:演示“增量式”把新功能(增量)不断塞进同一个可执行文件里——侧重“过程”。
- 第二段:用 pytest + Git 标签演示“每个增量都是一个可测试、可发布的版本”——侧重“结果”(可交付物)。
你可以任选其一跑通,或两段都跑。
1️⃣ 过程示例:在线购物系统(增量 1→3)
目录结构
shopping/
├─ increment1.py # 增量 1:商品浏览
├─ increment2.py # 增量 2:加入购物车
├─ increment3.py # 增量 3:下单 & 支付
└─ main.py # 统一入口,按增量号动态加载
increment1.py
# 增量 1:商品浏览
def run(store):
print("\n=== 增量 1:商品浏览 ===")
for sku, price in store.items():
print(f"{sku}: ¥{price}")
increment2.py
# 增量 2:购物车
CART = {}
def add_to_cart(store, sku, qty=1):
if sku not in store:
print("商品不存在")
return
CART[sku] = CART.get(sku, 0) + qty
print(f"已添加 {sku} × {qty}")
def run(store):
print("\n=== 增量 2:购物车 ===")
while True:
sku = input("输入商品SKU加入购物车(空回车结束):").strip()
if not sku:
break
add_to_cart(store, sku)
print("当前购物车:", CART)
increment3.py
# 增量 3:下单 & 支付
from increment2 import CART
def checkout(store):
total = 0
for sku, qty in CART.items():
total += store[sku] * qty
print("\n=== 结算 ===")
print("订单明细:", CART)
print(f"应付金额:¥{total}")
confirm = input("确认支付?(y/n) ").lower()
if confirm == 'y':
print("支付成功!")
else:
print("已取消")
def run(store):
if not CART:
print("\n购物车为空")
return
checkout(store)
main.py
import sys
import importlib
# 商品库
STORE = {"apple": 5, "banana": 3, "pear": 4}
if __name__ == "__main__":
# 从命令行读取要演示的增量号 1/2/3
inc = int(sys.argv[1]) if len(sys.argv) > 1 else 1
module = importlib.import_module(f"increment{inc}")
module.run(STORE)
运行方式
python main.py 1 # 只看商品
python main.py 2 # 可加入购物车
python main.py 3 # 最终结算
每加 1 个数字,就“多交付一个增量”。
2️⃣ 结果示例:pytest + Git 标签,每个增量一个可发布版本
目录结构
calculator/
├─ calc.py
├─ test_calc.py
└─ README.md
增量 1:支持加法
calc.py
def add(a, b):
return a + b
test_calc.py
from calc import add
def test_add():
assert add(2, 3) == 5
提交并打标签
git init
git add .
git commit -m "增量1:加法"
git tag v1.0-inc1
增量 2:支持减法
calc.py
def add(a, b):
return a + b
def sub(a, b):
return a - b
test_calc.py
from calc import add, sub
def test_add(): ...
def test_sub():
assert sub(5, 3) == 2
提交 & 标签
git add .
git commit -m "增量2:减法"
git tag v1.0-inc2
增量 3:支持乘除
calc.py
def add(a, b): ...
def sub(a, b): ...
def mul(a, b): return a * b
def div(a, b):
if b == 0:
raise ValueError("除零错误")
return a / b
test_calc.py
...
def test_mul(): ...
def test_div(): ...
提交 & 标签
git commit -am "增量3:乘除"
git tag v1.0-inc3
验证
pytest # 所有增量测试一次性通过
git checkout v1.0-inc2 # 回到“只交付加/减”的版本
pytest # 仅运行当时的测试
🎯 小结
- 第一段代码演示“增量模型”的过程——每跑一个数字就完成一次增量开发。
- 第二段代码演示“增量模型”的结果——每个 Git 标签就是一个可交付的增量版本。
你可以把两段思路结合:用第一段的方法开发,用第二段的方式管理版本。
“Code”(代码)是计算机科学中用于编写程序的指令集合,它通过特定的编程语言规则,让计算机能够理解并执行相应的操作。以下从几个关键方面为你详细介绍:
一、代码的核心作用
- 实现功能:无论是手机APP、网站、游戏还是智能设备的控制逻辑,都需要通过代码来编写具体功能。例如,微信的消息发送功能、抖音的视频播放逻辑,背后都是大量代码在运行。
- 控制流程:代码可以定义计算机执行任务的顺序,比如“如果用户点击按钮,就显示弹窗”“重复执行某个操作直到满足条件”等。
- 数据处理:对输入的数据进行计算、存储、筛选等操作,比如Excel中的公式本质上是简化的代码逻辑,而大数据分析平台则依赖复杂代码处理海量信息。
二、常见的编程语言及应用场景
不同的编程语言有各自的设计特点,适用于不同场景:
编程语言 | 典型应用场景 | 特点 |
---|---|---|
Python | 数据分析、人工智能、自动化脚本、Web开发 | 语法简洁,可读性强,库丰富,适合入门 |
JavaScript | 网页交互、前端开发、Node.js后端开发 | 浏览器默认支持,能让网页“动起来” |
Java | 安卓APP、企业级应用、大型系统 | 跨平台性好,稳定性高,应用广泛 |
C/C++ | 操作系统、游戏引擎、嵌入式开发 | 运行速度快,接近硬件底层 |
SQL | 数据库操作(查询、插入、修改数据) | 专门用于管理关系型数据库的语言 |
三、代码的基本组成
以Python为例,简单代码通常包含:
- 变量:存储数据的容器,如
name = "豆包"
中,name
是变量,存储了字符串“豆包”。 - 函数:实现特定功能的代码块,如
print("Hello World")
中,print
是内置函数,用于输出内容。 - 条件语句:根据条件执行不同代码,如:
age = 18 if age >= 18: print("成年") else: print("未成年")
- 循环语句:重复执行代码,如打印1到5:
for i in range(1, 6): print(i)
四、学习代码的意义
- 解决实际问题:通过编程自动化重复工作(如批量处理文件)、开发工具提升效率。
- 理解数字世界:了解软件和互联网产品的运行原理,增强对技术的掌控力。
- 职业发展:编程是IT、人工智能、数据分析等领域的核心技能,需求广泛。
如果需要具体编程语言的示例或学习资源,可以进一步说明你的需求哦!
To indicate a span of code, wrap it with backtick quotes (`). Unlike a pre-formatted code block, a code span indicates code within a normal paragraph. For example:
Use the printf()
function.
will produce:
Use the printf()
function.
To include a literal backtick character within a code span, you can use multiple backticks as the opening and closing delimiters:
There is a literal backtick (`) here.
which will produce this:
There is a literal backtick (`) here.
The backtick delimiters surrounding a code span may include spaces — one after the opening, one before the closing. This allows you to place literal backtick characters at the beginning or end of a code span:
A single backtick in a code span: `
A backtick-delimited string in a code span: `foo`
will produce:
A single backtick in a code span: `
A backtick-delimited string in a code span: `foo`
With a code span, ampersands and angle brackets are encoded as HTML entities automatically, which makes it easy to include example HTML tags. Markdown will turn this:
Please don’t use any <blink>
tags.
into:
Please don't use any <blink>
tags.
You can write this:
—
is the decimal-encoded equivalent of —
.
to produce:
—
is the decimal-encoded
equivalent of —
.
Images
Admittedly, it’s fairly difficult to devise a “natural” syntax for placing images into a plain text document format.
Markdown uses an image syntax that is intended to resemble the syntax for links, allowing for two styles: inline and reference.
Inline image syntax looks like this:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IiAhMVTD-1587811807046)(/path/to/img.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AZLO7eGW-1587811807049)(/path/to/img.jpg “Optional title”)]
That is:
An exclamation mark: !;
followed by a set of square brackets, containing the alt attribute text for the image;
followed by a set of parentheses, containing the URL or path to the image, and an optional title attribute enclosed in double or single quotes.
Reference-style image syntax looks like this:
Where “id” is the name of a defined image reference. Image references are defined using syntax identical to link references:
As of this writing, Markdown has no syntax for specifying the dimensions of an image; if this is important to you, you can simply use regular HTML tags.