[Tutor] registering itunes with events

2006-11-17 Thread Orri Ganel
Hello all,

As I mentioned in my recent emails, I've been working on a program that
will display the currently playing iTunes track (for example, when
iTunes is minimized to the system tray so that you don't have to enlarge
it again just to see the name of the song).  Unfortunately, of the
hundreds of configurations I've found online, not one seems able to
properly register iTunes with events, which would allow me to do away
with the infinite while loop and time.sleep(1) and instead just call the
update method whenever OnPlayerPlayEvent or OnPlayerStopEvent got
called.  I was playing around with it and managed for a bit to get it
working, but all subsequent attempts to replicate the phenomenon have
been fruitless.  My best guess is something along the lines of:

iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
iTunesEvents = win32com.client.WithEvents(iTunes, iTunesEventHandler)

where iTunesEventHandler is a class with methods OnPlayerPlayEvent,
etc.  From what I can glean, it seems like I'm missing some essential
step that will keep the events connection open b/c the above tends to
just close after calling iTunesEventHandler.__init__() Also, pythoncom
seems to play a role in some of the online scripts I've found, but none
of them seem to do anything.  The trend seems to be:

pythoncom.CoInitialize()
iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
iTunesEvents = win32com.client.WithEvents(iTunes, iTunesEventHandler)

Sometimes followed with

pythoncom.PumpWaitingMessages()

Right now, I'd just like to be able to keep a connection open with
iTunes that will catch OnPlayerPlayEvents and call the correct method.
So if anyone has any idea how to do that, I'd be grateful.

Thanks,
Orri

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


Re: [Tutor] Tutor Digest, Vol 33, Issue 67

2006-11-17 Thread Alan Gauld
"Danny Yoo" <[EMAIL PROTECTED]> wrote

>> Note that this call will create a new name list inside the 
>> recursive
>> call. You probably need to do:
>>
>>   name += ListRegistryKeys(item)
>
>
> Mutating the list while iterating over it is possibly bad.

I'd go so far as to say almost certainly bad!
Oops, well caught Danny, apologies to the OP..

Alan G. 


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


Re: [Tutor] problem with defining a global class instance

2006-11-17 Thread Alan Gauld
"sharath B N" <[EMAIL PROTECTED]> wrote

> i am sort of newbie to python. I am trying to do a super Market
> simulation with OOP in python. I have problems with using a class
> instance as global...
> def generate (... ,,...)
>
> " in this function i define the global variables "
> global  stock,stockManager, manager etc.

Please don't do this!
Global variables are dangerous enough as they are but by
hiding their definitions inside a function you make them
even more difficult to use. The global keyword is really
intended to tell your function to use an existing global
variable not to create new ones (although it does do that
if the variable doesn't exist, its a side-effect that should
be avoided IMHO)

Just declare your global variables in the outer scope of your module:

stock = 0
stockManager = None
manager = 'whatever'

> class Manager

By defining the class here the name is at a global level and
you can create instances as and where you need them.

> def  create_stockManager(..)
> """ this is a method in class manager"""
> stockManager = StockManager( name)
> stockManager.create_Stock(..)
> now this gives an attribute error sayin  stockManager has no
> attribute create_Stock

You need to define the method in your StockManager class
definition. ie


class StockManager:
   .
   
   def create_Stock(self)
   ...
   ...

> if i create the StockManager instance in the generate func
> itself...then this problem doesnt comebut i need it this way for
> the program to make sense..

I'm not sure what you mean by the last bit. Are you saying
that if you try to call create_Stock inside the generate function
you don't get an error but if you call it outside you do?
The only reason I can think of for that is that you are actually
defining the class inside the function:

def generate(.):
class StockManager:
def create_Stock(...


If so that will limit the class definition to inside generate
which you don't want. Simply saying "global stockManager"
in generate doesn't make your class global it just creates
a new name in the global namespace. Another reason not
to try hiding your global data declarations inside the
generate function.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] registering itunes with events

2006-11-17 Thread Alan Gauld

"Orri Ganel" <[EMAIL PROTECTED]> wrote

> been fruitless.  My best guess is something along the lines of:
>
> iTunes = 
> win32com.client.gencache.EnsureDispatch("iTunes.Application")
> iTunesEvents = win32com.client.WithEvents(iTunes, 
> iTunesEventHandler)
>
> where iTunesEventHandler is a class with methods OnPlayerPlayEvent,

Are you sure its a class? I haven't looked into this but from
past experience with Win32 I'd expect the event handler to
be a function that takes the event and then calls the appropriate
handler function. But I haven't checked the Python implementation
so I could be completely off track there.

Alan G. 


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


[Tutor] problem with defining a global class instance

2006-11-17 Thread sharath B N
hi,
i am sort of newbie to python. I am trying to do a super Market
simulation with OOP in python. I have problems with using a class
instance as global...
def generate (... ,,...)

" in this function i define the global variables "
global  stock,stockManager, manager etc.


class Manager
...
...
...
def  create_stockManager(..)
""" this is a method in class manager"""
stockManager = StockManager( name)
stockManager.create_Stock(..)


now this gives an attribute error sayin  stockManager has no
attribute create_Stock

if i create the StockManager instance in the generate func
itself...then this problem doesnt comebut i need it this way for
the program to make sense..
can somebody help me
thnks
Sharath
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] registering itunes with events

2006-11-17 Thread Orri Ganel
Alan Gauld wrote:

>"Orri Ganel" <[EMAIL PROTECTED]> wrote
>
>  
>
>>been fruitless.  My best guess is something along the lines of:
>>
>>iTunes = 
>>win32com.client.gencache.EnsureDispatch("iTunes.Application")
>>iTunesEvents = win32com.client.WithEvents(iTunes, 
>>iTunesEventHandler)
>>
>>where iTunesEventHandler is a class with methods OnPlayerPlayEvent,
>>
>>
>
>Are you sure its a class? I haven't looked into this but from
>past experience with Win32 I'd expect the event handler to
>be a function that takes the event and then calls the appropriate
>handler function. But I haven't checked the Python implementation
>so I could be completely off track there.
>
>Alan G. 
>
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>
It's a class that I have to define myself, so you're right that it 
doesn't come with win32com et al.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Read the builtin module source, also itchy ellipses.

2006-11-17 Thread Thomas

Hi,

I sometimes find it useful to read the source code of a module and for
example I can type string.__file__ to find the location of the string
module.

However the .__file__ method is not available for the module builtin. Is it
possible to read the source code for built in functions and if so how do I
find them? I realise some/all may be written in C rather than python but it
would still be interesting to read them.

In an unrelated follow-up may I also ask: can the ellipsis (...) be used in
code? I tried a googlecode search but none of the examples worked for me.
Can someone post an example line of code that uses ... in it. This has
been causing an itch in my brain, please reply with the scratch.

Thanks in advance,
Tom
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read the builtin module source, also itchy ellipses.

2006-11-17 Thread Kent Johnson
Thomas wrote:
> Hi,
>  
> I sometimes find it useful to read the source code of a module and for 
> example I can type string.__file__ to find the location of the string 
> module.
>  
> However the .__file__ method is not available for the module builtin. Is 
> it possible to read the source code for built in functions and if so how 
> do I find them? I realise some/all may be written in C rather than 
> python but it would still be interesting to read them.

It's all in C, see Python\bltinmodule.c in the source distribution.
>  
> In an unrelated follow-up may I also ask: can the ellipsis (...) be used 
> in code? I tried a googlecode search but none of the examples worked for 
> me. Can someone post an example line of code that uses ... in it. 
> This has been causing an itch in my brain, please reply with the scratch.

Syntactically ellipsis is allowed in a slice list as shown in the syntax 
definition here:
http://docs.python.org/ref/slicings.html

As a practical matter I think the only containers that actually support 
this kind of slicing are the arrays in Numeric and numpy. Here is a 
brief example:
http://numpy.scipy.org//numpydoc/numpy-6.html#pgfId-36074

You could also support this syntax in a class of your own. Here is a 
simple example that just captures the argument to __getitem__() to show 
how the mechanism works:

In [4]: class Foo(object):
...: def __getitem__(self, s):
...: self.s = s
...: return None
...:

In [5]: f=Foo()

In [6]: f[...,1]

In [7]: f.s
Out[7]: (Ellipsis, 1)

Kent

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


Re: [Tutor] OT: Vim was: free IDE for Python?

2006-11-17 Thread Mike Hansen
 



From: [EMAIL PROTECTED] on behalf of [EMAIL PROTECTED]
Sent: Thu 11/16/2006 10:32 PM
To: [EMAIL PROTECTED]
Cc: tutor@python.org
Subject: Re: [Tutor] OT: Vim was: free IDE for Python?



Hi Alan,
Greetings.

Alan Gauld wrote:
>> I have to chuckle when you recommend Vim for ease of use.
>
> Me too, and I've been a vi/elvis/viper/vim user for over 20 years(*).
> vi/vim could never be described as easy to learn, but...

I too use vim for a variety of editing tasks. From xml, python to normal
text editing, not  a power user yet.
Do you have any tips/tricks to share for python on vim. Your vimrc file
or any plugins you use.

How would you run the python script from vim?
- !python %
OR any other way? I dislike the command line window popup to execute the
scripts. (and press Enter key twice).
I kindda wish, that  for !python % should :split the window and
display the results for non-interactive run.
If interactive session, the cmd.exe could stay open..

What the settings of ppl using vim for python?

--
Senthil
___


Here's what I'm doing. Not sure if it's that helpful to you.

I use the mini-buffer explorer plug-in and the taglist plugin.

set smartindent

" shuts off the annoying "#" comment in smartindent to jump to col 1
inoremap # X#

autocmd BufRead *.py set smartindent 
cinwords=if,elif,else,for,while,try,except,finally,def,class

I've mapped a key to run pyflakes and display the results in a different buffer

:new | r!pyflakes #

Mike

-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified individual or 
entity.

  Its contents may be privileged, confidential, and exempt from disclosure 
under the law.
  Any dissemination, distribution, or copying of this communication is strictly 
prohibited.

  Please notify the sender immediately if you are not the intended recipient.

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


[Tutor] python CRM ERP

2006-11-17 Thread Picio
Hello,
Can you point me to some CRM and ERP software written in Python, that
in your "guru" opinion is a good product? Maybe also Open Source?
Thanks
Picio
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Vim was: free IDE for Python?

2006-11-17 Thread William O'Higgins Witteman
On Fri, Nov 17, 2006 at 11:02:18AM +0530, [EMAIL PROTECTED] wrote:

>What the settings of ppl using vim for python?

A few Python-specific things that I have in my .vimrc are as follows:

set autoindent
set ts=2
set shiftwidth=2
set expandtab
set shiftround
set smarttab
filetype plugin on
syntax on
:inoremap ( ()i
:inoremap [ []i
:inoremap " ""i
:inoremap { {}i
:inoremap < <>i
autocmd BufNewFile *.py 0r ~/.vim/skel/skel.py

The python skeleton file looks like this:

#!/usr/bin/python

import 

Finally, my Python filetype file looks like this (just my changes):

:inoremap ' ''i
abbr dd #debug
set spell!
" Code Macros

" Define a class in Python, with docstring
inoremap class class :""

" Define a function in Python, with docstring
inoremap def def ():""

" This is to create blocks when coding in Python
im : :

That's about it.  I'm just a vim novice - there are lots of things I
haven't learned or don't use enough and forget.
-- 

yours,

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


Re: [Tutor] python CRM ERP

2006-11-17 Thread Christopher Arndt
Picio schrieb:
> Hello,
> Can you point me to some CRM and ERP software written in Python, that
> in your "guru" opinion is a good product? Maybe also Open Source?

Have you asked Google?

http://tinyerp.org/

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


[Tutor] a question about passing values between functions

2006-11-17 Thread kristinn didriksson
Hello,
I am still wrestling with the concept of values going
between functions. (just starting out)The program
below works seems to work, but here is my question.
In my understanding, return area in the first routine
makes the value of area an instance of areaCirc and I
use areaCirc in the other program to call the value
for the area that was calculated in the first routine.
Is that the right way to think of this program? Did I
use return area correctly?
This is a new vocabulary, so I hope I am making sense.
Basically, I am wondering if I needed to use the
return area statement.
Thanks,
Kristinn

---
# this program is a redo of ex2 ch3 with a
twist!
  2 # use two functions--one to compute the area
of a pizza, and one to
  3 # to compute cost per square inch.
  4 # Given are the diameter and the price. A =
pi(r**2)
  5
  6 # define the function that computes the area
  7 import math
  8 def areaCirc():
  9 diameter = input("Please input the
diameter of the pizza: ")
 10 area = 4*(math.pi)*(diameter/2)**2
 11 print "The area of the pizza is %0.2f" %
(area)
 12 return area
 13
 14
 15 def unitCost():
 16 price = input("Please input the cost of
the pizza per square inch: ")
 17 area = areaCirc()
 18 cost = area * price
 19 print "The cost of the pizza is %0.2f"
%(cost)
 20
 21 unitCost()
---


 

Sponsored Link

Compare mortgage rates for today. 
Get up to 5 free quotes. 
Www2.nextag.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem making '.exe' from python.

2006-11-17 Thread Chris Hengge

Whether using py2exe or pyInstaller I've noticed a problem and I'm not sure
how to fix it...

When I create a .exe and then make a shortcut to the file and run it.. the
program will crash without warning.
If I run the .exe directly it runs fine.

My assumption of the problem:
Since it is still built on python and my variables for files or module calls
are designed for "they are in the same location I run the script from" when
I run the script from a shortcut it will fail finding the wanted pieces.

Is this true? And if so, how do I fix it? I could write fixed paths into my
code, but that is unrealistic and makes running my scripts to test them a
pain.

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


Re: [Tutor] Problem making '.exe' from python.

2006-11-17 Thread Jason Massey

Check to make sure that under the shortcut properties that you have the
"Start in" field filled out with the directory the script is located.

On 11/17/06, Chris Hengge <[EMAIL PROTECTED]> wrote:


Whether using py2exe or pyInstaller I've noticed a problem and I'm not
sure how to fix it...

When I create a .exe and then make a shortcut to the file and run it.. the
program will crash without warning.
If I run the .exe directly it runs fine.

My assumption of the problem:
Since it is still built on python and my variables for files or module
calls are designed for "they are in the same location I run the script from"
when I run the script from a shortcut it will fail finding the wanted
pieces.

Is this true? And if so, how do I fix it? I could write fixed paths into
my code, but that is unrealistic and makes running my scripts to test them a
pain.

Thanks.

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



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


[Tutor] str() conversion of unicode?

2006-11-17 Thread Tod Haren
I'm trying to calculate md5's on records from a Paradox database.  The
problem is that apparently some of the records have non ASCII
characters.  I'm at a loss as to how I should handle this.

This snippet is called for each row in my recordset:

m = md5.new()
for f in rs.fields.keys():
val = str(rs.fields[f].value)
m.update(val)
m = m.hexdigest()

the str() convertion throws this error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in
position 1: ordinal not in range(128)

print u'\xbd' renders the '1/2' symbol.  I would eliminate the str()
but md5 barks when you pass anything but a string apparently.  Is
there another way to coerce the field values to a string?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Vim was: free IDE for Python?

2006-11-17 Thread Senthil_OR
William O'Higgins Witteman wrote:
> On Fri, Nov 17, 2006 at 11:02:18AM +0530, [EMAIL PROTECTED] wrote:
> 
>> What the settings of ppl using vim for python?
> 
> A few Python-specific things that I have in my .vimrc are as follows:

Thanks for sharing William. 
I think ppl in this list , might also be interested in this _vimrc ( I
am on windows) snippet, which on pressing Alt+D over a module name will
take us to its documentation.

function! OnlineDoc() 
if &ft =~ "python" 
let s:urlTemplate = "http://docs.python.org/lib/module-%.html";
else 
return 
endif 
let s:browser = "\"C:\\Program Files\\Internet
Explorer\\IEXPLORE.EXE\"" 
let s:wordUnderCursor = expand("") 
let s:url = substitute(s:urlTemplate, "%", s:wordUnderCursor, "g") 

let s:cmd = "silent !start " . s:browser . " " . s:url 
execute  s:cmd 
endfunction 

" online doc search 
map   :call OnlineDoc() 

- It is written with modifications from vim tips page.

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


Re: [Tutor] Problem making '.exe' from python.

2006-11-17 Thread Chris Hengge

Awesome! Thank you!

This has been driving me crazy for weeks.
Now to figure out how to tell Inno to add that to a link, and I'm on my way!

On 11/17/06, Jason Massey <[EMAIL PROTECTED]> wrote:


Check to make sure that under the shortcut properties that you have the
"Start in" field filled out with the directory the script is located.

On 11/17/06, Chris Hengge <[EMAIL PROTECTED]> wrote:

> Whether using py2exe or pyInstaller I've noticed a problem and I'm not
> sure how to fix it...
>
> When I create a .exe and then make a shortcut to the file and run it..
> the program will crash without warning.
> If I run the .exe directly it runs fine.
>
> My assumption of the problem:
> Since it is still built on python and my variables for files or module
> calls are designed for "they are in the same location I run the script from"
> when I run the script from a shortcut it will fail finding the wanted
> pieces.
>
> Is this true? And if so, how do I fix it? I could write fixed paths into
> my code, but that is unrealistic and makes running my scripts to test them a
> pain.
>
> Thanks.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

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


Re: [Tutor] a question about passing values between functions

2006-11-17 Thread Bob Gailer
kristinn didriksson wrote:
> Hello,
> I am still wrestling with the concept of values going
> between functions. (just starting out)The program
> below works seems to work, but here is my question.
> In my understanding, return area in the first routine
> makes the value of area an instance of areaCirc 
area = 4*( ... is what makes area a local variable (not instance). 
Assignment to a name in a function where the name has not been declared 
global makes that name local to the function.
> use areaCirc in the other program to call the value
> for the area that was calculated in the first routine.
> Is that the right way to think of this program? Did I
> use return area correctly?
>   
Yes.
> This is a new vocabulary, so I hope I am making sense.
> Basically, I am wondering if I needed to use the
> return area statement.
> Thanks,
> Kristinn
>
> ---
> # this program is a redo of ex2 ch3 with a
> twist!
>   2 # use two functions--one to compute the area
> of a pizza, and one to
>   3 # to compute cost per square inch.
>   4 # Given are the diameter and the price. A =
> pi(r**2)
>   5
>   6 # define the function that computes the area
>   7 import math
>   8 def areaCirc():
>   9 diameter = input("Please input the
> diameter of the pizza: ")
>  10 area = 4*(math.pi)*(diameter/2)**2
>  11 print "The area of the pizza is %0.2f" %
> (area)
>  12 return area
>  13
>  14
>  15 def unitCost():
>  16 price = input("Please input the cost of
> the pizza per square inch: ")
>  17 area = areaCirc()
>  18 cost = area * price
>  19 print "The cost of the pizza is %0.2f"
> %(cost)
>  20
>  21 unitCost()
> ---
If I were working this assignment I'd structure it thus:
get the diameter
get the cost per square inch
call a function to compute the area
call a function to compute the cost

-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] Problem making '.exe' from python.

2006-11-17 Thread Chris Hengge

Just wanted to wrap that up so nobody wastes time replying.

I've fixed my Inno scripts and all my packages work now, case closed!

Thanks again Jason.

On 11/17/06, Chris Hengge <[EMAIL PROTECTED]> wrote:


Awesome! Thank you!

This has been driving me crazy for weeks.
Now to figure out how to tell Inno to add that to a link, and I'm on my
way!

On 11/17/06, Jason Massey <[EMAIL PROTECTED]> wrote:
>
> Check to make sure that under the shortcut properties that you have the
> "Start in" field filled out with the directory the script is located.
>
> On 11/17/06, Chris Hengge <[EMAIL PROTECTED]> wrote:
>
> >  Whether using py2exe or pyInstaller I've noticed a problem and I'm
> > not sure how to fix it...
> >
> > When I create a .exe and then make a shortcut to the file and run it..
> > the program will crash without warning.
> > If I run the .exe directly it runs fine.
> >
> > My assumption of the problem:
> > Since it is still built on python and my variables for files or module
> > calls are designed for "they are in the same location I run the script from"
> > when I run the script from a shortcut it will fail finding the wanted
> > pieces.
> >
> > Is this true? And if so, how do I fix it? I could write fixed paths
> > into my code, but that is unrealistic and makes running my scripts to test
> > them a pain.
> >
> > Thanks.
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>

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


[Tutor] any win32com experts out there?

2006-11-17 Thread Orri Ganel
Hello again.  As I've mentioned in the last couple of emails, I'm having 
trouble registering iTunes for events, and I don't know if it's a bug 
with iTunes or with win32com, or if there's something I'm doing wrong.  
So, if any win32com gurus out there know why the following doesn't work 
and how to change it so it does work, a tip would be much appreciated.

 >>> import win32com.client
 >>> class iTunesEvents:
def __init__(self):
print "starting iTunesEvents"
def OnPlayerPlayEvent(self, track):
print "playing"
def OnPlayerStopEvent(self, track):
print "stopped"
def OnDatabaseChangedEvent(self, deleted, changed):
print "database changed"
def OnPlayerPlayingTrackChangedEvent(self, track):
print "info on current track changed"
def OnCOMCallsDisabledEvent(self, reason):
print "com calls disabled"
def OnCOMCallsEnabledEvent(self):
print "com calls enabled"
def OnQuittingEvent(self):
print "quitting"
def OnAboutToPromptUserToQuitEvent(self):
print "prompting user to quit"
def OnSoundVolumeChangedEvent(self, newvolume):
print "volume changed"

   
 >>> iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
 >>> iTEvents = win32com.client.WithEvents(iTunes, iTunesEvents)
starting iTunesEvents
 >>> iTunes.Play()
 >>> iTunes.Pause()
 >>> iTunes.Play()
 >>> iTunes.Pause()

iTunes did, in fact, play and pause as expected, but as can be seen, no 
events were caught.

Stumped,
Orri


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


Re: [Tutor] a question about passing values between functions

2006-11-17 Thread Alan Gauld

"kristinn didriksson" <[EMAIL PROTECTED]> wrote

> In my understanding, return area in the first routine
> makes the value of area an instance of areaCirc

No, it simply assigns the value returned by areaCirc to
area within .
areaCirc assigns

4*(math.pi)*(diameter/2)**2

to its local variable area then returns area.
So in effect the assignment line inside XXX
could simply be rewritten:

area = 4*(math.pi)*(diameter/2)**2

> Is that the right way to think of this program? Did I
> use return area correctly?

You used return area correctly but are getting confused
between objects and instances and functions.

functions (as defined with a def keyword) return values,
which may be instances of classes or builtin objects
Classes return instances of themselves.
But the syntax in both cases is thesame which is,
admittedly, confusing.

> Basically, I am wondering if I needed to use the
> return area statement.

Yes, that is how to pass data out of one function to
another part of the program.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

> ---
># this program is a redo of ex2 ch3 with a
> twist!
>  2 # use two functions--one to compute the area
> of a pizza, and one to
>  3 # to compute cost per square inch.
>  4 # Given are the diameter and the price. A =
> pi(r**2)
>  5
>  6 # define the function that computes the area
>  7 import math
>  8 def areaCirc():
>  9 diameter = input("Please input the
> diameter of the pizza: ")
> 10 area = 4*(math.pi)*(diameter/2)**2
> 11 print "The area of the pizza is %0.2f" %
> (area)
> 12 return area
> 13
> 14
> 15 def unitCost():
> 16 price = input("Please input the cost of
> the pizza per square inch: ")
> 17 area = areaCirc()
> 18 cost = area * price
> 19 print "The cost of the pizza is %0.2f"
> %(cost)
> 20
> 21 unitCost()
> ---
>
>
>
> 
> Sponsored Link
>
> Compare mortgage rates for today.
> Get up to 5 free quotes.
> Www2.nextag.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


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


[Tutor] A Million Sevens

2006-11-17 Thread Thomas

Earlier today I typed the following into my pythonwin interactive
interpreter in windows xp:


int('7' * 10 ** 6)


I expected either an error message or it to get stuck and require me to
stop the process manually.

I read that unlike long integers in C, longs in python are only limited by
the amount of memory (and virtual memory) your system has.

Can you guess what it did?

I'm temped to end the post here, but I'm new to this list and its possible
that people might be annoyed by me not getting to the point within my
initial post, so here's what it did:

It thought about it for about 2 seconds then restarted my pc! explanations
welcome.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Million Sevens

2006-11-17 Thread Chris Hengge

Well, I dont get the point.. its not locking up my system or anything.. its
just crunching away... even while I type this...
I guess your point is that it should stop since a 32 bit O/S can only count
to:
4,294,967,296

On 11/17/06, Thomas <[EMAIL PROTECTED]> wrote:


Earlier today I typed the following into my pythonwin interactive
interpreter in windows xp:

>>> int('7' * 10 ** 6)

 I expected either an error message or it to get stuck and require me to
stop the process manually.

 I read that unlike long integers in C, longs in python are only limited
by the amount of memory (and virtual memory) your system has.

Can you guess what it did?

I'm temped to end the post here, but I'm new to this list and its possible
that people might be annoyed by me not getting to the point within my
initial post, so here's what it did:

It thought about it for about 2 seconds then restarted my pc! explanations
welcome.

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



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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Chris Hengge

I'm thinking you either have a problem with a memory leak (my memory isn't
changing, just at 100% CPU), or your CPU overheated from poor cooling since
it is at 100% utilization.

On 11/17/06, Chris Hengge <[EMAIL PROTECTED]> wrote:


Well, I dont get the point.. its not locking up my system or anything..
its just crunching away... even while I type this...
I guess your point is that it should stop since a 32 bit O/S can only
count to:
4,294,967,296

On 11/17/06, Thomas <[EMAIL PROTECTED]> wrote:

> Earlier today I typed the following into my pythonwin interactive
> interpreter in windows xp:
>
> >>> int('7' * 10 ** 6)
>
>  I expected either an error message or it to get stuck and require me to
> stop the process manually.
>
>  I read that unlike long integers in C, longs in python are only limited
> by the amount of memory (and virtual memory) your system has.
>
> Can you guess what it did?
>
> I'm temped to end the post here, but I'm new to this list and its
> possible that people might be annoyed by me not getting to the point within
> my initial post, so here's what it did:
>
> It thought about it for about 2 seconds then restarted my pc!
> explanations welcome.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Luke Paireepinart
Chris Hengge wrote:
> I'm thinking you either have a problem with a memory leak (my memory 
> isn't changing, just at 100% CPU), or your CPU overheated from poor 
> cooling since it is at 100% utilization.
yeah I second this...
there's no reason why this would reboot your computer.
At Chris: It's building a string that's 1,000,000 characters long, so it 
should be increasing your memory usage by at least 1,000,000 characters,
or 1 mb.  But that's over a probably long period of time, so you just 
didn't notice any change.
>
> On 11/17/06, *Chris Hengge* <[EMAIL PROTECTED] 
> > wrote:
>
> Well, I dont get the point.. its not locking up my system or
> anything.. its just crunching away... even while I type this...
> I guess your point is that it should stop since a 32 bit O/S can
> only count to:
> 4,294,967,296
>
> On 11/17/06, *Thomas* < [EMAIL PROTECTED]
> > wrote:
>
> Earlier today I typed the following into my pythonwin
> interactive interpreter in windows xp:
>  
> >>> int('7' * 10 ** 6)
>  
> I expected either an error message or it to get stuck and
> require me to stop the process manually.
>  
> I read that unlike long integers in C, longs in python are
> only limited by the amount of memory (and virtual memory) your
> system has.
>  
> Can you guess what it did?
>  
> I'm temped to end the post here, but I'm new to this list and
> its possible that people might be annoyed by me not getting to
> the point within my initial post, so here's what it did:
>  
> It thought about it for about 2 seconds then restarted my pc!
> explanations welcome.
>

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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Collin Hockey
A mildly educated guess would be that python tried to access memory 
Windows wasn't going to allow it to have, effectively causing a BSOD and 
  making Windows restart the system.

Thomas wrote:
> Earlier today I typed the following into my pythonwin interactive 
> interpreter in windows xp:
>  
>  >>> int('7' * 10 ** 6)
>  
> I expected either an error message or it to get stuck and require me to 
> stop the process manually.
>  
> I read that unlike long integers in C, longs in python are only limited 
> by the amount of memory (and virtual memory) your system has.
>  
> Can you guess what it did?
>  
> I'm temped to end the post here, but I'm new to this list and its 
> possible that people might be annoyed by me not getting to the point 
> within my initial post, so here's what it did:
>  
> It thought about it for about 2 seconds then restarted my pc! 
> explanations welcome.
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Million Sevens

2006-11-17 Thread Tim Peters
[Thomas]
> Earlier today I typed the following into my pythonwin interactive
> interpreter in windows xp:
>
> >>> int('7' * 10 ** 6)
>
> I expected either an error message

Unlikely, if your box has enough RAM to run WinXP :-)

> or it to get stuck and require me to stop the process manually.

Not that either, but it will take a long time.  There are two
quadratic-time base conversions going on here, one to convert the
million-digit decimal string to a large binary integer, and then again
in the other direction to display the result as a decimal string.

In all, using Python 2.4.3 from a "DOS box" shell under WinXP on a
pretty fast box, this consumed about 16 minutes of CPU time, and
process memory use peaked at a relatively measly 11.3 MB.

> I read that unlike long integers in C, longs in python are only limited by
> the amount of memory (and virtual memory) your system has.

That's right.

> Can you guess what it did?

Nope.

> I'm temped to end the post here, but I'm new to this list and its possible
> that people might be annoyed by me not getting to the point within my
> initial post,

/Very/ good guess :-)

> so here's what it did:
>
> It thought about it for about 2 seconds then restarted my pc! explanations
> welcome.

Don't have one:  as above, it worked fine when I tried it.  I wasn't
using PythonWin, although hard to guess why that would matter.  I
wouldn't be surprised to see a GUI shell die a horrid death of some
kind when asked to display a million-character string, but 2 seconds
is far too short a time for your attempt to have gotten that far.

If you want to play, break it into two steps:

>>> i = int('7' * 10 ** 6)

That much does /only/ the decimal-string -> large binary integer part.
 Don't display i until the next step.  For display, it's /enormously/
faster to convert to a power-of-2 output base (instead of to decimal),
like

>>> print hex(i)

In more detail,

>>> from time import clock as now
>>> from math import log10
>>> log10(i)
99.89085553051
>>> t = now(); ashex = hex(i); now() - t
0.0097427830685340843

That is, converting the big binary integer to a hex string took less
than a hundreth of a second, instead of the minutes required to
convert to a decimal string.

>>> len(ashex)
830485
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] any win32com experts out there?

2006-11-17 Thread Alan Gauld
I'm no expert but I get the same response when I tried it.

I googled a bit and found this long chain of useful looking stuff:

http://www.velocityreviews.com/forums/t345123-how-to-receive-events-eg-user-mouse-clicks-from-ie.html

One possibility mentioned at the end is:


Adding win32gui.PumpWaitingMessages() to the wait loop
seems to allow both event hooks to run without blocking each other.

But it discusses IE not iTunes so it may not be relevant...

Alan G.

"Orri Ganel" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hello again.  As I've mentioned in the last couple of emails, I'm 
> having
> trouble registering iTunes for events, and I don't know if it's a 
> bug
> with iTunes or with win32com, or if there's something I'm doing 
> wrong.
> So, if any win32com gurus out there know why the following doesn't 
> work
> and how to change it so it does work, a tip would be much 
> appreciated.
>
> >>> import win32com.client
> >>> class iTunesEvents:
>def __init__(self):
>print "starting iTunesEvents"
>def OnPlayerPlayEvent(self, track):
>print "playing"
>def OnPlayerStopEvent(self, track):
>print "stopped"
>def OnDatabaseChangedEvent(self, deleted, changed):
>print "database changed"
>def OnPlayerPlayingTrackChangedEvent(self, track):
>print "info on current track changed"
>def OnCOMCallsDisabledEvent(self, reason):
>print "com calls disabled"
>def OnCOMCallsEnabledEvent(self):
>print "com calls enabled"
>def OnQuittingEvent(self):
>print "quitting"
>def OnAboutToPromptUserToQuitEvent(self):
>print "prompting user to quit"
>def OnSoundVolumeChangedEvent(self, newvolume):
>print "volume changed"
>
>
> >>> iTunes = 
> >>> win32com.client.gencache.EnsureDispatch("iTunes.Application")
> >>> iTEvents = win32com.client.WithEvents(iTunes, iTunesEvents)
> starting iTunesEvents
> >>> iTunes.Play()
> >>> iTunes.Pause()
> >>> iTunes.Play()
> >>> iTunes.Pause()
>
> iTunes did, in fact, play and pause as expected, but as can be seen, 
> no
> events were caught.
>
> Stumped,
> Orri
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Chris Hengge

Not that it changes your reply, but just for my own sanity:
int('7' * 10 ** 6) <- does this not just type-cast a char into an int?

Meaning that rather then consuming 1024k as you stated, it would consume
2048k at the peak of the calculation(2bytes per char? * 1m = 2048k) then
typecasting to int would drop it back down to 1k (1byte per int * 1m = 1024k

So, just for sake of getting to a point since I missed the one by the
original poster.. why would you not convert that 7 from a char to an int
first? That calculation is almost instant, and it doesn't require the memory
rollercoaster that this calculation would require..

Anyways.. back to the poster...

Perhaps you wanted an error like this?

print '=' * 10
Traceback (most recent call last):
 File "", line 1, in ?
MemoryError


On 11/17/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:


Chris Hengge wrote:
> I'm thinking you either have a problem with a memory leak (my memory
> isn't changing, just at 100% CPU), or your CPU overheated from poor
> cooling since it is at 100% utilization.
yeah I second this...
there's no reason why this would reboot your computer.
At Chris: It's building a string that's 1,000,000 characters long, so it
should be increasing your memory usage by at least 1,000,000 characters,
or 1 mb.  But that's over a probably long period of time, so you just
didn't notice any change.
>
> On 11/17/06, *Chris Hengge* <[EMAIL PROTECTED]
> > wrote:
>
> Well, I dont get the point.. its not locking up my system or
> anything.. its just crunching away... even while I type this...
> I guess your point is that it should stop since a 32 bit O/S can
> only count to:
> 4,294,967,296
>
> On 11/17/06, *Thomas* < [EMAIL PROTECTED]
> > wrote:
>
> Earlier today I typed the following into my pythonwin
> interactive interpreter in windows xp:
>
> >>> int('7' * 10 ** 6)
>
> I expected either an error message or it to get stuck and
> require me to stop the process manually.
>
> I read that unlike long integers in C, longs in python are
> only limited by the amount of memory (and virtual memory) your
> system has.
>
> Can you guess what it did?
>
> I'm temped to end the post here, but I'm new to this list and
> its possible that people might be annoyed by me not getting to
> the point within my initial post, so here's what it did:
>
> It thought about it for about 2 seconds then restarted my pc!
> explanations welcome.
>


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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Alan Gauld
Tim,

Every now and then you pop up on the tutor list to answer
"interesting" posts like this:

"Tim Peters" <[EMAIL PROTECTED]> wrote
> That much does /only/ the decimal-string -> large binary integer 
> part.
> Don't display i until the next step.  For display, it's /enormously/
> faster to convert to a power-of-2 output base (instead of to 
> decimal),
> like
>
 print hex(i)

But given the number of other groups you contribute to, I can't 
believe you
have time to read all of them, so how do you filter out the 
interesting
ones?

Yours Curiously,

Alan G.


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


Re: [Tutor] A Million Sevens

2006-11-17 Thread Thomas
On 18/11/06, Chris Hengge <[EMAIL PROTECTED]> wrote:
> Not that it changes your reply, but just for my own sanity:
> int('7' * 10 ** 6) <- does this not just type-cast a char into an int?
>
> Meaning that rather then consuming 1024k as you stated, it would consume 
> 2048k at the peak of the calculation(2bytes per char? * 1m = 2048k) then 
> typecasting to int would drop it back down to 1k (1byte per int * 1m = 1024k
>
> So, just for sake of getting to a point since I missed the one by the 
> original poster.. why would you not convert that 7 from a char to an int 
> first? That calculation is almost instant, and it doesn't require the memory 
> rollercoaster that this calculation would require..
>
> Anyways.. back to the poster...

Thanks, original poster here, I was just reading my new book, Core
Python Programming (2nd Edition) and it said that there was no
in-built limit to the size of a long in python (as compared to C) so I
wanted to test it out. I just typed the first way of getting a massive
number that came into my head.

> Perhaps you wanted an error like this?
>
> print '=' * 10
> Traceback (most recent call last):
>   File "", line 1, in ?
> MemoryError

Now that is exactly what I thought I might get. I'll try it again with
some of the variations mentioned on Monday when I get back to that
computer.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Million Sevens

2006-11-17 Thread Luke Paireepinart
Chris Hengge wrote:
> Not that it changes your reply, but just for my own sanity:
> int('7' * 10 ** 6) <- does this not just type-cast a char into an int?
it typecasts a 1,000,000 character long string to an integer.
>
> Meaning that rather then consuming 1024k as you stated, it would 
> consume 2048k at the peak of the calculation(2bytes per char? * 1m = 
> 2048k)
um, I'm not sure how you came up with these numbers.
Obviously there are other considerations here besides the amount of 
memory required to store the data (like the memory used by the string 
class, and the fact that strings are probably stored as a linked list, 
so 1 byte for the char, and 4 bytes for the char pointer).  If you just 
considered the characters, they're only 1 byte, not 2, assuming you're 
using regular ascii characters (0x00 - 0xff is 255 unique bit strings, 
and since english and other latin-based languages only need 26*2 (lower 
+ uppercase letters) + 10 for numbers, and various other symbols, 255 is 
plenty.), so it'd be 1024k of memory usage.  if you considered the 
pointers also, it'd be 5116k.  and various other things raise this 
memory usage more.

 >then typecasting to int would drop it back down to 1k (1byte per int * 
1m = 1024k
no, typecasting it to int won't drop it to 1k.
What's happening is that, inside the function call (the 'int' type 
conversion)
a 7-million character string, i.e. '777'
is generated, and then the 'int()' call converts it to the equivalent 
integer,
...7
Not to a million different 7 integers.
Do you see this?  It's only one number.
and I don't know where you got 1 byte per int.  Even if they were 
1,000,000 individual sevens, the C library reserves the same amount of 
memory for an int no matter what size it is,
so even though 7 is a low number, there'd still be 4 bytes of usage per 
int (depending on the implementation)
Depending on how exactly Python stores long ints, it should use 
substantially less memory than the equivalent character string would.

Recall that a 2 byte unsigned integer can hold all numbers less than 
2**16 (65536), a
4 byte (the default long-int size) unsigned integer can hold all numbers 
less than 2**32 (4294967296)
which is ten decimal digits long.  So you see that the memory usage of 
the python long integer is going to be much smaller than the usage of 
the character string that represents it.
The string's size usage grows linearly with the data it represents ('7' 
will take x memory, '77' will take x*2 memory, '777' will take x*3 memory)
while the size of the integer grows much faster than the memory usage 
memory usage
(0-256 takes 1 byte, 0-65536 takes 2 bytes, 0-16777216 takes 3 bytes, 
0-4294967296 takes 4 bytes, any number less than 308 digits long takes 
128 bytes)

>
> So, just for sake of getting to a point since I missed the one by the 
> original poster.. why would you not convert that 7 from a char to an 
> int first? That calculation is almost instant, and it doesn't require 
> the memory rollercoaster that this calculation would require..
Because, that's not what's happening.  He's not converting 1,000,000 '7' 
chars to the integer 7,
he's converting a string of 1,000,000 7s into an integer with 1,000,000 
digits, all of which are 7.

Consider this example:
 >>> '7' * 5
'7'

You see, it's a string of length 5 with the contents just the original 
character, '7', repeating.
You can do something similar with a string rather than a character,
 >>> 'hello' * 3
'hellohellohello'

Now consider this:
 >>> '7' * 2 ** 5
''

In the order of operations for Python, exponents come before 
multiplications, so this is equivalent to
'7' * 32
as evidenced by:
 >>> len('7' * 2 ** 5)
32

Hope that helps and makes sense,
-Luke

>
> Anyways.. back to the poster...
>
> Perhaps you wanted an error like this?
>
> print '=' * 10
> Traceback (most recent call last):
>   File "", line 1, in ?
> MemoryError
>
>
> On 11/17/06, *Luke Paireepinart* <[EMAIL PROTECTED] 
> > wrote:
>
> Chris Hengge wrote:
> > I'm thinking you either have a problem with a memory leak (my memory
> > isn't changing, just at 100% CPU), or your CPU overheated from poor
> > cooling since it is at 100% utilization.
> yeah I second this...
> there's no reason why this would reboot your computer.
> At Chris: It's building a string that's 1,000,000 characters long,
> so it
> should be increasing your memory usage by at least 1,000,000
> characters,
> or 1 mb.  But that's over a probably long period of time, so you just
> didn't notice any change.
> >
> > On 11/17/06, *Chris Hengge* <[EMAIL PROTECTED]
> 
> > mailto:[EMAIL PROTECTED]>>> wrote:
> >
> > Well, I dont get the point.. its not locking up my system or
> > anything.. its just crunching away... even while I type this..