<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                >[success] # 數據處理模塊model ~~~ 1.save --- 數據保存的方法 2.load --- 數據讀取的方法 3.Model --- 數據存儲的基類 活用__dict__方法 db_path --- 方法用來獲取對應的子類名字數據保存的地址 all --- 方法參考django的all方法 find_all --- django fittler().all() find_by --- 類似djangofirst 返回第一個值 find --- 根據id查詢 delete --- 刪除 __repr__ --- 簡單來說, 它的作用是得到類的 字符串表達 形式 save --- 用來保存數據 4.User --- 用來處理用戶的類 salted_password --- 密碼加鹽的方法 ~~~ >[danger] ##### 代碼 ~~~ import json from utils import log def save(data, path): """ data 是 dict 或者 list path 是保存文件的路徑 """ s = json.dumps(data, indent=2, ensure_ascii=False) with open(path, 'w+', encoding='utf-8') as f: # log('save', path, s, data) f.write(s) # 讀取文件信息 def load(path): with open(path, 'r', encoding='utf-8') as f: s = f.read() # log('load', s) return json.loads(s) class Model: """ 數據存儲的基類 """ # 獲取儲存地址 @classmethod def db_path(cls): """ 那個類使用,獲取那個類的類名,和保存文件一直 """ classname = cls.__name__ path = 'data/{}.txt'.format(classname) return path @classmethod def all(cls): path = cls.db_path() # 讀取文件將字符串轉換成python 對象 models = load(path) # 參考django orm all方法 ms = [cls(m) for m in models] return ms @classmethod def find_all(cls, **kwargs): """ 類似 django fittler().all() :param kwargs: :return: """ ms = [] log('kwargs, ', kwargs, type(kwargs)) k, v ='','' for key, value in kwargs.items(): k, v =key, value all = cls.all() # 得到所有的 對象 for m in all: # 利用dict的魔方方法獲取對應的k,v ,判斷查詢的對象是否存在 if v == m.__dict__[k]: ms.append(m) return ms @classmethod def find_by(cls, **kwargs): """ 用法如下,kwargs 是只有一個元素的 dict u = User.find_by(username='gua') 類似djangofirst 返回第一個值 """ log('kwargs, ', kwargs, type(kwargs)) k, v = '', '' for key, value in kwargs.items(): k, v = key, value all = cls.all() for m in all: # 也可以用 getattr(m, k) 取值 if v == m.__dict__[k]: return m return None @classmethod def find(cls, id): """ 只查詢 id :param id: :return: """ return cls.find_by(id=id) @classmethod def delete(cls, id): """ 刪除 先找到全部,然后刪除指定 """ models = cls.all() index = -1 for i,e in enumerate(models): if e.id == id: index = i break if index == -1: # 沒找到 pass else: models.pop(index) # 根據init 來保存記錄文件 l = [m.__dict__ for m in models] path = cls.db_path() save(l, path) return def __repr__(self): """ __repr__ 是一個魔法方法 簡單來說, 它的作用是得到類的 字符串表達 形式 比如 print(u) 實際上是 print(u.__repr__()) """ classname = self.__class__.__name__ properties = ['{}: ({})'.format(k, v) for k, v in self.__dict__.items()] s = '\n'.join(properties) return '< {}\n{} \n>\n'.format(classname, s) def save(self): """ 先讀取所有內容,在吧要添加的內容添加進去 :return: """ models = self.all() if self.id is None: # 設置 self.id # 先看看是否是空 list if len(models) == 0: # 我們讓第一個元素的 id 為 1(當然也可以為 0) self.id = 1 else: m = models[-1] # log('m', m) self.id = m.id + 1 models.append(self) else: # 更改內容 # index = self.find(self.id) index = -1 for i, m in enumerate(models): if m.id == self.id: index = i break log('debug', index) models[index] = self l = [m.__dict__ for m in models] path = self.db_path() save(l, path) class User(Model): def __init__(self, form): self.id = form.get('id', None) self.username = form.get('username', '') self.password = form.get('password', '') # 加密 def salted_password(self, password, salt='$!@><?>HUI&DWQa`'): """$!@><?>HUI&DWQa`""" import hashlib def sha256(ascii_str): return hashlib.sha256(ascii_str.encode('ascii')).hexdigest() hash1 = sha256(password) hash2 = sha256(hash1 + salt) return hash2 def hashed_password(self, pwd): import hashlib # 用 ascii 編碼轉換成 bytes 對象 p = pwd.encode('ascii') s = hashlib.sha256(p) # 返回摘要字符串 return s.hexdigest() def validate_register(self): pwd = self.password self.password = self.salted_password(pwd) # 查找用戶是否存在 if User.find_by(username=self.username) is None: self.save() return self else: return None def validate_login(self): u = User.find_by(username=self.username) if u is not None: return u.password == self.salted_password(self.password) else: return False ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看