@@ -46,6 +46,8 @@ class Session(object):
46
46
47
47
Examples
48
48
--------
49
+ >>> # scalars
50
+ >>> i, s = 5, 'string'
49
51
>>> # axes
50
52
>>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
51
53
>>> # groups
@@ -55,30 +57,31 @@ class Session(object):
55
57
56
58
create a Session by passing a list of pairs (name, object)
57
59
58
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)])
60
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
61
+ ... ('arr1', arr1), ('arr2', arr2)])
59
62
60
63
create a Session using keyword arguments (but you lose order on Python < 3.6)
61
64
62
- >>> s = Session(a=a, b=b, a01=a01, arr1=arr1, arr2=arr2)
65
+ >>> ses = Session(i=i, s=s, a=a, b=b, a01=a01, arr1=arr1, arr2=arr2)
63
66
64
67
create a Session by passing a dictionary (but you lose order on Python < 3.6)
65
68
66
- >>> s = Session({'a': a, 'b': b, 'a01': a01, 'arr1': arr1, 'arr2': arr2})
69
+ >>> ses = Session({'i': i, 's': s, 'a': a, 'b': b, 'a01': a01, 'arr1': arr1, 'arr2': arr2})
67
70
68
71
load Session from file
69
72
70
- >>> s = Session('my_session.h5') # doctest: +SKIP
73
+ >>> ses = Session('my_session.h5') # doctest: +SKIP
71
74
72
75
create a session with metadata
73
76
74
77
>>> # Python <= 3.5
75
- >>> s = Session([('arr1', arr1), ('arr2', arr2)], meta=[('title', 'my title'), ('author', 'John Smith')])
76
- >>> s .meta
78
+ >>> ses = Session([('arr1', arr1), ('arr2', arr2)], meta=[('title', 'my title'), ('author', 'John Smith')])
79
+ >>> ses .meta
77
80
title: my title
78
81
author: John Smith
79
82
>>> # Python 3.6+
80
- >>> s = Session(arr1=arr1, arr2=arr2, meta=Metadata(title='my title', author='John Smith')) # doctest: +SKIP
81
- >>> s .meta
83
+ >>> ses = Session(arr1=arr1, arr2=arr2, meta=Metadata(title='my title', author='John Smith')) # doctest: +SKIP
84
+ >>> ses .meta
82
85
title: my title
83
86
author: John Smith
84
87
"""
@@ -369,46 +372,57 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
369
372
--------
370
373
In one module:
371
374
375
+ >>> # scalars
376
+ >>> i, s = 5, 'string'
372
377
>>> # axes
373
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
378
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
374
379
>>> # groups
375
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
380
+ >>> a01 = a['a0,a1'] >> 'a01'
376
381
>>> # arrays
377
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
378
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
382
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
383
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
384
+ ... ('arr1', arr1), ('arr2', arr2)])
379
385
>>> # metadata
380
- >>> s .meta.title = 'my title' # doctest: +SKIP
381
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
386
+ >>> ses .meta.title = 'my title'
387
+ >>> ses .meta.author = 'John Smith'
382
388
>>> # save the session in an HDF5 file
383
- >>> s .save('input.h5') # doctest: +SKIP
389
+ >>> ses .save('input.h5')
384
390
385
391
In another module: load the whole session
386
392
387
393
>>> # the load method is automatically called when passing
388
394
>>> # the path of file to the Session constructor
389
- >>> s = Session('input.h5') # doctest: +SKIP
390
- >>> s # doctest: +SKIP
391
- Session(a, b, a01, arr1, arr2)
392
- >>> s .meta # doctest: +SKIP
395
+ >>> ses = Session('input.h5')
396
+ >>> ses
397
+ Session(i, s, a, b, a01, arr1, arr2)
398
+ >>> ses .meta
393
399
title: my title
394
400
author: John Smith
395
401
396
402
Load only some objects
397
403
398
- >>> s = Session() # doctest: +SKIP
399
- >>> s.load('input.h5', ['a', 'b', 'arr1', 'arr2']) # doctest: +SKIP
400
- >>> a, b, arr1, arr2 = s['a', 'b', 'arr1', 'arr2'] # doctest: +SKIP
401
- >>> # only if you know the order of arrays stored in session
402
- >>> a, b, a01, arr1, arr2 = s.values() # doctest: +SKIP
404
+ >>> ses = Session()
405
+ >>> ses.load('input.h5', names=['s', 'a', 'b', 'arr1', 'arr2'], display=True)
406
+ opening input.h5
407
+ loading scalar object s ... done
408
+ loading Axis object a ... done
409
+ loading Axis object b ... done
410
+ loading Array object arr1 ... done
411
+ loading Array object arr2 ... done
403
412
404
413
Using .csv files (assuming the same session as above)
405
414
406
- >>> s.save('data') # doctest: +SKIP
407
- >>> s = Session() # doctest: +SKIP
408
- >>> # load all .csv files starting with "output" in the data directory
409
- >>> s.load('data') # doctest: +SKIP
410
- >>> # or only arrays (i.e. all CSV files starting with 'arr')
411
- >>> s.load('data/arr*.csv') # doctest: +SKIP
415
+ >>> ses.save('data')
416
+ >>> ses = Session()
417
+ >>> # load all .csv files from the 'data' directory
418
+ >>> ses.load('data', display=True)
419
+ opening data
420
+ loading Array object arr1 ... done
421
+ loading Array object arr2 ... done
422
+ >>> # or only arrays containing the character '1' in their names
423
+ >>> ses.load('data/*1.csv', display=True)
424
+ opening data/*1.csv
425
+ loading Array object arr1 ... done
412
426
"""
413
427
if display :
414
428
print ("opening" , fname )
@@ -434,15 +448,15 @@ def load(self, fname, names=None, engine='auto', display=False, **kwargs):
434
448
def save (self , fname , names = None , engine = 'auto' , overwrite = True , display = False , ** kwargs ):
435
449
r"""
436
450
Dumps objects from the current session to a file, or several .csv files.
437
- The Excel and CSV formats only accepts objects of Array type (plus metadata).
451
+ The Excel and CSV formats only dumps objects of Array type (plus metadata).
438
452
439
453
Parameters
440
454
----------
441
455
fname : str
442
456
Path of the file for the dump.
443
457
If objects are saved in CSV files, the path corresponds to a directory.
444
458
names : list of str or None, optional
445
- List of names of Array/Axis/Group objects to dump.
459
+ List of names of objects to dump.
446
460
If `fname` is None, list of paths to CSV files.
447
461
Defaults to all objects present in the Session.
448
462
engine : {'auto', 'pandas_csv', 'pandas_hdf', 'pandas_excel', 'xlwings_excel', 'pickle'}, optional
@@ -455,31 +469,48 @@ def save(self, fname, names=None, engine='auto', overwrite=True, display=False,
455
469
456
470
Examples
457
471
--------
472
+ >>> # scalars
473
+ >>> i, s = 5, 'string'
458
474
>>> # axes
459
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
475
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
460
476
>>> # groups
461
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
477
+ >>> a01 = a['a0,a1'] >> 'a01'
462
478
>>> # arrays
463
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
464
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
479
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
480
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
481
+ ... ('arr1', arr1), ('arr2', arr2)])
465
482
>>> # metadata
466
- >>> s .meta.title = 'my title' # doctest: +SKIP
467
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
483
+ >>> ses .meta.title = 'my title'
484
+ >>> ses .meta.author = 'John Smith'
468
485
469
486
Save all objects
470
487
471
- >>> s.save('output.h5') # doctest: +SKIP
488
+ >>> ses.save('output.h5', display=True)
489
+ dumping i ... done
490
+ dumping s ... done
491
+ dumping a ... done
492
+ dumping b ... done
493
+ dumping a01 ... done
494
+ dumping arr1 ... done
495
+ dumping arr2 ... done
472
496
473
497
Save only some objects
474
498
475
- >>> s.save('output.h5', ['a', 'b', 'arr1']) # doctest: +SKIP
499
+ >>> ses.save('output.h5', names=['s', 'a', 'b', 'arr1', 'arr2'], display=True)
500
+ dumping s ... done
501
+ dumping a ... done
502
+ dumping b ... done
503
+ dumping arr1 ... done
504
+ dumping arr2 ... done
476
505
477
506
Update file
478
507
479
- >>> arr1, arr4 = ndtest((3, 3)), ndtest((2, 3)) # doctest: +SKIP
480
- >>> s2 = Session([('arr1', arr1), ('arr4', arr4)]) # doctest: +SKIP
508
+ >>> arr1, arr4 = ndtest((3, 3)), ndtest((2, 3))
509
+ >>> ses2 = Session([('arr1', arr1), ('arr4', arr4)])
481
510
>>> # replace arr1 and add arr4 in file output.h5
482
- >>> s2.save('output.h5', overwrite=False) # doctest: +SKIP
511
+ >>> ses2.save('output.h5', overwrite=False, display=True)
512
+ dumping arr1 ... done
513
+ dumping arr4 ... done
483
514
"""
484
515
if engine == 'auto' :
485
516
_ , ext = os .path .splitext (fname )
@@ -584,24 +615,39 @@ def to_pickle(self, fname, names=None, overwrite=True, display=False, **kwargs):
584
615
585
616
Examples
586
617
--------
618
+ >>> # scalars
619
+ >>> i, s = 5, 'string'
587
620
>>> # axes
588
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
621
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
589
622
>>> # groups
590
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
623
+ >>> a01 = a['a0,a1'] >> 'a01'
591
624
>>> # arrays
592
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
593
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
625
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
626
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
627
+ ... ('arr1', arr1), ('arr2', arr2)])
594
628
>>> # metadata
595
- >>> s .meta.title = 'my title' # doctest: +SKIP
596
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
629
+ >>> ses .meta.title = 'my title'
630
+ >>> ses .meta.author = 'John Smith'
597
631
598
- Save all arrays
632
+ Save all objects
599
633
600
- >>> s.to_pickle('output.pkl') # doctest: +SKIP
634
+ >>> ses.to_pickle('output.pkl', display=True)
635
+ dumping i ... done
636
+ dumping s ... done
637
+ dumping a ... done
638
+ dumping b ... done
639
+ dumping a01 ... done
640
+ dumping arr1 ... done
641
+ dumping arr2 ... done
601
642
602
643
Save only some objects
603
644
604
- >>> s.to_pickle('output.pkl', ['a', 'b', 'arr1']) # doctest: +SKIP
645
+ >>> ses.to_pickle('output.pkl', names=['s', 'a', 'b', 'arr1', 'arr2'], display=True)
646
+ dumping s ... done
647
+ dumping a ... done
648
+ dumping b ... done
649
+ dumping arr1 ... done
650
+ dumping arr2 ... done
605
651
"""
606
652
self .save (fname , names , ext_default_engine ['pkl' ], overwrite , display , ** kwargs )
607
653
@@ -626,24 +672,39 @@ def to_hdf(self, fname, names=None, overwrite=True, display=False, **kwargs):
626
672
627
673
Examples
628
674
--------
675
+ >>> # scalars
676
+ >>> i, s = 5, 'string'
629
677
>>> # axes
630
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
678
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
631
679
>>> # groups
632
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
680
+ >>> a01 = a['a0,a1'] >> 'a01'
633
681
>>> # arrays
634
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
635
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
682
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
683
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
684
+ ... ('arr1', arr1), ('arr2', arr2)])
636
685
>>> # metadata
637
- >>> s .meta.title = 'my title' # doctest: +SKIP
638
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
686
+ >>> ses .meta.title = 'my title'
687
+ >>> ses .meta.author = 'John Smith'
639
688
640
- Save all arrays
689
+ Save all objects
641
690
642
- >>> s.to_hdf('output.h5') # doctest: +SKIP
691
+ >>> ses.to_hdf('output.h5', display=True)
692
+ dumping i ... done
693
+ dumping s ... done
694
+ dumping a ... done
695
+ dumping b ... done
696
+ dumping a01 ... done
697
+ dumping arr1 ... done
698
+ dumping arr2 ... done
643
699
644
700
Save only some objects
645
701
646
- >>> s.to_hdf('output.h5', ['a', 'b', 'arr1']) # doctest: +SKIP
702
+ >>> ses.to_hdf('output.h5', names=['s', 'a', 'b', 'arr1', 'arr2'], display=True)
703
+ dumping s ... done
704
+ dumping a ... done
705
+ dumping b ... done
706
+ dumping arr1 ... done
707
+ dumping arr2 ... done
647
708
"""
648
709
self .save (fname , names , ext_default_engine ['hdf' ], overwrite , display , ** kwargs )
649
710
@@ -672,24 +733,35 @@ def to_excel(self, fname, names=None, overwrite=True, display=False, **kwargs):
672
733
673
734
Examples
674
735
--------
736
+ >>> # scalars
737
+ >>> i, s = 5, 'string'
675
738
>>> # axes
676
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
739
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
677
740
>>> # groups
678
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
741
+ >>> a01 = a['a0,a1'] >> 'a01'
679
742
>>> # arrays
680
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
681
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
743
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
744
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
745
+ ... ('arr1', arr1), ('arr2', arr2)])
682
746
>>> # metadata
683
- >>> s .meta.title = 'my title' # doctest: +SKIP
684
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
747
+ >>> ses .meta.title = 'my title'
748
+ >>> ses .meta.author = 'John Smith'
685
749
686
- Save all arrays
750
+ Save all arrays (and arrays only)
687
751
688
- >>> s.to_excel('output.xlsx') # doctest: +SKIP
752
+ >>> ses.to_excel('output.xlsx', display=True)
753
+ dumping i ... Cannot dump i. int is not a supported type
754
+ dumping s ... Cannot dump s. str is not a supported type
755
+ dumping a ... Cannot dump a. Axis is not a supported type
756
+ dumping b ... Cannot dump b. Axis is not a supported type
757
+ dumping a01 ... Cannot dump a01. LGroup is not a supported type
758
+ dumping arr1 ... done
759
+ dumping arr2 ... done
689
760
690
- Save only some objects
761
+ Save only some arrays
691
762
692
- >>> s.to_excel('output.xlsx', ['a', 'b', 'arr1']) # doctest: +SKIP
763
+ >>> ses.to_excel('output.xlsx', names=['arr1'], display=True)
764
+ dumping arr1 ... done
693
765
"""
694
766
self .save (fname , names , ext_default_engine ['xlsx' ], overwrite , display , ** kwargs )
695
767
@@ -716,24 +788,35 @@ def to_csv(self, fname, names=None, display=False, **kwargs):
716
788
717
789
Examples
718
790
--------
791
+ >>> # scalars
792
+ >>> i, s = 5, 'string'
719
793
>>> # axes
720
- >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2") # doctest: +SKIP
794
+ >>> a, b = Axis("a=a0..a2"), Axis("b=b0..b2")
721
795
>>> # groups
722
- >>> a01 = a['a0,a1'] >> 'a01' # doctest: +SKIP
796
+ >>> a01 = a['a0,a1'] >> 'a01'
723
797
>>> # arrays
724
- >>> arr1, arr2 = ndtest((a, b)), ndtest(a) # doctest: +SKIP
725
- >>> s = Session([('a', a), ('b', b), ('a01', a01), ('arr1', arr1), ('arr2', arr2)]) # doctest: +SKIP
798
+ >>> arr1, arr2 = ndtest((a, b)), ndtest(a)
799
+ >>> ses = Session([('i', i), ('s', s), ('a', a), ('b', b), ('a01', a01),
800
+ ... ('arr1', arr1), ('arr2', arr2)])
726
801
>>> # metadata
727
- >>> s .meta.title = 'my title' # doctest: +SKIP
728
- >>> s .meta.author = 'John Smith' # doctest: +SKIP
802
+ >>> ses .meta.title = 'my title'
803
+ >>> ses .meta.author = 'John Smith'
729
804
730
- Save all arrays
805
+ Save all arrays (and arrays only)
731
806
732
- >>> s.to_csv('./Output') # doctest: +SKIP
807
+ >>> ses.to_csv('output', display=True)
808
+ dumping i ... Cannot dump i. int is not a supported type
809
+ dumping s ... Cannot dump s. str is not a supported type
810
+ dumping a ... Cannot dump a. Axis is not a supported type
811
+ dumping b ... Cannot dump b. Axis is not a supported type
812
+ dumping a01 ... Cannot dump a01. LGroup is not a supported type
813
+ dumping arr1 ... done
814
+ dumping arr2 ... done
733
815
734
816
Save only some arrays
735
817
736
- >>> s.to_csv('./Output', ['a', 'b', 'arr1']) # doctest: +SKIP
818
+ >>> ses.to_csv('output', names=['arr1'], display=True)
819
+ dumping arr1 ... done
737
820
"""
738
821
self .save (fname , names , ext_default_engine ['csv' ], display = display , ** kwargs )
739
822
0 commit comments