Skip to content

Commit

Permalink
First cut at a basic Python implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed May 4, 2013
1 parent 53ced6f commit bc4fc2a
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/Record/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,61 @@
positions within the array.
"""

from _Record import Record
_marker = object()


class Record(object):

__record_schema__ = None
__slots__ = ('_data', '_schema')

def __init__(self, data=None):
cls_schema = type(self).__record_schema__
if cls_schema is None:
cls_schema = {}
self._schema = schema = cls_schema
if data is not None:
self._data = data
else:
self._data = (None, ) * len(schema)

def __getstate__(self):
return self._data

def __setstate__(self, state):
self.__init__(state)

def __getitem__(self, key):
if isinstance(key, int):
pos = key
else:
try:
pos = self._schema[key]
except IndexError:
raise TypeError('invalid record schema')
return self._data[pos]

def __getattr__(self, key):
if key in self.__slots__:
return object.__getattribute__(self, key)
try:
return self.__getitem__(key)
except KeyError:
raise AttributeError(key)

def __setitem__(self, key, value):
if isinstance(key, int):
pos = key
else:
try:
pos = self._schema[key]
except IndexError:
raise TypeError('invalid record schema')
old = self._data
self._data = old[:pos] + (value, ) + old[pos + 1:]

def __setattr__(self, key, value):
if key in self.__slots__:
object.__setattr__(self, key, value)
else:
self.__setitem__(key, value)

0 comments on commit bc4fc2a

Please sign in to comment.