Commits

Nacho Álvarez committed 8688efe

Initial import

Comments (0)

Files changed (4)

Empty file added.

backends/__init__.py

Empty file added.

backends/pytables.py

+from tables import *
+import tables
+
+
+class TestBackend:
+
+  def __init__(self, filename, mode):
+
+    self.mode = mode
+
+    if mode == "w":
+      self.h5file = openFile(filename, mode, title = "test file")
+
+      keyLen = 150
+      valueLen = 400
+
+      class Item(IsDescription):
+        key = StringCol(keyLen)
+        value = StringCol(valueLen)
+
+      self.table = self.h5file.createTable('/', 'testdb', Item)
+      self.test_db_items = self.table.row
+
+    elif mode == "r":
+      self.table = tables.openFile(filename)
+      self.test_db_items = self.table.root.testdb
+      self.next_rec_num = 0   # Initialise next record counter
+      self.num_records = len(self.test_db_items)
+
+  def __setitem__(self, key, value):
+
+    self.test_db_items['key'] = key
+    self.test_db_items['value'] = value
+    self.test_db_items.append()
+
+  def __getitem__(self, key):
+
+    return self.test_db_items[key]
+
+  def __len__(self):
+
+    return len(self.test_db_items)
+
+  def first(self):
+
+    return (self.test_db_items[0][0], self.test_db_items[0][1])
+
+  def iteritems(self):
+
+    while(self.next_rec_num < self.num_records):
+      key = self.test_db_items[self.next_rec_num][0]
+      value = self.test_db_items[self.next_rec_num][1]
+  
+      self.next_rec_num += 1
+
+      yield (key, value)
+
+  def close(self):
+
+    self.table.flush()
+
+    if self.mode == "w":
+      self.h5file.close()
+
+#!/usr/bin/python
+import csv
+import time
+import getopt
+import imp
+import os
+
+FILENAME='/tmp/fichero1.csv'
+
+class Test:
+
+  def __init__(self, csv_reader, module):
+
+    # import backend
+    f, filename, description = imp.find_module('backends')
+    example_package = imp.load_module('backends', f, filename, description)
+
+    f, filename, description = imp.find_module(module, example_package.__path__)
+    try:
+        moduleObj = imp.load_module('backend.module', f, filename, description)
+    finally:
+        f.close()
+
+    # open test write database for chosen backend
+    test_file = '/tmp/test.db' 
+    test_db = moduleObj.TestBackend(test_file, "w")
+
+    # start write test to disk
+    start_write = time.time()
+    
+    i = 0
+    for row in csv_reader:
+      test_db[str(i)] = str(row)
+      i += 1
+
+    test_db.close()
+
+    end_write = time.time()
+
+    print "Module writing time: " + str(end_write - start_write)
+    
+    # start read test to memory from disk
+    start_read = time.time()
+
+    test_db = moduleObj.TestBackend(test_file, "r")
+
+    i = 0
+    for (key, value) in test_db.iteritems():
+      memvar = value
+      i += 1
+
+    test_db.close()
+
+    end_read = time.time()
+
+    print "Module reading time: " + str(end_read - start_read)
+
+def getCSVReader():
+  # CSV dialect where lines end in "\n"
+  csv.register_dialect('endline', lineterminator='\n')
+  csv_reader = csv.reader(open(FILENAME,'r'), delimiter=',')
+  return csv_reader
+
+# test experiment
+tester = Test(getCSVReader(), "pytables")