Re: [Tutor] get_python_lib()

2008-01-15 Thread Vishnu Mohan
Kakada wrote:
> Hi list,
>
> I was just wondering why the below function return different location on 
> different machine?
>
> import distutils.sysconfig
> distutils.sysconfig.get_python_lib()
> On my computer:
> '/usr/lib/python2.5/site-packages'
> On my friend's computer:
> '/usr/local/lib/python2.5/site-packages'
>
> What is the difference between these two? Note: I have both location on my 
> filestem.
>   

Most of the packages will install itself, by default, into 
/usr/local/lib/python$VER/site-packages/(third-party).
But most distibutions will put their software into 
/usr/lib/python$VER/site-packages/. May be your friend might
have changed $PYTHONHOME like env's so that it is pointing to /usr/local 
or while installing the python he might have
set --prefix to the /usr/local/

-VishnuMohan, Montalvo Systems.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with while loop

2007-09-10 Thread Vishnu Mohan

> Now I just need to figure out how to only get 4 digit pin numbers :)
>
Use regular expressions
The following is the code with re.


from random import randint
import re

counter = 0
pinPattern = re.compile(r'^\d{4}$')
howmany = raw_input( "How many: " )
if pinPattern.match(howmany):
while counter < int(howmany):
pin = randint(,)
print pin
counter += 1
else:
print "%s is not valid 4 digit integer"%howmany


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


Re: [Tutor] Automatic generation of an "all possible combinations" array

2007-06-14 Thread Vishnu Mohan
Another simplest way of doing it  is

 >>>
from random import *
from sys import *

def bin_list(n):
bin_val = 0
if n >= 0:
bin_val = 2**n - 1
list = []
while bin_val >= 0:
list.append([((bin_val >> y) & 1) for y in range(n-1,-1,-1)])
bin_val -= 1
shuffle(list)
return list
 >>>
bin_list(3) gives output as
   [ [0, 1],
 [1, 1],
 [0, 0],
 [1, 0] ]
Output list of patterns is random, we get different patterns for next run.

-VishnuMohan



Hugh M wrote:
> The 2**n different lists that you are seeking have a direct 
> association to the binary representation of the integers 0 through 
> (2**n)-1.
>
> You can use this fact and the "repeated division method" for 
> converting numbers between different bases to generate these lists and 
> form the desired list of lists:
>
> def bit_list_maker(n):
> x = 2**n
> solution_set = []
> for i in range(x):
> this_answer = []
> while i>0:
> this_answer.append(i%2)
> i=i/2
> while len(this_answer) this_answer.append(0)
> this_answer.reverse()
> solution_set.append(this_answer)
> return solution_set
> *
> *
> Another fun way to do it is to build up the lists recursively.  The 
> possibilities for n bits can be built from the possibilities for n-1 
> bits by adding a 1 and a 0 to each possibility (ending up with twice 
> as many elements):
>
> def recursive_bit_list(n):
> if n==1:
> return [[0],[1]]
> else:
> return map(lambda x: x+[0], recursive_bit_list(n-1)) + \
>map(lambda x: x+[1], recursive_bit_list(n-1))
>
> Hope this helps!
>
> -Hugh
>
>  
> On 6/14/07, *Andy Cheesman* <[EMAIL PROTECTED] 
> > wrote:
>
> Hi people
>
> I am trying to generate an array of all possible combinations of
> 1, and
> zeros (see example data) for a rather nice Kinetic mote Carlo program
> which I am writing python. So far, I've been working out for
> combinations for 4 or less species by hand as it is quick! but I am
> looking to automate the process so I can compute combinations for
> large
>   numbers of possible species.
> I could automate the generation of the array by the use of multiple
> loops but that doesn't seem rather pythonic. I was wondering if anyone
> had any sensible suggestions or pointers for efficient mechanisms for
> the array.
>
> Many Thanks
> Andy
>
> Example Data
> 3 species
> array([[1, 1, 1],
>[1, 1, 0],
>[1, 0, 1],
>[0, 1, 1],
>[1, 0, 0],
>[0, 1, 0],
>[0, 0, 1],
>[0, 0, 0]])
> 4 species
> array([[1, 1, 1, 1],
>[0, 1, 1, 1],
>[1, 0, 1, 1],
>[1, 1, 0, 1],
>[1, 1, 1, 0],
>[1, 1, 0, 0],
>[1, 0, 1, 0],
>[1, 0, 0, 1],
>[0, 1, 1, 0],
>[0, 1, 0, 1],
>[0, 0, 1, 1],
>[1, 0, 0, 0],
>[0, 1, 0, 0],
>[0, 0, 1, 0],
>[0, 0, 0, 1],
>[0, 0, 0, 0]])
>
> ___
> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] locking files

2007-06-14 Thread Vishnu Mohan

You can look into the flock or lockf  methods in fcntl module
(or)
the following link will help you

http://www.python.org/doc/2.4/lib/module-fcntl.html

VishnuMohan


Alan Gauld wrote:

"Jeff Peery" <[EMAIL PROTECTED]> wrote

  
does anyone know if there is a way in python to lock 
a file so others cannot open it, or can only read it? 



Thats normally controlled by your OS and the rules vary 
slightly between them. In general if you open a file for 
writing the OS should lock it against writing (and in 
some cases against reading too!). So if you want to 
lock an existing file while you read and write you could use 
a mode string of 'r+'. If you want to create a new locked 
file use 'w+''. BUT remember that its up to you to manage 
where the cursor is, otherwise you could wind up overwriting 
your data.


Another way to protect access, but less reliable, is 
to change the file permissions (using os.chmod()) 
at the start of the operation and change themback at 
the end. But that will only protect ahainst access by 
other users not against access by other programs that 
you might be running! Also you need to have 
permission to change permissions in the first place!


HTH,


  


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


Re: [Tutor] Tutor Digest, Vol 40, Issue 25

2007-06-12 Thread Vishnu Mohan

>
> Message: 2
> Date: Sun, 10 Jun 2007 09:20:28 -0500
> From: David Hamilton <[EMAIL PROTECTED]>
> Subject: [Tutor] Correct use of range function..
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I just finished doing an exercise in a tutorial on the range function 
> and while I got it to work, my answer seems ugly. I'm wondering if I'm 
> missing something in the way I'm using the range function.
> The tutorial ask me to print a string backwards. My solution works, but 
> it it just doesn't "feel" right :).  My result is difficult to read and 
> I feel like I'm probably over complicating the solution. Suggestions?
>
> word="reverse"
> #Start at the end of the string, count back to the start, printing each 
> letter
> for  i in range(len(word)-1,-1,-1):
> print word[i],
>
>   


Another best way of doing it is:

word = 'reverse'
rev= ''
for i in range(len(word)):
 print word[-(i+1)]
 rev += word[-(i+1)]
print rev



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