Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Karim

Le 07/04/2012 04:01, Dave Angel a écrit :

On 04/06/2012 03:19 PM, Karim wrote:

Le 06/04/2012 19:31, Alan Gauld a écrit :

On 06/04/12 09:47, Karim wrote:


If you have any idea to get the caller name inside the caller.


Its not normally very helpful since in Python the same function can
have many names:

def F(x):
return x*x

a = F
b = F
c - lambda y: F(y)

print F(1), a(2), b(3), c(4)

Now, why would knowing whether the caller used F,a or b
to call the same function object help? And what do you
do in the case of c()? Do you return 'c' or 'F'?

Maybe you could use it to tell you the context from
which they were calling? But in that case there are
usually better, more reliable, techniques
  - like examining the stackframe.

HTH,

Thanks Steven, Moduok and Steven for all your answers!

The reason is simple I wanted to optimize some code using pyuno for
openoffice.org doc generation I have several methods to set
text with "Heading 1", ... "Heading" title style:

def title(self, text='', style="Heading 1"):
  self._cursor_text.setPropertyValue('ParaStyleName', style)
  self.add_text(text)

def title1(self, text=''):
  self.title(text=text)

def title2(self, text=''):
  self.title(text='', style="Heading 2")

...

def title9(self, text=''):
  self.title(text='', style="Heading 9")

-


I just wanted to improve a little by doing something like that (pseudo
code):

def title9(self, text=''):
  self.title(text='', style="Heading " +.split()[1])


In short the number in the funtion name is the number of the title
depth in the document.
There is no big deal if Iit's not feasible;

Cheers
Karim




Those are methods, not functions.  So if you have a bunch of methods,
differing only in the numeric suffix their names have, and one of the
parameters to a method they each call, there's probably a simpler way.

First, you could create function objects (using approaches like
partial), turn them into methods, and attach them to a class with
generated names (somebody else will have to help you do it;  I just am
pretty sure it's possible)

Second, ifyou can control the code which will be calling these methods,
you could just have that code parameterize things a little differently.
For example, instead of calling

obj.title9("my text")

it might call
 obj.title(9, "my text")

where title() is a pretty simple, single method.




Thanks Dave,

In fact at first I did that:

obj.title(text='my text', heading=9)

But I wanted something more flashing to recognize and more simple to 
write because I've got a lot of call in my 1000 pages document creation.


I will take a look at partial.

Cheers

PS: By the I thanked Steven twice this one an mistake sorry and thank 
you Alan!

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


Re: [Tutor] Tkinter GUI crashing problem

2012-04-06 Thread Steven D'Aprano

myles broomes wrote:

Im working the Tkinter and I'm having a problem with the GUI I made. It
crashes whenever I hit the submit button. Heres my code:


Define "crashes". Does it:

* cause your computer to Blue Screen of Death?

* lock up your computer until you Ctrl-Alt-Delete?

* cause Windows to put up an error message saying the application has
  crashed and would you like to submit diagnostics to Microsoft?

* print a standard Python traceback?

* something else?


If you get a traceback, please COPY AND PASTE the entire traceback into an 
email and send that. Do not retype it by hand, summarise, simplify or 
otherwise reword it.





--
Steven

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


Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Dave Angel
On 04/06/2012 03:19 PM, Karim wrote:
> Le 06/04/2012 19:31, Alan Gauld a écrit :
>> On 06/04/12 09:47, Karim wrote:
>>
>>> If you have any idea to get the caller name inside the caller.
>>
>>
>> Its not normally very helpful since in Python the same function can
>> have many names:
>>
>> def F(x):
>>return x*x
>>
>> a = F
>> b = F
>> c - lambda y: F(y)
>>
>> print F(1), a(2), b(3), c(4)
>>
>> Now, why would knowing whether the caller used F,a or b
>> to call the same function object help? And what do you
>> do in the case of c()? Do you return 'c' or 'F'?
>>
>> Maybe you could use it to tell you the context from
>> which they were calling? But in that case there are
>> usually better, more reliable, techniques
>>  - like examining the stackframe.
>>
>> HTH,
>
> Thanks Steven, Moduok and Steven for all your answers!
>
> The reason is simple I wanted to optimize some code using pyuno for
> openoffice.org doc generation I have several methods to set
> text with "Heading 1", ... "Heading " title style:
>
> def title(self, text='', style="Heading 1"):
>  self._cursor_text.setPropertyValue('ParaStyleName', style)
>  self.add_text(text)
>
> def title1(self, text=''):
>  self.title(text=text)
>
> def title2(self, text=''):
>  self.title(text='', style="Heading 2")
>
> ...
>
> def title9(self, text=''):
>  self.title(text='', style="Heading 9")
>
> -
>
>
> I just wanted to improve a little by doing something like that (pseudo
> code):
>
> def title9(self, text=''):
>  self.title(text='', style="Heading " + .split()[1])
>
>
> In short the number in the funtion name is the number of the title
> depth in the document.
> There is no big deal if Iit's not feasible;
>
> Cheers
> Karim
>
>
>

Those are methods, not functions.  So if you have a bunch of methods,
differing only in the numeric suffix their names have, and one of the
parameters to a method they each call, there's probably a simpler way.

First, you could create function objects (using approaches like
partial), turn them into methods, and attach them to a class with
generated names (somebody else will have to help you do it;  I just am
pretty sure it's possible)

Second, ifyou can control the code which will be calling these methods,
you could just have that code parameterize things a little differently. 
For example, instead of calling

   obj.title9("my text")

it might call
obj.title(9, "my text")

where title() is a pretty simple, single method.





-- 

DaveA

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


Re: [Tutor] Emailing code

2012-04-06 Thread Steven D'Aprano

myles broomes wrote:

This question isnt so much related to a specific program, just something im
curious about. What is the best way to email code? I find that when i copy
and paste it into an email, the indentation and spacing gets all messed up.


Don't send HTML, because that is poison to correct formatting of Python code. 
Mail clients feel free to reformat indentation and spacing in arbitrary ways 
with HTML emails. Find the setting in your mail client to turn off all 
formatting. My guess is that you are using Outlook or Outlook Express -- look 
for the setting about "Rich Text", and turn that off.


If you can't turn it off, at least turn on the option to send both HTML (rich 
text) and plain text. The HTML version will probably be mangled, but the plain 
text version should be okay.


If you are using Thunderbird, turning off format=flowed can sometimes help.

http://kb.mozillazine.org/Plain_text_e-mail_%28Thunderbird%29

For large amounts of code, don't copy and paste into your email. Nearly every 
email program in the world allows you to attach attachments. Just attach the 
.py file and send that.





--
Steven

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


Re: [Tutor] Tkinter GUI crashing problem

2012-04-06 Thread Alan Gauld

On 06/04/12 23:07, myles broomes wrote:


Im working the Tkinter and I'm having a problem with the GUI I made.

> It crashes whenever I hit the submit button. Heres my code:

What do you mean by crashes?
It looks to me like it should lock up rather than  crash.
Your update_txt method goes into an infinite loop:

>  while self.guess_ent.get():
>  guess = self.guess_ent.get()
>  if int(guess)>  number:
>  message += "Lower..."
>  self.txt_box.delete(0.0,END)
>  self.txt_box.insert(0.0,message)
>  else:
>  message += "Higher..."
>  self.txt_box.delete(0.0,END)
>  self.txt_box.insert(0.0,message)
>

Since this never exits the screen never refreshes to allow the user to 
enter a new value in the guess_ent box.


loops inside event handling methods are always risky and should be 
avoided if at all possible. Open ended while loops are especially prone 
to non termination. If you need to process something repeatedly its 
usually better in a GUI program to do it via a timer event that 
continually calls the function. (Or using a background thread but thats 
a whole different can of worms)


But in this case the whole while loop is completely redundant. Tkinter 
provides an event loop, you just need to handle one guess at a time and 
let the user press submit each time.


If you really are crashing please run the code from inside an OS console 
and capture the stack trace and send it to us.


--
Alan G
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] How do you save work in progress in Pyscripter ?

2012-04-06 Thread Alan Gauld

On 06/04/12 20:11, Thomas Mujica wrote:


*Was able to write and successfully run this but I can’t seem to be able
to “save” it*


You have written the code at the interactive prompt which is really 
intended for experimenting.


To save the code as a python script you need to create a plain text
file and save it with a .py extension (assuming you are on Windows).

You could use something as basic as Notepad to create it but most IDEs 
or specialist programming editors will be more useful. I don;t know 
pyscripter but usually you can use the File->New menu to create a new 
blank editor window where you can type the code. You then save it with 
File->Save/SaveAs as usual. How you run it varies by IDE...


Note you don't type the >>> and other stuff that the interpreter puts 
out, so your code would look like:


from decimal import *

getcontext().prec=3 #change decimal precision to 3 decimal places*

for a in xrange(1,30): #NESTED note no matter what the variables
for b in xrange(1,30): #LOOPS DO 29 ITERATIONS*
c=pow(a,2)+pow(b,2) # this is really c squared because a and b are
h=pow(c,.5) #pow is .5 so h is the square root of c …..h is the
d=round(h,2)
if a==1 and b==1: #will allow titles to be printed at beginning of
   print "Side a","Side b","Hypotenuse"
if h==d and d==h: #will eliminate all values of h that are not
   print" ",a,"\t",b,"\t\t",h #for alignment purpose “3 spaces “


There are some odd things in that code that I could comment on, but for 
now I'll ignore them and see if you can get the script running first.


HTH
--
Alan G
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] Tkinter GUI crashing problem

2012-04-06 Thread Emile van Sebille

On 4/6/2012 3:07 PM myles broomes said...


import random
from tkinter import *



What version of python on what platform please...

Emile



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


[Tutor] Tkinter GUI crashing problem

2012-04-06 Thread myles broomes

Im working the Tkinter and I'm having a problem with the GUI I made. It crashes 
whenever I hit the submit button. Heres my code:



#Guess my number 2.0
#The guess my number game but using a GUI

 

import random
from tkinter import *


class Application(Frame):
"""GUI to hold widgets. """
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.createWidgets()

 

def createWidgets(self):
"""GUI widgets. """
#Label instructing the user to take a guess
Label(self,text="Take a guess:").grid(row=0,column=0)

#Entry widget where the user makes a guess
self.guess_ent = Entry(self)
self.guess_ent.grid(row=1,column=0)

#Button that updates the text box
self.submit_bttn = 
Button(self,command=self.update_txt,text="Submit guess")
self.submit_bttn.grid(row=2,column=0)

#Text box to update the user on whether their guess is correct
self.txt_box = Text(self,width=35,height=5,wrap=WORD)
self.txt_box.grid(row=3,column=0)

 

def update_txt(self):
"""Updates the text box widget. """
#Get user input from the entry widget
number = random.randint(1,100)
message = ""
guess = None
while self.guess_ent.get():
guess = self.guess_ent.get()
if int(guess) > number:
message += "Lower..."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)
else:
message += "Higher..."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)

if int(guess) == number:
message += "Congrarulations! You guessed correctly! A 
new number has been generated."
self.txt_box.delete(0.0,END)
self.txt_box.insert(0.0,message)

 

#main
root = Tk()
root.title("Guess my number")
app = Application(root)
root.mainloop()

 

Its frustrating because I dont even get an error code. I coded the last 
function in a seperate program without a GUI and it runs fine so I have no clue 
what the problem can be. Any help would be much appreciated.


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


Re: [Tutor] Emailing code

2012-04-06 Thread wesley chun
On Fri, Apr 6, 2012 at 12:14 PM, myles broomes
 wrote:
> This question isnt so much related to a specific program, just something im
> curious about. What is the best way to email code? I find that when i copy
> and paste it into an email, the indentation and spacing gets all messed up.


as an alternative to emailing code, you can paste(bin) it online and
just send (or tweet or G+ or otherwise post) the URL... there are many
providers of this type of service out there:
http://www.similarsitesearch.com/alternatives-to/pastebin.com

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun : wescpy at gmail : @wescpy/+wescpy
    Python training & consulting : http://CyberwebConsulting.com
    "Core Python" books : http://CorePython.com
    Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How do you save work in progress in Pyscripter ?

2012-04-06 Thread Thomas Mujica
Please help a newbie

 

Was able to write and successfully run this but I can't seem to be able to
"save" it

 

Luckily I had saved it to Word and then I was able to copy and paste it back
into PyScripter.

 

I'm using 

 

Python Scripter Version 2.5.3.0 x86

 

*** Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32. ***

*** Remote Python engine  is active ***

>>> 

 

>>> from decimal import *

>>> getcontext()

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-9, Emax=9,
capitals=1, flags=[], traps=[DivisionByZero, InvalidOperation, Overflow])

>>> getcontext().prec=3#change decimal precision to 3
decimal places

>>> for a in xrange(1,30): #NESTED   note no matter what the variables are
the syntax is always xrange

... for b in xrange(1,30): #LOOPS DO 29 ITERATIONS

... c=pow(a,2)+pow(b,2)# this is really c squared
because a and b are raised to exponent 2

... h=pow(c,.5)   #pow is .5 so h is the
square root of c ...h is the hypotenuse

... d=round(h,2)

... if a==1 and b==1: #will allow titles to be
printed at beginning of print out

... print "Side a","Side b","Hypotenuse"

... if h==d and d==h:#will eliminate all values of h
that are not integers from printout

... print"  ",a,"\t",b,"\t\t",h  #for alignment purpose "3
spaces " before a then tab b, and 2tabs   ...
# before h

 

 

 

Side a Side b Hypotenuse

3  4  5.0

4  3  5.0

5  1213.0

6  8  10.0

7  2425.0

8  6  10.0

8  1517.0

9  1215.0

102426.0

125  13.0

129  15.0

121620.0

158  17.0

152025.0

161220.0

182430.0

201525.0

202129.0

212029.0

212835.0

247  25.0

241026.0

241830.0

282135.0

 

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


Re: [Tutor] Emailing code

2012-04-06 Thread Emile van Sebille

On 4/6/2012 12:14 PM myles broomes said...

This question isnt so much related to a specific program, just something
im curious about. What is the best way to email code? I find that when i
copy and paste it into an email, the indentation and spacing gets all
messed up.




Set your email client content type to deliver plain text -- anything 
html-ish allows compression of spaces hence the formatting loss.


Emile

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


Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Karim

Le 06/04/2012 19:31, Alan Gauld a écrit :

On 06/04/12 09:47, Karim wrote:


If you have any idea to get the caller name inside the caller.



Its not normally very helpful since in Python the same function can 
have many names:


def F(x):
   return x*x

a = F
b = F
c - lambda y: F(y)

print F(1), a(2), b(3), c(4)

Now, why would knowing whether the caller used F,a or b
to call the same function object help? And what do you
do in the case of c()? Do you return 'c' or 'F'?

Maybe you could use it to tell you the context from
which they were calling? But in that case there are
usually better, more reliable, techniques
 - like examining the stackframe.

HTH,


Thanks Steven, Moduok and Steven for all your answers!

The reason is simple I wanted to optimize some code using pyuno for 
openoffice.org doc generation I have several methods to set

text with "Heading 1", ... "Heading " title style:

def title(self, text='', style="Heading 1"):
 self._cursor_text.setPropertyValue('ParaStyleName', style)
 self.add_text(text)

def title1(self, text=''):
 self.title(text=text)

def title2(self, text=''):
 self.title(text='', style="Heading 2")

...

def title9(self, text=''):
 self.title(text='', style="Heading 9")

-

I just wanted to improve a little by doing something like that (pseudo 
code):


def title9(self, text=''):
 self.title(text='', style="Heading " + .split()[1])


In short the number in the funtion name is the number of the title depth 
in the document.

There is no big deal if Iit's not feasible;

Cheers
Karim








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


[Tutor] Emailing code

2012-04-06 Thread myles broomes

This question isnt so much related to a specific program, just something im 
curious about. What is the best way to email code? I find that when i copy and 
paste it into an email, the indentation and spacing gets all messed up.
Myles Broomes
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to use g_timeout_add () function?

2012-04-06 Thread Mark Lawrence

Please don't top post and please reply to the list

Top posting fixed.

- Forwarded Message -
From: Lion Chen 
To: Mark Lawrence 
Cc:
Sent: Friday, 6 April 2012, 16:43
Subject: Re: [Tutor] How to use g_timeout_add () function?

fixed top posting

> On 06/04/2012 15:17, Lion Chen wrote:
>> Hello all, i have a question:
>>
>> when i check gtk_time_out in the gtk+2 reference, it said "
>> |gtk_timeout_add|has been deprecated since version 2.4 and should not be
>> used in newly-written code. Use |g_timeout_add()|instead."
>>
>> but i don't know how tu use the g_timout_add() function:
>> my_id = g_timeout_add(500, myfunction())
>>
>> or:
>>
>> my_id = gtk.g_timeout_add(500, myfunction())
>>
>> everytime i run the program, it prompted me a message like modules do
>> not have g_timeout_add() attribute.
>>
>> so i still have to use gtk_timeout_add
>>
>> anybody help me?
>>
>> Lion Chen
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> It's much easier for us to help if you provide an exact snippet of
> code that reproduces the problem with the error message cut and
> pasted. Having said that there's nothing to stop you using
> gtk_timeout_add as it's only deprecated, i.e. it's been marked for
> removal at some time in the future.
>

the problem is solved.
in Python,  should use gobject.timeout_add() replace the g_timeout_add()
g_timeout_add() is for c.

--
Cheers.

Mark Lawrence.


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


Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Alan Gauld

On 06/04/12 09:47, Karim wrote:


If you have any idea to get the caller name inside the caller.



Its not normally very helpful since in Python the same function can have 
many names:


def F(x):
   return x*x

a = F
b = F
c - lambda y: F(y)

print F(1), a(2), b(3), c(4)

Now, why would knowing whether the caller used F,a or b
to call the same function object help? And what do you
do in the case of c()? Do you return 'c' or 'F'?

Maybe you could use it to tell you the context from
which they were calling? But in that case there are
usually better, more reliable, techniques
 - like examining the stackframe.

HTH,
--
Alan G
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] which gets called

2012-04-06 Thread John Fabiani
On Friday, April 06, 2012 06:54:28 AM John Fabiani wrote:
> Hi,
> 
> I want to create a class that inherits two other classes.
> 
> class NewClass( A,B)
> 
> But both "A" and "B" contain a method with the same name ("onKeyDown").
> 
> If my "NewClass" does not contain something to override the methods which
> one would be called if
> 
> myinstance = NewClass()
> 
> myinstance.onKeyDown()
> 
> 
> Second to insure the right one is called is it possible to do the following
> 
> NewClass(object):
> 
>   def onKeyDown(self, event):
>   b.onKeyDown(event)
> 
> Johnf

Thanks guys!

The class I'm creating is inheriting from classes I did not create.  And of 
course the inherited classes are from different authors.  So I'm attempting to 
create a wrapper and the problem comes from the keyboard events.  Each of the 
classes has a onKeyDown method and I only want one to work and then pass the 
data to the second.

But you have helped (along with the links).  And I have successfully got the 
right method called.  The issue is now getting the second (B) to fire 
correctly.

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


Re: [Tutor] which gets called

2012-04-06 Thread Mark Lawrence

On 06/04/2012 14:54, John Fabiani wrote:

Hi,

I want to create a class that inherits two other classes.

class NewClass( A,B)

But both "A" and "B" contain a method with the same name ("onKeyDown").

If my "NewClass" does not contain something to override the methods which one
would be called if

myinstance = NewClass()

myinstance.onKeyDown()



Please see 
http://docs.python.org/tutorial/classes.html#multiple-inheritance.  This 
references http://www.python.org/download/releases/2.3/mro/  Having read 
these why not try typing code into the interactive prompt and see what 
happens?  Worst case you get an exception, if you don't understand it 
cut and paste it to a reply to this and we'll help out.




Second to insure the right one is called is it possible to do the following

NewClass(object):

   def onKeyDown(self, event):
   b.onKeyDown(event)


It's B.onKeyDown(self, event), without the self you'll get an unbound 
method error.




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




--
Cheers.

Mark Lawrence.

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


Re: [Tutor] How to use g_timeout_add () function?

2012-04-06 Thread Mark Lawrence

On 06/04/2012 15:17, Lion Chen wrote:

Hello all, i have a question:

when i check gtk_time_out in the gtk+2 reference, it said "
|gtk_timeout_add|has been deprecated since version 2.4 and should not be
used in newly-written code. Use |g_timeout_add()|instead."

but i don't know how tu use the g_timout_add() function:
my_id = g_timeout_add(500, myfunction())

or:

my_id = gtk.g_timeout_add(500, myfunction())

everytime i run the program, it prompted me a message like modules do
not have g_timeout_add() attribute.

so i still have to use gtk_timeout_add

anybody help me?

Lion Chen

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


It's much easier for us to help if you provide an exact snippet of code 
that reproduces the problem with the error message cut and pasted. 
Having said that there's nothing to stop you using gtk_timeout_add as 
it's only deprecated, i.e. it's been marked for removal at some time in 
the future.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] which gets called

2012-04-06 Thread Brian van den Broek
On 6 April 2012 15:54, John Fabiani  wrote:
> Hi,
>
> I want to create a class that inherits two other classes.
>
> class NewClass( A,B)
>
> But both "A" and "B" contain a method with the same name ("onKeyDown").
>
> If my "NewClass" does not contain something to override the methods which one
> would be called if
>
> myinstance = NewClass()
>
> myinstance.onKeyDown()


Hi John,

Easy enough to sort out with a little experiment:

>>> class A(object):
def doit(self):
print "A"


>>> class B(object):
def doit(self):
print "B"


>>> class C(A,B):
def __init__(self):
self.doit()


>>> c=C()
A

> Second to insure the right one is called is it possible to do the following
>
> NewClass(object):
>
>  def onKeyDown(self, event):
>      b.onKeyDown(event)
>

Perhaps this helps, some:

>>> class D(A,B):
def __init__(self):
self.doit()

def doit(self):
print "D"
super(D, self).doit()


>>> d=D()
D
A
>>> class E(A,B):
def __init__(self):
B.doit(self)


>>> e=E()
B
>>>


Best,

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


Re: [Tutor] which gets called

2012-04-06 Thread Steven D'Aprano

John Fabiani wrote:

Hi,

I want to create a class that inherits two other classes.

class NewClass( A,B)

But both "A" and "B" contain a method with the same name ("onKeyDown"). 

If my "NewClass" does not contain something to override the methods which one 
would be called if


myinstance = NewClass()

myinstance.onKeyDown()



This depends on whether classes A and B are designed for cooperative multiple 
inheritance or not.


The short answer is, A.onKeyDown will be called, because A is listed first.

The longer answer is, if A.onKeyDown uses super() to manager multiple 
inheritance, both A and B.onKeyDown may be called.



Here is an example with no cooperative multiple inheritance:

class A(object):
def onKeyDown(self):
print('A deals with keydown event')

class B(object):
def onKeyDown(self):
print('B deals with keydown event')

class NewClass(A, B):
pass


And in use, you will see that A blocks B:

py> instance = NewClass()
py> instance.onKeyDown()
A deals with keydown event



And here is a second example using super() for cooperative multiple inheritance:

class A(object):
def onKeyDown(self):
print('A deals with keydown event')
super(A, self).onKeyDown()
# in Python 3, you can just use "super().onKeyDown()"

class B(object):
def onKeyDown(self):
print('B deals with keydown event')
# B does not call super(), because there are no
# further parent classes to call.

class NewClass(A, B):
pass


And in use:


py> instance = NewClass()
py> instance.onKeyDown()
A deals with keydown event
B deals with keydown event






Second to insure the right one is called is it possible to do the following

NewClass(object):
  def onKeyDown(self, event):
  b.onKeyDown(event)


Yes, but that normally should not be necessary if you design your classes 
carefully.




--
Steven

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


Re: [Tutor] How to use g_timeout_add () function?

2012-04-06 Thread Evert Rol
> Hello all,  i have a question:
> 
> when i check gtk_time_out in the gtk+2 reference, it said " gtk_timeout_add 
> has been deprecated since version 2.4 and should not be used in newly-written 
> code. Use g_timeout_add() instead."
> 
> but i don't know how tu use the g_timout_add() function:
> my_id = g_timeout_add(500, myfunction())
> 
> or:
> 
> my_id = gtk.g_timeout_add(500, myfunction())
> 
> everytime i run the program, it prompted me a message like modules do not 
> have g_timeout_add() attribute.

Although the error is reasonably clear, it's always good to specify the whole 
traceback (copy-paste it). It could show other (non)obvious mistakes.

If your module does not have g_timeout_add, then either your module is old, or 
it is in another (sub)module. You would have to search the documentation for 
the latter.
You actually don't say whether the above statement comes from the GTK2 
documentation, or the pygtk documentation; I would assume the latter, since 
that makes more sense. But if the former, GTK and PyGTK may not be 100% in sync.


> so i still have to use gtk_timeout_add
> 
> anybody help me? 


PyGTK has its own mailing list (and even an IRC channel), which may be more 
practical in this specific case. Have a look over there: 
http://www.pygtk.org/feedback.html

Cheers,

  Evert


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


Re: [Tutor] which gets called

2012-04-06 Thread Evert Rol
> Hi,
> 
> I want to create a class that inherits two other classes.
> 
> class NewClass( A,B)
> 
> But both "A" and "B" contain a method with the same name ("onKeyDown"). 
> 
> If my "NewClass" does not contain something to override the methods which one 
> would be called if
> 
> myinstance = NewClass()
> 
> myinstance.onKeyDown()
> 

If I remember correctly, A.onKeyDown.
But things can get more complicated in other cases. See also the following post 
for a read on the MRO (method resolution order); could help to clarify things 
(or confuse you further): 
http://python-history.blogspot.com/2010/06/method-resolution-order.html


> Second to insure the right one is called is it possible to do the following
> 
> NewClass(object):
> 
>  def onKeyDown(self, event):
>  b.onKeyDown(event)

What is b here? Are you (trying to) save(ing) a parent as a instance in the 
class? Or should that be uppercase B?
The latter would work, I think, though you'll have to put `self` here 
explicitly (since you're calling it without an instance, *and* you want to tell 
the method the instance is NewClass() instead of eg B()):

  def onKeyDown(self, event):
  B.onKeyDown(self, event)


Cheers,

  Evert

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


Re: [Tutor] a problem with a server and client

2012-04-06 Thread Khalid Al-Ghamdi
actually, you are right... in addition to that problem, there was the
encodeing/decoding issue you mentioned on both the client and server.

thanks

On Fri, Apr 6, 2012 at 5:08 PM, Khalid Al-Ghamdi wrote:

> yah i did the search, and tried the solution, but it didn't work nice
> of you to have tried, though...
>
> anyhow, i found where the problem is... on the client side it should be
> connect() instead of bind() in :
>
> > tcpCliSock.bind(ADDR)
>
> thanks
>
>
> On Fri, Apr 6, 2012 at 4:59 PM, Evert Rol  wrote:
>
>> > i'm trying to implement a server that adds a time stamp to incoming
>> text form a client.
>> >
>> > the server's code is (but doesn't seem to have the problem as demoed by
>> the error below:
>> >
>> > from socket import *
>> > from time import ctime
>> >
>> > HOST = ''
>> > PORT = 21567
>> > BUFSIZ = 1024
>> > ADDR =(HOST, PORT)
>> >
>> > tcpSerSock = socket(AF_INET, SOCK_STREAM)
>> >
>> > tcpSerSock.bind(ADDR)
>> > tcpSerSock.listen(5)
>> >
>> > while True:
>> > print('waiting for connection ...')
>> > tcpCliSock, addr =tcpSerSock.accept()
>> > print('...connected from: ', addr)
>> >
>> > while True:
>> > data = tcpCliSock.recv(BUFSIZ)
>> > if not data:
>> > break
>> > tcpCliSock.send('[{}] {}'.format(bytes(ctime(), 'utf-8'),data))
>> >
>> > tcpCliSock.close()
>> > tcpSerSock.close()
>> >
>> >
>> >
>> > the client's code is:
>> >
>> > from socket import *
>> >
>> >
>> > HOST = 'localhost'
>> > PORT = 21567
>> > BUFSIZ = 1024
>> > ADDR =(HOST, PORT)
>> >
>> > tcpCliSock = socket(AF_INET, SOCK_STREAM)
>> >
>> > tcpCliSock.bind(ADDR)
>> >
>> > while True:
>> > data=input('> ')
>> > if not data:
>> > break
>> > tcpCliSock.send(data)
>> > data = tcpCliSock.recv(BUFSIZ)
>> > if not data:
>> > break
>> > print(data.decode('utf-8'))
>> >
>> > tcpCliSock.close()
>> >
>> > the problem is i get the following error when i enter some text:
>> >
>> > Traceback (most recent call last):
>> >   File "C:\Python32\tsTclnt3.py", line 17, in 
>> > tcpCliSock.send(data)
>> > TypeError: 'str' does not support the buffer interface
>>
>> Did you try to search on the error string? That would have gotten you the
>> solution (even) faster.
>> The first two Google hits (and probably all the rest of them), tell me
>> that Python 3's socket.send() method wants bytes as input, not str. See
>> http://docs.python.org/py3k/library/socket.html#socket.socket.send
>>
>> Hope that helps,
>>
>>  Evert
>>
>>
>> >
>> > can you help?
>> > ___
>> > 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


[Tutor] How to use g_timeout_add () function?

2012-04-06 Thread Lion Chen
Hello all, i have a question:

when i check gtk_time_out in the gtk+2 reference, it said "
|gtk_timeout_add|has been deprecated since version 2.4 and should not be
used in newly-written code. Use |g_timeout_add()|instead."

but i don't know how tu use the g_timout_add() function:
my_id = g_timeout_add(500, myfunction())

or:

my_id = gtk.g_timeout_add(500, myfunction())

everytime i run the program, it prompted me a message like modules do
not have g_timeout_add() attribute.

so i still have to use gtk_timeout_add

anybody help me?

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


Re: [Tutor] a problem with a server and client

2012-04-06 Thread Khalid Al-Ghamdi
yah i did the search, and tried the solution, but it didn't work nice
of you to have tried, though...

anyhow, i found where the problem is... on the client side it should be
connect() instead of bind() in :

> tcpCliSock.bind(ADDR)

thanks

On Fri, Apr 6, 2012 at 4:59 PM, Evert Rol  wrote:

> > i'm trying to implement a server that adds a time stamp to incoming text
> form a client.
> >
> > the server's code is (but doesn't seem to have the problem as demoed by
> the error below:
> >
> > from socket import *
> > from time import ctime
> >
> > HOST = ''
> > PORT = 21567
> > BUFSIZ = 1024
> > ADDR =(HOST, PORT)
> >
> > tcpSerSock = socket(AF_INET, SOCK_STREAM)
> >
> > tcpSerSock.bind(ADDR)
> > tcpSerSock.listen(5)
> >
> > while True:
> > print('waiting for connection ...')
> > tcpCliSock, addr =tcpSerSock.accept()
> > print('...connected from: ', addr)
> >
> > while True:
> > data = tcpCliSock.recv(BUFSIZ)
> > if not data:
> > break
> > tcpCliSock.send('[{}] {}'.format(bytes(ctime(), 'utf-8'),data))
> >
> > tcpCliSock.close()
> > tcpSerSock.close()
> >
> >
> >
> > the client's code is:
> >
> > from socket import *
> >
> >
> > HOST = 'localhost'
> > PORT = 21567
> > BUFSIZ = 1024
> > ADDR =(HOST, PORT)
> >
> > tcpCliSock = socket(AF_INET, SOCK_STREAM)
> >
> > tcpCliSock.bind(ADDR)
> >
> > while True:
> > data=input('> ')
> > if not data:
> > break
> > tcpCliSock.send(data)
> > data = tcpCliSock.recv(BUFSIZ)
> > if not data:
> > break
> > print(data.decode('utf-8'))
> >
> > tcpCliSock.close()
> >
> > the problem is i get the following error when i enter some text:
> >
> > Traceback (most recent call last):
> >   File "C:\Python32\tsTclnt3.py", line 17, in 
> > tcpCliSock.send(data)
> > TypeError: 'str' does not support the buffer interface
>
> Did you try to search on the error string? That would have gotten you the
> solution (even) faster.
> The first two Google hits (and probably all the rest of them), tell me
> that Python 3's socket.send() method wants bytes as input, not str. See
> http://docs.python.org/py3k/library/socket.html#socket.socket.send
>
> Hope that helps,
>
>  Evert
>
>
> >
> > can you help?
> > ___
> > 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


[Tutor] which gets called

2012-04-06 Thread John Fabiani
Hi,

I want to create a class that inherits two other classes.

class NewClass( A,B)

But both "A" and "B" contain a method with the same name ("onKeyDown"). 

If my "NewClass" does not contain something to override the methods which one 
would be called if

myinstance = NewClass()

myinstance.onKeyDown()


Second to insure the right one is called is it possible to do the following

NewClass(object):

  def onKeyDown(self, event):
  b.onKeyDown(event)

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


Re: [Tutor] a problem with a server and client

2012-04-06 Thread Evert Rol
> i'm trying to implement a server that adds a time stamp to incoming text form 
> a client.
> 
> the server's code is (but doesn't seem to have the problem as demoed by the 
> error below:
> 
> from socket import *
> from time import ctime
> 
> HOST = ''
> PORT = 21567
> BUFSIZ = 1024
> ADDR =(HOST, PORT)
> 
> tcpSerSock = socket(AF_INET, SOCK_STREAM)
> 
> tcpSerSock.bind(ADDR)
> tcpSerSock.listen(5)
> 
> while True:
> print('waiting for connection ...')
> tcpCliSock, addr =tcpSerSock.accept()
> print('...connected from: ', addr)
> 
> while True:
> data = tcpCliSock.recv(BUFSIZ)
> if not data:
> break
> tcpCliSock.send('[{}] {}'.format(bytes(ctime(), 'utf-8'),data))
> 
> tcpCliSock.close()
> tcpSerSock.close()
> 
> 
> 
> the client's code is:
> 
> from socket import *
> 
> 
> HOST = 'localhost'
> PORT = 21567
> BUFSIZ = 1024
> ADDR =(HOST, PORT)
> 
> tcpCliSock = socket(AF_INET, SOCK_STREAM)
> 
> tcpCliSock.bind(ADDR)
> 
> while True:
> data=input('> ')
> if not data:
> break
> tcpCliSock.send(data)
> data = tcpCliSock.recv(BUFSIZ)
> if not data:
> break
> print(data.decode('utf-8'))
> 
> tcpCliSock.close()
> 
> the problem is i get the following error when i enter some text:
> 
> Traceback (most recent call last):
>   File "C:\Python32\tsTclnt3.py", line 17, in 
> tcpCliSock.send(data)
> TypeError: 'str' does not support the buffer interface

Did you try to search on the error string? That would have gotten you the 
solution (even) faster.
The first two Google hits (and probably all the rest of them), tell me that 
Python 3's socket.send() method wants bytes as input, not str. See 
http://docs.python.org/py3k/library/socket.html#socket.socket.send

Hope that helps,

  Evert


> 
> can you help?
> ___
> 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


[Tutor] a problem with a server and client

2012-04-06 Thread Khalid Al-Ghamdi
hi,

i'm trying to implement a server that adds a time stamp to incoming text
form a client.

the server's code is (but doesn't seem to have the problem as demoed by the
error below:

from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR =(HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)

tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
print('waiting for connection ...')
tcpCliSock, addr =tcpSerSock.accept()
print('...connected from: ', addr)

while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send('[{}] {}'.format(bytes(ctime(), 'utf-8'),data))

tcpCliSock.close()
tcpSerSock.close()



the client's code is:

from socket import *


HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR =(HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)

tcpCliSock.bind(ADDR)

while True:
data=input('> ')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf-8'))

tcpCliSock.close()

the problem is i get the following error when i enter some text:

Traceback (most recent call last):
  File "C:\Python32\tsTclnt3.py", line 17, in 
tcpCliSock.send(data)
TypeError: 'str' does not support the buffer interface

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


Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Modulok
On 4/6/12, Karim  wrote:
>
> Hello all,
>
>
> I have :
>
>  def foo():
> print( getattr(foo, 'func_name'))
>
> Where I get the name of the function but I add to give the name of this
> function. Indeed it is not very helpful...
> I checked the globals() but how I can do to get
> globals()['toto'].func_name. This is not more helpful ;o)
>
> If you have any idea to get the caller name inside the caller.
>

The following works, but only on implementations which provide stack frame
support. As the docs kindly point out:

"...this isn't guaranteed to exist in all implementations of Python."

Example code:


import inspect
def foo():
'''Print my own name.'''
frame_info = inspect.getframeinfo(inspect.currentframe())
print(frame_info.function)


foo()


That said, there's probably a better way to solve whatever bigger problem
you're trying solve.

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


Re: [Tutor] Is socket.socket() a func or a class

2012-04-06 Thread Steven D'Aprano

Khalid Al-Ghamdi wrote:

hi all,

I'm reading this book that says when creating a socket you have to use the
socket.socket() *function *as in :

ss=socket.socket()

but whey i check they type it says it's a class which makes sense cause
you're creating a socket object.


type(ss)



so, which is it? or do authors loosely use these terms interchangeably in
this context?


Yes. Sometimes people call things a function when technically they mean a 
"callable". A callable is anything that can be called like a function: 
functions, classes, types, methods, etc.



--
Steven

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


Re: [Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Steven D'Aprano

Karim wrote:


Hello all,


I have :

def foo():
   print( getattr(foo, 'func_name'))


Why not this?

def foo():
print 'foo'


You already know the name of the function.

There is no portable way of retrieving the name of the current function from 
within that function.




--
Steven

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


[Tutor] How to have the name of a function inside the code of this function?

2012-04-06 Thread Karim


Hello all,


I have :

def foo():
   print( getattr(foo, 'func_name'))

Where I get the name of the function but I add to give the name of this 
function. Indeed it is not very helpful...
I checked the globals() but how I can do to get 
globals()['toto'].func_name. This is not more helpful ;o)


If you have any idea to get the caller name inside the caller.

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


Re: [Tutor] Is socket.socket() a func or a class

2012-04-06 Thread Alan Gauld

On 06/04/12 07:10, Khalid Al-Ghamdi wrote:


I'm reading this book that says when creating a socket you have to use
the socket.socket() _function _as in :

...

 >>> type(ss)


so, which is it? or do authors loosely use these terms interchangeably
in this context?


It just looks like sloppy terminology to me.

>>> import socket as s
>>> s.socket

>>> s.gethostbyname


--
Alan G
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] Running scripts at login

2012-04-06 Thread Alan Gauld

On 06/04/12 03:07, Michael Lewis wrote:


What if I want to send the executable to someone and have their machine
run the script at startup? Assume I don't have access to their machine
to add the script to the startup program group. How can I make that happen?


When you say executable in  reference to a Python script you need to be 
careful about what you mean.


Because Python is an interpreted language the executable can be either 
the python interpreter (python.exe) or the script that the interpreter 
is executing (myscript.py).


If your friend already has Python installed on their computer (and many 
nowadays do) then you just need to send the python script and ask them 
to save it into their startup group (or more likely create a link to it 
in their startup group) If they are non computer literate you could 
write an installer or batch file that copied the file to the relevant place.


If your friend does not have Python installed you can either:
1) send them the Python installer and your script
2) get them to download and install python before running your script.
3) use a program like py2exe to bundle your script and the
   interpreter into a single self executing file

HTH
--
Alan G
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