Python project- Courier Management Service
This project aims to define a set of functions that provide functionality for a courier service. We will import the pickle and os modules as mentioned below:
import pickle
import os
Pickle module
The pickle
module in Python allows you to serialize and deserialize Python objects, such as lists and dictionaries, to and from a byte stream. This is useful when you want to store data in a file or transmit it over a network. When you "pickle" an object, you convert it into a byte stream, which can then be written to a file or transmitted over a network. You can then "unpickle" the byte stream to reconstruct the original object in Python.
The pickle
module is implemented in Python's standard library, so you don't have to install any additional packages to use it.
Os module
The os
(operating system) module is a built-in Python module that provides functions for interacting with the operating system. It allows you to perform tasks such as reading and writing to files, creating and deleting directories, and executing shell commands.
Let’s make the functions required for this project.
addItem: prompts the user to enter information about a new courier item and stores this information in a file called “courier.dat” using the pickle module.
def addItem():
itemno = int(input("Enter the unique item no : "))
tid = input("Enter the tracking id : ")
desc = input("Enter the product description : ")
wt = input("Enter the weight of the Courier : ")
dadr = input("Enter the destination address : ")
sadr = input("Enter the sender address : ")
status = "Packed"
cost = int(input("Enter the cost of Shipping : "))
cdata = [itemno,tid,desc,wt,dadr,sadr,status,cost]
f = open("courier.dat","ab")
pickle.dump(cdata,f)
print("Details Added Successfully ")
f.close()
display: reads the information stored in “courier.dat” and displays the product description, weight, and destination address for each item.
def display():
f = open("courier.dat","rb")
try:
while True:
cdata = pickle.load(f)
print(cdata[2] + " weights " + str(cdata[3]) + " will be delivered to " + cdata[4])
except:
f.close()
print("-" * 40)
search: prompts the user to enter a tracking id and searches the “courier.dat” file for a matching tracking id. If a match is found, the function displays the product description, weight, and destination address for the matching item.
def search():
td = input("Enter tracking id to be searched : ")
found = False
f = open("courier.dat","rb")
try:
while True:
cdata = pickle.load(f)
if cdata[1] == td:
print("Found, Here are the details ")
found = True
print(cdata[2] + " weights " + str(cdata[3]) + " will be delivered to " + cdata[4])
break
except:
if found == False:
print("No Courier Found with Such a Tracking Id ")
f.close()
print("-" * 40)
remove: prompts the user to enter a tracking id and searches the “courier.dat” file for a matching tracking id. If a match is found, the function removes the item from the “courier.dat” file.
def remove():
td = input("Enter tracking id to be removed : ")
found = False
f = open("courier.dat","rb")
g = open("temp.dat","wb")
try:
while True:
cdata = pickle.load(f)
if cdata[1] == td:
print("Found, Here are the details ")
found = True
print(cdata[2] + " weights " + str(cdata[3]) + " will be delivered to " + cdata[4])
else:
pickle.dump(cdata,g)
except:
if found == False:
print("No Courier Found with Such a Tracking Id ")
else:
print("Courier Deleted Successfully")
f.close()
g.close()
print("-" * 40)
os.remove("courier.dat")
os.rename("temp.dat","courier.dat")
login: allows a user to either login as an admin or as a member. If the user chooses to login as an admin, they are prompted to enter a password. If the password is correct, the user is then prompted to enter a new user name and password, which are then stored in a file called “users.dat” using the pickle module. If the user chooses to login as a member, they are prompted to enter their user name and password. The function then searches the “users.dat” file for a matching user name and password and grants access if a match is found. If no match is found, the function displays an error message. The function returns a boolean value indicating whether the login was successful or not.
def login():
print("^" * 50)
print("\t ABC Courier Services ")
print("^" * 50)
print("Press 1 - Login as Admin")
print("Press 2 - Login as Member ")
ch = int(input("Enter your choice : "))
if ch == 1:
password = input("Enter password ")
if password == "1221":
usrname = input("Enter the New User name : ")
pwd = input("Enter the new Password : ")
f = open("users.dat","ab")
pickle.dump([usrname,pwd],f)
f.close()
print("^" * 50)
print("User Added Successfully ")
print("^" * 50)
elif ch == 2:
usrname = input("Enter the User name : ")
pwd = input("Enter the Password : ")
f = open("users.dat","rb")
found = False
try:
while True:
d = pickle.load(f)
if d[0] == usrname and d[1] == pwd:
print("Access Granted ")
found = True
break
except:
if found == False:
print("Invalid User Name or Password ")
f.close()
return found
Let’s use all the above functions that we just created and conclude this project.
if login():
while True:
print("-" * 30)
print("Press 1 - Add New Product")
print("Press 2 - Display All Couriered Products")
print("Press 3 - Search a Courier")
print("Press 4 - Delete a Courier")
print("Press 5- Exit")
ch = int(input("Enter your choice : "))
if ch == 1:
addItem()
elif ch == 2:
display()
elif ch == 3:
search()
elif ch == 4:
remove()
elif ch == 5:
break
Let’s now test our code. In the below screenshot code, we gave our choice as 1 to login as admin. We enter our password as ‘1221’. Then we created a new user in our CMS.
Let’s try all our options:
We can conclude that this code provides functionality for a courier service, including adding new courier items, displaying the details of existing items, searching for items by tracking id, and deleting items. The code also includes a login function that allows a user to either login as an admin or as a member and stores user information in a separate file using the pickle module.