12
12
from larray .core .axis import Axis
13
13
from larray .core .array import LArray , get_axes , ndtest , zeros , zeros_like , sequence , aslarray
14
14
from larray .util .misc import float_error_handler_factory , is_interactive_interpreter , renamed_to , inverseop
15
- from larray .inout .session import check_pattern , handler_classes , ext_default_engine
15
+ from larray .inout .session import ext_default_engine , get_file_handler
16
+
17
+
18
+ def check_pattern (k , pattern ):
19
+ return k .startswith (pattern )
16
20
17
21
18
22
# XXX: inherit from OrderedDict or LArray?
@@ -253,8 +257,11 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
253
257
_ , ext = os .path .splitext (fname )
254
258
ext = ext .strip ('.' ) if '.' in ext else 'csv'
255
259
engine = ext_default_engine [ext ]
256
- handler_cls = handler_classes [engine ]
257
- handler = handler_cls (fname )
260
+ handler_cls = get_file_handler (engine )
261
+ if engine == 'pandas_csv' and 'sep' in kwargs :
262
+ handler = handler_cls (fname , kwargs ['sep' ])
263
+ else :
264
+ handler = handler_cls (fname )
258
265
objects = handler .read_items (names , display = display , ** kwargs )
259
266
for k , v in objects .items ():
260
267
self [k ] = v
@@ -275,11 +282,15 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
275
282
engine : {'auto', 'pandas_csv', 'pandas_hdf', 'pandas_excel', 'xlwings_excel', 'pickle'}, optional
276
283
Dump using `engine`. Defaults to 'auto' (use default engine for the format guessed from the file extension).
277
284
overwrite: bool, optional
278
- Whether or not to overwrite an existing file, if any. Ignored for CSV files. If False, file is updated .
279
- Defaults to True.
285
+ Whether or not to overwrite an existing file, if any. Ignored for CSV files and 'pandas_excel' engine .
286
+ If False, file is updated. Defaults to True.
280
287
display : bool, optional
281
288
Whether or not to display which file is being worked on. Defaults to False.
282
289
290
+ Notes
291
+ -----
292
+ See Notes section from :py:meth:`~Session.to_csv` and :py:meth:`~Session.to_excel`.
293
+
283
294
Examples
284
295
--------
285
296
>>> # axes
@@ -309,12 +320,12 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
309
320
_ , ext = os .path .splitext (fname )
310
321
ext = ext .strip ('.' ) if '.' in ext else 'csv'
311
322
engine = ext_default_engine [ext ]
312
- handler_cls = handler_classes [engine ]
313
- handler = handler_cls (fname , overwrite )
314
- if engine != 'pandas_hdf' :
315
- items = self .filter (kind = LArray ).items ()
323
+ handler_cls = get_file_handler (engine )
324
+ if engine == 'pandas_csv' and 'sep' in kwargs :
325
+ handler = handler_cls (fname , overwrite , kwargs ['sep' ])
316
326
else :
317
- items = self .items ()
327
+ handler = handler_cls (fname , overwrite )
328
+ items = self .items ()
318
329
if names is not None :
319
330
names_set = set (names )
320
331
items = [(k , v ) for k , v in items if k in names_set ]
@@ -387,7 +398,7 @@ def to_globals(self, names=None, depth=0, warn=True, inplace=False):
387
398
388
399
def to_pickle (self , fname , names = None , overwrite = True , display = False , ** kwargs ):
389
400
"""
390
- Dumps all array objects from the current session to a file using pickle.
401
+ Dumps LArray, Axis and Group objects from the current session to a file using pickle.
391
402
392
403
WARNING: never load a pickle file (.pkl or .pickle) from an untrusted source, as it can lead to arbitrary code
393
404
execution.
@@ -397,7 +408,8 @@ def to_pickle(self, fname, names=None, overwrite=True, display=False, **kwargs):
397
408
fname : str
398
409
Path for the dump.
399
410
names : list of str or None, optional
400
- List of names of objects to dump. Defaults to all objects present in the Session.
411
+ Names of LArray/Axis/Group objects to dump.
412
+ Defaults to all objects present in the Session.
401
413
overwrite: bool, optional
402
414
Whether or not to overwrite an existing file, if any.
403
415
If False, file is updated. Defaults to True.
@@ -406,16 +418,21 @@ def to_pickle(self, fname, names=None, overwrite=True, display=False, **kwargs):
406
418
407
419
Examples
408
420
--------
409
- >>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(4), ndtest((3, 2)) # doctest: +SKIP
410
- >>> s = Session([('arr1', arr1), ('arr2', arr2), ('arr3', arr3)]) # doctest: +SKIP
421
+ >>> # axes
422
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
423
+ >>> # groups
424
+ >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
425
+ >>> # arrays
426
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
427
+ >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
411
428
412
429
Save all arrays
413
430
414
431
>>> s.to_pickle('output.pkl') # doctest: +SKIP
415
432
416
- Save only some arrays
433
+ Save only some objects
417
434
418
- >>> s.to_pickle('output.pkl', ['arr1 ', 'arr3 ']) # doctest: +SKIP
435
+ >>> s.to_pickle('output.pkl', ['a ', 'b', 'arr1 ']) # doctest: +SKIP
419
436
"""
420
437
self .save (fname , names , ext_default_engine ['pkl' ], overwrite , display , ** kwargs )
421
438
@@ -462,61 +479,85 @@ def to_hdf(self, fname, names=None, overwrite=True, display=False, **kwargs):
462
479
463
480
def to_excel (self , fname , names = None , overwrite = True , display = False , ** kwargs ):
464
481
"""
465
- Dumps all array objects from the current session to an Excel file.
482
+ Dumps LArray, Axis and Group objects from the current session to an Excel file.
466
483
467
484
Parameters
468
485
----------
469
486
fname : str
470
- Path for the dump.
487
+ Path of the file for the dump.
471
488
names : list of str or None, optional
472
- List of names of objects to dump. Defaults to all objects present in the Session.
489
+ Names of LArray/Axis/Group objects to dump.
490
+ Defaults to all objects present in the Session.
473
491
overwrite: bool, optional
474
492
Whether or not to overwrite an existing file, if any. If False, file is updated. Defaults to True.
475
493
display : bool, optional
476
494
Whether or not to display which file is being worked on. Defaults to False.
477
495
496
+ Notes
497
+ -----
498
+ - each array is saved in a separate sheet
499
+ - all Axis objects are saved together in the same sheet named __axes__
500
+ - all Group objects are saved together in the same sheet named __groups__
501
+
478
502
Examples
479
503
--------
480
- >>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(4), ndtest((3, 2)) # doctest: +SKIP
481
- >>> s = Session([('arr1', arr1), ('arr2', arr2), ('arr3', arr3)]) # doctest: +SKIP
504
+ >>> # axes
505
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
506
+ >>> # groups
507
+ >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
508
+ >>> # arrays
509
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
510
+ >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
482
511
483
512
Save all arrays
484
513
485
514
>>> s.to_excel('output.xlsx') # doctest: +SKIP
486
515
487
- Save only some arrays
516
+ Save only some objects
488
517
489
- >>> s.to_excel('output.xlsx', ['arr1 ', 'arr3 ']) # doctest: +SKIP
518
+ >>> s.to_excel('output.xlsx', ['a ', 'b', 'arr1 ']) # doctest: +SKIP
490
519
"""
491
520
self .save (fname , names , ext_default_engine ['xlsx' ], overwrite , display , ** kwargs )
492
521
493
522
dump_excel = renamed_to (to_excel , 'dump_excel' )
494
523
495
524
def to_csv (self , fname , names = None , display = False , ** kwargs ):
496
525
"""
497
- Dumps all array objects from the current session to CSV files.
526
+ Dumps LArray, Axis and Group objects from the current session to CSV files.
498
527
499
528
Parameters
500
529
----------
501
530
fname : str
502
531
Path for the directory that will contain CSV files.
503
532
names : list of str or None, optional
504
- List of names of objects to dump. Defaults to all objects present in the Session.
533
+ Names of LArray/Axis/Group objects to dump.
534
+ Defaults to all objects present in the Session.
505
535
display : bool, optional
506
536
Whether or not to display which file is being worked on. Defaults to False.
507
537
538
+ Notes
539
+ -----
540
+ - each array is saved in a separate file
541
+ - all Axis objects are saved together in the same CSV file named __axes__.csv
542
+ - all Group objects are saved together in the same CSV file named __groups__.csv
543
+
508
544
Examples
509
545
--------
510
- >>> arr1, arr2, arr3 = ndtest((2, 2)), ndtest(4), ndtest((3, 2)) # doctest: +SKIP
511
- >>> s = Session([('arr1', arr1), ('arr2', arr2), ('arr3', arr3)]) # doctest: +SKIP
546
+ >>> # axes
547
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
548
+ >>> # groups
549
+ >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
550
+ >>> # arrays
551
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
552
+ >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
512
553
513
554
Save all arrays
514
555
515
556
>>> s.to_csv('./Output') # doctest: +SKIP
516
557
517
558
Save only some arrays
518
559
519
- >>> s.to_csv('./Output', ['arr1 ', 'arr3 ']) # doctest: +SKIP
560
+ >>> s.to_csv('./Output', ['a ', 'b', 'arr1 ']) # doctest: +SKIP
520
561
"""
521
562
self .save (fname , names , ext_default_engine ['csv' ], display = display , ** kwargs )
522
563
0 commit comments