Python 来自交互式shell的相同值
使用此函数,我创建了一个列表,其中包含Python 来自交互式shell的相同值,python,django,interactive,Python,Django,Interactive,使用此函数,我创建了一个列表,其中包含field.name和getattr(loan2.request.customer\u product,field.name)。比如说, def unittest(execute=False): with timetravel(datetime(2017,03,01)) as now: loan2 = compute_loan(user, {'product_id': 1,
field.name
和getattr(loan2.request.customer\u product,field.name)
。比如说,
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())
问题是我更喜欢这样的东西
+----------------------------------------------------+---------+
| 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 |
+----------------------------------------------------+-----------------+
我想要交互式shell的返回值。有人能帮我解决这个问题吗?我可以在代码中修改什么
谢谢
p.S.如果问题不清楚,请告诉我。根据这里的TextTable源代码,您似乎应该使用类似的东西
In [7]: loaner.request.customer.sum_new_pmt
Out[7]: 56.000121522936645
根据这里的TextTable源代码,您似乎应该使用类似的东西
In [7]: loaner.request.customer.sum_new_pmt
Out[7]: 56.000121522936645
(扩展我的评论)首先,我建议不要以显示的方式打印测试结果。Python有几个测试模块;我的选择是 由于从字符串到浮点的转换并不总是给出与字符串对应的精确值(因为二进制浮点不能精确表示值),因此在比较浮点数时,最好避免进行相等比较,但要留出一些回旋余地。我的建议是为此使用
round
(或等效地,字符串格式设置为特定精度)-例如断言round(139.908,3)=round(computed_value,3)
是对问题的一个很好的解释,带有一些有用的代码 (扩展我的评论)首先,我建议不要以显示的方式打印测试结果。Python有几个测试模块;我的选择是 由于从字符串到浮点的转换并不总是给出与字符串对应的精确值(因为二进制浮点不能精确表示值),因此在比较浮点数时,最好避免进行相等比较,但要留出一些回旋余地。我的建议是为此使用
round
(或等效地,字符串格式设置为特定精度)-例如断言round(139.908,3)=round(computed_value,3)
是对问题的一个很好的解释,带有一些有用的代码 这些值是相同的。不同之处在于Interavitive shell正在打印值的
repr
。@cco是的,可以,但有了这段代码,我想打印repr。你能修改代码来允许这样的事情吗?如果不知道任何关于Texttable
,很难知道该建议什么,但我认为使用mylist.append([field.name,repr(getattr(loan2.request.customer\u product,field.name))
可能会奏效。@cco你几乎是对的,但它没有显示所有的小数。在这个新结构中,它附加了Decimal(“…”)
和u“…”
,但它没有显示所有适当的小数。这样做的目的是我想在单元测试中使用assertEqual函数。所以我需要参数中的精确值。例如,self.assertEqual(loaner.request.customer.sum\u new\u pmt,56.000)
是错误的,而self.assertEqual(loaner.request.customer.sum\u new\u pmt,56.000121522936645)
是正确的。我认为进行比较的最明智的方法是对浮动进行四舍五入(预期和计算)在比较它们之前达到特定的精度。值是相同的。不同之处在于Interavitive shell正在打印值的repr
。@cco是的,可以,但有了这段代码,我想打印repr。你能修改代码来允许这样的事情吗?如果不知道任何关于Texttable
,很难知道该建议什么,但我认为使用mylist.append([field.name,repr(getattr(loan2.request.customer\u 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)
是正确的。我认为进行比较的最明智的方法是在比较之前将浮动(预期和计算)四舍五入特定的精度。是的,但对于Django而言,目前是assertEqual,但对于Django而言,目前是assertEqual