In: Computer Science
This program is in Python
### simulates packets of data arriving at irregular times. The data
gets
### read from the file and then you must place it in a queue where
it will wait
### its turn to get processed. The "processing" happens in a
threaded subroutine
### called "process_input_data". That function should pull data
from the queue,
### hash the student ID number, and then store the data in a hash
table which
### you have to implement. You should decide ahead of time (by
looking at the
### student_data.txt file) what the hash function should be and how
big the hash
### table will be. There are multiple acceptable answers. Finally,
prove that
### your hash table works by retrieving and printing information
about students
### with the following IDs:
### 427980112
### 258399712
### 948140115
# import the libraries needed to make all this work
import time
import os
from threading import Thread
# let the program know where you've put the data file
myDir = '/home/project_x'
filename = 'student_data.txt'
# create a class of type "student" that holds the data fields for
each student.
# add whatever methods you see fit
class student:
# YOUR CODE HERE
# DEFINE A CLASS TO HOLD STUDENT DATA
# this function will pop students out of the queue and place
them in the hash
# table, which is to be a global variable called "hash_table"
def process_input_data(stop):
global student_queue, hash_table
while not stop() or len(student_queue)>0:
if len(student_queue)>0 :
# YOUR CODE HERE
# POP ITEMS FROM QUEUE AND
# PUT THEM IN THE HASH TABLE
################################################################################
def main():
global student_queue
student_queue = []
# set up the thread so that it can process students once they're
in the queue.
# do not modify this
stop_threads = False
thr = Thread(target=process_input_data,args =(lambda :
stop_threads, ))
thr.start()
# load data from file, enforcing delays in order to
simulate
# asynchronous data arrivals
os.chdir(myDir)
with open(filename,'r') as infile:
# mark the initial time
tStart = time.time()
# for each line in the input file...
for line in infile:
# ... split the line into its components, and then ...
t, firstname, lastname, gpa, id_nmbr, mjr = line.split()
# ... wait until the elapsed time matches the 'arrival' time of
the
# line of data in the text file. This is how we are
simulating
# data packets arriving at irregular times.
while ((time.time()-tStart)< float(t) ):
pass
# when it's time, create a new object of type "student" and
push
# it onto the queue
# YOUR CODE HERE
# CREATE OBJECT USING NAMES, GPA, ID, MAJOR, & PUSH IT TO
student_queue
# this is needed to stop the process_input_data function once all
the data
# has been read from the file into the queue
stop_threads = True
thr.join()
# add code to search the hash table for the students with these
student IDs
# and to print all their information
### 427980112
### 258399712
### 948140115
# YOUR CODE HERE
# FIND STUDENTS AND PRINT THEIR INFO
if __name__=="__main__":
main()
#create a small data to run the program named "student_data.txt"
Please find the program and code has been written wherever YOUR CODE has been written.
import time
import os
from threading import Thread
# let the program know where you've put the data file
myDir ="C:/Users/SAGAR/Desktop/"
filename = 'student_data.txt'
student_queue=[]
hash_table={}
class Student:
def __init__(self,firstname,lastname,gpa, id_nmbr, mjr):
self.fname=firstname
self.lname=lastname
self.gpa=gpa
self.id_number=id_nmbr
self.major=mjr
def getid(self):
return self.id_number
def printinfo(self):
print("Name:"+self.fname+" "+self.lname+" ID:"+self.id_number+" GPA:"+self.gpa+" Major:"+self.mjr)
def process_input_data(stop):
global student_queue, hash_table
while not stop or len(student_queue) > 0:
if len(student_queue) > 0:
student= student_queue.pop()
hash_table.add(student.getid(),student)
def main():
global student_queue
student_queue = []
global hash_table
hash_table={}
# set up the thread so that it can process students once they're in the queue.
# do not modify this
stop_threads = False
thr = Thread(target=process_input_data, args=(lambda: stop_threads,))
thr.start()
# load data from file, enforcing delays in order to simulate
# asynchronous data arrivals
os.chdir(myDir)
with open(filename, 'r') as infile:
# mark the initial time
tStart = time.time()
# for each line in the input file...
for line in infile:
# ... split the line into its components, and then ...
t, firstname, lastname, gpa, id_nmbr, mjr = line.split()
# ... wait until the elapsed time matches the 'arrival' time of the
# line of data in the text file. This is how we are simulating
# data packets arriving at irregular times.
while (time.time() - tStart) < float(t):
pass
# when it's time, create a new object of type "student" and push
# it onto the queue
student= Student(firstname,lastname,gpa,id_nmbr,mjr)
student_queue.append(student)
# this is needed to stop the process_input_data function once all the data
# has been read from the file into the queue
stop_threads = True
thr.join()
# add code to search the hash table for the students with these student IDs
# and to print all their information
### 427980112
student_1= hash_table.get("427980112")
###258399712
student_2= hash_table.get("258399712")
### 948140115
student_3= hash_table.get("948140115")
student_1.printinfo()
student_2.printinfo()
student_3.printinfo()
if __name__ == "__main__":
main()