Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread Alan Gauld
> all_types:
>  datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
>  datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> .
>  datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> there is such a list
>
> how do i search for a particular string say teststring in this list

This isn't a list in any sense that Python will recognise.
Can you show us the actual data structures please?

> if teststring in all_types:

This would only work if teststring was exactly equal to a
whole string in your list, partial string matches only work against a 
single string:

>>> if "fred" in ["fred", "jack"]: print True
True
>>> if "red"  in ["fred", "jack"]: print True
>>>

>>> for item in ["fred", "jack"]:
>>>  if "red" in item: print item
...
fred
>>>

> if teststring in all_types.type:

No idea what you think this would do.
lists don't have a type attribute and you can't access bits
of strings that way either.

What Python tutorial are you reading?
Almost all the tutorials cover string searching and list access.

> should i use lambda

Almost certainly not.
Why do you think lambda would be useful here?

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] Exercise in writing a python function.

2006-08-09 Thread Alan Gauld
> The current specifications for the function are:
>
> def incr(mult,z,zlim,mpylist):
> # mult is a vector of exponents for the multipliers in mpylist.
> # z is a positive odd  integer.
> # zlim is the upper bound  critical value for the sum of  ( 
> mpylist[k][0] *
> mpylist[k][1] )

Just to clarify what this means.
you have mpylist = [(2,3),(3,5),(5,7)]
So the products list is: [6,15,35]
So zlim in this case should be greater than the
sum of products: 6+15+35 = 56

Have I got that right?

> # where kth multiplier is mpylist [k][0]
> # and kth  multiplier index is mpylist [k][1]

Not sure what you mean by the multiplier index?
The sum above shows mpylist[k][1] being used in the multiplication,
not as an index?

> # function incr returns the next value of vector mult.

I'm still not clear what mult does, your example above doesn't
refer to mult anywhere?

> # mult may be thought as a number written in a variable base.
> # mult[0] is the least significant and matches multiplier 
> mpylist[0][0]
> # adding one to mult would mean adding 1 to mult[0]
> # unless doing so would make sum of multiplier * index exceed zlim.
> # in that case mult[0] is set to zero, and 1 is added to mult[1]
> # unless doing so would make sum of multilier * index exceed zlim
> # in that case, mult[0] and mult[1] is set to zero,
> # and 1 is added to mult[2]
> # unless . . .

Sorry, you lost me there, can you provide a concrete example?
Or maybe some of our resident math experts recognise what
you are up to and can explain? Brian? Danny?

> # mult[0] is set to -1 to indicate that the largest possible value 
> of mult
> has been exceeded.
> # mpylist[0][0] = 2 and all other multipliers  in mpylist are odd.
> # mult[0], the index on multiplier 2, must not equal 1.  It may 
> equal zero,
> or any integer > 1,
> # provided the zlim constraint is met.

The only guidance I'd give in this kind of situation is that I'd adopt
functional programming approaches and try to copy the structure
of the spec in my code. This will be a bitch to test/debug unless
you have a wealth of manually validated examples to use as test
cases!

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] Unusual behavior in readline

2006-08-09 Thread Alan Gauld


>>>You don't need to initialise Entry, the for loop does that for you.
>
> just a habit- I've always initialized my vars up front.
> so I know they are initialized.

Fair enough, good practice for other languages certainly.

>> >>BTW Why not just put all this stuff in the body of the try?
>
> Because the try is only setup to catch an IO error.
> If something other than an IOerror were to occur, it would be
> missed unless other exceptions were trapped.

Its missed in the else clause too.
But the else clause adds two extra control structures
(the except and the else) to the code for the reader to
negotiate which significantly impairs comprehension
of the main execution path.

> The most probable is the IOerror, which is why I trapped it.

Which is fine but no reason to put the except clause immediately
after the try line, that just clutters the code block.

>>>You should very rarely need an else clause when using try/except.

> If the exception doesn't happen, then what ?

The code just goes through:

try:
   if False: raise ValueError# never happens
   print 'hello'
   print 'world'
   print 'this code is pretty safe'
except ValueError: print 'its a mistake!"

No need for an else clause.

> That's what the else clause is for

Nope, the else clause is for a few unusual cases where you
need to do stuff if no exception occurs that is not part of the
main processing. I've only ever seen a real valid use of it
once in dealing with housekeeping of sockets, and even
there a finally block could have been used at the cost of
an extra try statement. The else just kept it a bit tidier.

But else should not be used for mainstream processing.
One of the biggest advantages of try/except handling is that
it removes all the error processing out of the way of the
reader to the bottom of the block. You can just write the
happy path stuff as a continuous sequence without worrying
about error paths. (FWIW This is also the recommended
practice in writing use cases, for the same reasons)

Of course else can be used as you did and the code will
work, but you lose a big advantage of try/except style.

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] searching for a string in a dictionary

2006-08-09 Thread John Fouhy
On 09/08/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > all_types:
> >  > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> >  > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> > .
> >  > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> > there is such a list
> >
> > how do i search for a particular string say teststring in this list
>
> This isn't a list in any sense that Python will recognise.
> Can you show us the actual data structures please?

I think what he wants is:

if 'teststring' in [s.type for s in all_types]

...

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


Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread Alan Gauld

> On 09/08/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
>> > all_types:
>> > > > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
>> >
>> > how do i search for a particular string say teststring in this 
>> > list
>>

> I think what he wants is:
>
>if 'teststring' in [s.type for s in all_types]

But type is still not accessible as an attribute.
It looked to me like he wanted a list of dictionaries
in which case it wouyld be:

if teststring in [s['type'] for s in all_types]

But the submitted data is not in any usable format so far
as Python is concxerned and the data structure will be
critical to how the code works.

Anil, we need to get more specific information from you
about your code and error messages, we can't guess
what you have done.

Alan G 

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


Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread John Fouhy
On 09/08/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> But type is still not accessible as an attribute.
> It looked to me like he wanted a list of dictionaries
> in which case it wouyld be:
>
> if teststring in [s['type'] for s in all_types]

Err, that is what I meant :-)

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


[Tutor] python import problem

2006-08-09 Thread kakada
Hi all,

I have problem with import statement.

supposed that I have two files in different folders : modules/myfile.py
and translate/factory.py.


the folders modules and translate are in the same level, so if I want to
import factory.py into myfile.py, how can I do?


I have already done in myfile.py:

from translate import factory

but it is still not work. Any solution?

Thanks and regards,
da
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread anil maran
if teststring in [s['type'] for s in all_types]:                print 'contained'            else:                print 'notcontained'this workedthanks a lot guyspyTutor rocksAlan Gauld  wrote: > On 09/08/06, Alan Gauld <[EMAIL PROTECTED]> wrote:>> > all_types:>> > >> > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>>> >>> > how do i search for a particular string say teststring in this >> > list>>>
 I think what he wants is:>>if 'teststring' in [s.type for s in all_types]But type is still not accessible as an attribute.It looked to me like he wanted a list of dictionariesin which case it wouyld be:if teststring in [s['type'] for s in all_types]But the submitted data is not in any usable format so faras Python is concxerned and the data structure will becritical to how the code works.Anil, we need to get more specific information from youabout your code and error messages, we can't guesswhat you have done.Alan G  
		Get your email and more, right on the  new Yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] html file - construct attach send... email python

2006-08-09 Thread anil maran
What is the best way to construct an email in python and also attach a html filethe html file to be attached is not on disk, but should be dynamically constructed in the python scriptthanks a lot 
		Stay in the know. Pulse on the new Yahoo.com.  Check it out. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread anil maran
doesTypeExist=filter(lambda x: typeofstring in x.type, all_types) if 'teststring' in [s.type for s in all_types]this works which is better in terms of speed???for+in or filter+lambdathanks a lotAlan Gauld <[EMAIL PROTECTED]> wrote: > On 09/08/06, Alan Gauld <[EMAIL PROTECTED]> wrote:>> > all_types:>> > >> > datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>>> >>> > how do i search for a particular string say teststring in this >> > list>>> I think what he wants is:>>if
 'teststring' in [s.type for s in all_types]But type is still not accessible as an attribute.It looked to me like he wanted a list of dictionariesin which case it wouyld be:if teststring in [s['type'] for s in all_types]But the submitted data is not in any usable format so faras Python is concxerned and the data structure will becritical to how the code works.Anil, we need to get more specific information from youabout your code and error messages, we can't guesswhat you have done.Alan G  
	
	
		Want to be your own boss? Learn how on  Yahoo! Small Business. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Global Variables

2006-08-09 Thread Magnus Wirström
Hi

I know that this was in the list in the last days but i deleted it by 
mistake. How to use global variables? Or perhaps you could point me to 
another solution. I'm converting a packup app that i made to a gui 
driven app. The console app was very straight forward and didn't use any 
own functions. That was working fine then but i discover now that i 
probably have to rethink that because i need to communicate to different 
events. I'm new to all this so i have a problem to approach this. I 
would be really happy if you could give me hints and tips or suggestions 
how to make this. The gui i want to work with is wxPython and i'm using 
Boa Constructor to build my gui and app.

Thanks
Magnus

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


[Tutor] about tkinter

2006-08-09 Thread linda.s
Is that possible to open two Tkinter from one python shell?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] searching for a string in a dictionary

2006-08-09 Thread Alan Gauld
> doesTypeExist=filter(lambda x: typeofstring in x.type, all_types)
>
> if 'teststring' in [s.type for s in all_types]
> this works which is better in terms of speed???

These do two different things.
The first returns a list of all_types entries that match
The second returns true or false if *any* string matches.

The first can be rewritten without lambda using a list comprehension:

[ item for item in all_types if teststring in item['type'] ]

Calling functions in python is usually fairly slow so using 
filter/lambda
with its function call per item is probably slower than either of the
list comprehension methods. If in doubt measure it...

Alan G.

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


Re: [Tutor] python import problem

2006-08-09 Thread Alan Gauld
> the folders modules and translate are in the same level, so if I 
> want to
> import factory.py into myfile.py, how can I do?

You need to make your folders into packages.
Its fairly simple, you basically create an init.py file
But its worth reading about packages in the docs
Come back here ifv you have any questions after that.

The other way would be to add each folder to
your PYTHONPATH

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] Exercise in writing a python function.

2006-08-09 Thread Kermit Rose
 
 
From: Alan Gauld 
Date: 08/09/06 03:30:28 
To: Kermit Rose; tutor@python.org 
Subject: Re: [Tutor] Exercise in writing a python function. 
 
> The current specifications for the function are: 
> 
> def incr(mult,z,zlim,mpylist): 
> # mult is a vector of exponents for the multipliers in mpylist. 
> # z is a positive odd integer. 
> # zlim is the upper bound critical value for the sum of ( 
> mpylist[k][0] * 
> mpylist[k][1] ) 
 
Just to clarify what this means. 
you have mpylist = [(2,3),(3,5),(5,7)] 
So the products list is: [6,15,35] 
So zlim in this case should be greater than the 
sum of products: 6+15+35 = 56 
 
***
 
Oops.  That is what  I said.
 
I made a mistake in the specificiations.
 
I should have specified that 
for this example
that
3*mult[0] + 5 * mult[1] + 7 * mult[2] must be < zlim.
 
>
 
 
 
# where kth multiplier is mpylist [k][0] 
> # and kth multiplier index is mpylist [k][1] 
 
Not sure what you mean by the multiplier index? 
The sum above shows mpylist[k][1] being used in the multiplication, 
not as an index? 
 
 
**
 
Yes.  It's confusing because of my mistake of confusing mpylist[k][1] with
mult[k].
 
 

 
> # function incr returns the next value of vector mult. 
 
I'm still not clear what mult does, your example above doesn't 
refer to mult anywhere? 
 
 
*
 
Yes, another consequence of my mistake in the specifications.
 
 
Kermit   <  [EMAIL PROTECTED]   >
 
 

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


[Tutor] Exercise in writing a python function

2006-08-09 Thread Kermit Rose
  
Message: 2
Date: Wed, 9 Aug 2006 16:04:27 +1200
From: "John Fouhy" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Exercise in writing a python function.
To: "Tutor mailing list" 
 
 
Hi Kermit,
 
Your basic data structure is a list (actually, several related lists),
which you work your way through.  So I would start off with a for
loop:
 
for k in range(len(mult)):
 
Then, in the body of the loop, your basic logic is:
 
Add 1 to mult[k].
If mult[k] is not too big, exit.
Otherwise, ...
 
Hmm, actually, I'm not sure I do understand.  Does mpylist ever
change?  How is mult related to zlim?
 
--
John.
 
*
 
Thank you.
 
I confused things by my mistake in the specifications.
 
The criterion for exiting the outer loop is that
 
mpylist[0][1] * mult[0] + mpylist[1][1] * mult[1] + mpylist[2][1] * mult[2]
+ . . . 
 
be > zlim.
 
 
 
 
 

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


[Tutor] python

2006-08-09 Thread Tom Wilson
could someone explain to me how the "import" function works?

tom

_
The new Windows Live Toolbar helps you guard against viruses 
http://toolbar.live.com/?mkt=en-gb

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


[Tutor] programming exercise in Python

2006-08-09 Thread Kermit Rose
 
 
Thanks to John,
 
I've written tenative code for the incr routine.
 
Now to test and possibly debug it.
 
Here is the routine.  It's very short.
 
#
#
# def incr(mult,zlim,mpylist):
# # mult is a vector of exponents for the multipliers in mpylist.
# # z is the integer to be factored
# # zlim is the critical value for the sum of multiplier * index
# # where kth multiplier is mpylist [k][0]
# # and kth index is mpylist [k][1]
#
# # function incr returns the next value of vector mult.
# # mult may be thought as a number written in a variable base.
# # mult[0] is the least significant and matches multiplier mpylist[0][0]
# # adding one to mult would mean adding 1 to mult[0]
# # unless doing so would make sum of mpylist[k][1] * mult[k] exceed zlim.
# # in that case mult[0] is set to zero, and 1 is added to mult[1]
# # unless doing so would make sum of multilier * index exceed zlim
# # in that case, mult[0] and mult[1] is set to zero,
# # and 1 is added to mult[2]
# # unless . . .
#
#
# # mult[0] is set to -1 to indicate that the largest possible value of mult
has been exceeded.
# # mpylist[0] = 2 and all other values in mpylist are odd.
# # mult[0], the index on multiplier 2, must not equal 1.  It may equal zero
 or any integer > 1,
# # provided it meets the zlim constraint.
#
# lenmult = len(mult)
# for k in range(lenmult):
# mult[k] = mult[k] + 1
# if k == 0:
# if mult[k] == 1:
#mult[k] = 2 
# testsum = 0
# if k > 0:
# mult[k-1] = 0
# for j in range(k,lenmult):
# testsum = testsum + mpylist[j][1] * mult[j]
# if testsum < zlim:
# return mult
# mult[0] = -1
# return mult
#
#

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


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Danny Yoo


On Wed, 9 Aug 2006, Kermit Rose wrote:


> I've written tenative code for the incr routine.
>
> Now to test and possibly debug it.

Just as a note: it's usually a much better idea to write your test cases 
first, even before writing any code.  It'll give you a better idea of what 
you want your function to do.

(It'll also probably give the other here a better idea of what the 
function is supposed to do.  I myself don't quite know either what it's 
supposed to do based solely on the description.  *grin*)


What are some small test cases you're thinking of for this function?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about tkinter

2006-08-09 Thread Danny Yoo


On Wed, 9 Aug 2006, linda.s wrote:

> Is that possible to open two Tkinter from one python shell?


It is possible to open more Tkinter "toplevel" windows.  Is this what you 
are asking for?  See:

 http://www.pythonware.com/library/tkinter/introduction/toplevel.htm

If we build a new Toplevel, we should see a new window on our screen.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python

2006-08-09 Thread Danny Yoo


On Tue, 8 Aug 2006, Tom Wilson wrote:

> could someone explain to me how the "import" function works?

Hi Tom,

Take a look at:

 http://www.python.org/doc/tut/node8.html

and see if that helps; if not, come back and please feel free to ask more 
questions about it.  In a short summary: import allows us to load 
additional "modules" into our program.  These modules are often written by 
other people, and provide a lot of interesting functions.  There are a set 
of modules that come standard with each Python distribution:

 http://www.python.org/doc/lib/

For example, if we wanted to get an approximate value for pi, we could 
find that in the 'math' module:

 http://www.python.org/doc/lib/module-math.html

and to use it, we'd use 'import' to first pull math support into our 
program:

##
import math
##

after which we can reach in and get a piece of pi:

##
print math.pi
##
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] html file - construct attach send... email python

2006-08-09 Thread Danny Yoo


On Wed, 9 Aug 2006, anil maran wrote:

> What is the best way to construct an email in python and also attach a 
> html file


Hi Anil,

This is a repeat of one of your previous questions; you just sent this 
question out yesterday:

 http://mail.python.org/pipermail/tutor/2006-August/048452.html

We are forgetful, but we're not THAT senile yet.

Please do not repost your questions so early: give some people more time 
to construct a good answer for you.  Repeating a question so soon like 
this tends to demotivate people.


Have you looked at the 'email' module?

 http://www.python.org/doc/lib/module-email.html

And in general, do you know about the Standard Library, or the Python 
Cookbook?

 http://www.python.org/doc/lib/
 http://aspn.activestate.com/ASPN/Python/Cookbook/

I want to make sure you know about these documentation resources.


Good luck to you.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise in writing a python function

2006-08-09 Thread Alan Gauld
> I confused things by my mistake in the specifications.
>
> The criterion for exiting the outer loop is that
>
> mpylist[0][1] * mult[0] + mpylist[1][1] * mult[1] + mpylist[2][1] * 
> mult[2]
> + . . .
>
> be > zlim.

Can you try rewriting the spec correctly, because I'm still not
sure I understand it? Are you now saying that mpylist[n][0] is never 
used?

Can you show us some sample data structures - just a few entries
and how the calculations should work?

Alan G. 

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


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Kermit Rose
 
 
From: Danny Yoo 
Date: 08/09/06 11:01:14 
To: Kermit Rose 
Cc: tutor@python.org 
Subject: Re: [Tutor] programming exercise in Python 
 
 
 
On Wed, 9 Aug 2006, Kermit Rose wrote: 
 
 
> I've written tenative code for the incr routine. 
> 
> Now to test and possibly debug it. 
 
Just as a note: it's usually a much better idea to write your test cases 
first, even before writing any code. It'll give you a better idea of what 
you want your function to do. 
 
(It'll also probably give the other here a better idea of what the 
function is supposed to do. I myself don't quite know either what it's 
supposed to do based solely on the description. *grin*) 
 
 
What are some small test cases you're thinking of for this function? 
s function? 
 
 

 
Hello Danny.
 
The test cases are
 
For mpylist = [[2, 1], [3, 2], [7, 3], [5, 2], [31, 5], [127, 7]]
 

zlim = 7
 
Initial value of mult = [0,0,0,0,0,0]
 
next few values of mult are:
 
[2, 0, 0, 0, 0, 0]   because mult[0] must skip over value of 1 and 2 * 1 < 7
 
[3, 0, 0, 0, 0, 0]   because 3 * 1 < 7
[4, 0, 0, 0, 0, 0]   because 4 * 1 < 7
[5, 0, 0, 0, 0, 0]   because 5 * 1 < 7
[6, 0, 0, 0, 0, 0]   because 6 * 1 < 7
[0, 1, 0, 0, 0, 0]   because 1 * 2 < 7
[2, 1, 0, 0, 0, 0]   because 2 * 1 + 1 * 2 < 7
[3, 1, 0, 0, 0, 0]   because 3 * 1 + 1 * 2 < 7
[4, 1, 0, 0, 0, 0]   because 4 * 1 + 1 * 2 < 7
[0, 2, 0, 0, 0, 0]   because 0 * 1 + 2 * 2 < 7
[2, 2, 0, 0, 0, 0]   because 2 * 1 + 2 * 2 < 7  and mult[0] value must skip
1
[0, 3, 0, 0, 0, 0]   because 0 * 1 + 3 * 2 < 7
[0, 0, 1, 0, 0, 0]   because 0 * 1 + 0 * 2 + 1 * 3 < 7
[2, 0, 1, 0, 0, 0]   because 2 * 1 + 0 * 2 + 1 * 3 < 7
[3, 0, 1, 0, 0, 0]   because 3 * 1 + 0 * 2 + 1 * 3 < 7
[0, 1, 1, 0, 0, 0]   because 0 * 1 + 1 * 2 + 1 * 3 < 7
 
 
I had the program generate the test cases for me, and then inspected them to
verify that 
they were what I desired.
 
The subroutine passed this initial test.
 
Thanks 
 
 
 
 
 
 

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


Re: [Tutor] about tkinter

2006-08-09 Thread Alan Gauld
Linda,

> Is that possible to open two Tkinter from one python shell?

Tkinter is a python module. You can't really open a Tkinter, 
you can only import the module. What you can do is write 
a Tkinter application with multiple windows.

You can have as many Tkinter applications running as 
you like but they will all be separate processes.

Can you be clearer about what exactly you want to do?

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


Re: [Tutor] python

2006-08-09 Thread Alan Gauld
Tom,

> could someone explain to me how the "import" function works?

Which tutorial are you reading? They should all describe import.

My tutorial gives the basic description on the Simple sequences 
topic and then much more info in the Modules & Functions topic 
and again in the Namespaces topic.

Between them and the Python documentation you should find 
all you need. But if you are still puzzled come back to us with 
specific questions.

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] programming exercise in Python

2006-08-09 Thread Alan Gauld
> # lenmult = len(mult)

This is pretty redundant, it only saves two characters typing,
you might as well do

for k in range(len(mult)):

> # for k in range(lenmult):
> # mult[k] = mult[k] + 1


> # if k == 0:
> # if mult[k] == 1:
> #mult[k] = 2

The whole of this bit only happens on the first time through
the outer loop. The effect of it is to set mult[0] to 2 if its initial
value was 0, so why not move it outside the loop and do:

if mult[0] == 0: mult[0] = 1

Then the  increment of the whole list will uplift it to 2 along
with all the other increments.

> # testsum = 0
> # if k > 0:
> # mult[k-1] = 0
> # for j in range(k,lenmult):
> # testsum = testsum + mpylist[j][1] * mult[j]

My brain is bending with this bit! I'm not sure if its right or not...
I'm still not sure i understand what its trying to do...


> # if testsum < zlim:
> # return mult
> # mult[0] = -1
> # return mult

But I think this can be better expressed as:

  if testsum >= zlim:
   mult[0] = -1   # failure condiotion
 return mult


As a matter of interest what does this bizarre algorithm
accomplish in a practical sense? What is its purpose?
I am truly intrigued...


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] programming exercise in Python

2006-08-09 Thread Kermit Rose
 
 
From: Alan Gauld 
Date: 08/09/06 12:53:59 
To: Kermit Rose; tutor@python.org 
Subject: Re: [Tutor] programming exercise in Python 
 
> # lenmult = len(mult) 
 
This is pretty redundant, it only saves two characters typing, 
you might as well do 
 
**
 
hmm
 
I had gotten in the habit of replacing  potential multiple function calls
and array references with variable name references 
acting from the hypothesis 
that variable name references take much less time than array referrences or
function calls.
 
I agree that in this case I did not need to do so.
 
>>>
 
for k in range(len(mult)): 
 
> # for k in range(lenmult): 
> # mult[k] = mult[k] + 1 
 
 
> # if k == 0: 
> # if mult[k] == 1: 
> # mult[k] = 2 
 
The whole of this bit only happens on the first time through 
the outer loop. The effect of it is to set mult[0] to 2 if its initial 
value was 0, so why not move it outside the loop and do: 
 
if mult[0] == 0: mult[0] = 1 
 
Then the increment of the whole list will uplift it to 2 along 
with all the other increments. 
 
**
 
Yes.  I see that now.   
 
I did see that I asked the computer to do extra work by 
having it  do work conditional of the initial value of k, but did not
immediately see
how to move that work outside the loop.
 
 
 

 
> # testsum = 0 
> # if k > 0: 
> # mult[k-1] = 0 
> # for j in range(k,lenmult): 
> # testsum = testsum + mpylist[j][1] * mult[j] 
 
My brain is bending with this bit! I'm not sure if its right or not... 
I'm still not sure i understand what its trying to do... 
 
 
for k = 0, checks to see if it may add 1 to mult[0]
for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to mult[1]
for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to mult[2],
etc
 

 
> # if testsum < zlim: 
> # return mult 
> # mult[0] = -1 
> # return mult 
 
But I think this can be better expressed as: 
 
if testsum >= zlim: 
mult[0] = -1 # failure condiotion 
return mult 
 
 
*
 
The indentation did not survive  repeated postings.
 
..if testsum < zlim:
..return mult
..mult[0] = -1
..return mult
 
if testsum < zlim I return mult
if testsum >= zlim, I advance the k loop
 
failure condition is that it get all the way through the k loop and not be
able to increment mult.
 
 
 
>
 
As a matter of interest what does this bizarre algorithm 
accomplish in a practical sense? What is its purpose? 
I am truly intrigued... 
 
 
***
 
:)
 
The incr routine generates a vector of exponents to apply to the list
 
mpylist [n] [0]
 
m = product of   mpylist [k] [0] ** mult[k]
 
Another subroutine uses the number m 
 
in an algorithm I've devised,
 
 to find a  divisor of the number z,
 
from which I had calculated the critical value of zlim,
 
and the mpylist vector.
 
Experimentation will tell if this factor routine is as good as I would like
it to be.
 
Kermit   <  [EMAIL PROTECTED]  >
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Alan Gauld
>> # testsum = 0
>> # if k > 0:
>> # mult[k-1] = 0
>> # for j in range(k,lenmult):
>> # testsum = testsum + mpylist[j][1] * mult[j]
>
> My brain is bending with this bit! I'm not sure if its right or 
> not...
>
> for k = 0, checks to see if it may add 1 to mult[0]
> for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to 
> mult[1]
> for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to 
> mult[2],

So you work along mult setting each member to zero after incrementing 
it?

> The incr routine generates a vector of exponents to apply to the 
> list
>
> mpylist [n] [0]
>
> m = product of   mpylist [k] [0] ** mult[k]
>

But haven't you set all of mult[k] to zero, using the example 
algorithm above?

> Experimentation will tell if this factor routine is as good as I 
> would like
> it to be.

Its got me baffled, thats for sure! :-)

Alan G.

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


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Kermit Rose
 
 
 
From: Alan Gauld 
Date: 08/09/06 13:52:09 
To: Kermit Rose 
Cc: tutor@python.org 
Subject: Re: [Tutor] programming exercise in Python 
 
>> # testsum = 0 
>> # if k > 0: 
>> # mult[k-1] = 0 
>> # for j in range(k,lenmult): 
>> # testsum = testsum + mpylist[j][1] * mult[j] 
> 
> My brain is bending with this bit! I'm not sure if its right or 
> not... 
> 
> for k = 0, checks to see if it may add 1 to mult[0] 
> for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to 
> mult[1] 
> for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to 
> mult[2], 
 
So you work along mult setting each member to zero after incrementing 
it? 
 
 
**
 
Yes.  After each member has gone through all allowed incrementations, I
reset it to zero.
 
Remember that mult is returned as soon as any member is incremented.
 
 
>
 
> The incr routine generates a vector of exponents to apply to the 
> list 
> 
> mpylist [n] [0] 
> 
> m = product of mpylist [k] [0] ** mult[k] 
> 
 
But haven't you set all of mult[k] to zero, using the example 
algorithm above? 
 
***
 
Ah, now I understandd how I misled you.
 
> for k = 0, checks to see if it may add 1 to mult[0] 
 
if it can add 1 to mult[0], do so, and return mult.  otherwise continue
 
> for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to 
> mult[1] 
 
if it can add 1 to mult[1], do so, and return mult.  otherwise continue
 
> for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to 
> mult[2], 
 
if it can add 1 to mult[2], do so, and return mult.  otherwise continue
etc
 
 
 
Kermit   <  [EMAIL PROTECTED]  >
 
 

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


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Danny Yoo

> I had the program generate the test cases for me, and then inspected 
> them to verify that they were what I desired.

Hi Kermit,

Ah.  Try not to do that next time.

It's way too easy to be convinced that some test is working by just 
copying the output of the code and looking for reasonable output.  But 
it's much more useful to write out the complete test case without 
preconceptions, without the aid of the code you're trying to test.  This 
works because:

 * You can't cheat yourself.  *grin*

 * Writing out the test cases first can help in writing the
   real function.

Those test cases act as documentation that other people can look at. 
They're a form of specification --- a requirement --- that allows others 
to understand the intent of the function.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] html file - construct attach send... email python

2006-08-09 Thread Terrence Brannon
Here's a script I wrote to email myself the Python Daily webpage as
HTML:
http://dev.metaperl.com/Members/tbrannon/python/code-snippets/script-to-
email-python-daily/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] programming exercise in Python

2006-08-09 Thread Alan Gauld
> it's much more useful to write out the complete test case without 
> preconceptions, without the aid of the code you're trying to test. 
> This works because:

> * Writing out the test cases first can help in writing the
>   real function.
>
> Those test cases act as documentation that other people can look at.

Can I echo that.

After I saw your test output I immediately understood how the
function was supposed to work. If you could have posted that
sample input/output data along with the spec before writing
the code we could have been much more helpful - I think... ;-)

Alan G. 

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


[Tutor] rstrip() failure ?

2006-08-09 Thread dave s
I have a string problem. The following code skips the loop if the string 'app' 
is on the banned list ...

self.ban_app_list = 
[' ', 'live_dat', 'exact_sl', 'html_str', 'valid_da', 'datacore', 'check_co', 
'logger']

if app in self.ban_app_list:
continue

the string 'app' is derived from some more string splicing. All works well 
except for 'logger', the 'app' string becomes 'logger  ' so no match. So I 

app.rstrip()

thinking it would sort the problem. no go :(

len(app) is still 8. OK ... I am stuck ...

Is there a way to show the ascii values of the string - kind of hexedit for a 
string

I am sure the padding blanks are just spaces rstrip() appears to disagree

Any help greatly appreciated

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


Re: [Tutor] [Doh!] rstrip() failure ?

2006-08-09 Thread dave s
On Wednesday 09 August 2006 20:45, dave s wrote:
> I have a string problem. The following code skips the loop if the string
> 'app' is on the banned list ...
>
> self.ban_app_list =
> [' ', 'live_dat', 'exact_sl', 'html_str', 'valid_da', 'datacore',
> 'check_co', 'logger']
>
> if app in self.ban_app_list:
>   continue
>
> the string 'app' is derived from some more string splicing. All works well
> except for 'logger', the 'app' string becomes 'logger  ' so no match. So I
>
> app.rstrip()
>
> thinking it would sort the problem. no go :(
>
> len(app) is still 8. OK ... I am stuck ...
>
> Is there a way to show the ascii values of the string - kind of hexedit for
> a string
>
> I am sure the padding blanks are just spaces rstrip() appears to disagree
>
> Any help greatly appreciated
>
> Dave
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Stupid basic error held me up for a couple of hours :)

app.rstrip()  should have been
app = app.rstrip()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rstrip() failure ?

2006-08-09 Thread Danny Yoo
> Is there a way to show the ascii values of the string - kind of hexedit 
> for a string

Try running repr() on the string: it will show an external 
programmer-friendly representation that should be easier to read.  For 
example:

##
>>> msg = 'hello\000world'
>>> print repr(msg)
'hello\x00world'
##
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rstrip() failure ?

2006-08-09 Thread dave s
On Wednesday 09 August 2006 21:11, you wrote:
> > Is there a way to show the ascii values of the string - kind of hexedit
> > for a string
>
> Try running repr() on the string: it will show an external
> programmer-friendly representation that should be easier to read.  For
> example:
>
> ##
>
> >>> msg = 'hello\000world'
> >>> print repr(msg)
>
> 'hello\x00world'
> ##

Thanks for that :) 

Have just found my solution without resorting to ascii strings after all but 
the above will be usefull

Thanks once again

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


[Tutor] What techniques should I use to make my code run faster? Informational

2006-08-09 Thread anil maran
  What techniques should I use to make my code run faster? Always profile before you optimize for speed.   You should always optimize for readability first: it's easier to tune   readable code than to read 'optimized' code, especially if the optimizations   are not effective. Before using any technique that makes the code less   readable, you should check that it's actually a bottleneck in your   application by running your application with the built-in profile.py   script. If your program spends 10% of its time running a particular   method, even if you increase its speed tenfold you've only shaved 9% off the   total running time. Always use a good algorithm when it is available.   The exception to the above rule is when there are known large differences in the   time complexity of alternative algorithms. Reducing
 running time from   quadratic to linear, or from exponential to polynomial, is always worth   doing unless you are sure that the data sets will always be tiny (less than   a couple of dozen items).  Use the simplest option that could possibly work.   Don't use a regular _expression_ if you just want to see if a string starts   with a particular substring: use .startswith instead. Don't use   .index if you just want to see if a string contains a particular   letter: use in instead. Don't use StringIO if you could   just use a list of strings. In general, keeping it simple cuts down on   bugs and makes your code more readable. Even a complicated combination of   .index calls will be much faster than a regular _expression_, and   probably easier to decipher if you're just matching rather than capturing   the result. Build strings
 as a list and use ''.join at the end.   Yes, you already saw this one above under "Python Idioms", but it's such   an important one that I thought I'd mention it again.   join is a string method called on the separator,   not the list. Calling it from the empty string concatenates the pieces   with no separator, which is a Python quirk and rather surprising at first.   This is important: string building with + is quadratic time instead oflinear!  Wrong: for s in strings: result += sRight: result = ''.join(strings) Use tests for object identity when appropriate: if x is notNone rather than if x != None. It is much more efficient to   test objects for identity than equality, because identity only checks   their address in memory (two objects are identical if they are the same   object in the same physical location) and
 not their actual data.  Use dictionaries for searching, not lists. To find items in common   between two lists, make the first into a dictionary and then look for items   in the second in it. Searching a list for an item is linear-time, while   searching a dict for an item is constant time. This can often let you   reduce search time from quadratic to linear.  Use the built-in sort wherever possible. sort can   take a custom comparison function as a parameter, but this makes it very   slow because the function has to be called at least O(n log n) times in the   inner loop. To save time, turn the list of items into a list of tuples,   where the first element of each tuple has the precalculated value of the   function for each item (e.g. extracting a field), and the last element is the   item itself. This idiom is called
 DSU for 'decorate-sort-undecorate.'   In the 'decorate' step, make a list of tuples   containing (transformed_value, second_key, ... , original value).   In the 'sort' step, use the built-in sort on the tuples. In the   'undecorate' step, retrieve the original list in the sorted order by   extracting the last item from each tuple. For example:  aux_list = [i.Count, i.Name, ... i) for i in items]aux_list.sort()#sorts by Count, then Name, ... , then by item itselfsorted_list = [i[-1] for i in items] #extracts last item Use map and/or filter to apply functions to lists.   map applies a function to each item in a list (technically, sequence)   and returns a list of the results. filter applies a function to each   item in a sequence, and returns a list containing only those items for which   the function evaluated True (using the
 __nonzero__ built-in   method). These functions can make code much shorter. They also make it much   faster, since the loop takes place entirely in the C API and never has to   bind loop variables to Python objects.  Worse:strings = []for d in data:strings.append(str(d))Better:strings = map(str, data) Use list comprehensions where there are conditions attached, or wherethe functions are methods or take more than one parameter. These are   cases where map and filter do badly, since you have to   make up a new one-argument function that does the operation you want. This   makes them much slower, since more work is done in the Python layer. List   comprehensions are often surprisingly readable.  Worse:res

Re: [Tutor] What techniques should I use to make my code run faster?Informational

2006-08-09 Thread Alan Gauld
Hi Anil,

It looks like you found the info below somewhere, it would be
nice to credit the source and if possible provide a URL - not
least so we can all bookmark it and see if it gets updated
over time?... :-)

Thanks for posting,

Alan G.

- Original Message - 
From: "anil maran" <[EMAIL PROTECTED]>
To: 
Sent: Wednesday, August 09, 2006 11:16 PM
Subject: [Tutor] What techniques should I use to make my code run 
faster?Informational


>  What techniques should I use to make my code run faster?
Always profile before you optimize for speed.   You should always
optimize for readability first: it's easier to tune readable code than
to read 'optimized' code, etc

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


Re: [Tutor] python import problem

2006-08-09 Thread wesley chun
> > if I want to import factory.py into myfile.py, how can I do?
>
> You need to make your folders into packages.
> Its fairly simple, you basically create an init.py file
> But its worth reading about packages in the docs
> Come back here ifv you have any questions after that.
>
> The other way would be to add each folder to
> your PYTHONPATH

the rules for importing modules in packages is going to change over
the next few releases... read up on those changes starting from here:

http://docs.python.org/dev/whatsnew/pep-328.html

the simplest solution, as alan's described, is to put all directories
that have your Python modules in the PYTHONPATH environment variable.
if you are on a Unix-based system, just set this variable in your
shell.  if you are on Win32, right-mouse My Computer -> System
Properties -> Advanced -> Environment Variables -> Edit (User or
System variables) -> Ok.

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] What techniques should I use to make my code run faster?Informational

2006-08-09 Thread anil maran
http://jaynes.colorado.edu/PythonIdioms.html#idioms_efficientAlan Gauld <[EMAIL PROTECTED]> wrote: Hi Anil,It looks like you found the info below somewhere, it would benice to credit the source and if possible provide a URL - notleast so we can all bookmark it and see if it gets updatedover time?... :-)Thanks for posting,Alan G.- Original Message - From: "anil maran" <[EMAIL PROTECTED]>To: Sent: Wednesday, August 09, 2006 11:16 PMSubject: [Tutor] What techniques should I use to make my code run faster?Informational>  What techniques should I use to make my code run faster?Always profile before you optimize for speed.   You should alwaysoptimize for readability first: it's easier to tune readable code thanto
 read 'optimized' code, etc 
		Do you Yahoo!? 
Get on board. You're invited to try the new Yahoo! Mail Beta.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What techniques should I use to make my code run faster?Informational

2006-08-09 Thread wesley chun
the overall root document of this useful read is:

http://jaynes.colorado.edu/PythonGuidelines.html

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


Re: [Tutor] html file - construct attach send... email python (fwd)

2006-08-09 Thread Danny Yoo
Can someone help Amil?  My hands won't allow me to type too long.  Thanks!

-- Forwarded message --
Date: Wed, 9 Aug 2006 17:09:19 -0700 (PDT)
From: anil maran <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] html file - construct attach send... email python

i wasnt able to find a useful way to construct email
i wasnt sure... abt how to use it
thanks for help


Danny Yoo <[EMAIL PROTECTED]> wrote:

On Wed, 9 Aug 2006, anil maran wrote:

> What is the best way to construct an email in python and also attach a
> html file


Hi Anil,

This is a repeat of one of your previous questions; you just sent this
question out yesterday:

  http://mail.python.org/pipermail/tutor/2006-August/048452.html

We are forgetful, but we're not THAT senile yet.

Please do not repost your questions so early: give some people more time
to construct a good answer for you.  Repeating a question so soon like
this tends to demotivate people.


Have you looked at the 'email' module?

  http://www.python.org/doc/lib/module-email.html

And in general, do you know about the Standard Library, or the Python
Cookbook?

  http://www.python.org/doc/lib/
  http://aspn.activestate.com/ASPN/Python/Cookbook/

I want to make sure you know about these documentation resources.


Good luck to you.



-
Do you Yahoo!?
  Next-gen email? Have it all with the  all-new Yahoo! Mail Beta.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python import problem

2006-08-09 Thread kakada
បានសរសេរ Alan Gauld:
Hi Alan,
>> the folders modules and translate are in the same level, so if I want to
>> import factory.py into myfile.py, how can I do?
>
> You need to make your folders into packages.
> Its fairly simple, you basically create an init.py file
> But its worth reading about packages in the docs
> Come back here ifv you have any questions after that.
Sure, I can do it if I move translate folder into modules folder then
create an empty init.py file.
But I want another way, and PYTHONPATH is your right suggestion
>
> The other way would be to add each folder to
> your PYTHONPATH
It works for me now:

mport sys
import os.path
sys.path.append(os.path.join(sys.path[0] ,"translate"))



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