Fields
Fields are named components within a structure. At their most basic, they represent simple values
that can be converted directly to Python primitives, like int and str. More complex fields
could contain multiple attributes, value lists or nested structures.
Using Fields
Fields are assigned as attributes to a Structure’s class definition. This assignment allows for naming individual fields, as well as configuring each field as necessary:
import steel
class Header(steel.Structure):
tag = steel.FixedBytes(b"DATA")
major_version = steel.Integer(size=1)
minor_version = steel.Integer(size=1)
created_date = steel.Timestamp(tz="utc")
title = steel.NullTerminatedString(encoding="ascii")
@property
def version(self) -> str:
f"{obj.major_version}.{obj.minor_version}"
Field are added from top to bottom, in the order that the data appears in the target data structure.
So a data stream for the Header above could look like this:
data = (
b"DATA" # tag
b"\x01" # major_version
b"\x05" # minor_version
b"OW\xb0h" # created_date
b"Example\x00" # title
)
# Resulting value: b'DATA\x01\x05OW\xb0hExample\x00'
After loading the structure, fields can be accessed as instance attributes, which will present the data in native data types defined by each field:
obj = Header.loads(data)
(
obj.tag, # DATA
obj.version, # 1.5
obj.created_date # datetime.datetime(2025, 8, 28, 9, 19, 11)
obj.title # 'Example'
)
Field Types
Steel provides fields in various categories, with detailed documentation for each:
And custom fields can be added as well: