Created
May 6, 2013 16:42
-
-
Save harvimt/5526304 to your computer and use it in GitHub Desktop.
Metaprogramming to get attributes for repr and update.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db_loc = 'sqlite:///%s\\data.sqlite' % store_loc | |
logging.info('db_loc=%s', db_loc) | |
engine = sqlalchemy.create_engine(db_loc, echo=False) | |
sess = scoped_session(sessionmaker(bind=engine)) | |
class Base(object): | |
'''define basic object methods for debugging & initializing.''' | |
def __init__(self, *args, **kwargs): | |
self.update(*args, **kwargs) | |
def update(self, *args, **kwargs): | |
prop_names = (k for k in sorted(vars(self)) if not k.startswith('_')) | |
kv_prop = itertools.izip(prop_names, args) | |
for k, v in itertools.chain(kv_prop, kwargs.iteritems()): | |
setattr(self, k, v) | |
def __repr__(self): | |
attrs = ( | |
'%s=%s' % (k, getattr(self, k)) | |
for k in vars(self) if not k.startswith('_sa_')) | |
return "%s(%s)" % (self.__class__.__name__, ', '.join(attrs)) | |
Base = declarative_base(cls=Base) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment