forked from syndicate-lang/preserves
Friendlier record accessors
This commit is contained in:
parent
5d9b03171d
commit
c1898ef73f
|
@ -97,6 +97,17 @@ class Record(object):
|
|||
ctor.constructorInfo = RecordConstructorInfo(label, arity)
|
||||
ctor.isClassOf = lambda v: \
|
||||
isinstance(v, Record) and v.key == label and len(v.fields) == arity
|
||||
def ensureClassOf(v):
|
||||
if not ctor.isClassOf(v):
|
||||
raise TypeError("Record: expected %r/%d, got %r" % (label, arity, v))
|
||||
return v
|
||||
ctor.ensureClassOf = ensureClassOf
|
||||
for fieldIndex in range(len(fieldNames)):
|
||||
fieldName = fieldNames[fieldIndex]
|
||||
# Stupid python scoping bites again
|
||||
def getter(fieldIndex):
|
||||
return lambda v: ensureClassOf(v)[fieldIndex]
|
||||
setattr(ctor, '_' + fieldName, getter(fieldIndex))
|
||||
return ctor
|
||||
|
||||
class RecordConstructorInfo(object):
|
||||
|
|
|
@ -204,3 +204,18 @@ class CodecTests(unittest.TestCase):
|
|||
self._roundtrip(SequenceStream([StringStream([b'abc']), StringStream([b'def'])]),
|
||||
_buf(0x2C, 0x25, 0x63, 'abc', 0x35, 0x25, 0x63, 'def', 0x35, 0x3C),
|
||||
back=(u'abc', u'def'))
|
||||
|
||||
class RecordTests(unittest.TestCase):
|
||||
def test_getters(self):
|
||||
T = Record.makeConstructor('t', 'x y z')
|
||||
T2 = Record.makeConstructor('t', 'x y z')
|
||||
U = Record.makeConstructor('u', 'x y z')
|
||||
t = T(1, 2, 3)
|
||||
self.assertTrue(T.isClassOf(t))
|
||||
self.assertTrue(T2.isClassOf(t))
|
||||
self.assertFalse(U.isClassOf(t))
|
||||
self.assertEqual(T._x(t), 1)
|
||||
self.assertEqual(T2._y(t), 2)
|
||||
self.assertEqual(T._z(t), 3)
|
||||
with self.assertRaises(TypeError):
|
||||
U._x(t)
|
||||
|
|
|
@ -5,7 +5,7 @@ except ImportError:
|
|||
|
||||
setup(
|
||||
name="preserves",
|
||||
version="0.0.4",
|
||||
version="0.0.5",
|
||||
author="Tony Garnock-Jones",
|
||||
author_email="tonyg@leastfixedpoint.com",
|
||||
license="GNU General Public License v3 or later (GPLv3+)",
|
||||
|
|
Loading…
Reference in New Issue