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

StarWriter.lines #62

Merged
merged 7 commits into from
Jul 23, 2024
Merged

StarWriter.lines #62

merged 7 commits into from
Jul 23, 2024

Conversation

jahooker
Copy link
Contributor

I've made a few small modifications to StarWriter.
Now, you can instantiate the class without writing to disk.
To write, you need to explicitly call StarWriter.write.
You can also just see the contents of the file that a StarWriter would write, with StarWriter.file_contents.

jahooker added 2 commits July 16, 2024 14:32
Generates strings that are written out by `StarWriter.write`.
@alisterburt
Copy link
Collaborator

very nice! will review properly some time after Thursday (prepping/running a workshop)

@alisterburt
Copy link
Collaborator

maybe @jojoelfe has some time now?

@codecov-commenter
Copy link

codecov-commenter commented Jul 17, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.73%. Comparing base (5b19406) to head (4e311c5).
Report is 7 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
+ Coverage   80.93%   82.73%   +1.80%     
==========================================
  Files           7        7              
  Lines         278      307      +29     
==========================================
+ Hits          225      254      +29     
  Misses         53       53              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jojoelfe
Copy link
Collaborator

I'll try to look at this tonight, but can't really promise too much.

@jahooker Thanks so much for the PR! Can you maybe write one or two lines what the motivation for this is? I do think it's a bit nicer that the constructor does not immediately write to disk, but when would one want to do this?

@jahooker
Copy link
Contributor Author

Hi! Thanks for taking the time to consider my changes!

Quite a few times now, I have found myself wanting — in an interactive Python session — to see what starfile.write would generate, but I do not want the hassle of coming up with a name for the output file (e.g. test.star or delete_me.star). Also, I have found myself wanting in scripts to use starfile simply to print text. If I want to put the output in a file, I might use shell redirection (e.g. myscript.py > foo.star). And finally, maybe the file system is super slow (or broken altogether) — computing infrastructure can have bad days, after all. My changes allow for a separation of the concerns of text generation and file writing. You might do:

#!python3
import starfile
from starfile.writer import StarWriter
from elsewhere import foo

bar = starfile.read('bar.star')
writer = StarWriter(foo(bar), '')
print(''.join(writer.file_contents()))

Admittedly, the interface could be nicer. Perhaps StarWriter.file_contents should return a string outright, not a generator. And perhaps it should be possible to instantiate a StarWriter without providing a filename (even a nonsensical one like ''). To that end, I might make some more changes.

Copy link
Collaborator

@jojoelfe jojoelfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think this usecase makes sense. There are a couple thoughts I have:

  • I guess there is a chance that this breaks some existing if somebody used the StarWriter class. But as far as I can tell the StarWriter class is not documented anywhere, so I don't think this is a large concern.
  • Since the StarWriter class is not really supposed to be a public API, wouldn't it be nice to have an exposed function so one could do this:
import starfile
from elsewhere import foo

bar = starfile.read('bar.star')
starstring = starfile.to_string(foo(bar))
  • I do like having a generator for the lines, as this might help people who want to stream/paginate starfiles. But to me looks like in the current state the actual data is returned as a single string by the file_contents() function.
  • If we do not care about the API stability, it would make sense to remove the filename from the constructor and instead make it an argument to the write() method

@jahooker jahooker changed the title StarWriter.file_contents StarWriter.lines Jul 18, 2024
@jahooker jahooker changed the title StarWriter.lines StarWriter.lines Jul 18, 2024
@jojoelfe
Copy link
Collaborator

@alisterburt I don't think I can trigger the tests on this PR. Can you do it?

@alisterburt
Copy link
Collaborator

@jojoelfe done, thanks!

@jojoelfe I've given you admin here

@jojoelfe
Copy link
Collaborator

Hey @jahooker, I've just added a few tests and minor fixes.

The speed test for python3.9 keeps on failing, but on my machine the new code does not cause performance recession with py3.9 and py3.10... Maybe it just gets assigned to a runner with performance problems.

If you don't have anything to add (maybe some documentation would be nice, but can also be done later) this seems good to got to me.

Thanks again!

Johannes

Copy link
Contributor Author

@jahooker jahooker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jojoelfe! Looks good. I'll write a little bit of documentation.

@jojoelfe jojoelfe merged commit b6ebefa into teamtomo:main Jul 23, 2024
4 of 5 checks passed
@jojoelfe
Copy link
Collaborator

Great, I merged for now and created an issue for the documentation.

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.

4 participants