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.

129 lines
2.9 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. import functools
  41. from contextlib import contextmanager
  42. Str = type(u'')
  43. def inline_args(f):
  44. # print '@@', f.__name__, type(f), isinstance(f, types.FunctionType), isinstance(f, types.TypeType), isinstance(f, types.BuiltinFunctionType)
  45. if isinstance(f, types.FunctionType):
  46. @functools.wraps(f)
  47. def _f_func(self, args):
  48. return f(self, *args)
  49. return _f_func
  50. elif isinstance(f, (type, types.BuiltinFunctionType)):
  51. @functools.wraps(f)
  52. def _f_builtin(_self, args):
  53. return f(*args)
  54. return _f_builtin
  55. elif isinstance(f, types.MethodType):
  56. @functools.wraps(f.__func__)
  57. def _f(self, args):
  58. return f.__func__(self, *args)
  59. return _f
  60. elif isinstance(f, functools.partial):
  61. # wraps does not work for partials in 2.7: https://bugs.python.org/issue3445
  62. # @functools.wraps(f)
  63. def _f(self, args):
  64. return f(*args)
  65. return _f
  66. else:
  67. @functools.wraps(f.__call__.__func__)
  68. def _f(self, args):
  69. return f.__call__.__func__(self, *args)
  70. return _f
  71. try:
  72. from contextlib import suppress # Python 3
  73. except ImportError:
  74. @contextmanager
  75. def suppress(*excs):
  76. '''Catch and dismiss the provided exception
  77. >>> x = 'hello'
  78. >>> with suppress(IndexError):
  79. ... x = x[10]
  80. >>> x
  81. 'hello'
  82. '''
  83. try:
  84. yield
  85. except excs:
  86. pass
  87. ###}
  88. try:
  89. compare = cmp
  90. except NameError:
  91. def compare(a, b):
  92. if a == b:
  93. return 0
  94. elif a > b:
  95. return 1
  96. else:
  97. return -1
  98. import sre_parse
  99. import sre_constants
  100. def get_regexp_width(regexp):
  101. try:
  102. return sre_parse.parse(regexp).getwidth()
  103. except sre_constants.error:
  104. raise ValueError(regexp)