本文整理匯總了Python中gzip.decompress方法的典型用法代碼示例。如果您正苦於以下問題:Python gzip.decompress方法的具體用法?Python gzip.decompress怎麽用?Python gzip.decompress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gzip
的用法示例。
在下文中一共展示了gzip.decompress方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: patch_id
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def patch_id(repo):
url = repo + 'repodata/repomd.xml'
repomd = requests.get(url)
if not repomd.ok:
return None
root = ET.fromstring(repomd.text)
cs = root.find(
'.//{http://linux.duke.edu/metadata/repo}data[@type="updateinfo"]/{http://linux.duke.edu/metadata/repo}location')
try:
url = repo + cs.attrib['href']
except AttributeError:
return None
repomd = requests.get(url).content
root = ET.fromstring(decompress(repomd))
return root.find('.//id').text
# take the first package name we find - often enough correct
示例2: testRequest
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def testRequest(self):
runner.run(
self._func, 'p', ('foo', 'bar'), host=_HOST, port=_PORT)
response = urllib.request.urlopen(
'http://%s:%s/profile' % (_HOST, _PORT))
response_data = gzip.decompress(response.read())
stats = json.loads(response_data.decode('utf-8'))
curr_filename = inspect.getabsfile(inspect.currentframe())
self.assertEqual(stats['p']['objectName'],
'_func @ %s (function)' % curr_filename)
self.assertTrue(len(stats['p']['callStats']) > 0)
self.assertTrue(stats['p']['totalTime'] > 0)
self.assertTrue(stats['p']['primitiveCalls'] > 0)
self.assertTrue(stats['p']['totalCalls'] > 0)
# pylint: enable=missing-docstring, blacklisted-name
示例3: testRequest
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def testRequest(self):
runner.run(
self._func, 'h', ('foo', 'bar'), host=_HOST, port=_PORT)
response = urllib.request.urlopen(
'http://%s:%s/profile' % (_HOST, _PORT))
response_data = gzip.decompress(response.read())
stats = json.loads(response_data.decode('utf-8'))
self.assertTrue(stats['h']['runTime'] > 0)
heatmaps = stats['h']['heatmaps']
curr_filename = inspect.getabsfile(inspect.currentframe())
self.assertEqual(stats['h']['objectName'],
'_func @ %s (function)' % curr_filename)
self.assertEqual(len(heatmaps), 1)
self.assertDictEqual(
heatmaps[0]['executionCount'], {'101': 1, '102': 1})
self.assertListEqual(
heatmaps[0]['srcCode'],
[['line', 100, u' def _func(foo, bar):\n'],
['line', 101, u' baz = foo + bar\n'],
['line', 102, u' return baz\n']])
# pylint: enable=missing-docstring, blacklisted-name
示例4: testRequest
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def testRequest(self):
runner.run(
self._func, 'c', ('foo', 'bar'), host=_HOST, port=_PORT)
response = urllib.request.urlopen(
'http://%s:%s/profile' % (_HOST, _PORT))
response_data = gzip.decompress(response.read())
stats = json.loads(response_data.decode('utf-8'))
curr_filename = inspect.getabsfile(inspect.currentframe())
self.assertEqual(stats['c']['objectName'],
'_func @ %s (function)' % curr_filename)
self.assertEqual(
stats['c']['sampleInterval'], flame_graph._SAMPLE_INTERVAL)
self.assertTrue(stats['c']['runTime'] > 0)
self.assertTrue(len(stats['c']['callStats']) >= 0)
self.assertTrue(stats['c']['totalSamples'] >= 0)
# pylint: enable=missing-docstring, blacklisted-name, protected-access
示例5: testRequest
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def testRequest(self):
runner.run(
self._func, 'm', ('foo', 'bar'), host=_HOST, port=_PORT)
response = urllib.request.urlopen(
'http://%s:%s/profile' % (_HOST, _PORT))
response_data = gzip.decompress(response.read())
stats = json.loads(response_data.decode('utf-8'))
curr_filename = inspect.getabsfile(inspect.currentframe())
self.assertEqual(stats['m']['objectName'],
'_func @ %s (function)' % curr_filename)
self.assertEqual(stats['m']['totalEvents'], 2)
self.assertEqual(stats['m']['codeEvents'][0][0], 1)
self.assertEqual(stats['m']['codeEvents'][0][1], 91)
self.assertEqual(stats['m']['codeEvents'][0][3], '_func')
self.assertEqual(stats['m']['codeEvents'][1][0], 2)
self.assertEqual(stats['m']['codeEvents'][1][1], 92)
self.assertEqual(stats['m']['codeEvents'][1][3], '_func')
# pylint: enable=missing-docstring, blacklisted-name
示例6: read_data
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def read_data(self, f):
"""Retrieve (uncompressed) data from WAD file object"""
f.seek(self.offset)
# assume files are small enough to fit in memory
data = f.read(self.compressed_size)
if self.type == 0:
return data
elif self.type == 1:
return gzip.decompress(data)
elif self.type == 2:
n, = struct.unpack('<L', data[:4])
target = data[4:4+n].rstrip(b'\0').decode('utf-8')
logger.debug(f"file redirection: {target}")
return None
elif self.type == 3:
return zstd_decompress(data)
raise ValueError(f"unsupported file type: {self.type}")
示例7: get_items_from_gzip
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def get_items_from_gzip(binary: bytes) -> Tuple[str, Dict[str, bytes]]:
"""
Return decompressed gzip contents.
Parameters
----------
binary : bytes
byte array of gz file
Returns
-------
Tuple[str, bytes]
File type + decompressed file
"""
archive_file = gzip.decompress(binary)
return "gz", {"gzip_file": archive_file}
示例8: main
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def main():
# Install the library.
os.makedirs(os.path.join(INSTALL_PATH, 'lib'), exist_ok=True)
with open(LIBRARY_PATH, 'wb') as fd:
fd.write(gzip.decompress(base64.b85decode(ENCODED_LIB_CONTENTS)))
os.fchmod(fd.fileno(), 0o755)
# Install the wrapper script.
os.makedirs(os.path.join(INSTALL_PATH, 'bin'), exist_ok=True)
with open(SCRIPT_PATH, 'w') as fd:
fd.write(DROPBOX_WRAPPER_CONTENTS)
os.fchmod(fd.fileno(), 0o755)
print("Installed the library and the wrapper script at:\n %s\n %s" % (LIBRARY_PATH, SCRIPT_PATH))
print("(To uninstall, simply delete them.)")
# Check that the correct 'dropbox' is in the $PATH.
result = subprocess.check_output(['which', 'dropbox']).decode().rstrip()
if result != SCRIPT_PATH:
print()
print("You will need to fix your $PATH! Currently, %r takes precedence." % result)
示例9: getstream
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def getstream(file):
# TODO -- gcs
if file.startswith('http://') or file.startswith('https://'):
response = requests.get(file)
status_code = response.status_code #TODO Do something with this
if file.endswith('.gz'):
content = response.content
text = gzip.decompress(content).decode('utf-8')
else:
text = response.text
f = io.StringIO(text)
return text
elif file.endswith('.gz'):
f = gzip.open(file, mode='rt')
else:
f = open(file, encoding='UTF-8')
return f
示例10: download_sifts_xml
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def download_sifts_xml(pdb_id, outdir='', force_rerun=False):
"""Download the SIFTS file for a PDB ID.
Args:
pdb_id (str): PDB ID
outdir (str): Output directory, current working directory if not specified.
force_rerun (bool): If the file should be downloaded again even if it exists
Returns:
str: Path to downloaded file
"""
baseURL = 'ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/'
filename = '{}.xml.gz'.format(pdb_id.lower())
outfile = op.join(outdir, filename.split('.')[0] + '.sifts.xml')
if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
response = urlopen(baseURL + filename)
with open(outfile, 'wb') as f:
f.write(gzip.decompress(response.read()))
return outfile
示例11: __iter__
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def __iter__(self):
keys = self._get_keys()
if self.shuffle:
random.shuffle(keys)
for key in keys:
chunk = json.loads(gzip.decompress(self.db[key]).decode('utf-8'))
# discard long examples
trunc_chunk = []
lens = []
for feat in chunk:
if feat['input_len'] > self.max_len:
continue
trunc_chunk.append(feat)
lens.append(feat['input_len'])
dataset = GPT2FeatureDataset(trunc_chunk, self.max_len)
sampler = BucketSampler(lens, self.bucket_size, self.batch_size,
droplast=True, shuffle=self.shuffle)
loader = DataLoader(dataset, batch_sampler=sampler,
num_workers=0, # can test multi-worker
collate_fn=GPT2FeatureDataset.collate)
yield from loader
示例12: download
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def download(self, mirror, meta):
"""Fetches compressed JSON data from NIST.
Nothing is done if we have already seen the same version of
the feed before.
Returns True if anything has been loaded successfully.
"""
url = mirror + self.download_uri
_log.info('Loading %s', url)
r = requests.get(url, headers=meta.headers_for(url))
r.raise_for_status()
if r.status_code == 200:
_log.debug('Loading JSON feed "%s"', self.name)
self.parse(gzip.decompress(r.content))
meta.update_headers_for(url, r.headers)
return True
else:
_log.debug('Skipping JSON feed "%s" (%s)', self.name, r.reason)
return False
示例13: raw_decode
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def raw_decode(cls, data, content_encoding=None):
content_encoding = ContentEncoding.of(content_encoding)
if content_encoding.is_gzip:
try:
data = gzip.decompress(data)
except (ValueError, TypeError):
raise ActorMessageDecodeError('gzip decompress failed')
try:
if content_encoding.is_json:
data = json.loads(data.decode('utf-8'))
else:
data = msgpack.unpackb(data, raw=False)
except json.JSONDecodeError:
raise ActorMessageDecodeError('json decode failed')
except msgpack.UnpackException:
raise ActorMessageDecodeError('msgpack decode failed')
return data
示例14: get_feed_content_divided_to_lines
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def get_feed_content_divided_to_lines(self, url, raw_response):
"""Fetch feed data and divides its content to lines
Args:
url: Current feed's url.
raw_response: The raw response from the feed's url.
Returns:
List. List of lines from the feed content.
"""
if self.feed_url_to_config and self.feed_url_to_config.get(url).get('is_zipped_file'): # type: ignore
response_content = gzip.decompress(raw_response.content)
else:
response_content = raw_response.content
return response_content.decode(self.encoding).split('\n')
示例15: content
# 需要導入模塊: import gzip [as 別名]
# 或者: from gzip import decompress [as 別名]
def content(self):
if self._content:
return self._content
encode = self.rep.msg.get('content-encoding', None)
try:
body = self.rep.read()
except socket.timeout:
body = b''
if encode == 'gzip':
body = gzip.decompress(body)
elif encode == 'deflate':
try:
body = zlib.decompress(body, -zlib.MAX_WBITS)
except:
body = zlib.decompress(body)
# redirect = self.rep.msg.get('location', None) # handle 301/302
self._content = body
return body