加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-17 17:21:36 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #encoding:utf8from datetime import datetimefrom sqlalchemy.engine import RowProxydef Print(object): print object for key,value in object.__d

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#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,exclude)
    for key,value in fields.items():
        if handle_ignore(union,dst_obj):
            continue
        value = handle_coding(dir,value)
        value = handle_datetime(key,dt)
        setattr(dst_obj,value)
    if callback_list:
        for callback in callback_list:
            callback(dst_obj)
    if debug:
        Print(dst_obj)
    return dst_obj 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读