本文整理汇总了Python中dill.dump方法的典型用法代码示例。如果您正苦于以下问题:Python dill.dump方法的具体用法?Python dill.dump怎么用?Python dill.dump使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dill
的用法示例。
在下文中一共展示了dill.dump方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def save(self, dirpath: typing.Union[str, Path]):
"""
Save the :class:`DataPack` object.
A saved :class:`DataPack` is represented as a directory with a
:class:`DataPack` object (transformed user input as features and
context), it will be saved by `pickle`.
:param dirpath: directory path of the saved :class:`DataPack`.
"""
dirpath = Path(dirpath)
data_file_path = dirpath.joinpath(self.DATA_FILENAME)
if not dirpath.exists():
dirpath.mkdir(parents=True)
dill.dump(self, open(data_file_path, mode='wb'))
示例2: save
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def save(self, dirpath: typing.Union[str, Path]):
"""
Save the :class:`DSSMPreprocessor` object.
A saved :class:`DSSMPreprocessor` is represented as a directory with
the `context` object (fitted parameters on training data), it will
be saved by `pickle`.
:param dirpath: directory path of the saved :class:`DSSMPreprocessor`.
"""
dirpath = Path(dirpath)
data_file_path = dirpath.joinpath(self.DATA_FILENAME)
if not dirpath.exists():
dirpath.mkdir(parents=True)
dill.dump(self, open(data_file_path, mode='wb'))
示例3: exec_in_new_process
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def exec_in_new_process(func, *args, **kargs):
"""Launches a function in a separate process. Takes variable number of arguments which are passed to the function.
The process IS NOT FORKED by 'exec'ed.
:param func: Function to be executed in a separate process.
:param args: position arguments passed to the func
:param kargs: named arguments passed to the func
:return:
"""
# Store function handle and arguments into a pickle
new_process_runnable_handle, new_process_runnable_file = mkstemp(suffix='runnable')
with os.fdopen(new_process_runnable_handle, 'wb') as f:
dill.dump((func, args, kargs), f)
bootstrap_package_name = '{}.{}'.format(__package__, os.path.splitext(os.path.basename(__file__))[0])
# Popen this script (__main__) below will be an entry point
process = subprocess.Popen(args=[sys.executable,
'-m',
bootstrap_package_name,
new_process_runnable_file],
executable=sys.executable)
return process
示例4: test_requirements_analyzer__model_works
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def test_requirements_analyzer__model_works(tmpdir):
from proxy_model import model
reqs = get_object_requirements(model)
for r in reqs.custom:
for p, src in r.to_sources_dict().items():
join = os.path.join(tmpdir, p)
os.makedirs(os.path.dirname(join), exist_ok=True)
with open(join, 'w') as f:
f.write(src)
with open(os.path.join(tmpdir, 'model.pkl'), 'wb') as f:
dill.dump(model, f)
shutil.copy(fs.current_module_path('use_model.py'), tmpdir)
cp = subprocess.run('python use_model.py', shell=True, cwd=tmpdir)
assert cp.returncode == 0
示例5: save
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def save(self, dirpath: typing.Union[str, Path]):
"""
Save the :class:`DataPack` object.
A saved :class:`DataPack` is represented as a directory with a
:class:`DataPack` object (transformed user input as features and
context), it will be saved by `pickle`.
:param dirpath: directory path of the saved :class:`DataPack`.
"""
dirpath = Path(dirpath)
data_file_path = dirpath.joinpath(self.DATA_FILENAME)
if data_file_path.exists():
raise FileExistsError(
f'{data_file_path} already exist, fail to save')
elif not dirpath.exists():
dirpath.mkdir()
dill.dump(self, open(data_file_path, mode='wb'))
示例6: save
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def save(self, dirpath: typing.Union[str, Path]):
"""
Save the :class:`DSSMPreprocessor` object.
A saved :class:`DSSMPreprocessor` is represented as a directory with
the `context` object (fitted parameters on training data), it will
be saved by `pickle`.
:param dirpath: directory path of the saved :class:`DSSMPreprocessor`.
"""
dirpath = Path(dirpath)
data_file_path = dirpath.joinpath(self.DATA_FILENAME)
if data_file_path.exists():
raise FileExistsError(
f'{data_file_path} instance exist, fail to save.')
elif not dirpath.exists():
dirpath.mkdir()
dill.dump(self, open(data_file_path, mode='wb'))
示例7: main
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def main(cwd=''):
"""
Main driver
Args:
cwd (str): current working directory (need this for testing)
"""
# Loop over variants, exact and inexact solves
results = {}
for variant in ['semi-implicit-stab']:
results[(variant, 'exact')] = run_SDC_variant(variant=variant)
# dump result
fname = 'data/results_SDC_variants_AllenCahn_1E-03'
file = open(cwd + fname + '.pkl', 'wb')
dill.dump(results, file)
file.close()
assert os.path.isfile(cwd + fname + '.pkl'), 'ERROR: dill did not create file'
# visualize
show_results(fname, cwd=cwd)
示例8: main
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def main(cwd=''):
"""
Main driver
Args:
cwd (str): current working directory (need this for testing)
"""
# Loop over variants, exact and inexact solves
results = {}
for variant in ['multi-implicit', 'semi-implicit', 'fully-implicit', 'semi-implicit_v2', 'multi-implicit_v2']:
results[(variant, 'exact')] = run_SDC_variant(variant=variant, inexact=False)
results[(variant, 'inexact')] = run_SDC_variant(variant=variant, inexact=True)
# dump result
fname = 'data/results_SDC_variants_AllenCahn_1E-03'
file = open(cwd + fname + '.pkl', 'wb')
dill.dump(results, file)
file.close()
assert os.path.isfile(cwd + fname + '.pkl'), 'ERROR: dill did not create file'
# visualize
# show_results(fname, cwd=cwd)
示例9: write
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def write(self, fileName):
"""
Write the current state into a temporary file then atomically rename it to the main
state file.
:param str fileName: Path to the state file.
"""
with open(fileName + '.tmp', 'wb') as fH:
# Based on answer by user "Mark" at:
# http://stackoverflow.com/questions/2709800/how-to-pickle-yourself
# We can't pickle nested classes. So we have to pickle the variables of the class
# If we ever change this, we need to ensure it doesn't break FileID
dill.dump(self.__dict__, fH)
os.rename(fileName + '.tmp', fileName)
# Functions related to logging
示例10: _createJobStateFile
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def _createJobStateFile(self):
"""
Create the job state file for the current job and fill in the required
values.
:return: Path to the job state file
:rtype: str
"""
jobStateFile = os.path.join(self.localTempDir, '.jobState')
jobState = {'jobProcessName': get_process_name(self.workDir),
'jobName': self.jobName,
'jobDir': self.localTempDir}
with open(jobStateFile + '.tmp', 'wb') as fH:
dill.dump(jobState, fH)
os.rename(jobStateFile + '.tmp', jobStateFile)
return jobStateFile
示例11: saga_cv_cache
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def saga_cv_cache(*args):
arghash = sha1(repr(args).encode('utf-8')).hexdigest()
fn = "res/baseline_linear_{}.dill".format(arghash)
try:
with open(fn, 'rb') as f:
out = dill.load(f)
logging.info("Loaded cached version.")
except FileNotFoundError:
logging.info("Computing...")
out = saga_cv(*args)
with open(fn, 'wb') as f:
dill.dump(out, f)
return out
示例12: persist
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def persist(self, X, y, thesaurus):
"""
Save the data and the processed thesaurus.
Parameters
----------
X: sparse matrix
The train data: Will be compressed.
y: sparse matrix
The label data: Will be compressed.
thesaurus: ThesaurusReader
ThesaurusReader object: Will be pickled.
"""
print('Persisting features to disk')
self._delete_old_files()
self._save(self._persist_name('X'), X)
self._save(self._persist_name('y'), y)
with open(self._persist_name('TR'), mode='wb') as f:
pickle.dump(thesaurus, f)
示例13: evaluate_checkpoint
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def evaluate_checkpoint(sess,model):
dataset = 'cifar'
#with tf.Session() as sess:
# Iterate over the samples batch-by-batch
num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
adv_x_samples=[]
adv_y_samples=[]
for ibatch in range(num_batches):
bstart = ibatch * eval_batch_size
bend = min(bstart + eval_batch_size, num_eval_examples)
x_batch = mnist.test.images[bstart:bend,:]
y_batch = mnist.test.labels[bstart:bend]
x_batch_adv = attack.perturb(x_batch, y_batch, sess)
if(ibatch == 0):
adv_x_samples = x_batch_adv
adv_y_samples = y_batch
else:
adv_x_samples = np.concatenate((adv_x_samples, x_batch_adv), axis = 0)
adv_y_samples = np.concatenate((adv_y_samples, y_batch), axis = 0)
if(args.attack == 'xent'):
atck = 'pgd'
f = open(os.path.join(args.log_dir, 'Adv_%s_%s.p' % (dataset, atck)), "w")
elif(args.attack == 'cw_pgd'):
atck = 'cw_pgd'
f = open(os.path.join(args.log_dir, 'Adv_%s_%s.p' % (dataset, atck)), "w")
else:
f = open(os.path.join(args.log_dir, "custom.p"), "w")
pickle.dump({"adv_input":adv_x_samples,"adv_labels":adv_y_samples},f)
f.close()
示例14: write_dict
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def write_dict(self, d, fn, args):
path = os.path.join(args.log_dir, "{}-{}-{}-tau_{:.4f}.pkl".format(args.name, self.ds_name, fn, self.tau))
if args.verbose: print("Saving stats in", path)
pickle.dump(d, open(path, "wb"))
示例15: dump
# 需要导入模块: import dill [as 别名]
# 或者: from dill import dump [as 别名]
def dump(self, args):
dicts = [{"args": args}, self.counts, self.counts_legal, self.counts_correct]
fns = ["args", "counts", "counts_legal", "counts_correct"]
for result, fn in zip(dicts, fns):
self.write_dict(result, fn, args)