Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 11:25 PM 10/11/2007, Aditya Lal wrote:
>Hi Dick,
>
>You are deleting from the SAME list that you are traversing. This 
>results in problems.

Aditya,

Well, if deleting from the same list I'm traversing is the problem, 
why doesn't inserting the line
lstB = lstA

and deleting from lstB clear it up? The output is exactly the same. 
I'm really puzzled. See 

I thank you for showing me another way to get the result I want, but 
first I want to know why my code didn't work.

Dick

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Aditya Lal
lstB = lstA

The above just points lstB to lstA and *does not* create a copy. If you do
not understand "pointers", think of lstB an alias of lstA. If you want to
create a copy you need to explicitly do it.

lstB = []
for word in lstA :
   lstB.append(word)

or shortcut

lstB = lstA[:]

So, even though you created a new variable lstB it was actually modifying
lstA.

HTH
Aditya

On 10/12/07, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> At 11:25 PM 10/11/2007, Aditya Lal wrote:
> >Hi Dick,
> >
> >You are deleting from the SAME list that you are traversing. This
> >results in problems.
>
> Aditya,
>
> Well, if deleting from the same list I'm traversing is the problem,
> why doesn't inserting the line
> lstB = lstA
>
> and deleting from lstB clear it up? The output is exactly the same.
> I'm really puzzled. See 
>
> I thank you for showing me another way to get the result I want, but
> first I want to know why my code didn't work.
>
> Dick
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Alan Gauld
"Dick Moores" <[EMAIL PROTECTED]> wrote

> Well, if deleting from the same list I'm traversing is the problem,
> why doesn't inserting the line
> lstB = lstA
>
> and deleting from lstB clear it up? The output is exactly the same.

You are not copying the list you are just making a second name
refer to the same list. You need to make a new copy, as in:

lstB = lstA[:]   # a full slice creates a copy

But thats not an efficient approach if you have a big list, the
best route is to build a new list, preferably using a list
comprehension.

> I thank you for showing me another way to get the result I want, but
> first I want to know why my code didn't work.

The reason is as stated. Your solution didn't do what you thought it 
did :-)

The other way you can do it is to use a while loop:

n = 0
while n < len(myList):  #computed each time
 if some_test(myList[n]):
 del(myList[n])
 else:
  n += 1

Note we do not increment n when we delete an element.

I cover this in the branching topic of my tutorial in the section
Modifying collections from inside loops
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] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 12:18 AM 10/12/2007, Aditya Lal wrote:
>lstB = lstA[:]

Thanks! I'd completely forgotten how to copy a list.

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 10:53 PM 10/11/2007, Dick Moores wrote:
>Please see the code and it's output here:
>
>
>
>I'm attempting to eliminate the elements (strings) of lstA that are
>not well-formed in that they contain at least one character that is
>not in the string astr.
>
>Problems:
>1) If lstA[n] is removed, lstA[n+1] is skipped--isn't examined at
>all, and remains in the final form of lstA whether well-formed or not.
>
>Using print statements is as far as my debugging skills go, so you
>can see of bunch of them in the code. I did try to use winpdb for the
>first time, but haven't figured it out yet. Ulipad now has it built
>in, so I'd like to learn to use it it..
>
>2) I'm not able to get the "for char in wordWithCommas:" loop to stop
>examining a wordWithCommas after it's word has been removed. I've
>tried "continue" and "break" in various places to no avail.
>
>I'd appreciate some help.

Now that Aditya and Alan have come to my rescue, problem #1 has been 
solved. But #2 remains..

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 10:55 AM 10/12/2007, Kent Johnson wrote:
>Dick Moores wrote:
>>BTW is Kent's Korner still going at your SIG? Can we expect some 
>>more of these gems to appear soon on < 
>>http://personalpages.tds.net/~kent37/kk/>?
>
>Yes, it's a regular monthly feature of the meeting. I missed last 
>month but there should be another one in a few weeks. The meeting is 
>the fourth Thursday so new issues appear about that time.

Great! I've just set  to 
get a notification of a change on Kent's Korner page.

>And of course anyone near Manchester, NH on the fourth Thursday of 
>the month is welcome to drop by and get the presentation in person!

I won't be get there for a while at least--I live near Seattle. :(

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Dick Moores wrote:
> At 12:34 AM 10/12/2007, Alan Gauld wrote:
>> lstB = lstA[:]   # a full slice creates a copy
>>
>> But thats not an efficient approach if you have a big list, the
>> best route is to build a new list, preferably using a list
>> comprehension.
> 
> Alan, here's a test I made up. It doesn't show your contention is 
> correct, but I imagine you or someone else will show me I don't know 
> what I'm doing. :-)
> 
> Maybe lstA isn't big enough, or complex enough?

If all you want to do is copy the list, then
   lstB = lstA[:]
is fine, or you can use
   lstB = list(lstA)

But the original question was about *filtering* the list. With the copy 
you are back to your original problem - how do I delete items from a 
list while iterating over it? The list comp is probably faster for this 
than anything you can come up with that starts with a copy and then 
deletes items.

Kent

> 
> ===
> # timing3WaysCopyList.py
> import time
> print "starting at", time.strftime('%H:%M:%S')
> 
> n = 1
> lstA = [1000*'qwerty123456']*1000
> 
> print "First Way: lstB = lstA[:]"
> print "starting at", time.strftime('%H:%M:%S')
> timeStart = time.time()
> lstB = lstA[:]
> timeEnd = time.time()
> print "Time was %.4g seconds" % (timeEnd - timeStart)
> print
> 
> print "Second Way: for x in lstA"
> print "starting at", time.strftime('%H:%M:%S')
> timeStart = time.time()
> lstC = []
> for x in lstA:
>  lstC.append(x)
> timeEnd = time.time()
> print "Time was %.4g seconds" % (timeEnd - timeStart)
> print
> 
> print "Third Way: List Comprehension"
> print "starting at", time.strftime('%H:%M:%S')
> timeStart = time.time()
> lstD = [x for x in lstA]
> timeEnd = time.time()
> print "Time was %.4g seconds" % (timeEnd - timeStart)
> print
> ==
> 
> Output:
> 
> First Way: lstB = lstA[:]
> Time was 0.281 seconds
> 
> Second Way: for x in lstA; lstC.append(x)
> Time was 7.359 seconds
> 
> Third Way: List Comprehension [x for x in lstA]
> Time was 4.203 seconds
> ===
> 
> Dick
> 
> 
> ___
> 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] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 11:29 AM 10/12/2007, Alan Gauld wrote:
>"Dick Moores" <[EMAIL PROTECTED]> wrote
>
> >>But thats not an efficient approach if you have a big list, the
> >>best route is to build a new list, preferably using a list
> >>comprehension.
> >
> > Alan, here's a test I made up. It doesn't show your contention is
> > correct, but I imagine you or someone else will show me I don't know
> > what I'm doing. :-)
>
>Efficiency isn't always measured in terms of speed!
>
>Think about the memory resources. Imagine you have a list
>of a few million entries. Now with your approach you make
>a copy (doubling the memory usage) then delete the ones
>you don't need - possibly most of them. But with a LC you
>start with the original list and build a new list with only those
>elements you need. Thus the maximum memory use is
>the final product.
>
>The reason thats significant is that on most OS proceses
>don't release memory back to the OS untul,after they die.
>So your approach will leave your program consuming
>twice the size of the list forever more (until it dies) whereas
>the other approach only uses List1 + List2...
>
>Of course for
>1) a short lived process on a PC
>2) with lots of RAM and
>3) a single user
>
>or for small size lists
>
>then that won't make much difference.
>But if any of those conditions is not true it could be significant

Aha! See, I figured you'd tell me I was all wet.

Thanks, Alan.

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Dick Moores wrote:
> BTW is Kent's Korner still going at your SIG? Can we expect some more of 
> these gems to appear soon on < http://personalpages.tds.net/~kent37/kk/>?

Yes, it's a regular monthly feature of the meeting. I missed last month 
but there should be another one in a few weeks. The meeting is the 
fourth Thursday so new issues appear about that time.

And of course anyone near Manchester, NH on the fourth Thursday of the 
month is welcome to drop by and get the presentation in person!

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Alan Gauld
"Dick Moores" <[EMAIL PROTECTED]> wrote

>>But thats not an efficient approach if you have a big list, the
>>best route is to build a new list, preferably using a list
>>comprehension.
>
> Alan, here's a test I made up. It doesn't show your contention is
> correct, but I imagine you or someone else will show me I don't know
> what I'm doing. :-)

Efficiency isn't always measured in terms of speed!

Think about the memory resources. Imagine you have a list
of a few million entries. Now with your approach you make
a copy (doubling the memory usage) then delete the ones
you don't need - possibly most of them. But with a LC you
start with the original list and build a new list with only those
elements you need. Thus the maximum memory use is
the final product.

The reason thats significant is that on most OS proceses
don't release memory back to the OS untul,after they die.
So your approach will leave your program consuming
twice the size of the list forever more (until it dies) whereas
the other approach only uses List1 + List2...

Of course for
1) a short lived process on a PC
2) with lots of RAM and
3) a single user

or for small size lists

then that won't make much difference.
But if any of those conditions is not true it could be significant

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] 2 problems in a small script

2007-10-12 Thread Dick Moores


At 07:53 AM 10/12/2007, you wrote:
Dick Moores wrote:
Terrific, Kent. I promise I'll
use 'em whenever I can. And I'll also try again to understand generators:
<
http://docs.python.org/tut/node11.html#SECTION0011100
>. But I'll begin with the section just before that, on iterators:
<
http://docs.python.org/tut/node11.html#SECTION001190
>. Looks like tough going..
Maybe you like my writeup better:

http://personalpages.tds.net/~kent37/kk/4.html
Definitely!
And I found a couple more:
Python Generators – What Are They?

http://www.neotitans.com/resources/python-generators-tutorial.html

Tutorial on Python Iterators and Generators

http://heather.cs.ucdavis.edu/~matloff/Python/PyIterGen.pdf 
BTW is Kent's Korner still going at your SIG? Can we expect some more of
these gems to appear soon on
<
http://personalpages.tds.net/~kent37/kk/>?
There's not much reason to write
iterators from scratch anymore; in most cases it is simpler to write a
generator function. It's good to understand the concept of an iterator
but the details of the plumbing are not so important.
OK.
Thanks,
Dick



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


[Tutor] slice lists and slicing syntax questions

2007-10-12 Thread gregg
Hello Python Tutor!


I have been using Python for years now, in all kinds of environments, but
I don't really understand the restrictions on slicing.

As described in http://jtauber.com/blog/2005/08/16/python_slice_questions/
and
http://mail.python.org/pipermail/python-list/2006-February/368291.html,
in matrix oriented languages like R, Octave / Matlab, and NumPy, vectors,
lists, and matrices support slice lists of arbitrary indices.

example:  x is vector of length 5, with value "a","b","c","d","e" , then:

x[3,1,1,1,3,2] # gives [d, b, b, b, d, c]


What is the python equivalent?
  a.  Am I understanding the situation correctly?
  b.  Why was this particular way of doing slices chosen?
  c.  What is the best solution for "masking" vectors?  Is it to use
filter?Using NumPy?  To use a list comprehension, a la:

[a[j] for j in good_indices]



(I ask this because I think this is a misfeature, and I hope that I can be
corrected in my perception, not because I dislike Python.  Quite the
contrary!)

Thanks,

Gregg Lind


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Dick Moores wrote:
> Terrific, Kent. I promise I'll use 'em whenever I can. And I'll also 
> try again to understand generators: 
> . 
> But I'll begin with the section just before that, on iterators: 
> . 
> Looks like tough going..

Maybe you like my writeup better:
http://personalpages.tds.net/~kent37/kk/4.html

There's not much reason to write iterators from scratch anymore; in most 
cases it is simpler to write a generator function. It's good to 
understand the concept of an iterator but the details of the plumbing 
are not so important.

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 12:34 AM 10/12/2007, Alan Gauld wrote:
>lstB = lstA[:]   # a full slice creates a copy
>
>But thats not an efficient approach if you have a big list, the
>best route is to build a new list, preferably using a list
>comprehension.

Alan, here's a test I made up. It doesn't show your contention is 
correct, but I imagine you or someone else will show me I don't know 
what I'm doing. :-)

Maybe lstA isn't big enough, or complex enough?

===
# timing3WaysCopyList.py
import time
print "starting at", time.strftime('%H:%M:%S')

n = 1
lstA = [1000*'qwerty123456']*1000

print "First Way: lstB = lstA[:]"
print "starting at", time.strftime('%H:%M:%S')
timeStart = time.time()
lstB = lstA[:]
timeEnd = time.time()
print "Time was %.4g seconds" % (timeEnd - timeStart)
print

print "Second Way: for x in lstA"
print "starting at", time.strftime('%H:%M:%S')
timeStart = time.time()
lstC = []
for x in lstA:
 lstC.append(x)
timeEnd = time.time()
print "Time was %.4g seconds" % (timeEnd - timeStart)
print

print "Third Way: List Comprehension"
print "starting at", time.strftime('%H:%M:%S')
timeStart = time.time()
lstD = [x for x in lstA]
timeEnd = time.time()
print "Time was %.4g seconds" % (timeEnd - timeStart)
print
==

Output:

First Way: lstB = lstA[:]
Time was 0.281 seconds

Second Way: for x in lstA; lstC.append(x)
Time was 7.359 seconds

Third Way: List Comprehension [x for x in lstA]
Time was 4.203 seconds
===

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
jim stockford wrote:
> 
> On Oct 12, 2007, at 11:48 AM, Kent Johnson wrote:
> 
>> If all you want to do is copy the list, then
>>lstB = lstA[:]
>> is fine, or you can use
>>lstB = list(lstA)
> 
> why choose one over the other? is there a performance
> or other difference?

The timeit module helps figure out if there is a performance difference. 
  Slicing seems to have a slight edge over a wide range if list sizes:
src $ python -m timeit  -s 'l1=range(1000)' 'l2=list(l1)' 

10 loops, best of 3: 5.12 usec per loop
src $ python -m timeit  -s 'l1=range(1000)' 'l2=l1[:]'
10 loops, best of 3: 4.27 usec per loop

src $ python -m timeit  -s 'l1=range(10)' 'l2=list(l1)'
100 loops, best of 3: 0.487 usec per loop
src $ python -m timeit  -s 'l1=range(10)' 'l2=l1[:]'
100 loops, best of 3: 0.258 usec per loop

src $ python -m timeit  -s 'l1=range(1)' 'l2=list(l1)'
1 loops, best of 3: 126 usec per loop
src $ python -m timeit  -s 'l1=range(1)' 'l2=l1[:]'
1 loops, best of 3: 108 usec per loop

Other than that I think it is personal preference, whichever you prefer.

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Dick Moores wrote:
> At 03:41 AM 10/12/2007, Kent Johnson wrote:
>>   finalList = [ word for word in lstA if all(c in astr for c in word) ]
> 
> I'm just not comfortable with list comprehensions yet,

Hmm, too bad. I use list comps and generator expressions constantly.

> but I see that 
> although yours is very succinct, it's clear enough.

Or maybe *because* it is succinct. Most list comps and genexps are 
either applying a function to every element of a list (map):
[ f(x) for x in seq ]

or filtering a list:
[ x for x in seq if f(x) ]

and occasionally both:
[ f(x) for x in seq if g(x) ]

where f and g could be actual functions or simple expressions.

I think the reason I like them so much is because they are succinct, 
high-level, and flow in the same order as my thoughts. For example, the 
requirement "Give me a list of the square of every element of seq" 
translates directly to
[  # Give me a list
x*x# of the square
for x in seq ] # of every element of seq

and "I need a list of every element in seq that is > 2" becomes
[  # I need a list
x for x in seq # of every element in seq
if x>2 ]   # that is > 2

I have a writeup here:
http://personalpages.tds.net/~kent37/kk/3.html

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Aditya Lal wrote:
> finalList = [ word for word in lstA if wellformed(word) ]

or do everything in one statement:
   finalList = [ word for word in lstA if all(c in astr for c in word) ]

Note to Dick:
You don't have to convert word to a list before iterating it:
 wordWithCommas = list(word)
 print "wordWithCommas is", wordWithCommas
 for char in wordWithCommas:

could be just
 for char in word:

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Aditya Lal
w.r.t. prob 2, there is no break/continue in the code that you have given. I
added the "break" statement after you remove the word from lstB and code
does seems to work for me.

  
if word in lstB:
lstB.remove(word)
print
print "Removed", word
break
  

Can you specify where did you try putting break/continue ?


On 10/12/07, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> At 10:53 PM 10/11/2007, Dick Moores wrote:
> >Please see the code and it's output here:
> >
> >
> >
> >I'm attempting to eliminate the elements (strings) of lstA that are
> >not well-formed in that they contain at least one character that is
> >not in the string astr.
> >
> >Problems:
> >1) If lstA[n] is removed, lstA[n+1] is skipped--isn't examined at
> >all, and remains in the final form of lstA whether well-formed or not.
> >
> >Using print statements is as far as my debugging skills go, so you
> >can see of bunch of them in the code. I did try to use winpdb for the
> >first time, but haven't figured it out yet. Ulipad now has it built
> >in, so I'd like to learn to use it it..
> >
> >2) I'm not able to get the "for char in wordWithCommas:" loop to stop
> >examining a wordWithCommas after it's word has been removed. I've
> >tried "continue" and "break" in various places to no avail.
> >
> >I'd appreciate some help.
>
> Now that Aditya and Alan have come to my rescue, problem #1 has been
> solved. But #2 remains..
>
> Dick
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 03:41 AM 10/12/2007, Kent Johnson wrote:
>Aditya Lal wrote:
>>finalList = [ word for word in lstA if wellformed(word) ]
>
>or do everything in one statement:
>   finalList = [ word for word in lstA if all(c in astr for c in word) ]

I'm just not comfortable with list comprehensions yet, but I see that 
although yours is very succinct, it's clear enough. And I'd forgotten 
about the all() and any() (both new with 2.5 
.)


>Note to Dick:
>You don't have to convert word to a list before iterating it:
> wordWithCommas = list(word)
> print "wordWithCommas is", wordWithCommas
> for char in wordWithCommas:
>
>could be just
> for char in word:

Sure, strings are iterable. Dumb of me.

Thanks,

Dick


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Dick Moores
At 05:35 AM 10/12/2007, Kent Johnson wrote:
>Dick Moores wrote:
>>At 03:41 AM 10/12/2007, Kent Johnson wrote:
>>>   finalList = [ word for word in lstA if all(c in astr for c in word) ]
>>I'm just not comfortable with list comprehensions yet,
>
>Hmm, too bad. I use list comps and generator expressions constantly.
>
>>but I see that although yours is very succinct, it's clear enough.
>
>Or maybe *because* it is succinct. Most list comps and genexps are 
>either applying a function to every element of a list (map):
>[ f(x) for x in seq ]
>
>or filtering a list:
>[ x for x in seq if f(x) ]
>
>and occasionally both:
>[ f(x) for x in seq if g(x) ]
>
>where f and g could be actual functions or simple expressions.
>
>I think the reason I like them so much is because they are succinct, 
>high-level, and flow in the same order as my thoughts. For example, 
>the requirement "Give me a list of the square of every element of 
>seq" translates directly to
>[  # Give me a list
>x*x# of the square
>for x in seq ] # of every element of seq
>
>and "I need a list of every element in seq that is > 2" becomes
>[  # I need a list
>x for x in seq # of every element in seq
>if x>2 ]   # that is > 2
>
>I have a writeup here:
>http://personalpages.tds.net/~kent37/kk/3.html
>
>Kent

Terrific, Kent. I promise I'll use 'em whenever I can. And I'll also 
try again to understand generators: 
. 
But I'll begin with the section just before that, on iterators: 
. 
Looks like tough going..

Dick


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


[Tutor] Help with packages and namespaces please

2007-10-12 Thread Andrew Wu
Hi all,

I'm new to the list and relatively new to python, and very new to the use
and creation of packages.

Let's say I have these files in one directory:

PrintBase.py
PrintHello.py
PrintBye.py

PrintBase defines a very simple base class PrintBase, whose __init__ method
initializes a member to an optionally provided string (otherwise the string
is empty) and whose PrintMe method prints the string given (or empty string
otherwise).
PrintHello defines a class that inherits from PrintBase and whose PrintMe
method prepends a 'Hello, ' to the provided string.
PrintBye defines a class that inherits from PrintBase and whose PrintMe
method prepends a 'Good-bye' to the provided string.

Since all the files are in the same directory, I can use import statements
like from PrintHello import PrintHello ...

I'd like to reorganize the files so they're like the Sound example in the
Python tutorial:

PrintMe/
PrintMe/PrintBase/PrintBase.py
PrintMe/PrintHello/PrintHello.py
PrintMe/PrintBye/PrintBye.py

I've created empty __init__.py files in each of the subdirectories.  Here I
run into a number of problems and get confused - my goal is to keep the
import statements as they are (the actual files I'd be editing are many and
I'd rather avoid having to edit them all) - is there something I can put in
the __init__.py files so that the modules are brought into namespace w/o
having to use absolute module notation?

As simple tests, I've tried running different import commands in the python
interpreter:

I've tried adding the absolute directory paths to __path__ in each
__init__.py per subdirectory, and adding the paths to sys.path.  When I do
that and run the python intepreter and try 'from PrintMe.PrintHello import
PrintHello' I get an error saying 'Error when calling the metaclass bases,
module.__init__() takes at most 2 arguments (3 given)'.  The same code
functioned fine when all the files were in the same directory, so I'm
confused about why the interpreter thinks I'm passing 3 arguments along.

W/o those directories in sys.path I get an import error (there is no module
named 'PrintBase').  Also, if I'm in one of the subdirectories I get a
similar error when attempting to import the toplevel (PrintMe) package.

(Essentially I would like to structure the directories (er package?) so that
files in one subdirectory can subclass base classes from a sibling (not
parent) directory, ideally in a way that doesn't require each file to state
something like 'from 'PrintMe.PrintHello ... import ...' but instead 'from
PrintHello import ...'.  Is this possible, and if so how can I go about
doing it?)



Many Thanks!

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Kent Johnson
Dick Moores wrote:
> Terrific, Kent. I promise I'll use 'em whenever I can. And I'll also 
> try again to understand generators: 
> . 
> But I'll begin with the section just before that, on iterators: 
> . 
> Looks like tough going..

Maybe you like my writeup better:
http://personalpages.tds.net/~kent37/kk/4.html

There's not much reason to write iterators from scratch anymore; in most 
cases it is simpler to write a generator function. It's good to 
understand the concept of an iterator but the details of the plumbing 
are not so important.

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread jim stockford

On Oct 12, 2007, at 11:48 AM, Kent Johnson wrote:

> If all you want to do is copy the list, then
>lstB = lstA[:]
> is fine, or you can use
>lstB = list(lstA)

why choose one over the other? is there a performance
or other difference?

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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Terry Carroll
On Fri, 12 Oct 2007, Kent Johnson wrote:

> Dick Moores wrote:
>
> > I'm just not comfortable with list comprehensions yet,
> 
> Hmm, too bad. I use list comps and generator expressions constantly.

It took me a while to get list comprehensions.  It's one of those things 
that suddenly snaps into place and you're suddenly comfortable with.

What took me the longest time is to realize that it's really a way of 
generating one list from another list.  Once I got that, I started 
naturally turning to list comprehensions whenever I found myself 
generating one list from another.


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


Re: [Tutor] 2 problems in a small script

2007-10-12 Thread Alan Gauld

"Terry Carroll" <[EMAIL PROTECTED]> wrote

> It took me a while to get list comprehensions.  It's one of those 
> things
> that suddenly snaps into place and you're suddenly comfortable with.

Me too and I still don't like the syntax.

its the

[x for x in...

bit at the start that reads badly for me, I'd have preferred
if there was a seperator of some sort:

[x | for x in ...

or something. The problem is most punctuation is already
used for something that is valid in an expression. The only thing that
I think is OK is an exclamation mark but that, although somewhat
similar to a | looks like a factorial operarion.

But once you get used to it they are concise if not quite as
descriptive as their functional.equivalents map/filter.

Alan G. 


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


Re: [Tutor] slice lists and slicing syntax questions

2007-10-12 Thread Alan Gauld
<[EMAIL PROTECTED]> wrote

> in matrix oriented languages like R, Octave / Matlab, and NumPy, 
> vectors,
> lists, and matrices support slice lists of arbitrary indices.

Python is a general purpose language and not matrix oriented.
In fact it doesn't have native support for a matrix as such only
through add on modules such as numpy.

So not surprisingly normal python slicing doesn't do fancy tricks
As a recent thread on extended slicing shows numpy (and
potentially, other third party modules) can do fancy tricks with
slicing of multi dimensional structures, but they aren't 'standard'
python.

In fact, Python was the first language I had seen that treated
slicing as an operation and I thought it was really neat!
Up till then(~1997) I had always had to write functions to do that
(or use library functions if they existed).

> example:  x is vector of length 5, with value "a","b","c","d","e" , 
> then:
>
> x[3,1,1,1,3,2] # gives [d, b, b, b, d, c]
>
> What is the python equivalent?
>
> [a[j] for j in good_indices]

I think that's probably as close as you can get.
I can't think of anything more concise.

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] 2 problems in a small script

2007-10-12 Thread Ian Witham
On 10/12/07, Dick Moores <[EMAIL PROTECTED]> wrote:
>
> Please see the code and it's output here:
> 
>
>
> I'm attempting to eliminate the elements (strings) of lstA that are
> not well-formed in that they contain at least one character that is
> not in the string astr.



Hi Dick,

If your interested, this could be done succinctly with set types. (no
iterating required!)

def well_formed(word):
return not set('#.<@') & set(word)

>>> well_formed('porkpie')
True
>>> well_formed('p#rkpie')
False

And then it seems you are coming to terms with list comprehensions...

my_list = [word for word in my_list if well_formed(my_word)]

OR...

my_list = filter(well_formed, my_list)

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