相同的值来自交互式外壳


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

我希望从交互式外壳中返回值。谁能帮我解决这个问题?我可以在代码中修改什么?

谢谢!

P.S。请让我知道问题是否不清楚。

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

由于从字符串到float的转换并不总是给出与字符串相对应的确切值(因为二进制浮点不能准确表示值),在比较浮点数时,最好避免比较平等,但是要允许一些摆动的房间。我的建议是为此使用round(或等效地,将字符串格式化为特定的精度) - 例如assert round(139.908, 3) == round(computed_value, 3)

比较浮力在Python中几乎平等的最佳方法是什么?是对问题的很好的解释,并有一些有用的代码。

根据TextTable源代码此处https://github.com/foutaise/texttable/blob/master/master/texttable.py#l304似乎应该使用

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

最新更新