libpython-clj2.codegen

Generate a namespace on disk for a python module or instances

write-namespace!

(write-namespace! py-mod-or-cls {:keys [output-fname output-dir ns-symbol ns-prefix symbol-name-remaps exclude], :or {output-dir "src", ns-prefix "python", exclude default-exclude}})(write-namespace! py-mod-or-cls)

Generate a clojure namespace file from a python module or class. If python hasn’t been initialized yet this will call the default python initialization. Accessing the generated namespace without initialization will cause an error.

Once generated this namespace is safe to be used for AOT,

Options:

  • :output-fname - override the autogenerated file path.
  • :output-dir - Defaults “src”. Set the output directory. The final filename, if :output-fname is not provided, is built up from :ns-prefix and py-mod-or-cls`.
  • :ns-symbol - The fully qualified namespace symbol. If not provided is built from :ns-prefix and py-mod-or-cls.
  • :ns-prefix - The prefix used for all python namespaces. Defaults to “python”.
  • :symbol-name-remaps - A list of remaps used to avoid name clashes with clojure.core or builtin java symbols.
  • :exclude - List of symbols used like (:refer-clojure :exclude %s). You can see the default list as codegen/default-exclude.

Example:

user> (require '[libpython-clj2.codegen :as codegen])
nil
user> (codegen/write-namespace!
       "builtins" {:symbol-name-remaps {"AssertionError" "PyAssertionError"
                                          "Exception" "PyException"}})
:ok
user> (require '[python.builtins :as python])
nil
user> (doc python/list)
-------------------------
python.builtins/list
[[self & [args {:as kwargs}]]]
  Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
nil
user> (doto (python/list)
        (.add 1)
        (.add 2))
[1, 2]