22
33
44## python执行环境:
5+
56解释器环境与选项;
7+
68python解释器启动:
9+
710python [ options] [ -c cmd | filename | - ] [ args]
811
912![ python_options] ( /images/python_options.png )
1013
14+ ---
15+
1116## python解释器环境变量
17+
1218![ python_variables] ( /images/python_variables.png )
1319
1420## python代码的测试、调试与探查
@@ -18,7 +24,7 @@ python [options] [-c cmd | filename | - ] [args]
1824内置函数help()或对象的默认方法__ doc__ 可以显示这些文档字符串;
1925
2026Example:
21-
27+ ``` python
2228In [22 ]: def Sum (num1 ,num2 ):
2329 ... : """ the sumary of num1 and num2.
2430 ...: >>> Sum(2,5)
@@ -44,7 +50,9 @@ Out[24]: 'the sumary of num1 and num2.\n >>> Sum(2,5)\n 7\n >>> Sum(12,
4450```
4551
4652## doctest模块:
53+
4754doctest模块允许在文档字符串内潜入注释以显示各种语句的期望行为,尤其是函数和方法的结果。
55+
4856- 此处的文档字符串看起来如同一个交互式shell会话;
4957- 可用于测试文档是否与程序主体保持同步,或基于文档对程序本身做测试;
5058
@@ -110,12 +118,14 @@ Out[3]: TestResults(failed=1, attempted=2)
110118```
111119
112120创建可自测试的模块:
121+
113122 在模块的尾部添加如下代码即可:
114123 if __name__ == '__main__':
115124 import doctest
116125 doctest.testmod()
117126
118127此类模块在python解释器中直接运行时即能进行自我测试;
128+
119129``` python
120130sslinux@ sslinux- pygo:~ $ python test.py
121131**********************************************************************
@@ -133,14 +143,17 @@ Got:
133143```
134144
135145python:
146+
136147 unitest 统一测试框架
137148
138149
139150## Python访问MariaDB
140151
141152依赖于模块:MySQLdb --依赖于--> easy_install
153+
142154编译、安装;
143155
156+ ``` bash
144157setuptools.1.1.1.tar.gz
145158tar xf setuptools.1.1.1.tar.gz
146159cd setuptools
@@ -156,14 +169,18 @@ python setup.py install
156169vim /etc/ld.so.conf.d/mysql.conf
157170 # 添加MySQL或MariaDB的库文件目录;
158171ldconfig # 使其生效;
172+ ```
173+
159174
160175``` python
161176import MySQLdb as mysql
162177```
178+
163179### 创建MySQL连接:
164- ![ MySQL_Connections] ( /images/mysql_connections.png )
165180
181+ ![ MySQL_Connections] ( /images/mysql_connections.png )
166182
183+ ``` python
167184conn = mysql.connect(host = ' 10.60.4.167' ,user = ' sslinux' ,passwd = ' sslinux' ,db = ' mydb' )
168185conn.stat() # 获取服务器状态;
169186conn.ping() # 测试连通性;
@@ -192,8 +209,13 @@ cur.execute(sqlins)
192209
193210cur.execute(' select * form t1' )
194211cur.fetchall()
212+ ```
213+
214+ ---
195215
196216# 批量插入:
217+
218+ ``` python
197219sqlins = ' insert into t1 (name) value ("tom"),("stu1"),("stu2")'
198220
199221sqlins = ' insert into t1 (name) value (%s )' # 无论数据类型为何,都用%s;
@@ -206,9 +228,10 @@ sqlins = 'insert into t2 (id,name,gerder,age) values (%s,%s,%s,%s)'
206228cur.execute(sqlins,(0 ,' tom' ,' male' ,30 ))
207229cur.execute(' select * from t2' )
208230cur.fetchall()
209-
231+ ```
210232
211233# 一次插入多行数据;
234+ ``` python
212235datains = ((2 ,' str2' ,' female' ,17 ),(3 ,' stu3' ,' male' ,27 ))
213236cur.executemany(sqlins,datains)
214237
@@ -222,9 +245,12 @@ a[2][2]
222245
223246cur.close() # 关闭游标;
224247conn.close() # 关闭连接;
248+ ```
225249
250+ ### 使用流程:
251+
252+ ``` python
226253
227- 使用流程:
228254import MySQLdb as mysql
229255
230256建立连接:
@@ -251,7 +277,7 @@ cur = conn.curson()
251277关闭:
252278 cur.close()
253279 conn.close()
254-
280+ ```
255281
256282## 练习:
257283把/etc/passwd文件中的内容的每一行存储在MariaDB的表中;
0 commit comments