Library Reference

Hashlib

A common interface to many hash functions.

new(name, string='') - Returns a new hash object implementing the given hash function. Initializing the hash uses the given string data.

Named constructor functions are also available. These are much faster than using new():

md5(), sha1(), sha224(), sha256(), sha384(), and sha512()

More algorithms may be available on your platform, but the above are guaranteed to exist.

NOTE: If you want the adler32 or crc32 hash functions, they are available in the zlib module.

Choose your hash function wisely. Some have known collision weaknesses. sha384 and sha512 will be slow on 32 bit platforms.

Hash objects have these methods:

  • update(arg): Update the hash object with the string arg. Repeated calls are equivalent to a single call with the concatenation of all the arguments.
  • digest(): Return the digest of the strings passed to the update() method so far. This may contain non-ASCII characters, including NUL bytes.
  • hexdigest(): Like digest(), except the digest is returned as a string of double length containing only hexadecimal digits.
  • copy(): Return a copy (clone) of the hash object. This can be used to efficiently compute the digests of strings that share a common initial substring.

As an example, to obtain the digest of the string 'Nobody inspects the

spammish repetition', use the follwoing code:

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
>>> m.digest()
'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'

More condensed:

>>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'

math

This module provides access to the mathematical functions. The version of this module is forked from the version that comes with Python 2.7.

Number functions

  • math.ceil - Returns a whole number rounded up to the nearest whole number
>>> import math
>>> math.ceil(4.5)
5
>>> math.ceil(-5.01)
-5
  • math.fabs - Returns the absolute value
>>> math.fabs(-3.768)
3.768
  • math.floor - Similar to ceil, but it rounds down to the nearest whole number.
>>> math.floor(4.5)
4
>>> math.floor(-5.01)
-6

Logarithmic and power functions

  • math.exp - Returns e raised to the power of the second passed in number.
>>> math.exp(3)
20.085536923187668
  • math.log - Return the natural logarithm of the passed in number.
>>> math.log(20.085536923187668)
3.0
  • math.sqrt - Returns the square root of a number.
>>> math.sqrt(81)
9.0
  • math.pow - Return x raised to the power y.
>>> math.pow(3, 3)
27.0

Trigonometric functions

The following functions are available: acos, asin, atan, cos, sin, and tan. If you want the hyperbolic versions of these, see the cmath library. You can also use math.pi to get an accurate representation of pi.

>>> math.cos(math.pi)
-1.0

cmath

This library contains functions to help calculating complex numbers.

Hyperbolic Trigonometric Functions

This library contains the following hyperbolic trigonometric functions: acosh, asinh, atanh, cosh, sinh, and tanh.

>>> import cmath
>>> cmath.cosh(0)
(1+0j)

Returned will always be a complex number.

collections

A collection of specialized container datatypes.

Deque

Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Deques have a popleft and appendleft that allow you to place and remove items at the beginning of the list.

>>> from collections import deque
>>> d = deque('abc')
>>> d.appendleft("x")
>>> d
deque(['x', 'a', 'b', 'c'])
>>> d.popleft()
'x'
>>> d
deque(['a', 'b', 'c'])

It also has the normal pop and append methods that adds and removes from the right (end).

defaultdict

A defaultdict is a lot like a normal dictionary, except it takes an iterable in the constructor which is used to generate a new value each time a key is implicitly created.

>>> from collections import defaultdict
>>> dd = defaultdict(list)
>>> dd['my_key'].append("x")
>>> dd
defaultdict(list, {'my_key': ['x']})

With a normal dictionary, a KeyError would get raised in the above example when calling .append, but instead, the defaultdict will implicitly create the key using list to generate a key for you by default. lambda: [] would also work.

itertools

Tools for optimizing loops.

result = []
>>> for x in itertools.chain([1,2,3], [4,5,6]):
...     result.append(x)
>>> result
[1, 2, 3, 4, 5, 6]
>>> result = itertools.compress([1,1,2,2,3,3,4,4], [0,1,0,1])
>>> list(result)
[1,2]
>>> result = []
>>> for x in itertools.cycle([1,2]):
...     result.append(x)
...     if len(result) > 5:
...         break
>>> result
[1, 2, 1, 2, 1, 2]
>>> r = itertools.dropwhile(lambda x: x < 5, [1,2,3,4,5,6,7,8,9,10])
>>> list(r)
[5, 6, 7, 8, 9, 10]
>>> r = itertools.permutations('ABCD', 2)
>>> set(r)
set([
    ('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'A'), ('B', 'C'), ('B', 'D'),
    ('C', 'A'), ('C', 'B'), ('C', 'D'), ('D', 'A'), ('D', 'B'), ('D', 'C')
])
r = itertools.product('ABCD', 'xy')
>>> list(r)
[
    ('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'),
    ('C', 'x'), ('C', 'y'), ('D', 'x'), ('D', 'y')
]
>>> r = itertools.repeat(10, 3)
>>> list(r)
[10, 10, 10]
>>> r = itertools.starmap(pow, [(2,5), (3,2), (10,3)])
>>> list(r)
[32, 9, 1000]
>>> r = itertools.takewhile(lambda x: x<5, [1,4,6,4,1])
>>> list(r)
[1, 4]
>>> r = itertools.tee([1,2,3], 3)
>>> list(list(x) for x in r)
[[1,2,3], [1,2,3], [1,2,3]]

array

More optimized list.

>>> import array
>>> a = array.array('c', 'abcdefg')
>>> a.append('h')
>>> a.tostring(),
'abcdefgh'
>>> a.tolist()
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> a.reverse()
array.array('c', 'hgfedcba'))
>>> array.array('i', [1,2,3,3,3,4,5,6]).count(3)
3
>>> a = array.array('i', [1,2,3])
>>> a.extend([4,5,6])
>>> a
array.array('i', [1, 2, 3, 4, 5, 6])
>>> array.array('f', [3.4, 5.0, 8.3, 5.6]).pop(1)
5.0

binascii

Library for converting between various encoding representations.

bin = b'R\x9d\x94r\x12\xa4\x1a\xec\xb4\x11\x90\xdcY\x9a\x96\xccReWimBsDWONrzoeO'
b64 = b"Up2UchKkGuy0EZDcWZqWzFJlV2ltQnNEV09OcnpvZU8="
hex = b'529d947212a41aecb41190dc599a96cc526557696d427344574f4e727a6f654f'
>>> import binascii
>>> binascii.a2b_base64(b64)
bin
>>> binascii.b2a_base64(bin)
b64 + "\n"
>>> binascii.hexlify(bin)
hex
>>> binascii.unhexlify(hex)
bin

json

Library for encoding and decoding json objects.

>>> import json
>>> json.loads('{"x": 4}')
{"x": 4}

For the time being, json.dumps is not implemented.

>>> json.dumps({"x": 4}), '{"x": 4}') #not working
AttributeError: 'module' object has no attribute 'dumps'

md5

>>> import md5
>>> m = md5.new()
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
>>> m.digest()
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>>> m.hexdigest()
'bb649c83dd1ea5c9d9dec9a18df0ffe9'

pyaes

>>> key = b'R\x9d\x94r\x12\xa4\x1a\xec\xb4\x11\x90\xdcY\x9a\x96\xccReWimBsDWONrzoeO'
>>> iv = b'Ze~o&G\xadvH\xc2v\x04\x86\xbc\x84\x92'
>>> ciphertext = b''
>>> encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCBC(key, iv))
>>> ciphertext += encrypter.feed("abc123")
>>> ciphertext += encrypter.feed()
>>> ciphertext
b'\x19\xd6X\x1c\xcf\x91\xb2\xd2\\\xa4\x91\xeb\xf9\xb9Yo'

sha

>>> import hashlib
>>> s = hashlib.sha1()
>>> s1.update("Nobody inspects")
>>> s1.update(" the spammish repetition")
>>> s1.digest()
>>> s1.hexdigest()
'531b07a0f5b66477a21742d2827176264f4bbfe2'

sha256

>>> import hashlib
>>> s = hashlib.sha256.new()
>>> s256.update("Nobody inspects")
>>> s256.update(" the spammish repetition")
>>> s256.digest()
>>> s256.hexdigest()

Sha3

>>> import sha3
>>> k = sha3.sha3_512()
>>> k.update(b"data")
>>> k.hexdigest()
'1065aceeded3a5e4412e2187e919bffeadf815f5bd73d37fe00d384fe29f55f08462fdabe1007b993ce5b8119630e7db93101d9425d6e352e22ffe3dcb56b825'

hmac

>>> h = hmac.new(bin, digestmod=hashlib.sha256)
>>> h.update(b"hello")
>>> h.hexdigest()
'c2526ced74b228bc2c6f4c8c6a406898904d2cf3ba5d33e7ea2aaddddcd8c2b5')

ecdsa

Elliptic curve signature library

>>> from ecdsa import SigningKey
>>> sk = SigningKey.generate() # uses NIST192p
>>> vk = sk.verifying_key
>>> signature = sk.sign(b"message")
>>> vk.verify(signature, b"message")
True

random

Tools for generating random numbers.

>>> state1 = random.getstate()
>>> r1 = random.random()
>>> random.setstate(state1)
>>> r2 = random.random()
>>> r1 == r2
True