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.

misc.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Mar 19 18:13:56 2020
  5. @author: ljia
  6. """
  7. def options_string_to_options_map(options_string):
  8. """Transforms an options string into an options map.
  9. Parameters
  10. ----------
  11. options_string : string
  12. Options string of the form "[--<option> <arg>] [...]".
  13. Return
  14. ------
  15. options_map : dict{string : string}
  16. Map with one key-value pair (<option>, <arg>) for each option contained in the string.
  17. """
  18. if options_string == '':
  19. return
  20. options_map = {}
  21. words = []
  22. tokenize(options_string, ' ', words)
  23. expect_option_name = True
  24. for word in words:
  25. if expect_option_name:
  26. is_opt_name, word = is_option_name(word)
  27. if is_opt_name:
  28. option_name = word
  29. if option_name in options_map:
  30. raise Exception('Multiple specification of option "' + option_name + '".')
  31. options_map[option_name] = ''
  32. else:
  33. raise Exception('Invalid options "' + options_string + '". Usage: options = "[--<option> <arg>] [...]"')
  34. else:
  35. is_opt_name, word = is_option_name(word)
  36. if is_opt_name:
  37. raise Exception('Invalid options "' + options_string + '". Usage: options = "[--<option> <arg>] [...]"')
  38. else:
  39. options_map[option_name] = word
  40. expect_option_name = not expect_option_name
  41. return options_map
  42. def tokenize(sentence, sep, words):
  43. """Separates a sentence into words separated by sep (unless contained in single quotes).
  44. Parameters
  45. ----------
  46. sentence : string
  47. The sentence that should be tokenized.
  48. sep : string
  49. The separator. Must be different from "'".
  50. words : list[string]
  51. The obtained words.
  52. """
  53. outside_quotes = True
  54. word_length = 0
  55. pos_word_start = 0
  56. for pos in range(0, len(sentence)):
  57. if sentence[pos] == '\'':
  58. if not outside_quotes and pos < len(sentence) - 1:
  59. if sentence[pos + 1] != sep:
  60. raise Exception('Sentence contains closing single quote which is followed by a char different from ' + sep + '.')
  61. word_length += 1
  62. outside_quotes = not outside_quotes
  63. elif outside_quotes and sentence[pos] == sep:
  64. if word_length > 0:
  65. words.append(sentence[pos_word_start:pos_word_start + word_length])
  66. pos_word_start = pos + 1
  67. word_length = 0
  68. else:
  69. word_length += 1
  70. if not outside_quotes:
  71. raise Exception('Sentence contains unbalanced single quotes.')
  72. if word_length > 0:
  73. words.append(sentence[pos_word_start:pos_word_start + word_length])
  74. def is_option_name(word):
  75. """Checks whether a word is an option name and, if so, removes the leading dashes.
  76. Parameters
  77. ----------
  78. word : string
  79. Word.
  80. return
  81. ------
  82. True if word is of the form "--<option>".
  83. word : string
  84. The word without the leading dashes.
  85. """
  86. if word[0] == '\'':
  87. word = word[1:len(word) - 2]
  88. return False, word
  89. if len(word) < 3:
  90. return False, word
  91. if word[0] == '-' and word[1] == '-' and word[2] != '-':
  92. word = word[2:]
  93. return True, word
  94. return False, word

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