One Python module per week | array

Column address: One Python module per week

This module defines a data structure that looks like a list, but it requires all members to have the same type.

You can use the following table as a simple reference. The array standard library document contains a complete list of type codes.

Code Type Minimum size (bytes)
b int 1
B int 1
h signed short 2
H unsigned short 2
i signed int 2
I unsigned int 2
l signed long 4
L unsigned long 4
q signed long long 8
Q unsigned long long 8
f float 4
d double float 8

Initialization

array requires two parameters, the first is the data type, and the second is the data to be passed in.

import array
import binascii

s = b'This is the array.'
a = array.array('b', s)

print('As byte string:', s)
print('As array      :', a)
print('As hex        :', binascii.hexlify(a))

# output
# As byte string: b'This is the array.'
# As array      : array('b', [84, 104, 105, 115, 32, 105, 115, 32,
#  116, 104, 101, 32, 97, 114, 114, 97, 121, 46])
# As hex        : b'54686973206973207468652061727261792e'

Operate Arrays

You can operate array in the same way as other Python sequences. The supported operations include slicing, iterating, and adding elements to the end.

import array
import pprint

a = array.array('i', range(3))
print('Initial :', a)

a.extend(range(3))
print('Extended:', a)

print('Slice   :', a[2:5])

print('Iterator:')
print(list(enumerate(a)))

# output
# Initial : array('i', [0, 1, 2])
# Extended: array('i', [0, 1, 2, 0, 1, 2])
# Slice   : array('i', [2, 0, 1])
# Iterator:
# [(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2)]

Arrays and Files

You can write the contents of the array to a file, or read the contents of the file and save them to an array.

import array
import binascii
import tempfile

a = array.array('i', range(5))
print('A1:', a)

# Write the array of numbers to a temporary file
output = tempfile.NamedTemporaryFile()
a.tofile(output.file)  # must pass an *actual* file
output.flush()

# Read the raw data
with open(output.name, 'rb') as input:
    raw_data = input.read()
    print('Raw Contents:', binascii.hexlify(raw_data))

    # Read the data into an array
    input.seek(0)
    a2 = array.array('i')
    a2.fromfile(input, len(a))
    print('A2:', a2)
    
# output
# A1: array('i', [0, 1, 2, 3, 4])
# Raw Contents: b'0000000001000000020000000300000004000000'
# A2: array('i', [0, 1, 2, 3, 4])

This example demonstrates reading the data "raw" directly from the binary instead of reading it into a new array and converting the bytes to the appropriate type.

tofile() formats the data with tobytes(), then fromfile() converts it back to the array instance with frombytes().

import array
import binascii

a = array.array('i', range(5))
print('A1:', a)

as_bytes = a.tobytes()
print('Bytes:', binascii.hexlify(as_bytes))

a2 = array.array('i')
a2.frombytes(as_bytes)
print('A2:', a2)

# output
# A1: array('i', [0, 1, 2, 3, 4])
# Bytes: b'0000000001000000020000000300000004000000'
# A2: array('i', [0, 1, 2, 3, 4])

A byte string that both tobytes() and frombytes() operate on, not a Unicode string.



Related documents:

https://pymotw.com/3/array/index.html

Tags: Python

Posted on Tue, 03 Dec 2019 02:11:38 -0800 by CooKies37