[Tutor] Two dimesional array

2008-06-19 Thread kinuthiA muchanE

On Thu, 2008-06-19 at 02:30 +0200, [EMAIL PROTECTED] wrote:
> Message: 2
> Date: Wed, 18 Jun 2008 19:43:21 +0530
> From: "amit sethi" <[EMAIL PROTECTED]>
> Subject: [Tutor] (no subject)
> To: tutor@python.org
> Message-ID:
> <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi , Could you please tell me , how i can traverse a two dimensional
>>> from numpy import * # import the necessary module

> >>> arry = array((1,2,3,4)) # create a rank-one array
> >>> print arry
> [1 2 3 4]
> >>> print arry.shape
> (4,) # this means it is a rank 1 array with a length of 4  (the trailing 
> comma means it is a tuple)
> 
> To get to the first element in the array:
>  print arry[0]
> 1
> 
> To get to the last element:
> >>> print arry[-1]
> 4
> >>> 
>  
> >>> arry2 = array(([5,6,7,8],[9,10,11,12])) # create a a rank-two array, 
> >>> two-dimensional, if wish
> >>> print arry2
> [[ 5  6  7  8]
>  [ 9 10 11 12]]
> >>> print arry2.shape #
> (2, 4) # this means that it is a rank 2 (ie 2-dimensional) array, with each 
> axis having a length of 4
> >>> 
> To get to the first element in the first axis:
> >>> print arry2[0,0]
> 5
> To get to the last element in the second axis:
> >>> print arry2[1,-1]
> 12
> 
> Does this help?
> Kinuthia...



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


Re: [Tutor] Newbie: Sorting lists of lists

2008-06-19 Thread Kent Johnson
On Wed, Jun 18, 2008 at 10:48 PM, Forrest Y. Yu <[EMAIL PROTECTED]> wrote:
> I know, it's NOT beautiful code, but it seems work. If you have no better
> way, try this:
>
> """Sort on the second elements."""
> def xchg12(l) :
> for m in l:
> m[0], m[1] = m[1], m[0]
>
> l = [[1, 2, 3], [2, 3, 1], [3, 2,1], [1, 3, 2]]
> print l
> xchg12(l)
> l.sort()
> xchg12(l)
> print l

This is not too far from the decorate-sort-undecorate idiom which used
to be the standard way to do something like this. Instead of modifying
the elements of the original list, create a new list whose elements
are the key and the item from the old list, and sort that:

In [6]: l = [[1, 2, 3], [2, 3, 1], [3, 2, 1], [1, 3, 2]]

In [7]: m = [ (i[1], i) for i in l ]

In [8]: m.sort()

In [9]: m
Out[9]: [(2, [1, 2, 3]), (2, [3, 2, 1]), (3, [1, 3, 2]), (3, [2, 3, 1])]

In [10]: l = [ i[1] for i in m ]

In [11]: l
Out[11]: [[1, 2, 3], [3, 2, 1], [1, 3, 2], [2, 3, 1]]

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


Re: [Tutor] compiling python

2008-06-19 Thread Kent Johnson
On Wed, Jun 18, 2008 at 11:29 PM, Sean Novak <[EMAIL PROTECTED]> wrote:
> Hello Python Guru's and Newbies alike.  I've been able to write a bit of
> Python for a web app that works wonderfully on my dev machine.  However, I
> have limited access to the machine that will actually host this app.  Will
> compiling a Python program eliminate library dependency requirements for the
> host machine?  Essentially, are the libraries compiled into the binary?

You can use py2exe or py2app to create an executable that includes the
libraries. This is not compiling, it is more a packaging step.

You may also be able to include the libraries in the same directory as
your app. What kind of access do you have to the host? What libraries
do you need?

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


Re: [Tutor] re adlines, read, and my own get_contents()

2008-06-19 Thread Kent Johnson
On Thu, Jun 19, 2008 at 2:44 AM, John [H2O] <[EMAIL PROTECTED]> wrote:
>
> I've defined:
>
> def get_contents(infile=file_object):
>   """ return a list of lines from a file """
>   contents=infile.read()
>   contents = contents.strip().split('\n')
>   return contents

> I think I understand the differences, but can someone tell me if there's any
> difference between what I define and the readlines() method?

readline() and readlines() include the trailing newline in the lines
they return; your function does not.
readline() and readlines() don't strip leading and trailing space.

contents = contents.splitlines(True) would duplicate readlines() I think.

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


Re: [Tutor] compiling python

2008-06-19 Thread Kent Johnson
On Thu, Jun 19, 2008 at 7:11 AM, Sean Novak <[EMAIL PROTECTED]> wrote:
> missed answering that last part before I sent the email.
>
> import urllib
> import libxml2dom
> import xml.dom.ext

urllib is part of the standard lib. Perhaps you could replace the xml
libs with something that is in the standard lib, for example
xml.etree.ElementTree if you are using Python 2.5.

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


Re: [Tutor] Newbie: Sorting lists of lists

2008-06-19 Thread Mark Tolonen


"Kent Johnson" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

On Wed, Jun 18, 2008 at 8:30 PM, Keith Troell <[EMAIL PROTECTED]> wrote:
Let's say I have a list of lists l == [[1, 2, 3], [2, 3, 1], [3, 2, 1], 
[1,

3, 2]]

If I do a l.sort(), it sorts on the first element of each listed list:


l.sort()
l

[[1, 2, 3], [1, 3, 2], [2, 3, 1], [3, 2, 1]]


How can I sort on the second or third elements of the listed lists?


Use the key= parameter of sort to specify the sort key.
operator.itemgetter is convenient for the actual key:

In [3]: from operator import itemgetter
In [4]: l.sort(key=itemgetter(1))
In [5]: l
Out[5]: [[1, 2, 3], [3, 2, 1], [2, 3, 1], [1, 3, 2]]

A longer explanation is here:
http://personalpages.tds.net/~kent37/kk/7.html

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



You can use a lambda that builds the key you want to sort on from the 
original element:



L=[[1,2,3],[2,3,1],[3,2,1],[1,3,2]]


Sort on 2nd value:


sorted(L,key=lambda x: x[1])

[[1, 2, 3], [3, 2, 1], [2, 3, 1], [1, 3, 2]]

Sort on 2nd, then by 3rd value


sorted(L,key=lambda x: (x[1],x[2]))

[[3, 2, 1], [1, 2, 3], [2, 3, 1], [1, 3, 2]]

-Mark 



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


[Tutor] inheritance/classmethods/metaclasses

2008-06-19 Thread Eric Abrahamsen
I'm probably in over my head here, but I really want to know how this  
works, and I hope someone will be willing to take a moment to explain  
it...


I'm making a few classes for a very simple ORM, where the classes  
reflect objects that I'm persisting via pickle. I'd like to add and  
delete instances via add() and delete() classmethods, so that the  
classes can keep track of the comings and goings of their instances.


My main question is, how can I abstract this classmethod behavior for  
several classes, either into a metaclass, or a super class? I've done  
some very scary reading, and some fruitless experimentation, and am  
over my head. I know the answer is probably "do something else  
entirely", but I'd really like to know how this is supposed to work.  
What I'd like is a Model class, which provides an add() classmethod  
like this:


class Model(object):
@classmethod
def add(cls, *args)
inst = cls(args)
cls.instances.append(inst)

Then, say, a File class:

class File():
# inherit from Model, or use Model as a metaclass?
instances = []
def __init__(self, *args)
self.filename = args[0]
self.keyword = args[1]

The desired behavior, of course, is this:

File.add('somefilename','keywordforthisfile')

and to be able to extend this behavior to other classes.

Any pointers would be much appreciated...

Eric


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


Re: [Tutor] inheritance/classmethods/metaclasses

2008-06-19 Thread Kent Johnson
On Thu, Jun 19, 2008 at 9:50 AM, Eric Abrahamsen
<[EMAIL PROTECTED]> wrote:

> I'm making a few classes for a very simple ORM, where the classes reflect
> objects that I'm persisting via pickle. I'd like to add and delete instances
> via add() and delete() classmethods, so that the classes can keep track of
> the comings and goings of their instances.

What you have almost works. Try this:

class Model(object):
@classmethod
def add(cls, *args):
   inst = cls(*args)
   cls.instances.append(inst)

class File(Model):
   instances = []
   def __init__(self, *args):
   self.filename = args[0]
   self.keyword = args[1]


File.add('somefilename','keywordforthisfile')

print File.instances


You have to define instances in each class; a metaclass could do that
for you. Anyway I hope this gets you a little farther.

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


[Tutor] Learning Python from books

2008-06-19 Thread Zameer Manji
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

Has anyone here attempted to learn Python from books ? I recently
purchased "Learning Python" 3rd Edition (9780596513986) and if anyone
here is a good bottom-up learner than it is the perfect book. The author
goes over each feature in python, explaining it's syntax, usage and the
"pythonic" way of using them in programs. It has really helped me
understand some of the more powerful tools in python and I am sure it
will make an excellent reference book in the future. With that said, has
anyone else attempted to learn python from books and if so which one ?
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)

iQEcBAEBCgAGBQJIWqvxAAoJEA759sZQuQ1BS+kIAK+6TWqesUQjEQygwsMI3seU
ZWUkd4wlextCOXIH6mse4TMHdAU3+NHDo1V4QwSYxNTEjwpWFr1Kg2BE0zyMyxPD
gs8Kaov6/DGQgyFFt5DRcKvkQ0M5St7I/PuP+n/eelMUK1culvXx3oycKBqh8D21
R/g0SQ0M9pUDLuBEygbgjFw1WVsf/h8/zCc38qHQ+QOQvrVaEciV3I5JLyp3+u8g
JyjzxjHSRHd4Ik4cnaeKa2OyFn5JaPXxPmrmPiE3WEWnJxLWucXAP/CEm7e9aamQ
Fv4SYuVmOIGpbAqySgEpQH2yRsd+0qaNKSJq4hqjQ1/z2Bx6hM5LTqJAVjfZMW4=
=9xVw
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Python from books

2008-06-19 Thread jay
Me personally, both "Learning Python" and "Core Python Programming".  I am
by no means an expert, but both of these books are excellent and were quite
helpful.

jay

On Thu, Jun 19, 2008 at 1:56 PM, Zameer Manji <[EMAIL PROTECTED]> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA512
>
> Has anyone here attempted to learn Python from books ? I recently
> purchased "Learning Python" 3rd Edition (9780596513986) and if anyone
> here is a good bottom-up learner than it is the perfect book. The author
> goes over each feature in python, explaining it's syntax, usage and the
> "pythonic" way of using them in programs. It has really helped me
> understand some of the more powerful tools in python and I am sure it
> will make an excellent reference book in the future. With that said, has
> anyone else attempted to learn python from books and if so which one ?
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.7 (MingW32)
>
> iQEcBAEBCgAGBQJIWqvxAAoJEA759sZQuQ1BS+kIAK+6TWqesUQjEQygwsMI3seU
> ZWUkd4wlextCOXIH6mse4TMHdAU3+NHDo1V4QwSYxNTEjwpWFr1Kg2BE0zyMyxPD
> gs8Kaov6/DGQgyFFt5DRcKvkQ0M5St7I/PuP+n/eelMUK1culvXx3oycKBqh8D21
> R/g0SQ0M9pUDLuBEygbgjFw1WVsf/h8/zCc38qHQ+QOQvrVaEciV3I5JLyp3+u8g
> JyjzxjHSRHd4Ik4cnaeKa2OyFn5JaPXxPmrmPiE3WEWnJxLWucXAP/CEm7e9aamQ
> Fv4SYuVmOIGpbAqySgEpQH2yRsd+0qaNKSJq4hqjQ1/z2Bx6hM5LTqJAVjfZMW4=
> =9xVw
> -END PGP SIGNATURE-
> ___
> 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] Learning Python from books

2008-06-19 Thread Jason Conner
I use the same books - Learning Python and Core Python Programming, 2nd ed.
I found I got about halfway through Learning Python before I switched to CPP
and had no problems. I also use "Python Phrasebook" (Brad Dayley, 2007) as a
handy reference guide to some common problems as well.

Core Python Programming, 2nd ed. (Wesley Chun, 2007) is my most frequent
instructional guide, though.

On Fri, Jun 20, 2008 at 9:39 AM, jay <[EMAIL PROTECTED]> wrote:

> Me personally, both "Learning Python" and "Core Python Programming".  I am
> by no means an expert, but both of these books are excellent and were quite
> helpful.
>
> jay
>
>
> On Thu, Jun 19, 2008 at 1:56 PM, Zameer Manji <[EMAIL PROTECTED]> wrote:
>
>> -BEGIN PGP SIGNED MESSAGE-
>> Hash: SHA512
>>
>> Has anyone here attempted to learn Python from books ? I recently
>> purchased "Learning Python" 3rd Edition (9780596513986) and if anyone
>> here is a good bottom-up learner than it is the perfect book. The author
>> goes over each feature in python, explaining it's syntax, usage and the
>> "pythonic" way of using them in programs. It has really helped me
>> understand some of the more powerful tools in python and I am sure it
>> will make an excellent reference book in the future. With that said, has
>> anyone else attempted to learn python from books and if so which one ?
>> -BEGIN PGP SIGNATURE-
>> Version: GnuPG v1.4.7 (MingW32)
>>
>> iQEcBAEBCgAGBQJIWqvxAAoJEA759sZQuQ1BS+kIAK+6TWqesUQjEQygwsMI3seU
>> ZWUkd4wlextCOXIH6mse4TMHdAU3+NHDo1V4QwSYxNTEjwpWFr1Kg2BE0zyMyxPD
>> gs8Kaov6/DGQgyFFt5DRcKvkQ0M5St7I/PuP+n/eelMUK1culvXx3oycKBqh8D21
>> R/g0SQ0M9pUDLuBEygbgjFw1WVsf/h8/zCc38qHQ+QOQvrVaEciV3I5JLyp3+u8g
>> JyjzxjHSRHd4Ik4cnaeKa2OyFn5JaPXxPmrmPiE3WEWnJxLWucXAP/CEm7e9aamQ
>> Fv4SYuVmOIGpbAqySgEpQH2yRsd+0qaNKSJq4hqjQ1/z2Bx6hM5LTqJAVjfZMW4=
>> =9xVw
>> -END PGP SIGNATURE-
>> ___
>> 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] Learning Python from books

2008-06-19 Thread Lowell Tackett
"Python Programming [for the absolute beginner]" by Michael Dawson is-in my 
humble opinion-a programming pedagogical pacesetter.

>From the virtual desk of Lowell Tackett  



--- On Thu, 6/19/08, Zameer Manji <[EMAIL PROTECTED]> wrote:

> From: Zameer Manji <[EMAIL PROTECTED]>
> Subject: [Tutor] Learning Python from books
> To: "Python Tutor mailing list" 
> Date: Thursday, June 19, 2008, 2:56 PM
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA512
> 
> Has anyone here attempted to learn Python from books ? I
> recently
> purchased "Learning Python" 3rd Edition
> (9780596513986) and if anyone
> here is a good bottom-up learner than it is the perfect
> book. The author
> goes over each feature in python, explaining it's
> syntax, usage and the
> "pythonic" way of using them in programs. It has
> really helped me
> understand some of the more powerful tools in python and I
> am sure it
> will make an excellent reference book in the future. With
> that said, has
> anyone else attempted to learn python from books and if so
> which one ?
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.7 (MingW32)
> 
> iQEcBAEBCgAGBQJIWqvxAAoJEA759sZQuQ1BS+kIAK+6TWqesUQjEQygwsMI3seU
> ZWUkd4wlextCOXIH6mse4TMHdAU3+NHDo1V4QwSYxNTEjwpWFr1Kg2BE0zyMyxPD
> gs8Kaov6/DGQgyFFt5DRcKvkQ0M5St7I/PuP+n/eelMUK1culvXx3oycKBqh8D21
> R/g0SQ0M9pUDLuBEygbgjFw1WVsf/h8/zCc38qHQ+QOQvrVaEciV3I5JLyp3+u8g
> JyjzxjHSRHd4Ik4cnaeKa2OyFn5JaPXxPmrmPiE3WEWnJxLWucXAP/CEm7e9aamQ
> Fv4SYuVmOIGpbAqySgEpQH2yRsd+0qaNKSJq4hqjQ1/z2Bx6hM5LTqJAVjfZMW4=
> =9xVw
> -END PGP SIGNATURE-
> ___
> 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] inheritance/classmethods/metaclasses

2008-06-19 Thread Eric Abrahamsen

What you have almost works. Try this:


No kidding – that's what I get for wild stabs in the dark, I thought  
I'd tried that. I'm pleased that it really is that simple (and that,  
whatever metaclasses are used for, I don't need to worry about them  
yet).


Thanks!

Eric

(Grrr, bit by the reply-to bug. Not that this is really worth re- 
posting...)

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