栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python ORM实战之CURD再现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python ORM实战之CURD再现

table_name self._model.__table__ return %s.fdb % os.path.join(config.FDB_HOME, table_name) def __call__(self, model): self._model model self.driver FileDriver(self._path) # save raw by sort if hasattr(self._model, meta ): meta getattr(self._model, meta ) if hasattr(meta, fields ): self.driver.fields getattr(meta, fields ) def to_obj(self, kwargs): return self._model(**kwargs) classmethod def filter_raw(cls, raw, condition): for k, v in condition.iteritems(): if raw.get(k) ! v: return False return True def all(self): result [] contents self.driver.read() for line in contents: obj self.to_obj(line) result.append(obj) return result def query(self, **kwargs): result [] contents self.driver.read() for line in contents: if self.filter_raw(line, kwargs): obj self.to_obj(line) result.append(obj) return result def query_or(self, conditions): result [] contents self.driver.read() for line in contents: ret False for condition in conditions: if self.filter_raw(line, condition): ret True if ret: obj self.to_obj(line) result.append(obj) return result def add(self, **kwargs): self.driver.add(kwargs) def delete(self, pk): key self._model.__primary_key__ with self.driver.lock: result [] for item in self.query(): if getattr(item, key) pk: continue result.append(dict(item)) self.driver.update_no_lock(result)

对象实例定义与抽象 可以说是Model映射层 参考如下

from utils.db.f_manager import baseManager
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267698.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号