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

Make give pyfdb more pythonic interfaces #15

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

TomHodson
Copy link

@TomHodson TomHodson commented Feb 20, 2024

This PR fixes two issues to try to make the objects returned by pyfdb behave more like native python objects.

Issue 1 DataRetriever does not inherit from io.BaseIO

Objects that represent a stream like pattern in python is usually derived from io.BaseIO or a child of that. pyodc, for example, checks if an object is derived from io.BaseIO in order to determine whether it should be treated like a filename and opened or treated like a stream object.

I change DataRetriever to inherit from io.RawIOBase and also slightly modiy DataRetriever.seek to implement the correct interface. There's a slight issue here in that really DataRetriever.seek should accept a parameter whence = io.SEEK_CUR or io.SEEK_END but for now I have just put a NotImplementedError in that path.

I have also added a default value for DataRetriever.read(count = -1) because this is required by the type checker and made it return an empty bytearray if count=0. After this the type checker is now happy to let you pass DataRetriever objects anyway a file object would go.

Issue 2 ListIterator is not an iterator

The way ListIterator is implemented is a valid pattern for creating iterables in python, however it has the downside that because __next__ is not implemented you can't call next(it) to get just on value from the output of fdb.list

I slightly refactor ListIterator to implement __next__.

EDIT: I've now tested this and am happy that it's correct.

@tbkr
Copy link
Collaborator

tbkr commented Mar 21, 2024

Hi @TomHodson, all issues with the building cicd pipeline aside: Is there any reason this MR is WIP?

@simondsmart
Copy link

This looks fundamentally good. Can you clarify why the checks were failing (the logs have expired)

seek must accept a whence parameters that takes values io.SEEK_SET, SEEK_CUR or SEEK_END

The latter two are not implemented by the underlying fdb library so I have put a warning in.
This allows you you to call next(iterator) to just get the first entry without constructing the full stream.
@codecov-commenter
Copy link

codecov-commenter commented Oct 3, 2024

Codecov Report

Attention: Patch coverage is 3.03030% with 32 lines in your changes missing coverage. Please review.

Project coverage is 32.46%. Comparing base (ab8ebf1) to head (1e0f479).

Files with missing lines Patch % Lines
pyfdb/pyfdb.py 0.00% 32 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #15      +/-   ##
===========================================
- Coverage    33.00%   32.46%   -0.54%     
===========================================
  Files            5        5              
  Lines          303      308       +5     
===========================================
  Hits           100      100              
- Misses         203      208       +5     

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

For DataReader to be considered file like it has to match the interface exactly, hence DataReader.read has to have a default argument for count(), it also needs to return a bytes object for all code paths.
@TomHodson TomHodson changed the title WIP: Make pyfdb more pythonic Make give pyfdb more pythonic interfaces Oct 3, 2024
@TomHodson TomHodson marked this pull request as ready for review October 3, 2024 14:13
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