Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 225 The heapreplace function is not quite as commonly used as the others. It pops the smallest element off the heap and then pushes a new element onto it. This is more efficient than a heappop followed by a heappush: >>> heapreplace(heap, 0.5) 0 >>> heap [0.5, 1, 5, 3, 2, 7, 9, 8, 4, 6] >>> heapreplace(heap, 10) 0.5 >>> heap [1, 2, 5, 3, 6, 7, 9, 8, 4, 10] The remaining two functions of the heapq module, nlargest(n, iter) and nsmallest(n, iter), are used to find the n largest or smallest elements, respectively, of any iterable object iter. You could do this by using sorting (for example, using the sorted function) and slicing, but the heap algorithm is faster and more memory-efficient (and, not to mention, easier to use). Deques (and Other Collections) Double-ended queues, or deques, can be useful when you need to remove elements in the order in which they were added. In Python 2.4, the collections module was added, which contains the deque type. ■Note As of Python 2.4, the collections module only contains the deque type. Possible future additions are B-trees and Fibonacci heaps. A deque is created from an iterable object (just like sets) and has several useful methods: >>> from collections import deque >>> q = deque(range(5)) >>> q.append(5) >>> q.appendleft(6) >>> q deque([6, 0, 1, 2, 3, 4, 5]) >>> q.pop() 5 >>> q.popleft() 6 >>> q.rotate(3) >>> q deque([2, 3, 4, 0, 1]) >>> q.rotate(-1) >>> q deque([3, 4, 0, 1, 2])
226 CHAPTER 10 ■ BATTERIES INCLUDED The reason for the usefulness of the deque is that it allows appending and popping efficiently at the beginning (to the left), as opposed to lists. As a nice side effect, you can also rotate the elements (that is, shift them to the right or left, wrapping around the ends) efficiently. Deque objects also have extend and extendleft methods, with extend working like the corresponding list method, and extendleft working analogously to appendleft. Note that the elements in the iterable used in extendleft will appear in the deque in reverse order. time The time module contains functions for, among other things, getting the current time, manipulating times and dates, and reading dates from strings and formatting dates as strings. Dates can be represented as either a real number (the seconds since 0 hours, January 1 in the “epoch,” a platform-dependent year; for UNIX, it’s 1970), or a tuple containing nine integers. These integers are explained in Table 10-5. For example, the tuple (2002, 1, 21, 12, 2, 56, 0, 21, 0) represents January 21, 2002, at 12:02:56, which is a Monday, and the 21st day of the year. (No daylight savings.) Table 10-5. The Fields of Python Date Tuples Index Field Value 0 Year For example, 2000, 2001, and so on 1 Month In the range 1–12 2 Day In the range 1–31 3 Hour In the range 0–23 4 Minute In the range 0–59 5 Second In the range 0–61 6 Weekday In the range 0–6, where Monday is 0 7 Julian day In the range 1–366 8 Daylight Savings 0, 1, or –1 Some of these values need some explaining: The range for seconds is 0–61 to account for leap seconds and double leap seconds. The Daylight Savings number is a Boolean value (true or false), but if you use –1, mktime (a function that converts such a tuple to a timestamp measured in seconds since the epoch) will probably get it right. Some of the most important functions in the time module are described in Table 10-6.
- Page 206 and 207: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 208 and 209: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 210 and 211: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 212 and 213: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 214 and 215: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 216 and 217: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 218 and 219: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 220 and 221: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 222 and 223: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 224 and 225: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 226 and 227: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 228 and 229: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 246 and 247: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 264 and 265: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
CHAPTER 10 ■ BATTERIES INCLUDED 225<br />
The heapreplace function is not quite as commonly used as the others. It pops the smallest<br />
element off the heap and then pushes a new element on<strong>to</strong> it. This is more efficient than a<br />
heappop followed by a heappush:<br />
>>> heapreplace(heap, 0.5)<br />
0<br />
>>> heap<br />
[0.5, 1, 5, 3, 2, 7, 9, 8, 4, 6]<br />
>>> heapreplace(heap, 10)<br />
0.5<br />
>>> heap<br />
[1, 2, 5, 3, 6, 7, 9, 8, 4, 10]<br />
The remaining two functions of the heapq module, nlargest(n, iter) and<br />
nsmallest(n, iter), are used <strong>to</strong> find the n largest or smallest elements, respectively, of any<br />
iterable object iter. You could do this by using sorting (for example, using the sorted function)<br />
and slicing, but the heap algorithm is faster and more memory-efficient (and, not <strong>to</strong> mention,<br />
easier <strong>to</strong> use).<br />
Deques (and Other Collections)<br />
Double-ended queues, or deques, can be useful when you need <strong>to</strong> remove elements in the<br />
order in which they were added. In <strong>Python</strong> 2.4, the collections module was added, which<br />
contains the deque type.<br />
■Note As of <strong>Python</strong> 2.4, the collections module only contains the deque type. Possible future additions<br />
are B-trees and Fibonacci heaps.<br />
A deque is created from an iterable object (just like sets) and has several useful methods:<br />
>>> from collections import deque<br />
>>> q = deque(range(5))<br />
>>> q.append(5)<br />
>>> q.appendleft(6)<br />
>>> q<br />
deque([6, 0, 1, 2, 3, 4, 5])<br />
>>> q.pop()<br />
5<br />
>>> q.popleft()<br />
6<br />
>>> q.rotate(3)<br />
>>> q<br />
deque([2, 3, 4, 0, 1])<br />
>>> q.rotate(-1)<br />
>>> q<br />
deque([3, 4, 0, 1, 2])