-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path21-pack.py
45 lines (36 loc) · 1.12 KB
/
21-pack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of Python Challenge Solutions
# https://github.com/scorphus/PythonChallengeSolutions
# Licensed under the BSD-3-Clause license:
# https://opensource.org/licenses/BSD-3-Clause
# Copyright (c) 2018-2020, Pablo S. Blum de Aguiar <scorphus@gmail.com>
# No link for this mission, please check the output data of mission 20
from cache import autocached
import bz2
import zlib
@autocached
def unpack(package):
"""Unpacks package, reversing its content, and alternating between the
different formats, while “logging” as it goes along"""
with open(package, "rb") as fd:
content = fd.read()
log = ""
while True:
try:
content = zlib.decompress(content)
log += " "
continue
except zlib.error:
pass
try:
content = bz2.decompress(content)
log += "#"
continue
except OSError:
pass
content = content[::-1]
log += "\n"
if content[0] != 120:
return log.rstrip()
print(unpack("package.pack"))