pykit package

Subpackages

Submodules

pykit.autolog module

class pykit.autolog.AutoLogInputManager[source]

Bases: object

A manager class for handling automatic input loading of dataclass fields.

classmethod getInputs() List[Any][source]

Returns the list of registered classes for input loading.

Returns:

A list of registered classes.

logged_classes: List[Any] = []
classmethod register_class(class_to_register: Any)[source]

Registers a class for automatic input loading.

Parameters:

class_to_register – The class type to register.

class pykit.autolog.AutoLogOutputManager[source]

Bases: object

A manager class for handling automatic logging of output members (fields/methods).

logged_members: Dict[Type, List[Dict[str, Any]]] = {}
classmethod publish(instance: Any, table: LogTable)[source]

Publishes the values of all registered members of an instance to a LogTable.

Parameters:
  • instance – The instance whose members are to be published.

  • table – The LogTable to publish data to.

classmethod publish_all(table: LogTable, root_instance=None)[source]

Publishes all registered members of all registered class instances.

This method scans for all instances of classes registered with autologgable_output and publishes their decorated members to the provided LogTable. It also handles caching of root instances to avoid redundant garbage collection scans.

Parameters:
  • table – The LogTable to publish data to.

  • root_instance – An optional list of root instances to start publishing from. If None, it scans for all instances of registered classes.

classmethod register_member(class_type: Type, member_name: str, is_method: bool, log_type: LoggableType | None, key: str = '', custom_type: str = '', unit: str | None = None)[source]

Registers a member (field or method) of a class for automatic output logging.

Parameters:
  • class_type – The class to which the member belongs.

  • member_name – The name of the member (field or method).

  • is_method – True if the member is a method, False otherwise.

  • log_type – The LogValue.LoggableType to log the member as.

  • key – The key to use for logging. Defaults to the member name.

  • custom_type – A custom type string for the log entry.

  • unit – The unit string for the log entry.

root_cache: List[Any] = []
pykit.autolog.autolog(cls=None, /)[source]

A class decorator that adds ‘toLog’ and ‘fromLog’ methods to a dataclass for automatic logging.

The ‘toLog’ method serializes the dataclass fields to a LogTable. The ‘fromLog’ method deserializes the data from a LogTable into the dataclass fields.

This decorator is designed to be used with dataclasses and supports nested dataclasses decorated with @autolog.

Parameters:

cls – The class to decorate.

Returns:

The decorated class or a wrapper function.

pykit.autolog.autolog_output(key: str, log_type: LoggableType | None = None, custom_type: str = '', unit: str | None = None)[source]

A decorator for methods or fields in a class to automatically log their output.

Usage:

@autologgable_output class MyComponent:

my_value = 5

@autolog_output(“MyValue”) def get_my_value(self):

return self.my_value

Parameters:
  • key – The key to use for logging the member’s value.

  • log_type – The LogValue.LoggableType to log the value as. If None, it’s inferred.

  • custom_type – A custom type string for the log entry.

  • unit – The unit string for the log entry.

Returns:

A decorator function.

pykit.autolog.autologgable_output(cls)[source]

A class decorator that scans for methods/fields decorated with @autolog_output and registers them with AutoLogOutputManager.

Parameters:

cls – The class to decorate.

Returns:

The decorated class.

pykit.logdatareciever module

class pykit.logdatareciever.LogDataReciever[source]

Bases: object

An abstract base class for classes that receive and process log data.

end()[source]

Called when the logging process ends. Can be used for cleanup.

putTable(table: LogTable)[source]

Processes a LogTable instance. This method is called for each log entry.

Parameters:

table – The LogTable containing the log data for a specific timestamp.

start()[source]

Called when the logging process starts. Can be used for initialization.

timestampKey: str = '/Timestamp'

pykit.loggedrobot module

class pykit.loggedrobot.LoggedRobot[source]

Bases: IterativeRobotBase

A robot base class that provides logging and replay functionality. This class extends IterativeRobotBase and integrates with the Logger to automatically handle the logging of robot data and periodic loops.

Constructor for the LoggedRobot. Initializes the robot, sets up the logger, and creates I/O objects.

default_period = 0.02
endCompetition() None[source]

Called at the end of the competition to clean up resources.

printOverrunMessage()[source]

Prints a message when the main loop overruns.

startCompetition() None[source]

The main loop of the robot. Handles timing, logging, and calling the periodic functions. This method replaces the standard IterativeRobotBase.startCompetition to inject logging and precise timing control.

pykit.logger module

class pykit.logger.Logger[source]

Bases: object

Manages the logging and replay of data for the robot.

classmethod addDataReciever(reciever: LogDataReciever)[source]

Adds a data receiver to the logger.

Parameters:

reciever – The LogDataReciever to add.

checkConsole: bool = True
cycleCount: int = 0
dashboardInputs: list[LoggedNetworkInput] = []
dataRecievers: list[LogDataReciever] = []
classmethod end()[source]

Stops the logger and all data receivers, and performs necessary cleanup.

entry: LogTable = <pykit.logtable.LogTable object>
classmethod getTimestamp() int[source]

Returns the current timestamp for logging. In replay mode, it gets the timestamp from the log entry. In normal mode, it gets the current FPGA timestamp.

Returns:

The current timestamp in microseconds.

classmethod isReplay() bool[source]

Checks if the logger is currently in replay mode.

Returns:

True if in replay mode, False otherwise.

metadata: dict[str, str] = {}
outputTable: LogTable = <pykit.logtable.LogTable object>
classmethod periodicAfterUser(userCodeLength: int, periodicBeforeLength: int)[source]

Called periodically after the user’s robot code. This method finalizes the log entry for the current cycle by recording outputs, performance data, and then sends the log table to all registered receivers.

Parameters:
  • userCodeLength – The execution time of the user’s code in microseconds.

  • periodicBeforeLength – The execution time of the periodicBeforeUser method in microseconds.

classmethod periodicBeforeUser()[source]

Called periodically before the user’s robot code. This method updates the log table with new data, either from the replay source or from the live robot hardware.

classmethod processInputs(prefix: str, inputs)[source]

Processes an I/O object, either by logging its state or by updating it from the log.

In normal mode, it calls ‘toLog’ on the inputs object to record its state. In replay mode, it calls ‘fromLog’ on the inputs object to update its state from the log.

Parameters:
  • prefix – The prefix for the log entries.

  • inputs – The I/O object to process.

classmethod recordMetadata(key: str, value: str)[source]

Records metadata information. This is only active when not in replay mode.

Parameters:
  • key – The key for the metadata.

  • value – The metadata value.

classmethod recordOutput(key: str, value: Any, unit: str | None = None)[source]

Records an output value to the log table. This is only active when not in replay mode.

Parameters:
  • key – The key under which to record the value.

  • value – The value to record.

classmethod registerDashboardInput(dashboardInput: LoggedNetworkInput)[source]

Registers a dashboard input for periodic updates.

Parameters:

dashboardInput – The LoggedNetworkInput to register.

replaySource: LogReplaySource | None = None
running: bool = False
classmethod setReplaySource(replaySource: LogReplaySource)[source]

Sets the replay source for the logger.

Parameters:

replaySource – The LogReplaySource to use for replaying data.

classmethod start()[source]

Starts the logger. This initializes logging or replay and sets up the necessary tables.

classmethod startReciever()[source]

Starts all registered data receivers.

pykit.logreplaysource module

class pykit.logreplaysource.LogReplaySource[source]

Bases: object

An abstract base class for providing log data for replay.

end()[source]

Cleans up resources used by the replay source. This method can be overridden by subclasses for cleanup tasks.

start()[source]

Initializes the replay source. This method should be implemented by subclasses to prepare for data replay.

timestampKey: str = '/Timestamp'
updateTable(_table: LogTable) bool[source]

Updates the provided LogTable with the next set of data from the replay source.

Parameters:

_table – The LogTable to populate with new data.

Returns:

True if the table was successfully updated, False if the end of the replay is reached.

pykit.logtable module

class pykit.logtable.LogTable(timestamp: int, prefix='/')[source]

Bases: object

Represents a table of loggable values for a single timestamp. It stores data as key-value pairs where keys are strings and values are LogValue objects.

Initializes a new LogTable.

Parameters:
  • timestamp – The timestamp for the log entries in this table, in microseconds.

  • prefix – The prefix for all keys in this table.

addStructSchema(struct: Any, seen: Set[str])[source]

Adds the schema for a WPILib struct and its nested structs to the log table.

Parameters:
  • struct – The struct instance.

  • seen – A set of already processed type strings to avoid recursion loops.

addStructSchemaNest(structname: str, schema: str)[source]

Adds the schema for a nested WPILib struct to the log table.

Parameters:
  • structname – The name of the struct.

  • schema – The schema string of the struct.

static clone(source: LogTable)[source]

Creates a shallow copy of a LogTable.

Parameters:

source – The LogTable to clone.

Returns:

A new LogTable instance with the same data.

data: dict[str, LogValue]
depth: int
get(key: str, defaultValue: Any) Any[source]

Gets a value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found.

Returns:

The retrieved value or the default value.

getAll(subtableOnly: bool = False) dict[str, LogValue][source]

Returns all log values in the table.

Parameters:

subtableOnly – If True, returns only the entries within the current subtable’s prefix.

Returns:

A dictionary of all log entries.

getBoolean(key: str, defaultValue: bool) bool[source]

Gets a boolean value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getBooleanArray(key: str, defaultValue: list[bool]) list[bool][source]

Gets a boolean array value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getDouble(key: str, defaultValue: float) float[source]

Gets a double value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getDoubleArray(key: str, defaultValue: list[float]) list[float][source]

Gets a double array value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getFloat(key: str, defaultValue: float) float[source]

Gets a float value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getFloatArray(key: str, defaultValue: list[float]) list[float][source]

Gets a float array value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getInteger(key: str, defaultValue: int) int[source]

Gets an integer value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getIntegerArray(key: str, defaultValue: list[int]) list[int][source]

Gets an integer array value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getRaw(key: str, defaultValue: bytes) bytes[source]

Gets a raw (bytes) value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getString(key: str, defaultValue: str) str[source]

Gets a string value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getStringArray(key: str, defaultValue: list[str]) list[str][source]

Gets a string array value from the log table.

Parameters:
  • key – The key of the value to retrieve.

  • defaultValue – The value to return if the key is not found or the type is incorrect.

Returns:

The retrieved value or the default value.

getSubTable(subtablePrefix: str) LogTable[source]

Returns a new LogTable instance representing a subtable of the current one. The new table shares the same underlying data but has an extended prefix.

Parameters:

subtablePrefix – The prefix for the subtable.

Returns:

A new LogTable for the specified subtable.

getTimestamp() int[source]

Returns the timestamp of the log table.

Returns:

The timestamp in microseconds.

prefix: str
put(key: str, value: Any, typeStr: str = '', unit: str | None = None)[source]

Puts a value into the log table, automatically handling WPILib structs and arrays. The value is wrapped in a LogValue object.

Parameters:
  • key – The key for the log entry.

  • value – The value to be logged.

  • typeStr – An optional custom type string.

putValue(key: str, log_value: LogValue)[source]

Puts a LogValue object into the log table.

Parameters:
  • key – The key for the log entry.

  • log_value – The LogValue object to be stored.

setTimestamp(timestamp: int) None[source]

Sets the timestamp of the log table.

Parameters:

timestamp – The new timestamp in microseconds.

writeAllowed(key: str, logType: LoggableType, customType: str) bool[source]

Checks if a write operation is allowed for a given key and type. Prevents changing the type of an existing log entry.

Parameters:
  • key – The key of the log entry.

  • logType – The LoggableType of the new value.

  • customType – The custom type string of the new value.

Returns:

True if writing is allowed, False otherwise.

pykit.logvalue module

class pykit.logvalue.LogValue(value: Any, typeStr: str = '', unit: str | None = None)[source]

Bases: object

Represents a value in the log table, encapsulating its type, a custom type string, and the value itself.

Initializes a LogValue, inferring the loggable type from the value’s Python type.

Parameters:
  • value – The value to be logged.

  • typeStr – An optional custom type string.

Raises:

TypeError – If the value type is not supported.

class LoggableType(*values)[source]

Bases: Enum

Enum for the different types of loggable values.

Boolean = 2
BooleanArray = 7
Double = 5
DoubleArray = 10
Float = 4
FloatArray = 9
Integer = 3
IntegerArray = 8
Raw = 1
String = 6
StringArray = 11
static fromNT4Type(typeStr: str) LoggableType[source]

Converts an NT4 type string to a LoggableType.

Parameters:

typeStr – The NT4 type string.

Returns:

The corresponding LoggableType, or Raw if not found.

static fromWPILOGType(typeStr: str) LoggableType[source]

Converts a WPILOG type string to a LoggableType.

Parameters:

typeStr – The WPILOG type string.

Returns:

The corresponding LoggableType, or Raw if not found.

getNT4Type() str[source]

Returns the NT4 type string corresponding to this loggable type.

Returns:

The NT4 type string.

getWPILOGType() str[source]

Returns the WPILOG type string corresponding to this loggable type.

Returns:

The WPILOG type string.

custom_type: str
getNT4Type() str[source]

Gets the NT4 type string for this value.

Returns:

The custom type string if available, otherwise the default NT4 type.

getWPILOGType() str[source]

Gets the WPILOG type string for this value.

Returns:

The custom type string if available, otherwise the default WPILOG type.

log_type: LoggableType
unit: str | None = None
value: Any
static withType(log_type: LoggableType, data: Any, typeStr: str = '', unit: str | None = None) LogValue[source]

Creates a LogValue with a specified loggable type.

Parameters:
  • log_type – The LoggableType to assign.

  • data – The value.

  • typeStr – An optional custom type string.

Returns:

A new LogValue instance.