Skip to content

Instantly share code, notes, and snippets.

@kaka19ace
Created July 10, 2015 00:33
Show Gist options
  • Save kaka19ace/760380b8c267a3fc288c to your computer and use it in GitHub Desktop.
Save kaka19ace/760380b8c267a3fc288c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @file models/base.py
# @author kaka_ace <[email protected]>
# @date
# @brief
#
from sqlalchemy.ext.declarative import (
declarative_base,
DeclarativeMeta,
)
# MetaClass
class ModelMeta(DeclarativeMeta):
def __new__(cls, name, bases, d):
return DeclarativeMeta.__new__(cls, name, bases, d)
def __init__(self, name, bases, d):
DeclarativeMeta.__init__(self, name, bases, d)
#
_Base = declarative_base(metaclass=ModelMeta)
class BaseModel(_Base):
__abstract__ = True
# base model _column_name_sets
_column_name_sets = NotImplemented
def to_dict(self):
"""
"""
return dict(
(column_name, getattr(self, column_name, None)) \
for column_name in self._column_name_sets
)
@classmethod
def get_column_name_sets(cls):
"""
maybey the fields name is different from real column name
"""
return cls._column_name_sets
__str__ = lambda self: str(self.to_dict())
__repr__ = lambda self: repr(self.to_dict())
def modelmeta__new__(cls, name, bases, namespace, **kwds):
column_name_sets = set()
for k, v in namespace.items():
if getattr(v, '__class__', None) is None:
continue
if v.__class__.__name__ == 'Column':
column_name_sets.add(k)
# obj = type.__new__(cls, name, bases, dict(namespace))
obj = DeclarativeMeta.__new__(cls, name, bases, dict(namespace))
# update set
obj._column_name_sets = column_name_sets
return obj
# modify BaseModel' metatype ModelMeta' __new__ definition
setattr(ModelMeta, '__new__', modelmeta__new__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment