Skip to content

Instantly share code, notes, and snippets.

@josh04
Created October 30, 2023 09:35
Show Gist options
  • Save josh04/aee7617cbc06e44459751de656c93607 to your computer and use it in GitHub Desktop.
Save josh04/aee7617cbc06e44459751de656c93607 to your computer and use it in GitHub Desktop.
Python script for turning a list of timestamps into the values used by ros_randomise_i.lua (and shuffling them)
import json
import random
json_str = '''["00:00:00.000",
"00:00:08.840",
"00:04:32.840",
"00:06:34.520",
"00:09:22.440",
"00:11:01.960",
"00:12:36.160",
"00:15:12.720",
"00:16:15.880",
"00:17:35.880",
"00:19:26.920",
"00:21:33.360",
"00:23:31.840",
"00:27:21.160",
"00:29:03.960",
"00:32:22.160",
"00:37:17.080",
"00:42:51.480",
"00:44:25.000",
"00:46:32.640",
"00:49:21.640",
"00:55:19.360",
"00:59:20.680",
"01:03:16.200",
"01:06:36.000",
"01:09:58.840",
"01:13:03.720",
"01:18:07.000",
"01:23:11.600",
"01:26:25.000",
"01:29:37.040",
"01:34:03.360",
"01:37:41.120",
"01:39:24.680",
"01:41:45.920",
"01:44:04.720",
"01:47:16.840",
"01:51:44.840",
"01:53:42.920",
"01:57:06.440",
"01:59:34.880",
"02:03:08.440",
"02:07:19.960",
"02:09:56.480",
"02:19:00.200",
"02:21:51.520"]'''
json_obj = json.loads(json_str)
print(json_obj[-1])
end_stamp = json_obj[-1]
def milliseconds_from_timestamp(timestamp):
main, milli = timestamp.split('.')
time = main.split(':')
total = int(time[0]) * 60 * 60 * 1000 + int(time[1]) * 60 * 1000 + int(time[2]) * 1000 + int(milli)
return total
end_int = milliseconds_from_timestamp(end_stamp)
out_arr = []
for previous, current in zip(json_obj, json_obj[1:]):
out_arr.append({"start": milliseconds_from_timestamp(previous) / end_int, "endval": milliseconds_from_timestamp(current) / end_int});
random.shuffle(out_arr)
obj = {"vals": out_arr}
print(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment