交互式shell中的值相同

时间:2017-03-04 23:08:18

标签: python django interactive

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。如果问题不清楚,请告诉我。

2 个答案:

答案 0 :(得分:0)

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

TypeError: Cannot read property 'scheduler$3' of null

答案 1 :(得分:0)

(扩展我的意见)
首先,我建议不要以您展示的方式打印您的测试结果。 Python有几个测试模块;我的选择是Pytest

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

What is the best way to compare floats for almost-equality in Python?是对问题的一个很好的解释,有一些有用的代码。