微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

处理 KeyError 的上下文管理器类

如何解决处理 KeyError 的上下文管理器类

我正在尝试实现一个 context manager class 来处理字典上的 KeyError。

想想这个:

bikes = ['Honda','Yamaha','Kawasaki','Suzuki']
colors = ['Red','Blue','Green','White','Black']

有了这两个列表,我必须构建一个包含每个品牌和颜色的销售额的两层字典,例如:

bike_sales_by_color = {
    'Honda': {
        'Red': 100,'Blue': 125,},'Yamaha': {
        'Black': 50,'White': 60,# etc...
}

在这个例子中,销售额只是随机数)。

解决这个问题的实现是最普通的/常规的:

def get_bikes_sales():
    bikes_sales = {}
    for bike in bikes:  # iterate over two lists
        for color in colors:
            try:  # try to assign a value to the second-level dict.
                bikes_sales[bike][color] = 100  # random sales int value
            except KeyError:  # handle key error if bike is not yet in the first-level dict.
                bikes_sales[bike] = {}
                bikes_sales[bike][color] = 100  # random sales int value
    return bikes_sales

上面函数的行为是预期的,但我想要一个用户定义的类来保存我们每次不得不面对这个问题时重复这段代码,我认为上下文管理器类将是实现的方式

这是我所做的,但没有按预期工作:

class DynamicDict:
    """
    Context manager class that immediately creates a new key with the intended value
    and an empty dict as the new created key's value if KeyError is raised.
    Useful when trying to build two or more level dictionaries.
    """

    def __init__(self):
        self.dictionary = {}

    def __enter__(self):
        return self.dictionary

    def __exit__(self,exc_type,exc_val,exc_tb):
        if exc_type is KeyError:
            self.dictionary[exc_val.args[0]] = {}
            return True

这样我们就可以制作类似的东西:

with DynamicDict() as bikes_sales:
    for bike in bikes:
        for color in colors:
            bikes_sales[bike][color] = 100

但是 with 块内的迭代在上下文管理器处理的第一个 KeyError 之后停止,我只得到以下结果:{'Honda': {}}

解决方法

您对 get_bikes_sales 的实现非常 Python 化(尝试/除外)。
使用上下文管理器并不能解决问题 - 只是将其移到其他地方。

为什么不创建一个动态创建(任意嵌套)字典的函数:

import itertools
import pprint


def generate_nested_dict(*categories,values):
    result = {}

    # create all combinations of all categories,alternatively recursion could be used.
    for tree in (x for x in itertools.product(*categories)):
        _dictHandle = result  # keeps track of the parent level with in the dict

        # Each tree is Honda->Red,Honda->Blue,...
        for i,k in enumerate(tree):
            if k not in _dictHandle:
                if i < len(tree) - 1:
                    # add nested dict level
                    _dictHandle[k] = {}
                else:
                    # nested level exists
                    if len(values) == 1:
                        _dictHandle[k] = values[0]
                    else:
                        _dictHandle[k] = values.pop(0)
                    # add value
            _dictHandle = _dictHandle[k]
    return result


bikes = ['Honda','Yamaha','Kawasaki','Suzuki']
fuels = ['Petrol','Diesel','Electric','Soda']
colors = ['Red','Blue','Green','White','Black']
sales = [
    (100 * (i + 1)) + (10 * (j + 1)) for i in range(len(bikes))
    for j in range(len(colors))
]

# different values
bike_sales_by_color = generate_nested_dict(bikes,colors,values=sales)
pprint.pprint(bike_sales_by_color)

# different values and one category more
bike_sales_by_fuel_and_color = generate_nested_dict(
    bikes,fuels,values=[100]
)
pprint.pprint(bike_sales_by_fuel_and_color)

出:

{'Honda': {'Black': 150,'Blue': 120,'Green': 130,'Red': 110,'White': 140},'Kawasaki': {'Black': 350,'Blue': 320,'Green': 330,'Red': 310,'White': 340},...
{'Honda': {'Diesel': {'Black': 100,'Blue': 100,'Green': 100,'Red': 100,'White': 100},'Electric': {'Black': 100,'Petrol': {'Black': 100,'Soda': {'Black': 100,'White': 100}},...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。