Len功能不像预期的那样行为
我正在构建一个函数,该函数正在基于传递的标签的dist上应用AWS实例。
预期行为: 当传递多个test_customer_id
时,该函数应返回标签的字典:
{'foo': 'bar', 'Account': 'shared'}
单位测试功能的当前行为仅返回:
{'foo': 'bar', 'Account': [['test_customer_id1', 'test_customer_id2']]}
我该如何解决此问题?
def get_acct_value(tags, customer_id, ceid):
customer_id = [customer_id]
if "Account" in tags:
if tags["Account"] == customer_id:
pass
else:
if len(customer_id) > 1:
tags["Account"] = "shared"
elif len(customer_id) == 1:
tags["Account"] = customer_id
else:
raise exceptions.CustomerNotFoundError(f"No customer(s) found on {ceid}")
else:
if len(customer_id) > 1:
tags["Account"] = "shared"
elif len(customer_id) == 1:
tags["Account"] = customer_id
else:
raise exceptions.CustomerNotFoundError(f"No customer(s) found on {ceid}")
return tags
单元测试:
TAGS_NO_VALUE = {'foo': 'bar'}
TEST_CUSTOMER_ID_LIST = ["test_customer_id1", "test_customer_id2"]
TEST_CEID = "test_ceid"
def test_get_account_value_customer_list():
response = tagging.get_acct_value(TAGS_NO_VALUE, TEST_CUSTOMER_ID_LIST, TEST_CEID)
print(response)
其他单元测试:
所有三个测试都应返回:{'accult':customer_id,'foo':'bar'}
TEST_CUSTOMER_ID = "test_customer_id"
TAGS_UNEXPECTED_VALUE = {'Account': '', 'foo': 'bar'}
TAGS_EXPECTED_VALUE = {'Account': customer_id, 'foo': 'bar'}
def test_get_acct_value_no_value():
response = tagging.get_acct_value(TAGS_NO_VALUE,
TEST_CUSTOMER_ID, TEST_CEID)
print(response)
def test_get_acct_value_unexpected_value():
response = tagging.get_acct_value(TAGS_UNEXPECTED_VALUE, TEST_CUSTOMER_ID, TEST_CEID)
print(response)
def test_get_acct_value_expected_value():
response = tagging.get_acct_value(TAGS_EXPECTED_VALUE, TEST_CUSTOMER_ID, TEST_CEID)
print(response)
I'm building a function that's applying an additional tag to an aws instance based on a dict of tags that are passed in.
Expected behavior:
When more than one TEST_CUSTOMER_ID
is are passed in, the function should return the following dictionary of tags:
{'foo': 'bar', 'Account': 'shared'}
The current behavior of the unit test function is only returning:
{'foo': 'bar', 'Account': [['test_customer_id1', 'test_customer_id2']]}
How can I fix this?
def get_acct_value(tags, customer_id, ceid):
customer_id = [customer_id]
if "Account" in tags:
if tags["Account"] == customer_id:
pass
else:
if len(customer_id) > 1:
tags["Account"] = "shared"
elif len(customer_id) == 1:
tags["Account"] = customer_id
else:
raise exceptions.CustomerNotFoundError(f"No customer(s) found on {ceid}")
else:
if len(customer_id) > 1:
tags["Account"] = "shared"
elif len(customer_id) == 1:
tags["Account"] = customer_id
else:
raise exceptions.CustomerNotFoundError(f"No customer(s) found on {ceid}")
return tags
Unit test:
TAGS_NO_VALUE = {'foo': 'bar'}
TEST_CUSTOMER_ID_LIST = ["test_customer_id1", "test_customer_id2"]
TEST_CEID = "test_ceid"
def test_get_account_value_customer_list():
response = tagging.get_acct_value(TAGS_NO_VALUE, TEST_CUSTOMER_ID_LIST, TEST_CEID)
print(response)
Other unit tests:
All three tests should return: {'Account': customer_id, 'foo': 'bar'}
TEST_CUSTOMER_ID = "test_customer_id"
TAGS_UNEXPECTED_VALUE = {'Account': '', 'foo': 'bar'}
TAGS_EXPECTED_VALUE = {'Account': customer_id, 'foo': 'bar'}
def test_get_acct_value_no_value():
response = tagging.get_acct_value(TAGS_NO_VALUE,
TEST_CUSTOMER_ID, TEST_CEID)
print(response)
def test_get_acct_value_unexpected_value():
response = tagging.get_acct_value(TAGS_UNEXPECTED_VALUE, TEST_CUSTOMER_ID, TEST_CEID)
print(response)
def test_get_acct_value_expected_value():
response = tagging.get_acct_value(TAGS_EXPECTED_VALUE, TEST_CUSTOMER_ID, TEST_CEID)
print(response)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在这里很复杂。让我们以不同的方式打破此功能:
首先,我们知道,如果
customer_ids
列表为空,我们应该提出例外。 首先执行此操作。它被标记为“界限检查”,应在尝试处理任何数据之前完成 - 这样您就不必在代码的每个分支上重做它。其次,我们知道,如果列表大于一个,我们希望我们的标签被“共享”,这意味着我们有一个以上的客户ID。如果我们的列表大于一个,则将其设置为具有名称
tag
的临时变量。如果列表正是一个,我们将其设置为唯一可用的客户ID。最后,我们进行实际工作 - 将帐户设置为我们选择的标签。如果Len(customer_ids)> 1 else customer_id [0] 。
值得注意的是,您的近端问题是
customer_ids
的类型必须在中传递,必须是列表。如果这是一个孤独的价值,那么您会遇到问题。您尝试通过将其投入到列表中来解决此问题,但是如果您想接受列表或单个值,最好做类似的事情:这将导致这样的事情:
我添加了初始检查为了
customer_ids
确保它不是none
,它会破坏您的第二个检查(如果不是一个,则将值转换为列表),因为list(列表)(无)
投掷typeerror
。请注意,我会尽快命名此函数
update_account_tags()
或类似的内容,因为它返回没有值,只是标签字典,该字典的帐户具有更新的值。一些指导:如果您发现自己进行检查,
如果a在b
中,则b是字典,并且您打算用a
做某事,最好的事情要做的是使用字典的函数get()
。my_default
这可以是您想要的,默认情况下是none
。因此,这些都是等效的:其次,如果您发现自己正在进行这样的检查的情况:
您也可以简单地做到这一点:
结果是相同的,并且在计算上同样复杂。 (如果
customer_id
被替换为get_customer_id()
之类的函数,这可能不是完全正确的,但是作为第一个本能,它会做得很好。)I think you're complicating yourself a great deal here. Let's break out this function differently:
First, we know that if
customer_ids
, a list, is empty, we should raise an exception. Do this first. It's labelled 'bounds checking', and should be done before you try and process any data - that way you don't have to redo it on every branch of your code.Secondly, we know that if the list is greater than one, we want our tag to be 'shared', meaning we have more than one customer id. Let's set a temporary variable with the name
tag
with 'shared' if we have a list greater than one. If the list is exactly one, we set it to the only available customer id.Finally, we do the actual work - setting the account to the tag we have selected. Lines 4 and five could be combined to
tags["Account"] = "shared" if len(customer_ids) > 1 else customer_id[0]
.Notably, your proximal issue is that the type of
customer_ids
being passed in must be a list. If it is a solitary value then you'll have an issue. You try to solve this by just casting it to a list, but if you want to accept either a list or a single value, you're better doing something like this:This would result in something like:
I've added an initial check for
customer_ids
to ensure it is notNone
, which would break your second check (to convert the value to a list if it is not one), sincelist(None)
throws aTypeError
.Note that I would sooner name this function
update_account_tags()
or something like that, since it returns no value, just a dictionary of tags, which has an updated value for account.Some guidance: if you find yourself doing a check,
if a in b
, where b is a dictionary, and you're planning to do something witha
, the best thing to do is use the dictionary's functionget()
.my_default
here can be whatever you want, and by default isNone
. So these are equivalent:Secondly, if you find yourself in a situation where you're doing a check like this:
You might as well simply do this:
The result is the same, and it's equally computationally complex. (If
customer_id
is replaced with a function likeget_customer_id()
this may not be entirely true, but as a first instinct it'll do you well.)