Skip to content

Commit

Permalink
Merge pull request #59 from nathanielatom/master
Browse files Browse the repository at this point in the history
Allow read/write tdms for any file-like object
  • Loading branch information
adamreeve committed Jan 30, 2017
2 parents cc13536 + bd983ae commit f623b3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions nptdms/tdms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from copy import copy
import numpy as np
import tempfile
from io import BytesIO
from io import UnsupportedOperation

from nptdms.utils import Timer
from nptdms.common import toc_properties
Expand All @@ -39,14 +39,14 @@


def fromfile(file, dtype, count, *args, **kwargs):
""" Wrapper around np.fromfile to support BytesIO fake files."""
"""Wrapper around np.fromfile to support any file-like object"""

if isinstance(file, BytesIO):
return np.fromstring(
try:
return np.fromfile(file, dtype=dtype, count=count, *args, **kwargs)
except (TypeError, IOError, UnsupportedOperation):
return np.frombuffer(
file.read(count * np.dtype(dtype).itemsize),
dtype=dtype, count=count, *args, **kwargs)
else:
return np.fromfile(file, dtype=dtype, count=count, *args, **kwargs)


def read_property(f):
Expand Down
12 changes: 6 additions & 6 deletions nptdms/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
except ImportError:
OrderedDict = dict
from datetime import datetime
from io import BytesIO
from io import UnsupportedOperation
import logging
import numpy as np
from nptdms.common import toc_properties
Expand Down Expand Up @@ -276,10 +276,10 @@ def _map_property_value(value):


def to_file(file, array):
"""Wrapper around ndarray.tofile to support BytesIO
"""
if isinstance(file, BytesIO):
"""Wrapper around ndarray.tofile to support any file-like object"""

try:
array.tofile(file)
except (TypeError, IOError, UnsupportedOperation):
# tostring actually returns bytes
file.write(array.tostring())
else:
array.tofile(file)

0 comments on commit f623b3c

Please sign in to comment.