可能是什麼這個代碼不添加到字典中的原因(假設它會爲else
塊):爲什麼key:value對不會被添加到字典中?
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()
也許我「M剛過尋找簡單的東西,但是這博格爾斯我的腦海裏......當我做同樣的事情在我的Python的外殼,它的工作原理...
merge_agent_log
是SQLAlchemy的對象。
- 什麼可能會可能導致這個原因嗎?
- 我錯過了什麼嗎?我在這兩天是具有...
- 使用
data["foo"] = "bar"
任何情況下的字典中的條目會失敗有沒有?
,請複製粘貼'merge_agent_log'類的定義。 – 2014-10-31 11:03:16
如果'merge_agent_log.data'實際上是一個'dict'(不是一個字典或者一些奇特的字典子類),沒有理由不應該設置你的鍵/值......「假設它進入else塊」 , 當然。 – 2014-10-31 11:05:07
好奇心! Try:merge_agent_log.data [now] .append({「status」:dup_agent_log.status,「change_agent」:dup_agent_log.change_agent,「source」:dup_agent_log.source}) – user3378649 2014-10-31 11:11:39