File size: 2,531 Bytes
7288748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from typing import Dict, List

from video import YoutubeVideo
from errors import DifferentNumberOfArgumentsError

def accepts_types(*expected_types):
    """Decorator that checks that the arguments of a method are valid.
    :raise TypeError: If type of argument isn't valid
    :raise DifferentNumberOfArgumentsError: If number of arguments passed to the
        decorator and to the method (minus self) aren't the same
    """
    def check_types(func):
        def wrapper(*args, **kwargs):
            args_without_self = args[1:]
            _raise_error_if_number_of_passed_and_expected_arguments_dont_match(args_without_self, expected_types)
            _raise_type_error_if_passed_and_expected_types_dont_match(args_without_self, expected_types)
            return func(*args, **kwargs)
        return wrapper
    return check_types

def _raise_error_if_number_of_passed_and_expected_arguments_dont_match(passed_args, expected_types):
    if len(passed_args) != len(expected_types):
        msg = "Number of arguments passed in decorator " \
              f"{len(expected_types)} doesn't match with number of " \
              f"arguments in method, i.e., {len(passed_args)}"
        raise DifferentNumberOfArgumentsError(msg)
    
def _raise_type_error_if_passed_and_expected_types_dont_match(passed_args, expected_types):
    for (arg, expected_type) in zip(passed_args, expected_types):
        if not isinstance(arg, expected_type):
            raise TypeError(f"Argument '{arg}' is of type {type(arg)}. "
                            f"'{expected_type}' expected instead")

def create_videos(video_parameters: List[Dict]) -> List[YoutubeVideo]:
    """Factory function that creates a list of YoutubeVideos from a list of
    dictionaries representing video parameters
    """
    youtube_videos = []
    for params in video_parameters:
        youtube_video = YoutubeVideo(channel_name=params["channel_name"],
                                     url=params["url"])
        youtube_videos.append(youtube_video)
    return youtube_videos

def nest_list(list: list, nested_list_length: int) -> List[List]:
    new_list = []
    nested_list = []
    for item in list:
        nested_list.append(item)
        if len(nested_list) == nested_list_length:
            new_list.append(nested_list)
            nested_list = []
    if len(nested_list) != 0:
        new_list.append(nested_list)
    return new_list

def is_google_colab():
    try:
        import google.colab
        return True
    except:
        return False