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")
topID = 0 seenID = topID codeFreezeID = 1 def registerType(name): global topID topID += 1 name = name + "ID" globals()["name"] = topID def identity(fn): return 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):