This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.8 KiB

  1. from collections import deque
  2. class fzset(frozenset):
  3. def __repr__(self):
  4. return '{%s}' % ', '.join(map(repr, self))
  5. def classify_bool(seq, pred):
  6. true_elems = []
  7. false_elems = []
  8. for elem in seq:
  9. if pred(elem):
  10. true_elems.append(elem)
  11. else:
  12. false_elems.append(elem)
  13. return true_elems, false_elems
  14. def classify(seq, key=None):
  15. d = {}
  16. for item in seq:
  17. k = key(item) if (key is not None) else item
  18. if k in d:
  19. d[k].append(item)
  20. else:
  21. d[k] = [item]
  22. return d
  23. def bfs(initial, expand):
  24. open_q = deque(list(initial))
  25. visited = set(open_q)
  26. while open_q:
  27. node = open_q.popleft()
  28. yield node
  29. for next_node in expand(node):
  30. if next_node not in visited:
  31. visited.add(next_node)
  32. open_q.append(next_node)
  33. try:
  34. STRING_TYPE = basestring
  35. except NameError: # Python 3
  36. STRING_TYPE = str
  37. ###{standalone
  38. import types
  39. import functools
  40. from contextlib import contextmanager
  41. Str = type(u'')
  42. def inline_args(f):
  43. # print '@@', f.__name__, type(f), isinstance(f, types.FunctionType), isinstance(f, types.TypeType), isinstance(f, types.BuiltinFunctionType)
  44. if isinstance(f, types.FunctionType):
  45. @functools.wraps(f)
  46. def _f_func(self, args):
  47. return f(self, *args)
  48. return _f_func
  49. elif isinstance(f, (type, types.BuiltinFunctionType)):
  50. @functools.wraps(f)
  51. def _f_builtin(_self, args):
  52. return f(*args)
  53. return _f_builtin
  54. elif isinstance(f, types.MethodType):
  55. @functools.wraps(f.__func__)
  56. def _f(self, args):
  57. return f.__func__(self, *args)
  58. return _f
  59. elif isinstance(f, functools.partial):
  60. # wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
  61. # @functools.wraps(f)
  62. def _f(self, args):
  63. return f(*args)
  64. return _f
  65. else:
  66. @functools.wraps(f.__call__.__func__)
  67. def _f(self, args):
  68. return f.__call__.__func__(self, *args)
  69. return _f
  70. try:
  71. from contextlib import suppress # Python 3
  72. except ImportError:
  73. @contextmanager
  74. def suppress(*excs):
  75. '''Catch and dismiss the provided exception
  76. >>> x = 'hello'
  77. >>> with suppress(IndexError):
  78. ... x = x[10]
  79. >>> x
  80. 'hello'
  81. '''
  82. try:
  83. yield
  84. except excs:
  85. pass
  86. ###}
  87. try:
  88. compare = cmp
  89. except NameError:
  90. def compare(a, b):
  91. if a == b:
  92. return 0
  93. elif a > b:
  94. return 1
  95. else:
  96. return -1
  97. import sre_parse
  98. import sre_constants
  99. def get_regexp_width(regexp):
  100. try:
  101. return sre_parse.parse(regexp).getwidth()
  102. except sre_constants.error:
  103. raise ValueError(regexp)