text stringlengths 0 601k |
|---|
let rec lines_of_t t = let indent = String . make 4 ' ' in match t with | Line x -> [ x ] | Block xs -> let all = List . concat ( List . map lines_of_t xs ) xs in List . map ( function | " " -> " " | x -> indent ^ x ) x all |
let string_of_ts ts = String . concat " \ n " ( List . concat ( List . map lines_of_t ts ) ts ) ts |
let output_doc description = let process descr = descr |> String . split_on_char ' \ n ' |> List . map ( function l -> Line ( String . trim l ) l ) l in let descr_lines = description |> List . map process |> List . concat |> function | Line " " :: tl -> tl | lst -> lst in [ Line { } " " " || ] @ descr_lines @ [ Line { ... |
let fresh_id = let counter = ref 5 in fun ( ) -> incr counter ; " tmp_ " ^ string_of_int ! counter |
let rec typecheck : type a . a typ -> string -> t list = fun ty v -> let open Printf in let raise_type_error = Line ( sprintf { | raise TypeError ( TypeError " % s " , repr ( repr % s ) s ) s } | ( Rpcmarshal . ocaml_of_t ty ) ty v ) v in let handle_basic b = let python_of_basic : type a . a basic -> string = function ... |
let rec value_of : type a . a typ -> string = let open Printf in function | Basic Int64 -> " long ( long0 ) long0 " | Basic Int -> " long ( long0 ) long0 " | Basic Int32 -> " 0 " | Basic Char -> " ' c ' " | Basic String -> { " | string } " | | Basic Float -> " 1 . 1 " | Basic Bool -> " True " | DateTime -> { " | 197001... |
let exn_var myarg = let open Printf in let inner : type a b . ( a , b ) b tag -> t list = function | tag -> let has_arg = match tag . tcontents with | Unit -> false | _ -> true in if List . mem tag . tname reserved_exns then [ ] else [ Line " " ; Line " " ; Line ( sprintf " class % s ( sRpc_light_failure ) sRpc_light_f... |
let skeleton_method unimplemented i ( BoxedFunction m ) m = let inputs = Method ( . find_inputs m . ty ) ty in let output = Method ( . find_output m . ty ) ty in let inputs = List . filter ( function | Idl . Param . Boxed { Idl . Param . typedef ; _ } -> ( match typedef . ty with | Unit -> false | _ -> true ) true ) tr... |
let example_stub_user i ( BoxedFunction m ) m = let open Printf in [ Line " " ; Line " # import necessary libraries if needed " ; Line " # we assume that your library providing the client is called myclient and it \ provides a connect method " ; Line " import myclient " ; Line " " ; Line { | if __name__ == " __main__ }... |
let example_skeleton_user i m = let open Printf in [ Line " " ; Line " # import additional libraries if needed " ; Line " " ; Line ( sprintf " class % s_myimplementation ( s_myimplementation % s_skeleton ) s_skeleton " : i . Interface . details . Idl . Interface . name i . Interface . details . Idl . Interface . name )... |
let skeleton_of_interface unimplemented suffix i = let open Printf in [ Line " " ; Line " " ; Line ( sprintf " class % s_ % s ( sobject ) sobject " : i . Interface . details . Idl . Interface . name suffix ) suffix ; Block ( output_doc i . Interface . details . Idl . Interface . description @ [ Line " " ; Line " def __... |
let test_impl_of_interface = skeleton_of_interface false " test " |
let skeleton_of_interface = skeleton_of_interface true " skeleton " |
let server_of_interface i = let open Printf in let typecheck_method_wrapper ( BoxedFunction m ) m = let inputs = Method ( . find_inputs m . ty ) ty in let inputs = List . filter ( function | Idl . Param . Boxed { Idl . Param . typedef ; _ } -> ( match typedef . ty with | Unit -> false | _ -> true ) true ) true inputs i... |
let test_impl_of_interfaces i = let open Printf in [ Line " " ; Line " " ; Line ( sprintf " class % s_server_test ( s_server_test % s_server_dispatcher ) s_server_dispatcher " : i . Interfaces . name i . Interfaces . name ) name ; Block [ Line { " " " | Create a server which will respond to all calls , returning arbitr... |
let commandline_parse _ ( BoxedFunction m ) m = let open Printf in let inputs = Method ( . find_inputs m . ty ) ty in let inputs = List . filter ( function | Idl . Param . Boxed { Idl . Param . typedef ; _ } -> ( match typedef . ty with | Unit -> false | _ -> true ) true ) true inputs in [ Line " " ; Line ( sprintf " d... |
let commandline_run _ ( BoxedFunction m ) m = let open Printf in [ Line " " ; Line ( sprintf " def % s ( sself ) sself " : m . Method . name ) name ; Block ( output_doc m . Method . description @ [ Line " use_json = False " ; Line " try " : ; Block [ Line ( sprintf " request = self . _parse_ % s ( s ) s " m . Method . ... |
let commandline_of_interface i = let open Printf in [ Line " " ; Line " " ; Line ( sprintf " class % s_commandline ( s_commandlineobject ) s_commandlineobject " : i . Interface . details . Idl . Interface . name ) name ; Block ( [ Line { " " " | Parse command - line arguments and call an implementation } . " " " | ; Li... |
let of_interfaces ( ? helpers = inline_defaults ) inline_defaults i = let open Printf in let dispatch_class i comma = Line ( sprintf { | " % s " : self . % s . _dispatch % s } | i . Interface . details . Idl . Interface . name i . Interface . details . Idl . Interface . name comma ) comma in let dispatch_dict methods =... |
type assoc_repr = | List | Dict |
let get_python_default an : string option = Atd . Annot . get_opt_field ~ parse ( : fun s -> Some s ) ~ sections [ " : python " ] ~ field " : default " an |
let get_python_assoc_repr an : assoc_repr = Atd . Annot . get_field ~ parse ( : function | " list " -> Some List | " dict " -> Some Dict | _ -> None ) ~ default : List ~ sections [ " : python " ] ~ field " : repr " an |
let get_python_decorators an : string list = Atd . Annot . get_fields ~ parse ( : fun s -> Some s ) ~ sections [ " : python " ] ~ field " : decorator " an |
let get_python_text an : string list = Atd . Annot . get_fields ~ parse ( : fun s -> Some s ) ~ sections [ " : python " ] ~ field " : text " an |
let get_python_json_text an : string list = get_python_text an @ Atd . Annot . get_fields ~ parse ( : fun s -> Some s ) ~ sections [ " : python " ] ~ field " : json_py . text " an |
type install_tool_type = Pip | Conda |
type install_source_type = | Package_PyPI of tool_def_type | Package_Source of tool_def_type * string | Package_Conda of tool_def_type |
type python_package_spec = install_tool_type * install_source_type * ( string option ) |
let create_python_tool ~ host ( ~ run_program : Machine . Make_fun . t ) ~ install_path ? check_bin ( ? python_version ` = Python_3 ) package_spec = let open KEDSL in let pkg_id ? version ~ sep name = match version with | None -> name | Some v -> name ^ sep ^ v in let describe_source ? version name = sprintf " # source... |
let default_python_packages = let open Machine . Tool . Default in [ ( Pip , Package_PyPI pyensembl , None , [ ] ) ; ( Pip , Package_PyPI isovar , Some " isovar - protein - sequences . py " , [ ] ) ; ( Pip , Package_PyPI vaxrank , None , [ ( " wkhtmltopdf " , ` Latest ) ; ] ) ; ( Pip , Package_PyPI vcfannotatepolyphen ... |
let default ~ host ~ run_program ~ install_path ( ) = let pkg_to_tool pkg_spec = create_python_tool ~ host ~ run_program ~ install_path pkg_spec in Machine . Tool . Kit . of_list ( List . map ~ f : pkg_to_tool default_python_packages ) |
let handle_python_error msg closure = match closure ( ) with | result -> result | exception Py . E ( x , y ) -> let s = ( Printf . sprintf " % s \ n % s \ n % s \ n " msg ( Py . Object . to_string x ) ( Py . Object . to_string y ) ) in Stdlib . failwith s |
let import_once module_name = let imported = ref None in fun ( ) -> match ! imported with | None -> let msg = " While loading " ^ module_name in let pymod = handle_python_error msg ( fun ( ) -> Py . import module_name ) in imported := ( Some pymod ) ; pymod | Some pymod -> pymod |
let option_find f x = try Some ( f x ) x with Not_found -> None |
let substring_between string before after = String . sub string before ( after - before ) before |
let int_of_octal octal = int_of_string ( " 0o " ^ octal ) octal |
let int_of_hex hex = int_of_string ( " 0x " ^ hex ) hex |
let split_left_on_char ( ? from = 0 ) 0 char s = try substring_between s from ( String . index_from s from char ) char with Not_found -> if from = 0 then s else substring_between s from ( String . length s ) s |
let split_right_on_char ( ? from = 0 ) 0 char s = try substring_between s ( String . index_from s from char + 1 ) 1 ( String . length s ) s with Not_found -> if from = 0 then s else substring_between s from ( String . length s ) s |
let trim_carriage_return line = let length = String . length line in if String . sub line ( length - 1 ) 1 1 = " \ r " then String . sub line 0 ( length - 1 ) 1 else line |
let input_lines channel = let accu = ref [ ] in try while true do accu := trim_carriage_return ( input_line channel ) channel :: ! accu ; done ; assert false with End_of_file -> List . rev ! accu |
let write_and_close channel f arg = try let result = f arg in close_out channel ; result with e -> close_out_noerr channel ; raise e |
let with_temp_file contents f = let ( file , channel ) channel = Filename . open_temp_file " pyml_tests " " . py " in Fun . protect begin fun ( ) -> write_and_close channel ( output_string channel ) channel contents ; Stdcompat . In_channel . with_open_bin file ( f file ) file end ~ finally ( : fun ( ) -> Sys . remove ... |
let with_pipe f = let ( read , write ) write = Unix . pipe ( ) in let in_channel = Unix . in_channel_of_descr read and out_channel = Unix . out_channel_of_descr write in Fun . protect begin fun ( ) -> f in_channel out_channel end ~ finally : begin fun ( ) -> close_in_noerr in_channel ; close_out_noerr out_channel end |
let with_stdin_from channel f arg = let stdin_backup = Unix . dup Unix . stdin in Unix . dup2 ( Unix . descr_of_in_channel channel ) channel Unix . stdin ; Fun . protect begin fun ( ) -> f arg end ~ finally : begin fun ( ) -> Unix . dup2 stdin_backup Unix . stdin end |
let with_channel_from_string s f = with_pipe begin fun in_channel out_channel -> output_string out_channel s ; close_out out_channel ; f in_channel end |
let with_stdin_from_string s f arg = with_channel_from_string s ( fun channel -> with_stdin_from channel f arg ) arg |
let initialized = ref already_initialized |
type op = | LT | LE | EQ | NE | GT | GE |
let maybe_raise_python_error ( ) = if C . _PyErr_Occurred ( ) <> 0 then let ptype , pvalue , ptraceback = allocate_n pyobject ~ count : 1 , allocate_n pyobject ~ count : 1 , allocate_n pyobject ~ count : 1 in let _ = C . _PyErr_Fetch ptype pvalue ptraceback in let x = C . _PyObject_Str !@ pvalue in let res = C . _PyUni... |
let wrap x = if x = null then ( maybe_raise_python_error ( ) ; raise Invalid_object ) else Gc . finalise C . _Py_DecRef x ; x |
let wrap_status x = if x = - 1 then maybe_raise_python_error ( ) else ( ) |
let wrap_iter x = let err = C . _PyErr_Occurred ( ) = 1 in if x = null && not err then raise End_iteration else wrap x |
module PyList = struct let create l = let lst = C . _PyList_New ( List . length l |> Int64 . of_int ) in List . iteri ( fun i x -> C . _Py_IncRef x ; wrap_status ( C . _PyList_SetItem lst ( Int64 . of_int i ) x ) ) l ; wrap lst let insert l i v = wrap_status ( C . _PyList_Insert l i v ) let append l v = wrap_status ( C... |
module PyTuple = struct let create l = let tpl = C . _PyTuple_New ( Array . length l |> Int64 . of_int ) in Array . iteri ( fun i x -> C . _Py_IncRef x ; wrap_status ( C . _PyTuple_SetItem tpl ( Int64 . of_int i ) x ) ) l ; wrap tpl end |
module PySet = struct let create obj = wrap ( C . _PySet_New obj ) end |
module PyUnicode = struct let create s = wrap ( C . _PyUnicode_FromStringAndSize s ( String . length s |> Int64 . of_int ) ) end |
module PyBytes = struct let create b = wrap ( C . _PyBytes_FromStringAndSize ( Bytes . to_string b ) ( Bytes . length b |> Int64 . of_int ) ) end |
module PyBuffer = struct type b = C . pybuffer type t = { buf : b ; data : char CArray . t ; } let create ? readonly ( : readonly = true ) obj = let b = allocate_n ~ finalise : C . _PyBuffer_Release C . _Py_buffer ~ count : 1 in wrap_status ( C . _PyObject_GetBuffer obj b ( if readonly then 0 else 1 ) ) ; { buf = b ; d... |
module PyByteArray = struct let from_list x = wrap ( C . _PyByteArray_FromStringAndSize ( CArray . of_list char x |> CArray . start ) ( List . length x |> Int64 . of_int ) ) let create x = wrap ( C . _PyByteArray_FromObject x ) let get_string x = let s = C . _PyByteArray_AsString x in let len = C . _PyByteArray_Size x ... |
module PyNumber = struct let create_int i = wrap ( C . _PyLong_FromLong i ) let create_int64 i = wrap ( C . _PyLong_FromLongLong i ) let create_float i = wrap ( C . _PyFloat_FromDouble i ) let add a b = wrap ( C . _PyNumber_Add a b ) let sub a b = wrap ( C . _PyNumber_Subtract a b ) let mul a b = wrap ( C . _PyNumber_M... |
module PyIter = struct type t = pyobject let get x : t = wrap ( C . _PyObject_GetIter x ) let next x = wrap_iter ( C . _PyIter_Next x ) let map fn x = let dst = ref [ ] in let _ = try while true do let n = next x in dst := fn n ::! dst done with End_iteration -> ( ) in List . rev ! dst end |
module Object = struct type t = pyobject let is_null x = x = null let decref = C . _Py_DecRef let incref = C . _Py_IncRef let length obj = let l = C . _PyObject_Length obj in if Int64 . to_int l = - 1 then maybe_raise_python_error ( ) ; l let to_string a = let x = wrap ( C . _PyObject_Str a ) in C . _PyUnicode_AsUTF8 x... |
module PyDict = struct let create = Object . pydict_create let contains d k = C . _PyDict_Contains d k = 1 let copy d = wrap ( C . _PyDict_Copy d ) let clear d = C . _PyDict_Clear d let merge a b update = wrap_status ( C . _PyDict_Merge a b update ) let dict_items x = wrap ( C . _PyDict_Items x ) let dict_keys x = wrap... |
let get_module_dict ( ) = C . _PyImport_GetModuleDict ( ) |
module PyModule = struct let import name = wrap ( C . _PyImport_Import ( PyUnicode . create name ) ) let set name m = let d = get_module_dict ( ) in Object . set_item d ( PyUnicode . create name ) m let get name = wrap ( C . _PyImport_AddModule name ) let get_dict name = wrap ( C . _PyModule_GetDict ( get name ) ) let ... |
module PyCell = struct let create obj = wrap ( C . _PyCell_New obj ) let get cell = wrap ( C . _PyCell_Get cell ) let set cell v = wrap_status ( C . _PyCell_Set cell v ) end |
module PySlice = struct let create a b c = wrap ( C . _PySlice_New a b c ) end |
module PyWeakref = struct let new_ref ? callback ( : callback = Object . none ( ) ) obj = wrap ( C . _PyWeakref_NewRef obj callback ) let new_proxy ? callback ( : callback = Object . none ( ) ) obj = wrap ( C . _PyWeakref_NewProxy obj callback ) let get_object ref = wrap ( C . _PyWeakref_GetObject ref ) end |
module PyThreadState = struct type t = C . thread let save ( ) = C . _PyEval_SaveThread ( ) let restore thr = C . _PyEval_RestoreThread thr let get ( ) = C . _PyThreadState_Get ( ) let swap thr = C . _PyThreadState_Swap thr let clear thr = C . _PyThreadState_Clear thr let delete thr = clear thr ; C . _PyThreadState_Del... |
let new_interpreter ( ) = C . _Py_NewInterpreter ( ) |
let end_interpreter thr = C . _Py_EndInterpreter thr |
type t = | Ptr of Object . t | Cell of Object . t | Nil | Bool of bool | Int of int | Int64 of int64 | Float of float | String of string | Bytes of Bytes . t | List of t list | Tuple of t array | Dict of ( t * t ) list | Set of t list | Slice of t * t * t |
let rec to_object = function | Ptr o -> o | Cell c -> PyCell . create c | Nil -> Object . none ( ) | Bool b -> Object . from_bool b | Int i -> PyNumber . create_int i | Int64 i -> PyNumber . create_int64 i | Float f -> PyNumber . create_float f | String s -> PyUnicode . create s | Bytes b -> PyBytes . create b | List l... |
let program_name : wchar_string option ref = ref None |
let finalize ( ) = match ! program_name with | Some p -> C . _PyMem_RawFree p ; program_name := None | None -> ( ) ; C . _Py_Finalize ( ) ; initialized := false |
let wchar_s s = let s ' = C . _Py_DecodeLocale s null in Gc . finalise C . _PyMem_RawFree s ' ; s ' |
let initialize ? initsigs ( : initsigs = true ) ( ) = if not ! initialized then let name = wchar_s Sys . argv . ( 0 ) in let _ = if name <> null then let _ = program_name := Some name in C . _Py_SetProgramName name in let _ = C . _Py_InitializeEx ( if initsigs then 1 else 0 ) in let _ = C . _PyEval_InitThreads ( ) in l... |
let exec s = not ( C . _PyRun_SimpleStringFlags s null ) |
let globals ( ) = let x = C . _PyEval_GetGlobals ( ) in if Object . is_null x then None else Some x |
let locals ( ) = let x = C . _PyEval_GetLocals ( ) in if Object . is_null x then None else Some x |
let builtins ( ) = C . _PyEval_GetBuiltins ( ) |
let eval ? globals ? locals s = let g = match globals with | Some x -> to_object x | None -> PyModule . get_dict " __main__ " in let l = match locals with | Some x -> to_object x | None -> PyDict . create [ ] in wrap ( C . _PyRun_StringFlags s ( 258 ) g l null ) |
let ( !$ ) obj = to_object obj |
let run fn ? kwargs ( : kwargs [ ] ) = args = Object . call fn ~ args ( : List . map to_object args |> Array . of_list ) ~ kwargs ( : List . map ( fun ( key , value ) -> !$ key , !$ value ) kwargs ) |
let ( $ ) fn args = run fn args |
let ( . $ ) obj attr = Object . get_attr obj ( !$ attr ) |
let ( . ) <-$ ( obj , key ) value = Object . set_attr obj ( !$ key ) ( !$ value ) |
let ( $| ) obj item = Object . get_item obj ( !$ item ) |
let ( <-$| ) ( obj , key ) value = Object . set_item obj ( !$ key ) ( !$ value ) |
let append_path files = let sys = PyModule . import " sys " in let path = Object . get_attr_s sys " path " in let p = Object . to_list Object . to_string path @ files in Object . set_attr_s sys " path " ( PyList . create ( List . map PyUnicode . create p ) ) |
let prepend_path files = let sys = PyModule . import " sys " in let path = Object . get_attr_s sys " path " in let p = files @ Object . to_list Object . to_string path in Object . set_attr_s sys " path " ( PyList . create ( List . map PyUnicode . create p ) ) |
let pickle ? kwargs obj = let pickle = PyModule . import " pickle " in run ( pickle . $ String " dumps " ) ? kwargs [ Ptr obj ] |> Object . to_bytes |
let unpickle ? kwargs b = let pickle = PyModule . import " pickle " in run ( pickle . $ String " loads " ) ? kwargs [ Bytes b ] |
let print ? kwargs args = run ( eval " print " ) args ? kwargs |> ignore |
let all_methods = ref [ ] |
let c_function fn m ~ name = let pymethod = allocate_n C . _Py_method ~ count : 1 in all_methods := pymethod :: ! all_methods ; setf !@ pymethod ml_name name ; setf !@ pymethod ml_meth fn ; setf !@ pymethod ml_flags 1 ; setf !@ pymethod ml_doc " doc " ; let name = to_object ( String name ) in wrap ( _PyCFunction_New py... |
module Numpy = struct let npy_intp = intptr_t type npy_intp = Intptr . t let shape pyobject = Object . to_list Object . to_int ( pyobject . $ ( String " shape " ) ) type t = { get_version : unit -> Unsigned . uint ; get_ptr : pyobject -> npy_intp ptr -> unit ptr ; object_type : pyobject -> int -> int ; new_array : pyob... |
module CamlModule = struct type pyvalue = t type t = Object . t let create name = PyModule . get name let add_int = PyModule . add_int let add_string = PyModule . add_string let add_object t name o = PyModule . add_object t name ( to_object o ) let fns = ref [ ] let add_fn t name fn = fns := fn :: ! fns ; let fn _none ... |
let ( ) = initialize ( ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.