Created
June 2, 2020 16:22
-
-
Save gokhansolak/314a4fa5bc9fa9c9dc415a8ba47aa2cb to your computer and use it in GitHub Desktop.
This script will renumerate the item indexes in a YAML dictionary.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This script will renumerate the item indexes in a | |
# YAML dictionary. ROS currently does not support easy | |
# acquisition of list of objects from parameter server, | |
# so I prefer using indexed dictionaries. However, it | |
# is tiring to index them manually. This script does it. | |
# Example yaml file (iter_prefix = 'p'): | |
# https://github.com/gokhansolak/lfd-experiments-iros2019/blob/master/data/grasp/pregrasp.yaml | |
import os, argparse, sys | |
import re | |
def renumerate_param_list(filename, it_prefix='i'): | |
it_ptr = r'( +'+it_prefix+')\d*:' | |
regex_obj = re.compile(it_ptr) | |
new_lines = [] | |
with open(filename, 'r') as f: | |
old_lines = f.readlines() | |
enum_index = 0 | |
for line in old_lines: | |
new_line = regex_obj.sub(r'\g<1>'+str(enum_index)+':', line) | |
# changed? increment | |
if regex_obj.match(line): | |
enum_index += 1 | |
new_lines.append(new_line) | |
root, ext = os.path.splitext(filename) | |
with open(root+'_enum'+ext, 'w') as f: | |
f.writelines(new_lines) | |
return enum_index | |
if __name__ == '__main__': | |
parser=argparse.ArgumentParser() | |
parser.add_argument('-f', '--filename', help='Name of the file incl. extension', required="True") | |
parser.add_argument('-i', '--iter_prefix', help='Prefix of the iterator. E.g., k for k0, k1, k2...', default="i") | |
args = parser.parse_args() | |
last_index = renumerate_param_list(args.filename, args.iter_prefix) | |
print('Renumerated '+str(last_index)+' items.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment