PJzhang: python Basics of seven courses -five

 Cat Ning! ! !

Reference links: Yi Ling -21 micro lesson easily grasp the compulsory entry zero-based python

https://www.liaoxuefeng.com/wiki/1016959663602400

 

Day 13: The Organization Code

Class is a noun that means a class of things, there are multiple instances of objects.

list / tuple / dict / set is a class, [ 'p', 'j', 'z'], ( 'p', 'j', 'z'), { 'age': 19}, { ' p ',' j ',' z '} is an example of the former corresponding to

 

Before and after must remember init is double underlined, otherwise it will error TypeError: Student () takes no arguments

Test Results

 

Write code very often in front of their own time to write code that are defective, not the problem on the code logic, this time in addition to see the error, re-live it again, troubleshooting non-logic errors.

The above is a student number, language, mathematics, English achievement, and now we want to get his highest score and the average score, as well as the total number.

Code bit long

Two classes, student achievement, student achievement table

The first class, initialization student achievement and digitization, all property, there is no way

The second class includes a property, i.e., a list, a list of methods to load data to calculate the maximum subjects, subjects average calculated

Results of the

 

Day 14: Organization Code

The program will appear abnormal exception in the process of implementation

Open a file does not exist, FileNotFoundError

Divided by 0, ZeroDivisionError

Letter string into digital, ValueError

Access the dictionary does not exist in the key, KeyError

 

Case of division by zero, after which the content can not be printed to show only the first line

result

 

Skip the error, the second block, the error content must appear in the error, but also concerned about the case

 

The results, hello xhli not print

 

Abnormal circumstances directly print out

result

 

The word finally is mainly used to clean up, regardless of the previous code has not been implemented, the final will finally print out the

The results, comment out print (1/0) also still show finally print the results.

 

爬虫模拟报错

结果,第一行有显示,但是后面的就报错了url return none:

 

针对未能爬取内容进行略过处理

结果

 

第十五天:开源模块

包package和模块module都是为了对代码进行更好的组织,其中模块中包含类,函数,语句,包中含有__init__.py和模块

import module,来引用一个模块,或者from package import module

模块搜索路径,首先搜索脚本所在目录,然后搜索PYTHONPATH路径列表,这个在python安装的时候已经自定义好路径。

现在搞一个三级目录,python目录下有module1文件夹,module1中含有module2和__init__.py空文件,module2中含有module3和__init__.py空文件,module3下含有__init__.py文件。

python目录下ceshi.py

ceshi1.py引用ceshi.py,ceshi1.py也是在python目录之下

结果,引用成功

自动生成一个缓存文件夹

 

现在我们把ceshi.py放入module3文件夹中去,并调取ceshi.py利用

 

将ceshi1.py放在python目录的上一级目录中:/root/桌面/

执行ceshi1.py,显示如下

如果我们指定了调用的目录名字,那就可以了

命令行输入export PYTHONPATH='/root/桌面/python/',ceshi.py在module3目录之下
再次执行ceshi1.py

但是如果关掉终端窗口,就失效了,还需重新输入export PYTHONPATH='/root/桌面/python/'

 

如果在代码中写好调用路径也可以

如下是结果,这个具有永久性调用,方便他人调用自己的一部分代码块

 

__ name__的使用,ceshi.py和ceshi1.py在同一个目录下

结果

调用一下ceshi.py

结果,引入其他模块的时候,对方的__name__就是它的文件名称,不带py后缀

 在ceshi.py中添加一个if判断语句

结果,说明在其他文件中调用,其中__name__不等于__main__,执行ceshi.py,__name__等于__main__

 

python模块分为自带的标准库模块和开源模块

标准库模块如sys,os,json,re,random,multiprocessing(多线程)等

开源库如下:django,flask可以用于开发后台,BeautifulSoup可以用于网页标题的解析,numpy,scipy可以用于矩阵,matplotlib可以用于生成各种特征图,散点之类的,最后是两个深度学习框架

 

这么多高效模块,安装有几种办法选择

python3 -m pip install requests,安装的都是python3的模块

pip3 install requests,这种方法比较方便,pip3 uninstall requests,这个是卸载包的功能

 

安装anaconda可以实现一劳永逸的安装好绝大部分的开源模块

https://www.anaconda.com/ 

power8和power9是IBM高端服务器中的处理器芯片版本

kali linux自带有hash验证功能,这个很方便

bash '/root/下载/Anaconda3-2019.03-Linux-x86_64.sh'

直接回车,下一步

回复yes

继续回车选择默认选项

选择yes,就生效了

source ~/.bashrc,刷新环境变量,多了一个(base)

conda config --show,可以看到一系列的conda的配置情况,图示没有展示完全

auto_activate_base: True,所以才有了(base)
现在去掉(base)
conda config --set auto_activate_base False

 

如何卸载掉anaconda呢,最简单的办法,rm -rf ~/anaconda3,彻底删掉这个文件夹就好。

 

好的,第13-第15天的完了。

 

Guess you like

Origin www.cnblogs.com/landesk/p/11129924.html
Recommended