Python标准库
来源:互联网 发布:新歌2016网络红歌榜 编辑:程序博客网 时间:2024/06/05 05:02
#!/usr/bin/env python3import osimport shutilimport globimport reimport mathimport randomfrom urllib.request import urlopenimport smtplibfrom datetime import dateimport zlibfrom timeit import Timerimport doctestimport unittest#----------------标准库概览----------------print('123')print(os.getcwd())# os.chdir('/server/accesslogs') # 修改当前的工作目录# os.system('mkdir today') # 执行系统命令 mkdir # dir(os)# help(os)#针对日常的文件和目录管理任务,:mod:shutil 模块提供了一个易于使用的高级接口:# shutil.copyfile('test.py','test1.py')# shutil.move('/build/executables', 'installdir')# 文件通配符# glob模块提供了一个函数用于从目录通配符搜索中生成文件列表:print(glob.glob('*.py'))# 命令行参数# 通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量# import sys# print(sys.argv)# 字符串正则匹配print(re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest'))print(re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat'))# 数学math.cos(math.pi / 4)math.log(1024, 2)# 随机生成random.choice(['apple', 'pear', 'banana'])random.sample(range(100), 10) # sampling without replacement#[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]random.random() # random float#0.17970987693706186random.randrange(6) # random integer chosen from range(6)# 访问互联网# for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):# line = line.decode('utf-8') # print('line = ' + line)# if 'EST' in line or 'EDT' in line:# print(line)# 日期和时间now = date.today()print(now)birthday = date(1993, 6, 22)age = now - birthdayprint('活了{}天'.format(age.days))# 数据压缩s = b'sdllkasmlamdamclamm't = zlib.compress(s) #压缩print(zlib.decompress(t)) #解压zlib.crc32(s) # ?# 性能度量time1 = Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()print(time1)time2 = Timer('a,b = b,a', 'a=1; b=2').timeit()print(time2)# 测试模块# doctest模块提供了一个工具,扫描模块并根据程序中内嵌的文档字符串执行测试。# def average(values):# """Computes the arithmetic mean of a list of numbers.# >>> print(average([20, 30, 70]))# 40.0# """# return (sum(values)/len(values))# doctest.testmod() # unittest模块不像 doctest模块那么容易使用,不过它可以在一个独立的文件里提供一个更全面的测试集class TestStatisticalFunctions(unittest.TestCase): def test_average(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3) self.assertRaises(ZeroDivisionError, average, []) self.assertRaises(TypeError, average, 20, 30, 70)unittest.main() # Calling from the command line invokes all tests
0 0
- python -- Python 标准库
- python 标准库库
- Python标准库
- python标准库
- Python标准库
- python 标准库
- python学习标准库
- Python标准库简介
- python学习标准库
- [Python]标准库--inspect
- Python标准库简介
- python标准库学习
- python标准库1
- Python 标准库 urllib2
- python标准库_csv
- Python安装标准库
- Python标准库介绍
- python标准库
- ubantu 14 关闭防火墙 SELinux
- C++成员变量指针和成员函数指针
- 百度地图探索一
- Android Studio 2.3使用CMake方式NDK入门
- MongoDB:将Json数据直接写入MongoDB的方法
- Python标准库
- 高性能的内核 Socket 实现 Fastsocket
- Mac下安装mysql5.7 完整步骤
- 基础练习 时间转换
- Spring整合Struts2 之 深度解析
- 测试内容(水杯为例)
- 邮票分你一半
- Android TelephonyManager类
- Java 抽象类的使用 接口的使用, 区别等