Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 17:47, Wayne Werner  wrote:
> On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores 
> wrote:
>>
>> On Sun, Nov 7, 2010 at 16:41, Wayne Werner  wrote:
>> > On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts  wrote:
>> 
>> I should have mentioned that I'm using 3.1 .
>>
>> So this version of my function uses a generator, range(), no?
>
> Correct. The rule of thumb for this type of thing is that if you care about
> the entire object/collection, you should use a listcomp (or range in 2.x),
> but if you only care about individual elements you should always use a
> generator. Your function is a perfect example of this - you only care about
> the individual #s from 1 up to n, not the collection of numbers as a whole,
> so a generator is what you should prefer.
>
>>
>> def proper_divisors(n):
>>    sum_ = 0
>>    for x in range(1,n):
>>        if n % x == 0:
>>            sum_ += x
>>    return sum_
>
> Generators are super powerful, and my preferred reference on the subject is
> here: www.dabeaz.com/generators/



Thank you for the advice and the link.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores wrote:

> On Sun, Nov 7, 2010 at 16:41, Wayne Werner  wrote:
> > On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts  wrote:
> 
> I should have mentioned that I'm using 3.1 .
>
> So this version of my function uses a generator, range(), no?
>

Correct. The rule of thumb for this type of thing is that if you care about
the entire object/collection, you should use a listcomp (or range in 2.x),
but if you only care about individual elements you should always use a
generator. Your function is a perfect example of this - you only care about
the individual #s from 1 up to n, not the collection of numbers as a whole,
so a generator is what you should prefer.


>
> def proper_divisors(n):
>sum_ = 0
>for x in range(1,n):
>if n % x == 0:
>sum_ += x
>return sum_
>

Generators are super powerful, and my preferred reference on the subject is
here: www.dabeaz.com/*generators*/

-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 16:41, Wayne Werner  wrote:
> On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts  wrote:
>>
>> 
>> here's a list comprehension
>> >>> a = [x*2 for x in range(10)]
>> >>> a
>> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>
>> here's the equivalent generator expression:
>> >>> a = (x*2 for x in range(10))
>>
>>  
>
> Since you're talking about generators and efficiency, it's probably a good
> idea to point out that range is only a generator in python 3.x. In python
> 2.x it creates a list.

I should have mentioned that I'm using 3.1 .

So this version of my function uses a generator, range(), no?

def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Alan Gauld


"Hugo Arts"  wrote


Yes. A cast or typecast means converting some data to a different
type, like converting floats to integers, strings to integers,


The term cast can be misleading however since in some
languages - those decended from C it means treating a piece
of data as if it were another type, which is different to converting 
it.


For example in C:

char c = '7';
int x = (int)c;  //this is type casting in C - it means treat c as 
an integer

int y = atoi(c);   // this is type conversion in C

x and y are not the same. x is effectively ord('7') whereas y is 7.
But in C only the first is referred to as a cast.

Not directly relevant in a Python thread but it can cause confusion
if newbies see references to casting in the C sense and think
it means type conversion.

Just feeling picky,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive visualization in python

2010-11-07 Thread davidheiserca

FYI...

There is a non-Python commercial program called XMLSpy which displays a visual 
tree rendition of an XML schema (.xsd) file. The schema file can be created or 
manipulated with Python/ElementTree.

Maybe it can help you in your program development.




  - Original Message - 
  From: Aravind Venkatesan 
  To: tutor@python.org 
  Sent: Saturday, November 06, 2010 3:56 PM
  Subject: [Tutor] Interactive visualization in python


  Hello,


  This is Aravind. I am a university graduate student. I am looking for a 
software module or package to visualize a hierarchial tree data structure in 
python. Here's the problem:
  I have a tree(hierarchially represented) with set of nodes and edges. I would 
like to visualize this tree first. Then i would like to have each node a 
clickable object so that when a node in the tree is clicked using a mouse, i 
want to show some data associated with that node(probably a graph) in another 
popup window. What kind of packages exists in python which will help me solve 
this?


  Regards,
  Aravind 


--


  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trying to generate change in print output

2010-11-07 Thread Alan Gauld


"Terry Green"  wrote


Am stumped, when I use this code:

race=int(row[2])
   raceChek=1

   if raceChek == race: print ('raceChek ', raceChek, 'race ', race)
   else: print ('raceChek ', raceChek,' no match ', 'race ', race);
raceChek = race



I Get this:

raceChek  1 race  1
raceChek  1  no match  race  2
raceChek  1  no match  race  3

Seems my test between race and raceChek doesn't work!   What is 
wrong?



From the segment of code you posted it loks like it works perfectly.

When race is different to 1 you get the second message when it
is equal to 1 tyou get the first.

What did you expect?

And how are you generating the repeated results?
Do you have a loop somewhere or just repeatedly run the same code?


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts  wrote:

> 
> here's a list comprehension
> >>> a = [x*2 for x in range(10)]
> >>> a
> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>
> here's the equivalent generator expression:
> >>> a = (x*2 for x in range(10))

 


Since you're talking about generators and efficiency, it's probably a good
idea to point out that range is only a generator in python 3.x. In python
2.x it creates a list.

Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Hugo Arts
On Mon, Nov 8, 2010 at 1:16 AM, Richard D. Moores  wrote:
> On Sun, Nov 7, 2010 at 15:53, Hugo Arts  wrote:
>> n is a proper
>> divisor of x if there is no remainder after division, after all. This
>> also means you won't have to do a cast, which tend to be fairly
>> expensive.
>
> I don't know what a cast is. Would that be the int(n/x)?
>

Yes. A cast or typecast means converting some data to a different
type, like converting floats to integers, strings to integers,
integers to strings, etc. They are fairly expensive because the
representation of the data changes.

> Here are 2 versions, of about equal speed:
>
> def proper_divisors(n):
>    """
>    Return the sum of the proper divisors of positive integer n
>    """
>    return sum([x for x in range(1,n) if n % x == 0])
>
> def proper_divisors(n):
>    sum_ = 0
>    for x in range(1,n):
>        if n % x == 0:
>            sum_ += x
>    return sum_
>
>> On another note, getting rid of the list comprehension and using a
>> generator expression will be even faster, since you won't have to
>> build the list.
>
> Could you spell that out for me?

here's a list comprehension
>>> a = [x*2 for x in range(10)]
>>> a
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

here's the equivalent generator expression:
>>> a = (x*2 for x in range(10))
>>> a
 at 0xb7792fa4>
>>> for x in a: print x
0
2
4
6
8
10
12
14
16
18

As you can see, they are constructed almost the same way, the only
difference being the type of parentheses. The main difference is what
comes out. A list comprehension computes all its elements and builds a
list out of them, very simple.

A generator expression builds a generator. When you create a
generator, nothing is actually done yet. Only when you *iterate* over
a generator (like I did with the for loop) does it start computing
results, one by one, giving each one out as it is needed.

If all you're doing with the results is iterating over them once and
then throwing them away (like you do here with sum()), using a
generator expression saves us some time and space, since we don't have
to build a list object that we won't be needing anyway.

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 15:53, Hugo Arts  wrote:
> On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores  wrote:
>> def proper_divisors(n):
>>     """
>>     Return the sum of the proper divisors of positive integer n
>>     """
>>     return sum([x for x in range(1,n) if int(n/x) == n/x])
>>
>> The list comprehension is this function is inefficient in that it computes
>> n/x twice. I'd like to do an  a = n/x and use a in
>> "if int(a) == a", but I don't know how.
>>
>
> You can't do that inside a list comprehension. Either get rid of the
> comprehension and do a regular loop, or get rid of the n/x expression.
>
> I'd suggest replacing the whole check with x % n == 0.

Wow! using n % x == 0 is about 3x faster than what I had before.

> n is a proper
> divisor of x if there is no remainder after division, after all. This
> also means you won't have to do a cast, which tend to be fairly
> expensive.

I don't know what a cast is. Would that be the int(n/x)?

Here are 2 versions, of about equal speed:

def proper_divisors(n):
"""
Return the sum of the proper divisors of positive integer n
"""
return sum([x for x in range(1,n) if n % x == 0])

def proper_divisors(n):
sum_ = 0
for x in range(1,n):
if n % x == 0:
sum_ += x
return sum_

> On another note, getting rid of the list comprehension and using a
> generator expression will be even faster, since you won't have to
> build the list.

Could you spell that out for me?

Thanks,

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension question

2010-11-07 Thread Hugo Arts
On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores  wrote:
> def proper_divisors(n):
>     """
>     Return the sum of the proper divisors of positive integer n
>     """
>     return sum([x for x in range(1,n) if int(n/x) == n/x])
>
> The list comprehension is this function is inefficient in that it computes
> n/x twice. I'd like to do an  a = n/x and use a in
> "if int(a) == a", but I don't know how.
>

You can't do that inside a list comprehension. Either get rid of the
comprehension and do a regular loop, or get rid of the n/x expression.

I'd suggest replacing the whole check with x % n == 0. n is a proper
divisor of x if there is no remainder after division, after all. This
also means you won't have to do a cast, which tend to be fairly
expensive.

On another note, getting rid of the list comprehension and using a
generator expression will be even faster, since you won't have to
build the list.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
def proper_divisors(n):
"""
Return the sum of the proper divisors of positive integer n
"""
return sum([x for x in range(1,n) if int(n/x) == n/x])

The list comprehension is this function is inefficient in that it computes
n/x twice. I'd like to do an  a = n/x and use a in
"if int(a) == a", but I don't know how.

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Adding cookies to cookiejar object (Python 3.01)

2010-11-07 Thread fjulll
I have succeded in importing a bunch of cookies from my browser to an array
and now want to put them in a cookiejar object to send in a HTTP-request.
I've read the documentation over and over again but can't figure out how to
actually add your own cookies to the jar. Does anyone know?

http://docs.python.org/release/3.0.1/library/http.cookiejar.html

Thank you!

Kind regards
Vilhelm
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trying to generate change in print output

2010-11-07 Thread Steven D'Aprano

Terry Green wrote:

Am stumped, when I use this code:

race=int(row[2])
raceChek=1


This causes IndentationError: unexpected indent.


if raceChek == race: print ('raceChek ', raceChek, 'race ', race)
else: print ('raceChek ', raceChek,' no match ', 'race ', race);
raceChek = race



I Get this:

raceChek  1 race  1
raceChek  1 race  1
raceChek  1 race  1

[...]

I don't see how. The code you show fails completely, due to the 
inconsistent indentation. Even if we fix the indentation, it will only 
print something *once*, not eighteen times.


You don't show us what the variable "row" has, so we don't know what 
value "race" has. Presumably it equals 1, but then later on you get:



raceChek  1  no match  race  2

[repeated many times]

raceChek  1  no match  race  3

[repeated many times]

So mysteriously race is changing value.

Obviously you are not showing us the actual code you are running, but 
only part of the code. Would you like us to guess what code you are running?




Seems my test between race and raceChek doesn't work!   What is wrong?


What makes you think that the problem is the test? It seems to me that 
the test


if raceChek == race:

is so simple that it couldn't possibly be broken. The problem is 
probably somewhere else. But since I don't know how you are getting 
eighteen lines of output, I don't know what you are doing and can't tell 
you what it is that is wrong.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] trying to generate change in print output

2010-11-07 Thread Terry Green
Am stumped, when I use this code:

 

 

race=int(row[2])

raceChek=1

 

if raceChek == race: print ('raceChek ', raceChek, 'race ', race)

else: print ('raceChek ', raceChek,' no match ', 'race ', race);
raceChek = race

 

 

I Get this:

 

raceChek  1 race  1

raceChek  1 race  1

raceChek  1 race  1

raceChek  1 race  1

raceChek  1 race  1

raceChek  1 race  1

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  2

raceChek  1  no match  race  3

raceChek  1  no match  race  3

raceChek  1  no match  race  3

raceChek  1  no match  race  3

raceChek  1  no match  race  3

 

Seems my test between race and raceChek doesn't work!   What is wrong?

thanks

 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive visualization in python

2010-11-07 Thread Terry Carroll

On Sun, 7 Nov 2010, Alan Gauld wrote:


Most GUI toolkits have a tree widget like the Wiondows Explorer tree view.
The Tkintrer version is included in the Tix module which extends the basic
Tkinter widgets.

I'm pretty sure wxPython will have one too.


I haven't used it, but wxPython's tree widget is wx.TreeCtrl

doc:

http://wxpython.org/docs/api/wx.TreeCtrl-class.html

example:

http://wiki.wxpython.org/AnotherTutorial#wx.TreeCtrl
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] test

2010-11-07 Thread Sandip Bhattacharya
On Sat, Nov 06, 2010 at 04:20:54PM +1100, Steven D'Aprano wrote:
> Luke Paireepinart wrote:
> >You don't get your own e-mails back.
> 
> I do.
> 
> Perhaps it's an option when you sign up?

I think it is an irritating gmail-only "feature". I 
use a google apps domain and face the same issue. I see
that the OP also uses gmail.

http://mail.google.com/support/bin/answer.py?hl=en&answer=82454

"""
Finally, if you're sending mail to a mailing list that you subscribe to,
those messages will only appear in 'Sent Mail.' 
"""

- Sandip

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive visualization in python

2010-11-07 Thread Alan Gauld


"Aravind Venkatesan"  wrote

This is Aravind. I am a university graduate student. I am looking 
for a
software module or package to visualize a hierarchial tree data 
structure in

python.


Most GUI toolkits have a tree widget like the Wiondows Explorer tree 
view.
The Tkintrer version is included in the Tix module which extends the 
basic
Tkinter widgets. The documentation is not fantastic but ithere should 
be

enough to get you going.

I'm pretty sure wxPython will have one too.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Interactive visualization in python

2010-11-07 Thread Aravind Venkatesan
Hello,

This is Aravind. I am a university graduate student. I am looking for a
software module or package to visualize a hierarchial tree data structure in
python. Here's the problem:
I have a tree(hierarchially represented) with set of nodes and edges. I
would like to visualize this tree first. Then i would like to have each node
a clickable object so that when a node in the tree is clicked using a mouse,
i want to show some data associated with that node(probably a graph) in
another popup window. What kind of packages exists in python which will help
me solve this?

Regards,
Aravind
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive visualization in python

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 2:50 AM, Alan Gauld wrote:

>
> "Aravind Venkatesan"  wrote
>
>
>  This is Aravind. I am a university graduate student. I am looking for a
>> software module or package to visualize a hierarchial tree data structure
>> in
>> python.
>>
>
> Most GUI toolkits have a tree widget like the Wiondows Explorer tree view.
> The Tkintrer version is included in the Tix module which extends the basic
> Tkinter widgets. The documentation is not fantastic but ithere should be
> enough to get you going.
>

As an alternative method using Tkinter, you can create objects on a canvas
and make them clickable with relative ease.

For serious graphing, matplotlib is the defacto standard.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor