0

So my confusion comes from how to even begin a setup with MVC. I understand that the View is simply printing info to the user, however I do not understand how I can pass variables from class to class.

For example how can I pass the user input "option" and pass it to the controller/main class. Shown in the third image (mainclass pt 2) I have a basic menu and I need all the functionality done. However #2 is creating a new txt file to print the data from the csv file into it. However when I run the code the new txt file is not created.

Any info on how I should reformat this code to be a proper MVC architecture along with any help to solving why my txt file is not being created would be extremely helpful.

Controller class:

import pandas as pd
import os

import View

'''variables to declare basic values such as name, filepath'''

csv_filepath = "C:/Users/Liam/PycharmProjects/assignment1/pipeline-incidents-comprehensive-data.csv"
Fname = "Liam Arscott"

'''Data frame reads the csv file using pandas, ensure utf-8 encoding'''
try:
    df = pd.read_csv(csv_filepath, encoding="utf-8")
except:
    '''written exception for if the file does not load'''
    print("file did not load or could not be located")

'''create list '''
C_list = []
for ind in df.index:
    '''index the df and append to the list'''
    col = (df['Incident Number'][ind], df['Incident Types'][ind], df['Reported Date'][ind],
           df['Nearest Populated Centre'][ind], df['Province'][ind], df['Company'][ind],
           df['Substance'][ind], df['Significant'][ind], df['What happened category'][ind])
    C_list.append(col)

    '''Create break statement once the for loop has gone over 100 times'''
    if ind == 99:
        break

View class:

import pandas as pd

'''declare csv_filepath for quick and easy use '''
csv_filepath = "C:/Users/Liam/PycharmProjects/assignment1/pipeline-incidents-comprehensive-data.csv"


'''File io '''
def read():
    return pd.read_csv("pipeline-incidents-comprehensive-data.csv", encoding="utf-8")


def load(file):
    file.to_csv("pipeline-incidents-comprehensive-data.csv", index=False)


class Menu:
    print("MENU")
    print("  1. Reload the data from the dataset")
    print("  2. Write dataset to new csv file")
    print("  3. Display 1 or many records")
    print("  4. Create a new record ")
    print("  5. Edit an existing record")
    print("  6. Delete an existing record")
    print("  7. Display all records")
    option = input("which menu option would you like?")

    if option == 1:
        file = read()
        print("refreshing data")
        load(file)
        print("data has been refreshed")

    if option == 2:
        with open("pipeline-incidents-comprehensive-data.csv", "r") as ffile, open("secfile.txt", "a") as secfile:
            for line in ffile:
                secfile.write(line)
        print(" dataset has been loaded to new file ")

Model class:


'''deal with current object using self and create class Columns to initialize attributes using the init method'''
class Columns:
    def __init__(self, incNum, incType, rDate, NPC, Province, Company, Sub, Sig, wtHap):
        self.incNum = incNum
        self.incType = incType
        self.rDate = rDate
        self.NPC = NPC
        self.Province = Province
        self.Company = Company
        self.Sub = Sub
        self.Sig = Sig
        self.wtHap = wtHap
        '''convert attribute values to formatted string literals using str method'''
        def __str__(self):
            return {self.incNum}, {self.incType}, {self.rDate}, {self.NPC}, {self.Province}, {self.Company}, {
                self.Sub}, {self.Sig}, {self.wtHap}
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Overall you're asking potentially a very broad question also I don't see any `mainclass` in pt 2 (whatever that is). As for passing `option` between classes: The way I've seen this implemented within the architecture is by having the Controller class store shared data and pass itself to the Model and View classes when they are instantiated so they can access them through it. An example of it being handled that way is in [this answer](https://stackoverflow.com/a/32865334/355230). – martineau Feb 25 '22 at 22:19
  • Off-topic: If you're going to be writing much Python code I **strongly** suggest that you read and immediately start following the [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/), as well as [PEP 257 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/) (because you seem to be misusing them). – martineau Feb 25 '22 at 22:25

0 Answers0