def create_times_from_collision_json(collision_json):
"""
Obtains all the times at which all the cars stored in the collision json -string entered the intersection (or where
created). The json must have the form {"time":<string with specified format>, "message":{"collision_code":<string>,
"collision_initial_conditions":<list of cars>}}. Also returns the time at which the collision in the jason should
start. Time format: "%Y-%m-%d %H:%M:%S,%f".
:param collision_json: <string >json with a collision report.
:return: dictionary with the cars. The key value is the name of the car.
"""
collision_information = JSONDecoder().decode(collision_json)
json_cars = collision_information["message"]["collision_initial_conditions"]
time_format = '%Y-%m-%d %H:%M:%S,%f'
collision_time = datetime.strptime(collision_information["time"], time_format)
car_creation_times = []
for json_car in json_cars:
car_creation_times.append((datetime.fromtimestamp(json_car["creation_time"]), json_car["car_name"]))
car_creation_times.sort(key=lambda x: x[0])
start_simulation_time = collision_time
for car_time in car_creation_times:
if car_time[0] < start_simulation_time:
start_simulation_time = car_time[0]
return start_simulation_time, car_creation_times
recreate_collisions.py 文件源码
python
阅读 28
收藏 0
点赞 0
评论 0
评论列表
文章目录