04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

226 CHAPTER 10 ■ BATTERIES INCLUDED<br />

The reason for the usefulness of the deque is that it allows appending and popping efficiently<br />

at the beginning (to the left), as opposed to lists. As a nice side effect, you can also rotate the<br />

elements (that is, shift them to the right or left, wrapping around the ends) efficiently. Deque<br />

objects also have extend and extendleft methods, with extend working like the corresponding<br />

list method, and extendleft working analogously to appendleft. Note that the elements in the<br />

iterable used in extendleft will appear in the deque in reverse order.<br />

time<br />

The time module contains functions for, among other things, getting the current time, manipulating<br />

times and dates, and reading dates from strings and formatting dates as strings. Dates<br />

can be represented as either a real number (the seconds since 0 hours, January 1 in the “epoch,”<br />

a platform-dependent year; for UNIX, it’s 1970), or a tuple containing nine integers. These integers<br />

are explained in Table 10-5. For example, the tuple<br />

(2002, 1, 21, 12, 2, 56, 0, 21, 0)<br />

represents January 21, 2002, at 12:02:56, which is a Monday, and the 21st day of the year.<br />

(No daylight savings.)<br />

Table 10-5. The Fields of Python Date Tuples<br />

Index Field Value<br />

0 Year For example, 2000, 2001, and so on<br />

1 Month In the range 1–12<br />

2 Day In the range 1–31<br />

3 Hour In the range 0–23<br />

4 Minute In the range 0–59<br />

5 Second In the range 0–61<br />

6 Weekday In the range 0–6, where Monday is 0<br />

7 Julian day In the range 1–366<br />

8 Daylight Savings 0, 1, or –1<br />

Some of these values need some explaining: The range for seconds is 0–61 to account for<br />

leap seconds and double leap seconds. The Daylight Savings number is a Boolean value (true<br />

or false), but if you use –1, mktime (a function that converts such a tuple to a timestamp measured<br />

in seconds since the epoch) will probably get it right. Some of the most important functions in<br />

the time module are described in Table 10-6.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!