IO Modules
For reading and writing data from / to files.
read_ascii(open_file, length_bytes=1)
Read an ASCII string of defined length from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
length_bytes
|
int
|
Length of the ASCII string in bytes that should be read. Default: 1 byte (1 character). |
1
|
Returns:
Type | Description |
---|---|
str
|
ASCII text decoded from file. |
Source code in AFMReader/io.py
read_bool(open_file)
Read a boolean from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
bool
|
Boolean decoded value. |
Source code in AFMReader/io.py
read_char(open_file)
Read a character from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open file object. |
required |
Returns:
Type | Description |
---|---|
str
|
A string type cast from the decoded character. |
Source code in AFMReader/io.py
read_double(open_file)
Read an 8 byte double from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
float
|
Float decoded from the double value. |
Source code in AFMReader/io.py
read_float(open_file)
Read a float from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
float
|
Float decoded value. |
Source code in AFMReader/io.py
read_hex_u32(open_file)
Read a hex encoded unsigned 32 bit integer value from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
str
|
String representing a hexadecimal encoded integer value. |
Source code in AFMReader/io.py
read_int16(open_file)
Read a signed 16 bit integer from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
int
|
Integer decoded value. |
Source code in AFMReader/io.py
read_int32(open_file)
Read a signed 32 bit integer from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
int
|
Integer decoded value. |
Source code in AFMReader/io.py
read_int8(open_file)
Read a signed 8 bit integer from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
int
|
Integer decoded value. |
Source code in AFMReader/io.py
read_null_separated_utf8(open_file, length_bytes=2)
Read an ASCII string of defined length from an open binary file.
Each character is separated by a null byte. This encoding is known as UTF-16LE (Little Endian). Eg: b'\x74\x00\x6f\x00\x70\x00\x6f' would decode to 'topo' in this format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
length_bytes
|
int
|
Length of the ASCII string in bytes that should be read. Default: 2 bytes (1 UTF-16LE character). |
2
|
Returns:
Type | Description |
---|---|
str
|
ASCII text decoded from file. |
Source code in AFMReader/io.py
read_null_terminated_string(open_file, encoding='utf-8')
Read an open file from the current position in the open binary file, until the next null value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open file object. |
required |
encoding
|
str
|
Encoding to use when decoding the bytes. |
'utf-8'
|
Returns:
Type | Description |
---|---|
str
|
String of the ASCII decoded bytes before the next null byte. |
Examples:
Source code in AFMReader/io.py
read_uint32(open_file)
Read an unsigned 32 bit integer from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
int
|
Integer decoded value. |
Source code in AFMReader/io.py
read_uint8(open_file)
Read an unsigned 8 bit integer from an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
Returns:
Type | Description |
---|---|
int
|
Integer decoded value. |
Source code in AFMReader/io.py
skip_bytes(open_file, length_bytes=1)
Skip a specified number of bytes when reading an open binary file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_file
|
BinaryIO
|
An open binary file object. |
required |
length_bytes
|
int
|
Number of bytes to skip. |
1
|
Returns:
Type | Description |
---|---|
bytes
|
The bytes that were skipped. |
Source code in AFMReader/io.py
unpack_hdf5(open_hdf5_file, group_path='/')
Read a dictionary from an open hdf5 file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_hdf5_file
|
File
|
An open hdf5 file object. |
required |
group_path
|
str
|
Path to the group in the hdf5 file to start reading the data from. |
'/'
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing the data from the hdf5 file. |
Examples:
Read the data from the root group of the hdf5 file.
>>> with h5py.File("path/to/file.h5", "r") as f:
>>> data = unpack_hdf5(open_hdf5_file=f, group_path="/")
Read data from a particular dataset in the hdf5 file.
>>> with h5py.File("path/to/file.h5", "r") as f:
>>> data = unpack_hdf5(open_hdf5_file=f, group_path="/dataset_name")