liveBook · Manning

Calcsize

As explained in The Quick Python Book, Fourth Edition

calcsize is a struct-module function that returns the number of bytes required by a specified format string.

calcsize is a struct-module function that accepts a format string and returns the number of bytes required for data in that format. Use it to determine how many bytes to read for each binary record before calling struct.unpack. section 13.7

In a binary-record workflow, calcsize connects the format definition to file reading:

  • Define one format string, such as 'hd7s', for the record layout. section 13.7
  • Pass that string to struct.calcsize to obtain the record size. section 13.7
  • Read that many bytes, then pass the record and the same format string to struct.unpack. section 13.7

Reading fixed-size records

With record_format = 'hd7s', set record_size = struct.calcsize(record_format), read record_size bytes in each loop iteration, and unpack each nonempty record with struct.unpack(record_format, record). section 13.7

import struct
 
record_format = 'hd7s'
record_size = struct.calcsize(record_format)
result_list = []
with open("data", 'rb') as input: 
    while True:
        record = input.read(record_size)                         #1
        if not record:                                           #2
            break
        result_list.append(struct.unpack(record_format, record)) #3
 
print(result_list)
 
[(42, 3.14, b'goodbye')]]
#1 Reads in a single record
#2 Checks to see if record is empty
#3 Unpacks record into a tuple; appends to results

Shows calcsize used to control one-record-at-a-time reads and struct.unpack used to recover the original values. section 13.7

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the byte-count operation and demonstrates its role in a read loop, but defers detailed format-string documentation.
Examples
Uses the 'hd7s' format to size, read, and unpack records containing 42, 3.14, and b'goodbye'.
What it assumes
Assumes familiarity with format strings, binary files, and struct.pack or struct.unpack.
What you can do after
Operate binary-record readers by calculating each record's byte length before calling struct.unpack.
How it connects
Connects format strings with struct.pack, struct.unpack, binary files, and endianness or C-size options.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.