changgong lee
changgong lee

Reputation: 3

Dumping a data structure like pickle in python

Could you please recommend library where I could dump a runtime data structure such as a Map or Set which behaves like the pickle library that does the object serialization in Python?

Upvotes: -1

Views: 121

Answers (1)

octachron
octachron

Reputation: 18892

For personal projects, you can use the Marshal module of the standard library which is essentially the OCaml version of pickle.

For instance, if I have a set

module Int_set = Set.Make(Int)
let set = Int_set.of_list [1;2;3;4]

that I want to seralize to a file named

let set_file = "set_file"

I can do it with

let () =
  Out_channel.with_open_bin set_file (fun chan ->
      Marshal.to_channel chan set []
  )

and I can read back this file with

let set: Int_set.t = In_channel.with_open_bin set_file  Marshal.from_channel

and check that everything is fine with

let () =
  let pp_sep ppf () = Format.fprintf ppf ",@ " in
  Format.printf "@[set={%a}@]@."
    Format.(pp_print_seq ~pp_sep pp_print_int) (Int_set.to_seq set)

However, like Python's pickle, the Marshal module is not safe: you should only unmarshal data from files that you trust as much as your executable. Thus for non-personal project, it might be better to consider other serialization formats.

(Another limitation is that Marshal cannot serialize reliably data structures that contain functions (it is not impossible but it is better to avoid Marshall Closures flag).)

Upvotes: 2

Related Questions