Preferences

class wpilib.Preferences[source]

Bases: 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.

TABLE_NAME = 'Preferences'
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
getBoolean(key, backup=None)[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

getFloat(key, backup=None)[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:

TableKeyNotDefinedException if key cannot be found

static getInstance()[source]

Returns the preferences instance.

Returns:the preferences instance
getInt(key, backup=None)[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:

TableKeyNotDefinedException if key cannot be found

getKeys()[source]
Returns:a list of the keys
getString(key, backup=None)[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

keys()[source]

Python style get list of keys.

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
remove(key)[source]

Remove a preference

Parameters:key – the key
valueChangedEx(source, key, value, isNew)[source]