简体   繁体   中英

python trouble with queue,dict,floats and multiple files

i have a problem with a float value and a queue of dicts. I really don't know whats happening. Let me show to be clear:

Here is a file called cqueue.py:

from collections import deque
def DestroyQueue(q):
        while LenQueue(q) >0:
                q.popleft()

def GetQueue(q):
        item=q.popleft()
        AddQueue(q,item)
        return item

def AddQueue(q,tdict={}):
        q.append(tdict)

def LenQueue(q):
        return len(q)


rcv_queue= deque()
can_queue= deque()

After some processing data, i have rcv_queue filled with lots of dicts. Each dict has many values (around 30 pairs or more).

Now, in a new file called "match.py", i have a routine like this:

qlen=LenQueue(rcv_queue)
while qlen>0:
    pdict = {}
    pdict = GetQueue(rcv_queue)
    #THINGS
    if something
        AddQueue(can_queue,pdict)
    qlen-=1
DestroyQueue(rcv_queue)
DbSimCases()

this "#THINGS" is about processing each dict and each pair of data, the new dict (pdict) is stored on can_queue, which has lower size (around 5 dict with something over 50 pairs). DestroyQueue is called to pop all dict and destroy the rcv_queue, which is not used anymore. So DbSimCases() is called to catch can_queue (from cqueue.py) and store on mysql database. The problem is: On match.py, everything is fine with the dict float values, but when can_queue is called from DbSimCases (a third file called base.py), float values are changed. To spot that problem i used print like this:

---on match.py

qlen=LenQueue(rcv_queue)
while qlen>0:
    pdict = {}
    pdict = GetQueue(rcv_queue)
    #THINGS
    if something
        AddQueue(can_queue,pdict)
        o=GetQueue(can_queue)
        print "pdict->args_score: " + str(p_args_score)
        print "queue->args_score: " + str(o['p_args_score'])
    qlen-=1
DestroyQueue(rcv_queue)
DbSimCases()

------/end

---on base.py

def DbSimCases()
    clen=LenQueue(can_queue)
    while clen > 0:
        pdict2={}
        pdict2=GetQueue(can_queue)
        print "pdict2->args_score: " + str(pdict2['p_args_score'])

So the result should be the same float 3 times. BUT i got this:

pdict->args_score: 1.8
queue->args_score: 1.8
pdict2->args_score: 0.0

Here the entire pdict and pdict2 (sorted and matching, line by line, to see many problems like described above): http://pastebin.com/ZffTMGxq

Someone could help me? Any clue what i'm doing wrong ? Thanks, every help will be VERY appreciated.

I fixed.

I create a third dict on match.py like this:

qlen=LenQueue(rcv_queue)
while qlen>0:
  pdict = {}
  pdict = GetQueue(rcv_queue)
  auxdict={}
  #THINGS
  if something
    auxdict['key']=pdict['key']
    AddQueue(can_queue, auxdict)
  qlen-=1
DestroyQueue(rcv_queue)
DbSimCases()

It act as a auxiliary dict and has just a few selected dicts. Pdict has many things i dont want, and using the same dict to pass to the new one, was a buggy decision and pratice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM