Commits

camlspotter committed 98ed449

unified dict access and attr access

  • Participants
  • Parent commits 78e76ef
  • Branches dev

Comments (0)

Files changed (2)

   val ( $<> ) : [> _Dict ] t -> string -> _Object t
     (** Dictionary access. Python's o[x] *)
 
+  val ( $ ) : [> _Object ] t -> string -> _Object t
+    (** Attribute/Dictionary access by string. *)
+
   val call : [> _Object ] t -> [> _Object ] t list -> _Object t
 
   val kwd_call : [> _Object ] t -> (string * [> _Object ] t) list -> _Object t
   let ( !:> ) o = (o :> _Object t)
   let ( $. ) = Object.getAttrString (* Python's o.x *)
   let ( $<> ) = Dict.getItemString (* Python's o[x] *)
+  let ( $ ) o = match Dict.coerce_opt o with
+    | None -> ($.) o
+    | Some d -> ($<>) d
   let call o = Object.call (Callable.coerce o)
   let kwd_call o = Object.kwd_call (Callable.coerce o)
   let show_error typ detail = Printf.sprintf "%s: %s" (Py.String.asString (Object.str typ)) (Py.String.asString (Object.str detail))

examples/matplotlib/finance_demo.ml

   (* import datetime *)
   let datetime = import_module "datetime" in
 
-  let date = call (datetime $<> "date") in
+  let date = call (datetime $ "date") in
 
   (* date1 = datetime.date( 2004, 2, 1) *)
   let date1 = date [ Int.fromLong 2004; Int.fromLong 2; Int.fromLong 1 ] in
         debug detail "detail";
   end;
 
-  let monday = dates $<> "MONDAY" in
+  let monday = dates $ "MONDAY" in
   debug monday "monday";
 
-  let dayLocator = dates $<> "DayLocator" in
+  let dayLocator = dates $ "DayLocator" in
   debug dayLocator "DayLocator";
 
   (* Class can be called as a function for New *)
-  let dayLocator_f = call (dates $<> "DayLocator") in
+  let dayLocator_f = call (dates $ "DayLocator") in
 
   let alldays = dayLocator_f [] in
   debug alldays "alldays";
 
-  let weekdayLocator = call (dates $<> "WeekdayLocator") in
+  let weekdayLocator = call (dates $ "WeekdayLocator") in
 
   let mondays = weekdayLocator [ monday ] in
 
-  let dateFormatter = call (dates $<> "DateFormatter") in
+  let dateFormatter = call (dates $ "DateFormatter") in
 
   let weekFormatter = dateFormatter [ Py.String.fromString "%b %d" ] in
   debug weekFormatter "weekFormatter";
   let dayFormatter = dateFormatter [ Py.String.fromString "%d" ] in
   debug dayFormatter "dayFormatter";
 
-  let quotes_historical_yahoo = call (finance $<> "quotes_historical_yahoo") in
-  debug (finance $<> "quotes_historical_yahoo") "quotes_historical_yahoo";
+  let quotes_historical_yahoo = call (finance $ "quotes_historical_yahoo") in
+  debug (finance $ "quotes_historical_yahoo") "quotes_historical_yahoo";
  
   let quotes = 
     try
 
   debug quotes "quotes";
 
-  let fig = call (pylab $<> "figure") [] in
+  let fig = call (pylab $ "figure") [] in
   debug fig "fig";
 
   (* CR jfuruse: warnings for unit cases. lousy... *)
-  kwd_call (fig $. "subplots_adjust") [ "bottom", Float.fromDouble 0.2];
-  let ax = call (fig $. "add_subplot") [ Int.fromLong 111 ] in
+  kwd_call (fig $ "subplots_adjust") [ "bottom", Float.fromDouble 0.2];
+  let ax = call (fig $ "add_subplot") [ Int.fromLong 111 ] in
   debug ax "ax";
 
-  call (ax $. "xaxis" $. "set_major_locator") [ mondays ];
-  call (ax $. "xaxis" $. "set_minor_locator") [ alldays ];
-  call (ax $. "xaxis" $. "set_major_formatter") [ weekFormatter ];
-  (* call (ax $. "xaxis" $. "set_minor_formatter") [ dayFormatter ]; *)
+  call (ax $ "xaxis" $ "set_major_locator") [ mondays ];
+  call (ax $ "xaxis" $ "set_minor_locator") [ alldays ];
+  call (ax $ "xaxis" $ "set_major_formatter") [ weekFormatter ];
+  (* call (ax $ "xaxis" $ "set_minor_formatter") [ dayFormatter ]; *)
 
   (* #plot_day_summary(ax, quotes, ticksize=3) *)
-  kwd_call (finance $<> "candlestick") [ "", ax; "", quotes; "width", !:> (Float.fromDouble 0.6) ];
+  kwd_call (finance $ "candlestick") [ "", ax; "", quotes; "width", !:> (Float.fromDouble 0.6) ];
 
-  call (ax $. "xaxis_date") [];
-  call (ax $. "autoscale_view") [];
+  call (ax $ "xaxis_date") [];
+  call (ax $ "autoscale_view") [];
   
-  kwd_call (pylab $<> "setp")
-    [ "", call (call (pylab $<> "gca") [] $. "get_xticklabels") []; 
+  kwd_call (pylab $ "setp")
+    [ "", call (call (pylab $ "gca") [] $ "get_xticklabels") []; 
       "rotation", !:> (Int.fromLong 45); 
       "horizontalalignment", !:> (Py.String.fromString "right") ];
 
-  call (pylab $<> "show") [];
+  call (pylab $ "show") [];
 
   ()