From 59bcced776ae9b57c14226d3b352648d81a98c4d Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 15 Aug 2021 23:40:28 -0400 Subject: [PATCH] @extend decorator --- implementations/python/preserves/schema.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/implementations/python/preserves/schema.py b/implementations/python/preserves/schema.py index e6d0d5d..37625b6 100644 --- a/implementations/python/preserves/schema.py +++ b/implementations/python/preserves/schema.py @@ -360,6 +360,10 @@ class Compiler: c._set_schema(self.root, module_path, n, d, None, None) ns[n] = c +# a decorator +def extend(cls): + return lambda f: setattr(cls, f.__name__, f) + if __name__ == '__main__': c = Compiler() schema_bin_filename = pathlib.Path(__file__).parent / '../../../schema/schema.bin' @@ -368,10 +372,11 @@ if __name__ == '__main__': x = Decoder(f.read()).next() print(c.root.schema.Schema.decode(x)) - def m(self, x): + @extend(c.root.schema.Schema) + def f(self, x): return ['yay', self.embeddedType, x] - c.root.schema.Schema.f = m print(c.root.schema.Schema.decode(x).f(123)) + print(f) print()