Skip to content

Instantly share code, notes, and snippets.

@alimsk
Last active February 27, 2022 21:26
Show Gist options
  • Save alimsk/4b5a66268376eec1e7a4b419b154d7f4 to your computer and use it in GitHub Desktop.
Save alimsk/4b5a66268376eec1e7a4b419b154d7f4 to your computer and use it in GitHub Desktop.
python calculate EXTENDED_ARG
print(split_arg(270))
# output:
# (14, 1, 0, 0)
# when used:
# EXTENDED_ARG 1
# JUMP_ABSOLUTE 14
import typing as t
def split_arg(value: int) -> t.Tuple[int, int, int, int]:
first, value = divmod(value, 256)
second, first = divmod(first, 256)
last, second = divmod(second, 256)
if last >= 256:
raise ValueError(f"too big numbers, max is 32 bit")
return value, first, second, last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment