3

我花了一个下午的大部分时间试图将字典对象修补为 utf-8 编码来代替 unicode。我正在尝试找到扩展字典对象并确保它的条目、键和值都是 utf-8 的最快和最佳性能的方法。

这是我想出的,它可以完成工作,但我想知道可以进行哪些改进。

class UTF8Dict(dict):
    def __init__(self, *args, **kwargs):
        d = dict(*args, **kwargs)
        d = _decode_dict(d)
        super(UTF8Dict,self).__init__(d)
    def __setitem__(self,key,value):
        if isinstance(key,unicode):
            key = key.encode('utf-8')
        if isinstance(value,unicode):
            value = value.encode('utf-8')
        return super(UTF8Dict,self).__setitem__(key,value)

def _decode_list(data):
    rv = []
    for item in data:
        if isinstance(item, unicode):
            item = item.encode('utf-8')
        elif isinstance(item, list):
            item = _decode_list(item)
        elif isinstance(item, dict):
            item = _decode_dict(item)
        rv.append(item)
    return rv

def _decode_dict(data):
    rv = {}
    for key, value in data.iteritems():
        if isinstance(key, unicode):
            key = key.encode('utf-8')
        if isinstance(value, unicode):
            value = value.encode('utf-8')
        elif isinstance(value, list):
            value = _decode_list(value)
        elif isinstance(value, dict):
            value = _decode_dict(value)
        rv[key] = value
    return rv

改善以下任何一项的建议将非常有帮助:

  • 表现
  • 覆盖更多边缘情况
  • 错误处理
4

1 回答 1

4

I agree with the comments that say that this may be misguided. That said, here are some holes in your current scheme:

  1. d.setdefault can be used to add unicode objects to your dict:

    >>> d = UTF8Dict()
    >>> d.setdefault(u'x', u'y')
    
  2. d.update can be used to add unicode objects to your dict:

    >>> d = UTF8Dict()
    >>> d.update({u'x': u'y'})
    
  3. the list values contained in a dict could be modified to include unicode objects, using any standard list operations. E.g.:

    >>> d = UTF8Dict(x=[])
    >>> d['x'].append(u'x')
    

Why do you want to ensure that your data structure contains only utf-8 strings?

于 2012-05-25T20:21:19.243 回答