Skip to content

Commit

Permalink
Merge pull request #57 from nathanielatom/master
Browse files Browse the repository at this point in the history
Allow TdmsFile to accept any file-like object
  • Loading branch information
adamreeve committed Jan 29, 2017
2 parents 69fffc7 + 60c8c76 commit c396458
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions nptdms/tdms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import numpy as np
from datetime import datetime, timedelta
import tempfile
from io import BytesIO
try:
import pytz
except ImportError:
Expand Down Expand Up @@ -93,14 +92,17 @@


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(
if isinstance(file, int) or (
hasattr(file, 'fileno') and callable(file.fileno)):
# satisfy criteria that 'argument must be an int, or
# have a fileno() method' - from numpy TypeError
return np.fromfile(file, dtype=dtype, count=count, *args, **kwargs)
else:
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_string(file):
Expand Down

0 comments on commit c396458

Please sign in to comment.