[英]Python dictionary swapping element value through iteration
我正在構建像python這樣的視頻編輯器。 我創建了使用字典存儲動畫對象的時間軸。 當我進入渲染階段時,我必須渲染電影,這是場景的重要時間軸,也是時間軸。 因此,每部電影至少有2本字典。 當我遍歷場景時,我遍歷字典並渲染場景:
timeline = self.tl.timeline_object.items() # get the items of the dictionary
for scene in timeline:
print(timeline, id(timeline))
scene[1]() # do all the render stuff
得到以下結果:
dict_items([('1', <class 'test_movie.first_scene.FirstScene'>), ('2', <class 'test_movie.second_scene.SecondScene'>)]) 140043388706584
dict_items([('1', <animations.FillScreen.FillScreen object at 0x7f5e643a67f0>), ('2', <animations.Write.Write object at 0x7f5e643a69b0>)]) 140043388706584
如您所見,電影和場景之間的字典交換(請看ID)。
我應該得到第一行:
dict_items([('1', <class 'test_movie.first_scene.FirstScene'>), ('2', <class 'test_movie.second_scene.SecondScene'>)]) 140043388706584
對於第二次迭代。
為什么我要獲取場景的字典,而不是存儲在timeline
中的“真實”字典? 以及如何解決這個問題?
代碼很長,涉及很多對象,我試圖向您展示最大數量而不會丟失您。 如果您要實際回購: https : //gitlab.com/planchon/psVidTex
否則,以下是重要文件:
class MovieName(Movie):
def __init__(self):
Movie.__init__(self)
# add the scene to the movie timeline (dictionnary + time array)
def prepare(self):
self.add_to_timeline(FirstScene, 0, 10)
self.add_to_timeline(SecondScene, 10, 20)
class Timeline(object):
timeline_object = {}
timeline = []
timeline_index = []
max_id = 0
def __init__(self):
pass
# ajoute un element dans la timeline retourne la timeline_id de l'object
# prend des frames comme unité de mesure du temps
def add_object(self, animation, start, end, position):
animation_id = self.get_global_id()
self.timeline_object[animation_id] = animation
self.add_into_timeline(start, [start, end, position, animation_id])
return animation_id
# add the tuple [start, end, position, animation_id] into the timeline
def add_into_timeline(self, start, element):
index = bisect.bisect(self.timeline_index, start)
self.timeline_index.insert(index, start)
self.timeline.insert(index, element)
# get the next id
def get_global_id(self):
self.max_id = self.max_id + 1
return str(self.max_id)
class FirstScene(Scene):
def __init__(self):
Scene.__init__(self)
def prepare(self):
self.add_to_timeline(FillScreen("col"), 0, 10)
self.add_to_timeline(Write("test"), 0, 10)
class Movie(object):
tl = Timeline()
def __init__(self):
self.prepare()
self.init_all_scene()
def render(self):
pass
def prepare(self):
pass
def init_all_scene(self):
timeline = self.tl.timeline_object.items()
for scene in timeline:
print(timeline, id(timeline))
scene[1]()
# add the scene to the timeline
def add_to_timeline(self, scene, start, end):
return self.tl.add_object(scene, start, end, 0)
class Scene(Container):
tl = Timeline()
def __init__(self, **kargs):
self.prepare()
def add_to_timeline(self, anim, start, end):
return self.tl.add_object(anim, start, end, 0)
def prepare(self):
pass
def render(self):
pass
在這種情況下, Write
和FillScreen
不是相關的對象。 如有必要,您可以在存儲庫中找到它們。
問題在於,Timeline類未正確初始化,並且timeline_object
在所有不同的時間軸之間共享。
所以要解決,我只是修改了Timeline類:
class Timeline(object):
def __init__(self):
self.timeline_object = {}
self.timeline = []
self.timeline_index = []
self.max_id = 0
通過更改數組在init中的位置,可以使它們位於對象而非元對象的本地。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.