Skip to content

Latest commit

 

History

History

adapter

Adapter Design Pattern

Videos

Section Video Links
Adapter Overview Adapter Overview Adapter Overview Adapter Overview
Adapter Use Case Adapter Use Case Adapter Use Case Adapter Use Case
Python isinstance() Function Python isinstance() Function Python isinstance() Function Python isinstance() Function
Python time Module Python time Module Python time Module Python time Module

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

Overview

... Refer to Book or Design Patterns In Python website to read textual content.

Terminology

... Refer to Book or Design Patterns In Python website to read textual content.

Adapter UML Diagram

Adapter Pattern UML Diagram

Source Code

... Refer to Book or Design Patterns In Python website to read textual content.

Output

python ./adapter/adapter_concept.py
method A
method B
method A
method B

Example Use Case

... Refer to Book or Design Patterns In Python website to read textual content.

Example UML Diagram

Adapter Pattern in Context

Output

python ./adapter/client.py
Company A is busy, trying company B
Company B is busy, trying company A
Company A is busy, trying company B
Company B is busy, trying company A
Company A building Cube id:2968196317136, 2x3x7
Company A is busy, trying company B
Company B building Cube id:2968196317136, 8x2x8
Company A building Cube id:2968196317040, 4x6x4
Company A is busy, trying company B
Company B is busy, trying company A
Company A building Cube id:2968196317136, 5x4x8
Company A is busy, trying company B
Company B building Cube id:2968196317136, 2x2x9
5 cubes have been manufactured

New Coding Concepts

Python isinstance() Function

Syntax: isinstance(object, type)

Returns: True or False

You can use the inbuilt function isinstance() to conditionally check the type of an object.

>>> isinstance(1,int) 
True
>>> isinstance(1,bool) 
False
>>> isinstance(True,bool) 
True
>>> isinstance("abc",str)    
True
>>> isinstance("abc",(int,list,dict,tuple,set)) 
False
>>> isinstance("abc",(int,list,dict,tuple,set,str)) 
True

You can also test your custom classes.

class my_class:
    "nothing to see here"

CLASS_A = my_class()
print(type(CLASS_A))
print(isinstance(CLASS_A, bool))
print(isinstance(CLASS_A, my_class))

Outputs


<class '__main__.my_class'>
False
True

You can use it in logical statements as I do in /adapter/adapter_concept.py.

Python time Module

The time module provides time related functions, most notably in my case, the current epoch (ticks) since January 1, 1970, 00:00:00 (UTC) .

The time module provides many options that are outlined in more detail at https://docs.python.org/3/library/time.html

In /adapter/cube_a.py, I check the time.time() at various intervals to compare how long a task took.

    now = int(time.time())
    if now > int(CubeA.last_time + 1):
        CubeA.last_time = now
        return True

I also use the time module to sleep for a second between loops to simulate a 1 second delay. See /adapter/client.py

    # wait some time before manufacturing a new cube
    time.sleep(1)

When executing /adapter/cube_a.py you will notice that the process will run for about 10 seconds outputting the gradual progress of the construction of each cube.

Summary

... Refer to Book or Design Patterns In Python website to read textual content.