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.

121 lines
2.5 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. ###}
  58. try:
  59. from contextlib import suppress # Python 3
  60. except ImportError:
  61. @contextmanager
  62. def suppress(*excs):
  63. '''Catch and dismiss the provided exception
  64. >>> x = 'hello'
  65. >>> with suppress(IndexError):
  66. ... x = x[10]
  67. >>> x
  68. 'hello'
  69. '''
  70. try:
  71. yield
  72. except excs:
  73. pass
  74. try:
  75. compare = cmp
  76. except NameError:
  77. def compare(a, b):
  78. if a == b:
  79. return 0
  80. elif a > b:
  81. return 1
  82. return -1
  83. import sre_parse
  84. import sre_constants
  85. def get_regexp_width(regexp):
  86. try:
  87. return sre_parse.parse(regexp).getwidth()
  88. except sre_constants.error:
  89. raise ValueError(regexp)