Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 227 Table 10-6. Some Important Functions in the time Module Function asctime([tuple]) localtime([secs]) mktime(tuple) sleep(secs) strptime(string[, format]) time() Description Converts time tuple to a string Converts seconds to a date tuple, local time Converts time tuple to local time Sleeps (does nothing) for secs seconds Parses a string into a time tuple Current time (seconds since the epoch, UTC) The function time.asctime formats the current time as a string, such as >>> time.asctime() 'Fri Dec 21 05:41:27 2001' You can also supply a date tuple (such as those created by localtime) if you don’t want the current time. (For more elaborate formatting, see the strftime function, described in the standard documentation.) The function time.localtime converts a real number (seconds since epoch) to a date tuple, local time. If you want universal time, use gmtime instead. The function time.mktime converts a date tuple to the time since epoch in seconds; it is the inverse of localtime. The function time.sleep makes the interpreter wait for a given number of seconds. The function time.strptime converts a string of the format returned by asctime to a date tuple. (The optional format argument follows the same rules as those for strftime. See the standard documentation.) The function time.time returns the current (universal) time as seconds since the epoch. Even though the epoch may vary from platform to platform, you can reliably time something by storing the result of time before and after the event (such as a function call) and then computing the difference. For an example of these functions, see the next section, which covers the random module. OTHER TIME FUNCTIONS The functions shown in Table 10-6 are just a selection of those available from the time module. Most of the functions in that module perform tasks similar to or related to those described in this section. If you need something not covered by the functions described here, you should take a look at the section about the time module in the standard library reference (http://python.org/doc/lib/module-time.html); chances are you may find exactly what you are looking for. There are also some more recent time-related modules available: datetime and timeit. You can find more information about both in the library reference, and timeit is also discussed briefly in Chapter 16.
228 CHAPTER 10 ■ BATTERIES INCLUDED random The random module contains functions that return random numbers, which can be useful for simulations or any program that generates random output. ■Note Actually, the numbers generated are pseudo-random. That means that while they appear completely random, there is a predictable system that underlies them. However, because the module is so good at pretending to be random, you probably won’t ever have to worry about this (unless you want to use these numbers for strong-cryptography purposes, in which case they may not be “strong” enough to withstand determined attack—but if you’re into strong cryptography, you surely don’t need me to explain such elementary issues). If you need real randomness, you should check out the urandom function of the os module. The class SystemRandom in the random module is based on the same kind of functionality, and gives you data that is close to real randomness. Some important functions in this module are shown in Table 10-7. Table 10-7. Some Important Functions in the random Module Function Description random() Returns a random real number n such that 0 ≤ n < 1 getrandbits(n) uniform(a, b) randrange([start], stop, [step]) choice(seq) shuffle(seq[, random]) sample(seq, n) Returns n random bits, in the form of a long integer Returns a random real number n such that a ≤ n < b Returns a random number from range(start, stop, step) Returns a random element from the sequence seq Shuffles the sequence seq in place Chooses n random, unique elements from the sequence seq The function random.random is one of the most basic random functions; it simply returns a pseudo-random number n such that 0 < n < 1. Unless this is exactly what you need, you should probably use one of the other functions, which offer extra functionality. The function random.getrandbits returns a given number of bits (binary digits), in the form of a long integer. This is probably mostly useful if you’re really into random stuff (for example, working with cryptography). The function random.uniform, when supplied with two numerical parameters a and b, returns a random (uniformly distributed) real number n such that a < n < b. So, for example, if you want a random angle, you could use uniform(0,360). The function random.randrange is the standard function for generating a random integer in the range you would get by calling range with the same arguments. For example, to get a
- 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 256 and 257: 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
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
CHAPTER 10 ■ BATTERIES INCLUDED 227<br />
Table 10-6. Some Important Functions in the time Module<br />
Function<br />
asctime([tuple])<br />
localtime([secs])<br />
mktime(tuple)<br />
sleep(secs)<br />
strptime(string[, format])<br />
time()<br />
Description<br />
Converts time tuple <strong>to</strong> a string<br />
Converts seconds <strong>to</strong> a date tuple, local time<br />
Converts time tuple <strong>to</strong> local time<br />
Sleeps (does nothing) for secs seconds<br />
Parses a string in<strong>to</strong> a time tuple<br />
Current time (seconds since the epoch, UTC)<br />
The function time.asctime formats the current time as a string, such as<br />
>>> time.asctime()<br />
'Fri Dec 21 05:41:27 2001'<br />
You can also supply a date tuple (such as those created by localtime) if you don’t want the<br />
current time. (For more elaborate formatting, see the strftime function, described in the standard<br />
documentation.)<br />
The function time.localtime converts a real number (seconds since epoch) <strong>to</strong> a date tuple,<br />
local time. If you want universal time, use gmtime instead.<br />
The function time.mktime converts a date tuple <strong>to</strong> the time since epoch in seconds; it is the<br />
inverse of localtime.<br />
The function time.sleep makes the interpreter wait for a given number of seconds.<br />
The function time.strptime converts a string of the format returned by asctime <strong>to</strong> a date<br />
tuple. (The optional format argument follows the same rules as those for strftime. See the<br />
standard documentation.)<br />
The function time.time returns the current (universal) time as seconds since the epoch.<br />
Even though the epoch may vary from platform <strong>to</strong> platform, you can reliably time something<br />
by s<strong>to</strong>ring the result of time before and after the event (such as a function call) and then computing<br />
the difference. For an example of these functions, see the next section, which covers the random<br />
module.<br />
OTHER TIME FUNCTIONS<br />
The functions shown in Table 10-6 are just a selection of those available from the time module. Most of the<br />
functions in that module perform tasks similar <strong>to</strong> or related <strong>to</strong> those described in this section. If you need<br />
something not covered by the functions described here, you should take a look at the section about the time<br />
module in the standard library reference (http://python.org/doc/lib/module-time.html); chances<br />
are you may find exactly what you are looking for.<br />
There are also some more recent time-related modules available: datetime and timeit. You can find<br />
more information about both in the library reference, and timeit is also discussed briefly in Chapter 16.