Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created May 24, 2020 04:11
Show Gist options
  • Save dfuenzalida/05eb0f6ba306386ace1a04eb22680352 to your computer and use it in GitHub Desktop.
Save dfuenzalida/05eb0f6ba306386ace1a04eb22680352 to your computer and use it in GitHub Desktop.
Sample run of funapp.clj - interop of Clojure and Python with libpython-clj
(ns appcompany.funapp
(:require [libpython-clj.python :as py :refer [py. py.- py.. py* py**]]
appcompany.python
[libpython-clj.require :refer [require-python import-python]]))
(import-python)
(require-python '[builtins :as python])
(require-python '[base64 :as pybase64])
(require-python '[funniest :as pyfunniest])
(defn encode-python [s]
(let [bytes (python/bytearray s "ascii") ;; bytes = bytearray(s, "ascii")
encoded (pybase64/b64encode bytes) ;; encoded = base64.b64encode(bytes)
b64enc (py. encoded decode "utf-8")] ;; b64enc = encoded.decode("utf-8")
b64enc))
(defn decode-java [s]
(-> (.decode (java.util.Base64/getDecoder) s)
(String. "UTF-8")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NEW CODE BELOW
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -main [& args]
(let [pyjoke (py/from-import funniest joke)]
(println (pyjoke))
;; Encode base 64 in Python and decode in java
(println (-> "IT WORKS!" encode-python decode-java))))
(-main)
# I ran the code above with: clj src/appcompany/funapp.clj
# Output:
May 23, 2020 9:09:52 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Executing python initialize with options:{:python-executable "./env/bin/python", :program-name nil, :python-home nil, :library-path nil}
May 23, 2020 9:09:52 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Detecting startup-info for Python executable: ./env/bin/python
May 23, 2020 9:09:52 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Startup info detected: {:python-home "/home/denis/Projects/libpython-clj/interop/env", :lib-version "3.6", :libname "python3.6m", :java-library-path-addendum "/home/denis/Projects/libpython-clj/interop/env/lib"}
May 23, 2020 9:09:52 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Setting java library path: /home/denis/Projects/libpython-clj/interop/env/lib:/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
May 23, 2020 9:09:52 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Reference thread starting
May 23, 2020 9:09:53 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Library python3.6m found at [:system "python3.6m"]
May 23, 2020 9:09:53 PM clojure.tools.logging$eval494$fn__497 invoke
INFO: Library c found at [:system "c"]
WARNING: AssertionError already refers to: class java.lang.AssertionError in namespace: builtins, being replaced by: #'builtins/AssertionError
WARNING: Exception already refers to: class java.lang.Exception in namespace: builtins, being replaced by: #'builtins/Exception
Wenn ist das Nunstück git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput.
IT WORKS!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment