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.

119 lines
2.5 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, value=None):
  15. d = {}
  16. for item in seq:
  17. k = key(item) if (key is not None) else item
  18. v = value(item) if (value is not None) else item
  19. if k in d:
  20. d[k].append(v)
  21. else:
  22. d[k] = [v]
  23. return d
  24. def bfs(initial, expand):
  25. open_q = deque(list(initial))
  26. visited = set(open_q)
  27. while open_q:
  28. node = open_q.popleft()
  29. yield node
  30. for next_node in expand(node):
  31. if next_node not in visited:
  32. visited.add(next_node)
  33. open_q.append(next_node)
  34. try:
  35. STRING_TYPE = basestring
  36. except NameError: # Python 3
  37. STRING_TYPE = str
  38. ###{standalone
  39. import types
  40. from functools import wraps, partial
  41. from contextlib import contextmanager
  42. Str = type(u'')
  43. def smart_decorator(f, create_decorator):
  44. if isinstance(f, types.FunctionType):
  45. return wraps(f)(create_decorator(f, True))
  46. elif isinstance(f, (type, types.BuiltinFunctionType)):
  47. return wraps(f)(create_decorator(f, False))
  48. elif isinstance(f, types.MethodType):
  49. return wraps(f)(create_decorator(f.__func__, True))
  50. elif isinstance(f, partial):
  51. # wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
  52. return create_decorator(f.__func__, True)
  53. else:
  54. return create_decorator(f.__func__.__call__, True)
  55. try:
  56. from contextlib import suppress # Python 3
  57. except ImportError:
  58. @contextmanager
  59. def suppress(*excs):
  60. '''Catch and dismiss the provided exception
  61. >>> x = 'hello'
  62. >>> with suppress(IndexError):
  63. ... x = x[10]
  64. >>> x
  65. 'hello'
  66. '''
  67. try:
  68. yield
  69. except excs:
  70. pass
  71. ###}
  72. try:
  73. compare = cmp
  74. except NameError:
  75. def compare(a, b):
  76. if a == b:
  77. return 0
  78. elif a > b:
  79. return 1
  80. else:
  81. return -1
  82. import sre_parse
  83. import sre_constants
  84. def get_regexp_width(regexp):
  85. try:
  86. return sre_parse.parse(regexp).getwidth()
  87. except sre_constants.error:
  88. raise ValueError(regexp)