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.

127 lines
2.8 KiB

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