def testProtocol(self): z = ProtocolFn("foo") z.extend(int, lambda x: "int") z.extend(ProtocolFn, lambda f: "fn") z.extend(float, lambda f: "float") self.assertEqual(z(1), "int") self.assertEqual(z(1.0), "float") self.assertEqual(z(z), "fn")
return identity writeDispatcher = ProtocolFn("freeze-write") read = MultiMethod(lambda id, *args: id) _codeAttrs = filter(lambda x: not x.startswith("_"), dir(pyTypeCode)) def writeCode(code, strm, state): strm.write(codeFreezeID) for attr in _codeAttrs: write(getattr(code, attr), strm, state) writeDispatcher.extend(pyTypeCode, writeCode) def write(obj, strm, state = None): if state is None: state = WriterState() if state.hasSeen(obj): strm.write(seenID) strm.write(state.getID(obj)) writeDispatcher(obj, strm, state) class WriterState(object):