ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [TIL] 내일 배움 캠프 40일차 10/7 - ItisFuture
    내일배움캠프/TIL 2022. 10. 7. 23:35

    오늘 한 것

    팀원들과 코드리뷰

    numpy복습, pandas기초 

    프로젝트 코드 리팩토링(간단히)

    간단한 알고리즘

     

     

    회고

    프로젝트가 끝나고 머신러닝을 배우는 주가 되었습니다. 약 일주일을 공부하고 프로젝트를 시작하는데 사실 머신러닝을 일주일 공부하고 프로젝트를 하는 것이 말이 안된다고 생각하지만, 머신러닝이 위주가 아닌 장고를 위주로 하는 것으로 예상이 되어 어느정도 납득을 했다. 그래도 머신러닝을 겉핥기식이겠지만 나중에 나의 프로젝트에서 추천 시스템을 활용할 때 좋은 기초가 될 것으로 생각하여 열심히 하는 중이다. 또한 오늘은 팀원들과 각자의 주석을 달고 코드리뷰를 하였다. 본인이 쓴 것을 설명하는 시간이라고 봐도 무방하다. 서로가 어떤 코드가 괜찮은지 토의하는 날이 오면 좋겠다. 

    코드 리팩토링을 하고 싶은 함수가 두개 정도 있는데 오늘 하나 해봤다. 바꾸고나니 아름다워보이고 기분이 좋아졌다.

     

    Before

    def get_storys_author(request):
        user = request.user
        # 현재 로그인한 사람이 팔로잉한 사람들
        followings = [f.follow for f in FollowModel.objects.filter(user=user)]
        story_authors = []
        viewed_story_authors = []
        for follow in followings:
            instance_list = []
            # 팔로잉한 사람의 스토리를 한명씩 가져옴
            user_storys = Story.objects.filter(author=follow, is_end=False)
            for story in user_storys:
                # 그 스토리의 시간이 24시간이 지났다면 유효성이 끝났음을 처리한다.
                if (timezone.now() - timedelta(days=1)) > story.created_at:
                    story.is_end = True
                    story.save()
                    continue
                # 유효하다면 인스턴스 리스트에 넣는다.
                instance_list.append(story)
            if len(instance_list) != 0:
                # 로그인한 사용자가 본 스토리인지 아닌지 판별하여 넣어준다.
                for story in instance_list:
                    try:
                        StoryViewed.objects.get(story=story, user=user)
                    except StoryViewed.DoesNotExist:
                        # 본 스토리가 아니라면 보지않은 스토리 작성자에 넣어준다.
                        story_authors.append(story.author)
                        break
                else:
                    # 예외를 발생시키지 않고 모두 돌았다면 봤던 사용자로 넣어준다.
                    viewed_story_authors.append(story.author)
        return story_authors, viewed_story_authors

     

    Refactor

    def get_storys_author(request):
        user = request.user
        # 현재 로그인한 사람이 팔로잉한 사람들
        followings = [f.follow for f in FollowModel.objects.filter(user=user).exclude(follow = user)]
        new_story_authors = []
        viewed_story_authors = []
        for follow in followings:
            # 유효한 스토리(24시간이 지나지 않은 스토리)가 하나라도 있는지 판별할 변수이다.
            is_valid = False
            # 팔로잉한 사람의 스토리를 한명씩 가져옴
            user_storys = Story.objects.filter(author=follow, is_end=False)
            for story in user_storys:
                # 그 스토리의 시간이 24시간이 지났다면 유효성이 끝났음을 처리한다.
                if (timezone.now() - timedelta(days=1)) > story.created_at:
                    story.is_end = True
                    story.save()
                    continue
                # 유효하다면 해당 스토리 작성자는 유효함을 남긴다.
                is_valid = True
                try:
                    StoryViewed.objects.get(story=story, user=user)
                except StoryViewed.DoesNotExist:
                    # 본 스토리가 아니라면 보지않은 스토리 작성자에 넣어준다.
                    new_story_authors.append(story.author)
                    break
            # 유효기간이 지난 스토리 혹은 다 봤던 스토리
            else:
                # 유효기간이 지나지 않은 스토리가 하나라도 있나
                if is_valid:
                    viewed_story_authors.append(story.author)
        return new_story_authors, viewed_story_authors

    Only Code

    ###################### Before
    def get_storys_author(request):
    
        user = request.user
        followings = [f.follow for f in FollowModel.objects.filter(user=user)]
        story_authors = []
        viewed_story_authors = []
        
        for follow in followings:
        
            instance_list = []
            user_storys = Story.objects.filter(author=follow, is_end=False)
            
            for story in user_storys:
            
                if (timezone.now() - timedelta(days=1)) > story.created_at:
                    story.is_end = True
                    story.save()
                    continue
                    
                instance_list.append(story)
                
            if len(instance_list) != 0:
            
                for story in instance_list:
                
                    try:
                        StoryViewed.objects.get(story=story, user=user)
                        
                    except StoryViewed.DoesNotExist:
                        story_authors.append(story.author)
                        break
                        
                else:
                    viewed_story_authors.append(story.author)
                    
        return story_authors, viewed_story_authors
        
        
    ################## Refator ######################
    def get_storys_author(request):
    
    user = request.user
    followings = [f.follow for f in FollowModel.objects.filter(user=user).exclude(follow = user)]
    new_story_authors = []
    viewed_story_authors = []
    
    for follow in followings:
        is_valid = False
        user_storys = Story.objects.filter(author=follow, is_end=False)
    
        for story in user_storys:
            if (timezone.now() - timedelta(days=1)) > story.created_at:
                story.is_end = True
                story.save()
                continue
    
            is_valid = True
    
            try:
                StoryViewed.objects.get(story=story, user=user)
            except StoryViewed.DoesNotExist:
                new_story_authors.append(story.author)
                break
        else:
            if is_valid:
                viewed_story_authors.append(story.author)
    
    return new_story_authors, viewed_story_authors

     내일 할 것

    머신러닝 pandas기초 강의 공부

    numpy, pandas 기초 포스팅

    개인 프로젝트

    간단한 알고리즘

    댓글

Designed by Tistory.