A shelf is an object similar to a dictionary but which allows data persistence. You could thus save your data in a file as if it were a dictionary. This julia implementation is based on the Shelve version of python. So if you used the Shelve version of python you would not be out of place. However this implementation does not stop there, you could also use LopShelve to map your data from database (Sqlite) to dictionary
- Add the Shelve module by entering the following lines in your REPL
]add LopShelve
- Import the LopShelve module then create a Shelf object with the open method! as following (Please specify the name of the file to open without extensions, if it does not exist it will be created)
using LopShelve: open!
data = open!("test_file")
- You can then use your Shelf object as a dictionary.
data["user_name"] = "machkouroke"
data["password"] = "abcdefgh"
- And write the Shelf object to disk using
commit
.
commit(data)
- You can also use Julia's
do...end
syntax to automatically open, modify, and write to disk in one go (Recommended)
open!("test_file") do data
data["user_name"] = "machkouroke"
data["password"] = "abcdefgh"
end
- You can delete a Shelf's file with the
delete
function
delete(data)
- If you use the do...end syntax
open!("test_file"; deletion=true) do data
data["user_name"] = "machkouroke"
data["password"] = "abcdefgh"
end
data = open!("test_file.db", "table_name")
- You can then use your Shelf object as a dictionary with table's primary key for indexing
For example The
table_name
table has two columns: username (Primary key) and password so we can register a user as follows
data["machkouroke"] = (password="abcdefgh")
data["johndoe"] = (password="abcdefghj")
length(data) # 2
"machkouroke" in data # true
"alex" in data # false
for i in data
print(i)
end
Output
Dict{Symbol, AbstractVector} with 2 entries:
:username => ["machkouroke"]
:Title => ["abcdefgh"]
Dict{Symbol, AbstractVector} with 2 entries:
:username => ["johndoe"]
:Title => ["abcdefghj"]
- You can also delete a ShelfSql file with the
delete
function
delete(data)