返回顶部

收藏

thrift ttypes 与 sqlalchemy metadata 进行 完全/部分 成员拷贝

更多
#encoding:utf8

from datetime import datetime
from sqlalchemy.engine import RowProxy

def Print(object):
    print object
    for key, value in object.__dict__.items():
        print '%-20r:%-r'%(key, value) 

def Replicate(dir=True, debug=False, \
              tcls=None, tobj=None, mcls=None, mobj=None, \
              tencoding='utf-8', mencoding='unicode', \
              include=None, exclude=None, dt=None, union=True,\
              callback_list=None):

    src_obj = tobj if dir else mobj
    dst_obj = mobj if dir else tobj
    src_cls = tcls if dir else mcls
    dst_cls = mcls if dir else tcls

    def get_source_fields(debug, src_obj, include, exclude):
        if isinstance(src_obj, RowProxy):
            src_obj_items = src_obj.items()
        else:
            src_obj_items = src_obj.__dict__.items()
        inc_keys = set(include) if include else \
                   set([item[0] for item in src_obj_items])
        exc_keys = set(exclude) if exclude else set()
        com_keys = inc_keys & exc_keys
        dif_keys = inc_keys - com_keys
        fields = dict()
        for key in dif_keys:
            value = getattr(src_obj, key)
            if value is None:
                continue 
            fields[key] = value 
        return fields 

    def handle_ignore(union, key, dst_obj):
        rv = False
        try:
            getattr(dst_obj, key)
        except Exception, e:
            if not union:
                rv = True
        return rv

    def handle_coding(dir, value):
        if dir and \
           isinstance(value, basestring) and \
           not isinstance(value, unicode):
            value = value.decode(tencoding)
        if not dir and \
           isinstance(value, unicode):
            value = value.encode(tencoding)
        return value

    def handle_datetime(key, value, dt):
        str_to_dt = lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S")
        dt_to_str = lambda x: x.strftime('%Y-%m-%d %H:%M:%S')
        if key in dt:
            value = str_to_dt(value) if dir else dt_to_str(value) 
        return value

    if src_obj is None:
        raise Exception('source obj should not be None!')
    dst_obj = dst_obj if dst_obj else dst_cls()

    fields = get_source_fields(debug, src_obj, include, exclude)
    for key, value in fields.items():
        if handle_ignore(union, key, dst_obj):
            continue
        value = handle_coding(dir, value)
        value = handle_datetime(key, value, dt)
        setattr(dst_obj, key, value)
    if callback_list:
        for callback in callback_list:
            callback(dst_obj)
    if debug:
        Print(dst_obj)
    return dst_obj 

标签:python

收藏

0人收藏

支持

0

反对

0

发表评论