Skip to content

Conversation

@lizard-boy
Copy link

Summary

Enables generating code directly into files:

with open('code.py', 'w') as f: module.write_code(f)

rather than

code = module.code with open('code.py', 'w') as f: f.write(code)

It does this with a subclass of CodegenState that avoids building in memory:

  • the tokens: List[str] field of CodegenState
  • the "".join(state.tokens) string of code

Test Plan

  • hatch run test passes
  • we could add some tests that compare the contents of written files written directly vs indirectly

Notes

  • CodegenState.pop_trailing_newline() isn't supported because the token is already written to the file. this is fixable if desired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants