This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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