From e695de57b29574c208d00f4212e5014df9c6ccab Mon Sep 17 00:00:00 2001 From: morkolai <nikolai-mork@live.no> Date: Fri, 20 Mar 2020 09:17:03 +0100 Subject: [PATCH] #69 Fikset docstring og parameternavn --- soitool/compressor.py | 52 ++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/soitool/compressor.py b/soitool/compressor.py index 985400e..bd3e400 100644 --- a/soitool/compressor.py +++ b/soitool/compressor.py @@ -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) -- GitLab