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

Split out IO logic from high level functions (#392) #393

Merged

Conversation

jstockwin
Copy link
Member

Pull request

Adds two new functions (extract_text_from_io and extract_pages_from_io) to high_level.py, allowing you to call extract_text and extract_pages without having the file locally.

Closes #392

Notes

  • I've created the new functions, *_from_io to avoid breaking changes to the original functions. This is not very consistent with the extract_text_to_fp at the top of the file, perhaps. Happy to implement alternatives.
  • I've used inf as the new IO parameter, even though I don't really like the name. This is to be consistent with extract_text_to_fp. Other places in the code use fp. Happy to change.
  • I added the test in a new file, as the existing code has test_highlevel_extracttext.py. Is this correct, or should this all be in one test_high_level.py?

How Has This Been Tested?

I've added tests for extract_text_from_io, but couldn't find any existing tests for extract_pages?

Checklist

  • I have added tests that prove my fix is effective or that my feature
    works
  • I have added docstrings to newly created methods and classes
  • I have optimized the code at least one time after creating the initial
    version
  • I have updated the README.md or I am verified that this
    is not necessary (not necessary)
  • I have updated the readthedocs documentation or I
    verified that this is not necessary (not necessary as the read the docs page on this is auto generated from docstrings)
  • I have added a consice human-readable description of the change to
    CHANGELOG.md

@jstockwin jstockwin force-pushed the add-io-to-high-level-functions branch 2 times, most recently from a0aee18 to eeb272a Compare March 16, 2020 17:07
@pietermarsman
Copy link
Member

I thought you were going to do something like this read_csv() method in pandas. You can pass a path or a file-like object. So the user does not need to learn a new method if (s)he wants the same functionality.

@jstockwin jstockwin force-pushed the add-io-to-high-level-functions branch 2 times, most recently from 71ffd07 to 080df1a Compare March 17, 2020 09:59
@jstockwin
Copy link
Member Author

@pietermarsman PR updated to keep the same function name and simply allow either a string or a file-like object. I've added an open_filename context manager to help do this. I was tempted to put this in utils, but it didn't quite seem to fit...

I've essentially followed advice from here about how to do this... https://stackoverflow.com/questions/7268353/how-to-accept-both-filenames-and-file-like-objects-in-python-functions

Copy link
Member

@pietermarsman pietermarsman left a comment

Choose a reason for hiding this comment

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

Looks perfect!

I suggested 2 minor changes.

CHANGELOG.md Outdated Show resolved Hide resolved
pdfminer/high_level.py Outdated Show resolved Hide resolved
@jstockwin jstockwin force-pushed the add-io-to-high-level-functions branch from f1545ec to abd72c4 Compare March 26, 2020 10:06
@jstockwin
Copy link
Member Author

@pietermarsman thanks. I've updated the first commit to resolve contributing being out of date, and added a new commit moving the open_filename to utils.

Please take another look when you get chance.

Copy link
Member

@pietermarsman pietermarsman left a comment

Choose a reason for hiding this comment

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

😄

@pietermarsman pietermarsman merged commit 1a4a06d into pdfminer:develop Mar 26, 2020
@jstockwin jstockwin deleted the add-io-to-high-level-functions branch March 27, 2020 08:47
davidfraser pushed a commit to j5int/pdfminer.six that referenced this pull request Sep 15, 2020
…er#393)

* Allow file-like inputs to high level functions (pdfminer#392)

* PR Review - move open_filename to utils

(cherry picked from commit 1a4a06d)
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.

Allow calling high level functions with file-like objects
2 participants