[Tutor] manipulating data

2007-11-07 Thread Bryan Fodness
I would like to have my data in a format so that I can create a contour plot.

My data is in a file with a format, where there may be multiple fields

field = 1

1a  0
2a  0
3a  5
4a  5
5a  5
6a  5
7a  5
8a  5
9a  0
10a 0
1b  0
2b  0
3b  5
4b  5
5b  5
6b  5
7b  5
8b  5
9b  0
10b 0

field = 2

1a  0
2a  0
3a  0
4a  4
5a  4
6a  4
7a  4
8a  0
9a  0
10a 0
1b  0
2b  0
3b  0
4b  4
5b  4
6b  4
7b  4
8b  0
9b  0
10b 0

field = 3

1a  0
2a  0
3a  0
4a  0
5a  3
6a  3
7a  0
8a  0
9a  0
10a 0
1b  0
2b  0
3b  0
4b  0
5b  3
6b  3
7b  0
8b  0
9b  0
10b 0

where,

   a   b
  a   b   ab
10  00|00   00|00   00|00
9   00|00   00|00   00|00
8   01|10   00|00   00|00
7   01|10   00|00   00|00
6   01|10   00|00   000111|111000
5   01|10   00|00   000111|111000
4   01|10   00|00   00|00
3   01|10   00|00   00|00
2   00|00   00|00   00|00
1   00|00   00|00   00|00

I could possibly have many of these that I will add together and
normalize to one.
Also, there are 60 a and b blocks, the middle 40 are 0.5 times the
width of the outer 20.

I thought about filling an array, but there is not a one to one symmetry.

I cannot seem to get my head around this. Can anybody help me get started?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a typing speed tester

2007-11-07 Thread Evert Rol
   Hi Tom,

> I'm trying to write a program to test someones typing speed and show
> them their mistakes. However I'm getting weird results when looking
> for the differences in longer (than 100 chars) strings:
>
> import difflib
>
> # a tape measure string (just makes it easier to locate a given index)
> a =  
> '1-3-5-7-9-12-15-18-21-24-27-30-33-36-39-42-45-48-51-54-57-60-63-66-69 
> -72-75-78-81-84-87-90-93-96-99-103-107-111-115-119-123-127-131-135-139 
> -143-147-151-155-159-163-167-171-175-179-183-187-191-195--200'
>
> # now with a few mistakes
> b = '1-3-5-7- 
> l-12-15-18-21-24-27-30-33-36-39o42-45-48-51-54-57-60-63-66-69-72-75-78 
> -81-84-8k-90-93-96-9l-103-107-111-115-119-12b-1v7-131-135-139-143-147- 
> 151-m55-159-163-167-a71-175j179-183-187-191-195--200'
>
> s = difflib.SequenceMatcher(None, a ,b)
> ms = s.get_matching_blocks()
>
> print ms
>
 [(0, 0, 8), (200, 200, 0)]
>
> Have I made a mistake or is this function designed to give up when the
> input strings get too long? If so what could I use instead to compute
> the mistakes in a typed text?


Ok, I wasn't on the list last year, but I was a few days ago, so  
persistence pays off; partly, as I don't have a full answer.

I got curious and looked at the source of difflib. There's a method  
__chain_b() which sets up the b2j variable, which contains the  
occurrences of characters in string b. So cutting b to 199  
characters, it looks like this:
b2j= 19 {'a': [168], 'b': [122], 'm': [152], 'k': [86], 'v':  
[125], '-': [1, 3, 5, 7, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 42,  
45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93,  
96, 99, 103, 107, 111, 115, 119, 123, 127, 131, 135, 139, 143, 147,  
151, 155, 159, 163, 167, 171, 179, 183, 187, 191, 195, 196], 'l': [8,  
98], 'o': [39], 'j': [175], '1': [0, 10, 13, 16, 20, 50, 80, 100,  
104, 108, 109, 110, 112, 113, 116, 117, 120, 124, 128, 130, 132, 136,  
140, 144, 148, 150, 156, 160, 164, 170, 172, 176, 180, 184, 188, 190,  
192], '0': [29, 59, 89, 101, 105, 198], '3': [2, 28, 31, 32, 34, 37,  
62, 92, 102, 129, 133, 137, 142, 162, 182], '2': [11, 19, 22, 25, 41,  
71, 121, 197], '5': [4, 14, 44, 49, 52, 55, 74, 114, 134, 149, 153,  
154, 157, 174, 194], '4': [23, 40, 43, 46, 53, 83, 141, 145], '7':  
[6, 26, 56, 70, 73, 76, 106, 126, 146, 166, 169, 173, 177, 186], '6':  
[35, 58, 61, 64, 65, 67, 95, 161, 165], '9': [38, 68, 88, 91, 94, 97,  
118, 138, 158, 178, 189, 193], '8': [17, 47, 77, 79, 82, 85, 181, 185]}

This little detour is because of how b2j is built. Here's a part from  
the comments of __chain_b():

# Before the tricks described here, __chain_b was by far the most
# time-consuming routine in the whole module!  If anyone sees
# Jim Roskind, thank him again for profile.py -- I never would
# have guessed that.

And the part of the actual code reads:
 b = self.b
 n = len(b)
 self.b2j = b2j = {}
 populardict = {}
 for i, elt in enumerate(b):
 if elt in b2j:
 indices = b2j[elt]
 if n >= 200 and len(indices) * 100 > n: # <--- !!
 populardict[elt] = 1
 del indices[:]
 else:
 indices.append(i)
 else:
 b2j[elt] = [i]

So you're right: it has a stop at the (somewhat arbitrarily) limit of  
200 characters. How that exactly works, I don't know (needs more  
delving into the code), though it looks like there also need to be a  
lot of indices (len(indices*100>n); I guess that's caused in your  
strings by the dashes, '1's and '0's (that's why I printed the b2j  
string).
If you feel safe enough and on a fast platform, you can probably up  
that limit (or even put it somewhere as an optional variable in the  
code, which I would think is generally better).
Not sure who the author of the module is (doesn't list in the file  
itself), but perhaps you can find out and email him/her, to see what  
can be altered.

Hope that helps.

   Evert


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


Re: [Tutor] New Introductory Book

2007-11-07 Thread Eddie Armstrong
Whatever the rationale for the price you could buy 2nd Ed 'Core', Chun 
*and *3rd edition(when it arrives) 'Learning Python', Lutz (the two 
standard, known and respected beginners texts) for the price of this.
Mmm, I wonder what I would buy or rather have as a student.

Eddie

PS (My apologies for inadvertently sending this to the original poster 
instead of the list)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manipulating data

2007-11-07 Thread Kent Johnson
Bryan Fodness wrote:
> I also have some information at the beginning of the file and between
> each field.  Is there a way to get the info at the beginning and tell
> it once it sees Leaf 1A to read the values for the next 120 and then
> repeat until there are no more Fields.

This should be a pretty simple modification to the technique I showed 
you using f.next(). Just add another loop to process the header fields. 
If you have variable-length sections then you may have to 'prefetch' the 
next line, something like this:

try:
   line = f.next()
   while True:
 if 'Leaf 1A' in line:
   break
 # process header line

   # 'line' already contains the next line
   while True:
 # process body line
 line = f.next()

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


Re: [Tutor] manipulating data

2007-11-07 Thread Kent Johnson
Bryan Fodness wrote:
> I would like to have my data in a format so that I can create a contour plot.
> 
> My data is in a file with a format, where there may be multiple fields
> 
> field = 1
> 
> 1a0

If your data is really this regular, it is pretty easy to parse. A 
useful technique is to access a file's next method directly. Something 
like this (not tested!):

f = open('data.txt')
fields = {} # build a dict of fields
try:
   while True:
 # Get the field line
 line = f.next()
 field = int(line.split()[-1]) # last part of the line as an int

 f.next() # skip blank line

 data = {} # for each field, map (row, col) to value
 for i in range(20): # read 20 data lines
   line = f.next()
   ix, value = f.split()
   row = int(ix[:-1])
   col = ix[-1]
   data[row, col] = int(value)

 fields[field] = data

 f.next()
except StopIteration:
   pass

This builds a dict whose keys are field numbers and values are 
themselves dicts mapping (row, col) pairs to a value.

> where,
> 
>a   b
>   a   b ab
> 1000|00   00|00   00|00
> 9 00|00   00|00   00|00
> 8 01|10   00|00   00|00
> 7 01|10   00|00   00|00
> 6 01|10   00|00   000111|111000
> 5 01|10   00|00   000111|111000
> 4 01|10   00|00   00|00
> 3 01|10   00|00   00|00
> 2 00|00   00|00   00|00
> 1 00|00   00|00   00|00

I guess this is the intended output? Do you want to actually create a 
printed table like this, or some kind of data structure that represents 
the table, or what?

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


Re: [Tutor] New Introductory Book

2007-11-07 Thread bhaaluu
Greetings,
Many books have the source code available for download somewhere,
or even a sample chapter? Are the examples in the book complete
programs, or are they snippets illustrating a concept? If the programs
are complete, what type of programs are they (business, science, other)?
Does the source code work with GNU/Linux, or is it for MS-Windows only?
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html

On Nov 6, 2007 6:15 PM, Michael H. Goldwasser <[EMAIL PROTECTED]> wrote:
>
> Thanks to the many voices supporting our decision to post to Tutor.
> We only posted to the most directly relevant mailing lists (announce,
> tutor, edusig).  As an introductory book, it seemed quite appropriate
> for tutor.
>
> In fact, the topic of our (developing) book was raised in a thread on
> Tutor this past August 9/10th. Ironically, the topic at that time is
> the same as that raised by Chris Calloway's question today, about the
> $102 list price.
>
> The discrepency is because this is being published primarily as an
> academic book through Prentice Hall's Education line (as opposed to
> the Prentice Proffessional label that publishes books such as Wesley
> Chun's Core Python Programming).  I'm not on the business side, so I
> don't know that I understand all the factors; could be a combination
> of the captive audience together with a lot of additional money spent
> on sending review copies to educators and sending representatives to
> campuses.  In any event, we believe that the book can be quite useful
> outside the traditional classroom for new programmers or those new to
> object-oriented programming.
>
> Best regards,
> Michael
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Marc Tompkins
This question has probably been asked and answered many times, but I can't
figure out how to word my question to get relevant results from Google.  So
I thought I'd try some human beings, eh?

I'm working with delimited files (ANSI X12 EDI nonsense, to be precise.)
First I load the records to a list:
tmpSegs = inString.split(self.SegTerm)

Now, I want to replace each string in that list with a string:

for seg in tmpSegs:   # 'seg' is short for 'segment'
seg = seg.split(self.ElemSep)

It doesn't work.  If I check the contents of tmpSegs, it's still a list of
strings - not a list of lists.  So I do this instead:
tmpSegs2 = []
for seg in tmpSegs:
tmpSegs2.append(seg.split(self.sElemSep))
del tmpSegs

This works.  And, for the size of files that I'm likely to run into,
creating the extra list and burning the old one is probably not going to
swamp the machine - but it feels clumsy to me, like if I just knew the
secret I could be much more elegant about it.  I don't want the next guy who
reads my code to scratch his head and say 'What was he thinking?' -
especially if it's me in a year's time!

What puzzles me most is that I can replace 'seg' with just about anything
else -
for seg in tmpSegs:
seg = seg.strip()
or
for seg in tmpSegs:
seg = 'bananas'
or
for seg in tmpSegs:
seg = seg + ' bananas'
and it works.  So why can't I replace it with its own split?

It's not a burning question - development continues - but I'm really
curious.

Thanks!
-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Eric Brunson
Marc Tompkins wrote:
> This question has probably been asked and answered many times, but I 
> can't figure out how to word my question to get relevant results from 
> Google.  So I thought I'd try some human beings, eh?
>
> I'm working with delimited files (ANSI X12 EDI nonsense, to be 
> precise.)  First I load the records to a list:
> tmpSegs = inString.split(self.SegTerm)
>
> Now, I want to replace each string in that list with a string:
>
> for seg in tmpSegs:   # 'seg' is short for 'segment'
> seg = seg.split(self.ElemSep)

A list is an array of pointers to objects, what you've done here is 
create a new name referencing an item in the list, then making that new 
name point to something different.

Try this (untested code):

for index in xrange(0, len(tmpSegs)):
tmpSegs[index] = tmpSegs[index].split(self.ElemSep)


This will iterate through the list and set each reference in the list to 
point to your new object.

Hope that helps,
e.


>
> It doesn't work.  If I check the contents of tmpSegs, it's still a 
> list of strings - not a list of lists.  So I do this instead:
> tmpSegs2 = []
> for seg in tmpSegs:
> tmpSegs2.append(seg.split(self.sElemSep))
> del tmpSegs
>
> This works.  And, for the size of files that I'm likely to run into, 
> creating the extra list and burning the old one is probably not going 
> to swamp the machine - but it feels clumsy to me, like if I just knew 
> the secret I could be much more elegant about it.  I don't want the 
> next guy who reads my code to scratch his head and say 'What was he 
> thinking?' - especially if it's me in a year's time!
>
> What puzzles me most is that I can replace 'seg' with just about 
> anything else -
> for seg in tmpSegs:
> seg = seg.strip()
> or
> for seg in tmpSegs:
> seg = 'bananas'
> or
> for seg in tmpSegs:
> seg = seg + ' bananas'
> and it works.  So why can't I replace it with its own split?
>
> It's not a burning question - development continues - but I'm really 
> curious.
>
> Thanks!
> -- 
> www.fsrtechnologies.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] manipulating data

2007-11-07 Thread Bryan Fodness
I also have some information at the beginning of the file and between
each field.  Is there a way to get the info at the beginning and tell
it once it sees Leaf 1A to read the values for the next 120 and then
repeat until there are no more Fields.

File Rev = G
Treatment = Dynamic Dose
Last Name = Fodness
First Name = Bryan
Patient ID = 0001
Number of Fields = 4
Number of Leaves = 120
Tolerance = 0.50

Field = 10
Index = 0.
Carriage Group = 1
Operator =
Collimator = 0.0
Leaf  1A =   0.00
Leaf  2A =   0.00
Leaf  3A =   0.00
Leaf  4A =   0.00
...
Leaf 57B =   0.00
Leaf 58B =   0.00
Leaf 59B =   0.00
Leaf 60B =   0.00
Note = 0
Shape = 4
  500   500
  500  -500
 -500  -500
 -500   500
Magnification = 1.00

Field = 8
Index = 0.4000
Carriage Group = 1
Operator =
Collimator = 0.0
Leaf  1A =   0.00
Leaf  2A =   0.00
Leaf  3A =   0.00
Leaf  4A =   0.00
...
Leaf 57B =   0.00
Leaf 58B =   0.00
Leaf 59B =   0.00
Leaf 60B =   0.00
Note = 0
Shape = 4
  400   400
  400  -400
 -400  -400
 -400   400
Magnification = 1.00

I would like to have a data structure that I can use in one of the
graphing utilities (matpolotlib?).  I probably want to populate an
array with values, but I have not figured out how I want to do that
yet.

On Nov 7, 2007 8:52 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Bryan Fodness wrote:
> > I would like to have my data in a format so that I can create a contour 
> > plot.
> >
> > My data is in a file with a format, where there may be multiple fields
> >
> > field = 1
> >
> > 1a0
>
> If your data is really this regular, it is pretty easy to parse. A
> useful technique is to access a file's next method directly. Something
> like this (not tested!):
>
> f = open('data.txt')
> fields = {} # build a dict of fields
> try:
>   while True:
> # Get the field line
> line = f.next()
> field = int(line.split()[-1]) # last part of the line as an int
>
> f.next() # skip blank line
>
> data = {} # for each field, map (row, col) to value
> for i in range(20): # read 20 data lines
>   line = f.next()
>   ix, value = f.split()
>   row = int(ix[:-1])
>   col = ix[-1]
>   data[row, col] = int(value)
>
> fields[field] = data
>
> f.next()
> except StopIteration:
>   pass
>
> This builds a dict whose keys are field numbers and values are
> themselves dicts mapping (row, col) pairs to a value.
>
> > where,
> >
> >a   b
> >   a   b ab
> > 1000|00   00|00   00|00
> > 9 00|00   00|00   00|00
> > 8 01|10   00|00   00|00
> > 7 01|10   00|00   00|00
> > 6 01|10   00|00   000111|111000
> > 5 01|10   00|00   000111|111000
> > 4 01|10   00|00   00|00
> > 3 01|10   00|00   00|00
> > 2 00|00   00|00   00|00
> > 1 00|00   00|00   00|00
>
> I guess this is the intended output? Do you want to actually create a
> printed table like this, or some kind of data structure that represents
> the table, or what?
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] making a typing speed tester

2007-11-07 Thread Tom
Hi, I asked this question last year but got no response.

I'm trying to write a program to test someones typing speed and show
them their mistakes. However I'm getting weird results when looking
for the differences in longer (than 100 chars) strings:

import difflib

# a tape measure string (just makes it easier to locate a given index)
a = 
'1-3-5-7-9-12-15-18-21-24-27-30-33-36-39-42-45-48-51-54-57-60-63-66-69-72-75-78-81-84-87-90-93-96-99-103-107-111-115-119-123-127-131-135-139-143-147-151-155-159-163-167-171-175-179-183-187-191-195--200'

# now with a few mistakes
b = 
'1-3-5-7-l-12-15-18-21-24-27-30-33-36-39o42-45-48-51-54-57-60-63-66-69-72-75-78-81-84-8k-90-93-96-9l-103-107-111-115-119-12b-1v7-131-135-139-143-147-151-m55-159-163-167-a71-175j179-183-187-191-195--200'

s = difflib.SequenceMatcher(None, a ,b)
ms = s.get_matching_blocks()

print ms

>>>[(0, 0, 8), (200, 200, 0)]

Have I made a mistake or is this function designed to give up when the
input strings get too long? If so what could I use instead to compute
the mistakes in a typed text?

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


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Kent Johnson
Marc Tompkins wrote:
> I'm working with delimited files (ANSI X12 EDI nonsense, to be 
> precise.)  First I load the records to a list:
> tmpSegs = inString.split(self.SegTerm)
> 
> Now, I want to replace each string in that list with a string:
> 
> for seg in tmpSegs:   # 'seg' is short for 'segment'
> seg = seg.split(self.ElemSep)
> 
> It doesn't work.  If I check the contents of tmpSegs, it's still a list 
> of strings - not a list of lists.  So I do this instead:
> tmpSegs2 = []
> for seg in tmpSegs:
> tmpSegs2.append(seg.split(self.sElemSep))
> del tmpSegs
> 
> This works.  And, for the size of files that I'm likely to run into, 
> creating the extra list and burning the old one is probably not going to 
> swamp the machine - but it feels clumsy to me, like if I just knew the 
> secret I could be much more elegant about it.

Creating a new list is fine, actually. You can do it more elegantly with 
a list comprehension, and it's fine to assign to the same name, which 
will implicitly delete the old list:
tmpSegs = [ seg.split(self.ElemSep) for seg in tmpSegs ]

If you really want to replace elements in the same list you can use 
enumerate to get indices:

for i, seg in enumerate(tmpSegs):
   tmpSegs[i] = seg.split(self.ElemSep)

or use slice assignment:
tmpSegs[:] = [ seg.split(self.ElemSep) for seg in tmpSegs ]

but I doubt those are needed in this case.

> What puzzles me most is that I can replace 'seg' with just about 
> anything else -
> for seg in tmpSegs:
> seg = seg.strip()
> or
> for seg in tmpSegs:
> seg = 'bananas'
> or
> for seg in tmpSegs:
> seg = seg + ' bananas'
> and it works.  So why can't I replace it with its own split?

If by 'it works' you mean that tmpSegs is changed...you are mistaken or 
there is something you are not showing. Can you show why you think this 
changes tmpSegs?

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


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Marc Tompkins
Try this (untested code):

for index in xrange(0, len(tmpSegs)):
   tmpSegs[index] = tmpSegs[index].split(self.ElemSep)


Thank you - that works nicely, and it's a much better replacement for
something else I was doing to achieve the same result (you know, the old
count+=1 nonsense - every day, in every way, I'm struggling to become more
and more Pythonic.)

Now, I did already (intellectually) understand this:

A list is an array of pointers to objects, what you've done here is
create a new name referencing an item in the list, then making that new
name point to something different.

Given that, however, I still don't entirely understand why the other
examples I gave DO work.  Seems it should be one or the other, no?

I'm probably just being dense, though.

Marc

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


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Marc Tompkins
> > What puzzles me most is that I can replace 'seg' with just about
> > anything else -
> > for seg in tmpSegs:
> > seg = seg.strip()
> > or
> > for seg in tmpSegs:
> > seg = 'bananas'
> > or
> > for seg in tmpSegs:
> > seg = seg + ' bananas'
> > and it works.  So why can't I replace it with its own split?
>
> If by 'it works' you mean that tmpSegs is changed...you are mistaken or
> there is something you are not showing. Can you show why you think this
> changes tmpSegs?


I see I have been misled by trying too many things in a short time and
confusing my results.  I could have sworn that late last night, before I hit
on the solution of assigning to a new list altogether, I tried the above
examples and followed them with another "for seg in tmpSegs" loop that
simply printed the contents.

I now suspect myself of being too sleepy (and probably inadvertently
assigning to a new list anyway!) at the time, because - as you already know
- when I try it now, tmpSegs is unchanged.  I hang my head in shame.


Thanks for the responses -

Marc
-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread Eric Brunson
Kent Johnson wrote:
> Marc Tompkins wrote:
>   
>> I'm working with delimited files (ANSI X12 EDI nonsense, to be 
>> precise.)  First I load the records to a list:
>> tmpSegs = inString.split(self.SegTerm)
>>
>> Now, I want to replace each string in that list with a string:
>>
>> for seg in tmpSegs:   # 'seg' is short for 'segment'
>> seg = seg.split(self.ElemSep)
>>
>> It doesn't work.  If I check the contents of tmpSegs, it's still a list 
>> of strings - not a list of lists.  So I do this instead:
>> tmpSegs2 = []
>> for seg in tmpSegs:
>> tmpSegs2.append(seg.split(self.sElemSep))
>> del tmpSegs
>>
>> This works.  And, for the size of files that I'm likely to run into, 
>> creating the extra list and burning the old one is probably not going to 
>> swamp the machine - but it feels clumsy to me, like if I just knew the 
>> secret I could be much more elegant about it.
>> 
>
> Creating a new list is fine, actually. You can do it more elegantly with 
> a list comprehension, and it's fine to assign to the same name, which 
> will implicitly delete the old list:
> tmpSegs = [ seg.split(self.ElemSep) for seg in tmpSegs ]
>
> If you really want to replace elements in the same list you can use 
> enumerate to get indices:
>
> for i, seg in enumerate(tmpSegs):
>tmpSegs[i] = seg.split(self.ElemSep)
>   

Very nice use of enumerate(), I like this better than my solution.

Thanks, Kent.

> or use slice assignment:
> tmpSegs[:] = [ seg.split(self.ElemSep) for seg in tmpSegs ]
>
> but I doubt those are needed in this case.
>
>   
>> What puzzles me most is that I can replace 'seg' with just about 
>> anything else -
>> for seg in tmpSegs:
>> seg = seg.strip()
>> or
>> for seg in tmpSegs:
>> seg = 'bananas'
>> or
>> for seg in tmpSegs:
>> seg = seg + ' bananas'
>> and it works.  So why can't I replace it with its own split?
>> 
>
> If by 'it works' you mean that tmpSegs is changed...you are mistaken or 
> there is something you are not showing. Can you show why you think this 
> changes tmpSegs?
>
> Kent
> ___
> 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] In-place expansion of list members... possible?

2007-11-07 Thread Eric Brunson
John Fouhy wrote:
> On 08/11/2007, Marc Tompkins <[EMAIL PROTECTED]> wrote:
>   
>> Now, I did already (intellectually) understand this:
>>   A list is an array of pointers to objects, what you've done here is
>>   create a new name referencing an item in the list, then making that new
>>   name point to something different.
>> Given that, however, I still don't entirely understand why the other
>> examples I gave DO work.  Seems it should be one or the other, no?
>> 
>
> Could you post some code/results (or an interpreter session) showing
> one of the other examples working?
>
> If you did, for example,
>
> tmpSegs = inString.split(self.SegTerm)
> for seg in tmpSegs:
> seg = 'bananas'
>
> I would expect tmpSegs to be unchanged.  If it is changed, then that
> seems strange to me..
>
>   
I'd like to see it, too.  I'll admit, I didn't finish reading your post, 
so I missed replying to that, but honestly I don't think it should do 
what you say it does.

:-)

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


Re: [Tutor] In-place expansion of list members... possible?

2007-11-07 Thread John Fouhy
On 08/11/2007, Marc Tompkins <[EMAIL PROTECTED]> wrote:
> Now, I did already (intellectually) understand this:
>   A list is an array of pointers to objects, what you've done here is
>   create a new name referencing an item in the list, then making that new
>   name point to something different.
> Given that, however, I still don't entirely understand why the other
> examples I gave DO work.  Seems it should be one or the other, no?

Could you post some code/results (or an interpreter session) showing
one of the other examples working?

If you did, for example,

tmpSegs = inString.split(self.SegTerm)
for seg in tmpSegs:
seg = 'bananas'

I would expect tmpSegs to be unchanged.  If it is changed, then that
seems strange to me..

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


[Tutor] Elegant argument index sort

2007-11-07 Thread Dinesh B Vadhia
I'm sorting a 1-d (NumPy) matrix array (a) and wanting the index results (b).  
This is what I have:

b = a.argsort(0)
b = b+1

The one (1) is added to b so that there isn't a zero index element.  Is there a 
more elegant way to do this?

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


Re: [Tutor] New Introductory Book

2007-11-07 Thread wesley chun
eddie,

> Whatever the rationale for the price you could buy 2nd Ed 'Core', Chun
> *and *3rd edition(when it arrives) 'Learning Python', Lutz (the two
> standard, known and respected beginners texts) for the price of this.
> Mmm, I wonder what I would buy or rather have as a student.


i've been skimming through michael's and david's book over the past
week. fiscally, you are correct with your remark, but i have to be
honest and say that michael's and david's book spends a bit more time
introducing the concepts of OOP/OOD more carefully and more thought
out than either mine or david's and mark's books.

our books target existing programmers who (may already have some OO
under their belt and/or) want to pick up python right away, rather
than someone new to object-oriented methodologies (and perhaps
programming) using python as the primary development vehicle. of
course there is an OO intro in Core Python, but it is not a thorough
treatment. as i've hinted, it's really the target audience.

still, your point is well taken. fwiw, most aspects of the selling of
a book (including its cover price) is almost -always out of the
control of the author(s).

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


Re: [Tutor] repeated times

2007-11-07 Thread Anna Martelli Ravenscroft
I'm not sure if this is what youre asking but if you want to collect  
all of the output into a file without overwriting, open the output  
file with " a" instead of "w" and the output will. be appended. Iwould  
suggest in that case that you include info about the user input so you  
can distinguish the output . Hth

Sent from my iPhone

On Nov 7, 2007, at 6:30 PM, "linda.s" <[EMAIL PROTECTED]> wrote:

> On 11/4/07, Aditya Lal <[EMAIL PROTECTED]> wrote:
>> On 11/4/07, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
>>> * linda.s (Sun, 4 Nov 2007 01:39:46 -0800)
 On Nov 2, 2007 1:03 AM, ALAN GAULD < [EMAIL PROTECTED]>  
 wrote:
 I want to run an .exe file and get the output many times.
>>> Given that I know that you know about loops I have to
>>> ask what you see as the problem?
>>
>> I want to run it many times and export all the output to a text  
>> file.
>
> OK, Do you mean you want to run the program multiple times
> but put the output in the same file?

 Yes. For example, I want to run the exe file one hundred times and
 save all the results into one file.
 Is there any example code to do that?
>>>
>>> There's no reason to do that in Python. You should use a batch or
>>> shell script for that. If you really insist on using Python then  
>>> look
>>> at the subprocess module...
>>>
>>> Thorsten
>>>
>>
>> On Unix, you can execute "script " on the command prompt.  
>> This
>> will create a new session in which you execute the program as many  
>> times.
>> After you are done press ^D to come out of session. Everything of  
>> that
>> session will be saved in the file .
>>
>> On Windows, you can probably build something like "script" in  
>> python. BTW,
>> does your executable takes any user inputs ?
>>
>> --
>> Aditya
>
> My executable takes any user inputs and the input will be adjusted
> based on the former result. So I need do some analysis based on the
> output every time.
> hanks,
> Linda
> ___
> 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] From Numpy Import *

2007-11-07 Thread Michael H. Goldwasser

On Wednesday November 7, 2007, Dinesh B Vadhia wrote: 

>Hello!  The standard Python practice for importing modules is, for example:
>
>import sys
>import os
>etc.
>
>In NumPy (and SciPy) the 'book' suggests using:
>
>from numpy import *
>from scipy import *
>
>However, when I instead use 'import numpy' it causes all sorts of errors 
> in my existing code.

The issue is the following.  The numpy module includes many definitions, for
example a class named array.   When you use the syntax,

   from numpy import *

That takes all definitions from the module and places them into your
current namespace.  At this point, it would be fine to use a command
such as 

  values = array([1.0, 2.0, 3.0])

which instantiates a (numpy) array.


If you instead use the syntax

   import numpy

things brings that module as a whole into your namespace, but to
access definitions from that module you have to give a qualified
name, for example as

  values = numpy.array([1.0, 2.0, 3.0])

You cannot simply use the word array as in the first scenario.  This
would explain why your existing code would no longer work with the
change.

>What do you suggest?

The advantage of the "from numpy import *" syntax is mostly
convenience.   However, the better style is "import numpy" precisely
becuase it does not automatically introduce many other definitions
into your current namespace.

If you were using some other package that also defined an "array" and
then you were to use the "from numpy import *", the new definition
would override the other definition.  The use of qualified names helps
to avoid these collisions and makes clear where those definitions are
coming from.

With regard,
Michael

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


[Tutor] From Numpy Import *

2007-11-07 Thread Dinesh B Vadhia
Hello!  The standard Python practice for importing modules is, for example:

import sys
import os
etc.

In NumPy (and SciPy) the 'book' suggests using:

from numpy import *
from scipy import *

However, when I instead use 'import numpy' it causes all sorts of errors in my 
existing code.

What do you suggest?

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


Re: [Tutor] From Numpy Import *

2007-11-07 Thread Kent Johnson
Michael H. Goldwasser wrote:
>from numpy import *
>import numpy

There is a third option which provides the safety/control of import 
numpy with a little less typing:
   import numpy as np
   values = np.array([1.0, 2.0, 3.0])

and you can also import just the names you need:

   from numpy import array
   values = array(...)

Kent

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


Re: [Tutor] repeated times

2007-11-07 Thread linda.s
On 11/4/07, Aditya Lal <[EMAIL PROTECTED]> wrote:
> On 11/4/07, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> > * linda.s (Sun, 4 Nov 2007 01:39:46 -0800)
> > > On Nov 2, 2007 1:03 AM, ALAN GAULD < [EMAIL PROTECTED]> wrote:
> > > > > > >I want to run an .exe file and get the output many times.
> > > > >> Given that I know that you know about loops I have to
> > > > >> ask what you see as the problem?
> > > > >
> > > > >I want to run it many times and export all the output to a text file.
> > > >
> > > > OK, Do you mean you want to run the program multiple times
> > > > but put the output in the same file?
> > >
> > > Yes. For example, I want to run the exe file one hundred times and
> > > save all the results into one file.
> > > Is there any example code to do that?
> >
> > There's no reason to do that in Python. You should use a batch or
> > shell script for that. If you really insist on using Python then look
> > at the subprocess module...
> >
> > Thorsten
> >
>
> On Unix, you can execute "script " on the command prompt. This
> will create a new session in which you execute the program as many times.
> After you are done press ^D to come out of session. Everything of that
> session will be saved in the file .
>
> On Windows, you can probably build something like "script" in python. BTW,
> does your executable takes any user inputs ?
>
> --
> Aditya

My executable takes any user inputs and the input will be adjusted
based on the former result. So I need do some analysis based on the
output every time.
hanks,
Linda
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor