当前位置: 首页>>代码示例>>Python>>正文


Python Client.create方法代码示例

本文整理汇总了Python中twilio.rest.Client.create方法的典型用法代码示例。如果您正苦于以下问题:Python Client.create方法的具体用法?Python Client.create怎么用?Python Client.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twilio.rest.Client的用法示例。


在下文中一共展示了Client.create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from twilio.rest import Client [as 别名]
# 或者: from twilio.rest.Client import create [as 别名]
class RedisMatching:
    def __init__(self, config):
        self.db = Redis(host=config['host'], port=config['port'], db=config['db'], decode_responses=True)
        self.msg_client = Client(config['account_sid'], config['auth_token']).messages
        self.msg_sender = config['sender']

    def set_userdata(self, user_data):
        user_data['timestamp'] = int(time())
        return self.db.hmset(user_data['phone_numb'], user_data)

    def get_userdata(self, user_name):
        return self.db.hgetall(user_name)

    def get_all_user(self):
        return self.db.keys()

    def remove_userdata(self, user_name):
        return self.db.delete(user_name)

    def send_message(self, phone_numb, message):
        phone_numb = '+82'+phone_numb[1:]
        result = self.msg_client.create(to=phone_numb,
                                        from_=self.msg_sender,
                                        body=message
                                        )
        return result

    def initialize(self):
        for key in self.get_all_user():
            self.db.delete(key)

    def match_user(self):
        def make_food_vector(user_data):
            food_vector = [ int(user_data['cutlet']),
                            int(user_data['hamburger']),
                            int(user_data['noodle']),
                            int(user_data['korean_food']) ]
            return food_vector
        def make_message_text(target_name, user_data, similarity=0):
            if user_data['gender'] == 'male' : user_data['gender'] = '남'
            elif user_data['gender'] == 'female' : user_data['gender'] = '여'
            text ='%s님, 혼밥러 매칭완료!\n\n상대 : %s님\n연락처 : %s\n성별 : %s\n유사도 : %0.2f' \
                    %(target_name, user_data['user_name']
                        , user_data['phone_numb'], user_data['gender']
                        , similarity*100)
            return text

        while(len(self.get_all_user()) >= 2):
            user_list = self.get_all_user()
            user_data_list = list(map(self.get_userdata, user_list))
            user_data_list = [[user_data['timestamp'], user_data] for user_data in user_data_list]
            user_data_list = [user_data[1] for user_data in sorted(user_data_list)]
            print(user_data_list)

            target_user = user_data_list[0]
            target_vec = make_food_vector(target_user)

            similarity_list = []
            for user_data in user_data_list[1:]:
                user_vec = make_food_vector(user_data)
                similarity = RedisMatching.cosine_similarity(target_vec, user_vec)
                similarity_list.append( (similarity, user_data) )

            max_val = max(similarity_list)
            print(max_val)
            text = make_message_text(target_user['user_name'], max_val[1], max_val[0])
            self.send_message(target_user['phone_numb'], text)
            print(text)
            text = make_message_text(max_val[1]['user_name'], target_user, max_val[0])
            self.send_message(max_val[1]['phone_numb'], text)
            print(text)

            # self.remove_userdata(target_user['phone_numb'])
            # self.remove_userdata(max_val[1]['phone_numb'])

            return True

    @staticmethod
    def cvt_unit_vec(vector):
        scalar =  math.sqrt(sum([i*i for i in vector]))
        return [idx/scalar for idx in vector]

    @staticmethod
    def cosine_similarity(a, b):
        a = RedisMatching.cvt_unit_vec(a)
        b = RedisMatching.cvt_unit_vec(b)
        return sum([i*j for i,j in zip(a, b)])

    @staticmethod
    def custom_similarity(a, b):
        # a = RedisMatching.cvt_unit_vec(a)
        # b = RedisMatching.cvt_unit_vec(b)
        c = [i-j for i,j in zip(a, b)]
        c = [i*i for i in c]
        return 300 - math.sqrt(sum(c))
开发者ID:BbChip0103,项目名称:monday_sql,代码行数:97,代码来源:redis_matching.py


注:本文中的twilio.rest.Client.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。