Make all your classes permanent in a flash
Project description
Pyrmanent
A base class to make your Python classes permanent in a flash.
Features
- Easy to use.
- Great compatibility.
- No database needed.
- Ask for new features through the issues section.
- Join our Telegram group for support or development.
Installation
You can install or upgrade the module with:
pip install pyrmanent --upgrade
Usage guide
Basic usage:
Making your custom clases permanent is as easy as adding Pyrmanent as the base class.
from pyrmanent import Pyrmanent
class MyClass(Pyrmanent):
pass
Call the save method to save the current instance data:
my_class_instance = MyClass()
my_class_instance.my_data = "Hello world!"
my_class_instance.save()
To load a previously saved instance data, just create the instance and data will be automatically loaded:
my_class_instance = MyClass()
print(my_class_instance.my_data)
"Hello world!"
Configuration
Instance name
The name parameter allows saving different instances of the same class. Each one will
keep its own data.
first_instance = MyClass(name="first")
first_instance.my_data = "This is the first instance"
first_instance.save()
second_instance = MyClass(name="second")
second_instance.my_data = "And this the second one"
second_instance.save()
Saving path
The path for the saved files can be customized with the folder parameter. If not provided,
the files will be saved on the execution folder.
my_class_instance = MyClass(folder="data")
Auto saving
The autosave parameter allows using the autosave method to save data only when True.
This is specially useful for your custom class methods.
class MyClass(Pyrmanent):
def set_title(self, title):
self.title = title
self.autosave()
# The instance data will be saved when the method is called
my_class_instance = MyClass(autosave=True)
my_class_instance.set_title("Test")
# You should manually save the instance data
my_class_instance = MyClass(autosave=False)
my_class_instance.set_title("Test")
my_class_instance.save()
Override the __init__ method
The __init__ method can be overrided to add attributes and provide default configurations,
like for example the saving path in below example. Remember that the super() call should
be always done at the end.
class MyClass(Pyrmanent):
def __init__(self, name, autosave=True):
self.first_value = 1
self.second_value = 2
super().__init__(name=name, folder="data", autosave=autosave)
Reset instance data
To reset instance data, call the reset method and then initialize the instance again.
my_class_instance.reset()
my_class_instance = MyClass()
Use dill instead of pickle
Dill extends the compatibility with several data types that pickle module can't serialize. It provides the same interface, so it's totally compatible without any code change needed. To start using dill, just install it using pip and it will be used by default:
pip install dill --upgrade
To stop using dill, just uninstall it. You can find more information about dill in the official repo.
License
Copyright © 2021 Sergio Abad. See license for details.
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
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 pyrmanent-1.1.0.tar.gz.
File metadata
- Download URL: pyrmanent-1.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6184d81c283a96fd58ae04cec78dea0d199b514422ecf4382b3d96e50e316ef0
|
|
| MD5 |
2ebb117aff29f16ecc437b22482b28ed
|
|
| BLAKE2b-256 |
92d4f0455a32ad504eccca1b9652f5641571775b39c2d37e790f8dfb6ebe29cb
|
File details
Details for the file pyrmanent-1.1.0-py3-none-any.whl.
File metadata
- Download URL: pyrmanent-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3955d9a8827aec6951884b8c57843b2e5a4f54baf3b6286b31aa09feac742ad
|
|
| MD5 |
258584afb51afa52dd7387be77845753
|
|
| BLAKE2b-256 |
93810ba186ad26e5b4c4a6eb3bcf358063e67fa01fb89cb4e98e5409219c5345
|