This article is work in progress.

The reader will learn howto use dictionaries and JSONin the different areas of the flecsimo project and how to benefit from them.

Areas of use

In the flecsimo project, dictionary data structures and their representation as a JSON structure are the common data format of mostly all application areas, subsystems and network communication:

  • All mqtt messsage payloads are formatted as JSON strings (see KB-8 How to create MQTT-message data).
  • The base/msg classes provide methods to create JSON representations of the instance member variables (as mqtt payloads).
  • The standard sqlite.Row row factory wraps SQL result sets so that they can be accessed like a dictionary (and therefore can be easily handled as JSON structure).

Some basics

JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format inspired by JavaScript object literal syntax (see docs.python.org). The Python standard library Lib/json  provide functions to (de-) serialize Python objects. Although the capabilities for (de-) serialization of general Python objects like classes is limited (and therefore the marshal  or pickle  modules should be used for this purpose), it's perfect to (de-) serialize (i. e. "convert") data structures into strings and backwards. The string representation of a JSON object could look like this: {'temp': 22.2, 'room': 'kitchen'}.

Dictionary

The dictionary data type is often described as an "associative array". One way to define a dictionary in Python is to declare it like: d = {'temp': 22.2, 'room': 'kitchen'}. It allows to store different data types in hierarchical structures (as dictionaries which are elements of a dictionariy) and provide a simple index-like access to dictionary elements: d['temp'] one will return the temperature value and d['room'] will return the string kitchen. Dictionaries provide member functions to append , update and delete dictionary elements, and many more. Since Python version 3.7 standard dict objects preserve order as a language feature (and this was the case also in CPython since version 3.5). See docs.python.org for more details.

Dictionaries and JSON structures

Following the basic properties of both data types, every string representation of a JSON object could transformed into a dictionary, and every dictionary could be easily and without any losses serialized as a JSON object. The following step-by-step guides will show the main applications of this in the flecsimo context.

Step-by-step guide

Convert string to JSON and back





StepDescriptionRef









Convert JSON to dictionaries and back

StepDescriptionRef










Convert dictionaries to string and back

StepDescriptionRef









Access and modify values of dictionaries

StepDescriptionRef











Use dictionaries as value sets in SQLite


# Step 1:
import datetime
import sqlite3

# Step 2:
at = datetime.datetime.utcnow()

# Step 3: 
order_dict = {'id': 1000021, 'material': 'FXF-1100', 'variant'=None, 'qty':2.0, 'unit': 'PC'} 
order_dict['at'] = at

# Step 4:
insert_order = """
    INSERT INTO "main"."order" ("id", "material", "variant", "qty", "unit", "at") 
    VALUES (:id, :material, :variant, :qty, :unit, :at)"""

# Step 5:
conn = sqlite3.connect(site_db)
conn.execute('PRAGMA busy_timeout = 100;')

# Step 6:
with conn:
    conn.execute(insert_order, order_dict)

# Step 7:
conn.close




StepDescriptionRef
1The required imports. datetime is used since we want to generate an UTC-ISO format timestamp...
2... which happens in this step.
3

The essential thing is, that we "have" a dictionary using the placeholders of the SQL VALUES part as keys in the data structure. It is a flecsimo best practice to keep columns names, placeholder and dictionary keys in sync.

Appending the current timestamp by just setting the key is shown in the line number 10.


4Prepare the SQL statement. Again note: use the keys of the dictionary as placeholders in the VALUES part of the SQL. This will map automatically the dictionary values to the table columns
5This is fairly standard. The PRAGMA setting in line 19 is not required in general but recommended in most setting of flecsimo.
6

Using a context manager to execute the SQL statement on connection level.

Note:

  • The only arguments one has provide is the prepared SQL statement and the appropriate dictionary,
  • Using a connection context manager implies an auto-commit. Without context manager: don't forget to commit the operations.

7And finally: close the connection