Preferences

class wpilib.Preferences[source]

Bases: builtins.object

Provides a relatively simple way to save important values to the RoboRIO to access the next time the RoboRIO is booted.

This class loads and saves from a file inside the RoboRIO. The user can not access the file directly, but may modify values at specific fields which will then be saved to the file when save() is called.

This class is thread safe.

This will also interact with networktables.NetworkTable by creating a table called “Preferences” with all the key-value pairs. To save using NetworkTable, simply set the boolean at position ~S A V E~ to true. Also, if the value of any variable is ” in the NetworkTable, then that represents non-existence in the Preferences table.

Creates a preference class that will automatically read the file in a different thread. Any call to its methods will be blocked until the thread is finished reading.

FILE_NAME = '/home/lvuser/wpilib-preferences.ini'
NEW_LINE = '\n'
SAVE_FIELD = '~S A V E~'
TABLE_NAME = 'Preferences'
VALUE_PREFIX = '="'
VALUE_SUFFIX = '"\n'
containsKey(key)[source]

Returns whether or not there is a key with the given name.

Parameters:key – the key
Returns:True if there is a value at the given key
get(key, d=None)[source]

Returns the value at the given key.

Parameters:
  • key – the key
  • d – the return value if the key doesn’t exist (default is None)
Returns:

the value (or d/None if none exists)

getBoolean(key, backup)[source]

Returns the boolean at the given key. If this table does not have a value for that position, then the given backup value will be returned.

Parameters:
  • key – the key
  • backup – the value to return if none exists in the table
Returns:

either the value in the table, or the backup

Raises:

ValueError if value cannot be converted to integer

getFloat(key, backup)[source]

Returns the float at the given key. If this table does not have a value for that position, then the given backup value will be returned.

Parameters:
  • key – the key
  • backup – the value to return if none exists in the table
Returns:

either the value in the table, or the backup

Raises:

ValueError if value cannot be converted to integer

static getInstance()[source]

Returns the preferences instance.

Returns:the preferences instance
getInt(key, backup)[source]

Returns the int at the given key. If this table does not have a value for that position, then the given backup value will be returned.

Parameters:
  • key – the key
  • backup – the value to return if none exists in the table
Returns:

either the value in the table, or the backup

Raises:

ValueError if value cannot be converted to integer

getKeys()[source]
Returns:a list of the keys
getString(key, backup)[source]

Returns the string at the given key. If this table does not have a value for that position, then the given backup value will be returned.

Parameters:
  • key – the key
  • backup – the value to return if none exists in the table
Returns:

either the value in the table, or the backup

has_key(key)[source]

Python style contains key.

keys()[source]

Python style get list of keys.

put(key, value)[source]

Puts the given value into the given key position

Parameters:
  • key – the key
  • value – the value
putBoolean(key, value)[source]

Puts the given float into the preferences table.

The key may not have any whitespace nor an equals sign.

This will NOT save the value to memory between power cycles, to do that you must call save() (which must be used with care) at some point after calling this.

Parameters:
  • key – the key
  • value – the value
putFloat(key, value)[source]

Puts the given float into the preferences table.

The key may not have any whitespace nor an equals sign.

This will NOT save the value to memory between power cycles, to do that you must call save() (which must be used with care) at some point after calling this.

Parameters:
  • key – the key
  • value – the value
putInt(key, value)[source]

Puts the given int into the preferences table.

The key may not have any whitespace nor an equals sign.

This will NOT save the value to memory between power cycles, to do that you must call save() (which must be used with care) at some point after calling this.

Parameters:
  • key – the key
  • value – the value
putString(key, value)[source]

Puts the given string into the preferences table.

The value may not have quotation marks, nor may the key have any whitespace nor an equals sign.

This will NOT save the value to memory between power cycles, to do that you must call save() (which must be used with care) at some point after calling this.

Parameters:
  • key – the key
  • value – the value
read()[source]

The internal method to read from a file. This will be called in its own thread when the preferences singleton is first created.

remove(key)[source]

Remove a preference

Parameters:key – the key
save()[source]

Saves the preferences to a file on the RoboRIO.

This should NOT be called often. Too many writes can damage the RoboRIO’s flash memory. While it is ok to save once or twice a match, this should never be called every run of IterativeRobot.teleopPeriodic().

The actual writing of the file is done in a separate thread. However, any call to a get or put method will wait until the table is fully saved before continuing.