本文整理汇总了Python中tempfile.NamedTemporaryFile方法的典型用法代码示例。如果您正苦于以下问题:Python tempfile.NamedTemporaryFile方法的具体用法?Python tempfile.NamedTemporaryFile怎么用?Python tempfile.NamedTemporaryFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempfile
的用法示例。
在下文中一共展示了tempfile.NamedTemporaryFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_full_tokenizer
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def test_full_tokenizer(self):
vocab_tokens = [
"[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn",
"##ing", ","
]
with tempfile.NamedTemporaryFile(delete=False) as vocab_writer:
vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))
vocab_file = vocab_writer.name
tokenizer = tokenization.FullTokenizer(vocab_file)
os.unlink(vocab_file)
tokens = tokenizer.tokenize(u"UNwant\u00E9d,running")
self.assertAllEqual(tokens, ["un", "##want", "##ed", ",", "runn", "##ing"])
self.assertAllEqual(
tokenizer.convert_tokens_to_ids(tokens), [7, 4, 5, 10, 8, 9])
示例2: isUpdatesAvailable
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def isUpdatesAvailable(cls, path):
if sys.version_info < (3, 0):
return False
# pylint: disable=broad-except
if not os.path.isfile(os.path.join(path, "files.xml")):
return True
try:
available = dict()
for it in ET.parse(os.path.join(path, "files.xml")).iter():
if it.tag == "File":
available[it.text] = datetime.datetime.strptime(it.attrib["Modified"], "%d-%m-%Y")
path = NamedTemporaryFile()
path.close()
urllib.request.urlretrieve("https://www.gurux.fi/obis/files.xml", path.name)
for it in ET.parse(path.name).iter():
if it.tag == "File":
tmp = datetime.datetime.strptime(it.attrib["Modified"], "%d-%m-%Y")
if not it.text in available or available[it.text] != tmp:
return True
except Exception as e:
print(e)
return True
return False
示例3: convert
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def convert(netflow, tmpdir, opts='', prefix=None):
'''
Convert `nfcapd` file to a comma-separated output format.
:param netflow : Path of binary file.
:param tmpdir : Path of local staging area.
:param opts : A set of options for `nfdump` command.
:param prefix : If `prefix` is specified, the file name will begin with that;
otherwise, a default `prefix` is used.
:returns : Path of CSV-converted file.
:rtype : ``str``
:raises OSError: If an error occurs while executing the `nfdump` command.
'''
logger = logging.getLogger('SPOT.INGEST.FLOW.PROCESS')
with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
command = COMMAND.format(netflow, opts, fp.name)
logger.debug('Execute command: {0}'.format(command))
Util.popen(command, raises=True)
return fp.name
示例4: convert
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def convert(logfile, tmpdir, opts='', prefix=None):
'''
Copy log file to the local staging area.
:param logfile: Path of log file.
:param tmpdir : Path of local staging area.
:param opts : A set of options for the `cp` command.
:param prefix : If `prefix` is specified, the file name will begin with that;
otherwise, a default `prefix` is used.
:returns : Path of log file in local staging area.
:rtype : ``str``
'''
logger = logging.getLogger('SPOT.INGEST.PROXY.PROCESS')
with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
command = COMMAND.format(opts, logfile, fp.name)
logger.debug('Execute command: {0}'.format(command))
Util.popen(command, raises=True)
return fp.name
示例5: convert
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def convert(pcap, tmpdir, opts='', prefix=None):
'''
Convert `pcap` file to a comma-separated output format.
:param pcap : Path of binary file.
:param tmpdir : Path of local staging area.
:param opts : A set of options for `tshark` command.
:param prefix : If `prefix` is specified, the file name will begin with that;
otherwise, a default `prefix` is used.
:returns : Path of CSV-converted file.
:rtype : ``str``
:raises OSError: If an error occurs while executing the `tshark` command.
'''
logger = logging.getLogger('SPOT.INGEST.DNS.PROCESS')
with tempfile.NamedTemporaryFile(prefix=prefix, dir=tmpdir, delete=False) as fp:
command = COMMAND.format(pcap, opts, fp.name)
logger.debug('Execute command: {0}'.format(command))
Util.popen(command, raises=True)
return fp.name
示例6: parse_config
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def parse_config(config_strings):
temp_file = tempfile.NamedTemporaryFile()
config_path = f'{temp_file.name}.py'
with open(config_path, 'w') as f:
f.write(config_strings)
config = Config.fromfile(config_path)
is_two_stage = True
is_ssd = False
is_retina = False
reg_cls_agnostic = False
if 'rpn_head' not in config.model:
is_two_stage = False
# check whether it is SSD
if config.model.bbox_head.type == 'SSDHead':
is_ssd = True
elif config.model.bbox_head.type == 'RetinaHead':
is_retina = True
elif isinstance(config.model['bbox_head'], list):
reg_cls_agnostic = True
elif 'reg_class_agnostic' in config.model.bbox_head:
reg_cls_agnostic = config.model.bbox_head \
.reg_class_agnostic
temp_file.close()
return is_two_stage, is_ssd, is_retina, reg_cls_agnostic
示例7: predict_on_batch
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def predict_on_batch(self, inputs):
# write test fasta file
temp_input = tempfile.NamedTemporaryFile(suffix = ".txt")
test_fname = temp_input.name
encode_sequence_into_fasta_file(ofname = test_fname, seq = inputs.tolist())
# test gkmsvm
temp_ofp = tempfile.NamedTemporaryFile(suffix = ".txt")
threads_option = '-T %s' % (str(self.threads))
verbosity_option = '-v 0'
command = ' '.join(['gkmpredict',
test_fname,
self.model_file,
temp_ofp.name,
threads_option,
verbosity_option])
#process = subprocess.Popen(command, shell=True)
#process.wait() # wait for it to finish
exit_code = os.system(command)
temp_input.close()
assert exit_code == 0
# get classification results
temp_ofp.seek(0)
y = np.array([line.split()[-1] for line in temp_ofp], dtype=float)
temp_ofp.close()
return np.expand_dims(y, 1)
示例8: save_pyoptix_conf
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def save_pyoptix_conf(nvcc_path, compile_args, include_dirs, library_dirs, libraries):
try:
config = ConfigParser()
config.add_section('pyoptix')
config.set('pyoptix', 'nvcc_path', nvcc_path)
config.set('pyoptix', 'compile_args', os.pathsep.join(compile_args))
config.set('pyoptix', 'include_dirs', os.pathsep.join(include_dirs))
config.set('pyoptix', 'library_dirs', os.pathsep.join(library_dirs))
config.set('pyoptix', 'libraries', os.pathsep.join(libraries))
tmp = NamedTemporaryFile(mode='w+', delete=False)
config.write(tmp)
tmp.close()
config_path = os.path.join(os.path.dirname(sys.executable), 'pyoptix.conf')
check_call_sudo_if_fails(['cp', tmp.name, config_path])
check_call_sudo_if_fails(['cp', tmp.name, '/etc/pyoptix.conf'])
check_call_sudo_if_fails(['chmod', '644', config_path])
check_call_sudo_if_fails(['chmod', '644', '/etc/pyoptix.conf'])
except Exception as e:
print("PyOptiX configuration could not be saved. When you use pyoptix.Compiler, "
"nvcc path must be in PATH, OptiX library paths must be in LD_LIBRARY_PATH, and pyoptix.Compiler "
"attributes should be set manually.")
示例9: test_prepareHostConfig
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def test_prepareHostConfig(settings, detectSystemDevices):
"""
Test paradrop.core.config.hostconfig.prepareHostConfig
"""
from paradrop.core.config.hostconfig import prepareHostConfig
devices = {
'wan': [{'name': 'eth0'}],
'lan': list(),
'wifi': list()
}
detectSystemDevices.return_value = devices
source = tempfile.NamedTemporaryFile(delete=True)
source.write("{test: value}")
source.flush()
settings.HOST_CONFIG_FILE = source.name
settings.DEFAULT_LAN_ADDRESS = "1.2.3.4"
settings.DEFAULT_LAN_NETWORK = "1.0.0.0/24"
config = prepareHostConfig()
assert config['test'] == 'value'
示例10: DownloadFileTemp
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def DownloadFileTemp(self, url, max_retries=5, show_progress=False):
"""Downloads a file to temporary storage.
Args:
url: The address of the file to be downloaded.
max_retries: The number of times to attempt to download
a file if the first attempt fails.
show_progress: Print download progress to stdout (overrides default).
Returns:
A string containing a path to the temporary file.
"""
destination = tempfile.NamedTemporaryFile()
self._save_location = destination.name
destination.close()
if self._beyondcorp.CheckBeyondCorp():
url = self._SetUrl(url)
max_retries = -1
file_stream = self._OpenStream(url, max_retries)
self._StreamToDisk(file_stream, show_progress)
return self._save_location
示例11: test_csv
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def test_csv():
expected = '\n'.join([
'source_context, test_context',
'smother/tests/demo.py:11,test2',
'smother/tests/demo.py:4,test4',
'',
])
runner = CliRunner()
with NamedTemporaryFile(mode='w+') as tf:
result = runner.invoke(
cli,
['-r', 'smother/tests/.smother_2',
'csv',
tf.name
]
)
assert result.exit_code == 0
tf.seek(0)
assert tf.read() == expected
示例12: test_semantic_csv
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def test_semantic_csv():
expected = '\n'.join([
'source_context, test_context',
'smother.tests.demo,test4',
'smother.tests.demo:bar,test2',
'',
])
runner = CliRunner()
with NamedTemporaryFile(mode='w+') as tf:
result = runner.invoke(
cli,
['-r', 'smother/tests/.smother_2',
'--semantic',
'csv',
tf.name
]
)
assert result.exit_code == 0
tf.seek(0)
assert tf.read() == expected
示例13: translate_files_slurm
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def translate_files_slurm(args, cmds, expected_output_files):
conda_env = '/private/home/pipibjc/.conda/envs/fairseq-20190509'
for cmd in cmds:
with TempFile('w') as script:
sh = f"""#!/bin/bash
source activate {conda_env}
{cmd}
"""
print(sh)
script.write(sh)
script.flush()
cmd = f"sbatch --gres=gpu:1 -c {args.cpu + 2} {args.sbatch_args} --time=15:0:0 {script.name}"
import sys
print(cmd, file=sys.stderr)
check_call(cmd, shell=True)
# wait for all outputs has finished
num_finished = 0
while num_finished < len(expected_output_files):
num_finished = 0
for output_file in expected_output_files:
num_finished += 1 if check_finished(output_file) else 0
if num_finished < len(expected_output_files):
time.sleep(3 * 60)
print("sleeping for 3m ...")
示例14: gatys
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def gatys(content, style, outfile, size, weight, stylescale, algparams):
"""Runs Gatys et al style-transfer algorithm
References:
* https://arxiv.org/abs/1508.06576
* https://github.com/jcjohnson/neural-style
"""
# Gatys can only process one combination of content, style, weight and scale at a time, so we need to iterate
tmpout = NamedTemporaryFile(suffix=".png")
runalgorithm("gatys", [
"-content_image", content,
"-style_image", style,
"-style_weight", weight * 100, # Because content weight is 100
"-style_scale", stylescale,
"-output_image", tmpout.name,
"-image_size", size if size is not None else shape(content)[0],
*algparams
])
# Transform to original file format
convert(tmpout.name, outfile)
tmpout.close()
示例15: __on_apply
# 需要导入模块: import tempfile [as 别名]
# 或者: from tempfile import NamedTemporaryFile [as 别名]
def __on_apply(self, *__):
from ...models import BackupJSON
try:
paraphrase = self.paraphrase_widget.entry.get_text()
if not paraphrase:
paraphrase = " "
output_file = path.join(GLib.get_user_cache_dir(),
path.basename(NamedTemporaryFile().name))
status = GPG.get_default().decrypt_json(self._filename, paraphrase, output_file)
if status.ok:
BackupJSON.import_file(output_file)
self.destroy()
else:
self.__send_notification(_("There was an error during the import of the encrypted file."))
except AttributeError:
Logger.error("[GPG] Invalid JSON file.")