【发布时间】:2014-06-07 13:46:06
【问题描述】:
我正在尝试从 sqlalchemy 中的 String 派生 MyFraction 类并覆盖运算符,以便字符串像分数一样排序。
下面的代码失败了(我意识到 self 是比较器工厂),但我不确定如何让它工作:
from sqlalchemy import Column, Integer, String
from fractions import Fraction
class MyFraction(String):
class comparator_factory(String.Comparator):
def __eq__(self, other) :
return Fraction(self) == Fraction(other)
def __ne__(self, other) :
return Fraction(self) != Fraction(other)
def __gt__(self, other) :
return Fraction(self) > Fraction(other)
def __lt__(self, other) :
return Fraction(self) < Fraction(other)
从关于 sqlalchemy (http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#redefining-and-creating-new-operators) 的文档中,很难知道应该怎么做(有一个叫做“goofy”的操作,但我很难理解它与上述问题的关系)。
【问题讨论】:
标签: python sqlalchemy