builtins.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import sys as _sys
  2. def print(*args, sep=' ', end='\n'):
  3. s = sep.join([str(i) for i in args])
  4. _sys.stdout.write(s + end)
  5. def max(*args, key=None):
  6. if key is None:
  7. key = lambda x: x
  8. if len(args) == 0:
  9. raise TypeError('max expected 1 arguments, got 0')
  10. if len(args) == 1:
  11. args = args[0]
  12. if len(args) == 2:
  13. a, b = args
  14. return a if key(a) > key(b) else b
  15. args = iter(args)
  16. res = next(args)
  17. if res is StopIteration:
  18. raise ValueError('max() arg is an empty sequence')
  19. while True:
  20. i = next(args)
  21. if i is StopIteration:
  22. break
  23. if key(i) > key(res):
  24. res = i
  25. return res
  26. def min(*args, key=None):
  27. if key is None:
  28. key = lambda x: x
  29. if len(args) == 0:
  30. raise TypeError('min expected 1 arguments, got 0')
  31. if len(args) == 1:
  32. args = args[0]
  33. if len(args) == 2:
  34. a, b = args
  35. return a if key(a) < key(b) else b
  36. args = iter(args)
  37. res = next(args)
  38. if res is StopIteration:
  39. raise ValueError('min() arg is an empty sequence')
  40. while True:
  41. i = next(args)
  42. if i is StopIteration:
  43. break
  44. if key(i) < key(res):
  45. res = i
  46. return res
  47. def all(iterable):
  48. for i in iterable:
  49. if not i:
  50. return False
  51. return True
  52. def any(iterable):
  53. for i in iterable:
  54. if i:
  55. return True
  56. return False
  57. def enumerate(iterable, start=0):
  58. n = start
  59. for elem in iterable:
  60. yield n, elem
  61. ++n
  62. def sum(iterable):
  63. res = 0
  64. for i in iterable:
  65. res += i
  66. return res
  67. def map(f, iterable):
  68. for i in iterable:
  69. yield f(i)
  70. def filter(f, iterable):
  71. for i in iterable:
  72. if f(i):
  73. yield i
  74. def zip(a, b):
  75. a = iter(a)
  76. b = iter(b)
  77. while True:
  78. ai = next(a)
  79. bi = next(b)
  80. if ai is StopIteration or bi is StopIteration:
  81. break
  82. yield ai, bi
  83. def reversed(iterable):
  84. a = list(iterable)
  85. a.reverse()
  86. return a
  87. def sorted(iterable, reverse=False, key=None):
  88. a = list(iterable)
  89. a.sort(reverse=reverse, key=key)
  90. return a
  91. ##### str #####
  92. def __f(self: str, *args, **kwargs) -> str:
  93. def tokenizeString(s: str):
  94. tokens = []
  95. L, R = 0,0
  96. mode = None
  97. curArg = 0
  98. # lookingForKword = False
  99. while(R<len(s)):
  100. curChar = s[R]
  101. nextChar = s[R+1] if R+1<len(s) else ''
  102. # Invalid case 1: stray '}' encountered, example: "ABCD EFGH {name} IJKL}", "Hello {vv}}", "HELLO {0} WORLD}"
  103. if curChar == '}' and nextChar != '}':
  104. raise ValueError("Single '}' encountered in format string")
  105. # Valid Case 1: Escaping case, we escape "{{ or "}}" to be "{" or "}", example: "{{}}", "{{My Name is {0}}}"
  106. if (curChar == '{' and nextChar == '{') or (curChar == '}' and nextChar == '}'):
  107. if (L<R): # Valid Case 1.1: make sure we are not adding empty string
  108. tokens.append(s[L:R]) # add the string before the escape
  109. tokens.append(curChar) # Valid Case 1.2: add the escape char
  110. L = R+2 # move the left pointer to the next char
  111. R = R+2 # move the right pointer to the next char
  112. continue
  113. # Valid Case 2: Regular command line arg case: example: "ABCD EFGH {} IJKL", "{}", "HELLO {} WORLD"
  114. elif curChar == '{' and nextChar == '}':
  115. if mode is not None and mode != 'auto':
  116. # Invalid case 2: mixing automatic and manual field specifications -- example: "ABCD EFGH {name} IJKL {}", "Hello {vv} {}", "HELLO {0} WORLD {}"
  117. raise ValueError("Cannot switch from manual field numbering to automatic field specification")
  118. mode = 'auto'
  119. if(L<R): # Valid Case 2.1: make sure we are not adding empty string
  120. tokens.append(s[L:R]) # add the string before the special marker for the arg
  121. tokens.append("{"+str(curArg)+"}") # Valid Case 2.2: add the special marker for the arg
  122. curArg+=1 # increment the arg position, this will be used for referencing the arg later
  123. L = R+2 # move the left pointer to the next char
  124. R = R+2 # move the right pointer to the next char
  125. continue
  126. # Valid Case 3: Key-word arg case: example: "ABCD EFGH {name} IJKL", "Hello {vv}", "HELLO {name} WORLD"
  127. elif (curChar == '{'):
  128. if mode is not None and mode != 'manual':
  129. # # Invalid case 2: mixing automatic and manual field specifications -- example: "ABCD EFGH {} IJKL {name}", "Hello {} {1}", "HELLO {} WORLD {name}"
  130. raise ValueError("Cannot switch from automatic field specification to manual field numbering")
  131. mode = 'manual'
  132. if(L<R): # Valid case 3.1: make sure we are not adding empty string
  133. tokens.append(s[L:R]) # add the string before the special marker for the arg
  134. # We look for the end of the keyword
  135. kwL = R # Keyword left pointer
  136. kwR = R+1 # Keyword right pointer
  137. while(kwR<len(s) and s[kwR]!='}'):
  138. if s[kwR] == '{': # Invalid case 3: stray '{' encountered, example: "ABCD EFGH {n{ame} IJKL {", "Hello {vv{}}", "HELLO {0} WOR{LD}"
  139. raise ValueError("Unexpected '{' in field name")
  140. kwR += 1
  141. # Valid case 3.2: We have successfully found the end of the keyword
  142. if kwR<len(s) and s[kwR] == '}':
  143. tokens.append(s[kwL:kwR+1]) # add the special marker for the arg
  144. L = kwR+1
  145. R = kwR+1
  146. # Invalid case 4: We didn't find the end of the keyword, throw error
  147. else:
  148. raise ValueError("Expected '}' before end of string")
  149. continue
  150. R = R+1
  151. # Valid case 4: We have reached the end of the string, add the remaining string to the tokens
  152. if L<R:
  153. tokens.append(s[L:R])
  154. # print(tokens)
  155. return tokens
  156. tokens = tokenizeString(self)
  157. argMap = {}
  158. for i, a in enumerate(args):
  159. argMap[str(i)] = a
  160. final_tokens = []
  161. for t in tokens:
  162. if t[0] == '{' and t[-1] == '}':
  163. key = t[1:-1]
  164. argMapVal = argMap.get(key, None)
  165. kwargsVal = kwargs.get(key, None)
  166. if argMapVal is None and kwargsVal is None:
  167. raise ValueError("No arg found for token: "+t)
  168. elif argMapVal is not None:
  169. final_tokens.append(str(argMapVal))
  170. else:
  171. final_tokens.append(str(kwargsVal))
  172. else:
  173. final_tokens.append(t)
  174. return ''.join(final_tokens)
  175. str.format = __f
  176. def __f(self, chars=None):
  177. chars = chars or ' \t\n\r'
  178. i = 0
  179. while i < len(self) and self[i] in chars:
  180. ++i
  181. return self[i:]
  182. str.lstrip = __f
  183. def __f(self, chars=None):
  184. chars = chars or ' \t\n\r'
  185. j = len(self) - 1
  186. while j >= 0 and self[j] in chars:
  187. --j
  188. return self[:j+1]
  189. str.rstrip = __f
  190. def __f(self, chars=None):
  191. chars = chars or ' \t\n\r'
  192. i = 0
  193. while i < len(self) and self[i] in chars:
  194. ++i
  195. j = len(self) - 1
  196. while j >= 0 and self[j] in chars:
  197. --j
  198. return self[i:j+1]
  199. str.strip = __f
  200. def __f(self, width: int):
  201. delta = width - len(self)
  202. if delta <= 0:
  203. return self
  204. return '0' * delta + self
  205. str.zfill = __f
  206. def __f(self, width: int, fillchar=' '):
  207. delta = width - len(self)
  208. if delta <= 0:
  209. return self
  210. assert len(fillchar) == 1
  211. return fillchar * delta + self
  212. str.rjust = __f
  213. def __f(self, width: int, fillchar=' '):
  214. delta = width - len(self)
  215. if delta <= 0:
  216. return self
  217. assert len(fillchar) == 1
  218. return self + fillchar * delta
  219. str.ljust = __f
  220. ##### list #####
  221. def __qsort(a: list, L: int, R: int, key):
  222. if L >= R: return;
  223. mid = a[(R+L)//2];
  224. mid = key(mid)
  225. i, j = L, R
  226. while i<=j:
  227. while key(a[i])<mid: ++i;
  228. while key(a[j])>mid: --j;
  229. if i<=j:
  230. a[i], a[j] = a[j], a[i]
  231. ++i; --j;
  232. __qsort(a, L, j, key)
  233. __qsort(a, i, R, key)
  234. def __f(self, reverse=False, key=None):
  235. if key is None:
  236. key = lambda x:x
  237. __qsort(self, 0, len(self)-1, key)
  238. if reverse:
  239. self.reverse()
  240. list.sort = __f
  241. type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
  242. type.__getitem__ = lambda self, *args: self # for generics
  243. def help(obj):
  244. if hasattr(obj, '__func__'):
  245. obj = obj.__func__
  246. print(obj.__signature__)
  247. print(obj.__doc__)
  248. del __f
  249. class Exception: pass
  250. from _long import long