/
store.py
1787 lines (1362 loc) · 64.6 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Saving and loading file-like objects.
"""
import copy
import csv
import json
import operator
import os
import pathlib
import pickle
import subprocess
import warnings
import zipfile
import pandas as pd
from .ops import confirmed, find_executable, is_url
def _check_path_to_file(path_to_file, verbose=False, verbose_end=" ... ", ret_info=False):
"""
Get information about the path of a file.
:param path_to_file: path where a file is saved
:type path_to_file: str or pathlib.Path
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param verbose_end: a string passed to ``end`` for ``print``, defaults to ``" ... "``
:type verbose_end: str
:param ret_info: whether to return the file path information, defaults to ``False``
:type ret_info: bool
:return: a relative path and a filename (if ``ret_info=True``)
:rtype: tuple
**Test**::
>>> from pyhelpers.dir import cd
>>> from pyhelpers.store import _check_path_to_file
>>> file_path = cd()
>>> try:
... _check_path_to_file(file_path, verbose=True)
... except AssertionError as e:
... print(e)
The input for `path_to_file` may not be a file path.
>>> file_path = cd("test_store.py")
>>> _check_path_to_file(file_path, verbose=True)
>>> print("Passed.")
Saving "test_store.py" ... Pass.
>>> file_path = cd("tests", "test_store.py")
>>> _check_path_to_file(file_path, verbose=True)
>>> print("Passed.")
Updating "test_store.py" at "tests\\" ... Passed.
"""
abs_path_to_file = pathlib.Path(path_to_file).absolute()
assert not abs_path_to_file.is_dir(), "The input for `path_to_file` may not be a file path."
filename = pathlib.Path(abs_path_to_file).name if abs_path_to_file.suffix else ""
try:
rel_path = pathlib.Path(os.path.relpath(abs_path_to_file.parent))
if rel_path == rel_path.parent:
rel_path = abs_path_to_file.parent
msg_fmt = "{} \"{}\""
else:
msg_fmt = "{} \"{}\" {} \"{}\\\""
# The specified path exists?
os.makedirs(abs_path_to_file.parent, exist_ok=True)
except ValueError:
if verbose == 2:
warn_msg = "Warning: \"{}\" is outside the current working directory".format(
str(abs_path_to_file.parent))
print(warn_msg)
rel_path = abs_path_to_file.parent
msg_fmt = "{} \"{}\""
if verbose:
if os.path.isfile(abs_path_to_file):
status_msg, prep = "Updating", "at"
else:
status_msg, prep = "Saving", "to"
if verbose_end:
verbose_end_ = verbose_end
else:
verbose_end_ = "\n"
if rel_path == rel_path.parent:
print(msg_fmt.format(status_msg, filename), end=verbose_end_)
else:
print(msg_fmt.format(status_msg, filename, prep, rel_path), end=verbose_end_)
if ret_info:
return rel_path, filename
""" == Save data ============================================================================= """
# Pickle files
def save_pickle(pickle_data, path_to_pickle, verbose=False, **kwargs):
"""
Save data to a `Pickle <https://docs.python.org/3/library/pickle.html>`_ file.
:param pickle_data: data that could be dumped by the built-in module `pickle.dump`_
:type pickle_data: any
:param path_to_pickle: path where a pickle file is saved
:type path_to_pickle: str or os.PathLike[str]
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `pickle.dump`_
.. _`pickle.dump`: https://docs.python.org/3/library/pickle.html#pickle.dump
**Example**::
>>> from pyhelpers.store import save_pickle
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> pickle_dat = 1
>>> pickle_pathname = cd("tests\\data", "dat.pickle")
>>> save_pickle(pickle_dat, pickle_pathname, verbose=True)
Saving "dat.pickle" to "tests\\data\\" ... Done.
>>> # Get an example dataframe
>>> pickle_dat = example_dataframe()
>>> pickle_dat
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> save_pickle(pickle_dat, pickle_pathname, verbose=True)
Updating "dat.pickle" at "tests\\data\\" ... Done.
.. seealso::
- Examples for the function :py:func:`pyhelpers.store.load_pickle`.
"""
_check_path_to_file(path_to_pickle, verbose=verbose, ret_info=False)
try:
pickle_out = open(path_to_pickle, mode='wb')
pickle.dump(pickle_data, pickle_out, **kwargs)
pickle_out.close()
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e))
# Spreadsheets
def save_spreadsheet(spreadsheet_data, path_to_spreadsheet, index=False, engine=None, delimiter=',',
verbose=False, **kwargs):
"""
Save data to a `CSV <https://en.wikipedia.org/wiki/Comma-separated_values>`_ or
an `Microsoft Excel <https://en.wikipedia.org/wiki/Microsoft_Excel>`_ file.
The file extension can be `".txt"`, `".csv"`, `".xlsx"` or `".xls"`;
engines can include: `xlsxwriter`_ (for .xlsx) and `openpyxl`_ (for .xlsx/.xlsm).
:param spreadsheet_data: data that could be saved as a spreadsheet
(e.g. with a file extension ".xlsx" or ".csv")
:type spreadsheet_data: pandas.DataFrame
:param path_to_spreadsheet: path where a spreadsheet is saved
:type path_to_spreadsheet: str or os.PathLike[str] or None
:param index: whether to include the index as a column, defaults to ``False``
:type index: bool
:param engine: options include ``'openpyxl'`` for latest Excel file formats,
``'xlrd'`` for .xls ``'odf'`` for OpenDocument file formats (.odf, .ods, .odt), and
``'pyxlsb'`` for Binary Excel files; defaults to ``None``
:type engine: str or None
:param delimiter: separator for saving a `".xlsx"` (or `".xls"`) file as a `".csv"` file,
defaults to ``','``
:type delimiter: str
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `pandas.DataFrame.to_excel`_ or `pandas.DataFrame.to_csv`_
.. _`xlsxwriter`: https://pypi.org/project/XlsxWriter/
.. _`openpyxl`: https://pypi.org/project/openpyxl/
.. _`pandas.DataFrame.to_excel`:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html
.. _`pandas.DataFrame.to_csv`:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html
**Examples**::
>>> from pyhelpers.store import save_spreadsheet
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> # Get an example dataframe
>>> spreadsheet_dat = example_dataframe()
>>> spreadsheet_dat
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> spreadsheet_pathname = cd("tests\\data", "dat.csv")
>>> save_spreadsheet(spreadsheet_dat, spreadsheet_pathname, index=True, verbose=True)
Saving "dat.csv" to "tests\\data\\" ... Done.
>>> spreadsheet_pathname = cd("tests\\data", "dat.xls")
>>> save_spreadsheet(spreadsheet_dat, spreadsheet_pathname, verbose=True)
Saving "dat.xls" to "tests\\data\\" ... Done.
>>> spreadsheet_pathname = cd("tests\\data", "dat.xlsx")
>>> save_spreadsheet(spreadsheet_dat, spreadsheet_pathname, verbose=True)
Saving "dat.xlsx" to "tests\\data\\" ... Done.
"""
_, spreadsheet_filename = _check_path_to_file(
path_to_file=path_to_spreadsheet, verbose=verbose, ret_info=True)
try: # to save the data
if spreadsheet_filename.endswith(".xlsx"): # a .xlsx file
spreadsheet_data.to_excel(
excel_writer=path_to_spreadsheet, index=index, engine=engine, **kwargs)
elif spreadsheet_filename.endswith(".xls"): # a .xls file
if engine is None or engine == 'xlwt':
engine = 'openpyxl'
spreadsheet_data.to_excel(
excel_writer=path_to_spreadsheet, index=index, engine=engine, **kwargs)
elif spreadsheet_filename.endswith((".csv", ".txt")): # a .csv file
spreadsheet_data.to_csv(
path_or_buf=path_to_spreadsheet, index=index, sep=delimiter, **kwargs)
else:
raise AssertionError('File extension must be ".txt", ".csv", ".xlsx" or ".xls"')
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e.args[0])) if verbose else ""
def save_multiple_spreadsheets(spreadsheets_data, sheet_names, path_to_spreadsheet, mode='w',
index=False, confirmation_required=True, verbose=False, **kwargs):
"""
Save data to a multi-sheet `Microsoft Excel`_ file.
The file extension can be `".xlsx"` or `".xls"`.
:param spreadsheets_data: a sequence of pandas.DataFrame
:type spreadsheets_data: list or tuple or iterable
:param sheet_names: all sheet names of an Excel workbook
:type sheet_names: list or tuple or iterable
:param path_to_spreadsheet: path where a spreadsheet is saved
:type path_to_spreadsheet: str or os.PathLike[str]
:param mode: mode to write to an Excel file; ``'w'`` (default) for 'write' and ``'a'`` for 'append'
:type mode: str
:param index: whether to include the index as a column, defaults to ``False``
:type index: bool
:param confirmation_required: whether to prompt a message for confirmation to proceed,
defaults to ``True``
:type confirmation_required: bool
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `pandas.ExcelWriter`_
.. _`Microsoft Excel`: https://en.wikipedia.org/wiki/Microsoft_Excel
.. _pandas.ExcelWriter:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html
**Examples**::
>>> from pyhelpers.store import save_multiple_spreadsheets
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> # Get an example dataframe
>>> dat1 = example_dataframe()
>>> dat1
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> dat2 = dat1.T
>>> dat2
London Birmingham Manchester Leeds
Easting 530034 406689 383819 582044
Northing 180381 286822 398052 152953
>>> dat = [dat1, dat2]
>>> dat_sheets = ['TestSheet1', 'TestSheet2']
>>> dat_pathname = cd("tests\\data", "dat.xlsx")
>>> save_multiple_spreadsheets(dat, dat_sheets, dat_pathname, index=True, verbose=True)
Saving "dat.xlsx" to "tests\\data\\" ...
'TestSheet1' ... Done.
'TestSheet2' ... Done.
>>> save_multiple_spreadsheets(dat, dat_sheets, dat_pathname, mode='a', index=True,
... verbose=True)
Updating "dat.xlsx" at "tests\\data\\" ...
'TestSheet1' ... This sheet already exists; [pass]|new|replace: new
saved as 'TestSheet11' ... Done.
'TestSheet2' ... This sheet already exists; [pass]|new|replace: new
saved as 'TestSheet21' ... Done.
>>> save_multiple_spreadsheets(dat, dat_sheets, dat_pathname, mode='a', index=True,
... confirmation_required=False, verbose=True)
Updating "dat.xlsx" at "tests\\dataz\" ...
'TestSheet1' ... Failed. Sheet 'TestSheet1' already exists and if_sheet_exists is se ...
'TestSheet2' ... Failed. Sheet 'TestSheet2' already exists and if_sheet_exists is se ...
>>> save_multiple_spreadsheets(dat, dat_sheets, dat_pathname, mode='a', index=True,
... confirmation_required=False, verbose=True,
... if_sheet_exists='replace')
Updating "dat.xlsx" at "tests\\data\\" ...
'TestSheet1' ... Done.
'TestSheet2' ... Done.
"""
assert path_to_spreadsheet.endswith(".xlsx") or path_to_spreadsheet.endswith(".xls")
_check_path_to_file(path_to_spreadsheet, verbose=verbose, ret_info=False)
if os.path.isfile(path_to_spreadsheet) and mode == 'a':
excel_file = pd.ExcelFile(path_or_buffer=path_to_spreadsheet)
cur_sheet_names = excel_file.sheet_names
excel_file.close()
else:
cur_sheet_names = []
engine = 'openpyxl' if mode == 'a' else None
excel_writer = pd.ExcelWriter(path=path_to_spreadsheet, engine=engine, mode=mode, **kwargs)
def _write_excel():
try:
sheet_data.to_excel(excel_writer, sheet_name=sheet_name, index=index)
try:
sheet_name_ = excel_writer.sheets[sheet_name].get_name()
except AttributeError:
sheet_name_ = excel_writer.sheets[sheet_name].title
if sheet_name_ in sheet_names:
msg_ = "Done."
else:
msg_ = "saved as '{}' ... Done. ".format(sheet_name_)
print(msg_) if verbose else ""
except Exception as e:
print("Failed. {}".format(e))
print("") if verbose else ""
for sheet_data, sheet_name in zip(spreadsheets_data, sheet_names):
# sheet_data, sheet_name = spreadsheets_data[0], sheet_names[0]
print("\t'{}'".format(sheet_name), end=" ... ") if verbose else ""
if (sheet_name in cur_sheet_names) and confirmation_required:
if_sheet_exists = input(f"This sheet already exists; [pass]|new|replace: ")
if if_sheet_exists != 'pass':
excel_writer.if_sheet_exists = if_sheet_exists
print("\t\t", end="")
# suffix_msg = "(Note that a suffix has been added to the sheet name.)"
_write_excel()
else:
_write_excel()
excel_writer.close()
# JSON files
def save_json(json_data, path_to_json, method=None, verbose=False, **kwargs):
"""
Save data to a `JSON <https://www.json.org/json-en.html>`_ file.
:param json_data: data that could be dumped by as a JSON file
:type json_data: any json data
:param path_to_json: path where a json file is saved
:type path_to_json: str or os.PathLike[str]
:param method: an open-source module used for JSON serialization, options include
``None`` (default, for the built-in `json module`_), ``'orjson'`` (for `orjson`_) and
``'rapidjson'`` (for `python-rapidjson`_)
:type method: str or None
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `json.dump`_ (if ``method=None``),
`orjson.dumps`_ (if ``method='orjson'``) or `rapidjson.dump`_ (if ``method='rapidjson'``)
.. _`json module`: https://docs.python.org/3/library/json.html
.. _`orjson`: https://pypi.org/project/orjson/
.. _`python-rapidjson`: https://pypi.org/project/python-rapidjson
.. _`json.dump`: https://docs.python.org/3/library/json.html#json.dump
.. _`orjson.dumps`: https://github.com/ijl/orjson#serialize
.. _`rapidjson.dump`: https://python-rapidjson.readthedocs.io/en/latest/dump.html
**Examples**::
>>> from pyhelpers.store import save_json
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> json_pathname = cd("tests\\data", "dat.json")
>>> json_dat = {'a': 1, 'b': 2, 'c': 3, 'd': ['a', 'b', 'c']}
>>> save_json(json_dat, json_pathname, indent=4, verbose=True)
Saving "dat.json" to "tests\\data\\" ... Done.
>>> # Get an example dataframe
>>> example_df = example_dataframe()
>>> example_df
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> # Convert the dataframe to JSON format
>>> json_dat = json.loads(example_df.to_json(orient='index'))
>>> json_dat
{'London': {'Easting': 530034, 'Northing': 180381},
'Birmingham': {'Easting': 406689, 'Northing': 286822},
'Manchester': {'Easting': 383819, 'Northing': 398052},
'Leeds': {'Easting': 582044, 'Northing': 152953}}
>>> save_json(json_dat, json_pathname, indent=4, verbose=True)
Updating "dat.json" at "tests\\data\\" ... Done.
>>> save_json(json_dat, json_pathname, method='orjson', verbose=True)
Updating "dat.json" at "tests\\data\\" ... Done.
>>> save_json(json_dat, json_pathname, method='rapidjson', indent=4, verbose=True)
Updating "dat.json" at "tests\\data\\" ... Done.
"""
_check_path_to_file(path_to_json, verbose=verbose, ret_info=False)
try:
if method == 'orjson':
import orjson
json_out = open(path_to_json, mode='wb')
json_dumps = orjson.dumps(json_data, **kwargs)
json_out.write(json_dumps)
else:
json_out = open(path_to_json, mode='w')
if method == 'rapidjson':
import rapidjson
rapidjson.dump(json_data, json_out, **kwargs)
else:
json.dump(json_data, json_out, **kwargs)
json_out.close()
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e))
# Joblib
def save_joblib(joblib_data, path_to_joblib, verbose=False, **kwargs):
"""
Save data to a `Joblib <https://pypi.org/project/joblib/>`_ file.
:param joblib_data: data that could be dumped by `joblib.dump`_
:type joblib_data: any
:param path_to_joblib: path where a pickle file is saved
:type path_to_joblib: str or os.PathLike[str]
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `joblib.dump`_
.. _`joblib.dump`: https://joblib.readthedocs.io/en/latest/generated/joblib.dump.html
**Example**::
>>> from pyhelpers.store import save_joblib
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> import numpy
>>> joblib_pathname = cd("tests\\data", "dat.joblib")
>>> # Example 1:
>>> joblib_dat = example_dataframe().to_numpy(dtype=int)
>>> joblib_dat
array([[530034, 180381],
[406689, 286822],
[383819, 398052],
[582044, 152953]])
>>> save_joblib(joblib_dat, joblib_pathname, verbose=True)
Saving "dat.joblib" to "tests\\data\\" ... Done.
>>> # Example 2:
>>> numpy.random.seed(0)
>>> joblib_dat = numpy.random.rand(100, 100)
>>> joblib_dat
array([[0.5488135 , 0.71518937, 0.60276338, ..., 0.02010755, 0.82894003,
0.00469548],
[0.67781654, 0.27000797, 0.73519402, ..., 0.25435648, 0.05802916,
0.43441663],
[0.31179588, 0.69634349, 0.37775184, ..., 0.86219152, 0.97291949,
0.96083466],
...,
[0.89111234, 0.26867428, 0.84028499, ..., 0.5736796 , 0.73729114,
0.22519844],
[0.26969792, 0.73882539, 0.80714479, ..., 0.94836806, 0.88130699,
0.1419334 ],
[0.88498232, 0.19701397, 0.56861333, ..., 0.75842952, 0.02378743,
0.81357508]])
>>> save_joblib(joblib_dat, joblib_pathname, verbose=True)
Saving "dat.joblib" to "tests\\data\\" ... Done.
"""
_check_path_to_file(path_to_joblib, verbose=verbose, ret_info=False)
try:
import joblib
joblib.dump(value=joblib_data, filename=path_to_joblib, **kwargs)
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e))
# Feather files
def save_feather(feather_data, path_to_feather, verbose=False, **kwargs):
"""
Save a dataframe to a `Feather <https://arrow.apache.org/docs/python/feather.html>`_ file.
:param feather_data: a dataframe to be saved as a feather-formatted file
:type feather_data: pandas.DataFrame
:param path_to_feather: path where a feather file is saved
:type path_to_feather: str or os.PathLike[str]
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param kwargs: [optional] parameters of `pandas.DataFrame.to_feather`_
.. _`pandas.DataFrame.to_feather`:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_feather.html
**Example**::
>>> from pyhelpers.store import save_feather
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> # Get an example dataframe
>>> feather_dat = example_dataframe()
>>> feather_dat
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> feather_pathname = cd("tests\\data", "dat.feather")
>>> save_feather(feather_dat.reset_index(), feather_pathname, verbose=True)
Saving "dat.feather" to "tests\\data\\" ... Done.
>>> save_feather(feather_dat, feather_pathname, verbose=True)
Updating "dat.feather" at "tests\\data\\" ... Done.
"""
assert isinstance(feather_data, pd.DataFrame)
_check_path_to_file(path_to_feather, verbose=verbose, ret_info=False)
try:
if list(feather_data.index) != range(len(feather_data)):
feather_data.reset_index().to_feather(path_to_feather, **kwargs)
else:
feather_data.to_feather(path_to_feather, **kwargs)
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e)) if verbose else ""
# Images
def save_svg_as_emf(path_to_svg, path_to_emf, verbose=False, inkscape_exe=None, **kwargs):
"""
Save a `SVG <https://en.wikipedia.org/wiki/Scalable_Vector_Graphics>`_ file (.svg) as
a `EMF <https://en.wikipedia.org/wiki/Windows_Metafile#EMF>`_ file (.emf).
:param path_to_svg: path where a .svg file is saved
:type path_to_svg: str
:param path_to_emf: path where a .emf file is saved
:type path_to_emf: str
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param inkscape_exe: absolute path to 'inkscape.exe', defaults to ``None``;
on Windows, the default installation path
"*C:\\\\Program Files\\\\Inkscape\\\\bin\\\\inkscape.exe*"
(or "*C:\\\\Program Files\\\\Inkscape\\\\inkscape.exe*") is used when ``inkscape_exe=None``
:type inkscape_exe: str or None
:param kwargs: [optional] parameters of `subprocess.call`_
.. _`subprocess.call`:
https://docs.python.org/3/library/subprocess.html#subprocess.call
**Examples**::
>>> from pyhelpers.store import save_svg_as_emf
>>> import matplotlib.pyplot as plt
>>> from pyhelpers.dir import cd
>>> x, y = (1, 1), (2, 2)
>>> plt.figure()
>>> plt.plot([x[0], y[0]], [x[1], y[1]])
>>> plt.show()
The above exmaple is illustrated in :numref:`fig-2`:
.. figure:: ../_images/fig.*
:name: fig-2
:align: center
:width: 76%
An example figure created for the function
:py:func:`save_svg_as_emf()<pyhelpers.store.save_svg_as_emf>`.
.. code-block:: python
>>> img_dir = cd("tests\\images")
>>> svg_file_pathname = cd(img_dir, "fig.svg")
>>> plt.savefig(svg_file_pathname) # Save the figure as a .svg file
>>> emf_file_pathname = cd(img_dir, "fig.emf")
>>> save_svg_as_emf(svg_file_pathname, emf_file_pathname, verbose=True)
Saving the .svg file as "tests\\images\\fig.emf" ... Done.
>>> plt.close()
"""
abs_svg_path, abs_emf_path = map(pathlib.Path, (path_to_svg, path_to_emf))
assert abs_svg_path.suffix.lower() == ".svg"
inkscape_exe_ = copy.copy(inkscape_exe)
if inkscape_exe_ is None:
exe_pathnames = [
"C:\\Program Files\\Inkscape\\inkscape.exe",
"C:\\Program Files\\Inkscape\\bin\\inkscape.exe",
]
inkscape_exe_ = find_executable(app_name="inkscape.exe", possibilities=exe_pathnames)
if os.path.isfile(inkscape_exe_):
if verbose:
if abs_emf_path.exists():
msg = f"Updating \"{abs_emf_path.name}\" at \"{os.path.relpath(abs_emf_path.parent)}\\\""
else:
msg = f"Saving the {abs_svg_path.suffix} file as \"{os.path.relpath(abs_emf_path)}\""
print(msg, end=" ... ")
try:
abs_emf_path.parent.mkdir(exist_ok=True)
subprocess.call([inkscape_exe_, '-z', path_to_svg, '-M', path_to_emf], **kwargs)
if verbose:
print("Done.")
except Exception as e:
print("Failed. {}".format(e))
else:
if verbose:
print("\"Inkscape\" (https://inkscape.org) is required to convert a SVG file to an EMF file;"
" however, it is not found on this device.\nInstall it and then try again.")
def save_fig(path_to_fig_file, dpi=None, verbose=False, conv_svg_to_emf=False, **kwargs):
"""
Save a figure object to a file of a supported file format.
This function relies on `matplotlib.pyplot.savefig`_ (and `Inkscape`_).
:param path_to_fig_file: path where a figure file is saved
:type path_to_fig_file: str or os.PathLike[str]
:param dpi: the resolution in dots per inch; if ``None`` (default), use ``rcParams['savefig.dpi']``
:type dpi: int or None
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool or int
:param conv_svg_to_emf: whether to convert a .svg file to a .emf file, defaults to ``False``
:type conv_svg_to_emf: bool
:param kwargs: [optional] parameters of `matplotlib.pyplot.savefig`_
.. _`matplotlib.pyplot.savefig`:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
.. _`Inkscape`: https://inkscape.org
**Examples**::
>>> from pyhelpers.store import save_fig
>>> from pyhelpers.dir import cd
>>> import matplotlib.pyplot as plt
>>> x, y = (1, 1), (2, 2)
>>> plt.figure()
>>> plt.plot([x[0], y[0]], [x[1], y[1]])
>>> plt.show()
The above exmaple is illustrated in :numref:`fig-1`:
.. figure:: ../_images/fig.*
:name: fig-1
:align: center
:width: 76%
An example figure created for the function :py:func:`save_fig()<pyhelpers.store.save_fig>`.
.. code-block:: python
>>> img_dir = cd("tests\\images")
>>> png_file_pathname = cd(img_dir, "fig.png")
>>> save_fig(png_file_pathname, dpi=300, verbose=True)
Saving "fig.png" to "tests\\images\\" ... Done.
>>> svg_file_pathname = cd(img_dir, "fig.svg")
>>> save_fig(svg_file_pathname, dpi=300, verbose=True, conv_svg_to_emf=True)
Saving "fig.svg" to "tests\\images\\" ... Done.
Saving the .svg file as "tests\\images\\fig.emf" ... Done.
>>> plt.close()
"""
_check_path_to_file(path_to_fig_file, verbose=verbose, ret_info=False)
file_ext = pathlib.Path(path_to_fig_file).suffix
try:
import matplotlib.pyplot
matplotlib.pyplot.savefig(path_to_fig_file, dpi=dpi, **kwargs)
print("Done.") if verbose else ""
except Exception as e:
print("Failed. {}.".format(e)) if verbose else ""
if file_ext == ".svg" and conv_svg_to_emf:
save_svg_as_emf(path_to_fig_file, path_to_fig_file.replace(file_ext, ".emf"), verbose=verbose)
# Web page
def save_web_page_as_pdf(web_page, path_to_pdf, page_size='A4', zoom=1.0, encoding='UTF-8',
verbose=False, wkhtmltopdf_exe=None, **kwargs):
"""
Save a web page as a `PDF <https://en.wikipedia.org/wiki/PDF>`_ file
by `wkhtmltopdf <https://wkhtmltopdf.org/>`_.
:param web_page: URL of a web page or pathname of an HTML file
:type web_page: str
:param path_to_pdf: path where a .pdf is saved
:type path_to_pdf: str
:param page_size: page size, defaults to ``'A4'``
:type page_size: str
:param zoom: a parameter to zoom in/out, defaults to ``1.0``
:type zoom: float
:param encoding: encoding format defaults to ``'UTF-8'``
:type encoding: str
:param verbose: whether to print relevant information in console, defaults to ``False``
:type verbose: bool
:param wkhtmltopdf_exe: absolute path to 'wkhtmltopdf.exe', defaults to ``None``;
on Windows, the default installation path
"*C:\\\\Program Files\\\\wkhtmltopdf\\\\bin\\\\wkhtmltopdf.exe*" is used
when ``wkhtmltopdf_exe=None``
:type wkhtmltopdf_exe: str or None
:param kwargs: [optional] parameters of `pdfkit.from_url <https://pypi.org/project/pdfkit/>`_
**Example**::
>>> from pyhelpers.store import save_web_page_as_pdf
>>> from pyhelpers.dir import cd
>>> import subprocess
>>> pdf_pathname = cd("tests\\documents", "pyhelpers.pdf")
>>> web_page_url = 'https://pyhelpers.readthedocs.io/en/latest/'
>>> save_web_page_as_pdf(web_page_url, pdf_pathname, verbose=True)
Saving "pyhelpers.pdf" to "tests\\documents\\" ...
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
>>> # Open the PDF file using the system's default application
>>> subprocess.Popen(pdf_pathname, shell=True)
>>> web_page_file = cd("docs\\build\\html\\index.html")
>>> save_web_page_as_pdf(web_page_file, pdf_pathname, verbose=True)
Updating "pyhelpers.pdf" at "tests\\documents\\" ...
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
>>> subprocess.Popen(pdf_pathname, shell=True)
"""
import pdfkit
wkhtmltopdf_exe_ = copy.copy(wkhtmltopdf_exe)
if wkhtmltopdf_exe_ is None:
wkhtmltopdf_exe_ = find_executable(
"wkhtmltopdf.exe", possibilities=["C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"])
if os.path.isfile(wkhtmltopdf_exe_):
pdfkit_configuration = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_exe)
else:
try:
pdfkit_configuration = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_exe_)
except (FileNotFoundError, OSError):
print("\"wkhtmltopdf\" (https://wkhtmltopdf.org/) is required to run this function; "
"however, it is not found on this device.\nInstall it and then try again.")
return None
try:
_check_path_to_file(
path_to_file=path_to_pdf, verbose=verbose, verbose_end=" ... \n", ret_info=False)
wkhtmltopdf_options = {
'enable-local-file-access': None,
'page-size': page_size,
'zoom': str(float(zoom)),
'encoding': encoding,
# 'margin-top': '0',
# 'margin-right': '0',
# 'margin-left': '0',
# 'margin-bottom': '0',
}
os.makedirs(os.path.dirname(path_to_pdf), exist_ok=True)
if os.path.isfile(path_to_pdf):
os.remove(path_to_pdf)
kwargs.update({'configuration': pdfkit_configuration, 'options': wkhtmltopdf_options})
if os.path.isfile(web_page):
status = pdfkit.from_file(web_page, path_to_pdf, verbose=verbose, **kwargs)
elif is_url(web_page):
status = pdfkit.from_url(web_page, path_to_pdf, verbose=verbose, **kwargs)
else:
status = None
if verbose and not status:
print("Failed. Check if the URL is available.")
except Exception as e:
if verbose:
print("Failed. {}".format(e), end="")
# A comprehensive function
def save_data(data, path_to_file, warning=True, **kwargs):
"""
Save data to a file of a specific format.
:param data: data that could be saved to
a file of `Pickle`_, `CSV`_, `Microsoft Excel`_, `JSON`_, `Joblib`_ or `Feather`_ format;
a URL of a web page or an `HTML file`_; or an image file of a `Matplotlib`-supported format
:type data: any
:param path_to_file: pathname of a file that stores the ``data``
:type path_to_file: str or os.PathLike[str]
:param warning: whether to show a warning messages, defaults to ``True``
:type warning: bool
:param kwargs: [optional] parameters of one of the following functions:
:py:func:`save_pickle()<pyhelpers.store.save_pickle>`,
:py:func:`save_spreadsheet()<pyhelpers.store.save_spreadsheet>`,
:py:func:`save_json()<pyhelpers.store.save_json>`,
:py:func:`save_joblib()<pyhelpers.store.save_joblib>`,
:py:func:`save_feather()<pyhelpers.store.save_feather>`,
:py:func:`save_fig()<pyhelpers.store.save_fig>` or
:py:func:`save_web_page_as_pdf()<pyhelpers.store.save_web_page_as_pdf>`
.. _`CSV`: https://en.wikipedia.org/wiki/Comma-separated_values
.. _`Pickle`: https://docs.python.org/3/library/pickle.html
.. _`Microsoft Excel`: https://en.wikipedia.org/wiki/Microsoft_Excel
.. _`JSON`: https://www.json.org/json-en.html
.. _`Joblib`: https://pypi.org/project/joblib/
.. _`Feather`: https://arrow.apache.org/docs/python/feather.html
.. _`HTML file`: https://fileinfo.com/extension/html
.. _`Matplotlib`:
https://matplotlib.org/stable/api/backend_bases_api.html#
matplotlib.backend_bases.FigureCanvasBase.get_supported_filetypes
**Examples**::
>>> from pyhelpers.store import save_data
>>> from pyhelpers.dir import cd
>>> from pyhelpers._cache import example_dataframe
>>> data_dir = cd("tests\\data")
>>> # Get an example dataframe
>>> dat = example_dataframe()
>>> dat
Easting Northing
London 530034 180381
Birmingham 406689 286822
Manchester 383819 398052
Leeds 582044 152953
>>> # Save the data to files different formats:
>>> dat_pathname = cd(data_dir, "dat.pickle")
>>> save_data(dat, dat_pathname, verbose=True)
Saving "dat.pickle" to "tests\\data\\" ... Done.
>>> dat_pathname = cd(data_dir, "dat.csv")
>>> save_data(dat, dat_pathname, index=True, verbose=True)
Saving "dat.csv" to "tests\\data\\" ... Done.
>>> dat_pathname = cd(data_dir, "dat.xlsx")
>>> save_data(dat, dat_pathname, index=True, verbose=True)
Saving "dat.xlsx" to "tests\\data\\" ... Done.
>>> dat_pathname = cd(data_dir, "dat.txt")
>>> save_data(dat, dat_pathname, index=True, verbose=True)
Saving "dat.txt" to "tests\\data\\" ... Done.
>>> dat_pathname = cd(data_dir, "dat.feather")
>>> save_data(dat, dat_pathname, verbose=True)
Saving "dat.feather" to "tests\\data\\" ... Done.
>>> # Convert `dat` to JSON format
>>> import json
>>> dat_ = json.loads(dat.to_json(orient='index'))
>>> dat_
{'London': {'Easting': 530034, 'Northing': 180381},
'Birmingham': {'Easting': 406689, 'Northing': 286822},
'Manchester': {'Easting': 383819, 'Northing': 398052},
'Leeds': {'Easting': 582044, 'Northing': 152953}}
>>> dat_pathname = cd(data_dir, "dat.json")
>>> save_data(dat_, dat_pathname, indent=4, verbose=True)
Saving "dat.json" to "tests\\data\\" ... Done.
.. seealso::
- Examples for the function :py:func:`pyhelpers.store.load_data`.
"""
path_to_file_ = path_to_file.lower()
if path_to_file_.endswith(".pickle"):
save_pickle(data, path_to_file, **kwargs)
elif path_to_file_.endswith((".csv", ".xlsx", ".xls", ".txt")):
save_spreadsheet(data, path_to_file, **kwargs)
elif path_to_file_.endswith(".json"):
save_json(data, path_to_file, **kwargs)
elif path_to_file_.endswith(".joblib"):
save_joblib(data, path_to_file, **kwargs)
elif path_to_file_.endswith(".feather"):
save_feather(data, path_to_file, **kwargs)
elif (is_url(data) or os.path.isfile(data)) and path_to_file_.endswith(".pdf"):