diff --git a/.gitignore b/.gitignore index fee50c7..b184a16 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ ## python ## *.pyc +.scannerwork diff --git a/python_code/bad/DIP.py b/python_code/bad/DIP.py index df45cf3..c9ce03c 100644 --- a/python_code/bad/DIP.py +++ b/python_code/bad/DIP.py @@ -4,7 +4,7 @@ class Worker(object): def work(self): - print "I'm working!!" + print("I'm working!!") class Manager(object): @@ -25,7 +25,7 @@ def manage(self): class SuperWorker(object): def work(self): - print "I work very hard!!!" + print("I work very hard!!!") # OK... now you can see what happend if we want the `Manager` to support `SuperWorker`. # 1. The `set_worker` must be modified or it will not pass the type-checking. @@ -44,7 +44,7 @@ def main(): try: manager.set_worker(super_worker) except AssertionError: - print "manager fails to support super_worker...." + print("manager fails to support super_worker....") if __name__ == "__main__": main() diff --git a/python_code/bad/ISP.py b/python_code/bad/ISP.py index 4384a9d..573a5f1 100644 --- a/python_code/bad/ISP.py +++ b/python_code/bad/ISP.py @@ -18,19 +18,19 @@ def eat(self): class Worker(AbstractWorker): def work(self): - print "I'm normal worker. I'm working." + print("I'm normal worker. I'm working.") def eat(self): - print "Lunch break....(5 secs)" + print("Lunch break....(5 secs)") time.sleep(5) class SuperWorker(AbstractWorker): def work(self): - print "I'm super worker. I work very hard!" + print("I'm super worker. I work very hard!") def eat(self): - print "Lunch break....(3 secs)" + print("Lunch break....(3 secs)") time.sleep(3) @@ -40,7 +40,7 @@ def __init__(self): self.worker = None def set_worker(self, worker): - assert isinstance(worker, AbstractWorker), "`worker` must be of type {}".format(AbstractWorker) + assert isinstance(worker, AbstractWorker),("`worker` must be of type {}".format(AbstractWorker)) self.worker = worker @@ -56,10 +56,10 @@ def lunch_break(self): class Robot(AbstractWorker): def work(self): - print "I'm a robot. I'm working...." + print("I'm a robot. I'm working....") def eat(self): - print "I don't need to eat...." # This code doing nothing but it is a must. (Bad!) + print("I don't need to eat....") # This code doing nothing but it is a must. (Bad!) def main(): diff --git a/python_code/bad/LSP.py b/python_code/bad/LSP.py index 5dd5260..54179ae 100644 --- a/python_code/bad/LSP.py +++ b/python_code/bad/LSP.py @@ -12,10 +12,10 @@ class Person(object): def __init__(self, position): self.position = position - def walk_North(self, dist): + def walk_north(self, dist): self.position[1] += dist - def walk_East(self, dist): + def walk_east(self, dist): self.position[0] += dist # `Prisoner` is a logicall natural extension of `Person` @@ -33,16 +33,16 @@ def __init__(self): def main(): prisoner = Prisoner() - print "The prisoner trying to walk to north by 10 and east by -3." + print("The prisoner trying to walk to north by 10 and east by -3.") try: - prisoner.walk_North(10) - prisoner.walk_East(-3) - except: + prisoner.walk_north(10) + prisoner.walk_east(-3) + except IndexError: pass - print "The location of the prison: {}".format(prisoner.PRISON_LOCATION) - print "The current position of the prisoner: {}".format(prisoner.position) + print("The location of the prison: {}".format(prisoner.PRISON_LOCATION)) + print("The current position of the prisoner: {}".format(prisoner.position)) if __name__ == "__main__": main() \ No newline at end of file diff --git a/python_code/bad/open_close.py b/python_code/bad/open_close.py index 78cf489..7e708d3 100644 --- a/python_code/bad/open_close.py +++ b/python_code/bad/open_close.py @@ -29,7 +29,7 @@ def total_area(self): def main(): shapes = [Rectangle(2, 3), Rectangle(1, 6)] calculator = AreaCalculator(shapes) - print "The total area is: ", calculator.total_area + print("The total area is: "), calculator.total_area if __name__ == '__main__': diff --git a/python_code/bad/single_responsibility.py b/python_code/bad/single_responsibility.py index fe38e25..61dace4 100644 --- a/python_code/bad/single_responsibility.py +++ b/python_code/bad/single_responsibility.py @@ -7,15 +7,15 @@ class IEmail(object): __metaclass__ = ABCMeta @abstractmethod - def setSender(self, sender): + def set_sender(self, sender): pass @abstractmethod - def setReceiver(self, receiver): + def set_receiver(self, receiver): pass @abstractmethod - def setContent(self, content): + def set_content(self, content): pass class Email(IEmail): @@ -39,7 +39,7 @@ def setReceiver(self, receiver): else: self.__receiver = receiver - def setContent(self, content): + def set_content(self, content): if self.content_type == 'MyML': self.__content = '\n'.join(['', content, '']) else: @@ -53,10 +53,10 @@ def __repr__(self): def main(): email = Email('IM', 'MyML') - email.setSender('qmal') - email.setReceiver('james') - email.setContent('Hello, there!') - print email + email.set_sender('qmal') + email.set_receiver('james') + email.set_content('Hello, there!') + print(email) if __name__ == '__main__': main() diff --git a/python_code/good/DIP.py b/python_code/good/DIP.py index 4ba6cbb..df90a19 100644 --- a/python_code/good/DIP.py +++ b/python_code/good/DIP.py @@ -18,7 +18,7 @@ def work(self): class Worker(IWorker): def work(self): - print "I'm working!!" + print("I'm working!!") class Manager(object): @@ -39,7 +39,7 @@ def manage(self): class SuperWorker(IWorker): def work(self): - print "I work very hard!!!" + print("I work very hard!!!") # Now, the manager support `SuperWorker`... # In addition, it will support any worker which obeys the interface defined by `IWorker`! @@ -57,7 +57,7 @@ def main(): manager.set_worker(super_worker) manager.manage() except AssertionError: - print "manager fails to support super_worker...." + print("manager fails to support super_worker....") if __name__ == "__main__": main() diff --git a/python_code/good/ISP.py b/python_code/good/ISP.py index bc04096..c9f9e73 100644 --- a/python_code/good/ISP.py +++ b/python_code/good/ISP.py @@ -28,19 +28,19 @@ class AbstractWorker(Workable, Eatable): class Worker(AbstractWorker): def work(self): - print "I'm normal worker. I'm working." + print("I'm normal worker. I'm working.") def eat(self): - print "Lunch break....(5 secs)" + print("Lunch break....(5 secs)") time.sleep(5) class SuperWorker(AbstractWorker): def work(self): - print "I'm super worker. I work very hard!" + print("I'm super worker. I work very hard!") def eat(self): - print "Lunch break....(3 secs)" + print("Lunch break....(3 secs)") time.sleep(3) @@ -71,7 +71,7 @@ def lunch_break(self): class Robot(Workable): def work(self): - print "I'm a robot. I'm working...." + print("I'm a robot. I'm working....") # No need for implementation of `eat` which is not neccessary for a `Robot`. @@ -97,7 +97,7 @@ def main(): try: break_manager.set_worker(Robot()) break_manager.lunch_break() - except: + except IndexError: pass if __name__ == '__main__': diff --git a/python_code/good/LSP.py b/python_code/good/LSP.py index ad59b69..279affa 100644 --- a/python_code/good/LSP.py +++ b/python_code/good/LSP.py @@ -11,10 +11,10 @@ class FreeMan(object): def __init__(self, position): self.position = position - def walk_North(self, dist): + def walk_north(self, dist): self.position[1] += dist - def walk_East(self, dist): + def walk_east(self, dist): self.position[0] += dist # "is-a" relationship no longer holds since a `Prisoner` is not a `FreeMan`. @@ -27,16 +27,16 @@ def __init__(self): def main(): prisoner = Prisoner() - print "The prisoner trying to walk to north by 10 and east by -3." + print("The prisoner trying to walk to north by 10 and east by -3.") try: - prisoner.walk_North(10) - prisoner.walk_East(-3) - except: + prisoner.walk_north(10) + prisoner.walk_east(-3) + except IndexError: pass - print "The location of the prison: {}".format(prisoner.PRISON_LOCATION) - print "The current position of the prisoner: {}".format(prisoner.position) + print("The location of the prison: {}".format(prisoner.PRISON_LOCATION)) + print("The current positionadf of the prisoner: {}".format(prisoner.position)) if __name__ == "__main__": main() \ No newline at end of file diff --git a/python_code/good/open_close.py b/python_code/good/open_close.py index f3f6801..97a5472 100644 --- a/python_code/good/open_close.py +++ b/python_code/good/open_close.py @@ -44,7 +44,7 @@ def main(): shapes = [Rectangle(1, 6), Rectangle(2, 3)] calculator = AreaCalculator(shapes) - print "The total area is: ", calculator.total_area + print("The total area is: "), calculator.total_area if __name__ == '__main__': main() diff --git a/python_code/good/single_responsibility.py b/python_code/good/single_responsibility.py index 0454a53..72807e3 100644 --- a/python_code/good/single_responsibility.py +++ b/python_code/good/single_responsibility.py @@ -12,11 +12,11 @@ class IEmail(object): __metaclass__ = ABCMeta @abstractmethod - def setSender(self, sender): + def set_sender(self, sender): pass @abstractmethod - def setReceiver(self, receiver): + def set_receiver(self, receiver): pass @abstractmethod @@ -27,7 +27,7 @@ class IContent(object): __metaclass__ = ABCMeta @abstractmethod - def getString(self): + def get_string(self): pass class MyContent(IContent): @@ -35,7 +35,7 @@ class MyContent(IContent): def __init__(self, content): self.content = content - def getString(self): + def get_string(self): return "{}".format(self.content) class Email(IEmail): @@ -46,34 +46,34 @@ def __init__(self, protocol): self.__receiver = None self.__content = None - def setSender(self, sender): + def set_sender(self, sender): if self.protocol == 'IM': self.__sender = ''.join(["I'm ", sender]) else: self.__sender = sender - def setReceiver(self, receiver): + def set_receiver(self, receiver): if self.protocol == 'IM': self.__receiver = ''.join(["I'm ", receiver]) else: self.__receiver = receiver def setContent(self, content): - self.__content = content.getString() + self.__content = content.get_string() def __repr__(self): template = "Sender: {sender}\nReceiver: {receiver}\nContent:\n{content}" - +F return template.format(sender = self.__sender, receiver = self.__receiver, content = self.__content) def main(): email = Email('IM') - email.setSender('qmal') - email.setReceiver('james') + email.set_sender('qmal') + email.set_receiver('james') content = MyContent('Hello, there!') email.setContent(content) - print email + print(email) if __name__ == '__main__': main() diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..da36a9a --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,5 @@ +sonar.projectKey=swlabs:solid +sonar.projectName=solid +sonar.sources=python_code +sonar.language=py +sonar.login=squ_2cce35d72c1ee852759b5876b9e2d0aac3f774a8 \ No newline at end of file