class - Ensure two Python classes have properties of same name -


i looking towards building class interface in python finds python lacks interface constructs.

what need if programmer tries add new properties in 1 class without adding properties same name in class, exception raised (at compile time or run time)

an example:

class mongocompany:     company_name = mongofield()  class escompany:     company_name = esfield() 

an exception raised if programmer tries add field mongocompany without changing escompany.

class mongocompany:     company_name = mongofield()     company_phone = mongofield()  class escompany:     company_name = esfield()  mongocompany.init() 

edit:

background prevent programmers modifying mongodb's schema declared mongoengine's document class without adding corresponding modification elasticsearch's schema declared in file elasticsearch-dsl's doctype class.

yay! actual application of metaclasses isn't contrived sake of using metaclasses! can write metaclass throw if unexpected properties appear in class definition. need make sure programmers use it.

class requiredfieldsmeta(type):     _interface = {'company_name', 'num_employees'}      def __new__(cls, clsname, bases, attrs):         field in requiredfieldsmeta._interface:             if field not in attrs:                 raise attributeerror(                    'class %s missing required property %s'                     % (clsname, field))         name in attrs:             if not isdunder(name) , name not in requiredfieldsmeta._interface:                 raise attributeerror(                     'class %s has property %s'                     % (clsname, name))         return super(requiredfieldsmeta, cls).__new__(cls, clsname, bases, attrs)  # works fine: class mongocompany(metaclass=requiredfieldsmeta):     company_name = 'mongo inc.'     num_employees = 100  # throws attributeerror: class esycompany(metaclass=requiredfieldsmeta):     extra_prop = 'foobar' 

here's quick demo

note how don't make instantiation: our check gets run when class defined.

edit: in edit, reference function is_dunder. can simple name.startswith('__') or regex or whatever want, long gets rid of properties python, not programmer, put on class.


edit 2: fun, here two, more "elegant" (though less specific) implementations of our check:

def __new__(cls, clsname, bases, attrs):     attr_names = {a in attrs if not is_dunder(a)}      if attr_names.difference(requiredfieldsmeta._interface):         raise attributeerror('class %s has properties' % clsname)     if requiredfieldsmeta._interface.difference(attr_names):         raise attributeerror('class %s missing required properties' % clsname)      return super(requiredfieldsmeta, cls).__new__(cls, clsname, bases, attrs) 

or simply:

def __new__(cls, clsname, bases, attrs):     attr_names = {a in attrs if not is_dunder(a)}     if attr_names != requiredfieldsmeta._interface:         raise attributeerror(             'class %s not match required interface' % clsname)     return super(requiredfieldsmeta, cls).__new__(cls, clsname, bases, attrs) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -