def load(self, filename, varnames=None): if not os.path.exists(self.tmp_path): self.make_tmp() with ZipFile(filename, allowZip64=True) as inzip: for fname in inzip.namelist(): vn = strip_ext(fname) if isinstance(varnames, (str, unicode)): varnames = str2list(varnames) if varnames is not None and vn not in varnames: continue extract_fname = '%s/%s' % (self.tmp_path, fname) inzip.extract(fname, self.tmp_path) var = self.load_var(extract_fname) self[vn] = var
def save(self, filename=None, varnames=None): if not os.path.exists(self.tmp_path): self.make_tmp() if filename is None: filename = './%s.zip' % self.name outflist = [] if varnames is None: varnames = self.keys() elif isinstance(varnames, (str, unicode)): varnames = str2list(varnames) for vname in varnames: if vname in self.keys(): fn = self.dump_var(vname) outflist.append(fn) force_makedirs(os.path.dirname(filename)) with ZipFile(filename, 'w', allowZip64=True) as outzip: for fn in outflist: outzip.write(fn, os.path.basename(fn))
def takeout(self, varnames=None, dest=None, deepcopy=None): """ Parameters ---------- varnames: varnames to take to the dest. dest: a dict as dest of vars. Using globals() as default. deepcopy: if True, the vars taken out are deepcopied, to protect the version in the basket. if None, use the basket's default option, i.e., self.deepcopy . """ if dest is None: dest = self.scope if varnames is None: varnames = self.keys() elif isinstance(varnames, (str, unicode)): varnames = str2list(varnames) for v in varnames: if deepcopy is None: deepcopy = self.deepcopy if deepcopy: dest[v] = copy.deepcopy(self[v]) else: dest[v] = self[v]
def collect(self, varnames=None, source=None, deepcopy=False): """ Parameters ---------- varnames: varnames to collect. source: a dict of vars to collect from. Using globals() as default. deepcopy: if True, the vars put into the basket are deepcopied, to protect the version in the basket. if None, use the basket's default option, i.e., self.deepcopy . """ if source is None: source = self.scope if varnames is None: varnames = self.keys() elif isinstance(varnames, (str, unicode)): varnames = str2list(varnames) for v in varnames: if deepcopy is None: deepcopy = self.deepcopy if deepcopy: self[v] = copy.deepcopy(source.get(v, None)) else: self[v] = source.get(v, None)