Re: [Tutor] Table of replacements for deprecated fns from 'string'module?

2007-06-14 Thread Alan Gauld

"Stephen McInerney" <[EMAIL PROTECTED]> wrote

> Where is there a table of replacements for the deprecated 'string' 
> fns

I'm not sure exactly what you are after but comparing these lists 
might help:

import string
dir(string)
['Template', '_TemplateMetaclass', '__builtins__', '__doc__', 
'__file__', '__name__', '_float', '_idmap', '_idmapL', '_int', 
'_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 
'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 
'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 
'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 
'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 
'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 
'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 
'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 
'whitespace', 'zfill']

dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', 
'__eq__', '__ge__', '__getattribute__', '__getitem__', 
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', 
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', 
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 
'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 
'zfill']

Notice the long list of predicate methods that largely replace the
idiom of looking for a match in the string constants. ie instead of:

if char in string.lowercase:

you can use

if char.islower():

The only missing function I can see is maketrans().
(capwords seems to be replaced with title, and the type
conversions are now in their type objects)

The list of predicates is not complete however.

But of course the string objects add a number of extra methods
too. (eg startwith, endswith, and a bunch of operators etc)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Componentone - TrueDBGrid Control

2007-06-14 Thread Alan Gauld
"Pradeep Kumar" <[EMAIL PROTECTED]> wrote 

> Is wxPython have Componentone Trudbgrid like control. If yes,
> Please inform. if No, how we can made one.

I've no idea what a Trudbgrid is but assuming it's a bit like 
the live data grids used in Delphi then the answer is no, 
not as far as I can tell. However is an approximation in 
the form of a wx.grid.PyGridTableBase which you can 
subclass to connect to a database and then connect 
to a grid object using grid.SetTable()

Probably better asking on the wxPython mailing list 
though, they are more expert in wxPython matters.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


[Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread emilia12
hi list,

how to choose between "#!/usr/bin/env python" and
"#!/usr/local/bin/python" in the beginning of the script ?
e.



-

SCENA - Ĺäčíńňâĺíîňî ÁĹÇĎËŔŇÍÎ ńďčńŕíčĺ çŕ ěîáčëíč ęîěóíčęŕöčč č ňĺőíîëîăčč.
http://www.bgscena.com/

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


Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Thorsten Kampe
*  (Thu, 14 Jun 2007 13:14:13 +0300)
> how to choose between "#!/usr/bin/env python" and
> "#!/usr/local/bin/python" in the beginning of the script ?

Just choose. Say "I want" to the script. Say "I want '#!/usr/bin/env 
python'"

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


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

2007-06-14 Thread Andy Cheesman
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


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

2007-06-14 Thread Hugh M

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) 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


Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Ezra Taylor

I think Emilia means what's the difference.  From what little I know,
#!/usr/bin/env python will choose the first python that's in your path.
Were as the second option, you explicitly choose which instance of python
you want.  I'm using using python from Activestate.  So my shebang is to the
Activestate directory for python.  If I'm wrong, please correct.

Ezra

On 6/14/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:


hi list,

how to choose between "#!/usr/bin/env python" and
"#!/usr/local/bin/python" in the beginning of the script ?
e.



-

SCENA - Ĺäčíńňâĺíîňî ÁĹÇĎËŔŇÍÎ ńďčńŕíčĺ çŕ ěîáčëíč ęîěóíčęŕöčč č
ňĺőíîëîăčč.
http://www.bgscena.com/

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





--
Ezra Taylor
___
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


[Tutor] Certificate creation.

2007-06-14 Thread Lance Haig
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi All,

Is there a way to create ssl certificates with python and GNUTLS?

I am part of a project http://www.bongo-project.org which is a mail and
calendering solution. We use python in our project to a large extent but
we have a problem with the ssl key generation using GNUTLS.

Some machines take days to create enough random data to create the
certificates because they are headless and don't have key input for us
to capture.

We currently use a python script to run our set-up program and this in
turn creates the certificates.

Is there another way to create certificates from a python script that
will speed things up for these types of machines?

I would appreciate any help on this thanks

Regards

Lance
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFGcUsXOw09RVRgt9wRAgfkAJ993ha5e38OS5YAc0xAaDg7GMGxRgCgnfCR
j6oeFdWInj4xp2fIloiQxfM=
=TlaD
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread David Duncan

On 6/14/07, Ezra Taylor <[EMAIL PROTECTED]> wrote:


I think Emilia means what's the difference.  From what little I know,
#!/usr/bin/env python will choose the first python that's in your path.
Were as the second option, you explicitly choose which instance of python
you want.  I'm using using python from Activestate.  So my shebang is to the
Activestate directory for python.  If I'm wrong, please correct.

Ezra

On 6/14/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> hi list,
>
> how to choose between "#!/usr/bin/env python" and
> "#!/usr/local/bin/python" in the beginning of the script ?
> e.
>
>
>
> -
>
> SCENA - Ĺäčíńňâĺíîňî ÁĹÇĎËŔŇÍÎ ńďčńŕíčĺ çŕ ěîáčëíč ęîěóíčęŕöčč č
> ňĺőíîëîăčč.
> http://www.bgscena.com/
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



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



The real difference here is that by using the "env"  command, you have the
option to input many different settings preferences before calling the
python of your choice.  The statement above, is a very simple choice of the
python based upon the current environment, but it could be augmented very
easily.

I suggest that you check out the man page for the command "env".

--
David Duncan

Registered Linux User #279425
http://counter.li.org
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Senthil_OR
Okay, I guess, people are missing points here. 
 
When do you
 
#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that 
rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python
 
Consider the possiblities that in a different machine, python may be installed 
at /usr/bin/python or /bin/python in those cases, the above #! will fail.
For those cases, we get to call the env executable with argument which will 
determine the arguments path by searching in the $PATH and use it correctly.
 
Thus,
#/usr/bin/env python
Will figure out the correct location of python ( /usr/bin/python or /bin/python 
from $PATH) and make that as the interpreter for rest of the script.
- ( env is almost always located in /usr/bin/ so one need not worry what is env 
is not present at /usr/bin)
 
Hope this helps.
 
-- 
Senthil


The price of seeking to force our beliefs on others is that someday they might 
force their beliefs on us. -- Mario Cuomo 
 



From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Duncan
Sent: Thursday, June 14, 2007 19:44
To: tutor@python.org
Subject: Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"




On 6/14/07, Ezra Taylor <[EMAIL PROTECTED]> wrote: 

I think Emilia means what's the difference.  From what little I know, 
#!/usr/bin/env python will choose the first python that's in your path.  Were 
as the second option, you explicitly choose which instance of python you want.  
I'm using using python from Activestate.  So my shebang is to the Activestate 
directory for python.  If I'm wrong, please correct. 

Ezra 



On 6/14/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: 

hi list,

how to choose between "#!/usr/bin/env python" and
"#!/usr/local/bin/python" in the beginning of the script ? 
e.



-

SCENA - Ĺäčíńňâĺíîňî ÁĹÇĎËŔŇÍÎ ńďčńŕíčĺ çŕ ěîáčëíč ęîěóíčęŕöčč 
č ňĺőíîëîăčč.
http://www.bgscena.com/

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





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




The real difference here is that by using the "env"  command, you have the 
option to input many different settings preferences before calling the python 
of your choice.  The statement above, is a very simple choice of the python 
based upon the current environment, but it could be augmented very easily. 

I suggest that you check out the man page for the command "env".  

-- 
David Duncan

Registered Linux User #279425
http://counter.li.org 
___
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] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread wesley chun
> Okay, I guess, people are missing points here.
>
> #!/usr/local/bin/python
> You are specifying the location to the python executable in your machine,
> that rest of the script needs to be interpreted with.
> You are pointing to python is located at /usr/local/bin/python
>
> Consider the possiblities that in a different machine, python may be
> installed at /usr/bin/python or /bin/python in those cases, the above #!
> will fail.
> For those cases, we get to call the env executable with argument which will
> determine the arguments path by searching in the $PATH and use it correctly.
>
> Thus,
> #/usr/bin/env python
> Will figure out the correct location of python ( /usr/bin/python or
> /bin/python from $PATH) and make that as the interpreter for rest of the
> script.
> - ( env is almost always located in /usr/bin/ so one need not worry what is
> env is not present at /usr/bin)


senthil is correct in this regard.  the bottom line is that the one
with env is more flexible and will run on more systems and more types
of systems because it uses the "env" command to find where python is
located and executes it.

the other one simply will *not* work if python is not installed in
that exact location.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Finding all locations of a sequence

2007-06-14 Thread Lauren
Ok, please bear with me, I'm very new to programming and python. And
my question is rather...convoluted.

I have a bunch of sequences (about 4100 or so), and want to know where
they are in a very, very large string of letters. But wait, there's
more. Some of these sequences match to more than 'word' (for
example...to be consistent with later, chicken could match to poultry
or chicken).

example of what I want to do (on a much smaller scale):

Say I have chicken and I want to know where it occurs in a string of
words, but I want it to match to both chicken and poultry and have the
output of:

chicken  (locations of chicken and poultry in the string)

or something like that.

The string I'm dealing with is really large, so whatever will get
through it the fastest is ideal for me.

I hope this all makes sense...

If it's possible to have pseudocode that would be helpful.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get max quantity

2007-06-14 Thread Kent Johnson
Brian Wisti wrote:
> Hi Carlos,
> 
> On 6/13/07, Carlos <[EMAIL PROTECTED]> wrote:
>> Hello,
>>
>> If I have a dictionary like:
>>
>> inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
>>
>> How can I get the item with the largest quantity? I tried:
>>
>> max(inventory)
>>
>> but got:
>>
>> 'pears'
>>
>> What I would like to get is 'oranges', at least in this case.
>>
>> Thanks,
>> Carlos
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> 
> There are indeed several ways to sort this particular cat. Here's my
> own favorite:
> 
> # Sort the list of keys by inventory count, from high to low
 inventory
> {'pears': 217, 'apples': 430, 'oranges': 525, 'bananas': 312}
 items = inventory.keys()
 items
> ['pears', 'apples', 'oranges', 'bananas']
 items.sort(cmp=lambda a,b: cmp(inventory[b], inventory[a]))
 items
> ['oranges', 'apples', 'bananas', 'pears']
 items[0]
> 'oranges'

You should learn how to use the key parameter to sort, it is much more 
efficient than using cmp and IMO easier to write and understand. In this 
case, you could use key=inventory.__getitem__

> 
> It does result in another list, same as the the approaches listed by
> Kent and Jason. If *all* you were interested was the key associated
> with the greatest inventory count, you could wrap your favorite
> solution in a function and return the key from that.

Neither of my original suggestions created another list. This is 
actually a significant difference between my solutions using max() and 
yours and Jason's using sort. If the list is long using max() could be 
much more efficient because it just makes a single pass through the 
list. max() has O(n) complexity whereas in general sort is O(n log n) so 
as n gets large the advantage of max() should increase. Also for large n 
there should be an advantage to not creating the intermediate list.

As always, if you care about the time taken by an operation, measure 
(with timeit); I am just making educated guesses.

Kent
> 
> Kind Regards,
> 
> Brian Wisti
> http://coolnamehere.com/
> ___
> 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] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Rolando Pereira
[EMAIL PROTECTED] escreveu:
> Okay, I guess, people are missing points here. 
>  
> When do you
>  
> #!/usr/local/bin/python
> You are specifying the location to the python executable in your machine, 
> that rest of the script needs to be interpreted with.
> You are pointing to python is located at /usr/local/bin/python
>  
> Consider the possiblities that in a different machine, python may be 
> installed at /usr/bin/python or /bin/python in those cases, the above #! will 
> fail.
> For those cases, we get to call the env executable with argument which will 
> determine the arguments path by searching in the $PATH and use it correctly.
>  
> Thus,
> #/usr/bin/env python
> Will figure out the correct location of python ( /usr/bin/python or 
> /bin/python from $PATH) and make that as the interpreter for rest of the 
> script.
> - ( env is almost always located in /usr/bin/ so one need not worry what is 
> env is not present at /usr/bin)
>  
> Hope this helps.
>  
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

I've always used #!/usr/bin/python...

Perhaps I should use the "#!/usr/bin/env python" one.

-- 
_
ASCII ribbon campaign ( )
  - against HTML email  X
  & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding all locations of a sequence

2007-06-14 Thread Teresa Stanton
OK, I'm going to take a shot at this.  If what I'm understanding is correct,
a dictionary might help.  But that would depend on the format of the
original sequence.  If you have a list:

Lst1 = ['cow', 'pig', 'chicken', 'poultry', 'beef', 'pork']

Then you could:

Lst1.index('chicken')

And get 2, because the list starts with 0,  not 1, as the first index.

Or this:

>>> for i in Lst1:
if i == 'chicken':
print Lst1.index(i)
if i == 'poultry':
print Lst1.index(i)


2
3

Now, Kent or Alan and perhaps others will have a much more sophisticated way
of doing this same problem.  I'm still not exactly sure what it is you are
looking for, because there isn't enough information for me to really get a
grasp on your problem. My response is a simple list structure that has
simple operations.

Hope it helps :)

T

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Lauren
Sent: Thursday, June 14, 2007 11:35 AM
To: tutor@python.org
Subject: [Tutor] Finding all locations of a sequence

Ok, please bear with me, I'm very new to programming and python. And
my question is rather...convoluted.

I have a bunch of sequences (about 4100 or so), and want to know where
they are in a very, very large string of letters. But wait, there's
more. Some of these sequences match to more than 'word' (for
example...to be consistent with later, chicken could match to poultry
or chicken).

example of what I want to do (on a much smaller scale):

Say I have chicken and I want to know where it occurs in a string of
words, but I want it to match to both chicken and poultry and have the
output of:

chicken  (locations of chicken and poultry in the string)

or something like that.

The string I'm dealing with is really large, so whatever will get
through it the fastest is ideal for me.

I hope this all makes sense...

If it's possible to have pseudocode that would be helpful.
___
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] Finding all locations of a sequence

2007-06-14 Thread Paulo Nuin
Hi Lauren

You can use two approaches:

 1- String method find

This returns a int value with the lowest position of your search on the 
string (sequence) you are searching. From the documentation:

*find*( sub[, start[, end]])
Return the lowest index in the string where substring sub is found, such 
that sub is contained in the range [start, end]. Optional arguments 
start and end are interpreted as in slice notation. Return |-1| if sub 
is not found.
Example:

position = sequence1.find('chicken')

If your search is not found on your sequence, it will return -1.
You put this in a while loop and you can then search for all occurrences 
of your search string.

2- Use regular expression
You have to import the re module and use the finditer method. The 
finditer method will return the iterator of your regular expression 
matches. Basically you will have to compile a regular expression

mysearch = re.compile('chicken')

and the use the finditer method:

iterator = mysearch.finditer(sequence1)

And then you use a loop to return all the matches

for match in iterator:
... print match.span()

Each span object is a pair of positions (begin and end) 
of your regular expression matches in the sequence. 
To know more about regex check this page

http://www.amk.ca/python/howto/regex/

HTH

Paulo









Lauren wrote:
> Ok, please bear with me, I'm very new to programming and python. And
> my question is rather...convoluted.
>
> I have a bunch of sequences (about 4100 or so), and want to know where
> they are in a very, very large string of letters. But wait, there's
> more. Some of these sequences match to more than 'word' (for
> example...to be consistent with later, chicken could match to poultry
> or chicken).
>
> example of what I want to do (on a much smaller scale):
>
> Say I have chicken and I want to know where it occurs in a string of
> words, but I want it to match to both chicken and poultry and have the
> output of:
>
> chicken  (locations of chicken and poultry in the string)
>
> or something like that.
>
> The string I'm dealing with is really large, so whatever will get
> through it the fastest is ideal for me.
>
> I hope this all makes sense...
>
> If it's possible to have pseudocode that would be helpful.
> ___
> 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] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Bill Campbell
On Thu, Jun 14, 2007, Rolando Pereira wrote:
>[EMAIL PROTECTED] escreveu:
>> Okay, I guess, people are missing points here. 
>>  
>> When do you
>>  
>> #!/usr/local/bin/python
>> You are specifying the location to the python executable in your machine, 
>> that rest of the script needs to be interpreted with.
>> You are pointing to python is located at /usr/local/bin/python
>>  
>> Consider the possiblities that in a different machine, python may be 
>> installed at /usr/bin/python or /bin/python in those cases, the above #! 
>> will fail.
>> For those cases, we get to call the env executable with argument which will 
>> determine the arguments path by searching in the $PATH and use it correctly.
>>  
>> Thus,
>> #/usr/bin/env python
>> Will figure out the correct location of python ( /usr/bin/python or 
>> /bin/python from $PATH) and make that as the interpreter for rest of the 
>> script.
>> - ( env is almost always located in /usr/bin/ so one need not worry what is 
>> env is not present at /usr/bin)
>>  
...
>I've always used #!/usr/bin/python...
>
>Perhaps I should use the "#!/usr/bin/env python" one.

The case where ``#!/usr/bin/env python'' won't work is where
there are multiple versions of python on the system, and one
wants to run a version that's not first in the PATH.  In that
case one needs to put the full path to the proper executable.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``The trouble with fighting for human freedom is that one spends most of
one's time defending scoundrels. For it is against scoundrels that
oppressive laws are first aimed, and oppression must be stopped at the
beginning if it is to be stopped at all.'' -- H. L. Mencken
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding all locations of a sequence

2007-06-14 Thread Lauren
Ok, what I have is a RNA sequence (about 5 million nucleotides
[characters] long) and have (4100) subsequences (from another
sequence) and the sub-sequences are 6 characters long, that I want to
find in it.
The problem is the exceptional bond of U:G, which results in U bonding
to A (as per normal) and G (the abnormal bond) and G to bond with C
(as per normal) and U. Normally I'd go to search software already
available, however what I need done isn't covered b y anything out
there so far. That and I believe that they do not deal with the
exceptional bond. In any event, I want to know where the subsequences
can bind in the larger RNA sequence (ie, the location of binding in
the larger sequence) so I'm not (just) for where they would bind
normally, but also where the abnormal bonds would figure in.
Unfortunately my first attempt at this was unbearably slow, so I'm
hoping there is a faster way.

So an example with this would be:

Subseq  AU can bind to UA (which is normal) and UG (not so
normal) and I want to know where UA, and UG are in the large
RNA sequence, and the locations to show up as one...thing.

I don't know if that is more helpful or not than the chicken example...

Thanks again for the help


On 14/06/07, Teresa Stanton <[EMAIL PROTECTED]> wrote:
> OK, I'm going to take a shot at this.  If what I'm understanding is correct,
> a dictionary might help.  But that would depend on the format of the
> original sequence.  If you have a list:
>
> Lst1 = ['cow', 'pig', 'chicken', 'poultry', 'beef', 'pork']
>
> Then you could:
>
> Lst1.index('chicken')
>
> And get 2, because the list starts with 0,  not 1, as the first index.
>
> Or this:
>
> >>> for i in Lst1:
> if i == 'chicken':
> print Lst1.index(i)
> if i == 'poultry':
> print Lst1.index(i)
>
>
> 2
> 3
>
> Now, Kent or Alan and perhaps others will have a much more sophisticated way
> of doing this same problem.  I'm still not exactly sure what it is you are
> looking for, because there isn't enough information for me to really get a
> grasp on your problem. My response is a simple list structure that has
> simple operations.
>
> Hope it helps :)
>
> T
>
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
> Of Lauren
> Sent: Thursday, June 14, 2007 11:35 AM
> To: tutor@python.org
> Subject: [Tutor] Finding all locations of a sequence
>
> Ok, please bear with me, I'm very new to programming and python. And
> my question is rather...convoluted.
>
> I have a bunch of sequences (about 4100 or so), and want to know where
> they are in a very, very large string of letters. But wait, there's
> more. Some of these sequences match to more than 'word' (for
> example...to be consistent with later, chicken could match to poultry
> or chicken).
>
> example of what I want to do (on a much smaller scale):
>
> Say I have chicken and I want to know where it occurs in a string of
> words, but I want it to match to both chicken and poultry and have the
> output of:
>
> chicken  (locations of chicken and poultry in the string)
>
> or something like that.
>
> The string I'm dealing with is really large, so whatever will get
> through it the fastest is ideal for me.
>
> I hope this all makes sense...
>
> If it's possible to have pseudocode that would be helpful.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
>


-- 
Lauren

[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding all locations of a sequence

2007-06-14 Thread Paulo Nuin
Hi Lauren

I use the find string method to search DNA motifs. Here is an example

while sp < len(fasta[j].sequence):
pos = string.find(fasta[j].sequence, 
motif[i], sp)
if pos != -1 and pos > 0:
plist.append(int(size) - pos)
mlist.append(toprint)
sp = pos
else:
sp = len(fasta[j].sequence)-1
sp+=1
pos = 0
sp = 0

You might even be able to trim a bit this code, but it is a start.

HTH

Paulo



Lauren wrote:
> Ok, what I have is a RNA sequence (about 5 million nucleotides
> [characters] long) and have (4100) subsequences (from another
> sequence) and the sub-sequences are 6 characters long, that I want to
> find in it.
> The problem is the exceptional bond of U:G, which results in U bonding
> to A (as per normal) and G (the abnormal bond) and G to bond with C
> (as per normal) and U. Normally I'd go to search software already
> available, however what I need done isn't covered b y anything out
> there so far. That and I believe that they do not deal with the
> exceptional bond. In any event, I want to know where the subsequences
> can bind in the larger RNA sequence (ie, the location of binding in
> the larger sequence) so I'm not (just) for where they would bind
> normally, but also where the abnormal bonds would figure in.
> Unfortunately my first attempt at this was unbearably slow, so I'm
> hoping there is a faster way.
>
> So an example with this would be:
>
> Subseq  AU can bind to UA (which is normal) and UG (not so
> normal) and I want to know where UA, and UG are in the large
> RNA sequence, and the locations to show up as one...thing.
>
> I don't know if that is more helpful or not than the chicken example...
>
> Thanks again for the help
>
>
> On 14/06/07, Teresa Stanton <[EMAIL PROTECTED]> wrote:
>   
>> OK, I'm going to take a shot at this.  If what I'm understanding is correct,
>> a dictionary might help.  But that would depend on the format of the
>> original sequence.  If you have a list:
>>
>> Lst1 = ['cow', 'pig', 'chicken', 'poultry', 'beef', 'pork']
>>
>> Then you could:
>>
>> Lst1.index('chicken')
>>
>> And get 2, because the list starts with 0,  not 1, as the first index.
>>
>> Or this:
>>
>> 
> for i in Lst1:
>   
>> if i == 'chicken':
>> print Lst1.index(i)
>> if i == 'poultry':
>> print Lst1.index(i)
>>
>>
>> 2
>> 3
>>
>> Now, Kent or Alan and perhaps others will have a much more sophisticated way
>> of doing this same problem.  I'm still not exactly sure what it is you are
>> looking for, because there isn't enough information for me to really get a
>> grasp on your problem. My response is a simple list structure that has
>> simple operations.
>>
>> Hope it helps :)
>>
>> T
>>
>> -Original Message-
>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
>> Of Lauren
>> Sent: Thursday, June 14, 2007 11:35 AM
>> To: tutor@python.org
>> Subject: [Tutor] Finding all locations of a sequence
>>
>> Ok, please bear with me, I'm very new to programming and python. And
>> my question is rather...convoluted.
>>
>> I have a bunch of sequences (about 4100 or so), and want to know where
>> they are in a very, very large string of letters. But wait, there's
>> more. Some of these sequences match to more than 'word' (for
>> example...to be consistent with later, chicken could match to poultry
>> or chicken).
>>
>> example of what I want to do (on a much smaller scale):
>>
>> Say I have chicken and I want to know where it occurs in a string of
>> words, but I want it to match to both chicken and poultry and have the
>> output of:
>>
>> chicken  (locations of chicken and poultry in the string)
>>
>> or something like that.
>>
>> The string I'm dealing with is really large, so whatever will get
>> through it the fastest is ideal for me.
>>
>> I hope this all makes sense...
>>
>> If it's possible to have pseudocode that would be helpful.
>> ___
>> 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] Finding all locations of a sequence

2007-06-14 Thread Terry Carroll
On Thu, 14 Jun 2007, Lauren wrote:

> Subseq  AU can bind to UA (which is normal) and UG (not so
> normal) and I want to know where UA, and UG are in the large
> RNA sequence, and the locations to show up as one...thing.

How about something like this?



def seqsearch(seq, targets):
   """
   return a list of match objects, each of which identifies where any of
   the targets are found in the string seq
seq: string to be searched
targets: list or tuple of alternate targets to be searched

   note: re.findall is not used, because it wont catch overlaps
   """
   
   import re
   resultlist=[]
   pos=0
   regext_text = "|".join(targets)
   regex = re.compile(regext_text)
   while True:
  result = regex.search(seq, pos)
  if result is None:
 break
  resultlist.append(result)
  pos = result.start()+1
   return resultlist

targets = ["UA", "UG"]
sequence="UUCAAUUUGATACCAUAGCUUCCGUGCGATACCAAGCGU"
#++   ++
# 0 1 2 3 4 5
# 012345678901234567890123456789012345678901234567890
# note: matches at 15 & 28
matches = seqsearch(sequence, targets)
for m in matches:
   print "match %s found at location %s" % (sequence[m.start():m.end()],
m.start()) 


This prints, as expected:

match UA found at location 15
match UG found at location 28

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


Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Alan Gauld
>From the welter of posts, coming back to the original,
let's summarise:

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> how to choose between "#!/usr/bin/env python" and
> "#!/usr/local/bin/python" in the beginning of the script ?

Use env if you want maximum flexibility across
different machines. It will find where the first python
in the PATH lives and use it.

Use a hard path if you have multiple pythons installed
and it matters which one is used. But be aware that
a hard path may not work on another machine
or OS version.

And neither makes any difference on a non-Unix
based OS. (I count cygwin as being Unix based)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Finding all locations of a sequence

2007-06-14 Thread Alan Gauld
"Lauren" <[EMAIL PROTECTED]> wrote

Caveat: I am not into the realms of DNA sequencing so this may
not be viable but...

> Say I have chicken and I want to know where it occurs in a string of
> words, but I want it to match to both chicken and poultry and have 
> the
> output of:
>
> chicken  (locations of chicken and poultry in the string)

When searching for more than one pattern at a time I'd go
for a regex. A simple string search is faster on its own but
a single regex search will typically be faster than a repeated
string search.

For the simple case above a search for  (chicken)|(poultry)
should work:

>>> import re
>>> s = ''' there are a lot of chickens in my poultry farm but
... very few could be called a spring chicken'''
...
>>> regex = '(chicken)|(poultry)'
>>> r = re.compile(regex)
...
>>> r.findall(s)
...
[('chicken', ''), ('', 'poultry'), ('chicken', '')]
>>> [match for match in r.finditer(s)]
[<_sre.SRE_Match object at 0x01E75920>, <_sre.SRE_Match object at 
0x01E758D8>, <_sre.SRE_Match object at 0x01E75968>]
>>>

The match objects will let you find the location in the original
string which I suspect you will need?

> The string I'm dealing with is really large, so whatever will get
> through it the fastest is ideal for me.

Again I expect a regex to be fastest for multiple seach criteria
over a single pass. Now what your regex will look like for
R/DNA sequences I have no idea, but if you can describe it I'm
sure somebody here can help formulate a suitable pattern

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