Status InitList(LinkList& L){ L = (LinkList)malloc(sizeof(LNode)); if (!L) return ERROR; L->next = nullptr; return OK; }
Status InsertList(LinkList& L, int i, ElemType m){ int j = 1; LNode* pre = L; if (i < 1) return ERROR; while (j++ < i && pre->next != nullptr) pre = pre->next; LNode* newLNode = (LNode*) malloc(sizeof (LNode)); newLNode->data = m; newLNode->next = pre->next; pre->next = newLNode; return OK; }
Status FindInList(LinkList& L, int i, ElemType &m){ int j = 1; LNode* pre = L; if (i < 1) return ERROR; while (j++ < i && pre->next != nullptr) pre = pre->next; if (j - 1 != i || pre->next == nullptr) return ERROR;
m = pre->next->data; return OK; }
intmain(){ LinkList L = nullptr; int i;
if (InitList(L) != OK) { printf("hhhhhh"); return0; }
for (i = 1; i < 10; i++) { if (InsertList(L, i,i) != OK) { printf("hhhhh:%d", i); return0; } else{ printf("insert Ok:%d\n",i); } }
ElemType m = 0; for (i = 1; i < 10; i++) { if (FindInList(L, i, m) == OK) { printf("%d %d\n", i, m); } else{ printf("find wrong:%d\n",i); } }
for c in s: iford(c) inrange(ord('a'),ord('z')+1): c = chr((ord(c)+4-ord('a'))%26+ord('a')) print(c,end='') iford(c) inrange(ord('A'),ord('Z')+1): c = chr((ord(c)+4-ord('A'))%26+ord('A')) print(c,end='')
void setElementAt(Object obj, int index) void removeElementAt(int index) boolean removeElement(Object obj) void removeAllElements() Object elementAt(int index) int indexOf(Object elem) int indexOf(Object elem, int index) int lastIndexOf(Object elem) int lastIndexOf(Object elem, int index) ...
# description:UDP service # author:GuiYoung # time:2021.9.11 import socket
IP = '127.0.0.1' PORT = 8888 BUFLEN = 512
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto('hello, I am client'.encode(),(IP,PORT)) data, _ = s.recvfrom(512) print(f'receive info from service:{data.decode()}') s.close()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# description:UDP service # author:GuiYoung # time:2021.9.11 import socket
IP = '0.0.0.0' PORT = 8888 BUFLEN = 512 print("服务器启动")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((IP,PORT))
#创建连接 r = redis.Redis( host="localhost", port=6379, db=0#逻辑库号数,只能和一个数据库绑定在一起,无法中途改变 )
#创建连接池 try: pool = redis.ConnectionPool( host="localhost", port=6379, db=0, max_connections=20 ) except Exception as e: logging.error(e) else: #从连接池中获取连接,不必关闭,垃圾回收时,连接会自动被归还到连接池 r = redis.Redis( connection_pool=pool ) del r#此时连接已被归还到连接池
字符串操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
r.set("country","Singapore") r.set("city","Singapore") r.expire("city",3) time.sleep(3) try : city = r.get("city").decode("utf-8")#若key不存在,则会报错 except AttributeError: pass else: print(city) r.delete("country","city") #当key已不存在时,不会报错 dict_ = {"country":"德国","city":"柏林"} r.mset(dict_) result = r.mget("country","city")#返回元组类型 for one in result: print(one.decode("utf-8"))
列表操作
1 2 3 4 5 6 7 8
try: r.rpush("dname","d1","d2","d3") r.lpop("dname") result=r.lrange("dname",0,-1)#返回元组类型 for one in result: print(one.decode("urf-8")) except Exception as e: pass
集合操作
1 2 3 4 5 6 7 8 9
r.sadd("employee",8001,8002,8003) r.srem("employee",8001) result = r.members("employ")#元组
#有序集合 dict_ = {"QAQ":0,"QWQ":0,"QVQ":0}#前面为词条,后面为分数值 r.zadd("keyword",dict_) r.zincrby("keyword","10","QAQ")#为QAQ词条加十分 result = r.zrevrange("keyword",0,-1)#元组类型
哈希表
1 2 3 4 5
r.hmset("she",{"name":"Lily","age":19}) #redis4.0开始弃用hmset,推荐使用hset r.hset("she","dream","flower and star") r.hdel("she","dream") print(r.hexists("she","name"))#返回True result = r.hgetall("she")#元组
import logging import random from concurrent.futures import ThreadPoolExecutor import redis from models import redis_db
defbuy(user_id): try: con_for_buy = redis.Redis( connection_pool=redis_db.pool ) pipline = con_for_buy.pipeline() if con_for_buy.exists("kill_flag"): total_num = int(con_for_buy.get("kill_total").decode("utf-8")) num = int(con_for_buy.get("kill_num").decode("utf-8")) pipline.watch("kill_num", "kill_user") # watch()里放即将要修改的key if num < total_num: pipline.incr("kill_num") pipline.lpush("kill_user", user_id) pipline.execute() pipline.reset() except Exception as e: logging.error(e) finally: del con_for_buy
defis_param_exist(): try: con = redis.Redis( connection_pool=redis_db.pool ) if con.exists("kill_total", "kill_flag", "kill_num"): returnTrue else: returnFalse except Exception as e: logging.error(e) returnFalse finally: del con
defcreate_param(): try: con = redis.Redis( connection_pool=redis_db.pool ) con.set("kill_total", 20) con.set("kill_num", 0) con.set("kill_flag", 1) con.expire("kill_flag",600) returnTrue except Exception as e: logging.error(e) returnFalse finally: del con
defdelete_param(): try: con = redis.Redis( connection_pool=redis_db.pool ) con.delete("kill_total","kill_num","kill_flag","kill_user") returnTrue except Exception as e: logging.error(e) returnFalse finally: del con
if __name__ == "__main__": s = set() whileTrue: iflen(s) == 1000: break s.add(random.randint(10000,100000))
if delete_param() isnotTrue: exit() if create_param() isnotTrue: exit() if is_param_exist() isnotTrue: exit()
try: executor_ = ThreadPoolExecutor(50) for i inrange(0,1000): user_id = s.pop() executor_.submit(buy,user_id) con = redis.Redis( connection_pool=redis_db.pool ) except Exception as e: logging.error(e) finally: del executor_,con
RPUSH blue QAQ QWQ QVQ # 右加数据 LPUSH blue ToT # 左加数据 LSET blue 2 hhh # L代表list,代表修改第三个元素 LRANGE blue 0 -1 # 输出指定范围数据,开始为0,结束为-1 LLEN blue # 获取列表长度 LINDEX blue 0 #获取列表某个元素 LINSERT blue BEFORE QWQ 233 #将233元素插到QWQ之前 LINSERT blue AFTER QWQ 666 #将666元素插到QWQ之后 LPOP blue # 删除最左侧的元素 RPOP blue # 删除最右侧的元素 LREM blue 1 QAQ # 删除1个QAQ元素
# Getter function @property deffirst_name(self): return self._first_name # Repeated property code, but for a different name (bad!) @property deflast_name(self): return self._last_name
# Setter function @first_name.setter deffirst_name(self, value): ifnotisinstance(value,str): raise TypeError("Expected a string") self._first_name = value
@last_name.setter deflast_name(self, value): ifnotisinstance(value,str): raise TypeError("Expected a string") self._last_name = value
# Deleter function (optional) @first_name.deleter deffirst_name(self): raise AttributeError("Can't delete attribute")
if __name__ == '__main__': person_1 = Person("Lily") print(person_1.first_name) # Calls the getter try: person_1.last_name = 1 except Exception as error: print(error)
try: del person_1.first_name except Exception as error: print(error)