2017-09-04 96 views
0

輸入:重新編碼命名的元組

import mouse 
import pickle 
from collections import namedtuple 

#https://github.com/boppreh/keyboard/blob/master/keyboard/mouse.py 

#from ._mouse_event import ButtonEvent, MoveEvent, WheelEvent, LEFT, RIGHT, 
MIDDLE, X, X2, UP, DOWN, DOUBLE 


ButtonEvent = namedtuple('ButtonEvent', ['event_type', 'button', 'time']) 
WheelEvent = namedtuple('WheelEvent', ['delta', 'time']) 
MoveEvent = namedtuple('MoveEvent', ['x', 'y', 'time']) 

with open ('outfile', 'rb') as fp: 
    itemlist = pickle.load(fp) 

#print (type (itemlist)) 
print (itemlist[:3]) 

z=0 
xx=50 
yy=10 
itemlist2=[] 

for i in itemlist: 
    old_event = itemlist[z] 
    new_event = MoveEvent(x=xx, y=yy, time=old_event.time) 
    itemlist2.append(new_event) 
    z=z+1 
    xx=xx+1 
    yy=yy+1 
print() 

print (itemlist2[:3]) 

print (type (itemlist)) 
print (type (itemlist[0])) 
print (type (itemlist[0][0])) 

print() 

print (type (itemlist2)) 
print (type (itemlist2[0])) 
print (type (itemlist2[0][0])) 

輸出:

[MoveEvent(x=1369, y=162, time=1504533266.029788), MoveEvent(x=1358, y=165, time=1504533266.0377884), MoveEvent(x=1350, y=167, time=1504533266.045789)] 

[MoveEvent(x=50, y=10, time=1504533266.029788), MoveEvent(x=51, y=11, time=1504533266.0377884), MoveEvent(x=52, y=12, time=1504533266.045789)] 
<class 'list'> 
<class 'mouse._mouse_event.MoveEvent'> 
<class 'int'> 

<class 'list'> 
<class '__main__.MoveEvent'> 
<class 'int'> 

新來這裏 - 蟒蛇 當我重新組裝命名元組的名單爲itemlist2我需要保持 -class mouse._mouse_event.MoveEvent - - 不知道該怎麼做

我想我也可以嘗試編輯'itemlist'直接if這是可能的

時打印的清單長得一模一樣(除了變更值)

回答

0

你創造了文件的新MoveEvent namedtuple類型:

MoveEvent = namedtuple('MoveEvent', ['x', 'y', 'time']) 

如果你想使用原來,你需要使用mouse._mouse_event.MoveEvent或以上將其重命名:

MoveEvent = mouse._mouse_event.MoveEvent 

因爲mouse.py包括這一行:

from ._mouse_event import ButtonEvent, MoveEvent, WheelEvent, LEFT, RIGHT, MIDDLE, X, X2, UP, DOWN, DOUBLE 

你或許可以使用此代碼:

import mouse 
from mouse import MoveEvent