5. Procedure: Creating the Classes 1. Create a folder named oopfa1_lab8 2. Open your IDE in that folder. 3. Cr
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
5. Procedure: Creating the Classes 1. Create a folder named oopfa1_lab8 2. Open your IDE in that folder. 3. Cr
Task Create a simple TextFileReaderWriter .py file and Class
that will be able to read from and write (override) to a text file.
The read and write method should be overriden according to the
requirement of Text File Reading and Writing as performed in
Laboratory Activity 5.
5. Procedure: Creating the Classes 1. Create a folder named oopfa1<lastname>_lab8 2. Open your IDE in that folder. 3. Create the base FileReaderWriter .py file and Class using the code below: FileReaderWriter.py > ... 1 class FileReaderWriter(): def read(self): 2 def write (self): ~ 3456 3 5 print("This is the default read method") print("This is the default write method")
4. Create the CSVFile ReaderWriter .py and Class using the code below: CSVFile ReaderWriter.py > ... 1 from FileReaderWriter import FileReaderWriter 2 import csv 3 4 class CSVFileReaderWriter (FileReaderWriter): def read (self, filepath): 5 6 7 8 9 10 11 12 13 14 DHDBH55 15 16 with open (filepath, newline='') as csvfile: data = csv. reader (csvfile, delimiter=',', quotechar='|') for row in data: print (row) return data def write(self, filepath, data): with open(filepath, 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv. QUOTE_MINIMAL) writer.writerow(data)
5. Create the JSONFileReaderWriter Class using the code below JSONFileReaderWriter.py > ... 1 from FileReaderWriter import FileReaderWriter import json 2 3 4 class JSONFileReaderWriter (FileReaderWriter): def read (self, filepath): 5 6 with open(filepath, "r") as read_file: data = json.load(read_file) 7 8 print (data) 9 return data 10 11 def write(self, filepath, data): 12 with open(filepath, "w") as write_file: json.dump(obj=data, fp=write_file) 13 23
Testing and Observing Polymorphism 1. Create a .csv file named sample.csv with the following content. (you may use the IDE or plain notepad) sample.csv Apple, Banana, Mango, Orange, Cherry
2. Create a .json file named sample.json with the following content. (you may use the IDE or plain notepad) {}sample.json > ... 1 { "description":"This is a JSON Sample", "accounts": [ {"id":1,"name":"Jack"}, {"id":2,"name": "Rose"} ] 123 2 4 5 6 7 }
3. Create the main.py that will test the functionality of the classes. main.py > ... 2 1 from FileReaderWriter import FileReaderWriter from CSVFileReaderWriter import CSVFileReaderWriter from JSONFileReaderWriter import JSONFileReaderWriter 3 4 5 # Test the default class df = FileReaderWriter() 6 7 df.read() 8 df.write() 9 10 # Test the polymoprhed methods C = CSVFileReaderWriter() 11 12 c.read("sample.csv") 13 c.write(filepath="sample2.csv", data=["Hello","World"]) 14 15 j = JSONFileReaderWriter() 16 j.read("sample.json") 17 j.write(data= ['foo', {'bar': ('baz', None, 1.0, 2)}],filepath="sample2.json") 4. Run the program and observe the output carefully the values in sample2.csv and sample2.json.