0

I have written code to simulate the working of cache memory.

In this model, I try to realize the FIFO-algorithm which allows us to delete the last unused element (data, value, whatever).

I wrote a special function, which gives me a list o with numbers (these numbers are addresses in memory).

q=Queue.Queue(800)# Cache - memory. This is a queue which is more likely help me to simulate FIFO-algorithm
QW=[] # External memory
l=raw_input("Enter a desire operation:")#I enter my operation.
for i in range(len(o)):
    time.sleep(0.4)
    u = time.time()
    k=o.pop(0) #o - is the list with numbers (these numbers are addresses in memory). Here I get each address through pop.
    while l=='read': #If operation is "read" then i need to get my adress from q (cache-mem) or from QW (Is the external memory) and put it in q - (is the Cache-memory).
        if k not in q:
            if j in QW and k==j:
                q.put(j)
        else:
            q.get(k)
    while l=='record':#If operation is "record" then i need to write (append to QW) an address in QW or q, but only if the same address have existed already in QW or q.
        if k not in q:
            QW.append(k)
            print QW
        else:
            q.put(k)
    print q.get()

But I get the error:

TypeError: argument of type 'instance' is not iterable at line
  if k not in q
Ajean
  • 5,528
  • 14
  • 46
  • 69
Alex Skys
  • 13
  • 6
  • Where did `Queue.Queue` come from? Is it from some 3rd party library? – Anthony Kong Sep 29 '16 at 06:20
  • Hi Alex, Correction: You can do `if k not in q.queue`but see [this](http://stackoverflow.com/a/16506527/3727050) for issues and thread safety – urban Sep 29 '16 at 06:25

1 Answers1

0

You cant do that with Queues I think, instead use collections.dequeue which are almost similar while collections have fast append(), pop(). You can change your code first line to q=collections.deque([],800) and in place of q.put,q.get use q.append ,q.popleft().

rakesh
  • 4,368
  • 1
  • 19
  • 13
  • rakesh, thanks! But what q.popleft(). does do? it deletes and reads or reads only? – Alex Skys Sep 29 '16 at 07:55
  • I tried to change my line where is the q=, but i get the mistake NameError: name 'collections' is not defined – Alex Skys Sep 29 '16 at 07:58
  • You have to import the module first by executing the statement ``import collections``. – Schmuddi Sep 29 '16 at 16:57
  • This function q.popleft() does 2 opperations: remove and return, although i need return only. Does someone knows about this function in queue – Alex Skys Oct 01 '16 at 10:09
  • collections.queue isn't that i need, because it deletes the elements which have more low number, right? I would specify time of addition for every element. It needs to deleting of element in time order – Alex Skys Oct 09 '16 at 09:25
  • You can use access dequeue like lists.If you need return only you can use q[index]. Also if you want to delete in time order then you should add it in that order. If you add using q.append(num), then you can get it using q[index] or pop it using q.pop() – rakesh Dec 09 '16 at 08:02