文档:Tutorial/docs/L0/Python at camp3 · InternLM/Tutorial · GitHub
视频:Python 前置基础知识
作业:Tutorial/docs/L0/Python/task.md at camp3 · InternLM/Tutorial · GitHub
任务一:
使用本地VScode和插件Remote-SSH 远程连接到InternLM开发机。打开文件到root目录,在root目录下创建wordcount.py。
以下是wordcount.py的代码:
import re
def wordcount(text):
text = re.sub(r'[^\w\s]','',text)
text = text.lower()
text = text.split()
counts = {}
for i in text:
if i not in counts:
counts[i] = 1
else:
counts[i] += 1
return counts
if __name__ == '__main__':
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
print(wordcount(text))
用Run Python --> Run Python File in Terminal 运行一下wordcount.py 看看有没有报错。结果出来了,代码没有问题。
任务二 (1):
在Vscode debug 单个python file,试试debug刚才写的wordcount.py,设置断点之后在右上角选Python Debugger: Debug Python File:
点击Run and Debug之后开始debug:可以在terminal那里看到程序运行到断点上面一行输出的结果。
debug能发现代码里的错误,把句子分解成单词之后,text 不再是一个句子,而是由每个单词组成的list,所以直接这样print会出错。
修正后的wordcount.py:
import re
def wordcount(text):
print('原来输入的句子: ' + text)
text = re.sub(r'[^\w\s]','',text)
print('去掉标点符号之后: ' + text)
text = text.lower()
print('所有字母都变成小写字母之后: ' + text)
words = text.split()
print('把句子分解成单词: ' + str(words))
counts = {}
for i in words:
if i not in counts:
counts[i] = 1
else:
counts[i] += 1
return counts
if __name__ == '__main__':
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
print(wordcount(text))
修正错误后重新debug
修正后debug就没有出现错误提示了。
任务二 (2):
使用命令行开启debug server, create a launch json file ->Python Debugger ->Remote Attach ->localhost ->5768
配置完成后,pip install debugpy
(base) root@intern-studio-50138422:~# python -m debugpy --listen 5678 --wait-for-client wordcount.py
并且连接到debug derver
开启debug server 跟之前debug单个python file的功能是一样的。
最终的结果: