compress.py
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
import zlib
from base64 import binascii
#-----------------------------------------------------------
# COMPRESS
#-----------------------------------------------------------
def z_compress(data):
#return buffer(zlib.compress(data))
return zlib.compress(data)
#-----------------------------------------------------------
# DECOMPRESS
#-----------------------------------------------------------
def z_decompress(data):
return zlib.decompress(data)
#-----------------------------------------------------------
# B2A
# Binary >> Base64 Ascii
#-----------------------------------------------------------
def b2a(data):
return binascii.b2a_base64(data)
#-----------------------------------------------------------
# A2B
# Base64 Ascii >> Binary
#-----------------------------------------------------------
def a2b(data):
return binascii.a2b_base64(data)
#-----------------------------------------------------------
# TEST
#-----------------------------------------------------------
def test():
data = '1234567890123456789012345678901234567890123456789012345678901234567890'
print 'ROW DATA: %3d bytes' % sys.getsizeof(data)
data = z_compress(data)
print 'COMPRESS: %3d bytes' % sys.getsizeof(data)
data = b2a(data)
print 'B2A: %3d bytes' % sys.getsizeof(data)
data_lists = sqlite_test(data)
for data in data_lists:
print 'ROW_DATA: %3d bytes' % sys.getsizeof(data)
data = a2b(data)
print 'A2B: %3d bytes' % sys.getsizeof(data)
data = z_decompress(data)
print 'DECOMPRESS: %3d bytes' % sys.getsizeof(data)
def sqlite_test(data):
import sqlite3
#---Connect to sqlite
con = sqlite3.connect(":memory:")
cur = con.cursor()
#---Check if there is a table
sql = 'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name=?'
cur.execute(sql, ('test',))
#---Create when there is no table
if not cur.fetchone()[0]:
sql = 'CREATE TABLE test(data TEXT)'
con.execute(sql)
#---Data plunge
sql = 'INSERT INTO test (data) VALUES (?)'
con.execute(sql, (data,))
con.commit()
#---Data acquisition
sql = 'SELECT * from test'
cur.execute(sql)
row_lists = []
for row in cur:
row_lists.append(row[0])
#---Returns data
return row_lists
if __name__ == '__main__' : test()
result
ROW DATA: 110 bytes
COMPRESS: 61 bytes
B2A: 69 bytes
ROW_DATA: 168 bytes
A2B: 61 bytes
DECOMPRESS: 110 bytes
Recommended Posts