Advertisement
Guest User

Untitled

a guest
Dec 27th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from inspect import isfunction
  5.  
  6.  
  7. class SchemaBase:
  8.  
  9.     def __init__(self, *args, **kwargs):
  10.         print('calling base init')
  11.         self._extras = {}
  12.         for name, value in kwargs.items():
  13.             if name in self.__fields__:
  14.                 setattr(self, name, value if value else None)
  15.             else:
  16.                 self._extras[name] = value
  17.  
  18.         # I can't figure out a better way to do this, sigh!
  19.         for name, value in self.__fields__.items():
  20.             if not hasattr(self, name):
  21.                 setattr(self, name, value)
  22.  
  23.  
  24. class SchemaMeta(type):
  25.  
  26.     def __new__(self, name, bases, attrs):
  27.         klass_attrs = {}
  28.         fields = {}
  29.         for name, attr in attrs.items():
  30.             if not isfunction(attr) and not name.startswith('_'):
  31.                 # we ignore class setup for now
  32.                 # TODO: replace with fields
  33.                 fields[name] = None
  34.             else:
  35.                 klass_attrs[name] = attr
  36.  
  37.         if klass_attrs.get('__init__'):
  38.             klass_attrs['__init__'] = \
  39.                 auto_super_caller(klass_attrs.get('__init__'))
  40.         klass = super().__new__(self, name, bases, klass_attrs)
  41.         klass.__fields__ = fields
  42.         return klass
  43.  
  44.  
  45. def auto_super_caller(class_init):
  46.     orig_init = class_init
  47.     def init_wrapper(self, *args, **kwargs):
  48.         super(self.__class__, self).__init__(*args, **kwargs)
  49.         if orig_init:
  50.             orig_init(self, *args, **kwargs)
  51.     return init_wrapper
  52.  
  53.  
  54. def schema(cls):
  55.     attrs = cls.__dict__.copy()
  56.     attrs.pop('__dict__', None)
  57.     attrs.pop('__weakref__', None)
  58.     if hasattr(cls, '__qualname__'):
  59.         attrs['__qualname__'] = cls.__qualname__
  60.  
  61.     bases = tuple([SchemaBase] + list(cls.__bases__))
  62.     return SchemaMeta(cls.__name__, bases, attrs)
  63.  
  64.  
  65. class A:
  66.  
  67.     def __init__(self, *args, **kwargs):
  68.         print('calling A init')
  69.  
  70.     def foo(self):
  71.         pass
  72.  
  73. class B:
  74.  
  75.     def __init__(self, *args, **kwargs):
  76.         print('calling B init')
  77.  
  78.     def baz(self):
  79.         pass
  80.  
  81.  
  82. @schema
  83. class T(A, B):
  84.     a = 1
  85.     b = 2
  86.     c = 3
  87.  
  88.     def __init__(self, *args, **kwargs):
  89.         print('calling T init')
  90.         #super().__init__(*args, **kwargs)
  91.  
  92.     def bar(self):
  93.         pass
  94.  
  95. a = T(a=10, b=11, x=1)
  96. b = T(a=20, c=22, y=2)
  97. c = T(b=31, c=32)
  98.  
  99. print(dir(T))
  100. print(dir(a))
  101. print(dir(b))
  102. print(dir(c))
  103.  
  104. print(id(a), id(b), id(c))
  105.  
  106. print(T, a.a, b.a, c.a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement