Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Alan Gauld

Dick Moores [EMAIL PROTECTED] wrote

code I time. What's an example of code that would have System be 
greater than zero? And what's the distinction between User and 
System? (I'm using Win XP, if that's relevant.)


It may be that XP doesn't report System time.

Well, here's one that reads in the text of Dickens' _Little Dorrit_ 
and returns the word count:


IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 5.94446752311 s, 0.594446752311 s.
  System:0.0 s,0.0 s.


I would definitely have expected some system time there.

This one is a Gui that has an Exit button. I called it and then hit 
the button:


IPython CPU timings (estimated):
  User  : 10.5294301371 s.
  System:0.0 s.


That suggests that it is counting elapsed time as user time
in which case I'm not sure how it counts system time.
It may be an XP issue.

Anyone got different results under Linux/MacOS?

Alan G.


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 01:00 AM 8/3/2008, Alan Gauld wrote:

Dick Moores [EMAIL PROTECTED] wrote

code I time. What's an example of code that would have System be 
greater than zero? And what's the distinction between User and 
System? (I'm using Win XP, if that's relevant.)


It may be that XP doesn't report System time.

Well, here's one that reads in the text of Dickens' _Little Dorrit_ 
and returns the word count:


IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 5.94446752311 s, 0.594446752311 s.
  System:0.0 s,0.0 s.


I would definitely have expected some system time there.

This one is a Gui that has an Exit button. I called it and then hit 
the button:


IPython CPU timings (estimated):
  User  : 10.5294301371 s.
  System:0.0 s.


That suggests that it is counting elapsed time as user time
in which case I'm not sure how it counts system time.
It may be an XP issue.

Anyone got different results under Linux/MacOS?


_Little Dorrit_ is available in us-ascii at 
http://www.gutenberg.org/etext/963; _War and Peace_ at 
http://www.gutenberg.org/etext/2600.


Dick


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Kent Johnson
On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? (I'm using
 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given (for Windows platforms this is reported
as 0.0).

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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 05:15 AM 8/3/2008, Kent Johnson wrote:

On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? 
(I'm using

 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given


Thanks, Kent. RTFM!


(for Windows platforms this is reported
as 0.0).


I wonder why it is reported for Windows at all?

Dick


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-03 Thread Dick Moores

At 05:15 AM 8/3/2008, Kent Johnson wrote:

On Sun, Aug 3, 2008 at 4:00 AM, Alan Gauld [EMAIL PROTECTED] wrote:
 Dick Moores [EMAIL PROTECTED] wrote

 code I time. What's an example of code that would have System be greater
 than zero? And what's the distinction between User and System? 
(I'm using

 Win XP, if that's relevant.)

 It may be that XP doesn't report System time.

From the IPython help for 'run':

-t: print timing information at the end of the run.  IPython will
giveyou an estimated CPU time consumption for your script, which
underUnix uses the resource module to avoid the wraparound
problems oftime.clock().  Under Unix, an estimate of time spent on
system tasksis also given


Thanks, Kent. RTFM!


(for Windows platforms this is reported
as 0.0).


I wonder why it is reported for Windows at all?

Dick


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


[Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Dick Moores
I'm pretty new to Python's dictionaries, but I had a need for a 
function that would find the values in a dict that have more than one 
key each. It took me several hours to write. See 
http://py77.python.pastebin.com/f397582d8. Seems to do the job, 
both with the example shown, and with the dict of colors at 
http://py77.python.pastebin.com/f796752ff.


But I can't believe the function needs to be so complex. And also, I 
suppose I've reinvented the wheel (again). Please instruct me.


My apologies in advance to Kent for not using a single list comprehension.

Thanks,

Dick Moores

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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Andre Engels
On Sat, Aug 2, 2008 at 11:07 AM, Dick Moores [EMAIL PROTECTED] wrote:
 I'm pretty new to Python's dictionaries, but I had a need for a function
 that would find the values in a dict that have more than one key each. It
 took me several hours to write. See
 http://py77.python.pastebin.com/f397582d8. Seems to do the job, both with
 the example shown, and with the dict of colors at
 http://py77.python.pastebin.com/f796752ff.

 But I can't believe the function needs to be so complex. And also, I suppose
 I've reinvented the wheel (again). Please instruct me.

 My apologies in advance to Kent for not using a single list comprehension.

Well, list comprehension does indeed seem the solution to your
problem, although a single list comprehension would not be necessary.
I came to http://py77.python.pastebin.com/m4dcbb34f (note: this is
untested, I have no Python on the computer I am working on now), or
http://py77.python.pastebin.com/f76ba5002 to indeed just use a single
list comprehension (or rather, two nested list comprehensions, again
untested for the above reason).

-- 
André Engels, [EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Dick Moores

At 03:27 AM 8/2/2008, Andre Engels wrote:
Content-Transfer-Encoding: 
base64Content-Disposition: inlineOn Sat, Aug 2, 
2008 at 11:07 AM, Dick Moores [EMAIL PROTECTED] wrote:

 I'm pretty new to Python's dictionaries, but I had a need for a function
 that would find the values in a dict that have more than one key each. It
 took me several hours to write. See
 
http://py77.python.pastebin.com/f397582d8ˆÙY[\ÈÈÈH›Ø‹›ÝÚ]ˆHexample 
shown, and with the dict of colors at 
http://py77.python.pastebin.com/f796752ff.


[EMAIL PROTECTED] the function needs to be so complex. And also, I suppose
 I've reinvented the wheel (again). Please instruct me.

[EMAIL PROTECTED] list comprehension.

Well, list comprehension does indeed seem the solution to your
problem, although a single list comprehension would not be necessary.
I came to http://py77.python.pastebin.com/m4dcbb34f (note: this is
untested, I have no Python on the computer I am working on now), or
http://py77.python.pastebin.com/f76ba5002 to indeed just use a single
list comprehension (or rather, two nested list comprehensions, again
untested for the above reason).


You genius! How could you answer so quickly and 
accurately (20 minutes!) without access to Python?


Both of your scripts work, after I corrected one 
typo. See http://py77.python.pastebin.com/f7b6e51e8.


Thanks!

Dick Moores


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Dick Moores

At 03:49 AM 8/2/2008, Dick Moores wrote:

At 03:27 AM 8/2/2008, Andre Engels wrote:
Content-Transfer-Encoding: 
base64Content-Disposition: inlineOn Sat, Aug 2, 
2008 at 11:07 AM, Dick Moores [EMAIL PROTECTED] wrote:

 I'm pretty new to Python's dictionaries, but I had a need for a function
 that would find the values in a dict that have more than one key each. It
 took me several hours to write. See
 
http://py77.python.pastebin.com/f397582d8ˆÙY[\ÈÈÈH›Ø‹›ÝÚ]ˆHexample 
shown, and with the dict of colors at 
http://py77.python.pastebin.com/f796752ff.


[EMAIL PROTECTED] the function needs to be so complex. And also, I suppose
 I've reinvented the wheel (again). Please instruct me.

[EMAIL PROTECTED] list comprehension.

Well, list comprehension does indeed seem the solution to your
problem, although a single list comprehension would not be necessary.
I came to http://py77.python.pastebin.com/m4dcbb34f (note: this is
untested, I have no Python on the computer I am working on now), or
http://py77.python.pastebin.com/f76ba5002 to indeed just use a single
list comprehension (or rather, two nested list comprehensions, again
untested for the above reason).


You genius! How could you answer so quickly and 
accurately (20 minutes!) without access to Python?


Both of your scripts work, after I corrected one 
typo. See http://py77.python.pastebin.com/f7b6e51e8.


And here's your one-liner 
(http://py77.python.pastebin.com/f789096e9) at 
work on the dict of colors at http://py77.python.pastebin.com/f796752ff.


Dick


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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Kent Johnson
On Sat, Aug 2, 2008 at 6:07 AM, Dick Moores [EMAIL PROTECTED] wrote:
 I'm pretty new to Python's dictionaries, but I had a need for a function
 that would find the values in a dict that have more than one key each.

From your sample output it appears that you want not just the values,
but a list of (value, keys) pairs for which there are more than one
key. It is easy to just build a reverse dict and filter its items:

In [15]: from collections import defaultdict
In [16]: rev=defaultdict(list)
In [18]: for k, v in d.iteritems():
rev[v].append(k)

In [20]: [ [v, keys] for v, keys in rev.iteritems() if len(keys)  1 ]

Out[20]:
[[1, ['a', 'e', 'g']],
 [2, ['b', 'f', 'i', 'h']],
 [4, ['d', 'j']],
 ['U.S. Senator', ['John McCain', 'Barack Obama']],
 [56, [45, 55]]]

This also has the advantage of making only two passes over the data so
its performance should be O(n). Your solution and Andre's make one or
more passes over the data for each data element so they will have
O(n*n) performance meaning for a large dict they could be very slow.

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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Dick Moores

At 05:02 AM 8/2/2008, Kent Johnson wrote:

On Sat, Aug 2, 2008 at 6:07 AM, Dick Moores [EMAIL PROTECTED] wrote:
 I'm pretty new to Python's dictionaries, but I had a need for a function
 that would find the values in a dict that have more than one key each.

From your sample output it appears that you want not just the values,
but a list of (value, keys) pairs for which there are more than one
key. It is easy to just build a reverse dict and filter its items:

In [15]: from collections import defaultdict
In [16]: rev=defaultdict(list)
In [18]: for k, v in d.iteritems():
rev[v].append(k)

In [20]: [ [v, keys] for v, keys in rev.iteritems() if len(keys)  1 ]

Out[20]:
[[1, ['a', 'e', 'g']],
 [2, ['b', 'f', 'i', 'h']],
 [4, ['d', 'j']],
 ['U.S. Senator', ['John McCain', 'Barack Obama']],
 [56, [45, 55]]]

This also has the advantage of making only two passes over the data so
its performance should be O(n). Your solution and Andre's make one or
more passes over the data for each data element so they will have
O(n*n) performance meaning for a large dict they could be very slow.


Wow, the genius' genius appears!

You're using some Python tools I didn't know about. More study!

I made a function of your code and time tested it against mine, using 
as d that dict of colors at http://py77.python.pastebin.com/f796752ff.


Yours is 73 times faster!

In [12]: run -t -N10 fcn_values_dupe_keys.py
Time was 0.297 seconds
Time was 0.328 seconds
Time was 0.344 seconds
Time was 0.328 seconds
Time was 0.313 seconds
Time was 0.344 seconds
Time was 0.39 seconds
Time was 0.297 seconds
Time was 0.312 seconds
Time was 0.297 seconds

IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 3.3092253345 s, 0.33092253345 s.
  System:0.0 s,0.0 s.

In [13]: run -t -N10 kent1.py
Time was 0 seconds
Time was 0 seconds
Time was 0 seconds
Time was 0 seconds
Time was 0 seconds
Time was 0.015 seconds
Time was 0 seconds
Time was 0 seconds
Time was 0 seconds
Time was 0 seconds

IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 0.044969961266 s, 0.0044969961266 s.
  System:0.0 s,0.0 s.

BTW Kent, I'm going to take this opportunity to ask you about 
System in the IPython timing results. It's always zero for the code 
I time. What's an example of code that would have System be greater 
than zero? And what's the distinction between User and System? (I'm 
using Win XP, if that's relevant.)


Thanks,

Dick

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


Re: [Tutor] I can't believe this needs to be this complex

2008-08-02 Thread Dick Moores

At 10:08 AM 8/2/2008, Alan Gauld wrote:

Dick Moores [EMAIL PROTECTED] wrote

BTW Kent, I'm going to take this opportunity to ask you about 
System in the IPython timing results. It's always zero for the 
code I time. What's an example of code that would have System be 
greater than zero? And what's the distinction between User and 
System? (I'm using Win XP, if that's relevant.)


In timing user time is time that the CPU spends executing user
code - your program. System time is time the CPU spends doing
OS things.

If your code had a blocking call that waited for input on a port say,
then the OS might be doing other stuff in the background while
your code waited. This would show as system time.
In some OR even time spent reading files from disk is
counted as system time because your code is executing
low level OS functions.

Try timing a function that does nothing but read a large file.
See if there is any system time showing up.


Well, here's one that reads in the text of Dickens' _Little Dorrit_ 
and returns the word count:


In [11]: run -t -N10 timing_test_little_dorrit.py
339832
339832
339832
339832
339832
339832
339832
339832
339832
339832

IPython CPU timings (estimated):
Total runs performed: 10
  Times :  Total   Per run
  User  : 5.94446752311 s, 0.594446752311 s.
  System:0.0 s,0.0 s.


Or time a GUI
app that waits for user input...


This one is a Gui that has an Exit button. I called it and then hit the button:

In [4]: run -t ToolkitV15.py

IPython CPU timings (estimated):
  User  : 10.5294301371 s.
  System:0.0 s.

So no non-zero System time yet. But thanks for your explanation.

Dick


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