forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.py
More file actions
75 lines (58 loc) · 2.17 KB
/
prototype.py
File metadata and controls
75 lines (58 loc) · 2.17 KB
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
73
74
75
import copy
class Website:
def __init__(self, name, domain, description, author, **kwargs):
'''Examples of optional attributes (kwargs):
category, creation_date, technologies, keywords.
'''
self.name = name
self.domain = domain
self.description = description
self.author = author
for key in kwargs:
setattr(self, key, kwargs[key])
def __str__(self):
summary = [f'Website "{self.name}"\n',]
infos = vars(self).items()
ordered_infos = sorted(infos)
for attr, val in ordered_infos:
if attr == 'name':
continue
summary.append(f'{attr}: {val}\n')
return ''.join(summary)
class Prototype:
def __init__(self):
self.objects = dict()
def register(self, identifier, obj):
self.objects[identifier] = obj
def unregister(self, identifier):
del self.objects[identifier]
def clone(self, identifier, **attrs):
found = self.objects.get(identifier)
if not found:
raise ValueError(f'Incorrect object identifier: {identifier}')
obj = copy.deepcopy(found)
for key in attrs:
setattr(obj, key, attrs[key])
return obj
def main():
keywords = ('python', 'data', 'apis', 'automation')
site1 = Website('ContentGardening',
domain='contentgardening.com',
description='Automation and data-driven apps',
author='Kamon Ayeva',
category='Blog',
keywords=keywords)
prototype = Prototype()
identifier = 'ka-cg-1'
prototype.register(identifier, site1)
site2 = prototype.clone(identifier,
name='ContentGardeningPlayground',
domain='play.contentgardening.com',
description='Experimentation for techniques featured on the blog',
category='Membership site',
creation_date='2018-08-01')
for site in (site1, site2):
print(site)
print(f'ID site1 : {id(site1)} != ID site2 : {id(site2)}')
if __name__ == '__main__':
main()