Commits

camlspotter  committed 780404a

added hist2d_demo.ml and changed the type of Xapi.call_obj*

  • Participants
  • Parent commits 509b1bd

Comments (0)

Files changed (7)

     if( Is_block(v) ){
         return PyObject_val(Field(v, 0));
     } else {
-        printf("PyObject_opt_avl: NULL\n");
+        // printf("PyObject_opt_val: NULL\n");
         return NULL;
     }
 }

File examples/matplotlib/OMakefile

 
 MyOCamlProgram(finance_demo, finance_demo)
 MyOCamlProgram(finance_demo_oo, finance_demo_oo)
+MyOCamlProgram(hist2d_demo, hist2d_demo)

File examples/matplotlib/finance_demo.ml

   debug fig "fig";
 
   (* CR jfuruse: warnings for unit cases. lousy... *)
-  call_obj_ (fig % "subplots_adjust") ~kwd:(dict_of_klist [ "bottom", Py.of_float 0.2]) [];
+  call_obj_ (fig % "subplots_adjust") ~kwd:[ "bottom", Py.of_float 0.2] [];
   let ax = call_obj (fig % "add_subplot") [ Py.of_int 111 ] |> Dict.coerce in
   debug ax "ax";
 
   (* call_obj (ax % "xaxis" % "set_minor_formatter") [ dayFormatter ]; *)
 
   (* #plot_day_summary(ax, quotes, ticksize=3) *)
-  call_obj_ (finance % "candlestick") ~kwd:(dict_of_klist [ "", !:> ax; "", !:> quotes; "width", !:> (Py.of_float 0.6) ]) [];
+  call_obj_ (finance % "candlestick") ~kwd:[ "", !:> ax; "", !:> quotes; "width", !:> (Py.of_float 0.6) ] [];
 
   call_obj_ (ax % "xaxis_date") [];
   call_obj_ (ax % "autoscale_view") [];
   
   call_obj_ (pylab % "setp")
-    ~kwd: (dict_of_klist [ "", call_obj (call_obj (pylab % "gca") [] %! "get_xticklabels") []; 
-                           "rotation", !:> (Py.of_int 45); 
-                           "horizontalalignment", !:> (Py.of_string "right") ])
+    ~kwd: [ "", call_obj (call_obj (pylab % "gca") [] %! "get_xticklabels") []; 
+            "rotation", !:> (Py.of_int 45); 
+            "horizontalalignment", !:> (Py.of_string "right") ]
     [];
 
   call_obj_ (pylab % "show") []

File examples/matplotlib/hist2d_demo.ml

+module Api = Opycaml.Api
+open Api
+open Opycaml.Xapi
+
+let debug = Object.debug
+
+let main () =
+  (* from pylab import * *)
+  let pylab = import_module "pylab" in
+
+  let randn x = call_obj (pylab % "randn") [x] |> Number.coerce in
+
+  let x = Number.add (randn (Py.of_int 1000)) (Py.of_int 5) in
+  let y = randn (Py.of_int 1000) in
+  
+  call_obj_ (pylab % "hist2d") [x; y] ~kwd:[ "bins", Py.of_int 40 ];
+  call_obj_ (pylab % "show") []
+
+
+let () = 
+  try main () with
+  | Api.Error(ty, detail) -> 
+      prerr_endline "Expected type error!";
+      debug ty "type";
+      debug detail "detail";

File js_of_ocaml/finance_demo_oo.ml

   let import_module name = (Import.importModule name)#getDict
   let (%) o k = o#getItemString k
   let (%!) o k = (o |> Dict.coerce)#getItemString k
-  let call_obj o = (Callable.coerce o)#call
-  let call_obj_ o ?kwd args = (Callable.coerce o)#call ?kwd args |> ignore
-  let (!:>) o = (o :> o_Object)
-  
+
   let dict_of_alist kvs =
     let d = Api.Dict.new_ () |> new OPy.o_Dict in
     List.iter (fun (k,v) -> d#setItem k v) kvs;
     d
-    
+
   let dict_of_klist kvs =
     kvs |> List.map (fun (s,o) -> OPy.String.fromString s, o) |> dict_of_alist
   
+  let call_obj o ?kwd args = 
+    let kwd = match kwd with
+      | None -> None
+      | Some xs -> dict_of_klist xs
+    in
+    (Callable.coerce o)#call o ~kwd
+
+  let call_obj_ o ?kwd args = call_obj o ?kwd args |> ignore
+
+  let (!:>) o = (o :> o_Object)
+  
 end
 
 class o_Date t = object
 
 let (!:>) = Object.obj
 
-let call_obj o = Object.call (Callable.coerce o)
-
-let call_obj_ o ?kwd xs = Object.call (Callable.coerce o) ?kwd xs |> ignore
-
 let dict_of_alist kvs =
   let d = Dict.new_ () in
   List.iter (fun (k,v) -> Dict.setItem d k v) kvs;
 let dict_of_klist kvs =
   kvs |> List.map (fun (s,o) -> Py.String.fromString s, o) |> dict_of_alist
 
+let call_obj o ?kwd xs = 
+  let kwd = match kwd with
+    | None -> None
+    | Some xs -> Some (dict_of_klist xs)
+  in
+  Object.call (Callable.coerce o) ?kwd xs
+
+let call_obj_ o ?kwd xs = call_obj o ?kwd xs  |> ignore
+
 let import_module name = Module.getDict (Import.importModule name)
 val ( !:> ) : [> _Object ] t -> _Object t
 (** Forget Python object class *)
 
-val call_obj : [> _Object ] t -> ?kwd:[> _Dict ] t -> [> _Object ] t list -> _Object t
+val call_obj : [> _Object ] t -> ?kwd:(string * [> _Object ] t) list -> [> _Object ] t list -> _Object t
 (** Dynamically typed function call *)
 
-val call_obj_ : [> _Object ] t -> ?kwd:[> _Dict ] t -> [> _Object ] t list -> unit
+val call_obj_ : [> _Object ] t -> ?kwd:(string * [> _Object ] t) list -> [> _Object ] t list -> unit
 (** Dynamically typed function call *)
 
 val dict_of_alist : ([> _Object ] t * [> _Object ] t) list -> _Dict t