示例#1
0
def test_compression():
    """ Test compression and decompression functions """
    f_sha = hashlib.sha256(open(_f, 'rb').read()).hexdigest()
    temp_name = os.path.join('tests', next(tempfile._get_candidate_names()))

    fc = c.compress(_f, temp_name)
    fd = c.decompress(fc)
    assert f_sha == hashlib.sha256(open(fd, 'rb').read()).hexdigest()
    os.remove(fc); os.remove(fd)

    fc = c.compress(_f, temp_name, f_type='zip')
    fd = c.decompress(fc)
    assert f_sha == hashlib.sha256(open(fd, 'rb').read()).hexdigest()
    os.remove(fc); os.remove(fd)
示例#2
0
def test_compression():
    """ Test compression and decompression functions """
    f_sha = hashlib.sha256(open(_f, 'rb').read()).hexdigest()
    temp_name = os.path.join('tests', next(tempfile._get_candidate_names()))

    fc = c.compress(_f, temp_name)
    fd = c.decompress(fc)
    assert f_sha == hashlib.sha256(open(fd, 'rb').read()).hexdigest()
    os.remove(fc)
    os.remove(fd)

    fc = c.compress(_f, temp_name, f_type='zip')
    fd = c.decompress(fc)
    assert f_sha == hashlib.sha256(open(fd, 'rb').read()).hexdigest()
    os.remove(fc)
    os.remove(fd)
示例#3
0
 def read(self, fileName):
     uncompressed = QByteArray()
     # Read data
     f = QFile(fileName)
     if (f.open(QIODevice.ReadOnly)) :
         compressed = f.readAll()
         f.close()
         uncompressed, length = decompress(compressed, 48 * 48)
     
     # Check the data
     if (uncompressed.count() != 48 * 48) :
         self.mError = self.tr("This is not a valid Droidcraft map file!")
         return None
     
     uncompressed = uncompressed.data()
     # Build 48 x 48 map
     # Create a Map -> Create a Tileset -> Add Tileset to map
     # -> Create a TileLayer -> Fill layer -> Add TileLayer to Map
     map = Map(Map.Orientation.Orthogonal, 48, 48, 32, 32)
     mapTileset = Tileset.create("tileset", 32, 32)
     mapTileset.loadFromImage(QImage(":/tileset.png"), "tileset.png")
     map.addTileset(mapTileset)
     # Fill layer
     mapLayer =  TileLayer("map", 0, 0, 48, 48)
     # Load
     for i in range(0, 48 * 48):
         tileFile = int(uncompressed[i])&0xff
         y = int(i / 48)
         x = i - (48 * y)
         tile = mapTileset.tileAt(tileFile)
         mapLayer.setCell(x, y, Cell(tile))
     
     map.addLayer(mapLayer)
     return map
示例#4
0
 def test5(self):
     """
     Decompress a fairly large string without too many compressions in it.
     :return:
     """
     self.assertEqual(decompress('abadbadsakdlfjieafnealfjiasfja#5nad#7kkkjj'), \
         'abadbadsakdlfjieafnealfjiasfjaaaaanadddddddkkkjj')
示例#5
0
 def test6(self):
     """
     Decompress a string with a lot of compressions in it
     :return:
     """
     self.assertEqual(decompress('k#6j#7n#5i#7o#6k'),
                      'kkkkkkjjjjjjjnnnnniiiiiiiooooook')
示例#6
0
    def read(self, fileName):
        uncompressed = QByteArray()
        # Read data
        f = QFile(fileName)
        if (f.open(QIODevice.ReadOnly)):
            compressed = f.readAll()
            f.close()
            uncompressed, length = decompress(compressed, 48 * 48)

        # Check the data
        if (uncompressed.count() != 48 * 48):
            self.mError = self.tr("This is not a valid Droidcraft map file!")
            return None

        uncompressed = uncompressed.data()
        # Build 48 x 48 map
        # Create a Map -> Create a Tileset -> Add Tileset to map
        # -> Create a TileLayer -> Fill layer -> Add TileLayer to Map
        map = Map(Map.Orientation.Orthogonal, 48, 48, 32, 32)
        mapTileset = Tileset.create("tileset", 32, 32)
        mapTileset.loadFromImage(QImage(":/tileset.png"), "tileset.png")
        map.addTileset(mapTileset)
        # Fill layer
        mapLayer = TileLayer("map", 0, 0, 48, 48)
        # Load
        for i in range(0, 48 * 48):
            tileFile = int(uncompressed[i]) & 0xff
            y = int(i / 48)
            x = i - (48 * y)
            tile = mapTileset.tileAt(tileFile)
            mapLayer.setCell(x, y, Cell(tile))

        map.addLayer(mapLayer)
        return map
示例#7
0
def loads(frames, deserialize=True, deserializers=None):
   """ Transform bytestream back into Python value """
   frames = frames[::-1]  # reverse order to improve pop efficiency
   if not isinstance(frames, list):
      frames = list(frames)
   try:
      small_header = frames.pop()
      small_payload = frames.pop()
      msg = loads_msgpack(small_header, small_payload)
      if not frames:
         return msg

      header = frames.pop()
      header = msgpack.loads(header, use_list=False, raw=False, **msgpack_opts)
      #print("Header: ", header)
      keys = header["keys"]
      headers = header["headers"]
      bytestrings = set(header["bytestrings"])
      #print("Number of keys: ", len(keys))
      for key in keys:
         head = headers[key]
         count = head["count"]
         if count:
            fs = frames[-count::][::-1]
            del frames[-count:]
         else:
            fs = []

         if deserialize or key in bytestrings:
            if "compression" in head:
               fs = decompress(head, fs)
            fs = merge_frames(head, fs)
            value = _deserialize(head, fs, deserializers=deserializers)
         else:
            value = Serialized(head, fs)

         #print("Key: ", key)
         def put_in(keys, coll, val):
            """Inverse of get_in, but does type promotion in the case of lists"""
            if keys:
               holder = reduce(operator.getitem, keys[:-1], coll)
               #print("Holder: ", holder)
               if isinstance(holder, tuple):
                  holder = list(holder)
                  coll = put_in(keys[:-1], coll, holder)
               holder[keys[-1]] = val
            else:
               coll = val
            return coll

         #print("BEFORE PUT_IN (on this iteration):\nKey: ", key, ", Msg: ", msg, ", Value: ", value)
         msg = put_in(key, msg, value)
         #print("\nAFTER PUT_IN (on this iteration):\nKey: ", key, ", Msg: ", msg, ", Value: ", value)
      #print("\nMESSAGE BEING RETURNED:\n", msg, "\n")
      return msg
   except Exception:
      #logger.critical("Failed to deserialize", exc_info=True)
      print("CRITICAL: Failed to deserialize.")
      raise      
示例#8
0
 def test_decompress(self):
     g = test_compressed_graph()
     g1 = compression.decompress(g)
     for supersource, supertarget in g.edges:
         for source in supersource.split('+'):
             for target in supertarget.split('+'):
                 self.assertTrue(source in g1.nodes)
                 self.assertTrue(target in g1.nodes)
                 self.assertTrue((source, target) in g1.edges)
示例#9
0
def deserialize_bytes(b):
    frames = unpack_frames(b)
    header, frames = frames[0], frames[1:]
    if header:
        header = msgpack.loads(header, raw=False, use_list=False)
    else:
        header = {}
    frames = compression.decompress(header, frames)
    return deserialize(header, frames)
示例#10
0
def test_compression():

    filename = "lena.bmp"
    image = io.imread(filename)
    filename = filename.split(".")[0]

    co.write_as_mcf(image, filename)

    vec = co.read_as_np(filename)
    com = co.compress(image)

    co.write_as_mcf(com, "comp_lena")
    img2 = co.read_as_np("comp_lena")
    print(img2.shape)
    image = co.decompress(img2, image.shape)
    plt.imshow(image, cmap="gray")
    plt.show()
示例#11
0
def loads_msgpack(header, payload):
   """ Read msgpack header and payload back to Python object

   See Also:
     dumps_msgpack
   """
   if header:
      header = msgpack.loads(header, use_list=False, **msgpack_opts)
   else:
      header = {}

   if header.get("compression"):
      try:
         decompress = compressions[header["compression"]]["decompress"]
         payload = decompress(payload)
      except KeyError:
         print("ERROR: data is compressed as ", str(header["compression"]), " but we don't have this installed...")
         raise ValueError("Data is compressed as {} but we don't have this installed".format(str(header["compression"])))

   return msgpack.loads(payload, use_list=False, raw=False, **msgpack_opts)
示例#12
0
    def decodeLayerData(self, tileLayer, layerData, format):
        if format in [Map.LayerDataFormat.XML, Map.LayerDataFormat.CSV]:
            raise
            
        _layerData = QByteArray()
        _layerData.append(layerData)
        decodedData = QByteArray.fromBase64(_layerData)
        size = (tileLayer.width() * tileLayer.height()) * 4

        if (format == Map.LayerDataFormat.Base64Gzip or format == Map.LayerDataFormat.Base64Zlib):
            decodedData, size = decompress(decodedData, size)

        if (size != decodedData.length()):
            return DecodeError.CorruptLayerData

        data = decodedData.data()
        x = 0
        y = 0

        for i in range(0, size - 3, 4):
            gid = data[i] | data[i + 1] << 8 | data[i + 2] << 16 | data[i + 3] << 24

            result, ok = self.gidToCell(gid)
            if (not ok):
                self.mInvalidTile = gid
                if self.isEmpty():
                    return DecodeError.TileButNoTilesets
                else:
                    return DecodeError.InvalidTile

            tileLayer.setCell(x, y, result)

            x += 1
            if (x == tileLayer.width()):
                x = 0
                y += 1

        return DecodeError.NoError
示例#13
0
文件: backup.py 项目: sharph/forklift
 def _dec(self, data):
     return compression.decompress(self.config,
                                   crypto.decrypt(self.config, data))
示例#14
0
 def deserialize(self):
    frames = compression.decompress(self.header, self.frames)
    return deserialize(self.header, frames)
示例#15
0
 def test4(self):
     """
     Decompressing a small string
     :return:
     """
     self.assertEqual(decompress('a#5'), 'aaaaa')
示例#16
0
	def test_compress_decompress(self):
		for testCase in self.toTestList:
			# test compress output
			self.assertEqual(compress(testCase[0]),testCase[1])
			# test decompress output -- compressing then decompressing
			self.assertEqual(decompress(compress(testCase[0])),testCase[0])
示例#17
0
def draw_boxes(frame, x_coords, y_coords):
    if not x_coords or not y_coords:
        return 0
    else:
        x_coords, y_coords = compression.decompress(x_coords, y_coords)

    rectangles = []
    label_coords = []

    if len(x_coords) == len(y_coords):
        for i in range(0, (len(x_coords)-1), 2):
            for j in range(0, len(y_coords)-1, 2):
                x_min = x_coords[i]
                x_max = x_coords[i+1]
                y_min = y_coords[j]
                y_max = y_coords[j+1]
                width = (x_max - x_min)
                height = (y_max - y_min)
                num_pixels = np.sum(frame[y_min:y_max+1, x_min:x_max+1])
                if num_pixels < 5:
                    continue
                else:
                    rect = patches.Rectangle((x_min, y_min), width, height, linewidth=1, edgecolor='r',
                                             facecolor='none')
                    rectangles.append(rect)
                    x_label = x_max - 10
                    y_label = y_min - 2
                    label_coords.append((x_label, y_label))
    else:
        if len(x_coords) > len(y_coords):
            for i in range(0, len(y_coords)-1, 2):
                y_min = y_coords[i]
                y_max = y_coords[i+1]
                for j in range(0, len(x_coords)-1, 2):
                    x_min = x_coords[j]
                    x_max = x_coords[j+1]
                    width = (x_max - x_min)
                    height = (y_max - y_min)
                    rect = patches.Rectangle((x_min, y_min), width, height, linewidth=1, edgecolor='r',
                                             facecolor='none')
                    rectangles.append(rect)
                    x_label = x_max
                    y_label = y_min + 1
                    label_coords.append((x_label, y_label))
        else:
            for i in range(0, len(x_coords)-1, 2):
                x_min = y_coords[i]
                x_max = y_coords[i+1]
                for j in range(0, len(y_coords)-1, 2):
                    y_min = x_coords[j]
                    y_max = x_coords[j+1]
                    width = (x_max - x_min)
                    height = (y_max - y_min)
                    rect = patches.Rectangle((x_min, y_min), width, height, linewidth=1, edgecolor='r',
                                             facecolor='none')
                    rectangles.append(rect)
                    x_label = x_max
                    y_label = y_min + 1
                    label_coords.append((x_label, y_label))

    if rectangles:
        return rectangles, label_coords
    else:
        return 0
示例#18
0

if __name__ == "__main__":
    cli_input = sys.argv

    if invalid_input(cli_input):
        print("Please insert a valid command")
        raise Exception(
            "Commands should be like './main.py -(c/x) file.(txt/lz78) [-o result_file_name]'"
        )

    f_name = ""

    if cli_input[1] == "-c":
        if len(cli_input) == 5:
            f_name = cli_input[4] + ".z78"
        else:
            f_name = cli_input[2].split(".")[0] + ".z78"

        lz78comp.compress(cli_input[2], f_name)
        print("File ", cli_input[2], " compressed to ", f_name, " !")

    elif cli_input[1] == "-x":
        if len(cli_input) == 5:
            f_name = cli_input[4] + ".txt"
        else:
            f_name = cli_input[2].split(".")[0] + ".txt"

        decoded_text = lz78comp.decompress(cli_input[2])
        lz78comp.create_txt_file(f_name, decoded_text)
        print("File ", cli_input[2], " decompressed to ", f_name, " !")
示例#19
0
from sys import argv, exit
from compression import compress, decompress, usage

if len(argv) < 3:
	usage(argv[0])

if int(argv[1]) == 1:
	print "%r compressed is %r" % (argv[2], compress(argv[2]))
elif int(argv[1]) == 0:
	print "%r decompressed is %r" % (argv[2], decompress(argv[2]))
else:
	usage(argv[0])
示例#20
0
文件: backup.py 项目: sharph/forklift
 def _load_manifest(self, mid):
     data = self.transport.read_manifest(mid)
     return msgpack.loads(
         compression.decompress(self.config,
                                crypto.auth_then_decrypt(self.config,
                                                         data)))
示例#21
0
    def deserialize(self):
        from compression import decompress

        frames = decompress(self.header, self.frames)
        return deserialize(self.header, frames)
示例#22
0
 def loadObject(self,name):
     file = open(self.path % name ,'rb')
     obj = compression.decompress(file.read())
     file.close()
示例#23
0
 def do_decompress(self, output):
     compression.decompress(self.compressed_image_entry.get(), self.network_entry2.get(), output)
示例#24
0
import os
from compression import compress, decompress

degree = 2
directory1 = 'mri_mini'
directory2 = directory1 + '_compressed'
directory3 = directory1 + '_decompressed'

for file in os.listdir(directory1):
    compress(directory1, file, degree, directory2)

for file in os.listdir(directory2):
    decompress(directory2, file, directory3)

for file in os.listdir(directory1):
    f1 = open(directory1 + '/' + file, 'rb')
    f2 = open(directory3 + '/' + file, 'rb')
    f1 = f1.read()
    f2 = f2.read()

    if len(f1) != len(f2):
        print(False)
        print(file, 'len')

    for i in range(0, len(f1)):
        if f1[i] != f2[i]:
            print(False)
            print(file, 'val', i)
            break