Re: [Tutor] OrderedDict?

2016-06-02 Thread Alex Hall
Thanks. I've never used that module before, and didn't realize it's not 
imported by default, which must be why my first tries failed. I've got it 
working now.
> On Jun 1, 2016, at 18:45, Alan Gauld via Tutor  wrote:
> 
> On 01/06/16 16:36, Alex Hall wrote:
> 
>> I'm trying to find the OrderedDict documentation. I found one page, but it
>> wasn't very clear on how to actually make an OrderedDict. Plus, an empty
>> constructor in Python's interpreter returns an error that the class doesn't
>> exist
> 
> It's in the collections module:
> 
 import collections as coll
 help(coll.OrderedDict)
> ...
 od = coll.OrderedDict()
 od
> OrderedDict()
 
> 
> 
> 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

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



[Tutor] Tkinter and threading

2016-06-02 Thread Marco Soldavini
Hello,
probably this is a very naive question, but I've read some stuff on
Tkinter and its infinite loop.

Then about how can i bind actions to elements, for example buttons.

What if I want to run another loop beside the graphical interface in
the same python script?

For example a state machine with a var state which can have some
discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE)
and a text element on the gui that reports that state.

So a button cause a transition > the python loop (not the gui loop)
detects the transition and changes the state var value > the gui
refresh its value on screen.

I wrote some code to try it without threading but it does not give the
expected result as it seems the button update status action is already
triggered. I am missing some basic point here

from Tkinter import *

state = "STOPPED"

root = Tk()

myContainer1 = Frame(root)
myContainer1.pack()
label1=Label(myContainer1, text=state, font = "Helvetica 16 bold italic")
label1.pack()

def change(new_state):
label1.config(text=new_state)

button1 = Button(myContainer1, text="START")
button1.bind("", change("START"))
button1.pack()
button2 = Button(myContainer1)
button2["text"]= "STOP"
button2.pack()
root.mainloop()


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


Re: [Tutor] Tkinter and threading

2016-06-02 Thread Alan Gauld via Tutor
On 02/06/16 14:40, Marco Soldavini wrote:

> What if I want to run another loop beside the graphical interface in
> the same python script?

You need to do it in a separate thread.
Keep the Tkinter loop on your main thread and use it to trigger
actions.

> For example a state machine with a var state which can have some
> discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE)
> and a text element on the gui that reports that state.

A state machine should not need a loop! It should be triggered
by state changes alone. ie state should only be changed by
calling a function. That function sets the state value
and calls any processing function associated with the
transition. (Whether before or after you set value depends
on whether you are using Moore or Mealy semantics, in
practice it makes little difference.)

You might need a loop to detect (some of the) changes but
setting the state machine should be entirely separate.

> So a button cause a transition > the python loop (not the gui loop)
> detects the transition and changes the state var value > the gui
> refresh its value on screen.

The button generates an event.
The event causes an event-handling function to be called.
That function may well cause a state transition that your state
machine can detect. (But it could just as well call your state
machine directly)

> I wrote some code to try it without threading but it does not give the
> expected result as it seems the button update status action is already
> triggered. I am missing some basic point here

If you want two parallel event loops you pretty much need threads.
Without threads you need to develop some kind of event-driven callback
solution - which may very well be preferable!


-- 
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] Tkinter and threading

2016-06-02 Thread Peter Otten
Marco Soldavini wrote:

> Hello,
> probably this is a very naive question, but I've read some stuff on
> Tkinter and its infinite loop.
> 
> Then about how can i bind actions to elements, for example buttons.
> 
> What if I want to run another loop beside the graphical interface in
> the same python script?

I usually adapt http://effbot.org/zone/tkinter-threads.htm

> For example a state machine with a var state which can have some
> discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE)
> and a text element on the gui that reports that state.
> 
> So a button cause a transition > the python loop (not the gui loop)
> detects the transition and changes the state var value > the gui
> refresh its value on screen.
> 
> I wrote some code to try it without threading but it does not give the
> expected result as it seems the button update status action is already
> triggered. I am missing some basic point here

Indeed your problem has nothing to do with threads.
> 
> from Tkinter import *
> 
> state = "STOPPED"
> 
> root = Tk()
> 
> myContainer1 = Frame(root)
> myContainer1.pack()
> label1=Label(myContainer1, text=state, font = "Helvetica 16 bold italic")
> label1.pack()
> 
> def change(new_state):
> label1.config(text=new_state)
> 
> button1 = Button(myContainer1, text="START")
> button1.bind("", change("START"))

In the line above you invoke the change function and bind its result (None) 
to the button1 widget's  event. Instead you should pass a function 
that will be invoked by Tkinter when the button is pressed:

def start(event):
change("START")

button1.bind("", start)

You can define simple functions inline, so

button1.bind("", lambda event: change("START"))

would also work. However, the usual way to bind actions to Button widgets is 
to pass the callback function as the command argument to the constructor:

button = Button(container, command=your_func_that_takes_no_args)

With that approach your script will become

from Tkinter import *

def change(new_state):
label1.config(text=new_state)

def start():
change("RUNNING")

def stop():
change("STOPPED")

root = Tk()

myContainer1 = Frame(root)
myContainer1.pack()

label1 = Label(myContainer1, font="Helvetica 16 bold italic")
label1.pack()

stop()

button1 = Button(myContainer1, text="Start", command=start)
button1.pack()

button2 = Button(myContainer1, text="Stop", command=stop)
button2.pack()

root.mainloop()




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


[Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Olaoluwa Thomas
Hi Tutor,

I'm trying to parse words in a file line by line and place all words into
another list but I keep getting a list with nested lists.
I would normally pore over it and go to google and fix my problems but this
one escapes me and frankly, I'm tired of being stuck in the same place for
almost a week.

Here's the code:
fname = raw_input('Enter file name:\n')
try:
fhand = open(fname)
except:
print 'File cannot be found or opened:', fname
exit()
lst = list()
for line in fhand:
words = line.split()
#print words (this was a test that a portion of my code was working)
lst.append(words)
print lst

A text file with the following contents
"But soft
what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief"

would give me the output in the attached screenshot
[image: Inline image 2]

whereas I want only one list containing strings not nested lists.

Any help would be appreciated.

*Warm regards,*

*Olaoluwa O. Thomas,*
*+2347068392705*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Alan Gauld via Tutor
On 02/06/16 18:05, Olaoluwa Thomas wrote:

> lst = list()
> for line in fhand:
> words = line.split()

words is now a list of words

 a test that a portion of my code was working)
> lst.append(words)

So you append that list to lst and get a list of lists.
Try using + instead:

lst += words

> would give me the output in the attached screenshot
> [image: Inline image 2]

This is a text based list attachments often get
stripped out by the server

-- 
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] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Steven D'Aprano
On Thu, Jun 02, 2016 at 06:05:43PM +0100, Olaoluwa Thomas wrote:

> fname = raw_input('Enter file name:\n')
> try:
> fhand = open(fname)
> except:
> print 'File cannot be found or opened:', fname
> exit()
> lst = list()
> for line in fhand:
> words = line.split()
> #print words (this was a test that a portion of my code was working)
> lst.append(words)

If you printed words, you should have seen that it was a list.

If you append a list to a list, what do you get? At the interactive 
prompt, try it:


py> L = [1, 2, 3]
py> L.append([4, 5, 6])
py> L
[1, 2, 3, [4, 5, 6]]


Append takes a single argument, and adds it *unchanged* to the end of 
the list.

What you want is the extend method. It takes a list as argument, and 
appends each item individually:


py> L.extend([7, 8, 9])
py> L
[1, 2, 3, [4, 5, 6], 7, 8, 9]


But if you didn't know about that, you could have done it the 
old-fashioned way:

lst = list()
for line in fhand:
words = line.split()
for word in words:
lst.append(word)



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


Re: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Olaoluwa Thomas
Thanks, everyone, for your help.

The objective was to extract all words from each line and place them in a
list IF they didn't already exist in it.
I sorted it out by adding little bits of everyone's suggestions.

Here's the code that fixed it.

fname = raw_input('Enter file name:\n')
try:
fhand = open(fname)
except:
print 'File cannot be found or opened:', fname
exit()
lst = list()
for line in fhand:
words = line.split()
for word in words:
if word in lst:
continue
else:
lst.append(word)
lst.sort()
print lst

I named the file WordExtract.py since it does just that. :)

*Warm regards,*

*Olaoluwa O. Thomas,*
*+2347068392705*

On Thu, Jun 2, 2016 at 6:44 PM, Steven D'Aprano  wrote:

> On Thu, Jun 02, 2016 at 06:05:43PM +0100, Olaoluwa Thomas wrote:
>
> > fname = raw_input('Enter file name:\n')
> > try:
> > fhand = open(fname)
> > except:
> > print 'File cannot be found or opened:', fname
> > exit()
> > lst = list()
> > for line in fhand:
> > words = line.split()
> > #print words (this was a test that a portion of my code was working)
> > lst.append(words)
>
> If you printed words, you should have seen that it was a list.
>
> If you append a list to a list, what do you get? At the interactive
> prompt, try it:
>
>
> py> L = [1, 2, 3]
> py> L.append([4, 5, 6])
> py> L
> [1, 2, 3, [4, 5, 6]]
>
>
> Append takes a single argument, and adds it *unchanged* to the end of
> the list.
>
> What you want is the extend method. It takes a list as argument, and
> appends each item individually:
>
>
> py> L.extend([7, 8, 9])
> py> L
> [1, 2, 3, [4, 5, 6], 7, 8, 9]
>
>
> But if you didn't know about that, you could have done it the
> old-fashioned way:
>
> lst = list()
> for line in fhand:
> words = line.split()
> for word in words:
> lst.append(word)
>
>
>
> --
> Steve
> ___
> 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] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Oscar Benjamin
On 2 June 2016 at 19:19, Olaoluwa Thomas  wrote:
> Thanks, everyone, for your help.
>
> The objective was to extract all words from each line and place them in a
> list IF they didn't already exist in it.
> I sorted it out by adding little bits of everyone's suggestions.

Well done. It looks like you have it working now which is good.

Now that you've told us about the extra bit that you only wanted to
store *unique* words I thought I'd tell you about Python's set data
type. A set is a container similar to a list but different. You can
create a set by using curly brackets {} rather than square brackets []
for a list e.g.:

>>> myset = {3,2,6}
>>> myset
set([2, 3, 6])

Notice first that when we print the set out it doesn't print the
elements in the same order that we put them in. This is because a set
doesn't care about the order of the elements. Also a set only ever
stores one copy of each item that you add so

>>> myset.add(-1)
>>> myset
set([2, 3, -1, 6])
>>> myset.add(6)
>>> myset
set([2, 3, -1, 6])

The add method adds an element but when we add an element that's
already in the set it doesn't get added a second time: a set only
stores unique elements (which is what you want to do). So you wrote:

> lst = list()
> for line in fhand:
> words = line.split()
> for word in words:
> if word in lst:
> continue
> else:
> lst.append(word)
> lst.sort()
> print lst

Using a set we could instead write:

unique_words = set()
for line in fhand:
words = line.split()
for word in words:
unique_words.add(word)
lst = sorted(unique_words)
print lst

This is made simpler because we didn't need to check if the word was
already in the set (the set.add method takes care of this for us).
However since a set doesn't have an "order" it doesn't have a sort
method. If we want a sorted list we can use the sorted function to get
that from the set.

Also there is a set method "update" which can add many elements at
once given e.g. a list like words so we can do:

unique_words = set()
for line in fhand:
words = line.split()
unique_words.update(words)
lst = sorted(unique_words)
print lst

I've mentioned two big differences between a set and a list: a set is
unordered and only stores unique elements. There is another
significant difference which is about how long it takes the computer
to perform certain operations with sets vs lists. In particular when
we do this

  if word in lst:
continue
else:
lst.append(word)

Testing if word is "in" a list can take a lot longer than testing if
it is "in" a set. If you need to test many times whether different
objects are "in" a list then it can often make your program a lot
slower than if you used a set. You can understand this intuitively by
thinking that "if word in lst" requires the computer to loop through
all items of the list comparing them with word. With sets the computer
has a cleverer way of doing this that is much faster when the set/list
is large (look up hash tables if you're interested).


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


Re: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop

2016-06-02 Thread Alex Hall
Use lst.extend() instead of lst.append() and you should get what you're
after.

On Thu, Jun 2, 2016 at 1:05 PM, Olaoluwa Thomas 
wrote:

> Hi Tutor,
>
> I'm trying to parse words in a file line by line and place all words into
> another list but I keep getting a list with nested lists.
> I would normally pore over it and go to google and fix my problems but this
> one escapes me and frankly, I'm tired of being stuck in the same place for
> almost a week.
>
> Here's the code:
> fname = raw_input('Enter file name:\n')
> try:
> fhand = open(fname)
> except:
> print 'File cannot be found or opened:', fname
> exit()
> lst = list()
> for line in fhand:
> words = line.split()
> #print words (this was a test that a portion of my code was working)
> lst.append(words)
> print lst
>
> A text file with the following contents
> "But soft
> what light through yonder window breaks
> It is the east and Juliet is the sun
> Arise fair sun and kill the envious moon
> Who is already sick and pale with grief"
>
> would give me the output in the attached screenshot
> [image: Inline image 2]
>
> whereas I want only one list containing strings not nested lists.
>
> Any help would be appreciated.
>
> *Warm regards,*
>
> *Olaoluwa O. Thomas,*
> *+2347068392705*
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Alex Hall
Automatic Distributors, IT department
ah...@autodist.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Practice Exercises for Beginner ?

2016-06-02 Thread Andrei Colta
Hi,

Anyone can recommend practical work on learning python.. seems reading and 
reading does not helping.

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


Re: [Tutor] Tkinter and threading

2016-06-02 Thread Marco Soldavini
Thanks for you answers!

On Thu, Jun 2, 2016 at 6:39 PM, Peter Otten <__pete...@web.de> wrote:

>> For example a state machine with a var state which can have some
>> discrete string values (like RUNNING, STOPPED, PAUSED, ABORTED, IDLE)
>> and a text element on the gui that reports that state.
>>
>> So a button cause a transition > the python loop (not the gui loop)
>> detects the transition and changes the state var value > the gui
>> refresh its value on screen.
>>
>> I wrote some code to try it without threading but it does not give the
>> expected result as it seems the button update status action is already
>> triggered. I am missing some basic point here
>
> Indeed your problem has nothing to do with threads.
>>


Yes i know it could be done without threads, but for study reason i
want to separate the logic from representation (the HMI). That's how
usually program industrial machines. HMI serves only to display and
change variables, not to execute logic.

I want to build a mockup of a machine I saw at work, with statuses and alarms.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Practice Exercises for Beginner ?

2016-06-02 Thread boB Stepp
On Thu, Jun 2, 2016 at 3:43 PM, Andrei Colta  wrote:

> Anyone can recommend practical work on learning python.. seems reading and 
> reading does not helping.

The usual advice:  Find one or more projects that interest you and
start trying to accomplish them, learning more Python along the way as
you find the need.

Surely when you started studying Python you had some hopes and goals
towards which to apply your new knowledge?  Go for it!  Even if you
don't know enough to do the full project you envision, you can start
working on a piece of the puzzle.  Say your goal is to write the
greatest ever Dungeons and Dragons adventure game.  That may be way
too big for your current knowledge and skills, but I bet you could
start in on some of it.  For instance, you know you will have to have
the program simulate the rolling of dice.  Surely that would be
doable?  So write the needed function(s) that would accomplish that
piece.  Once accomplished, find a new piece you can work on.  Etc.

The idea is if you are interested in the project up front, then you
will be motivated to follow it through to its happy completion,
learning tons of Python and general programming skills along the way!

Good luck on your journeys!

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