Python Database Server?

sandorlev 0 Tallied Votes 236 Views Share

Hello, I've been working on this self-made database. What do you think about it? What features should I create for it? I'd also like to get help in a select method, because I don't have the foggiest idea how it could be done. And what about making it a database server? What does a database server exactly do?

The usage is like this:

>>> db = Database('databases/family')
>>> table = db.get_table('general')
>>> for row in table:
print row


[0, 'Levente', 'Sandor', 15, 'Levi']
[1, 'Tamas', 'Karvalics', 15, 'Tomi']
[2, 'Matyas', 'Sandor', 17, 'Matyi']
[3, 'Peter', 'Karvalics', 17, 'Peti']
[4, 'Marta', 'Csonka', 43, 'Marti']
[5, 'Attila', 'Sandor', 45, 'Ati']

And then you can interact table with its methods.

import os

class Database:

    def __init__(self, path, mode='w'):
        self._path = path
        if mode == 'c':
            os.mkdir(self._path, 0755)

    def get_table(self, table_name):
        table_path = '%s/%s.slt' % (self._path, table_name)
        types_path = '%s/%s.slc' % (self._path, table_name)
        table_types = []
        table_content = [[]]
        f = open(types_path, 'r')
        content = f.readlines()
        f.close()
        for line in content:
            type_ = line.strip()
            table_types.append(type_)
        f = open(table_path, 'r')
        content = f.readlines()
        f.close()
        for column in content[0].split('\t'):
            column = column.strip()
            table_content[0].append(column)
        for row in content[1:]:
            row = row.split('\t')
            row[-1] = row[-1].strip()
            for column in row:
                index = row.index(column)
                type_ = table_types[index]
                exec('column = %s(column)' % (type_))
                row.pop(index)
                row.insert(index, column)
            table_content.append(row)
        table = _table(table_path, types_path, table_types,
                       self, table_name, table_content)
        return table

    def insert_table(self, table_name):
        table_path = '%s/%s.slt' % (self._path, table_name)
        types_path = '%s/%s.slc' % (self._path, table_name)
        f = open(table_path, 'w')
        f.write('Line')
        f.close()
        f = open(types_path, 'w')
        f.write('int')
        f.close()

    def remove_table(self, table_name):
        table_path = '%s/%s.slt' % (self._path, table_name)
        types_path = '%s/%s.slc' % (self._path, table_name)
        os.remove(table_path)
        os.remove(types_path)


class _table(list):

    def __init__(self, path, types_path, types, db, name, content):
        self._path = path
        self._types_path = types_path
        self._types = types
        self._db = db
        self._name = name
        self += content

    def checkout(self):
        while self:
            self.pop()
        table = self._db.get_table(self._name)
        self.__init__(table._path, table._types_path, table._types,
                      table._db, table._name, table)

    def commit(self):
        f = open(self._path, 'w')
        for row in self:
            for column in row:
                column = str(column)
                if column == str(row[-1]):
                    column += '\n'
                else:
                    column += '\t'
                f.write(column)
        f.close()
        f = open(self._types_path, 'w')
        for type_ in self._types:
            type_ += '\n'
            f.write(type_)
        f.close()

    #TODO: edit column

    def edit_row(self, number, **values):
        for arg in values:
            if arg in self[0]:
                index = self[0].index(arg)
                for row in self[1:]:
                    if row[0] == number:
                        row.pop(index)
                        row.insert(index, values[arg])

    def insert_column(self, name, type_='str', default_value=None, **values):
        self[0].append(name)
        self._types.append(type_)
        if default_value:
            exec('value = %s(default_value)' % type_)
        else:
            exec('value = %s()' % type_)
        for row in self[1:]:
            row.append(value)
        for arg in values:
            for row in self[1:]:
                if 'line%s' % str(row[0]) == arg:
                    exec('value = %s(values[arg])' % type_)
                    row[-1] = value

    def insert_row(self, **values):
        row = []
        for type_ in self._types:
            exec('row.append(%s())' % type_)
        for arg in values:
            if arg in self[0]:
                index = self[0].index(arg)
                row.pop(index)
                row.insert(index, values[arg])
        self.append(row)
        self._renumber_rows()

    def remove_column(self, name):
        index = self[0].index(name)
        self._types.pop(index)
        for row in self:
            row.pop(index)

    def remove_row(self, number):
        for row in self:
            if row[0] == number:
                self.remove(row)
        self._renumber_rows()

    def _renumber_rows(self):
        for count, row in enumerate(self[1:]):
            row[0] = count