Skip to content
Snippets Groups Projects
Commit e695de57 authored by morkolai's avatar morkolai
Browse files

#69 Fikset docstring og parameternavn

parent 3d85c8de
No related branches found
No related tags found
1 merge request!38#69 Komprimering og dekomprimering
Pipeline #75782 passed
......@@ -5,46 +5,62 @@ import json
import codecs
def compress(soi):
def compress(obj):
"""
Compress a soi using the lzma algorithm.
Compress a json compatible object using the lzma algorithm.
JSON compapitle object are python types that are posible to convert to
JSON types. Se overview in this link:
https://docs.python.org/3/library/json.html#py-to-json-table
Parameters
----------
soi : dict
soi as dict to be compresesed
obj : json compatible object
Object to compress
Returns
-------
string
The compressed soi as a string
The compressed data casted from bytest to string
"""
soi = json.dumps(soi)
soi = soi.encode(encoding="ascii")
return str(lzma.compress(soi))
obj = json.dumps(obj)
obj = obj.encode(encoding="ascii")
return str(lzma.compress(obj))
def decompress(file):
def decompress(lzma_compressed_json_string):
"""
Decompress to python dict.
Decompress a lzma compressed json string.
Decompresses a json compatible python object which is casted from bytes to
string.
Parameters
----------
file : string
lzma_compressed_json_string : string
The string of the compressed object
Returns
-------
dict
The decompressed file as dict
python object
One of these python objects:
https://docs.python.org/3/library/json.html#py-to-json-table
"""
# Removes extra b' notation
file = file[2:-1].encode(encoding="ascii")
lzma_compressed_json_string = lzma_compressed_json_string[2:-1].encode(
encoding="ascii"
)
# Remove double \\
file = codecs.escape_decode(file, "hex")
lzma_compressed_json_string = codecs.escape_decode(
lzma_compressed_json_string, "hex"
)
# Decompress
file = lzma.decompress(file[0])
lzma_compressed_json_string = lzma.decompress(
lzma_compressed_json_string[0]
)
# Cast to ascii string
file = file.decode(encoding="ascii")
lzma_compressed_json_string = lzma_compressed_json_string.decode(
encoding="ascii"
)
# Return as python datastructure
return json.loads(file)
return json.loads(lzma_compressed_json_string)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment