[Tutor] Is it possible to tell, from which class an method was inherited from
Is it possible to tell, from which class an method was inherited from. take an example below class A: def foo(): pass class B(A): def boo(A): pass class C(B): def coo() pass class D(C): def doo() pass >>> dir (D) ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo'] Is there any method to tell me form which classes boo, coo, foo where inherited from? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to plot graph?
hi all, i installed matplotlib, numpy and scipy. i tried to run attached script to learn how it plot a grph but got error as below: Traceback (most recent call last): File "C:\Python25\myscript\plot\plot1.py", line 3, in from scipy import * File "C:\Python25\Lib\site-packages\scipy\linalg\__init__.py", line 31, in from numpy.testing import NumpyTest ImportError: cannot import name NumpyTest pls advise. thanks From: waynejwer...@gmail.com Date: Tue, 18 Jan 2011 06:14:07 -0600 Subject: Re: [Tutor] How to plot graph? To: tc...@hotmail.com CC: tutor@python.org On Tue, Jan 18, 2011 at 4:30 AM, tee chwee liong wrote: hi all, i'm new to python and this is advanced for me. is there a way to plot data with python? i want to plot EyVt, EyHt on the Y-axis and Lane on the X-axis as attached or below. currently i'm using Python2.5 and Win XP. thanks a lot. Take a look at the matplotlib library: http://matplotlib.sourceforge.net/ Especially the gallery page - there are several examples of what you can do. It may not be the easiest thing to start into as a beginner, but it's definitely available. If you're interested in doing a lot of scientific stuff, I'd also recommend using Python(X, Y): http://www.pythonxy.com/ as it bundles most of the tools you'll need (such as matplotlib) into one easy installer. Give it a try and if you get stuck, send us another email with what you've done, what you expect it to do, and what it actually does instead (including the full text of any and all tracebacks). HTH, Wayne import numpy as np from numpy import cos from scipy import * from pylab import plot, show, ylim, yticks from matplotlib import * from pprint import pprint n1 = 1.0 n2 = 1.5 #alpha, beta, intensity data = np.array([ [10,22, 4.3], [20,42, 4.2], [30,62, 3.6], [40,83, 1.3], [45,102,2.8], [50,123,3.0], [60,143,3.2], [70,163,3.8], ]) # Populate arrays x = np.array([row[0] for row in data]) y1 = np.array([row[1] for row in data]) rhotang1 = n1*cos(data[:,0]) - n2*cos(data[:,1]) rhotang2 = n1*cos(data[:,0]) + n2*cos(data[:,1]) y3 = rhotang1 / rhotang2 plot(x, y1, 'r--', x, y3, 'g--') show() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to tell, from which class an method was inherited from
On 01/19/2011 03:55 AM, Jojo Mwebaze wrote: > Is it possible to tell, from which class an method was inherited from. > take an example below > > |class A: > >def foo(): > pass > class B(A): > >def boo(A): > pass > > class C(B): >def coo() > > pass > class D(C): > >def doo() > pass > dir (D) > > ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo'] > > | > > Is there any method to tell me form which classes boo, coo, foo where > inherited from? > > > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Try using the information here: http://stackoverflow.com/questions/1938755/getting-the-superclasses-of-a-python-class >From there, you can use the following (probably sub-prime): def findRootParent(obj, method, prev=None): for parent in obj.__bases__: if hasattr(parent, method): findRootParent(parent, method, parent) print "I'm in %s, it has it" % obj.__name__ else: print "%s first had %s" % (obj.__name__, method) Here's a little test and some output: class A(object): def test1(): pass def test2(): pass class B(A): def test3(): pass class C(B): def test4(): pass findRootParent(C, "test1") Output: A first had test1 I'm in B, it has it I'm in C, it has it That's just me hacking together a solution. I don't know if its the best or if one of the gurus on the list have a better one. It doesn't really work if you have multiple inheritance: class A(object): def test1(): pass class B(object): def test2(): pass class C(A, B): pass findRootParent(C, "test1") findRootParent(C, "test2") Output: A first had test1 I'm in C, it has it C first had test1 C first had test2 B first had test2 I'm in C, it has it Hope it helps, ~Corey ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to tell, from which class an method was inherited from
Jojo Mwebaze wrote: > Is it possible to tell, from which class an method was inherited from. > take an example below > > class A: >def foo(): > pass > class B(A): >def boo(A): > pass > class C(B): >def coo() > pass > class D(C): >def doo() > pass > dir (D) > ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo'] > > Is there any method to tell me form which classes boo, coo, foo where > inherited from? You can check the classes in method resolution order (mro): import inspect class A: def foo(self): pass def bar(self): pass class B(A): def bar(self): pass class C(B): def baz(self): pass for methodname in dir(C) + ["spam"]: for class_ in inspect.getmro(C): if methodname in class_.__dict__: print "%s.%s()" % (class_.__name__, methodname) break else: print "no attribute named %r found" % methodname ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Decoding from strange symbols
Hi, I am trying to decode a string I took from file: file = open ("./Downloads/lamp-post.csv", 'r') data = file.readlines() data[0] '\xff\xfeK\x00e\x00y\x00w\x00o\x00r\x00d\x00\t\x00C\x00o\x00m\x00p\x00e\x00t\x00i\x00t\x00i\x00o\x00n\x00\t\x00G\x00l\x00o\x00b\x00a\x00l\x00 \x00M\x00o\x00n\x00t\x00h\x00l\x00y\x00 \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00 \x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00 \x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00 \x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00 \x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00 \x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00 \x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00 \x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00 \x002\x000\x001\x000\x00\t\x00A\x00d\x00 \x00s\x00h\x00a\x00r\x00e\x00\t\x00S\x00e\x00a\x00r\x00c\x00h\x00 \x00s\x00h\x00a\x00r\x00e\x00\t\x00E\x00s\x00t\x00i\x00m\x00a\x00t\x00e\x00d\x00 \x00A\x00v\x00g\x00.\x00 \x00C\x00P\x00C\x00\t\x00E\x00x\x00t\x00r\x00a\x00c\x00t\x00e\x00d\x00 \x00F\x00r\x00o\x00m\x00 \x00W\x00e\x00b\x00 \x00P\x00a\x00g\x00e\x00\t\x00L\x00o\x00c\x00a\x00l\x00 \x00M\x00o\x00n\x00t\x00h\x00l\x00y\x00 \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\n' How do I convert this to something human readable? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Decoding from strange symbols
Oleg Oltar wrote: > I am trying to decode a string I took from file: > > file = open ("./Downloads/lamp-post.csv", 'r') > data = file.readlines() > data[0] > > '\xff\xfeK\x00e\x00y\x00w\x00o\x00r\x00d\x00\t\x00C\x00o\x00m\x00p\x00e\x00t\x00i\x00t\x00i\x00o\x00n\x00\t\x00G\x00l\x00o\x00b\x00a\x00l\x00 > How do I convert this to something human readable? If you stare at it long enough you'll see the usual ascii characters interspersed with zero-bytes shown by Python as "\x00". This is an UTF-16 file. Open it with import codecs filename = "./Downloads/lamp-post.csv" with codecs.open(filename, "r", encoding="utf-16") as file: for line in file: print line Note that 'line' will now contain a unicode string instead of a byte string. If you want to write that to a file you have to encode it manually line = u"äöü" with open("tmp.txt", "w") as f: f.write(line.encode("utf-8")) or use codecs.open() again: with codecs.open("tmp.txt", "w", encoding="utf-8") as f: f.write(line) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Decoding from strange symbols
Oleg Oltar wrote: Hi, I am trying to decode a string I took from file: [...] How do I convert this to something human readable? In general, you can't unless you know the encoding. A file filled with arbitrary bytes could be anything. However, you can sometimes guess the encoding, either by looking at it and reasoning carefully, as Peter Otten did when he suggested your file was UTF-16, or by statistical analysis, or some other combination of techniques. Guessing encodings is pretty much a black art, so if you need to do this a lot you should use an existing package like this one: http://chardet.feedparser.org/ Once you have the encoding, or at least a guess for the encoding: bytes = open(filename).read() text = bytes.decode(encoding) or use the codecs module, as Peter showed. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to plot graph?
tee chwee liong wrote: hi all, i installed matplotlib, numpy and scipy. i tried to run attached script to learn how it plot a grph but got error as below: If you're having problems with numpy and scipy, you will probably get more help from a specialist numpy mailing list. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems passing a parameter in a GUI
"David Holland" wrote This works :- def create_widgets(self): self.submit_bttn=Button(self, text="Submit", command=self.reveal) But when I changed it to use a number I get an error message. def create_widgets(self,x): x= self.submit_bttn=Button(self, text="Submit", command=self.reveal(x)) Spot the difference in the commasnd parameter? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Decoding from strange symbols
"Oleg Oltar" wrote I am trying to decode a string I took from file: file = open ("./Downloads/lamp-post.csv", 'r') I assume you know what its supposed to represent? What the columns etc are intended to be? Otherwise it will be a challenge! There is a section here that looks like the months of the year (English) in reverse: \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00 \x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00 \x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00 \x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00 \x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00 \x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00 \x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00 \x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00 Otherwise its all a bit arbitrary... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why super does not work !
"Steven D'Aprano" wrote I'm sorry, but I don't believe you :/ If it "just works" in Lisp, why > does Lisp allow you to override the default linearization? At 2:00am last night I wrote a long explanatory reposte then discovered my wife's Netbook wouldn'ty let me post it! Now I can't be bothered retyping it all so I'll summarise! :-) MI is harder than SI - always. Lisp makes it as easy as it can get and works well enough that for the past 20+ years its been the stabndartd OOP idiom in Lisp. But OOP is not the most common idiom in the Lisp community so we are not dealing with typical OOP programmers. I used Lisp MI for about 5 years and had no issues that could not be addressed using the standard Lisp tools (redefining the linearization is one such tool.but not one used often) The definition of before/after and around specifiers is the one that is used most.in my experience. Diamonds are usually avoided (you can make one if you work hard at it) but mostly Lisp pulls all parents into a suingle set so there are no duplicate paremts but the order can be a bit "random"! This is less of an issue that it sounds because of the before/after safeguards. So I'm not recommending wholesale use of MI simply saying that it is a valid and convenient tool in languages that expect it (Eiffell is another where I've had success with MI - but sadly less so with the standard library - another tale...) As to my own experience with super - I just can't find cases where it works with MI and, although not recommended, using explicit calls to the superclasses has worked in most cases. Maybe if I was writing production code in Python I'd be less gung ho, but I only use Python for tools and prototyping so it hasn't bitten me so far! :-) Maybe its no coincidence that Smalltalk has always resisted adding MI. Even an experimental branch with MI seems to have died out :-) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Dear tutors, I'm writing a program that can play Othello. Everything has been going fine, but there is a frequently occurring problem with printing the board with the squares filled in. Here's the code for the board: class board(object): def create_board(self): #Create a virtual board, that will be played on self.board = [] for squares in range(64): self.board.append(" ") self.board_table =\ """ A B C D E F G H 1 |%s|%s|%s|%s|%s|%s|%s|%s| 2 |%s|%s|%s|%s|%s|%s|%s|%s| 3 |%s|%s|%s|%s|%s|%s|%s|%s| 4 |%s|%s|%s|%s|%s|%s|%s|%s| 5 |%s|%s|%s|%s|%s|%s|%s|%s| 6 |%s|%s|%s|%s|%s|%s|%s|%s| 7 |%s|%s|%s|%s|%s|%s|%s|%s| 8 |%s|%s|%s|%s|%s|%s|%s|%s| """ def display_board(self): print self.board_table % self.board v = board() v.create_board() v.display board() I get this error: Traceback (most recent call last): File "C:\Documents and Settings\Owner\My Documents\Dropbox\Shared_With_Chris\Jake's Programs\AI\Othello_Unfinished.py", line 76, in v.display_board() File "C:\Documents and Settings\Owner\My Documents\Dropbox\Shared_With_Chris\Jake's Programs\AI\Othello_Unfinished.py", line 72, in display_board print self.board_table % self.board TypeError: not enough arguments for format string I tried doing: def display_board(self): print self.board_table % self.board[:] But that didn't help either, and I still got the same TypeError. If you could please figure out what's wrong, it would be greatly appreciated. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 1/19/2011 1:09 PM Jacob Bender said... def display_board(self): print self.board_table % self.board[:] Self.board is a list, but the formatting substitution needs to be passed a tuple. Try: print self.board_table % tuple(self.board) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Correct Path for tkinter using python3.1 on Ubuntu Maverick!!
Hello Python Collective, Well, I'm sure this topic came up before on this forum, and I was confident that I mastered it having had to set it up correctly on my Mac systems and linux systems in the path. Here are my findings. I am trying to run this code: #!/usr/bin/env python3.1 from tkinter import label widget = Label( None, text = 'Hello Gui World' ) widget.pack() widget.mainloop() --in order to do several tkinter tutorials that I am following in a book and I keep getting this error: Traceback (most recent call last): File "/home/myhomename/pythonproj/hellogui2.py", line 3, in import messagebox ImportError: No module named messagebox First off, I checked my path, and in having checked several on-line tutorials on how to set up the tkinter module, one of them made mention that the lib-tk folder is no longer available in python3.1 and that this should be the correct path on Linux systems: /usr/lib/python3.1/tkinter , which when I looked inside this directory, I did manage to find the files messagebox.py and messagebox.pyc. I also wen to this link: http://wiki.python.org/moin/TkInter and ran python3.1 in my Terminal: >>>import _tkinter # with underscore, and lowercase 't' >>>import Tkinter # no underscore, uppercase 'T' prior to V3.0; lowercase 't' from v3.0 >>>Tkinter._test() # note underscore in _test. Also, if you are using Python 3.1, try tkinter._test() instead The first line ran ok, but in order to get the second and third line to work I had to change them to( making Tkinter lower case tkinter: >>>import tkinter # no underscore, uppercase 'T' prior to V3.0; lowercase 't' from v3.0 >>>tkinter._test() # note underscore in _test. Also, if you are using Python 3.1, try tkinter._test() instead Is this because my path is still missing some essential module, or does it have to do with the fact that its deprecated in 3.1? Again, I've made sure that my .bashrc file contains export PYTHONPATH = /usr/lib/python3.1/tkinter. Is there anything else that I'm to do? Any help would be greatly appreciated. Best Regards, freesparks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Battery Level Monitoring and Voice
Hello everyone, Since I got a simple answer on the Python Win32 web site I decided to post it here for those who would want that information, especially those who can not see. The first 2 lines are all that is needed along with the battery level instances using that object, ( BatteryPercentage = wmi.InstancesOf('win32_battery')) This program uses the computer voices and will list them if the specified one is not found. In this case I used Karen. When the list is displayed you do not need to insert everything in the name, just the obvious part of the name, like Karen. Just make sure you spell it correctly or you will always get the list printed on the screen. this also will sound off every minute because of the sleep amounts I use. It can also be changed to voice the percentage every percent change, just do what you want to do. Bruce #BatteryLevel.py import win32com.client wmi=win32com.client.GetObject('winmgmts:') from random import randint, uniform import msvcrt #INPUT KEYBOARD COMMANDS! import sys import time import pyTTS #VOICE SETUP! tts = pyTTS.Create() purge = pyTTS.tts_purge_before_speak async = pyTTS.tts_async PITCH=0 tts.Volume = 100 tts.Rate = 2 VOICECOUNT = len(tts.GetVoiceNames()) VOICES=[] for v in range( VOICECOUNT): VOICES.append( tts.GetVoiceNames()[v]) def setVoice2Name( name): "SET VOICE BY NAME!" t=-1 for v in range( VOICECOUNT): if name in VOICES[ v]: tts.SetVoiceByName( VOICES[v]) t=v if t==-1: tts.Speak( " %d Voices with %s Name Not Found! " % (VOICECOUNT, name)) print " %d Voices with %s Name Not Found! " % (VOICECOUNT, name) print "Voices:" for v in range( VOICECOUNT): print "%d= %s" % (v, VOICES[ v]) def Speak(text, pitch=999): if pitch==999: pitch=PITCH tts.Speak( " %s" % (pitch, text), async, purge) def SpeakNext(text, pitch=999): if pitch==999: pitch=PITCH tts.Speak( " %s" % (pitch, text), async) def setRate( rate): tts.Rate= rate if __name__ == '__main__': setVoice2Name( "Karen") PITCH=3 setRate( 2) # clear the keyboard buffer while msvcrt.kbhit(): ch = msvcrt.getch() ch=""; ch1=""; sch="" while ch != chr(27) and ch != chr(13): while msvcrt.kbhit(): ch1 = msvcrt.getch() ch = ch1 if ch1 == chr(0) or ch1 == chr(224): ch = msvcrt.getch() sch = ch1+ch #SAVE ANY MULTIKEYS! if ch != chr(27) and ch != chr(13): BatteryPercentage = wmi.InstancesOf('win32_battery') print (" %d Percent battery charge remaining! " % BatteryPercentage[0].EstimatedChargeRemaining) print "Hay! Battery power is getting low! Plug in the power cord:" print "Please Press Enter To Exit This Battery Alert!" SpeakNext(" %d Percent battery charge remaining! " % BatteryPercentage[0].EstimatedChargeRemaining) SpeakNext( "Hay! Battery power is getting low! Plug in the power cord:") SpeakNext( "Please Press Enter To Exit This Battery Alert!") #time.sleep(10) c = 0 while c < 30: if msvcrt.kbhit(): SpeakNext( "Key Was Hit!") c = 100 time.sleep(1) else: time.sleep(2) c += 1 #if msvcrt.kbhit(): #SpeakNext( "Key Was Hit!") #time.sleep(1) #else: #time.sleep(5) SpeakNext( "OK, Goodbye!") time.sleep(2) #RELEASE VOICE INSTANCE FROM MEMORY! del tts sys.exit() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP question
If you want to test if one outcome is equal to another, you probably want to know if The name of the outcome is the same The odds of the outcome is the same So, you'd implement it like so class Outcome: def __eq__(self, other): if self.name == other.name and self.odds == other.odds: return True return False Make sense? Nick On Wed, Jan 19, 2011 at 9:07 AM, Ben Ganzfried wrote: > yeah I was actually pretty confused about those methods as well. I mean I > get the general idea that we are testing to see if two outcomes are the same > (hence a winning bet)...when you say expand on them, what do you mean > exactly? > > Thanks again, really much appreciated! > > > On Tue, Jan 18, 2011 at 4:31 PM, Nick Stinemates wrote: > >> You may also want to look in to the __eq__ and __ne__ methods. You're just >> re-implementing standard behavior (so redefining them is useless.) You'll >> probably want to expand on them to be more concise. >> >> >> On Tue, Jan 18, 2011 at 10:00 AM, Ben Ganzfried >> wrote: >> >>> Thanks, Nick! >>> >>> >>> On Tue, Jan 18, 2011 at 12:52 PM, Nick Stinemates >> > wrote: >>> Updated inline. Check the updated definiton of winAmount. Nick On Tue, Jan 18, 2011 at 9:25 AM, Ben Ganzfried >>> > wrote: > Hey guys, > > I'm trying to get a version of Roulette working and I had a quick > question. Here is my code: > > class Outcome: > > def __init__(self, name, odds): > self.name = name > self.odds = odds > > def winAmount(self, amount): > return self.odds*amount > > > def __eq__(self, other): > if (self == other): > return True > else: > return False > > def __ne__(self, other): > if (self != other): > return True > else: > return False > > def __str__(self): > return "%s (%d:1)" % ( self.name, self.odds ) > > Now whenever I try to call the winAmount method, I get the following > error: NameError: global name 'odds' is not defined > > I'm wondering the following: I had thought that by initializing "odds" > in the first initialization method that "odds" should then be accessible > to > all the other methods in the class. This does not seem to be the case. > So > what is the ideal fix? On the one hand I still need to initialize "odds", > right? But if "odds" is within the initialization method, how else can it > be accessed by the other methods? > > Thanks so much! > > Ben > > ___ > 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] Problems passing a parameter in a GUI
Dave, Sorry I did not include the traceback but now James Reynolds (thank you very much) has provided a solution I don't need the info any more. Thank you both for your help and time. Traceback (most recent call last): File "/home/david/Documents/learnJava2010/v6c.py", line 46, in app=Application(root) File "/home/david/Documents/learnJava2010/v6c.py", line 9, in __init__ self.create_widgets(x) File "/home/david/Documents/learnJava2010/v6c.py", line 15, in create_widgets x= self.submit_bttn=Button(self, text="Submit", command=self.reveal(x)) File "/home/david/Documents/learnJava2010/v6c.py", line 24, in reveal self.secret_text.delete(0.0, END) AttributeError: Application instance has no attribute 'secret_text' --- On Tue, 18/1/11, Dave Angel wrote: From: Dave Angel Subject: Re: [Tutor] Problems passing a parameter in a GUI To: "David Holland" Cc: tutor@python.org Date: Tuesday, 18 January, 2011, 22:39 On 01/-10/-28163 02:59 PM, David Holland wrote: > Hi All, > I hope this makes sense > > I am trying to write a GUI where you click on a button and each time > you click on the button it shows in the entry text how many times you > have clicked. However when I changed an earlier version of the code to > pass a parameter it errors. > > This works :- > > > > from Tkinter import * > > class Application(Frame): > > """GUI App""" > > def __init__(self, master): > > """Initialize the frame.""" > > Frame.__init__(self, master) > > self.grid() > > self.create_widgets() > > > > def create_widgets(self): > > self.inst_lbl=Label(self, text="Click on the button to get >instructions") > > self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W) > > self.pw_lbl=Label(self, text="Password") > > self.submit_bttn=Button(self, text="Submit", command=self.reveal) > > self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W) > > self.secret_text=Text(self, width=35, height=5, wrap=WORD) > > self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W) > > self.helpinfo() > > > > def reveal(self): > > Calmexob = Calmex() > > results = Calmexob.getmess() > > self.secret_text.delete(0.0, END) > > self.secret_text.insert(0.0,results) > > > > def helpinfo(self): > > self.secret_text.insert(0.0,"Start") > > > > class Calmex(object): > > > > def __init__(self): > > """Default arg""" > > > > > > def getmess(self): > > mesg="Test" > > return mesg > > > > #main > > root=Tk() > > root.title("Test") > > root.geometry("400x300") > > app=Application(root) > > root.mainloop() > > > > > > But when I changed it to use a number I get an error message. > > Here is the code > > from Tkinter import * > > class Application(Frame): > > """GUI App""" > > def __init__(self, master): > > """Initialize the frame.""" > > Frame.__init__(self, master) > > self.grid() > > x=0 > > self.create_widgets(x) > > > > def create_widgets(self,x): > > self.inst_lbl=Label(self, text="Click on the button to get >instructions") > > self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W) > > self.pw_lbl=Label(self, text="Password") > > x= self.submit_bttn=Button(self, text="Submit", >command=self.reveal(x)) > > self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W) > > self.secret_text=Text(self, width=35, height=5, wrap=WORD) > > self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W) > > self.helpinfo() > > > > def reveal(self, x): > > Calmexob = Calmex(x) > > results = Calmexob.getmess(x) > > self.secret_text.delete(0.0, END) > > self.secret_text.insert(0.0,results) > > x=x+1 > > return x > > > > def helpinfo(self): > > self.secret_text.insert(0.0,"Start") > > > > class Calmex(object): > > > > def __init__(self,x): > > """Default arg""" > > > > > > def getmess(self,x): > > mesg="Test"+str(x) > > return mesg > > > > #main > > root=Tk() > > root.title("Test") > > root.geometry("400x300") > > app=Application(root) > > root.mainloop() > > > > > > > > > > > > > > > > > > > > The error message is > > AttributeError: Application instance has no attribute 'secret_text' > > > > The only change is that I have declared x and changed the function reveal > to take and pass x as an arguement. > > > > Thanks in advance. I hope this makes sense. > > > > > > > > > I don't have time to try to understand all the tk stuff. And it sure would have been nice if you showed the whole error message, including the traceback. You didn't even tell us what line it was on. But your problem is that you have an instance of Application without the secret_text at
Re: [Tutor] Tutor Digest, Vol 83, Issue 83
I did understand the difference (it was me who did trying to write the program) however until James Reynolds provided me with the solution I could not think of a better way to do this. Thanks for the help. --- On Wed, 19/1/11, tutor-requ...@python.org wrote: -- Message: 7 Date: Wed, 19 Jan 2011 17:59:40 - From: "Alan Gauld" To: tutor@python.org Subject: Re: [Tutor] Problems passing a parameter in a GUI Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "David Holland" wrote > This works :- > > def create_widgets(self): > self.submit_bttn=Button(self, text="Submit", > command=self.reveal) > > > But when I changed it to use a number I get an error message. > >def create_widgets(self,x): > x= self.submit_bttn=Button(self, text="Submit", > command=self.reveal(x)) Spot the difference in the commasnd parameter? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 83, Issue 83 * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 1/19/2011 4:09 PM, Jacob Bender wrote: Dear tutors, I'm writing a program that can play Othello. Everything has been going fine, but there is a frequently occurring problem with printing the board with the squares filled in. Here's the code for the board: Emile gave you the answer. A couple of other matters: Please always provide a meaningful subject. We track discussions by subject, and can ignore those that fall outside our interest / expertise. Read the documentation before posting. You will find "If /format/ requires a single argument, /values/ may be a single non-tuple object. [4] Otherwise, /values/ must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary)." An easier way to create the list: self.board = [" "]*64 -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 83, Issue 84
On 1/19/2011 4:53 PM, tutor-requ...@python.org wrote: Send Tutor mailing list submissions to tutor@python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-requ...@python.org You can reach the person managing the list at tutor-ow...@python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: Decoding from strange symbols (Alan Gauld) 2. Re: Why super does not work ! (Alan Gauld) 3. (no subject) (Jacob Bender) 4. Re: (no subject) (Emile van Sebille) 5. Correct Path for tkinter using python3.1 on Ubuntu Maverick!! (Nevins Duret) 6. Re: OOP question (Nick Stinemates) -- Message: 1 Date: Wed, 19 Jan 2011 18:04:23 - From: "Alan Gauld" To: tutor@python.org Subject: Re: [Tutor] Decoding from strange symbols Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Oleg Oltar" wrote I am trying to decode a string I took from file: file = open ("./Downloads/lamp-post.csv", 'r') I assume you know what its supposed to represent? What the columns etc are intended to be? Otherwise it will be a challenge! There is a section here that looks like the months of the year (English) in reverse: \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00 \x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00 \x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00 \x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00 \x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00 \x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00 \x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00 \x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00 Otherwise its all a bit arbitrary... HTH, Thanks! That did the trick! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to plot graph?
actually i just want to plot a simple x and y graph. any suggestion? how about using excel to plot? any sample code that i can follow to: 1) launch excel 2) read x-y from a text file 3) plot graph thanks > Date: Thu, 20 Jan 2011 01:20:53 +1100 > From: st...@pearwood.info > To: tutor@python.org > Subject: Re: [Tutor] How to plot graph? > > tee chwee liong wrote: > > hi all, > > > > i installed matplotlib, numpy and scipy. i tried to run attached script to > > learn how it plot a grph but got error as below: > > If you're having problems with numpy and scipy, you will probably get > more help from a specialist numpy mailing list. > > > -- > Steven > ___ > 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 plot graph?
actually i just want to plot a simple x and y graph. any suggestion? how about using excel to plot? any sample code that i can follow to: 1) launch excel 2) read x-y from a text file 3) plot graph thanks x,y is simple in many modules(beyond is more computational. What version of python, platform and modules are you using? -- The lawyer in me says argue...even if you're wrong. The scientist in me... says shut up, listen, and then argue. But the lawyer won on appeal, so now I have to argue due to a court order. Furthermore, if you could be a scientific celebrity, would you want einstein sitting around with you on saturday morning, while you're sitting in your undies, watching Underdog?...Or better yet, would Einstein want you to violate his Underdog time? Can you imagine Einstein sitting around in his underware? Thinking about the relativity between his cotton nardsac, and his Fruit of the Looms? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it possible to tell, from which class an method was inherited from
Thanks guys for the responses, inspect.classify_class_attrs(klass) does the magic Regards On Wed, Jan 19, 2011 at 3:58 PM, Peter Otten <__pete...@web.de> wrote: > Jojo Mwebaze wrote: > > > Is it possible to tell, from which class an method was inherited from. > > take an example below > > > > class A: > >def foo(): > > pass > > class B(A): > >def boo(A): > > pass > > class C(B): > >def coo() > > pass > > class D(C): > >def doo() > > pass > > > dir (D) > > ['__doc__', '__module__', 'boo', 'coo', 'doo', 'foo'] > > > > Is there any method to tell me form which classes boo, coo, foo where > > inherited from? > > You can check the classes in method resolution order (mro): > > > import inspect > > class A: >def foo(self): >pass >def bar(self): >pass > > class B(A): >def bar(self): >pass > > class C(B): >def baz(self): >pass > > for methodname in dir(C) + ["spam"]: >for class_ in inspect.getmro(C): >if methodname in class_.__dict__: >print "%s.%s()" % (class_.__name__, methodname) >break >else: >print "no attribute named %r found" % methodname > > > ___ > 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