Skip to content

Instantly share code, notes, and snippets.

@csm10495
Created May 8, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csm10495/059f6b6cbe66221491bd950dddb38682 to your computer and use it in GitHub Desktop.
Save csm10495/059f6b6cbe66221491bd950dddb38682 to your computer and use it in GitHub Desktop.
Script to make a Python dictionary from C++ enum
"""
Brief:
This file can be used to make a Python dict from a c++ enum.
Simply copy-paste the enum from typedef to end as input and
out.txt will be created with it as a Python dict.
Author:
Charles Machalow
"""
try:
raw_input
except:
raw_input = input
with open('out.txt', 'w') as f:
currentItemValue = 0
while True:
line = raw_input()
if line.strip() != "quit":
if 'typedef' in line:
structName = line.replace("typedef enum", "").replace("{", "").strip().strip("_")
f.write("%s = {\n" % structName)
elif '}' in line:
f.write("}\n")
break
else:
line = line.replace(',', '')
spEquals = list(map(str.strip, line.split("=")))
if len(spEquals) == 2:
name, val = spEquals
if '0x' in val:
val = int(val, 16)
else:
val = int(val)
f.write(' 0x%02X : \"%s\",\n' % (val, name))
currentItemValue = val
else:
name = line.strip()
f.write(' 0x%02X : \"%s\",\n' % (currentItemValue, name))
currentItemValue += 1
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment