Skip to content

Commit 4c552cb

Browse files
committed
make it possible to import submodules of modules
1 parent ba6d25a commit 4c552cb

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject clojure-python "0.1.1"
1+
(defproject clojure-python "0.2.0"
22
:description "Improve seamlessness of Clojure Jython interop."
33
:dependencies [[org.clojure/clojure "1.3.0"]
44
[org.python/jython-standalone "2.5.2"]]

src/clojure_python/core.clj

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(ns clojure-python.core
2+
(:require (clojure [string :as s]))
23
(:import (org.python.util PythonInterpreter)
34
(org.python.core.*)))
45

@@ -30,24 +31,31 @@
3031
(append-paths ~libpaths)
3132
~@body))
3233

33-
(defmacro py-import
34-
"define a library using the same name it has in python
35-
if multiple arguments are given, the first is assumed
36-
to be a library that has been imported,
37-
and the others are objects to import from this library"
38-
([lib] ; import a library
39-
`(do (.exec *interp* (str "import " ~(name lib)))
40-
(def ~lib
41-
(-> *interp*
42-
.getLocals
43-
(.__getitem__ ~(name lib))
44-
.__dict__))))
45-
([lib & objects] ; import object from a library
46-
(cons 'do
47-
(map
48-
(fn [obj]
49-
`(def ~obj (.__finditem__ ~lib ~(name obj))))
50-
objects))))
34+
(defmacro py-import-lib
35+
"import lib
36+
defaults to use same name it has in python
37+
if it something like foo.bar, the name is bar."
38+
[lib & libs]
39+
(let [lib-sym (or (last libs) lib)
40+
lib-strs (map name (cons lib libs))
41+
py-name (s/join "." lib-strs)]
42+
`(do (.exec *interp* (str "import " ~py-name))
43+
(def ~lib-sym
44+
(-> *interp*
45+
.getLocals
46+
(.__getitem__ ~(first lib-strs))
47+
~@(map (fn [lib#] `(.__getattr__ ~lib#))
48+
(next lib-strs))
49+
.__dict__)))))
50+
51+
(defmacro py-import-obj
52+
"import objects from lib"
53+
[lib obj & objs]
54+
(cons 'do
55+
(map
56+
(fn [o#]
57+
`(def ~o# (.__finditem__ ~lib ~(name o#)))))
58+
(cons obj objs)))
5159

5260
(defmacro py-fn
5361
"create a native clojure function applying the python

test/clojure_python/t_core.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,29 @@
4141

4242
(fact "importing python modules works"
4343
(with-test-interp
44-
(base/py-import example)
44+
(base/py-import-lib example)
4545
(class example))
4646
=>
4747
org.python.core.PyStringMap)
4848

4949
(fact "importing python functions works"
5050
(with-test-interp
51-
(base/py-import example)
51+
(base/py-import-lib example)
5252
(base/import-fn example hello)
5353
(fn? hello))
5454
=>
5555
true)
5656

5757
(fact "calling python functions works"
5858
(with-test-interp
59-
(base/py-import example)
59+
(base/py-import-lib example)
6060
(base/import-fn example hello)
6161
(hello "world"))
6262
=>
6363
"hello, world how are you."
6464

6565
(with-test-interp
66-
(base/py-import example)
66+
(base/py-import-lib example)
6767
((base/py-fn example hello)
6868
"person"))
6969
=>

0 commit comments

Comments
 (0)