-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
72 lines (54 loc) · 1.89 KB
/
example.py
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
63
64
65
66
67
68
69
70
71
72
from abc import ABC, abstractmethod
class ThirdPartyYoutubeLib(ABC):
@abstractmethod
def list_videos(self):
pass
@abstractmethod
def get_video_info(self, id):
pass
@abstractmethod
def download_video(self, id):
pass
class ThirdPartyYoutubeClass(ThirdPartyYoutubeLib):
def list_videos(self):
print("ThirdPartyYoutubeClass: list_videos")
return []
def get_video_info(self, id):
print("ThirdPartyYoutubeClass: get_video_info")
return "video info"
def download_video(self, id):
print("ThirdPartyYoutubeClass: download_video")
return "video"
class CachedYoutubeClass(ThirdPartyYoutubeLib):
def __init__(self, service):
self.service = service
self.list_cache = None
self.video_cache = {}
def list_videos(self):
if not self.list_cache:
self.list_cache = self.service.list_videos()
else:
print("CachedYoutubeClass: list_videos")
return self.list_cache
def get_video_info(self, id):
if not self.video_cache.get(id):
self.video_cache[id] = self.service.get_video_info(id)
else:
print("CachedYoutubeClass: get_video_info")
return self.video_cache[id]
def download_video(self, id):
if not self.video_cache.get(id):
self.video_cache[id] = self.service.download_video(id)
else:
print("CachedYoutubeClass: download_video")
return self.video_cache[id]
class YoutubeManager:
def __init__(self, service):
self.service = service
def render_video_page(self, id):
print("YoutubeManager: render_video_page")
return self.service.get_video_info(id)
if __name__ == "__main__":
service = CachedYoutubeClass(ThirdPartyYoutubeClass())
manager = YoutubeManager(service)
manager.render_video_page("video_id")