From bd1c1b7fb41db1cd40b2faeee936b8eabade6dbe Mon Sep 17 00:00:00 2001 From: Seunghoon Lee Date: Tue, 14 May 2024 12:44:52 +0900 Subject: [PATCH] cli tool to launch python with zluda --- cli/zluda-python.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/cli/zluda-python.py b/cli/zluda-python.py index e5a63b716..958f2121c 100644 --- a/cli/zluda-python.py +++ b/cli/zluda-python.py @@ -1,5 +1,26 @@ import os import sys +from typing import Dict, Mapping + + +class Interpreter: + env_globals: Dict + env_locals: Mapping + + def __init__(self, env_globals, env_locals): + self.env_globals = env_globals + self.env_locals = env_locals + + def execute(self, s: str): + try: + exec(s, self.env_globals, self.env_locals) + except Exception as e: + print(f'{e.__class__.__name__}: {e}') + + def from_file(self, path): + with open(path, 'r', encoding='utf-8') as fp: + for line in fp.readlines(): + self.execute(line) if __name__ == '__main__': @@ -9,14 +30,16 @@ if __name__ == '__main__': load(find()) import torch - print(f'Python with ZLUDA {sys.version}') - print('Type "help", "copyright", "credits" or "license" for more information.') + interpreter = Interpreter({ + 'torch': torch, + }, {}) - while True: - print('>>> ', end='') - try: - exec(input(), { - 'torch': torch, - }) - except Exception as e: - print(f'{e.__class__.__name__}: {e}') + if len(sys.argv) > 1: + interpreter.from_file(sys.argv[1]) + else: + print(f'Python with ZLUDA {sys.version}') + print('Type "help", "copyright", "credits" or "license" for more information.') + + while True: + print('>>> ', end='') + interpreter.execute(input())