Tesla
Tesla

Reputation: 141

How do I get access to objects of another class?

So I want to implement two classes JobQueue and Process. The idea is that JobQueue manages the objects of type Process and prioritizes the objects with the shortest processing time. So the first attribute shall represent the ID of the process and the second attribute shall be the processing time, both being integers. How does my class Jobqueue get access to these objects from the other class?

>>>class Process:

     def __init__(self,pid,procTime):
       self.pid=str(pid)
       self.procTime=procTime
>>>p1=Process(1234,55)
>>>p2=Process(4544,34)

>>>class JobQueue:

     queue=[]
     def insert(p): #how can I insert a certain object that I created before (p1 or p2)?
     queue.append([p.pid,p.procTime])

Some help would be much appreciated.

Edit: Here are some examples what the code should do in the end:

>>>p1=Process(5234,60)
>>>p2=Process(8824,17)
>>>p2=Process(2291,34)
>>>j=JobQueue()
>>>j.extractMin() #Giving me the minimum of the queue and deletes it. If it is empty, the output shall be float('inf')
inf
>>>j.insert(p1) #the method insert inserts the objects to the queue
>>>j.insert(p3)
>>>print(j.minimum()) #prints the ID with the shortest processing time of all objects
2291
>>>j.decreaseKey(p1,23) #replaces the current processing time of p1 by 23
>>>print(j.minimum())
5234
>>>j.insert(p2)
>>>print(j.extractMin())
8824
>>>print(j.extractMin())
5234
>>>print(j.minimum())
2291

I guess these are enough examples to give you an insight what kind of code I want to write.

Upvotes: 0

Views: 2339

Answers (3)

JohanL
JohanL

Reputation: 6891

Well, you are pretty much there already. Just pass your objects as arguments to your method. That is, you pass the Process objects to JobQueue.insert(or rather to an object created from that class). However, when defining a method the first argument is always self. Thus, you have to write like:

class JobQueue:
    def __init__(self):
        self.queue=[] # This gives a unique queue for each object

    def insert(self, p):
        self.queue.append([p.pid,p.procTime])

Also, you can consider storing the Process object p as it is in your queue, rather than its different parts:

    def insert(self, p):
        self.queue.append(p)

That way you can access all of the Process objects, including functions to update process times.

Upvotes: 2

George
George

Reputation: 109

Something like this:

class Process:
    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime

class JobQueue:
    queue=[]
    def insert(self,p): 
        self.queue.append([p.pid,p.procTime])

p = Process(1,2)
jq=JobQueue()
jq.insert(p)
print (jq.queue)

The OP:

[['1', 2]]

Upvotes: 1

DSLima90
DSLima90

Reputation: 2838

You can insert in the init method of your job queue:

class Process:

    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime



class JobQueue:

    def __init__(self,procs = None):

        self.queue = []
        for p in procs:
            self.insert(p)

    def insert(p):
        self.queue.append([p.pid,p.procTime])

p1=Process(1234,55)
p2=Process(4544,34)
a = JobQueue(procs = [p1, p2])

Upvotes: 1

Related Questions