Hacker News new | ask | show | jobs
by speedgoose 492 days ago
No version control ?
1 comments

You caught me trying to escape some embarrassment. Here's an example:

To set some context: The code handles isolated execution of machine learning code.

Original version:

    def run_code(code, timeout=3600):
        try:
            p = Process(target=exec, args=(code,))
            p.start()
            p.join(timeout)
            if p.is_alive():
                p.terminate()
                return "Timeout"
            return "Success"
        except Exception as e:
            return f"Failed: {str(e)}"
New version:

    class ProcessExecutor(Executor):
        def __init__(self, execution_id: str, code: str, 
                     working_dir: Path, dataset: pd.DataFrame, 
                     timeout: int = 3600):
            super().__init__(code, timeout)
            self.working_dir = Path(working_dir).resolve() / execution_id
            self.working_dir.mkdir(parents=True, exist_ok=True)
            self.dataset = dataset

        def run(self) -> ExecutionResult:
            start_time = time.time()
            code_file: Path = self.working_dir / self.code_file_name
            with open(code_file, "w") as f:
                f.write(self.code)
                
            try:
                process = subprocess.Popen(
                    [sys.executable, str(code_file)],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    cwd=str(self.working_dir),
                    text=True,
                )
                stdout, stderr = process.communicate(timeout=self.timeout)
                return ExecutionResult(
                    term_out=[stdout],
                    exec_time=time.time() - start_time,
                    model_artifacts=self._collect_artifacts(),
                    performance=extract_performance(stdout),
                )
            except subprocess.TimeoutExpired:
                process.kill()
                return ExecutionResult(
                    term_out=[],
                    exec_time=self.timeout,
                    exception=TimeoutError(
                        f"Execution exceeded {self.timeout}s timeout"
                    ),
                )
It may be that his does something important yours doesn’t, but yours is much easier to understand! Short and clear is good.
100%. I code like OP and find it much easy to understand. The latter could be well structured but a novice coder like me needs comments to understand it.
Fair enough! Thats a good point :)
without further context, i think the original version is the better code.
Thanks!
The second snippet of code seems to come from a template. Perhaps your colleague had already worked on on a similar problem before (e.g., python code to runs processes)?
Perhaps, shall check with him! But this is only one example, there’s a lot of other places as well. In case you’re interested to see more the project we’re working on is https://github.com/plexe-ai/smolmodels
Does your co-founder come from 90s/early 2000s Java background?
No, he’s from a Python background :)