This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

113 рядки
2.4 KiB

  1. import functools
  2. import types
  3. from collections import deque
  4. from contextlib import contextmanager
  5. class fzset(frozenset):
  6. def __repr__(self):
  7. return '{%s}' % ', '.join(map(repr, self))
  8. def classify_bool(seq, pred):
  9. true_elems = []
  10. false_elems = []
  11. for elem in seq:
  12. if pred(elem):
  13. true_elems.append(elem)
  14. else:
  15. false_elems.append(elem)
  16. return true_elems, false_elems
  17. def classify(seq, key=None):
  18. d = {}
  19. for item in seq:
  20. k = key(item) if (key is not None) else item
  21. if k in d:
  22. d[k].append(item)
  23. else:
  24. d[k] = [item]
  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. try:
  37. STRING_TYPE = basestring
  38. except NameError: # Python 3
  39. STRING_TYPE = str
  40. Str = type(u'')
  41. def inline_args(f):
  42. # print '@@', f.__name__, type(f), isinstance(f, types.FunctionType), isinstance(f, types.TypeType), isinstance(f, types.BuiltinFunctionType)
  43. if isinstance(f, types.FunctionType):
  44. @functools.wraps(f)
  45. def _f_func(self, args):
  46. return f(self, *args)
  47. return _f_func
  48. elif isinstance(f, (type, types.BuiltinFunctionType)):
  49. @functools.wraps(f)
  50. def _f_builtin(_self, args):
  51. return f(*args)
  52. return _f_builtin
  53. elif isinstance(f, types.MethodType):
  54. @functools.wraps(f.__func__)
  55. def _f(self, args):
  56. return f.__func__(self, *args)
  57. return _f
  58. else:
  59. @functools.wraps(f.__call__.__func__)
  60. def _f(self, args):
  61. return f.__call__.__func__(self, *args)
  62. return _f
  63. try:
  64. compare = cmp
  65. except NameError:
  66. def compare(a, b):
  67. if a == b:
  68. return 0
  69. elif a > b:
  70. return 1
  71. else:
  72. return -1
  73. try:
  74. from contextlib import suppress # Python 3
  75. except ImportError:
  76. @contextmanager
  77. def suppress(*excs):
  78. '''Catch and dismiss the provided exception
  79. >>> x = 'hello'
  80. >>> with suppress(IndexError):
  81. ... x = x[10]
  82. >>> x
  83. 'hello'
  84. '''
  85. try:
  86. yield
  87. except excs:
  88. pass