Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable writing module code directly to file #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants