2022年6月16日

Python: 练习 深拷贝和浅拷贝

from copy import copy, deepcopy a_1 = [1, ["a", "b"]] a_2 = a_1.copy() print(id(a_1), id(a_1[0]), id(a_1[1])) print(id(a_2), id(a_2[0]), id(a_2[1])) print("*" * 20) # 浅拷贝 a_1 = [1, ["a", "b"]] a_2 = copy(a_1) print(id(a_1), id(a_1[0]), id(a_1[1])) print(id(a_2), id(a_2[0]), id(a_2[1])) print("*" * 20) # 深拷贝 a_1 = [1, ["a", "b"]] a_2 = deepcopy(a_1) print(id(a_1), id(a_1[0]), id(a_1[1])) print(id(a_2), id(a_2[0])…
2022年6月16日

Python: 练习 yield 创建生成器

generator_1 = (i for i in range(3)) print(generator_1) # print(*generator_1) print(next(generator_1)) print("*" * 20) for i in generator_1: print(i) print("*" * 20) def fibonacci(num): a = 0 b = 1 i = 0 while i < num: result = a a, b = b, a + b i += 1 yield result generator_2 = fibonacci(100) print(next(generator_2)) print("*" * 20) for i in generator_2: print(i)…
2022年6月16日

Python: 练习 with语句和上下文管理器 __enter__() 和 __exit__()方法 @contextmanager 装饰器

from contextlib import contextmanager class OpenFile(object): def __init__(self, file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): self.file = file self.mode = mode def __enter__(self): print("OpenFileA上文方法") self.fd = open(self.file, self.mode) return self.fd def __exit__(self, exc_type, exc_val, exc_tb): print("OpenFileA下文方法") self.fd.close() @contextman…
2022年6月16日

Python: 练习 property属性 将方法当做属性进行使用

class PersonA(object): def __init__(self): self.__age = 20 @property def age(self): return self.__age @age.setter def age(self, new): self.__age = new class PersonB(object): def __init__(self): self.__age = 20 def get_age(self): return self.__age def set_age(self, new): self.__age = new age = property(get_age, set_age) person = PersonA() print("PersonA") print(person.age) person.age = 30 print(person.age)…
2022年6月16日

Python: 练习 装饰器 带有参数的装饰器 类装饰器 多个装饰器装饰一个函数

练习 # 装饰器 def decorator_1(func): print(f"装饰1:{func}") def inner(*args, **kwargs): print(f"inner1:{func}") return func(*args, **kwargs) return inner # 带有参数的装饰器 def decorator_2(arg): def decorator_2_(func): print(f"装饰2:{func}") def inner(*args, **kwargs): print(f"inner2:{func}") print(f"decorator_2 参数:{arg}") return func(*args, **kwargs) return inner return decorator_2_ # 类装饰器 class Decorator_cls(o…
2022年6月16日

Python: 闭包 closure

1 闭包三要素 必须有一个内嵌函数 内嵌函数必须引用外部函数中变量 外部函数返回值必须是内嵌函数 2 语法 # 语法 def 外部函数名(参数): 外部变量 def 内部函数名(参数): 使用外部变量 return 内部函数名 # 调用 变量 = 外部函数名(参数) 变量(参数) 3 练习 def closure(name): def func(word): nonlocal name name = name + "说" print(f"{name}:{word}") return func closure_1 = closure("张三") closure_2 = closure("李四") closure_1("嘻嘻!") closure_2("哈哈") 运行结果: 张三说:嘻嘻! 李四说:哈哈…
2022年6月12日

Python: 练习 正则表达式 匹配文件名/扩展名

# 匹配文件名 import re file_name0 = "123.test.abc.zip" file_name1 = ".test.abc.zip" file_name2 = ".zip" file_name3 = "test" re_ = r".*?(?=\.[^\.]*$)|[^\.]+" file_name_0 = re.match(re_, file_name0) file_name_1 = re.match(re_, file_name1) file_name_2 = re.match(re_, file_name2) file_name_3 = re.match(re_, file_name3) print(file_name_0) print(file_name_1) print(file_name_2) print(file_name_3) # 匹配扩展名 impor…
2022年6月10日

Python: 练习 Excel 删除某列数据

from openpyxl import * import os def modify_file(): file_list = os.listdir(cwd) for file in file_list: if os.path.isfile(file): print(f"公司:{file} 文件修改 >>> ", end="") wb = load_workbook(file) ws_name=wb.sheetnames[0] ws = wb[ws_name] ws.delete_cols(1) # 删除第 13 列数据 ws['A1'].comment = None wb.save(file) print(f"OK!") print("全部完成") if __name__ == '__main__': os.chdir("../files") cwd = os.getcwd() dir…
2022年6月10日

Python: 练习 移动文件

from openpyxl import * import os def modify_file(): file_list = os.listdir(cwd) for file in file_list: if os.path.isfile(file): print(f"公司:{file} 文件修改 >>> ", end="") wb = load_workbook(file) ws_name=wb.sheetnames[0] ws = wb[ws_name] ws.delete_cols(1,3) # 删除第 13 列数据 ws['A1'].comment = None wb.save(file) print(f"OK!") print("全部完成") if __name__ == '__main__': os.chdir("../files1") cwd = os.getcwd()…
2022年6月8日

Python: 练习 socket tcp网络通信 收发信息/大文件 简单http server Chunk编码分块传输

import sys import threading import socket import os.path import json import struct import hashlib import re class NetworkCommunicateRole(object): def role_choose(self): while True: role = input("请选择角色 Http服务(h)/服务端(s)/客户端(c):") if role == "h": return "http_server", elif role == "s": return "server", elif role == "c": while True: server_ip_ = input("请输入服务端IP地址和端口(x.x.x.x:xx):") server_ip = self.check_ip(ser…