作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我定义了以下 SQLAlchemy 类:
Base = sqlalchemy.ext.declarative.declarative_base()
class NSASecrets(Base):
__tablename__ = 'nsasecrets';
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True);
text = sqlalchemy.Column(sqlalchemy.String);
author = sqlalchemy.Column(sqlalchemy.String);
现在我想做的是能够根据某种逻辑屏蔽“作者”字段,例如:
if (allowed):
nsasecrets = session.query(NSASecrets,**mask=False**);
else:
nsasecrets = session.query(NSASecrets,**mask=True**);
for nsasecret in nsasecrets:
print '{0} {1}'.format(author, text);
因此,根据这个“mask”参数,我希望在 False 情况下输出为“John Smith” - 输出未屏蔽,或者当输出被屏蔽时为“J*** **h” 。现在显然我可以在这个打印中完成它,但问题是打印分散在代码周围,我看到以受控集中方式执行此操作的唯一方法是创建具有已屏蔽值的 SQLAlchemy 对象。那么有没有众所周知的解决方案呢?或者我应该创建自己的 session 管理器来重载“查询”接口(interface),还是我缺少一些其他可能的解决方案?
谢谢
最佳答案
这通常是我们在 Python 中使用名为 descriptors 的东西所做的事情。 。将描述符与 SQLAlchemy 映射列组合的一种简单方法是使用 synonym ,尽管同义词此时有点过时,有利于一个不太“神奇”的系统,称为 hybrids 。此处可以使用其中任何一种,下面是混合的示例:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, synonym_for
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
class NSASecrets(Base):
__tablename__ = 'nsasecrets'
id = Column(Integer, primary_key=True)
_text = Column("text", String)
_author = Column("author", String)
def _obfuscate(self, value):
return "%s%s" % (value[0], ("*" * (len(value) - 2)))
@hybrid_property
def text(self):
return self._obfuscate(self._text)
@text.setter
def text(self, value):
self._text = value
@text.expression
def text(cls):
return cls._text
@hybrid_property
def author(self):
return self._obfuscate(self._author)
@author.setter
def author(self, value):
self._author = value
@author.expression
def author(cls):
return cls._author
n1 = NSASecrets(text='some text', author="some author")
print n1.text
print n1.author
请注意,这与查询没有太大关系。在数据到达行集中时对其进行格式化的想法是一种不同的方式,并且也有一些方法可以实现这一点,但如果您只关心引用“文本”和“作者”的打印语句,将其保留为 python 访问模式可能更方便。
关于python - SQLAlchemy - 动态掩码对象中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185824/
我是一名优秀的程序员,十分优秀!