Re: [Tutor] while loops causing python.exe to crash on windows

2010-06-07 Thread Alan Gauld

"Alex Hall"  wrote


I am not sure how else to explain it. I want to loop until the value
of a variable changes, but while that loop is taking place, the user
should be able to perform actions set up in a wx.AcceleratorTable.


And here we have the critical clue. You are trying to write this
loop in a GUI application. GUIs and long running loops don't
mix. GUIs are driven by events and if you write a long loop
the GUI cannot receive any events until the loop finishes
and so "locks up"

In a GUI environment you simulate a long loop with
1) A Timer event that does someting then sets up another timer
2) The null event (if the framework suipports it) whereby
when no other events are present the framework calls your code.

for a "listener", which will sit in the background and only perform 
an

action when it detects a certain thing. In this case, a listener to
watch for a variable to turn from False to True, then to act when it
sees that change.


The listener sits inside a Timer that fires every, say, 1/10 sec.
That should leave plenty time to respond to the variable change
and give wx time to process any mouse clicks, key presses etc.

BTW. If you are doing any significant GUI work in wxPython
it will be helpful to buy the wxPython in Action book. It is very
clear and includes examples of using Timers etc.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] while loops causing python.exe to crash on windows

2010-06-07 Thread Walter Prins
On 7 June 2010 02:37, Alex Hall  wrote:

> I am not sure how else to explain it. I want to loop until the value
> of a variable changes, but while that loop is taking place, the user
> should be able to perform actions set up in a wx.AcceleratorTable.
> Looping, though, causes Windows to tell me that python.exe is not
> responding, so I have to close the entire thing. I guess I am looking
> for a "listener", which will sit in the background and only perform an
> action when it detects a certain thing. In this case, a listener to
> watch for a variable to turn from False to True, then to act when it
> sees that change.
>

Notwithstanding what everyone else have said I'll add the following for what
it's worth:

You need to do some research into how GUI systems typically work,
particularly the message queue and each applications "magic" and hidden
message processing loop etc.  Basically in a GUI app there's a hidden loop
that continually checks for messages (calls) to your application and
dispatches those calls to the handlers defined in your application.
However, if your handlers do not complete quickly, then while they run,
control obviously does not return to this loop again, and consequently no
further events can/will be processed by your app.  Some OS's flag up
applications which do not process their event queues for a while as "not
responding" or similar.

However, in most windowing systems there's a way to process pending messages
that have built up in the queue without actually returning from the handler
that you're in.  In Delphi the call is "Application.ProcessMessages", in VB
it's "DoEvents".  In WxPython it's Yield().  (You can google each of those
to see the same issue in different languages/platforms if you like, it might
be instructive.)

So bottom line, in theory you can call Yield() in your loop and you should
be OK -- your app should suddenly be responsive again even while in the
loop.   This type of solution is however the more "cheap and dirty"
alternative and can introduce it's own set of problems and downsides.  (What
if for example another call/message is processed to the same event handler
from which you called Yield() alrady?  The point is you may open yourself up
to re-entrancy issues if you're not careful.  Also if the loop is very tight
you might end-up consuming a lot of CPU doing nothing, so a sleep() type
call might also be advisable in the loop to prevent spending more time
calling Yield() than doing anything else. )

Anyway, I've done a quick google and the following page seems to be a good
discussion of the above thoughts:
http://wiki.wxpython.org/LongRunningTasks

HTH,

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


Re: [Tutor] Misc question about scoping

2010-06-07 Thread Tino Dai
>answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
>>   x.values(),Answer.objects.filter(fk_questionSet=1). \
>>   filter(fk_question=1).values('widgetAnswer').order_by(). \
>>   annotate(widgetCount=Count('widgetAnswer')
>>
>>
> The first time there's a suspected problem with this code, you'll probably
> end up with a similar refactored set of 10-15 lines.  I'm sure because I've
> got code like that scattered throughout my codebase and that's what I end up
> doing.  The difference is that I rattle off the one-liners as part of the
> original coding effort, and only break it out when there's a need to -- I'm
> not striving to compact things into one-liners.
>

Emile,

  Actually, I have already broken it out into 4 or 5 separate lines. The
one-liner is cool for "Hey, look what I can do", but isn't for code
maintenance. What I was striving for a happy medium between compactness and
readability.


>
> BTW, doesn't
>
>
>   dict(map(lambda x: (str(x[1]),x[0]),map(lambda x:x.values()
>
> simply map the values of a dict back into a dict?
>
>

I couldn't find a way to reverse the order of the elements to ultimately for
a dictionary, so I resorted to this hack instead.

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


[Tutor] a class query

2010-06-07 Thread Payal
Hi all,
I know the difference  between
class Parent :
class Parent(object) :

But in some softwares i recall seeing,
class Parent() :

Is this legal syntax?

With warm regards,
-Payal
-- 



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


Re: [Tutor] a class query

2010-06-07 Thread Robert
lol,  for a second I thought this question comes from PayPal



On Mon, Jun 7, 2010 at 10:01 AM, Payal  wrote:
> Hi all,
> I know the difference  between
> class Parent :
> class Parent(object) :
>
> But in some softwares i recall seeing,
> class Parent() :
>
> Is this legal syntax?
>
> With warm regards,
> -Payal
> --
>
>
>
> ___
> Tutor maillist  -  tu...@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] while loops causing python.exe to crash on windows

2010-06-07 Thread Mark Lawrence

On 07/06/2010 01:44, Alex Hall wrote:

Further to the other comments that you've had, could you please refer to 
the following, thanks.


http://www.catb.org/~esr/faqs/smart-questions.html

Kindest regards.

Mark Lawrence

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


Re: [Tutor] a class query

2010-06-07 Thread bob gailer

On 6/7/2010 10:01 AM, Payal wrote:

Hi all,
I know the difference  between
class Parent :
class Parent(object) :

But in some softwares i recall seeing,
class Parent() :

Is this legal syntax?
   


Teach: To answer that question, just try it at the interactive prompt. 
If it is not legal syntax you will get a syntax error!


Feed: Yes

Originally it was not legal. Then in some version it became legal.

--
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] a class query

2010-06-07 Thread python
Not the OP, but I was surprised to see class Name() work (in Python
2.6.5 at least).

Is this equivalent to class Name( object ) or does this create an old
style class?

Going forward into the 2.7/3.x world, is there a preferred style?

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


Re: [Tutor] a class query

2010-06-07 Thread Mark Lawrence

On 07/06/2010 16:12, pyt...@bdurham.com wrote:

Not the OP, but I was surprised to see class Name() work (in Python
2.6.5 at least).

Is this equivalent to class Name( object ) or does this create an old
style class?

Going forward into the 2.7/3.x world, is there a preferred style?

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



RTFM? :)

Kindest regards.

Mark Lawrence.

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


Re: [Tutor] a class query

2010-06-07 Thread python
Hi Mark,

>> I was surprised to see class Name() work (in Python 2.6.5 at least). Is this 
>> equivalent to class Name( object ) or does this create an old style class? 
>> Going forward into the 2.7/3.x world, is there a preferred style?

> RTFM? :)

I am reading TFM :)

Here's why I'm confused. The following paragraph from TFM seems to
indicate that old style classes are the default:

Quote: For compatibility reasons, classes are still old-style by
default. New-style classes are created by specifying another new-style
class (i.e. a type) as a parent class, or the “top-level type” object if
no other parent is needed. The behaviour of new-style classes differs
from that of old-style classes in a number of important details in
addition to what type() returns. Some of these changes are fundamental
to the new object model, like the way special methods are invoked.
Others are “fixes” that could not be implemented before for
compatibility concerns, like the method resolution order in case of
multiple inheritance.
http://docs.python.org/reference/datamodel.html#newstyle

Yet TFM for 2.6.5 shows all class examples without specifying a parent
class.
http://docs.python.org/tutorial/classes.html

I've been taught that the proper way to create new style classes has
been to always specify an explicit "object" parent class when creating a
new class not based on other classes. 

Somewhere along the line I seemed to have missed the fact that it is no
longer necessary to define classes with 'object' as a parent in order to
get a new style class.

In other words, it seems that the following are now equivalent:

class Name:

-AND-

class Name( object ):

My impression was the "class Name:" style created an old style class.

Thanks for your help!

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


Re: [Tutor] a class query

2010-06-07 Thread Steven D'Aprano
On Tue, 8 Jun 2010 01:12:28 am pyt...@bdurham.com wrote:
> Not the OP, but I was surprised to see class Name() work (in Python
> 2.6.5 at least).
>
> Is this equivalent to class Name( object ) or does this create an old
> style class?

In Python 2.x, all classes are old-style unless you directly or 
indirectly inherit from object. If you inherit from nothing, it is an 
old-style class regardless of whether you say 

class Name: pass

or 

class Name(): pass

In Python 3.x, there are no old-style classes.

> Going forward into the 2.7/3.x world, is there a preferred style?

No.



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


Re: [Tutor] a class query

2010-06-07 Thread Steven D'Aprano
On Tue, 8 Jun 2010 02:03:18 am pyt...@bdurham.com wrote:

> Here's why I'm confused. The following paragraph from TFM seems to
> indicate that old style classes are the default:

Yes, if you don't inherit from object, or another class that inherits 
from object (like the built-ins), you get an old-style class.


> Yet TFM for 2.6.5 shows all class examples without specifying a
> parent class.
> http://docs.python.org/tutorial/classes.html

As the docs also say, they were mostly written before the existence of 
new-style classes, and they haven't been updated to show the new 
syntax. Since Python 3.0 will make such a change obsolete, they will 
probably never be updated.


> I've been taught that the proper way to create new style classes has
> been to always specify an explicit "object" parent class when
> creating a new class not based on other classes.
>
> Somewhere along the line I seemed to have missed the fact that it is
> no longer necessary to define classes with 'object' as a parent in
> order to get a new style class.

That only holds for Python 3.x, since old-style classes don't exist any 
longer.


> In other words, it seems that the following are now equivalent:
>
> class Name:
>
> -AND-
>
> class Name( object ):

Only in Python 3.x.


> My impression was the "class Name:" style created an old style class.

Only in Python 2.x (and 1.x, but who still uses that?).



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


Re: [Tutor] a class query

2010-06-07 Thread Mark Lawrence

On 07/06/2010 17:03, pyt...@bdurham.com wrote:

Hi Mark,


I was surprised to see class Name() work (in Python 2.6.5 at least). Is this 
equivalent to class Name( object ) or does this create an old style class? 
Going forward into the 2.7/3.x world, is there a preferred style?



RTFM? :)


I am reading TFM :)

Here's why I'm confused. The following paragraph from TFM seems to
indicate that old style classes are the default:

Quote: For compatibility reasons, classes are still old-style by
default. New-style classes are created by specifying another new-style
class (i.e. a type) as a parent class, or the “top-level type” object if
no other parent is needed. The behaviour of new-style classes differs
from that of old-style classes in a number of important details in
addition to what type() returns. Some of these changes are fundamental
to the new object model, like the way special methods are invoked.
Others are “fixes” that could not be implemented before for
compatibility concerns, like the method resolution order in case of
multiple inheritance.
http://docs.python.org/reference/datamodel.html#newstyle

Yet TFM for 2.6.5 shows all class examples without specifying a parent
class.
http://docs.python.org/tutorial/classes.html

I've been taught that the proper way to create new style classes has
been to always specify an explicit "object" parent class when creating a
new class not based on other classes.

Somewhere along the line I seemed to have missed the fact that it is no
longer necessary to define classes with 'object' as a parent in order to
get a new style class.

In other words, it seems that the following are now equivalent:

class Name:

-AND-

class Name( object ):

My impression was the "class Name:" style created an old style class.

Thanks for your help!

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


Hi Malcolm,

I see that Stephen D'Aprano has already replied twice so I won't bother. 
 Apart from that no offence meant, I hope none taken.


Kindest regards.

Mark Lawrence.


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


Re: [Tutor] a class query

2010-06-07 Thread python
> In Python 2.x, all classes are old-style unless you directly or indirectly 
> inherit from object. If you inherit from nothing, it is an old-style class 
> regardless of whether you say class Name: pass or class Name(): pass. In 
> Python 3.x, there are no old-style classes.

Thanks Steven!

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


Re: [Tutor] a class query

2010-06-07 Thread python
Steven,

Thanks again for your explanations. I thought I had missed a major
change in Python class behavior - relieved to find that I'm up-to-date.

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


Re: [Tutor] a class query

2010-06-07 Thread python
Hi Mark,

> I see that Stephen D'Aprano has already replied twice so I won't bother. 
> Apart from that no offence meant, I hope none taken.

Your RTFM reply actually gave me a good laugh. No (zero) offence taken.
And I appreciate your many helpful posts in these forums.

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


[Tutor] Strange range and round behaviour

2010-06-07 Thread Adam Bark
Just out of curiosity does anyone know why you get a deprecation warning if
you pass a float to range but if you use round, which returns a float, there
is no warning?

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(2.0)
__main__:1: DeprecationWarning: integer argument expected, got float
[0, 1]
>>> range(round(2.0))
[0, 1]
>>> round(2.0)
2.0
>>> round(2.0, 0)
2.0
>>> round.__doc__
'round(number[, ndigits]) -> floating point number\n\nRound a number to a
given
precision in decimal digits (default 0 digits).\nThis always returns a
floating
point number.  Precision may be negative.'
>>>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loops / listeners

2010-06-07 Thread Francesco Loffredo

Hi all,
I think that simply erasing those "continue" statements will let Python 
respond again. Those statements are both useless and damaging, because 
the following "time.sleep(.1)" statements will NEVER be executed. And 
this, in turn, is IMHO the reason why Python stops responding: it lacks 
the time to do so.

Hope that helps
Francesco

Il 07/06/2010 5.09, Alex Hall wrote:

Hi all,
First off, I apologize to the list for my previous thread; somehow,
despite my having written the post, it ended up blank ()

I have a main loop which will continue for as long as neither player1
nor player2 has won. Inside that loop I have a call to a function
which should basically wait until the user completes a turn, then
return, exiting this loop and returning to the main loop, the one
looping until there is a winner. Once back there, the second player's
idling function is called; once that user completes a turn, the main
loop switches back to the first user, and so on. Here is the main
loop:
  player1.takeTurn() #someone has to start off the game

  #now loop until someone wins...
  while not player1.isWinner and not player2.isWinner:
   if player1.grid.turnOver: #player1 is done for this turn
player1.grid.speak("player 2 is going.")
#lock p1 out of the window, unlock p2, and then loop p2 until s/he
does something
player1.lock(); player2.unlock(); player2.takeTurn()
   else: #player2 has completed a turn
player2.grid.speak("player 1 is going.")
#opposite of above - lock p2, unlock p1, loop until p1 is done
player2.lock(); player1.unlock(); player1.takeTurn()
   #end if
   continue #is this doing anything?
   time.sleep(.1) #again, is this important?
  #end while

The lock and unlock functions are simply there to disable and enable
keyboard/mouse commands, respectively, so lock causes the player's
screen to stop responding to input while unlock unfreezes the screen.
This way, if you and I are playing a game, I can't keep shooting even
if my turn is over.
TakeTurn() is that smaller loop I was talking about:
  def takeTurn(self):
   while not self.grid.turnOver: #turnOver is true once a turn-ending
function is called in grid
continue
time.sleep(.1)
   #end while
  #end def

That is inside the Player class. Grid is just another object that is
really the most important part of all the game logic; Grid holds the
wx frame on which everything is drawn, the boolean to tell if the turn
is over, the ships, and more. That is why the function checks
self.grid.turnOver, instead of just self.turnOver; turnOver tells if
the player has performed a move that ends a turn or not. Firing ends a
turn, while just moving around does not.

The question, then, is this: when I run the code, Windows says
"python.exe has stopped responding". I know that this is because it is
getting stuck, probably in the takeTurn() loop. Ordinarily, a
situation like this would probably call for:
while ok:
  ok=#program logic

The hard part with my program, though, is that all input is set up
with a wx.AcceleratorTable object, so I cannot just dump everything
into a huge while loop and have it process that way. What I am looking
for is some kind of listener object, so that I can just monitor
self.grid.turnOver and call a function, or perform a few lines of
code, when the listener detects a change in turnOver. On the other
hand, there may be something simple that I am missing in my while loop
that would cause the problem of python.exe crashing to not happen
(well, not crashing, but rather not responding). I hope it is the
latter problem, but I am not sure what else I could add to the loop(s)
to stop this problem. Thanks, and sorry again for the blank message;
still not sure how that happened. Hopefully this one works!





Nessun virus nel messaggio in arrivo.
Controllato da AVG - www.avg.com
Versione: 9.0.829 / Database dei virus: 271.1.1/2921 -  Data di rilascio: 
06/06/10 08:25:00


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


Re: [Tutor] a class query

2010-06-07 Thread Mark Lawrence

On 07/06/2010 17:30, pyt...@bdurham.com wrote:

Hi Mark,


I see that Stephen D'Aprano has already replied twice so I won't bother. Apart 
from that no offence meant, I hope none taken.


Your RTFM reply actually gave me a good laugh. No (zero) offence taken.
And I appreciate your many helpful posts in these forums.

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



Hi Malcolm,

Thanks for your kind response. Mind you it will cost you, if you ever 
get into my neck of the woods it will be at least 2 pints of Ringwood 
Old Thumper.


Cheers.

Mark.

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


Re: [Tutor] Strange range and round behaviour

2010-06-07 Thread Lee Harr

> Just out of curiosity does anyone know why you get a deprecation warning if
> you pass a float to range but if you use round, which returns a float, there
> is no warning?


It has nothing to do with the round.
It's just that the warning is only shown once:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 

[GCC 4.4.3] on linux2

>>> range(2.0)
__main__:1: DeprecationWarning: integer argument expected, got float
[0, 1]
>>> range(2.0)
[0, 1]
>>> 
$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(round(2.0))
__main__:1: DeprecationWarning: integer argument expected, got float
[0, 1]
>>> range(round(2.0))
[0, 1]

  
_
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] no. of references

2010-06-07 Thread Payal
Hi,
If I have a list (or a dict), is there any way of knowing how many other
variables are referencing the same object?

With warm regards,
-Payal
-- 



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


Re: [Tutor] a class query

2010-06-07 Thread Payal
On Tue, Jun 08, 2010 at 02:11:10AM +1000, Steven D'Aprano wrote:
> In Python 2.x, all classes are old-style unless you directly or 
> indirectly inherit from object. If you inherit from nothing, it is an 
> old-style class regardless of whether you say 
> 
> class Name: pass
> 
> or 
> 
> class Name(): pass
> 
> In Python 3.x, there are no old-style classes.

Thanks, that clears my doubt.


With warm regards,
-Payal
-- 

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


[Tutor] accented characters to unaccented

2010-06-07 Thread KB SU
Hi,

I have open url and read like following:

$import urllib
$txt = urllib.urlopen("http://www.terme-catez.si";).read()
$txt

Gives output like below:
other parts are skipped ---
r\n  2010\r\n  http://www.terme-catez.si";
target="_blank">Terme
 \xc4\x8cate\xc5\xbe\r\n  Slovenija\r\n  \r\n  Spletne
re\
xc5\xa1itve\r\n  © 1996-\r\n  2010\r\n  http://www.tme
dia.biz" target="_blank">(T)media\r\n  \r\n\r\n\r\n\r\n  \r\n\r\n  \r\n\r\n
\r\n\r\n\r\nhttp://www.google-analytics.com/urchin.js";
type="text/j
avascript">\r\n\r\n_uacct =
"UA-1815955-
1";\r\nurchinTracker();\r\n\r\n\r\n\r\n\r\n'

If you see above, in junk of HTLM, there is text like 'Terme
\xc4\x8cate\xc5\xbe'  (original is 'Terme Čatež'). Now, I want to convert
code like '\xc4\x8c' or '\xc5\xbe' to unaccented chars so that 'Terme
\xc4\x8cate\xc5\xbe' become 'Terme Catez'. Is there any way convert from
whole HTML.

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