Обновить историю объекта Python как словарь и сохранить первый элемент
Я новичок в программировании классов. Я пытаюсь сохранить начальные атрибуты (которые являются словарем) моего объекта в его истории, а затем обновить историю изменениями атрибутов. Ниже код:
import datetime
import pytz
class Property:
""" Simple property class """
@staticmethod
def _current_time():
utc_time = datetime.datetime.utcnow()
return pytz.utc.localize(utc_time)
def __init__(self, link, attributes):
self._link = link
self.__attributes = attributes
self._history = [(Property._current_time(), attributes)]
def update(self, new_attributes):
def dictdiff(d1, d2):
return dict(set(d2.items()) - set(d1.items()))
attr_change = dictdiff(self.__attributes, new_attributes)
self.__attributes.update(attr_change)
self._history.append((Property._current_time(), attr_change))
def show_attributes(self):
return self.__attributes
def show_history(self):
# how to show changes in time?
return self._history
prop = Property(1234, {"Price":3000, "Active":"Yes"})
prop.update({"Price":2500, "Active":"Yes"})
prop.update({"Active":"No"})
prop.show_history()
И вывод:
Out[132]:
[(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
{'Price': 2500, 'Active': 'No'}),
(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
{'Price': 2500}),
(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
{'Active': 'No'})]
Первый пункт в истории должен быть:
(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
{"Price":3000, "Active":"Yes"})
Я попробовал это, но безуспешно. Кажется, что функция init обновляет историю инициализации при обновлении атрибутов, и в то же время в истории я хочу видеть атрибуты, которые были инициализированы впервые.
2 ответа
Проблема в том, что вы модифицируете словарь в истории с помощью этого кода:
self.__attributes.update(attr_change)
Вы в основном делаете это:
>>> attributes = {}
>>> history = [attributes]
>>> attributes.update(foo=3)
>>> history
[{'foo': 3}]
Это легко исправить, сохранив копию словаря в истории:
self._history = [(Property._current_time(), attributes.copy())]
Смотрите также Как скопировать словарь и редактировать только копию.
Вы хотите, чтобы первая запись истории была копией attributes
,
self._history = [(Property._current_time(), dict(attributes))]
Как ваш код сейчас, первая запись истории ссылается на текущий словарь атрибутов.