Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
COMP 202 - Foundations of Programming
Important notice
• Please read the entire assignment guidelines and this PDF before starting. You must do this assignment individually.
• Make sure to follow all the programming standards and to add docstrings with three examples for each method in the assignment (you may use only one example from thisdocument and you should think of another two examples of your own).
The main learning objectives for this assignment are:
Figure 1: The file qrcode binary.txt opened in a text editor.
QR Code
A QR code (short for Quick Response Code) is a two-dimensional barcode that can storeinformation, which can be quickly accessed by scanning it with a device like a smartphone,tablet, or dedicated QR code reader. Unlike traditional barcodes, which store data in a linear fashion, QR codes use both horizontal and vertical dimensions to encode data, allowingthem to hold much more information.
A QR code consists of black and white squares arranged in a grid. The black squares represent the binary ”1” and the white squares represent the binary ”0”. The information isencoded in a format that is easily readable by devices with cameras.
In this assignment, binary representations of QR codes are stored in Txt files1. We will firstimplement some helper functions. Then we will create a TxtData class, which representsthe data stored in the Txt files. Fianlly, we will implement a QR code class that represents a QR code (the data itself + other information and functionalities).
Figure 2: The file small data.txt opened in a text editor.
1 Question 1: Helper functions [25 points]
1.1 convert date [10 points]
A function that takes in an input date str (string) and returns a dictionary with ”Day”, ”Month”, and ”Year” as keys and strings as values. The input date str should have the format ”dd/mm/yyyy” (2 digits/2 digits/4 digits) and this function should convert the string to the desired dictionary.
In your code, check that the input string has the right format. More specifically, checkthat it can be split into three parts by the ”/” character and that each part has the right length. You can assume that the input string has the correct format if it meets these requirements. Raise a ValueError if any of these requirements are not met.
1.2 get data [15 points]
A function that takes one input file path (string) and returns a nested list of integers ,representing the data in the file. The method should read the file located at file path. You can assume that the file exists.
The file should only contain ”0”s and ”1”s. Otherwise, raise a ValueError.
See below for an example. You can find the files small data and small data error can be found in figure 2 and 3.
Figure 3: The file small data error.txt opened in a text editor.
2 Question 2: Class TxtData [45 points]
• data: a nested list of integer representing the data;• rows: an integer indicating the number of rows in data;• cols: an integer indicating the number of columns in data;
2.1 init [5 points]
Figure 1 shows you how qrcode binary.txt looks like.
>>> my_txt.cols
33
2.2 _ _str_ _ [5 points]
2.3 get pixels [5 points]
2.4 get data at [5 points]
2.5 pretty save [10 points]
”1”s are changed into ” ” (two blocks) and ”0”s and changed into ” ” (two spaces). It then saves the prettier QR code into a new file named file name. You can assume that there is no file with the name file name that exists beforehand.
The block character has the Unicode U+2588. To get the character in Python, you can use either ”\u2588” or chr(0x2588). See figure 4 for an example of a file saved from this method.
2.6 equals [5 points]
An instance method that takes in one explicit input another data (TxtData) and returns a boolean indicating if the two TxtData objects are equal.
Two TxtData objects are considered equal if the data attributes are the same. In the example below, file qrcode binary copy.txt is an exact copy of file qrcode binary.txt, while qrcode binary 1.txt has exactly one different value than qrcode binary.txt.
Figure 4: The file saved from pretty save method opened in a text editor.
2.7 approximately equals [10 points]
imately equal.
Two TxtData objects are considered approximately equal if the inconsistent rate of the two data attributes is not greater than the input precision. The inconsistent rate can be cal culated by getting the number of inconsistent values and dividing that by the total number of values (number of pixels).
In the examples below, qrcode binary 1.txt has exactly one different value than qrcode binary.txt and qrcode binary 2.txt has two different values.
3 Question 3: Class QRCode [30 points]
- last update date: a dictionary with ”Day”, ”Month”, and ”Year” as keys and strings representing the last update date of the QR code as values.
- owner: a string representing the owner of the QR code.
- data: a TxtData object representing the QR code itself.
- error correction: a float indicating the error correction capability of the QR code. QR codes have built-in error correction capabilities, which enable them to still be readable even if part of the code is damaged or obscured. This attribute indicates the max value of the damage rate before the QR code is unreadable. A QR code is considered to be corrupted if it is no longer readable.
Include the following instance methods and write your QRCode class in a file called qrcode.py. This file should import the helper.py file that you created in Question 1 and the txtdata.py file that you created in Question 2.
3.1 _ _init _ _ [10 points]
A constructor that takes in four explicit inputs file path (string), last update date (string, optional), owner (string, optional), error correction (float, optional).
It should construct a TxtData object with the file path input to get the data attribute.
You can assume that the last update date input has the format ”dd/mm/yyyy”. This constructor should call a helper method and convert the string to the desired dictionary for the last update date attribute. If no input is given, the default value is ”00/00/0000”.
If no input is given for owner, the default value is ”Default Owner”.
If no input is given for error correction, the default value is 0.0.
3.2_ _str_ _ [10 points]
A _ _str_ _ method that returns a string of the format:
”The QR code was created by OWNER and last updated in LAST UPDATE YEAR.
The details regarding the QR code file are as follows:
This TxtData object has ROWS rows and COLS columns.”
3.3 equals [5 points]
3.4 is corrupted [5 points]
An instance method that takes in one explicit input precise qrcode (QRCode) and returns whether the self object is corrupted. To know if a QR code is corrupted, you first compare it with a precise QR code. A QR code is considered corrupted if it’s not approximately equal with the precise QR code with the error correction as the precision.
In the example below, almost half of the data inside the file qrcode binary c.txt is different from the data inside the file qrcode binary.txt.