python_封裝redis_list方法

來源:互聯網
上載者:User

標籤:簡單的   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方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.