本文整理汇总了Python中lzma.open方法的典型用法代码示例。如果您正苦于以下问题:Python lzma.open方法的具体用法?Python lzma.open怎么用?Python lzma.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lzma
的用法示例。
在下文中一共展示了lzma.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _token_to_filenames
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def _token_to_filenames(token):
if token[0] == '!':
pattern = token[1:]
filenames = glob.glob(pattern)
if not filenames:
raise RuntimeError('No filenames matched "%s" pattern' % pattern)
elif token[0] == '@':
filelist_name = sys.stdin if token == '@-' else token[1:]
with open(filelist_name) as filelist:
filenames = [line.rstrip('\n') for line in filelist]
directory = os.path.dirname(token[1:])
if directory != '.':
filenames = [f if f[0] != '/' else directory + '/' + f for f in filenames]
else:
filenames = [token]
return filenames
示例2: next_filehandle
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def next_filehandle(self):
"""Go to the next file and retrun its filehandle or None (meaning no more files)."""
filename = self.next_filename()
if filename is None:
fhandle = None
elif filename == '-':
fhandle = io.TextIOWrapper(sys.stdin.buffer, encoding=self.encoding)
elif filename == '<filehandle_input>':
fhandle = self.filehandle
else:
filename_extension = filename.split('.')[-1]
if filename_extension == 'gz':
myopen = gzip.open
elif filename_extension == 'xz':
myopen = lzma.open
elif filename_extension == 'bz2':
myopen = bz2.open
else:
myopen = open
fhandle = myopen(filename, 'rt', encoding=self.encoding)
self.filehandle = fhandle
return fhandle
示例3: save_structure_to_file
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def save_structure_to_file(structure: JsonExportable, filename: str) -> None:
"""Saves a :class:`Post`, :class:`Profile`, :class:`StoryItem` or :class:`Hashtag` to a '.json' or '.json.xz' file
such that it can later be loaded by :func:`load_structure_from_file`.
If the specified filename ends in '.xz', the file will be LZMA compressed. Otherwise, a pretty-printed JSON file
will be created.
:param structure: :class:`Post`, :class:`Profile`, :class:`StoryItem` or :class:`Hashtag`
:param filename: Filename, ends in '.json' or '.json.xz'
"""
json_structure = {'node': structure._asdict(),
'instaloader': {'version': __version__, 'node_type': structure.__class__.__name__}}
compress = filename.endswith('.xz')
if compress:
with lzma.open(filename, 'wt', check=lzma.CHECK_NONE) as fp:
json.dump(json_structure, fp=fp, separators=(',', ':'))
else:
with open(filename, 'wt') as fp:
json.dump(json_structure, fp=fp, indent=4, sort_keys=True)
示例4: test_not_closing_opened_fid
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_not_closing_opened_fid(self):
# Test that issue #2178 is fixed:
# verify could seek on 'loaded' file
with temppath(suffix='.npz') as tmp:
with open(tmp, 'wb') as fp:
np.savez(fp, data='LOVELY LOAD')
with open(tmp, 'rb', 10000) as fp:
fp.seek(0)
assert_(not fp.closed)
np.load(fp)['data']
# fp must not get closed by .load
assert_(not fp.closed)
fp.seek(0)
assert_(not fp.closed)
#FIXME: Is this still true?
示例5: check_compressed
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def check_compressed(self, fopen, suffixes):
# Test that we can load data from a compressed file
wanted = np.arange(6).reshape((2, 3))
linesep = ('\n', '\r\n', '\r')
for sep in linesep:
data = '0 1 2' + sep + '3 4 5'
for suffix in suffixes:
with temppath(suffix=suffix) as name:
with fopen(name, mode='wt', encoding='UTF-32-LE') as f:
f.write(data)
res = self.loadfunc(name, encoding='UTF-32-LE')
assert_array_equal(res, wanted)
with fopen(name, "rt", encoding='UTF-32-LE') as f:
res = self.loadfunc(f)
assert_array_equal(res, wanted)
# Python2 .open does not support encoding
示例6: test_utf8_file
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_utf8_file(self):
utf8 = b"\xcf\x96"
with temppath() as path:
with open(path, "wb") as f:
f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
ctl = np.array([
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
dtype=np.unicode)
assert_array_equal(test, ctl)
# test a mixed dtype
with open(path, "wb") as f:
f.write(b"0,testNonethe" + utf8)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
assert_equal(test['f0'], 0)
assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8"))
示例7: test_gzip_loadtxt
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_gzip_loadtxt():
# Thanks to another windows brokenness, we can't use
# NamedTemporaryFile: a file created from this function cannot be
# reopened by another open call. So we first put the gzipped string
# of the test reference array, write it to a securely opened file,
# which is then read from by the loadtxt function
s = BytesIO()
g = gzip.GzipFile(fileobj=s, mode='w')
g.write(b'1 2 3\n')
g.close()
s.seek(0)
with temppath(suffix='.gz') as name:
with open(name, 'wb') as f:
f.write(s.read())
res = np.loadtxt(name)
s.close()
assert_array_equal(res, [1, 2, 3])
示例8: _python2_bz2open
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def _python2_bz2open(fn, mode, encoding, newline):
"""Wrapper to open bz2 in text mode.
Parameters
----------
fn : str
File name
mode : {'r', 'w'}
File mode. Note that bz2 Text files are not supported.
encoding : str
Ignored, text bz2 files not supported in Python2.
newline : str
Ignored, text bz2 files not supported in Python2.
"""
import bz2
_check_mode(mode, encoding, newline)
if "t" in mode:
# BZ2File is missing necessary functions for TextIOWrapper
warnings.warn("Assuming latin1 encoding for bz2 text file in Python2",
RuntimeWarning, stacklevel=5)
mode = mode.replace("t", "")
return bz2.BZ2File(fn, mode)
示例9: abspath
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def abspath(self, path):
"""
Return absolute path of file in the Repository directory.
If `path` is an URL, then `abspath` will return either the location
the file exists locally or the location it would exist when opened
using the `open` method.
Parameters
----------
path : str
Can be a local file or a remote URL. This may, but does not
have to, include the `baseurl` with which the `Repository` was
initialized.
Returns
-------
out : str
Complete path, including the `DataSource` destination directory.
"""
return DataSource.abspath(self, self._fullpath(path))
示例10: _check_mode
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def _check_mode(mode, encoding, newline):
"""Check mode and that encoding and newline are compatible.
Parameters
----------
mode : str
File open mode.
encoding : str
File encoding.
newline : str
Newline for text files.
"""
if "t" in mode:
if "b" in mode:
raise ValueError("Invalid mode: %r" % (mode,))
else:
if encoding is not None:
raise ValueError("Argument 'encoding' not supported in binary mode")
if newline is not None:
raise ValueError("Argument 'newline' not supported in binary mode")
示例11: _compress_image_stream
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def _compress_image_stream(self, stream):
outfile = os.path.join(self.workflow.source.workdir,
EXPORTED_COMPRESSED_IMAGE_NAME_TEMPLATE)
if self.method == 'gzip':
outfile = outfile.format('gz')
fp = gzip.open(outfile, 'wb', compresslevel=6)
elif self.method == 'lzma':
outfile = outfile.format('xz')
fp = lzma.open(outfile, 'wb')
else:
raise RuntimeError('Unsupported compression format {0}'.format(self.method))
_chunk_size = 1024**2 # 1 MB chunk size for reading/writing
self.log.info('compressing image %s to %s using %s method',
self.workflow.image, outfile, self.method)
data = stream.read(_chunk_size)
while data != b'':
fp.write(data)
data = stream.read(_chunk_size)
self.uncompressed_size = stream.tell()
return outfile
示例12: test_record_unicode
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_record_unicode(self):
utf8 = b'\xcf\x96'
with temppath() as path:
with open(path, 'wb') as f:
f.write(b'1.312 foo' + utf8 + b' \n1.534 bar\n4.444 qux')
dt = [('num', np.float64), ('val', 'U4')]
x = np.fromregex(path, r"(?u)([0-9.]+)\s+(\w+)", dt, encoding='UTF-8')
a = np.array([(1.312, 'foo' + utf8.decode('UTF-8')), (1.534, 'bar'),
(4.444, 'qux')], dtype=dt)
assert_array_equal(x, a)
regexp = re.compile(r"([0-9.]+)\s+(\w+)", re.UNICODE)
x = np.fromregex(path, regexp, dt, encoding='UTF-8')
assert_array_equal(x, a)
#####--------------------------------------------------------------------------
示例13: test_utf8_file
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_utf8_file(self):
utf8 = b"\xcf\x96"
latin1 = b"\xf6\xfc\xf6"
with temppath() as path:
with open(path, "wb") as f:
f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
ctl = np.array([
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
dtype=np.unicode)
assert_array_equal(test, ctl)
# test a mixed dtype
with open(path, "wb") as f:
f.write(b"0,testNonethe" + utf8)
test = np.genfromtxt(path, dtype=None, comments=None,
delimiter=',', encoding="UTF-8")
assert_equal(test['f0'], 0)
assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8"))
示例14: test_gzip_loadtxt
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def test_gzip_loadtxt():
# Thanks to another windows brokeness, we can't use
# NamedTemporaryFile: a file created from this function cannot be
# reopened by another open call. So we first put the gzipped string
# of the test reference array, write it to a securely opened file,
# which is then read from by the loadtxt function
s = BytesIO()
g = gzip.GzipFile(fileobj=s, mode='w')
g.write(b'1 2 3\n')
g.close()
s.seek(0)
with temppath(suffix='.gz') as name:
with open(name, 'wb') as f:
f.write(s.read())
res = np.loadtxt(name)
s.close()
assert_array_equal(res, [1, 2, 3])
示例15: _python2_bz2open
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import open [as 别名]
def _python2_bz2open(fn, mode, encoding, newline):
"""Wrapper to open bz2 in text mode.
Parameters
----------
fn : str
File name
mode : {'r', 'w'}
File mode. Note that bz2 Text files are not supported.
encoding : str
Ignored, text bz2 files not supported in Python2.
newline : str
Ignored, text bz2 files not supported in Python2.
"""
import bz2
_check_mode(mode, encoding, newline)
if "t" in mode:
# BZ2File is missing necessary functions for TextIOWrapper
raise ValueError("bz2 text files not supported in python2")
else:
return bz2.BZ2File(fn, mode)