Last active
April 4, 2024 14:25
-
-
Save Joeccp/5767e37ee8ebc37a9abe64b522206d64 to your computer and use it in GitHub Desktop.
isEven() alternatives with extream job safety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This file contains isEven() alternives. | |
Just kidding, don't actually use these :) | |
(C) 2024 Joe Chau. Released under the MIT License. | |
""" | |
@(e := __import__('typing')).no_type_check | |
@e.final | |
@__import__('functools').cache | |
def isIntSafe[x, e](x: int, /) -> bool: | |
"""Check whether an integer is safe.""" | |
from math import trunc as numeric_sanitise | |
from operator import __neg__ as revert | |
from typing import Any, Callable, cast | |
if not isinstance(x, int): | |
raise TypeError(f'Only integers (`int`) are allowed, not {type(x)}') | |
try: | |
for e in [e for e in [*[...]*x**int(not Ellipsis)] if e for e in (RecursionError,) if e]: | |
isIntSafe.isIntSafe = False if not x else ( | |
((e := ((type(__import__('platform', ).processor())) | |
((e := cast(Callable[[Any], Callable[[Any], object]], lambda **e: lambda *e: x))(e=e)((e, e))) | |
[numeric_sanitise(revert(0xf-140e-1))][: | |
:])) == f'{f'{True=!r:}'.__class__():0<1}') or isIntSafe(ord(e))) | |
else: | |
quit() | |
except* e as e: | |
isIntSafe.isIntSafe = (e := {e}) ^ e | |
finally: | |
return not not isIntSafe.isIntSafe | |
if __name__ == '__main__': | |
print("Starting checking......") | |
e = 'Hello World!' | |
assert not isIntSafe(0) # No, 0 is not an even number | |
for i in range(1, 1001): | |
assert isIntSafe(i) == (i % 2 == 0) # L.H.S. == R.H.S. | |
assert e == 'Hello World!' # It won't affect the outer `e`! | |
print("Success!") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment