Python之常用模块一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/10/14 14:27
# @Author : Ropon
# @File : 18_01.py

#常用模块一
# random
# time
# os
# sys
# 序列化模块
# json
# pickle
# collections 数据类型扩展,面向对象进阶

import random

# 取随机小数:数学计算
# print(random.random()) #取0-1之间的小数
# print(random.uniform(1, 2)) 取1-2之间的小数

# 取随机整数:彩票 抽奖
# print(random.randint(1, 2)) #[1, 2] 顾头也顾尾
# print(random.randrange(1, 2)) #[1, 2) 顾头不顾尾
# print(random.randrange(1, 100, 2)) #[1, 3, 5, 7 ...) 顾头不顾尾

# 从一个列表中随机取值:抽奖
# lst = ['a', 'b', (1, 2), 123]
# print(random.choice(lst))
# print(random.sample(lst, 2)) #随机取2个不重复的值

# 打乱一个列表顺序,在原列表基础上修改,节省空间:洗牌
# random.shuffle(lst)
# print(lst)

# 验证码
# 4位数字验证码
# 6位数字验证码
# 6位数字+字母验证码

# 4位或6位数字验证码
# def func(n=6):
# s = ''
# for i in range(n):
# num = (random.randint(0, 9))
# s += str(num)
# return s
#
# print(func())
# print(func(4))

# 6位数字+字母验证码
# print(chr(65)) #65+25 97+25
# print(chr(97))
# def func(n=6):
# s = ''
# for i in range(n):
# num = str(random.randint(0, 9))
# alpha_upper = chr(random.randint(65, 90))
# alpha_lower = chr(random.randint(97, 122))
# res = random.choice([num, alpha_upper, alpha_lower])
# s += res
# return s
#
# print(func())
# print(func(4))

# 4位数字验证码
# 6位数字验证码
# 6位数字+字母验证码

# def func(n = 6, alpha = True):
# s = ''
# for i in range(n):
# num = str(random.randint(0, 9))
# if alpha:
# alpha_upper = chr(random.randint(65, 90))
# alpha_lower = chr(random.randint(97, 122))
# num = random.choice([num, alpha_upper, alpha_lower])
# s += num
# return s
#
# print(func())
# print(func(4,False))



# 发红包
# 红包数量 钱数
# 拼手机红包

# def hongbao(amount=5, count=3):
# s = []
# min = 1 #分
# amount = amount * 100 #单位分
# max = amount - (count-1) #单位分
# for i in range(count-1):
# # num = random.randint(0, amount)
# num = random.uniform(0, int(amount))
# while num < min or num > max:
# num = random.uniform(0, int(amount))
# amount -= num
# s.append(round((num / 100), 2))
# s.append(round((amount / 100), 2))
# random.shuffle(s)
# return s

# def hongbao(amount=5, count=3):
# s = []
# min = 1 #分
# amount = amount * 100 #单位分
# max = amount - (count-1) #单位分
# for i in range(count-1):
# num = random.randint(0, amount)
# # while num < min or num > max:
# # num = random.randint(0, amount)
# amount -= num
# s.append(round((num / 100), 2))
# s.append(round((amount / 100), 2))
# random.shuffle(s)
# return s
#
# print(hongbao(1, 6))

# 0--------------------------------20
# 0 2 4 10 16 20

# def hongbao(amount=10, count=5):
# ret = random.sample(range(1, amount * 100), count - 1) #分为单位
# ret.extend([0, amount * 100]) #追加多个值0 amount * 100
# ret.sort() #排序
# # return [((ret[i+1] - ret[i]) / 100 for i in range(num))] # 列表生产式
# for i in range(count):
# yield (ret[i+1] - ret[i]) / 100
#
# res = hongbao(10, 6)
# for i in res:
# print(i)

# def redbags(money, num=10):
# choice = random.sample(range(1, money * 100), num - 1)
# choice.extend([0,money*100])
# choice.sort()
# return [(choice[i + 1] - choice[i]) / 100 for i in range(num)]
# print(redbags(100,10))

# def hongbao(money,n):
# k=n
# sum=0#sum为前n个人抢得的总和,为了方便计算最后一个人的金额,初始值为0
# round=n#剩余人次
# while k>1:
# current_money = money # 当前剩余的钱,初始值为money
# for i in range(1,n+1):
# get_money=random.randint(0,int(2*current_money/round))
# print('id[%s] have geted money %s'%(i,get_money))
# current_money -= get_money
# round -= 1
# sum += get_money
# k-=1
#
#
# if k==1:#最后一个人,分得剩余的所有
# print('id[%s] have geted money %s'%(n,money-sum))
# print(current_money)
# print(hongbao(100,10))

def hongbao(amount=5, count=3):
s = []
sum = 0 #分
amount = amount * 100 #单位分
for i in range(count-1): #0 1 2
max = amount -sum - (count - 1 - i) # 单位分 500-(3-1-0)=498
num = random.randint(1, max) #1,498 400
s.append((num / 100))
sum = sum + num
s.append((amount - sum) / 100)
random.shuffle(s)
return s

print(hongbao(100, 8))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/10/14 15:42
# @Author : Ropon
# @File : 18_02.py

import time

# time.sleep(2)
# print("123")

# 时间格式
# 字符串 格式化时间 人能识别的
# 结构化时间
# 时间戳 1970-1-1 0:0:0 英国伦敦时间 以秒为单位 计算机能识别的
# print(time.time())
# print(time.strftime('%Y-%m-%d %H:%M:%S'))
# print(time.strftime('%y-%m-%d %H:%M:%S'))
# print(time.strftime('%c'))

# print(time.localtime())
# time.struct_time(tm_year=2018, tm_mon=10, tm_mday=14, tm_hour=15, tm_min=49, tm_sec=45, tm_wday=6, tm_yday=287, tm_isdst=0)
# tm_isdst=0 是否夏令时

# struct_time = time.localtime()
# print(struct_time.tm_year)

# 时间戳换成字符串时间
# print(time.time())
# struct_time = time.localtime(1500000000)
# ret = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)
# print(ret)

# 字符串转换时间戳
# struct_time = time.strptime('2018-8-8', '%Y-%m-%d')
# print(struct_time)
# res = time.mktime(struct_time)
# print(res)

# 1.查看一下2000000000时间戳时间表示的年月日
# 时间戳 --> 结构化 --> 格式化
# struct_time = time.localtime(2000000000)
# # print(struct_time)
# print(time.strftime('%Y-%m-%d', struct_time))

# 2.将2008-8-8转换成时间戳时间
# struct_time = time.strptime('2008-8-8', '%Y-%m-%d')
# print(time.mktime(struct_time))

# 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
#2018-10-1
# def get_time():
# st = time.localtime()
# st2 = time.strptime('{0}-{1}-1'.format(st.tm_year, st.tm_mon), '%Y-%m-%d')
# return time.mktime(st2)
# print(get_time())

# 4.计算时间差 - 函数
# 2018-8-19 22:10:8 2018-8-20 11:07:3
# 经过了多少时分秒

# def func(str_time1, str_time2):
# # str_time1 = '2018-8-19 22:10:8'
# # str_time2 = '2018-8-20 11:07:3'
# struct_t1 = time.strptime(str_time1, '%Y-%m-%d %H:%M:%S')
# struct_t2 = time.strptime(str_time2, '%Y-%m-%d %H:%M:%S')
# timetamp1 = time.mktime(struct_t1)
# timetamp2 = time.mktime(struct_t2)
# sub_time = timetamp2 - timetamp1
# gm_time = time.gmtime(sub_time)
# #1970-1-1 00:00:00
# return '过去了{0}年{1}月{2}日{3}时{4}分{5}秒'.format(gm_time.tm_year-1970, gm_time.tm_mon-1, gm_time.tm_mday-1,
# gm_time.tm_hour, gm_time.tm_min, gm_time.tm_sec)
# s1 = '2018-10-12 22:10:10'
# s2 = '2018-10-14 23:09:07'
# print(func(s1, s2))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/10/14 20:36
# @Author : Ropon
# @File : 18_03.py

import sys

# sys 是和Python解释器交互模块
# sys.argv
# print(sys.argv) # argv的第一个参数 是python命令的后面的值

# usr = sys.argv[1]
# pwd = sys.argv[2]
# if usr == 'ropon' and pwd == '123':
# print("登录成功")
# else:
# exit()

# 1、程序员 运维人员 在命令行运行代码
# 2、操作系统 陷入input事件,程序阻塞,从而退出CPU竞争

# sys.path
# print(sys.path) # 模块搜索路径
# 模块存在硬盘中,impor 模块 --> 载入内存中
# 也就是说一个模块是否能被顺利导入,导入时依次从sys.path 列表中每个元素对应路径开始寻找。
# 自定义模块
# sys.modules
import re
# print(sys.modules) # 是我们导入到内存中所有模块的名字或者说是模块的内存地址
# print(sys.modules['re'].findall('\d', 'dfsd43534'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/10/14 21:03
# @Author : Ropon
# @File : 18_04.py

import os
# os 与操作系统交互的模块
# os.makedirs('test1/test2') # 可创建多级文件夹
# os.mkdir('test3') # 仅创建一级文件夹
# os.mkdir('test3/test4') # 仅创建一级文件夹

# os.removedirs('test1/test2') # 若目录为空,则删除,并递归上级目录,若为空,则删除,以此类推
# os.rmdir('test3/test4') # 删除单级空目录,若目录不为空则无法删除报错

# print(os.stat('D:/Ropon/Seafile/Work/python/code/day18/18_04.py')) # 获取文件或目录信息

# exec/eval 执行的是字符串类型的python代码
# os.system() 和 os.popen() 执行的是字符串类型的命令行代码
# os.system('test.bat') # 运行shell/bat命令,直接显示,或者说执行操作系统命令,没有返回值
# st = os.popen('test.bat').read() # 运行shell/bat命令,通过.read()获取执行结果
# print(st)
# ret = os.popen('dir')
# s = ret.read()
# # print(s)
# print(s.split('\n'))

# os.listdir() # 列出指定目录下所有文件和子目录,包括隐藏文件,并以列表形式打印
# os.path.join(path1[, path2[, ...]]) # 拼接目录,第一个绝对路径之前的参数将被忽略
# print(os.path.join('test', 'D:\Ropon', 'python'))
# files = os.listdir('D:\Ropon\Seafile\Work\python\code')
# for path in files:
# print(os.path.join('D:\Ropon\Seafile\Work\python\code', path))

# os.getcwd() # 获取当前工作目录,即当前python脚本工作目录
# print(os.getcwd())

# os.chdir() # 切换当前脚本工作目录
# os.chdir('D:\Ropon\Seafile\Work')
# ret = os.popen('dir')
# s = ret.read()
# print(s)

# os.rename("oldname","newname") # 重命名文件/目录
# os.remove() # 删除一个文件