forked from dsc-kabu-hack/python4TensorFlow-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quizes_projects_4_OOP.py
1098 lines (887 loc) · 32.5 KB
/
quizes_projects_4_OOP.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# OBJECT ORIENTED PROGRAMMING WITH PYTHON QUIZES AND PROJECTS:
# Declaring a class: (empty class uses pass to show it has no members or attributes)
class Fruits:
pass
#try out this
class Apple:
color = ""
size = ""
taste = ""
# assign a varibale the function to make it callable
callAple = Apple()
# now assign values to them using the .DOT notation
callAple.color = "Green"
callAple.size = "Small"
callAple.taste = "very sweet"
# print the elements
print(callAple.color, callAple.size,callAple.taste, end="")
# 1
# complete the code below to print some poems
class ___:
color = 'unknown'
rose = Flower()
rose.color = ___
violet = ___
___
this_pun_is_for_you = ___
print("Roses are {},".format(rose.color))
print("violets are {},".format(violet.color))
print(this_pun_is_for_you)
#soln
class Flower:
color = 'unknown'
rose = Flower()
rose.color = "red"
violet = Flower()
violet.color="blue"
this_pun_is_for_you = "this pun is for you"
print("Roses are {},".format(rose.color))
print("violets are {},".format(violet.color))
print(this_pun_is_for_you)
# 2
#Creating new instances of class objects can be a great way to keep track of values using attributes associated with
# the object. The values of these attributes can be easily changed at the object level. The following code illustrates
# a famous quote by George Bernard Shaw,using objects to represent people. Fill in the blanks to make the code satisfy
# the behavior described in the quote.
# “If you have an apple and I have an apple and we exchange these apples then
# you and I will still each have one apple. But if you have an idea and I have
# an idea and we exchange these ideas, then each of us will have two ideas.”
# George Bernard Shaw
class Person:
apples = 0
ideas = 0
johanna = Person()
johanna.apples = 1
johanna.ideas = 1
martin = Person()
martin.apples = 2
martin.ideas = 1
def exchange_apples(you, me):
#Here, despite G.B. Shaw's quote, our characters have started with #different amounts of apples so we can better observe the results.
#We're going to have Martin and Johanna exchange ALL their apples with #one another.
#Hint: how would you switch values of variables,
#so that "you" and "me" will exchange ALL their apples with one another?
#Do you need a temporary variable to store one of the values?
#You may need more than one line of code to do that, which is OK.
___
return you.apples, me.apples
def exchange_ideas(you, me):
#"you" and "me" will share our ideas with one another.
#What operations need to be performed, so that each object receives
#the shared number of ideas?
#Hint: how would you assign the total number of ideas to
#each idea attribute? Do you need a temporary variable to store
#the sum of ideas, or can you find another way?
#Use as many lines of code as you need here.
you.ideas ___
me.ideas ___
return you.ideas, me.ideas
exchange_apples(johanna, martin)
print("Johanna has {} apples and Martin has {} apples".format(johanna.apples, martin.apples))
exchange_ideas(johanna, martin)
print("Johanna has {} ideas and Martin has {} ideas".format(johanna.ideas, martin.ideas))
#soln
# “If you have an apple and I have an apple and we exchange these apples then
# you and I will still each have one apple. But if you have an idea and I have
# an idea and we exchange these ideas, then each of us will have two ideas.”
# George Bernard Shaw
class Person:
apples = 0
ideas = 0
johanna = Person()
johanna.apples = 1
johanna.ideas = 1
martin = Person()
martin.apples = 2
martin.ideas = 1
def exchange_apples(you, me):
#Here, despite G.B. Shaw's quote, our characters have started with #different amounts of apples so we can better observe the results.
#We're going to have Martin and Johanna exchange ALL their apples with #one another.
#Hint: how would you switch values of variables,
#so that "you" and "me" will exchange ALL their apples with one another?
#Do you need a temporary variable to store one of the values?
#You may need more than one line of code to do that, which is OK.
x = 0
x=you.apples
you.apples=me.apples
me.apples=x
return you.apples, me.apples
def exchange_ideas(you, me):
#"you" and "me" will share our ideas with one another.
#What operations need to be performed, so that each object receives
#the shared number of ideas?
#Hint: how would you assign the total number of ideas to
#each idea attribute? Do you need a temporary variable to store
#the sum of ideas, or can you find another way?
#Use as many lines of code as you need here.
x = 0
x = you.ideas
you.ideas += me.ideas
me.ideas+=x
return you.ideas, me.ideas
exchange_apples(johanna, martin)
print("Johanna has {} apples and Martin has {} apples".format(johanna.apples, martin.apples))
exchange_ideas(johanna, martin)
print("Johanna has {} ideas and Martin has {} ideas".format(johanna.ideas, martin.ideas))
# 3
# The City class has the following attributes: name, country (where the city is located), elevation (measured in meters),
# and population (approximate, according to recent statistics). Fill in the blanks of the max_elevation_city function to
# return the name of the city and its country (separated by a comma), when comparing the 3 defined instances for a
# specified minimal population. For example,
# calling the function for a minimum population of 1 million: max_elevation_city(1000000) should return "Sofia, Bulgaria".
# define a basic city class
class City:
name = ""
country = ""
elevation = 0
population = 0
# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052
# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675
# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509
def max_elevation_city(min_population):
# Initialize the variable that will hold
# the information of the city with
# the highest elevation
return_city = City()
# Evaluate the 1st instance to meet the requirements:
# does city #1 have at least min_population and
# is its elevation the highest evaluated so far?
if ___
return_city = ___
# Evaluate the 2nd instance to meet the requirements:
# does city #2 have at least min_population and
# is its elevation the highest evaluated so far?
if ___
return_city = ___
# Evaluate the 3rd instance to meet the requirements:
# does city #3 have at least min_population and
# is its elevation the highest evaluated so far?
if ___
return_city = ___
#Format the return string
if return_city.name:
return ___
else:
return ""
print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print ""
#soln
# define a basic city class
class City:
name = ""
country = ""
elevation = 0
population = 0
# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052
# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675
# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509
def max_elevation_city(min_population):
# Initialize the variable that will hold
# the information of the city with
# the highest elevation
return_city = City()
# Evaluate the 1st instance to meet the requirements:
# does city #1 have at least min_population and
# is its elevation the highest evaluated so far?
if city1.population >= min_population and city1.elevation > return_city.elevation:
return_city = city1
# Evaluate the 2nd instance to meet the requirements:
# does city #2 have at least min_population and
# is its elevation the highest evaluated so far?
if city2.population >= min_population and city2.elevation > return_city.elevation:
return_city = city2
# Evaluate the 3rd instance to meet the requirements:
# does city #3 have at least min_population and
# is its elevation the highest evaluated so far?
if city3.population >= min_population and city3.elevation > return_city.elevation:
return_city = city3
# Format the return string
if return_city.name:
return ("{}, {}".format(return_city.name, return_city.country))
else:
return ""
print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print ""
# 4
# We have two pieces of furniture: a brown wood table and a red leather couch. Fill in the blanks following
# the creation of each Furniture class instance, so that the describe_furniture function can format a
# sentence that describes these pieces as follows: "This piece of furniture is made of {color} {material}"
class Furniture:
color = ""
material = ""
table = Furniture()
___
___
couch = Furniture()
___
___
def describe_furniture(piece):
return ("This piece of furniture is made of {} {}".format(piece.color, piece.material))
print(describe_furniture(table))
# Should be "This piece of furniture is made of brown wood"
print(describe_furniture(couch))
# Should be "This piece of furniture is made of red leather"
#soln
class Furniture:
color = ""
material = ""
table = Furniture()
table.color="brown"
table.material="wood"
couch = Furniture()
couch.color="red"
couch.material="leather"
def describe_furniture(piece):
return ("This piece of furniture is made of {} {}".format(piece.color, piece.material))
print(describe_furniture(table))
# Should be "This piece of furniture is made of brown wood"
print(describe_furniture(couch))
# Should be "This piece of furniture is made of red leather"
#methods in classes Exmaple
class Animals:
def animalSounds(self):
print("Wouh wouh!!")
dog = Animals()
print(dog.animalSounds())
#EX 2
class Dog:
def barking(self):
print("Wouuh, wouuh")
lion = Dog()
print(lion.barking())
# Ex 3
class Bmw:
def bmwType(self):
brand="bmw3201"
print("This brand is {}, and is the best in the market".format(brand))
brandCar = Bmw()
print(brandCar.bmwType())
#EX 4
#uses self and declared above the function to allow the member varibales to be changeable
class Bmw:
brand = "bmw3201"
def bmwType(self):
print("This brand is {}, and is the best in the market".format(self.brand))
brandCar = Bmw()
print(brandCar.bmwType())
brandCar.brand = "BMWX3"
print(brandCar.bmwType())
#createing more than one instance from the same class
class Bmw:
brand = "bmw3201"
def bmwType(self):
print("This brand is {}, and is the best in the market".format(self.brand))
brandCar = Bmw()
print(brandCar.bmwType())
brandCar.brand = "BMWX3"
print(brandCar.bmwType())
#Instance 2
newbrand = Bmw()
newbrand.brand = "BMWX6"
print(newbrand.bmwType())
#Return values from classes and methods
class Monkey:
farms_attacked = 0
def moneky_mtoto(self):
return self.farms_attacked * 12
monkeys = Monkey()
print(monkeys.moneky_mtoto())
new_mobkey = monkeys.farms_attacked = 10
print(monkeys.moneky_mtoto())
# Q5
#OK, now it’s your turn! Have a go at writing methods
# for a class. Create a Dog class with dog_years based
# on the Piglet class shown before (one human year is about 7 dog years).
class Dog:
years = 0
__
fido=Dog()
fido.years=3
print(fido.dog_years())
# soln
class Dog:
years = 0
def dog_years(self):
return self.years*7
fido=Dog()
fido.years=3
print(fido.dog_years())
# CONSTRUCTOR AND IMPORTANCT METHODS:
#Constructors allows us to pass in parameters directly to the instances created
#example
class Coffee:
def __init__(self, brand, flavour):
self.brand = brand
self.flavour = flavour
drinkCoffee = Coffee("mac Coffee", "black")
print(drinkCoffee.brand)
print(drinkCoffee.flavour)
#Q 6:
# Want to see this in action? In this code, there's a Person class that has an attribute name,
# which gets set when constructing the object. Fill in the blanks so that 1) when an instance of the class is
# created, the attribute gets set correctly, and 2) when the greeting() method is called, the greeting states the assigned name.
class Person:
def __init__(self, name):
self.name = ___
def greeting(self):
# Should return "hi, my name is " followed by the name of the Person.
return ___
# Create a new instance with a name of your choice
some_person = ___
# Call the greeting method
print(some_person.___)
#soln
class Person:
def __init__(self, name):
self.name = name
def greeting(self):
# Should return "hi, my name is " followed by the name of the Person.
user_name = ("hi, my name is {}".format(self.name))
return user_name
# Create a new instance with a name of your choice
some_person = Person("owen")
# Call the greeting method
print(some_person.greeting())
#soln 2
class Person:
def __init__(self, name):
self.name = name
def greeting(self):
# Should return "hi, my name is " followed by the name of the Person.
return "hi, my name is {}".format(self.name)
# Create a new instance with a name of your choice
some_person = Person("Owen")
# Call the greeting method
print(some_person.greeting())
#using __str__ to print direct out put and not storage address by computer on ommiting str conversion
class Computer:
def __init__(self,model,storage):
self.model =model
self.storage=storage
def __str__(self):
return "Model {} had a storage of {}".format(self.model,self.storage)
pc = Computer("HP",str(10))
print(pc) # Model HP had a storga of 10
#using DocString for documenting your work. (Typing help to the funciton or methods show what it does)
>>> def to_seconds(hours, minutes, seconds):
... """Returns the amount of seconds in the given hours, minutes and seconds."""
... return hours*3600+minutes*60+seconds
... >>> help(to_seconds)
Help on function to_seconds in module __main__:
to_seconds(hours, minutes, seconds)
Returns the amount of seconds in the given hours, minutes and seconds.
#more example
class ClassName:
"""Documentation for the class."""
def method_name(self, other_parameters):
"""Documentation for the method."""
body_of_method
def function_name(parameters):
"""Documentation for the function."""
body_of_function
# Q 7:
#write code to define an Elevator class. The elevator has a current floor, it also has a top and a bottom floor
# that are the minimum and maximum floors it can go to. Fill in the blanks to make the elevator go through the floors requested.
class Elevator:
def __init__(self, bottom, top, current):
"""Initializes the Elevator instance."""
pass
def up(self):
"""Makes the elevator go up one floor."""
pass
def down(self):
"""Makes the elevator go down one floor."""
pass
def go_to(self, floor):
"""Makes the elevator go to the specific floor."""
pass
elevator = Elevator(-1, 10, 0)
#soln
class Elevator:
def __init__(self, bottom, top, current):
"""Initializes the Elevator instance."""
self.bottom = bottom
self.top=top
self.current=current
def up(self,current):
"""Makes the elevator go up one floor."""
if self.current < self.top:
return self.current+1
def down(self, bottom):
"""Makes the elevator go down one floor."""
if self.current > self.top:
return self.current -1
def go_to(self, floor):
"""Makes the elevator go to the specific floor."""
self.floor=floor
return self.floor
elevator = Elevator(-1, 10, 0)
#soln
class Elevator:
def __init__(self, bottom, top, current):
"""Initializes the Elevator instance."""
self.bottom = bottom
self.top = top
self.current=current
def up(self):
"""Makes the elevator go up one floor."""
if self.current < self.top:
return self.current + 1
def down(self):
"""Makes the elevator go down one floor."""
if self.current >self.bottom:
return self.current - 1
def go_to(self, floor):
"""Makes the elevator go to the specific floor."""
if self.floor <= self.top and self.floor>=self.bottom:
return self.current = floor
elevator = Elevator(-1, 10, 0)
#soln
class Elevator:
def __init__(self, bottom, top, current):
"""Initializes the Elevator instance."""
self.bottom = 0
self.top = 0
self.current = 0
def up(self):
"""Makes the elevator go up one floor."""
if self.current != self.top:
return self.current + 1
def down(self):
"""Makes the elevator go down one floor."""
if self.current != self.bottom:
return self.current - 1
def go_to(self, floor):
"""Makes the elevator go to the specific floor."""
self.current = floor
return self.current
elevator = Elevator(-1, 10, 0)
#soln
class Elevator:
def __init__(self, bottom, top, current):
"""Initializes the Elevator instance."""
self.bottom = 0
self.top = 10
self.current = 0
def up(self):
"""Makes the elevator go up one floor."""
if self.current == 10:
self.current += 0
else:
self.current += 1
def down(self):
"""Makes the elevator go down one floor."""
if self.current <= -1:
self.current -= 0
"""Makes the elevator go down one floor."""
else:
self.current -= 1
def go_to(self, floor):
"""Makes the elevator go to the specific floor."""
self.current = floor
def __str__(self):
return "Current floor: {}".format(self.current)
elevator = Elevator(-1, 10, 0)
elevator.up()
elevator.current #should output 1
elevator.down()
elevator.current #should output 0
elevator.go_to(10)
elevator.current #should output 10
#INHERITANCE
#This allows us to reuse our code base:
#Inherits behavior from the root parent for all the siblings
class Cars:
def __init__(self,max_speed, model):
self.speed = max_speed
self.model = model
#create an instance of Zubaru class
class Zubaru(Cars):
pass
class Bmw(Cars):
pass
zubaru_squad = Zubaru("200km/h", "Zubaru impreza")
bima_squad = Bmw("400km/hr", "BMW X6")
print(zubaru_squad.speed)
print(bima_squad.model)
#Example 2
class Animal:
sound=""
def __init__(self,name):
self.name = name
def speak(self):
print("{sound} I'm {name}! {sound}".format(name = self.name, sound=self.sound))
class Piglet(Animal):
sound = "Oink"
hamlet=Piglet("Hamlet")
print(hamlet.speak())
class Dog(Animal):
sound = "Wouuuuuuh"
barking = Dog("Simba")
print(barking.speak())
# Q 8
#Let’s create a new class together and inherit from it. Below we have a base class called Clothing. Together,
#let’s create a second class, called Shirt, that inherits methods from the Clothing class. Fill in the blanks to make it work properly.
class Clothing:
material = ""
def __init__(self,name):
self.name = name
def checkmaterial(self):
print("This {} is made of {}".format(self.___,self.___))
class Shirt(___):
material="Cotton"
polo = Shirt("Polo")
polo.checkmaterial()
#soln
class Clothing:
material = ""
def __init__(self, name):
self.name = name
def checkmaterial(self):
print("This {} is made of {}".format(self.name, self.material))
class Shirt(Clothing):
material = "Cotton"
polo = Shirt("Polo")
print(polo.checkmaterial())
#summary
#In object-oriented programming, the concept of inheritance allows you to build relationships between objects,
# grouping together similar concepts and reducing code duplication. Let's create a custom Fruit class with color and flavor attributes:
>>> class Fruit:
... def __init__(self, color, flavor):
... self.color = color
... self.flavor = flavor
#We defined a Fruit class with a constructor for color and flavor attributes. Next, we'll define an Apple class along with a new Grape
# class, both of which we want to inherit properties and behaviors from the Fruit class:
>>> class Apple(Fruit):
... pass
...
>>> class Grape(Fruit):
... pass
# In Python, we use parentheses in the class declaration to have the class inherit from the Fruit class. So in this example,
# we’re instructing our computer that both the Apple class and Grape class inherit from the Fruit class. This means that they
# both have the same constructor method which sets the color and flavor attributes. We can now create instances of our Apple
# and Grape classes:
>>> granny_smith = Apple("green", "tart")
>>> carnelian = Grape("purple", "sweet")
>>> print(granny_smith.flavor)
tart
>>> print(carnelian.color)
purple
# COMPOSITION:
#Allows you to use an instances of a class where classes are not related to each other
class Repository:
def __init__(self):
self.packages = {}
def add_packages(self, package):
self.packages[package.name]=package
def total_size(self):
result = 0
for package in self.packages.value():
result += package.size
return result
# 9
# Let’s expand a bit on our Clothing classes from the previous in-video question. Your mission: Finish the
# "Stock_by_Material" method and iterate over the amount of each item of a given material that is in stock.
# When you’re finished, the script should add up to 10 cotton Polo shirts.
class Clothing:
stock={ 'name': [],'material' :[], 'amount':[]}
def __init__(self,name):
material = ""
self.name = name
def add_item(self, name, material, amount):
Clothing.stock['name'].append(self.name)
Clothing.stock['material'].append(self.material)
Clothing.stock['amount'].append(amount)
def Stock_by_Material(self, material):
count=0
n=0
for item in Clothing.stock['___']:
if item == material:
count += Clothing.___['amount'][n]
n+=1
return count
class shirt(Clothing):
material="Cotton"
class pants(Clothing):
material="Cotton"
polo = shirt("Polo")
sweatpants = pants("Sweatpants")
polo.add_item(polo.name, polo.material, 4)
sweatpants.add_item(sweatpants.name, sweatpants.material, 6)
current_stock = polo.Stock_by_Material("Cotton")
print(current_stock)
#soln
class Clothing:
stock={ 'name': [],'material' :[], 'amount':[]}
def __init__(self,name):
material = ""
self.name = name
def add_item(self, name, material, amount):
Clothing.stock['name'].append(self.name)
Clothing.stock['material'].append(self.material)
Clothing.stock['amount'].append(amount)
def Stock_by_Material(self, material):
count=0
n=0
for item in Clothing.stock['material']:
if item == material:
count += Clothing.stock['amount'][n]
n+=1
return count
class shirt(Clothing):
material="Cotton"
class pants(Clothing):
material="Cotton"
polo = shirt("Polo")
sweatpants = pants("Sweatpants")
polo.add_item(polo.name, polo.material, 4)
sweatpants.add_item(sweatpants.name, sweatpants.material, 6)
current_stock = polo.Stock_by_Material("Cotton")
print(current_stock)
# You can have a situation where two different classes are related, but there is no inheritance going on.
# This is referred to as composition -- where one class makes use of code contained in another class.
# For example, imagine we have a Package class which represents a software package. It contains attributes
# about the software package, like name, version, and size. We also have a Repository class which represents
# all the packages available for installation. While there’s no inheritance relationship between the two classes,
# they are related. The Repository class will contain a dictionary or
# list of Packages that are contained in the repository. Let's take a look at an example Repository class definition:
>>> class Repository:
... def __init__(self):
... self.packages = {}
... def add_package(self, package):
... self.packages[package.name] = package
... def total_size(self):
... result = 0
... for package in self.packages.values():
... result += package.size
... return result
# UPNEXT ARE MODULES::
# modules allow us to organize functions, methods,classes and any other data related together in a structured way.
#EX 1:
import random
print(random.randint(1,5))
print(random.randint(1,5))
print(random.randint(1,5))
2
5
4
# EX 2
# using datetime
import datetime
current_time = datetime.datetime.now()
print(type(current_time))
print(current_time)
print(current_time.year)
print(current_time.time())
print(current_time + datetime.timedelta(days=30)) #prints after certain time scheduled
#output
<class 'datetime.datetime'>
2020-08-21 18:12:51.615840
2020
18:12:51.615840
2020-09-20 18:12:51.615840
#final quiz combinations
#Begin Portion 1
#In this exercise, we'll create a few classes to simulate a server that's taking connections from
# the outside and then a load balancer that ensures that there are enough servers to serve those connections.
# To represent the servers that are taking care of the connections, we'll use a Server class. Each connection
# is represented by an id, that could, for example, be the IP address of the computer connecting to the server.
# For our simulation, each connection creates a random amount of load in the server, between 1 and 10.
# Run the following code that defines this Server class.
import random
class Server:
def __init__(self):
"""Creates a new server instance, with no active connections."""
self.connections = {}
def add_connection(self, connection_id):
"""Adds a new connection to this server."""
connection_load = random.random()*10+1
# Add the connection to the dictionary with the calculated load
def close_connection(self, connection_id):
"""Closes a connection on this server."""
# Remove the connection from the dictionary
def load(self):
"""Calculates the current load for all connections."""
total = 0
# Add up the load for each of the connections
return total
def __str__(self):
"""Returns a string with the current load of the server"""
return "{:.2f}%".format(self.load())
#End Portion 1#
#create a server instance
server = Server()
server.add_connection("192.168.1.1")
print(server.load()) # should give you a 0 meaning no connected server
#close server connecition
server.close_connection("192.168.1.1")
print(server.load())
# LOADSERVER
class LoadBalancing:
def __init__(self):
"""Initialize the load balancing system with one server"""
self.connections = {}
self.servers = [Server()]
def add_connection(self, connection_id):
"""Randomly selects a server and adds a connection to it."""
server = random.choice(self.servers)
self.connection_id = connection_id
# Add the connection to the dictionary with the selected server
self.connections[self.connection_id]=server
print (server.load())
# Add the connection to the server
Server.add_connection(server)
def close_connection(self, connection_id):
"""Closes the connection on the the server corresponding to connection_id."""
# Find out the right server
server = self.connections[connection_id]
# Close the connection on the server
Server.close_connection(server)
# Remove the connection from the load balancer
del self.connections[connection_id]
#LOAD2
class LoadBalancing:
def __init__(self):
"""Initialize the load balancing system with one server"""
self.connections = {}
self.servers = [Server()]
def add_connection(self, connection_id):
"""Randomly selects a server and adds a connection to it."""
server = random.choice(self.servers)
self.connection_id = connection_id
# Add the connection to the dictionary with the selected server
self.connections[self.connection_id]=server
print (server.load())
# Add the connection to the server
server.add_connection(server)
def close_connection(self, connection_id):
"""Closes the connection on the the server corresponding to connection_id."""
# Find out the right server
server = self.connections[connection_id]
# Close the connection on the server
server.close_connection(server)
# Remove the connection from the load balancer
del self.connections[connection_id]
def avg_load(self):
"""Calculates the average load of all servers"""
# Sum the load of each server and divide by the amount of servers