【问题标题】:Class attributes in Python objects "erasing" themselvesPython 对象中的类属性“擦除”自身
【发布时间】:2019-09-13 08:31:53
【问题描述】:

我正在使用一些旧的 Python2 代码,这些代码来自于早前 (http://www.dpacontest.org/home/index.html) 的差分功率分析 (DPA) 竞赛。我正在修改的代码可以在这里找到 (https://svn.comelec.enst.fr/dpacontest/code/reference/)

当前让我抓狂的问题: 本质上,这段代码中的一些 python 对象会“擦除”自己或转到它们的默认值,我不知道为什么。我在执行此操作的逻辑流中找不到任何代码,而且我认为这不是范围问题。

我尝试重写部分代码以使用 numpy 而不是映射 lambda 函数。我还尝试了无数不同的代码顺序,并从类中提取方法并尝试在本地/内联运行它们。

main.py:

loc_subkeys = brk.get_subkeys()

des_breaker.py

def get_subkeys(self):
    """
    Returns a vector of currently best sboxes subkeys.
    This is an array of 8 integers.
    """
    sk = np.array([])
    for i in range(8):
        sk = np.append(sk, self.__sbox_breakers[i].get_key())
    return sk

sbox_breaker.py

def get_key(self):
    "Gives the current best key"
    if self.__best_key is None:
        marks = np.array([])
        print("p0: ", len(list(self.__key_estimators[0]._key_estimator__p0)))
        print("p1: ", len(list(self.__key_estimators[0]._key_estimator__p1)))
        print("p0: ", len(list(self.__key_estimators[0]._key_estimator__p0)))
        print("p1: ", len(list(self.__key_estimators[0]._key_estimator__p1)))
        for i in range(64):
            ke = self.__key_estimators[i]
            marks = np.append(marks, ke.get_mark())
            self.__best_key = np.argmax(marks)
        return self.__best_key

key_estimator.py - 属性

class key_estimator:
    """
    Provides methods to give a mark to the key relatively to the probability f
    the correctness of the key.
    """
    __sbox = None
    __key = None
    __cnt0 = 0    # The accumulated traces count in partition 0
    __cnt1 = 0    # The accumulated traces count in partition 1
    __p0 = None  # The bit=0 estimated partition
    __p1 = None  # The bit=1 estimated partition
    __diff = np.array([])  # The differential trace

sbox_breaker 中的打印语句是我的。他们的输出是我现在唯一的线索:

p0: 5003(好)

p1: 5003(好)

p0: 0 (???)

p1: 0

什么给了?第二次围绕 key_estimator 类的属性似乎已经删除了自己。这发生在所有属性上,而不仅仅是 p0 和 p1。

该程序的第一个循环有效,但在第二次迭代(从 main 开始)时它失败了,因为属性已自行删除。我可以通过打印对象的属性手动“擦除”它们。

【问题讨论】:

  • 您能否提供链接到您提供的代码的 htaccess 登录名
  • 是的,很抱歉它在网页上。用户:访客,通行证:访客。我也想我弄清楚了我的问题。问题是 Python2 映射 -> Python3 映射。这些属性是似乎不会持续存在的地图,因此将它们放入列表已经解决了我的问题。

标签: python-3.x oop printing attributes


【解决方案1】:

所以我似乎在睡觉后解决了这个问题。类属性是由 map 创建的,它在 Python2 中返回一个列表,而不是 Python3。使用 list() 将它们放入列表可以解决持久性问题。我无法告诉你为什么打印地图属性会导致它自行清除。

【讨论】:

  • 因为地图是惰性迭代器,所以它们是单程的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
相关资源
最近更新 更多