class FileDB

Example

# Import fileDB class
from robot_hat import fileDB

# Create fileDB object with a config file
db = fileDB("./config")

# Set some values
db.set("apple", "10")
db.set("orange", "5")
db.set("banana", "13")

# Read the values
print(db.get("apple"))
print(db.get("orange"))
print(db.get("banana"))

# Read an none existing value with a default value
print(db.get("pineapple", default_value="-1"))

Now you can checkout the config file config in bash.

cat config

API

class robot_hat.fileDB(db: str, mode: str = None, owner: str = None)

Bases: object

A file based database.

A file based database, read and write arguements in the specific file.

__init__(db: str, mode: str = None, owner: str = None)

Init the db_file is a file to save the datas.

Parameters
  • db (str) – the file to save the datas.

  • mode (str) – the mode of the file.

  • owner (str) – the owner of the file.

file_check_create(file_path: str, mode: str = None, owner: str = None)

Check if file is existed, otherwise create one.

Parameters
  • file_path (str) – the file to check

  • mode (str) – the mode of the file.

  • owner (str) – the owner of the file.

get(name, default_value=None)

Get value with data’s name

Parameters
  • name (str) – the name of the arguement

  • default_value (str) – the default value of the arguement

Returns

the value of the arguement

Return type

str

set(name, value)

Set value by with name. Or create one if the arguement does not exist

Parameters
  • name (str) – the name of the arguement

  • value (str) – the value of the arguement