Re: [Tutor] OOP help needed

2016-07-27 Thread Peter Otten
Jim Byrnes wrote:

> OOP has always driven me crazy.  I read the material and follow the
> examples until I feel I understand them, but when I try to implement it
> I end up with an error filled mess.
> 
> So I decided to give it another try.  When I got to the chapter on
> tkinter I decided to solve all the exercises using OOP even though the
> book solutions did not use OOP. The first one went fine:

No, it didn't. The Goodbye.quit() method is missing the self argument and 
uses the inexistent self.window attribute.
You don't see these bugs when you run the script because there is a global 
quit()... let's say function... that is called instead of the method. 

You can put a print() into Goodbye.quit() to verify the above.
 
> #exer1.py
> 
> import tkinter
> 
> class Goodbye:
>def __init__(self):
> 
>  self.frame = tkinter.Frame(window)
>  self.frame.pack()
> 
>  self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
>#command=quit)
>command=lambda: quit() )

The lambda is superfluous -- command=quit will already invoke the global 
quit(). But what you actually intended is achieved with command=self.quit.
self.quit is called "bound method".

>  self.goodbye_button.pack()
> 
>def quit():
   print("you'll never see this")
>  self.window.destroy()
> 
> if __name__=='__main__':
>window = tkinter.Tk()
>myapp = Goodbye()
>window.mainloop()


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


[Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 1

2016-07-27 Thread kanishk srivastava
Hello,

I am working through Michael Dawson's book - Python Programming for
absolute beginners 3e.

Completed chapter 3, but unable to solve challenge 1.

Below code is what I have written but unsure on how to have the program
generate the message at random. Any help will be appreciated.

# Fortune Cookie
# Demonstrates random message generation


print("\t\tFortune Cookie")
print("\t\tWelcome user!")

# fortune messages
message1 = "the earth is a school learn in it."
message2 = "be calm when confronting an emergency crisis."
message3 = "you never hesitate to tackle the most difficult problems."
message4 = "hard words break no bones, fine words butter no parsnips."
message5 = "Make all you can, save all you can, give all you can."

import random


print("Your today's fortune  ")

input("\n\nPress the enter key to exit")


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


Re: [Tutor] OOP help needed

2016-07-27 Thread Alan Gauld via Tutor
On 27/07/16 04:44, Jim Byrnes wrote:
> OOP has always driven me crazy.  I read the material and follow the 
> examples until I feel I understand them, but when I try to implement it 
> I end up with an error filled mess.

That suggests that its not the OOP concept thats confusing
you but the language syntax. How to turn the concept into code?

> So I decided to give it another try.  When I got to the chapter on 
> tkinter I decided to solve all the exercises using OOP even though the 
> book solutions did not use OOP. The first one went fine:

Actually not as fine as you thought. In effect you got lucky by
making a mistake that still resulted in your code doing
approximately what you expected. But it didn't really do
what you thought it did.

> import tkinter
> 
> class Goodbye:
>def __init__(self):
> 
>  self.frame = tkinter.Frame(window)
>  self.frame.pack()

You are using a global variable as your parent here. It would be
better to pass that in as an argument. Or better still to make
the call to Tk() inside the __init__ method. That's not really
an OOP thing though just a general good practice issue.
It's best to avoid relying on global variables in your
functions.

>  self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
>#command=quit)
>command=lambda: quit() )
>  self.goodbye_button.pack()

Here you assign quit to the button's command. That's OK because
there is a top level built-in function called quit which exits
the interpreter. It's a bit of a brutal way to exit your GUI
but it works.

But I guess you really wanted to call your quit method. Remember
to access anything in your class you have to use the self
prefix, so you should have said:

command=self.quit

or

command=lambda: self.quit()

Lambda doesn't really help in this case but it doesn't do
any harm either.

>def quit():
>  self.window.destroy()

When you define a method inside a class you need to
explicitly include the self parameter. So this should be:

def quit(self):
  self.window.destroy()

But there's a snag, you don't store the window inside the
class. So self.window will cause an error. You either need
a line like

self.window = window

in your__init__ method

or use the global window variable like

def quit():
  window.destroy()

My preference would be to create a self.window instance variable,
inside init()then access the self.window in quit(). You would also
call mainloop() using self.window in your init()

> if __name__=='__main__':
>window = tkinter.Tk()
>myapp = Goodbye()
>window.mainloop()

So if you took my advice this section of code would look like:

if __name__=='__main__':
Goodbye()


and init() would look like:

def __init__(self):
 self.window = tkinter.Tk()
 self.frame = tkinter.Frame(self.window)
 self.frame.pack()

 self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
  command=self.quit)
 self.goodbye_button.pack()

 self.window.mainloop()

If you read through that and understand it, it should give
you the clues as to why the second one behaved as it did.


-- 
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] Python Programming for the absolute beginner 3e Ch3 Challenge 1

2016-07-27 Thread Alan Gauld via Tutor
On 27/07/16 02:39, kanishk srivastava wrote:
> Hello,
> 
> I am working through Michael Dawson's book - Python Programming for
> absolute beginners 3e.
> 
> Completed chapter 3, but unable to solve challenge 1.

I don't know the book so don't know how much you
know yet. So thee are some suggestions below that
might not make sense to you because you haven't
covered the material. Apologies in advance!

> print("\t\tFortune Cookie")
> print("\t\tWelcome user!")
> 
> # fortune messages
> message1 = "the earth is a school learn in it."
> message2 = "be calm when confronting an emergency crisis."
> message3 = "you never hesitate to tackle the most difficult problems."
> message4 = "hard words break no bones, fine words butter no parsnips."
> message5 = "Make all you can, save all you can, give all you can."

Have you covered lists? Rather than a set of numbered messages you could
put them in a list and access each message by index:

messages = ["the earth is a school learn in it.",
"be calm when confronting an emergency crisis.",
"you never hesitate to tackle the most difficult problems.",
"hard words break no bones, fine words butter no parsnips.",
"Make all you can, save all you can, give all you can."
   ]

And access them with

messages[0], messages[1], ... messages[4]

Although in this case you don't even need to do that,
read on...

> import random

I don't know how many of the random module functions you cover
but there are a lot of them. One of them, random.choice(aList),
selects a random item from a list. So now that you have a list
of messages it becomes trivial to select a random message from
the list:

> print("Your today's fortune  ")

use print(random.choice(messages)) here

hth
-- 
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] OOP help needed

2016-07-27 Thread Jim Byrnes

On 07/26/2016 11:38 PM, Ben Finney wrote:

Jim Byrnes  writes:


So I decided to give it another try. When I got to the chapter on
tkinter I decided to solve all the exercises using OOP even though the
book solutions did not use OOP.


Hmm, that sounds ill advised.

OOP is one tool among many; trying to apply it where it's a poor fit
will result in bad design and probably avoidable errors.

When learning to use a hacksaw, trying to solve everything using that
tool merely to learn it, would be a poor choice.


With anything more complex I would agree.  I am simply trying to get 
myself thinking about how OOP works and there aren't enough exercises in 
the book calling for OOP to give me much repetition.



# exer2.py

import tkinter

class Count:

  def __init__(self):
''' Increment a button labeled 0, by 1 with each click '''
[…]
self.count_btn = tkinter.Button(self.frame, textvariable=self.label,
  command=lambda: self.increment(self.label ))


Here you address the ‘self.increment’ name, which should work.


  def increment(self, label):
count = int(self.label.get())
self.label.set(str(count + 1))


This is the method that an instance will address via ‘self.increment’.


In exer2.py if I do command=lambda: increment(self.label)


The lambda expression creates a function, and that function then behaves
like any other function. Within that function, the name ‘increment’ is
not defined; within the scope where the function was defined, the name
‘increment’ is also not defined. Within the global scope the name
‘increment’ is not defined.

So yes, you'll get NameError from that code.


Why do I get this error?  The situations look the same to me


The difference is that when you invoke ‘self.instance’, the lookup of
‘self’ succeeds because it's defined within the function (you defined it
in the parameters of ‘__init__’, so ‘__init__’ knows that name when it
is running).

You never defined the name ‘increment’ within the function, nor within
the global scope. And you shouldn't because there's no need: you access
the instance's own method by accessing the instance first: you ask for
“the ‘instance’ attribute from the ‘self’ object”: ‘self.instance’.



OK thank you.

Regards,  Jim


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


Re: [Tutor] OOP help needed

2016-07-27 Thread Jim Byrnes

On 07/27/2016 03:12 AM, Peter Otten wrote:

Jim Byrnes wrote:


OOP has always driven me crazy.  I read the material and follow the
examples until I feel I understand them, but when I try to implement it
I end up with an error filled mess.

So I decided to give it another try.  When I got to the chapter on
tkinter I decided to solve all the exercises using OOP even though the
book solutions did not use OOP. The first one went fine:


No, it didn't. The Goodbye.quit() method is missing the self argument and
uses the inexistent self.window attribute.
You don't see these bugs when you run the script because there is a global
quit()... let's say function... that is called instead of the method.

You can put a print() into Goodbye.quit() to verify the above.


OK right.  I ended up concentrating on exer2 when the problem was in 
exer1.  I should have known better than using quit() as a name.



#exer1.py

import tkinter

class Goodbye:
   def __init__(self):

 self.frame = tkinter.Frame(window)
 self.frame.pack()

 self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
   #command=quit)
   command=lambda: quit() )


The lambda is superfluous -- command=quit will already invoke the global
quit(). But what you actually intended is achieved with command=self.quit.
self.quit is called "bound method".


Ok, thanks.


 self.goodbye_button.pack()

   def quit():

   print("you'll never see this")

 self.window.destroy()

if __name__=='__main__':
   window = tkinter.Tk()
   myapp = Goodbye()
   window.mainloop()




Regards,  Jim


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


Re: [Tutor] OOP help needed

2016-07-27 Thread Jim Byrnes

On 07/27/2016 04:04 AM, Alan Gauld via Tutor wrote:

On 27/07/16 04:44, Jim Byrnes wrote:

OOP has always driven me crazy.  I read the material and follow the
examples until I feel I understand them, but when I try to implement it
I end up with an error filled mess.


That suggests that its not the OOP concept thats confusing
you but the language syntax. How to turn the concept into code?


That's exactly my problem, which is why I am solving problems with OOP 
when it's not necessary.  I wanted the practice.



So I decided to give it another try.  When I got to the chapter on
tkinter I decided to solve all the exercises using OOP even though the
book solutions did not use OOP. The first one went fine:


Actually not as fine as you thought. In effect you got lucky by
making a mistake that still resulted in your code doing
approximately what you expected. But it didn't really do
what you thought it did.


import tkinter

class Goodbye:
   def __init__(self):

 self.frame = tkinter.Frame(window)
 self.frame.pack()


You are using a global variable as your parent here. It would be
better to pass that in as an argument. Or better still to make
the call to Tk() inside the __init__ method. That's not really
an OOP thing though just a general good practice issue.
It's best to avoid relying on global variables in your
functions.


Ok thanks.  When I wrote that I was mimicking the style used in the 
book. I have read about avoiding globals if possible, but didn't think 
it through.



 self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
   #command=quit)
   command=lambda: quit() )
 self.goodbye_button.pack()


Here you assign quit to the button's command. That's OK because
there is a top level built-in function called quit which exits
the interpreter. It's a bit of a brutal way to exit your GUI
but it works.

But I guess you really wanted to call your quit method. Remember
to access anything in your class you have to use the self
prefix, so you should have said:

command=self.quit

or

command=lambda: self.quit()

Lambda doesn't really help in this case but it doesn't do
any harm either.


   def quit():
 self.window.destroy()


When you define a method inside a class you need to
explicitly include the self parameter. So this should be:

def quit(self):
  self.window.destroy()

But there's a snag, you don't store the window inside the
class. So self.window will cause an error. You either need
a line like

self.window = window

in your__init__ method

or use the global window variable like

def quit():
  window.destroy()

My preference would be to create a self.window instance variable,
inside init()then access the self.window in quit(). You would also
call mainloop() using self.window in your init()


if __name__=='__main__':
   window = tkinter.Tk()
   myapp = Goodbye()
   window.mainloop()


So if you took my advice this section of code would look like:

if __name__=='__main__':
Goodbye()


and init() would look like:

def __init__(self):
 self.window = tkinter.Tk()
 self.frame = tkinter.Frame(self.window)
 self.frame.pack()

 self.goodbye_button = tkinter.Button(self.frame, text='Goodbye',
  command=self.quit)
 self.goodbye_button.pack()

 self.window.mainloop()

If you read through that and understand it, it should give
you the clues as to why the second one behaved as it did.




Ok thanks.  I don't want to belabor the point but I basically had it 
that way because I didn't know any better. Now I know of a 
different/better way to do it.


Regards,  Jim

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


Re: [Tutor] python programmin problem

2016-07-27 Thread Danny Yoo
On Sun, Jul 24, 2016 at 4:30 PM, monik...@netzero.net
 wrote:
> Thank you all for your answers. I do not have a teacher or any body else who 
> could guide me. I have taken all python classes offered in my area and many 
> on line.
> The question is one of questions asked by interviews for a qa position that 
> also does some automation with python.


If you're training for problems like this, then look for something
less focused on a specific programming language.  As we said earlier,
solving these kinds of problems isn't really a matter of Python
programming at this level: you want to look specifically for material
about algorithms.


For this kind of thing, definitely look into something like the
"Develop a strong understanding of algorithms and data structures"
recommendations at:


https://www.google.com/about/careers/students/guide-to-technical-development.html


I've also heard very good things about Skiena's "The Algorithm Design Manual":

http://www.algorist.com/

The author has a bunch of videos and lecture material online that
might help you:

http://www3.cs.stonybrook.edu/~algorith/video-lectures/

In particular, see Lectures 18 and 19.

*Especially Lecture 19*.


You will see a problem being explained that you should be *very*
interested in.  :)  Here's a link just to give you a taste:

https://youtu.be/0yjebrZXJ9A?t=3m


I hope that this helps point you in the right direction.  Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python programmin problem

2016-07-27 Thread Danny Yoo
> You will see a problem being explained that you should be *very*
> interested in.  :)  Here's a link just to give you a taste:
>
> https://youtu.be/0yjebrZXJ9A?t=3m
>
>
> I hope that this helps point you in the right direction.  Good luck!


He has some more recent videos from 2012:

 https://youtu.be/Qc2ieXRgR0k?list=PLOtl7M3yp-DV69F32zdK7YJcNXpTunF2b

where he again goes over the familiar example in:

https://youtu.be/o0V9eYF4UI8?t=34m52s
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor