1
+ import copy
2
+
3
+ class Website :
4
+ def __init__ (self , name , domain , description , author , ** kwargs ):
5
+ '''Examples of optional attributes (kwargs):
6
+ category, creation_date, technologies, keywords.
7
+ '''
8
+ self .name = name
9
+ self .domain = domain
10
+ self .description = description
11
+ self .author = author
12
+
13
+ for key in kwargs :
14
+ setattr (self , key , kwargs [key ])
15
+
16
+ def __str__ (self ):
17
+ summary = [f'Website "{ self .name } "\n ' ,]
18
+
19
+ infos = vars (self ).items ()
20
+ ordered_infos = sorted (infos )
21
+ for attr , val in ordered_infos :
22
+ if attr == 'name' :
23
+ continue
24
+ summary .append (f'{ attr } : { val } \n ' )
25
+
26
+ return '' .join (summary )
27
+
28
+
29
+ class Prototype :
30
+ def __init__ (self ):
31
+ self .objects = dict ()
32
+
33
+ def register (self , identifier , obj ):
34
+ self .objects [identifier ] = obj
35
+
36
+ def unregister (self , identifier ):
37
+ del self .objects [identifier ]
38
+
39
+ def clone (self , identifier , ** attrs ):
40
+ found = self .objects .get (identifier )
41
+ if not found :
42
+ raise ValueError (f'Incorrect object identifier: { identifier } ' )
43
+ obj = copy .deepcopy (found )
44
+ for key in attrs :
45
+ setattr (obj , key , attrs [key ])
46
+
47
+ return obj
48
+
49
+ def main ():
50
+ keywords = ('python' , 'data' , 'apis' , 'automation' )
51
+ site1 = Website ('ContentGardening' ,
52
+ domain = 'contentgardening.com' ,
53
+ description = 'Automation and data-driven apps' ,
54
+ author = 'Kamon Ayeva' ,
55
+ category = 'Blog' ,
56
+ keywords = keywords )
57
+
58
+ prototype = Prototype ()
59
+ identifier = 'ka-cg-1'
60
+ prototype .register (identifier , site1 )
61
+
62
+ site2 = prototype .clone (identifier ,
63
+ name = 'ContentGardeningPlayground' ,
64
+ domain = 'play.contentgardening.com' ,
65
+ description = 'Experimentation for techniques featured on the blog' ,
66
+ category = 'Membership site' ,
67
+ creation_date = '2018-08-01' )
68
+
69
+ for site in (site1 , site2 ):
70
+ print (site )
71
+ print (f'ID site1 : { id (site1 )} != ID site2 : { id (site2 )} ' )
72
+
73
+ if __name__ == '__main__' :
74
+ main ()
75
+
0 commit comments