def check(Unpickler): for proto in range(pickle.HIGHEST_PROTOCOL + 1): unpickler = Unpickler(io.BytesIO(self.dumps('abc', proto))) self.assertEqual(unpickler.load(), 'abc') unpickler = Unpickler(io.BytesIO()) self.assertEqual(unpickler.persistent_load('def'), 'def') r = weakref.ref(unpickler) del unpickler self.assertIsNone(r())
def __getitem__(self, key): try: return self.__vars__[key] except: try: with open(f'{self.name}.blue', 'rb') as fp: p = Unpickler(fp) data = p.load().__vars__.get(key) if data is None: raise else: return data except: raise KeyError(f"Key '{key}' does not exist.")
def loadTrainExamples(self, examples_file): if not os.path.isfile(examples_file): print(examples_file) print("File with trainExamples not found.") sys.exit() else: print("File with trainExamples found. Read it.") with open(examples_file, "rb") as f: return Unpickler(f).load()
def unpickle_old_pyfits(fn): """ This function unpickles everything in the specified filename, and correctly adapts pyfits files pre 2.3 to the new module structure. """ import _pickle from _pickle import Unpickler import imp,sys def fg(modname,classname): if 'NP_pyfits' in modname: modname = modname.replace('NP_pyfits','core') if '.' in modname: mod = __import__(modname,fromlist=[0]) else: mod = __import__(modname) return getattr(mod,classname) objs = [] if isinstance(fn, str): f = open(fn) else: f = fn try: u = Unpickler(f) #u.find_class = fg #u.find_global = fg while True: objs.append(u.load()) except EOFError: pass finally: if isinstance(fn, str): f.close() if len(objs) == 1: return objs[0] else: return objs
def __init__(self, name, **kwargs): self.name = name self.__vars__ = {} self.__blues__ = {} try: with open(f'{self.name}.blue', 'rb') as fp: p = Unpickler(fp) q = p.load() self.__vars__ = q.__vars__ self.__blues__ = q.__blues__ self.__loaded__ = q except: with open(f'{self.name}.blue', 'wb') as fp: p = Pickler(fp) p.dump(self)
def loadlastturn(): global justloaded, boardwidth, boardheight, gamemods, whoseturn, phase, subphase, action, subaction file = open('lastturn', 'rb') loader = Unpickler(file) clear(summoners) clear(teams) clear(board) clear(modifiers) clear(gamemods) extend(summoners, loader.load()) extend(teams, loader.load()) extend(board, loader.load()) boardwidth = loader.load() boardheight = loader.load() extend(modifiers, loader.load()) extend(gamemods, loader.load()) cachepics() whoseturn = loader.load() phase = loader.load() subphase = loader.load() file.close() justloaded = True phase()
def load_input(): global inp print("Loading...") pf = open(options.pickle, "rb") inp = Unpickler(pf).load() pf.close()