標籤:簡單的 value redis ima https 安裝 color ret class
xshell 進入 虛擬環境 安裝 redis
workon py3env # 進入虛擬環境
pip install redis # 安裝redis
deactivate # 退出虛擬環
簡單的封裝下redis中的list方法:
import redisclass ListDB(): def __init__(self, key): self.conn = redis.StrictRedis(decode_responses=True) self.key = key # 新增資料 def newData(self, *value, lpush=False): """ :param value: 添加的資料 :param lpush: boolen, True表示從頭部添加資料,預設從尾部添加資料 :return: 添加後列表的長度 """ # if lpush == "lpush": # self.conn.lpush(self.key, *list) # elif lpush == "rpush": # self.conn.lpush(self.key, *list) return self.conn.lpush(self.key, *value) if lpush else self.conn.lpush(self.key, *value) # 返回列表中元素的值。index從0開始,當index超出索引時返回null def lindex(self, *list): return self.conn.lindex(self.key, *list) # 查看索引範圍內元素的值 def lrange(self, *data): return self.conn.lrange(self.key, *data) # 返回列表的長度 def llen(self): return self.conn.llen(self.key) # 修改資料 def lset(self, index, value): return self.conn.lset(self.key, index, value) # 刪除資料 def deletePop(self, pop=False): # if data == "lpop": # return self.conn.lpop(self.key) # elif data == "rpop": # return self.conn.rpop(self.key) return self.conn.lpop(self.key) if pop else self.conn.rpop(self.key)li = ListDB(‘table_test‘)a = ‘a‘, ‘b‘# li.newData(‘test‘, ‘vs‘)li.newData(‘test‘, ‘vs‘, lpush=True)print("返回列表中元素的值。index從0開始,當index超出索引時返回null: %s" % li.lindex(0))li.lset(0,‘hello‘)print("查看索引範圍內元素的值: {}".format(li.lrange(0, -1)))print("返回列表的長度: {}".format(li.llen()))li.deletePop(True)li.deletePop()print("查看索引範圍內元素的值: {}".format(li.lrange(0, -1)))
結果:
python_基礎封裝資料庫方法
含笑半步顛√
部落格連結:https://www.cnblogs.com/lixy-88428977
聲明:本文為博主學習感悟總結,水平有限,如果不當,歡迎指正。如果您認為還不錯,歡迎轉載。轉載與引用請註明作者及出處。
python_封裝redis_list方法