我遇到了
deep_update
方法的问题,该方法应该递归更新字典,包括遇到时合并集合。但是,我的单元测试特别针对涉及集合的情况而失败。就是这个功能
@staticmethod
def deep_update(main_dict: Dict, update_dict: Dict) -> None:
"""
深度更新字典
:param main_dict: 原始字典
:param update_dict: 新字典
:return: 更新后的字典
>>> dict1 = {'a': {'b': 1}}
>>> UseDict.deep_update(dict1, {'a': {'c': 2}})
>>> assert dict1 == {'a': {'b': 1, 'c': 2}}
"""
for key, value in update_dict.items():
if key in main_dict:
main_value = main_dict[key]
# Deep update for nested dictionaries
if isinstance(main_value, dict) and isinstance(value, dict):
UseDict.deep_update(main_value, value)
# Concatenate lists
elif isinstance(main_value, list) and isinstance(value, list):
main_dict[key] = main_value + value
# Union sets
elif isinstance(main_value, set) and isinstance(value, set):
main_dict[key] = main_value | value
# Concatenate tuples
elif isinstance(main_value, tuple) and isinstance(value, tuple):
main_dict[key] = main_value + value
else:
# Update primitive types or handle other types directly
main_dict[key] = value
else:
# If key doesn't exist in main_dict, add it
main_dict[key] = value
return main_dict
这些例子都可以正常工作。 但是
set
和 tuples
的单元测试却没有:
例子:
# Example 1: Deep update with nested dictionaries
main_dict = {'a': {'b': 1}}
update_dict = {'a': {'c': 2}}
print("Input (Nested Dicts):")
print("Main Dictionary:", main_dict)
print("Update Dictionary:", update_dict)
UseDict.deep_update(main_dict, update_dict)
print("Updated Dictionary (Nested Dicts):", main_dict)
# Expected Output: {'a': {'b': 1, 'c': 2}}
# Example 2: Deep update with lists
main_dict = {'a': [1, 2]}
update_dict = {'a': [3, 4]}
print("\nInput (Lists):")
print("Main Dictionary:", main_dict)
print("Update Dictionary:", update_dict)
UseDict.deep_update(main_dict, update_dict)
print("Updated Dictionary (Lists):", main_dict)
# Expected Output: {'a': [1, 2, 3, 4]}
# Example 3: Deep update with sets
main_dict = {'a': {1, 2}}
update_dict = {'a': {3, 4}}
print("\nInput (Sets):")
print("Main Dictionary:", main_dict)
print("Update Dictionary:", update_dict)
answer = UseDict.deep_update(main_dict, update_dict)
print(answer)
# print("Updated Dictionary (Sets):", main_dict)
# Expected Output: {'a': {1, 2, 3, 4}}
# Example 4: Deep update with tuples
main_dict = {'a': (1, 2)}
update_dict = {'a': (3, 4)}
print("\nInput (Tuples):")
print("Main Dictionary:", main_dict)
print("Update Dictionary:", update_dict)
UseDict.deep_update(main_dict, update_dict)
print("Updated Dictionary (Tuples):", main_dict)
# Expected Output: {'a': (1, 2, 3, 4)}
测试:
def test_deep_update():
# Test with nested dictionaries
main_dict = {'a': {'b': 1}}
update_dict = {'a': {'c': 2}}
useDict.deep_update(main_dict, update_dict)
assert main_dict == {'a': {'b': 1, 'c': 2}}
# Test with lists
main_dict = {'a': [1, 2]}
update_dict = {'a': [3, 4]}
useDict.deep_update(main_dict, update_dict)
assert main_dict == {'a': [1, 2, 3, 4]}
# Test with sets
main_dict = {'a': {1, 2}}
update_dict = {'a': {3, 4}}
UseDict.deep_update(main_dict, update_dict)
assert main_dict == {'a': {1, 2, 3, 4}}
# Test with tuples
main_dict = {'a': (1, 2)}
update_dict = {'a': (3, 4)}
useDict.deep_update(main_dict, update_dict)
assert main_dict == {'a': (1, 2, 3, 4)}
# Test with conflicting types (dict vs list)
main_dict = {'a': {'b': 1}}
update_dict = {'a': [2, 3]}
useDict.deep_update(main_dict, update_dict)
assert main_dict == {'a': [2, 3]} # List should replace the dict
预计套装:
assert main_dict == {'a': {1, 2, 3, 4}}
预期元组:
assert main_dict == {'a': (1, 2, 3, 4)}
答案是在编写测试后再次进行
pip install .
。并且代码有效