2022年8月15日

Python: 练习 不定长参数

a = 'aa' b = 'bb' tuple_ = (a, b) dict_ = {'c': 'cc', 'd': 'dd'} def test(*args, **kwargs): print(args) for item in args: print(item) print(kwargs) for key, value in kwargs.items(): print(key, value) if __name__ == '__main__': test('vv', 'ww', x='xx', y='yy', *tuple_, **dict_) 结果: ('vv', 'ww', 'aa', 'bb') vv ww aa bb {'x': 'xx', 'y': 'yy', 'c': 'cc', 'd': 'dd'} x xx y yy c cc d dd 进程已结束,退出代码0…
2022年7月28日

Python: 练习 获取类中的自定义属性和方法

import inspect class A(object): a = 10 @classmethod def get_attribute(cls): print('1', vars(cls)) print('2', dir(cls)) print('3', cls.__dir__(cls)) print('4', cls.__dict__) print('5', locals()) print('6', globals()) print('*' * 20) attr_ = inspect.getmembers(cls, lambda a: not inspect.ismethod(a)) print('1', attr_) attr_ = filter(lambda a: not a[0].startswith('__'), attr_) print('2', attr_) attr_ = lis…
2022年7月17日

Django Vue 练习

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>XX商城-注册</title> <link rel="stylesheet" type="text/css" href="{{ static('css/rese…
2022年6月22日

Python: 练习 简单http server 简单web框架

2022/02/simple_http_server_web_framework.zip import sys import threading import socket import os.path import json import struct import hashlib import re import framework from content_type import content_type 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…
2022年6月21日

Python: 练习 函数装饰器实现单例

def decorator(cls): _instance = {} print(f"instance: {_instance}") def inner(): if cls not in _instance: _instance[cls] = cls() print(f"instance: {_instance}") return _instance[cls] return inner @decorator class ClassA(object): def __init__(self): self.a = 10 obj_1 = ClassA() obj_2 = ClassA() print(id(obj_1)) print(id(obj_2)) # ClassA = decorator(ClassA) # obj_A = ClassA() # print(obj_A.a)…
2022年6月19日

Python: 练习 PyMySQL

import pymysql connector = pymysql.connect(host="localhost", port=3306, user="root", password="", database="python", charset="utf8") cursor = connector.cursor() print(cursor.execute("show tables;")) print(cursor.fetchall()) print(cursor.execute("desc class;")) print(cursor.fetchall()) print(cursor.execute("select * from class;")) print(cursor.fetchall()) id_ = input("请输入ID:") paramete…