为什么键值:值对不会被添加到词典中?

时间:2014-10-31 10:56:00

标签: python dictionary sqlalchemy

这段代码不能添加到字典中的原因是什么(假设它进入else块):

if dup_agent_log.modify_stamp > merge_agent_log.modify_stamp:
    merge_agent_log.set_status_log(dup_agent_log.status, None, dup_agent_log.agents_id, dup_agent_log.source, dup_agent_log.status_type)
else:
    print merge_agent_log.data # {}
    now = str(datetime.datetime.now())
    merge_agent_log.data[now] = [{"status": dup_agent_log.status, "change_agent": dup_agent_log.change_agent, "source": dup_agent_log.source}]
    print "after", merge_agent_log.data # after {}

transaction.savepoint()

AgentStatusLog()类(模型):

class AgentStatusLog(Base):
__tablename__ = "agent_status_log"
id = Column(
    Integer,
    Sequence("agent_status_log_id", optional=True),
    primary_key=True
)
status_id = Column(Integer, ForeignKey(StatusLookup.id))
_status = relationship(StatusLookup, uselist=False)
status = Column(Unicode(100), index=True)
previous_status = Column(Unicode(50), index=True)
effective_stamp = Column(DateTime, index=True)
modify_stamp = Column(DateTime, index=True)
agents_id = Column(Integer, ForeignKey(Agents.id))
agent = relationship(Agents, primaryjoin=Agents.id == agents_id, uselist=False)
_data = Column("data", Unicode(2048))
status_type = Column(Unicode(40), index=True)
change_agents_id = Column(Integer, ForeignKey(Agents.id))
change_agent = relationship(Agents, primaryjoin=Agents.id == change_agents_id, uselist=False)
source = Column(Unicode(100), index=True)


@property
def data(self):
    if not self._data:
        return {}
    return json.loads(self._data)

@data.setter
def data(self, value):
    self._data = json.dumps(value)

def set_data(self, field, value):
    data = self.data
    data[field] = value
    self.data = data

def get_data(self):
    if not self._data:
        return {}
    return json.loads(self._data)

def unset_data(self, field):
    data = self.get_data()
    if field in data:
        del data[field]
        self.data = data

@classmethod
def by_id(cls, id):
    return Session.query(cls).filter(cls.id == id).first()

@classmethod
def by_agents_id(cls, aid):
    return Session.query(cls).filter(cls.agents_id == aid).first()

@classmethod
def set_status_log(cls, status, change_agent=None, agent_id=None, source=None, status_type=None):
    if agent_id:
        sl = Session.query(cls).filter(cls.agents_id == agent_id).first()
        sl.modify_stamp = func.now()
        sl.source = source
        if sl.status:
            sl.previous_status = sl.status
            sl.data[str(datetime.datetime.now())] = [{"status": sl.status, "change_agent": change_agent, "source": source}]
        sl.status = status
        sl.change_agents_id = change_agent
        sl.status_type = status_type
        transaction.commit()

也许我只是在寻找一些简单的东西,但这令人难以置信......当我在我的python shell中做同样的事情时,它的工作原理......
merge_agent_log是一个SQLAlchemy对象。

  • 可能导致这种情况的原因是什么?
  • 我错过了什么吗?我已经有两天了......
  • 在任何情况下,使用data["foo"] = "bar"时,dict的输入都会失败吗?

1 个答案:

答案 0 :(得分:1)

这里没有dict失败,只是你创造了不止一个dict并且没有意识到它。

让我们仔细分析merge_agent_log.data[now] = something:首先调用属性getter。它会显示_data并使用json.loads创建一个新的字典。然后,您修改此新dict,为密钥something添加now

但你不修改_data

因此,下次使用merge_agent_log.data调用属性getter时,您将从_data读取相同的字符串,并创建另一个与之前相同内容的字典。

试试这个:

data = merge_agent_log.data
data[now] = something
merge_agent_log.data = data  # calls property setter, which modifies _data