You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpp2python.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Mar 20 11:09:04 2020
  5. @author: ljia
  6. """
  7. import re
  8. def convert_function(cpp_code):
  9. # f_cpp = open('cpp_code.cpp', 'r')
  10. # # f_cpp = open('cpp_ext/src/median_graph_estimator.ipp', 'r')
  11. # cpp_code = f_cpp.read()
  12. python_code = cpp_code.replace('else if (', 'elif ')
  13. python_code = python_code.replace('if (', 'if ')
  14. python_code = python_code.replace('else {', 'else:')
  15. python_code = python_code.replace(') {', ':')
  16. python_code = python_code.replace(';\n', '\n')
  17. python_code = re.sub('\n(.*)}\n', '\n\n', python_code)
  18. # python_code = python_code.replace('}\n', '')
  19. python_code = python_code.replace('throw', 'raise')
  20. python_code = python_code.replace('error', 'Exception')
  21. python_code = python_code.replace('"', '\'')
  22. python_code = python_code.replace('\\\'', '"')
  23. python_code = python_code.replace('try {', 'try:')
  24. python_code = python_code.replace('true', 'True')
  25. python_code = python_code.replace('false', 'False')
  26. python_code = python_code.replace('catch (...', 'except')
  27. # python_code = re.sub('std::string\(\'(.*)\'\)', '$1', python_code)
  28. return python_code
  29. # # python_code = python_code.replace('}\n', '')
  30. # python_code = python_code.replace('option.first', 'opt_name')
  31. # python_code = python_code.replace('option.second', 'opt_val')
  32. # python_code = python_code.replace('ged::Error', 'Exception')
  33. # python_code = python_code.replace('std::string(\'Invalid argument "\')', '\'Invalid argument "\'')
  34. # f_cpp.close()
  35. # f_python = open('python_code.py', 'w')
  36. # f_python.write(python_code)
  37. # f_python.close()
  38. def convert_function_comment(cpp_fun_cmt, param_types):
  39. cpp_fun_cmt = cpp_fun_cmt.replace('\t', '')
  40. cpp_fun_cmt = cpp_fun_cmt.replace('\n * ', ' ')
  41. # split the input comment according to key words.
  42. param_split = None
  43. note = None
  44. cmt_split = cpp_fun_cmt.split('@brief')[1]
  45. brief = cmt_split
  46. if '@param' in cmt_split:
  47. cmt_split = cmt_split.split('@param')
  48. brief = cmt_split[0]
  49. param_split = cmt_split[1:]
  50. if '@note' in cmt_split[-1]:
  51. note_split = cmt_split[-1].split('@note')
  52. if param_split is not None:
  53. param_split.pop()
  54. param_split.append(note_split[0])
  55. else:
  56. brief = note_split[0]
  57. note = note_split[1]
  58. # get parameters.
  59. if param_split is not None:
  60. for idx, param in enumerate(param_split):
  61. _, param_name, param_desc = param.split(' ', 2)
  62. param_name = function_comment_strip(param_name, ' *\n\t/')
  63. param_desc = function_comment_strip(param_desc, ' *\n\t/')
  64. param_split[idx] = (param_name, param_desc)
  65. # strip comments.
  66. brief = function_comment_strip(brief, ' *\n\t/')
  67. if note is not None:
  68. note = function_comment_strip(note, ' *\n\t/')
  69. # construct the Python function comment.
  70. python_fun_cmt = '"""'
  71. python_fun_cmt += brief + '\n'
  72. if param_split is not None and len(param_split) > 0:
  73. python_fun_cmt += '\nParameters\n----------'
  74. for idx, param in enumerate(param_split):
  75. python_fun_cmt += '\n' + param[0] + ' : ' + param_types[idx]
  76. python_fun_cmt += '\n\t' + param[1] + '\n'
  77. if note is not None:
  78. python_fun_cmt += '\nNote\n----\n' + note + '\n'
  79. python_fun_cmt += '"""'
  80. return python_fun_cmt
  81. def function_comment_strip(comment, bad_chars):
  82. head_removed, tail_removed = False, False
  83. while not head_removed or not tail_removed:
  84. if comment[0] in bad_chars:
  85. comment = comment[1:]
  86. head_removed = False
  87. else:
  88. head_removed = True
  89. if comment[-1] in bad_chars:
  90. comment = comment[:-1]
  91. tail_removed = False
  92. else:
  93. tail_removed = True
  94. return comment
  95. if __name__ == '__main__':
  96. # python_code = convert_function("""
  97. # if (print_to_stdout_ == 2) {
  98. # std::cout << "\n===========================================================\n";
  99. # std::cout << "Block gradient descent for initial median " << median_pos + 1 << " of " << medians.size() << ".\n";
  100. # std::cout << "-----------------------------------------------------------\n";
  101. # }
  102. # """)
  103. python_fun_cmt = convert_function_comment("""
  104. /*!
  105. * @brief Returns the sum of distances.
  106. * @param[in] state The state of the estimator.
  107. * @return The sum of distances of the median when the estimator was in the state @p state during the last call to run().
  108. */
  109. """, ['string', 'string'])

A Python package for graph kernels, graph edit distances and graph pre-image problem.