PyQtSerializer


NamePyQtSerializer JSON
Version 0.0.1 PyPI version JSON
download
home_page
SummaryA PyQt Serialization Library
upload_time2024-02-23 17:37:49
maintainer
docs_urlNone
authorAhmed Essam (https://github.com/Were-Logan-0110)
requires_python
license
keywords python encryption serialize serialization pickle pyqt qtsave
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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**)
```python
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"3qĠXo`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**
```py
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"3qĠXo`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
```py
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
```py
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**](https://github.com/Were-Logan-0110/PyQtSerializer).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "PyQtSerializer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,encryption,serialize,serialization,pickle,pyqt,qtsave",
    "author": "Ahmed Essam (https://github.com/Were-Logan-0110)",
    "author_email": "<headnuts92@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/19/ee/0cf9a40e660a64eb61f14e9063ace79aaa8b8421dce3743c2c0035e954a3/PyQtSerializer-0.0.1.tar.gz",
    "platform": null,
    "description": "\r\n# PyQtSerializer\r\n\r\n#### `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.\r\n\r\n## Installation\r\n\r\nYou can install `PyQtSerializer` via pip:\r\n\r\n> **`pip install PyQtSerializer`**\r\n\r\n## Features\r\n\r\n* Serialize `PyQt` widgets Automatically and data into JSON, Pickle, or YAML format.\r\n* Support for encryption of code objects, standard data types, dictionary keys, and more.\r\n* Easy integration with `PyQt` applications.\r\n## Usage\r\n\r\nHere's a simple example demonstrating how to use `PyQtSerializer` within a `PyQt` application:\r\n## Using Inheritance (**preferred**)\r\n```python\r\nfrom PyQtSerializer import PyQtSerializer\r\n# Or from PyQtSerializer.PyQtSerializer import PyQtSerializer If autocompelete wasn't working\r\nclass MyWindow(QMainWindow, PyQtSerializer):\r\n    def __init__(self):\r\n        super().__init__(key=b\"3q\u0083\u0086Xo`u>\r\n3\u00cb1B) \u00ad\") # Other Options\r\n        self.setWindowTitle(\"PyQt Serializer Example\")\r\n        self.setGeometry(100, 100, 400, 150)\r\n        self.centralWidget = QWidget()\r\n        self.setCentralWidget(self.centralWidget)\r\n        self.layout = QVBoxLayout()\r\n        self.lineEdit = QLineEdit()\r\n        self.layout.addWidget(self.lineEdit)\r\n\r\n        self.checkBox = QCheckBox(\"Remember me\")\r\n        self.layout.addWidget(self.checkBox)\r\n        self.centralWidget.setLayout(self.layout)\r\n\r\n        self.loadData()\r\n    def GetSetting(self):\r\n        self.loadData()\r\n        self.getValue(\"Setting\",evalValue=False)\r\n    def AddNewSetting(self):\r\n        self.setValue(\"Setting\",True,serializeValue = False)\r\n    def loadData(self):\r\n        try:\r\n            # Load UI State Into The Window\r\n            self.load()\r\n        except Exception as e:\r\n            print(\"Error loading data:\", e)\r\n\r\n    def closeEvent(self, event):\r\n        # Saves The UI State Into File\r\n        self.dump()\r\n        event.accept()\r\napp = QApplication(sys.argv)\r\nwindow = MyWindow()\r\nwindow.show()\r\nsys.exit(app.exec_())\r\n\r\n```\r\n# Using PyQtSerializer Class\r\n## `NOTE`: \r\n**When Inheritance Is Not Used You Will Have To Provide And Object Name For Each Widget You Want To Save It's State**\r\n```py\r\nfrom PyQtSerializer import PyQtSerializer\r\n# Or from PyQtSerializer.PyQtSerializer import PyQtSerializer If autocompelete wasn't working\r\nclass MyWindow(QMainWindow):\r\n    def __init__(self):\r\n        super().__init__()\r\n        self.setWindowTitle(\"PyQt Serializer Example\")\r\n        self.setGeometry(100, 100, 400, 150)\r\n        self.centralWidget = QWidget()\r\n        self.setCentralWidget(self.centralWidget)\r\n        self.layout = QVBoxLayout()\r\n        self.lineEdit = QLineEdit()\r\n        self.layout.addWidget(self.lineEdit)\r\n        self.lineEdit.setObjectName(\"lineEdit\")\r\n# When Inheritance Is Not Used You Will Have To Provide And Object Name For Each Widget You Want To Save It's State\r\n        self.checkBox = QCheckBox(\"Remember me\")\r\n        self.checkBox.setObjectName(\"checkBox\")\r\n        self.layout.addWidget(self.checkBox)\r\n        self.centralWidget.setLayout(self.layout)\r\n        # Initialize The Serializer\r\n        self.serializer = PyQtSerializer(b\"3q\u0083\u0086Xo`u>\r\n3\u00cb1B)\u00ad\", target=self)\r\n        # Load data if available\r\n        self.loadData()\r\n\r\n    def loadData(self):\r\n        try:\r\n            self.serializer.load()\r\n            print(f\"Text: {self.serializer.getValue('text')}\")\r\n            print(f\"checked: {self.serializer.getValue('checked')}\")\r\n        except Exception as e:\r\n            print(\"Error loading data:\", e)\r\n    def GetSetting(self):\r\n        self.loadData()\r\n        self.serializer.getValue(\"Setting\",evalValue=False)\r\n    def AddNewSetting(self):\r\n        self.serializer.setValue(\"Setting\",True,serializeValue = False)\r\n    def closeEvent(self, event):\r\n        text = self.lineEdit.text()\r\n        checked = self.checkBox.isChecked()\r\n        self.serializer.setValue(\"text\", text, serializeValue=True)\r\n        self.serializer.setValue(\"checked\", checked, serializeValue=True)\r\n        self.serializer.dump()\r\n        event.accept()\r\n\r\napp = QApplication(sys.argv)\r\nwindow = MyWindow()\r\nwindow.show()\r\nsys.exit(app.exec_())\r\n```\r\n## Using The `Serializer` Class\r\n```py\r\nfrom PyQtSerializer.Serializer import Serializer\r\nclass Class:\r\n    def __init__(self, name, age) -> None:\r\n        self.name = name\r\n        self.age = age\r\n\r\ndef SayHello(name):\r\n    print(f\"Hello, {name}\")\r\n\r\nCLASS = Class(\"Ahmed\", 17)\r\ndata = {\r\n    \"list\": [1, 2, \"3\", {\"key\", \"val\"}],\r\n    \"tuple\": (1, 2, 3),\r\n    \"dict\": {\"key\": \"Value\"},\r\n    \"object\": CLASS,\r\n    \"func\": SayHello,\r\n}\r\nserializer = Serializer(\r\n    data,\r\n    saveFormat=\"YAML\",\r\n    serializeData=True,\r\n    usePickleForClasses=True,\r\n    encryptCodeObjects=True,\r\n    encryptStdDataTypes=True,\r\n    encryptDictNames=True,\r\n    initObjects=False,\r\n    encryptStrings=True,\r\n    encryptNumbers=True,\r\n    encryptionDepth=-1,\r\n)\r\nprint(f\"Encryption Key: \r\n\t{serializer.encryptionKey}\r\n\r\n\")\r\nprint(f\"Serialized Data: \r\n\t{serializer.data}\r\n\r\n\")\r\nserializer.Serialize(filePath=\"_settings.yaml\", hex=False)\r\ndeserializedData = serializer.Deserialize(\r\n    filePath=\"_settings.yaml\",\r\n    hex=False,\r\n    deserializeData=True,\r\n    isEncrypted=True,\r\n    classDict=globals(),\r\n    setAttrsAfterInit=False,\r\n    parseDigits=True,\r\n    initObjects=False,\r\n    returnGlobalsForPickle=False,\r\n)\r\nprint(f\"Deserialized Data: \r\n\t<{deserializedData}>\")\r\ndeserializedData.get(\"func\")(deserializedData.get(\"object\").name)\r\n```\r\n## You Can Also Use `serialize` And `deserialize` Functions If You Don't Want To Save To A File\r\n```py\r\nfrom PyQtSerializer import serialize, deserialize,generateEncryptionKey\r\n\r\n\r\nclass Class:\r\n    def __init__(self, name, age) -> None:\r\n        self.name = name\r\n        self.age = age\r\n\r\n\r\ndef SayHello(name):\r\n    print(f\"Hello, {name}\")\r\n\r\n\r\nCLASS = Class(\"Ahmed\", 17)\r\ndata = {\r\n    \"list\": [1, 2, \"3\", {\"key\", \"val\"}],\r\n    \"tuple\": (1, 2, 3),\r\n    \"dict\": {\"key\": \"Value\"},\r\n    \"object\": CLASS,\r\n    \"func\": SayHello,\r\n}\r\nkey = generateEncryptionKey()\r\nserializedData = serialize(\r\n    data,\r\n    usePickleForClasses=True,\r\n    encryptCodeObjects=True,\r\n    encryptStdDataTypes=True,\r\n    encryptDictNames=True,\r\n    initObjects=False,\r\n    encryptStrings=True,\r\n    encryptNumbers=True,\r\n    encryptionDepth=-1,\r\n    encryptedObjectTypes=[bool],\r\n    key=key\r\n)\r\ndeserializedData = deserialize(\r\n    serializedData,\r\n    decryptionKey=key,\r\n    isEncrypted=True,\r\n    classDict=globals(),\r\n    setAttrsAfterInit=False,\r\n    parseDigits=True,\r\n    initObjects=True,\r\n    returnGlobalsForPickle=False\r\n)\r\nprint(f\"Deserialized Data: \r\n\t<{deserializedData}>\")\r\ndeserializedData.get(\"func\")(deserializedData.get(\"object\").name)\r\n```\r\n## **Parameters**\r\n\r\n|Parameter|Type|Description|Default|\r\n|---|---|---|---|\r\n|target|`QObject`,`optional`|Target Widget To Be Serialized If Class Wasnot Inherited You Will Have To Provide Every QObject A Unique Object Name|`None`|\r\n|data|`object`|The data to be serialized.||\r\n|saveFormat|`str[\"JSON\", \"PICKLE\", \"YAML\"]`|The data to be serialized.|`YAML`|\r\n|usePickleForClasses|`bool`, `optional`|Whether to use pickle serialization for class objects if not class attributes will be serialized instead.|`True`|\r\n|encryptCodeObjects|`bool`, `optional`|Whether to encrypt code objects (e.g., objects, functions).|`True`|\r\n|encryptStdDataTypes|`bool`, `optional`|Whether to encrypt standard data types (e.g., `str`, `int`, `float`).|`True`|\r\n|encryptDictNames|`bool`, `optional`|Whether to encrypt dictionary keys and lib key params.|`True`|\r\n|initObjects|`bool`, `optional`|Whether to initialize objects during deSerialization.|`True`|\r\n|encryptStrings|`bool`, `optional`|Whether to encrypt string data.|`True`|\r\n|encryptNumbers|`bool`, `optional`|Whether to encrypt numeric data.|`True`|\r\n|encryptionDepth|`int`, `optional`|Depth of encryption.|`-1`|\r\n|key|`Bytes`, `optional`|Encryption key (16 Bytes). If not provided, a random key will be generated and returned.||\r\n|serializedData|`object`|The serialized data to be deserialized.||\r\n|isEncrypted|`bool`, `optional`|Whether the serialized data is encrypted.|`False`|\r\n|decryptionKey|`Bytes`, `optional`|Decryption key (16 Bytes) if the data is encrypted.|`None`|\r\n|classDict|`dict`, `optional`|Dictionary containing class definitions for initializing objects.|`{}`|\r\n|setAttrsAfterInit|`bool`, `optional`|Whether to set attributes after initializing objects.|`False`|\r\n|parseDigits|`bool`, `optional`|Whether to parse string data that represents numeric values into actual numeric types.|`False`|\r\n|initObjects|`bool`, `optional`|Whether to initialize objects during deserialization.|`False`|\r\n|returnGlobalsForPickle|`bool`, `optional`|Whether to return global scope for pickle deserialization.|`False`|\r\n## Contributing\r\n\r\nContributions are welcomed! Please feel free to submit issues, feature requests, or pull requests on the [**GitHub repository**](https://github.com/Were-Logan-0110/PyQtSerializer).\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A PyQt Serialization Library",
    "version": "0.0.1",
    "project_urls": null,
    "split_keywords": [
        "python",
        "encryption",
        "serialize",
        "serialization",
        "pickle",
        "pyqt",
        "qtsave"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "31ab6104e772bc76188dd4e456dfce0b85f2c0efeccc6f59fa100e743c820bbb",
                "md5": "ac1e1aa10fd43f2be4575c75da67026b",
                "sha256": "aef29373a72e0e5f74ae061006baf1f95614d874e18af71a7c6a46e881bd3250"
            },
            "downloads": -1,
            "filename": "PyQtSerializer-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ac1e1aa10fd43f2be4575c75da67026b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15664,
            "upload_time": "2024-02-23T17:37:47",
            "upload_time_iso_8601": "2024-02-23T17:37:47.559502Z",
            "url": "https://files.pythonhosted.org/packages/31/ab/6104e772bc76188dd4e456dfce0b85f2c0efeccc6f59fa100e743c820bbb/PyQtSerializer-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "19ee0cf9a40e660a64eb61f14e9063ace79aaa8b8421dce3743c2c0035e954a3",
                "md5": "f2e194b11210da61424f40259fd8333e",
                "sha256": "07821a9cc8eb3abc270792403674ddd69e862c2868344a5b42334d5da72e6746"
            },
            "downloads": -1,
            "filename": "PyQtSerializer-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f2e194b11210da61424f40259fd8333e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15759,
            "upload_time": "2024-02-23T17:37:49",
            "upload_time_iso_8601": "2024-02-23T17:37:49.737314Z",
            "url": "https://files.pythonhosted.org/packages/19/ee/0cf9a40e660a64eb61f14e9063ace79aaa8b8421dce3743c2c0035e954a3/PyQtSerializer-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-23 17:37:49",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pyqtserializer"
}
        
Elapsed time: 0.19539s