Storage Module in Python
There are several places in our everyday lives where a database management system might be useful for storing relevant information. Using a DBMS is usually relatively easy, but when we need to store complex structured data, things may become more complicated. There are several varieties of database management systems, with the Relational Database Management System (RDBMS) being the most widespread in everyday usage (RDBMS). Using a relational database management system (RDBMS) is a great way to keep track of and organize all of your important data and documents. Relational database management systems are useful, but they aren’t always required or even feasible. We may wonder if there are other options, if there are other ways to save data, whether there are other tools available, etc.
The affirmative to all of these queries! When it isn’t necessary to employ a relational database management system (RDBMS), we may resort to alternative tools and approaches for data storage instead. Several tools exist, with many being tied to the language’s standard library. This indicates that, in the realm of code, there exist libraries offering viable alternatives to the more commonplace Relational Database Management System. Python has packages that make it possible to implement a database management system–like interface, hence this concept is not language–specific. Shelve Module is a Python library or module that is useful for long-term data storage. In this lesson, we’ll study Python’s Shelve Module, which is used for archival purposes inside a Python program.
program.
Shelve Module in Python
Python’s Shelve Module is widely used because it provides a powerful instrument for storing data persistently in files with Python code. This module’s name, “Shelve,” gives us a good idea of what it does: it acts as a shelf object that stores and retrieves information from a file. With the Shelve Module, we create a dictionary-like object called a shelf that is saved permanently on our hard drive. With the Python Shelve Module, all of our data and information may be safely stored indefinitely.
want.
Shelve Module: Introduction
Unless we decide to delete it, the Python Shelve Module safely stores all the data and information contained in a file on our computer. The Shelve Module is not only useful for archiving data inside a file, but also for editing existing data and appending new data to the same file. Using the Shelve Module and its associated functions inside a Python application, we may create, read, write, and delete files as needed. The file produced by the Shelve Module is very reminiscent to the DBMS database used in operating systems like UNIX. The unique dictionary object in the file accepts just strings as keys, and the objects in the shelved files may be used as values.
keys.
Shelve Module: Classes
In this part, we’ll talk about the classes available in the Shelve Module of Python, just as we would in any other Python module. Python Shelve defines the following three classes:
Module:
S. No. | Module Class | Description |
---|---|---|
1 | Shelf Class | The shelf class is the base class from the Shelve Module, and it is used for the shelf implementation in a shelve file. We can initialize the Shelf class from the Shelve Module using a dict-like object inside the Python program. |
2 | DBFileNameShelf Class | This is a sub-class of the base Shelf class, and it accepts the name of the shelve files as the parameter to its constructor rather than a dict-like object parameter. |
3 | BsdDbShelf Class | BsdDbShelf Class is also a sub-class from the base shelf class, and this class accepts a dict-like object as a parameter which makes this sub-class different from the DBFileNameShelf class. The dict-like object parameter which we pass as a parameter to its constructors should support first(), last(), previous(), next() and set_location() methods. |
Here, we’ve cataloged every kind of class and subclass found in the Shelve Module and identified the object types that each of these classes accepts as a parameter.
classes.
Working with the Shelve Module
Python’s Shelve Module is a library that comes standard with both Python 2 and Python 3. There is no need to run an installation or the pip command to get started with the Shelve Module. The Shelve module’s functionality may be imported into a Python application with the ‘import shelve’ command. The construction of the Shelve Module will be broken down into three sections: creating and saving a shelve file, reading and retrieving values from the file, and finally updating the data stored in the file. The next three sections will teach us how to use and apply the Shelve Module.
steps:
- Creating a shelve file to store data
- Retrieving Data from the Shelve file
- Updating data in the Shelve file
Each of the aforementioned sections will be covered in detail using a working example application that demonstrates how to use the Shelve Module’s features to accomplish the aforementioned goals. By making a sample shelf, we can study how the various components of the Shelve Module operate in practice.
file.
Creating a Shelve File to Store Data:
Like with any other file, a shelf file must be created before any data can be saved to it. To create a shelf file, we only need to use its open() method, which is quite similar to the open() function we use when working with regular files. In order to open or create a new shelve using the open() function of the Shelve Module, the following syntax must be used.
file:
-
>> shelve = open(NameOfFile, flag =
‘c’
, protocol = None, writeback = True)
The DbfilenameShelf object it returns will be responsible for gathering the user-supplied data and writing it to the specified file. The syntax for the open() function is as follows, and an explanation of its arguments follows.
above:
-
NameOfFile:
It is the name of the file which we want to or have assigned to the database (shelve file) we are creating/opening using the
open()
-
flag:
The flag parameters for the
open()
function’s syntax is c, w, n, and r, in which ‘c’ is the default one. The ‘c’ keyword in the flag parameter represents we gave read and write access for the file, ‘w’ represents we gave write-only access, ‘r’ represents we gave only read access, and ‘n’ represents that we are creating a new file with NameOfFile name. -
Writeback:
Writeback parameter is false by default, but if we set this parameter as true, the entries we give in the shelve file will be cached in the program’s cache memory. -
Protocol:
The protocol parameter in the function denotes the pickle protocol for the entries.
Now that we know how to utilize the open() function of the Shelve Module and what its arguments and syntax are, we can examine its implementation in a sample application. For starters:
Examine the following Python code in which we use a shelf file to save information.
file:
Product Code:
-
# Importing the Shelve Module
-
import
shelve
-
# Creating a shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
print(
“A Shelve file with SampleFile name is successfully created”
)
-
# Making entries inside the shelve file
-
shelveVariable[
‘Employee Name’
] =
‘Jackie Chan’
-
shelveVariable[
‘Employee Age’
] =
27
-
shelveVariable[
‘Employee department’
] = Management
-
shelveVariable[
‘Employee Performance’
] = Excellent
-
shelveVariable[
‘Employee Score’
] =
9.2
-
shelveVariable.close()
-
print(
“All entries are successfully made inside the sample shelve file”
)
Explanation:
A Shelve file with SampleFile name is successfully created All entries are successfully made inside the sample shelve file
In order to utilize the open() function of the Shelve Module and generate a sample shelve file, we have first imported it into the example application. Finally, we created an example shelf file called “SampleFile” using the open() method inside the initialized shelveVariable so that we could utilize it later for entries. The example shelf file we developed was then utilized to create many entries using the dictionary technique and an initialized shelveVariable. We then utilized the initialized variable to make many entries in the individual columns, and finally we used the close() method to save these changes to the file.
The preceding code snippet creates a directory named “SampleFile.dir” on our device’s shelf and populates it with items when executed as an example.
it.
Note: Every time we access the shelve file present in our system, sync() and close() operations are performed along with it, which may slow down the overall process.
Methods in Shelve Module:
In the above code snippet, we utilized the open() function from the shelf module to generate a database shelve file. Several helpful techniques, each with their own set of capabilities, are at our disposal thanks to the Shelve Module. The shelf module has a variety of different approaches, including the ones listed below:
Sr No | Method Name | Description of Method |
---|---|---|
1 | get() | This method will return the value associated with the key (provided as an argument inside the method) present in the database file. |
2 | keys() | Keys() method is used to return the names of all keys present in a shelve file (which name we have provided as an argument in the method). |
3 | values() | The values() method works very similarly to the keys() method of the shelve module as it returns the values present in a shelve file. |
4 | items() | If we want to get both keys and values from a shelve file in a single call, we can use this items() method by providing the name of the shelve file as an argument in the method. |
5 | close() | The close method first synchronizes the items we gave for the shelve file, and then it closes the persistent dict object, which we initialized while opening/creating the shelve file. |
6 | update() | If we want to update our shelve file and add more entries, we can use the update() method to do the same. |
7 | pop() | The pop() method is exactly opposite to the update() method as it is used to remove an entry (Key and value resemble with it) from the Shelve file. |
8 | sync() | sync() method is used to write back all the entries into the cache memory of the program whenever we set writeback to True while opening the shelve file. |
In the future, we’ll make use of a few of the shelf module’s methods to conduct different operations on the database shelve file we just built.
examples.
Retrieving Data from the Shelve File:
When a shelf file has been created and data inputs have been completed, the first step is to verify that everything was entered correctly. One certain approach to be sure is by seeing whether each of these entries appears in the output. This technique is especially helpful in situations when we are dealing with someone else’s shelf file or when we have no idea what information is included inside our own shelve file. Data stored in a Shelve may be retrieved using many different methods, including get(), values(), items(), and keys (). We will use all three of these ways to get information from the sample shelf file we made in the first example; they all function differently and provide the same information, but in different formats. I Sole value retrieval using the get() function:
To further grasp how the get() function works, we’ll use it in an example application to display the value associated with a certain key. Exhibit 2
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Print data entries from the sample shelve file in the output
-
print(
“Name of Employee in the file: ”
, shelveVariable[
‘Employee Name’
])
-
print(
“Age of Employee: ”
, shelveVariable[
‘Employee Age’
])
-
print(
“Department of Employee: ”
, shelveVariable[
‘Employee department’
])
-
print(
“Score of Employee: ”
, shelveVariable[
‘Employee Score’
])
Productiveness:
Name of Employee in the file: Jackie Chan Age of Employee: 27 Department of Employee: Management Score of Employee: 9.2
The result shows the data value corresponding to the key provided in the get() function. (ii) Taking everything out of the shelf file at once
The items() function of the shelf object returns all the things included in the example shelve file if we use it to fetch the whole file at once. Here’s a software that demonstrates how to put this technique into practice: Example 3:
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Print all data entries from the sample shelve file in list form
-
print(
“Items in the sample shelve file: ”
, list(shelveVariable.items()))
Productiveness:
Items in the sample shelve file: [('Employee Name', 'Jackie Chan'), ('Employee Age', 27), ('Employee department', 'Management'), ('Employee Performance', 'Excellent'), ('Employee Score', 9.2)]
The items() method successfully printed all the items in the example file, as seen by the output. Key extraction from the shelved file (iii)
The keys() function may be used inside the application after opening the shelf file to output just the keys that are present in the example shelve. Case in point number four:
Resulting
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Print only keys from the sample shelve file in the output
-
print(
“Keys in the sample shelve file: ”
, list(shelveVariable.keys()))
:
Keys in the sample shelve file: ['Employee Name', 'Employee Age', 'Employee department', 'Employee Performance', 'Employee Score']
With the keys() function, the output contains all of the keys found in the example shelf file. (iv) Getting only the numbers out of the shelved file:
Similar to the previously implemented technique that made use of the keys() function, this one also requires using the values() function to print all values from the shelf file. 5th Instance:
Resulting
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Print only values from the sample shelve file in the output
-
print(
“Values in the sample shelve file: ”
, list(shelveVariable.values()))
Values in the sample shelve file: ['Jackie Chan', 27, 'Management', 'Excellent', 9.2]
In the output, you’ll find both the keys and all the values from the example shelf file ()
method.
Updating Data in the Shelve File:
There are three things we can do with this approach, including updating the information currently in the shelf file, but we will only be doing two of them today. We can do three things with an existing key in the file: (1) change its value; (2) add new entries; and (3) delete an item. To change the value associated with a key, we just need to assign the new value to the key by writing it along with the key object. The shelf file is being updated by adding new entries.
Adding new entries requires defining a dictionary variable in the code and then assigning a key and value to the variable. The update() function may then be used to append new dictionary entries to the file by passing in the variable’s name as an argument. Case No. 6
Final Product:
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Adding
new
fields inside the sample shelve file
-
sampleDict = {
‘Employee Salary’
:
50000
,
‘Employee Sub-department’
:
‘Field Work’
}
-
shelveVariable.update(sampleDict)
-
# Print data entries from the sample shelve file in list form
-
print(
“Items in the sample shelve file: ”
, list(shelveVariable.items()))
Items in the sample shelve file: [('Employee Name', 'Jackie Chan'), ('Employee Age', 27), ('Employee department', 'Management'), ('Employee Performance', 'Excellent'), ('Employee Score', 9.2), ('Employee Salary', 50000), ('Employee Sub-department', 'Field Work')]
As we printed out everything in the example shelf file, we saw that several additional things had been added. Taking something from the shelf (ii)
The pop() function may be used to delete a file entry that already exists in the file. Just by entering the name of the key, the function will delete both the key and its corresponding value from the shelved file. We may be sure of this by printing out the whole contents of the shelved-items file. Exhibit 7:
Final Product:
-
# Importing the Shelve Module
-
import
shelve
-
# Opening the sample shelve file with open() function
-
shelveVariable = shelve.open(
“SampleFile”
)
-
# Delete a field from the shelve file
-
shelveVariable.pop(
‘Employee Performance’
)
-
# Print data entries from the sample shelve file in list form
-
print(
“Items in the sample shelve file: ”
, list(shelveVariable.items()))
Items in the sample shelve file: [('Employee Name', 'Jackie Chan'), ('Employee Age', 27), ('Employee department', 'Management'), ('Employee Score', 9.2), ('Employee Salary', 50000), ('Employee Sub-department', 'Field Work')]
You can see that we have successfully eliminated the employee performance key-value item from the report by using the pop() technique.