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

Allow pre-loaded mido.MidiFile objects to be passed as an argument to PrettyMIDI #241

Merged
merged 4 commits into from
Feb 24, 2024

Conversation

a-pillay
Copy link
Contributor

  • Implemented mido_object argument in PrettyMIDI to pass pre-loaded mido.MidiFile objects
  • Updated docstring with the aforementioned change
  • Implemented a unit test to verify PrettyMIDI object initialization logic

This closes #200 and #201

…o.MidiFile objects

- implemented unit test to verify PrettyMIDI object initialization
@craffel
Copy link
Owner

craffel commented Feb 15, 2024

We actually need to put the new arg at the end of the args list to avoid breaking code that uses positional arguments.

@a-pillay
Copy link
Contributor Author

@craffel I agree.

I have modified mido_object to be the last argument in PrettyMIDI. Please let me know if anything else needs to be addressed!

@craffel
Copy link
Owner

craffel commented Feb 17, 2024

Need to change argument order in docstring 😅

@a-pillay
Copy link
Contributor Author

I was considering setting the mido_object description as the last one in the argument docstring, but proceeded with keeping it immediately after midi_file to highlight how the former will be prioritized.

However, I can set the mido_object description as the last one (in sync with the argument order) if you could confirm that it sounds good. I find both options equally logical.

@craffel
Copy link
Owner

craffel commented Feb 18, 2024

Docstring argument lists must match the order of the arguments.

@a-pillay
Copy link
Contributor Author

Sounds good! I have updated the docstrings as suggested. Thanks for putting up with my repeated requests for clarifications!

@craffel
Copy link
Owner

craffel commented Feb 21, 2024

Can you raise a ValueError if both a filename and a mido object are provided? This should not work silently because the correct behavior is ambiguous. The logic would look like

if mido_object is not None and midi_file is not None:
   raise ValueError("A midi_file and midi_object cannot both be provided.")

and then the rest of the logic can proceed assuming that only one of them has been provided (e.g. you don't need an elif).

…he midi_file and mido_object arguments are provided.

Related Changes:
- Updated test_pm_object_initialization to reflect this change.
- Updated PrettyMIDI docstring to reflect this change.
- Added ValueError tests in test_pm_object_initialization
@a-pillay
Copy link
Contributor Author

Done! Now, a ValueError will be raised if both mido_object and midi_file arguments are provided at the same time. Also, updated the concerned docstring and unit test to reflect the same. I have also added tests for verifying these ValueErrors but since the existing tests don't use a toolkit like unittest, I have implemented them in an alternate way.

else:
# Otherwise, try passing it in as a file pointer
midi_data = mido.MidiFile(file=midi_file, charset=charset)
if mido_object is not None or midi_file is not None:
Copy link
Owner

Choose a reason for hiding this comment

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

This conditional is no longer necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need that conditional to handle cases when both the midi_file and mido_object is None; in this case, the logic part of the else block on line 135 will get executed (same as existing logic).

Having this line ensures we only need to implement the logic from line 94 to line 134 once to handle both midi_file and mido_object .

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, I missed the preexisting logic. Thanks.

else:
# Otherwise, try passing it in as a file pointer
midi_data = mido.MidiFile(file=midi_file, charset=charset)
if mido_object is not None or midi_file is not None:
Copy link
Owner

Choose a reason for hiding this comment

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

Ah, I missed the preexisting logic. Thanks.

@craffel craffel merged commit 280352a into craffel:main Feb 24, 2024
@craffel
Copy link
Owner

craffel commented Feb 24, 2024

Thanks!

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.

Init with mido object parameter
2 participants