mercredi 22 avril 2020

Implementation of MVC Design Pattern in Python3

I am trying to implement MVC using Python3.8. I have used this https://www.tutorialspoint.com/python_design_patterns/python_design_patterns_model_view_controller.htm Python2's example for practice. But, I am receiving the following error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

My code is as following: model.py

import json

class Person:
    def __init__(self, first = None, last = None):
        self.first = first
        self.last = last

    def name(self):
        return ('%s %s' %(self.first, self.last))

    @classmethod
    def getAll(self):
        database = open('data.txt', 'r')
        result = []
        jsonList = json.loads(database.read())
        for item in jsonList:
            item = json.loads(item)
            person = Person(item['first'], item['last'])
            result.append(person)
        return result

view.py

from model import Person
def showAllView(list):
    print ('In our db we have %i users. Here they are:' % len(list))
    for item in list:
        print (item.name())

def startView():
    print ('MVC - the simplest example')
    print ('Do you want to see everyone in my db?[y/n]')

def endView():
    print ('Goodbye!')

controller.py

from model import Person
import view

def showAll():
    #gets list of all Person objects
    people_in_db = Person.getAll()
    return view.showAllView(people_in_db)

def start():
    view.startView()
    answer = input('Enter y or n')
    if answer == 'y':
        return showAll()
    else:
        return view.endView()

if __name__ == "__main__":
    start()

Please, guide me in this and help me finding the error. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire