Section | Video Links |
---|---|
Adapter Overview | |
Adapter Use Case | |
Python isinstance() Function | |
Python time Module |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python ./adapter/adapter_concept.py
method A
method B
method A
method B
... Refer to Book or Design Patterns In Python website to read textual content.
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
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.
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.
... Refer to Book or Design Patterns In Python website to read textual content.