From 544a3b0a10dcc3848baea12a9b64afc3b87510d6 Mon Sep 17 00:00:00 2001 From: Mexes Date: Wed, 11 Sep 2024 10:31:38 +0000 Subject: [PATCH] fix: formatted script files --- scripts/data/format_args.py | 30 ++++++++++++++++++------------ scripts/data/generate_data.py | 6 ++++-- scripts/misc/create_issues.py | 24 +++++++++++++++++------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/scripts/data/format_args.py b/scripts/data/format_args.py index 9d3c18cd..9d054e8e 100644 --- a/scripts/data/format_args.py +++ b/scripts/data/format_args.py @@ -20,7 +20,7 @@ def serialize(obj): return 1 if obj else 0 elif isinstance(obj, int): # This covers u8, u16, u32, u64, u128, felt252 - assert(obj >= 0 and obj < 2 ** 252) + assert obj >= 0 and obj < 2**252 return obj elif isinstance(obj, str): if obj == "0" * 64: @@ -30,25 +30,27 @@ def serialize(obj): # TODO: there might still be collisions with hashes # Try to cast to int and then to low/high parts num = int(obj) - assert(num >= 0 and num < 2 ** 256) - lo = num % 2 ** 128 - hi = num // 2 ** 128 + assert num >= 0 and num < 2**256 + lo = num % 2**128 + hi = num // 2**128 return (lo, hi) - elif obj.startswith('0x'): + elif obj.startswith("0x"): # Split into 31-byte chunks and save the remainder src = bytes.fromhex(obj[2:]) num_chunks = len(src) // 31 main_len = num_chunks * 31 rem_len = len(src) - main_len - main = [int.from_bytes(src[i:i+31], 'big') for i in range(0, main_len, 31)] + main = [ + int.from_bytes(src[i : i + 31], "big") for i in range(0, main_len, 31) + ] # TODO: check if this is how byte31 is implemented - rem = int.from_bytes(src[main_len:].rjust(31, b'\x00'), 'big') + rem = int.from_bytes(src[main_len:].rjust(31, b"\x00"), "big") return tuple([len(main)] + main + [rem, rem_len]) else: # Reversed hex string into 4-byte words then into BE u32 - assert(len(obj) == 64) + assert len(obj) == 64 rev = list(reversed(bytes.fromhex(obj))) - return tuple(int.from_bytes(rev[i:i+4], 'big') for i in range(0, 32, 4)) + return tuple(int.from_bytes(rev[i : i + 4], "big") for i in range(0, 32, 4)) elif isinstance(obj, list): arr = list(map(serialize, obj)) return tuple([len(arr)] + arr) @@ -68,6 +70,7 @@ def flatten_tuples(src): :return: an object that can only contain integers and lists, top-level tuple converts to a list. """ res = [] + def append_obj(obj, to): if isinstance(obj, int): to.append(obj) @@ -81,6 +84,7 @@ def append_obj(obj, to): append_obj(item, to) else: raise NotImplementedError(obj) + append_obj(src, res) return res @@ -92,12 +96,14 @@ def format_cairo1_run(args: list) -> str: :param args: Python object containing already processed arguments. :return: Returns string with removed commas. """ + def format_item(item): if isinstance(item, list): arr = " ".join(map(format_item, item)) - return f'[{arr}]' + return f"[{arr}]" else: return str(item) + return format_item(args) @@ -106,12 +112,12 @@ def format_args(): Expects a single CLI argument containing file path. Output is compatible with the Scarb runner arguments format. """ - if (len(sys.argv) != 2): + if len(sys.argv) != 2: raise TypeError("Expected single argument") args = json.loads(Path(sys.argv[1]).read_text()) res = flatten_tuples(serialize(args)) print([res]) -if __name__ == '__main__': +if __name__ == "__main__": format_args() diff --git a/scripts/data/generate_data.py b/scripts/data/generate_data.py index aa4855f2..691a42e6 100755 --- a/scripts/data/generate_data.py +++ b/scripts/data/generate_data.py @@ -190,13 +190,15 @@ def format_coinbase_input(input: dict): "block_time": 0, "is_coinbase": False, }, - "witness": ["0x0000000000000000000000000000000000000000000000000000000000000000"], + "witness": [ + "0x0000000000000000000000000000000000000000000000000000000000000000" + ], } def format_output(output: dict): """Formats transaction output according to the Cairo type.""" - value = (Decimal(str(output["value"])) * Decimal('100000000')).to_integral_value() + value = (Decimal(str(output["value"])) * Decimal("100000000")).to_integral_value() return { "value": int(value), "pk_script": f'0x{output["scriptPubKey"]["hex"]}', diff --git a/scripts/misc/create_issues.py b/scripts/misc/create_issues.py index 4cd113ff..a6b88330 100644 --- a/scripts/misc/create_issues.py +++ b/scripts/misc/create_issues.py @@ -3,14 +3,20 @@ import sys import argparse + def create_issue(title, body, labels): cmd = [ - "gh", "issue", "create", - "--title", title, - "--body", body, - "--label", ",".join(labels) + "gh", + "issue", + "create", + "--title", + title, + "--body", + body, + "--label", + ",".join(labels), ] - + try: subprocess.run(cmd, check=True) print(f"Successfully created issue: {title}") @@ -19,8 +25,11 @@ def create_issue(title, body, labels): print(f"Error message: {e}") sys.exit(1) + def main(): - parser = argparse.ArgumentParser(description="Create GitHub issues from a JSON file.") + parser = argparse.ArgumentParser( + description="Create GitHub issues from a JSON file." + ) parser.add_argument("json_file", help="Path to the JSON file containing issue data") args = parser.parse_args() @@ -41,5 +50,6 @@ def main(): for issue in data["issues"]: create_issue(issue["title"], issue["body"], issue["labels"]) + if __name__ == "__main__": - main() \ No newline at end of file + main()