Design Pattern Examples
Overview of object-oriented design patterns
adapter_exercise.py
Go to the documentation of this file.
6
7from .adapter_frontendclass import MemoryBlock, DataReaderWriter, DataReaderWriterException, DataReaderWriterInitException
8
9
14
15# ! [Using Adapter in Python]
17 print()
18 print("Adapter Exercise")
19
20 try:
21
22 with DataReaderWriter(MemoryBlock.MEMORY_BLOCK_0) as dataReaderWriter:
23 memoryBlockSize = dataReaderWriter.MemoryBlockByteSize
24 # Create the data to be written
25 dataSize = 16
26 byteOffset = 41
27 writeData = []
28 for index in range(0, dataSize):
29 writeData.append(index + 1)
30
31 readData = dataReaderWriter.Read(0, memoryBlockSize)
32 dataDump = dataReaderWriter.BufferToString(readData, memoryBlockSize, 2)
33 print(" Initial memory block contents:")
34 print(dataDump)
35
36 # Display the data to be written
37 dataDump = dataReaderWriter.BufferToString(writeData, len(writeData), 2)
38 print(" Data to be written to memory block:")
39 print(dataDump)
40
41 print(" Writing data to byte offset {}...".format(byteOffset))
42 # Write the data to the external component
43 dataReaderWriter.Write(byteOffset, writeData, len(writeData))
44
45 print(" Reading back the memory block...")
46 # Read the data from the external component
47 readData = dataReaderWriter.Read(0, memoryBlockSize)
48 print()
49
50 # Display the data read back. Should be the same as was written.
51 dataDump = dataReaderWriter.BufferToString(readData, memoryBlockSize, 2)
52 print(" Current memory block contents:")
53 print(dataDump)
54 except DataReaderWriterInitException as ex:
55 print("Error with startup or shutdown! {0}".format(ex))
56 except DataReaderWriterException as ex:
57 print("Error with reading or writing! {0}".format(ex))
58
59 print(" Done.")
60# ! [Using Adapter in Python]
def Adapter_Exercise()
Example of using the Adapter Pattern.