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
#---Connectez-vous à sqlite
con = sqlite3.connect(":memory:")
cur = con.cursor()
#---Vérifiez s'il y a une table
sql = 'SELECT COUNT(*) FROM sqlite_master WHERE type="table" AND name=?'
cur.execute(sql, ('test',))
#---Créer lorsqu'il n'y a pas de table
if not cur.fetchone()[0]:
sql = 'CREATE TABLE test(data TEXT)'
con.execute(sql)
#---Plongée des données
sql = 'INSERT INTO test (data) VALUES (?)'
con.execute(sql, (data,))
con.commit()
#---L'acquisition des données
sql = 'SELECT * from test'
cur.execute(sql)
row_lists = []
for row in cur:
row_lists.append(row[0])
#---Renvoie des données
return row_lists
if __name__ == '__main__' : test()
résultat
ROW DATA: 110 bytes
COMPRESS: 61 bytes
B2A: 69 bytes
ROW_DATA: 168 bytes
A2B: 61 bytes
DECOMPRESS: 110 bytes
Recommended Posts