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.

125 lines
2.6 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. try:
  46. classtype = types.ClassType # Python2
  47. except AttributeError:
  48. classtype = type # Python3
  49. def smart_decorator(f, create_decorator):
  50. if isinstance(f, types.FunctionType):
  51. return wraps(f)(create_decorator(f, True))
  52. elif isinstance(f, (classtype, type, types.BuiltinFunctionType)):
  53. return wraps(f)(create_decorator(f, False))
  54. elif isinstance(f, types.MethodType):
  55. return wraps(f)(create_decorator(f.__func__, True))
  56. elif isinstance(f, partial):
  57. # wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
  58. return create_decorator(f.__func__, True)
  59. else:
  60. return create_decorator(f.__func__.__call__, True)
  61. ###}
  62. try:
  63. from contextlib import suppress # Python 3
  64. except ImportError:
  65. @contextmanager
  66. def suppress(*excs):
  67. '''Catch and dismiss the provided exception
  68. >>> x = 'hello'
  69. >>> with suppress(IndexError):
  70. ... x = x[10]
  71. >>> x
  72. 'hello'
  73. '''
  74. try:
  75. yield
  76. except excs:
  77. pass
  78. try:
  79. compare = cmp
  80. except NameError:
  81. def compare(a, b):
  82. if a == b:
  83. return 0
  84. elif a > b:
  85. return 1
  86. return -1
  87. import sre_parse
  88. import sre_constants
  89. def get_regexp_width(regexp):
  90. try:
  91. return sre_parse.parse(regexp).getwidth()
  92. except sre_constants.error:
  93. raise ValueError(regexp)