Re: [Tutor] for _ in

2010-03-09 Thread spir
On Tue, 9 Mar 2010 11:25:19 +0800
Joson  wrote:

> if labels is None: labels = [OrderedSet() for _ in xrange(ndim)]
> ..
> 
> It's a library named "divisi". "OrderedSet" is a string set defined in this
> lib. "ndim" is integer.  What's the meaning of "for _ in" ?
> 
> Joson

   [OrderedSet() for _ in xrange(n)]

builds a list of ndim empty ordered sets. '_' is the index, but is unused; '_' 
is commonly used as required, but unused, variable name (not only in python).
This is just a tricky python idiom, because there is no builtin syntax to do 
this.
   [OrderedSet()] * ndim
would build a list with ndim times the *same* set, so that if one item is 
changed, all are changed. The above syntax instread creates *distinct* sets.

Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> lists = [list()] *3
>>> lists
[[], [], []]
>>> list1 = lists[1]
>>> list1.append('*')
>>> lists
[['*'], ['*'], ['*']]

>>> lists = [list() for _ in range(3)]  # _ is unused
>>> lists
[[], [], []]
>>> list1 = lists[1]
>>> list1.append('*')
>>> lists
[[], ['*'], []]

>>> lists = [list()] * 3
>>> for i in range(len(lists)) : lists[i].append(i)
... 
>>> lists
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

>>> lists = [list() for i in range(3)]
>>> for i in range(len(lists)) : lists[i].append(i)
... 
>>> lists
[[0], [1], [2]]


Denis
-- 


la vita e estrany

spir.wikidot.com

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


[Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread Rafik Ouerchefani
Hello,

I'm trying to split 5 chars words (codes) into 2 variables. The first must
contain the first 4 characters. The second variable will only contain the
last character. I'm working in a loop pre-generated codes.

Any magic command for this ? :-)

Examples of codes :
3cmvA
3cmvB
3cmvC
3cmwA
1g18A
2g88A
1mo4A
 2odwA
1u94A
1xmvA
1xp8A
2zr9A

Thanks a lot !

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


Re: [Tutor] pgdb and console output

2010-03-09 Thread pylist1

On Mon, 8 Mar 2010, Alan Gauld wrote:


"hithere there"  wrote

 #! /usr/bin/env python

 import pgdb, sys

 db = pgdb.connect (dsn='192.168.0.1:familydata',
user='postgres', password='')
 cursor = db.cursor ()

 cursor.execute ("select * from names")

 rows = cursor.fetchall ()

 for i in (rows):
print i

 #viewtable (db)


No idea what viewtable does, never seen it before...
The only case I found on Google was in a tutorial which defined viewtable() 
as a function, it wasn't in the pgdb module...



 #sys.stdout.write()


This writes nothing to stdout. Apparently successfully from your comment 
below.


Try:

sys.stdout.write(str(i))


Yes that does indeed work like this:

for i in (rows):
sys.stdout.write(str(i))

But it leaves formatting issues with leaving the command prompt in the 
middle of the screen. That's not an issue at the moment.



 The code as is will display the data to the console.  I have read the
 db API 2.0 at python.org.  The problem is viewtable (db) will not work
 or sys.stdout.write () to write the table data to the console screen.
 What am I doing wrong here or what do I need to do different?


The print is writing to the console I assume? That is the normal method.


 Can anyone point me to a book specificly for pgdb, python and postgre?


It doesn't look lie you need a  postgres specific boook but just to go 
through the standard Python tutorial. It should make most things clear.



 Last thing is can I define the classes in a separate files and
 reference them?  Kinda like code reuse in .Net?  I new to Python so
 bear with me.


Yes, just define them in a file then import that file as a module. The 
standard tutorial explains that too.


Ok got that I think. Python references the current running directory first 
then to site packages directory.


I found after my initial email your pdf book you had wrote.  it seems to 
have exactly what I am after.  The console sqlite addressbook.py 
application.  That is the way I am wantting to do mine.  Going to use the 
way you done but with Postgre and pgdb and see how I do.  Thanks for 
writting the book.  It was a big help atleast that part.


One last thing is your using "raw_input".  What's the differenct between 
that and "str input"?  If the OS is using UTF-8 and the databse is UTF-8 
should it really matter? So raw_input could mean any type of input?


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


Re: [Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread Serdar Tumgoren
> Hello,
>
> I'm trying to split 5 chars words (codes) into 2 variables. The first must
> contain the first 4 characters. The second variable will only contain the
> last character. I'm working in a loop pre-generated codes.
>
> Any magic command for this ? :-)
>
> Examples of codes :
> 3cmvA
> 3cmvB
> 3cmvC
> 3cmwA
> 1g18A
> 2g88A
> 1mo4A
>  2odwA
> 1u94A
> 1xmvA
> 1xp8A
> 2zr9A
>
> Thanks a lot !
>
> a+
>

Hmm...This sounds a bit like homework. Can you show us any code that you've
already tried? Meantime, you might want to explore Python's slice syntax:

http://docs.python.org/tutorial/introduction.html#strings
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread عمـ نوفل ـاد
On Tue, Mar 9, 2010 at 10:31 AM, Rafik Ouerchefani
wrote:

> Hello,
>
> I'm trying to split 5 chars words (codes) into 2 variables. The first must
> contain the first 4 characters. The second variable will only contain the
> last character. I'm working in a loop pre-generated codes.
>
> Any magic command for this ? :-)
>
> Examples of codes :
> 3cmvA
> 3cmvB
> 3cmvC
> 3cmwA
> 1g18A
> 2g88A
> 1mo4A
>  2odwA
> 1u94A
> 1xmvA
> 1xp8A
> 2zr9A
>
> Thanks a lot !
>
> a+
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> What about indexing
part1 = word[:4]
part2 = word[4]


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread Stefan Behnel

Emad Nawfal (عمـ نوفل ـاد), 09.03.2010 16:45:

What about indexing
part1 = word[:4]


That's slicing.


part2 = word[4]


That's indexing.

Stefan

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


[Tutor] Macbook Pro+python programming

2010-03-09 Thread Marco Rompré
Does anybody know any good python emulator that would work perfectly with
Mac OSX Leopard?

Because I just got a brand new Mac (the one on apple store) and it seems
that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz
dual core and each time I open terminal, then type idle and try to use IDLE
the screen freezes (something you would never expect with a computer with my
specs) and I must force quit the application to be able to continue using my
computer.

Please someone help me
I'm at university and I always do python in my intro course.

Thank you
-- 
Marc-O. Rompré
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Macbook Pro+python programming

2010-03-09 Thread Shashwat Anand
On Wed, Mar 10, 2010 at 12:05 AM, Marco Rompré wrote:

> Does anybody know any good python emulator that would work perfectly with
> Mac OSX Leopard?
>

Why emulator ? Python 2.6.1 is pre-installed. You can install newer versions
too.

>
> Because I just got a brand new Mac (the one on apple store) and it seems
> that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz
> dual core and each time I open terminal, then type idle and try to use IDLE
> the screen freezes (something you would never expect with a computer with my
> specs) and I must force quit the application to be able to continue using my
> computer.
>

The problem you seems is to be having with IDLE. I never had any problem,
but you can try vim+bpython, or textmate or other eclipse+pydev. IDLE is a
poor choice for coding in python IMHO.

>
> Please someone help me
> I'm at university and I always do python in my intro course.
>

Lucky that you do it in your intro course. We had C++ which kinda sucks :(

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


Re: [Tutor] split a 5 chars word into 4 and 1

2010-03-09 Thread عمـ نوفل ـاد
On Tue, Mar 9, 2010 at 11:03 AM, Stefan Behnel  wrote:

> Emad Nawfal (عمـ نوفل ـاد), 09.03.2010 16:45:
>
>  What about indexing
>> part1 = word[:4]
>>
>
> That's slicing.
>
>  part2 = word[4]
>>
>
> That's indexing.
>
> Stefan
> Thanks Stefan.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] Macbook Pro+python programming

2010-03-09 Thread Brett Wunderlich

On Mar 9, 2010, at 12:35 PM, tutor-requ...@python.org wrote:

> Does anybody know any good python emulator that would work perfectly with
> Mac OSX Leopard?
> 
> Because I just got a brand new Mac (the one on apple store) and it seems
> that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz
> dual core and each time I open terminal, then type idle and try to use IDLE
> the screen freezes (something you would never expect with a computer with my
> specs) and I must force quit the application to be able to continue using my
> computer.

Python (and idle) on your MacBook Pro should work just fine. If it does not, 
you should work on fixing that, as the Mac OS uses Python for various things in 
the background, especially for installations. I would suggest using Disk 
Utility (in the /Applications/Utilities folder) to do a "Verify Disk". If it 
finds problems, you'll need to boot the MacBook Pro from your install disk and 
use Disk Utility to do a "Repair Disk". If no problems are found then I would 
start with an Archive and Install of the operating system. As always, before 
doing anything like the above, have a complete backup of any important data.

http://discussions.apple.com is a great place to go for advice on any of the 
above.

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


[Tutor] SQLite error messages

2010-03-09 Thread Alan Harris-Reid

Hi there,

I am using the sqlite3 module with Python 3.1, and have some code which 
goes something like as follows...


import sqlite3
con = sqlite3.connect('MyDatabase.db')

try:
   execresult = con.execute('INSERT INTO MyTable (field_name) VALUES 
("MyValue")')

   con.commit()
except:
   con.rollback()
   
If con.execute() fails, nothing is returned, and although the code 
correctly executes the rollback next, I have no idea why, and therefore 
cannot create a suitable error-handler with meaningful messages.  

I notice from the SQLite website that there are error codes, but it 
looks like the sqlite3 module is not reporting them.  


Has anyone any ideas how to get around this problem?

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


Re: [Tutor] SQLite error messages

2010-03-09 Thread Benno Lang
On 10 March 2010 11:37, Alan Harris-Reid  wrote:
> Hi there,
>
> I am using the sqlite3 module with Python 3.1, and have some code which goes
> something like as follows...
>
> import sqlite3
> con = sqlite3.connect('MyDatabase.db')
>
> try:
>   execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
> ("MyValue")')
>   con.commit()
> except:
>   con.rollback()
>   If con.execute() fails, nothing is returned, and although the code
> correctly executes the rollback next, I have no idea why, and therefore
> cannot create a suitable error-handler with meaningful messages.
> I notice from the SQLite website that there are error codes, but it looks
> like the sqlite3 module is not reporting them.

Do you mean numerical error codes? Which page on the SQLite website
are you referring to? Certainly the exception contains usable data.
Try something like this:

try:
  execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
("MyValue")')
  con.commit()
except Exception as error:
  print("Didn't work:", error)
  con.rollback()

(I didn't create a table, so I get "Didn't work: no such table: MyTable")

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


[Tutor] Difflib comparing string sequnces

2010-03-09 Thread Vincent Davis
I have never used the difflib or similar and have a few questions.
I am working with DNA sequences of length 25. I have a list of 230,000 and
need to look for each sequence in the entire genome (toxoplasma parasite) I
am not sure how large the genome is but more that 230,000 sequences.
The are programs that do this and really fast, and they eve do partial
matches but not quite what I need. So I am looking to build a custom
solution.
I need to look for each of my sequences of 25 characters example(
AGCCTCCCATGATTGAACAGATCAT).
The genome is formatted as a continuos string
(CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG.)

I don't care where or how many times on if it exists. This is simple I
think, str.find(AGCCTCCCATGATTGAACAGATCAT)

But I also what to find a close match defined as only wrong at 1 location
and I what to record the location. I am not sure how do do this. The only
thing I can think of is using a wildcard and performing the search with a
wildcard in each position. ie 25 time.
For example
AGCCTCCCATGATTGAACAGATCAT
AGCCTCCCATGATAGAACAGATCAT
close match with a miss-match at position 13


  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SQLite error messages

2010-03-09 Thread Sander Sweers
- Original message -
> I am using the sqlite3 module with Python 3.1, and have some code which
> goes something like as follows...
>
> import sqlite3
> con = sqlite3.connect('MyDatabase.db')
>
> try:
>        execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
> ("MyValue")')
>        con.commit()
> except:

Here you catch all exceptions. Normally you would catch a specific exception 
like ValueError.  
>        con.rollback()
>

Do you know finally? It is run after all the exceptions have been handled and 
this is where I would put the rollback.

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