开发者

Implementing sub-table (view into a table): designing class relationship

I'm using Python 3, but the question isn't really tied to the specific language.

I have class Table that implements a table with a primary key. An instance of that class contains the actual data (which is very large).

I want to allow users to create a sub-table by providing a filter for the rows of the Table. I don't want to copy the table, so I was planning to keep in the sub-table just the subset of the primary keys from the parent table.

Obviously, the sub-table is just a view into the parent table; it will change if the parent table changes, will become invalid if the parent table is destroyed, and will lose some of its rows if they are deleted from the parent table. [EDIT: to clarify, if parent table is changed, I don't care what happens to the sub-table; any behavior is fine.]

How should I connect th开发者_运维知识库e two classes? I was thinking of:

class Subtable(Table):
  def __init__(self, table, filter_function):
    # ...

My assumption was that Subtable keeps the interface of Table, except slightly overrides the inherited methods just to check if the row is in. Is this a good implementation?

The problem is, I'm not sure how to initialize the Subtable instance given that I don't want to copy the table object passed to it. Is it even possible?

Also I was thinking to give class Table an instance method that returns Subtable instance; but that creates a dependency of Table on Subtable, and I guess it's better to avoid?


I'm going to use the following (I omitted many methods such as sort, which work quite well in this arrangement; also omitted error handling):

class Table:
    def __init__(self, *columns, pkey = None):
        self.pkey = pkey
        self.__columns = columns
        self.__data = {}

    def __contains__(self, key):
        return key in self.__data

    def __iter__(self):
        for key in self.__order:
            yield key

    def __len__(self):
        return len(self.__data)

    def items(self):
        for key in self.__order:
            yield key, self.__data[key]

    def insert(self, *unnamed, **named):
        if len(unnamed) > 0:
            row_dict = {}
            for column_id, column in enumerate(self.__columns):
                row_dict[column] = unnamed[column_id]
        else:
            row_dict = named
        key = row_dict[self.pkey]
        self.__data[key] = row_dict

class Subtable(Table):
    def __init__(self, table, row_filter):
        self.__order = []
        self.__data = {}
        for key, row in table.items():
            if row_filter(row):
                self.__data[key] = row

Essentially, I'm copying the primary keys only, and create references to the data tied to them. If a row in the parent table is destroyed, it will still exist in the sub-table. If a row is modified in the parent table, it is also modified in the sub-table. This is fine, since my requirements was "anything goes when parent table is modified".

If you see any issues with this design, let me know please.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜