DelphiLearn PythonPythonPython GUIRAD Studio

Learn To Build A High-Performance Data Serialization Pipeline With A Python Windows GUI

blogbanner3-7

Data serialization is the process of converting structured data to a format that allows us to share or store the data in a form that allows recovery of its original structure. In some cases, the secondary intention of data serialization is to minimize the data’s size which then reduces disk space or bandwidth requirements.

This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Serialization in Python GUI apps and gets the output.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0_rundemo1
Open Demo01.dproj

1. Flat vs Nested Data

Before beginning to serialize data, it is important to identify or decide how the data should be structured during data serialization – flat or nested. The differences in the two styles are shown in the below examples.

Flat style:

Nested style:

 

2. Serializing Text

2.1. Simple File (Flat Data)

If the data that we want to be serialized is located in a file and contains flat data, Python offers repr to serialize data:

repr

The repr method in Python takes a single object parameter and returns a printable representation of the input:

Output in Python GUI:

2_1_1-3447989
repr in Python GUI.

 

2.2. CSV File (Flat Data)

The CSV module in Python implements classes to read and write tabular data in CSV format.

Simple example for reading:

Output in Python GUI:

2_2_1-2661022
Reading CSV File in Python GUI.

Simple example for writing:

Output in Python GUI:

2_2_2-1932276
Writing CSV File in Python GUI.

There is no expected output in the Python GUI, but, let’s see the result in Ms. Excel:

2_2_3-4853772
Writing CSV File using Python GUI.

 

2.3. JSON File (Nested Data)

Python provides built-in JSON libraries to encode and decode JSON.

There are two basic formats for JSON data. Either in a string or the object datastructure. The object datastructure, in Python, consists of lists and dictionaries nested inside each other. The object datastructure allows one to use python methods (for lists and dictionaries) to add, list, search and remove elements from the datastructure. The String format is mainly used to pass the data into another program or load into a datastructure.

To load JSON back to a data structure, use the “loads” method. This method takes a string and turns it back into the json object datastructure:

To encode a data structure to JSON, use the “dumps” method. This method takes an object and returns a String:

Output:

2_3-7752298
Encode JSON in Python GUI.

Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).

We can use it exactly the same way:

Output:

2_3_2-2714931
Python Pickle in Python GUI.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

close
Related posts
DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Scientific Computing With The NumPy Library In A Delphi Windows App

DelphiPythonPython GUIWindows

Learn To Work With Real-World Graphics Using The Python Matplotlib Library In A Delphi Windows App

DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Easily Processing Images With Pillow Library In A Delphi Windows App

DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Computer Vision Tasks With Powerful OpenCV Library In A Delphi Windows App

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish