Package for easy process variable in file like if it was usual variable in python
Project description
Short Overview.
local_simple_database is a simple Python package(py>=2.7 or py>=3.4) with the main purpose to help storing and retrieving data from human-readable txt files with one line of code. All the interactions with files are being made in a process-thread safe manner.
Long Overview.
This package consists of 2 main classes with which user should interact:
class_local_simple_database
class_local_dict_database
One small example
Let’s say you want to store file with int variable with name int_times_I_ve_eaten.
Then using this package you can define handler of databases:
from local_simple_database import class_local_simple_database
DB = class_local_simple_database("folder_with_all_my_databases")
and then just use everywhere in your code DB[“int_times_I_ve_eaten”] like if it was usual dictionary.
DB["int_times_I_ve_eaten"] += 1 # To increase value in the file
DB["int_times_I_ve_eaten"] # To get current value from the file
After running this code in the folder with path = “./folder_with_all_my_databases”
will be created file “./folder_with_all_my_databases/int_times_I_ve_eaten.txt” with value.
Value is stored in a human-readable txt file, so you can always access it.
To get it some time later, just use:
int_value_I_was_afraid_to_lose = DB["int_times_I_ve_eaten"]
How to name database-s
Name of database should satisfy template “type_name”
Examples: int_balls, float_seconds_left, str_my_name, dict_useless_heap
So just by the name you can define the type of database, isn’t it awesome.
Installation
Install via pip:
pip install local_simple_database
Basic usage.
1) class_local_simple_database
This class was built to handle (saving-retrieving) one value data like integers or floats.
For now on supported types of databases are: [“int”, “float”, “str”] (Probably will be enhanced soon)
This means that you can use a database with one value inside with types: integer, float, string
Initialization of databases handler
from local_simple_database import class_local_simple_database
DB = class_local_simple_database(
str_path_database_dir=".",
)
Arguments:
- str_path_database_dir: If the explicit path to directory with database-s is not given,
- then will be used path “./local_database”Folder for database-s will be created automatically
A few examples of Usage
After you’ve initialized DB variable you can use:
1) Integer database
If you want to store/access/modify simple int in file
# Process 1
DB["int_red_cars_drove"] += 1
DB["int_red_cars_drove"] += 2
# Oh now, last one was burgundy
DB["int_red_cars_drove"] -= 1
# Process 2
print("red cars already found", DB["int_red_cars_drove"])
# If there was no such DataBase yet, than in will be created and 0 value will be returned.
DB["int_red_cars_drove"] = 5
print("red cars already found", DB["int_red_cars_drove"])
2) Float database
DB["float_last_price_of_watermelon"] = 7.49
# Too many watermelons this year, need to apply 30% discount
DB["float_last_price_of_watermelon"] *= 0.7
print(
"Hello my best customer, current price on watermelon is: ",
DB["float_last_price_of_watermelon"]
)
2) class_local_dict_database
This class was built to handle (saving-retrieving) dictionary with data from a file.
Work with such database-s is a little different from class_local_simple_database so it was necessary to put it in a separate class
Initialization of databases handler
DB = class_local_dict_database(
str_path_database_dir=".",
default_value=None,
)
Arguments:
- str_path_database_dir: If the explicit path to database-s is not given,
- then will be used path “./local_database”Folder for database-s will be created automatically
default_value: value to use if key in DB not found.
A few examples of Usage
Initialization of DB obj.
from local_simple_database import class_local_simple_database
DB = class_local_dict_database(
str_path_database_dir=".",
default_value=None,
)
1) Basic Save-Get data from dict database.
# Set methods
# Set value for whole DB:
DB["dict_very_useful_heap"] = {"Mike": 50, "Stan": 1000000}
## Set keys for one DB with dict
DB["dict_useless_heap"]["random_key"] = 1
DB["dict_useless_heap"]["random_key"] += 3
DB["dict_useless_heap"][2] = ["Oh my God, what a list is doing here", "Aaa"]
DB["dict_useless_heap"][99] = {"Are you serious?": {"You'd better be!": "Bbb"}}
# Get methods
## To get whole dict for DB use:
DB["dict_useless_heap"].get_value() # Sorry for that, I don't know how to do it without additional method
## To get string representation of whole dict:
str(DB["dict_useless_heap"])
print(DB["dict_useless_heap"])
## To get one key from dict:
int_random_key = DB["dict_useless_heap"]["random_key"]
2) Set default value:
# You can set the default value for all databases OR for only one:
## 1) Set default value for all database-s:
DB.change_default_value(0)
## 2) Set default value for one database:
DB["cars"].change_default_value(0)
# They you can use DB similarly as collections.defaultdict
DB["cars"]["red"] += 1
# Oh no, that was burgundy once again
DB["cars"]["red"] -= 1
DB["cars"]["burgundy"] += 1
Advanced usage.
1) class database additional arguments
Both 2 main classes (class_local_simple_database, class_local_dict_database) have additional arguments:
float_max_seconds_per_file_operation=0.05
This variable is necessary for multiprocessing safe work.It set time in which access by process file can’t be accessed by any other process. By default, it set to 10 ms.If you use operation which from accessing value till setting new value needs more time, you are more than welcome to increase it.You can set it to 0.0 if you are not using threads-processes and want the maximum speed.str_datetime_template_for_rolling=””
This variable allows setting rolling save of database results using the DateTime template.If the value is not empty, then saving/retrieving results will be done from deeper folders with names satisfy the evaluation of the DateTime string template.E.G. To save daily results use “%Y%m%d” (Then deeper folder names will be like “20191230”, “20191231”, …)E.G. To save hourly results use “%Y%m%d_%H” (Then deeper folder names will be like “20191230_0”, “20191230_23”, …)
# Full definition of class_local_simple_database
DB = class_local_simple_database(
str_path_database_dir=".",
float_max_seconds_per_file_operation=0.05,
str_datetime_template_for_rolling=""
)
# Full definition of class_local_dict_database
DB = class_local_dict_database(
str_path_database_dir=".",
default_value=None,
float_max_seconds_per_file_operation=0.05,
str_datetime_template_for_rolling=""
)
2) Get values in ALL databases in the directory.
To get a dictionary with data in all databases by database name, use:
DB.get_dict_DBs_data_by_DB_name()
If you were using rolling, then you can get dictionary with results like {“datetime_1”: dict_all_DBs_date_1, }
DB.get_dict_every_DB_by_datetime()
If you were using rolling, and interested only in one database. {“datetime_1”: database_value_1, …}
# Please replace *str_database_name* on name of DB which values you want to get
DB.get_one_DB_data_daily(
str_database_name,
value_to_use_if_DB_not_found=None
)
Links
Releases
See CHANGELOG.
Contributing
Fork it (<https://github.com/stas-prokopiev/local_simple_database/fork>)
Create your feature branch (git checkout -b feature/fooBar)
Commit your changes (git commit -am ‘Add some fooBar’)
Push to the branch (git push origin feature/fooBar)
Create a new Pull Request
Contacts
License
This project is licensed under the MIT License.
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
Hashes for local_simple_database-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1266104de879782e193f863b53681db04c3cec170d12d9f776e19f5ac1caab0 |
|
MD5 | 7d2c8df6eb594c09cec94f69bd57c578 |
|
BLAKE2b-256 | 12a6968bff1daa32eaac099d19b50af480082772e4598f9bbc5122c6669dc828 |
Hashes for local_simple_database-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4e6adb0836741b4ba012d3b0501f4365106b80f01ae64ad45e823edd9797111 |
|
MD5 | a084bcfdbe5d843746aa34abb96aa371 |
|
BLAKE2b-256 | 1654a19318dc0eb1ba1f608b6bec3ac4acba5c95d290c47500c87425d190989c |