【发布时间】:2017-03-04 23:08:18
【问题描述】:
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.name 和getattr(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.如果问题不清楚,请告诉我。
【问题讨论】:
-
值相同。不同之处在于 interavtive shell 正在打印值的
repr。 -
@cco 是的,但是使用此代码,我想打印代表。你能修改代码来允许这样的事情吗?
-
如果不了解
Texttable,很难知道建议什么,但我认为使用mylist.append([field.name, repr(getattr(loan2.request.customer_product, field.name))])可能会成功。 -
@cco 你几乎是对的,但它没有显示所有小数。有了这个新结构,它附加了
Decimal('...')和u'...',但它没有显示所有适当的小数。这样做的目的是我想在单元测试中使用 assertEqual 函数。所以我需要参数中的确切值。例如,self.assertEqual(loaner.request.customer.sum_new_pmt, 56.000)是错误的,而self.assertEqual(loaner.request.customer.sum_new_pmt, 56.000121522936645)是正确的 -
我认为进行比较的最明智的方法是将浮点数(预期的和计算的)舍入到特定的精度,然后再进行比较。
标签: python django interactive