44 function gtag ( ) { dataLayer . push ( arguments ) ; }
55 gtag ( 'js' , new Date ( ) ) ;
66
7- gtag ( 'config' , 'G-LN7PG6FJ2D' ) ; </ script > < link rel ="stylesheet " type ="text/css " href ="css/default.css " /> < link rel ="stylesheet " type ="text/css " href ="highlight/solarized-light.css " /> < script type ="text/javascript " src ="highlight/highlight.min.js "> </ script > < script type ="text/javascript " src ="js/jquery.min.js "> </ script > < script type ="text/javascript " src ="js/page_effects.js "> </ script > < script > hljs . initHighlightingOnLoad ( ) ; </ script > </ head > < body > < div id ="header "> < h2 > Generated by < a href ="https://github.com/weavejester/codox "> Codox</ a > with < a href ="https://github.com/xsc/codox-theme-rdash "> RDash UI</ a > theme</ h2 > < h1 > < a href ="index.html "> < span class ="project-title "> < span class ="project-name "> libpython-clj</ span > < span class ="project-version "> 2.018</ span > </ span > </ a > </ h1 > </ div > < div class ="sidebar primary "> < h3 class ="no-link "> < span class ="inner "> Project</ span > </ h3 > < ul class ="index-link "> < li class ="depth-1 "> < a href ="index.html "> < div class ="inner "> Index</ div > </ a > </ li > </ ul > < h3 class ="no-link "> < span class ="inner "> Topics</ span > </ h3 > < ul > < li class ="depth-1 current "> < a href ="Usage.html "> < div class ="inner "> < span > LibPython-CLJ Usage</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="embedded.html "> < div class ="inner "> < span > Embedding Clojure In Python</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="environments.html "> < div class ="inner "> < span > Python Environments</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="new-to-clojure.html "> < div class ="inner "> < span > So Many Parenthesis!</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="scopes-and-gc.html "> < div class ="inner "> < span > Scopes And Garbage Collection</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="slicing.html "> < div class ="inner "> < span > Slicing And Slices</ span > </ div > </ a > </ li > </ ul > < h3 class ="no-link "> < span class ="inner "> Namespaces</ span > </ h3 > < ul > < li class ="depth-1 "> < div class ="no-link "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > libpython-clj2</ span > </ div > </ div > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.codegen.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > codegen</ span > </ div > </ a > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.embedded.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > embedded</ span > </ div > </ a > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.java-api.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > java-api</ span > </ div > </ a > </ li > < li class ="depth-2 "> < a href ="libpython-clj2.python.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > python</ span > </ div > </ a > </ li > < li class ="depth-3 "> < a href ="libpython-clj2.python.np-array.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > np-array</ span > </ div > </ a > </ li > < li class ="depth-2 "> < a href ="libpython-clj2.require.html "> < div class ="inner "> < span class ="tree " style ="top: -52px; "> < span class ="top " style ="height: 61px; "> </ span > < span class ="bottom "> </ span > </ span > < span > require</ span > </ div > </ a > </ li > </ ul > </ div > < div class ="document " id ="content "> < div class ="doc "> < div class ="markdown "> < h1 > LibPython-CLJ Usage</ h1 >
7+ gtag ( 'config' , 'G-LN7PG6FJ2D' ) ; </ script > < link rel ="stylesheet " type ="text/css " href ="css/default.css " /> < link rel ="stylesheet " type ="text/css " href ="highlight/solarized-light.css " /> < script type ="text/javascript " src ="highlight/highlight.min.js "> </ script > < script type ="text/javascript " src ="js/jquery.min.js "> </ script > < script type ="text/javascript " src ="js/page_effects.js "> </ script > < script > hljs . initHighlightingOnLoad ( ) ; </ script > </ head > < body > < div id ="header "> < h2 > Generated by < a href ="https://github.com/weavejester/codox "> Codox</ a > with < a href ="https://github.com/xsc/codox-theme-rdash "> RDash UI</ a > theme</ h2 > < h1 > < a href ="index.html "> < span class ="project-title "> < span class ="project-name "> libpython-clj</ span > < span class ="project-version "> 2.019-SNAPSHOT</ span > </ span > </ a > </ h1 > </ div > < div class ="sidebar primary "> < h3 class ="no-link "> < span class ="inner "> Project</ span > </ h3 > < ul class ="index-link "> < li class ="depth-1 "> < a href ="index.html "> < div class ="inner "> Index</ div > </ a > </ li > </ ul > < h3 class ="no-link "> < span class ="inner "> Topics</ span > </ h3 > < ul > < li class ="depth-1 current "> < a href ="Usage.html "> < div class ="inner "> < span > LibPython-CLJ Usage</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="embedded.html "> < div class ="inner "> < span > Embedding Clojure In Python</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="environments.html "> < div class ="inner "> < span > Python Environments</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="new-to-clojure.html "> < div class ="inner "> < span > So Many Parenthesis!</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="scopes-and-gc.html "> < div class ="inner "> < span > Scopes And Garbage Collection</ span > </ div > </ a > </ li > < li class ="depth-1 "> < a href ="slicing.html "> < div class ="inner "> < span > Slicing And Slices</ span > </ div > </ a > </ li > </ ul > < h3 class ="no-link "> < span class ="inner "> Namespaces</ span > </ h3 > < ul > < li class ="depth-1 "> < div class ="no-link "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > libpython-clj2</ span > </ div > </ div > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.codegen.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > codegen</ span > </ div > </ a > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.embedded.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > embedded</ span > </ div > </ a > </ li > < li class ="depth-2 branch "> < a href ="libpython-clj2.java-api.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > java-api</ span > </ div > </ a > </ li > < li class ="depth-2 "> < a href ="libpython-clj2.python.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > python</ span > </ div > </ a > </ li > < li class ="depth-3 "> < a href ="libpython-clj2.python.np-array.html "> < div class ="inner "> < span class ="tree "> < span class ="top "> </ span > < span class ="bottom "> </ span > </ span > < span > np-array</ span > </ div > </ a > </ li > < li class ="depth-2 "> < a href ="libpython-clj2.require.html "> < div class ="inner "> < span class ="tree " style ="top: -52px; "> < span class ="top " style ="height: 61px; "> </ span > < span class ="bottom "> </ span > </ span > < span > require</ span > </ div > </ a > </ li > </ ul > </ div > < div class ="document " id ="content "> < div class ="doc "> < div class ="markdown "> < h1 > LibPython-CLJ Usage</ h1 >
88< p > Python objects are essentially two dictionaries, one for 'attributes' and one for
99'items'. When you use python and use the '.' operator, you are referencing attributes.
1010If you use the '[]' operator, then you are referencing items. Attributes are built in,
@@ -270,7 +270,7 @@ <h5>New sugar (fixme)</h5>
270270Concrete examples shown below.</ p >
271271< p > < strong > Nested attribute access</ strong >
272272To achieve a chain of method/attribute access, use the < code > py..</ code > for.</ p >
273- < pre > < code class ="language-clojure "> (py.. (requests/get "http://www.google.com")
273+ < pre > < code class ="language-clojure "> (py.. (requests/get "http://www.google.com")
274274 -content
275275 (decode "latin-1"))
276276</ code > </ pre >
@@ -348,4 +348,74 @@ <h3>Numpy</h3>
348348please refer to the datatype library < a href ="https://github.com/techascent/tech.datatype/tree/master/docs "> documentation</ a > .</ p >
349349< p > Just keep in mind, careless usage of zero copy is going to cause spooky action at a
350350distance.</ p >
351+ < h3 > Pickle</ h3 >
352+ < p > Speaking of numpy, you can pickle python objects and transform the result via numpy and dtype
353+ to a java byte array and back:</ p >
354+ < pre > < code class ="language-clojure "> user> (require '[libpython-clj2.python :as py])
355+ nil
356+ user> (py/initialize!)
357+ Sep 03, 2022 11:23:34 AM clojure.tools.logging$eval5948$fn__5951 invoke
358+ INFO: Detecting startup info
359+ Sep 03, 2022 11:23:34 AM clojure.tools.logging$eval5948$fn__5951 invoke
360+ INFO: Startup info {:lib-version "3.9", :java-library-path-addendum "/home/chrisn/miniconda3/lib", :exec-prefix "/home/chrisn/miniconda3", :executable "/home/chrisn/miniconda3/bin/python3", :libnames ("python3.9m" "python3.9"), :prefix "/home/chrisn/miniconda3", :base-prefix "/home/chrisn/miniconda3", :libname "python3.9m", :base-exec-prefix "/home/chrisn/miniconda3", :python-home "/home/chrisn/miniconda3", :version [3 9 1], :platform "linux"}
361+ Sep 03, 2022 11:23:34 AM clojure.tools.logging$eval5948$fn__5951 invoke
362+ INFO: Prefixing java library path: /home/chrisn/miniconda3/lib
363+ Sep 03, 2022 11:23:35 AM clojure.tools.logging$eval5948$fn__5951 invoke
364+ INFO: Loading python library: python3.9
365+ Sep 03, 2022 11:23:35 AM clojure.tools.logging$eval5948$fn__5951 invoke
366+ INFO: Reference thread starting
367+ :ok
368+ user> (def data (py/->python {:a 1 :b 2}))
369+ #'user/data
370+ user> (def pickle (py/import-module "pickle"))
371+ #'user/pickle
372+ user> (def bdata (py/py. pickle dumps data))
373+ #'user/bdata
374+ user> bdata
375+ b'\x80\x04\x95\x11\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x01a\x94K\x01\x8c\x01b\x94K\x02u.'
376+ user> (def np (py/import-module "numpy"))
377+ #'user/np
378+ user> (py/py. np frombuffer bdata :dtype "int8")
379+ [-128 4 -107 17 0 0 0 0 0 0 0 125 -108 40
380+ -116 1 97 -108 75 1 -116 1 98 -108 75 2 117 46]
381+ user> (require '[libpython-clj2.python.np-array])
382+ nil
383+ user> (def ary (py/py. np frombuffer bdata :dtype "int8"))
384+ #'user/ary
385+ user> (py/->jvm ary)
386+ #tech.v3.tensor<int8>[28]
387+ [-128 4 -107 17 0 0 0 0 0 0 0 125 -108 40 -116 1 97 -108 75 1 -116 1 98 -108 75 2 117 46]
388+ user> (require '[tech.v3.datatype :as dt])
389+ nil
390+ user> (dt/->byte-array *2)
391+ [-128, 4, -107, 17, 0, 0, 0, 0, 0, 0, 0, 125, -108, 40, -116, 1, 97, -108, 75, 1,
392+ -116, 1, 98, -108, 75, 2, 117, 46]
393+ user> (require '[tech.v3.tensor :as dtt])
394+ nil
395+ user> (dtt/as-tensor *2)
396+ nil
397+ user> (def bdata *3)
398+ #'user/bdata
399+ user> bdata
400+ [-128, 4, -107, 17, 0, 0, 0, 0, 0, 0, 0, 125, -108, 40, -116, 1, 97, -108, 75, 1,
401+ -116, 1, 98, -108, 75, 2, 117, 46]
402+ user> (type bdata)
403+ [B
404+ user> (def tens (dtt/reshape bdata [(dt/ecount bdata)]))
405+ #'user/tens
406+ user> (def pdata (py/->python tens))
407+ #'user/pdata
408+ user> pdata
409+ [-128 4 -107 17 0 0 0 0 0 0 0 125 -108 40
410+ -116 1 97 -108 75 1 -116 1 98 -108 75 2 117 46]
411+ user> (py/python-type *1)
412+ :ndarray
413+ user> (def py-ary *2)
414+ #'user/py-ary
415+ user> (def py-bytes (py/py. py-ary tobytes))
416+ #'user/py-bytes
417+ user> (py/py. pickle loads py-bytes)
418+ {'a': 1, 'b': 2}
419+ user>
420+ </ code > </ pre >
351421</ div > </ div > </ div > </ body > </ html >
0 commit comments