Source file ppx_python_runtime.ml
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
open! Base
let python_of_bool = Py.Bool.of_bool
let bool_of_python = Py.Bool.to_bool
let python_of_int = Py.Int.of_int
let int_of_python = Py.Int.to_int
let python_of_float = Py.Float.of_float
let float_of_python = Py.Float.to_float
let python_of_string = Py.String.of_string
let string_of_python = Py.String.to_string
let python_of_array = Py.List.of_array_map
let array_of_python = Py.List.to_array_map
let python_of_list = Py.List.of_list_map
let list_of_python = Py.List.to_list_map
let python_of_option f = function
| None -> Py.none
| Some v -> f v
;;
let option_of_python f pyobject =
if Stdlib.( = ) pyobject Py.none then None else Some (f pyobject)
;;
module Dict_str_keys = struct
type t = Pytypes.pyobject
let internalized_keys = Hashtbl.create (module String)
let internalized_key key =
Hashtbl.findi_or_add internalized_keys key ~default:python_of_string
;;
let set t key value =
let key = internalized_key key in
Py.Dict.set_item t key value
;;
let find t key =
let key = internalized_key key in
Py.Dict.find t key
;;
let create assoc =
let t = Py.Dict.create () in
List.iter assoc ~f:(fun (key, value) -> set t key value);
t
;;
let dict ~expected_field_names =
let expected_field_names = Set.of_list (module String) expected_field_names in
Py.Dict.to_bindings_string dict
|> List.filter ~f:(fun (dict_field_name, _) ->
not (Set.mem expected_field_names dict_field_name))
|> List.map ~f:(fun (field_name, _) -> "'" ^ field_name ^ "'")
|> String.concat ~sep:","
|> Printf.sprintf "unexpected extra field names %s"
|> failwith
;;
end
exception Not_found_s = Not_found_s