Simple Save is an easy way to work with data in your Python script.
You can save and load your data without much effort or knowledge about any storage method.
Moreover, it provides the possibilities to use data and variables globally in a script.
The library does not reinvent the wheel, but enriches it with not having to deal with it.
Installation from pip pip install simplesave
Installation with pip + github pip install git+https://github.com/princessmiku/simplesave
It's possible to use it "local" or global (Planed) in your script
Different
A local storage is allocated in a variable and the global storage is called directly from
the library without a variable, so it can be used from anywhere.
- Internal
- Json
- XML (Planed)
- SQLite (Planed)
All functions work the same on all types
An internal memory stores the data in the class directly, when you restart the script, it loses all the data. If you want a way to save, this method is nothing for you.
This method is best suited for caches.
from simplesave import Storage, INTERNAL
storage: Storage = Storage(INTERNAL)
Accept any python possible type and class
The JSON storage is built in the same way as the internal one, with the addition that you can store it in a JSON and load it from there.
With this storage method a file specification with file_path=xxx
is possible,
if no specification is set it falls back to the file simplesave.json
.
from simplesave import Storage, JSON
storage: Storage = Storage(JSON, file_path="data.json")
Accept all regular JSON Possible types
- String
- Integer
- Boolean
- Float
- List
- Dictionary
Lists content and dictionary are not checked if they correspond to the json format, this is the user's task to avoid it.
XML is the next planned feature
SQLite is planned
All functions work the same on all types
path
is always a stringindex
is always an integerfill
is always a list with string/integer objectsvalue
can be anything, but can also be limited by certain storage methods, as not all types are supported by this one, for example JSON
The library works with a key point principle,
which means that entries are separated with a dot in the path -> user.5521.information.email
.
If there are unknown variables in the path then they are marked with a ?
-> user.?.inofrmation.email
The filling of this goes through several ways
One way is to just do it directly in your own code
user_id = "5521"
my_path = f"user.{user_id}.information.email"
The library has the possibility to take it over, otherwise the ?
would have no sense.
As an example we will use the get_variable
function, but all functions where a path is specified can use the.
With the fill you can pass a list with the values to be entered, strings and integers are allowed in there.
storage.get_value("user.?.information.email", fill=[5521])
it is also possible to do it via args and write the parameters one after the other, but note if the function
needs something else like the value
or the index
first.
Strings and integers are allowed in there.
storage.get_value("user.?.information.email", 5521)
Example of multiple args
storage.get_value("user.?.information.email.?", 5521, "main_address")
At the end, it's possible to combine each other. But you have to take note that first the args and then the list is taken.
storage.get_value("user.?.information.email.?", 5521, fill=["main_address"])
Of course, it is also possible to use several fill parameters, just to mention it
storage.get_value("user.?.information.email.?", fill=[5521, "main_address"])
functions for getting data from the storage
Get a value from the storage
- path
- any
result = storage.get_value("config.port")
Get a value from the storage, if this a list, get the item at the position
- path
- index
- any
result = storage.get_value_by_index("config.ports", 1)
Get the type of the value
- path
- type of the value
val_type = storage.get_value_type("config.ports", 1)
Check if path exists
- path
- true if path exists, else false
exists = storage.exists_path("config.ports")
Set a value on the given path, its override the value if there's one
- path
- value
storage.set_value("config.port", 2255)
Add a value the list in a path, its generate automatically a list (without data loss), if there's no list
- path
- value
storage.add_value("config.port", 2255)
Delete all data in the path with the path itself (not the complete only the last)
- path
storage.delete("config.port")
Remove from a list a value by the value
- path
- value
storage.delete("config.ports", 2255)
Remove from a list a value with the index
- path
- index
storage.delete("config.ports", 1)
Set a path entry to null, its delete all sub data
- path
storage.null("config.ports")
Save all the data of the current storage in your file
storage.save()
Not completely used but Translated with DeepL translator (free version)