Commit ba49bc50 authored by Pierre Fernbach's avatar Pierre Fernbach
Browse files

[Scripts] use context manager for opening files

parent 6fc60fcd
......@@ -50,10 +50,9 @@ class PathPlanner(AnymalPathPlanner):
self.ps.addGoalConfig(self.q_goal)
self.alpha = alpha
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -34,10 +34,9 @@ class PathPlanner(AnymalPathPlanner):
self.ps.addGoalConfig(self.q_goal)
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -10,17 +10,14 @@ class AnymalContactGenerator(Parent):
def __init__(self, path_planner):
super().__init__(path_planner)
self.status_filename = self.path_planner.status_filename
f = open(self.status_filename, "a")
if self.path_planner.ps.numberPaths() > 0:
print("Path planning OK.")
f.write("Planning_success: True" + "\n")
f.close()
else:
print("Error during path planning")
f.write("Planning_success: False" + "\n")
f.close()
import sys
sys.exit(1)
with open(self.status_filename, "a") as f:
if self.path_planner.ps.numberPaths() > 0:
print("Path planning OK.")
f.write("Planning_success: True" + "\n")
else:
print("Error during path planning")
f.write("Planning_success: False" + "\n")
sys.exit(1)
def write_status(self, max_configs, throw_if_goal_not_reached=True):
if len(self.configs) < 2:
......@@ -44,11 +41,10 @@ class AnymalContactGenerator(Parent):
else:
cg_too_many_states = False
f = open(self.status_filename, "a")
f.write("cg_success: " + str(cg_success) + "\n")
f.write("cg_reach_goal: " + str(cg_reach_goal) + "\n")
f.write("cg_too_many_states: " + str(cg_too_many_states) + "\n")
f.close()
with open(self.status_filename, "a") as f:
f.write("cg_success: " + str(cg_success) + "\n")
f.write("cg_reach_goal: " + str(cg_reach_goal) + "\n")
f.write("cg_too_many_states: " + str(cg_too_many_states) + "\n")
if (not cg_success) or cg_too_many_states:
sys.exit(1)
if throw_if_goal_not_reached and not cg_reach_goal:
......
......@@ -61,10 +61,9 @@ class PathPlanner(AnymalPathPlanner):
self.ps.addGoalConfig(self.q_goal)
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -100,10 +100,9 @@ class PathPlanner(TalosPathPlanner):
print(("init config : ", self.q_init))
print(("goal config : ", self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -49,10 +49,9 @@ class PathPlanner(TalosPathPlanner):
self.ps.addGoalConfig(self.q_goal)
self.alpha = alpha
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -31,10 +31,9 @@ class PathPlanner(TalosPathPlanner):
self.ps.addGoalConfig(self.q_goal)
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
from hpp.corbaserver.rbprm.scenarios.talos_contact_generator import TalosContactGenerator as Parent
import sys
import sys
class TalosContactGenerator(Parent):
......@@ -11,17 +10,14 @@ class TalosContactGenerator(Parent):
def __init__(self, path_planner):
super().__init__(path_planner)
self.status_filename = self.path_planner.status_filename
f = open(self.status_filename, "a")
if self.path_planner.ps.numberPaths() > 0:
print("Path planning OK.")
f.write("Planning_success: True" + "\n")
f.close()
else:
print("Error during path planning")
f.write("Planning_success: False" + "\n")
f.close()
import sys
sys.exit(1)
with open(self.status_filename, "a") as f:
if self.path_planner.ps.numberPaths() > 0:
print("Path planning OK.")
f.write("Planning_success: True" + "\n")
else:
print("Error during path planning")
f.write("Planning_success: False" + "\n")
sys.exit(1)
def write_status(self, max_configs):
if len(self.configs) < 2:
......@@ -45,10 +41,9 @@ class TalosContactGenerator(Parent):
else:
cg_too_many_states = False
f = open(self.status_filename, "a")
f.write("cg_success: " + str(cg_success) + "\n")
f.write("cg_reach_goal: " + str(cg_reach_goal) + "\n")
f.write("cg_too_many_states: " + str(cg_too_many_states) + "\n")
f.close()
with open(self.status_filename, "a") as f:
f.write("cg_success: " + str(cg_success) + "\n")
f.write("cg_reach_goal: " + str(cg_reach_goal) + "\n")
f.write("cg_too_many_states: " + str(cg_too_many_states) + "\n")
if (not cg_success) or cg_too_many_states or (not cg_reach_goal):
sys.exit(1)
......@@ -13,10 +13,9 @@ class PathPlanner(Parent):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
if __name__ == "__main__":
......
......@@ -32,10 +32,9 @@ class PathPlanner(TalosPathPlanner):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -13,10 +13,9 @@ class PathPlanner(Parent):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
if __name__ == "__main__":
......
......@@ -28,10 +28,9 @@ class PathPlanner(TalosPathPlanner):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -70,10 +70,9 @@ class PathPlanner(TalosPathPlanner):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -60,10 +60,9 @@ class PathPlanner(TalosPathPlanner):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def run(self):
self.init_problem()
......
......@@ -84,10 +84,9 @@ class PathPlanner(TalosPathPlanner):
print("q_init= " + str(self.q_init))
print("q_goal= " + str(self.q_goal))
# write problem in files :
f = open(self.status_filename, "w")
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
f.close()
with open(self.status_filename, "w") as f:
f.write("q_init= " + str(self.q_init) + "\n")
f.write("q_goal= " + str(self.q_goal) + "\n")
def init_problem(self):
super().init_problem()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment