python类(class)
短信预约 -IT技能 免费直播动态提醒
类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例,把所有相似的功能都封装到一个类里,最理想的情况是一个程序只有一个主程序,然后在程序里实例化类
练习代码如下:
# coding: utf-8
__author__ = 'www.py3study.com'
import os
import platform
import itertools
import time
class makepassowrd(object):
def __init__(self):
self.rawlist = []
self.denylist = ['', ' ', '@']
self.pwlist = []
self.minlen = 6
self.maxlen = 16
self.timeout = 3
self.flag = 0
self.run = {
'0': exit,
'1': self.getrawlist,
'2': self.adddenylist,
'3': self.clearrawlist,
'4': self.setrawlist,
'5': self.modifypasswordlen,
'6': self.createpasswordlist,
'7': self.showpassword,
'8': self.createpasswordfile
}
self.main()
def main(self):
while True:
self.mainMenu()
op = input(u'输入选项:')
if op in map(str, range(len(self.run))):
self.run.get(op)()
else:
self.tipMainMenuInputError()
continue
def mainMenu(self):
self.clear()
print(u'||')
print(u'='*40)
print(u'||')
print(u'|| 0:退出程序')
print(u'|| 1:输入密码原始字符')
print(u'|| 2:添加非法字符到列表')
print(u'|| 3:清空原始密码列表')
print(u'|| 4:整理原始密码列表')
print(u'|| 5:改变默认密码长度({}-{})'.format(self.minlen, self.maxlen))
print(u'|| 6:创建密码列表')
print(u'|| 7:显示所有密码')
print(u'|| 8:创建字典文件')
print(u'||')
print(u'=' * 40)
print(u'||')
print(u'当前非法字符为: {}'.format(self.denylist))
print(u'当前原始密码元素为: {}'.format(self.rawlist))
print(u'共有密码{}个'.format(len(self.pwlist)))
if self.flag:
print(u'已在当前目录创建密码文件dict.txt')
else:
print(u'尚未创建密码文件')
def clear(self):
OS = platform.system()
if (OS == u'Windows'):
os.system('cls')
else:
os.system('clear')
def tipMainMenuInputError(self):
self.clear()
print(u'只能输入0-7的整数,等待{}秒后重新输入'.format(self.timeout))
time.sleep(self.timeout)
def getrawlist(self):
self.clear()
print(u'输入回车后直接退出')
print(u'当前原始密码列表为:{}'.format(self.rawlist))
st = None
while not st == '':
st = input("请输入密码元素字符串:")
if st in self.denylist:
print(u'这个字符串是预先设定的非法字符串')
continue
else:
self.rawlist.append(st)
self.clear()
print(u'输入回车后直接退出')
print(u'当前原始密码列表为:{}'.format(self.rawlist))
def adddenylist(self):
self.clear()
print(u'输入回车后直接退出')
print(u'当前非法字符为:{}'.format(self.denylist))
st = None
while not st == '':
st = input(u'请输入需要添加的非法字符串:')
self.denylist.append(st)
self.clear()
print(u'输入回车后直接退出')
print(u'当前非法字符串为:{}'.format(self.denylist))
def clearrawlist(self):
self.rawlist = []
def setrawlist(self):
a = set(self.rawlist)
b = set(self.denylist)
self.rawlist = []
for str in set(a - b):
self.rawlist.append(str)
def modifypasswordlen(self):
self.clear()
while True:
print(u'当前密码长度为{}-{}'.format(self.minlen, self.maxlen))
min = input('请输入密码最小长度:')
max = input('请输入密码最大长度:')
try:
self.minlen = int(min)
self.maxlen = int(max)
except ValueError:
print('密码长度只能输入数字[6-18]')
break
if self.minlen not in range(6,19) or self.maxlen not in range(6,19):
print('密码长度只能输入数字[6-18]')
self.minlen = 6
self.maxlen = 16
continue
if self.minlen == self.maxlen:
res = input(u'确定将密码长度设定为{}吗?(Yy/Nn):'.format(self.minlen))
if res not in list('YyNn'):
print(u'输入错误,请重新输入')
continue
elif res in list('Yy'):
print(u'好吧,你确定就好')
break
else:
print(u'给个机会改一下吧')
continue
elif self.minlen > self.maxlen:
print(u'最小长度比最大长度还大,可能吗?请重新输入')
self.minlen = 6
self.maxlen = 16
continue
else:
print(u'设置完毕,等待{}秒后回主菜单'.format(self.timeout))
time.sleep(self.timeout)
break
def createpasswordlist(self):
titlelist = []
swapcaselist = []
for st in self.rawlist:
swapcaselist.append(st.swapcase())
titlelist.append(st.title())
sub1 = []
sub2 = []
for st in set(self.rawlist + titlelist + swapcaselist):
sub1.append(st)
for i in range(2, len(sub1) + 1):
sub2 += list(itertools.permutations(sub1, i))
for tup in sub2:
PW = ''
for subPW in tup:
PW += subPW
if len(PW) in range(self.minlen, self.maxlen + 1):
self.pwlist.append(PW)
else:
pass
def showpassword(self):
for i in range(len(self.pwlist)):
if i % 4 == 0:
print("%s\n" % self.pwlist[i])
else:
print("%s\t" % self.pwlist[i])
print('\n')
print(u'等待{}秒,回到主菜单'.format(self.timeout))
time.sleep(self.timeout)
def createpasswordfile(self):
print(u'当前目录下创建字典文件:dict.txt')
time.sleep(self.timeout)
with open('dict.txt', 'w+') as fp:
for pw in self.pwlist:
fp.write(pw)
fp.write('\n')
self.flag = 1
if __name__ == '__main__':
mp = makepassowrd()
应该看到的结果
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341