Re: [Tutor] tutor

2007-07-14 Thread elis aeris

please think about your first programming class, and then think about how
long it took the prof to teach the concept of array.

yes, it's about a week, if he is really good.



python is a very young language, prefered by professionals of its
simplicity, as such, the number of character of the document divided by the
actual code has a very high ratio,

because its well written document of people who know multiple languages, I
have to as what appears to be the same question over and over because
Ican't  read
the documents.

it's too cryptic. and as I go, i just come across dumb problem like:


how I want to use it



organge = 5

f.write( "there are orange "florida" oranges on the counter")



yes i know that i have asked about how to include a qutation mark and I have
asked about how to include a variable, but separately.

i know  it 's less than 3  "  , but i don't know where

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


Re: [Tutor] tutor

2007-07-14 Thread Alan Gauld

"elis aeris" <[EMAIL PROTECTED]> wrote

> because its well written document of people who know multiple 
> languages, I
> have to as what appears to be the same question over and over 
> because
> Ican't  read the documents.

The official tutor is designed for experienced programmers.
There are many beginners tutors too. Try some of the ones here

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

until you find one you like and then go through it. It will only
take a day of your time to cover all the basics but it will be
well invested.

Slightly more advanced bt shorter tutors are here:

http://wiki.python.org/moin/BeginnersGuide/Programmers


-- 
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] Importing question

2007-07-14 Thread Alan Gauld

"Kent Johnson" <[EMAIL PROTECTED]> wrote

> You can find out where any module is located using its __file__
> attribute. This works for modules written in C, too.

But not on Windoze. If it's built-in there is no __file__ attribute.
But the print trick tells you if it's builtin and if it's not __file__ 
works.

Alan G.


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


Re: [Tutor] tutor

2007-07-14 Thread bhaaluu
Greetings,

I have found that this Python Book has been helpful:

http://ibiblio.org/obp/thinkCS/python/english2e.tgz

How to Think Like a Computer Scientist
Learning with Python

by Allen B. Downey, Jeffrey Elkner and Chris Meyers
Printed copies now available from Green Tea Press.

One of the most fundamental steps in designing a computer program
is to be able to state the problem clearly and unambiguously and to
gain a clear understanding of what is required for its solution. The
objective here is to eliminate unimportant aspects and zero in on
the root problem (most computer programs solve a problem).

>From my own experiences, I've found that some people are able to
learn from books with minimal outside help, some can learn from
books with maximum outside help, and others can't learn from books
at all, they need to have their hands held from the first step to the
final destination. The later usually don't make it too far after the
first journey.

As for myself, I am a hobbyist programmer who enjoys computer
programming much in the same way that others enjoy doing cryptix
and crossword puzzles. I'm usually able to learn from books with
minimal help. Even so, in the beginning, the documentation for a
new computer programming language is somewhat difficult to follow.
So, I stick with the tutorials that are easier to understand until I can
gain enough experience to understand the documentation. I also
use Internet search engines to look for relevant code snippets.
I can often learn how to solve a particular problem by studying
source code that is already working, and modifying it to suit myself.

http://www.google.com/codesearch/advanced_code_search

I really don't know how to advise someone who can't read.
Computer programming is a a literate activity. If one can't
read, a book is no good. If one can't read, then advice
from a programmer's mailing list will probably go unnoticed.

If money ($ USD) is no object, perhaps a good suggestion is:
Enroll in a Summer Computer Programming Camp!
Yeah, that's the ticket! 8^D

Can anyone recommend a good
Summer Python Computer Programming Camp?

42!
-- 
bhaaluu at gmail dot com

On 7/14/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
> "elis aeris" <[EMAIL PROTECTED]> wrote
>
> > because its well written document of people who know multiple
> > languages, I
> > have to as what appears to be the same question over and over
> > because
> > Ican't  read the documents.
>
> The official tutor is designed for experienced programmers.
> There are many beginners tutors too. Try some of the ones here
>
> http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
> until you find one you like and then go through it. It will only
> take a day of your time to cover all the basics but it will be
> well invested.
>
> Slightly more advanced bt shorter tutors are here:
>
> http://wiki.python.org/moin/BeginnersGuide/Programmers
>
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Newbie Project part 2...

2007-07-14 Thread Tony Noyeaux

Thanks to all the tutors,... your advice has been invaluable todate,.. the 
learning continues.
 
The code below..asks the user to pick one of 3 options,.. once they choose,... 
a random result happens based on their choice.
 
-
import randomprint "\t\t\t\tLife Simulator"print "You have just turned 18 years 
old. Your life awaits,... choices to be made.."print 
"\na)Army,\nb)Navy,\nc)Airforce"job=raw_input("What will u join?")
if job == "a":print random.choice(["You win Silver star", "You are killed 
in action"])elif job == "b":print random.choice(["You fall overboard", "You 
command you're own battleship"])elif job == "c":print random.choice(["You 
get shot down", "You become wing commander"])
raw_input("\n\nPress Enter to 
quit.")--
 
This works fine.
 
I am having trouble coding in the 2nd question,.. and all subsequent questions.
 
The next Question.. has to be a result of the random result.
 
So, if they choose .. b).. and they join the navy,... random result,..."you 
command battleship results"... then,... a specific question,... with, and abc 
responce,.. will trigger. Obviosly,. there will be alot of responces to all the 
different options stored. Just trying to get the framework working which i have 
not been able to sucessfully accomplish yet.
 
So for the sake of the example, the user chose B)Navy ---> You command 
battleship random resulted... Then this question appeared. "Will you reenlist 
for another four years"? random results of "You are Promoted to Secretary of 
the Navy", "You lose a leg in a torpedo attack".
 
All the actual responces and random results are just examples to get the 
structure and code working.
 
I am hoping to be able to print a summary of each happening ... as results are 
generated. So the user can see their own personal story build.. based on their 
responces etc.
 
 
Thanks again for the insights you've given todate.
 
 
 
Tony Noyeaux
_
Local listings, incredible imagery, and driving directions - all in one place! 
Find it!
http://maps.live.com/?wip=69&FORM=MGAC01___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] curses

2007-07-14 Thread max baseman
hello all sorry to bother I'm working on my first curses program ive  
been wanting to learn curses for a while now and now that a have a  
lop top with fedora core running in run level 3 ware im trying to  
program all the tools i'll use but curses will be my only gui ALAN  
has been helping me with this program

import curses
from time import sleep
scr=curses.initscr()
population=0
seconds=0
try:
 scr.nodelay(1)
 scr.leaveok(0)
 max_y, max_x = scr.getmaxyx()
 while 1:
 sleep(1)
 second=seconds=+1
 population=population+2.5
 scr.addstr(0,0,"seconds",seconds)
 scr.addch,max_y/2, max_x/2, str(population)[0]
 scr.refresh()
finally:
 curses.endwin()


depending on ware i run this i get different results on may mac OSX  
10.4 i only get a wired second thing in the top left corner that  
looks like this secooes

and when i run this on fedora core 6 i get the seconds word in top  
left but no number and i get a single digit in the midle that changes  
i think the single digit is part of population but not all i cant  
find out what is wrong


any help would be great :) 
  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI error

2007-07-14 Thread Martin Walsh
Hi Darren,

Darren Williams wrote:
>  Hi all Smile
> 
> I have a Windows hosting account with lunarpages.com and am having a bit
> of trouble with CGI. I have enabled both CGI and Python in the control
> panel and made sure the permissions for all my CGI scripts are set to
> both readable and executable for all users but I keep getting the same
> 'The specified CGI application misbehaved by not returning a complete
> set of HTTP headers' error.

Have you tried to run the script from the command line? If the code
provided below is accurate and has not been changed by your mail client,
then it will raise an IndentationError, and the script will exit before
printing the Content-type header.

> The script that i'm trying to execute (example_7.1.py) -

>   32. # Define function display data.
>   33. def display_data(name, age):

Here is an improper indent ...

>   34. print "\n"
>   35. print "\n"
>   36. print "\tInfo Form\n"
>   37. print "\n"
>   38. print "\n"
>   39. print name, ", you are", age, "years old."
>   40. print "\n"
>   41. print "\n"
>   42.
>   43. # Define main function.
>   44. def main():

... and here ...

>   45. form = cgi.FieldStorage()
>   46. if (form.has_key("action") and form.has_key("name") \
>   47. and form.has_key("age")):
>   48.  if (form["action"].value == "display"):
>   49. display_data(form["name"].value, form["age"].value)
>   50. else:
>   51.  generate_form()
>   52.
>   53. # Call main function.
>   54. main()

... and here ...

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


Re: [Tutor] tutor

2007-07-14 Thread Paul W. Frields
On Sat, 2007-07-14 at 08:54 -0400, bhaaluu wrote:
> Greetings,
> 
> I have found that this Python Book has been helpful:
> 
> http://ibiblio.org/obp/thinkCS/python/english2e.tgz
> 
> How to Think Like a Computer Scientist
> Learning with Python
> 
> by Allen B. Downey, Jeffrey Elkner and Chris Meyers
> Printed copies now available from Green Tea Press.

This book is excellent for people who are new to the whole concept of
programming.  (That includes ideas like arrays, loops, etc.)  At least
one of the authors is a high school teacher who works, or used to work,
not too far from where I live -- although I don't know him personally.

You can also just read it online at:

http://ibiblio.org/obp/thinkCS/python/english2e/html/

-- 
Paul W. Frields, RHCE  http://paul.frields.org/
  gpg fingerprint: 3DA6 A0AC 6D58 FEC4 0233  5906 ACDB C937 BD11 3717
  Fedora Project:  http://fedoraproject.org/wiki/PaulWFrields
  irc.freenode.net: stickster @ #fedora-docs, #fedora-devel, #fredlug


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tutor

2007-07-14 Thread Alan Gauld
Hi,

> books with maximum outside help, and others can't learn from books
> at all, they need to have their hands held from the first step to 
> the
> final destination.

IMO Anyone in that position is never going to be able to program
at any level of competency. Its a simple fact that to program at
anything beyond the most basic level you will need to read
manuals/references etc. If you can't read and learn you will
struggle to learn enough of anything to progress.

> and crossword puzzles. I'm usually able to learn from books with
> minimal help. Even so, in the beginning, the documentation for a
> new computer programming language is somewhat difficult to follow.

I'm intrigued by that statement. What do you find challenging?

Once you learn one language and understand the basic concepts
then learning a new language is usually a very straightforward 
process.
After all there are relatively few new things to learn:

How the basic control strucctures are done:
Sequences (including overall program structure)
Loops
Branches
Modules( ie. procedures/functions, later to include OOP if 
relevant)

How I/O is done - reading input and writing output, and file handling

Data structures supported:
numbers, strings, boolean, collections

Those can usually be learnt in a morning and after that
its a matter of gaining experience writing (and reading) code.

I generally expect someone at work (not a hobbyist) to be fairly
fluent in a new language within a week. I would never send
someone who can program on a training course for a new
language (except for C++!)

> use Internet search engines to look for relevant code snippets.

This is fine but has a danger that you wind up stitching
together snippets without really understanding how they
work. This leads to a debugging nightmare!

> I can often learn how to solve a particular problem by studying
> source code that is already working, and modifying it to suit 
> myself.
>
> http://www.google.com/codesearch/advanced_code_search

That is the best way to do it IMHO. It ensures you pick up
language idioms, find interesting shortcuts and once you
start to see whays to improve the code you know you've
pretty much got on top of things!

> Computer programming is a a literate activity. If one can't
> read, a book is no good.

Indeed. Some folks tried to introduce purely visual
programming some years ago but the limitations of
not using text were so high that most efforts would up
in a strange hybrid experience of limited value.
(One such experiment was Borland's ObjectWindows - did
anyone else ever use that?)

-- 
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] Newbie Project part 2...

2007-07-14 Thread Alan Gauld

"Tony Noyeaux" <[EMAIL PROTECTED]> wrote in
-
import random
print "\t\t\t\tLife Simulator"
print "You have just turned 18 years old. Your life awaits,... choices 
to be made.."
print "\na)Army,\nb)Navy,\nc)Airforce"
job=raw_input("What will u join?")
if job == "a":
print random.choice(["You win Silver star", "You are killed in 
action"])
elif job == "b":
print random.choice(["You fall overboard", "You command you're own 
battleship"])
elif job == "c":
print random.choice(["You get shot down", "You become wing 
commander"])

raw_input("\n\nPress Enter to quit.")
--

> This works fine.
>
> I am having trouble coding in the 2nd question,.. and all subsequent 
> questions.
>
> The next Question.. has to be a result of the random result.

I suggested using a nested dictionary structure to hold the questions.
Did you try that? It should make this a relatively trivial problem.

I'll show the structure for the example you have given so far:

questions: {'Q':
{"""
You have just turned 18 years old.
Your life awaits,... choices to be made..
""" : ['Army', 'Navy', 'Airforce']}
 {'a':
 { 'Q': {#army questions here}
 }
  'b':
{
 'Q': {''What kind of ship?': 
['battleship','carrier','tug']},
 'a': {  # battleship questions},
 'b': { # carrier questions},
 'c': { # tug questions }
 }
  'c':
{  'Q': {# airforce Q here}
}
}

Obviously keeping the structure clear is critical to success and
a good programmers editor will help balance the braces/quotes etc

You can extend the structure to have multiple questions at each
stage by simply putting each questoon/answer dictionary in a list.
You could also break the structure to ease layout so for example
three separate instances, one per service. You could
even encapsulate it in a class (I would!)

You are essentially building a tree structure and then navigating
down it in a part-random, part-deterministic manner.

-- 
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] tutor

2007-07-14 Thread bhaaluu
Greetings,

I'm enjoying this discussion.

On 7/14/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> Hi,
>
> > books with maximum outside help, and others can't learn from books
> > at all, they need to have their hands held from the first step to
> > the
> > final destination.
>
> IMO Anyone in that position is never going to be able to program
> at any level of competency. Its a simple fact that to program at
> anything beyond the most basic level you will need to read
> manuals/references etc. If you can't read and learn you will
> struggle to learn enough of anything to progress.

Reading and comprehension are fundamental skills.

I can show someone how to cut a wooden board with a saw,
and join the pieces with a hammer and 10p nails,
to make a bookcase. That doesn't require reading.
They just need to watch me, then do it themselves until
they get it right. It takes a little time to learn if they've
never done it before, but it doesn't require reading.
They can load books on the bookshelf. That doesn't
make them literate. If they want to build anything more
complicated than a bookshelf, then reading is a handy
skill to have!
But computer programming requires reading. The programmer
has to be able to read in order to write a program. Why?
Because documenting a program is something that every
programmer should do, and if you can't read, how can you
possibly WRITE documentation? =)

>
> > and crossword puzzles. I'm usually able to learn from books with
> > minimal help. Even so, in the beginning, the documentation for a
> > new computer programming language is somewhat difficult to follow.
>
> I'm intrigued by that statement. What do you find challenging?

All of it seems complicated at first because I'm not familiar with the
nuances of the new language. As you've noted, the language docs
are written for experienced programmers, and may not be suitable
for learning from. As I gain familiarity with the language, things that
seemed over my head when I first started, start to make sense.
I didn't understand anything at all when reading the OOP docs. Why?
I'm completely new to OOP, and I don't have a good grasp of the
vocabulary yet. However, as I'm reading more, and doing more
examples, I'm feeling more comfortable with the documentation.
That is not to say that I understand it yet. But it isn't quite as
difficult to read the docs as it was, say, a week ago. As a hobbyist,
I do my programming in my spare time. I do not have a CS background.
Sometimes, documentation assumes a certain level of understanding.
Often, I'm never able to understand certain things.

>
> Once you learn one language and understand the basic concepts
> then learning a new language is usually a very straightforward
> process.
> After all there are relatively few new things to learn:
>
> How the basic control strucctures are done:
> Sequences (including overall program structure)
> Loops
> Branches
> Modules( ie. procedures/functions, later to include OOP if
> relevant)
>
> How I/O is done - reading input and writing output, and file handling
>
> Data structures supported:
> numbers, strings, boolean, collections
>
> Those can usually be learnt in a morning and after that
> its a matter of gaining experience writing (and reading) code.

Not everyone can pick things up as fast as some people do.
I usually start out slowly. But the things I do learn, I try to learn
well. I type in a lot of code, and I run a lot of code that others
have written, to see what it will do. I agree that the basics are
pretty much the same for most computer programming languages.
One of the first languages I learned was a dialect of Lisp, a
functional programming language called Logo. I was pleased to
find a Logo interpreter written in Python a couple of days ago.
What a treasure! =) [Only a hobbyist like me might think so?]
(For more information about Logo, see:
http://www.cs.berkeley.edu/~bh/  )

>
> I generally expect someone at work (not a hobbyist) to be fairly
> fluent in a new language within a week. I would never send
> someone who can program on a training course for a new
> language (except for C++!)

I would expect nothing less from a professional programmer!
Why the exception for C++ ?

>
> > use Internet search engines to look for relevant code snippets.
>
> This is fine but has a danger that you wind up stitching
> together snippets without really understanding how they
> work. This leads to a debugging nightmare!

Yeah, I know, right? =)
I got into the computer programming hobby shortly after buying
my first computer. I'd download code snippets from BBSes and
run them on MS-DOS. It could be very dangerous! Plus those
ancient Borland compilers...

>
> > I can often learn how to solve a particular problem by studying
> > source code that is already working, and modifying it to suit
> > myself.
> >
> > http://www.google.com/codesearch/advanced_code_search
>
> That is the best way to do it IMHO. It ensures you pick up
> language idioms, f

Re: [Tutor] tutor

2007-07-14 Thread ALAN GAULD
> > I'm intrigued by that statement. What do you find challenging?
>
> All of it seems complicated at first because I'm not familiar with the
> nuances of the new language. I do not have a CS background.

That's one of the reasons I wrote my tutor. It became obvious to me 
that most web tutors assumed a CS training. I decided I neded to 
define the CS terminology and as much of the theory as was 
necessary to understand the other tutors. Kind of like the meat 
of my first year computing course at uni' but accessible to 
non-CS folks.

> > Those can usually be learnt in a morning and after that
> > its a matter of gaining experience writing (and reading) code.
>
> Not everyone can pick things up as fast as some people do.

True, but if you already understand the concepts its usually pretty 
easy to translate them to a new language. You don;t need to remember 
the new syntax, just create your own short reference card. 
I find most lamnguages can be represented on a single side 
of A4/Letter paper. Not the whole language but enough to tackle
basic problems and start getting some hands on experience.
Thats the level I mean by a morning.

> One of the first languages I learned was a dialect of Lisp, a
> functional programming language called Logo. 

Logo is great, I first used it on an old CP/M 8 bit Micro.
The syntax is easy and yet extensible, with less parens 
than Lisp. I even had a Windows version (ie it could write 
GUI programs not just run in a window!) MSWLogo, for a while, 
and an OOP version (objectlogo).

The comp.lang.logo newsgroup still has a little bit of traffic, 
I sometimes drop by for old times sake! Last time I looked 
a version of MSWLogo had been produced for XP...

> > fluent in a new language within a week. I would never send
> > someone who can program on a training course for a new
> > language (except for C++!)
>
> Why the exception for C++ ?

C++ is a really tricky language. Not only is it huge but there 
are lots of real sneaky gotchas lurking that only long periods 
of painful debugging will reveal without training. The biggest 
problem is that C++ creates zillions of temporary objects which
sometimes don't get deleted thus chewing up memory
(no garbage collection). also the rules around multiple inheritance 
and member data access are arcane beyond telling. For example:

class X {
private:
int foo;
protected:
int bar;
public:
int baz;
}

class C: public X{}

class D: protected X {...}

class E: private X {...}


C *c = new(C)
D *d = new(D)
E *e = new(E)

Now tell me which attributes are visible
1) to the methods of C,D,E and 
2) to the users of the instances c,d,e.

And thats only single inheritance!

Too much power and flexibility leads to too many permutations.

> > (One such experiment was Borland's ObjectWindows - did
> > anyone else ever use that?)
>
> Was that the one that used objects like old flowchart symbols
> to design a program? I may have seen pictures of it somewhere,
> but never actually saw it in person. =)

That's the one. It was quite powerful with very easy database access 
mechanisms (but without a loop construct!) but ultimately so finnicky 
to use that you were a lot quicker writing code. As soon as Delphi 
appeared ObjectWindows died a quiet but surprisingly prolonged death


Alan G.



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


Re: [Tutor] Platform-independent Excel reader

2007-07-14 Thread John Fouhy
On 13/07/07, encore jane <[EMAIL PROTECTED]> wrote:
> Does anyone know about a good native Excel file reader that is platform
> independent?

I have had success with pyExcelerator:
http://sourceforge.net/projects/pyexcelerator

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


[Tutor] reading random line from a file

2007-07-14 Thread max baseman
im writing a quick quote reader that spits out a random quote from a  
show but cant get it to pick randomly
i tried
a=randrange(820)+1
text.readline(a)

and i would prefer not having to bring evryline into the program then  
picking like

for line in text.readlines():
lines.append(text)

...



any help would be great thanks 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading random line from a file

2007-07-14 Thread Luke Paireepinart
max baseman wrote:
> im writing a quick quote reader that spits out a random quote from a  
> show but cant get it to pick randomly
> i tried
> a=randrange(820)+1
> text.readline(a)
>
> and i would prefer not having to bring evryline into the program then  
> picking like
>
> for line in text.readlines():
>   lines.append(text)
>   
You don't have to read the lines in this way.
Just do lines = text.readlines() directly.

There's no way that you can just directly read a specific line without 
reading in the rest of the file,
because Python doesn't know beforehand where newlines are located inside 
of the file.
So even if this were possible, it would still read in all of the file up 
to and including the line you want, so that it could
count the number of newlines.

Why is it a problem to input it all at once?
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading random line from a file

2007-07-14 Thread John Fouhy
On 15/07/07, max baseman <[EMAIL PROTECTED]> wrote:
> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly
> i tried
> a=randrange(820)+1
> text.readline(a)
>
> and i would prefer not having to bring evryline into the program then
> picking like

The 'fortune' program in unix/linux produces random quotes from a
quote file.  As I understand it, it builds separate index files for
each input file.  I'm not sure exactly how it works, but I would guess
the index files contain byte offsets for the start and end of each
quote.  So you would read the whole index file (which will be much
shorter than the main quote file), select one at random, then use
.seek() and .read() to read just the bytes you are interested in from
the main file.

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


Re: [Tutor] reading random line from a file

2007-07-14 Thread Alan Gauld

"max baseman" <[EMAIL PROTECTED]> wrote


> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly

You can either get theclines to be the same length and
use a random index to seek() to the start of the line you want.
Or you can build a separate index file which records where
each line starts and randomly select one of thiose. But that
requires that the quotes file is only changed programmatically
so that the index file can be rebuilt each time you add/delete
a quote. (Or you build an indexing program)

Its much easier (unless the file is huge) to just use readlines()

HTH

Alan G.


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


Re: [Tutor] reading random line from a file

2007-07-14 Thread Andreas Kostyrka
Well he could implement the indexing into his program and check mtimes to 
decide if he needs to reindex.

But yes, as long the file fits into memory, readlines (or 
list(file("quotes,txt")) makes more sense.

Andreas

-- Ursprüngl. Mitteil. --
Betreff:Re: [Tutor] reading random line from a file
Von:"Alan Gauld" <[EMAIL PROTECTED]>
Datum:  15.07.2007 06:39


"max baseman" <[EMAIL PROTECTED]> wrote


> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly

You can either get theclines to be the same length and
use a random index to seek() to the start of the line you want.
Or you can build a separate index file which records where
each line starts and randomly select one of thiose. But that
requires that the quotes file is only changed programmatically
so that the index file can be rebuilt each time you add/delete
a quote. (Or you build an indexing program)

Its much easier (unless the file is huge) to just use readlines()

HTH

Alan G.


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

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