[Tutor] How to plot graph?

2011-01-18 Thread tee chwee liong

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. 
Eg: 
Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1
+
Port Chnl Lane EyVt EyHt
+
0  1  1  75  55
0  1  2  10 35
0  1  3  25 35 
0  1  4  35 25
0  1  5  10 20
+
Time: 20s Platform: PC
Tempt   : 25
TAP0:0
TAP1:1

+
PortChnlLaneEyVtEyHt
+
01   1   75 55
01   2   10 35
01   3   25 35  
01   4   35 25
01   5   10 20
+
Time: 20s___
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 74

2011-01-18 Thread Art Cla Media
):
         a = self.sujet.getValue()
         self.text.set(data_base[a][2])


if __name__ == '__main__':
     app=App()
     app.mainloop()

# - end of file - #

class AbstractObserver(object):
     Interface general des observateurs
     def __init__(self, sujet):
         Constructeur
         self.sujet = sujet

     def update(self):
         Methode a implementer par les observateurs
         pass


On 01/18/2011 02:32 AM, Alan G wrote:
 Steven D'Apranosteveat  pearwood.info  writes:

 fact that multiple inheritance itself is often the wrong thing to use,
 and even when it is right, it is often tricky to get it right. To put it
 another way: don't use multiple inheritance unless you have to, there
 are better ways, such as by composition.
 Or use a language where MI is the normal and idiomatic way
 to do things because the language assumes it and so it just
 works. There are very few such languages but Lisp is one :-)

 Sadly Python isn't, and when using MI I always avoid super()
 Which is a huge shame since MI is where super() should
 be most useful... But in my experience MI in Python is
 definitely a place where explicit is better than implicit.

 Alan G.



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



--

Message: 4
Date: Tue, 18 Jan 2011 07:35:30 +0100
From: Izz ad-Din Ruhulessin izzaddin.ruhules...@gmail.com
To: Hugo Arts hugo.yo...@gmail.com
Cc: tutor@python.org
Subject: Re: [Tutor] Exactly duplicating strftime behavior?
Message-ID:
    AANLkTin-GdXd0rfupKHa=-RN+6d_ywYawDrv2Nsw=m...@mail.gmail.com
Content-Type: text/plain; charset=iso-8859-1

Hi Hugo,

Problem solved, thanks!

Kind regards,

Izz ad-Din

2011/1/17 Hugo Arts hugo.yo...@gmail.com

 On Mon, Jan 17, 2011 at 8:29 PM, Izz ad-Din Ruhulessin
 izzaddin.ruhules...@gmail.com wrote:
  Hi all,
  I'm designing a module which aims to provide full compatibility with
  Pythons' datetime module.
  However, I can't find out how some operators in strftime function,
 namely:
  those who use the locale. (%a, %A, %b, etc.)
  How do I access this locale and it's values?
  Thanks in advance.
  Kind regards,
  Izz ad-Din

 Check out the locale module:

 http://docs.python.org/library/locale.html#locale.nl_langinfo

 HTH,
 Hugo

-- next part --
An HTML attachment was scrubbed...
URL: 
http://mail.python.org/pipermail/tutor/attachments/20110118/522e990e/attachment-0001.html

--

Message: 5
Date: Tue, 18 Jan 2011 10:30:53 +
From: tee chwee liong tc...@hotmail.com
To: tutor@python.org
Subject: [Tutor] How to plot graph?
Message-ID: bay156-w28c9e6f96071b97a33cdfeb5...@phx.gbl
Content-Type: text/plain; charset=iso-8859-1


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. 
Eg: 
Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1
+
Port Chnl Lane EyVt EyHt
+
0  1  1  75  55
0  1  2  10 35
0  1  3  25 35 
0  1  4  35 25
0  1  5  10 20
+
Time: 20s               
-- next part --
An HTML attachment was scrubbed...
URL: 
http://mail.python.org/pipermail/tutor/attachments/20110118/80b250af/attachment.html
-- next part --
An embedded and charset-unspecified text was scrubbed...
Name: sampledata.txt
URL: 
http://mail.python.org/pipermail/tutor/attachments/20110118/80b250af/attachment.txt

--

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 83, Issue 74
*



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


Re: [Tutor] How to plot graph?

2011-01-18 Thread Wayne Werner
On Tue, Jan 18, 2011 at 4:30 AM, tee chwee liong tc...@hotmail.com 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/

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
___
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 !

2011-01-18 Thread Karim


By the way Tkinter does not support super() seems to be an 'old style' 
class, this is perhaps the raison why MI does not work in this context:


*Python 2.7.1rc1 (r271rc1:86455, Nov 16 2010, 21:53:40)
[GCC 4.4.3] on linux2
Type help, copyright, credits or license for more information.
 import Tkinter.Frame
Traceback (most recent call last):
  File stdin, line 1, in module
ImportError: No module named Frame
 from  Tkinter  import Frame
 class App(Frame):
... def __init__(self):
... super(App, self).__init__()
...
 a=App()
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 3, in __init__
TypeError: must be type, not classobj
 *

Steven, I corrected my code (even if I have a double Inheritance which 
is ok in this context):


* class ListObservable(Listbox, Observable):
def __init__(self):
Observable.__init__(self)
self.listeContenu = StringVar()
self.listeContenu.set(' '.join(sorted(data_base.keys(
Listbox.__init__(self, listvariable=self.listeContenu, 
selectmode='single')

self.grid(row=0, column=1, sticky=N+S+W)
self.bind('ListboxSelect', self.onSelect)
self._value = None

def onSelect(self, e):
if not self.curselection():
self.setValue(0)
else:
self.setValue(self.get(self.curselection()))
self.notify()

def setValue(self, select):
self._value = select

def getValue(self):
return self._value


class Entry1Observer(Entry, AbstractObserver):
def __init__(self, sujet=None):
AbstractObserver.__init__(self, sujet)
self.text  = StringVar()
Entry.__init__(self, textvariable=self.text)
self.grid(row=2, column=2)

def update(self):
a = self.sujet.getValue()
self.text.set(data_base[a][0])*

I still need a advice about the possible improvement  specially for 
Entry observer object.


Regards
Karim

On 01/18/2011 07:31 AM, Karim wrote:


Thanks Izz, Luke, Steven and Alan!
That's I figured out with MI and super.
Steven I understand the point to have Listbox contains a Listbox.
Before the code was in App class and I extracted it to do an outside 
class making the mistake.

But magic of Python it's working (but I know it's awful).
The code is working but I am not totally happy because of many 
EntryObserver almost identical objects (except from update and grid() 
options)

Is it possible to simplify this structure?

Regards
Karim

The rest of the code is below:

#!/usr/bin/env python2.7
Module ObserverGraphique.py

Une implementation du design pattern Observer.



from Tkinter import *
from observable import Observable
from observer import AbstractObserver


data_base = {
'Employe1': ['Joel',   'Durant',  '0623'],
'Employe2': ['Marc',   'Donce',   '0624'],
'Employe3': ['George', 'Roux','0625'],
'Employe4': ['Alain',  'Richard', '0626']
}


__all__ = ['App', 'ListObservable', 'Entry1Observer', 
'Entry2Observer', 'Entry3Observer']



class App(Frame):
Application graphique avec Tkinter implementant un Observer 
Design Pattern.

def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title(Exemple : Observer Design Pattern)
self.grid()
self.createLabels()
self.createObservable()
self.createObservers()
self.registerObservers()

   def createLabels(self):
Creation de widgets Label
self.label1 = Label(text=Nom :)
self.label2 = Label(text=Prenom :)
self.label3 = Label(text=Poste :)
self.label1.grid(row=1, column=1, sticky=W)
self.label2.grid(row=2, column=1, sticky=W)
self.label3.grid(row=3, column=1, sticky=W)

def createObservable(self):
Creation de la listBox observable.
self.sujet  = ListObservable()

def createObservers(self):
Creation des champs d'entre texte observateurs de la liste.
self.nom= Entry1Observer(self.sujet)
self.prenom = Entry2Observer(self.sujet)
self.poste  = Entry3Observer(self.sujet)

def registerObservers(self):
Enregistrement des observateurs.
self.sujet.attach(self.nom)
self.sujet.attach(self.prenom)
self.sujet.attach(self.poste)

class ListObservable(Listbox, Observable):
Creation de widget Listbox
def __init__(self):
#super(ListObservable, self).__init__()
Observable.__init__(self)
self._value = None
self.listeContenu = StringVar()
self.listeContenu.set(' '.join(sorted(data_base.keys(
self.liste = Listbox(listvariable=self.listeContenu, 
selectmode='single')

self.liste.grid(row=0, column=1, sticky=N+S+W)
self.liste.bind('Double-1', self.onSelect)
self.liste.selection_set(0)

def onSelect(self, e):
if not self.liste.curselection():
self.setValue(0)

[Tutor] Convert an IP address from binary to decimal

2011-01-18 Thread Tom Lin
Hi guys,

Please help me with this:
Convert an IP address from binary string to decimal format.There are
some preconditions:
1.IP address is in the form of '10010001100'.32 bits with no dot.
2.int(string, base) is not allowed, You have to implement the conversion .
3.Performance should be considered.

For example an IP address like ' '
would be converted to '255.255.255.255'

That's how I implement it. But I think it looks ugly and I wonder if
there is a better way to do this.

import re
import sys

def convert(bin_ip):
patt = re.compile(r'\d{8}')
bin_list = patt.findall(str(bin_ip))

dec_list = []
for bin in bin_list:
sum = 0
i = 7
for n in bin:
if int(n):
sum = sum + 2**i
i = i - 1
dec_list.append(str(sum))

dec_ip = '.'.join(dec_list)
print dec_ip

if __name__ == '__main__':
bin_ip = sys.argv[1:]
convert(bin_ip)


Thanks in advance and excuse my poor English.


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


Re: [Tutor] Convert an IP address from binary to decimal

2011-01-18 Thread Christian Witts
On 18/01/2011 14:45, Tom Lin wrote:
 Hi guys,

 Please help me with this:
 Convert an IP address from binary string to decimal format.There are
 some preconditions:
 1.IP address is in the form of '10010001100'.32 bits with no dot.
 2.int(string, base) is not allowed, You have to implement the conversion .
 3.Performance should be considered.

 For example an IP address like ' '
 would be converted to '255.255.255.255'

 That's how I implement it. But I think it looks ugly and I wonder if
 there is a better way to do this.

 import re
 import sys

 def convert(bin_ip):
 patt = re.compile(r'\d{8}')
 bin_list = patt.findall(str(bin_ip))

 dec_list = []
 for bin in bin_list:
 sum = 0
 i = 7
 for n in bin:
 if int(n):
 sum = sum + 2**i
 i = i - 1
 dec_list.append(str(sum))

 dec_ip = '.'.join(dec_list)
 print dec_ip

 if __name__ == '__main__':
 bin_ip = sys.argv[1:]
 convert(bin_ip)


 Thanks in advance and excuse my poor English.


 Best regards,
 Tom


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

If I knew the input to be perfect it would be a simple matter
'.'.join((str(int(input_ip[x:x+8], 2)) for x in range(4)))

-- 
Kind Regards,
Christian Witts

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


Re: [Tutor] What is __weakref__ ?

2011-01-18 Thread Karim


Hello Steven,

(1) slots = [] doesn't do anything special. You have misspelled 
__slots__.

Yes sorry my mistake :-[


(2) Classes don't become read only just because you add __slots__ to 
them. All you prevent is adding extra attributes, and why would you 
wish to do that?


I know this is ugly but until now it is the only way (with this side 
effect) I found to declare Enums class that I _understand_:


*class CategoryType(object):
Implements the enumeration and prevent associated newly created
instances to redefine enums value via special class variable 
'__slots__'

definition. It makes also, instances have no attributes at all.

__slots__ = ()

TRANSISTOR, MOS, BIPOLAR, CAPACITOR, RESISTOR, DIODE, ESD, PAD, \
COMPARATOR, OPAMP, ADC, DAC, PLL, OTHER = range(14)

def toString ( self, litteral ):
Convert the litteral integer number to its associated string 
representation.

if litteral == self.TRANSISTOR:
  return 'transistor'
elif litteral == self.MOS:
  return 'mos'
elif litteral == self.BIPOLAR:*
[...]

I discovered recently that I could configure __setattr__(self, name, 
value) by raising AttributeError to prevent to set up a new attribute value.

Or use an conditional statement to avoid to define it twice.
If you have good advice for enum (I hope I could understand) you welcome 
;-) .


Thus I use __slots__, __dict__ (in __setattr__) and I wanted to about 
much for __weakref__

Have you read the Fine Manual?

http://docs.python.org/reference/datamodel.html#slots



In fact, I already read that. But I am searching for doc about what is a 
weak reference.




 class K1(object):
... __slots__ = []  # Don't support weak-refs.
...
 class K2(object):
...__slots__ = '__weakref__'  # Do support weak-refs.
...
 import weakref
 k1 = K1()
 k2 = K2()
 weakref.ref(k1)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: cannot create weak reference to 'K1' object
 weakref.ref(k2)
weakref at 0xb7c6661c; to 'K2' at 0xb7ee762c


Same thing I don't know what to do with this object weakref :-[ as I 
don't know its meaning. That was my true question in fact;


Regards

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


Re: [Tutor] How to plot graph?

2011-01-18 Thread bob gailer

Thank you for seeking help.

Next time please start a new thread rather than hijacking an existing one.

--
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] Convert an IP address from binary to decimal

2011-01-18 Thread bob gailer

On 1/18/2011 8:08 AM, Christian Witts wrote:

On 18/01/2011 14:45, Tom Lin wrote:

Hi guys,

Please help me with this:
Convert an IP address from binary string to decimal format.There are
some preconditions:
1.IP address is in the form of '10010001100'.32 bits with no dot.
2.int(string, base) is not allowed, You have to implement the conversion .

Christian: note rule 2!


3.Performance should be considered.

For example an IP address like ' '
would be converted to '255.255.255.255'

That's how I implement it. But I think it looks ugly

Tom:

What exactly do you mean by ugly?

and I wonder if there is a better way to do this.

What do you mean by better?

The algorithm you programmed is one good way to accomplish your result. 
The only problems I see are using re, ** and int() which are less 
efficient than other ways.


The best might be - use slicing to get the 8 bit substrings, == to 
test for 1 and bit shifting.


Start with 0, add1 if current bit == 1 and shift left.

Then try * 2 instead of shift left.

Run timing tests on the various approaches. Given that this is a very 
fast algorithm timing tests might not be useful.

import re
import sys

def convert(bin_ip):
patt = re.compile(r'\d{8}')
bin_list = patt.findall(str(bin_ip))

dec_list = []
for bin in bin_list:
   sum = 0
   i = 7
   for n in bin:
 if int(n):
   sum = sum + 2**i
   i = i - 1
   dec_list.append(str(sum))

dec_ip = '.'.join(dec_list)
print dec_ip

if __name__ == '__main__':
   bin_ip = sys.argv[1:]
   convert(bin_ip)


If I knew the input to be perfect it would be a simple matter
'.'.join((str(int(input_ip[x:x+8], 2)) for x in range(4)))




--
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] Convert an IP address from binary to decimal

2011-01-18 Thread Steven D'Aprano
Tom Lin wrote:
 Hi guys,
 
 Please help me with this:

We don't do homework. We'll give you some hints but not do the work.

 Convert an IP address from binary string to decimal format.There are
 some preconditions:
 1.IP address is in the form of '10010001100'.32 bits with no dot.
 2.int(string, base) is not allowed, You have to implement the conversion .
 3.Performance should be considered.
 
 For example an IP address like ' '
 would be converted to '255.255.255.255'
 
 That's how I implement it. But I think it looks ugly and I wonder if
 there is a better way to do this.
 
 import re
 import sys
 
 def convert(bin_ip):
 patt = re.compile(r'\d{8}')
 bin_list = patt.findall(str(bin_ip))

The question does not say Search a string for zero or more IP addresses
in binary and convert them all. It says to convert an IP address
written in binary to dotted format. So throw away the regular
expression. It's wrong anyway: it would accept input like this:

1234

and try to convert that to an IP address.


Think about the problem you have. You are given as input a string of 32
0 or 1 characters. Anything else is an error -- there is no need to go
searching through a string looking for sequences of digits.

Once you have 32 characters, you can split it into four lots of eight
characters. How? Not with a regular expression!!! That is like using a
sledge-hammer to crack a peanut. Use slicing:


 s =  + x*24
 s
''
 s[0:8]
''


So you can take one 32-character string and slice it into four
8-character strings. Then *each* of the substrings can be converted from
binary into decimal. If you find a character other than 0 or 1, stop
immediately and raise an exception.

In pseudo-code:

Is the string exactly 32 characters? If not, then FAIL.
Split the string into four groups of eight.
For each group of eight, convert from binary to decimal. If you find
something other than 0 or 1, then FAIL.
Join the four groups with dots.


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


Re: [Tutor] What is __weakref__ ?

2011-01-18 Thread bob gailer

On 1/18/2011 8:08 AM, Karim wrote:


I know this is ugly but until now it is the only way (with this side 
effect) I found to declare Enums class that I _understand_:


*class CategoryType(object):
Implements the enumeration and prevent associated newly created
instances to redefine enums value via special class variable 
'__slots__'

definition. It makes also, instances have no attributes at all.

__slots__ = ()

TRANSISTOR, MOS, BIPOLAR, CAPACITOR, RESISTOR, DIODE, ESD, PAD, \
COMPARATOR, OPAMP, ADC, DAC, PLL, OTHER = range(14)

def toString ( self, litteral ):
Convert the litteral integer number to its associated 
string representation.

if litteral == self.TRANSISTOR:
  return 'transistor'
elif litteral == self.MOS:
  return 'mos'
elif litteral == self.BIPOLAR:*
[...]

IMHO this just cries out for a dictionary:

class CategoryType(object):
  __slots__ = ()
  enums = {
0: 'transistor',
1: 'mos',
...
}
  def toString(self, literal):
Convert the literal integer number to its associated string 
representation.

return self.enums[literal]

Note this does not handle invalid literal.

If you are happy with range 14) you could alternatively use a list:
  enums = ['transistor, 'mos',  ...]

--
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] What is __weakref__ ?

2011-01-18 Thread Karim


Hello Bob,

Thanks for this better and simple approach!
To be perfect I would like to be able to do.
 type = CategoryType()
 type.TRANSISTOR
0
I will use your enums declaration and the range part to use as keys for 
enums.

And I will raise a KeyError in toString() to handle invalid litteral.

Thanks!

Regards
Karim


On 01/18/2011 02:24 PM, bob gailer wrote:

On 1/18/2011 8:08 AM, Karim wrote:


I know this is ugly but until now it is the only way (with this side 
effect) I found to declare Enums class that I _understand_:


*class CategoryType(object):
Implements the enumeration and prevent associated newly created
instances to redefine enums value via special class variable 
'__slots__'

definition. It makes also, instances have no attributes at all.

__slots__ = ()

TRANSISTOR, MOS, BIPOLAR, CAPACITOR, RESISTOR, DIODE, ESD, PAD, \
COMPARATOR, OPAMP, ADC, DAC, PLL, OTHER = range(14)

def toString ( self, litteral ):
Convert the litteral integer number to its associated 
string representation.

if litteral == self.TRANSISTOR:
  return 'transistor'
elif litteral == self.MOS:
  return 'mos'
elif litteral == self.BIPOLAR:*
[...]

IMHO this just cries out for a dictionary:

class CategoryType(object):
  __slots__ = ()
  enums = {
0: 'transistor',
1: 'mos',
...
}
  def toString(self, literal):
Convert the literal integer number to its associated string 
representation.

return self.enums[literal]

Note this does not handle invalid literal.

If you are happy with range 14) you could alternatively use a list:
  enums = ['transistor, 'mos',  ...]

--
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] Convert an IP address from binary to decimal

2011-01-18 Thread Tom Lin
于 2011-1-18 21:08, Christian Witts 写道:
 On 18/01/2011 14:45, Tom Lin wrote:
 Hi guys,

 Please help me with this:
 Convert an IP address from binary string to decimal format.There are
 some preconditions:
 1.IP address is in the form of '10010001100'.32 bits with no dot.
 2.int(string, base) is not allowed, You have to implement the conversion .
 3.Performance should be considered.

 For example an IP address like ' '
 would be converted to '255.255.255.255'

 That's how I implement it. But I think it looks ugly and I wonder if
 there is a better way to do this.

 import re
 import sys

 def convert(bin_ip):
 patt = re.compile(r'\d{8}')
 bin_list = patt.findall(str(bin_ip))

 dec_list = []
 for bin in bin_list:
 sum = 0
 i = 7
 for n in bin:
 if int(n):
 sum = sum + 2**i
 i = i - 1
 dec_list.append(str(sum))

 dec_ip = '.'.join(dec_list)
 print dec_ip

 if __name__ == '__main__':
 bin_ip = sys.argv[1:]
 convert(bin_ip)


 Thanks in advance and excuse my poor English.


 Best regards,
 Tom


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
   
 If I knew the input to be perfect it would be a simple matter
 '.'.join((str(int(input_ip[x:x+8], 2)) for x in range(4)))

Thanks, Chris. Your code looks pretty simple. Awesome!
But one precondition is not to use int(string, base). How would you
implement the conversion?
___
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 !

2011-01-18 Thread Steven D'Aprano

Alan G wrote:

Steven D'Aprano steve at pearwood.info writes:

fact that multiple inheritance itself is often the wrong thing to use, 
and even when it is right, it is often tricky to get it right. To put it 
another way: don't use multiple inheritance unless you have to, there 
are better ways, such as by composition.


Or use a language where MI is the normal and idiomatic way 
to do things because the language assumes it and so it just 
works. There are very few such languages but Lisp is one :-)


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?


I point you again at the series of articles by Michele Simionato in my 
previous post. The problems he points out with MI are mostly general to 
MI itself, and not to any one specific language implementation. Here's 
another one where he discusses implementing mixins without inheritance, 
and traits:


http://www.artima.com/weblogs/viewpost.jsp?thread=246488

One problem with MI (another is that it encourages huge hierarchies that 
are impractical to use) is when you have diamond diagrams. This is 
unavoidable in languages where all objects have a common superclass, 
such as Python (new style classes only) and Lisp.


http://en.wikipedia.org/wiki/Diamond_problem

As you can see from the multiple designs chosen by different languages, 
there is no one right way to resolve the problem, but it's generally 
agreed that the least-worst (or most desirable, if you prefer) 
resolution is the so-called C3 linearization pioneered in Dylan and 
implemented by Python.


The problem with Lisp's default linearization is that it is not 
monotomic. See this paper for details:

http://192.220.96.201/dylan/linearization-oopsla96.html

Converted to Python, if you have this class hierarchy:

class Boat(object): pass

class Dayboat(Boat): pass

class Wheelboat(Boat): pass

class Engineless(Dayboat): pass

class SmallMultihull(Dayboat): pass

class PedalWheelboat(Engineless, Wheelboat): pass

class SmallCatamaran(SmallMultihull): pass

class Pedalo(PedalWheelboat, SmallCatamaran): pass


both Dylan and Python generate the optimal linearization:

Pedalo, PedalWheelboat, Engineless, SmallCatamaran, SmallMultihull, 
Dayboat, Wheelboat, Boat, object


while CLOS gives (or at least gave, at the time the paper was written) 
the surprising result that Wheelboat is promoted ahead of Dayboat:


Pedalo, PedalWheelboat, Engineless, Wheelboat, SmallCatamaran, 
SmallMultihull, Dayboat, Boat, object


Why is this surprising? Because Dayboat is more specific in the 
linearization of *both* of Pedalo's direct parents, and yet it is less 
specific in Pedalo itself. This is a bad thing.



Both Lisp and Python/Dylan agree that there are potential class diagrams 
that are inconsistent. Here's an example from Python:


 class A(object):
... pass
...
 class B(object):
... pass
...
 class C(A, B):
... pass
...
 class D(B, A):
... pass
...
 class E(C, D):
... pass
...
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases B, A


This gives you a clue as to part of the problem with MI: in general, 
multiple inheritance is inconsistent. It's only usable at all by 
imposing certain restrictions on it. Clearly there are no such 
impossible or inconsistent class diagrams if you limit yourself to 
single inheritance. MI is inherently more complicated.




Sadly Python isn't, and when using MI I always avoid super()


Diamonds were rare in old-style classes in Python, but when they 
occurred, Python's MI was potentially buggy. That is, there were 
combinations of class order that lead to real bugs (although other 
combinations did not). Unfortunately, *all* new-style classes form a 
diamond in MI.


This is the problem that linearization solves for new-style classes, but 
only if you use super(). If you call the methods explicitly, you're 
doing it wrong, and it's only by chance (or by hard work, or by 
duplicating the logic of Python's MRO!) that your class hierarchy is not 
buggy.


One problem with MI is that automatic method resolution does not play 
well with manual method resolution, and so mixing classes that use 
super() with classes that don't is a recipe for trouble. This is not a 
problem with MI, or with super, but with the classes that don't use 
super(). So if you're avoiding super(), you're classes are unsafe for 
others to inherit from using MI.


In practice, unless you control the class and can take responsibility 
for it, or if it is documented as being MI friendly, you should never 
inherit from it except using single inheritance.




--
Steven

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


Re: [Tutor] Convert an IP address from binary to decimal

2011-01-18 Thread Steven D'Aprano
Tom Lin wrote:

 But one precondition is not to use int(string, base). How would you
 implement the conversion?

It's not hard. You have two choices: each character of the string
represents a bit, and should be either a 0 or a 1 (anything else is
an error). In which case its numeric value is either 0 or 1.

if bit == '0': value = 0
elif bit == '1': value = 1
else: raise ValueError(not a binary IP address)

If you are allowed to assume that the characters will only by 0 or 1,
and that there will never be anything else, you can do something even
smarter and faster. Here's a clue:

 total = 0
 s = abaa
 for c in s:
... total += (c == a)
...
 total
3

The boolean flags True and False are actually disguised numbers with
values 1 and 0.



-- 
Steven

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


Re: [Tutor] What is __weakref__ ?

2011-01-18 Thread bob gailer

On 1/18/2011 9:34 AM, Karim wrote:


Hello Bob,

Thanks for this better and simple approach!
To be perfect I would like to be able to do.
 type = CategoryType()
 type.TRANSISTOR


Add to the class:

def __getattr__(self, name):
return self.enums[name]

Note this does not handle the upper/lower case issue.

--
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


[Tutor] OOP question

2011-01-18 Thread Ben Ganzfried
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 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


Re: [Tutor] OOP question

2011-01-18 Thread Nick Stinemates
Updated inline. Check the updated definiton of winAmount.

Nick

On Tue, Jan 18, 2011 at 9:25 AM, Ben Ganzfried ben.ganzfr...@gmail.comwrote:

 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] What is __weakref__ ?

2011-01-18 Thread Karim

On 01/18/2011 03:38 PM, bob gailer wrote:


def __getattr__(self, name):
return self.enums[name]

Note this does not handle the upper/lower case issue.


Thanks for the trick!

works perfect!

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


[Tutor] Trying to import matplotlib : ImportError: No module named dateutil.rrule

2011-01-18 Thread Thierry Platini

Hello everyone,

I am completely new in learning python and have a little background in  
programing.


My problem occurs with the importation of matplotlib.

I did download the module from http://sourceforge.net/projects/matplotlib/ 
.


and I believe successfully install the corresping .egg file.


a simple import matplotlib does not generate any errors.
However, import matplotlib.pyplot  or import pylab does generate  
the following error.


Traceback (most recent call last):
  File stdin, line 1, in module
  File /Library/Frameworks/Python.framework/Versions/2.6/lib/ 
python2.6/site-packages/matplotlib.egg/pylab.py, line 1, in module

from matplotlib.pylab import *
  File /Library/Frameworks/Python.framework/Versions/2.6/lib/ 
python2.6/site-packages/matplotlib.egg/matplotlib/pylab.py, line 220,  
in module

from matplotlib import mpl  # pulls in most modules
  File /Library/Frameworks/Python.framework/Versions/2.6/lib/ 
python2.6/site-packages/matplotlib.egg/matplotlib/mpl.py, line 3, in  
module

from matplotlib import axes
  File /Library/Frameworks/Python.framework/Versions/2.6/lib/ 
python2.6/site-packages/matplotlib.egg/matplotlib/axes.py, line 19,  
in module

import matplotlib.dates as mdates
  File /Library/Frameworks/Python.framework/Versions/2.6/lib/ 
python2.6/site-packages/matplotlib.egg/matplotlib/dates.py, line 119,  
in module
from dateutil.rrule import rrule, MO, TU, WE, TH, FR, SA, SU,  
YEARLY, \

ImportError: No module named dateutil.rrule


I would really appreciate if someone can help me out with this.
As I said, I am pretty new and would need detailed explanation about  
how to solve the problem.

I might need to let you know that I'm working on mac OS X version 10.5.8


Thanks by advance,

Thierry

___
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 !

2011-01-18 Thread Karim

On 01/17/2011 11:36 PM, Dave Angel wrote:

On 01/-10/-28163 02:59 PM, Karim wrote:



Hello,

I implemented Observer DP on a listbox (Tkinter) as follows and I don't
understand why super() is not working and Observable.__init__(self) is
working, cf below:

class ListObservable(Listbox, Observable):
Creation de widget Listbox
def __init__(self):
super(ListObservable, self).__init__()
#Observable.__init__(self)
self._value = None
self.listeContenu = StringVar()
self.listeContenu.set(' '.join(sorted(data_base.keys(
self.liste = Listbox(listvariable=self.listeContenu, 
selectmode='single')

self.liste.grid(row=0, column=1, sticky=N+S+W)
self.liste.bind('Button-1', self.onSelect)

_The error is:_
Traceback (most recent call last):
File ./observerAppliGraphique.py, line 118, in module
app=App()
File ./observerAppliGraphique.py, line 37, in __init__
self.sujet.attach(self.nom)
File /home/karim/guiObserver/observable.py, line 11, in attach
self._observers.append(observer)
AttributeError: 'ListObservable' object has no attribute '_observers'

And the Observable class is:

class Observable(object):
Sujet a observer
def __init__(self):
self._observers = []
print('constructeur observable')

def attach(self, observer):
Attache un nouvel observateur
self._observers.append(observer)

def detach(self, observer):
Retire un nouvel observateur
self._observers.remove(observer)

def notify(self):
Avertit tous les observateurs que l'observable change d'etat
for observer in self._observers:
observer.update()

Just looking at what you supply, I'd suppose that ListBox doesn't 
correctly call super() in its own __init__() method.


So I took a look at Tkinter.py, and sure enough, it just calls its own 
direct parent, Widget.l


super() only works if all the classes you use are done consistently.

DaveA


Thanks for this precision!

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


Re: [Tutor] OOP question

2011-01-18 Thread Steven D'Aprano

Ben Ganzfried wrote:

Hey guys,

I'm trying to get a version of Roulette working and I had a quick question.


There's a quick answer and long answer. The quick answer is, you have to 
refer to self.odds, not just odds. The long answer is below:



Here is my code:

[...]

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?


The relevant method here is:

def winAmount(self, amount):
return odds*amount

This tells Python:

Look up the value for the name 'odds', and the name 'amount', multiply 
the two values, and return the result.


How does Python look up names? It looks in *namespaces* -- first it 
looks in the function's local namespace (local variables like 'amount'), 
then it looks in the global namespace (global variables), and finally it 
looks in the builtin namespace (built-in functions like len, min, max, 
etc). You get a NameError, because there is no name 'odds' in any of 
those namespaces.


To look for attributes, you have to look in the instance namespace:

return self.odds*amount

This looks up 'self' as above, and finds it as a local variable. Then it 
looks for an attribute 'odds' inside self, which is the result you want.


This is much more general than just methods inside classes. The same 
process occurs whenever you use attribute access:


import math
math.sin(x)

s = hello sailor
s.upper()



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


[Tutor] Problems passing a parameter in a GUI

2011-01-18 Thread David Holland
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.   








  ___
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 !

2011-01-18 Thread Steven D'Aprano

Steven D'Aprano wrote:

Alan G wrote:
Or use a language where MI is the normal and idiomatic way to do 
things because the language assumes it and so it just works. There are 
very few such languages but Lisp is one :-)


I'm sorry, but I don't believe you :/ 


That's a bit harsh... Let me rephrase that as I'm sorry, I find that 
hard to believe.




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


Re: [Tutor] What is __weakref__ ?

2011-01-18 Thread Steven D'Aprano

Karim wrote:

Same thing I don't know what to do with this object weakref :-[ as I 
don't know its meaning. That was my true question in fact;



http://mindtrove.info/python-weak-references/
http://www.doughellmann.com/PyMOTW-ja/weakref/index.html


--
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

2011-01-18 Thread James Reynolds
You have other issues at play here, but to answer your question from below:

You are calling the method reveal prior to creating the global object
secret_text

You can try moving:

x=   self.submit_bttn=Button(self, text=Submit, command=self.reveal(x))


below self.secret_text = 

You will also find that you have not defined something called
self.submit_bttn

and there are more errors beyond that, but that's past the scope of your
question.

On Tue, Jan 18, 2011 at 4:36 PM, David Holland davholla2...@yahoo.co.ukwrote:

 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.





 ___
 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

2011-01-18 Thread James Reynolds
I took another look at what you are trying to do, and if I wanted to count
the number of times the button was pressed in the same way you are trying I
would do something like this instead:

from Tkinter import *

class Application(Frame):

GUI App

def __init__(self, master):

Initialize the frame.

Frame.__init__(self, master)

self.grid()

self.create_widgets()

self.c = Counter()


 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):

self.c.a +=1

Calmexob = Calmex(self.c.a)

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 Counter:

a = 0


 class Calmex(object):



def __init__(self,a):

Default arg

self.a = a



def getmess(self):

mesg=Test + str(self.a)

return mesg


 #main

root=Tk()

root.title(Test)

root.geometry(400x300)

app=Application(root)

root.mainloop()


I haven't looked at the whole program, just what you have already done.
Really, all you want to do is keep track of how many times reveal gets
called, or at least I think that is what you are trying to do, so just add a
simple counter, which I chose to use an object to do.

I then passed the attribute holding this value to instantiate your Calmex
instance and once getmess gets called it should do the right thing.

This might not be what you are after though, at the end of the day.




On Tue, Jan 18, 2011 at 5:02 PM, James Reynolds eire1...@gmail.com wrote:

 You have other issues at play here, but to answer your question from below:

 You are calling the method reveal prior to creating the global object
 secret_text

 You can try moving:

  x=   self.submit_bttn=Button(self, text=Submit, command=self.reveal(x))


 below self.secret_text = 

 You will also find that you have not defined something called
 self.submit_bttn

 and there are more errors beyond that, but that's past the scope of your
 question.

 On Tue, Jan 18, 2011 at 4:36 PM, David Holland 
 davholla2...@yahoo.co.ukwrote:

 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)

Re: [Tutor] Problems passing a parameter in a GUI

2011-01-18 Thread Dave Angel

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 attribute, but you're trying to use it.  You should get in 
the habit of initializing all attributes in __init__().  Anyway, it 
looks like the only places that could get the error are inside the 
methods reveal() and selfinfo().  If either gets called before 
create_widget() finishes, you could get this error.


The stack trace would show just what combination of method calls is 
making this happen.


DaveA

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


Re: [Tutor] What is __weakref__ ?

2011-01-18 Thread Karim


Thanks for the links Steven!

Regards
Karim

On 01/18/2011 10:49 PM, Steven D'Aprano wrote:

Karim wrote:

Same thing I don't know what to do with this object weakref :-[ as I 
don't know its meaning. That was my true question in fact;



http://mindtrove.info/python-weak-references/
http://www.doughellmann.com/PyMOTW-ja/weakref/index.html




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