Re: [Tutor] Question regarding list editing (in place)

2008-02-14 Thread David J. Weller-Fahy
Sorry for the delayed reply - the list software was "helping" me by not 
sending me the list copy.  Heh.

* Kent Johnson <[EMAIL PROTECTED]> [2008-02-05 13:43]:
> bob gailer wrote:
>> dirs = [dir for dir in dirs if not dir.startswith(u'.')]
>
> Except to filter the directory list for os.walk() you have to modify the 
> list in place. Use this:
> dirs[:] = [dir for dir in dirs if not dir.startswith(u'.')]

That is exactly what I was missing!  Thanks for the nudge, that works.

... later ...

Of course, if I had paid more attention to section "3.1.4 Lists" in the 
python tutorial, I might have noticed the example which is similar to that 
form:

#v+
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
#v-

Ah, well.  Thanks again for the tips, gentlemen.

Regards,
-- 
David J. Weller-Fahy| 'These are the questions that kept me out
largely at innocent dot com |  of the really *good* schools.'
dave at weller-fahy dot com |  - One of The Group
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] designing POOP

2008-02-14 Thread Tiger12506
> Now I'm curious.
>
> MVC is one of the oldest, best established and well proven design
> patterns going. It first appeared in Smalltalk in the late 1970's and
> has been copied in almost every GUI and Web framework ever since.
> I've used it on virtually(*) every GUI I've ever built(**) to the
> extent that
> I don't even think about it much anymore, therefore:
>
> What do you dislike about it? And what do you use in its place?

I may not be the most newbie around here, but I am certainly very newbish 
compared to you and Kent. That means that I do not have perhaps an adequate 
amount of experience in practical application, but I dislike the concept 
because in every situation that I have encountered so far, it has tripped me 
up. I like things to be explicit, and don't care for the level of 
abstraction common in MVC. I know it seems naive, but I like to be able to 
model object designs after tangible things, and to me, a View does not know 
how to keep a state or execute methods because it does not exist. A 
Controller does not exist alone of the model, and without the view it fails 
to work. Sure it helps tremendously in interchanging code pieces-reusability 
a big issue here, but for my purposes, it just seems extraneous.

Seperating drawing and computations can be/cannot be convenient for me 
depending on the application, (my 'plotting' library is a code reusability 
mess) but in general my style of coding (more procedural) tends to get the 
job done and also tends to cut corners in just enough places to make me feel 
that the application is more efficient with resources.

Asking what I use in its place very neatly makes me blush because I use no 
set model, but tend to mix them at my convenience. Have you noticed that I 
haven't posted code on this list in a long, long time??? Hehe.

At any rate, I had noticed that I stepped past my line of authority and 
experience to the point at which I was significantly less capable to pose as 
an expert, and therefore I wrote "everyone but me would be wise to use it". 
My apologies.

> (*)The only exceptions were ObjectVision, a strange graphical
> Windows app builder from Borland and a weird scripting language for
> the
> very early X Windows toolkits(X10), which looked like csh and produced
> truly ugly GUIs very quickly. And, of course, native Tcl/Tk has a
> slightly
> different model.
>
> (**)Those include GUIs built in Smalltalk, Lisp, TurboPascal, Delphi,
> C/C++, ObjectiveC, Java, Python (of course) and even PL/SQL
>
> An alternative perspective is always interesting.
>
> Alan G.

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


Re: [Tutor] most efficient way to do this

2008-02-14 Thread bob gailer
Christopher Spears wrote:
> I created a file called arrays.py:
>
> #!/usr/bin/python
>
> locations = ["/home/",
> "/office/" ,
> "/basement/" ,
> "/attic/"]
>
> Now I want to add the word "chris" on to each element
> of the locations list, so I wrote another script
> called chris_arrays.py:
>
> #!/usr/bin/python/
>
> from arrays import locations
>
> add_on = "chris"
> new_loc = []
>
> for i in range(len(locations)):
> new_loc.append(locations[i] + add_on)
>   
Alan gave the "best" answer.

FWIW I offer an intermediate improvement:

for location in locations:
new_loc.append(location + add_on)


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] most efficient way to do this

2008-02-14 Thread Alan Gauld

"Christopher Spears" <[EMAIL PROTECTED]> wrote 

> from arrays import locations
> 
> add_on = "chris"
> new_loc = []
> 
> for i in range(len(locations)):
>new_loc.append(locations[i] + add_on)
> 
> print new_loc
> 
> Is this the most efficient way to do this?  

No.
For a start you could use a direct for loop rather than 
the index method. But even better this is a standard 
list comprehension job:

add_on = 'chris'
new_loc = [item+add_on for item in locations]

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] designing POOP

2008-02-14 Thread Alan Gauld
"Tiger12506" <[EMAIL PROTECTED]> wrote

> Ah yes... I don't like the Model-View-Controller architecture. That 
> the
> major reason why I dislike most information available on C++. This 
> is a
> personal issue though. The Model-View-Controller is a very common 
> thing, and
> everyone but me would be wise to use it. :-)

Now I'm curious.

MVC is one of the oldest, best established and well proven design
patterns going. It first appeared in Smalltalk in the late 1970's and
has been copied in almost every GUI and Web framework ever since.
I've used it on virtually(*) every GUI I've ever built(**) to the 
extent that
I don't even think about it much anymore, therefore:

What do you dislike about it? And what do you use in its place?

(*)The only exceptions were ObjectVision, a strange graphical
Windows app builder from Borland and a weird scripting language for 
the
very early X Windows toolkits(X10), which looked like csh and produced
truly ugly GUIs very quickly. And, of course, native Tcl/Tk has a 
slightly
different model.

(**)Those include GUIs built in Smalltalk, Lisp, TurboPascal, Delphi,
C/C++, ObjectiveC, Java, Python (of course) and even PL/SQL

An alternative perspective is always interesting.

Alan G.



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


[Tutor] most efficient way to do this

2008-02-14 Thread Christopher Spears
I created a file called arrays.py:

#!/usr/bin/python

locations = ["/home/",
"/office/" ,
"/basement/" ,
"/attic/"]

Now I want to add the word "chris" on to each element
of the locations list, so I wrote another script
called chris_arrays.py:

#!/usr/bin/python/

from arrays import locations

add_on = "chris"
new_loc = []

for i in range(len(locations)):
new_loc.append(locations[i] + add_on)

print new_loc

Is this the most efficient way to do this?  I'm using
these scripts to test some changes I want to make to a
script at work.  Basically, I want one file to hold a
list of paths.  The other file imports the list and
then asks the user for input that it then adds on to
each string in the list.  I want the paths to be in a
separate file, so I can access these paths with
different scripts.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] read from standard input

2008-02-14 Thread Tiger12506
> while 1 < 2:
while 1:
or
while True:
is more common

>x = raw_input()
raw_input() always return a string, no matter what you type in.

>if type(x) != int or x == 11:
type(x) is always 

x can never be 11, but can possibly be '11'.  (Notice quotes indicating 
string instead of integer)

If you want an integer you must say
x = int(raw_input())

>break
>else:
>print x
> [/code]
>
> but don't work. and i'm interest in a general way to read until it is
> nothing to read.
>
>
> to ilustrate that in C:
> [code]
> int x;
> while( scanf("%d",&x) == 1 && x != 11)
> printf("%d\n", x);
> [/code]
>
>
> Thanks!
>





> ___
> 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] designing POOP

2008-02-14 Thread Tiger12506
> Hmm. Not to me. The second version couples the game state with the 
> display. I would rather have

True...

> This is an example of Model-View-Controller architecture (google it). 
> Notice that the Game and Display are now reusable (maybe there are both 
> GUI and text interfaces to the game, for example, or versions for wxPython 
> and PyQt) and testable independently of each other.

Ah yes... I don't like the Model-View-Controller architecture. That the 
major reason why I dislike most information available on C++. This is a 
personal issue though. The Model-View-Controller is a very common thing, and 
everyone but me would be wise to use it. :-) 

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


Re: [Tutor] Binary chop function - this works, but I'm not sure why

2008-02-14 Thread Arun Srinivasan
On Thu, Feb 14, 2008 at 11:41 AM, Arun Srinivasan
<[EMAIL PROTECTED]> wrote:
>
> On Thu, Feb 14, 2008 at 5:27 AM, bob gailer <[EMAIL PROTECTED]> wrote:
>  >
>  > Arun Srinivasan wrote:
>  > > I'm trying to learn Python, and I decided to try kata 2  from the
>  > > CodeKate website. It's basically just a challenge to implement a
>  > > binary search in different ways.
>  > >
>  > > I wrote an implementation that works, but I'm confused as to why.
>  > >
>  > > def chop(search_int, sorted_list):
>  > > if len(sorted_list) == 1 or 2:
>  > Yet another way to express that is:
>  >
>  >   if 1 <= len(sorted_list) <= 2:
>  >
>  > > for x in sorted_list:
>  > > if x == search_int:
>  > > return sorted_list.index(x)
>  > > return -1
>  > >
>  > > midpoint = (len(sorted_list) - 1) / 2
>  > > mp_value = sorted_list[midpoint]
>  > >
>  > > if mp_value == search_int:
>  > > return midpoint
>  > > elif mp_value > search_int:
>  > > return chop(search_int, sorted_list[:midpoint])
>  > > else:
>  > > return chop(search_int, sorted_list[midpoint + 1:])
>  > >
>  > > Basically, it only returns the index if it matches in the if statement
>  > > at the beginning of the function, but since that is limited to lists
>  > > of length 2 or 1, why doesn't it return only 0 or 1 as the index? I
>  > > think there is something about recursion here that I'm not fully
>  > > comprehending.
>  > >
>  > >
>  > >
>  > > 
>  > >
>  > > ___
>  > > Tutor maillist  -  Tutor@python.org
>  > > http://mail.python.org/mailman/listinfo/tutor
>  > >
>  >
>  >
>  > --
>  > Bob Gailer
>  > 919-636-4239 Chapel Hill, NC
>  >
>  >
>
>  Oy, I should have seen that. Thanks for the responses - time to go
>  back and fix this.
>

Turns out it was simple to fix - just needed to fix that test so it
wasn't silly, add the lower bound tracking (thanks Alan), and make
sure to test for empty lists. Here's the function in all (rather, what
little there is) of its glory:

def chop(search_int, sorted_list, lbound = 0):
if len(sorted_list) == 0:
return -1

if len(sorted_list) in [1,2]:
if sorted_list[0] == search_int: return lbound
elif len(sorted_list) == 2 and  sorted_list[1] == search_int:
return lbound + 1
else: return -1

midpoint = (len(sorted_list) - 1) / 2
mp_value = sorted_list[midpoint]

if mp_value == search_int:
return midpoint
elif mp_value > search_int:
return chop(search_int, sorted_list[:midpoint])
else:
return chop(search_int, sorted_list[midpoint + 1:], lbound +
midpoint + 1)

Alan, could you please explain why

if sorted_list[0] == search_int: return 0
elif len(sorted_list) == 2 and  sorted_list[1] == search_int: return 1
else: return -1

is faster than:

>for x in sorted_list:
>if x == search_int:
>return sorted_list.index(x)
>return -1

Thanks for the help!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Binary chop function - this works, but I'm not sure why

2008-02-14 Thread Arun Srinivasan
On Thu, Feb 14, 2008 at 5:27 AM, bob gailer <[EMAIL PROTECTED]> wrote:
>
> Arun Srinivasan wrote:
> > I'm trying to learn Python, and I decided to try kata 2  from the
> > CodeKate website. It's basically just a challenge to implement a
> > binary search in different ways.
> >
> > I wrote an implementation that works, but I'm confused as to why.
> >
> > def chop(search_int, sorted_list):
> > if len(sorted_list) == 1 or 2:
> Yet another way to express that is:
>
>   if 1 <= len(sorted_list) <= 2:
>
> > for x in sorted_list:
> > if x == search_int:
> > return sorted_list.index(x)
> > return -1
> >
> > midpoint = (len(sorted_list) - 1) / 2
> > mp_value = sorted_list[midpoint]
> >
> > if mp_value == search_int:
> > return midpoint
> > elif mp_value > search_int:
> > return chop(search_int, sorted_list[:midpoint])
> > else:
> > return chop(search_int, sorted_list[midpoint + 1:])
> >
> > Basically, it only returns the index if it matches in the if statement
> > at the beginning of the function, but since that is limited to lists
> > of length 2 or 1, why doesn't it return only 0 or 1 as the index? I
> > think there is something about recursion here that I'm not fully
> > comprehending.
> >
> >
> >
> > 
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> --
> Bob Gailer
> 919-636-4239 Chapel Hill, NC
>
>

Oy, I should have seen that. Thanks for the responses - time to go
back and fix this.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Binary chop function - this works, but I'm not sure why

2008-02-14 Thread bob gailer
Arun Srinivasan wrote:
> I'm trying to learn Python, and I decided to try kata 2  from the 
> CodeKate website. It's basically just a challenge to implement a 
> binary search in different ways.
>
> I wrote an implementation that works, but I'm confused as to why.
>
> def chop(search_int, sorted_list):
> if len(sorted_list) == 1 or 2:
Yet another way to express that is:

   if 1 <= len(sorted_list) <= 2:
> for x in sorted_list:
> if x == search_int:
> return sorted_list.index(x)
> return -1
>
> midpoint = (len(sorted_list) - 1) / 2
> mp_value = sorted_list[midpoint]
>
> if mp_value == search_int:
> return midpoint
> elif mp_value > search_int:
> return chop(search_int, sorted_list[:midpoint])
> else:
> return chop(search_int, sorted_list[midpoint + 1:])
>
> Basically, it only returns the index if it matches in the if statement 
> at the beginning of the function, but since that is limited to lists 
> of length 2 or 1, why doesn't it return only 0 or 1 as the index? I 
> think there is something about recursion here that I'm not fully 
> comprehending.
>
>
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] Binary chop function - this works, but I'm not sure why

2008-02-14 Thread Alan Gauld

"Arun Srinivasan" <[EMAIL PROTECTED]> wrote

> I wrote an implementation that works, but I'm confused as to why.
>
> def chop(search_int, sorted_list):
>if len(sorted_list) == 1 or 2:

|This is not doing what you think it is.

Pythopn sees this as:

if ( len(sorted_list == 1) or 2:

So it evaluates whether the list is one element, if it isn't
it then checks if 2 is true, which it always is. So the if
condition is always trie and yuou code always executes
the if block. Now the if block as written will always find
the element in the list if it exists.

If you rewrote the if test like so you would get a more
effective test:

if len(sorted_list) in [1,2]:

>for x in sorted_list:
>if x == search_int:
>return sorted_list.index(x)
>return -1

You could also rewrite the block as:

if sorted_list[0] == search_int: return 0
elif len(sorted_list) == 2 and  sorted_list[1] == search_int: return 1
else return -1

Which should be marginally faster and only works for lists of
length 1 or 2.

>midpoint = (len(sorted_list) - 1) / 2
>mp_value = sorted_list[midpoint]
>
>if mp_value == search_int:
>return midpoint
>elif mp_value > search_int:
>return chop(search_int, sorted_list[:midpoint])
>else:
>return chop(search_int, sorted_list[midpoint + 1:])
>
> Basically, it only returns the index if it matches in the if 
> statement at
> the beginning of the function, but since that is limited to lists of 
> length
> 2 or 1, why doesn't it return only 0 or 1 as the index? I think 
> there is
> something about recursion here that I'm not fully comprehending.

The rest of the code is broken because, as you say, it only returns 0 
or 1.
you need to add the lower bound to the return value but you don't 
track
the lower bound!

Alan G. 


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