酸洗过程是确定性的吗? [英] Is the pickling process deterministic?

查看:46
本文介绍了酸洗过程是确定性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pickle 是否总是为某个输入值产生相同的输出?我想在腌制内容相同但插入/删除历史不同的字典时可能会有问题.我的目标是使用 Pickle 和 SHA1 创建函数参数的签名",以实现 memoize.

解决方案

我想在腌制内容相同但插入/删除历史不同的字典时可能会遇到问题.

对:

<预><代码>>>>pickle.dumps({1: 0, 9: 0}) == pickle.dumps({9: 0, 1: 0})错误的

另见:pickle.dumps 不适合散列

<块引用>

我的目标是创建一个签名"函数参数,使用 Pickle 和 SHA1,用于 memoize 实现.

这有许多基本问题.想出一个正确映射相等的对象到字符串的转换是不可能的——想想对象身份的问题:

<预><代码>>>>a = 对象()>>>b = 对象()>>>a == b错误的>>>pickle.dumps(b) == pickle.dumps(a)真的

根据您的具体要求,您可以将对象层次结构转换为可以散列的层次结构:

def hashblize(obj):"将容器层次结构转换为可以散列的层次结构.不要在递归结构中使用它!此外,如果您传递字典,这将没有用没有总顺序的键.实际上,也许您最好根本不使用此功能.""尝试:哈希(对象)除了类型错误:如果 isinstance(obj, dict):在 sorted(obj.iteritems())) 中为 (k, v) 返回元组((k, hashablize(v))elif hasattr(obj, '__iter__'):返回元组(hashablize(o) for o in obj)别的:raise TypeError(无法散列 %r 类型的对象"% type(obj))别的:返回对象

Does Pickle always produce the same output for a certain input value? I suppose there could be a gotcha when pickling dictionaries that have the same contents but different insert/delete histories. My goal is to create a "signature" of function arguments, using Pickle and SHA1, for a memoize implementation.

解决方案

I suppose there could be a gotcha when pickling dictionaries that have the same contents but different insert/delete histories.

Right:

>>> pickle.dumps({1: 0, 9: 0}) == pickle.dumps({9: 0, 1: 0})
False

See also: pickle.dumps not suitable for hashing

My goal is to create a "signature" of function arguments, using Pickle and SHA1, for a memoize implementation.

There's a number of fundamental problems with this. It's impossible to come up with an object-to-string transformation that maps equality correctly—think of the problem of object identity:

>>> a = object()
>>> b = object()
>>> a == b
False
>>> pickle.dumps(b) == pickle.dumps(a)
True

Depending on your exact requirements, you may be able to transform object hierarchies into ones that you could then hash:

def hashablize(obj):
    """Convert a container hierarchy into one that can be hashed.
    
    Don't use this with recursive structures!
    Also, this won't be useful if you pass dictionaries with
    keys that don't have a total order.
    Actually, maybe you're best off not using this function at all."""
    try:
        hash(obj)
    except TypeError:
        if isinstance(obj, dict):
            return tuple((k, hashablize(v)) for (k, v) in sorted(obj.iteritems()))
        elif hasattr(obj, '__iter__'):
            return tuple(hashablize(o) for o in obj)
        else:
            raise TypeError("Can't hashablize object of type %r" % type(obj))
    else:
        return obj

这篇关于酸洗过程是确定性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆