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

Read from a file-like object #28

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

Conversation

IamJeffG
Copy link

This PR extends GuanoPy to allow reading from any file-like object (one that implements methods seek, read, tell), not just via filename.

This is important when the very large WAV file exists over a network and we want to extract GUANO metadata without downloading the entire sound data.

What I changed

  • GuanoPy.__init__ arg switched from filename to file:

    • If file is a string, opens the file by that name,
    • otherwise if file is set, treat it as a file-like object (one that implements methods seek, read, tell).
  • Add filename property that returns a string only if this GuanoPy object references a file by its filename. This is settable if you want to write a "new" file.

  • Places that GuanoPy actually has to open the file (_load and write methods), check if the self.filename property is set (i.e. we have a string); if so, open it as usual, otherwise simply return the file-like object from a contextlib.nullcontext()

  • New test case to read a from file-like object: an already open file handle.

__init__ arg switched from filename to file:
  If `file` is a string, opens the file by that name,
  otherwise if `file` is set, treat it as a file-like object
  (one that implements methods seek, read, tell).

Add `filename` property that returns a string only if
this GuanoPy object references a file by its filename.
This is settable if you want to write a "new" file.
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.

1 participant