Coverage for pesummary/core/file/formats/pickle.py: 100.0%

10 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3import pickle 

4from pesummary.utils.utils import check_filename 

5 

6__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

7 

8 

9def read_pickle(path): 

10 """Read a pickle file and return the object 

11 

12 Parameters 

13 ---------- 

14 path: str 

15 path to the pickle file you wish to read in 

16 """ 

17 with open(path, "rb") as f: 

18 return pickle.load(f) 

19 

20 

21def write_pickle( 

22 *args, outdir="./", label=None, filename=None, overwrite=False, 

23 default_filename="pesummary_{}.pickle", **kwargs 

24): 

25 """Write an object to a pickle file 

26 

27 Parameters 

28 ---------- 

29 *args: tuple 

30 all args passed to pickle.dump 

31 outdir: str, optional 

32 directory to write the dat file 

33 label: str, optional 

34 The label of the analysis. This is used in the filename if a filename 

35 if not specified 

36 filename: str, optional 

37 The name of the file that you wish to write 

38 overwrite: Bool, optional 

39 If True, an existing file of the same name will be overwritten 

40 """ 

41 filename = check_filename( 

42 default_filename=default_filename, outdir=outdir, label=label, 

43 filename=filename, overwrite=overwrite 

44 ) 

45 with open(filename, "wb") as f: 

46 pickle.dump(*args, f)