import tables, subprocess

class TestSmall(tables.IsDescription):
    Str  = tables.StringCol(16)
    Int  = tables.Int32Col()

class TestMedium(tables.IsDescription):
    Str  = tables.StringCol(160)
    Int  = tables.Int32Col()

class TestBig(tables.IsDescription):
    Str  = tables.StringCol(1600)
    Int  = tables.Int32Col()

def get_db_size(filename):
    sout = subprocess.Popen("sync;du -sh %s" % filename, shell=True,
                            stdout=subprocess.PIPE).stdout
    line = [l for l in sout][0]
    return line.split()[0]

def createFile(tdescr):
    filename = '/tmp/%s.h5' % tdescr.__name__
    f = tables.openFile(filename, 'w')
    table = f.createTable(f.root, 'table', tdescr, tdescr.__name__,
                          tables.Filters(complevel=1))
    # Fill the table with 1 million of entries
    row = table.row
    for i in xrange(1000*100):
        row['Str'] = str(i)
        row['Int'] = i
        row.append()
    # Close the file
    f.close()
    print "Size for %s: %s" %(filename, get_db_size(filename))

for tdescr in (TestSmall, TestMedium, TestBig):
    createFile(tdescr)

