A PyQt Serialization Library
Project description
PyQtSerializer
PyQtSerializer
is a Python library for serializing input data into a format suitable for secure storage/transmission or supporting non-default supported objects, particularly designed for PyQt
applications.
Installation
You can install PyQtSerializer
via pip:
pip install PyQtSerializer
Features
- Serialize
PyQt
widgets Automatically and data into JSON, Pickle, or YAML format. - Support for encryption of code objects, standard data types, dictionary keys, and more.
- Easy integration with
PyQt
applications.
Usage
Here's a simple example demonstrating how to use PyQtSerializer
within a PyQt
application:
Using Inheritance (preferred)
from PyQtSerializer import PyQtSerializer
# Or from PyQtSerializer.PyQtSerializer import PyQtSerializer If autocompelete wasn't working
class MyWindow(QMainWindow, PyQtSerializer):
def __init__(self):
super().__init__(key=b"3qXo`u>
3Ë1B) ") # Other Options
self.setWindowTitle("PyQt Serializer Example")
self.setGeometry(100, 100, 400, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.layout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.layout.addWidget(self.lineEdit)
self.checkBox = QCheckBox("Remember me")
self.layout.addWidget(self.checkBox)
self.centralWidget.setLayout(self.layout)
self.loadData()
def GetSetting(self):
self.loadData()
self.getValue("Setting",evalValue=False)
def AddNewSetting(self):
self.setValue("Setting",True,serializeValue = False)
def loadData(self):
try:
# Load UI State Into The Window
self.load()
except Exception as e:
print("Error loading data:", e)
def closeEvent(self, event):
# Saves The UI State Into File
self.dump()
event.accept()
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Using PyQtSerializer Class
NOTE
:
When Inheritance Is Not Used You Will Have To Provide And Object Name For Each Widget You Want To Save It's State
from PyQtSerializer import PyQtSerializer
# Or from PyQtSerializer.PyQtSerializer import PyQtSerializer If autocompelete wasn't working
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt Serializer Example")
self.setGeometry(100, 100, 400, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.layout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.layout.addWidget(self.lineEdit)
self.lineEdit.setObjectName("lineEdit")
# When Inheritance Is Not Used You Will Have To Provide And Object Name For Each Widget You Want To Save It's State
self.checkBox = QCheckBox("Remember me")
self.checkBox.setObjectName("checkBox")
self.layout.addWidget(self.checkBox)
self.centralWidget.setLayout(self.layout)
# Initialize The Serializer
self.serializer = PyQtSerializer(b"3qXo`u>
3Ë1B)", target=self)
# Load data if available
self.loadData()
def loadData(self):
try:
self.serializer.load()
print(f"Text: {self.serializer.getValue('text')}")
print(f"checked: {self.serializer.getValue('checked')}")
except Exception as e:
print("Error loading data:", e)
def GetSetting(self):
self.loadData()
self.serializer.getValue("Setting",evalValue=False)
def AddNewSetting(self):
self.serializer.setValue("Setting",True,serializeValue = False)
def closeEvent(self, event):
text = self.lineEdit.text()
checked = self.checkBox.isChecked()
self.serializer.setValue("text", text, serializeValue=True)
self.serializer.setValue("checked", checked, serializeValue=True)
self.serializer.dump()
event.accept()
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Using The Serializer
Class
from PyQtSerializer.Serializer import Serializer
class Class:
def __init__(self, name, age) -> None:
self.name = name
self.age = age
def SayHello(name):
print(f"Hello, {name}")
CLASS = Class("Ahmed", 17)
data = {
"list": [1, 2, "3", {"key", "val"}],
"tuple": (1, 2, 3),
"dict": {"key": "Value"},
"object": CLASS,
"func": SayHello,
}
serializer = Serializer(
data,
saveFormat="YAML",
serializeData=True,
usePickleForClasses=True,
encryptCodeObjects=True,
encryptStdDataTypes=True,
encryptDictNames=True,
initObjects=False,
encryptStrings=True,
encryptNumbers=True,
encryptionDepth=-1,
)
print(f"Encryption Key:
{serializer.encryptionKey}
")
print(f"Serialized Data:
{serializer.data}
")
serializer.Serialize(filePath="_settings.yaml", hex=False)
deserializedData = serializer.Deserialize(
filePath="_settings.yaml",
hex=False,
deserializeData=True,
isEncrypted=True,
classDict=globals(),
setAttrsAfterInit=False,
parseDigits=True,
initObjects=False,
returnGlobalsForPickle=False,
)
print(f"Deserialized Data:
<{deserializedData}>")
deserializedData.get("func")(deserializedData.get("object").name)
You Can Also Use serialize
And deserialize
Functions If You Don't Want To Save To A File
from PyQtSerializer import serialize, deserialize,generateEncryptionKey
class Class:
def __init__(self, name, age) -> None:
self.name = name
self.age = age
def SayHello(name):
print(f"Hello, {name}")
CLASS = Class("Ahmed", 17)
data = {
"list": [1, 2, "3", {"key", "val"}],
"tuple": (1, 2, 3),
"dict": {"key": "Value"},
"object": CLASS,
"func": SayHello,
}
key = generateEncryptionKey()
serializedData = serialize(
data,
usePickleForClasses=True,
encryptCodeObjects=True,
encryptStdDataTypes=True,
encryptDictNames=True,
initObjects=False,
encryptStrings=True,
encryptNumbers=True,
encryptionDepth=-1,
encryptedObjectTypes=[bool],
key=key
)
deserializedData = deserialize(
serializedData,
decryptionKey=key,
isEncrypted=True,
classDict=globals(),
setAttrsAfterInit=False,
parseDigits=True,
initObjects=True,
returnGlobalsForPickle=False
)
print(f"Deserialized Data:
<{deserializedData}>")
deserializedData.get("func")(deserializedData.get("object").name)
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
target | QObject ,optional |
Target Widget To Be Serialized If Class Wasnot Inherited You Will Have To Provide Every QObject A Unique Object Name | None |
data | object |
The data to be serialized. | |
saveFormat | str["JSON", "PICKLE", "YAML"] |
The data to be serialized. | YAML |
usePickleForClasses | bool , optional |
Whether to use pickle serialization for class objects if not class attributes will be serialized instead. | True |
encryptCodeObjects | bool , optional |
Whether to encrypt code objects (e.g., objects, functions). | True |
encryptStdDataTypes | bool , optional |
Whether to encrypt standard data types (e.g., str , int , float ). |
True |
encryptDictNames | bool , optional |
Whether to encrypt dictionary keys and lib key params. | True |
initObjects | bool , optional |
Whether to initialize objects during deSerialization. | True |
encryptStrings | bool , optional |
Whether to encrypt string data. | True |
encryptNumbers | bool , optional |
Whether to encrypt numeric data. | True |
encryptionDepth | int , optional |
Depth of encryption. | -1 |
key | Bytes , optional |
Encryption key (16 Bytes). If not provided, a random key will be generated and returned. | |
serializedData | object |
The serialized data to be deserialized. | |
isEncrypted | bool , optional |
Whether the serialized data is encrypted. | False |
decryptionKey | Bytes , optional |
Decryption key (16 Bytes) if the data is encrypted. | None |
classDict | dict , optional |
Dictionary containing class definitions for initializing objects. | {} |
setAttrsAfterInit | bool , optional |
Whether to set attributes after initializing objects. | False |
parseDigits | bool , optional |
Whether to parse string data that represents numeric values into actual numeric types. | False |
initObjects | bool , optional |
Whether to initialize objects during deserialization. | False |
returnGlobalsForPickle | bool , optional |
Whether to return global scope for pickle deserialization. | False |
Contributing
Contributions are welcomed! Please feel free to submit issues, feature requests, or pull requests on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PyQtSerializer-0.0.1.tar.gz
(15.8 kB
view hashes)
Built Distribution
Close
Hashes for PyQtSerializer-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aef29373a72e0e5f74ae061006baf1f95614d874e18af71a7c6a46e881bd3250 |
|
MD5 | ac1e1aa10fd43f2be4575c75da67026b |
|
BLAKE2b-256 | 31ab6104e772bc76188dd4e456dfce0b85f2c0efeccc6f59fa100e743c820bbb |