15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

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

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

Sat May 5 16:36:23 1990::fclihw@alwdbzpsdg.edu::641950583-6-10<br />

Thu Feb 15 17:46:04 2007::uzifzf@dpyivihw.gov::1171590364-6-8<br />

Thu Jun 26 19:08:59 2036::ugxfugt@jkhuqhs.net::2098145339-7-7<br />

Tue Apr 10 01:04:45 2012::zkwaq@rpxwmtikse.com::1334045085-5-10<br />

You may or may not be able to tell, but the output from this program is ripe for regular expression<br />

processing. Following our line-by-line explanation, we will implement several REs to operate on these<br />

data, as well as leave plenty for the end-of-chapter exercises.<br />

Line-by-Line Explanation<br />

Lines 16<br />

In our example script, we require the use of multiple modules. But since we are utilizing only one or two<br />

functions from these modules, rather than importing the entire module, we choose in this case to import<br />

only specific attributes from these modules. Our decision to use from-import rather than import was<br />

based solely on this reasoning. The from-import lines follow the Unix startup directive on line 1.<br />

Line 8<br />

doms is simply a set of higher-level domain names from which we will randomly pick for each randomly<br />

generated e-mail address.<br />

Lines 1012<br />

Each time gendata.py executes, between 5 and 10 lines of output are generated. (Our script uses the<br />

random.randint() function for all cases where we desire a random integer.) For each line, we choose a<br />

random integer from the entire possible range (0 to 231 - 1 [sys.maxint]), then convert that integer to a<br />

date using time.ctime(). System time in <strong>Python</strong> and most Unix-based computers is based on the<br />

number of seconds that have elapsed since the "epoch," midnight UTC/GMT on January 1, 1970. If we<br />

choose a 32-bit integer, that represents one moment in time from the epoch to the maximum possible<br />

time, 232 seconds after the epoch.<br />

Lines 1422<br />

The login name for the fake e-mail address should be between 4 and 7 characters in length. To put it<br />

together, we randomly choose between 4 and 7 random lowercase letters, concatenating each letter to<br />

our string one at a time. The functionality of the random.choice() function is given a sequence, return a<br />

random element of that sequence. In our case, the sequence is the set of all 26 lowercase letters of the<br />

alphabet, string.lowercase.<br />

We decided that the main domain name for the fake e-mail address should be between 4 and 12<br />

characters in length, but at least as long as the login name. Again, we use random lowercase letters to<br />

put this name together letter by letter.<br />

Lines 2425<br />

The key component of our script puts together all of the random data into the output line. The date<br />

string comes first, followed by the delimiter. We then put together the random e-mail address by<br />

concatenating the login name, the "@" symbol, the domain name, and a randomly chosen high-level

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

Saved successfully!

Ooh no, something went wrong!