Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

## python ##
*.pyc
.scannerwork
6 changes: 3 additions & 3 deletions python_code/bad/DIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Worker(object):

def work(self):
print "I'm working!!"
print("I'm working!!")


class Manager(object):
Expand All @@ -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.
Expand All @@ -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()
14 changes: 7 additions & 7 deletions python_code/bad/ISP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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

Expand All @@ -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():

Expand Down
16 changes: 8 additions & 8 deletions python_code/bad/LSP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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()
2 changes: 1 addition & 1 deletion python_code/bad/open_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':

Expand Down
16 changes: 8 additions & 8 deletions python_code/bad/single_responsibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(['<myML>', content, '</myML>'])
else:
Expand All @@ -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()
6 changes: 3 additions & 3 deletions python_code/good/DIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def work(self):
class Worker(IWorker):

def work(self):
print "I'm working!!"
print("I'm working!!")


class Manager(object):
Expand All @@ -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`!
Expand All @@ -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()
12 changes: 6 additions & 6 deletions python_code/good/ISP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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`.

Expand All @@ -97,7 +97,7 @@ def main():
try:
break_manager.set_worker(Robot())
break_manager.lunch_break()
except:
except IndexError:
pass

if __name__ == '__main__':
Expand Down
16 changes: 8 additions & 8 deletions python_code/good/LSP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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()
2 changes: 1 addition & 1 deletion python_code/good/open_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
22 changes: 11 additions & 11 deletions python_code/good/single_responsibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,15 +27,15 @@ class IContent(object):
__metaclass__ = ABCMeta

@abstractmethod
def getString(self):
def get_string(self):
pass

class MyContent(IContent):

def __init__(self, content):
self.content = content

def getString(self):
def get_string(self):
return "<MyML>{}</MyML>".format(self.content)

class Email(IEmail):
Expand All @@ -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()
5 changes: 5 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sonar.projectKey=swlabs:solid
sonar.projectName=solid
sonar.sources=python_code
sonar.language=py
sonar.login=squ_2cce35d72c1ee852759b5876b9e2d0aac3f774a8