I am implementing a SQL-like datbase system in python, and so I needed to implement a value changing function, but what I ended up with seems very convoluted.
Here is code relevant to the value changing:
def _get_primary_rowval(self):
idx = 0
for n in self.def_column:
if n.is_primary:
return idx
idx += 1
def _get_row_val_by_ident(self, ident):
idx = 0
idents = []
for n in self.def_column:
if n.identifier == ident:
return idx
idents.append(n.identifier)
idx += 1
raise AttributeError("No Columns have the identifier given. Identifier given: {0}, Identifiers Possible: {1}".format(ident, idents))
def change_val(self, col_ident, new_val, primary_value):
for n in self.rows:
if n.contents[self._get_primary_rowval()] == primary_value:
n.contents[self._get_row_val_by_ident(col_ident)] = new_val
Here is the code for the Row and Column Classes:
class TableObject(object):
def __init__(self, ident):
self.identifier = ident
class Column(TableObject):
def __init__(self, ident, arg_type, primary = False):
super().__init__(ident)
self.arg_type = arg_type
self.is_primary = primary
def check_type(self, applicant_object):
if type(applicant_object) == self.arg_type:
return True
else:
return False
class Row(TableObject):
def __init__(self, contents):
self.contents = []
for n in contents:
self.contents.append(n)
def get_contents(self):
return self.contents
I was wondering if there was a better/more Pythonic way to do this.