[Tutor] Adding tasks periodically to asyncio.get_event_loop().run_forever()

2016-05-30 Thread An Nguyen
Hello all,

I have a question related to AsyncIO. I have a while True: loop that
executes a list of tasks asynchronously every 10 seconds.

while True:
> loop = asyncio.get_event_loop()
> tasks = [ ]
> for arg in args:
> tasks.append(asyncio.ensure_future(my_coroutine(arg)))
> results = [ ]
> results = loop.run_until_complete(gather(*tasks))
> print(results)
> sleep(10)
>


Now how can I achieve the same outcome with loop.run_forever()?

What I want to achieve is to have a loop.run_forever() in my main(). Then
periodically, a list of tasks will be added to that event_loop for
execution.

Please enlighten me.

Thank you.
An.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Study Tips

2016-05-30 Thread Steve Lett
Hi folks,
Just started learning python. I've been having a really hard time in
getting started, and still am! I have a slight learning difficulty,
including a stroke in Jan.2010. You wouldnt know even if u were here
looking at me! Praise God for the great salvation!
I have a slight learning difficulty. Has anyone got any tips on how to
study programming and what approach would be best for me?

Out of a long list of books that I have collected ( python, Blender, etc )
I have decided to start on Introducing Python (hard copy 2nd Ed, ebook 3rd
ed) by Bill Lubanovic. Before that I was going to start with Python
Programming for the Absolute Beginner, Michael Dawson.

Any thoughts on these issues and especially the study tips already
mentioned.

Thank you, Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of model-view-controller design pattern

2016-05-30 Thread Alan Gauld via Tutor
On 29/05/16 05:33, boB Stepp wrote:

As promised I'm back so will try to answer these...

> Some other questions:
> 
> 1)  If I were to add code to filter user inputs, which file is the
> logical place to place such code?  My current impression is that it
> should go in the controller, but I could see it being in the view.
> However, if I were to write a Validation class, I could see that as
> having uses beyond checking just user input.

Filtering user input is one type of validation (others include
type and value checking) Usually the view (especially in a GUI)
will control the type checking and in  some cases the value
checking too (think calendar pickers). But the controller may
also do some data adjustment (such as reformatting a date
to suit a particular model). But mostly I expect the view
to collect the correct data in terms of primitive type/value.

An example where more complex validation might be required is
where you want to validate a credit card or an account number.
In that case the controller may call a business service before
passing the validated details onto the appropriate model(s)
to process.

Models should usually expect to be given good data. In some
cases you might want to do a belt n' braces data check in
the model too, but mostly you assume your front end is
catching the bad stuff (see file processing below).

> 2)  Currently I am using a while loop in the controller to keep the
> view 'alive'.  If I were to convert the view to a tkinter GUI, where
> would the root.mainloop() statement go?  In the controller's main()
> method?  What GUI-related stumbling blocks might I easily run into in
> going from CLI to GUI with this existing code?

See previous post. The main loop is almost always in the view.

> 3)  If I were to add data files in place of (or in addition to) user
> input, in which aspect of MVC would file I/O be handled?

This is where things get interesting. When processing files the
view/controller are only likely to be validating the filename and
initiating the process. This means that there needs to be a model object
somewhere that processes the file. But the content of the
file is unsafe  so that model needs to do all the data
validation that would normally be done in the view/controller.

Its not unusual to have a dedicated model for such batch processing
and it will then call the other model methods for each record processed,
effectively becoming a controller of sorts. There may
be opportunities for code reuse between the UI controller and
the batch controller.

> BTW, I did not attempt to write any classes as things seemed simple
> enough that functions seemed more appropriate and straightforward to
> me.  If that is a bad choice, please let me know!

At this level of simplicity its hard to see the advantages of the MVC
paradigm. (A bit like OOP itself!) Its only when things start to get
complicated that MVC starts to simplify things rather than add noise.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Regular Expressions

2016-05-30 Thread dirkjso...@gmail.com

On 05/24/2016 01:48 PM, Alan Gauld via Tutor wrote:

On 23/05/16 23:08, Terry--gmail wrote:


scripted worked great without the notes!  I'd like to know what it is in
the below Tripple-Quoted section that is causing me this problem...if
anyone recognizes. In IDLE's script file..._it's all colored green_,
which I thought meant Python was going to ignore everything between the
tripple-quotes!

Its all green forv me too and it runs perfectly - as in it does
absolutly nothing.


And if I add print('hello world') at the end it prionts ok too.

I even tried assigning your docsstring to a variable and printing
that and it too worked.

Linux Mint 17
Python 3.4.3
IDLE 3

So I don't think this is your entire problem. Maybe you should
show us some code that actually causes the error?


But if I run just the below portion of the script in
it's own file, I get the same While Scanning Tripple-Quotes error.

As above, it runs silently for me.


Hi Alan,

I moved my notes that contained any '\'s to a different python file.
However, if we run it, we get the error I was having. Here's the
script:

#!/usr/bin/env python3

'''
Regular Expressions - or at least some

Identifiers:

\d  any number
\D  anything but a number (digit)
\s  space
\S  anything but a space
\w  any character
\W  anything but a character
.   any character (or even a period itself if you use \.) except for a 
newline

a   search for just the letter 'a'
\b  the white space around words

Modifiers
{x}we are expecting "x" number of something
{1, 3}  we're expecting 1-3 in length of something -, so for digits we 
write  \d{1-3}

+  means Match 1 or more
?  means Match 0 or 1
*   Match 0 or more
$  Match the end of a string
^  Match the beginning of a string
|   Match either or   - so you might write  \d{1-3} | \w{5-6}
[ ]  a range or "variance" such as [A-Z] or [A-Za-z] Cap 1st letter 
followed by lower case
or [1-5a-qA-Z] starts with a number inclusive of 1-5 then 
lower case letter then

followed by any Cap letter! :)

White Space Characters  (may not be seen):
\n  new line
\s  space
\t   tab
\e  escape
\f  form feed
\r  return

DON'T FORGET!:
.  +  *  ?  [  ]  $  ^  (  )  {  }  |  \   if you really want to use 
these, you must escape them '\'


'''


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread boB Stepp
Hi and welcome to Tutor, Steve!  I am one of the learners, and I can
honestly say that you have found the right place to ask questions when
you get stuck.  Things on the list seem slow this weekend.  In the US,
where I am at, we are having Memorial Day weekend.  I still have to
chew over two meaty answers to questions I asked this weekend from two
resident Pythonistas, Peter Otten and Alan Gauld (Who is also one of
the list's moderators.), but I can't quite get out of holiday mode!
As a way out of learning about decorators, particularly property ones
(Which Peter used in his illustrative code.), I now will post a
lengthy response to your question(s), including some general stuff you
were not asking about that I think you might find helpful.

On Mon, May 30, 2016 at 12:45 AM, Steve Lett  wrote:

> I have a slight learning difficulty. Has anyone got any tips on how to
> study programming and what approach would be best for me?

First, I would like to mention some things I have observed that tend
to trip up newcomers to programming and especially in using this
mailing list.  When you successfully subscribed to Tutor, you should
have received a welcome email which attempted to explain some of the
rules this list follows.  If I recall correctly, it even contains a
link to a web page that explains how to ask intelligent questions on
technical mailing lists (Like this one.).  If you vary from this
practice, then you will probably be given that link again
(http://sscce.org/).  If you have not read it, please do.  It is quite
helpful, not just for you, but for those who *do* wish to help you.

For instance, I trimmed your post down to what I am going to try to
respond to, but I fear I may run on a bit.

The most basic thing is to always respond to the whole Tutor list, not
just to the person who posts a response to your email.  Tutor is a
collaborative learning environment.  Answers to your questions could
easily prove helpful to me and others.  Also, by posting to the entire
Tutor list, you ensure to get a variety of perspectives on your
issue(s).

I notice you have a Gmail address.  If you are using the Gmail email
program, too, then you need to make sure that ALL of your emails and
responses to Tutor are in plain text with no attachments.  If you
don't do this, you will have problems.  The main technical one is that
Python's structure is dependent on consistent and proper indentation
(Four spaces per indent preferred.).  If that indentation gets altered
by using rich text, html formatting, etc., which this list does not
use, then people will not be able to copy and paste your code as is.
The Gmail email program will not do plain text automatically for you,
so if you do not know how to ensure this, then you have some Googling
to do!  If you want to see what your just sent email looks like to the
list members, point your browser to either

https://mail.python.org/pipermail/tutor/

or,

https://www.mail-archive.com/tutor@python.org/

and find your email and view it.  Looks like you are doing fine so
far.  Note:  The first link will have your email show up very quickly.
The second often seems to have a noticeable lag before it shows up.

Also, make sure when you have a problem with one of your programs that
you do the following:

1)  Usually state your operating system (OS) and version of Python
being used.  As you appear to be newly learning both programming and
Python I highly recommend you start with Python 3, which is now the
only version of Python being actively developed.  Python 2 is still
heavily in use, but now is in maintenance mode and will eventually no
longer get even new bug fixes.  The two books you mentioned both use
Python 3, so I suspect you have already realized this.

2)  COPY and PASTE the code you are having issues with.  Do NOT try to
hand-type your code in.  You will probably make a typo along the way,
producing code in your email that is NOT the code you actually ran and
inevitably introducing new errors in that hand-copied code which have
nothing to do with your original problem.  Another Gmail warning:  I
have found that when I copy something from the Python interpreter and
attempt to paste it into an email, my indentations vanish!  This means
I have to go carefully through my code and ensure that all of the
indents are recreated.  Eventually I will have to switch email
clients, but I have many things tied to making Google's environment
work (Android Nexus 5 phone, calendars, etc.) that I dread having to
figure out how to stitch together a variety of programs to recreate my
currently seamless digital experience.).

[An aside:  There are two ways to run Python code, in the Python
interactive interpreter and by invoking Python (Often from the command
line.) on a file containing valid Python code.  The first is handy to
experiment in as you get immediate feedback.  The latter is the form
your applications will be done in.  Python supplies IDLE, which
functions as both an interactive Python en

Re: [Tutor] Learning Regular Expressions

2016-05-30 Thread boB Stepp
On Mon, May 30, 2016 at 12:13 PM, dirkjso...@gmail.com
 wrote:
> Hi bob,
>
> I had used the wrong email address when I wrote to python tutor on this, and
> evidently it
> took a while for the monitors to let it go ahead and be entered in the list,
> so I apologize
> for the delay in my reply.

That's okay.  Note that Tutor is a collaborative learning environment,
so that normally you should send your responses to the whole list, not
just me.  That way we can all learn from what is written.  So I am
sending this to the Tutor list, but not everything as I notice you
sent another response with pretty much everything to Alan and Tutor.

> I continued to mess with it, and it seems or appears that escape '\' was
> being noticed by
> the interpreter despite the fact it is in a triple quoted area!
>
> As for the traceback, (I am not sure what you mean by 'FULL TRACEBACK'),
> there was no
> highlighted point in the program. I simply got the message I printed here.

I am back at home now with access to Python 3.  What I meant was, if I
have this Python file, test_traceback.py:

#!/usr/bin/env python3

print('''I will deliberately use a backslash inside this triple-quote.\''')

When I run this code from the command line, I get:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>e:

E:\>cd /py_projects

E:\py_projects>py test_traceback.py
  File "test_traceback.py", line 5

^
SyntaxError: EOF while scanning triple-quoted string literal

This is what I mean by a full traceback.  Notice it tells me what line
of my code file where it first encountered a problem.  Just so you
have it in mind, with things involving quotes issues, the actual point
of error might be several lines earlier.  But in this case I kept
things simple to illustrate the point.

Now if you ran my one-line program in the Python interpreter, you
would get a different kind of problem:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
py3: print('''I will deliberately use a backslash inside this triple-quote.\''')
...
...
...
...
...

Notice that every time I hit return in the interpreter, trying to
*run* the code, it would not do so.  Instead, every time I press
 I get three dots.  This is because in my actual code it sees
the second triple-quote as being two single-quotes and a parenthesis.
It does not see the triple-quote as yet being completed, and,
additionally, it does not see the print function as being closed.  So
if I do these completions, I get:

py3: print('''I will deliberately use a backslash inside this triple-quote.\''')
...
...
...
...
... '
...
...
... '''
... )
I will deliberately use a backslash inside this triple-quote.''')




'



py3:

Notice the extra single-quote I threw in.  I hope it shows in more
detail what is going on.

HTH!

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Regular Expressions

2016-05-30 Thread Alan Gauld via Tutor
On 30/05/16 18:21, dirkjso...@gmail.com wrote:

> I moved my notes that contained any '\'s to a different python file.
> However, if we run it, we get the error I was having. Here's the
> script:

Runs fine for me.

Can you run it using the python command line interpreter rather
than IDLE? Do you still get errors? If so cut n paste the full
error to the list.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Regular Expressions

2016-05-30 Thread dirkjso...@gmail.com

Thanks Bob,





OK. The escape character \ still is active in escaping the next 
character or space when inside of triple quotes. So, I guess when the 
program is running, since I am not printing these things out, I don't 
care if anything in my notes is escaped unless it is a strategic single 
or double quotethus disrupting the bubble of the comment formed by 
such triple quotes.


Speaking of single and double quotes, I noticed that I had used two 
single ' to surround some things, so I changed them to double quotes so 
as not to confuse my triple quotes. Then I noticed that I had also used 
contractions with ' apostrophes which would also mess up my comment 
bubble, so I got rid of those also. As far as I can tell, the triple 
single quotes (''') should work at that point. In fact, they do if I 
wrap this whole section in a print( ) statement and paste it into IDLE 
SHELL. But! I found as soon as I had IDLE run my script I got the same 
error.


Here is where I am at now with this script:   (NOTE:  I have the final 
solution below this listing.)


#!/usr/bin/env python3

'''
Regular Expressions - or at least some

Identifiers:

\d  any number
\D  anything but a number (digit)
\s  space
\S  anything but a space
\w  any character
\W  anything but a character
\.   any character (or even a period itself if you use \.) except for a 
newline

\a   search for just the letter "a"
\b  the white space around words

Modifiers
{x}we are expecting "x" number of something
{1, 3}  we are expecting 1-3 in length of something -, so for digits we 
write  \d{1-3}

+  means Match 1 or more
?  means Match 0 or 1
*   Match 0 or more
$  Match the end of a string
^  Match the beginning of a string
|   Match either or   - so you might write  \d{1-3} | \w{5-6}
[ ]  a range or "variance" such as [A-Z] or [A-Za-z] Cap 1st letter 
followed by lower case
or [1-5a-qA-Z] starts with a number inclusive of 1-5 then 
lower case letter then

followed by any Cap letter! :)

White Space Characters  (may not be seen):
\n  new line
\s  space
\t   tab
\e  escape
\f  form feed
\r  return

DO NOT FORGET!:
.  +  *  ?  [  ]  $  ^  (  )  {  }  |  \if you really want to use 
these, you must escape them


'''

I was not getting a line number for the error because I was running it 
only from IDLE and I was looking only at the commented area...as that 
now was the only thing left in the scriptor was it? It hadn't 
occurred to me to try the linux command line.  When I called it from the 
Linux Console, I got:


[justme@ispy] ~/python_work$ python3 RE-1.py
  File "RE-1.py", line 69

^
SyntaxError: EOF while scanning triple-quoted string literal
[justme@ispy] ~/python_work$

Which puzzled me, as I didn't have 69 lines!  And guess what I found 
when I scrolled down past the end of my script? Way down out of sight 
below my program was  a single  ''' setting down there all by it's 
lonesome self!  -a fragment of me changing things all over the place 
trying to figure out what was going wrongit had slipped below my 
line of sight and with further chances continued to work it's way down 
the page.


I think all these fixes...this solves my problem!  Thanks for your time 
and help!  :)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread Alan Gauld via Tutor
On 30/05/16 06:45, Steve Lett wrote:

> Out of a long list of books that I have collected ( python, Blender, etc )
> I have decided to start on Introducing Python (hard copy 2nd Ed, ebook 3rd
> ed) by Bill Lubanovic. Before that I was going to start with Python
> Programming for the Absolute Beginner, Michael Dawson.

Bob has mentioned many useful bits of advice.

I'd just emphasise one thing:

write code., lots of it.

Don't just settle for the examples/exercises in your book.
Use them as a start but extend them. Add extra features.
Change the output format or the sort order.
Combine examples to make bigger programs.

Writing code means making mistakes and, in finding the solution,
you learn far more than from just reading code.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread David Rock
* Alan Gauld via Tutor  [2016-05-30 22:11]:
> On 30/05/16 06:45, Steve Lett wrote:
> 
> write code., lots of it.
> 
> Don't just settle for the examples/exercises in your book.
> Use them as a start but extend them. Add extra features.
> Change the output format or the sort order.
> Combine examples to make bigger programs.
> 
> Writing code means making mistakes and, in finding the solution,
> you learn far more than from just reading code.

And a corollary to this: have a purpose for why you are writing it.

Learning code for the sake of learning it will get old quickly.  You will get a
lot further if you are trying to solve a problem that you care about.  Think of
something you would like to automate, or calculate, or process.  Do you have
data you would like to analyze? As you learn different elements and apply them
to a practical use that does something for you, it will be more satisfying and
more likely to stick in your brain.

-- 
David Rock
da...@graniteweb.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread Alex Kleider


On 2016-05-30 12:02, boB Stepp wrote:
...

Are you totally new to programming in *any* language?  If yes, you
have much more to learn than *just* a programming language.  I am
going to assume that you are very new to programming in general.
Forgive me if I am mistaken!  But if you are, then some of the things
you must learn that are language-independent:



With the above comments in mind, this might be worth looking at:
http://openbookproject.net/thinkcs/python/english3e/

I cut my teeth on the original version which was Python 2.7 based and
was just the thing meeting the criteria mentioned by Bob.
I assume the Python 3 version has the same merits.
Best wishes,
Alex

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread Colby Christensen
I completely agree with what's been said. I also have used online learning 
sites like Coursera, Udacity and Lynda. There's something about being able see, 
hear and do that clicks for me.
Good Luck
Colby

> From: david.r...@gmail.com
> Date: Mon, 30 May 2016 18:07:57 -0500
> To: tutor@python.org
> Subject: Re: [Tutor] Study Tips
> 
> * Alan Gauld via Tutor  [2016-05-30 22:11]:
>> On 30/05/16 06:45, Steve Lett wrote:
>> 
>> write code., lots of it.
>> 
>> Don't just settle for the examples/exercises in your book.
>> Use them as a start but extend them. Add extra features.
>> Change the output format or the sort order.
>> Combine examples to make bigger programs.
>> 
>> Writing code means making mistakes and, in finding the solution,
>> you learn far more than from just reading code.
> 
> And a corollary to this: have a purpose for why you are writing it.
> 
> Learning code for the sake of learning it will get old quickly. You will get a
> lot further if you are trying to solve a problem that you care about. Think of
> something you would like to automate, or calculate, or process. Do you have
> data you would like to analyze? As you learn different elements and apply them
> to a practical use that does something for you, it will be more satisfying and
> more likely to stick in your brain.
> 
> -- 
> David Rock
> da...@graniteweb.com
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Study Tips

2016-05-30 Thread Steve Lett
Thank you for the reply.
On 30/05/2016 3:45 PM, "Steve Lett"  wrote:

> Hi folks,
> Just started learning python. I've been having a really hard time in
> getting started, and still am! I have a slight learning difficulty,
> including a stroke in Jan.2010. You wouldnt know even if u were here
> looking at me! Praise God for the great salvation!
> I have a slight learning difficulty. Has anyone got any tips on how to
> study programming and what approach would be best for me?
>
> Out of a long list of books that I have collected ( python, Blender, etc )
> I have decided to start on Introducing Python (hard copy 2nd Ed, ebook 3rd
> ed) by Bill Lubanovic. Before that I was going to start with Python
> Programming for the Absolute Beginner, Michael Dawson.
>
> Any thoughts on these issues and especially the study tips already
> mentioned.
>
> Thank you, Steve
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Regular Expressions

2016-05-30 Thread Terry--gmail

Thanks Alan

I noticed that I was using some double ' to encircle some things and 
some single ' for apostrophes in contractionsand fixed those...but 
apparently since you could run it, that part didn't matter. The problem 
was ultimately caused by a stray ''' which was a fragment of me messing 
with things trying to fix them and it slipped down my screen and was 
hidden from me when I would look at the script!


Thanks for double checking me. :)


On 05/30/2016 03:07 PM, Alan Gauld via Tutor wrote:

On 30/05/16 18:21, dirkjso...@gmail.com wrote:


I moved my notes that contained any '\'s to a different python file.
However, if we run it, we get the error I was having. Here's the
script:

Runs fine for me.

Can you run it using the python command line interpreter rather
than IDLE? Do you still get errors? If so cut n paste the full
error to the list.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of model-view-controller design pattern

2016-05-30 Thread boB Stepp
On Sun, May 29, 2016 at 9:10 AM, Alan Gauld via Tutor  wrote:
> On 29/05/16 05:33, boB Stepp wrote:

>> My current understanding is that the model contains program logic and
>> the data which the program logic manipulates.
>
> Correct, all the core entities which make up the program are represented
> by models. Most things stored in a database will
> have a corresponding model.

Ah, the first major misconception I have.  Until now I have been
thinking more in terms of a single overarching model.  From what you
say here (and elsewhere in your response) one might have a Customer
class/model, account class/model, etc., all of which map to "things
stored in a database".  Later you mention lower-level models, that I
presume are classes with lower level abstraction, and I suppose there
are higher level models which provide higher levels of abstraction,
hiding these lower level models/classes.  I chose a procedural
approach for my coding examples as that both comes most naturally in
my current stage of development and seemed simplest.  But it is
looking like MVC is most useful for an OOP approach to things.

>> The controller ties things together, refreshing the view appropriately,
>> receiving messages from the view when something there has
>> been changed by the user, interrogating the model when needed,
>> and receiving the model's results and refreshing the view
>> appropriately.
>
> The controller is where the confusion starts. Every implementation of
> the MVC pattern seems to change the way the controller is defined. You
> are broadly correct but some frameworks have a more view focussed
> interpretation, others more model focused. For example some GUI
> frameworks combine the view and controller concepts into a single Window
> object (the Delphi/VB/MFC/OWL Windows GUI frameworks all
> take that approach). The JSP type 1 framework is similar in that
> the JSP page does both roles. But in the JSP type 2 framework the
> controller is separated out into a distinct entity.

This is what motivated to post my questions.  I wanted an explanation
of why I could not get consistent explanations of MVC from the
different sources I read.  As usual I am thinking about things too
rigidly.

> What everybody(?!) seems to agree on is that events generated by the
> view are forwarded to a controller(*) which determines whether a model
> needs to be involved and is so what method of which model needs to
> be invoked.
>
> (*)If the controller is embedded in the view object then that is
> probably handled by an internal  superclass method of the framework
> and based on an event table (similar to Tkinter's binding mechanism)
>
> The model responds, but how the view is notified varies. In some cases
> the views register with models and the model automatically sends
> notification messages to all registered views (by-passing the
> controller). In other cases the model si8mply sends a reply to the
> controller that invoked it and the controller decides which views should
> be updated.

So there can be multiple controllers, too?  And I suppose at different
levels of abstraction to match the correspondingly available models at
different levels of abstraction?

> In practice it's hard to separate the controller and view
> entirely (which is why some frameworks combine them) but
> it should definitely be possible to separate the models
> from the more UI elements. What the controller does do
> is allow you to swap different views within a single UI.
> For example a list view, versus a form view versus a
> graphical view, all of the same object or set of objects.

I was intuitively suspecting this.

> But I'd probably not make the controller the main() function.
> Instead I'd have a separate main that constructed the objects.
> The controller then has a handle_event() type method that
> receives the event from the UI view.
>
> So you make the view responsible for initiating the
> event processing not the controller. The controller
> then becomes a fairly simple validate-dispatch mechanism.

This makes more sense to me.  When I was trying to imagine writing a
tkinter GUI to be the UI I could not visualize how to make the view
totally decoupled from the controller code.  Along with what you said
earlier this is all making a lot more sense now.

>> anything or missing any nuances?  Would this code scale upward as I
>> would hope?
>
> It would be a bit messy but could be adapted quite easily.
> For example instead of the if/elif chain use a dictionary
> for event dispatch.

What would be the "messy" issues?  I suspect these are the things that
I am not intuitively seeing and only realize retrospectively after I
get deep into coding.

Thanks!

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of model-view-controller design pattern

2016-05-30 Thread boB Stepp
On Mon, May 30, 2016 at 11:30 AM, Alan Gauld via Tutor  wrote:
> On 29/05/16 05:33, boB Stepp wrote:

>> 1)  If I were to add code to filter user inputs, which file is the
>> logical place to place such code?  My current impression is that it
>> should go in the controller, but I could see it being in the view.
>> However, if I were to write a Validation class, I could see that as
>> having uses beyond checking just user input.
>
> Filtering user input is one type of validation (others include
> type and value checking) Usually the view (especially in a GUI)
> will control the type checking and in  some cases the value
> checking too (think calendar pickers). But the controller may
> also do some data adjustment (such as reformatting a date
> to suit a particular model). But mostly I expect the view
> to collect the correct data in terms of primitive type/value.

I perhaps see a general principle developing here:  Before passing
data elsewhere, the data should be validated and in a form suitable
for where it is being sent.  In my simple code example, I was trying
to do this:  Change the diameter, which was currently in a string
format, to float prior to sending it to the model.  And likewise,
changing diameter and circumference back to strings prior to being
sent to the view.

> An example where more complex validation might be required is
> where you want to validate a credit card or an account number.
> In that case the controller may call a business service before
> passing the validated details onto the appropriate model(s)
> to process.

By "business service" are you intending this to mean a totally
different application, or just a different model within the same
overall program?

> Models should usually expect to be given good data. In some
> cases you might want to do a belt n' braces data check in
> the model too, but mostly you assume your front end is
> catching the bad stuff (see file processing below).

I take it "braces" are what we call "suspenders" in the US?  So what
you are talking about is redundant validation?  When would this be
actually called for as it seems to contradict DRY principles?  The
only thing that is coming to my mind now is code for something where
failure is not an option and the source of the incoming data (Which
should be doing proper validation.) was coded by someone else.  If I
were coding both myself, then I would think that I should be able to
trust that I did the job correctly the first time!


>> 3)  If I were to add data files in place of (or in addition to) user
>> input, in which aspect of MVC would file I/O be handled?
>
> This is where things get interesting. When processing files the
> view/controller are only likely to be validating the filename and
> initiating the process. This means that there needs to be a model object
> somewhere that processes the file. But the content of the
> file is unsafe  so that model needs to do all the data
> validation that would normally be done in the view/controller.
>
> Its not unusual to have a dedicated model for such batch processing
> and it will then call the other model methods for each record processed,
> effectively becoming a controller of sorts. There may
> be opportunities for code reuse between the UI controller and
> the batch controller.

I guess what I am getting out of this discussion of MVC, is to not be
too rigid in my thinking, but try to decouple these three broad areas
of responsibility as much as is practical, so as to make the resulting
code more maintainable and scalable.  Is this the correct big picture
idea?

Thanks!

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Correct use of model-view-controller design pattern

2016-05-30 Thread boB Stepp
One other thing I meant to ask about:

On Sun, May 29, 2016 at 9:10 AM, Alan Gauld via Tutor  wrote:

> The model responds, but how the view is notified varies. In some cases
> the views register with models and the model automatically sends
> notification messages to all registered views (by-passing the
> controller). In other cases the model si8mply sends a reply to the
> controller that invoked it and the controller decides which views should
> be updated.

This "views register with models" and "registered views" terminology
is unfamiliar to me.  Googling suggests registered views is referring
to a dynamically generated presentation, perhaps in the sense of a db
view.  I suspect I have an idea of what is being meant here, but
instead of potentially guessing, would you care to elaborate?

Thanks!

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor