Flylib.com

Books Software

 
 
 

2.15 The glob Module


2.15 The glob Module

The glob module generates lists of files matching given patterns, just like the Unix shell.

File patterns are similar to regular expressions, but simpler. An asterisk ( * ) matches zero or more characters , and a question mark ( ? ) matches exactly one character. You can also use brackets to indicate character ranges, such as [0-9] for a single digit. All other characters match themselves .

glob(pattern) returns a list of all files matching a given pattern. The glob module is demonstrated in Example 2-26.

Example 2-26. Using the glob Module
File: glob-example-1.py

import glob

for file in glob.glob("samples/*.jpg"):
    print file

samples/sample.jpg

Note that glob returns full pathnames, unlike the os.listdir function. glob uses the fnmatch module to do the actual pattern matching.


2.16 The fnmatch Module

The fnmatch module matches filenames against a pattern, as Example 2-27 shows.

The pattern syntax is the same as that used in Unix shells . An asterisk ( * ) matches zero or more characters , and a question mark ( ? ) matches exactly one character. You can also use brackets to indicate character ranges, such as [0-9] for a single digit. All other characters match themselves .

Example 2-27. Using the fnmatch Module to Match Files
File: fnmatch-example-1.py

import fnmatch
import os

for file in os.listdir("samples"):
    if fnmatch.fnmatch(file, "*.jpg"):
        print file

sample.jpg

In Example 2-28, the translate function converts a file pattern to a regular expression.

Example 2-28. Using the fnmatch Module to Convert a Pattern to a Regular Expression
File: fnmatch-example-2.py

import fnmatch
import os, re

pattern = fnmatch.translate("*.jpg")

for file in os.listdir("samples"):
    if re.match(pattern, file):
        print file

print "(pattern was %s)" % pattern

sample.jpg
(pattern was .*\.jpg$)

The fnmatch module is used by the glob and find modules.


2.17 The random Module

"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."

—John von Neumann, 1951

The random module contains a number of random number generators.

The basic random number generator (after an algorithm by Wichmann and Hill, 1982) can be accessed in several ways, as Example 2-29 shows.

Example 2-29. Using the random Module to Get Random Numbers
File: random-example-1.py

import random

for i in range(5):

    # random float: 0.0 <= number < 1.0
    print random.random(),

    # random float: 10 <= number < 20
    print random.uniform(10, 20),

    # random integer: 100 <= number <= 1000
    print random.randint(100, 1000),

    # random integer: even numbers in 100 <= number < 1000
    print random.randrange(100, 1000, 2)

0.946842713956 19.5910069381 709 172
0.573613195398 16.2758417025 407 120
0.363241598013 16.8079747714 916 580
0.602115173978 18.386796935 531 774
0.526767588533 18.0783794596 223 344

Note that the randint function can return the upper limit, while the other functions always return values smaller than the upper limit.

Example 2-30 shows how the choice function picks a random item from a sequence. It can be used with lists, tuples, or any other sequence (provided it can be accessed in random order, of course).

Example 2-30. Using the random Module for Random Items from a Sequence
File: random-example-2.py

import random

# random choice from a list
for i in range(5):
    print random.choice([1, 2, 3, 5, 9])

2
3
1
9
1

In 2.0 and later, the shuffle function can be used to shuffle the contents of a list (that is, generate a random permutation of a list in-place). Example 2-31 also shows how to implement that function under 1.5.2 and earlier.

Example 2-31. Using the random Module to Shuffle a Deck of Cards
File: random-example-4.py

import random

try:
    # available in 2.0 and later
    shuffle = random.shuffle
except AttributeError:
    def shuffle(x):
        for i in xrange(len(x)-1, 0, -1):
            # pick an element in x[:i+1] with which to exchange x[i]
            j = int(random.random() * (i+1))
            x[i], x[j] = x[j], x[i]

cards = range(52)

shuffle(cards)

myhand = cards[:5]

print myhand

[4, 8, 40, 12, 30]

The random module also contains random generators with non-uniform distribution. Example 2-32 uses the gauss function to generate random numbers with a gaussian distribution.

Example 2-32. Using the random Module for Gaussian Random Numbers
File: random-example-3.py

import random

histogram = [0] * 20

# calculate histogram for gaussian
# noise, using average=5, stddev=1
for i in range(1000):
    i = int(random.gauss(5, 1) * 2)
    histogram[i] = histogram[i] + 1

# print the histogram
m = max(histogram)
for v in histogram:
    print "*" * (v * 50 / m)

****
**********
*************************
***********************************
************************************************
**************************************************
*************************************
***************************
*************
***
*

See the Python Library Reference for more information on non-uniform generators.

The random-number generators provided in the standard library are pseudo-random generators. While this might be good enough for many purposes—including simulations, numerical analysis, and games —it's definitely not good enough for cryptographic use.