Skip to content

Commit

Permalink
Add back ability to write to file-like object.
Browse files Browse the repository at this point in the history
  • Loading branch information
adarob authored and craffel committed Oct 11, 2016
1 parent 5c1a0ec commit d207065
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pretty_midi/pretty_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import collections
import copy
import functools
import six

from .instrument import Instrument
from .containers import (KeySignature, TimeSignature, Lyric, Note,
Expand Down Expand Up @@ -48,7 +49,7 @@ def __init__(self, midi_file=None, resolution=220, initial_tempo=120.):
"""
if midi_file is not None:
# Load in the MIDI data using the midi module
if isinstance(midi_file, basestring):
if isinstance(midi_file, six.string_types):
# If a string was given, pass it as the string filename
midi_data = mido.MidiFile(filename=midi_file)
else:
Expand Down Expand Up @@ -1176,8 +1177,8 @@ def write(self, filename):
Parameters
----------
filename : str
Path to write .mid file to.
filename : str or file
Path or file to write .mid file to.
"""

Expand Down Expand Up @@ -1340,4 +1341,10 @@ def event_compare(event1, event2):
event.time -= tick
tick += event.time
# Write it out
mid.save(filename)
if isinstance(filename, six.string_types):
# If a string was given, pass it as the string filename
mid.save(filename=filename)
else:
# Otherwise, try passing it in as a file pointer
mid.save(file=filename)

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
install_requires=[
'numpy >= 1.7.0',
'mido >= 1.1.16',
'six',
]
)

0 comments on commit d207065

Please sign in to comment.