From 87deddc879ba2342b6f603ce804c664fe669d708 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 19 Nov 2018 16:55:22 +0000 Subject: [PATCH] RecordConstructorInfo for python --- implementations/python/preserves/preserves.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/implementations/python/preserves/preserves.py b/implementations/python/preserves/preserves.py index a02b238..f434692 100644 --- a/implementations/python/preserves/preserves.py +++ b/implementations/python/preserves/preserves.py @@ -75,6 +75,44 @@ class Record(object): for f in self.fields: encoder.append(f) + @staticmethod + def makeConstructor(labelSymbolText, fieldNames): + return Record.makeBasicConstructor(Symbol(labelSymbolText), fieldNames) + + @staticmethod + def makeBasicConstructor(label, fieldNames): + if type(fieldNames) == str: + fieldNames = fieldNames.split() + arity = len(fieldNames) + def ctor(*fields): + if len(fields) != arity: + raise Exception("Record: cannot instantiate %r expecting %d fields with %d fields"%( + label, + arity, + len(fields))) + return Record(label, fields) + ctor.constructorInfo = RecordConstructorInfo(label, arity) + ctor.isClassOf = lambda v: \ + isinstance(v, Record) and v.key == label and len(v.fields) == arity + return ctor + +class RecordConstructorInfo(object): + def __init__(self, key, arity): + self.key = key + self.arity = arity + + def __eq__(self, other): + return isinstance(other, RecordConstructorInfo) and \ + (self.key, self.arity) == (other.key, other.arity) + + def __hash__(self): + if self.__hash is None: + self.__hash = hash((self.key, self.arity)) + return self.__hash + + def __repr__(self): + return str(self.key) + '/' + str(self.arity) + # Blub blub blub class ImmutableDict(dict): def __init__(self, *args, **kwargs):