473,379 Members | 1,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Summer reading list

Found in a pamphlet at a pre-school:
---------------------------------------
Reading improves vocabulary
Reading raises cultural literacy through shared knowledge
Reading develops writing skills
Reading opens the mind to new ways of understanding
Reading is fun
Accordingly, I suggest the following works of literature:

* heapq.py (255 lines)
* sets.py (536 lines)
* textwrap.py (355 lines)
* csv.py (427 lines)

These make enjoyable reading, cover interesting topics/algorithms,
demonstrate superb modern python technique, and showcase
effective use of Python's newer features.

Learn from the masters:
Pinard, O'Connor, Peters, Wilson, Martelli, van Rossum,
Ward, Montanaro, Altis, Drake, and others

have-you-read-any-good-code-lately-ly yours,
Raymond Hettinger
P.S. The unittests for sets.py are *not* as enjoyable reading; however,
they are a highly instructive example of Greg's sophisticated use of the
testing framework and his unusally thorough approach to deciding
what and how to test. Lib/test/test_sets.py (692 lines)
Learning from Greg's example enabled me to use similar ideas in
developing Lib/test/test_random.py (298 lines).
Jul 18 '05 #1
6 2363
> Accordingly, I suggest the following works of literature:

* heapq.py (255 lines)
* sets.py (536 lines)
* textwrap.py (355 lines)
* csv.py (427 lines)

These make enjoyable reading, cover interesting topics/algorithms,
demonstrate superb modern python technique, and showcase
effective use of Python's newer features.


I read heapq.py from here:
http://cvs.sourceforge.net/cgi-bin/v...ist/src/Lib/he
apq.py

Quoting from the comments:

"""Usage:

heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0] # smallest item on the heap without popping it
heapify(x) # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
# new item; the heap size is unchanged"""

It might just be my Java background creeping in (I'm a Python newbie), but,
wouldn't it be better if this was OO?

heap = Heap()
heap.push(item)
item = heap.pop()
item = heap[0]
heapified = Heap(x)
item = heap.replace(item)

Otherwise the user could easily break the heap by doing something dumb to
the list...

Jul 18 '05 #2
On Tue, 2003-08-12 at 08:56, Joe Cheng wrote:
Quoting from the comments:

"""Usage:

heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0] # smallest item on the heap without popping it
heapify(x) # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
# new item; the heap size is unchanged"""

It might just be my Java background creeping in (I'm a Python newbie), but,
wouldn't it be better if this was OO?

heap = Heap()
heap.push(item)
item = heap.pop()
item = heap[0]
heapified = Heap(x)
item = heap.replace(item)

Otherwise the user could easily break the heap by doing something dumb to
the list...


True. But the flexibility of using the builtin is also nice. For
example, you can add a bunch of objects to the list, then heapify once,
rather than having to call heap.push() a bunch of times (which may be
slower, because you need to maintain the heap property after you push
each new item.)

I think the idea is that, if you want a real Heap class, you can build
one very easily (see below). And if you don't need a heap class, you
can gain some benefits from this approach because it is exposing and
operating on lists directly.

This probably comes under "practicality beats purity". (See 'The Zen of
Python', or type "import this" into your Python interpreter)

Quick heap class (any error corrections are appreciated):
---------------------------------------------------------

import heapq

class Heap:
def __init__( self, heap=[] ):
heapq.heapify( heap )
self._heap = heap

def __getitem__( self, i ):
return self._heap[ i ]

def push( self, item ):
return heapq.heappush( self._heap, item )

def pop( self ):
return heapq.heappop( self._heap )

def replace( self, item ):
return heapq.heapreplace( self._heap, item )

if __name__ == '__main__':
# Tests
heap = Heap()
heap.push(3)
heap.push(2)
heap.push(1)
item = heap.pop()

assert item == 1

item = heap[0]
assert item == 2

item = heap.replace(4)
assert item == 2
assert heap[0] == 3
assert heap[1] == 4

--
Chad Netzer
Jul 18 '05 #3
Chad Netzer
Quick heap class (any error corrections are appreciated):
--------------------------------------------------------- def __init__( self, heap=[] ):


make that
def __init__(self, heap = None):
if heap is None:
heap = []

Otherwise, all Heaps created with default args will share the
same data.

Andrew
da***@dalkescientific.com
Jul 18 '05 #4
Joe Cheng:
It might just be my Java background creeping in (I'm a Python newbie), but, wouldn't it be better if this was OO?


Here's perhaps the definitive statement on the topic, from Tim Peters:
http://aspn.activestate.com/ASPN/Mai...on-dev/1620162

Summary: heapq is a concrete interface, not an abstract one. It doesn't
try to encompass the different ways to do heaps. It's like bisect in that
it works on an existing data type.

Andrew
da***@dalkescientific.com
Jul 18 '05 #5
"Andrew Dalke" <ad****@mindspring.com> writes:
Joe Cheng:
It might just be my Java background creeping in (I'm a Python newbie), but,
wouldn't it be better if this was OO?


Here's perhaps the definitive statement on the topic, from Tim Peters:
http://aspn.activestate.com/ASPN/Mai...on-dev/1620162

Summary: heapq is a concrete interface, not an abstract one. It doesn't
try to encompass the different ways to do heaps. It's like bisect in that
it works on an existing data type.


That URL comes up blank for me. Found this, though:

http://www.python.org/dev/summary/20...003-04-30.html

| The idea of turning the heapq module into a class came up, and later
| led to the idea of having a more proper FIFO (First In, First Out)
| data structure. Both ideas were shot down. The reason for this was
| that the stdlib does not need to try to grow every single possible
| data structure in programming. Guido's design philosophy is to have a
| few very powerful data structures that other ones can be built off
| of. This is why the bisect and heapq modules just work on standard
| lists instead of defining a new class. Queue is an exception, but it
| is designed to mediate messages between threads instead of being a
| general implementation of a queue.
John
Jul 18 '05 #6
Andrew Dalke wrote:
Summary: heapq is a concrete interface, not an abstract one. It doesn't
try to encompass the different ways to do heaps. It's like bisect in that
it works on an existing data type.


more importantly, it works on *any* existing data type, as long as the
type behaves like a mutable sequence.

</F>


Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richard Jones | last post by:
Garbage Collection & Memory Management Summer School 20-21 July 2004, Canterbury, UK The performance of today's memory-hungry applications depends on efficient dynamic memory management,...
0
by: David Ascher | last post by:
In order to centralize discussion and minimize spam, we've created a new mailing list to discuss the Google Summer of Code from a Python POV. I strongly encourage people interested in discussing...
6
by: A.M. Kuchling | last post by:
For anyone who's interested: the Python wiki now contains a list of the PSF-mentored proposals that were accepted for Google's Summer of Code: http://wiki.python.org/moin/SummerOfCode --amk
1
by: Volkan Arslan | last post by:
------------------------------------------------------------- LASER Summer School on Software Engineering Practical Techniques of Software Quality Elba, Italy September 12 - 18, 2004 ...
2
by: ojorus | last post by:
Hi! A simple question maybe... I have a datetime column in a table, and want to automaticly insert local (Norwegian) time into the column. (I use PHP 5) I can not use NOW() since the mysql...
0
by: Carl Friedrich Bolz | last post by:
Last chance to join the Summer of PyPy! ======================================= Hopefully by now you have heard of the "Summer of PyPy", our program for funding the expenses of attending a...
0
by: jtauber | last post by:
Google's Summer of Code is on again! And the Python Software Foundation is again applying to be a mentoring organization. A mailing list has been set up for people who are interested in...
0
by: Michael | last post by:
Hi, We're (BBC Research) participating in Google's Summer of Code as a mentor organisation again, and I thought it worth spreading some extra publicity where I think there might be some...
0
by: Hanshika Sathey | last post by:
SUMMER DATING + FRIENDSHIP WITH ME..! Hii.... I'm Hanshika Sathey, from Mumbai.. Make Me your Best Friend Simple join to my Best Friends List.. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.