def fix_episode(episode_path):
try:
episode = load_episode(episode_path)
except EOFError:
print("Error reading: {}".format(episode_path))
os.remove(episode_path)
return
if episode.version == 2:
print("Version 2 already: {}".format(episode_path))
return
old_frames = episode.frames
episode.frames = []
for i in range(len(old_frames) - 1):
f = old_frames[i]
f.action = old_frames[i + 1].action
episode.frames.append(f)
episode.version = 2
s = pickle.dumps(episode)
with gzip.open(episode_path, "wb") as f:
f.write(s)
# pickler = pickle.Pickler(f)
# pickler.dump(episode)
# save_episode(episode_path, episode)
python类Pickler()的实例源码
def write_catastrophe_record(self, *args, **kwargs):
if self.catastrophe_coordinator is not None:
if self.catastrophe_coordinator.should_log():
record = CatastropheRecord(*args, **kwargs)
filename = self.catastrophe_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.catastrophe_coordinator.step()
def save_episode(episode_path, episode):
with gzip.open(episode_path, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(episode)
def fix_episode(episode_path):
try:
episode = load_episode(episode_path)
except EOFError:
print("Error reading: {}".format(episode_path))
os.remove(episode_path)
return
if episode.version == 2:
print("Version 2 already: {}".format(episode_path))
return
old_frames = episode.frames
episode.frames = []
for i in range(len(old_frames) - 1):
f = old_frames[i]
f.action = old_frames[i + 1].action
episode.frames.append(f)
episode.version = 2
s = pickle.dumps(episode)
with gzip.open(episode_path, "wb") as f:
f.write(s)
# pickler = pickle.Pickler(f)
# pickler.dump(episode)
# save_episode(episode_path, episode)
def write_catastrophe_record(self, *args, **kwargs):
if self.catastrophe_coordinator is not None:
if self.catastrophe_coordinator.should_log():
record = CatastropheRecord(*args, **kwargs)
filename = self.catastrophe_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.catastrophe_coordinator.step()
def write_block_record(self, *args, **kwargs):
if self.block_coordinator is not None:
if self.block_coordinator.should_log():
record = BlockRecord(*args, **kwargs)
filename = self.block_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.block_coordinator.step()
def save_episode(episode_path, episode):
with gzip.open(episode_path, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(episode)
def save_labels(filename, episode, frames):
"""
Args:
filename (str): file to save labeled episode to.
episode (episode): an episode to save labeled `frames` to (this is pickled and gzipped)
frames (list of frames): a time ordered list of labeled frames.
Summary:
Save labeled frames to a file.
"""
episode.frames = frames
with gzip.open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(episode)
def write_catastrophe_record(self, *args, **kwargs):
if self.catastrophe_coordinator is not None:
if self.catastrophe_coordinator.should_log():
record = CatastropheRecord(*args, **kwargs)
filename = self.catastrophe_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.catastrophe_coordinator.step()
def write_block_record(self, *args, **kwargs):
if self.block_coordinator is not None:
if self.block_coordinator.should_log():
record = BlockRecord(*args, **kwargs)
filename = self.block_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.block_coordinator.step()
def save_episode(episode_path, episode):
with gzip.open(episode_path, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(episode)
def fix_episode(episode_path):
try:
episode = load_episode(episode_path)
except EOFError:
print("Error reading: {}".format(episode_path))
os.remove(episode_path)
return
if episode.version == 2:
print("Version 2 already: {}".format(episode_path))
return
old_frames = episode.frames
episode.frames = []
for i in range(len(old_frames) - 1):
f = old_frames[i]
f.action = old_frames[i + 1].action
episode.frames.append(f)
episode.version = 2
s = pickle.dumps(episode)
with gzip.open(episode_path, "wb") as f:
f.write(s)
# pickler = pickle.Pickler(f)
# pickler.dump(episode)
# save_episode(episode_path, episode)
def write_block_record(self, *args, **kwargs):
if self.block_coordinator is not None:
if self.block_coordinator.should_log():
record = BlockRecord(*args, **kwargs)
filename = self.block_coordinator.get_filename()
with open(filename, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(record)
self.block_coordinator.step()
def save_episode(episode_path, episode):
with gzip.open(episode_path, "wb") as f:
pickler = pickle.Pickler(f)
pickler.dump(episode)
def fix_episode(episode_path):
try:
episode = load_episode(episode_path)
except EOFError:
print("Error reading: {}".format(episode_path))
os.remove(episode_path)
return
if episode.version == 2:
print("Version 2 already: {}".format(episode_path))
return
old_frames = episode.frames
episode.frames = []
for i in range(len(old_frames) - 1):
f = old_frames[i]
f.action = old_frames[i + 1].action
episode.frames.append(f)
episode.version = 2
s = pickle.dumps(episode)
with gzip.open(episode_path, "wb") as f:
f.write(s)
# pickler = pickle.Pickler(f)
# pickler.dump(episode)
# save_episode(episode_path, episode)
def dumps_with_persistent_ids(obj, protocol=None):
"""
Performs a pickle dumps on the given object, substituting all references to
a TradingEnvironment or AssetFinder with tokenized representations.
All arguments are passed to pickle.Pickler and are described therein.
"""
file = BytesIO()
pickler = pickle.Pickler(file, protocol)
pickler.persistent_id = _persistent_id
pickler.dump(obj)
return file.getvalue()
def __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()
def register(self, type, reduce_func):
"""Attach a reducer function to a given type in the dispatch table."""
if hasattr(pickle.Pickler, 'dispatch'):
# Python 2 pickler dispatching is not explicitly customizable.
# Let us use a closure to workaround this limitation.
def dispatcher(self, obj):
reduced = reduce_func(self, obj)
self.save_reduce(obj=obj, *reduced)
self.dispatch[type] = dispatcher
else:
self.dispatch_table[type] = lambda obj: reduce_func(self, obj)
def createPickleChild(self, name, child):
if not os.path.isdir(self.path):
resource.Resource.putChild(self, name, child)
# xxx use a file-extension-to-save-function dictionary instead
if type(child) == type(""):
fl = open(os.path.join(self.path, name), 'wb')
fl.write(child)
else:
if '.' not in name:
name = name + '.trp'
fl = open(os.path.join(self.path, name), 'wb')
from pickle import Pickler
pk = Pickler(fl)
pk.dump(child)
fl.close()
def __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()