Re: [Tutor] Print record x in a file

2005-01-23 Thread Kent Johnson
Jacob S. wrote:
import random
#the above gives the program the ability to get a
#pseudo random number
file = open('test.rantxt')
listcontents = file.readlines()
#gives you the file as a list of records or it did on
#(assuming each line is a record)
file.close()
lenoflist = len(listcontents)-1
#find the length of the list and take one of because
computers count from 0

Yes, but len returns counting from 1.
Anyway, you would have to add one to correct that anyway, wouldn't you?
If randrange is start <= x *<=* end, then you don't have to add one, you
just use the length.
If randrange is start<= x < end like __builtin__ range, you have to put
randrange(1,lenoflist+1)
x = random.randrange(0,lenoflist)

I would use randint because list indices need to be integers -- unless of
course I mistaken and
randrange returns an integer also. (But that would be repetitive to have to
functions do the same thing)
A quick check of the module docs (Jacob, do you know where to find the 
docs?) gives
randrange(  	[start,] stop[, step])
Return a randomly selected element from range(start, stop, step). This is equivalent to 
choice(range(start, stop, step)), but doesn't actually build a range object. New in version 1.5.2.

So the limits on randrange() are the same as for range() - it is start <= x < stop. And the returned 
value is an integer.

 Since len(listcontents) is one greater than the largest valid index of listcontents, the correct 
use of randrange() for this problem is
  x = random.randrange(0, len(listcontents))


print listcontents[x]

HTH,
Jacob
___
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] How would python messure up in performance?

2005-01-23 Thread Kevin
How well would a multi user texed based game created in just Python
run if there were over 300 - 400 players (Just a hypathetical
question) Would python be to slow to handle that amount of traffic?

Thanks

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


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Max Noel
On Jan 23, 2005, at 12:55, Kevin wrote:
How well would a multi user texed based game created in just Python
run if there were over 300 - 400 players (Just a hypathetical
question) Would python be to slow to handle that amount of traffic?
	It depends... What would be happening in said game? What would it 
involve, calculation-wise? What would you be running it on?

	In that kind of program, the core programming itself is rarely a 
bottleneck (IOW, Python should work just fine). The true elements on 
which your game's performance depend are:
1) Connection bandwidth and latency
2) Database access (especially if the DB is not on a remote server)
3) File I/O, if any

Could you give us a few more specs?
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] Print record x in a file

2005-01-23 Thread David Holland
 --- "Jacob S." <[EMAIL PROTECTED]> wrote: 
> > This will get a random record
> > I hope you do not think the comments are
> patronising
> > but you did say you are new so I did not want to
> give
> > naked code.
> >
> > import random
> > #the above gives the program the ability to get a
> > #pseudo random number
> > file = open('test.rantxt')
> > listcontents = file.readlines()
> > #gives you the file as a list of records or it did
> on
> > #(assuming each line is a record)
> > file.close()
> > lenoflist = len(listcontents)-1
> > #find the length of the list and take one of
> because
> > computers count from 0
> 
> Yes, but len returns counting from 1.
> Anyway, you would have to add one to correct that
> anyway, wouldn't you?
As len(list) gives the length and when the elements of
the list are number for element 0 onwards, the last
element is number len(list) -1.   Test yourself
anyway.
Randrange does gives an integer or it did when I
tested this as otherwise it would have errored.
I don't know if it always does, what do other people
think ?
However Kent's idea of random.choice(list) is better
than my idea as it is one less line of code !!

.
If you have a length of 5
> If randrange is start <= x *<=* end, then you don't
> have to add one, you
> just use the length.
> If randrange is start<= x < end like __builtin__
> range, you have to put
> randrange(1,lenoflist+1)
> 
> > x = random.randrange(0,lenoflist)
> 
> I would use randint because list indices need to be
> integers -- unless of
> course I mistaken and
> randrange returns an integer also. (But that would
> be repetitive to have to
> functions do the same thing)
> 
> > print listcontents[x]
> 
> HTH,
> Jacob
> 
>  





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] on the way to find pi

2005-01-23 Thread Ali Polatel
dear friends ,
I found a code which calculates pi with an interesting algorithm the programme code is below:
from sys import stdout
def f((q,r,t,k)):    n = (3*q+r) / t    if (4*q+r) / t == n:    return (10*q,10*(r-n*t),t,k,n)    else:    return (q*k, q*(4*k+2)+r*(2*k+1),t*(2*k+1),k+1)
# Call pi(20) for first 20 digits, or pi() for all digitsdef pi(n=-1):    printed_decimal = False    r = f((1,0,1,1))    while n != 0:    if len(r) == 5:    stdout.write(str(r[4]))    if not printed_decimal:    stdout.write('.')    printed_decimal = True    n -= 1    r = f(r[:4])    #stdout.write('\n')
if __name__ == '__main__':    from sys import argv    try:    digit_count = long(argv[1])    except:    digit_count=int(raw_input('How many digits? :'))    pi(digit_count)    
This code gives the number in an unusual format like "3.1415'None'" it has a number part and a string part . I want to seperate these from easc other but I couldn't manage. I mean when I try to turn it into string format then try to use things like [:4] or like that they don't work.Any idea how to seperate this 'None' from the number and make it a real normal number on which I can do operations like +1 -1 or like that :)
Regards
		Do you Yahoo!? 
The all-new My Yahoo! – What will yours do?___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] on the way to find pi

2005-01-23 Thread Max Noel
 This code gives the number in an unusual format like "3.1415'None'" 
it has a number part and a string part . I want to seperate these from 
easc other but I couldn't manage. I mean when I try to turn it into 
string format then try to use things like [:4] or like that they don't 
work.Any idea how to seperate this 'None' from the number and make it 
a real normal number on which I can do operations like +1 -1 or like 
that :)
Regards
	Well, you should try to have a look at where the "None" is appended to 
the string representing the number, and use a type check to decide 
whether or not you're appending/adding/whatever.

>>> type("abc")

>>> type(123)

>>> type(None)

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] on the way to find pi

2005-01-23 Thread Orri Ganel




Ali Polatel wrote:

  dear friends ,
  I found a code which calculates pi with an interesting algorithm
the programme code is below:
  from sys import stdout
  def f((q,r,t,k)):
    n = (3*q+r) / t
    if (4*q+r) / t == n:
    return (10*q,10*(r-n*t),t,k,n)
    else:
    return (q*k, q*(4*k+2)+r*(2*k+1),t*(2*k+1),k+1)
  # Call pi(20) for first 20 digits, or pi() for all digits
def pi(n=-1):
    printed_decimal = False
    r = f((1,0,1,1))
    while n != 0:
    if len(r) == 5:
    stdout.write(str(r[4]))
    if not printed_decimal:
    stdout.write('.')
    printed_decimal = True
    n -= 1
    r = f(r[:4])
    #stdout.write('\n')
  if __name__ == '__main__':
    from sys import argv
    try:
    digit_count = long(argv[1])
    except:
    digit_count=int(raw_input('How many digits? :'))
    pi(digit_count)
    
  This code gives the number in an unusual format like
"3.1415'None'" it has a number part and a string part . I want to
seperate these from easc other but I couldn't manage. I mean when I try
to turn it into string format then try to use things like [:4] or like
that they don't work.Any idea how to seperate this 'None' from the
number and make it a real normal number on which I can do operations
like +1 -1 or like that :)
  Regards
   
  Do you Yahoo!?
The all-new My Yahoo! – What will
yours do?
  

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

The reason for this is that pi() doesn't return anything (hence None).
What it does do is print the result (stdout.write).  So you can either
use it w/o print (just pi(5)) or modify the code so it returns pi all
at once instead of printing it as it goes (this will make it seem like
it is taking more time since it will have to calculate the whole thing
before printing anything, instead of printing the digits one and 4 at a
time (I've bolded the changes):

def pi(n=1000): # if you set n to -1, nothing will ever
be printed because #the loops waits for pi to be calculated before
printing anything
    from decimal import setcontext, getcontext, Decimal,
ExtendedContext
    setcontext(ExtendedContext) # No DvisionByZero Error
    getcontext().prec = n+1 # always just enough, and this way you can
# still perform operations on the results (if you set the precision to
# Infinity, it gives you an OverflowError to perform operations)
    pil = []
    printed_decimal = False
    r = f((1,0,1,1))
    while n != 0:
    if len(r) == 5:
    pil.append(str(r[4]))
    if not printed_decimal:
    pil.append(('.'))
    printed_decimal = True
    n -= 1
    r = f(r[:4])
    return Decimal(''.join(pil))

With Decimal(), you get an accurate string representation of
floating point numbers which treats the strings as floating point
numbers:

>>> pi(20)-3
Decimal("0.1415926535897932384")

The only reason the original pi(...) didn't have to deal with this
is that it wrote directly to stdout, without formatting the text as
string, float, etc.

-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.


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


[Tutor] Changing (Unix) environment for python shell/popen() commands

2005-01-23 Thread Scott W
Hey all.
I'm unfortunately stuck using python 1.5.2, primarily on Linux 
currently, and have done the usual searching (on list, active state, 
google), without luck.

I've got to shell out from my python code to execute a command, but 
_must_ set the environment at the same time (or prior to execution).  I 
say must, because in this case the obvious answer of modifying the 
command to be called or wrapping it with a shell script to set the 
environment won't work/isn't going to happen.

I saw some comments about setting os.environ[], but 
didn't seem to be seeing this work in subsequent calls using popen2().

Does anyone have a working piece of code setting some env variable 
successfully prior to(or along with) calling popen2() (or friends).

Also, is there any way to get the called programs return code via 
popen() (again, under python 1.5.2)?

I know this has been asked often enough, but once again, it seems I'm 
left wanting for _good_ documentation under python (ie compared to man 
pages, DevStudio Win32 docs, Linux info, Java docs, etc), including the 
half dozen python books I own  (great for some things, but no reference 
whatsoever to other things that I'd think would warrant some coverage)..

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


Re: [Tutor] Changing (Unix) environment for python shell/popen() commands

2005-01-23 Thread "Jörg Wölke"
> Hey all.

Hello!
 
> I'm unfortunately stuck using python 1.5.2, primarily on Linux 
> currently, and have done the usual searching (on list, active state, 
> google), without luck.
> 
> I've got to shell out from my python code to execute a command, but 
> _must_ set the environment at the same time (or prior to execution).  I 
> say must, because in this case the obvious answer of modifying the 
> command to be called or wrapping it with a shell script to set the 
> environment won't work/isn't going to happen.
> 
> I saw some comments about setting os.environ[], but 
> didn't seem to be seeing this work in subsequent calls using popen2().

What is the error?

> Does anyone have a working piece of code setting some env variable 
> successfully prior to(or along with) calling popen2() (or friends).
> Also, is there any way to get the called programs return code via 
> popen() (again, under python 1.5.2)?
 

>>> os.environ['murx']="hello world"
>>> os.system("echo $murx")
>>> x=os.popen("echo $murx").readline()
>>> x
'hello world\n'
>>> os.system("man bash")

> I know this has been asked often enough, but once again, it seems I'm 
> left wanting for _good_ documentation under python (ie compared to man 
> pages, DevStudio Win32 docs, Linux info, Java docs, etc), including the 
> half dozen python books I own  (great for some things, but no reference 
> whatsoever to other things that I'd think would warrant some coverage)..
> 
> Thanks,
> 
> Scott
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Wir sind jetzt ein Imperium und wir schaffen uns
unsere eigene Realität. Wir sind die Akteure der 
Geschichte, und Ihnen, Ihnen allen bleibt nichts,
als die Realität zu studieren, die wir geschaffen haben.
-- Karl Rove zu Ron Suskind (NYT)

Sparen beginnt mit GMX DSL: http://www.gmx.net/de/go/dsl
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Changing (Unix) environment for python shell/popen() commands

2005-01-23 Thread Danny Yoo


On Sun, 23 Jan 2005, Scott W wrote:

> I've got to shell out from my python code to execute a command, but
> _must_ set the environment at the same time (or prior to execution).
>
> I saw some comments about setting os.environ[], but
> didn't seem to be seeing this work in subsequent calls using popen2().
>
> Does anyone have a working piece of code setting some env variable
> successfully prior to(or along with) calling popen2() (or friends).

Hi Scott,


Hmm... That should have worked.  We should be able to assign to the
os.environ dictionary: as I understand it, a child process can inherit its
parent's environment.  Let me check this:

###
volado:~ dyoo$ cat print_name_var.py
import os
print "I see", os.environ["NAME"]
volado:~ dyoo$
volado:~ dyoo$
volado:~ dyoo$
volado:~ dyoo$ python print_name_var.py
I see
Traceback (most recent call last):
  File "print_name_var.py", line 2, in ?
print "I see", os.environ["NAME"]
  File "/sw/lib/python2.3/UserDict.py", line 19, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 'NAME'
###

Ok, that's expected so far.


Let me see what happens if we try to set to os.environ.

###
volado:~ dyoo$ python
Python 2.3.3 (#1, Aug 26 2004, 23:05:50)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['name'] = 'Scott'
>>> os.popen('python print_name_var.py').read()
Traceback (most recent call last):
  File "print_name_var.py", line 2, in ?
print "I see", os.environ["NAME"]
  File "/sw/lib/python2.3/UserDict.py", line 19, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 'NAME'
'I see\n'
###


On thing we have to be careful of is case-sensitivity: it does matter in
the case of environmental variables.

###
>>> os.environ['NAME'] = 'Scott'
>>> os.popen('python print_name_var.py').read()
'I see Scott\n'
###

And now it works.




> Also, is there any way to get the called programs return code via
> popen() (again, under python 1.5.2)?


Hi Scott,

Yes; although it's a little hard to find it, the Python 1.52 documentation
here:

http://www.python.org/doc/1.5.2p2/

does have some documentation on popen2:

http://www.python.org/doc/1.5.2p2/lib/module-popen2.html

You can use a popen2.Popen3 instance, which should give you access to
program return values.


If you have more questions, please feel free to ask.

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


[Tutor] Print record x in a file

2005-01-23 Thread David Holland
 --- [EMAIL PROTECTED] wrote: 
> Send Tutor mailing list submissions to
>  tutor@python.org
> 
> To subscribe or unsubscribe via the World Wide Web,
> visit
>  http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
>  [EMAIL PROTECTED]
> 
> You can reach the person managing the list at
>  [EMAIL PROTECTED]
> 
> When replying, please edit your Subject line so it
> is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>1. Re: Print record x in a file (Kent Johnson)
>2. How would python messure up in performance?
> (Kevin)
>3. Re: How would python messure up in
> performance? (Max Noel)
>4. Re: Print record x in a file (David Holland)
>5. on the way to find pi (Ali Polatel)
>6. Re: on the way to find pi (Max Noel)
>7. Re: on the way to find pi (Orri Ganel)
> 
> 
>
--
> 
> Message: 1
> Date: Sun, 23 Jan 2005 07:46:08 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Print record x in a file
> Cc: tutor python 
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1;
> format=flowed
> 
>   Since len(listcontents) is one greater than the
> largest valid index of listcontents, the correct 
> use of randrange() for this problem is
>x = random.randrange(0, len(listcontents))
> 


Kent,

I know that you know far more about python than me but
is that right ?
I created file with 4 records and this code did print
them all randomly:-

import random
i = 0
while i < 10:
file = open('filename')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1


While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
file = open('test.rantxt')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)#-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Liam Clarke
Don't you mean 

x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.


On Sun, 23 Jan 2005 21:55:27 + (GMT), David Holland
<[EMAIL PROTECTED]> wrote:
>  --- [EMAIL PROTECTED] wrote:
> > Send Tutor mailing list submissions to
> >  tutor@python.org
> >
> > To subscribe or unsubscribe via the World Wide Web,
> > visit
> >  http://mail.python.org/mailman/listinfo/tutor
> > or, via email, send a message with subject or body
> > 'help' to
> >  [EMAIL PROTECTED]
> >
> > You can reach the person managing the list at
> >  [EMAIL PROTECTED]
> >
> > When replying, please edit your Subject line so it
> > is more specific
> > than "Re: Contents of Tutor digest..."
> >
> >
> > Today's Topics:
> >
> >1. Re: Print record x in a file (Kent Johnson)
> >2. How would python messure up in performance?
> > (Kevin)
> >3. Re: How would python messure up in
> > performance? (Max Noel)
> >4. Re: Print record x in a file (David Holland)
> >5. on the way to find pi (Ali Polatel)
> >6. Re: on the way to find pi (Max Noel)
> >7. Re: on the way to find pi (Orri Ganel)
> >
> >
> >
> --
> >
> > Message: 1
> > Date: Sun, 23 Jan 2005 07:46:08 -0500
> > From: Kent Johnson <[EMAIL PROTECTED]>
> > Subject: Re: [Tutor] Print record x in a file
> > Cc: tutor python 
> > Message-ID: <[EMAIL PROTECTED]>
> > Content-Type: text/plain; charset=ISO-8859-1;
> > format=flowed
> >
> >   Since len(listcontents) is one greater than the
> > largest valid index of listcontents, the correct
> > use of randrange() for this problem is
> >x = random.randrange(0, len(listcontents))
> >
> 
> 
> Kent,
> 
> I know that you know far more about python than me but
> is that right ?
> I created file with 4 records and this code did print
> them all randomly:-
> 
> import random
> i = 0
> while i < 10:
> file = open('filename')
> listcontents = file.readlines()
> file.close()
> lenoflist = len(listcontents)-1
> x = random.randrange(0,lenoflist)
> print listcontents[x], i
> i = i +1
> 
> While although this code below does work many times
> nothing is printed out.
> import random
> i = 0
> while i < 10:
> file = open('test.rantxt')
> listcontents = file.readlines()
> file.close()
> lenoflist = len(listcontents)#-1
> x = random.randrange(0,lenoflist)
> print listcontents[x], i
> i = i +1
> 
> 
> ___
> ALL-NEW Yahoo! Messenger - all new features - even more fun! 
> http://uk.messenger.yahoo.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Max Noel
On Jan 23, 2005, at 22:08, Liam Clarke wrote:
Don't you mean
x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.
	random.randrange() returns an item (which can be a float or whatever, 
but by default is an int) in the specified range. In that case, an int 
between 0 and lenoflist.
	The advantage over random.randint is that you can specify a step. Thus,
random.randrange(0, 257, 2) will return an even number between 0 and 
256 inclusive.

While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
file = open('test.rantxt')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)#-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1
	Anyway, the problem with this function is that len returns a number of 
items, but Python, like most good programming languages (and 
mathematicians), counts starting from 0. Thus, the first element in a 
list is foo[0].
	foo[len(foo)] will raise an IndexError.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Alan Gauld

> How well would a multi user texed based game created in just Python
> run if there were over 300 - 400 players (Just a hypathetical
> question) Would python be to slow to handle that amount of traffic?

That depends entirely on how it was designed and what kind 
of hardware you ran it on. If you had a big mainframe or 
similar you could run 3 users and it wouldn't be a 
problem. If you write a simple application and run it on 
an old 66MHZ 486PC then 3 users might be an issue!

We need a bit more info about what you have in mind I think.

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


Re: [Tutor] Print record x in a file

2005-01-23 Thread Kent Johnson
Max Noel wrote:
On Jan 23, 2005, at 22:08, Liam Clarke wrote:
Don't you mean
x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.

random.randrange() returns an item (which can be a float or 
whatever, but by default is an int) in the specified range. In that 
case, an int between 0 and lenoflist.
The advantage over random.randint is that you can specify a step. Thus,
random.randrange(0, 257, 2) will return an even number between 0 and 256 
inclusive.
Also the output of randint() is inclusive of both endpoints - with the 
above statement you will have
0 <= x <= lenoflist. So in that case you would want lenoflist = 
len(listcontents) - 1
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Print record x in a file

2005-01-23 Thread Kent Johnson
David Holland wrote:
Kent,
I know that you know far more about python than me but
is that right ?
I created file with 4 records and this code did print
them all randomly:-
import random
i = 0
while i < 10:
file = open('filename')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1
While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
file = open('test.rantxt')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)#-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1
It sounds like your file has a blank line at the end. What do you get if you print 
len(listcontents)? Is it 4 or 5? Where do you think 'nothing' is coming from?

Try this:
import random
listcontents = [1,2,3,4]
for i in range(10):
lenoflist = len(listcontents)-1
x = random.randrange(0,lenoflist)
print listcontents[x]
I never get a 4...
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Kevin
Well not sure how to answer that but I will try. For file I/O you
would have a seperat file for each character that would store
HP/MANA/MOVE points, store items that the character would have on.
Areas would be loaded from there own files with all the creatures and
items for that area in the same file. Spells would probably have there
own seperate file to load from. As far as unning the game it would be
run on a redhat linux server.


On Sun, 23 Jan 2005 13:06:47 +, Max Noel <[EMAIL PROTECTED]> wrote:
> 
> On Jan 23, 2005, at 12:55, Kevin wrote:
> 
> > How well would a multi user texed based game created in just Python
> > run if there were over 300 - 400 players (Just a hypathetical
> > question) Would python be to slow to handle that amount of traffic?
> 
>It depends... What would be happening in said game? What would it
> involve, calculation-wise? What would you be running it on?
> 
>In that kind of program, the core programming itself is rarely a
> bottleneck (IOW, Python should work just fine). The true elements on
> which your game's performance depend are:
> 1) Connection bandwidth and latency
> 2) Database access (especially if the DB is not on a remote server)
> 3) File I/O, if any
> 
>Could you give us a few more specs?
> 
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting
> and sweating as you run through my corridors... How can you challenge a
> perfect, immortal machine?"
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Max Noel
On Jan 23, 2005, at 23:57, Kevin wrote:
Well not sure how to answer that but I will try. For file I/O you
would have a seperat file for each character that would store
HP/MANA/MOVE points, store items that the character would have on.
Areas would be loaded from there own files with all the creatures and
items for that area in the same file. Spells would probably have there
own seperate file to load from. As far as unning the game it would be
run on a redhat linux server.
	That's a bizarre design choice (for the characters, I mean), this just 
screams Database, especially since you may find yourself with 
concurrent write accesses on some files (e.g. a player attacks another 
while the other is moving).

	Anyway. This looks like you'll be doing lots of file I/O, with a few 
calculations inbetween, right? Then unless your server is really 
shitty, Python should handle this just fine.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Kevin
Thanks for the advise. I am no ware near ready to do this as i am just
learning to use python. I just thought it would be interesting to see
a mud made in python rather then always in C or C++. So for now I am
only going to start out making small programs to learn with. So thanks
for answering the question.


On Mon, 24 Jan 2005 00:04:25 +, Max Noel <[EMAIL PROTECTED]> wrote:
> 
> On Jan 23, 2005, at 23:57, Kevin wrote:
> 
> > Well not sure how to answer that but I will try. For file I/O you
> > would have a seperat file for each character that would store
> > HP/MANA/MOVE points, store items that the character would have on.
> > Areas would be loaded from there own files with all the creatures and
> > items for that area in the same file. Spells would probably have there
> > own seperate file to load from. As far as unning the game it would be
> > run on a redhat linux server.
> 
>That's a bizarre design choice (for the characters, I mean), this just
> screams Database, especially since you may find yourself with
> concurrent write accesses on some files (e.g. a player attacks another
> while the other is moving).
> 
>Anyway. This looks like you'll be doing lots of file I/O, with a few
> calculations inbetween, right? Then unless your server is really
> shitty, Python should handle this just fine.
> 
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone, panting
> and sweating as you run through my corridors... How can you challenge a
> perfect, immortal machine?"
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Combination

2005-01-23 Thread Guillermo Fernandez Castellanos
Got it.

import copy

def rec(list,length):
result=[]
if len(list)<=length:
return [list]
for elem in list:
temp=copy.deepcopy(list)
temp.remove(elem)
temp=rec(temp,length)
for i in temp:
if i not in result:
result.append(i)
return result

>>> rec([1,2,3,4,5],3)
[[3, 4, 5], [2, 4, 5], [2, 3, 5], [2, 3, 4], [1, 4, 5], [1, 3, 5], [1,
3, 4], [1, 2, 5], [1, 2, 4], [1, 2, 3]]

The logic is:
1) If I take a list L of length l > n, the n-length subgroups will be
the union of all the n-length subgroups of all the possible lists of
length l-1 derived from L.
2) We keep unique copies of the results (don't wanna have several
times [1,3,5] for instance). Will be easier when I'll install Python
2,4 and have the sets.
3) We stop the recursion when we have a list of th length we desire

Well... of course, thanks to Kent I've finded a bunch of much better
implementations. I still did mine for personal pleasure :-)

Thanks to all,

Guille

On Fri, 21 Jan 2005 12:44:54 -0800 (PST), Danny Yoo
<[EMAIL PROTECTED]> wrote:
> 
> 
> On Fri, 21 Jan 2005, Danny Yoo wrote:
> 
> > > I mean:
> > > if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find:
> > > (1,2,3) but then not (2,1,3), (3,1,2),...
> > > (1,2,4)
> > > (1,2,5)
> > > (2,3,5)
> > > (3,4,5)
> >
> >
> > There is a clean recursive way to define this.
> 
> Hi Guillermo,
> 
> Gaaa; I screwed up slightly.  *grin*
> 
> I just wanted to clarify: the function that I'm sketching out is not
> exactly the function that you're thinking of.  I'm doing more of a "give
> me all the subsets of L" kind of thing.
> 
> But if you can understand how to get all the subsets, you should be able
> to figure out how to get all the combinations of 'k' elements, because
> they're really similar problems.
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How would python messure up in performance?

2005-01-23 Thread Chad Crabtree
Kevin wrote:

>Thanks for the advise. I am no ware near ready to do this as i am
just
>
>learning to use python. I just thought it would be interesting to
see
>
>a mud made in python rather then always in C or C++. So for now I am
>
>only going to start out making small programs to learn with. So
thanks
>
>for answering the question.
>  
>
There are several efforts made in mud's in python on sourceforge. 
Check 
them out, you can read thier source.

__
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