Re: [Tutor] Integer division Help requested

2006-10-03 Thread Luke Paireepinart
Joseph John wrote:
> HI All
>I am trying out python
> I  understoon when
>  7/3  gives "2"
> But I cannot understand ,
>   when i give 7/-3  gives resuls "-3"
> I feel
>  7/3  should give -2
>
> since  integer  divison returns floor
>Advice requested
>Thanks
>   Joseph John
>
 >>> import math
 >>> math.floor(-3.2)
-4.0
 >>> math.floor(-4.1)
-5.0
 >>> math.floor(4.1)
4.0

The floor is the next lowest integer to the float.
floor(-3.1) will be -4
floor(3.) will be 3
HTH,
-Luke
> 
>
> 
>
> ___
> 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] Integer division Help requested

2006-10-03 Thread Joseph John
HI All    I am trying out python I  understoon when  7/3  gives "2"But I cannot understand ,   when i give 7/-3  gives resuls "-3"I feel  7/3  should give -2 
since  integer  divison returns floor    Advice requested   Thanks   Joseph John
      
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] save configuration of one application.

2006-10-03 Thread John Fouhy
On 04/10/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
> you can make a config.ini file and use that one module that parses ini
> files.
> I can't remember what it's called.
> configparser I think, but I wouldn't bet my life on it :)

Yes, ConfigParser.

The docs for ConfigParser are a bit confusing in places (IMO), but
there are people here who can help if you get stuck.

> Or you could just write the settings out to a file.

Using ConfigParser means your configuratino files will be
human-readable and human-editable.  If you don't care about that, you
could stick them in a dictionary and use pickle to write it to a file.
 Or use the shelve module.

> If you choose to go the latter route, keep in mind that  modules are
> compiled to .pyc files upon importation,
> so you'll have to remove those anytime you modify config.py or the old
> code will be used instead.

The python interpreter should check the timestamps on foo.py vs
foo.pyc, and recompile if it thinks things have changed.

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


Re: [Tutor] save configuration of one application.

2006-10-03 Thread Luke Paireepinart
hok kakada wrote:
> Hi,
>
> Can anybody guide me how to write the code for configure one application?
> For example, I want to set Font and Color for a textbox; later when the 
> application is opened again. the previous font is set.
>
> So, which related function are used to form this feature?
>   
you can make a config.ini file and use that one module that parses ini 
files.
I can't remember what it's called.
configparser I think, but I wouldn't bet my life on it :)

Or you could just write the settings out to a file.
Even better, if you're not making a real-world application and/or you can
trust your users not to muck around with stuff, just make all your 
settings in a class in a module you import.

Eg:

# config.py
class Settings(object):
   font = 'Veranda'
   color = 'Black'

#- program.py

from config import Settings
print Settings.color

#

Hope that makes sense.
If you choose to go the latter route, keep in mind that  modules are 
compiled to .pyc files upon importation,
so you'll have to remove those anytime you modify config.py or the old 
code will be used instead.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] save configuration of one application.

2006-10-03 Thread hok kakada
Hi,

Can anybody guide me how to write the code for configure one application?
For example, I want to set Font and Color for a textbox; later when the 
application is opened again. the previous font is set.

So, which related function are used to form this feature?

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


Re: [Tutor] What not to put in __init__()

2006-10-03 Thread Kent Johnson
Tony Cappellini wrote:
> 
> I've just inherited a lot of python code to maintain.
> 
> The __init__ functions in many of the classes are very long- some over 
> 100 lines.
> 
> I like to keep functions/methods short & readable at a glance, if possible.
> 
> 1. Is it good methodology to move some code from _init__ to it's own 
> method within the class?

Sure. __init__() is just another method, there's no reason not to break 
it up if that will make it more readable. You might prefix the names of 
the new functions with _ to indicate that they are implementation 
details, not part of the public interface.

I once worked on a Java class that had a 1000-line constructor, so 
consider yourself lucky :-)
>   
> 
>  For example there are many for loops with several levles of nested if 
> statements within them.
> I'd like to move each of these for/if chunks to distinct methods, so to 
> simplify the look of __init__, without modifying how it works.

Go for it. You might want to write some unit tests first if you don't 
have any.

Kent

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


Re: [Tutor] byte[] array

2006-10-03 Thread Kent Johnson
gert wohlgemuth wrote:
> Hi,
> 
> I'm used to java byte arrays (byte[]) and need the same type in python,
> cause we have a couple of established web services, which return a byte[] or
> take a byte[] as parameter.
> 
> As anybody an idea how I can do this in python?

A string can be used as a byte array. The struct module can help if you 
need to pack and unpack binary values into a string.

Kent

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


[Tutor] What not to put in __init__()

2006-10-03 Thread Tony Cappellini
I've just inherited a lot of python code to maintain.The __init__ functions in many of the classes are very long- some over 100 lines.I like to keep functions/methods short & readable at a glance, if possible.
1. Is it good methodology to move some code from _init__ to it's own method within the class?    For example there are many for loops with several levles of nested if statements within them.I'd like to move each of these for/if chunks to distinct methods, so to simplify the look of __init__, without modifying how it works.
thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Random Variable at root

2006-10-03 Thread Jonathon Sisson
Hugo,

You need to create an instance of the Root class before you can call
rootState in your final print statement.  Your code doesn't do this.  A
class is merely a template, something like blueprints to a house.  For
you to be able to unlock the front door (for instance), you need to
actually build a specific "instance" of the house first.  "313 Hillcrest
Lane" and "4214 Willowbrook Drive" might be two houses that have the
same blueprint, but maintain different states from each other because
they are different "instances" of class House.

To make this work, remove the final print statement and replace it with
this:

# instantiates ("builds") a copy of the Root class and names it "root"
root = Root(0)
print "The character state at the root is %0.f" % root._rootState

Note the change to the end of the print statement.  Your code has
rootState, mine has root._rootState (the specific instance of Root,
followed by the "dot" operator, followed by the attribute or method you
are trying to access).  I'm a bit confused as to whether you intended
_rootState to be a copy of the *original* value passed in, with
rootState maintaining a copy of the *current* value, or if that is a
typo.  Please elaborate.

Hope this helps clear up confusion (as opposed to creating more...heh).

Jonathon


halamillo wrote:
> Hello, 
>  I'm a really-green-to-python Biology Grad Student. I;m starting this
> code so I can assign either a 0 or a 1 at a root node that I will
> later evolve throughout a tree, but for some reason it is not printing
> the rootState value.  I know its probably a really stupid mistake, but
> I can't seem to work it out. Can anyone give me insight why? Thanks. 
>
> import random
> from random import random as rnd
> from math import exp
>
>
> class Root:
> """Single Node in a Tree"""
>
> def __init__( self, rootState ):
>
>
> self._rootState = rootState
>
> # random choice for root state from a list
> for i in range(1):
> rootState = random.choice([0, 1])
>
>
> def __str__( self ):
> """Root string representation"""
>
>
> return str( self._rootState )
> print "The character state at the root is %0.f" % rootState
>
>
> Hugo Alamillo
> Biological Sciences
> 265 Eastlick
> PO Box 644236
> Washington State University
> Pullman, WA 99164
>
>
>
>
>
> 
>
> ___
> 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] Random Variable at root

2006-10-03 Thread Eric Walstad
Hi Hugo,

halamillo wrote:
> I;m starting this
> code so I can assign either a 0 or a 1 at a root node that I will later 
> evolve throughout a tree, but for some reason it is not printing the 
> rootState value.

It's not clear to me which variable you are trying to assign a zero or 
one.  Here's my guess at how you are wanting to use your class:

 >>> import random
 >>> from random import random as rnd
 >>>
 >>>
 >>> class Root:
... """Single Node in a Tree"""
... def __init__(self, rootState):
... self._rootState = rootState
... # random choice for root state from a list
... self._rootState = random.choice([0, 1])
... def __str__(self):
... return str( self._rootState )
...
 >>>
 >>>
 >>> rs = 99
 >>> r = Root(rootState=rs)
 >>> print rs
99
 >>> print r
0
 >>> print "The character state at the root is %0.f" % float(r._rootState)
The character state at the root is 0


Note:
   1. I removed the loop in your __init__method because it was simply 
making two random choices when one should do.
   2. I assigned the random choice to the class' _rootState attribute, 
not the rootState argument to the __init__ method.  You can also make a 
"rootState" property of the class so that the last line, above, would 
look like:
 >>> print "The character state at the root is %0.f" % float(r.rootState)

I hope that helps.

Eric.


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


[Tutor] number game

2006-10-03 Thread Jonathon Sisson
Mike,

The algorithm you use is what causes the infinite loop (it cycles
through the same numbers repeatedly).  I've updated the code and I'll
post it here:

def num(number):
r=input("range >")
ran=range(r+1)
guess=r/2
print guess
guesses=1
min = 0
max = r
while guess!=number:
if guess < number:
min = guess
else:
max = guess
guess = (min + max) / 2
guesses += 1
print guess
print "i got the number",number,"in",guesses,"guesses"



mike viceano wrote:
> i wrote a program that guesses number in the most effective way i could 
> think of but i keep running into endless loops any help fixing it would be 
> great :)
>
> here it is:
>
>
> def num(number):
> r=input("range >")
> ran=range(r+1)
> guess=r/2
> print guess
> guesses=1
> while guess!=number:
> if guess < number:
> guess= ((r-guess)/2)+guess
> print guess
> guesses=guesses+1
> elif guess > number:
> guess= ((r-guess)/2)-guess
> print guess
> guesses=guesses+1
> print "i got the number",number,"in",guesses,"guesses"
>
>
>
> my only idea is to make it so it dosint re guess numbers but i would rather 
> just find out what is wrong
> and fix it
>
> _
> Express yourself - download free Windows Live Messenger themes! 
> http://clk.atdmt.com/MSN/go/msnnkwme002001msn/direct/01/?href=http://imagine-msn.com/themes/vibe/default.aspx?locale=en-us&source=hmtagline
>
> ___
> 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] Random Variable at root

2006-10-03 Thread halamillo
Hello,  I'm a really-green-to-python Biology Grad Student. I;m starting this code so I can assign either a 0 or a 1 at a root node that I will later evolve throughout a tree, but for some reason it is not printing the rootState value.  I know its probably a really stupid mistake, but I can't seem to work it out. Can anyone give me insight why? Thanks. import randomfrom random import random as rndfrom math import expclass Root:    """Single Node in a Tree"""    def __init__( self, rootState ):                self._rootState = rootState            # random choice for root state from a list        for i in range(1):            rootState = random.choice([0, 1])    def __str__( self ):        """Root string representation"""            return str( self._rootState )print "The character state at the root is %0.f" % rootState Hugo AlamilloBiological Sciences265 EastlickPO Box 644236Washington State UniversityPullman, WA 99164 ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] number game

2006-10-03 Thread mike viceano
i wrote a program that guesses number in the most effective way i could 
think of but i keep running into endless loops any help fixing it would be 
great :)

here it is:


def num(number):
r=input("range >")
ran=range(r+1)
guess=r/2
print guess
guesses=1
while guess!=number:
if guess < number:
guess= ((r-guess)/2)+guess
print guess
guesses=guesses+1
elif guess > number:
guess= ((r-guess)/2)-guess
print guess
guesses=guesses+1
print "i got the number",number,"in",guesses,"guesses"



my only idea is to make it so it dosint re guess numbers but i would rather 
just find out what is wrong
and fix it

_
Express yourself - download free Windows Live Messenger themes! 
http://clk.atdmt.com/MSN/go/msnnkwme002001msn/direct/01/?href=http://imagine-msn.com/themes/vibe/default.aspx?locale=en-us&source=hmtagline

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


[Tutor] byte[] array

2006-10-03 Thread gert wohlgemuth
Hi,

I'm used to java byte arrays (byte[]) and need the same type in python,
cause we have a couple of established web services, which return a byte[] or
take a byte[] as parameter.

As anybody an idea how I can do this in python?

Thx,

g.

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


Re: [Tutor] What is a Python "project"?

2006-10-03 Thread Brian van den Broek
Dick Moores said unto the world upon 03/10/06 01:41 PM:
> At 10:01 AM 10/3/2006, Mike Hansen wrote:



>> I've been doing some web programming, so my "projects" consist of
>> cheetah template files, CSS, config files, a handful of python
>> modules...
> 
> Why do you make python modules part of a project? They can be used 
> without copying them around, can't they? Or is it that by a project 
> is meant in part a list of pointers to all the files you mean for 
> that program to use, and you don't actually have to copy or move them 
> so they are all in the same folder/directory?
> 
> Thanks, Mike.
> 
> Dick


Hi Dick,

I've never used Wing, but unless its `project' concept is radically 
different than many other editors, it isn't about organizing the files 
on disk. Rather, it is about organizing a group of files into a 
collection the editor can open in one swell foop. The idea is to 
liberate you from having to recall just where the files live and allow 
you to merely open a group of related files and get on with what 
you're up to. (It is sort of like saving a group of tabs in firefox.)

Best,

Brian vdB

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


Re: [Tutor] question about sys.path and importing

2006-10-03 Thread Dick Moores
At 12:30 PM 10/3/2006, you wrote:
>Dick Moores wrote:
>>At 11:38 AM 10/3/2006, Kent Johnson wrote:
>>>Normally you will need to either
>>>- make 'mine' be a package, by creating an empty file named
>>>site-packages\mine\__init__.py, and changing your imports to include the
>>>package name (from mine import mycalc), or
>>>- add site-packages\mine to sys.path, maybe by creating a .pth file.
>>>http://www.python.org/doc/2.4.3/lib/module-site.html
>>I went with your first way, and it works with a script in python25\dev:
>># 1test-8.py
>>from mine import mycalc
>>print mycalc.numberCommas(12341234123)
>>  >>>
>>Evaluating 1test-8.py
>>12,341,234,123
>>But fails here:
>># 1test-9.py
>>from mine import mycalc
>>from mycalc import numberCommas
>>print numberCommas(12341234123)
>>  >>>
>>Evaluating 1test-9.py
>>Traceback (most recent call last):
>>File "", line 1, in 
>>ImportError: No module named mycalc
>>  >>>
>
>Well this is different code.

Yes, that was my point. Sorry I didn't make that clear.

>Try what you did in the first one:
>from mine import mycalc
>print mycalc.numberCommas(12341234123)
>
>or
>from mine.mycalc import numberCommas

Good! Didn't know I could do "from mine.mycalc import numberCommas".

Thanks,

Dick



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


Re: [Tutor] CGKit

2006-10-03 Thread Carlos
Thanks Luke and Kent,

To get CGKit running I need to do two different things:

First the Python Computer Graphics Kit is composed by an 
"cgkit-2.0.0alpha6.win32-py2.4.exe" file and some dependencies that are 
PyProtocols, numarray, PyOpenGL, PIL, pygame, PyODE and pySerial, with 
the exception of PyProtocols I have installed all of them. When I try to 
install PyProtocols from the command prompt it tells me that " The .NET 
Framework SDK needs to be installed before building extensions for 
Python." I went to Microsoft and installed the "Microsoft .NET Framework 
SDK v2.0", but the problem persists.

The second part is the Python Maya Plug-In. I think that this is the 
tricky one, it is asking me to install Py++, that I have already 
installed. Now my problem comes when I have to compile the source code 
with Boost.Python, this one asks for Boost Jam and Boost. I have been 
trying to install this things but still have no clue at how.

Thanks for your help
Carlos




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


Re: [Tutor] Keycodes

2006-10-03 Thread Luke Paireepinart
Chris Hengge wrote:
> Is it possible to capture keycodes for things like ctrl/alt/shift?
>  
> I've made a script using msvcrt but it seems to skips most of the non 
> alphanumeric keys.
>  
> Thanks.
You can in Pygame.
I don't know about msvcrt.
> 
>
> ___
> 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] Keycodes

2006-10-03 Thread Chris Hengge
Is it possible to capture keycodes for things like ctrl/alt/shift?
 
I've made a script using msvcrt but it seems to skips most of the non alphanumeric keys.
 
Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about sys.path and importing

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> At 11:38 AM 10/3/2006, Kent Johnson wrote:
>> Normally you will need to either
>> - make 'mine' be a package, by creating an empty file named
>> site-packages\mine\__init__.py, and changing your imports to include the
>> package name (from mine import mycalc), or
>> - add site-packages\mine to sys.path, maybe by creating a .pth file.
>> http://www.python.org/doc/2.4.3/lib/module-site.html
> 
> I went with your first way, and it works with a script in python25\dev:
> # 1test-8.py
> from mine import mycalc
> print mycalc.numberCommas(12341234123)
> 
>  >>>
> Evaluating 1test-8.py
> 12,341,234,123
> 
> But fails here:
> # 1test-9.py
> from mine import mycalc
> from mycalc import numberCommas
> print numberCommas(12341234123)
> 
>  >>>
> Evaluating 1test-9.py
> Traceback (most recent call last):
>File "", line 1, in 
> ImportError: No module named mycalc
>  >>>

Well this is different code. Try what you did in the first one:
from mine import mycalc
print mycalc.numberCommas(12341234123)

or
from mine.mycalc import numberCommas

Kent

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


Re: [Tutor] What is a Python "project"?

2006-10-03 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Dick Moores
> Sent: Tuesday, October 03, 2006 12:41 PM
> To: tutor@python.org
> Subject: Re: [Tutor] What is a Python "project"?
> 
> At 10:01 AM 10/3/2006, Mike Hansen wrote:
> >
> >
> > > -Original Message-
> > > From: [EMAIL PROTECTED]
> > > [mailto:[EMAIL PROTECTED] On Behalf Of Dick Moores
> > > Sent: Tuesday, October 03, 2006 12:52 AM
> > > To: tutor@python.org
> > > Subject: [Tutor] What is a Python "project"?
> > >
> > > I tried out Wing IDE Personal
> > > () off and on for 
> 30 days, and
> > > then, finding it easy to use (probably because it's designed for
> > > Python), decided to buy it. I'm happy with it, and very 
> pleased with
> > > the fast response from technical support available by email.
> > >
> > > I've never written anything in Python other than 
> single-file scripts.
> > > WingIDE has the ability to handle "projects", which apparently
> > > consist of files and "packages". But what is a project? 
> What does a
> > > project have that can't be put into a single .py file script?
> > >
> > > Thanks,
> > >
> > > Dick Moores
> > >
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> >
> >When your apps start getting larger, you'll find that you want to put
> >common functions into modules that you can hopefully reuse for other
> >programs.
> 
> Yes, I already have done this. But in scripts I write I simply import 
> the functions I want from that module. I don't think in terms of 
> making a project. Maybe I've missed your point?
> 
> >http://docs.python.org/tut/node8.html
> 
> Now, that page has a lot of stuff I didn't know. Thanks! Maybe I 
> should read the whole tutorial. I see that's it's been updated for 
> 2.5. And it's by GvR. http://docs.python.org/tut/tut.html
> 
> >I've been doing some web programming, so my "projects" consist of
> >cheetah template files, CSS, config files, a handful of python
> >modules...
> 
> Why do you make python modules part of a project? They can be used 
> without copying them around, can't they? Or is it that by a project 
> is meant in part a list of pointers to all the files you mean for 
> that program to use, and you don't actually have to copy or move them 
> so they are all in the same folder/directory?
> 
> Thanks, Mike.
> 
> Dick
> 

I use Komodo and VIM. With Komodo, a project is just a collection of
related files. I'd imagine that WingIDE does something similar. You set
up a project and add files to them. It's just a way to keep organized.

As far as the python modules in my project, they are ones that I've
written not 3rd party stuff. If the day comes that I use my own modules
in more than one application/project, I'll probably move them to site
packages.

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


Re: [Tutor] question about sys.path and importing

2006-10-03 Thread Dick Moores
At 11:38 AM 10/3/2006, Kent Johnson wrote:
>Normally you will need to either
>- make 'mine' be a package, by creating an empty file named
>site-packages\mine\__init__.py, and changing your imports to include the
>package name (from mine import mycalc), or
>- add site-packages\mine to sys.path, maybe by creating a .pth file.
>http://www.python.org/doc/2.4.3/lib/module-site.html

I went with your first way, and it works with a script in python25\dev:
# 1test-8.py
from mine import mycalc
print mycalc.numberCommas(12341234123)

 >>>
Evaluating 1test-8.py
12,341,234,123

But fails here:
# 1test-9.py
from mine import mycalc
from mycalc import numberCommas
print numberCommas(12341234123)

 >>>
Evaluating 1test-9.py
Traceback (most recent call last):
   File "", line 1, in 
ImportError: No module named mycalc
 >>>

I guess I can live with that.

>Gee, maybe I should just invite you over and we can talk ;)

Careful now. I just might show up!

Dick

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


Re: [Tutor] What is a Python "project"?

2006-10-03 Thread Dick Moores
At 10:01 AM 10/3/2006, Mike Hansen wrote:
>
>
> > -Original Message-
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of Dick Moores
> > Sent: Tuesday, October 03, 2006 12:52 AM
> > To: tutor@python.org
> > Subject: [Tutor] What is a Python "project"?
> >
> > I tried out Wing IDE Personal
> > () off and on for 30 days, and
> > then, finding it easy to use (probably because it's designed for
> > Python), decided to buy it. I'm happy with it, and very pleased with
> > the fast response from technical support available by email.
> >
> > I've never written anything in Python other than single-file scripts.
> > WingIDE has the ability to handle "projects", which apparently
> > consist of files and "packages". But what is a project? What does a
> > project have that can't be put into a single .py file script?
> >
> > Thanks,
> >
> > Dick Moores
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>When your apps start getting larger, you'll find that you want to put
>common functions into modules that you can hopefully reuse for other
>programs.

Yes, I already have done this. But in scripts I write I simply import 
the functions I want from that module. I don't think in terms of 
making a project. Maybe I've missed your point?

>http://docs.python.org/tut/node8.html

Now, that page has a lot of stuff I didn't know. Thanks! Maybe I 
should read the whole tutorial. I see that's it's been updated for 
2.5. And it's by GvR. http://docs.python.org/tut/tut.html

>I've been doing some web programming, so my "projects" consist of
>cheetah template files, CSS, config files, a handful of python
>modules...

Why do you make python modules part of a project? They can be used 
without copying them around, can't they? Or is it that by a project 
is meant in part a list of pointers to all the files you mean for 
that program to use, and you don't actually have to copy or move them 
so they are all in the same folder/directory?

Thanks, Mike.

Dick



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


Re: [Tutor] question about sys.path and importing

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> This morning I was sternly warned by Wingware support not to leave my 
> module of useful functions in Python25\Lib. So I put it in a 
> subfolder in site-packages I named "mine". Importing of or from that 
> module, mycalc.py goes well, to my surprise, because of
> 
>  >>> import sys
>  >>> [x for x in sys.path if "site-packages" in x]
> ['e:\\Python25\\lib\\site-packages', 
> 'e:\\Python25\\lib\\site-packages\\win32', 
> 'e:\\Python25\\lib\\site-packages\\win32\\lib', 
> 'e:\\Python25\\lib\\site-packages\\Pythonwin', 
> 'e:\\Python25\\lib\\site-packages\\wx-2.6-msw-unicode']
>  >>>
> 
> in which "mine" isn't included, even though other folders in site-packages 
> are.
> 
> Can someone explain this, please?

If the program that does the import is also in site-packages\mine, that 
would explain it. When you run a script its directory is added to sys.path.

Normally you will need to either
- make 'mine' be a package, by creating an empty file named 
site-packages\mine\__init__.py, and changing your imports to include the 
package name (from mine import mycalc), or
- add site-packages\mine to sys.path, maybe by creating a .pth file.
http://www.python.org/doc/2.4.3/lib/module-site.html

Gee, maybe I should just invite you over and we can talk ;)

Kent

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


[Tutor] question about sys.path and importing

2006-10-03 Thread Dick Moores
This morning I was sternly warned by Wingware support not to leave my 
module of useful functions in Python25\Lib. So I put it in a 
subfolder in site-packages I named "mine". Importing of or from that 
module, mycalc.py goes well, to my surprise, because of

 >>> import sys
 >>> [x for x in sys.path if "site-packages" in x]
['e:\\Python25\\lib\\site-packages', 
'e:\\Python25\\lib\\site-packages\\win32', 
'e:\\Python25\\lib\\site-packages\\win32\\lib', 
'e:\\Python25\\lib\\site-packages\\Pythonwin', 
'e:\\Python25\\lib\\site-packages\\wx-2.6-msw-unicode']
 >>>

in which "mine" isn't included, even though other folders in site-packages are.

Can someone explain this, please?

Dick Moores

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


Re: [Tutor] What is a Python "project"?

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> I tried out Wing IDE Personal 
> () off and on for 30 days, and 
> then, finding it easy to use (probably because it's designed for 
> Python), decided to buy it. I'm happy with it, and very pleased with 
> the fast response from technical support available by email.
> 
> I've never written anything in Python other than single-file scripts. 
> WingIDE has the ability to handle "projects", which apparently 
> consist of files and "packages". But what is a project? What does a 
> project have that can't be put into a single .py file script?

When a program gets big enough, it is awkward to put everything into a 
single .py file. Breaking the program up into modules or packages makes 
it easier to understand, navigate and test the code. You might have test 
code in a separate module from the code being tested and utility scripts 
that are not part of the main program. You could have a command-line 
front end and a GUI front end. Also you might have non-Python files in 
the project such as configuration files, templates, localization 
resources, build scripts, files needed by tests...

Kent

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


Re: [Tutor] What is a Python "project"?

2006-10-03 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Dick Moores
> Sent: Tuesday, October 03, 2006 12:52 AM
> To: tutor@python.org
> Subject: [Tutor] What is a Python "project"?
> 
> I tried out Wing IDE Personal 
> () off and on for 30 days, and 
> then, finding it easy to use (probably because it's designed for 
> Python), decided to buy it. I'm happy with it, and very pleased with 
> the fast response from technical support available by email.
> 
> I've never written anything in Python other than single-file scripts. 
> WingIDE has the ability to handle "projects", which apparently 
> consist of files and "packages". But what is a project? What does a 
> project have that can't be put into a single .py file script?
> 
> Thanks,
> 
> Dick Moores
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

When your apps start getting larger, you'll find that you want to put
common functions into modules that you can hopefully reuse for other
programs. 

http://docs.python.org/tut/node8.html

I've been doing some web programming, so my "projects" consist of
cheetah template files, CSS, config files, a handful of python
modules...

I hope that helps.

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


Re: [Tutor] [Fwd: Re: database web app, what tool?]

2006-10-03 Thread Mike Hansen
> To:   Liam Clarke <[EMAIL PROTECTED]>
> References:   <[EMAIL PROTECTED]> 
> <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
> 
> 
> 
> Thank you Liam,
> 
> 
> Our web server is IIS, but it would be no problem to use 
> another one for this
> particular purpose.
> 
> I'v been looking for information on how to design tables for 
> the web with
> python, without succes so far. Can you point me any tutorial?
> 
> 
> Paulino
> 
> 

Do you mean HTML tables? If so, it'd probably be best to use a
templating system like cheetah. If you want to do it "the hard way", you
can do it from your python program with print statements.(print
"...") I think there's a module out there that can generate
HTML for you.

I'm not sure how far you've gone with doing web programming with python.
You might look at this article

http://www.devshed.com/c/a/Python/Writing-CGI-Programs-in-Python/

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


Re: [Tutor] CGKit

2006-10-03 Thread Kent Johnson
Carlos wrote:
> Hi,
> 
> I'm new to python and have been trying to install CGKit, it has a bunch 
> of dependencies that I have to install. /At this point /the problem that 
> I have is that I need to install Py++, I already installed pygccxml and 
> GCC-XML. To install Py++ I downloaded it, and extracted it, next I 
> opened a windows command prompt and went to the corresponding directory, 
> then I typed "python setup.py install" this is supposed to install the 
> package? Because I only get a message from windows that states that to 
> install anything I must go to the control panel. Can you tell me where 
> is the mistake?

It looks like you are trying to build CGKit from source on a Windows 
machine. If you are on Windows you should download the binary installer 
(.exe) for the Python version you are using and run that. According to 
this page:
http://cgkit.sourceforge.net/doc2/externaldeps.html
if you use only the "general purpose modules" then the only external 
dependency is Python itself. So see if you can get that much working first.

Kent

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


Re: [Tutor] what is "user time" in os.times()?

2006-10-03 Thread Michael P. Reilly
Dang; sorry, thought I hit "Reply to all".I ran your program on two separate systems, three versions of Python here at work:1. Pent M 2GHz/Windows XP Pro, running Cygwin Python 2.4.3:
 flips   heads  tails diff   %100 499598 500402 804 99.9196 per centTime was 2.863 secondsos.times() is (2.5778, 0.156, 0.0, 0.0, 14526.053)2. AMD Opteron/Linux RHE 4, running Python 
2.2.3: flips   heads  tails diff   %100 499111 500889 1778 99.8222 per centTime was 6.972 secondsos.times() is (6.9698, 0.02, 0.0, 0.0, 3496342.93
)3. Pent M 2GHz/Windows XP Pro, running Windows Python 2.4.3: flips   heads  tails diff   %100 499667 500333 666 99.9334 per centTime was 2.172
 secondsos.times() is (0.265625, 2.125, 0.0, 0.0, 0.0)The second machine is a workhorse doing a lot, and it is also a virtual machine.  The first and third is my work laptop - notice that the difference between cygwin (Posix/UNIX) and Windows is how the user/system time is computed.  And also how the real time is returned.
The Windows APIs might swap values or might just handle things differently.  I can't say without a lot more research.  -ArcegeOn 10/3/06, 
Dick Moores <[EMAIL PROTECTED]> wrote:At 05:24 AM 10/3/2006, Michael P. Reilly wrote:
>It is not really a "ghost".  On most systems, there is a split>between what happens while you are a "user" and what happens deep>inside the operation "system".  The function is showing you how much
>time elapsed while in those two areas of the system.>>To show you what I mean, take a simple program that writes to the disk:>>def show_me_the_spam(breakfast):> file = open("
spam.txt", "w")> for i in range(10:> print >>file, "spam, spam, %s and spam" % breakfast> file.close()>show_me_the_spam("eggs")(
0.25, 0.15625, 0.0, 0.0, 0.0)  run included creating spam.txt(0.171875, 0.09375, 0.0, 0.0, 0.0) after spam.txt already thereThe (user time)/(system time) ratios are 1.6 and 1.83, respectively.For my coinFlip.py
 I got(0.3125, 4.015625, 0.0, 0.0, 0.0)the (user time)/(system time) ratio was 0.079. Why the bigdifference? From your explanation I would have thought that yourscript would have a LOWER ratio than coinFlip.py
 because of the fileoperations.Here's coinFlips.py and its result again:==# coinFlip.pyimport time, osfrom random import choiceheads, tails = 0, 0
flips = 100timeStart = time.time()for x in xrange(flips): coin = choice(["heads", "tails"]) if coin == "heads": heads += 1 else:
 tails += 1timeEnd = time.time()osTimes = os.timesdifference = abs(heads - tails)print " flips   heads  tails diff   %"print flips, heads, tails, difference, 100 -
(100*difference*1.0/flips), "per cent"print "Time was %.4g seconds" % (timeEnd - timeStart)print "os.times() is", osTimes()==
  >>>Evaluating 1coinFlip.py   flips   heads  tails diff   %100 500902 499098 1804 99.8196 per centTime was 4.156 secondsos.times() is (0.3125, 4.015625, 0.0, 0.0, 0.0)  >>>
If I reduce flips from 100 to 1000, I get >>>Evaluating 1coinFlip.py  flips   heads  tails diff   %1000 482 518 36 96.4 per centTime was 0.016 secondsos.times() is (0.296875
, 0.09375, 0.0, 0.0, 0.0) >>>with a ratio of 3.17. So what's happening during the 1,000,000 loopthat's put into "system" space?>Most of the execution of the program would happen as the "user";
>things like calling functions, evaluating string parameters,>counting in the loop.>>But the parts that would need to talk to hardware, parts that might>be dangerous to have the program have direct control over or might
>need to be shared with other programs at the same time (like a>printer or a keyboard or a disk drive) - these parts of the program>are hidden underneath function calls and statements like "open" and
>"print" and put into a "system" space.>>How much time you spend in "system" and "user" space tells you how>CPU and resource intensive your program is.
>>The 3rd and 4th values are the same, but cumulative for all that>programs "children".  A program can spawn subprocesses to help it>(see os.system and the like).>>All the above times are CPU times - time spent while the program has
>been using fractions of CPU seconds.  The last value shows that>actual real duration of the program, similar to your (timeEnd ->timeStart), but a bit more accurate.  (Though it appears that value
>isn't supported on your system?)My system isa Dell  Dimension 4600i  running Windows XP Professional Service Pack2 (build 2600).Why wouldn't that value be supported? Should I call Dell? Bang on
MS's door? (I live about a mile away.)  I feel a bit cheated. ;)Thanks,  Michael, for your extended answer..Dick___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor-- There's so many different worlds,
So many different suns.And we have just one world,Bu

Re: [Tutor] CGKit

2006-10-03 Thread Luke Paireepinart

> To install Py++ I downloaded it, and extracted it, next I 
> opened a windows command prompt and went to the corresponding directory, 
> then I typed "python setup.py install" this is supposed to install the 
> package? Because I only get a message from windows that states that to 
> install anything I must go to the control panel. 
It would be helpful if you included the actual error message here.
> Can you tell me where 
> is the mistake?
>   
Maybe python isn't in your path.
try this:

path = %path%;c:/python24
python setup.py install
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 07:08 AM 10/3/2006, Kent Johnson wrote:
>Dick Moores wrote:
> > At 05:54 AM 10/3/2006, Kent Johnson wrote:
> >> Dick Moores wrote:
> >>> Very interesting. I thought a line of that template looked
> >>> familiar. I was seeing "_t0 = _timer()" regularly when I had the -s
> >>> option set without any setup:
> >>> C:\>python -m timeit  -r 3 -s"for x in range(1):" " x*x"
> >>> Traceback (most recent call last):
> >>>File "E:\Python25\lib\runpy.py", line 95, in run_module
> >>>  filename, loader, alter_sys)
> >>>File "E:\Python25\lib\runpy.py", line 52, in _run_module_co
> >>>  mod_name, mod_fname, mod_loader)
> >>>File "E:\Python25\lib\runpy.py", line 32, in _run_code
> >>>  exec code in run_globals
> >>>File "E:\Python25\lib\timeit.py", line 285, in 
> >>>  sys.exit(main())
> >>>File "E:\Python25\lib\timeit.py", line 249, in main
> >>>  t = Timer(stmt, setup, timer)
> >>>File "E:\Python25\lib\timeit.py", line 116, in __init__
> >>>  code = compile(src, dummy_src_name, "exec")
> >>>File "", line 4
> >>>  _t0 = _timer()
> >>>^
> >>> But I don't understand what the error has to do with _t0 . That's
> >>> OK, don't bother to explain. I don't understand classes yet anyway.
> >>> Should get into them soon, with Wes Chun's book.
> >> If you substitute your code into the template by hand and look at
> >> the actual exception (not shown above) you should see the problem.
> >> It doesn't have anything to do with classes.
> >
> > I meant timeit.py has classes.
> >
> > OK, I called timeit.py with this template:
> >
> > template = """
> > def inner(_it, _timer):
> >  %(setup)s
> >  _t0 = _timer()
> >  for _i in _it:
> >  %("x=0" "while x<100": " x*x")s
> >  _t1 = _timer()
> >  return _t1 - _t0
> > """
>
>OK, let's go back to your first example and I will explain in more detail.

Sorry to be so dumb.


>timeit.py contains this template:
>template = """
>def inner(_it, _timer):
>  %(setup)s
>  _t0 = _timer()
>  for _i in _it:
>  %(stmt)s
>  _t1 = _timer()
>  return _t1 - _t0
>"""
>
>Whatever you specify for setup code is substituted for %(setup)s; the
>timed statement is substituted for %(stmt)s. This is done using standard
>string formatting operations. The result of the substitutions is a
>function definition which is compiled and run.
>
>So if you run
>python -m timeit -s"for x in range(1):" " x*x"
>
>the generated function looks like this:
>def inner(_it, _timer):
>  for x in range(1):
>  _t0 = _timer()
>  for _i in _it:
>   x*x
>  _t1 = _timer()
>  return _t1 - _t0
>
>The for statement should begin an indented block; the next statement is
>*not* indented, so you get an IndentationError.

Yes, that's exactly what I got when using timeit with that code at 
the command line, which I didn't understand.  John Fouhy explained it.

>Does that help?

Yes, you've shown me a couple of things. How to substitute in a 
template. How it can be useful to look at the code of modules. And in 
this case I'll be able to use that template in a copy of timeit.py 
without going to the command line, with which I usually struggle.

Thanks again, again.

Dick

>Kent
>
>___
>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] what is "user time" in os.times()?

2006-10-03 Thread Dick Moores
At 05:24 AM 10/3/2006, Michael P. Reilly wrote:
>It is not really a "ghost".  On most systems, there is a split 
>between what happens while you are a "user" and what happens deep 
>inside the operation "system".  The function is showing you how much 
>time elapsed while in those two areas of the system.
>
>To show you what I mean, take a simple program that writes to the disk:
>
>def show_me_the_spam(breakfast):
> file = open("spam.txt", "w")
> for i in range(10:
> print >>file, "spam, spam, %s and spam" % breakfast
> file.close()
>show_me_the_spam("eggs")

(0.25, 0.15625, 0.0, 0.0, 0.0)  run included creating spam.txt
(0.171875, 0.09375, 0.0, 0.0, 0.0) after spam.txt already there

The (user time)/(system time) ratios are 1.6 and 1.83, respectively.

For my coinFlip.py I got
(0.3125, 4.015625, 0.0, 0.0, 0.0)

the (user time)/(system time) ratio was 0.079. Why the big 
difference? From your explanation I would have thought that your 
script would have a LOWER ratio than coinFlip.py because of the file 
operations.

Here's coinFlips.py and its result again:

==
# coinFlip.py

import time, os
from random import choice

heads, tails = 0, 0
flips = 100

timeStart = time.time()
for x in xrange(flips):
 coin = choice(["heads", "tails"])
 if coin == "heads":
 heads += 1
 else:
 tails += 1

timeEnd = time.time()
osTimes = os.times
difference = abs(heads - tails)
print " flips   heads  tails diff   %"
print flips, heads, tails, difference, 100 -
(100*difference*1.0/flips), "per cent"
print "Time was %.4g seconds" % (timeEnd - timeStart)
print "os.times() is", osTimes()
==

  >>>
Evaluating 1coinFlip.py
   flips   heads  tails diff   %
100 500902 499098 1804 99.8196 per cent
Time was 4.156 seconds
os.times() is (0.3125, 4.015625, 0.0, 0.0, 0.0)
  >>>


If I reduce flips from 100 to 1000, I get
 >>>
Evaluating 1coinFlip.py
  flips   heads  tails diff   %
1000 482 518 36 96.4 per cent
Time was 0.016 seconds
os.times() is (0.296875, 0.09375, 0.0, 0.0, 0.0)
 >>>

with a ratio of 3.17. So what's happening during the 1,000,000 loop 
that's put into "system" space?

>Most of the execution of the program would happen as the "user"; 
>things like calling functions, evaluating string parameters, 
>counting in the loop.
>
>But the parts that would need to talk to hardware, parts that might 
>be dangerous to have the program have direct control over or might 
>need to be shared with other programs at the same time (like a 
>printer or a keyboard or a disk drive) - these parts of the program 
>are hidden underneath function calls and statements like "open" and 
>"print" and put into a "system" space.
>
>How much time you spend in "system" and "user" space tells you how 
>CPU and resource intensive your program is.
>
>The 3rd and 4th values are the same, but cumulative for all that 
>programs "children".  A program can spawn subprocesses to help it 
>(see os.system and the like).
>
>All the above times are CPU times - time spent while the program has 
>been using fractions of CPU seconds.  The last value shows that 
>actual real duration of the program, similar to your (timeEnd - 
>timeStart), but a bit more accurate.  (Though it appears that value 
>isn't supported on your system?)

My system is
a Dell  Dimension 4600i  running Windows XP Professional Service Pack 
2 (build 2600).

Why wouldn't that value be supported? Should I call Dell? Bang on 
MS's door? (I live about a mile away.)  I feel a bit cheated. ;)

Thanks,  Michael, for your extended answer..

Dick




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


[Tutor] CGKit

2006-10-03 Thread Carlos
Hi,

I'm new to python and have been trying to install CGKit, it has a bunch 
of dependencies that I have to install. /At this point /the problem that 
I have is that I need to install Py++, I already installed pygccxml and 
GCC-XML. To install Py++ I downloaded it, and extracted it, next I 
opened a windows command prompt and went to the corresponding directory, 
then I typed "python setup.py install" this is supposed to install the 
package? Because I only get a message from windows that states that to 
install anything I must go to the control panel. Can you tell me where 
is the mistake?

By the way, has someone managed to install CGKit and can guide me in the 
process? I'm having a lot of trouble with it.

This is the page, just in case http://cgkit.sourceforge.net

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> At 05:54 AM 10/3/2006, Kent Johnson wrote:
>> Dick Moores wrote:
>>> Very interesting. I thought a line of that template looked 
>>> familiar. I was seeing "_t0 = _timer()" regularly when I had the -s 
>>> option set without any setup:
>>> C:\>python -m timeit  -r 3 -s"for x in range(1):" " x*x"
>>> Traceback (most recent call last):
>>>File "E:\Python25\lib\runpy.py", line 95, in run_module
>>>  filename, loader, alter_sys)
>>>File "E:\Python25\lib\runpy.py", line 52, in _run_module_co
>>>  mod_name, mod_fname, mod_loader)
>>>File "E:\Python25\lib\runpy.py", line 32, in _run_code
>>>  exec code in run_globals
>>>File "E:\Python25\lib\timeit.py", line 285, in 
>>>  sys.exit(main())
>>>File "E:\Python25\lib\timeit.py", line 249, in main
>>>  t = Timer(stmt, setup, timer)
>>>File "E:\Python25\lib\timeit.py", line 116, in __init__
>>>  code = compile(src, dummy_src_name, "exec")
>>>File "", line 4
>>>  _t0 = _timer()
>>>^
>>> But I don't understand what the error has to do with _t0 . That's 
>>> OK, don't bother to explain. I don't understand classes yet anyway. 
>>> Should get into them soon, with Wes Chun's book.
>> If you substitute your code into the template by hand and look at 
>> the actual exception (not shown above) you should see the problem. 
>> It doesn't have anything to do with classes.
> 
> I meant timeit.py has classes.
> 
> OK, I called timeit.py with this template:
> 
> template = """
> def inner(_it, _timer):
>  %(setup)s
>  _t0 = _timer()
>  for _i in _it:
>  %("x=0" "while x<100": " x*x")s
>  _t1 = _timer()
>  return _t1 - _t0
> """

OK, let's go back to your first example and I will explain in more detail.

timeit.py contains this template:
template = """
def inner(_it, _timer):
 %(setup)s
 _t0 = _timer()
 for _i in _it:
 %(stmt)s
 _t1 = _timer()
 return _t1 - _t0
"""

Whatever you specify for setup code is substituted for %(setup)s; the 
timed statement is substituted for %(stmt)s. This is done using standard 
string formatting operations. The result of the substitutions is a 
function definition which is compiled and run.

So if you run
python -m timeit -s"for x in range(1):" " x*x"

the generated function looks like this:
def inner(_it, _timer):
 for x in range(1):
 _t0 = _timer()
 for _i in _it:
  x*x
 _t1 = _timer()
 return _t1 - _t0

The for statement should begin an indented block; the next statement is 
*not* indented, so you get an IndentationError.

Does that help?
Kent

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 05:54 AM 10/3/2006, Kent Johnson wrote:
>Dick Moores wrote:
>>Very interesting. I thought a line of that template looked 
>>familiar. I was seeing "_t0 = _timer()" regularly when I had the -s 
>>option set without any setup:
>>C:\>python -m timeit  -r 3 -s"for x in range(1):" " x*x"
>>Traceback (most recent call last):
>>File "E:\Python25\lib\runpy.py", line 95, in run_module
>>  filename, loader, alter_sys)
>>File "E:\Python25\lib\runpy.py", line 52, in _run_module_co
>>  mod_name, mod_fname, mod_loader)
>>File "E:\Python25\lib\runpy.py", line 32, in _run_code
>>  exec code in run_globals
>>File "E:\Python25\lib\timeit.py", line 285, in 
>>  sys.exit(main())
>>File "E:\Python25\lib\timeit.py", line 249, in main
>>  t = Timer(stmt, setup, timer)
>>File "E:\Python25\lib\timeit.py", line 116, in __init__
>>  code = compile(src, dummy_src_name, "exec")
>>File "", line 4
>>  _t0 = _timer()
>>^
>>But I don't understand what the error has to do with _t0 . That's 
>>OK, don't bother to explain. I don't understand classes yet anyway. 
>>Should get into them soon, with Wes Chun's book.
>
>If you substitute your code into the template by hand and look at 
>the actual exception (not shown above) you should see the problem. 
>It doesn't have anything to do with classes.

I meant timeit.py has classes.

OK, I called timeit.py with this template:

template = """
def inner(_it, _timer):
 %(setup)s
 _t0 = _timer()
 for _i in _it:
 %("x=0" "while x<100": " x*x")s
 _t1 = _timer()
 return _t1 - _t0
"""
And got this error:

E:\Python25\Scripts-NotMine>timeit.py
Traceback (most recent call last):
   File "E:\Python25\Scripts-NotMine\timeit.py", li
 sys.exit(main())
   File "E:\Python25\Scripts-NotMine\timeit.py", li
 t = Timer(stmt, setup, timer)
   File "E:\Python25\Scripts-NotMine\timeit.py", li
 src = template % {'stmt': stmt, 'setup': setup
KeyError: '"x=0" "while x<100": " x*x"'

So what's the error? I've probably misunderstood how to do the substitution..

Dick



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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> Very interesting. I thought a line of that template looked familiar. 
> I was seeing "_t0 = _timer()" regularly when I had the -s option set 
> without any setup:
> 
> C:\>python -m timeit  -r 3 -s"for x in range(1):" " x*x"
> Traceback (most recent call last):
>File "E:\Python25\lib\runpy.py", line 95, in run_module
>  filename, loader, alter_sys)
>File "E:\Python25\lib\runpy.py", line 52, in _run_module_co
>  mod_name, mod_fname, mod_loader)
>File "E:\Python25\lib\runpy.py", line 32, in _run_code
>  exec code in run_globals
>File "E:\Python25\lib\timeit.py", line 285, in 
>  sys.exit(main())
>File "E:\Python25\lib\timeit.py", line 249, in main
>  t = Timer(stmt, setup, timer)
>File "E:\Python25\lib\timeit.py", line 116, in __init__
>  code = compile(src, dummy_src_name, "exec")
>File "", line 4
>  _t0 = _timer()
>^
> But I don't understand what the error has to do with _t0 . That's OK, 
> don't bother to explain. I don't understand classes yet anyway. 
> Should get into them soon, with Wes Chun's book.

If you substitute your code into the template by hand and look at the 
actual exception (not shown above) you should see the problem. It 
doesn't have anything to do with classes.

Kent

Kent

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 04:35 AM 10/3/2006, Kent Johnson wrote:
>Dick Moores wrote:
> > At 03:05 AM 10/3/2006, Kent Johnson wrote:
> >> timeit runs the setup code once, then runs the timed code many times
> >> with the timer running. If "x=0" is outside the loop, then the while
> >> loop only runs once, because x == 100 after the first time through the
> >> loop. So your first version is effectively timing this:
> >>
> >> setup:
> >> x=100
> >>
> >> timed code:
> >> while x<100:
> >>x+=1
> >>
> >> which is of course a lot faster than actually running the loopp 100 times.
> >
> > Thanks, Kent. I was beginning to understand this, and now you've
> > nailed it down for me.
>
>You might want to look at the source, timeit.py. There is a code
>template (called 'template') near the beginning. Your setup and timed
>code are inserted into the template, then it is compiled and run.

Very interesting. I thought a line of that template looked familiar. 
I was seeing "_t0 = _timer()" regularly when I had the -s option set 
without any setup:

C:\>python -m timeit  -r 3 -s"for x in range(1):" " x*x"
Traceback (most recent call last):
   File "E:\Python25\lib\runpy.py", line 95, in run_module
 filename, loader, alter_sys)
   File "E:\Python25\lib\runpy.py", line 52, in _run_module_co
 mod_name, mod_fname, mod_loader)
   File "E:\Python25\lib\runpy.py", line 32, in _run_code
 exec code in run_globals
   File "E:\Python25\lib\timeit.py", line 285, in 
 sys.exit(main())
   File "E:\Python25\lib\timeit.py", line 249, in main
 t = Timer(stmt, setup, timer)
   File "E:\Python25\lib\timeit.py", line 116, in __init__
 code = compile(src, dummy_src_name, "exec")
   File "", line 4
 _t0 = _timer()
   ^
But I don't understand what the error has to do with _t0 . That's OK, 
don't bother to explain. I don't understand classes yet anyway. 
Should get into them soon, with Wes Chun's book.

>One of the dangers of timeit is that you may time something different
>than what you think you are timing, as you did. It's an easy mistake to
>make and hard to protect against.

Yes. thanks, Kent.

Dick




>Kent
>
>___
>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] Is there Python code for accessing an object's reference count?

2006-10-03 Thread Dick Moores
At 04:44 AM 10/3/2006, Kent Johnson wrote:
>Dick Moores wrote:
> > At 03:25 AM 10/3/2006, Andre Roberge wrote:
> >> On 10/3/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> >>> Is there Python code for accessing an object's reference count?
> >> See sys.getrefcount.
> >
> > Fascinating. And surprising.
> >  >>> from sys import getrefcount
> >  >>> getrefcount(1)
> > 493
> >  >>> getrefcount(2)
> > 157
> >  >>> getrefcount(3)
> > 60
> >  >>> getrefcount(22)
> > 12
> >  >>> getrefcount(222)
> > 3
> >  >>> getrefcount(34523452345345)
> > 2
> >  >>> getrefcount(2.23452345345)
> > 2
> >  >>>
> >
> > That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed
> > python, called python,  and tried it again. Essentially no change.
> > When would this go to zero? Only when I shut off my computer?
>
>When you exit Python.
>
>There are quite a few modules loaded automatically when Python starts
>up. It's not too surprising that a few of them reference 1. For example,
>defining a function that uses a constant creates a reference to the
>constant:
>In [2]: sys.getrefcount(100)
>Out[2]: 43
>
>In [3]: def foo():
> ...: return 100
> ...:
>
>In [4]: sys.getrefcount(100)
>Out[4]: 44

Ah, got it. Thanks again, Kent.

Dick


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


Re: [Tutor] Is there Python code for accessing an object's reference count?

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> At 03:25 AM 10/3/2006, Andre Roberge wrote:
>> On 10/3/06, Dick Moores <[EMAIL PROTECTED]> wrote:
>>> Is there Python code for accessing an object's reference count?
>> See sys.getrefcount.
> 
> Fascinating. And surprising.
>  >>> from sys import getrefcount
>  >>> getrefcount(1)
> 493
>  >>> getrefcount(2)
> 157
>  >>> getrefcount(3)
> 60
>  >>> getrefcount(22)
> 12
>  >>> getrefcount(222)
> 3
>  >>> getrefcount(34523452345345)
> 2
>  >>> getrefcount(2.23452345345)
> 2
>  >>>
> 
> That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed 
> python, called python,  and tried it again. Essentially no change. 
> When would this go to zero? Only when I shut off my computer?

When you exit Python.

There are quite a few modules loaded automatically when Python starts 
up. It's not too surprising that a few of them reference 1. For example, 
defining a function that uses a constant creates a reference to the 
constant:
In [2]: sys.getrefcount(100)
Out[2]: 43

In [3]: def foo():
...: return 100
...:

In [4]: sys.getrefcount(100)
Out[4]: 44

Kent

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> At 03:05 AM 10/3/2006, Kent Johnson wrote:
>> timeit runs the setup code once, then runs the timed code many times
>> with the timer running. If "x=0" is outside the loop, then the while
>> loop only runs once, because x == 100 after the first time through the
>> loop. So your first version is effectively timing this:
>>
>> setup:
>> x=100
>>
>> timed code:
>> while x<100:
>>x+=1
>>
>> which is of course a lot faster than actually running the loopp 100 times.
> 
> Thanks, Kent. I was beginning to understand this, and now you've 
> nailed it down for me.

You might want to look at the source, timeit.py. There is a code 
template (called 'template') near the beginning. Your setup and timed 
code are inserted into the template, then it is compiled and run.

One of the dangers of timeit is that you may time something different 
than what you think you are timing, as you did. It's an easy mistake to 
make and hard to protect against.

Kent

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 03:05 AM 10/3/2006, Kent Johnson wrote:
>Dick Moores wrote:
> > I DID have setup code, the "x=0".  I now notice that if the "x=0" is
> > not stated as the setup code, the time difference is enormous,
> > 132-to-1 in this case.
> >
> > python -m timeit  -s"x=0" "while x<100:" "  x+=1"
> > 1000 loops, best of 3: 0.116 usec per loop
> >
> > python -m timeit "x=0" "while x<100:" "  x+=1"
> > 10 loops, best of 3: 15.3 usec per loop
>
>timeit runs the setup code once, then runs the timed code many times
>with the timer running. If "x=0" is outside the loop, then the while
>loop only runs once, because x == 100 after the first time through the
>loop. So your first version is effectively timing this:
>
>setup:
>x=100
>
>timed code:
>while x<100:
>x+=1
>
>which is of course a lot faster than actually running the loopp 100 times.

Thanks, Kent. I was beginning to understand this, and now you've 
nailed it down for me.

Dick



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


Re: [Tutor] Is there Python code for accessing an object's reference count?

2006-10-03 Thread Dick Moores
At 03:25 AM 10/3/2006, Andre Roberge wrote:
>On 10/3/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> > Is there Python code for accessing an object's reference count?
>
>See sys.getrefcount.

Fascinating. And surprising.
 >>> from sys import getrefcount
 >>> getrefcount(1)
493
 >>> getrefcount(2)
157
 >>> getrefcount(3)
60
 >>> getrefcount(22)
12
 >>> getrefcount(222)
3
 >>> getrefcount(34523452345345)
2
 >>> getrefcount(2.23452345345)
2
 >>>

That refcount for 1 (and 2, 3, and 12) is puzzling to me. I closed 
python, called python,  and tried it again. Essentially no change. 
When would this go to zero? Only when I shut off my computer?

I just answered that last question by shutting off my computer, and found
 >>> from sys import getrefcount
 >>> getrefcount(1)
493
 >>> q = 1
 >>> getrefcount(1)
499
 >>> w = 1
 >>> getrefcount(1)
500
 >>> del w
 >>> getrefcount(1)
499
 >>> q = 2
 >>> getrefcount(1)
498
 >>>

Still 493. Why?

Then after "q = 1", 499, not 494. Why?

The other refcounts, after w=1, del w, and q=2, are as I expected.

Thanks,

Dick




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


Re: [Tutor] Is there Python code for accessing an object's reference count?

2006-10-03 Thread Andre Roberge
On 10/3/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> Is there Python code for accessing an object's reference count?

See sys.getrefcount.

André Roberge
>
> And does it matter to Python what this count is, other than whether
> it is zero or greater than zero (for garbage collection)?
>
> Thanks,
>
> Dick Moores
>
> ___
> 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] Is there Python code for accessing an object's reference count?

2006-10-03 Thread Dick Moores
Is there Python code for accessing an object's reference count?

And does it matter to Python what this count is, other than whether 
it is zero or greater than zero (for garbage collection)?

Thanks,

Dick Moores

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


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
> I DID have setup code, the "x=0".  I now notice that if the "x=0" is 
> not stated as the setup code, the time difference is enormous, 
> 132-to-1 in this case.
> 
> python -m timeit  -s"x=0" "while x<100:" "  x+=1"
> 1000 loops, best of 3: 0.116 usec per loop
> 
> python -m timeit "x=0" "while x<100:" "  x+=1"
> 10 loops, best of 3: 15.3 usec per loop

timeit runs the setup code once, then runs the timed code many times 
with the timer running. If "x=0" is outside the loop, then the while 
loop only runs once, because x == 100 after the first time through the 
loop. So your first version is effectively timing this:

setup:
x=100

timed code:
while x<100:
   x+=1

which is of course a lot faster than actually running the loopp 100 times.

Kent

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


[Tutor] what is "user time" in os.times()?

2006-10-03 Thread Dick Moores
In a recent(ongoing?) thread on python-list, "PATCH: Speed up direct 
string concatenation by 20+%!", Larry Hastings, the op, says
he computed his benchmark times using 
"sum(os.times()[:2])".  Curious, I looked up os.times and tried it 
out on a little script I wrote to demonstrate the Law of Large 
Numbers (to myself):

==
# coinFlip.py

import time, os
from random import choice

heads, tails = 0, 0
flips = 100

timeStart = time.time()
for x in xrange(flips):
coin = choice(["heads", "tails"])
if coin == "heads":
heads += 1
else:
tails += 1

timeEnd = time.time()
osTimes = os.times
difference = abs(heads - tails)
print " flips   heads  tails diff   %"
print flips, heads, tails, difference, 100 - 
(100*difference*1.0/flips), "per cent"
print "Time was %.4g seconds" % (timeEnd - timeStart)
print "os.times() is", osTimes()
==

 >>>
Evaluating 1coinFlip.py
  flips   heads  tails diff   %
100 500902 499098 1804 99.8196 per cent
Time was 4.156 seconds
os.times() is (0.3125, 4.015625, 0.0, 0.0, 0.0)
 >>>

 From the os module doc for times():

"Return a 5-tuple of floating point numbers indicating accumulated 
(processor or other) times, in seconds. The items are: user time, 
system time, children's user time, children's system time, and 
elapsed real time since a fixed point in the past, in that order. See 
the Unix manual page times(2) or the corresponding Windows Platform 
API documentation. Availability: Macintosh, Unix, Windows."

Now, I won't ask (yet) about what the 3rd, 4th, and 5th elements of 
the 5-tuple are, but I'd like to know what the first one, user time, 
is. Who is this ghost in the machine?

The man page times(2) was no help, to me anyway. 
()

Dick Moores






http://www.bigbiz.com/cgi-bin/manpage?times

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