来自交互式 shell 的相同值

Same value from the interactive shell

def unittest(execute=False):
                with timetravel(datetime(2017,03,01)) as now:
                    loan2 = compute_loan(user, {'product_id': 1,
                                       'frequency': '1week',
                                       'first_debit_date': now })

            if not execute:
                print('You are wrong')
                pass
            else:
                field_list = CustomerProduct._meta.get_fields()
                mylist = []
                exclude = ['id','contract', 'actor_actions',
                        'locked', 'description', 'target_actions',
                        'action_object_actions', 'created', 'modified',
                        'request', 'withdrawal_first_date', 'deposit_date']
                table = Texttable(max_width = 6000)
                for field in field_list:
                    if field.name not in exclude:
                        mylist.append([field.name, getattr(loan2.request.customer_product, field.name)])
                table.add_rows(mylist)
                print(table.draw())

使用这个函数,我创建了一个包含 field.namegetattr(loan2.request.customer_product, field.name) 的列表列表。例如,

+----------------------------------------------------+---------+
| debit_frequency                                    | 1week   |
+----------------------------------------------------+---------+
| broker_pmt                                         | 17.865  |
+----------------------------------------------------+---------+
| broker_pre_withdrawal_daily_interest_rate          | 0.001   |
+----------------------------------------------------+---------+
| broker_total_post_pre_withdrawal_interest_amount   | 139.908 |
+----------------------------------------------------+---------+

问题是我更喜欢

+----------------------------------------------------+-----------------+
| debit_frequency                                    | u'1week'        |
+----------------------------------------------------+-----------------+
| broker_pmt                                         | 17.865903434    |
+----------------------------------------------------+-----------------+
| broker_pre_withdrawal_daily_interest_rate          | 0.0014348934    |
+----------------------------------------------------+-----------------+
| broker_total_post_pre_withdrawal_interest_amount   | 139.9083498304  |
+----------------------------------------------------+-----------------+

事实上,当我用类似

的方式查询数据库时,这些值是相同的
In [7]: loaner.request.customer.sum_new_pmt
Out[7]: 56.000121522936645

我想要交互式 shell 的返回值。谁能帮我解决这个问题?我可以在代码中修改什么?

谢谢!

P.S.如果问题不清楚请告诉我。

根据这里的 TextTable 源代码 https://github.com/foutaise/texttable/blob/master/texttable.py#L304 看来你应该使用这样的东西

....
table = Texttable(max_width = 6000)
table.set_precision(20)
....

(扩展我的评论)
首先,我建议不要按照您显示的方式打印测试结果。 Python有几个测试模块;我的选择是 Pytest.

由于从字符串到浮点数的转换并不总是给出与字符串对应的准确值(因为二进制浮点数不能准确表示该值),所以在比较浮点数时最好避免比较平等,但要留出一些回旋余地。我的建议是为此使用 round (或等效地,字符串格式达到特定精度) - 例如assert round(139.908, 3) == round(computed_value, 3).

What is the best way to compare floats for almost-equality in Python? 很好地解释了这些问题,并提供了一些有用的代码。