[Tutor] Way to search this mailing list?

2006-10-10 Thread Chris Hengge
Just a simple question... Am I missing some great feature? or is there
no way to search rather then going through the archives by hand?

Thanks.

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


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Tim Peters
[Dick Moores]
>>> Would this be a better random source than choice([0,1]), which uses
>>> random()?

[Tim Peters]
>> "Better" as measured against what criteria?

[Dick]
> I meant would it be closer to true randomness than random(), even if
> much slower?

Define "closer to true randomness" ;-)

I don't mean to be difficult, but if the phrase "cryptographically
strong" doesn't mean something vital to you already, there's little
for you to worry about here.  If you want /true/ randomness, you can
buy a certified hardware random number generator, based on
non-deterministic physical processes (like timing radioactive decay,
or measuring thermal noise).

Anything short of that is more-or-less a hack designed to pass various
tests for randomness.  The Mersenne Twister is one of the best-tested
generators in existence, producing a sequence indistinguishable from
"truly random" via all tests in common use.  Nevertheless, if an
intelligent /adversary/ knows you're using the Mersenne Twister, they
can exploit that under some conditions to predict the sequence you're
using.  OTOH, if you use a "cryptographically strong" generator, or a
hardware source of true randomness, and your adversary knows that,
current theory says that even if have they have enormous computer
resources to throw at it, and you don't make "stupid mistakes" in the
/way/ you use it, they won't be able to predict the sequence you're
seeing at significantly better than chance rate.

The /pragmatic/ answer to "would it be closer to true randomness?"
thus depends on what you're trying to achieve.  The theoretical answer
is "yes", but that may be of no relevance to what you're trying to
achieve.

>>  It's very much slower than using choice([0, 1])  (or choice("01"), or
>> randrange(2), or ...), and can't (even in theory) be reproduced by setting
>> a seed.

> That's fine. Why would one want to?

For example, any non-trivial program eventually grows a test suite to
verify that the program continues to run correctly as time goes on.
Testing a program that makes random decisions is, in general, very
much easier if you have a way to force it to make the same decisions
each time the test is run.  For example, here's a tiny piece of
Python's Lib/test/test_random.py:

def test_genrandbits(self):
# Verify cross-platform repeatability
self.gen.seed(1234567)
self.assertEqual(self.gen.getrandbits(100),
 97904845777343510404718956115L)

Without the ability to force the seed, that test wouldn't be possible.
 As is, it tests that given the specific seed 1234567, the next call
to getrandbits(100) will return exactly 97904845777343510404718956115
regardless of platform.

Even if there is no test, non-trivial programs do "surprising" things
at times.  In a program that makes random decisions, if something
/very/ surprising is seen, and you have no way to reproduce the
decisions made before the surprising occurrence, you may be completely
stuck in debugging it.  OTOH, if there's a way to force the same
decisions to be made, you can, e.g., run the program under a debugger
to see everything that happens before disaster strikes.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread Alan Gauld
> yeh, I am using IDLE.
> why, is it a problem??

IDLE is a Tkinter program.
So you are trying to run a Tkinter program inside a Tkinter program
which can confuse things. The latest versions of IDLE are a lot better
but I stilll strongly recommend running any Tkinter , indeed any
GUI) programme, from the OS prompt - or by double clicking in
Windows exploder

Alan G.

> On 10/10/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
>>
>> Asrarahmed Kadri wrote:
>> >
>> >
>> > Folks,
>> >
>> > Just started to get my feet wet with basics of Tkinter 
>> > Programming.
>> > Here is a code which is giving me some problem; when I run it, it
>> > displays me the button widget, but when I click it, the window 
>> > doesn't
>> > goes away. the program gets stuck up. Can anyone help me to 
>> > resolve
>> > this issue??
>> >
>> > *import sys
>> > from Tkinter  import **
>> >
>> > *root = Tk()
>> > widget = Button(root, text='Hello widget world', 
>> > command=root.quit)
>> > widget.pack( )
>> > widget.mainloop( )*
>> >
>> shouldn't you mainloop your root?
>> also, are you using IDLE?
>> > Thanks.
>> >
>> > Regards,
>> > Asrar
>> >
>> >
>> > --
>> > To HIM you shall return.
>> > 
>> >
>> > ___
>> > Tutor maillist  -  Tutor@python.org
>> > http://mail.python.org/mailman/listinfo/tutor
>> >
>>
>>
>
>
> -- 
> To HIM you shall return.
>





> ___
> 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] Tip: Firefox quicksearch for module docs

2006-10-10 Thread John Fouhy
For all you firefox users out there, create a bookmark in your "Quick
searches" bookmark folder with the keyword 'python' and the location
'http://www.python.org/doc/current/lib/module-%s.html'.

Then, you can just type (for example): "python random" into the search
bar, and go straight to
http://www.python.org/doc/current/lib/module-random.html.  Nifty :-)

(more quick searches here:
http://wormus.com/leakytap/Internet/CustomKeywordspython )

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


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread Asrarahmed Kadri
The error message is as under:
 

Microsoft Visual C++ Runtime Library
 Runtime Error! Program: C:\python\Lib\site-packages\pythonwin\Pythonwin.exe
 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more inforamtion.Any idea??? what is this and how can it be fixed???
 
On 10/10/06, John Fouhy <[EMAIL PROTECTED]> wrote:
On 11/10/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
> shouldn't you mainloop your root?By convention, you should, but you don't have to.  mainloop ismainloop; calling it on root just makes it easy to find.--John.___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread John Fouhy
On 11/10/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
> shouldn't you mainloop your root?

By convention, you should, but you don't have to.  mainloop is
mainloop; calling it on root just makes it easy to find.

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


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Alan Gauld
> I'm thinking that just for the hell of it I could use urandom() as a
> source of random decimal digits. Or in a coin tossing program. 
> Here's
> a list of 7817 '1's and 0's generated by urandom():
>
> >>> from os import urandom
> >>> lst = list(urandom(100))
> >>> tosses = [y for y in lst if y in '01']
> >>> len(tosses)
> 7817
> >>>
>
> Would this be a better random source than choice([0,1]), which uses 
> random()?

It depends on what you mean by better.
Its not really any more random if thats what you mean,
it might be faster but I doubt if by much. I assume that
choice simply divides random by the number of options
and selects by index. In pseudo code:

def choice(seq):
ran = random()  # number between 0,1
num = 1/len(seq)  # 4 numbers means quarter interval?
for n in range(1,len(seq)+1):
if ran < num * n: break
return seq[n-1]

That probably has some boundary errors but I'd guess its how it works,
and if written in C is probably faster than generating a big list then
extracting the 1s and zeros into another list.

But in matters of timing its better to test than guess... :-)

Alan G. 


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


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread Asrarahmed Kadri
yeh, I am using IDLE.
why, is it a problem?? 
On 10/10/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
Asrarahmed Kadri wrote:>>> Folks,>> Just started to get my feet wet with basics of Tkinter Programming.
> Here is a code which is giving me some problem; when I run it, it> displays me the button widget, but when I click it, the window doesn't> goes away. the program gets stuck up. Can anyone help me to resolve
> this issue??>> *import sys> from Tkinter  import **>> *root = Tk()> widget = Button(root, text='Hello widget world', command=root.quit)> widget.pack( )> widget.mainloop
( )*>shouldn't you mainloop your root?also, are you using IDLE?> Thanks.>> Regards,> Asrar>>> --> To HIM you shall return.> 
>> ___> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor
>-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread Asrarahmed Kadri
i changed the code from widget.mainloop() to root.mainloop(); but still its not working
 
 
On 10/10/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote:

yeh, I am using IDLE.
why, is it a problem?? 

On 10/10/06, Luke Paireepinart <[EMAIL PROTECTED]
> wrote: 
Asrarahmed Kadri wrote:>>> Folks,>> Just started to get my feet wet with basics of Tkinter Programming. 
> Here is a code which is giving me some problem; when I run it, it> displays me the button widget, but when I click it, the window doesn't> goes away. the program gets stuck up. Can anyone help me to resolve 
> this issue??>> *import sys> from Tkinter  import **>> *root = Tk()> widget = Button(root, text='Hello widget world', command=root.quit)> widget.pack( )> widget.mainloop
 ( )*>shouldn't you mainloop your root?also, are you using IDLE?> Thanks.>> Regards,> Asrar>>> --> To HIM you shall return.>  
>> ___> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor >
-- To HIM you shall return. -- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with this Tkinter code

2006-10-10 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
>  
>  
> Folks,
>  
> Just started to get my feet wet with basics of Tkinter Programming. 
> Here is a code which is giving me some problem; when I run it, it 
> displays me the button widget, but when I click it, the window doesn't 
> goes away. the program gets stuck up. Can anyone help me to resolve 
> this issue??
>
> *import sys
> from Tkinter  import **
>
> *root = Tk()
> widget = Button(root, text='Hello widget world', command=root.quit)
> widget.pack( )
> widget.mainloop( )*
>
shouldn't you mainloop your root?
also, are you using IDLE?
> Thanks.
>  
> Regards,
> Asrar
>  
>
> -- 
> To HIM you shall return.
> 
>
> ___
> 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] Help me with this Tkinter code

2006-10-10 Thread Asrarahmed Kadri
 
 
Folks,
 
Just started to get my feet wet with basics of Tkinter Programming. Here is a code which is giving me some problem; when I run it, it displays me the button widget, but when I click it, the window doesn't goes away. the program gets stuck up. Can anyone help me to resolve this issue??

import sysfrom Tkinter  import *
root = Tk()widget = Button(root, text='Hello widget world', command=root.quit)widget.pack( )widget.mainloop( )
Thanks.
 
Regards,
Asrar
 
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to read content in a tar file with tarfile module

2006-10-10 Thread Dave Kuhlman
On Tue, Oct 10, 2006 at 01:19:20PM -0600, Hugo Gonz?lez Monteverde wrote:
> Magnus Wirstr?m wrote:
> > I have written a app that makes a tar file and all works well... Now i 
> > want to expand that app so it can read read the tar and give me the 
> > contents of the tar file. How is the best way to do this ? I can't find 
> > a "listdir" like function in tarfile. Can anyone point me in the right 
> > direction?
> 
> Hi, is this what you're looking for?? (tarfile Object docs)
> 
> getmembers( )
> 
> Return the members of the archive as a list of TarInfo objects. The list 
> has the same order as the members in the archive.
> 
> getnames( )
> 
> Return the members as a list of their names. It has the same order as 
> the list returned by getmembers().
> 

Note also the presence of a "next()" method in TarFile objects. 
That (along with an "__iter__" method: see Lib/tarfile.py in the
source) is your clue that each TarFile instance is an iterator.  It
iterates over its TarInfo members.  So, you can do the following:

In [11]: f = tf.open('tmp.tar.gz', 'r:gz')
In [12]: for x in f:
   : print x.name

See:

http://docs.python.org/lib/module-tarfile.html
http://docs.python.org/lib/tarfile-objects.html
http://docs.python.org/lib/tar-examples.html

Note that the TarFile object implementation of the iterator
protocol is slightly non-standard.  next() returns None when
exhausted, whereas the iterator protocol specifies that it should
throw a StopIteration exception.  See:

http://docs.python.org/lib/typeiter.html

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] multithreading random()

2006-10-10 Thread John Fouhy
On 10/10/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> I can get what appears to be a random number precise(?) to 17 digits.
> How many base-10 digits would a "53-bit
> precision float" have, if converted to base 10?
>
>  >>> 2 ** 52
> 4503599627370496L
>  >>> 2**53
> 9007199254740992L
>  >>>
>
>  >>> len(str(2**53))-1
> 15
>
> So is the answer to my question something like len(str(2**53)) -1?
> (minus 1 because of the L in the long integer)

The string representation of longs doesn't include the 'L':

>>> str(999L)
'999'
>>> repr(999L)
'999L'

I think the answer is "approximately, yes".  But you have to be a bit
careful with talking about floating point numbers in terms of
decimals.  eg:

>>> 1.1
1.1001

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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Alan Gauld

"Dick Moores" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Encapsulate the while loop in a generator:
> def count(limit):
>   n=0
>   while n yield n
> n += 1
>
> All 3 are essentially the same, aren't they. Which makes me feel 
> even
> dumber, because I don't understand any of them. I've consulted 3
> books, and still don't understand the use of yield.

Think of yield as being the same as return except that next time
you "call the function" all the state is preserved and it picks up 
processing
after the yield.

So first time you call count above it returns 0
next time you call it it executes the n+= 1 and goes round the loop
again until it hits yield when it returns 1.
next time you call it executes y+=1 again, but because the state has
been remembered n goes to 2 and yield returns that
and so on until you reach n = limit at which point it just
exits with StopIteration.  Here is a short example:

>>> def y(n):
...   j = 0
...   while j < n:
... yield j
... j += 1
...
>>> try:
...x = y(7)
...for n in range(20):
...  print x.next()
... except StopIteration:
...   print 'Reached the end'
...
0
1
2
3
4
5
6
Reached the end
>>>

Does that help?


-- 
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] multithreading random()

2006-10-10 Thread John Fouhy
On 10/10/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> Didn't know about Google's code search,
> . What was your search string?

Search string was just "jumpahead lang:python".  Had to scroll past a
bunch of results for python/random.py before I found those, though.

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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Dick Moores
At 12:31 PM 10/10/2006, Marc Poulin wrote:

>--- Dick Moores <[EMAIL PROTECTED]> wrote:
> >
> > Andrei's
> > Write your own iterator:
> >  >>> def hugerange(minval, maxval):
> > ... val = minval
> > ... while val < maxval:
> > ... yield val
> > ... val += 1
> >
> > All 3 are essentially the same, aren't they. Which
> > makes me feel even
> > dumber, because I don't understand any of them. I've
> > consulted 3
> > books, and still don't understand the use of yield.
> >
>
>This is an example of a "coroutine".
>See http://en.wikipedia.org/wiki/Coroutine
>
>def hugerange(minval, maxval):
> val = minval
> while val < maxval:
> yield val
> ### return value of "val" to calling routine
> ### at this point and place
> ### hugerange() function temporarily
> ### on hold ...
> ###
> ### next call to hugerange()
> ### resumes at this point with
> ### all previous variable values
> ### still intact ...
> val += 1

Ah, it's beginning to make sense. However, it seems there's a need to 
do this because the while loop is inside a function (made generator). 
The while loop of mine in 
 is not in a 
function, so it seems I don't need a generator. Am I correct? I have 
a real interest in speed, however, and being forced to use the while 
loop for k larger than 2**31-1 really slows things down. It seems a 
while loop is about 20% slower than an equivalent for loop. Would 
using a generator be a help speedwise? I suspect it wouldn't. How about it?

Thanks very much, Marc.

Dick



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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Kent Johnson
Dick Moores wrote:
> At 11:34 AM 10/10/2006, Kent Johnson wrote:
>> Dick Moores wrote:
>>> Here are the suggestions I've received:
>> 
>>
>>> All 3 are essentially the same, aren't they. 
>>> Which makes me feel even dumber, because I 
>>> don't understand any of them. I've consulted 3 
>>> books, and still don't understand the use of yield.
>> Yes, they are pretty much the same. Mine is 
>> bare-bones, just doing what you specifically 
>> asked for. Danny's is a full replacement for xrange(); Andrei's is in 
>> between.
>>
>> Here is a good introduction to generators:
>> http://www.python.org/doc/2.2.3/whatsnew/node5.html
> 
> While I'm studying that, could you modify my 
> while loop so that it incorporates your 
> suggestion in a way that improves it? That would 
> be a big help to me in understanding generators 
> and the use of yield, I believe.

I'm not certain I would call it an improvement, but it would look just 
like your loop with xrange(). If the generator function is called 
count(), your loop would be
for i in count(2**100):  # ;)
   # process i

The big advantage of generators is they let you encapsulate the logic 
that generates a sequence. This makes it easy to reuse a sequence 
without having to duplicate the sequence generation logic in multiple 
loops. Sometimes this can make your loop clearer because it can focus on 
processing the sequence rather than generating it. Sometimes it can 
greatly simplify your logic. This happens when both the generation of a 
sequence and the processing of it involve maintaining state.

Here's a very artificial and possibly helpful example. Consider the sequence
1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ...

Imagine you want to do something with every third element of that 
sequence, for example sum them. Maybe it's not so hard to do this in a 
simple loop but my head starts to hurt thinking about it.

OK, so let's encapsulate the sequence into a generator:
In [13]: def multicount(limit):
: for i in range(1, limit+1):
: for j in range(1, i+1):
: yield j
:
:

In [14]: list(multicount(5))
Out[14]: [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5]

Now summing every third element is easy:
In [16]: sum(list(multicount(5))[::3])
Out[16]: 10

Does that help?

Kent

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


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Dick Moores
At 12:51 PM 10/10/2006, Tim Peters wrote:
>[Dick Moores]
>...
>>I'm thinking that just for the hell of it I could use urandom() as a
>>source of random decimal digits.
>
>You could, yes.
>
>>Or in a coin tossing program. Here's a list of 7817 '1's and 0's
>>generated by urandom():
>
>Note that the length of the list will vary from run to run.  Since you
>grab a million bytes, and there's a 1 in 128 chance of gettiing a 0 or
>1 byte, if  you run this code many times the /average/ length of the
>final list will be about 100./128 = 7812.5.

Yes, I saw that, thanks to Kent's earlier explanation.

>>  >>> from os import urandom
>>  >>> lst = list(urandom(100))
>>  >>> tosses = [y for y in lst if y in '01']
>>  >>> len(tosses)
>>7817
>>  >>>
>
>>Would this be a better random source than choice([0,1]), which uses random()?
>
>"Better" as measured against what criteria?

I meant would it be closer to true randomness than random(), even if 
much slower?

>  It's very much slower
>than using choice([0, 1])  (or choice("01"), or randrange(2), or ...),
>and can't (even in theory) be reproduced by setting a seed.

That's fine. Why would one want to?

>The only reason to use urandom() is if you /need/
>cryptographic-strength randomness.  For any meaning of "better" other
>than "cryptographically strong", no, urandom() isn't better.  If
>cryptographic strength is what you need, then urandom is not only
>better, there's no choice about it -- as the docs for random() say,
>the Mersenne Twister "completely unsuitable for cryptographic
>purposes".

No /need/, here. Just curiosity. I had enough of crypto in the Navy.

>Note that the random.SystemRandom class (see the docs) supplies the
>full range of convenience methods (like choice() and randrange()), but
>using urandom() as the base generator instead of the Mersenne Twister.

Great! I'll check them out.

Thanks very much, Tim.

Dick



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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Dick Moores
At 11:34 AM 10/10/2006, Kent Johnson wrote:
>Dick Moores wrote:
>>Here are the suggestions I've received:
>
>
>>All 3 are essentially the same, aren't they. 
>>Which makes me feel even dumber, because I 
>>don't understand any of them. I've consulted 3 
>>books, and still don't understand the use of yield.
>
>Yes, they are pretty much the same. Mine is 
>bare-bones, just doing what you specifically 
>asked for. Danny's is a full replacement for xrange(); Andrei's is in between.
>
>Here is a good introduction to generators:
>http://www.python.org/doc/2.2.3/whatsnew/node5.html

While I'm studying that, could you modify my 
while loop so that it incorporates your 
suggestion in a way that improves it? That would 
be a big help to me in understanding generators 
and the use of yield, I believe.

I don't want to give up on generators, even 
though your referenced article grants me 
permission to do so: "Generators are considered a 
new part of the core Python language, but 
learning or using them isn't compulsory; if they 
don't solve any problems that you have, feel free to ignore them."

>>I've realized now that all 3 scripts require an 
>>immense number of calls to random()
>
>Um, yeah, more than 2**31-1, right? :-)

At least several trillions, I'm thinking now, naively.

>By the way your approach to testing the 
>'goodness' of random() is pretty naive. For 
>example a random() that just repeats 0...99 over 
>and over would score very well. For large repeat 
>counts it could even repeat the same number for 
>a while and score well: 1 etc.

Sure, but passing the 3 tests I had in mind would 
be necessary if not sufficient conditions. 
Obviously random() would not be of the sorts you 
mention. Functions like those wouldn't be chosen for use in Python, right?

>  You might be interested in this article and the links at the bottom:
>http://en.wikipedia.org/wiki/Pseudo-random_number_generator

I'll be sure to check this out. And a friend 
tells me he has the Knuth vol. 2 (with that 
"Chapter 3, pp.1–193. Extensive coverage of 
statistical tests for non-randomness).

Dick



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


Re: [Tutor] How to read content in a tar file with tarfile module

2006-10-10 Thread Carlos Hanson
On Tue, October 10, 2006 11:15 am, Magnus Wirström wrote:
> Hi everyone
>
> I have written a app that makes a tar file and all works well... Now
> i want to expand that app so it can read read the tar and give me the
>  contents of the tar file. How is the best way to do this ? I can't
> find a "listdir" like function in tarfile. Can anyone point me in the
> right direction? Thanks Magnus

There are two choices depending on your needs.  First is the list()
method of a TarFile object:

>>> from tarfile import TarFile
>>> tar = TarFile('files.tar')
>>> tar.list()
-rw-r--r-- chanson/chanson  0 2006-10-10 11:31:08 file.1
-rw-r--r-- chanson/chanson  0 2006-10-10 11:31:11 file.2
-rw-r--r-- chanson/chanson  0 2006-10-10 11:31:15 file.3

The second option is the getmembers() method which returns TarInfo
objects:

>>> members = tar.getmembers()
>>> for member in members:
... print member.name
...
file.1
file.2
file.3
>>> for member in members:
... print "%s %s/%s\t%s %s %s" % (
... member.mode, member.uname, member.gname,
... member.size, member.mtime, member.name)
...
420 chanson/chanson 0 1160505068 file.1
420 chanson/chanson 0 1160505071 file.2
420 chanson/chanson 0 1160505075 file.3


-- 
Carlos Hanson
Web and System Administrator
Tigard-Tualatin School District
503.431.4053


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


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Tim Peters
[Dick Moores]
...
> I'm thinking that just for the hell of it I could use urandom() as a
> source of random decimal digits.

You could, yes.

> Or in a coin tossing program. Here's a list of 7817 '1's and 0's
> generated by urandom():

Note that the length of the list will vary from run to run.  Since you
grab a million bytes, and there's a 1 in 128 chance of gettiing a 0 or
1 byte, if  you run this code many times the /average/ length of the
final list will be about 100./128 = 7812.5.

>  >>> from os import urandom
>  >>> lst = list(urandom(100))
>  >>> tosses = [y for y in lst if y in '01']
>  >>> len(tosses)
> 7817
>  >>>

> Would this be a better random source than choice([0,1]), which uses random()?

"Better" as measured against what criteria?  It's very much slower
than using choice([0, 1])  (or choice("01"), or randrange(2), or ...),
and can't (even in theory) be reproduced by setting a seed.

The only reason to use urandom() is if you /need/
cryptographic-strength randomness.  For any meaning of "better" other
than "cryptographically strong", no, urandom() isn't better.  If
cryptographic strength is what you need, then urandom is not only
better, there's no choice about it -- as the docs for random() say,
the Mersenne Twister "completely unsuitable for cryptographic
purposes".

Note that the random.SystemRandom class (see the docs) supplies the
full range of convenience methods (like choice() and randrange()), but
using urandom() as the base generator instead of the Mersenne Twister.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Marc Poulin

--- Dick Moores <[EMAIL PROTECTED]> wrote:
> 
> Andrei's
> Write your own iterator:
>  >>> def hugerange(minval, maxval):
> ... val = minval
> ... while val < maxval:
> ... yield val
> ... val += 1
> 
> All 3 are essentially the same, aren't they. Which
> makes me feel even 
> dumber, because I don't understand any of them. I've
> consulted 3 
> books, and still don't understand the use of yield.
> 

This is an example of a "coroutine".
See http://en.wikipedia.org/wiki/Coroutine

def hugerange(minval, maxval):
val = minval
while val < maxval:
yield val
### return value of "val" to calling routine
### at this point and place
### hugerange() function temporarily
### on hold ...
###
### next call to hugerange()
### resumes at this point with
### all previous variable values
### still intact ...
val += 1


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Dick Moores
Thanks very much, Kent and Alan.

I'm thinking that just for the hell of it I could use urandom() as a 
source of random decimal digits. Or in a coin tossing program. Here's 
a list of 7817 '1's and 0's generated by urandom():

 >>> from os import urandom
 >>> lst = list(urandom(100))
 >>> tosses = [y for y in lst if y in '01']
 >>> len(tosses)
7817
 >>>

Would this be a better random source than choice([0,1]), which uses random()?

Dick

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


Re: [Tutor] How to read content in a tar file with tarfile module

2006-10-10 Thread Hugo González Monteverde
Magnus Wirström wrote:
> I have written a app that makes a tar file and all works well... Now i 
> want to expand that app so it can read read the tar and give me the 
> contents of the tar file. How is the best way to do this ? I can't find 
> a "listdir" like function in tarfile. Can anyone point me in the right 
> direction?

Hi, is this what you're looking for?? (tarfile Object docs)

getmembers( )

Return the members of the archive as a list of TarInfo objects. The list 
has the same order as the members in the archive.

getnames( )

Return the members as a list of their names. It has the same order as 
the list returned by getmembers().

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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Kent Johnson
Dick Moores wrote:
> Here are the suggestions I've received:


> 
> All 3 are essentially the same, aren't they. Which makes me feel even 
> dumber, because I don't understand any of them. I've consulted 3 
> books, and still don't understand the use of yield.

Yes, they are pretty much the same. Mine is bare-bones, just doing what 
you specifically asked for. Danny's is a full replacement for xrange(); 
Andrei's is in between.

Here is a good introduction to generators:
http://www.python.org/doc/2.2.3/whatsnew/node5.html

> I've realized now that all 3 scripts require an immense number of 
> calls to random()

Um, yeah, more than 2**31-1, right? :-)

By the way your approach to testing the 'goodness' of random() is pretty 
naive. For example a random() that just repeats 0...99 over and over 
would score very well. For large repeat counts it could even repeat the 
same number for a while and score well: 1 etc. 
You might be interested in this article and the links at the bottom:
http://en.wikipedia.org/wiki/Pseudo-random_number_generator

Kent

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


[Tutor] How to read content in a tar file with tarfile module

2006-10-10 Thread Magnus Wirström
Hi everyone

I have written a app that makes a tar file and all works well... Now i 
want to expand that app so it can read read the tar and give me the 
contents of the tar file. How is the best way to do this ? I can't find 
a "listdir" like function in tarfile. Can anyone point me in the right 
direction?
Thanks
Magnus
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Dick Moores

>
>> >>> for x in xrange(2**31):
>> pass
>>
>>Traceback (most recent call last):
>>   File "", line 1, in 
>> for x in xrange(2**31):
>>OverflowError: long int too large to convert to int
>

Here are the suggestions I've received:

Danny's
>#
>def myxrange(m, n=None, skip=1):
> """An xrange-like function that can deal with long ints.
> Note: doesn't deal with negative ranges particularly well."""
> if n is None:
>m, n = 0, m
> i = m
> while i < n:
> yield i
> i = i + skip
>#

Kent's
Encapsulate the while loop in a generator:
def count(limit):
   n=0
   while n>> def hugerange(minval, maxval):
... val = minval
... while val < maxval:
... yield val
... val += 1

All 3 are essentially the same, aren't they. Which makes me feel even 
dumber, because I don't understand any of them. I've consulted 3 
books, and still don't understand the use of yield.

Let me show you what I was doing when I ran into the 2**31-1 
limitation: 

This is one of my attempts to test the degree of uniformity of 
random(). The others are seeing how close the average random() return 
comes to 0.5, and using random() in an approximation of pi.

I've realized now that all 3 scripts require an immense number of 
calls to random(), so many that I wouldn't be able to use my computer 
for anything else for weeks, maybe months. But be that as it may, I'd 
still like to know how to use yield in my 1histogram-5a.

For your convenience, here's the list h, and the while loop I used:

h = 100*[0]

c = 0
while c < k:
 r = random()
 i = int(math.floor(100*r))
 h[i] += 1
 c += 1

So, for a random() return of 0.27862506717494118, i would be 27, and 
h[27] would be incremented by 1.
Thanks,

Dick


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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Danny Yoo

> >>> for x in xrange(2**31):
> pass
>
> Traceback (most recent call last):
>   File "", line 1, in 
> for x in xrange(2**31):
> OverflowError: long int too large to convert to int


Hi Dick,

Hmmm... I'd consider this a misfeature in the implementation.  Apparently 
xrange (and range) must be built with regular ints, not long ints.  This 
limitation is documented, although I think this is inconsistent and a bad 
thing.

 http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-73
 http://www.python.org/doc/2.3.5/lib/built-in-funcs.html#l2h-55


In the meantime, you can build your own version of xrange that should work 
on long ints.  Here is an example:

#
def myxrange(m, n=None, skip=1):
 """An xrange-like function that can deal with long ints.
 Note: doesn't deal with negative ranges particularly well."""
 if n is None:
m, n = 0, m
 i = m
 while i < n:
 yield i
 i = i + skip
#


This is a "generator" that does pretty much what you'll want.

##
>>> list(myxrange(1, 5))
[1, 2, 3, 4]
>>> list(myxrange(2**32, 2**32 + 5))
[4294967296L, 4294967297L, 4294967298L, 4294967299L, 4294967300L]
##


Note: this is a quick-and-dirty function; I wouldn't use it in production 
yet.  It needs some more work to reject bad input.  If you want, we can 
talk about exhaustively testing the function using robust equivalence 
class testing.  *grin*


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Andrei
Dick Moores  rcblue.com> writes:

 
> I can think of 2 workarounds for this 2**31 limitation in xrange(). 
> Use a while loop instead. Or use 2 or more for loops, keeping the 
> number of cycles in each under 2**31.
> 
> Are there others?

Write your own iterator:

>>> def hugerange(minval, maxval):
... val = minval
... while val < maxval:
... yield val
... val += 1
...
>>> for i in hugerange(2**31, 2**31 + 2):
... print i
...
2147483648
2147483649

It might be slower than Python's xrange implementation (haven't tested), but it
will take anything you throw at it as long as Python itself can handle it.

Yours,

Andrei

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


Re: [Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Kent Johnson
Dick Moores wrote:
>  >>> for x in xrange(2**31):
>  pass
> 
> Traceback (most recent call last):
>File "", line 1, in 
>  for x in xrange(2**31):
> OverflowError: long int too large to convert to int
>  >>>
> 
> I can think of 2 workarounds for this 2**31 limitation in xrange(). 
> Use a while loop instead. Or use 2 or more for loops, keeping the 
> number of cycles in each under 2**31.
> 
> Are there others?

Encapsulate the while loop in a generator:
def count(limit):
   n=0
   while nhttp://mail.python.org/mailman/listinfo/tutor


[Tutor] Workaround for limitation in xrange()?

2006-10-10 Thread Dick Moores
 >>> for x in xrange(2**31):
 pass

Traceback (most recent call last):
   File "", line 1, in 
 for x in xrange(2**31):
OverflowError: long int too large to convert to int
 >>>

I can think of 2 workarounds for this 2**31 limitation in xrange(). 
Use a while loop instead. Or use 2 or more for loops, keeping the 
number of cycles in each under 2**31.

Are there others?

Thanks,

Dick Moores

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


Re: [Tutor] Need problems to enhance programming skills

2006-10-10 Thread Geoframer
Hi Asrarahmed,
 
I recently started learning python. To reinforce the core language concepts I used the following website :
 
http://www.spoj.pl/
 
This site features a large number of programming problems which you can solve in any number of programming language (including Python) and you can have the programs checked online to see if they are valid. The programming exercises are not specific to python but more of the kind you find in programming contests. You can still get valuable lessons from them though.

 
Have fun - Geoframer 
On 10/10/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote:

 
 
Folks,
 
Have you got set of programming problems in python to reinforce the core language concepts and also build logic.
 
thanks.
 
Regards,
Asrar-- To HIM you shall return. ___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor

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


[Tutor] Need problems to enhance programming skills

2006-10-10 Thread Asrarahmed Kadri
 
 
Folks,
 
Have you got set of programming problems in python to reinforce the core language concepts and also build logic.
 
thanks.
 
Regards,
Asrar-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seeking good resources for Tkinter

2006-10-10 Thread Asrarahmed Kadri
Thanks for the reply.
 
The rule is: "when in doubt, always ask". 
Sorry if a similar question was answered previously.Regards,
Asrar
 
On 10/10/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> I want to learn the GUI programming in Python. Can you suggest some> nice web> resources as well as books to start.
It never fails to amaze me how often we get near identicalquestions being asked within a few days of each other and thennever mentioned again for weeks!There are many different GUI toolkits for Python but the two most
commonly used are:wxPython andTkinter.Tkinter is based ob Tcl/Tk and comes as standard in Pythonbut has slightly less sophisticated widgets and some folksdon't like its looks.Try the Tkinter Topic Guide in the Documentation
section of the Python web site for links to references and tutorials.There are some books too.wxPython has its own web site and is based on a C++framework called wxWidgets which also has a web site
full of references which are very good if you can read C++.HTH,Alan G.___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What are these things urandom() returns?

2006-10-10 Thread Alan Gauld
> source. The returned data should be unpredictable enough for
> cryptographic applications, though its exact quality depends on the
> OS implementation.

> >>> from os import urandom
> >>> urandom(10)
> '[EMAIL PROTECTED]'
> >>>
>
> ???

Its a string of bytes which you can use to encrypt data.
The simplest form of reversible encryption is the xor operation.
You simply xor the crypto string with the data to get an encrypted 
string.

To return to the data xor the data with the original cryptioo string
and you get the original data back.

Look at a 4 bit pattern to see this in action:

Data = 1010
Crypto =   0010
Data xor Crypto result = 1000

Now apply xor to result:

1000 xor 0010 = 1010, the original data...

Of course much more sophiisticated algorithms are used in
modern cryptography but the general principle applies, you
apply some random stream of crypto key to a set of data and
get an encrypted result. You then apply the same set of crypto
data (or a set that can reliable be derived from it) to the
encrypted stream to retrieve the original data. Examples
are DES and RSA.

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] What are these things urandom() returns?

2006-10-10 Thread Kent Johnson
Dick Moores wrote:
> "urandom(n)
> 
> Return a string of n random bytes suitable for cryptographic use.
> This function returns random bytes from an OS-specific randomness 
> source. The returned data should be unpredictable enough for 
> cryptographic applications, though its exact quality depends on the 
> OS implementation. On a UNIX-like system this will query 
> /dev/urandom, and on Windows it will use CryptGenRandom. If a 
> randomness source is not found, NotImplementedError will be raised. 
> New in version 2.4."
> 
>  >>> from os import urandom
>  >>> urandom(10)
> '[EMAIL PROTECTED]'

It's a string where each character (byte) is selected randomly from the 
range 0-255. In Python a string can be used as a container for arbitrary 
byte values that are not text and maybe don't fit your notion of what a 
string is.

When the Python interpreter prints a value automatically (not as the 
result of an explicit print) as above, it prints repr(value). For a 
string s, repr(s) uses \x escapes to display any non-printable characters.

So what you see above is the repr() of a string whose first bytes 
contain the values (in hexadecimal)
c0
f0
ea
0c
dd
95
75 == ord('u') so it prints as 'u' rather than \x75
40 == ord('@')
5a == ord('Z')
89

HTH,
Kent

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


Re: [Tutor] multithreading random()

2006-10-10 Thread Dick Moores
At 09:51 PM 10/9/2006, you wrote:
>On 10/10/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> > And another question. That page also says, "Almost all module
> > functions depend on the basic function random(), which generates a
> > random float uniformly in the semi-open range [0.0, 1.0). Python uses
> > the Mersenne Twister as the core generator. It produces 53-bit
> > precision floats and has a period of 2**19937-1." What is a 53-bit
> > precision float? Would that be something like a float accurate to 8
> > or 9 figures (dividing 53 by 8). Or what?
>
>Hi Dick,
>
>Have a look here: http://en.wikipedia.org/wiki/Double_precision
>and here: http://en.wikipedia.org/wiki/IEEE_floating-point_standard
>
>Basically, floating point numbers are of the form "x times 2**y",
>where x is a binary number between 1 and 10 (that's binary 10, decimal
>2).  53 bits means 53 binary digits of precision (including the
>initial 1).
>
>Does that help?

Well, before I dig into those references that I know are going to be 
difficult to understand, could you give me the bottom line about random()?

 >>> from random import random
 >>> print "%.17f" % random()
0.54188753195519013
 >>>

I can get what appears to be a random number precise(?) to 17 digits. 
How many base-10 digits would a "53-bit
precision float" have, if converted to base 10?

 >>> 2 ** 52
4503599627370496L
 >>> 2**53
9007199254740992L
 >>>

 >>> len(str(2**53))-1
15

So is the answer to my question something like len(str(2**53)) -1? 
(minus 1 because of the L in the long integer)

Thanks,

Dick



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


Re: [Tutor] multithreading random()

2006-10-10 Thread Dick Moores
At 09:57 PM 10/9/2006, you wrote:
>On 10/10/06, Dick Moores <[EMAIL PROTECTED]> wrote:
> > Please refer to
> > , from 
> which I quote:
> >
> > "The functions supplied by this module are actually bound methods of
> > a hidden instance of the random.Random class. You can instantiate
> > your own instances of Random to get generators that don't share
> > state. This is especially useful for multi-threaded programs,
> > creating a different instance of Random for each thread, and using
> > the jumpahead() method to make it likely that the generated sequences
> > seen by each thread don't overlap."
> >
> > Could someone point me to an example script where this was done? I'd
> > very much like to learn how to do that.
>
>Using google's code search, I found this:
>
> def _create_random_generators(self, num, delta, firstseed=None):
> """Return list of 'num' distinct generators.
> Each generator has its own unique segment of delta elements
> from Random.random()'s full period.
> Seed the first generator with optional arg firstseed (default
> is None, to seed from current time).
> """
> g = Random(firstseed)
> result = [g]
> for i in range(num - 1):
> laststate = g.getstate()
> g = Random()
> g.setstate(laststate)
> g.jumpahead(delta)
> result.append(g)
> return result
>
>Although, from looking at the docs you quoted, this may not be correct
>for python2.3+.

Thanks, John, That's a start.

Didn't know about Google's code search, 
. What was your search string?

Dick





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


[Tutor] What are these things urandom() returns?

2006-10-10 Thread Dick Moores
"urandom(n)

Return a string of n random bytes suitable for cryptographic use.
This function returns random bytes from an OS-specific randomness 
source. The returned data should be unpredictable enough for 
cryptographic applications, though its exact quality depends on the 
OS implementation. On a UNIX-like system this will query 
/dev/urandom, and on Windows it will use CryptGenRandom. If a 
randomness source is not found, NotImplementedError will be raised. 
New in version 2.4."

 >>> from os import urandom
 >>> urandom(10)
'[EMAIL PROTECTED]'
 >>>

???

Thanks,

Dick Moores
Win XP, Python 2.5


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