- 博客(133)
- 收藏
- 关注
原创 密码安全性检查的代码
# 低级密码要求:# 1. 密码由单纯的数字或字母组成# 2. 密码长度小于等于8位## 中级密码要求:# 1. 密码必须由数字、字母或特殊字符# 2. 密码长度不能低于8位## 高级密码要求:# 1. 密码必须由数字、字母及特殊字符# 2. 密码只能由字母开头# 3. 密码长度不能低于16位def passwd_check(): s...
2020-07-08 09:28:35
569
原创 函数传递参数顺序
# 函数传参顺序: 位置参数 *args 默认参数 **kwargsdef func(a, *args, b = 1, **kwargs): print('a:',a) print('*args:',args) print('b:',b) print('kwargs:',kwargs)func(1,3,9,b='hello',name ='alex', ...
2020-04-14 11:10:02
1389
原创 python 列表深浅拷贝
import copylis = [1,'a', (123), ['abc']]lis1 = lislis2 = lis[:]lis3 = lis.copy()lis4 = copy.deepcopy(lis)lis[-1].append('helloworld')print('lis-->', lis) #lis--> [1, 'a', 123, ['abc', '...
2020-04-14 11:06:57
126
原创 Python抽象方法
from abc import ABCMeta,abstractmethodclass BaseMessage(object):#抽象类方法一 def send(self, x1): """ 必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。 """ raise NotImplement...
2020-04-14 11:03:40
452
原创 python编码问题
# 编码和解码# ascii 码:# 不支持 中文# 支持 英文 数字 符号# 8位 一个字节# gbk码 国标:# 支持 中文,英文,数字,符号# 英文 16位 二个字节# 中文 16位 二个字节# unicode 万国码# 支持 中文,英文,数字,符号# 英文 32 位 四个...
2020-04-14 11:01:02
78
原创 python 打印进度条
import timefor i in range(0,101,2): time.sleep(0.1) char_num = i//2 per_str = '\r%s%%:%s\n'%(i,'*'*char_num)\ if i == 100 else '\r%s%%:%s'%(i, '*'* char_num) print(per_...
2020-04-14 10:59:52
174
原创 __str__和__repr__
class A: passa = A()print(a)#<__main__.A object at 0x000002653840F940>类似于str(a)类似于a.__str__())###class B: def __str__(self): return 'abc'b = B()print(b)#abc类似于str(b)x = st...
2020-04-14 10:57:28
99
原创 python 继承父类方法
import random as rclass Fish: def __init__(self): self.x = r.randint(0,10) self.y = r.randint(0,10) def move(self): self.x = -1 print('位置是:', self.x, self.y...
2020-04-14 10:54:47
964
原创 汉诺塔递归
#汉诺塔递归def hanoi(n, A, B, C): if n > 0: hanoi(n-1, A, C, B)#n-1个盘子,借C柱子移动到 B print('%s->%s' % (A, C))#第N个盘子A移动到C hanoi(n-1, B, A, C)#n-1个盘子,借C柱子移动到 B# h(n) = 2h(n...
2020-04-14 10:47:37
78
原创 map_reduce_filter
# map(lambda x: x + 1, [1, 4, 6, 9, 13])返回一个迭代器# map(),后面的序列按照前面func的逻辑进行处理,然后返回一个迭代器print(list(map(lambda x: x + 1, [1, 4, 6, 9, 13])))print(list(map(lambda x: x.upper(), ['abc', 'ok', 'raim...
2020-04-14 10:46:35
76
原创 zip和enumerate
a = [1, 7, 0]b = ['hello', 'ok']c = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]# for i in zip(a, b):# print(i)## (1, 'hello')# (7, 'ok')print(list(zip(b, c)))# [('hello', 5...
2020-04-14 10:44:48
183
原创 迭代器
#迭代器协议: 对象必须提供一个next方法, 执行此方法会返回下一个值, 或者引发Stopiteration异常,然后终止迭代#可迭代对象 必须实现__iter__ 和 __next__ 方法################################## for循环的原理, 转换为一个可迭代对象,然后调__next__方法,最后捕获异常# for i in ('hello'...
2020-04-14 10:42:50
69
原创 groupby查找输出最大的元素
from operator import itemgetterfrom itertools import groupbystudents = [ {'name': 'raimond', 'age': 19, 'score': 99}, {'name': 'jack', 'age': 30, 'score': 60}, {'name': 'Sue', 'age'...
2020-04-14 10:41:07
203
1
原创 itemgetter联合查找输出最大的元素
from operator import itemgetterfrom itertools import groupbystudents = { {'name': 'Peter', 'age': 19, 'score': 95}, {'name': 'Lily', 'age': 22, 'score': 90}, {'name': 'Stanley', 'ag...
2020-04-14 10:37:13
67
原创 把一个整数翻转
def reverse_int(num): is_negative = False if num < 0:#如果是负数,先转换为正数 is_negative = True num = num * -1 res = 0 while num > 0: res = res * 10 res +=...
2020-04-14 10:33:35
145
原创 生产者消费者模型
import timedef consumer(name): print('我是[%s],开始吃面包了' % name) while True: bread = yield time.sleep(1) print('%s 把【%s】个面包吃掉了' %(name, bread)) def producer(): ...
2020-04-14 10:29:06
58
原创 装饰器(高阶函数_闭包_函数嵌套)
"#装饰器 本质就是函数, 作用是给其他函数添加新功能#1 不修改被修饰函数的源代码#2 不修改被修饰函数的调用方法“”import timedef timmer(func): def wrapper(*args, **kwargs): start_time = time.time() res = func(*args, **kwargs)...
2020-04-14 10:24:37
153
原创 求 100~999 之间的所有水仙花数
思路:整数除10取余数,返回最后一位数print(1651%10)print(435%10)# 整数地板除10,截掉最后一位数print(456546//10)print(1239//10)```pythonfor i in range(100, 1000): sum = 0 temp = i while temp: sum = sum...
2020-04-14 10:19:22
678
原创 生产者消费者模型
import time,randomimport queue,threadingq = queue.Queue()def Producer(name):count = 0while count <10:print(“making…”)time.sleep(random.randrange(3))q.put(count)print(‘Producer %s has produ...
2019-11-14 21:03:32
80
原创 信号量
import threading,timeclass myThread(threading.Thread):def run(self):if semaphore.acquire():print(self.name)time.sleep(3)semaphore.release()if name==“main”:semaphore=threading.Semaphore(10)thr...
2019-11-14 19:31:57
70
原创 event使用
import threadingimport timeclass Boss(threading.Thread):def run(self): print("BOSS:今晚大家都要加班到22:00。") print(event.isSet()) event.set() time.sleep(5) print("BOSS:<22:00>可以下班...
2019-11-14 18:40:25
182
原创 递归锁
import threadingimport timeclass MyThread(threading.Thread):def actionA(self): r_lock.acquire() print(self.name,"get lock_A", time.ctime()) time.sleep(2) r_lock.acquire() print...
2019-11-14 17:37:38
70
原创 python中threading死锁
import threadingimport timeclass MyThread(threading.Thread):def actionA(self): A.acquire() print(self.name,"get lock_A", time.ctime()) time.sleep(2) B.acquire() print(self.name...
2019-11-14 17:33:39
163
原创 python同步锁
import threadingimport timedef sub():global numtemp = numtime.sleep(0.0001)num = temp - 1num = 100l = []for i in range(100):t = threading.Thread(target=sub)t.start()l.append(t)for t in l:...
2019-11-14 00:11:11
116
原创 threading之setDaemon
import threadingimport timedef music():print(‘Listen music %s’% time.ctime())time.sleep(3)print(‘Stop the music %s’% time.ctime())def game():print(‘play games %s’% time.ctime())time.sleep(6)p...
2019-11-13 16:22:26
317
原创 threading之join
import threadingimport timedef Hi(num):print(‘hello %d’%num)time.sleep(3)if name == ‘main’:t1 = threading.Thread(target=Hi, args=(10,))#创建一个线程对象t1.start()t2 = threading.Thread(target=Hi, args...
2019-11-13 10:59:20
103
原创 单向链表
# coding:utf-8class Node(object): '''节点''' def __init__(self, elem): self.elem = elem self.next = Noneclass SingleCycleList(object): '''单向循环链表''' def __init__(self,...
2019-09-03 23:12:39
58
原创 链表
# coding:utf-8class Node(object): '''节点''' def __init__(self, elem): self.elem = elem self.next = Noneclass SingleLinkList(object): '''单链表''' def __init__(self, nod...
2019-09-01 23:43:12
51
原创 双端队列
class Deque(object): def __init__(self): self.__list = [] def add_front(self, item): self.__list.insert(0, item) def add_rear(self, item): self.__list.append(it...
2019-08-28 21:30:50
58
原创 队列queue()
# coding:uft-8class Queue(object): def __init__(self): self.__list = [] def enqueue(self, item): self.__list.append(item) # self.__list.insert(0, item) def deq...
2019-08-28 17:46:35
110
原创 栈的实现Stack()
# coding:uft-8class Stack(object): def __init__(self): self.__list = [] def push(self, item): self.__list.append(item) def pop(self): return self.__list.pop()...
2019-08-28 15:16:50
70
原创 自定义异常类
class Myerror(BaseException): def __init__(self, msg, user): self.msg = msg self.user = user def __str__(self): return ('<<%s %s>>'% (self.user, self.msg))...
2019-08-27 09:58:13
171
原创 异常处理
try: print('start.....') name=1 age l=[] l[3] dic={'a':1} dic['b'] import os os.hello print('end....')except NameError as e: print('NameError: ',e)except...
2019-08-27 09:32:41
67
原创 创建单例
IP = '10.60.1.1'PORT = 5809class MySql: __instance = None def __init__(self, ip, port): self.ip = ip self.port = port @classmethod def single_instance(cls): ...
2019-08-26 22:52:56
54
原创 自定义元类
#自定义元类class Mymeta(type): #控制类的建立 def __init__(self, class_name, class_bases, class_dic): if not class_name.istitle(): raise TypeError('类名必须首字母大写') if not class_di...
2019-08-26 22:22:07
75
原创 定义类的方式
# 用内置的元类type,来实例化得到我们的类class_name='Chinese'class_bases=(object,)class_body="""country="China"def __init__(self,name,age, sex): self.name=name self.age=age self.sex=sexdef speak(self)...
2019-08-26 18:37:44
115
原创 exec函数
code = '''name = 'raimond'age = 30def course(name,age): print(name,age) '''local_dic = {}exec(code, {}, local_dic)print(local_dic)D:\Python\Python36\python.exe D:/Python_test/test2.py...
2019-08-26 17:35:43
125
原创 del魔法方法析构函数
伪代码class Mongdb: def __init__(self, ip, port): self.ip = ip self.port = port self.conn = connect(ip, port)#申请系统资源 def __del__(self):#对象被清除的条件下,自动执行 self.conn....
2019-08-26 17:03:05
194
原创 str 魔法方法
class Foo: def __init__(self, name):#实例化的时候自动触发 self.name = name def __str__(self):#print打印对象的时候自动调用 print(self.name) return 'print'f = Foo('raimond')print(f)l = ...
2019-08-26 15:30:28
98
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人