与Python和googleappengin的随机匹配

2024-04-30 04:03:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个网站,其中两个音乐视频是随机选择从一个数据库和前往投票头。我需要一个算法,将继续为用户挑选唯一的匹配排除匹配他们在过去有,但与新的匹配替换视频。您可以在此处查看页面示例:http://10.showtownmvp.appspot.com/

我在googleappengine-Python上运行这个程序,有一个投票表和视频表来存储结果。我想保持它尽可能随机,避免多个查询,所以如果您有关于如何在NDB中建模的建议或有一个好的算法,我将感谢您的帮助!你知道吗


Tags: 用户程序com算法数据库http示例视频
1条回答
网友
1楼 · 发布于 2024-04-30 04:03:31

我对这个问题的解决方案是从数据存储中查询所有视频并随机选择一个。我还为用户运行了一个过去投票/匹配的查询,并将其转换为一个列表,这样我就可以在不运行多个查询的情况下操作它。通过使用随机视频,我使用while循环找到了第二个不在上一个匹配列表中的视频。如果没有找到视频,程序将从视频列表中删除随机选择,然后选择一个新的样本并再次运行搜索。代码如下:

    class MainHandler(views.Template):              

        def post(self):
            # NOTE: we are posting genre and state.
            user = self.user_check()
            self.videos = models.videos.Videos.fetch_featured()

            try:
                random.sample(self.videos,2)

                if user:
                    self.user_votes = models.voting.Voting.query_by_user(user.key)

                    if self.user_votes != None:
                        self.user_votes = [[x.video_one,x.video_two] for x in self.user_votes]
                        page_vids = False
                        while page_vids == False and len(self.videos)>1:
                            rand_vid = random.choice(self.videos)
                            page_vids = self.find_match(rand_vid)
                            self.videos.remove(rand_vid)
                    else:
                        page_vids = random.sample(self.videos,2)

                else:
                    page_vids = random.sample(self.videos,2)

            except:
                page_vids = None


        def find_match(self, rand_vid):
            i =0

            while i < len(self.videos):
                if rand_vid.key != self.videos[i].key and ([rand_vid.key,self.videos[i].key] not in self.user_votes and [self.videos[i].key, rand_vid.key] not in self.user_votes):
                    return [rand_vid,self.videos[i]]
                i+=1
            return False




    class Videos(ndb.Model):
        acc_key = ndb.KeyProperty()
        musician_key = ndb.KeyProperty()
        musician_name = ndb.StringProperty()
        embed_link = ndb.StringProperty()
        genre_tag = ndb.StringProperty()
        video_title = ndb.StringProperty()
        featured = ndb.BooleanProperty(default = False)
        likes_count = ndb.IntegerProperty()
        video_added = ndb.DateTimeProperty(auto_now_add = True)

        @classmethod
        def query_by_account(cls, acc_key):
            return cls.query(cls.acc_key == acc_key).fetch()

        @classmethod
        def fetch_featured(cls):
            return cls.query(cls.featured == True).fetch(100)


class Voting(ndb.Model):
        voter_acc_key = ndb.KeyProperty()
        voter_type = ndb.StringProperty()
        video_one = ndb.KeyProperty()
        video_one_artist_key = ndb.KeyProperty()
        video_two = ndb.KeyProperty()
        video_two_artist_key = ndb.KeyProperty()
        voter_choice = ndb.KeyProperty()
        video_set_check = ndb.KeyProperty(repeated = True)
        voter_ip = ndb.StringProperty()
        vote_time = ndb.DateTimeProperty(auto_now_add = True)


        @classmethod
        def query_by_user(cls, acc_key):
            return cls.query(cls.voter_acc_key == acc_key).fetch(2000)

相关问题 更多 >