Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PyBatScoreSOLID
#python 3.6.9 #PyBatScoreSOLID: example of SOLID principles where Single Responsibility Principle,Open-closed Principle(class Batter uses class Person's method to print name; we can create/expand class Bowler to print name of a bowler based on class Person's methods;Similarly, class PerAbsTran is opened for extension as both class Abs2Per and class Per2Abs use it to define two different versions of convert method; note that both Person and PerAbsTran classes are closed for modifications),Interface Segregation Principle(class Person provides name of player whereas class Batter offers match played, score/wicket,match average of a player; so the two interfaces contain cohesive methods),Dependency Inversion Principle(Given the appropriate converter,High value module class Transform depends on abstract class PerAbsTran to convert score from absolute to percent or from percent to absolute) are highlighted #this code is created by Rezaul Hoque on April 19,2022; #contact: jewelmrh@yahoo.com; Dhaka, Bangladesh,https://rezaulhoque.wordpress.com;https:hoquestake.blogspot.com #note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; from abc import ABC class Person(): def __init__(self,first, last): self.first = first self.last = last @property def filast(self): return f"{self.first} \t {self.last}" class PerAbsTran(ABC): def convert(self,s:float,total:int)->float: pass class Abs2Per(PerAbsTran): def convert(self,s:float,total: int)->float: print(f'{s/total*100}') return s/total*100 class Per2Abs(PerAbsTran): def convert(self,s2:float,total: int)->float: print(f'{s2/100*total}') return s2/100*total class Transform: def __init__(self,converter:PerAbsTran): self.converter=converter def disp(self): self.converter.convert(s,total) class Batter(Person): def __init__(self,first,last,match,run): super().__init__(first,last) self.run = run self.match=match def mscore(self): return self.run def matchno(self): return self.match def mavg(self): return self.run/self.match class Detail: def __init__(self): self.thelist= [ ] def add(self,score): self.thelist.append(score) #private method def __banner(self): print("Batters' score of a team:\n") print("~"*35) print("~"*35) print("name(first,last) match run avg\n") def totm(self): total=0 for s in self.thelist: total += s.matchno() return total def tot(self): total=0 for s in self.thelist: total += s.mscore() return total def display(self): for i in self.thelist: ##important:string objects are not callable like i.filast();they should be called like i.filast print(f"{i.filast}\t{i.matchno()}\t{i.mscore()}\t{i.mavg()}") if __name__ == '__main__': det = Detail() det._Detail__banner() det.add(Batter('hh','cc',5,182)) det.add(Batter('tt','nn',6,270)) det.add(Batter('yy','mm',8,350)) det.display() print("-"*35) print("Total ",det.totm()," ",det.tot()) total=det.tot() # a=Abs2Per() p=Per2Abs() #s=float(input("Enter score:\n")) s=float(input("Enter percentage:\n")) # t=Transform(a) # t.disp() e=Transform(p) e.disp()
run
|
edit
|
history
|
help
0
EquivalentRandomDigits
mat
4
Lab_III_3_01_12_2020
Prime Numbers :-> Sieve of Eratosthenes
EJ2_PYTHON_20.321.212-7
Binary number into Decimal
Hesap makinesi
Принцип наименьшего времени Ферма
linked_lists