3 Replies - 229 Views - Last Post: 26 September 2019 - 03:31 AM Rate Topic: -----

#1 __raphael   User is offline

  • New D.I.C Head

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

Update progress bar - MVP pattern

Posted 25 September 2019 - 02:43 AM

I'm studying the MVP pattern and having a hard time following the principles in order to update in real time a progress bar. As I understand the Presenter checks if there's any update in the Model and then outputs the result, so there's no instantiation of the Presenter in the Model, only the Presenter should instantiate the Model and the View.

My question is: how should I update the progress bar by following the MVP principle? I could of course call presenter.update_progress_bar() from Model, but then it would infringe the MVP principle.

Here's a minimal working example:

PS: for now, I'm using CLI.


/main.py

import modules

def main():
    modules.View(modules.Presenter).run()

if __name__ == "__main__":
    main()



/modules/__init__.py

from modules.Model.Model import Model
from modules.Model.progressbar import ProgressBar
from modules.View.View import View
from modules.Presenter.Presenter import Presenter



/modules/Model/Model.py


class Model:
def __init__(self):
    pass

def long_process(self):
    import time
    for i in range(10):
        time.sleep(0.1)
        print("Update the progress bar.")
    return True



/modules/Model/progressbar.py

# MIT license: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
import sys
class ProgressBar:
def progress(count, total, status=''):
    bar_len = 60
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)

    sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
    sys.stdout.flush()



/modules/View/View.py

import sys
class View:
def __init__(self, presenter):
    self.presenter = presenter(self)

def run(self):
    self.presenter.long_process()

def update_progress_bar(self, msg):
    sys.stdout.write(msg)

def hide_progress_bar(self, msg):
    sys.stdout.write(msg)

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



/modules/Presenter/Presenter.py


class Presenter:
def __init__(self, view):
    import modules
    self.model = modules.Model()
    self.view = view

def long_process(self):
    if self.model.long_process():
        self.view.update_status('Long process finished correctly')
    else:
        self.view.update_status('error')

def update_progress_bar(self, i, total):
    from modules import ProgressBar
    ProgressBar.progress(i, total)
    self.view.update_progress_bar(ProgressBar.progress(i, total))

def end_progress_bar(self):
    self.view.end_progress_bar('\n')




Any suggestions?
Thanks

I could do:

class Model:
def __init__(self, presenter):
    self.presenter = presenter  # Doesn't follow MVP

def long_process(self):
    import time
    for i in range(10):
        time.sleep(0.1)
        self.presenter.update_progress_bar(i, 10)  # Doesn't follow MVP
        print("Update the progress bar.")
    return True



But this is wrong since the Model now instantiates the Presenter.

Is This A Good Question/Topic? 0
  • +

Replies To: Update progress bar - MVP pattern

#2 __raphael   User is offline

  • New D.I.C Head

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

Re: Update progress bar - MVP pattern

Posted 26 September 2019 - 01:42 AM

EDIT: I solved the issue with callback function, very easy. I didn't know the term -:)/>/>

In Presenter:
def callback(self, count, total):
        print(count, total)



In Model:

def long_process(self, callback):
        for i in range(10):
            time.sleep(0.1)
            callback(i+1, 10)
        return True

self.update_progress_bar(count, total)

This post has been edited by DK3250: 26 September 2019 - 03:28 AM
Reason for edit:: Double quote removed

Was This Post Helpful? 0
  • +
  • -

#3 __raphael   User is offline

  • New D.I.C Head

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

Re: Update progress bar - MVP pattern

Posted 26 September 2019 - 02:00 AM

I tried to edit the post, but couldn't find the edit button. So, I clicked on "fast reply" and made a mess, sorry.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,791
  • Joined: 27-December 13

Re: Update progress bar - MVP pattern

Posted 26 September 2019 - 03:31 AM

I have removed the quotes in post #2.
The Edit button will only be available after a certain number of posts (I think it is 10 posts) - until then, you don't have edit rights.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1