SQLAlchemy:混合值对象,查询元组结果

发布于 2024-11-25 07:09:29 字数 1012 浏览 0 评论 0原文

我正在尝试遵循构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-12-02 07:09:29

虽然它不是一个完全成熟的 Python 功能,但约定是以与函数和方法相同的方式注释属性,即通过在属性下方放置一个字符串。像上面这样的评论会被 Sphinx 等工具接收。您可以在 http://www.sqlalchemy.org/docs/orm/mapper_config.html#sqlalchemy.orm.mapper.Mapper.class_manager

编辑:哦为什么它有一个实际的“.key”。当您说:

for row in session.query(MyClass.mycustomthing, MyClass.myothercustomthing):
   print row.word, row.someotherword

“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:

for row in session.query(MyClass.mycustomthing, MyClass.myothercustomthing):
   print row.word, row.someotherword

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文