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

Development: Make iter_backwards work on file-like objects #255

Merged
merged 4 commits into from
Aug 25, 2022

Conversation

priitlatt
Copy link
Contributor

In utilities package codemagic.utilities.backwards_file_reader there is a function iter_backwards that can be used to iterate over lines in file from bottom to top.
Currently it only works with files that are saved to disk and the function takes file path (as a string or pathlib.Path object) as an argument.

But in reality the files are not always saved to the disk, and can be stored in-memory. In such cases the path is not available and as a result iter_backwards function cannot be used.

Changes in this PR add support to iterate lines on file-like objects too.

Examples of updated usage:

  • Iterate backwards over a file that is saved to disk
import tempfile

from codemagic.utilities.backwards_file_reader import iter_backwards

with tempfile.NamedTemporaryFile('w') as tf:
    tf.write('one\ntwo\nthree')
    tf.flush()
    print(list(iter_backwards(tf.name)))  # prints ['three', 'two', 'one']
  • Iterate backwards over an in-memory file
import io
import tempfile

from codemagic.utilities.backwards_file_reader import iter_backwards

sio = StringIO('one\ntwo\nthree')
print(list(iter_backwards(sio)))  # prints ['three', 'two', 'one']

with tempfile.TemporaryFile('r+') as tf:
    tf.write('one\ntwo\nthree')
    tf.flush()
    print(list(iter_backwards(tf)))  # prints ['three', 'two', 'one']

@priitlatt priitlatt added the enhancement New feature or request label Aug 25, 2022
@priitlatt priitlatt marked this pull request as ready for review August 25, 2022 13:14
@priitlatt priitlatt merged commit 3f8be16 into master Aug 25, 2022
@priitlatt priitlatt deleted the feature/iter-backwards-on-file-descriptors branch August 25, 2022 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants