[Tutor] tkinter issue in python 3

2011-08-07 Thread Khalid Al-Ghamdi
Hi all,

I'm studying this
tutorialabout
tkinter, but it that it's code in a 2.X.

I'm using the following code in python 3, but when i hit quite in the
resulting widget it hangs. I've tried modifying the things I know about to
no avail. Can anyone help?

Here's my code:

from tkinter import *

class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()

self.button=Button(frame, text='Quit', fg='red', command=frame.quit)
self.button.pack(side=LEFT)

self.hi_there=Button(frame, text='hello', command=self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print('hi there, everyone')

root=Tk()
app = App(root)
root.mainloop()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] commandline unable to read numbers?

2011-08-07 Thread Dave Angel

On 08/07/2011 01:00 AM, Steven D'Aprano wrote:

Robert Sjoblom wrote:

I have a quite odd problem, and I've come across it before but
probably ignored it at the time because I had other concerns. I've
tried googling for the answer but haven't really come closer to
solving it.
This is what happens:
C:\[path]\nester>C:\Python32\python.ex
e setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to 
you), or

 4. quit
Your selection [default 1]:
1
Please choose one of the four options!


Looks like a bug in the setup.py script. You should report it to the 
author of the package.


Have you tried just pressing enter without entering anything?



No matter what I enter it will loop back. It seems my commandline
can't read numbers? The other time I noticed it was while working on a
notebook example:

[...]

This code works in IDLE, so I know it's nothing in the actual code
that's a problem, 


Apart from the fact that it is incomplete and won't run as given, it 
seems fine.




but when I run it in commandline it will just repeat
"is not a valid choice." Note that it does this no matter what I
actually enter, it won't actually get any kind of input except the
enter key. So I suppose it's a problem with input() (I'm using python
3.2 btw). Anyone have any insights?


You're not telling us how you're running it from the command line. My 
guess is that when you try, you're ending up with a different version 
of Python, namely Python 2.x, where input() has different semantics.


Try putting

print("choice = ", choice, type(choice))

immediately after the call to input in your code, and seeing what it 
prints. My guess is that it will claim choice is a int instead of a 
string.



One other thing to test is to print sys.version at the beginning of your 
program.  That will confirm Steven's hypothesis that you have two 
different versions of Python.  (Clearly, you'll also need to import sys)




--

DaveA

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


Re: [Tutor] tkinter issue in python 3

2011-08-07 Thread Peter Otten
Khalid Al-Ghamdi wrote:

> I'm studying this
> tutorialabout
> tkinter, but it that it's code in a 2.X.
> 
> I'm using the following code in python 3, but when i hit quite in the
> resulting widget it hangs. I've tried modifying the things I know about to
> no avail. Can anyone help?
> 
> Here's my code:
> 
> from tkinter import *
> 
> class App:
> def __init__(self, master):
> frame = Frame(master)
> frame.pack()
> 
> self.button=Button(frame, text='Quit', fg='red',
> command=frame.quit) self.button.pack(side=LEFT)
> 
> self.hi_there=Button(frame, text='hello', command=self.say_hi)
> self.hi_there.pack(side=LEFT)
> 
> def say_hi(self):
> print('hi there, everyone')
> 
> root=Tk()
> app = App(root)
> root.mainloop()

There is nothing suspicious in the above code. 

If you are running it from within IDLE, try again from the commandline.

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


Re: [Tutor] commandline unable to read numbers?

2011-08-07 Thread Peter Otten
Robert Sjoblom wrote:

> I have a quite odd problem, and I've come across it before but
> probably ignored it at the time because I had other concerns. I've
> tried googling for the answer but haven't really come closer to
> solving it.
> This is what happens:
> C:\[path]\nester>C:\Python32\python.ex
> e setup.py register
> running register
> running check
> We need to know who you are, so please choose either:
>  1. use your existing login,
>  2. register as a new user,
>  3. have the server generate a new password for you (and email it to you),
>  or 4. quit
> Your selection [default 1]:
> 1
> Please choose one of the four options!
> We need to know who you are, so please choose either:
>  1. use your existing login,
>  2. register as a new user,
>  3. have the server generate a new password for you (and email it to you),
>  or 4. quit
> Your selection [default 1]:
> 
> No matter what I enter it will loop back. It seems my commandline
> can't read numbers? The other time I noticed it was while working on a
> notebook example:
> 
> class Menu:
> """Display a menu and respond to choices when run."""
> def __init__(self):
> self.notebook = Notebook()
> self.choices = {
> "1": self.show_notes,
> "2": self.search_notes,
> "3": self.add_note,
> "4": self.modify_note,
> "5": self.quit
> }
> 
> def display_menu(self):
> print("""
> Notebook Menu
> 
> 1. Show All Notes
> 2. Search Notes
> 3. Add Note
> 4. Modify Note
> 5. Quit
> """)
> 
> def run(self):
> """Display the menu and respond to choices."""
> while True:
> self.display_menu()
> choice = input("Enter an option: ")
> action = self.choices.get(choice)
> if action:
> action()
> else:
> print("{0} is not a valid choice.".format(choice))
> 
> This code works in IDLE, so I know it's nothing in the actual code
> that's a problem, but when I run it in commandline it will just repeat
> "is not a valid choice." Note that it does this no matter what I
> actually enter, it won't actually get any kind of input except the
> enter key. So I suppose it's a problem with input() (I'm using python
> 3.2 btw). Anyone have any insights?

I think you are on the right track: Python 3.2's input() has a nasty bug on 
Windows (http://bugs.python.org/issue11272). If you repeat the following in 
your interactive interpreter (invoked from the commmandline)

>>> input()
123
'123'

and see '123\r' instead of just '123' you are affected. I believe the bug is 
fixed in 3.2.1, so the easiest solution to your problem would be to switch 
to that version.


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


Re: [Tutor] commandline unable to read numbers? (Steven D'Aprano)

2011-08-07 Thread Robert Sjoblom
> Robert Sjoblom wrote:
>> I have a quite odd problem, and I've come across it before but
>> probably ignored it at the time because I had other concerns. I've
>> tried googling for the answer but haven't really come closer to
>> solving it.
>> This is what happens:
>> C:\[path]\nester>C:\Python32\python.ex
>> e setup.py register
>> running register
>> running check
>> We need to know who you are, so please choose either:
>>  1. use your existing login,
>>  2. register as a new user,
>>  3. have the server generate a new password for you (and email it to you), or
>>  4. quit
>> Your selection [default 1]:
>> 1
>> Please choose one of the four options!
>
> Looks like a bug in the setup.py script. You should report it to the
> author of the package.
>
> Have you tried just pressing enter without entering anything?
Yes, and it goes back to "We need to know who you are, so please
choose either:" The setup.py in question is the distutils.core one
(from distutils.core import setup).

>> No matter what I enter it will loop back. It seems my commandline
>> can't read numbers? The other time I noticed it was while working on a
>> notebook example:
> [...]
>> This code works in IDLE, so I know it's nothing in the actual code
>> that's a problem,
>
> Apart from the fact that it is incomplete and won't run as given, it
> seems fine.
I only copied the relevant part, but here's the entire code if necessary:
http://pastebin.com/PkB6P5fk

>> but when I run it in commandline it will just repeat
>> "is not a valid choice." Note that it does this no matter what I
>> actually enter, it won't actually get any kind of input except the
>> enter key. So I suppose it's a problem with input() (I'm using python
>> 3.2 btw). Anyone have any insights?
>
> You're not telling us how you're running it from the command line. My
> guess is that when you try, you're ending up with a different version of
> Python, namely Python 2.x, where input() has different semantics.
I run it like this:
C:\Python32\python.exe setup.py register
(or in the case of menu.py: C:\Python32\python.exe menu.py) -- which,
when started, prints:

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

C:\>C:\Python32\python.exe
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>


>
> Try putting
>
> print("choice = ", choice, type(choice))
>
> immediately after the call to input in your code, and seeing what it
> prints. My guess is that it will claim choice is a int instead of a string.
I thought so too at first, but that's not the issue:
C:\Python32\python.exe menu.py

Notebook Menu

1. Show All Notes
2. Search Notes
3. Add Note
4. Modify Note
5. Quit

Enter an option: 1
 
 is not a valid choice.

-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Kayode Odeyemi
Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel':
34510}";

#loop through and get the keys of each
for k,v in x:
keys = dict(x).keys()
print keys

I get ValueError: too many values to unpack

Any help will be much appreciated.

Regards


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Steve Willoughby

On 07-Aug-11 08:37, Kayode Odeyemi wrote:

Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011,
'tel': 34510}";


First of all, that's not a dict.  It's just a tuple of strings.
so, when you say:


#loop through and get the keys of each
for k,v in x:


You'll get one iteration, where k=the first string and v=the second.
However, you ignore k and v in all the code that follows, so I'm really 
confused what you're trying to do here.



 keys = dict(x).keys()


Now you try to create a dict out of a tuple of strings, which won't 
quite work as written but in principle would only have created a 
dictionary with the first string as the key, and the second as value, 
not nested dictionaries.


The dict() constructor really expects to see a sequence of key/value 
pairs, so it's going to take your sequence x as a list of two such 
pairs, and then raise an exception because the strings are not key/value 
pairs.  This would succeed if you said

  keys = dict((x,)).keys()
perhaps, but it's still not at all what you're trying to accomplish.


print keys


Note that you are re-assigning the value of keys each time through the 
loop but printing its last value after the loop exits.  Is that what you 
intended?



You're expecting Python to take a string that looks like Python source 
code and know that you want it to be interpreted as source code. 
Instead of using string values, use actual Python syntax directly, like:


 x = {
'pk': 1L,
'model': 'trans',
'fields': {
'updated': 2011,
'tel': 3456
}
}


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Knacktus

Am 07.08.2011 17:37, schrieb Kayode Odeyemi:

Hello all,

Please I need help figuring out this permutation.

I have a dict like this:

x = "{'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
3456}", "{'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011,
'tel': 34510}";
This is not a dict, it's a tuple of two strings. (By the way, the ";" is 
only needed if you would want to place several statements in one line, 
which you shouldn't do.)


A tuple of dicts would be:

x = {'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel': 
3456}}, {'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 
34510}}


Note the additional closing "}" for each entry. You can loop over the 
entries of your tuple and check the type:


for entry in x:
print type(entry)

Now you can extend this loop and loop over the keys and values for each 
entry:


for entry in x:
for key, value in entry.items():
print key
print value

If key is "fields" then value is another dictionary. So if you wish you 
could also get the entries of those dicts explicitly:


for entry in x:
for key, value in entry.items():
print key
if key == "fields":
for field_key, field_value in value.items():
print "%s" % field_key
print "  %s" % field_value
else:
print "  %s" % value

For string formatting used here see:

http://docs.python.org/library/stdtypes.html#string-formatting-operations

HTH,

Jan



#loop through and get the keys of each
for k,v in x:
 keys = dict(x).keys()
print keys

I get ValueError: too many values to unpack

Any help will be much appreciated.

Regards


--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde



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


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


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-07 Thread Steve Willoughby

On 07-Aug-11 09:17, Steve Willoughby wrote:


First of all, that's not a dict. It's just a tuple of strings.
so, when you say:


#loop through and get the keys of each
for k,v in x:


You'll get one iteration, where k=the first string and v=the second.
However, you ignore k and v in all the code that follows, so I'm really
confused what you're trying to do here.


I just noticed I glossed over a key point, for that to work you'd need x 
to be a sequence of such tuples.  As it stands there, you'll get an 
error because each element of x is a single string, so it can't pull two 
values out of each of them.


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] commandline unable to read numbers? (Steven D'Aprano)

2011-08-07 Thread Steven D'Aprano

Robert Sjoblom wrote:
[...]

> Have you tried just pressing enter without entering anything?



Yes, and it goes back to "We need to know who you are, so please
choose either:" The setup.py in question is the distutils.core one
(from distutils.core import setup).


It sounds like Peter Otten has the solution: it's a bug in input on 
Windows systems.


Follow the instructions in his post.


--
Steven

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


Re: [Tutor] commandline unable to read numbers?

2011-08-07 Thread Robert Sjoblom
> I think you are on the right track: Python 3.2's input() has a nasty bug on
> Windows (http://bugs.python.org/issue11272). If you repeat the following in
> your interactive interpreter (invoked from the commmandline)
>
 input()
> 123
> '123'
>
> and see '123\r' instead of just '123' you are affected. I believe the bug is
> fixed in 3.2.1, so the easiest solution to your problem would be to switch
> to that version.
Well that's what I get ('123\r'); installing 3.2.1 fixed it. setup.py
works, as does menu.py (except the search function, but that's a
different problem). Thank you (and everyone else who put time and
effort into trying to solve this)!

-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] gzip

2011-08-07 Thread questions anon
Thank you, I didn't realise that was all I needed.
Moving on to the next problem:
I would like to loop through a number of directories and decompress each
*.gz file and leave them in the same folder but the code I have written only
seems to focus on the last folder. Not sure where I have gone wrong.
Any feedback will be greatly appreciated.


import gzip
import os

MainFolder=r"D:/DSE_work/temp_samples/"

for (path, dirs, files) in os.walk(MainFolder):
for dir in dirs:
outputfolder=os.path.join(path,dir)
print "the path and dirs are:", outputfolder
for gzfiles in files:
print gzfiles
if gzfiles[-3:]=='.gz':
print 'dealing with gzfiles:', dir, gzfiles
f_in=os.path.join(outputfolder,gzfiles)
print f_in
compresseddata=gzip.GzipFile(f_in, "rb")
newFile=compresseddata.read()
f_out=open(f_in[:-3], "wb")
f_out.write(newFile)


On Sun, Aug 7, 2011 at 11:22 AM, Walter Prins  wrote:

>
>
> On 7 August 2011 01:52, questions anon  wrote:
>
>> How can I output the decompressed file? something like:
>> output=file_content.write(filepath[:-3])
>>
>>
> See here:
> http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
> And here:
> http://docs.python.org/library/stdtypes.html#file.write
>
> Walter
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor