medved/lib/net/Message.py

28 lines
605 B
Python
Raw Normal View History

2018-04-02 22:41:10 +00:00
import pickle
class Message:
2018-04-02 23:47:50 +00:00
"""Message. Just a pickled dict."""
2018-04-02 22:41:10 +00:00
def __init__(self, data=None):
if data is None:
data = {}
self._data = data
def set(self, key, value):
self._data[key] = value
def get(self, key):
return self._data.get(key, None)
def data(self):
return self._data
def dump(self):
return pickle.dumps(self)
@classmethod
def load(cls, pickled_data):
try:
obj = pickle.loads(pickled_data)
return cls(obj.data())
except EOFError:
return cls({})