It's often said that Python comes with "batteries included", in reference to the vast library of modules and packages that are distributed with the interpreter.
Some important modules of the standard library:
In addition to the builtin numeric types in the Python standard library, there are several modules devoted to implementing other types and mathematical operations.
The math module defines logarithmic, exponentiation, trigonometric, and hyperbolic functions, as well as angular conversions and more. The cmath module implements similar functions, but can handle complex numbers.
Example:
In [2]:
import math
import cmath
# Complex
for cpx in [3j, 1.5 + 1j, -2 - 2j]:
# Polar coordinate conversion
plr = cmath.polar(cpx)
print 'Complex:', cpx
print 'Polar:', plr, '(in radians)'
print 'Amplitude:', abs(cpx)
print 'Angle:', math.degrees(plr[1]), '(grades)'
The random module brings functions for random number generation.
Examples:
In [8]:
import random
import string
# Choose a letter
print random.choice(string.ascii_uppercase)
# Choose a number from 1 to 10
print random.randrange(1, 11)
# Choose a float from 0 to 1
print random.random()
In the standard library there is the decimal module that defines operations with real numbers with fixed precision.
Example:
In [9]:
from decimal import Decimal
t = 5.
for i in range(50):
t = t - 0.1
print 'Float:', t
t = Decimal('5.')
for i in range(50):
t = t - Decimal('0.1')
print 'Decimal:', t
With this module, it is possible to reduce the introduction of rounding errors arising from floating point arithmetic.
In version 2.6, the module fractions, which deals with rational numbers, is also available.
Example:
In [10]:
from fractions import Fraction
# Three fractions
f1 = Fraction('-2/3')
f2 = Fraction(3, 4)
f3 = Fraction('.25')
print "Fraction('-2/3') =", f1
print "Fraction('3, 4') =", f2
print "Fraction('.25') =", f3
# Sum
print f1, '+', f2, '=', f1 + f2
print f2, '+', f3, '=', f2 + f3
Fractions can be initialized in several ways: as a string, as a pair of integers, or as a real number. The module also has a function called gcd()
which calculates the greatest common divisor (gcd) of two integers.
Files in Python are represented by objects of type *file*, which offer various methods for file operations. Files can be opened for reading ('r', which is the default), writing ('w'), or appending ('a'), in text or binary ('b') mode.
In Python:
The standard input, output and error are handled by Python as open files. The input in read mode and the other in the recording mode.
Sample of writing:
In [3]:
import sys
# Create an object of type file
temp = open('temp.txt', 'w')
# Write output
for i in range(20):
temp.write('%03d\n' % i)
temp.close()
temp = open('temp.txt')
# Write in terminal
for x in temp:
# writing in sys.stdout sends
# text to standard output
sys.stdout.write(x)
temp.close()
At each iteration in the second loop, the object returns a line from the file each time.
Reading example:
In [3]:
import sys
import os.path
fn = 'test.txt'
if not os.path.exists(fn):
print 'Try again...'
sys.exit()
# Numbering lines
for i, s in enumerate(open(fn)):
print i + 1, s,
It is possible to read all the lines with the method readlines()
:
In [4]:
# Prints a list with all the lines from a file
print open('temp.txt').readlines()
The objects of type file also have the method seek()
, which allow going to any position in the file.
In version 2.6 the module io is available, which implements file operations and text manipulation routines separately.
Modern operating systems store files in hierarchical structures called file systems.
Several features related to file systems are implemented in the module os.path, such as:
os.path.basename()
: returns the final component of a path.os.path.dirname()
: returns a path without the final component.os.path.exists()
: returns True if the path exists or False otherwise.os.path.getsize()
: returns the size of the file in bytes.glob is another module related to the file system:
In [2]:
import os.path
import glob
# Shows a list of file names
# and their respective sizes
for arq in sorted(glob.glob('*.py')):
print arq, os.path.getsize(arq)
The glob.glob() function returns a list of filenames that meet the criteria passed as a parameter in a similar way to the ls
command available on UNIX systems.
The module os implements some functions to facilitate the creation of temporary files, freeing the developer from some concerns, such as:
Example:
In [4]:
import os
text = 'Test'
# creates a temporary file
temp = os.tmpfile()
# writes in the temp file
temp.write(text)
# Go back to the beginning the the file
temp.seek(0)
# Shows file content
print temp.read()
# Closes file
temp.close()
There is also the tempnam()
function, which returns a valid name for temporary file, including a path that respects the conventions of the operating system. However, it is up to the developer to ensure that the routine is used so as not to compromise the security of the application.
Python has modules to work with multiple formats of compressed files.
Example of writing a ".zip" file:
In [6]:
"""
Writing text in a compressed file
"""
import zipfile
text = """
**************************************
This text will be compressed and ...
... stored inside a zip file.
***************************************
"""
# Creates a new zip
zip = zipfile.ZipFile('arq.zip', 'w',
zipfile.ZIP_DEFLATED)
# Writes a string in zip as if it were a file
zip.writestr('text.txt', text)
# closes the zip
zip.close()
Reading example:
In [7]:
"""
Reading a compressed file
"""
import zipfile
# Open the zip file for reading
zip = zipfile.ZipFile('arq.zip')
# Gets a list of compressed files
arqs = zip.namelist()
for arq in arqs:
# Shows the file name
print 'File:', arq
# get file info
zipinfo = zip.getinfo(arq)
print 'Original size:', zipinfo.file_size
print 'Compressed size:', zipinfo.compress_size
# Shows file content
print zip.read(arq)
Python also provides modules for gzip, bzip2 and tar formats that are widely used in UNIX environments.
In the standard library, Python also provides a module to simplify the processing of files in CSV (Comma Separated Values) format.
In CSV format, the data is stored in text form, separated by commas, one record per line.
Writing example:
In [ ]:
import csv
# Data
dt = (('temperatura', 15.0, 'C', '10:40', '2006-12-31'),
('peso', 42.5, 'kg', '10:45', '2006-12-31'))
# A writing routine which receives one object of type file
out = csv.writer(file('dt.csv', 'w'))
# Writing the tuples in file
out.writerows(dt)
Reading example:
In [9]:
import csv
# The reading routine receives a file object
dt = csv.reader(file('dt.csv'))
# For each record in file, prints
for reg in dt:
print reg
In [10]:
import os
import sys
import platform
def uid():
"""
uid() -> returns the current user identification
or None if not possible to identify
"""
# Ambient variables for each operating system
us = {'Windows': 'USERNAME',
'Linux': 'USER'}
u = us.get(platform.system())
return os.environ.get(u)
print 'User:', uid()
print 'plataform:', platform.platform()
print 'Current dir:', os.path.abspath(os.curdir)
exep, exef = os.path.split(sys.executable)
print 'Executable:', exef
print 'Executable dir:', exep
Process execution example:
In [ ]:
import sys
from subprocess import Popen, PIPE
# ping
cmd = 'ping -c 1 '
# No Windows
if sys.platform == 'win32':
cmd = 'ping -n 1 '
# Local just for testing
host = '127.0.0.1'
# Comunicates with another process
# a pipe with the command stdout
py = Popen(cmd + host, stdout=PIPE)
# Shows command output
print py.stdout.read()
The subprocess module provides a generic way of running processes with Popen() function which allows communication with the process through operating system pipes.
Python has two modules to handle time:
Example with time:
In [12]:
import time
# localtime() Returns a date and local time in the form
# of a structure called struct_time, which is a
# collection with the items: year, month, day, hour, minute,
# secund, day of the week, day of the year and e daylight saving time
print time.localtime()
# asctime() returns a date and hour with string, according to
# operating system configuration
print time.asctime()
# time() returns system time in seconds
ts1 = time.time()
# gmtime() converts seconds to struct_time
tt1 = time.gmtime(ts1)
print ts1, '->', tt1
# Adding an hour
tt2 = time.gmtime(ts1 + 3600.)
# mktime() converts struct_time to seconds
ts2 = time.mktime(tt2)
print ts2, '->', tt2
# clock() returs time since the program started, in seconds
print 'The program took', time.clock(), \
'seconds up to now...'
# Counting seconds...
for i in xrange(5):
# sleep() waits the number of seconds specified as parameter
time.sleep(1)
print i + 1, 'second(s)'
In datetime, four types are defined for representing time:
Example:
In [14]:
import datetime
# datetime() receives as parameter:
# year, month, day, hour, minute, second and
# returns an object of type datetime
dt = datetime.datetime(2020, 12, 31, 23, 59, 59)
# Objects date and time can be created from
# a datetime object
date = dt.date()
hour = dt.time()
# How many time to 12/31/2020
dd = dt - dt.today()
print 'Date:', date
print 'Hour:', hour
print 'How many time to 12/31/2020:', dd
Objects of types date and datetime return dates in ISO format.
Regular expression is a form of identifying patterns in character strings. In Python, the re module provides a syntactic parser that allows the use of such expressions. The patterns are defined by characters that have special meaning to the parser.
Main characteres:
.
): In standard mode means any character except the newline.^
): In standard mode, means beginning of the string.$
): In standard mode, means end of the string.\
): Escape character, allows using special chars as normal chars.[]
): Any character of the listed inside the brackets.*
): Zero or more ocurrrences of previous expression.+
): One or more ocurrences of previous expression.?
): Zero or one ocurrence of previous expression.{n}
): n ocurrences of previous expression.|
): logical “or”.()
): Delimit a group of expressions.\d
: Digit. Same as [0-9]
.\D
: Non digit. Same as [^0-9]
.\s
: Any spacing character ([ \t\n\r\f\v]
).\S
: Any nonspacing character ([^ \t\n\r\f\v]
).\w
: Alphanumeric character or underline ([a-zA-Z0-9_]
).\W
: Not an Alphanumeric character or underline ([^a-zA-Z0-9_]
).Exemplos:
In [22]:
import re
# Compile the regular expression using compile()
# the compiled regular expression is stored and
# can be reused
rex = re.compile('\w+')
# Finds the occurrences according to the expression
bands = 'Yes, Genesis & Camel'
print bands, '->', rex.findall(bands)
# Identify occurrences of Björk (and their variations)
bjork = re.compile('[Bb]j[öo]rk')
for m in ('Björk', 'björk', 'Biork', 'Bjork', 'bjork'):
# match() finds occurrences at the beginning of the string
# to find at any part of the string, use search()
print m, '->', bool(bjork.match(m))
# Replacing text
text = 'The next track is Stairway to Heaven'
print text, '->', re. sub('[Ss]tairway [Tt]o [Hh]eaven',
'The Rover', text)
# Splitting text
bands = 'Tool, Porcupine Tree and NIN'
print bands, '->', re.split(',?\s+and?\s+', bands)
The behaviour of the functions of this module can be changed by options, to treat strings as unicode, for instance.
In [1]:
Out[1]: