小编Ehs*_*ghi的帖子

super()失败并出现错误:当父不从对象继承时,TypeError"参数1必须是type,而不是classobj"

我得到一些我无法弄清楚的错误.任何线索我的示例代码有什么问题?

class B:
    def meth(self, arg):
        print arg

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)
Run Code Online (Sandbox Code Playgroud)

我从"超级"内置方法的帮助下得到了示例测试代码."C"级是

这是错误:

Traceback (most recent call last):
  File "./test.py", line 10, in ?
    print C().meth(1)
  File "./test.py", line 8, in meth
    super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

仅供参考,这是来自python本身的帮助(超级):

Help on class super in module __builtin__:

class super(object)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound …
Run Code Online (Sandbox Code Playgroud)

python inheritance object parent super

186
推荐指数
4
解决办法
11万
查看次数

邪恶的十进制/浮动中的邪恶

我有大量的python代码试图处理4位小数精度的数字,并且由于很多原因我被困在python 2.4中.代码执行非常简单的数学(它是一个信用管理代码,主要采用或添加信用)

它混合了float和Decimal的使用(MySQLdb为SQL DECIMAL类型返回Decimal对象).在使用了几个奇怪的错误之后,我发现所有的根本原因是代码中的一些地方浮动和Decimals被比较.

我得到这样的案例:

>>> from decimal import Decimal
>>> max(Decimal('0.06'), 0.6)
Decimal("0.06")
Run Code Online (Sandbox Code Playgroud)

现在我担心的是我可能无法在代码中捕获所有这些情况.(一个普通的程序员会继续做x> 0而不是x> Decimal('0.0000')并且很难避免)

我想出了一个补丁(灵感来自python 2.7中对十进制包的改进).

import decimal
def _convert_other(other):
     """Convert other to Decimal.

     Verifies that it's ok to use in an implicit construction.
     """
     if isinstance(other, Decimal):
         return other
     if isinstance(other, (int, long)):
         return Decimal(other)
     # Our small patch begins
     if isinstance(other, float):
         return Decimal(str(other))
     # Our small patch ends
     return NotImplemented
decimal._convert_other = _convert_other
Run Code Online (Sandbox Code Playgroud)

我只是在一个非常早期的加载库中执行它,它将通过在比较之前允许float到Decimal转换来更改十进制包行为(以避免将python的默认对象命中对象).

我特意使用"str"而不是"repr",因为它修复了一些float的舍入情况.例如

>>> Decimal(str(0.6))
Decimal("0.6")
>>> Decimal(repr(0.6))
Decimal("0.59999999999999998")
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:我在这里遗漏了什么?这相当安全吗?还是我在这里打破一些东西?(我认为该软件包的作者有很强的理由避免浮动这么多)

python floating-point decimal python-2.4

16
推荐指数
1
解决办法
5588
查看次数

减小MySQL NDB binlog的大小

我正在运行NDB Cluster,我看到在mysql api节点上,有一个非常大的二进制日志表.

+---------------------------------------+--------+-------+-------+------------+---------+
| CONCAT(table_schema, '.', table_name) | rows   | DATA  | idx   | total_size | idxfrac |
+---------------------------------------+--------+-------+-------+------------+---------+
| mysql.ndb_binlog_index                | 83.10M | 3.78G | 2.13G | 5.91G      |    0.56 |
Run Code Online (Sandbox Code Playgroud)

是否有任何建议的方法来减少它的大小而不破坏任何东西?我知道这将限制时间点恢复的时间范围,但数据已经失控,我需要做一些清理工作.

mysql

11
推荐指数
1
解决办法
664
查看次数