Quick and easy key-value store for your Configuration or State
- EZConfig uses compact, intuitive syntax.
- The source code should be readable and easy to follow: It was written with simplicity in mind, rather than comprehensive features.
- You can use it in place of modules like Configparser, or manually keeping information in YAML/text files.
- Extend it for your own use, or import it as a small library whenever you need to store State or Configuration (or both).
- Keys can be read individually and assigned to variables, or they can be read all-at-once as a dictionary.
- The database is SQLite. One file per EZConfig instance. (Store state and config easily)
- Backup() method saves the current config snapshot.
mycfg = EZConfig('database_file_name.db')
The file extension db is optional, you can use no extension (e.g. 'appconfig') or another extension such as '.sqlite'
mycfg.write('server_name', 'localhost')
The write() method also returns the value:
server = mycfg.write('server_name','localhost')
# Variable 'server' is assigned 'localhost', so you can assign and write the config all at once
server = mycfg.read('server_name')
# returns the value
port = mycfg.read('port_number', value_if_null='error')
# Returns exception if the key is missing/null.
update_interval = mycfg.read('update_interval',60)
# Returns 60 as a default interval if none exists
By default a Null key will return None.
mycfg_dict = {}
mycfg_dict = mycfg.read_all()
mycfg.delete('server_name', 'value_only')
# deletes _only_ the value 'localhost' and sets the value of the key 'server_name' to Null
mycfg.delete('mykey')
# deletes the entire key, removing the row from the database entirely
mycfg.query("SELECT key, value, modified FROM config WHERE key='server_name';")
# returns tuple ('server_name', 'localhost', 'yyyy-mm-dd HH:MM:SS')
mycfg.backup('mycfg_backup.db')
# saves the current config into a sqlite database called 'mycfg_backup.db'
If you need the name of the config database file, the property 'dbname' can be used
mydb = mycfg.dbname
Method | Comments |
---|---|
write(key, value) | Value can be None Returns the value assigned |
read(key, value_if_null=None) | See notes about value_if_null |
read_all(output=None) | This will by default return a dictionary For debug, this can print to the console |
delete(key, delete_level='row') | By default will delete the row from the database |
query(sql_query) | DROP is not supported and will raise an exception |
backup(filename) | Config can be in use; uses sqlite native backup |
The database is only one table (config). You can use the SQLite client tool sqlite3
to use SQL directly on the database:
SELECT * FROM config;
SELECT * FROM sqlite_master; # Shows the schema, including the trigger to update the 'modified' timestamp.
A column in the database called modified
is stored in the database when the key-value pair is created, and updated each time the key is updated.