Skip to content

Commit

Permalink
row item values should be converted "on get" instead of "on read"
Browse files Browse the repository at this point in the history
  • Loading branch information
Infactum committed Jan 30, 2016
1 parent 2f21378 commit b352107
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions onec_dtools/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,6 @@ def decorator(t):
gen = read_value_from_blob(self.db_file, table_description.blob_offset,
parsed_row[field_name].offset, decorator(field_type))
parsed_row[field_name] = NValue(parsed_row[field_name].size, gen)
parsed_row.conversion_fns.pop(field_name, None)

yield parsed_row
18 changes: 17 additions & 1 deletion onec_dtools/db_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def get_null_field_parser_info(field_description):
return fpi


class RowItem(collections.UserDict):
def __init__(self, **kwargs):
self.conversion_fns = {}
super().__init__(kwargs)

def __getitem__(self, key):
item = super().__getitem__(key)
if key in self.conversion_fns:
return self.conversion_fns[key](item)
return item


class Row(object):
def __init__(self, table_description):
self.columns = []
Expand All @@ -116,4 +128,8 @@ def parse(self, buffer):
"""Возвращает словарь колонок и их значений.
:param buffer: последовательность байт, содержащая данные 1й строки
"""
return {c.name: c.conversion_fn(buffer[c.offset:c.offset + c.length]) for c in self.columns}
row_item = RowItem()
for c in self.columns:
row_item[c.name] = buffer[c.offset:c.offset + c.length]
row_item.conversion_fns[c.name] = c.conversion_fn
return row_item

0 comments on commit b352107

Please sign in to comment.