Debian Codegraph @

pypy/module/__builtin__/app_operation.py

raw symbols SCIP
1 import _operator
2
3 def bin(x):
4 """Return the binary representation of an integer.
5
6 >>> bin(2796202)
7 '0b1010101010101010101010'
8
9 """
10 value = _operator.index(x)
11 return int.__format__(value, "#b")
12
13 def oct(x):
14 """Return the octal representation of an integer.
15
16 >>> oct(342391)
17 '0o1234567'
18
19 """
20 x = _operator.index(x)
21 return int.__format__(x, "#o")
22
23 def hex(x):
24 """Return the hexadecimal representation of an integer.
25
26 >>> hex(12648430)
27 '0xc0ffee'
28
29 """
30 x = _operator.index(x)
31 return int.__format__(x, "#x")
32
33
34 # anext and aiter adapted from: https://github.com/python/cpython/pull/8895
35
36 _NOT_PROVIDED = object() # sentinel object to detect when a kwarg was not given
37
38
39 def aiter(obj):
40 """aiter(async_iterable) -> async_iterator
41 aiter(async_callable, sentinel) -> async_iterator
42 Like the iter() builtin but for async iterables and callables.
43 """
44 typ = type(obj)
45 try:
46 meth = typ.__aiter__
47 except AttributeError:
48 raise TypeError(f"'{type(obj).__name__}' object is not an async iterable")
49 ait = meth(obj)
50 if not hasattr(ait, '__anext__'):
51 raise TypeError(f"aiter() returned not an async iterator of type '{type(ait).__name__}'")
52 return ait
53
54 def anext(iterator, default=_NOT_PROVIDED):
55 """anext(async_iterator[, default])
56 Return the next item from the async iterator.
57 If default is given and the iterator is exhausted,
58 it is returned instead of raising StopAsyncIteration.
59 """
60 typ = type(iterator)
61
62 try:
63 __anext__ = typ.__anext__
64 except AttributeError:
65 raise TypeError(f"'{type(iterator).__name__}' object is not an async iterator")
66
67 if default is _NOT_PROVIDED:
68 return __anext__(iterator)
69
70 async def anext_impl():
71 try:
72 # The C code is way more low-level than this, as it implements
73 # all methods of the iterator protocol. In this implementation
74 # we're relying on higher-level coroutine concepts, but that's
75 # exactly what we want -- crosstest pure-Python high-level
76 # implementation and low-level C anext() iterators.
77 return await __anext__(iterator)
78 except StopAsyncIteration:
79 return default
80
81 return anext_impl()
82
83