Skip to content

Instantly share code, notes, and snippets.

@andyfangdz
Created July 8, 2018 07:13
Show Gist options
  • Save andyfangdz/d4d52daa9f8a75223e76e92657036bb0 to your computer and use it in GitHub Desktop.
Save andyfangdz/d4d52daa9f8a75223e76e92657036bb0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"thrift_defs = \"\"\"\n",
" T_STOP = 0,\n",
" T_VOID = 1,\n",
" T_BOOL = 2,\n",
" T_BYTE = 3,\n",
" T_I08 = 3,\n",
" T_I16 = 6,\n",
" T_I32 = 8,\n",
" T_U64 = 9,\n",
" T_I64 = 10,\n",
" T_DOUBLE = 4,\n",
" T_STRING = 11,\n",
" T_UTF7 = 11,\n",
" T_STRUCT = 12,\n",
" T_MAP = 13,\n",
" T_SET = 14,\n",
" T_LIST = 15,\n",
" T_UTF8 = 16,\n",
" T_UTF16 = 17\n",
"\"\"\"\n",
"ttype_lines = [ttype_line.strip() for ttype_line in thrift_defs.split(',')]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"import collections\n",
"\n",
"multidict = lambda *args, **kwargs: collections.defaultdict(list, *args, **kwargs)\n",
"ttypesdict = lambda *args, **kwargs: collections.defaultdict(lambda: ['T_UNKNOWN'], *args, **kwargs)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"typesdict = multidict()\n",
"for line in ttype_lines:\n",
" parts = line.split('=')\n",
" typesdict[int(parts[1].strip())].append(parts[0].strip())"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"finaldict = ttypesdict(typesdict)\n",
"last_ttype = 17\n",
"\n",
"output = \"\"\"\n",
"std::string TTypeNames[] = {{\n",
" {}\n",
"}}\n",
"\"\"\".format(', '.join('\"{}\"'.format(' or '.join(finaldict[i])) for i in range(last_ttype + 1)))"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"std::string TTypeNames[] = {\n",
" \"T_STOP\", \"T_VOID\", \"T_BOOL\", \"T_BYTE or T_I08\", \"T_DOUBLE\", \"T_UNKNOWN\", \"T_I16\", \"T_UNKNOWN\", \"T_I32\", \"T_U64\", \"T_I64\", \"T_STRING or T_UTF7\", \"T_STRUCT\", \"T_MAP\", \"T_SET\", \"T_LIST\", \"T_UTF8\", \"T_UTF16\"\n",
"}\n",
"\n"
]
}
],
"source": [
"print(output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment