You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I defined a class named CodeLine to load code from a file, create Code mobject for what i get. Then I add a arrow mobject to markup which line of those code are running (just like debug) to show the running process of the code.
I made some method for my class. CodeLine.move_up makes arrow to previous line of code, CodeLine.move_down makes arrow to next line of code. Looks like they actually work. Then I create a method named CodeLine.jump_to that I want to use this method to move arrow to specified row. Obviously, for this method, I must define a attribute to record current line number. When I jump to a line, current line number must be changed.
I use ApplyMethod and CodeLine.shift to move the arrow and change current line number at same time. But in this time, something wrong! The arrow moved but current line number wasn't changed. There are the part of origin code.
frommanimlibimport*classCodeLine(VGroup):
def__init__(self, code_path: str, **kwargs):
self.code_path=code_pathself.line_height=0.306self.code_content, self.total_lines=self.load_code_file()
self.code_mob=Code(self.code_content)
self.arrow_mob=Arrow(self.code_mob.get_corner(LEFT) +LEFT, self.code_mob.get_corner(LEFT))
ifself.total_lines%2==0:
self.arrow_mob.shift(DOWN*0.145+UP*self.line_height* (self.total_lines//2))
else:
self.arrow_mob.shift(UP*self.line_height* (self.total_lines//2))
self.current_line_number=1super().__init__(self.code_mob, self.arrow_mob)
defload_code_file(self):
withopen(self.code_path) asf:
content=f.readlines()
total_lines=len(content)
content="".join(iforiincontent)
return (content, total_lines)
defmove_up(self):
ifself.current_line_number>1:
self.current_line_number-=1returnself.arrow_mob.shift(self.line_height*UP)
else:
log.warning(f"You can't move the arrow to previous line because persent line number"+ \
f"is {self.current_line_number}.")
returnNonedefmove_down(self):
ifself.current_line_number<self.total_lines:
self.current_line_number+=1returnself.arrow_mob.shift(self.line_height*DOWN)
else:
log.warning(f"You can't move the arrow to next line because persent line number"+ \
f"is {self.current_line_number}.")
returnNonedefjump_to(self, line_number: int):
ifline_number<=self.total_linesorline_number>=1:
diff=line_number-self.current_line_numberlog.info(f"{line_number} - {self.current_line_number} = {diff}")
self.current_line_number=line_numberifdiff<0:
returnself.arrow_mob.shift(UP*abs(diff) *self.line_height)
elifdiff>0:
returnself.arrow_mob.shift(DOWN*abs(diff) *self.line_height)
else:
log.warning(f"You can't jump to out of range of code lines number")
returnNone
self.current_line_number is not changed!
The first jump_to will move arrow to line 4, but the second will move arrow to line 6. The reason is when arrow moved, the arrtibute of self.current_line_number is not change to line_number. So the value of current_line_number still is 1 when second jump_to applied and it will cause that method think you want to move down the arrow 2 lines (diff = line_number - self.current_line_number is 2).
The reason what I found is...
I found the reason out! The reason is when ApplyMethod be called, it will create a copy of mobject, so, by the reason, the copy of CodeLine's attribute has been changed but not the origin CodeLine which we used.
For deal with it, I try to use class attributes instead of instance attributes. The code that be changed to...
frommanimlibimport*classCodeLine(VGroup):
current_line_number=1# * changeddef__init__(self, code_path: str, **kwargs):
self.code_path=code_pathself.line_height=0.306self.code_content, self.total_lines=self.load_code_file()
self.code_mob=Code(self.code_content)
self.arrow_mob=Arrow(self.code_mob.get_corner(LEFT) +LEFT, self.code_mob.get_corner(LEFT))
ifself.total_lines%2==0:
self.arrow_mob.shift(DOWN*0.145+UP*self.line_height* (self.total_lines//2))
else:
self.arrow_mob.shift(UP*self.line_height* (self.total_lines//2))
# self.current_line_number = 1 # * changedsuper().__init__(self.code_mob, self.arrow_mob)
defload_code_file(self):
withopen(self.code_path) asf:
content=f.readlines()
total_lines=len(content)
content="".join(iforiincontent)
return (content, total_lines)
defmove_up(self):
ifself.current_line_number>1:
self.current_line_number-=1returnself.arrow_mob.shift(self.line_height*UP)
else:
log.warning(f"You can't move the arrow to previous line because persent line number"+ \
f"is {self.current_line_number}.")
returnNonedefmove_down(self):
ifself.current_line_number<self.total_lines:
self.current_line_number+=1returnself.arrow_mob.shift(self.line_height*DOWN)
else:
log.warning(f"You can't move the arrow to next line because persent line number"+ \
f"is {self.current_line_number}.")
returnNonedefjump_to(self, line_number: int):
ifline_number<=self.total_linesorline_number>=1:
diff=line_number-self.current_line_numberlog.info(f"{line_number} - {self.current_line_number} = {diff}")
CodeLine.current_line_number=line_number# * changedifdiff<0:
returnself.arrow_mob.shift(UP*abs(diff) *self.line_height)
elifdiff>0:
returnself.arrow_mob.shift(DOWN*abs(diff) *self.line_height)
else:
log.warning(f"You can't jump to out of range of code lines number")
returnNone
By the way, problem has benn solved. But I don't think that is a good way, Because that will cause that I can't to create second CodeLine instance, the CodeLine become to a singleton class... That is too bad. But I can't find another way to avoid that problem...
What can I do? Can I change the manim's origin code to make it works? Or mine?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Situation:
I defined a class named CodeLine to load code from a file, create Code mobject for what i get. Then I add a arrow mobject to markup which line of those code are running (just like debug) to show the running process of the code.
I made some method for my class.
CodeLine.move_upmakes arrow to previous line of code,CodeLine.move_downmakes arrow to next line of code. Looks like they actually work. Then I create a method namedCodeLine.jump_tothat I want to use this method to move arrow to specified row. Obviously, for this method, I must define a attribute to record current line number. When I jump to a line, current line number must be changed.I use
ApplyMethodandCodeLine.shiftto move the arrow and change current line number at same time. But in this time, something wrong! The arrow moved but current line number wasn't changed. There are the part of origin code.What happend
self.current_line_numberis not changed!The first
jump_towill move arrow to line 4, but the second will move arrow to line 6. The reason is when arrow moved, the arrtibute ofself.current_line_numberis not change toline_number. So the value ofcurrent_line_numberstill is 1 when secondjump_toapplied and it will cause that method think you want to move down the arrow 2 lines (diff = line_number - self.current_line_numberis 2).The reason what I found is...
I found the reason out! The reason is when
ApplyMethodbe called, it will create a copy of mobject, so, by the reason, the copy of CodeLine's attribute has been changed but not the origin CodeLine which we used.For deal with it, I try to use class attributes instead of instance attributes. The code that be changed to...
By the way, problem has benn solved. But I don't think that is a good way, Because that will cause that I can't to create second CodeLine instance, the CodeLine become to a singleton class... That is too bad. But I can't find another way to avoid that problem...
What can I do? Can I change the manim's origin code to make it works? Or mine?
Beta Was this translation helpful? Give feedback.
All reactions