From 8e33a16e4f98b26839791373c9fb0c9ae6264bac Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Sun, 26 Nov 2023 16:29:46 +0800 Subject: [PATCH 01/15] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=8B=93=E6=89=91=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../egg.py" | 37 ++++++ .../main.py" | 62 ++++++++++ .../test.py" | 71 +++++++++++ .../tradition.py" | 111 ++++++++++++++++++ .../main.py" | 31 ----- 5 files changed, 281 insertions(+), 31 deletions(-) create mode 100644 "061 \346\213\223\346\211\221\346\216\222\345\272\217/egg.py" create mode 100644 "061 \346\213\223\346\211\221\346\216\222\345\272\217/main.py" create mode 100644 "061 \346\213\223\346\211\221\346\216\222\345\272\217/test.py" create mode 100644 "061 \346\213\223\346\211\221\346\216\222\345\272\217/tradition.py" delete mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/058 \347\224\250\345\206\205\347\275\256\345\272\223\345\256\236\347\216\260\346\213\223\346\211\221\346\216\222\345\272\217/main.py" diff --git "a/061 \346\213\223\346\211\221\346\216\222\345\272\217/egg.py" "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/egg.py" new file mode 100644 index 0000000..2126ed3 --- /dev/null +++ "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/egg.py" @@ -0,0 +1,37 @@ +""" +背景,假如你在用python开发游戏的任务系统 +这个任务系统必须是无环的,否则就出bug了。 +""" + +# 拓扑图 +# 任务id:任务信息 +num2Task = { + 1: { + "name": "开始冒险", + "details": "前往风起之地", + }, + 2: { + "name": "解救云隐村", + "details": "帮助云隐村解决危机", + }, + 3: { + "name": "破晓之星", + "details": "追踪星银之路", + }, + 4: { + "name": "征服风神", + "details": "挑战风神巅峰", + }, + 5: { + "name": "龙之末裔", + "details": "揭开龙的秘密", + }, +} + +graph = { + 1: {2, 3}, + 2: {4}, + 3: {4}, + 4: {5}, + 5: {}, +} diff --git "a/061 \346\213\223\346\211\221\346\216\222\345\272\217/main.py" "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/main.py" new file mode 100644 index 0000000..d1ec85e --- /dev/null +++ "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/main.py" @@ -0,0 +1,62 @@ +from graphlib import TopologicalSorter, CycleError + + +def main(): + # test0() + # test1() + test3() + ... + + +def test0(): + # 返回前驱为0的节点 + ts = TopologicalSorter({ + "D": {"B", "C"}, + "C": {"A"}, + "B": {"A"} + }) + ts.prepare() # 先让它准备一下,看看有没有环 + print(ts.get_ready()) # 找到没有前驱的节点 + + +def test1(): + graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}} + ts = TopologicalSorter(graph) + c = ts.static_order() + print(list(c)[::-1]) + + +def test2(): + try: + ts2 = TopologicalSorter({ + 'a': {'b'}, + 'b': {'c'}, + 'c': {'d'}, + 'd': {'a'}, + }) + print(list(ts2.static_order())) + except CycleError: + ... + + +def test3(): + # 同样的图,换一个图的生成方式,用边生成 + ts = TopologicalSorter() + ts.add('d', 'b') + ts.add('d', 'c') + ts.add('b', 'a') + ts.add('c', 'a') + print(list(ts.static_order())) + ... + + +if __name__ == "__main__": + """ + 自创编译系统检测引入是否合理 + 工作流程管理 + + 在游戏项目中: + 任务系统 关卡设计 任务进度解锁 效果触发 + + """ + main() diff --git "a/061 \346\213\223\346\211\221\346\216\222\345\272\217/test.py" "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/test.py" new file mode 100644 index 0000000..8afcfde --- /dev/null +++ "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/test.py" @@ -0,0 +1,71 @@ +from typing import * + + +def main(): + num2Task = { + 1: { + "name": "构思", + "details": ..., + }, + 2: { + "name": "拍视频", + "details": ..., + }, + 3: { + "name": "找素材", + "details": ..., + }, + 4: { + "name": "剪辑", + "details": ..., + }, + 5: { + "name": "发b站", + "details": ..., + }, + } + graph = { + 1: {2, 3}, + 2: {4}, + 3: {4}, + 4: {5}, + 5: {}, + } + graph = { + 1: {2}, + 2: {3}, + 3: {1}, + } + arr = f(graph) + if len(arr) < len(graph): + print("cycle") + print() + + +def f(graph: Dict[int, Set[int]]) -> List[int]: + from collections import deque + + count_dict = {n: 0 for n in graph} + for node, child_set in graph.items(): + for child in child_set: + count_dict[child] += 1 + + q = deque() + for node, count in count_dict.items(): + if count == 0: + q.append(node) + + res = [] + while q: + n = q.popleft() + res.append(n) + child_set = graph[n] + for child in child_set: + count_dict[child] -= 1 + if count_dict[child] == 0: + q.append(child) + return res + + +if __name__ == '__main__': + main() diff --git "a/061 \346\213\223\346\211\221\346\216\222\345\272\217/tradition.py" "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/tradition.py" new file mode 100644 index 0000000..bfcb271 --- /dev/null +++ "b/061 \346\213\223\346\211\221\346\216\222\345\272\217/tradition.py" @@ -0,0 +1,111 @@ +""" +拓扑排序的传统实现 + +""" + + +def topological_sort(graph): + def dfs(node): + visited[node] = True + for child in graph[node]: + if not visited[child]: + dfs(child) + result.append(node) + + # 初始化 + visited = {node: False for node in graph} + result = [] + + # 遍历图中的每个节点 + for node in graph: + if not visited[node]: + dfs(node) + + # 结果是逆序的,反转得到拓扑排序 + return result[::-1] + + +def f(graph): + # 记录所有的入度量 + from collections import deque + count_graph = {n: 0 for n in graph} + for k, child_set in graph.items(): + for child in child_set: + count_graph[child] += 1 + # 所有入度为0的节点入队列 + q = deque() + for node, count in count_graph.items(): + if count == 0: + q.append(node) + res = [] + # 准备好开始队列了 + while q: + n = q.popleft() + res.append(n) + child_set = graph[n] + for child in child_set: + count_graph[child] -= 1 + if count_graph[child] == 0: + q.append(child) + return res + + +def topological_sort_no_cycle(graph): + # 能检测出是否有环的情况 + def dfs(node): + visited[node] = True + currently_visiting.add(node) + + for child in graph[node]: + if not visited[child]: + if dfs(child): + return True + elif child in currently_visiting: + # 如果在当前DFS路径上发现已经访问过的节点,说明存在环 + return True + + currently_visiting.remove(node) + result.append(node) + return False + + # 初始化 + visited = {node: False for node in graph} + result = [] + currently_visiting = set() + + # 遍历图中的每个节点 + for node in graph: + if not visited[node]: + if dfs(node): + # 如果在DFS中发现环,返回空列表表示拓扑排序不可行 + return [] + + # 结果是逆序的,反转得到拓扑排序 + return result[::-1] + + +def main(): + # 示例用法 + graph = { + 1: {2, 3}, + 2: {4}, + 3: {4, 5}, + 4: {5}, + 5: {}, + } + g = { + 1: {2}, + 2: {3}, + 3: {4}, + 4: {5}, + 5: {1}, + } + + # result = topological_sort_no_cycle(graph) + # result = topological_sort(g) + result = f(graph) + print("Topological Sort:", result) + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/058 \347\224\250\345\206\205\347\275\256\345\272\223\345\256\236\347\216\260\346\213\223\346\211\221\346\216\222\345\272\217/main.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/058 \347\224\250\345\206\205\347\275\256\345\272\223\345\256\236\347\216\260\346\213\223\346\211\221\346\216\222\345\272\217/main.py" deleted file mode 100644 index f1531ce..0000000 --- "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/058 \347\224\250\345\206\205\347\275\256\345\272\223\345\256\236\347\216\260\346\213\223\346\211\221\346\216\222\345\272\217/main.py" +++ /dev/null @@ -1,31 +0,0 @@ -# -*- encoding: utf-8 -*- -""" -PyCharm main -2022年10月19日 -by littlefean -""" -from typing import * -# 蓝桥杯 3.8.6 用不了这个模块 - -import graphlib -from graphlib import TopologicalSorter - - -def main(): - # 拓扑排序 - # graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}} - # print(tuple(ts.static_order())) - - # 检测有向图是否有环 - graph = {1: {2}, 2: {1}} - ts = TopologicalSorter(graph) - try: - print(ts.prepare()) - except graphlib.CycleError: - print("有环") - ... - return None - - -if __name__ == "__main__": - main() From fdd47750f354939507ec97a5355bc990cccc6430 Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Tue, 28 Nov 2023 00:06:18 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=9B=9B?= =?UTF-8?q?=E7=A7=8D=E8=BF=9E=E7=BB=AD=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.py" | 51 +++++++++++++++++++ .../test.py" | 17 +++++++ 2 files changed, 68 insertions(+) create mode 100644 "062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/main.py" create mode 100644 "062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/test.py" diff --git "a/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/main.py" "b/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/main.py" new file mode 100644 index 0000000..0f6d583 --- /dev/null +++ "b/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/main.py" @@ -0,0 +1,51 @@ +# .f().f().f() 的方式 +class MyClass1: + def __init__(self): + self._v = 0 + + def add(self, v): + self._v += v + return self + + +c1 = MyClass1() +c1.add(1).add(1).add(5).add(10) + + +def f(*args, **kwargs): + # 在这里对参数进行判断,进行不同的操作逻辑... + return f + + +f('eat')('sleep')('sing') + + +class MyClass2: + def __init__(self): + self._v = 0 + + def __getitem__(self, item): + self._v += item + return self + + def __call__(self, *args, **kwargs): + # 结尾加一个小括号只是消除pycharm的警告 + return self + + +c2 = MyClass2() +c2[1][1][2][10][5][6]() + + +# .a.a.a的方式 +class A: + @property + def b(self): + return self + + def __call__(self, *args, **kwargs): + return self + + +a = A() +a.b.b.b.b.b.b.b() diff --git "a/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/test.py" "b/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/test.py" new file mode 100644 index 0000000..ac77f7c --- /dev/null +++ "b/062 \345\233\233\347\247\215\351\223\276\345\274\217\350\260\203\347\224\250/test.py" @@ -0,0 +1,17 @@ +class Calculator: + def __init__(self): + self._value = 0 + + def __getattr__(self, item): + return self + + def __truediv__(self, other): + return self + + def __floordiv__(self, other): + return self + + +https = www = index = Calculator() + +https//www.littlefean.com/index.html From 5d44d60851689834d04f7d91bd8f3edf58f8eea7 Mon Sep 17 00:00:00 2001 From: shikukuya <89395757+shikukuya@users.noreply.github.com> Date: Sun, 3 Dec 2023 12:24:19 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmap=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/map.yml | 2 +- map.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/map.yml b/.github/workflows/map.yml index 841452a..b4ae4b5 100644 --- a/.github/workflows/map.yml +++ b/.github/workflows/map.yml @@ -30,6 +30,6 @@ jobs: - name: 推送 uses: actions-js/push@master with: - github_token: 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 + github_token: ${{ secrets.GITHUB_TOKEN }} message: "Actions: 更新map.md" branch: master diff --git a/map.py b/map.py index 82eeadf..c260a27 100644 --- a/map.py +++ b/map.py @@ -8,16 +8,13 @@ pn = 1 while True: + # 2023-12-03 更新api resp = get( - "https://api.bilibili.com/x/space/arc/search", - params={ - "mid": "480804525", # uid - "ps": 50, # 每页数量 - "pn": pn # 第几页 - }, + "https://api.bilibili.com/x/space/wbi/arc/search", + params={"mid": "480804525", "ps": 50, "pn": pn}, # uid # 每页数量 # 第几页 headers={ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42" - } + }, ).json() vlist = resp["data"]["list"]["vlist"] if len(vlist) > 0: @@ -32,7 +29,9 @@ for dir in sorted(listdir(), key=lambda x: x[:3]): if dir[0].isdigit(): - result.append(f"| [{dir.split()[0]}](./{dir.replace(' ', '%20')}) | {dir[4:].replace('_', '.')} | [{videos[dir[:3]]}](https://www.bilibili.com/video/{videos[dir[:3]]}) |") + result.append( + f"| [{dir.split()[0]}](./{dir.replace(' ', '%20')}) | {dir[4:].replace('_', '.')} | [{videos[dir[:3]]}](https://www.bilibili.com/video/{videos[dir[:3]]}) |" + ) print("write to map.md") From c02558dc33865477c0118ff8b25508c74b011304 Mon Sep 17 00:00:00 2001 From: shikukuya <89395757+shikukuya@users.noreply.github.com> Date: Sun, 3 Dec 2023 12:51:58 +0800 Subject: [PATCH 04/15] =?UTF-8?q?b=E7=AB=99=E6=8E=A5=E5=8F=A3=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=EF=BC=8Cmap=E5=BA=9F=E5=BC=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/map.yml | 35 --------------------- map.md | 65 --------------------------------------- map.py | 41 ------------------------ 3 files changed, 141 deletions(-) delete mode 100644 .github/workflows/map.yml delete mode 100644 map.md delete mode 100644 map.py diff --git a/.github/workflows/map.yml b/.github/workflows/map.yml deleted file mode 100644 index b4ae4b5..0000000 --- a/.github/workflows/map.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: 更新map - -on: - push: - branches: ["master"] - -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: 安装python3.9 - uses: actions/setup-python@v3 - with: - python-version: "3.9" - - name: 安装依赖 - run: | - python -m pip install --upgrade pip - pip install requests - - name: 运行map.py - run: | - python map.py - - name: 设置git邮箱和姓名 - run: | - git config --global user.email "github-actions[bot]@users.noreply.github.com" - git config --global user.name "github-actions[bot]" - - name: 合并分支 - run: | - git merge - - name: 推送 - uses: actions-js/push@master - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - message: "Actions: 更新map.md" - branch: master diff --git a/map.md b/map.md deleted file mode 100644 index 4f4b7fe..0000000 --- a/map.md +++ /dev/null @@ -1,65 +0,0 @@ -# 各个文件夹代表的视频 - -> 这个文件由 [此脚本](./map.py) 自动生成,并且在每次提交时自动运行 - -| 编号 | 标题 | 视频 | -| - | - | - | -| [000](./000%20巧用编辑器pyCharm) | 巧用编辑器pyCharm | [BV1pd4y1S7oe](https://www.bilibili.com/video/BV1pd4y1S7oe) | -| [001](./001%20一行代码二维数组) | 一行代码二维数组 | [BV1V5411d7kb](https://www.bilibili.com/video/BV1V5411d7kb) | -| [002](./002%20玩转推导式) | 玩转推导式 | [BV1qZ4y1h7in](https://www.bilibili.com/video/BV1qZ4y1h7in) | -| [003](./003%20虚数妙用) | 虚数妙用 | [BV1oY4y167Qb](https://www.bilibili.com/video/BV1oY4y167Qb) | -| [004](./004%20pass和点点点) | pass和点点点 | [BV1wZ4y1a7qi](https://www.bilibili.com/video/BV1wZ4y1a7qi) | -| [005](./005%20函数注解) | 函数注解 | [BV1qS4y1h7ee](https://www.bilibili.com/video/BV1qS4y1h7ee) | -| [006](./006%20help函数) | help函数 | [BV1i34y1h7HK](https://www.bilibili.com/video/BV1i34y1h7HK) | -| [007](./007%20eval函数) | eval函数 | [BV1r5411d7y2](https://www.bilibili.com/video/BV1r5411d7y2) | -| [008](./008%20多变量交换与拆包) | 多变量交换与拆包 | [BV12Y4y1b7b9](https://www.bilibili.com/video/BV12Y4y1b7b9) | -| [009](./009%20装饰器的本质) | 装饰器的本质 | [BV1sT4y1679u](https://www.bilibili.com/video/BV1sT4y1679u) | -| [010](./010%20极速递归) | 极速递归 | [BV1TB4y1y7Ck](https://www.bilibili.com/video/BV1TB4y1y7Ck) | -| [011](./011%20三个实用位运算) | 三个实用位运算 | [BV14Y411c7v1](https://www.bilibili.com/video/BV14Y411c7v1) | -| [012](./012%20元组小逗号) | 元组小逗号 | [BV1qa411J7h3](https://www.bilibili.com/video/BV1qa411J7h3) | -| [013](./013%20Counter做元素统计) | Counter做元素统计 | [BV1yT4y1B7zq](https://www.bilibili.com/video/BV1yT4y1B7zq) | -| [014](./014%20deque代替list) | deque代替list | [BV1NT4y1z7Km](https://www.bilibili.com/video/BV1NT4y1z7Km) | -| [015](./015%20三目运算) | 三目运算 | [BV1ht4y1x7bL](https://www.bilibili.com/video/BV1ht4y1x7bL) | -| [016](./016%20类型转化大全) | 类型转化大全 | [BV1Mr4y1t76B](https://www.bilibili.com/video/BV1Mr4y1t76B) | -| [017](./017%20翻转序列) | 翻转序列 | [BV14t4y1s7GN](https://www.bilibili.com/video/BV14t4y1s7GN) | -| [018](./018%20内置排序和自定义排序) | 内置排序和自定义排序 | [BV1234y1j7uX](https://www.bilibili.com/video/BV1234y1j7uX) | -| [019](./019%20数字大总结) | 数字大总结 | [BV16g411R7Bo](https://www.bilibili.com/video/BV16g411R7Bo) | -| [020](./020%20try%20catch完整写法) | try catch完整写法 | [BV1f5411D7Xg](https://www.bilibili.com/video/BV1f5411D7Xg) | -| [021](./021%20跳出多层循环) | 跳出多层循环 | [BV1h34y1L7iR](https://www.bilibili.com/video/BV1h34y1L7iR) | -| [022](./022%20赋值、复制、深拷贝) | 赋值、复制、深拷贝 | [BV1k5411Q7uf](https://www.bilibili.com/video/BV1k5411Q7uf) | -| [023](./023%20无穷大的应用inf) | 无穷大的应用inf | [BV1MY4y157Vu](https://www.bilibili.com/video/BV1MY4y157Vu) | -| [024](./024%20默认字典) | 默认字典 | [BV1294y117HM](https://www.bilibili.com/video/BV1294y117HM) | -| [025](./025%20切片和自定义切片) | 切片和自定义切片 | [BV1ar4y1V7kL](https://www.bilibili.com/video/BV1ar4y1V7kL) | -| [026](./026%20组合数combination) | 组合数combination | [BV1Va411L76e](https://www.bilibili.com/video/BV1Va411L76e) | -| [027](./027%20两种打印彩色文字的方式) | 两种打印彩色文字的方式 | [BV1Kr4y1V7yi](https://www.bilibili.com/video/BV1Kr4y1V7yi) | -| [028](./028%20product展平多重循环) | product展平多重循环 | [BV1zB4y1Q7zv](https://www.bilibili.com/video/BV1zB4y1Q7zv) | -| [029](./029%20小顶堆heapq) | 小顶堆heapq | [BV15L4y1N7mc](https://www.bilibili.com/video/BV15L4y1N7mc) | -| [030](./030%20用any、all对序列进行与或判断) | 用any、all对序列进行与或判断 | [BV1VB4y1q79m](https://www.bilibili.com/video/BV1VB4y1q79m) | -| [031](./031%20高精度小数运算-有理数和Decimal) | 高精度小数运算-有理数和Decimal | [BV1d94y1y7xo](https://www.bilibili.com/video/BV1d94y1y7xo) | -| [032](./032%20字符串f-string) | 字符串f-string | [BV1WY4y1E7xo](https://www.bilibili.com/video/BV1WY4y1E7xo) | -| [033](./033%20字符串转义字符总结) | 字符串转义字符总结 | [BV1ba411Q7JB](https://www.bilibili.com/video/BV1ba411Q7JB) | -| [034](./034%20字符串编码问题与编码转换) | 字符串编码问题与编码转换 | [BV1jY4y177WG](https://www.bilibili.com/video/BV1jY4y177WG) | -| [036](./036%20面向对象-基本理论) | 面向对象-基本理论 | [BV1pW4y1S7LY](https://www.bilibili.com/video/BV1pW4y1S7LY) | -| [037](./037%20面向对象-用dataclass简写类) | 面向对象-用dataclass简写类 | [BV1ZT411E75Z](https://www.bilibili.com/video/BV1ZT411E75Z) | -| [038](./038%20面向对象-类方法类属性优化类) | 面向对象-类方法类属性优化类 | [BV1kU4y1D7su](https://www.bilibili.com/video/BV1kU4y1D7su) | -| [040](./040%20面向对象-__name__,__class__) | 面向对象-..name..,..class.. | [BV1JG411s74V](https://www.bilibili.com/video/BV1JG411s74V) | -| [042](./042%20组合数据类型只读化) | 组合数据类型只读化 | [BV1XV4y1n7QE](https://www.bilibili.com/video/BV1XV4y1n7QE) | -| [043](./043%20面向对象-slots) | 面向对象-slots | [BV1vr4y1E7pa](https://www.bilibili.com/video/BV1vr4y1E7pa) | -| [044](./044%20面向对象-静态方法封装类) | 面向对象-静态方法封装类 | [BV1XS4y1E7ae](https://www.bilibili.com/video/BV1XS4y1E7ae) | -| [045](./045%20下划线保护类里面的资源) | 下划线保护类里面的资源 | [BV1Hr4y1E7Mg](https://www.bilibili.com/video/BV1Hr4y1E7Mg) | -| [045](./045%20面向对象-更安全地保护属性) | 面向对象-更安全地保护属性 | [BV1Hr4y1E7Mg](https://www.bilibili.com/video/BV1Hr4y1E7Mg) | -| [046](./046%20面向对象-类装饰器、装饰类的装饰器) | 面向对象-类装饰器、装饰类的装饰器 | [BV1PS4y1J7GG](https://www.bilibili.com/video/BV1PS4y1J7GG) | -| [046](./046%20面向对象-python变C艹) | 面向对象-python变C艹 | [BV1PS4y1J7GG](https://www.bilibili.com/video/BV1PS4y1J7GG) | -| [047](./047%20面向对象-魔术方法大总结) | 面向对象-魔术方法大总结 | [BV1b34y1H77x](https://www.bilibili.com/video/BV1b34y1H77x) | -| [049](./049%20面向对象-继承问题) | 面向对象-继承问题 | [BV1j34y1H76A](https://www.bilibili.com/video/BV1j34y1H76A) | -| [050](./050%20面向对象-抽象类) | 面向对象-抽象类 | [BV1ur4y1E7Di](https://www.bilibili.com/video/BV1ur4y1E7Di) | -| [051](./051%20面向对象-获取多继承顺序) | 面向对象-获取多继承顺序 | [BV1Pa411Q78f](https://www.bilibili.com/video/BV1Pa411Q78f) | -| [052](./052%20面向对象-enum枚举) | 面向对象-enum枚举 | [BV123411c7ib](https://www.bilibili.com/video/BV123411c7ib) | -| [053](./053%20面向对象-类里怎么写自己的注解?) | 面向对象-类里怎么写自己的注解? | [BV1ea411Q7M2](https://www.bilibili.com/video/BV1ea411Q7M2) | -| [054](./054%20面向对象-用type元类写类) | 面向对象-用type元类写类 | [BV1iB4y1v7HC](https://www.bilibili.com/video/BV1iB4y1v7HC) | -| [055](./055%20面向对象-用元类写出更离谱的类) | 面向对象-用元类写出更离谱的类 | [BV1jf4y1o7h9](https://www.bilibili.com/video/BV1jf4y1o7h9) | -| [056](./056%20全排列) | 全排列 | [BV1MR4y197BW](https://www.bilibili.com/video/BV1MR4y197BW) | -| [057](./057%20制作音乐musicpy) | 制作音乐musicpy | [BV1ug41187Ro](https://www.bilibili.com/video/BV1ug41187Ro) | -| [058](./058%20用import语句拆文件搞大项目) | 用import语句拆文件搞大项目 | [BV1Ge4y1e7uz](https://www.bilibili.com/video/BV1Ge4y1e7uz) | -| [059](./059%20总结itertools里的骚东西) | 总结itertools里的骚东西 | [BV19V4y1F7if](https://www.bilibili.com/video/BV19V4y1F7if) | -| [060](./060%20玩转闭包) | 玩转闭包 | [BV1Vx4y1u7YX](https://www.bilibili.com/video/BV1Vx4y1u7YX) | diff --git a/map.py b/map.py deleted file mode 100644 index c260a27..0000000 --- a/map.py +++ /dev/null @@ -1,41 +0,0 @@ -from os import listdir -from requests import get -from typing import Dict - -result = [] -format_ = "# 各个文件夹代表的视频\n\n> 这个文件由 [此脚本](./map.py) 自动生成,并且在每次提交时自动运行\n\n| 编号 | 标题 | 视频 |\n| - | - | - |\n{}\n" -videos: Dict[str, str] = {} # example: { "050": "BVxxx" } - -pn = 1 -while True: - # 2023-12-03 更新api - resp = get( - "https://api.bilibili.com/x/space/wbi/arc/search", - params={"mid": "480804525", "ps": 50, "pn": pn}, # uid # 每页数量 # 第几页 - headers={ - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42" - }, - ).json() - vlist = resp["data"]["list"]["vlist"] - if len(vlist) > 0: - for video in vlist: - if video["title"].lower().startswith("【python技巧"): - videos[video["title"][9:12]] = video["bvid"] - pn += 1 - else: - break - -print(videos) - -for dir in sorted(listdir(), key=lambda x: x[:3]): - if dir[0].isdigit(): - result.append( - f"| [{dir.split()[0]}](./{dir.replace(' ', '%20')}) | {dir[4:].replace('_', '.')} | [{videos[dir[:3]]}](https://www.bilibili.com/video/{videos[dir[:3]]}) |" - ) - -print("write to map.md") - -with open("./map.md", "w", encoding="utf-8") as f: - f.write(format_.format("\n".join(result))) - -print("success") From 19b48012af669739cd0612efad8a365a20541519 Mon Sep 17 00:00:00 2001 From: shikukuya <89395757+shikukuya@users.noreply.github.com> Date: Sun, 3 Dec 2023 13:41:32 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=AA=E6=9C=89?= =?UTF-8?q?=E6=88=91=E8=83=BD=E8=BF=90=E8=A1=8C=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 70 +++++++++--------- .../README.md" | 4 + .../main.py" | 8 ++ .../__pycache__/loader.cpython-312.pyc" | Bin 0 -> 890 bytes .../project/loader.py" | 9 +++ .../project/main.py" | 5 ++ .../project/my_utils.bin" | Bin 0 -> 256 bytes .../project/privkey.pem" | 27 +++++++ .../pubkey.pem" | 9 +++ .../real.py" | 2 + 10 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/README.md" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/main.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/__pycache__/loader.cpython-312.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/loader.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/main.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/my_utils.bin" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/privkey.pem" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/pubkey.pem" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/real.py" diff --git a/README.md b/README.md index 91cd499..9b52533 100644 --- a/README.md +++ b/README.md @@ -1,80 +1,78 @@ -# python技巧系列源码 +# python 技巧系列源码 ## 地址 -python技巧🐍系列视频的所有代码,个别章节可能包含图片,ppt,导图文件。 +python 技巧 🐍 系列视频的所有代码,个别章节可能包含图片,ppt,导图文件。 -- 此系列的b站视频链接 https://space.bilibili.com/480804525/channel/collectiondetail?sid=413538 -- *(可通过 [map.md](./map.md) 找视频),githubactions我给的token会在2023-3-30过期,别忘了* -- 此项目的github地址:[Littlefean/SmartPython: python小技巧系列源代码——来自b站视频 (github.com)](https://github.com/Littlefean/SmartPython) -- 此项目的gitee地址:https://gitee.com/littlefean/python-skill-bilibili +- 此系列的 b 站视频链接 https://space.bilibili.com/480804525/channel/collectiondetail?sid=413538 +- _~~(可通过 [map.md](./map.md) 找视频)~~_ **B 站接口变更,map 宣布废弃** +- 此项目的 github 地址:[Littlefean/SmartPython: python 小技巧系列源代码——来自 b 站视频 (github.com)](https://github.com/Littlefean/SmartPython) +- 此项目的 gitee 地址:https://gitee.com/littlefean/python-skill-bilibili ## 说明 -项目中可能会含有计划录但还没有录的代码内容,这部分内容可能涉及剧透。不太建议看,因为如果你已经看了,再看b站视频可能就没啥意思了 `/* 尤其是迷惑行为 */`,但如果你不经常看b站,经常看github,那就都可以了。 +项目中可能会含有计划录但还没有录的代码内容,这部分内容可能涉及剧透。不太建议看,因为如果你已经看了,再看 b 站视频可能就没啥意思了 `/* 尤其是迷惑行为 */`,但如果你不经常看 b 站,经常看 github,那就都可以了。 ## 分类 -三位数的序号是up的视频中对应的标题序号,表示up的更新时间顺序,使用map.py更新map.md时要用到(对应文件夹和b站视频) - -### 🐍语法基础向 +三位数的序号是 up 的视频中对应的标题序号,表示 up 的更新时间顺序,使用 map.py 更新 map.md 时要用到(对应文件夹和 b 站视频) + +### 🐍 语法基础向 1. 003 虚数妙用 2. 005 函数注解 -3. 006 help函数 -4. 007 eval函数 +3. 006 help 函数 +4. 007 eval 函数 5. 009 装饰器的本质 6. 012 元组小逗号 7. 015 三目运算 8. 016 类型转化大全 9. 019 数字大总结 -10. 020 try catch完整写法 -11. 032 字符串f-string +10. 020 try catch 完整写法 +11. 032 字符串 f-string 12. 033 字符串转义字符总结 13. 034 字符串编码问题与编码转换 14. **面向对象**系列 从 036~055 -### 🐍语法技巧向 +### 🐍 语法技巧向 1. 001 一行代码二维数组 2. 002 玩转推导式 -3. 004 pass和点点点 +3. 004 pass 和点点点 4. 008 多变量交换与拆包 5. 021 跳出多层循环 6. 022 赋值、复制、深拷贝 7. 025 切片和自定义切片 -8. 028 product展平多层循环 -9. 030 用any、all对序列进行与或判断 +8. 028 product 展平多层循环 +9. 030 用 any、all 对序列进行与或判断 10. 042 组合数据类型只读化 -### 🐍算法技巧向 +### 🐍 算法技巧向 1. 010 极速递归 2. 011 三个实用位运算 -3. 013 Counter做元素统计 -4. 014 deque代替list +3. 013 Counter 做元素统计 +4. 014 deque 代替 list 5. 017 翻转序列 6. 018 内置排序和自定义排序 -7. 023 无穷大的应用inf +7. 023 无穷大的应用 inf 8. 024 默认字典 -9. 026 组合数combination -10. 029 小顶堆heapq -11. 031 高精度小数运算-有理数和Decimal -12. 056 全排列permutations -13. 059 itertools总结 +9. 026 组合数 combination +10. 029 小顶堆 heapq +11. 031 高精度小数运算-有理数和 Decimal +12. 056 全排列 permutations +13. 059 itertools 总结 -### 🐍应用技巧向 +### 🐍 应用技巧向 1. 027 两种打印彩色文字的方式 -2. 057 编写音乐musicpy -3. 058 import语句拆分文件 +2. 057 编写音乐 musicpy +3. 058 import 语句拆分文件 -### 🐍python迷惑行为 +### 🐍python 迷惑行为 一些没有用的奇怪写法和“寄巧”可能会被收录到迷惑行为中,偏向娱乐和整活。里面开的“同事、老师、同学、舍友”的玩笑请不要当真。同时,里面的一些危险代码操作也不要轻易模仿。 - - ## 做视频的初衷 ### 最早 @@ -87,7 +85,7 @@ python技巧🐍系列视频的所有代码,个别章节可能包含图片,p ### 算法向 -python有很多内置的功能和库,在写算法题的时候能很方便的写好,而其他语言来说就会相对麻烦,尤其是面对leetcode周赛这种拼手速的场合,用python秒杀第一道简单题那是最合适不过了。(有些场合的算法比赛拿一血甚至有特殊奖励)所以up决定总结一下python的所有对算法有利的库和方法。这些方法融汇、散布在这个系列里。 +python 有很多内置的功能和库,在写算法题的时候能很方便的写好,而其他语言来说就会相对麻烦,尤其是面对 leetcode 周赛这种拼手速的场合,用 python 秒杀第一道简单题那是最合适不过了。(有些场合的算法比赛拿一血甚至有特殊奖励)所以 up 决定总结一下 python 的所有对算法有利的库和方法。这些方法融汇、散布在这个系列里。 注:过于依赖骚操作、语言特性、调库、不利于算法起步阶段,初学算法应注重基础。 @@ -95,6 +93,6 @@ python有很多内置的功能和库,在写算法题的时候能很方便的 若有建议可面留个言 -视频对应b站主页: +视频对应 b 站主页: -https://space.bilibili.com/480804525?spm_id_from=333.1007.0.0 +https://space.bilibili.com/480804525 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/README.md" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/README.md" new file mode 100644 index 0000000..b7d1a70 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/README.md" @@ -0,0 +1,4 @@ +1. 生成 rsa 私钥和公钥(私钥必须是 pksc1 格式,公钥必须是 pksc8 格式,不带密码) +2. 在 `real.py` 中编写要加密的代码 +3. 运行 `main.py`,会在 `project/my_utils.bin` 生成加密代码 +4. 在 `project/main.py` 中可以导入 `loader`,通过 `exec(loader.load_module())` 将加密代码导入到当前作用域 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/main.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/main.py" new file mode 100644 index 0000000..b355378 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/main.py" @@ -0,0 +1,8 @@ +import rsa # pip install rsa +from pathlib import Path + +root = Path(__file__).parent +pubkey = rsa.PublicKey.load_pkcs1_openssl_pem((root / "pubkey.pem").read_bytes()) +(root / "project" / "my_utils.bin").write_bytes( + rsa.encrypt((root / "real.py").read_bytes(), pubkey) +) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/__pycache__/loader.cpython-312.pyc" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/__pycache__/loader.cpython-312.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..e12c7432e0403de651d907eaf4bc35a85596651e GIT binary patch literal 890 zcmZWn&ubGw6rR~^vh8kD5yAA(gV1;gNYGnt#fz2}LPN272@7GHoziSK*=2SrY*DGU zloo_Gg0yIbR0V$&Ja|#m&_BS7OAe7y55+d!-U?L@o}AexrL_;{dvD&nnfcx~^En!o z0kc1+Oe8@7e)37v#7409oCQlj2RbxChI_$;1tB9qAOIww&>Gvx&1l*8hcOdIz-T+1 z*^iAA$8#vGhb9$v&dK9zYmHR%u1u>gB3Tb3c~sORGh#+@VM5yBRp|tz;*dthCKVg! zZ;m53X`=!ai&_EE2u5TA7jq5D!Kq<}AvX5zF<1iRV9gMM^&>!guo_E835YpAhqrL%H(_|CN4?=7?5Ku?R9>PE7c@+UTry@ARsY58&eM{=zFdF1 zx>b3&S()|UtZvOc*nTvl`s?rN(ej;R`pSHLuH;Ww{PjhaWhI**pKX`# zsy4O^lq0HXX*$A5+ocK*vjt0^FcEHNEVu;Dc?MJ9yVAbUJKwt^mgD8qWwLzR>o`}7 zb$hYyYOLo|e^t3!Q&OIisw#tD#KAqb2PE1s*Ozt6Ue(NxVE$UPC7T`1n<$&55nIEk zNN_7p%R}tZHG)#erE*3kK3S8yJh`iU{pFx{_F^sG=f(S~@&2kj XFeU9qAWXx>+%DjLIrl5@!JYpA str: + return rsa.decrypt((root / f"{name}.bin").read_bytes(), privkey).decode() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/main.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/main.py" new file mode 100644 index 0000000..e93cd90 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/main.py" @@ -0,0 +1,5 @@ +# 尝试导入utils +import loader + +exec(loader.load_module("my_utils")) +hello("world") diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/my_utils.bin" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\350\277\267\346\203\221\350\241\214\344\270\272/\345\217\252\346\234\211\346\210\221\350\203\275\350\277\220\350\241\214\347\232\204\344\273\243\347\240\201/project/my_utils.bin" new file mode 100644 index 0000000000000000000000000000000000000000..e7993a40c6d77a186ac8b7312e90fc89ad108bf9 GIT binary patch literal 256 zcmV+b0ssER<2bBJFF!4qsT;Jf%~KoMcKxPGV^Vy0mz^T6u_h&{@2UA2DaWXv|4%Mc zFCq1bQ(QQNgkxM!ncvhx_h}M}&;~Ix;i6S?zLqS^udJW#e}*AEQS#D=%{RHeTH0`ODR!sN9@nvfB>s0kqA+Zxn9)=#?>Z#za{Na0-$&;#{a|A^P6pz@${}z%15~v z?L@;acB6(){aF+tMmW5OXoCc47a_jf;+@wC None: + print("Hello", name) From fc9964ac76d3ad8536f3348ffd83f4f6d93edc20 Mon Sep 17 00:00:00 2001 From: shikukuya <89395757+shikukuya@users.noreply.github.com> Date: Sun, 3 Dec 2023 15:28:53 +0800 Subject: [PATCH 06/15] add pathlib --- .gitignore | 3 ++- .../a/a.shit" | 1 + .../a/test1/b.shit" | 1 + .../main.py" | 22 ++++++++++++++++++ .../\346\265\213\350\257\225123/t2.py" | 23 ------------------- 5 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/a.shit" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/test1/b.shit" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/main.py" diff --git a/.gitignore b/.gitignore index d247dcd..97c523a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ test .vscode venv 记录 -测试文件 \ No newline at end of file +测试文件 +*.ignore \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/a.shit" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/a.shit" new file mode 100644 index 0000000..4536b17 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/a.shit" @@ -0,0 +1 @@ +shit \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/test1/b.shit" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/test1/b.shit" new file mode 100644 index 0000000..ee9c51d --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/a/test1/b.shit" @@ -0,0 +1 @@ +shitbbb \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/main.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/main.py" new file mode 100644 index 0000000..4c83664 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/050 pathlib\345\277\253\351\200\237\345\244\204\347\220\206\346\226\207\344\273\266/main.py" @@ -0,0 +1,22 @@ +from pathlib import Path + +# 1. 获取文件所在目录,不是工作目录 +root = Path(__file__).parent +# 2. 遍历所有子文件(夹) +for dir in root.iterdir(): + print(dir.as_posix()) +# 3. glob pattern +for file in root.glob("./**/*.shit"): + print(file.as_posix()) +# 4. 读写文件 +(root / "a" / "a.shit").write_text("shit") +(root / "a" / "test1" / "b.shit").write_text( + (root / "a" / "a.shit").read_text() + "bbb" +) +# 5. 链接 (what == a/a.shit) +""" +error: short read while indexing 视频还没录但计划的/050 pathlib快速处理文件/what +error: 视频还没录但计划的/050 pathlib快速处理文件/what: failed to insert into database +error: unable to index file '视频还没录但计划的/050 pathlib快速处理文件/what' +""" +(root / "what.ignore").hardlink_to(root / "a" / "a.shit") diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\346\265\213\350\257\225123/t2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\346\265\213\350\257\225123/t2.py" index c0b5c46..e69de29 100644 --- "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\346\265\213\350\257\225123/t2.py" +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/\346\265\213\350\257\225123/t2.py" @@ -1,23 +0,0 @@ -# -*- encoding: utf-8 -*- -""" -PyCharm t2 -2022年07月23日 -by littlefean -""" -from typing import * - - -def main(): - def f(n): - return (-1) ** (n * (n - 1) / 2) - - res = "" - for i in range(1, 10000): - res += "-" if f(i) < 0 else "+" - print(res) - - return None - - -if __name__ == "__main__": - main() From f816245aa9bb32cb61fbb6c62f09aff9397a1f4c Mon Sep 17 00:00:00 2001 From: Yuxiao Hua Date: Sat, 9 Dec 2023 11:35:27 +0800 Subject: [PATCH 07/15] =?UTF-8?q?Update=20pycharm=E6=8A=80=E5=B7=A7.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pycharm\346\212\200\345\267\247.md" | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git "a/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" "b/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" index 1e493da..ded6889 100644 --- "a/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" +++ "b/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" @@ -8,25 +8,25 @@ 2. `a+b.pri` 直接回车变成 `print(a+b)` -3. shift + F6 统一修改变量名、函数名、类名甚至是文件夹名字,对应的代码引用会自动更改 +3. `shift + F6` 统一修改变量名、函数名、类名甚至是文件夹名字,对应的代码引用会自动更改 -4. ctrl alt L 自动格式化代码 +4. `ctrl alt L` 自动格式化代码 -5. alt shift ↑ ↓ 上下挪动当前代码行 +5. `alt shift ↑ ↓` 上下挪动当前代码行 -6. alt + 鼠标左键点击代码不同位置:在不同位置点出多个光标,同时写很多的地方 +6. `alt + 鼠标左键点击代码不同位置`:在不同位置点出多个光标,同时写很多的地方 -7. 鼠标中键上下滑动:在不同位置点出多个光标,同时写很多的地方 +7. `鼠标中键上下滑动`:在不同位置点出多个光标,同时写很多的地方 -8. ctrl + tab :在打开的多个代码文件窗口切换 +8. `ctrl + tab` :在打开的多个代码文件窗口切换 -9. ctrl+ /:注释,配合 shift+↑↓选中多行 +9. `ctrl+ /`:注释,配合 shift+↑↓选中多行 -10. ctrl+W:扩散选择 +10. `ctrl+W`:扩散选择 -11. 鼠标拖动文件可以左右分屏,上下分屏、田字格分屏…… +11. `鼠标拖动文件`可以左右分屏,上下分屏、田字格分屏…… -12. ctrl F 搜索 +12. `ctrl F` 搜索 13. 写下 `# todo` 出现蓝色条条,点击右侧滚动栏可以跳转 @@ -36,21 +36,25 @@ 16. Sourcery插件,自动优化代码 -17. 把结果运行代码分屏出去![窗口浮动](D:\LiRen\SmartPython\000 巧用编辑器pyCharm\窗口浮动.png) +17. 把结果运行代码分屏出去 + +![窗口浮动](https://github.com/HuaYuXiao/SmartPython/assets/117464811/4b4adb41-f8b7-47e5-b41d-ff1990e4e382) 18. 在左侧的项目栏里可以直接创建py文件、文件夹、挪动位置 -19. 新创建py文件的时候自动写好代码模板:设置,文件和代码模板,pythonscript![代码模板](D:\LiRen\SmartPython\000 巧用编辑器pyCharm\代码模板.png) +19. 新创建py文件的时候自动写好代码模板:设置,文件和代码模板,pythonscript + +![代码模板](https://github.com/HuaYuXiao/SmartPython/assets/117464811/18d4200e-6eee-48c3-aa45-e0d8532fae8c) 20. 自动快速写代码:设置、编辑器、实时模板 先点加号 - ![快捷代码模板](D:\LiRen\SmartPython\000 巧用编辑器pyCharm\快捷代码模板.png) +![快捷代码模板](https://github.com/HuaYuXiao/SmartPython/assets/117464811/96555389-33ad-4e57-b2a3-9b7822d898bb) 然后写这三个地方 - - ![快捷代码模板 (2)](D:\LiRen\SmartPython\000 巧用编辑器pyCharm\快捷代码模板 (2).png) + +![快捷代码模板 (2)](https://github.com/HuaYuXiao/SmartPython/assets/117464811/8051199c-78f6-402f-afa4-8618e94ed397) 21. debug(个人不怎么用) @@ -58,10 +62,9 @@ 23. def左侧的小+图标可以折叠 -24. ctrl+左键 点击一个变量、函数、类 跳转到相应位置 - -25. 在整个项目里搜索某个文件里是否写过一个单词:ctrl+shift+F +24. `ctrl+左键 点击一个变量、函数、类` 跳转到相应位置 +25. 在整个项目里搜索某个文件里是否写过一个单词:`ctrl+shift+F` ## 补充 @@ -69,25 +72,23 @@ 可以选定一部分代码执行 - ## 一些快捷键 -ctrl alt J 选中当前行 -ctrl alt enter 上方插入行 +`ctrl alt J` 选中当前行 +`ctrl alt enter` 上方插入行 -ctrl shift 上下,大幅度移动某一行 -alt shift 上下,小幅度移动一行 -alt 上下 快速滑动界面 +`ctrl shift 上下`,大幅度移动某一行 +`alt shift 上下`,小幅度移动一行 +`alt 上下` 快速滑动界面 -ctrl R 替换 +`ctrl R` 替换 -ctrl alt 左,跳回到上一次按中建跳转的地方 -ctrl W 扩展选择 +`ctrl alt 左`,跳回到上一次按中建跳转的地方 -ctrl X 删除当前这一行 +ctrl X` 删除当前这一行 -按住alt+鼠标点击,在多个地方同时写 +`按住alt+鼠标点击`,在多个地方同时写 -选定代码块,ctrl alt m,把这一块代码定义成一个函数,ctrl alt p ,把这一部分定义成参数 +`选定代码块,ctrl alt m`,把这一块代码定义成一个函数,`ctrl alt p` ,把这一部分定义成参数 -ctrl alt T 代码插入环绕 \ No newline at end of file +`ctrl alt T` 代码插入环绕 From 44287c59dd31fb758b9a5af92bf1e36c27929b13 Mon Sep 17 00:00:00 2001 From: Yuxiao Hua Date: Sat, 9 Dec 2023 11:36:08 +0800 Subject: [PATCH 08/15] =?UTF-8?q?Update=20pycharm=E6=8A=80=E5=B7=A7.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pycharm\346\212\200\345\267\247.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" "b/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" index ded6889..87947fc 100644 --- "a/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" +++ "b/000 \345\267\247\347\224\250\347\274\226\350\276\221\345\231\250pyCharm/pycharm\346\212\200\345\267\247.md" @@ -48,11 +48,11 @@ 20. 自动快速写代码:设置、编辑器、实时模板 - 先点加号 +先点加号 ![快捷代码模板](https://github.com/HuaYuXiao/SmartPython/assets/117464811/96555389-33ad-4e57-b2a3-9b7822d898bb) - 然后写这三个地方 +然后写这三个地方 ![快捷代码模板 (2)](https://github.com/HuaYuXiao/SmartPython/assets/117464811/8051199c-78f6-402f-afa4-8618e94ed397) From 5eb0ef807452dac1933be22321216ca2fbaad472 Mon Sep 17 00:00:00 2001 From: HuaYuXiao Date: Sat, 9 Dec 2023 12:06:11 +0800 Subject: [PATCH 09/15] update --- .../main.ipynb" | 175 ++++++++++++++++++ .../main.py" | 20 -- 2 files changed, 175 insertions(+), 20 deletions(-) create mode 100644 "001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.ipynb" delete mode 100644 "001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.py" diff --git "a/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.ipynb" "b/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.ipynb" new file mode 100644 index 0000000..f1c92a9 --- /dev/null +++ "b/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.ipynb" @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# 生成10行6列的二维数组\n", + "arr = [[0] * 6 for _ in range(10)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "注意点:最内层可以用乘号,但是外层就要用 `xxx for _ in range(???)` 来复制了\n", + "因为 乘法展开 是用的同一块内存地址\n", + "之所以最内层是可以用乘法的,因为python里的小整数都是共用的同一个内存,所有的`arr[0]` `arr[1]` `arr[2]` `arr[3]` …… 都是同一个内存地址" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0],\n", + " [0, 0, 0, 0, 0, 0]]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2530214936816 2530214936816 2530214936816\n" + ] + } + ], + "source": [ + "a = 1\n", + "b = 1\n", + "c = 1\n", + "print(id(a), id(b), id(c))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(a is b is c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "在Python中,小整数的缓存范围通常是在-5到256之间。这些整数值都在Python的小整数缓存范围内。在这个范围内,相同的整数会被重用,所以它们在内存中的标识是相同的。" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-3 True\n", + "-2 True\n", + "-1 True\n", + "0 True\n", + "1 True\n", + "2 True\n" + ] + } + ], + "source": [ + "for i in range(-3, 3):\n", + " s = f\"n1 = {i}\\n\"\n", + " s += f\"n2 = {i}\\n\"\n", + " s += \"print(i, n1 is n2)\"\n", + " exec(s)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-6 True\n", + "-5 True\n", + "256 True\n", + "257 True\n" + ] + } + ], + "source": [ + "for i in [-6, -5, 256, 257]:\n", + " s = f\"n1 = {i}\\n\"\n", + " s += f\"n2 = {i}\\n\"\n", + " s += \"print(i, n1 is n2)\"\n", + " exec(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在交互式解释器或使用`exec`时,解释器可能对一些整数进行缓存,即使它们超出了通常的缓存范围。这可能是因为解释器在执行时对一些常用的整数进行了特殊处理,以提高性能。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python310", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.py" "b/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.py" deleted file mode 100644 index 574a129..0000000 --- "a/001 \344\270\200\350\241\214\344\273\243\347\240\201\344\272\214\347\273\264\346\225\260\347\273\204/main.py" +++ /dev/null @@ -1,20 +0,0 @@ -# 生成了十行六列的二维数组 -arr = [[0] * 6 for _ in range(10)] - -# 注意点:最内层可以用乘号,但是外层就要用 xxx for _ in range(???) 来复制了 -# 因为 乘法展开 是用的同一块内存地址 -# 之所以最内层是可以用乘法的,因为python里的小整数都是共用的同一个内存,所有的0 1,2 3 …… 都是同一个内存地址 -a = 1 -b = 1 -c = 1 -print(id(a), id(b), id(c)) -# 140718669554512 140718669554512 140718669554512 -print(a is b is c) -for i in range(-10000, 100000): - s = f"n1 = {i}\n" - s += f"n2 = {i}\n" - s += "print(i, n1 is n2)" - exec(s) - -# 结果居然全是True -# 底层机制还有待研究 From 971455128e640bf564d742ec548d9fe26a7fc5bd Mon Sep 17 00:00:00 2001 From: Yuxiao Hua <1628280289@qq.com> Date: Sun, 10 Dec 2023 23:00:01 +0800 Subject: [PATCH 10/15] update --- .../more.ipynb" | 130 ++++++++++++++++++ .../\350\241\245\345\205\205.py" | 20 --- 2 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 "003 \350\231\232\346\225\260\345\246\231\347\224\250/more.ipynb" delete mode 100644 "003 \350\231\232\346\225\260\345\246\231\347\224\250/\350\241\245\345\205\205.py" diff --git "a/003 \350\231\232\346\225\260\345\246\231\347\224\250/more.ipynb" "b/003 \350\231\232\346\225\260\345\246\231\347\224\250/more.ipynb" new file mode 100644 index 0000000..59b2b60 --- /dev/null +++ "b/003 \350\231\232\346\225\260\345\246\231\347\224\250/more.ipynb" @@ -0,0 +1,130 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "PyCharm 复数\n", + "\n", + "2022年10月19日 by littlefean \n", + "\n", + "2023.12.10 updated by YuxiaoHua" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# -*- encoding: utf-8 -*-\n", + "from typing import *\n", + "import cmath" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z1 = (1+0j)\n", + "z2 = 1j\n" + ] + } + ], + "source": [ + "a1 = 1; b1 = 0\n", + "# 用实部 a1 和虚部 b1 创建复数 z1\n", + "z1 = complex(a1, b1) \n", + "print('z1 = ', z1)\n", + "\n", + "a2 = 0; b2 = 1\n", + "# 用实部 a2 和虚部 b2 创建复数 z2\n", + "z2 = complex(a2, b2) \n", + "print('z2 = ', z2)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "phase2 = 1.5707963267948966\n" + ] + } + ], + "source": [ + "# 传入一个虚数,返回这个虚数的弧度\n", + "phase2 = cmath.phase(z2)\n", + "print('phase2 = ', phase2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "angle = 1.5707963267948966\n" + ] + } + ], + "source": [ + "# 快速计算两个向量之间的夹角(弧度制)\n", + "angle = cmath.phase(z2) - cmath.phase(z1)\n", + "print('angle = ', angle)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1.4142135623730951, 0.7853981633974483)\n" + ] + } + ], + "source": [ + "# 长度 , 角度\n", + "z3 = z1 + z2\n", + "print(cmath.polar(z3)) " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/003 \350\231\232\346\225\260\345\246\231\347\224\250/\350\241\245\345\205\205.py" "b/003 \350\231\232\346\225\260\345\246\231\347\224\250/\350\241\245\345\205\205.py" deleted file mode 100644 index a9ac89f..0000000 --- "a/003 \350\231\232\346\225\260\345\246\231\347\224\250/\350\241\245\345\205\205.py" +++ /dev/null @@ -1,20 +0,0 @@ -# -*- encoding: utf-8 -*- -""" -PyCharm 复数 -2022年10月19日 -by littlefean -""" -from typing import * -import cmath - - -def main(): - print(cmath.phase(-1 + 0j)) # 传入一个虚数,返回这个虚数的弧度 - - print(cmath.polar(1 + 1j)) # ===> (长度 , 角度) - - return None - - -if __name__ == "__main__": - main() From 1ea6f9d002216a6c14602c9a129327feaf0d5761 Mon Sep 17 00:00:00 2001 From: Yuxiao Hua <1628280289@qq.com> Date: Sun, 10 Dec 2023 23:19:58 +0800 Subject: [PATCH 11/15] update --- .../show.ipynb" | 228 ++++++++++++++++++ .../show.py" | 61 ----- 2 files changed, 228 insertions(+), 61 deletions(-) create mode 100644 "004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.ipynb" delete mode 100644 "004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.py" diff --git "a/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.ipynb" "b/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.ipynb" new file mode 100644 index 0000000..19024d5 --- /dev/null +++ "b/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.ipynb" @@ -0,0 +1,228 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "PyCharm show\n", + "\n", + "2022年06月17日 by littlefean\n", + "\n", + "2023.12.10 updated by YuxiaoHua" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# -*- encoding: utf-8 -*-\n", + "from typing import *" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "# ... 可以写在注解里面\n", + "def test(a: ..., b: ...) -> ...:\n", + " ...\n", + "\n", + "# 有一个类我还不知道怎么写,我可以先写个占位符\n", + "class A:\n", + " ...\n", + "\n", + "class B:\n", + " pass\n", + "\n", + "def f():\n", + " ...\n", + "\n", + "def g():\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# 写个空循环\n", + "for _ in range(100):\n", + " ..." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "for _ in range(100):\n", + " pass\n", + "\n", + "# 经过测试发现 点点点和pass做空循环速度是一样的。" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "for _ in range(1000):\n", + " ...\n", + " ...\n", + " ...\n", + " ...\n", + "\n", + "# 经过测试发现,这样写四个,并没有出现速度慢了四倍的情况" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在`Python`中,`Ellipsis` 是一个特殊的对象,用来表示省略。它的类型是 `ellipsis` 类型。" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(id(...) == id(Ellipsis))\n", + "\n", + "# 没有小写开头的东西了,因为是单例了,虽然pycharm里这个单词还会变蓝\n", + "# print(id(...) == id(ellipsis)) " + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n" + ] + } + ], + "source": [ + "print(type(...), type(Ellipsis))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "这里的 `Ellipsis.__class__` 返回 `ellipsis` 类型,它表示省略对象的类型。这部分不需要括号,因为我们只是获取类型。\n", + "\n", + "而 `Ellipsis.__class__()` 中的括号是用来创建 `ellipsis` 类型的实例的。" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Ellipsis\n" + ] + } + ], + "source": [ + "print(Ellipsis.__class__, Ellipsis.__class__())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "但是,`ellipsis` 类型是一个特殊的单例对象,不允许通过构造函数创建多个实例。因此,即使你加上括号,它仍然会返回 `Ellipsis` 单例对象。" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'ellipsis' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[22], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mEllipsis\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mEllipsis\u001b[39m()\n", + "\u001b[0;31mTypeError\u001b[0m: 'ellipsis' object is not callable" + ] + } + ], + "source": [ + "a = Ellipsis()\n", + "b = Ellipsis()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ellipsis\n", + "(,)\n", + "\n" + ] + } + ], + "source": [ + "print(Ellipsis.__class__.__name__)\n", + "print(Ellipsis.__class__.__bases__)\n", + "print(Ellipsis.__class__.__class__)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.py" "b/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.py" deleted file mode 100644 index 4d547e1..0000000 --- "a/004 pass\345\222\214\347\202\271\347\202\271\347\202\271/show.py" +++ /dev/null @@ -1,61 +0,0 @@ -# -*- encoding: utf-8 -*- -""" -PyCharm show -2022年06月17日 -by littlefean -""" -from typing import * - - -# ... 可以写在注解里面 -def test(a: ..., b: ...) -> ...: - ... - - -# 有一个类我还不知道怎么写,我可以先写个占位符 -class A: - ... - - -class B: - pass - - -def f(): - ... - - -def g(): - pass - - -def main(): - # 写个空循环 - for _ in range(10): - ... - - # 经过测试发现 点点点和pass做空循环速度是一样的。 - - for _ in range(100): - ... - ... - ... - ... - - # 经过测试发现,这样写四个,并没有出现速度慢了四倍的情况 - - print(id(...) == id(Ellipsis)) - # print(id(...) == id(ellipsis)) # 没有小写开头的东西了,因为是单例了,虽然pycharm里这个单词还会变蓝 - - print(type(...)) - print(type(Ellipsis)) - print(Ellipsis.__class__) - print(Ellipsis.__class__()) - print(Ellipsis.__class__.__name__) - print(Ellipsis.__class__.__bases__) - print(Ellipsis.__class__.__class__) - return None - - -if __name__ == "__main__": - main() From 6aa2be3bc5a80122cbede855da2153043cd4ae0f Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Sat, 23 Dec 2023 13:48:27 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E9=93=BE=E6=9D=A1=E6=8A=80=E5=B7=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.py" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/061 \351\200\273\350\276\221\351\223\276\346\235\241\346\212\200\345\267\247/main.py" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/061 \351\200\273\350\276\221\351\223\276\346\235\241\346\212\200\345\267\247/main.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/061 \351\200\273\350\276\221\351\223\276\346\235\241\346\212\200\345\267\247/main.py" new file mode 100644 index 0000000..10fbd4c --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/061 \351\200\273\350\276\221\351\223\276\346\235\241\346\212\200\345\267\247/main.py" @@ -0,0 +1,88 @@ +""" +and 链条会拿到第一个 bool(?) == False 的,如果没有False,就拿到最后一个值 +潜台词:遇到 False就不用再看了,肯定废辣! + +or 链条会拿到第一个 bool(?) == True 的,如果没有True,就拿到最后一个值 +潜台词:遇到 True 就不用再看了,肯定稳辣! + +优先级 +not > and > or + +用途:找到第一个转换后不是False的,用or链条 +value = a or b or c or d or default_value + +and用法: +result = x > y and "x太小" or "x很好" + +value = a and b and c and d +这样的赋值语句将给 value 赋予 a、b、c 和 d 中第一个为假的值(或者最后一个为真的值)。 + +例如你想挑选第一个空数组并添加东西 +select_arr = arr1 and arr2 and arr3 and default_arr + +""" + + +def main(): + f5() + pass + + +def f1(): + a = [12, 41, 4] + b = 114 + c = 0 + d = None + e = False + + res = a and b and c and d and e + print(res) + + +def f2(): + a = [1] + b = [2, 4, 6] + c = [4445] + d = [444] + print(a and b and c and d) + pass + + +def f3(): + a = [] + b = [2, 4, 6] + c = [] + d = [444] + print(a or b or c or d) + + +def f4(): + a = [] + b = help + c = None + d = () + print(a or b or c or d) + + +def f5(): + # 优先级测试 and or + def g1(): + print(1) + + def g2(): + print(2) + + def g3(): + print(3) + + g1() and g2() or g3() + + # [g1() and g2()] or g3() + + # g1() and [g2() or g3()] + + pass + + +if __name__ == '__main__': + main() From a7fd0ffb3e33de6c9a13d74f229ce73fa4c8387e Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Tue, 23 Apr 2024 16:40:36 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=B7=E6=83=91?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=EF=BC=9A=E6=88=91=E7=BB=A7=E6=89=BF=E6=88=91?= =?UTF-8?q?=E8=87=AA=E5=B7=B1=E5=B9=B6=E4=BC=98=E5=8C=96=E8=BF=B7=E6=83=91?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../classSelf/main.py" | 17 ------- .../main.py" | 0 .../main.py" | 0 .../test.py" | 0 .../test.py" | 0 .../test1.py" | 0 .../test2.py" | 0 ...42\345\220\221\345\257\271\350\261\241.py" | 0 ...47\346\211\277\346\265\213\350\257\225.py" | 0 ...7\346\211\277\346\265\213\350\257\2252.py" | 0 .../cpp.py" | 2 +- .../javaclass.py" | 0 .../a.py" | 0 .../d.py" | 0 .../2/test.py" | 0 .../exec\346\227\240\351\231\220/test.py" | 0 .../\347\246\201\347\224\250def/test.py" | 0 .../test.py" | 0 .../main.py" | 0 .../change_print.py" | 10 +++++ .../demo.py" | 45 +++++++++++++++++++ .../strange_code.py" | 9 ++++ .../main.py" | 33 -------------- 23 files changed, 65 insertions(+), 51 deletions(-) delete mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/classSelf/main.py" rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/def/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\206\205\351\203\250\345\210\240\351\231\244\350\207\252\345\267\261/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" (98%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" (100%) rename "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" => "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" (100%) create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/change_print.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/demo.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/strange_code.py" delete mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\246\201\346\210\221\350\207\252\345\267\261/main.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/classSelf/main.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/classSelf/main.py" deleted file mode 100644 index eb26db6..0000000 --- "a/python\350\277\267\346\203\221\350\241\214\344\270\272/classSelf/main.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Mountain: - def __init__(self): - self.temple = Mountain() # 无限递归了 - - -class Human: - def __init__(self): - self.生孩子 = Human - - -h = Human() -h2 = h.生孩子().生孩子().生孩子().生孩子().生孩子() -print(h2) - - - - diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\344\273\243\347\240\201\345\212\240\345\257\206/main.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\345\207\275\346\225\260\345\245\227\345\210\227\350\241\250\357\274\214\345\210\227\350\241\250\345\245\227\345\207\275\346\225\260/main.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\267\262\345\272\237\345\274\203\343\200\221\344\270\215\345\244\237\346\234\211\350\266\243/\347\224\250\351\255\224\346\234\257\346\226\271\346\263\225\345\256\236\347\216\260\351\200\222\345\275\222/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/def/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\206\205\351\203\250\345\210\240\351\231\244\350\207\252\345\267\261/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/def/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\206\205\351\203\250\345\210\240\351\231\244\350\207\252\345\267\261/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test1.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/test2.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\225.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2252.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" similarity index 98% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" index 56a946d..c4df58b 100644 --- "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/cpp.py" @@ -33,5 +33,5 @@ def __str__(self): a = Int() for _ in range(1000): - a = +a + ++a print(a) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\345\217\230\346\210\220\344\272\206\345\210\253\347\232\204\350\257\255\350\250\200/javaclass.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/a.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/d.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/2/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/exec\346\227\240\351\231\220/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\246\201\347\224\250xxx\347\263\273\345\210\227/\347\246\201\347\224\250def/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\272\277\347\250\213\350\210\236\350\271\210/test.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" similarity index 100% rename from "python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\277\220\350\241\214\346\210\221\350\207\252\345\267\261/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" rename to "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\347\274\226\350\257\221C\344\273\243\347\240\201/main.py" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/change_print.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/change_print.py" new file mode 100644 index 0000000..f65cd72 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/change_print.py" @@ -0,0 +1,10 @@ +# print.f = print +print_ = print + + +def print(*args, **kwargs): + print_(*args, **kwargs) + return print_ + + +print("hello")("world") diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/demo.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/demo.py" new file mode 100644 index 0000000..3ec7a1f --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/demo.py" @@ -0,0 +1,45 @@ +class MyClass: + def __init__(self): + self.x = 10 + + pass + + +class MyClass(MyClass): + def __init__(self): + super().__init__() + self.x = 11 + self.y = 20 + + pass + + +a = MyClass() +print(a.x, a.y) +print(dir(a)) + +# 自己是可以扩展的 +# 与字典类似 +dic = {"a": 1, "b": 2} +# dic = dic | {"c": 3} +dic |= {"c": 3} +print(dic) + + +# 论如何说服你的团队成员学习并使用git +class int(int): + def __new__(cls, x): + res = super().__new__(cls, x) + res.y = 20 + import random + if random.random() < 0.01: + res += 1 + return res + + pass + + +n = int("100") +print(n, type(n)) + +# 甚至可以把 int 给换掉 diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/strange_code.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/strange_code.py" new file mode 100644 index 0000000..341e995 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\347\273\247\346\211\277\346\210\221\350\207\252\345\267\261/strange_code.py" @@ -0,0 +1,9 @@ +class A: + def __new__(cls, *args, **kwargs): + return A + + +class A(A()()()(A(A(A(A(A)))))): + pass + +# 这样的写法可以迷惑人,让人误以为是无限递归,让抄作业的人看不懂,但是实际上是可以运行的 \ No newline at end of file diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\246\201\346\210\221\350\207\252\345\267\261/main.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\246\201\346\210\221\350\207\252\345\267\261/main.py" deleted file mode 100644 index 816f28a..0000000 --- "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\346\210\221\350\246\201\346\210\221\350\207\252\345\267\261/main.py" +++ /dev/null @@ -1,33 +0,0 @@ -from typing import * - - -class A: - def __init__(self): - self.val = list(range(100)) - - def __iter__(self): - yield from self.val - - def __getitem__(self, arg): - arr = self.val - if isinstance(arg, tuple): - for item in arg: - self.__getitem__(item) # todo - elif isinstance(arg, (int, slice)): - print(f"self.val[{arg}]") - return self.val[arg] - - def __str__(self): - return f"{self.val}" - - -a = A() -# 我疯了 -# print(a[5, 5, "abc", (1, 2)]) -# # 我更离谱了 -# print(a[a]) - -print(a[1, 4, 5]) - -a.val = [[y] * 10 for y in range(10)] -print(a[1]) From 562cdb0187dad918eb063687e663bf3de49b53bc Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Wed, 12 Jun 2024 19:41:11 +0800 Subject: [PATCH 14/15] update factory mode --- .../example.py" | 15 ++++ .../example_1.py" | 35 ++++++++ .../example_2.py" | 49 +++++++++++ .../outline.txt" | 16 ++++ .../example.py" | 41 +++++++++ .../outline.txt" | 9 ++ .../show/__pycache__/a_class.cpython-311.pyc" | Bin 0 -> 3540 bytes .../show/__pycache__/b_class.cpython-311.pyc" | Bin 0 -> 809 bytes .../show/__pycache__/factory.cpython-311.pyc" | Bin 0 -> 4046 bytes .../show/a_class.py" | 53 +++++++++++ .../show/b_class.py" | 6 ++ .../show/client.py" | 30 +++++++ .../show/factory.py" | 63 +++++++++++++ .../study/PersonUtils.py" | 80 +++++++++++++++++ .../study/example_1.py" | 24 +++++ .../study/example_2.py" | 83 ++++++++++++++++++ .../study/ouline.txt" | 14 +++ .../study/show_2.py" | 77 ++++++++++++++++ .../example_1.py" | 70 +++++++++++++++ .../example.py" | 33 +++++++ .../outline.txt" | 11 +++ .../file_type_class/__init__.py" | 0 .../file_type_class/file_type_object.py" | 9 ++ .../file_type_class/test.py" | 15 ++++ ...41\351\200\202\351\205\215\345\231\250.py" | 0 ...73\351\200\202\351\205\215\345\231\250.py" | 0 26 files changed, 733 insertions(+) create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/b_class.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" new file mode 100644 index 0000000..425868d --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" @@ -0,0 +1,15 @@ +""" +python风格的设计模式 +""" + +""" +首先类可以分为很多情况,有些类的效果能用元类实现。 + +创建不出实例的类:抽象类 +只能创建一个实例的类:单例模式 +不能被继承的类:final类 +必须被继承才能有作用的类:基类、元类 + +类创建出的实例具有自我复制能力:原型模式 + +""" \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" new file mode 100644 index 0000000..6dd2186 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" @@ -0,0 +1,35 @@ +""" +单例模式 +懒汉模式 +""" + + +class Singleton: + __instance = None + + @staticmethod + def getInstance(): + if Singleton.__instance is None: + Singleton() + return Singleton.__instance + + def __init__(self): + if Singleton.__instance is not None: + raise Exception("This class is a singleton!") + else: + Singleton.__instance = self + + def someMethod(self): + pass + + +def main(): + # Usage + s1 = Singleton.getInstance() + s2 = Singleton.getInstance() + + print(s1 == s2) # True + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" new file mode 100644 index 0000000..7f0e206 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" @@ -0,0 +1,49 @@ +from abc import ABCMeta, abstractmethod +from typing import Dict + + +class SingletonMeta(type): + _instances: Dict[type, object] = {} + + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs) + return cls._instances[cls] + + +class Singleton(metaclass=SingletonMeta): + pass + + +class MyABC(metaclass=ABCMeta): + @abstractmethod + def f(self): + pass + + +class MyClassA(Singleton, MyABC): + def f(self): + print('A') + + +class MyClassB(Singleton, MyABC): + def f(self): + print('B') + + +def main(): + s1 = Singleton() + s2 = Singleton() + print(id(s1), id(s2)) # 打印两个对象的id,可以看到它们是同一个对象 + a1 = MyClassA() + a2 = MyClassA() + b1 = MyClassB() + b2 = MyClassB() + a1.f() # A + a2.f() # A + b1.f() # B + b2.f() # B + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..a2809eb --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,16 @@ +用过程性、灵活性和创造性的方式来理解单例模式。《从全局变量到单例模式的自然演进过程》 + +0. globals.py 文件,里面定义了很多的全局变量。 +1. 大字典:发现这些全局变量太多,并且有嵌套结构,可以写一个大字典。 +2. 工具类:发现大字典里写函数很麻烦,并且key没有代码提示,于是把大字典改成了一个类。键值对改成了类属性 +3. 单例类:发现函数需要用到一些属性。于是把类属性变成了实例属性。这就导致了需要创建实例才能用,但只用一个实例就可以了。 +4. 更安全的单例类(防止外部调用产生新实例): + 可以把类名前面加下划线,君子协议 + 可以del删掉这个类名称本身,但类还是在内存中不会被垃圾回收机制清理掉。 + 更安全的方法:创建单例类元类,这样创建出来的类都是一个实例了 +5. 懒汉式单例模式: + 这个单例的创建过程非常消耗时间,没必要一开始就创建。可以后面真正需要的时候才创建 + 但可能有线程安全问题,因为创建单例的过程不是原子操作。threading.Lock() + +以上顺序不一定就是后面一种比前面一种好,只是一种扩展性增强和不断优化的思路。 +可以说越是靠前,可能就越使用小轻量级的场景和项目,越是靠后,可能就越适合大型项目和复杂场景。 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" new file mode 100644 index 0000000..37ae489 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" @@ -0,0 +1,41 @@ +class A: + def __init__(self, x): + # === + # 这里中途可能花费了很大的时间复杂度用来计算出一个数值 + # === + self.x = x + + def build1(self): + for i in range(10000000): + self.x += 15 + + def build2(self): + for i in range(10000000): + self.x += 20 + + def clone(self): + return A(self.x) + + +class B: + + def __init__(self, a): + self.a = a + + def func(self): + print(self.a) + + +""" +原型模式可能应用到的地方: +1. 性能优化:由于创建复杂对象时,原型模式可以减少创建对象的过程,从而提高性能。 +地形生成、 + +2. 资源优化:由于创建复杂对象时,原型模式可以减少内存占用,从而节省系统资源。 +和第一个差不多 + +3. 代码复用:由于创建复杂对象时,原型模式可以减少代码量,从而提高代码复用率。 +子弹克隆 + + +""" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..d501192 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,9 @@ + + +1. 可能消耗很大计算量才能创建出一个实例,并且写在了工厂函数里。再次创建相同类型的实例可以直接克隆节省时间 +2. 使用简单化。比如在游戏中,让某个枪类给一个子弹属性, + 每次发射开火的时候都不停的复制这个子弹,所以直接给子弹写一个克隆方法就可以,不需要再写子弹的颜色之类的东西了。 + 这样给枪切换不同的子弹的时候,只需要更改蓝图子弹就可以了。 + + 还可能是机器学习模型训练的时候,每次训练都需要重新生成一个模型,这时候直接用原型模式就很方便。 + UI项目中,原型模式还可以直接生成好一个组件,然后每次用户拖拽到舞台上就是不停的复制一份这个组建,吸附在鼠标上然后放在舞台上 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..f039b03ff1419c6064d30e3c06bef6904b3321d1 GIT binary patch literal 3540 zcmbtWT}%{L6uz@NvkZSOupn9`tgSU}Ls6^YLB*Dc{YebCENF|E?Xok#;{L$Qpn#i{ zG}K@<);2VdN*gh#5YaS^Nlhy)8WLX@vJYeulP2w2eA|Zh#iyQgXZG?hjlC@2eCM1y z_ue_@yLaYRc6J7V)_u99sZb!~cQjIsH_7~NAOl1q5*H*PuAU27>Mb0xkoSmW=_Qi& zriGB(@NcwwtHgVVOL&H+%cAp@0l%j6M+2(nH@nbFmTAm?4rGAHq@I&Vy+z_+0E^5^ zR#}jE*(M2cn#^xzcS<%{lx;HKWLfYvWk_kge0{nk_LBMxIio&PN(YuDWdLSNnSdLl zEI_-I4VWWs0L+!_fO%35phL<9%$M?D;SQav2qe-7(K(;aHR_yS=Omqz{Wi7+Lae~? ze_jI^AW<$#yeu5GfW@WEs1+=hohT0$-bq_j04tSeqGhK1ghWXzo9`x!b(@XHK_xt7 zjH_~`DX>bShzRKjSW5*5(S?(Q$(cse?qc<@DH+1$J} zFHgkmJ}JJTcv^JEMQ1Gan}-pIpCuR!%pooBhd@C3P~KKxM!O2K2b=W=8%o^DN(?p> zxs??eY$$UpD>K-(Z!s2)ca+HoaqO<_JlmqmTS$p*rslO>^uZG{X?ISx3HY$yqzWduBn7&DA%Jg9$ zv08D!2um^qhlP=$M?hc$EXogXmYZ&fYS`Coi}SKn*0d+8c%dh|Z{413t6W1#tY|mE zbDMcrj^-gsHiTW`Lb7#hP!7{wrCZgG(4wtnJL3y>$f{BZcPvT1KN4!|&}2-$NngP( z6#$rg$E7oa{E+R!CpR3|YDbO_ZyDJ#Z7;c_e!uTt+1R=9bJO=+}dm?WyE=rVJD3BT71#vby#2Gi^ z#fu9EaVBmvZihG%IK&w@;%0)Cjj+}+3)QVIM6?!JQE?`xsb8DdE4PDuMU*eHA_%H( zfK&nth6-ow&X~OuQ#jf&es<>dGO+INdr&r6H+5p>z)`Sb?z*`9MC|0p@so`+?nd-9 zpN=<&V(n+*?VU5toiVXu3wgi)1VWG&!_V#cZ<$G%U)Wng=?Nd7*mpm$YSUOcYeb600=#J&rOxrziyC)`kUZCJtX~H3~a5Wkz zC`HL>%FM@uLyx}miov;DIzfCpQ0X9h4SLMushEXepu!r01c z>u6@{K(+(By@5c_P`0tagPbaEodi(rtvhW{W{(fd1!FIHd~@fB&X->YJ&)I2M>AUo zvJ!mqT?jAz`1GtnQ9wn&h*DZL|4>Nh@4`%io+7;F`3Nf=$UZWFjfp zJm~2EAeoL<2DJwLgPZ!Rl9}isOJOS~9OIH?5!y&jVbA+Xo)cScRbx+_k>_absGTg^-ViYuIKtz54=m{F1Cb-(1+?0#?a$Mfy}r_SMnCIyO`^vMhY3OF(M;Nqi& z)-76}<8zX3MkW;_<+Xg(;XFuiUUkK4h4?YfZ>;7klZ;-j`EnWy?x?YskPLLj T*g!KGcKjE9Qta1%5~}eZuBysF literal 0 HcmV?d00001 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..42cab2c43bb54a26605569a7b148f95d4a53b275 GIT binary patch literal 4046 zcmcgvTTB~A6rJ_j>jx0BHqf+rf%>$Xkbu)fN(m{?<{=y!+NyCKS&Mf8M}Cl5r!5wd zP(>r8L@h~+K7hqki zT4(+GfWc)5a4s%jbQu|8t43qMcY`R+>U z=ER>j&n;ZLk{Y{o_wuNF_g+Ws!nL13a3^{tb>pMd*H=>?MBS;ePg6hskow}hJN5lH zsSn@Rlx}$-^sc*4GrfLrh?>D36*#EKl>j68Us7)xX{R`Q^$l1-Gc1$f^$W6Guu?ff zGVj2lP14he)%uei8dawf5oQJZwsu(siQvA3ouE-IliGKGNMjo0rmaBx2P8l zpXp}6z2|Z+PRDQvnbm#TD`MTJSCb;7K zfdqeGj7zEp&cAu%EwCzX>rB`>$G8ioWM%bOg=*d-x(T=kxCx-hm2eYq<41B6laoFL z^GH|Ue%q*UgK7duqu-@5M1eX+QkcGVbQHP``lYo1E8T%U4T4BR!J1>L4E9TFI>ILG z>=^_#aXgE)=K)sITZQx%cNW>Y=2+F($4Y{I)0?6#(bLhr^ZYG7=4gW}&bKG{_WzGy zPBfMv7&6i$5p327rZ3{sU})2w2=xHz%Mg%s7t|gTzq<<7d_Wi+99olI)m8q5T`RI% z*i~fn8slt+={}HITg=e`SDf!m@DQc{F?5Gvh~f+%#tf7#qHYGV&s;wFLhjE6^;(=` z_EbxW+@IH}xj)Of!uo=JMV!-@po_=Jd0|ZV`uh7NIlLz8D&cw7?W~r(4SoKQ5T?(e z`6VN~xa*<_FILwFr7YWQKiqRvv1A)<-ES!6u;GP$UhQy5?fAU| zVIM*h0`9b0ro$ab-EmdG!-f1Yz)1d=d~$0{Uz4obHqw=}ZXY?CM)wUxMLS=bUBaB90ASxoZ>_yoO?`)sWp2tqj;R5RS1~R zX$?Su4SEn@TxRwItcuv2g`hi?uj8?&FnaNP?o1;evn<6PX)6LUU(6R$8rhOFujF9| z-jH!q1Fx*yIBuO7NYpgM`G$C9W1_M#W@=o0VpzXP`0m1DP>hLU!_oO7TMG;?j;F># z%WV_yC7x)E^NsOJN21aZGdWbtopwv95TOTf&}IbObm=PyY5(utKkwf`br> zhbL-8z|hnJRW-XXg72A{v_Ejo6vF^_1UUw<$TAF*B-OFPtCVC%j6CpKbb65pa mMpe3ms^$B7ZVLl3yBd~IwtQdFT+YDytcE3&EtgQJOZX3pbB7xM literal 0 HcmV?d00001 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" new file mode 100644 index 0000000..0afa988 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" @@ -0,0 +1,53 @@ +from typing import Dict, List + + +class A: + CURRENT_YEAR = 2021 + + def __init__(self, + a: int, + b: float, + c: str, + d: List[int], + e: Dict[str, int] + ): + self.a = a + self.b = b + self.c = c + self.d = d + self.e = e + print(self.CURRENT_YEAR) + + def __str__(self): + return f"A(a={self.a}, b={self.b}, c={self.c}, d={self.d}, e={self.e})" + + __repr__ = __str__ + + def compute(self): + return self.a + self.b + len(self.c) + sum(self.d) + sum(self.e.values()) + + def __eq__(self, other): + return self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e + + def __hash__(self): + return hash((self.a, self.b, self.c, tuple(self.d), tuple(self.e.items()))) + + def __lt__(self, other): + return self.a < other.a + + def __gt__(self, other): + return self.a > other.a + + def __le__(self, other): + return self.a <= other.a + + def __ge__(self, other): + return self.a >= other.a + + def __ne__(self, other): + return self.a != other.a + + def __bool__(self): + return bool(self.a) + + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" new file mode 100644 index 0000000..f7bef54 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" @@ -0,0 +1,6 @@ +class B: + def __init__(self, a: int): + self.a = a + + def __str__(self): + return f"<{self.a}>" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" new file mode 100644 index 0000000..5f12d02 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" @@ -0,0 +1,30 @@ +from a_class import A +from factory import AFactory + + +def main(): + + a_normal_1 = AFactory.get_normal_instance() + # ==== + a2 = A(1, 1.3, 'world', [3, 4], {'c': 3, 'd': 4}) + # ==== + + a3 = A(2, 2.3, 'python', [5, 6], {'e': 5, 'f': 6}) + + AFactory.SYSTEM_PERFORMANCE = 0.5 # 模拟系统性能下降,降低创建对象的速度 + + # ==== + a_normal_2 = AFactory.get_normal_instance() + a_normal_3 = AFactory.get_normal_instance() + + AFactory.SYSTEM_PERFORMANCE = 1 # 模拟系统性能恢复,恢复创建对象的速度 + + a_strange_1 = AFactory.get_strange_instance() + + # ==== + a_strange_2 = AFactory.get_strange_instance() + print(a_normal_1, a_normal_2, a_normal_3, a_strange_1, a_strange_2) + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" new file mode 100644 index 0000000..a7a65bf --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" @@ -0,0 +1,63 @@ +from a_class import A +from b_class import B +from abc import ABC, abstractmethod + + +class Factory(ABC): + + @staticmethod + @abstractmethod + def get_system_performance() -> int: + pass + + # SYSTEM_PERFORMANCE = 1000 + # SYSTEM_MEMORY = 1000000000 + # SYSTEM_DISK = 1000000000000 + # SYSTEM_CPU = 10 + + +class AFactory(Factory): + @staticmethod + def get_system_performance() -> int: + return 10010 + + SYSTEM_PERFORMANCE = 1001 + + @staticmethod + def get_normal_instance(): + result = A(1, 1.3, 'hello', [1, 2], {'a': 1, 'b': 2}) + result.d = [i * AFactory.get_system_performance() for i in result.d] + return result + + @staticmethod + def get_strange_instance(): + result = A(2, 123456789, 'python', [5, 6], {'e': 5, 'f': 6}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + @staticmethod + def get_happy_instance(): + result = A(2, 123456789, 'python', [5, 6], {'e': 5, 'f': 666}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + @staticmethod + def get_biggest_instance(): + result = A(2, float('inf'), 'python', [5, 6], {'e': 5, 'f': 666}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + +class BFactory(Factory): + + @staticmethod + def get_system_performance() -> int: + return 15 + + @classmethod + def get_normal_instance(cls): + return B(15 * BFactory.get_system_performance()) + + @staticmethod + def get_biggest_instance(): + return B(999 * BFactory.get_system_performance()) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" new file mode 100644 index 0000000..b70533a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" @@ -0,0 +1,80 @@ +from .show import MyClass +import platform + + +class PersonFactory1: + SYSTEM_PERFORMANCE = 100 + + @staticmethod + def _get_gender_by_name(name: str) -> bool: + """如果长度大于5,则判断为男性,否则为女性""" + return len(name) > 5 + + @staticmethod + def get_Bob(): + result = MyClass( + name='Tom', + age=20, + score_list=[90, 80, 70], + address='Beijing', + phone='13800138000', + email='tom@example.com', + hobby='reading' + ) + result.score_list = [int(i * PersonFactory1.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = PersonFactory1._get_gender_by_name(result.name) + return result + + @classmethod + def get_Alice(cls): + result = MyClass( + name='Jane', + age=25, + score_list=[85, 95, 90], + address='Shanghai', + phone='13900139000', + email='jane@example.com', + hobby='swimming' + ) + result.score_list = [int(i * cls.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = cls._get_gender_by_name(result.name) + return result + + +if platform.system() == 'Windows': + PersonFactory1.SYSTEM_PERFORMANCE = 150 +else: + PersonFactory1.SYSTEM_PERFORMANCE = 100 + + +class PersonFactory2(PersonFactory1): + SYSTEM_PERFORMANCE = 120 + + @staticmethod + def get_Tom(): + result = MyClass( + name='Bob', + age=20, + score_list=[90, 80, 70], + address='Beijing', + phone='13800138000', + email='bob@example.com', + hobby='reading' + ) + result.score_list = [int(i * PersonFactory2.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = PersonFactory2._get_gender_by_name(result.name) + return result + + @classmethod + def get_Jane(cls): + result = MyClass( + name='Alice', + age=25, + score_list=[85, 95, 90], + address='Shanghai', + phone='13900139000', + email='alice@example.com', + hobby='swimming' + ) + result.score_list = [int(i * cls.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = cls._get_gender_by_name(result.name) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" new file mode 100644 index 0000000..3e1c3e4 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" @@ -0,0 +1,24 @@ +""" +工厂模式举例 + +1. 直接写在类的静态方法中 +""" + + +class Vector: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + @staticmethod + def get_zero() -> 'Vector': + # 目的:简化参数,可以直接通过类名调用静态方法 + return Vector(0, 0) + + @staticmethod + def get_random() -> 'Vector': + # 目的:简化参数,可以直接通过类名调用静态方法 + import random + return Vector(random.randint(0, 100), random.randint(0, 100)) + + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" new file mode 100644 index 0000000..2421090 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" @@ -0,0 +1,83 @@ +""" +工厂模式 - 单独写一个工厂类的情况举例 +""" +from abc import ABCMeta, abstractmethod + + +class ResultType(metaclass=ABCMeta): + VALUE = 0 + + @abstractmethod + def f(self): + pass + + pass + + +class A(ResultType): + VALUE = 1 + + def f(self): + pass + + +class B(ResultType): + VALUE = 2 + + def f(self): + pass + + +class C(ResultType): + VALUE = 3 + + def f(self): + pass + + +class Factory: + + @staticmethod + def create_object(type_name): + """ + 但是这种情况可以直接被一个全局函数代替,不需要单独写一个工厂类 + """ + if type_name == 'A': + return A() + elif type_name == 'B': + return B() + elif type_name == 'C': + return C() + else: + raise ValueError('Invalid type name') + + COUNT = 0 + + @classmethod + def create_object_by_score(cls, score: int) -> ResultType: + """ + 但是如果需要记录创建对象的次数,可以用类方法来实现 + 这种情况的使用场景还有可能有: + 1. 记录创建对象的次数 + 2. 在创建对象时,会对其他环境产生依赖或者修改全局状态,可以用工厂模式来封装这些操作 + """ + cls.COUNT += 1 + if score >= 80: + return A() + elif score >= 60: + return B() + elif score >= 40: + return C() + else: + raise ValueError('Invalid type name') + + +def main(): + obj1 = Factory.create_object('A') + obj2 = Factory.create_object('B') + obj3 = Factory.create_object('C') + print(obj1, obj2, obj3) + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" new file mode 100644 index 0000000..23b542a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" @@ -0,0 +1,14 @@ +工厂模式: + +0. 正常写类:直接写一个类 +1. 出现工厂方法:但随着构造函数的参数越来越多,开始麻烦起来了。直接写在类里的一个静态方法(类方法也行) +2. 出现工厂方法的Utils文件:为了避免类中出现太多静态方法,可以把静态方法都放在一个Utils文件中。 + (单独写在一个文件里还可以更清晰,因为多个工厂函数内部可能调用到同一个其他的函数,那么这个被调用的函数就可以放在utils里,函数名前面加下划线表示私有) +3. 出现工厂类:发现可能集装在一个类中可能更好一些,可以写类属性,统一控制这些工厂方法。 + (java里必须写成类,因为函数没法单独存在) + (工厂类还可以不停的继承,实现,扩展,父工厂类可以有2个工厂函数,子工厂类可以有2+1个工厂函数) +4. 出现工厂类的继承或实现(抽象工厂):发现工厂类的某一个属性需要在不同的场景中频繁修改,于是开始了工厂类的继承或者实现。 + (这个属性可能是代表xx性能属性) + (如果直接从utils方法转成抽象工厂,那还需要再写一个工厂类) +5. 抽象工厂的抽象工厂:为了让A工厂类和B工厂类统一接口,所以出“AB统一工厂”这个抽象工厂,为了让C工厂类和D工厂类统一接口,所以出“CD统一工厂”。 + 然后可能会发现 CD统一工厂 和 AB统一工厂 有些重复,于是又出现了“统一工厂”这个更抽象的抽象工厂。 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" new file mode 100644 index 0000000..24e55fc --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" @@ -0,0 +1,77 @@ +from typing import List + + +class Animal: + def __init__(self, name): + self.name = name + + +class Basketball: + def __init__(self, name): + self.name = name + + +class Car: + def __init__(self, name): + self.name = name + + +class Desk: + def __init__(self, name): + self.name = name + + +from abc import ABC, ABCMeta, abstractmethod + + +class Factory(ABC): + FactoryLocation: List[int] = [100, 100] + FactoryName: str = "Factory" + + @abstractmethod + def test(self): + pass + + FactoryCommentList: List[str] = [] + + +class FactoryChina(Factory): + FactoryLocation: List[int] = [100, 100] + FactoryName: str = "China" + FactoryCommentList: List[str] = [] + + @staticmethod + def get_animal(name): + return Animal(name) + + @staticmethod + def get_ball(name): + return Basketball(name) + + def test(self): + print(self) + + +class FactoryUSA(Factory): + FactoryLocation: List[int, int] = [500, 200] + FactoryName: str = "USA" + FactoryCommentList: List[str] = [] + + @staticmethod + def get_car(name): + return Car(name) + + @staticmethod + def get_desk(name): + return Desk(name) + + def test(self): + print(self) + + +def main(): + a1 = FactoryChina.get_animal("dog") + b1 = FactoryChina.get_ball("basketball") + c1 = FactoryUSA.get_car("BMW") + d1 = FactoryUSA.get_desk("desk") + print(a1.name, b1.name, c1.name, d1.name) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" new file mode 100644 index 0000000..7b56f89 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" @@ -0,0 +1,70 @@ +""" +传统的抽象工厂模式 + +抽象工厂模式是一种创建型设计模式, +它提供一个创建一系列相关或相互依赖对象的接口, +而无需指定它们具体的类。 + +在抽象工厂模式中, +一个工厂接口负责创建相关对象, +而子类则负责实现这些对象的创建。 + + +北京工厂、上海工厂、广州工厂等, +这些所有的工厂都统一写一个接口,这个工厂的接口就是抽象工厂。 +抽象工厂不能说是生产工厂的工厂,而是工厂的统一规定,工厂的接口。 + +抽象工厂模式的选择是基于工厂模式的类写法来的。 + +1 工厂方法写在类本身中 +2 工厂方法单独写在一个类中(工厂类) +3 工厂类被统一成一个接口(抽象工厂类) +""" + + +class AbstractFactory(object): + def create_product_a(self): + pass + + def create_product_b(self): + pass + + +class ConcreteFactory1(AbstractFactory): + def create_product_a(self): + return ConcreteProductA1() + + def create_product_b(self): + return ConcreteProductB1() + + +class ConcreteFactory2(AbstractFactory): + def create_product_a(self): + return ConcreteProductA2() + + def create_product_b(self): + return ConcreteProductB2() + + +class AbstractProductA(object): + pass + + +class ConcreteProductA1(AbstractProductA): + pass + + +class ConcreteProductA2(AbstractProductA): + pass + + +class AbstractProductB(object): + pass + + +class ConcreteProductB1(AbstractProductB): + pass + + +class ConcreteProductB2(AbstractProductB): + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" new file mode 100644 index 0000000..a11e240 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" @@ -0,0 +1,33 @@ +class Person: + def __init__(self, name, age, hair_color=None, height=None, weight=None, address=None): + self.name = name + self.age = age + self.hair_color = hair_color + self.height = height + self.weight = weight + self.address = address + + +class PersonBuilder: + def __init__(self): + self.person = Person(None, None) + + def with_name(self, name): + self.person.name = name + return self + + def with_age(self, age): + self.person.age = age + return self + + def build(self): + return self.person + + +""" +好处: +参数太多的时候可以分步骤构建,顺序可能可以换,(但实际上py有默认参数,可以不用这么麻烦) +参数太多可以把几个捏在一起放在一个构造过程函数里 +""" +builder = PersonBuilder() +person = builder.with_name("John").with_age(30).build() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..c6d0dbb --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,11 @@ +一般来说python不需要构造器模式,因为: + +动态追加属性(虽然可能有编辑器警告,不太推荐) +可变默认参数,可以让参数灵活决定位置 +可以让构造函数没有参数,让属性全都是先给定一个默认值,后面只对需要的进行更改,这样可以减少代码量 + +如果遇到这种情况可以考虑构造器模式: + +1. 将构造函数的职责分解为多个小函数,每个小函数负责一个构造步骤 + 可选参数太多,统一写起来太麻烦,可以把三四个包起来形成一个步骤 +2. 创建对象的逻辑和对象本身分离。可以单独写一个 XxxGenerator 类 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" new file mode 100644 index 0000000..e69de29 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" new file mode 100644 index 0000000..d5152b8 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" @@ -0,0 +1,9 @@ +""" +将当前这个代码文件直接视为一个对象 +""" + +value = 100 +value2 = 200 + +_value3 = 300 +__value4 = 400 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" new file mode 100644 index 0000000..f0f442a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" @@ -0,0 +1,15 @@ +import file_type_object + + +def main(): + print(file_type_object) + print(file_type_object.value) + + print(file_type_object._value3) # 警告但可以访问 + print(file_type_object.__value4) # 反而又没有警告了 + + pass + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" new file mode 100644 index 0000000..e69de29 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" new file mode 100644 index 0000000..e69de29 From f9921e9d884aa760b0bffd1a21294be30944fa4f Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Tue, 18 Jun 2024 06:58:25 +0800 Subject: [PATCH 15/15] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=BF=B7?= =?UTF-8?q?=E6=83=91=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\346\211\277\346\265\213\350\257\2251.py" | 53 ++++++++++++++ ...7\346\211\277\346\265\213\350\257\2253.py" | 68 ++++++++++++++++++ ...7\346\211\277\346\265\213\350\257\2254.py" | 51 +++++++++++++ ...7\346\211\277\346\265\213\350\257\2255.py" | 42 +++++++++++ .../__pycache__/a.cpython-311.pyc" | Bin 0 -> 1020 bytes .../__pycache__/d.cpython-311.pyc" | Bin 0 -> 632 bytes .../a.py" | 2 + 7 files changed, 216 insertions(+) create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" new file mode 100644 index 0000000..1d49b1c --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" @@ -0,0 +1,53 @@ +def extend(*extend_cls_list): + def decorator(cls_function): + def new_function(*args, **kwargs): + if len(extend_cls_list) == 0: + return cls_function(lambda: ..., *args, **kwargs) + else: + super_obj = lambda: ... + for extend_cls in extend_cls_list: + current_super_obj = extend_cls(*args, **kwargs) + for key, value in current_super_obj.__dict__.items(): + if not key.startswith('__'): + setattr(super_obj, key, value) + return cls_function(super_obj, *args, **kwargs) + + return new_function + + return decorator + + +@extend() +def Animal(self, *args, **kwargs): + self.name = kwargs.get('name', 'Unknown') + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +@extend() +def Runner(self, *args, **kwargs): + self.speed = kwargs.get('speed', 0) + return self + + +@extend(Animal, Runner) +def Dog(self, *args, **kwargs): + self.age = kwargs.get('age', 0) + + self.super_to_string = self.to_string # 保存父类的to_string方法 + + def to_string(): + print(self.super_to_string()) + return f"I am {self.name}, I am {self.age} years old, and I run at {self.speed} km/h." + + self.to_string = to_string + + return self + + +dog = Dog(name='Rufus', age=3, speed=10) +print(dog.to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" new file mode 100644 index 0000000..6d3f512 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" @@ -0,0 +1,68 @@ +from copy import deepcopy + + +def Person(name: str, age: int): + # 定义this对象,不能用object(), 否则会报错 + this = lambda: ... + + def toString(): + return f"Person(name={this.name}, age={this.age}, defaultValue={this.defaultValue})" + + def sayHello(): + print(f"Hello, my name is {this.name} and I am {this.age} years old., my default value is {this.defaultValue}") + + # 给this对象添加属性 + this.name = name + this.age = age + this.defaultValue = 100 + # 给this对象添加方法 + this.toString = toString + this.sayHello = sayHello + # 返回this对象 + return this + + +p1 = Person("Alice", 25) +print(p1) +print(p1.toString()) +print(p1.age, p1.name) +print(p1.toString.__closure__) + + +# 单继承 +def Worker(name: str, age: int, salary: int): + this = Person(name, age) + + def work(): + print( + f"I am a worker, my name is {this.name}, I am {this.age} years old, and my salary is {this.salary}, my default value is {this.defaultValue}") + + this.salary = salary + this.defaultValue = 200 + this.work = work + return this + + +w1 = Worker("Bob", 30, 5000) +w1.sayHello() +print(w1.toString()) + + +def SuperWorker(name: str, age: int, salary: int, bonus: int): + super = Worker(name, age, salary) + this = deepcopy(super) + print(id(super), id(this)) + + def work(): + this.super.work() + print( + f"I am a super worker, my name is {this.name}, I am {this.age} years old, and my salary is {this.salary}, my bonus is {this.bonus}, my default value is {this.defaultValue}") + + this.super = super + this.bonus = bonus + this.work = work + return this + + +sw1 = SuperWorker("Charlie", 35, 7000, 1000) + diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" new file mode 100644 index 0000000..16b9b05 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" @@ -0,0 +1,51 @@ +import inspect + + +def extends(super_class): + def decorator(child_class): + + def new_class(*args, **kwargs): + + if super_class is not None: + print(args) + super_object = super_class(*args, **kwargs) + return child_class(super_object, *args, **kwargs) + else: + # bug + return child_class(lambda: ..., *args, **kwargs) + + new_class.__name__ = child_class.__name__ + new_class.__doc__ = child_class.__doc__ + + return new_class + + return decorator + + +@extends(None) +def Animal(self, name: str): + self.name = name + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +print(Animal("dog").to_string()) + + +@extends(Animal) # 太麻烦了 +def Fish(self, name: str, color: str): + self.name = name + self.color = color + + def to_string(): + return f"I am {self.name}, and my color is {self.color}" + + self.to_string = to_string + return self + + +print(Fish("goldfish", "gold").to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" new file mode 100644 index 0000000..9e8ee75 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" @@ -0,0 +1,42 @@ +def extend(extend_cls): + def decorator(cls_function): + def new_function(*args, **kwargs): + if extend_cls is not None: + super_obj = extend_cls(*args, **kwargs) + return cls_function(super_obj, *args, **kwargs) + else: + return cls_function(lambda: ..., *args, **kwargs) + + return new_function + + return decorator + + +@extend(None) +def Animal(self, *args, **kwargs): + self.name = kwargs.get('name', 'Unknown') + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +@extend(Animal) +def Dog(self, *args, **kwargs): + self.age = kwargs.get('age', 0) + + self.super_to_string = self.to_string # 保存父类的to_string方法 + + def to_string(): + print(self.super_to_string()) + return f"I am {self.name}, I am {self.age} years old" + + self.to_string = to_string + + return self + + +dog = Dog(name='Rufus', age=3) +print(dog.to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..9619eade2e7b53ec5783876a2c32cfb9043685bd GIT binary patch literal 1020 zcmcIi&1(}u6o0cj8`rI|HegLWlw3UI&|dT;;-wWUVl3nqBFmCpHMQA<$$F?*SaFM6 z$e{tH*Lbi}d-7sMME?N~X$&+h6oi;Iy#=ZuJ^5zME)94S-|YMCoA){I&Ag6A6+rs& zt917({yTgVkT?(yzo4)NI_P8pvLJv#bYYzUY$E?50=noxR?sP;Xi!~xAaDztHDG?= z1OHK~2WJgyabX>KGt}T2Nyg_?u)5N^Ps{uD=ldJ)I(99$ z=hk}9-gLL#c0a!8F24%5Ic=)E=uyk4Sd2e_2y$f#!G_&vZ1v(wwwmn&k;Z&*$*vrA zwH3K4ySVydU^v7G7#bnJb$^5w3OGXUFra^V2lZoD5#;;J{qk{>G)|gmep?&BM$ny| zMDS=)E0}B)MXsw{K)`OP@=Xiiw7pXb&IXx!s~?Tl8FuCU)pVlTK{YWQ!&n O!Pggop2sl%c)tN)V$T5p literal 0 HcmV?d00001 diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..0ac6aeace719c9f094d39695deade6ea469f2089 GIT binary patch literal 632 zcmY*V&o2W(6n?WmXe5fnA0RIDK+>B?BN5I*8kc4-be1Z1H`8?>4%1k%2ZvS&O-Sm1 z;N*gc;7@RD_C%_*w>zs%>RBNum%^E5ddv-tE25yP&(o{u=L&PIV0Y*yDzHf#&IjT)F zGpWbzRHGP*uFf%g!lZiCSh6e~JN}pHbvt1in&n^R=ey6XZ25HeWSgVOd{lWKI>T4&PO&E=