Skip to content

hertzforge.core.run_log

run_log

The run log: what happened on the floor, in order, as it happened.

A run log is kept while a test runs, by a log keeper who is not at the controller. Each thing written down (a level reached, an abort, a clock check, a person arriving, a note) is one entry. An entry is never edited: a mistake is put right by a correction, a later entry that names the one it corrects and carries the new values, so the reader sees the corrected view and the record keeps both.

The file is JSON Lines (.hflog): UTF-8 text, one JSON object per line, each line written, flushed and synced to the disk before the writer returns, so a crash loses at most the entry being written. The first line is the header, with the format's stamp and version. A file cut off in the middle of its last line is read up to that line, and the torn line is named (RunLog.problems); any other line that cannot be read is skipped and named the same way, and the rest is read.

Versions. FORMAT_VERSION is the newest version this build reads, and the one it writes. Each version has its own reader, so a later build reads every earlier version, and a file from a newer version is refused with a message to update. An older .vdlog file is the same format under another stamp (VDLOG_FORMAT), so its files are read by the same readers, and an older log takes further entries in its own file, under its own stamp, rather than splitting a test's record across two files. While Hertzforge is an alpha the format may still change; from the first release that is not an alpha, an older file always opens.

Integrity. The entries of each test are chained by SHA-256: every entry carries the hash of the entry before it in the same test and the hash of its own canonical form, which includes that previous hash. The entries between tests have a chain of their own. RunLog.verify checks every chain and names the first broken link by test and entry. The chain detects accidental damage, a file cut short and casual edits. It is not proof against deliberate alteration: anyone who edits an entry can compute every hash after it again, and the chain then checks. A file cut off at the end of an entry also leaves a valid chain; it is found only against a head hash recorded outside the file (RunLog.head, which a report's data index can carry). Every entry records the computer's login and machine name beside the author's typed initials; those are recorded, not authenticated.

One writer. A log open for writing holds a lock file beside it (<log>.lock); a second writer is refused with a message naming who holds it, and readers open the file read-only (RunLog.read). The lock is the operating system's, so a writer that crashes leaves no lock behind. Each entry can also be copied to a second path (mirror, the lab's share or the data folder); a mirror that cannot be written is reported (RunLog.mirror_problem) and never costs the entry its place in the log itself.

The entry kinds and the fields each may carry are one registry (KINDS). Each kind requires as little as it can: an abort needs only its time, the kind of stop and the author, because the first minute after a stop is the worst moment to be refused. What is still owed of an entry is a derived view, not a refusal.

Classes:

Name Description
EntryKind

One kind of entry: its name in the file, its label in words, the

RunLogError

A run log, or an entry for one, that cannot be read or written

RunLogLocked

The log is already open for writing elsewhere; the message names

FileRef

A file an entry names: its name, its SHA-256 as 64 lower-case hex

Header

A log's first line: its format version, its id, when and where it

Entry

One entry, as stored, or as corrected (RunLog.view).

ReadProblem

A line of the file that was not read as an entry: its number (the

Break

A broken link found by RunLog.verify: the chain ('test N', or

Verification

What RunLog.verify found: every break, in the order of the file,

RunLog

A run log: its header, its entries in the order written, and, when

Functions:

Name Description
file_ref

A FileRef for the file at path, hashed in chunks

chain_name

The name of the chain an entry with test reference test belongs

identity

(login, machine name) of this computer, each '' where the system

canonical

The canonical text of a stored entry (or header) that its hash is

band_hz

A band as (low, high) in Hz, from two frequencies or from text

check_fields

The fields of a new entry of kind, checked against KINDS.

Classes

EntryKind dataclass

EntryKind(name: str, label: str, required: tuple[str, ...] = (), optional: tuple[str, ...] = (), needs_test: bool = False, machine: bool = False)

One kind of entry: its name in the file, its label in words, the fields it needs and the fields it may carry.

needs_test is set for the kinds that mean nothing without a test (a test opened or closed). machine marks the kinds a lab's own machine log keeps: the trips, the setting changes, the clock checks, people in and out, the restarts, and the tests opened and closed that say what was running.

Attributes:

Name Type Description
fields tuple[str, ...]

Every field the kind may carry, the required first.

Attributes
fields property
fields: tuple[str, ...]

Every field the kind may carry, the required first.

RunLogError

Bases: ValueError

A run log, or an entry for one, that cannot be read or written one way; the message names the cause.

RunLogLocked

Bases: RunLogError

The log is already open for writing elsewhere; the message names who holds it.

FileRef dataclass

FileRef(name: str, sha256: str, size: int | None = None, format: str = '', holds: str = '', path: str = '')

A file an entry names: its name, its SHA-256 as 64 lower-case hex digits, and its size in bytes when known.

format, holds and path say, when known, the file's format, what it holds (its run and channels) and where it was when it was hashed, in the words a data index records them (data_index.IndexEntry), so the log's files go into a report's data index as they are. Each is left out of the file when empty.

Methods:

Name Description
to_dict

The file as the log stores it.

Methods:
to_dict
to_dict() -> dict[str, Any]

The file as the log stores it.

Header dataclass

Header(version: int, log_id: str, created: datetime, title: str = '', login: str = '', computer: str = '', format: str = FORMAT)

A log's first line: its format version, its id, when and where it was created, and its title.

Entry dataclass

Entry(id: int, kind: str, event_time: datetime, entered_at: datetime, author: str, login: str = '', computer: str = '', test: int | None = None, axis: str | None = None, run: int | None = None, fields: Mapping[str, Any] = dict(), text: str = '', files: tuple[FileRef, ...] = (), corrects: int | None = None, prev: str = '', hash: str = '', corrected_by: tuple[int, ...] = (), line: int = 0)

One entry, as stored, or as corrected (RunLog.view).

event_time is when the thing happened and entered_at when it was written down, each with its date and its UTC offset. test, axis and run are plain references (a test and run by number, an axis by name), any of them absent. corrects is the id of the entry a correction corrects. prev and hash are the entry's links in its test's chain. In the corrected view, corrected_by lists the corrections applied, in order. line is the entry's line in the file, the header being line 1.

Methods:

Name Description
is_late

Whether the entry was written down more than after after

to_dict

The entry as the log stores it, its hash included.

Attributes:

Name Type Description
label str

The kind in words, as KINDS names it.

chain str

The chain the entry belongs to: 'test N', or 'log' for an

Attributes
label property
label: str

The kind in words, as KINDS names it.

chain property
chain: str

The chain the entry belongs to: 'test N', or 'log' for an entry between tests.

Methods:
is_late
is_late(after: timedelta = LATE_AFTER) -> bool

Whether the entry was written down more than after after the event it records. Both times carry their offsets, so a change of daylight saving time between them counts as the real interval, not the difference of the wall clocks.

to_dict
to_dict() -> dict[str, Any]

The entry as the log stores it, its hash included.

ReadProblem dataclass

ReadProblem(line: int, reason: str)

A line of the file that was not read as an entry: its number (the header is line 1) and why.

Break dataclass

Break(chain: str, test: int | None, entry: int | None, line: int | None, reason: str)

A broken link found by RunLog.verify: the chain ('test N', or 'log' for the entries between tests), the test's number, the entry's id and line where they are known, and what was found.

Verification dataclass

Verification(breaks: tuple[Break, ...], entries: int, chains: int)

What RunLog.verify found: every break, in the order of the file, and how many entries and chains were checked. ok only when nothing is broken; first is the first break.

Attributes:

Name Type Description
ok bool

Whether every chain checked.

first Break | None

The first broken link, or None.

Attributes
ok property
ok: bool

Whether every chain checked.

first property
first: Break | None

The first broken link, or None.

RunLog

RunLog(path: str, header: Header, header_raw: Mapping[str, Any], *, clock: Callable[[], datetime] | None = None, who: tuple[str, str] | None = None)

A run log: its header, its entries in the order written, and, when opened for writing, the way to add to it.

Make one with RunLog.create (a new file), RunLog.open (an existing file, for writing) or RunLog.read (any file, read-only). A log open for writing holds the lock until close; use it in a with block.

Methods:

Name Description
create

A new run log at path, open for writing.

open

The run log at path, open for writing.

read

The run log at path, read-only, without the lock, so it can be

parse

A run log read from data, the bytes of a .hflog or .vdlog,

close

Release the lock and the file. A read-only log has neither;

entry

The entry with id entry_id, as stored.

corrections_of

The corrections of entry entry_id, in the order written.

current

Entry entry_id with its corrections applied in order, each

view

The corrected view: every entry but the corrections, in the

tests

The test numbers the log's entries refer to, in the order

chains

The names of the log's chains, in the order first written:

head

The head of a test's chain (test None for the entries

next_number

The number the next finding of kind ('deviation' or

clock_check_for

The clock check in force at at (an entry, by its corrected

recorder_time

at on the recorders' clock: its time plus the offset of the

verify

Check every chain, and the heads recorded outside the file.

append

Write one entry, synced to the disk before this returns.

correct

Write a correction of an earlier entry, which stays as it was.

Attributes:

Name Type Description
writable bool

Whether entries can be added: opened for writing and not yet

version int

The format version the file is written in.

entries tuple[Entry, ...]

Every entry as stored, corrections included, in the order

problems tuple[ReadProblem, ...]

Every line that was not read as an entry, and why: a torn last

Attributes
writable property
writable: bool

Whether entries can be added: opened for writing and not yet closed.

version property
version: int

The format version the file is written in.

entries property
entries: tuple[Entry, ...]

Every entry as stored, corrections included, in the order written.

problems property
problems: tuple[ReadProblem, ...]

Every line that was not read as an entry, and why: a torn last line, a line that is not JSON, a line that is not an entry.

Methods:
create classmethod
create(path: str | PathLike, *, title: str = '', mirror: str | PathLike | None = None, clock: Callable[[], datetime] | None = None, who: tuple[str, str] | None = None) -> RunLog

A new run log at path, open for writing.

Parameters:

Name Type Description Default
path str or PathLike

Where the log is written. Nothing may be there yet.

required
title str

The log's title (the article or program it is kept for).

''
mirror str or PathLike

A second path every entry is copied to as well.

None
clock callable

Returns the time entries are entered at, with its zone; the computer's own clock unless given.

None
who (str, str)

The (login, machine name) every entry records; read from the computer unless given.

None

Returns:

Type Description
RunLog

The new log, holding only its header.

Raises:

Type Description
RunLogError

When a file is already at path, or the title is not text.

RunLogLocked

When another writer holds the lock beside path.

open classmethod
open(path: str | PathLike, *, mirror: str | PathLike | None = None, clock: Callable[[], datetime] | None = None, who: tuple[str, str] | None = None) -> RunLog

The run log at path, open for writing.

A torn last line (a crash in the middle of an entry) is named in problems and ended, so the next entry starts on a line of its own; nothing in the file is removed. An empty file (a crash while it was being created) is given its header.

Parameters:

Name Type Description Default
path str or PathLike

The log.

required
mirror str or PathLike

A second path every entry is copied to as well; the whole log is copied there first.

None
clock callable

Returns the time entries are entered at, with its zone.

None
who (str, str)

The (login, machine name) every entry records.

None

Returns:

Type Description
RunLog

The log, with every entry read.

Raises:

Type Description
RunLogError

When the file is not a run log, is of a newer format version, or is of an older version (which opens read-only, since a log is only ever written in one version).

RunLogLocked

When another writer holds the lock.

FileNotFoundError

When there is no file at path.

read classmethod
read(path: str | PathLike) -> RunLog

The run log at path, read-only, without the lock, so it can be read while another program writes it.

Parameters:

Name Type Description Default
path str or PathLike

The log.

required

Returns:

Type Description
RunLog

The log, every entry that could be read in it, and the lines that could not in problems.

Raises:

Type Description
RunLogError

When the file is empty, is not a run log, or is of a newer format version than this build reads.

FileNotFoundError

When there is no file at path.

parse classmethod
parse(data: bytes, path: str = '<memory>') -> RunLog

A run log read from data, the bytes of a .hflog or .vdlog, read-only: read without the file, for a log held in memory (a snapshot kept in a project) or bytes read once and examined more than one way.

Parameters:

Name Type Description Default
data bytes

The log's bytes, its header line first.

required
path str

What the log is called in messages, and its path.

'<memory>'

Returns:

Type Description
RunLog

The log, every entry that could be read in it, and the lines that could not in problems.

Raises:

Type Description
RunLogError

When data is empty, is not a run log, or is of a newer format version than this build reads.

close
close() -> None

Release the lock and the file. A read-only log has neither; closing it does nothing.

entry
entry(entry_id: int) -> Entry

The entry with id entry_id, as stored.

corrections_of
corrections_of(entry_id: int) -> tuple[Entry, ...]

The corrections of entry entry_id, in the order written.

current
current(entry_id: int) -> Entry

Entry entry_id with its corrections applied in order, each one's values replacing the entry's and a value of None striking a field; corrected_by lists them. The stored entry is unchanged (entry).

view
view() -> list[Entry]

The corrected view: every entry but the corrections, in the order written, each with its corrections applied (current).

tests
tests() -> list[int]

The test numbers the log's entries refer to, in the order first referred to.

chains
chains() -> list[str]

The names of the log's chains, in the order first written: 'log' for the entries between tests, 'test N' for each test.

head
head(test: int | None = None) -> str

The head of a test's chain (test None for the entries between tests): the hash of its last entry, which the next entry will carry as its previous hash. A chain with no entries yet has its starting hash. A head recorded outside the file (printed at the test's close, in a report's data index) is what verify(heads=...) finds a file cut short against.

next_number
next_number(kind: str) -> str

The number the next finding of kind ('deviation' or 'anomaly') takes, numbered across the whole log: D-1, D-2, A-1.

clock_check_for
clock_check_for(at: Entry | datetime, recorder: str | None = None) -> Entry | None

The clock check in force at at (an entry, by its corrected event time, or a time with its zone): the latest clock check at or before it, of recorder when named. None when no clock check comes before it.

recorder_time
recorder_time(at: Entry | datetime, recorder: str | None = None) -> datetime | None

at on the recorders' clock: its time plus the offset of the clock check in force (clock_check_for), which records the recorders' clock less this computer's, in seconds. None when no clock check comes before it.

verify
verify(heads: Mapping[int | None, str] | None = None, tests: Iterable[int | None] | None = None) -> Verification

Check every chain, and the heads recorded outside the file.

Each entry's previous hash must be the hash of the entry before it in its chain (the chain's starting hash for the first), and its hash must be the hash of its own canonical form. Entry ids must run from 1 without a gap. A head in heads (by test number, None for the entries between tests) must still be in its chain; one that is not means the file was cut short or changed after the head was recorded.

This detects accidental damage, a file cut short (mid-entry on reading; at an entry's end only against heads) and casual edits. It is not proof against deliberate alteration: an edit whose author computes every following hash again is not detected.

Parameters:

Name Type Description Default
heads mapping

Heads recorded outside the file, by test number.

None
tests iterable

Check only these tests' chains (None for the entries between tests), as a file holding one test's entries is checked; the ids then need not run without a gap.

None

Returns:

Type Description
Verification

Every break in the order of the file, and the first.

append
append(kind: str, *, author: str, fields: Mapping[str, Any] | None = None, text: str = '', event_time: datetime | None = None, test: int | None = None, axis: str | None = None, run: int | None = None, files: Iterable[str | PathLike | FileRef] = ()) -> Entry

Write one entry, synced to the disk before this returns.

A deviation or anomaly opened without a number takes the next of its kind across the log (next_number); a number given must be of its kind and not already taken in the log. A correction is written with correct, not here.

Parameters:

Name Type Description Default
kind str

One of KINDS.

required
author str

The author's typed initials.

required
fields mapping

The kind's fields (check_fields); a field set to None or blank is left out.

None
text str

Free text.

''
event_time datetime

When the thing happened, with its zone; now, if not given.

None
test int

The test and run it belongs to, by number from 1.

None
run int

The test and run it belongs to, by number from 1.

None
axis str

The axis it belongs to, by name.

None
files iterable

Files it names, each a path (hashed now) or a FileRef.

()

Returns:

Type Description
Entry

The entry as written, with its id and hashes.

Raises:

Type Description
RunLogError

For a log not open for writing, and for anything the entry cannot hold one way, the message naming the field.

correct
correct(target: int | Entry, *, author: str, fields: Mapping[str, Any] | None = None, text: str = '', event_time: datetime | None = None) -> Entry

Write a correction of an earlier entry, which stays as it was.

Parameters:

Name Type Description Default
target int or Entry

The entry corrected, by id. A correction is not corrected itself; correct the original entry again.

required
author str

The author's typed initials.

required
fields mapping

The new values: any of the corrected entry's fields (None strikes an optional one), and any of CORRECTABLE (its event time, free text, test, axis and run).

None
text str

Why it is corrected.

''
event_time datetime

When the correction was made; now, if not given.

None

Returns:

Type Description
Entry

The correction as written. It belongs to the corrected entry's test, so it is in that test's chain.

Raises:

Type Description
RunLogError

When the entry is not in the log or is a correction, when no value is given, when a value is one the entry cannot hold, when a required field would be struck, or when a finding's number would change.

Functions:

file_ref

file_ref(path: str | PathLike, name: str | None = None) -> FileRef

A FileRef for the file at path, hashed in chunks (data_index.digest); name is the file's own name unless given.

chain_name

chain_name(test: Any) -> str

The name of the chain an entry with test reference test belongs to: 'test N', or 'log' for none.

identity

identity() -> tuple[str, str]

(login, machine name) of this computer, each '' where the system does not say.

canonical

canonical(stored: Mapping[str, Any]) -> str

The canonical text of a stored entry (or header) that its hash is taken over: keys sorted, no spaces, UTF-8 characters as themselves, and the entry's own hash left out. The previous hash is a key of the entry, so it is part of what is hashed.

band_hz

band_hz(value: Any, what: str = 'a band') -> tuple[float, float]

A band as (low, high) in Hz, from two frequencies or from text such as '180-220 Hz', '180 to 220', '1.2-2 kHz' or '200 Hz' (one frequency, both edges).

Refused with RunLogError naming what: text that is not one of those forms, and anything findings.band_of refuses (not two finite, non-negative frequencies, low edge above high).

check_fields

check_fields(kind: str, fields: Mapping[str, Any] | None) -> dict[str, Any]

The fields of a new entry of kind, checked against KINDS.

A field set to None or to blank text is left out, so a form can pass every box it has. Refused with RunLogError, each naming the kind and the field: a kind that is not in KINDS, a field the kind does not carry, a value of the wrong type or not finite, a required field missing, an abort's kind of stop not one of STOPS, and a sweep's span that band_hz does not read as a band.