Re: [Tutor] now = ctime()[11:20]

2010-07-17 Thread Alan Gauld


"Richard D. Moores"  wrote 


but then I began to wonder how to create an "alias" for
"ctime()[11:20]" so I wouldn't have to keep typing or pasting it.
"now" would be fine. But how to get it to execute ctime()?


Create a function?

def myNow():
return ctime()[11:20]

Or am I missing something?


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


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


Re: [Tutor] now = ctime()[11:20]

2010-07-17 Thread Luke Paireepinart
If you are asking how to get a variable to call a function each time it's 
accessed... Well that's kind of a weird request. I know you can create 
properties in a class that act like variables but you can do whatever you want 
behind the scenes, like calling the now() function on each access. A much more 
clear way to do this would be to create a function that calls the now function 
and just call your function whenever you want the value. Is there a specific 
reason why you would prefer to solve this without having to type 2 extra 
characters () and in turn make your code more readable and logical? The problem 
with the approach you hint at is that then the function call is implicit. What 
if someone else wanted to use your variable in a tight loop? They don't 
probably expect or want your variable to automatically update itself.

Also are you aware of the timeit module for dete

Perhaps I misunderstood your intent.

Sent from my iPhone

On Jul 17, 2010 at 1:32 AM, "Richard D. Moores"  wrote:

> Please see 
> 
> Foolishly, without thinking it through, I expected the 2 prints to
> show different times. I understood right away why they were identical,
> but then I began to wonder how to create an "alias" for
> "ctime()[11:20]" so I wouldn't have to keep typing or pasting it.
> "now" would be fine. But how to get it to execute ctime()?
> 
> Thanks,
> 
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] now = ctime()[11:20]

2010-07-17 Thread Luke Paireepinart
Sorry the iPhone email client is terrible. It sent the previous email before I 
was done writing it. Anyway, the timeit module is available whenever you want 
to profile something and determine how long it takes to run.

Sent from my iPhone

On Jul 17, 2010, at 1:32 AM, "Richard D. Moores"  wrote:

> Please see 
> 
> Foolishly, without thinking it through, I expected the 2 prints to
> show different times. I understood right away why they were identical,
> but then I began to wonder how to create an "alias" for
> "ctime()[11:20]" so I wouldn't have to keep typing or pasting it.
> "now" would be fine. But how to get it to execute ctime()?
> 
> Thanks,
> 
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Strange sqlite3 behavior

2010-07-17 Thread Lang Hurst
I just had the weirdest issue with sqlite3.  I was trying to update a 
field to "Active".  I have a little database of students and sometimes 
they get sent to juvi, or just check out for a couple of months and show 
back up.  Anyway, I wanted to just have a field that had either "Active" 
or "Inactive".  I know, could go all boolean, but wanted to leave my 
options open.


Anyway, my code was along the lines of

   UPDATE students SET active="Inactive" where id="123456"


That would work, but

   UPDATE students SET active="Active" where id="123456"


wouldn't.  It wouldn't do anything, the field still held "Inactive".  I 
tried it manually through sqlite3browser and had the same result.  Strange.


   UPDATE students SET active="Bob Sagat" where id="123456"


worked.  Anything but "Active" works.  I was unable to update to 
"Active".  You can imagine how frustrating this was.  When I finally 
realized that it was only Active that didn't work, I changed my program 
to either "Active Student" or "Inactive Student" and it works fine.


Just had to post this somewhere, after loosing even more hair.

-Lang


--
There are no stupid questions, just stupid people.

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


Re: [Tutor] Strange sqlite3 behavior

2010-07-17 Thread Tim Golden

On 17/07/2010 8:10 AM, Lang Hurst wrote:

I just had the weirdest issue with sqlite3.  I was trying to update a
field to "Active".  I have a little database of students and sometimes
they get sent to juvi, or just check out for a couple of months and show
back up.  Anyway, I wanted to just have a field that had either "Active"
or "Inactive".  I know, could go all boolean, but wanted to leave my
options open.

Anyway, my code was along the lines of

 UPDATE students SET active="Inactive" where id="123456"


That would work, but

 UPDATE students SET active="Active" where id="123456"


wouldn't.  It wouldn't do anything, the field still held "Inactive".


My guess -- without bothering to check the sqlite docs -- is that
sqlite uses single quotes to delimit strings and double quotes to
delimit column. Or at least that it can be set up to do that. If
that is the case then "Active" is taken to refer to the column
active and not to the string active. So you're just setting it
to itself.

Try it with 'Active' instead.

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


Re: [Tutor] append, list and variables

2010-07-17 Thread pk
Dnia 17-07-2010 o 02:57:46 Steven D'Aprano   
napisał(a):



On Sat, 17 Jul 2010 05:44:07 am pk wrote:


In general, the shorter and more descriptive a variable name is,
the better.


That's a generalisation that is contradictory. Short works against
descriptive. Compare:


Well, yes I agree. I've chosen a wrong word in this context. By writing
"descriptive" I meant that it should be as *informative, full of meaning
and precision* as possible; and at the same time as concise as possible.  
The word "meaningful" would probably be appropriate here.

Achieving the two goals can sometimes be hard and if there're no or few
comments, a program - especially a longer one - can be difficult to read
and understand. Fortunately, Python is not Perl (with its unnecessarily
rich syntax, terseness, names containing punctuation and special
characters), so in most cases it is easy to see what is going on - at
least I think so.

Wow, that's very general what I've just written and very much a repetition
(rephrasing) of your words, but I wanted to be clear.

Regards,
Piotr



n  # nice and short
number_of_widgets_ordered  # nice and descriptive, if a bit wordy
widgets_ordered  # a happy medium

Which is appropriate depends on the context. Generally, descriptive
names are self-documenting and are to be preferred, but it is possible
to overdo it:

def multiply_two_numbers_and_add_one(
first_number_to_multiply_by, second_number_to_multiply_by
):
return first_number_to_multiply_by*second_number_to_multiply_by + 1


The more terse version is simple enough to understand:

def mult_plus_one(x, y):
return x*y + 1



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


Re: [Tutor] now = ctime()[11:20]

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 00:03, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>>
>> but then I began to wonder how to create an "alias" for
>> "ctime()[11:20]" so I wouldn't have to keep typing or pasting it.
>> "now" would be fine. But how to get it to execute ctime()?
>
> Create a function?
>
> def myNow():
>    return ctime()[11:20]

Yeah! Why didn't I think of that?

> Or am I missing something?

Only that I'm slower than you thought I was. :)

Thanks, Alan.

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


Re: [Tutor] now = ctime()[11:20]

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 00:08, Luke Paireepinart  wrote:
> If you are asking how to get a variable to call a function each time it's 
> accessed... Well that's kind of a weird request. I know you can create 
> properties in a class that act like variables but you can do whatever you 
> want behind the scenes, like calling the now() function on each access. A 
> much more clear way to do this would be to create a function that calls the 
> now function and just call your function whenever you want the value.

That's essentially Alan's suggestion, right? I've adopted it. See the
current state of the script I'm working on at
. (I'll be posting a separate
question about it.)

 >Is there a specific reason why you would prefer to solve this
without having to type 2 extra characters ()

Actually, at age 73 I have trouble remembering the [11:20] part.

>and in turn make your code more readable and logical? The problem with the 
>approach you hint at is that then the function call is implicit. What if 
>someone else wanted to use your variable in a tight loop? They don't probably 
>expect or want your variable to automatically update itself.

Point taken, and instead I'm going with Alan's function. But also,
this script is only for my own use, and the calling of ctime()[11:20]
is just so I can see where the script is bogging down. Turns out the
problem is that the writing of the long string of digits gets close to
exhausting my laptop's memory. But that's for another thread.

> Also are you aware of the timeit module for dete

I used to use it to time snippets at the command line. I've never used
it in a script. I'll check it out again. Usually if I time something
within a script I do it by using t0 = time() and t1 = time() and
printing the difference.

> Perhaps I misunderstood your intent.

I don't think so.

Thanks, Luke.

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


[Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
That's the goal of the latest version of my script at
. The best I've been able to do
so far is a file with 800 million digits.

But it seems the writing of 800 million digits is the limit for the
amount of memory my laptop has (4 GB). So my question is, how can I do
this differently? I'm pretty brand new to opening and writing files.
Here, I can't write many shorter lines, because the end result I seek
is one long string. But am I correct?

I'd appreciate any advice.

BTW line 29 was added after getting the outputs noted at the bottom of
the script. Using close() does seem to shorten the time it takes for
my laptop to become usable again, but it's not decisive. Sometimes I
have to reboot in order to get healthy again. (64-bit Vista).

BTW2 It's probably not obvious why the list comprehension (line 19)
has random.choice(d) where d is '0123456789'. Without that, the random
ints of 1000 digits would never begin with a '0'. So I give them a
chance to by prefixing one random digit using choice(d), and cutting
the length of the rest from 1000 to 999.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 2:01 PM, Richard D. Moores  wrote:
> That's the goal of the latest version of my script at
> . The best I've been able to do
> so far is a file with 800 million digits.
>
> But it seems the writing of 800 million digits is the limit for the
> amount of memory my laptop has (4 GB). So my question is, how can I do
> this differently? I'm pretty brand new to opening and writing files.
> Here, I can't write many shorter lines, because the end result I seek
> is one long string. But am I correct?
>

You are not, and it would have been quite trivial to verify that:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("test.txt", 'w')
>>> f.write('hello')
>>> f.write('world\n')
>>> f.close()
>>>
$ cat test.txt
helloworld
$

As you can see, two subsequent writes will end up on a single line. If
you want a newline character, you have to specify it yourself, like I
did after the second write. So you can generate your large number in
small pieces, no problem at all.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread bob gailer

On 7/17/2010 8:01 AM, Richard D. Moores wrote:

That's the goal of the latest version of my script at
. The best I've been able to do
so far is a file with 800 million digits.

But it seems the writing of 800 million digits is the limit for the
amount of memory my laptop has (4 GB). So my question is, how can I do
this differently? I'm pretty brand new to opening and writing files.
Here, I can't write many shorter lines, because the end result I seek
is one long string. But am I correct?
   


Not correct. Check this out:

f = open("c:\test.txt", "w")
f.write("1234")
f.write("5678")
print open("c:\test.txt").read()

When you say "line" I assume you mean text followed by whatever the OS defines 
as end-of-line or end-of-record.

To write such a line in Python you must include "\n" at the end.

[snip]


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Mark Lawrence

On 17/07/2010 13:01, Richard D. Moores wrote:

That's the goal of the latest version of my script at
. The best I've been able to do
so far is a file with 800 million digits.

But it seems the writing of 800 million digits is the limit for the
amount of memory my laptop has (4 GB). So my question is, how can I do
this differently? I'm pretty brand new to opening and writing files.
Here, I can't write many shorter lines, because the end result I seek
is one long string. But am I correct?

I'd appreciate any advice.

BTW line 29 was added after getting the outputs noted at the bottom of
the script. Using close() does seem to shorten the time it takes for
my laptop to become usable again, but it's not decisive. Sometimes I
have to reboot in order to get healthy again. (64-bit Vista).

BTW2 It's probably not obvious why the list comprehension (line 19)
has random.choice(d) where d is '0123456789'. Without that, the random
ints of 1000 digits would never begin with a '0'. So I give them a
chance to by prefixing one random digit using choice(d), and cutting
the length of the rest from 1000 to 999.

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



As an alternative to the suggestions given so far, why not open the file 
for write in binary mode, i.e. use 'wb' instead of 'w', and don't bother 
converting to strings?  Then when reading the file use 'rb' instead of 'r'.


HTH.

Mark Lawrence.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread bob gailer

On 7/17/2010 9:01 AM, Mark Lawrence wrote:

[snip]


As an alternative to the suggestions given so far, why not open the 
file for write in binary mode, i.e. use 'wb' instead of 'w', and don't 
bother converting to strings?  Then when reading the file use 'rb' 
instead of 'r'.


You can only write strings to files. See 6.9 in the documentation:

file.write(/str/)
   Write a string to the file

b mode only affects how line ends are handled. See 2. Built-in Functions:

The default is to use text mode, which may convert '\n' characters to a 
platform-specific representation on writing and back on reading. Thus, 
when opening a binary file, you should append 'b' to the /mode/ value to 
open the file in binary mode, which will improve portability. (Appending 
'b' is useful even on systems that don't treat binary and text files 
differently, where it serves as documentation.)



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 3:34 PM, bob gailer  wrote:
>
> You can only write strings to files. See 6.9 in the documentation:
> file.write(str) Write a string to the file b mode only affects how line ends
> are handled. See 2. Built-in Functions:
>
> The default is to use text mode, which may convert '\n' characters to a
> platform-specific representation on writing and back on reading. Thus, when
> opening a binary file, you should append 'b' to the mode value to open the
> file in binary mode, which will improve portability. (Appending 'b' is
> useful even on systems that don’t treat binary and text files differently,
> where it serves as documentation.)
>

me and Bob are in tune, it seems, I was composing the same message.

Would like to add that in python 3.x the 'b' does make a difference,
since read and write will use the 'bytes' type. Also, if you don't
mind a binary file, I would also like to suggest the following
non-python solution:

$ dd if=/dev/urandom of=rand_numbers.txt count=100MB

and adjust the count argument to your liking.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
Thanks, guys. See . It made the
1billion digits file in 213 seconds! And my laptop didn't begin to
choke. You made my day!

I'll consider all the binary info later.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Alan Gauld


"Richard D. Moores"  wrote

amount of memory my laptop has (4 GB). So my question is, how can I 
do

this differently? I'm pretty brand new to opening and writing files.
Here, I can't write many shorter lines, because the end result I 
seek

is one long string. But am I correct?


Others have answered that but I will ask, have you considered what
you will do with this 4G string once you have created it? You will not
be able to read it back into your memory in one piece so you will
need to process it in chunks.

It may be as well considering what you intend to do with it before
creating it? There may be a better representation.

Just a thought,

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


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


Re: [Tutor] Strange sqlite3 behavior

2010-07-17 Thread Lang Hurst

On 07/17/2010 02:04 AM, Tim Golden wrote:

On 17/07/2010 8:10 AM, Lang Hurst wrote:

I just had the weirdest issue with sqlite3.  I was trying to update a
field to "Active".  I have a little database of students and sometimes
they get sent to juvi, or just check out for a couple of months and show
back up.  Anyway, I wanted to just have a field that had either "Active"
or "Inactive".  I know, could go all boolean, but wanted to leave my
options open.

Anyway, my code was along the lines of

 UPDATE students SET active="Inactive" where id="123456"


That would work, but

 UPDATE students SET active="Active" where id="123456"


wouldn't.  It wouldn't do anything, the field still held "Inactive".


My guess -- without bothering to check the sqlite docs -- is that
sqlite uses single quotes to delimit strings and double quotes to
delimit column. Or at least that it can be set up to do that. If
that is the case then "Active" is taken to refer to the column
active and not to the string active. So you're just setting it
to itself.

Try it with 'Active' instead.

TJG



Yep, you were correct.  Just ran that through sqlite3browser and any 
phrase seems to work with either single or double quotes, EXCEPT 
"Active".  'Active' works though.  Good call.  I worked around it, but 
damn that was driving me crazy last night.


--
There are no stupid questions, just stupid people.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Dave Angel

Richard D. Moores wrote:

That's the goal of the latest version of my script at
. The best I've been able to do
so far is a file with 800 million digits.

But it seems the writing of 800 million digits is the limit for the
amount of memory my laptop has (4 GB). So my question is, how can I do
this differently? I'm pretty brand new to opening and writing files.
Here, I can't write many shorter lines, because the end result I seek
is one long string. But am I correct?

I'd appreciate any advice.

BTW line 29 was added after getting the outputs noted at the bottom of
the script. Using close() does seem to shorten the time it takes for
my laptop to become usable again, but it's not decisive. Sometimes I
have to reboot in order to get healthy again. (64-bit Vista).

BTW2 It's probably not obvious why the list comprehension (line 19)
has random.choice(d) where d is '0123456789'. Without that, the random
ints of 1000 digits would never begin with a '0'. So I give them a
chance to by prefixing one random digit using choice(d), and cutting
the length of the rest from 1000 to 999.

Dick

  
Your code is both far more complex than it need be, and inaccurate in 
the stated goal of producing random digits.


There's no need to try to have all the "digits" in memory at the same 
time.  As others have pointed out, since you're using write() method, 
there's no newline automatically added, so there's no problem in writing 
a few bytes at a time.


Your concern over a leading zero applies equally to two leading zeroes, 
or three, or whatever.  So instead of using str() to convert an int to a 
string, use string formatting.  Either the % operator or the format() 
method.  Just use a format that guarantees zero-filling to the same 
width field as the size limit you supplied.


So generate a number between 0 and 99, or whatever, and convert 
that to a fixed width string possibly containing leading zeros.  Write 
the string out, and loop back around.


DaveA

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 11:06, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>
>> amount of memory my laptop has (4 GB). So my question is, how can I do
>> this differently? I'm pretty brand new to opening and writing files.
>> Here, I can't write many shorter lines, because the end result I seek
>> is one long string. But am I correct?
>
> Others have answered that but I will ask, have you considered what
> you will do with this 4G string once you have created it?

Windows Explorer tells me it is 976,563 KB.

> You will not
> be able to read it back into your memory in one piece so you will
> need to process it in chunks.

Yes, that's true. Here's what I'm doing with a 200-million-digit
string: . But this script with
a 500 million string already causes a memory problem, and 1 billion is
impossible. If I processed in chunks, they would need to overlap
slightly, because the sequences to be found, such as 05251896 (my
father's birth date) could overlap the boundary between 2 neighboring
chunks, and thus not be located. That's about as far as I've got in
thinking about the problem.

> It may be as well considering what you intend to do with it before
> creating it? There may be a better representation.

Now that you see what I want to do with 1 billion random digits,
please give me your suggestion(s). As I mentioned before, I'm very new
to reading from and writing to files.

I think the project is much more interesting/satisfying, though
technically almost identical, when locating sequences of digits in pi.
I have a 100-million-digit file for this (thanks to gmpy.pi()), but
haven't tried for anything bigger. Here's the script that handles
searches in the mantissa of pi:
. I've put the 1 million digits
of pi on my website in the Pi directory at 
should anyone want to try this out. I'm not sure what my downloading
limit is, so I may not leave the file up there more than 24 hours.

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


[Tutor] Return error message for my script in Blender

2010-07-17 Thread Andrew Martin
I am new to Blender and Python (2.6 on vista) and was trying to follow a
tutorial in the book Blender 2.49 Scripting by Michel Anders. I was trying
to write a script that would create a user interface where the user could
select various aspect of an insect and the code would create a polygonal
bug. However, when I copied code exactly from the book, I got an error
saying "'return' outside function". Here is the code I had in the text
editor:


#!BPY

import Blender
import mymesh2

Draw = Blender.Draw
THORAXSEGMENTS = Draw.Create(3)
TAILSEGMENTS = Draw.Create(5)
LEGSEGMENTS = Draw.Create(2)
WINGSEGMENTS = Draw.Create(2)
EYESIZE = Draw.Create(1.0)
TAILTAPER = Draw.Create(0.9)

if not Draw.PupBlock('Add CreepyCrawly', [\
('Thorax segments:'  , THORAXSEGMENTS, 2,  50,
'Number of thorax segments'),
('Tail segments:' , TAILSEGMENTS, 0,  50, 'Number of tail segments'),
('Leg segments:' , LEGSEGMENTS, 2,  10,
'Number of thorax segments with legs'),
('Wing segments:' , WINGSEGMENTS, 0,  10,
'Number of thorax segments with wings'),
('Eye size:' , EYESIZE, 0.1,10, 'Size of the eyes'),
('Tail taper:' , TAILTAPER, 0.1,10,
'Taper fraction of each tail segment'),]):
 return


Anybody know why I keep getting this error?

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 13:56, Dave Angel  wrote:

> Your code is both far more complex than it need be, and inaccurate in the
> stated goal of producing random digits.

Dave, please see the code I posted in this thread more recently,
.

> There's no need to try to have all the "digits" in memory at the same time.
>  As others have pointed out, since you're using write() method, there's no
> newline automatically added, so there's no problem in writing a few bytes at
> a time.

I write a 1000 at a time.

> Your concern over a leading zero applies equally to two leading zeroes, or
> three, or whatever.

I don't think so. Look again at my function, randIntOfGivenLength().
Try a for loop with it, say with randIntOfGivenLength(9) and looping
100 times:

for x in range(100):
 n = randIntOfGivenLength(9)
 n = str(n)
 print(n)

You'll see that all strings have 9 digits. The problem is that none of
them begin with "0". These are just strings of almost-random digits,
and in the script I give the 1st digit a chance to be a "0" by
prepending a digit -- just one -- chosen by random.choice() from the
string "0123456789". There's no need for zero-filling. I do agree that
it would be good to eliminate the need to use str(). I'll look at the
first part of your formatting suggestion, and skip the zero-filling.

>  So instead of using str() to convert an int to a
> string, use string formatting.  Either the % operator or the format()
> method.  Just use a format that guarantees zero-filling to the same width
> field as the size limit you supplied.
>
> So generate a number between 0 and 99,

That would be between 1,000,000,000 and 9,999,999,999 .

Dick

> or whatever, and convert that
> to a fixed width string possibly containing leading zeros.  Write the string
> out, and loop back around.
>
> DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 11:48 PM, Richard D. Moores  wrote:
> On Sat, Jul 17, 2010 at 13:56, Dave Angel  wrote:
>> Your concern over a leading zero applies equally to two leading zeroes, or
>> three, or whatever.
>
> I don't think so. Look again at my function, randIntOfGivenLength().
> Try a for loop with it, say with randIntOfGivenLength(9) and looping
> 100 times:
>
> for x in range(100):
>     n = randIntOfGivenLength(9)
>     n = str(n)
>     print(n)
>
> You'll see that all strings have 9 digits. The problem is that none of
> them begin with "0". These are just strings of almost-random digits,
> and in the script I give the 1st digit a chance to be a "0" by
> prepending a digit -- just one -- chosen by random.choice() from the
> string "0123456789".
>

This is great, but now your *second* digit will never be zero. So your
strings are still "almost" random.

 Look, randomness is complicated. It's crap like this that trips
up even cryptographers all the time. I know you won't need your digits
for security or anything, but if you're concerned that the first digit
of a sequence is never zero, you should be equally concerned by the
second digit not being zero. It's the same amount of non-randomness,
and your 'solution' just moves the problem around, it doesn't solve
anything. 

Hugo

P.S.: I just thought of this: how about generating numbers that are
one digit too long, and removing the first digit instead of adding an
extra one? It just gets rid of the digit exhibiting non-randomness
altogether. (disclaimer: IANA cryptanalyst, so there may be equally
many things wrong with this)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread ALAN GAULD
> Now that you see what I want to do with 1 billion random 
digits,
> please give me your suggestion(s). As I mentioned 
before, 
> I'm very new to reading from and writing to files.

The way old text editors used to do this - in the days when we only had 16K RAM 
etc!
was to use buffers and pages. Let's say you have a 100K buffer size and read in 
the 
first buffer full of text. When you get down to say 75K you delete the first 
50K and 
load the next 50K from the file. Your cursor is now 25K into a new set of 100K.

This allows you to page back a certain amount and page forward a lot. And so 
you progress losing 50% from top or bottom and loading the next/previous chunk 
into RAM. Its non-trivial to get right but in your case you are only looking to 
sequentially process so you only need to look forward so not quite so bad...

You need markers to keep your location and other markers to keep track of 
the buffers location within the file.

If you really are only doing sequential processing you can dispense with 
the buffer/pages and just read small chunks such that you always have two 
in memory at once. The chunks might only be a few Kb in size depending 
on the longest sequence you are looking for (a chunk of twice that size is 
a good guide)

The seek() tell() and read() methods are your friends for this kind of work

 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:07, Hugo Arts  wrote:
> On Sat, Jul 17, 2010 at 11:48 PM, Richard D. Moores  
> wrote:
>> On Sat, Jul 17, 2010 at 13:56, Dave Angel  wrote:
>>> Your concern over a leading zero applies equally to two leading zeroes, or
>>> three, or whatever.
>>
>> I don't think so. Look again at my function, randIntOfGivenLength().
>> Try a for loop with it, say with randIntOfGivenLength(9) and looping
>> 100 times:
>>
>> for x in range(100):
>>     n = randIntOfGivenLength(9)
>>     n = str(n)
>>     print(n)
>>
>> You'll see that all strings have 9 digits. The problem is that none of
>> them begin with "0". These are just strings of almost-random digits,
>> and in the script I give the 1st digit a chance to be a "0" by
>> prepending a digit -- just one -- chosen by random.choice() from the
>> string "0123456789".
>>
>
> This is great, but now your *second* digit will never be zero. So your
> strings are still "almost" random.

Aw, you're right. Just as I was feeling pretty good about myself.

>  Look, randomness is complicated. It's crap like this that trips
> up even cryptographers all the time. I know you won't need your digits
> for security or anything, but if you're concerned that the first digit
> of a sequence is never zero, you should be equally concerned by the
> second digit not being zero. It's the same amount of non-randomness,
> and your 'solution' just moves the problem around, it doesn't solve
> anything. 
>
> Hugo
>
> P.S.: I just thought of this: how about generating numbers that are
> one digit too long, and removing the first digit instead of adding an
> extra one? It just gets rid of the digit exhibiting non-randomness
> altogether. (disclaimer: IANA cryptanalyst, so there may be equally
> many things wrong with this)

Looks good to me. I'll do it. And post back.

Thanks, Hugo.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:39, Richard D. Moores  wrote:
> On Sat, Jul 17, 2010 at 15:07, Hugo Arts  wrote:

>> P.S.: I just thought of this: how about generating numbers that are
>> one digit too long, and removing the first digit instead of adding an
>> extra one? It just gets rid of the digit exhibiting non-randomness
>> altogether. (disclaimer: IANA cryptanalyst, so there may be equally
>> many things wrong with this)
>
> Looks good to me. I'll do it. And post back.

Done. See .

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


Re: [Tutor] Return error message for my script in Blender

2010-07-17 Thread bob gailer

On 7/17/2010 5:11 PM, Andrew Martin wrote:
I am new to Blender and Python (2.6 on vista) and was trying to follow 
a tutorial in the book Blender 2.49 Scripting by Michel Anders. I was 
trying to write a script that would create a user interface where the 
user could select various aspect of an insect and the code would 
create a polygonal bug. However, when I copied code exactly from the 
book, I got an error saying "'return' outside function". Here is the 
code I had in the text editor:



#!BPY

import Blender
import mymesh2

Draw = Blender.Draw
THORAXSEGMENTS = Draw.Create(3)
TAILSEGMENTS = Draw.Create(5)
LEGSEGMENTS = Draw.Create(2)
WINGSEGMENTS = Draw.Create(2)
EYESIZE = Draw.Create(1.0)
TAILTAPER = Draw.Create(0.9)

if not Draw.PupBlock('Add CreepyCrawly', [\
('Thorax segments:'  , THORAXSEGMENTS, 2,  50,
'Number of thorax segments'),
('Tail segments:' , TAILSEGMENTS, 0,  50, 'Number of tail segments'),
('Leg segments:' , LEGSEGMENTS, 2,  10,
'Number of thorax segments with legs'),
('Wing segments:' , WINGSEGMENTS, 0,  10,
'Number of thorax segments with wings'),
('Eye size:' , EYESIZE, 0.1,10, 'Size of the eyes'),
('Tail taper:' , TAILTAPER, 0.1,10,
'Taper fraction of each tail segment'),]):
 return


Anybody know why I keep getting this error?


The return statement is not in a function. What else can we say. Either 
you copied incorrectly or there is a problem with the book.


Do you understand?

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:36, ALAN GAULD  wrote:
>> Now that you see what I want to do with 1 billion random
> digits,
>> please give me your suggestion(s). As I mentioned
> before,
>> I'm very new to reading from and writing to files.
>
> The way old text editors used to do this - in the days when we only had 16K 
> RAM etc!
> was to use buffers and pages. Let's say you have a 100K buffer size and read 
> in the
> first buffer full of text. When you get down to say 75K you delete the first 
> 50K and
> load the next 50K from the file. Your cursor is now 25K into a new set of 
> 100K.
>
> This allows you to page back a certain amount and page forward a lot. And so
> you progress losing 50% from top or bottom and loading the next/previous chunk
> into RAM. Its non-trivial to get right but in your case you are only looking 
> to
> sequentially process so you only need to look forward so not quite so bad...
>
> You need markers to keep your location and other markers to keep track of
> the buffers location within the file.
>
> If you really are only doing sequential processing you can dispense with
> the buffer/pages and just read small chunks such that you always have two
> in memory at once. The chunks might only be a few Kb in size depending
> on the longest sequence you are looking for (a chunk of twice that size is
> a good guide)

The longest sequence I can imagine finding with, say, a .1 probability
is less than 30 bytes, I'd guess.

To quote myself from my earlier reply to you:

"If I processed in chunks, they would need to overlap
slightly, because the sequences to be found, such as 05251896 (my
father's birth date) could overlap the boundary between 2 neighboring
chunks, and thus not be located."

I don't fully understand your "you always have two [chunks] in memory
at once". Does that take care of the overlap I think I need? I don't
want you or anyone to give me the the whole answer, but could you
clarify a bit about keeping 2 chunks in memory? Let's say the chunks,
in order, and without overlap, are A, B, C, D, E, ...  So I'd have AB
to search, then BC, then CD, then DE, ... ? But then what if I find an
instance of, say, my father's birthdate in B when searching AB. Then
I'd find the same instance again in BC. How do I prevent that from
counting as 2 instances?

> The seek() tell() and read() methods are your friends for this kind of work.

I hope they're not shy about telling me their secrets. Would your
tutorial be a good place to start to inquire of them?

There's this table in chapter 9 of Learning Python, 4th ed., but no
tell(). And a search on  'tell('  gets not a hit in the whole book
(the PDF). Is tell() a Python 2.x thing? It's a built-in for 2.x
(),
but not, it seems, for 3.x. I'm not complaining -- just sayin'.

Table 9-2. Common file operations
Operation Interpretation
output = open(r'C:\spam', 'w') Create output file ('w' means write)
input = open('data', 'r') Create input file ('r' means read)
input = open('data') Same as prior line ('r' is the default)
aString = input.read() Read entire file into a single string
aString = input.read(N) Read up to next N characters (or bytes) into a string
aString = input.readline() Read next line (including \n newline) into a string
aList = input.readlines() Read entire file into list of line strings (with \n)
output.write(aString) Write a string of characters (or bytes) into file
output.writelines(aList) Write all line strings in a list into file
output.close() Manual close (done for you when file is collected)
output.flush() Flush output buffer to disk without closing
anyFile.seek(N) Change file position to offset N for next operation
for line in open('data'): use line File iterators read line by line
open('f.txt', encoding='latin-1') Python 3.0 Unicode text files (str strings)
open('f.bin', 'rb') Python 3.0 binary bytes files (bytes strings)

Thanks again, Alan.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
Alan,

I should have added that file.seek and file.read don't show in the 3.x
docs either.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sun, Jul 18, 2010 at 2:24 AM, Richard D. Moores  wrote:
> Alan,
>
> I should have added that file.seek and file.read don't show in the 3.x
> docs either.
>

You just need to know where to look:

http://docs.python.org/py3k/library/io.html#io.IOBase.tell

read and seek are in there as well. There is no more file class in
3.x, the io module takes care of all that. The documentation for the
open() function mentions all of this.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Steven D'Aprano
Do you care about speed? If this is a script that just needs to run 
once, it seems to me that the simplest, easiest to read solution is:

import random
def random_digit():
return "0123456789"[random.randrange(10)]

f = open('rand_digits.txt', 'w')
for i in xrange(10**9):
f.write(random_digit())

f.close()


This is, of course, horribly inefficient -- it generates digits one at a 
time, and worse, it writes them one at a time. I got bored waiting for 
it to finish after 20 minutes (at which time it was about 10% of the 
way through), but you could let it run in the background for as long as 
it takes.

If speed does matter, the first improvement is to generate larger 
streams of random digits at once. An even bigger improvement is to cut 
down on the number of disk-writes -- hard drives are a thousand times 
slower than RAM, so the more often you write to the disk, the worse off 
you are.


import random
def random_digits(n):
"Return n random digits with one call to random."
return "%0*d" % (n, random.randrange(10**n))

f = open('rand_digits.txt', 'w')
for i in xrange(1000):
buffer = [random_digits(10) for j in xrange(10)]
f.write(''.join(buffer))

f.close()

On my not-even-close-to-high-end PC, this generates one billion digits 
in 22 minutes:

[st...@sylar python]$ time python randdigits.py

real22m31.205s
user20m18.546s
sys 0m7.675s
[st...@sylar python]$ ls -l rand_digits.txt
-rw-rw-r-- 1 steve steve 10 2010-07-18 11:00 rand_digits.txt


Having generated the digits, it might be useful to look for deviations 
from randomness. There should be approximately equal numbers of each 
digit (100,000,000 each of 0, 1, 2, ..., 9), of each digraph 
(10,000,000 each of 00, 01, 02, ..., 98, 99), trigraphs (1,000,000 each 
of 000, ..., 999) and so forth.

The interesting question is, if you measure a deviation from the 
equality (and you will), is it statistically significant? If so, it is 
because of a problem with the random number generator, or with my 
algorithm for generating the sample digits?



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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 17:39, Hugo Arts  wrote:
> On Sun, Jul 18, 2010 at 2:24 AM, Richard D. Moores  wrote:
>> Alan,
>>
>> I should have added that file.seek and file.read don't show in the 3.x
>> docs either.
>>
>
> You just need to know where to look:
>
> http://docs.python.org/py3k/library/io.html#io.IOBase.tell
>
> read and seek are in there as well. There is no more file class in
> 3.x, the io module takes care of all that. The documentation for the
> open() function mentions all of this.

Ah. Thanks again, Hugo. Looks like tough going for me for a couple of
days. Months?

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


Re: [Tutor] Return error message for my script in Blender

2010-07-17 Thread Steven D'Aprano
On Sun, 18 Jul 2010 07:11:19 am Andrew Martin wrote:
> I am new to Blender and Python (2.6 on vista) and was trying to
> follow a tutorial in the book Blender 2.49 Scripting by Michel
> Anders. I was trying to write a script that would create a user
> interface where the user could select various aspect of an insect and
> the code would create a polygonal bug. However, when I copied code
> exactly from the book, I got an error saying "'return' outside
> function". Here is the code I had in the text editor:
[...]
> Anybody know why I keep getting this error?

The return statement can only be inside a function. Here is an example:

def f():
return "This is inside a function."

You have it outside a function, just like the error message says.


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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 18:01, Steven D'Aprano  wrote:
> Do you care about speed? If this is a script that just needs to run
> once, it seems to me that the simplest, easiest to read solution is:
>
> import random
> def random_digit():
>    return "0123456789"[random.randrange(10)]
>
> f = open('rand_digits.txt', 'w')
> for i in xrange(10**9):
>    f.write(random_digit())
>
> f.close()
>
>
> This is, of course, horribly inefficient -- it generates digits one at a
> time, and worse, it writes them one at a time. I got bored waiting for
> it to finish after 20 minutes (at which time it was about 10% of the
> way through), but you could let it run in the background for as long as
> it takes.
>
> If speed does matter, the first improvement is to generate larger
> streams of random digits at once. An even bigger improvement is to cut
> down on the number of disk-writes -- hard drives are a thousand times
> slower than RAM, so the more often you write to the disk, the worse off
> you are.
>
>
> import random
> def random_digits(n):
>    "Return n random digits with one call to random."
>    return "%0*d" % (n, random.randrange(10**n))
>
> f = open('rand_digits.txt', 'w')
> for i in xrange(1000):
>    buffer = [random_digits(10) for j in xrange(10)]
>    f.write(''.join(buffer))
>
> f.close()
>
> On my not-even-close-to-high-end PC, this generates one billion digits
> in 22 minutes:
>
> [st...@sylar python]$ time python randdigits.py
>
> real    22m31.205s
> user    20m18.546s
> sys     0m7.675s
> [st...@sylar python]$ ls -l rand_digits.txt
> -rw-rw-r-- 1 steve steve 10 2010-07-18 11:00 rand_digits.txt

My  took 218 secs.

> Having generated the digits, it might be useful to look for deviations
> from randomness. There should be approximately equal numbers of each
> digit (100,000,000 each of 0, 1, 2, ..., 9), of each digraph
> (10,000,000 each of 00, 01, 02, ..., 98, 99), trigraphs (1,000,000 each
> of 000, ..., 999) and so forth.

Yes. I'll do some checking. Thanks for the tips.
>
> The interesting question is, if you measure a deviation from the
> equality (and you will), is it statistically significant? If so, it is
> because of a problem with the random number generator, or with my
> algorithm for generating the sample digits?

Ah. Can't wait to see what turns up.

Thanks, Steve.

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


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Luke Paireepinart

On Jul 17, 2010, at 7:19 PM, "Richard D. Moores"  wrote:

> I don't fully understand your "you always have two [chunks] in memory
> at once". Does that take care of the overlap I think I need? I don't
> want you or anyone to give me the the whole answer, but could you
> clarify a bit about keeping 2 chunks in memory? Let's say the chunks,
> in order, and without overlap, are A, B, C, D, E, ...  So I'd have AB
> to search, then BC, then CD, then DE, ... ? But then what if I find an
> instance of, say, my father's birthdate in B when searching AB. Then
> I'd find the same instance again in BC. How do I prevent that from
> counting as 2 instances?

Imagine you have a series of Buildings in a block, and you are checking to see 
if a series of 5 windows in a row is on ( even between buildings). You start at 
one end of the block and you check the first five windows. If they are all on, 
you have found a match. If not, you move forward one window. Now repeat. After 
a few repetitions you will end up at the border between two buildings, and 
after a few more you will be looking exclusively at the 2nd building. Then 
after a few more you will be looking at the 3rd building and so on.

But are you ever looking at more than one building at a time? Yea but only 
adjacent buildings and only for 4 steps. Once, when you have 4 windows from the 
first, 1 from the second, again for 3 windows from the first, then 2 and then 1.

The way this translates into an algorithm is that instead of discarding your 
old set of data as soon as you need to load another set, you hold onto it long 
enough to get all the way engaged with that second set, and from there you can 
free up the previous block's memory and move forward. And since you are using a 
"sliding window" approach you shouldn't ever find one result twice. Although 
note that if you overlap your searches as I said earlier, you can run into some 
matching situations that are interesting. Say you search for 55 and your string 
is 1827355513. You'll find 55 twice even though there are only 3 fives in the 
search string. Just something to keep in mind.

Hope that helps!
-luke___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Alan Gauld

"Richard D. Moores"  wrote

I don't fully understand your "you always have two [chunks] in 
memory

at once". Does that take care of the overlap I think I need? I don't
want you or anyone to give me the the whole answer, but could you
clarify a bit about keeping 2 chunks in memory?


Luke gave a good illustration, I'll try to be a bit more clear than
my previous explanation.

Think of the buffer as being a window into the file.
We read the file in chunks and load these into the buffer.
Lets say 3 chunks per buffer.
When we first start up we read chunks 1,2,3 into the buffer.
We start processing chunk 1, then move into chunk 2. By the
time we are 50% through chunk 2 we no longer need chunk 1
so we move it out of memory and read in chunk 4. We move
chunk 2 & 3 to the top of the buffer and append chunk 4.
Now we continue processing until we hit chunk 3 and go
down 50%, we no longer need chunk 2 so delete it and
read chunk 5, move 3 and 4 to the top and load chunk 5
into the buffer.

And so it goes on.
The buffer is a continuous bit of memory holding 3 chunks
of data contiguously so you can process over the boundaries
but your program only has to store 3 chunks worth at a time.

I hope thats clearer.

It might help to think how this worked for an old style
text editor. The chunks represent a screens worth of display.
You only need to display the middle chunk. As you cursor
off the bottom of the screen you pull up the next chunk
in memory (and in the background read the next again
from file and lose the top chunk). This resulted in a user
experience that looked like the whole file was in memory
but in fact only about 6Kb was (3 screens full).

HTH,


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


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