1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import absolute_import , division , print_function
3
3
4
+ import fnmatch
4
5
import os
5
- import sys
6
6
import re
7
- import fnmatch
7
+ import sys
8
8
import warnings
9
9
from collections import OrderedDict , Iterable
10
10
11
11
import numpy as np
12
12
13
- from larray .core .metadata import Metadata
14
- from larray .core .group import Group
13
+ from larray .core .array import LArray , get_axes , ndtest , zeros , zeros_like , sequence
15
14
from larray .core .axis import Axis
16
15
from larray .core .constants import nan
17
- from larray .core .array import LArray , get_axes , ndtest , zeros , zeros_like , sequence , aslarray
18
- from larray .util . misc import float_error_handler_factory , is_interactive_interpreter , renamed_to , inverseop , basestring
16
+ from larray .core .group import Group
17
+ from larray .core . metadata import Metadata
19
18
from larray .inout .session import ext_default_engine , get_file_handler
19
+ from larray .util .misc import float_error_handler_factory , is_interactive_interpreter , renamed_to , inverseop , basestring
20
+
21
+
22
+ def _get_handler (engine , fname , overwrite , ** kwargs ):
23
+ if engine == 'auto' :
24
+ _ , ext = os .path .splitext (fname )
25
+ ext = ext .strip ('.' ) if '.' in ext else 'csv'
26
+ engine = ext_default_engine [ext ]
27
+ if engine == 'hdf' :
28
+ engine_hdf = 'auto'
29
+ if '_hdf' in engine :
30
+ engine_hdf , engine = engine .split ('_' )
31
+ handler_cls = get_file_handler (engine )
32
+ if engine == 'pandas_csv' and 'sep' in kwargs :
33
+ handler = handler_cls (fname , overwrite , kwargs ['sep' ])
34
+ elif engine == 'hdf' :
35
+ handler = handler_cls (fname , overwrite , engine = engine_hdf )
36
+ else :
37
+ handler = handler_cls (fname , overwrite )
38
+ return handler
20
39
21
40
22
41
# XXX: inherit from OrderedDict or LArray?
@@ -358,7 +377,7 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
358
377
List of objects to load.
359
378
If `fname` is None, list of paths to CSV files.
360
379
Defaults to all valid objects present in the file/directory.
361
- engine : {'auto', 'pandas_csv', 'pandas_hdf', 'pandas_excel', 'xlwings_excel', 'pickle'}, optional
380
+ engine : {'auto', 'pandas_csv', 'pandas_hdf', 'tables_hdf', ' pandas_excel', 'xlwings_excel', 'pickle'}, optional
362
381
Load using `engine`. Defaults to 'auto' (use default engine for the format guessed from the file extension).
363
382
display : bool, optional
364
383
Whether or not to display which file is being worked on. Defaults to False.
@@ -415,15 +434,7 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
415
434
engine = ext_default_engine ['csv' ]
416
435
else :
417
436
raise ValueError ("List of paths to only CSV files expected. Got {}" .format (names ))
418
- if engine == 'auto' :
419
- _ , ext = os .path .splitext (fname )
420
- ext = ext .strip ('.' ) if '.' in ext else 'csv'
421
- engine = ext_default_engine [ext ]
422
- handler_cls = get_file_handler (engine )
423
- if engine == 'pandas_csv' and 'sep' in kwargs :
424
- handler = handler_cls (fname , kwargs ['sep' ])
425
- else :
426
- handler = handler_cls (fname )
437
+ handler = _get_handler (engine , fname , False , ** kwargs )
427
438
metadata , objects = handler .read (names , display = display , ** kwargs )
428
439
for k , v in objects .items ():
429
440
self [k ] = v
@@ -442,7 +453,7 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
442
453
List of names of LArray/Axis/Group objects to dump.
443
454
If `fname` is None, list of paths to CSV files.
444
455
Defaults to all objects present in the Session.
445
- engine : {'auto', 'pandas_csv', 'pandas_hdf', 'pandas_excel', 'xlwings_excel', 'pickle'}, optional
456
+ engine : {'auto', 'pandas_csv', 'pandas_hdf', 'tables_hdf', ' pandas_excel', 'xlwings_excel', 'pickle'}, optional
446
457
Dump using `engine`. Defaults to 'auto' (use default engine for the format guessed from the file extension).
447
458
overwrite: bool, optional
448
459
Whether or not to overwrite an existing file, if any. Ignored for CSV files and 'pandas_excel' engine.
@@ -482,15 +493,7 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
482
493
>>> # replace arr1 and add arr4 in file output.h5
483
494
>>> s2.save('output.h5', overwrite=False) # doctest: +SKIP
484
495
"""
485
- if engine == 'auto' :
486
- _ , ext = os .path .splitext (fname )
487
- ext = ext .strip ('.' ) if '.' in ext else 'csv'
488
- engine = ext_default_engine [ext ]
489
- handler_cls = get_file_handler (engine )
490
- if engine == 'pandas_csv' and 'sep' in kwargs :
491
- handler = handler_cls (fname , overwrite , kwargs ['sep' ])
492
- else :
493
- handler = handler_cls (fname , overwrite )
496
+ handler = _get_handler (engine , fname , overwrite , ** kwargs )
494
497
meta = self .meta if overwrite else None
495
498
items = self .items ()
496
499
if names is not None :
0 commit comments