__getitem__, __setitem__ multiple keys Python

好久不见. 提交于 2019-12-22 10:59:24

问题


I am trying to create a class that stores data in a local buffer as well as acts as an interface to a database. I've got following code:

class Table(object):    
    def __init__(self, tableName, **columnDict):
       self.tableName      = tableName
       self.columns        = {}
       self.types          = {}
       self.columns['id']  = []
       self.types['id']    = 'INT PRIMARY KEY NOT NULL'
       for name in columnDict:
           self.columns[name] = []
           self.types[name]    = columnDict[name]

    def updateBufferRow(self, index, updateDict):
       for key in updateDict:
           self.columns[key][index] = updateDict[key]

    def getBufferRow(self, index):
       row = {}
       for key in self.columns:
           row[key] = self.columns[key][index]
       return row

    def __getitem__(self, key, **args):
       """ Allows using self[key] method """
       return self.getBufferRow(key)

    def __setitem__(self, key, value, **args):
       """ Allows using self[key] = value method """
       self.updateBufferRow(key, value)

Here is how I initialize the table:

testTable = Table('BestTable', test = 'TestType', test2='INT')

It works as expected the only thing if I try:

testTable[0]['test'] = "LALALA"

It does nothing, on the other hand this updates rather than overwrites the table:

testTable[0] = {"test": "LALALA"}

I know I have to rewrite updateBufferRow() and getBufferRow() methods, the only thing I am not quite sure is how to get multiple keys using __getitem__ and __setitem__ methods Any help/hints will be greatly appreciated. Thank you guys!


回答1:


The dict returned by your __getitem__ has no relation any more with your columns. You'll need to return something that perhaps looks like a dict but maps __setattr__ calls back to your table columns:

class Row(dict):
    def __init__(self, table, index, *args, **kw):
        self._table, self._index = table, index
        super(Row, self).__init__(*args, **kw)

    def __setitem__(self, key, value):
        super(Row, self).__setitem__(key, value)
        self._table.columns[key][self._index] = value

then return that instead of a regular dict:

def getBufferRow(self, index):
   row = {}
   for key in self.columns:
       row[key] = self.columns[key][index]
   return Row(self, index, row)


来源:https://stackoverflow.com/questions/15417962/getitem-setitem-multiple-keys-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!