Module that brings together useful tools for Python applications.
Project description
dcd_py_util
Module that brings together useful tools for Python applications.
Preferences: provides configuration info read from a JSON file.Logger: simplifies logging configuration by reading it from a JSON configuration file (the JSON file fromPreferences).Encryption: provides an easy way to encrypt and decrypt text stored in open configuration files, protecting passwords and sensitive data.
How to use
Preferences
- Create a JSON file named
prefs_config.jsonand place it in the root folder of your application. - Include the configuration need in the prefs_config.json file in JSON format. Example:
{
"object": {
"string": "bla bla bla",
"number": 123,
"float": 456.7,
"boolean": true,
"date": "2023-01-01",
"array": [
{
"key1": "val1",
"key2": "val2"
},
{
"another_key1": "another_val1",
"another_key2": "another_val2"
}
]
}
}
- Any data in a valid JSON format can be placed in the
prefs_config.json. - The configuration data will be available as a Python dictionary.
- To access the Python dictonary, use the
Preferencesclass like the example below:
from dcd_py_util.prefs_config import Preferences
prefs: Preferences = Preferences().get_preferences()
print(prefs[logging][name])
- The
Preferencesis a singleton. - It's possible to update the prefs file by using the method
Preferences().save_prefs()after change a value in the prefs dictionary. - The change will be visible to all variables (pointers) to the prefs dictionary.
Logger
- The logger configures two log output handlers: console and file. The file handler makes logfile rotation soon it achives the configured max size. The number logfiles kept can be also configured. The default values are 10 Mb as max size and 30 files to be kept.
- Place the
loggingconfiguration in theprefs_config.jsonlike in the below:
{
"logging": {
"name": "logger_name",
"folder": "./log",
"filename": "log_filename.log",
"max_size": 10485760,
"file_count": 30,
"console_level": "DEBUG",
"file_level": "DEBUG"
}
}
- The attribute
max_sizeinforms the max size of the file in bytes. The attributefile_countinforms the number of files to be kept. The rest of the attributes are pretty straightforward. - The encoding used for the file handler if
utf-8. - To access the logger in your Python code follow the example below:
from dcd_py_util.logger import Logger
logger: Logger = Logger()
logger.info("This is an INFO level log entry")
- The available log level methods are the same for the standard Python logging.Logger class.
- It's possible to pass a
logging.Loggerinstance in the constructor to use, for example, a Django logger. This feature is present just to standardize your source code.
Encryption
- To use the
encryptionmodule to encrypt a text, use:
from dcd_py_util import encryption
encrypted_text: str = encryption.encrypt("plain text to encrypt")
- To use the
encryptionmodule to encrypt a text and include the "Encrypted" label in front of it, use:
from dcd_py_util import encryption
encrypted_text_with_label: str = encryption.encrypt_with_label("plain text to encrypt")
- To use the
encryptionmodule to decrypt an encrypted text, having the "Encrypted" label in front of it or not, use:
from dcd_py_util import encryption
plain_text_decrypted: str = encryption.decrypt("Encrypted <encrypted text>")
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
dcd_py_util-0.0.4.tar.gz
(5.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dcd_py_util-0.0.4.tar.gz.
File metadata
- Download URL: dcd_py_util-0.0.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ecd850373a85b80d228051f067f6d39cbfdd609d17207f8287482cb594d5d5b
|
|
| MD5 |
9b02cedeffb6e482bbc785df3ebc0ef6
|
|
| BLAKE2b-256 |
7f798f7b85813a4ce7a704392a72f3096688da18204b7c245498382e973bac4a
|
File details
Details for the file dcd_py_util-0.0.4-py3-none-any.whl.
File metadata
- Download URL: dcd_py_util-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b35213c56ccc543029553a5f3f8f3d13d493bb64cd5aabfdf623ecb7b0c06b37
|
|
| MD5 |
f5c56b16ee90ac0ce55467440ea21fbe
|
|
| BLAKE2b-256 |
65c0be3f205e82c53d9532b34a0638cf7702985a89b0f48ef8ce97a1c60dbd5e
|