8 Replies - 823 Views - Last Post: 29 July 2019 - 06:24 AM Rate Topic: -----

#1 __raphael   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-July 19

MVP Python Implementation

Posted 28 July 2019 - 11:07 AM

Hi,
I'm currently learning Python and now MVP. I found your MVP example in java the clearest I could find on the web. Thank you. I have a question regarding the View. You instantiate the Presenter in the View and vice-versa. My question is, which one comes first in the main function? Because when you create the View obj in the main, the constructor calls the createUI(); right? The createUI(); calls the Presenter before you call view.setPresenter in the main? Could you also take a look at my question on stack overflow please? I translated your code to python: https://stackoverflo...ation-in-python

Thank you,
Raphael

Is This A Good Question/Topic? 1
  • +

Replies To: MVP Python Implementation

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: MVP Python Implementation

Posted 28 July 2019 - 11:15 AM

@__raphael please post your question in the relevant sub forum versus directing people to other sites and such.
Was This Post Helpful? 0
  • +
  • -

#3 __raphael   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-July 19

Re: MVP Python Implementation

Posted 28 July 2019 - 12:43 PM

@modi123_1 I'm sorry about that, you're absolutely right. Here is my question:

I'm trying to understand MVP by implementing the java code of this post in Python.

As I understand. the Presenter is instantiated in the View and vice-versa. So, which one comes first in the main function? Because when the View obj is created in the main, the constructor calls the createUI(); which calls the Presenter before view.setPresenter is called in the main?

In the python code (below), I had to call the createUI(); last in main() because the class Presenter hasn't been initialized inside the View, so self.Presenter.login(passwd) didn't exist yet. Is this correct procedure?

Thank you.

Here's the python version:

./main.py

import modules
def main():
    View = modules.View()
    Model = modules.Model()
    Presenter = modules.Presenter(View, Model)

    View.setPresenter(Presenter)
    View.createUI()

if __name__ == "__main__":
    main()



./models/Model.py

class Model:
    def __init__(self):
        self.password = "password"

    def setPassword(self, password):
        self.password = password

    def getPassword(self):
        return self.password



./models/View.py

class View:
    def __init__(self ):
        self.Presenter = None
        #self.createUI()

    def createUI(self):
        passwd = "password"
        self.Presenter.login(passwd)

    def setPresenter(self, Presenter):
        self.Presenter = Presenter

    def updateStatusLabel(self, text):
        print(text)



./models/Presenter.py

class Presenter:
    def __init__(self, View, Model):
        self.View = View
        self.Model = Model

    def login(self, password):
        result = "incorrect password"

        if self.Model.getPassword() == password:
            result = "Correct password"

        self.View.updateStatusLabel(result)



./models/__init__.py

from modules.Model import Model
from modules.View import View
from modules.Presenter import Presenter


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12641
  • View blog
  • Posts: 45,813
  • Joined: 27-December 08

Re: MVP Python Implementation

Posted 28 July 2019 - 12:48 PM

Hi, __raphael, and Welcome to Dream.in.Code! I have split your posts from this tutorial and moved them into a new thread in the Python forum.

Replies to a tutorial are for asking about that tutorial, not translating it to other languages. For comparison, it is unlikely that someone conversant in Spanish will be able to help you translate your writing to Arabic. Similar logic applies in programming.
Was This Post Helpful? 2
  • +
  • -

#5 __raphael   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-July 19

Re: MVP Python Implementation

Posted 28 July 2019 - 12:56 PM

Thanks @macosxnerd101, I will be more careful in the future.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: MVP Python Implementation

Posted 28 July 2019 - 02:56 PM

Hmm...

So, a MVP quick example:
class Model(object):
    def __init__(self):
        self.password = "password"
    # I'm unclear why you need getters and setters: it's python!
    # def setPassword(self, password): self.password = password
    # def getPassword(self): return self.password


class Presenter(object):
    def __init__(self, view):
        # bad var caps
        # self.View = View
        self.view = view
        # the presenter controls the model
        # the view has no knowledge of it, so it wouldn't be passed
        self.model = Model()
        self.validated = False

    def login(self, password):
        self.validated = self.model.password == password
        if self.validated:
            self.view.update_status("login successful")
            self.view.update_status("welcome to The View")
        else:
            self.view.update_status("incorrect password")
            self.view.update_status("please try again")


class View(object):
    def __init__(self, create_presenter):
        # here view is the main "controller" of the program
        # because presenter and view are cross referential
        # I'll pass a creation function
        self.presenter = create_presenter(self)
        # why self.createUI()

    # I don't like the name
    # def createUI(self):
    # instead
    def run(self):
        # because our presenter talks to models and holds state, we talk to that
        while not self.presenter.validated:
            self.presenter.login(input("please enter valid password: "))

    def update_status(self, msg):
        print(msg)


def main():
    # amusingly, the create_presentor function has the
    # same shape as our class Presenter constructor
    # so we'll just pass that
    View(Presenter).run()

if __name__ == "__main__":
    main()



Compare to the standard MVC:
class Model(object):
    def __init__(self):
        self.password = "password"


class View(object):
    def ask_password(self, msg):
        return input(msg)
    def update_status(self, msg):
        print(msg)

class Controller(object):
    def __init__(self, view, model):
        self.view, self.model = view, model

    def run(self):
        while True:
            if self.model.password == self.view.ask_password("please enter valid password: "):
                break
            self.view.update_status("incorrect password")
            self.view.update_status("please try again")
        self.view.update_status("login successful")
        self.view.update_status("welcome to The View")


def main():
    Controller(View(), Model()).run()

if __name__ == "__main__":
    main()



For comparison of the two: http://geekswithblog.../21/136454.aspx

The whole MVP thing, and it's various other bastard children, stems from the messiness of GUI programming where the UI bit controls all. However, I find the cross concern thing bothersome. The whole point of separations of concerns is, well, separation, after all. In the real world, of course, you'll find such relationships will get real blurry real fast and things often claim to be Mxx, when the really look like Myy.
Was This Post Helpful? 1
  • +
  • -

#7 __raphael   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-July 19

Re: MVP Python Implementation

Posted 29 July 2019 - 03:12 AM

Hello @baavgai

Thank you for the clear answer and examples. It's much more clear now the implementation of MVP. However I'm still having trouble to understand the "View(Presenter).run()", seems like a dependency loop, i.e., both should happens at the same time to work.

The MVC example is more clear to me than the MVP one since the controller is instantiated first in the main()) and have well defined dependencies (Model and View without parameters): "Controller(View(), Model()).run()".

I will try to understand better the "View(Presenter).run()" which is the only part that I'm having trouble now.

PS: I used get and setter just to be close to the original tutorial in java.

Have a nice day,
Raphael
Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: MVP Python Implementation

Posted 29 July 2019 - 05:39 AM

View Post__raphael, on 29 July 2019 - 05:12 AM, said:

I will try to understand better the "View(Presenter).run()" which is the only part that I'm having trouble now.

Sorry, I was just happy with the hack. :P

So,
# this function takes a view and returns a presenter bound to it
def create_presenter(view):
    return Presenter(view)

def main():
    # here, we'll pass a function that will take a view and return a presenter
    view = View(create_presenter)
    view.run()



If you need help visualizing this, we can put in some trace code. Indeed, this looks fun:
class Model(object):
    def __init__(self):
        print("Model init")
        self.password = "password"
        print("Model instantiated")


class Presenter(object):
    def __init__(self, view):
        print("Presenter init")
        self.view = view
        self.model = Model()
        self.validated = False
        print("Presenter instantiated")

    def login(self, password):
        self.validated = self.model.password == password
        if self.validated:
            self.view.update_status("login successful")
            self.view.update_status("welcome to The View")
        else:
            self.view.update_status("incorrect password")
            self.view.update_status("please try again")



class View(object):
    def __init__(self, create_presenter):
        print("View init")
        self.presenter = create_presenter(self)
        print("View instantiated")

    def run(self):
        print("running")
        while not self.presenter.validated:
            self.presenter.login(input("please enter valid password: "))

    def update_status(self, msg):
        print(msg)

# this function takes a view and returns a presentor bound to it
def create_presenter(view):
    print("create_presenter called")
    obj = Presenter(view)
    print("create_presenter returning")
    return obj

def main():
    # here, we'll pass a function that will take a view and return a presentor
    view = View(create_presenter)
    view.run()

if __name__ == "__main__":
    main()



Results:
View init
create_presenter called
Presenter init
Model init
Model instantiated
Presenter instantiated
create_presenter returning
View instantiated
running
please enter valid password:



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#9 __raphael   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-July 19

Re: MVP Python Implementation

Posted 29 July 2019 - 06:24 AM

Hi @baavgai

I was just now inserting some prints to know the order the program is executed, but your function "def create_presenter(view):" made all crystal clear :-)

Thank you very much for you time.


I wish you a great day,
Raphael
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1