Skip to content

Commit 0e84157

Browse files
committed
Made read method convenient to use directly from the core package
1 parent 4bea60e commit 0e84157

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

qtpandas/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# __import__('pkg_resources').declare_namespace(__name__)
33
__version__ = '1.0.2'
4-
from qtpandas.models.DataFrameModel import DataFrameModel
4+
from qtpandas.models.DataFrameModel import DataFrameModel, read
55
from qtpandas.models.ColumnDtypeModel import ColumnDtypeModel
66
from qtpandas.models.DataSearch import DataSearch
7-
from qtpandas.utils import superReadFile, superReadCSV, superReadText
87
from qtpandas.views.CSVDialogs import CSVExportDialog, CSVImportDialog
98
from qtpandas.views.EditDialogs import AddAttributesDialog, RemoveAttributesDialog
10-

qtpandas/models/DataFrameModel.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
DATAFRAME_ROLE = Qt.UserRole + 2
2222

2323

24+
def read(filepath, **kwargs):
25+
"""
26+
Read a data file into a DataFrameModel.
27+
28+
:param filepath: The rows/columns filepath to read.
29+
:param kwargs:
30+
xls/x files - see pandas.read_excel(**kwargs)
31+
.csv/.txt/etc - see pandas.read_csv(**kwargs)
32+
:return:
33+
"""
34+
return DataFrameModel(dataFrame=superReadFile(filepath, **kwargs),
35+
filePath=filepath)
36+
37+
2438
class DataFrameModel(QtCore.QAbstractTableModel):
2539
"""data model for use in QTableView, QListView, QComboBox, etc.
2640
@@ -223,6 +237,7 @@ def rename(self, index=None, columns=None, **kwargs):
223237
def applyFunction(self, func):
224238
"""
225239
Applies a function to the dataFrame with appropriate signals.
240+
The function must return a dataframe.
226241
:param func: A function (or partial function) that accepts a dataframe as the first argument.
227242
:return: None
228243
:raise:

qtpandas/utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,18 @@ def superReadFile(filepath, **kwargs):
170170
if isinstance(filepath, pd.DataFrame):
171171
return filepath
172172

173-
excels = ['.xlsx', '.xls']
174-
texts = ['.txt', '.tsv', '.csv']
175173
ext = os.path.splitext(filepath)[1].lower()
176174

177-
if ext in excels:
178-
return pd.read_excel(filepath, **kwargs)
179-
elif ext in texts:
180-
return superReadText(filepath, **kwargs)
175+
if ext in ['.xlsx', '.xls']:
176+
df = pd.read_excel(filepath, **kwargs)
181177
else:
182-
raise Exception("Unsupported filetype: {}\n Supported filetypes: {}".format(ext, excels + texts))
178+
# Assume it's a text-like file and try to read it.
179+
try:
180+
df = superReadText(filepath, **kwargs)
181+
except Exception as e:
182+
# TODO: Make this trace back better? Custom Exception? Raise original?
183+
raise Exception("Error reading file: {}".format(e))
184+
return df
183185

184186

185187
def dedupe_cols(frame):

0 commit comments

Comments
 (0)