Implementing a SQL like structure in Python, changing values 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.
Column index lookups

If I understand your intentions correctly,
the _get_primary_rowval and _get_row_val_by_ident functions are used to find column indexes.
They iterate over the list of column definitions until they find a match.
Do the columns get reordered often? Or ever?
In a table-like structure, I would expect the columns to not change much,
and so the column indexes should not change either.
Rather than looping to get the indexes,
it would be better to store them in a dictionary,
so that lookups become \$O(1)\$ operations.

Consider for example this use of these methods:


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



For every row,
those _get methods are called,
every time looping over the list of columns.
This is very inefficient and an unusual thing to do.

I suggest to rework this logic:
use a dictionary to store the column indexes.
If the columns change, update the dictionary accordingly.

Use boolean values directly

Instead of this:


def check_type(self, applicant_object):
    if type(applicant_object) == self.arg_type:
        return True
    else:
        return False



You can do simply this:

def check_type(self, applicant_object):
    return type(applicant_object) == self.arg_type


Copying a list

Instead of this:


def __init__(self, contents):
    self.contents = []
    for n in contents:
        self.contents.append(n)



Use the .copy method:

self.contents = contents.copy()