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.

150 lines
3.3 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. def dedup_list(l):
  62. """Given a list (l) will removing duplicates from the list,
  63. preserving the original order of the list. Assumes that
  64. the list entrie are hashable."""
  65. dedup = set()
  66. return [ x for x in l if not (x in dedup or dedup.add(x))]
  67. ###}
  68. try:
  69. from contextlib import suppress # Python 3
  70. except ImportError:
  71. @contextmanager
  72. def suppress(*excs):
  73. '''Catch and dismiss the provided exception
  74. >>> x = 'hello'
  75. >>> with suppress(IndexError):
  76. ... x = x[10]
  77. >>> x
  78. 'hello'
  79. '''
  80. try:
  81. yield
  82. except excs:
  83. pass
  84. try:
  85. compare = cmp
  86. except NameError:
  87. def compare(a, b):
  88. if a == b:
  89. return 0
  90. elif a > b:
  91. return 1
  92. return -1
  93. import sre_parse
  94. import sre_constants
  95. def get_regexp_width(regexp):
  96. try:
  97. return sre_parse.parse(regexp).getwidth()
  98. except sre_constants.error:
  99. raise ValueError(regexp)
  100. class Enumerator:
  101. def __init__(self):
  102. self.enums = {}
  103. def get(self, item):
  104. if item not in self.enums:
  105. self.enums[item] = len(self.enums)
  106. return self.enums[item]
  107. def __len__(self):
  108. return len(self.enums)
  109. def reversed(self):
  110. r = {v: k for k, v in self.enums.items()}
  111. assert len(r) == len(self.enums)
  112. return r