SQLAlchemy:混合值对象,查询元组结果
我正在尝试遵循构建 custom 的文档中的示例使用混合值对象的比较器,
class CaseInsensitiveWord(Comparator):
"Hybrid value representing a lower case representation of a word."
def __init__(self, word):
if isinstance(word, basestring):
self.word = word.lower()
elif isinstance(word, CaseInsensitiveWord):
self.word = word.word
else:
self.word = func.lower(word)
def operate(self, op, other):
if not isinstance(other, CaseInsensitiveWord):
other = CaseInsensitiveWord(other)
return op(self.word, other.word)
def __clause_element__(self):
return self.word
def __str__(self):
return self.word
key = 'word'
"Label to apply to Query tuple results"
但是,我不明白为什么将其添加到类定义的末尾:
key = 'word'
"Label to apply to Query tuple results"
这是做什么的?
I am trying to follow the examples from the documentation on building custom comparators using hybrid value objects,
class CaseInsensitiveWord(Comparator):
"Hybrid value representing a lower case representation of a word."
def __init__(self, word):
if isinstance(word, basestring):
self.word = word.lower()
elif isinstance(word, CaseInsensitiveWord):
self.word = word.word
else:
self.word = func.lower(word)
def operate(self, op, other):
if not isinstance(other, CaseInsensitiveWord):
other = CaseInsensitiveWord(other)
return op(self.word, other.word)
def __clause_element__(self):
return self.word
def __str__(self):
return self.word
key = 'word'
"Label to apply to Query tuple results"
I don't understand, however, why this was added to the end of the class definition:
key = 'word'
"Label to apply to Query tuple results"
What is this for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然它不是一个完全成熟的 Python 功能,但约定是以与函数和方法相同的方式注释属性,即通过在属性下方放置一个字符串。像上面这样的评论会被 Sphinx 等工具接收。您可以在 http://www.sqlalchemy.org/docs/orm/mapper_config.html#sqlalchemy.orm.mapper.Mapper.class_manager。
编辑:哦为什么它有一个实际的“.key”。当您说:
“word”和“someotherword”元组键是每个比较器上“.key”的值。如果你要对其调用 label() ,则会将其更改为其他内容。我不知道那是绝对必要的。
While it's not a fully baked Python feature, a convention is to comment attributes in the same way as functions and methods, i.e. by placing a string below the attribute. Comments like the above are picked up by tools like Sphinx. You can see examples of these docstrings getting generated in places like http://www.sqlalchemy.org/docs/orm/mapper_config.html#sqlalchemy.orm.mapper.Mapper.class_manager.
edit: oh why it has an actual ".key". When you say:
the "word" and "someotherword" tuple keys are the value of ".key" on each comparator. if you were to call label() on it, that would change it to something else. I don't know that it's strictly necessary to be there at all.