X Tutup
Skip to content
Merged
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
42 changes: 22 additions & 20 deletions patterns/creational/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,34 @@ def __del__(self):


def main():
import queue
"""
>>> import queue

def test_object(queue):
pool = ObjectPool(queue, True)
print('Inside func: {}'.format(pool.item))
>>> def test_object(queue):
... pool = ObjectPool(queue, True)
... print('Inside func: {}'.format(pool.item))

sample_queue = queue.Queue()
>>> sample_queue = queue.Queue()

sample_queue.put('yam')
with ObjectPool(sample_queue) as obj:
print('Inside with: {}'.format(obj))
print('Outside with: {}'.format(sample_queue.get()))
>>> sample_queue.put('yam')
>>> with ObjectPool(sample_queue) as obj:
... print('Inside with: {}'.format(obj))
Inside with: yam

sample_queue.put('sam')
test_object(sample_queue)
print('Outside func: {}'.format(sample_queue.get()))
>>> print('Outside with: {}'.format(sample_queue.get()))
Outside with: yam

>>> sample_queue.put('sam')
>>> test_object(sample_queue)
Inside func: sam

>>> print('Outside func: {}'.format(sample_queue.get()))
Outside func: sam

if not sample_queue.empty():
print(sample_queue.get())

"""

if __name__ == '__main__':
main()

### OUTPUT ###
# Inside with: yam
# Outside with: yam
# Inside func: sam
# Outside func: sam
import doctest
doctest.testmod()
58 changes: 31 additions & 27 deletions patterns/structural/3-tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,36 @@ def get_product_information(self, product: str) -> None:


def main():
ui = Ui()
ui.get_product_list()
ui.get_product_information("cheese")
ui.get_product_information("eggs")
ui.get_product_information("milk")
ui.get_product_information("arepas")

"""
>>> ui = Ui()
>>> ui.get_product_list()
PRODUCT LIST:
(Fetching from Data Store)
milk
eggs
cheese
<BLANKLINE>

>>> ui.get_product_information("cheese")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Cheese, Price: 2.00, Quantity: 10

>>> ui.get_product_information("eggs")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Eggs, Price: 0.20, Quantity: 100

>>> ui.get_product_information("milk")
(Fetching from Data Store)
PRODUCT INFORMATION:
Name: Milk, Price: 1.50, Quantity: 10

>>> ui.get_product_information("arepas")
(Fetching from Data Store)
That product 'arepas' does not exist in the records
"""

if __name__ == "__main__":
main()

### OUTPUT ###
# PRODUCT LIST:
# (Fetching from Data Store)
# cheese
# eggs
# milk
#
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Cheese, Price: 2.00, Quantity: 10
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Eggs, Price: 0.20, Quantity: 100
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Milk, Price: 1.50, Quantity: 10
# (Fetching from Data Store)
# That product "arepas" does not exist in the records
import doctest
doctest.testmod()
22 changes: 14 additions & 8 deletions patterns/structural/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ def render(self):
return "<i>{}</i>".format(self._wrapped.render())


def main():
"""
>>> simple_hello = TextTag("hello, world!")
>>> special_hello = ItalicWrapper(BoldWrapper(simple_hello))

>>> print("before:", simple_hello.render())
before: hello, world!

>>> print("after:", special_hello.render())
after: <i><b>hello, world!</b></i>
"""

if __name__ == '__main__':
simple_hello = TextTag("hello, world!")
special_hello = ItalicWrapper(BoldWrapper(simple_hello))
print("before:", simple_hello.render())
print("after:", special_hello.render())

### OUTPUT ###
# before: hello, world!
# after: <i><b>hello, world!</b></i>
import doctest
doctest.testmod()
34 changes: 20 additions & 14 deletions patterns/structural/front_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ def __init__(self, request):
self.type = self.tablet_type


if __name__ == '__main__':
front_controller = RequestController()
front_controller.dispatch_request(Request('mobile'))
front_controller.dispatch_request(Request('tablet'))

front_controller.dispatch_request(Request('desktop'))
front_controller.dispatch_request('mobile')


### OUTPUT ###
# Displaying mobile index page
# Displaying tablet index page
# cant dispatch the request
# request must be a Request object
def main():
"""
>>> front_controller = RequestController()

>>> front_controller.dispatch_request(Request('mobile'))
Displaying mobile index page

>>> front_controller.dispatch_request(Request('tablet'))
Displaying tablet index page

>>> front_controller.dispatch_request(Request('desktop'))
cant dispatch the request

>>> front_controller.dispatch_request('mobile')
request must be a Request object
"""

if __name__ == "__main__":
import doctest
doctest.testmod()
63 changes: 35 additions & 28 deletions patterns/structural/mvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,38 @@ def show_item_information(self, item_name):
self.view.show_item_information(item_type, item_name, item_info)


if __name__ == "__main__":

model = ProductModel()
view = ConsoleView()
controller = Controller(model, view)
controller.show_items()
controller.show_item_information("cheese")
controller.show_item_information("eggs")
controller.show_item_information("milk")
controller.show_item_information("arepas")


### OUTPUT ###
# PRODUCT LIST:
# cheese
# eggs
# milk
#
# PRODUCT INFORMATION:
# Name: Cheese, Price: 2.00, Quantity: 10
#
# PRODUCT INFORMATION:
# Name: Eggs, Price: 0.20, Quantity: 100
#
# PRODUCT INFORMATION:
# Name: Milk, Price: 1.50, Quantity: 10
#
# That product "arepas" does not exist in the records
def main():
"""
>>> model = ProductModel()
>>> view = ConsoleView()
>>> controller = Controller(model, view)

>>> controller.show_items()
PRODUCT LIST:
milk
eggs
cheese
<BLANKLINE>

>>> controller.show_item_information("cheese")
PRODUCT INFORMATION:
Name: cheese, Price: 2.00, Quantity: 10
<BLANKLINE>

>>> controller.show_item_information("eggs")
PRODUCT INFORMATION:
Name: eggs, Price: 0.20, Quantity: 100
<BLANKLINE>

>>> controller.show_item_information("milk")
PRODUCT INFORMATION:
Name: milk, Price: 1.50, Quantity: 10
<BLANKLINE>

>>> controller.show_item_information("arepas")
That product "arepas" does not exist in the records
"""

if __name__ == '__main__':
import doctest
doctest.testmod()
X Tutup