Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_python
提交
73558ebb
S
skill_tree_python
项目概览
CSDN 技术社区
/
skill_tree_python
通知
66
Star
14
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
73558ebb
编写于
12月 22, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化第3章代码
上级
0ddef961
变更
36
隐藏空白更改
内联
并排
Showing
36 changed file
with
1530 addition
and
231 deletion
+1530
-231
data/1.python初阶/3.进阶语法/1.列表推导式/dict.json
data/1.python初阶/3.进阶语法/1.列表推导式/dict.json
+7
-13
data/1.python初阶/3.进阶语法/1.列表推导式/dict.md
data/1.python初阶/3.进阶语法/1.列表推导式/dict.md
+88
-0
data/1.python初阶/3.进阶语法/1.列表推导式/dict.py
data/1.python初阶/3.进阶语法/1.列表推导式/dict.py
+1
-5
data/1.python初阶/3.进阶语法/1.列表推导式/iter.md
data/1.python初阶/3.进阶语法/1.列表推导式/iter.md
+0
-9
data/1.python初阶/3.进阶语法/1.列表推导式/list01.json
data/1.python初阶/3.进阶语法/1.列表推导式/list01.json
+7
-13
data/1.python初阶/3.进阶语法/1.列表推导式/list01.md
data/1.python初阶/3.进阶语法/1.列表推导式/list01.md
+60
-0
data/1.python初阶/3.进阶语法/1.列表推导式/list02.json
data/1.python初阶/3.进阶语法/1.列表推导式/list02.json
+7
-13
data/1.python初阶/3.进阶语法/1.列表推导式/list02.md
data/1.python初阶/3.进阶语法/1.列表推导式/list02.md
+69
-0
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.json
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.json
+4
-13
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.md
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.md
+56
-0
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.json
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.json
+4
-13
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.md
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.md
+51
-0
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.json
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.json
+4
-13
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.md
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.md
+55
-0
data/1.python初阶/3.进阶语法/3.断言/assert.json
data/1.python初阶/3.进阶语法/3.断言/assert.json
+4
-15
data/1.python初阶/3.进阶语法/3.断言/assert.md
data/1.python初阶/3.进阶语法/3.断言/assert.md
+105
-0
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.json
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.json
+4
-12
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.md
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.md
+95
-0
data/1.python初阶/3.进阶语法/4.with-as/withas.json
data/1.python初阶/3.进阶语法/4.with-as/withas.json
+4
-12
data/1.python初阶/3.进阶语法/4.with-as/withas.md
data/1.python初阶/3.进阶语法/4.with-as/withas.md
+99
-0
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.json
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.json
+4
-10
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.md
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.md
+103
-0
data/1.python初阶/3.进阶语法/6.字符串方法/str.json
data/1.python初阶/3.进阶语法/6.字符串方法/str.json
+4
-18
data/1.python初阶/3.进阶语法/6.字符串方法/str.md
data/1.python初阶/3.进阶语法/6.字符串方法/str.md
+148
-0
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.json
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.json
+4
-9
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.md
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.md
+91
-0
data/1.python初阶/3.进阶语法/8.文件/file_basic.json
data/1.python初阶/3.进阶语法/8.文件/file_basic.json
+4
-15
data/1.python初阶/3.进阶语法/8.文件/file_basic.md
data/1.python初阶/3.进阶语法/8.文件/file_basic.md
+61
-0
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.json
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.json
+4
-15
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.md
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.md
+98
-0
data/1.python初阶/3.进阶语法/9.常用标准库/std01.json
data/1.python初阶/3.进阶语法/9.常用标准库/std01.json
+4
-18
data/1.python初阶/3.进阶语法/9.常用标准库/std01.md
data/1.python初阶/3.进阶语法/9.常用标准库/std01.md
+66
-0
data/1.python初阶/3.进阶语法/9.常用标准库/std02.json
data/1.python初阶/3.进阶语法/9.常用标准库/std02.json
+4
-12
data/1.python初阶/3.进阶语法/9.常用标准库/std02.md
data/1.python初阶/3.进阶语法/9.常用标准库/std02.md
+47
-0
main.py
main.py
+7
-3
src/md.py
src/md.py
+157
-0
未找到文件。
data/1.python初阶/3.进阶语法/1.列表推导式/dict.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"author"
:
"huanhuilong"
,
"if key != 'w'"
:
[
"source"
:
"dict.md"
,
"if key == 'w'"
,
"depends"
:
[],
""
"exercise_id"
:
207
,
],
"type"
:
"code_options"
,
"key: install[key]"
:
[
"notebook_enable"
:
true
"install[key]"
]
},
"source"
:
"dict.py"
,
"depends"
:
[],
"exercise_id"
:
207
,
"type"
:
"code_options"
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/1.列表推导式/dict.md
0 → 100644
浏览文件 @
73558ebb
# Python 字典推导式
Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍
```
python
if
__name__
==
'__main__'
:
install
=
{
"w"
:
{
"platform"
:
"Window"
,
"desc"
:
"请下载 Windows 安装包安装:https://www.python.org/downloads/windows/"
},
"l"
:
{
"platform"
:
"Linux"
,
"desc"
:
"请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/"
,
},
"m"
:
{
"platform"
:
"MacOS"
,
"desc"
:
"请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装"
,
}
}
# TODO(you): 请在此实现 non_windows 赋值代码
print
(
"打算最近只用非Windows系统安装Python了:"
)
print
()
for
key
in
non_windows
:
target
=
non_windows
[
key
]
print
(
"安装平台:{}"
.
format
(
target
[
'platform'
]))
print
(
"安装说明:{}"
.
format
(
target
[
'desc'
]))
print
()
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
install
=
{
"w"
:
{
"platform"
:
"Window"
,
"desc"
:
"请下载 Windows 安装包安装:https://www.python.org/downloads/windows/"
},
"l"
:
{
"platform"
:
"Linux"
,
"desc"
:
"请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/"
,
},
"m"
:
{
"platform"
:
"MacOS"
,
"desc"
:
"请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装"
,
}
}
non_windows
=
{
key
:
install
[
key
]
for
key
in
install
if
key
!=
'w'
}
print
(
"打算最近只用非Windows系统安装Python了:"
)
print
()
for
key
in
non_windows
:
target
=
non_windows
[
key
]
print
(
"安装平台:{}"
.
format
(
target
[
'platform'
]))
print
(
"安装说明:{}"
.
format
(
target
[
'desc'
]))
print
()
```
## 答案
```
python
non_windows
=
{
key
:
install
[
key
]
for
key
in
install
if
key
!=
'w'
}
```
## 选项
### A
```
python
non_windows
=
{
key
:
install
[
key
]
for
key
in
install
:
if
key
!=
'w'
}
```
### B
```
python
non_windows
=
{
install
[
key
]
for
key
in
install
if
key
!=
'w'
}
```
### C
```
python
non_windows
=
{
key
:
install
[
key
]
for
key
in
install
}
```
data/1.python初阶/3.进阶语法/1.列表推导式/dict.py
浏览文件 @
73558ebb
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
# 描述:Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍
# 描述:Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍
def
test
()
:
if
__name__
==
'__main__'
:
install
=
{
install
=
{
"w"
:
{
"w"
:
{
"platform"
:
"Window"
,
"platform"
:
"Window"
,
...
@@ -28,7 +28,3 @@ def test():
...
@@ -28,7 +28,3 @@ def test():
print
(
"安装平台:{}"
.
format
(
target
[
'platform'
]))
print
(
"安装平台:{}"
.
format
(
target
[
'platform'
]))
print
(
"安装说明:{}"
.
format
(
target
[
'desc'
]))
print
(
"安装说明:{}"
.
format
(
target
[
'desc'
]))
print
()
print
()
if
__name__
==
'__main__'
:
test
()
data/1.python初阶/3.进阶语法/1.列表推导式/iter.md
浏览文件 @
73558ebb
...
@@ -21,8 +21,6 @@ if __name__ == '__main__':
...
@@ -21,8 +21,6 @@ if __name__ == '__main__':
请选出下列能
**正确**
实现这一功能的选项。
请选出下列能
**正确**
实现这一功能的选项。
## template
## template
```
python
```
python
...
@@ -71,10 +69,3 @@ reading = (book for book in books if len(book) < 4)
...
@@ -71,10 +69,3 @@ reading = (book for book in books if len(book) < 4)
```
python
```
python
reading
=
(
book
for
book
in
books
:
if
len
(
book
)
<=
4
)
reading
=
(
book
for
book
in
books
:
if
len
(
book
)
<=
4
)
```
```
data/1.python初阶/3.进阶语法/1.列表推导式/list01.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"author"
:
"huanhuilong"
,
"if n % 2 == 0"
:
[
"source"
:
"list01.md"
,
"if n % 2 != 0"
,
"depends"
:
[],
"where n % 2 == 0"
"exercise_id"
:
195
,
],
"type"
:
"code_options"
,
"for n in list"
:
[
"notebook_enable"
:
true
"for n in list:"
]
},
"source"
:
"list01.py"
,
"depends"
:
[],
"exercise_id"
:
195
,
"type"
:
"code_options"
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/1.列表推导式/list01.md
0 → 100644
浏览文件 @
73558ebb
# Python 列表推导式(1)
Python 独步天下的推导式表达式,使用列表推导式过滤出偶数列表
```
python
if
__name__
==
'__main__'
:
list
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
print
(
''
)
print
(
"# 使用列表推导式过滤出偶数"
)
# TODO(you): 请在此实现过滤代码
for
n
in
even
:
print
(
n
)
print
(
''
)
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
list
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
print
(
''
)
print
(
"# 使用列表推导式过滤出偶数"
)
even
=
[
n
for
n
in
list
if
n
%
2
==
0
]
for
n
in
even
:
print
(
n
)
print
(
''
)
```
## 答案
```
python
even
=
[
n
for
n
in
list
if
n
%
2
==
0
]
```
## 选项
### A
```
python
even
=
[
n
for
n
in
list
:
if
n
%
2
==
0
]
```
### B
```
python
even
=
[
n
for
n
in
list
if
n
%
2
!=
0
]
```
### C
```
python
even
=
[
for
n
in
list
if
n
%
2
==
0
]
```
data/1.python初阶/3.进阶语法/1.列表推导式/list02.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"author"
:
"huanhuilong"
,
"if '不热' in d['tags']"
:
[
"source"
:
"list02.md"
,
"if '热' in d['tags']"
,
"depends"
:
[],
""
"exercise_id"
:
195
,
],
"type"
:
"code_options"
,
"d for d"
:
[
"notebook_enable"
:
true
"for d"
]
},
"source"
:
"list02.py"
,
"depends"
:
[],
"exercise_id"
:
214
,
"type"
:
"code_options"
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/1.列表推导式/list02.md
0 → 100644
浏览文件 @
73558ebb
# Python 列表推导式(2)
Python 独步天下的推导式表达式,使用列表推导式过滤不热的天气
```
python
if
__name__
==
'__main__'
:
obj_list
=
[
{
"key"
:
"day1"
,
"value"
:
"大雨哗啦啦啦啦下"
,
'tags'
:
[
"不热"
]},
{
"key"
:
"day2"
,
"value"
:
"很热很热,哥哥不在家,我一个人出去旅行"
,
'tags'
:
[
"热"
]},
{
"key"
:
"day3"
,
"value"
:
"阴天有时下肉丸"
,
'tags'
:
[
"不热"
]}
]
print
(
''
)
print
(
"# 过滤出不热的日子"
)
# TODO(you): 请在此实现过滤代码
for
day
in
non_hot_days
:
print
(
"* [{}]: {}"
.
format
(
day
[
'key'
],
day
[
'value'
]))
print
(
''
)
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
obj_list
=
[
{
"key"
:
"day1"
,
"value"
:
"大雨哗啦啦啦啦下"
,
'tags'
:
[
"不热"
]},
{
"key"
:
"day2"
,
"value"
:
"很热很热,哥哥不在家,我一个人出去旅行"
,
'tags'
:
[
"热"
]},
{
"key"
:
"day3"
,
"value"
:
"阴天有时下肉丸"
,
'tags'
:
[
"不热"
]}
]
print
(
''
)
print
(
"# 过滤出不热的日子"
)
non_hot_days
=
[
d
for
d
in
obj_list
if
'不热'
in
d
[
'tags'
]]
for
day
in
non_hot_days
:
print
(
"* [{}]: {}"
.
format
(
day
[
'key'
],
day
[
'value'
]))
print
(
''
)
```
## 答案
```
python
non_hot_days
=
[
d
for
d
in
obj_list
if
'不热'
in
d
[
'tags'
]]
```
## 选项
### A
```
python
non_hot_days
=
[
d
for
d
in
obj_list
if
'热'
in
d
[
'tags'
]]
```
### B
```
python
non_hot_days
=
[
d
for
d
in
obj_list
:
if
'不热'
in
d
[
'tags'
]]
```
### C
```
python
non_hot_days
=
[
for
d
in
obj_list
if
'不热'
in
d
[
'tags'
]]
```
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.json
浏览文件 @
73558ebb
{
{
"multiline"
:
[
{
"u'二"
:
"'二"
},
{
"encode"
:
"decode"
,
"decode"
:
"encode"
},
{
"'utf-8'"
:
"'utf8'"
}
],
"source"
:
"encode.py"
,
"source"
:
"encode.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
107
,
"exercise_id"
:
107
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/10.字符编码与解码/encode.md
0 → 100644
浏览文件 @
73558ebb
# Python 字符编解码
二进制只是二进制,取决于怎么编码和解码,unicode 转 utf8
```
python
if
__name__
==
'__main__'
:
unicode_str
=
u
'二进制只是二进制,取决于怎么编码和解码'
print
(
unicode_str
)
utf8_str
=
unicode_str
.
encode
(
'utf-8'
)
print
(
utf8_str
)
unicode_str_again
=
utf8_str
.
decode
(
'utf-8'
)
print
(
unicode_str_again
)
assert
unicode_str_again
==
unicode_str
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
unicode_str
=
u
'二进制只是二进制,取决于怎么编码和解码'
print
(
unicode_str
)
utf8_str
=
unicode_str
.
encode
(
'utf-8'
)
print
(
utf8_str
)
unicode_str_again
=
utf8_str
.
decode
(
'utf-8'
)
print
(
unicode_str_again
)
assert
unicode_str_again
==
unicode_str
```
## 答案
```
python
```
## 选项
### A
```
python
```
### B
```
python
```
### C
```
python
```
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"1 if i % 2 == 0 else 0"
:
[
"0 if i % 2 == 0 else 1"
],
"even_count == 4"
:
[
"even_count == 3"
,
"even_count == 5"
],
"i % 2 == 0"
:
[
"i % 2 == 1"
]
},
"source"
:
"triple01.py"
,
"source"
:
"triple01.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
215
,
"exercise_id"
:
215
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/2.三元表达式/triple01.md
0 → 100644
浏览文件 @
73558ebb
# Python 三元表达式(1)
Python 没有问号表达式了!使用三元组表达式统计偶数个数
```
python
if
__name__
==
'__main__'
:
pi
=
[
3
,
14
,
15
,
9
,
26
,
5
,
35
,
8
,
97
,
932
]
even_count
=
0
for
i
in
pi
:
# TODO(you): 请在此实现统计代码
assert
even_count
==
4
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
pi
=
[
3
,
14
,
15
,
9
,
26
,
5
,
35
,
8
,
97
,
932
]
even_count
=
0
for
i
in
pi
:
even_count
+=
1
if
i
%
2
==
0
else
0
assert
even_count
==
4
```
## 答案
```
python
even_count
+=
1
if
i
%
2
==
0
else
0
```
## 选项
### A
```
python
even_count
+=
if
i
%
2
==
0
:
1
else
0
```
### B
```
python
even_count
+=
0
if
i
%
2
==
0
else
1
```
### C
```
python
even_count
+=
1
if
i
%
2
!=
0
else
0
```
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"if i % 4 == 0 else 1 if i % 2 == 0"
:
[
"if i % 2 == 0 else 1 if i % 4 == 0"
],
"2 if i % 4 == 0 else 1 if i % 2 == 0 else 0"
:
[
"1 if i % 4 == 0 else 2 if i % 2 == 0 else 0"
],
"even_count == 6"
:
[
"even_count == 5"
,
"even_count == 7"
]
},
"source"
:
"triple02.py"
,
"source"
:
"triple02.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
203
,
"exercise_id"
:
203
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/2.三元表达式/triple02.md
0 → 100644
浏览文件 @
73558ebb
# Python 三元表达式(2)
使用嵌套的三元组表达式统计数字频率,如果是2的倍数加1,如果是4的倍数加2,否则加0
```
python
if
__name__
==
'__main__'
:
pi
=
[
3
,
14
,
15
,
9
,
26
,
5
,
35
,
8
,
97
,
932
]
even_count
=
0
for
i
in
pi
:
# TODO(You): 请在此实现统计代码
assert
even_count
==
6
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
pi
=
[
3
,
14
,
15
,
9
,
26
,
5
,
35
,
8
,
97
,
932
]
even_count
=
0
for
i
in
pi
:
even_count
+=
2
if
i
%
4
==
0
else
1
if
i
%
2
==
0
else
0
assert
even_count
==
6
```
## 答案
```
python
even_count
+=
2
if
i
%
4
==
0
else
1
if
i
%
2
==
0
else
0
```
## 选项
### A
```
python
even_count
+=
if
i
%
4
==
0
:
2
else
:
if
i
%
2
==
0
:
1
else
:
0
```
### B
```
python
even_count
+=
1
if
i
%
2
==
0
else
2
if
i
%
4
==
0
else
0
```
### C
```
python
even_count
+=
2
if
i
%
2
==
0
else
1
if
i
%
4
==
0
else
0
```
data/1.python初阶/3.进阶语法/3.断言/assert.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"assert key_value_map is not None"
:
[
"assert key_value_map is None"
],
"assert type(key_value_map) == type({})"
:
[
"assert type(key_value_map) == type([])"
],
"assert key is not None"
:
[
"assert key is None"
],
"assert type(key) == type(\"\")"
:
[
"assert type(key) is not str"
]
},
"source"
:
"assert.py"
,
"source"
:
"assert.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
110
,
"exercise_id"
:
110
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/3.断言/assert.md
0 → 100644
浏览文件 @
73558ebb
# Python 断言
所谓断言,就是证明,使用 assert 对输入函数输入参数和函数返回结果分别做前校验和后校验
```
python
def
check_param
(
key_value_map
,
key
):
'''参数校验,断言就是对输入参数的一个证明,这些参数必须符合这些要求
key_value_map: 非空字典
key:非空字符串
'''
# TODO(You): 请在此实现校验代码
def
get
(
key_value_map
,
key
):
check_param
(
key_value_map
,
key
)
return
key_value_map
.
get
(
key
)
def
set
(
key_value_map
,
key
,
value
):
check_param
(
key_value_map
,
key
)
key_value_map
[
key
]
=
value
if
__name__
==
'__main__'
:
key_value_map
=
{}
set
(
key_value_map
,
"test"
,
{})
value
=
get
(
key_value_map
,
"test"
)
print
(
"后校验,返回值必须符合预期"
)
assert
type
(
value
)
==
type
({})
assert
value
==
{}
```
请选出下列对函数 check_param
**实现错误**
的选项。
## template
```
python
def
check_param
(
key_value_map
,
key
):
'''参数校验,断言就是对输入参数的一个证明,这些参数必须符合这些要求'''
assert
key_value_map
is
not
None
assert
type
(
key_value_map
)
==
type
({})
assert
key
is
not
None
assert
type
(
key
)
==
type
(
""
)
def
get
(
key_value_map
,
key
):
check_param
(
key_value_map
,
key
)
return
key_value_map
.
get
(
key
)
def
set
(
key_value_map
,
key
,
value
):
check_param
(
key_value_map
,
key
)
key_value_map
[
key
]
=
value
if
__name__
==
'__main__'
:
key_value_map
=
{}
set
(
key_value_map
,
"test"
,
{})
value
=
get
(
key_value_map
,
"test"
)
print
(
"后校验,返回值必须符合预期"
)
assert
type
(
value
)
==
type
({})
assert
value
==
{}
```
## 答案
```
python
def
check_param
(
key_value_map
,
key
):
assert
key_value_map
assert
type
(
key_value_map
)
==
dict
assert
key
assert
type
(
key
)
==
str
```
## 选项
### A
```
python
def
check_param
(
key_value_map
,
key
):
assert
key_value_map
is
not
None
assert
type
(
key_value_map
)
==
type
({})
assert
key
is
not
None
assert
type
(
key
)
==
type
(
""
)
```
### B
```
python
def
check_param
(
key_value_map
,
key
):
assert
key_value_map
is
not
None
and
type
(
key_value_map
)
==
type
({})
assert
key
is
not
None
and
type
(
key
)
==
type
(
""
)
```
### C
```
python
def
check_param
(
key_value_map
,
key
):
assert
key_value_map
is
not
None
assert
type
(
key_value_map
)
==
dict
assert
key
is
not
None
assert
type
(
key
)
==
str
```
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"with TimeSpan() as t"
:
[
"with TimeSpan()"
],
"__enter__(self)"
:
[
"enter(self)"
],
"__exit__"
:
[
"exit"
]
},
"source"
:
"enter_exit.py"
,
"source"
:
"enter_exit.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
178
,
"exercise_id"
:
178
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/4.with-as/enter_exit.md
0 → 100644
浏览文件 @
73558ebb
# Python with-as 语句(2)
实现一个范围耗时统计类。 实现了
`__enter__`
和
`__exit__`
成员的类,可以通过 with as 语法使用,程序进入和离开范围的时候会自动调用
`__enter__`
和
`__exit__`
方法。
```
python
import
time
class
TimeSpan
:
# TODO(You): 请正确实现计时器的__enter__和__exit成员
if
__name__
==
'__main__'
:
with
TimeSpan
()
as
t
:
for
i
in
range
(
0
,
1000
):
print
(
i
)
```
下列哪个实现是
**错误的**
?
## template
```
python
import
time
class
TimeSpan
:
def
__init__
(
self
)
->
None
:
self
.
start
=
None
def
__enter__
(
self
):
self
.
end
=
None
self
.
start
=
time
.
time
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
end
=
time
.
time
()
print
(
'耗时:{}毫秒'
.
format
((
self
.
end
-
self
.
start
)))
if
__name__
==
'__main__'
:
with
TimeSpan
()
as
t
:
for
i
in
range
(
0
,
1000
):
print
(
i
)
```
## 答案
```
python
class
TimeSpan
:
def
__enter__
(
self
):
return
time
.
time
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
end
=
time
.
time
()
print
(
'耗时:{}毫秒'
.
format
((
end
-
exc_val
)))
```
## 选项
### A
```
python
class
TimeSpan
:
def
__enter__
(
self
):
self
.
start
=
time
.
time
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
end
=
time
.
time
()
print
(
'耗时:{}毫秒'
.
format
((
self
.
end
-
self
.
start
)))
```
### B
```
python
class
TimeSpan
:
def
__enter__
(
self
):
self
.
end
=
None
self
.
start
=
time
.
time
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
end
=
time
.
time
()
print
(
'耗时:{}毫秒'
.
format
((
self
.
end
-
self
.
start
)))
```
### C
```
python
class
TimeSpan
:
def
__enter__
(
self
):
self
.
end
=
None
self
.
start
=
time
.
time
()
return
self
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
end
=
time
.
time
()
print
(
'耗时:{}毫秒'
.
format
((
self
.
end
-
self
.
start
)))
```
data/1.python初阶/3.进阶语法/4.with-as/withas.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"as f"
:
[
""
],
"with "
:
[
""
],
"with open(file, 'w') as f"
:
[
"with f = open(file, 'w')"
]
},
"source"
:
"withas.py"
,
"source"
:
"withas.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
118
,
"exercise_id"
:
118
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/4.with-as/withas.md
0 → 100644
浏览文件 @
73558ebb
# Python with-as 语句(1)
JSON文件读写
```
python
import
json
def
load_json
(
file
):
# TODO(You): 实现加载json文件代码
def
dump_json
(
file
,
obj
):
# TODO(You): 实现将dict写入json文件的代码
if
__name__
==
'__main__'
:
data
=
{
'test'
:
1
,
}
dump_json
(
'test.json'
,
data
)
load_json
(
'test.json'
)
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
import
json
def
load_json
(
file
):
with
open
(
file
,
'r'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
dump_json
(
file
,
obj
):
with
open
(
file
,
'w'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
,
indent
=
2
,
ensure_ascii
=
False
))
if
__name__
==
'__main__'
:
data
=
{
'test'
:
1
,
}
dump_json
(
'test.json'
,
data
)
load_json
(
'test.json'
)
```
## 答案
```
python
def
load_json
(
file
):
with
open
(
file
,
'r'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
dump_json
(
file
,
obj
):
with
open
(
file
,
'w'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
,
indent
=
2
,
ensure_ascii
=
False
))
```
## 选项
### A
```
python
def
load_json
(
file
):
with
open
(
file
,
'w'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
dump_json
(
file
,
obj
):
with
open
(
file
,
'r'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
,
ensure_ascii
=
False
,
indent
=
2
))
```
### B
```
python
def
load_json
(
file
):
with
open
(
file
,
'w'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
dump_json
(
file
,
obj
):
with
open
(
file
,
'r'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
,
indent
=
2
,
ensure_ascii
=
False
))
```
### C
```
python
def
load_json
(
file
):
with
open
(
file
,
'r'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
dump_json
(
file
,
obj
):
with
open
(
file
,
'w'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
,
ensure_ascii
=
False
,
indent
=
2
))
```
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"except Exception as e"
:
[
"except e"
,
"except as e"
],
"logger.error(traceback.format_exc()):"
:
[
"logger.error(e.stack())"
]
},
"source"
:
"try01.py"
,
"source"
:
"try01.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
237
,
"exercise_id"
:
237
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/5.异常捕获预处理/try01.md
0 → 100644
浏览文件 @
73558ebb
# Python 异常处理
'a.json'文件不存在,下面代码会有异常,请编写异常控制代码,控制异常的最小范围,出现异常正常打印日志和堆栈
```
python
import
json
import
traceback
import
logging
logger
=
logging
.
getLogger
(
__name__
)
def
load_json
(
file
):
with
open
(
file
,
'r'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
test
():
# TODO(You):
ret
=
load_json
(
'a.json'
)
if
__name__
==
'__main__'
:
test
()
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
import
json
import
traceback
import
logging
logger
=
logging
.
getLogger
(
__name__
)
def
load_json
(
file
):
with
open
(
file
,
'r'
)
as
f
:
return
json
.
loads
(
f
.
read
())
def
test
():
try
:
ret
=
load_json
(
'a.json'
)
return
{
'err'
:
'success'
,
'result'
:
ret
}
except
Exception
as
e
:
logger
.
error
(
f
"load json exception:
{
str
(
e
)
}
"
)
logger
.
error
(
traceback
.
format_exc
())
return
{
'err'
:
'exception'
}
if
__name__
==
'__main__'
:
test
()
```
## 答案
```
python
try
:
ret
=
load_json
(
'a.json'
)
return
{
'err'
:
'success'
,
'result'
:
ret
}
except
Exception
as
e
:
logger
.
error
(
f
"load json exception:
{
str
(
e
)
}
"
)
logger
.
error
(
traceback
.
format_exc
())
return
{
'err'
:
'exception'
}
```
## 选项
### A
```
python
try
:
ret
=
load_json
(
'a.json'
)
return
{
'err'
:
'success'
,
'result'
:
ret
}
except
Exception
(
e
):
logger
.
error
(
f
"load json exception:
{
str
(
e
)
}
"
)
logger
.
error
(
traceback
.
format_exc
())
return
{
'err'
:
'exception'
}
```
### B
```
python
try
:
ret
=
load_json
(
'a.json'
)
return
{
'err'
:
'success'
,
'result'
:
ret
}
except
e
:
logger
.
error
(
f
"load json exception:
{
str
(
e
)
}
"
)
logger
.
error
(
e
.
format_exc
())
return
{
'err'
:
'exception'
}
```
### C
```
python
try
:
ret
=
load_json
(
'a.json'
)
return
{
'err'
:
'success'
,
'result'
:
ret
}
except
Exception
as
e
:
logger
.
error
(
f
"load json exception:
{
str
(
e
)
}
"
)
logger
.
error
(
e
.
format_exc
())
return
{
'err'
:
'exception'
}
```
data/1.python初阶/3.进阶语法/6.字符串方法/str.json
浏览文件 @
73558ebb
{
{
"multiline"
:
[
{
"l.strip() != ''"
:
"l.strip() == ''"
},
{
"code.split('\n')"
:
"code.split()"
},
{
"int(left)+int(right))"
:
"left+right"
,
"int(left)-int(right))"
:
"left-right"
,
"int(left)*int(right))"
:
"left*right"
,
"int(left)/int(right))"
:
"left/right"
},
{
"and right != '0'"
:
""
}
],
"source"
:
"str.py"
,
"source"
:
"str.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
112
,
"exercise_id"
:
112
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/6.字符串方法/str.md
0 → 100644
浏览文件 @
73558ebb
# Python 字符串方法
Python 的字符串处理, 一个朴实无华的四则运算计算器,批量计算小学生四则运算表达式
```
python
import
re
def
naive_calc
(
code
):
# TODO(You): 请在此实现简易四则运算
def
test
():
code
=
'''
1+2
3+4
5-3
4*3
10/2
'''
naive_calc
(
code
)
if
__name__
==
'__main__'
:
test
()
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
import
re
def
naive_calc
(
code
):
code_lines
=
[
l
for
l
in
code
.
split
(
'
\n
'
)
if
l
.
strip
()
!=
''
]
for
line
in
code_lines
:
ret
=
re
.
match
(
"\s*(\d+)([\+\-\*\/])(\d+)\s*"
,
line
)
left
=
ret
.
group
(
1
)
op
=
ret
.
group
(
2
)
right
=
ret
.
group
(
3
)
if
op
==
'+'
:
print
(
'{}+{}={}'
.
format
(
left
,
right
,
int
(
left
)
+
int
(
right
)))
elif
op
==
'-'
:
print
(
'{}-{}={}'
.
format
(
left
,
right
,
int
(
left
)
-
int
(
right
)))
elif
op
==
'*'
:
print
(
'{}*{}={}'
.
format
(
left
,
right
,
int
(
left
)
*
int
(
right
)))
elif
op
==
'/'
and
right
!=
'0'
:
print
(
'{}/{}={}'
.
format
(
left
,
right
,
int
(
left
)
/
int
(
right
)))
def
test
():
code
=
'''
1+2
3+4
5-3
4*3
10/2
'''
naive_calc
(
code
)
if
__name__
==
'__main__'
:
test
()
```
## 答案
```
python
def
naive_calc
(
code
):
code_lines
=
[
l
for
l
in
code
.
split
(
'
\n
'
)
if
l
.
strip
()
!=
''
]
for
line
in
code_lines
:
ret
=
re
.
match
(
"\s*(\d+)([\+\-\*\/])(\d+)\s*"
,
line
)
left
=
ret
.
group
(
1
)
op
=
ret
.
group
(
2
)
right
=
ret
.
group
(
3
)
if
op
==
'+'
:
print
(
'{}+{}={}'
.
format
(
left
,
right
,
int
(
left
)
+
int
(
right
)))
elif
op
==
'-'
:
print
(
'{}-{}={}'
.
format
(
left
,
right
,
int
(
left
)
-
int
(
right
)))
elif
op
==
'*'
:
print
(
'{}*{}={}'
.
format
(
left
,
right
,
int
(
left
)
*
int
(
right
)))
elif
op
==
'/'
and
right
!=
'0'
:
print
(
'{}/{}={}'
.
format
(
left
,
right
,
int
(
left
)
/
int
(
right
)))
```
## 选项
### A
```
python
def
naive_calc
(
code
):
code_lines
=
[
l
for
l
in
code
.
split
()
if
l
.
strip
()
==
''
]
for
line
in
code_lines
:
ret
=
re
.
match
(
"\s*(\d+)([\+\-\*\/])(\d+)\s*"
,
line
)
left
=
ret
.
group
(
1
)
op
=
ret
.
group
(
2
)
right
=
ret
.
group
(
3
)
if
op
==
'+'
:
print
(
'{}+{}={}'
.
format
(
left
,
right
,
int
(
left
)
+
int
(
right
)))
elif
op
==
'-'
:
print
(
'{}-{}={}'
.
format
(
left
,
right
,
int
(
left
)
-
int
(
right
)))
elif
op
==
'*'
:
print
(
'{}*{}={}'
.
format
(
left
,
right
,
int
(
left
)
*
int
(
right
)))
elif
op
==
'/'
and
right
!=
'0'
:
print
(
'{}/{}={}'
.
format
(
left
,
right
,
int
(
left
)
/
int
(
right
)))
```
### B
```
python
def
naive_calc
(
code
):
code_lines
=
[
l
for
l
in
code
.
split
(
'
\n
'
)
if
l
.
strip
()
!=
''
]
for
line
in
code_lines
:
ret
=
re
.
match
(
"\s*(\d+)([\+\-\*\/])(\d+)\s*"
,
line
)
left
=
ret
.
group
(
1
)
op
=
ret
.
group
(
2
)
right
=
ret
.
group
(
3
)
if
op
==
'+'
:
print
(
'{}+{}={}'
.
format
(
left
,
right
,
left
+
right
))
elif
op
==
'-'
:
print
(
'{}-{}={}'
.
format
(
left
,
right
,
left
-
right
))
elif
op
==
'*'
:
print
(
'{}*{}={}'
.
format
(
left
,
right
,
left
*
right
))
elif
op
==
'/'
and
right
!=
'0'
:
print
(
'{}/{}={}'
.
format
(
left
,
right
,
left
/
right
))
```
### C
```
python
def
naive_calc
(
code
):
code_lines
=
[
l
for
l
in
code
.
split
(
'
\n
'
)
if
l
.
strip
()
!=
''
]
for
line
in
code_lines
:
ret
=
re
.
match
(
line
,
"\s*(\d+)([\+\-\*\/])(\d+)\s*"
)
left
=
ret
.
group
(
1
)
op
=
ret
.
group
(
2
)
right
=
ret
.
group
(
3
)
if
op
==
'+'
:
print
(
'{}+{}={}'
.
format
(
left
,
right
,
left
+
right
))
elif
op
==
'-'
:
print
(
'{}-{}={}'
.
format
(
left
,
right
,
left
-
right
))
elif
op
==
'*'
:
print
(
'{}*{}={}'
.
format
(
left
,
right
,
left
*
right
))
elif
op
==
'/'
and
right
!=
'0'
:
print
(
'{}/{}={}'
.
format
(
left
,
right
,
left
/
right
))
```
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"lambda item: item['age']"
:
[
"lambda (item): item['age']"
,
"lambda item: return item['age']"
,
"item => item['age']"
,
"item -> item['age']"
]
},
"source"
:
"lambda.py"
,
"source"
:
"lambda.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
108
,
"exercise_id"
:
108
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/7.lambda函数/lambda.md
0 → 100644
浏览文件 @
73558ebb
# Python 匿名表达式
Python 高阶函数编程,使用 lambda 表达式获取key,将list转成dict
```
python
def
list_to_dict
(
list
,
key_func
):
d
=
{}
for
item
in
list
:
k
=
key_func
(
item
)
v
=
item
list
=
d
.
get
(
k
)
if
list
is
None
:
d
[
k
]
=
[
v
]
else
:
d
[
k
].
append
(
v
)
return
d
if
__name__
==
'__main__'
:
list
=
[
{
"name"
:
"alice"
,
"age"
:
100
},
{
"name"
:
"middle"
,
"age"
:
100
},
{
"name"
:
"bob"
,
"age"
:
200
}
]
# TODO(You): 请在此调用 list_to_dict 方法,将 list 转成dict
print
(
ret
)
```
请选出下列
**实现错误**
的选项。
## template
```
python
def
list_to_dict
(
list
,
key_func
):
d
=
{}
for
item
in
list
:
k
=
key_func
(
item
)
v
=
item
list
=
d
.
get
(
k
)
if
list
is
None
:
d
[
k
]
=
[
v
]
else
:
d
[
k
].
append
(
v
)
return
d
if
__name__
==
'__main__'
:
list
=
[
{
"name"
:
"alice"
,
"age"
:
100
},
{
"name"
:
"middle"
,
"age"
:
100
},
{
"name"
:
"bob"
,
"age"
:
200
}
]
ret
=
list_to_dict
(
list
,
lambda
item
:
item
[
'age'
])
print
(
ret
)
```
## 答案
```
python
ret
=
list_to_dict
(
list
,
lambda
item
:
item
[
'age'
]
)
```
## 选项
### A
```
python
ret
=
list_to_dict
(
list
,
lambda
item
:
item
[
'age'
])
```
### B
```
python
def
get_key
(
item
):
return
item
[
'age'
]
ret
=
list_to_dict
(
list
,
get_key
)
```
### C
```
python
get_key
=
lambda
item
:
item
[
'age'
]
ret
=
list_to_dict
(
list
,
get_key
)
```
data/1.python初阶/3.进阶语法/8.文件/file_basic.json
浏览文件 @
73558ebb
{
{
"multiline"
:
[
{
"'w'"
:
"'r'"
,
"'r'"
:
"'w'"
},
{
"write"
:
"read"
,
"read"
:
"write"
},
{
"with open('/tmp/test.txt', 'w') as f"
:
"f = open('/tmp/test.txt', 'w')"
,
"with open('/tmp/test.txt', 'r') as f"
:
"f = open('/tmp/test.txt', 'r')"
}
],
"source"
:
"file_basic.py"
,
"source"
:
"file_basic.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
199
,
"exercise_id"
:
199
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/8.文件/file_basic.md
0 → 100644
浏览文件 @
73558ebb
# Python 文件读写
基本文件读写,写入 "test" 到 "/tmp/test.txt",再次打开读取
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
if
__name__
==
'__main__'
:
with
open
(
'/tmp/test.txt'
,
'w'
)
as
f
:
f
.
write
(
"test"
)
with
open
(
'/tmp/test.txt'
,
'r'
)
as
f
:
print
(
f
.
read
())
```
## 答案
```
python
if
__name__
==
'__main__'
:
with
open
(
'/tmp/test.txt'
,
'w'
)
as
f
:
f
.
write
(
"test"
)
with
open
(
'/tmp/test.txt'
,
'r'
)
as
f
:
print
(
f
.
read
())
```
## 选项
### A
```
python
if
__name__
==
'__main__'
:
with
open
(
'/tmp/test.txt'
,
'r'
)
as
f
:
f
.
write
(
"test"
)
with
open
(
'/tmp/test.txt'
,
'w'
)
as
f
:
print
(
f
.
read
())
```
### B
```
python
if
__name__
==
'__main__'
:
with
open
(
'/tmp/test.txt'
,
'r'
)
as
f
:
f
.
write
(
"test"
)
with
open
(
'/tmp/test.txt'
,
'r'
)
as
f
:
print
(
f
.
read
())
```
### C
```
python
if
__name__
==
'__main__'
:
f
=
open
(
'/tmp/test.txt'
,
'w'
):
f
.
write
(
"test"
)
f
=
open
(
'/tmp/test.txt'
,
'r'
):
print
(
f
.
read
())
```
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"break"
:
[
"continue"
],
"in_file.read"
:
[
"in_file.readline"
],
"piece.hex()"
:
[
"piece"
],
"sha256.update"
:
[
"sha256.append"
]
},
"source"
:
"file_piece_sha256.py"
,
"source"
:
"file_piece_sha256.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
213
,
"exercise_id"
:
213
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/8.文件/file_piece_sha256.md
0 → 100644
浏览文件 @
73558ebb
# Python 文件读写
一种基于 sha256 的文件分片 hash 计算方式
```
python
import
hashlib
def
file_piece_sha256
(
in_file_path
,
piece_size
):
sha256
=
hashlib
.
sha256
()
with
open
(
in_file_path
,
"rb"
)
as
in_file
:
# TODO(You): 请在此实现 hash 计算
return
sha256
.
digest
().
hex
()
if
__name__
==
'__main__'
:
ret
=
file_piece_sha256
(
'file_piece_sha256.py'
,
128
)
print
(
"file hash is: "
,
ret
)
```
请选出下列能
**正确**
实现TODO功能的选项。
## template
```
python
import
hashlib
def
file_piece_sha256
(
in_file_path
,
piece_size
):
sha256
=
hashlib
.
sha256
()
with
open
(
in_file_path
,
"rb"
)
as
in_file
:
while
True
:
piece
=
in_file
.
read
(
piece_size
)
if
piece
:
sha256
.
update
(
piece
.
hex
().
encode
(
'utf-8'
))
else
:
break
return
sha256
.
digest
().
hex
()
if
__name__
==
'__main__'
:
ret
=
file_piece_sha256
(
'file_piece_sha256.py'
,
128
)
print
(
"file hash is: "
,
ret
)
```
## 答案
```
python
while
True
:
piece
=
in_file
.
read
(
piece_size
)
if
piece
:
sha256
.
update
(
piece
.
hex
().
encode
(
'utf-8'
))
else
:
break
```
## 选项
### A
```
python
while
True
:
piece
=
in_file
.
read
(
piece_size
)
if
piece
:
sha256
.
update
(
piece
.
hex
().
encode
(
'utf-8'
))
else
:
continue
```
### B
```
python
while
True
:
piece
=
in_file
.
readline
(
piece_size
)
if
piece
:
sha256
.
update
(
piece
.
hex
().
encode
(
'utf-8'
))
else
:
continue
```
### C
```
python
while
True
:
piece
=
in_file
.
read
(
piece_size
)
if
piece
:
sha256
.
update
(
piece
.
encode
(
'utf-8'
))
else
:
continue
```
### D
```
python
while
True
:
piece
=
in_file
.
read
(
piece_size
)
if
piece
:
sha256
.
append
(
piece
.
encode
(
'utf-8'
))
else
:
continue
```
data/1.python初阶/3.进阶语法/9.常用标准库/std01.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"type([])"
:
[
"type({})"
],
"== True"
:
[
"== False"
],
"math.pow(2, 3)"
:
[
"math.pow(2, 4)"
],
"== float"
:
[
"== int"
],
"datetime.now().year"
:
[
"datetime.now().year()"
]
},
"source"
:
"std01.py"
,
"source"
:
"std01.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
222
,
"exercise_id"
:
222
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/9.常用标准库/std01.md
0 → 100644
浏览文件 @
73558ebb
# Python 常用标准库(1)
sys, os, math, time, date
请在使用的时候查文档,例如日期文档:https://docs.python.org/zh-cn/3/library/datetime.html#datetime.datetime
请选出以下所有的 assert
**不全部为 True**
的选项
## template
```
python
import
sys
import
os
import
math
import
time
from
datetime
import
datetime
if
__name__
==
'__main__'
:
assert
type
(
sys
.
argv
)
==
type
([])
assert
os
.
path
.
exists
(
__file__
)
==
True
assert
math
.
pow
(
2
,
3
)
==
8
assert
type
(
time
.
time
())
==
float
assert
datetime
.
now
().
year
>=
2021
```
## 答案
```
python
assert
type
(
sys
.
argv
)
==
type
([])
assert
os
.
path
.
exists
(
__file__
)
==
True
assert
math
.
pow
(
2
,
3
)
==
2
+
2
+
2
+
2
assert
type
(
time
.
time
())
==
int
assert
datetime
.
now
().
year
>=
2121
```
## 选项
### A
```
python
assert
type
(
sys
.
argv
)
==
type
([])
assert
os
.
path
.
exists
(
__file__
)
==
True
assert
math
.
pow
(
2
,
3
)
==
2
*
2
*
2
assert
type
(
time
.
time
())
==
float
assert
datetime
.
now
().
year
>=
2021
```
### B
```
python
assert
type
(
sys
.
argv
)
==
type
([])
assert
os
.
path
.
exists
(
__file__
)
==
True
assert
math
.
pow
(
2
,
3
)
==
8
assert
type
(
time
.
time
())
==
type
(
2.3
)
assert
datetime
.
now
().
year
>=
2021
```
### C
```
python
assert
type
(
sys
.
argv
)
==
list
assert
os
.
path
.
exists
(
__file__
)
==
True
assert
math
.
pow
(
2
,
3
)
==
8
assert
type
(
time
.
time
())
==
float
assert
datetime
.
now
().
year
>=
2021
```
data/1.python初阶/3.进阶语法/9.常用标准库/std02.json
浏览文件 @
73558ebb
{
{
"one_line"
:
{
"json.dumps"
:
[
"json.dump"
],
"json.loads"
:
[
"json.load"
],
"== \"test\""
:
[
"== 2"
]
},
"source"
:
"std02.py"
,
"source"
:
"std02.py"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
196
,
"exercise_id"
:
196
,
"type"
:
"code_options"
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"notebook_enable"
:
true
}
}
\ No newline at end of file
data/1.python初阶/3.进阶语法/9.常用标准库/std02.md
0 → 100644
浏览文件 @
73558ebb
# Python 常用标准库(2)
json 序列化和反序列化
```
python
import
json
if
__name__
==
'__main__'
:
# TODO(You): 请在此实现json序列化和反序列化代码
assert
obj
[
'key'
][
'items'
][
2
]
==
"test"
```
请选出下列能
**正确**
实现这一功能的选项。
## template
```
python
import
json
if
__name__
==
'__main__'
:
obj
=
json
.
loads
(
json
.
dumps
({
"key"
:
{
"items"
:
[
1
,
2
,
"test"
]}}))
assert
obj
[
'key'
][
'items'
][
2
]
==
"test"
```
## 答案
```
python
obj
=
json
.
loads
(
json
.
dumps
({
"key"
:
{
"items"
:
[
1
,
2
,
"test"
]}}))
```
## 选项
### A
```
python
obj
=
json
.
load
(
json
.
dumps
({
"key"
:
{
"items"
:
[
1
,
2
,
"test"
]}}))
```
### B
```
python
obj
=
json
.
loads
(
json
.
dump
({
"key"
:
{
"items"
:
[
1
,
2
,
"test"
]}}))
```
### C
```
python
obj
=
json
.
load
(
json
.
dump
({
"key"
:
{
"items"
:
[
1
,
2
,
"test"
]}}))
```
main.py
浏览文件 @
73558ebb
from
json
import
load
from
json
import
load
from
src.tree
import
TreeWalker
,
load_json
,
dump_json
from
src.tree
import
TreeWalker
from
src.md
import
MDWalker
import
os
import
os
import
re
import
re
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
walker
=
TreeWalker
(
"data"
,
"python"
,
"python"
)
# walker = TreeWalker("data", "python", "python")
walker
.
walk
()
# walker.walk()
md
=
MDWalker
(
'data/1.python初阶/3.进阶语法'
)
md
.
walk
()
src/md.py
0 → 100644
浏览文件 @
73558ebb
import
os
from
sys
import
path
,
version
from
types
import
new_class
from
.tree
import
load_json
,
dump_json
def
simple_list_md_load
(
p
):
with
open
(
p
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
lines
=
f
.
readlines
()
result
=
[]
for
line
in
lines
:
item
=
line
.
strip
(
'
\n
'
)
result
.
append
(
item
)
return
result
def
simple_list_md_dump
(
p
,
lines
):
with
open
(
p
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
'
\n
'
.
join
(
lines
))
def
simple_py_load
(
p
):
with
open
(
p
,
'r'
,
encoding
=
'utf-8'
)
as
f
:
lines
=
f
.
readlines
()
author
=
''
titles
=
[]
contents
=
[]
codes
=
[]
space_id
=
0
max_space_id
=
0
for
i
,
line
in
enumerate
(
lines
):
item
=
line
.
strip
(
'
\n
'
)
if
i
==
0
:
pass
elif
i
==
1
:
author
=
item
[
5
:]
elif
i
==
2
:
titles
.
append
(
item
[
5
:])
elif
i
==
3
:
contents
.
append
(
item
[
5
:])
elif
item
.
startswith
(
'#'
):
contents
.
append
(
item
[
2
:])
else
:
if
item
.
strip
()
==
''
:
codes
.
append
({
'space_id'
:
space_id
,
'code'
:
item
})
if
max_space_id
<
space_id
:
max_space_id
=
space_id
space_id
+=
1
else
:
codes
.
append
({
'space_id'
:
-
1
,
'code'
:
item
})
strip_codes
=
[]
for
line
in
codes
:
if
line
[
'space_id'
]
==
0
or
line
[
'space_id'
]
==
max_space_id
:
pass
else
:
strip_codes
.
append
(
line
[
'code'
])
return
{
'author'
:
author
,
'title'
:
'
\n
'
.
join
(
titles
),
'contents'
:
contents
,
'codes'
:
strip_codes
,
}
class
MDWalker
():
def
__init__
(
self
,
root
)
->
None
:
self
.
root
=
root
def
walk
(
self
):
for
base
,
dirs
,
files
in
os
.
walk
(
self
.
root
):
for
file
in
files
:
if
file
[
-
3
:]
==
'.py'
:
print
(
file
)
py_file
=
os
.
path
.
join
(
base
,
file
)
py
=
simple_py_load
(
py_file
)
md_file
=
os
.
path
.
join
(
base
,
file
.
replace
(
'.py'
,
'.md'
))
json_file
=
os
.
path
.
join
(
base
,
file
.
replace
(
'.py'
,
'.json'
))
print
(
py_file
)
print
(
md_file
)
print
(
json_file
)
if
os
.
path
.
exists
(
md_file
):
continue
md
=
[]
self
.
emit_head
(
md
,
py
)
self
.
emit_template
(
md
,
py
)
self
.
emit_answer
(
md
,
py
)
self
.
emit_options
(
md
,
py
)
simple_list_md_dump
(
md_file
,
md
)
if
os
.
path
.
exists
(
json_file
):
j
=
load_json
(
json_file
)
if
j
.
get
(
'one_line'
):
del
j
[
'one_line'
]
if
j
.
get
(
'multiline'
):
del
j
[
'multiline'
]
j
[
'author'
]
=
py
[
'author'
]
j
[
'notebook_enable'
]
=
True
dump_json
(
json_file
,
j
,
True
,
True
)
def
emit_head
(
self
,
md
,
py
):
title
=
py
[
'title'
]
contents
=
py
[
'contents'
]
codes
=
py
[
'codes'
]
md
.
append
(
f
'#
{
title
}
'
)
md
.
append
(
''
)
for
content
in
contents
:
md
.
append
(
content
)
md
.
append
(
''
)
md
.
append
(
'```python'
)
for
code
in
codes
:
md
.
append
(
code
)
md
.
append
(
'```'
)
md
.
append
(
''
)
md
.
append
(
'请选出下列能**正确**实现这一功能的选项。'
)
md
.
append
(
''
)
def
emit_template
(
self
,
md
,
py
):
codes
=
py
[
'codes'
]
md
.
append
(
f
'## template'
)
md
.
append
(
''
)
md
.
append
(
'```python'
)
for
code
in
codes
:
md
.
append
(
code
)
md
.
append
(
'```'
)
md
.
append
(
''
)
def
emit_answer
(
self
,
md
,
py
):
md
.
append
(
f
'## 答案'
)
md
.
append
(
''
)
md
.
append
(
'```python'
)
md
.
append
(
''
)
md
.
append
(
'```'
)
md
.
append
(
''
)
def
emit_options
(
self
,
md
,
py
):
md
.
append
(
f
'## 选项'
)
md
.
append
(
''
)
for
tag
in
[
'A'
,
'B'
,
'C'
]:
md
.
append
(
f
'###
{
tag
}
'
)
md
.
append
(
''
)
md
.
append
(
'```python'
)
md
.
append
(
''
)
md
.
append
(
'```'
)
md
.
append
(
''
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录