Skip to content

Commit 9ec6761

Browse files
committed
cleanup + lein & clojure version updates
1 parent 4c552cb commit 9ec6761

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed

project.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(defproject clojure-python "0.2.0"
22
:description "Improve seamlessness of Clojure Jython interop."
3-
:dependencies [[org.clojure/clojure "1.3.0"]
3+
:dependencies [[org.clojure/clojure "1.4.0"]
44
[org.python/jython-standalone "2.5.2"]]
5-
:dev-dependencies [[midje "1.3.0"]]
6-
:main clojure-python.core)
5+
:profiles {:dev {:dependencies [[midje "1.4.0"]]}}
6+
:plugins [[lein-midje "2.0.0"]])

src/clojure_python/core.clj

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
(ns clojure-python.core
2-
(:require (clojure [string :as s]))
3-
(:import (org.python.util PythonInterpreter)
4-
(org.python.core.*)))
2+
(:require [clojure [string :as str]])
3+
(:import [org.python.util PythonInterpreter]
4+
[org.python.core PyObject Py]))
55

66
(declare ^:dynamic *interp*)
77

88
(defn append-paths
9-
"appends a vector of paths to the python system path"
9+
"Appends a vector of paths to the python system path."
1010
[libpaths]
1111
(.exec *interp* "import sys")
1212
(doseq [p libpaths]
1313
(.exec *interp* (str "sys.path.append('" p "')")))
1414
*interp*)
1515

1616
(defn init
17-
"Establish a global python interpreter.
18-
The init function is only usefully called once.
19-
Alternatively, only use with-interpreter."
17+
"Establish a global python interpreter. The init function is only usefully
18+
called once. Alternatively, only use with-interpreter."
2019
[{:keys [libpaths] :as options}]
2120
(defonce ^:dynamic
2221
^{:doc "root binding serves as global python interpreter"}
2322
*interp*
24-
(org.python.util.PythonInterpreter.))
23+
(PythonInterpreter.))
2524
(append-paths libpaths))
2625

2726
(defmacro with-interpreter
2827
"Dynamically bind a new python interpreter for the calling context."
2928
[{:keys [libpaths] :as options} & body]
30-
`(binding [*interp* (org.python.util.PythonInterpreter.)]
29+
`(binding [*interp* (PythonInterpreter.)]
3130
(append-paths ~libpaths)
3231
~@body))
3332

3433
(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."
34+
"Import lib. Defaults to use same name it has in python. If it is something
35+
like foo.bar, the name is bar."
3836
[lib & libs]
3937
(let [lib-sym (or (last libs) lib)
4038
lib-strs (map name (cons lib libs))
41-
py-name (s/join "." lib-strs)]
39+
py-name (str/join "." lib-strs)]
4240
`(do (.exec *interp* (str "import " ~py-name))
4341
(def ~lib-sym
4442
(-> *interp*
@@ -49,28 +47,28 @@
4947
.__dict__)))))
5048

5149
(defmacro py-import-obj
52-
"import objects from lib"
50+
"Import objects from lib."
5351
[lib obj & objs]
5452
(cons 'do
5553
(map
5654
(fn [o#]
5755
`(def ~o# (.__finditem__ ~lib ~(name o#)))))
5856
(cons obj objs)))
5957

60-
(defmacro py-fn
61-
"create a native clojure function applying the python
62-
wrapper calls on a python function at the top level of the library
63-
use this where lambda is preferred over named function"
58+
(defmacro py-fn
59+
"Create a native clojure function applying the python wrapper calls on a python
60+
function at the top level of the library use this where lambda is preferred
61+
over named function."
6462
[lib fun]
6563
`(let [f# (.__finditem__
6664
~lib
6765
~(name fun))]
6866
(fn [& args#]
6967
(call f# args#))))
7068

71-
(defmacro import-fn
72-
"this is like import but it defines the imported item
73-
as a native function that applies the python wrapper calls"
69+
(defmacro import-fn
70+
"This is like import but it defines the imported item as a native function that
71+
applies the python wrapper calls."
7472
[lib fun & funs]
7573
(cons 'do
7674
(map
@@ -79,49 +77,48 @@
7977
(cons fun funs))))
8078

8179
(defmacro __
82-
"access attribute of class or attribute of attribute of (and so on) class"
80+
"Access attribute of class or attribute of attribute of (and so on) class."
8381
([class attr]
8482
`(.__findattr__ ~class ~(name attr)))
8583
([class attr & attrs]
8684
`(__ (__ ~class ~attr) ~@attrs)))
8785

88-
(defmacro _>
89-
"call attribute as a method
90-
basic usage: (_> [class attrs ...] args ...)
91-
usage with keyword args: (_> [class attrs ...] args ... :key arg :key arg)
92-
keyword args must come after any non-keyword args"
86+
(defmacro _>
87+
"Call attribute as a method.
88+
Basic usage: (_> [class attrs ...] args ...)
89+
Usage with keyword args: (_> [class attrs ...] args ... :key arg :key arg)
90+
Keyword args must come after any non-keyword args"
9391
([[class & attrs] & args]
9492
(let [keywords (map name (filter keyword? args))
9593
non-keywords (filter (fn [a] (not (keyword? a))) args)]
9694
`(call (__ ~class ~@attrs) [~@non-keywords] ~@keywords))))
9795

98-
(defn dir
99-
"it's slightly nicer to call the dir method in this way"
96+
(defn dir
97+
"It's slightly nicer to call the dir method in this way."
10098
[x] (seq (.__dir__ x)))
10199

102100
(defn pyobj-nth
103-
"nth item in a 'PyObjectDerived'"
101+
"Nth item in a 'PyObjectDerived'."
104102
[o i] (.__getitem__ o i))
105103

106104
(defn pyobj-range
107-
"access 'PyObjectDerived' items as non-lazy range"
105+
"Access 'PyObjectDerived' items as non-lazy range."
108106
[o start end] (for [i (range start end)] (pyobj-nth o i)))
109107

110108
(defn pyobj-iterate
111-
"access 'PyObjectDerived' items as Lazy Seq"
109+
"Access 'PyObjectDerived' items as Lazy Seq."
112110
[pyobj] (lazy-seq (.__iter__ pyobj)))
113111

114112
(defn java2py
115-
"to wrap java objects for input as jython, and unwrap Jython output as java"
113+
"To wrap java objects for input as jython, and unwrap Jython output as java."
116114
[args]
117-
(into-array
118-
org.python.core.PyObject
119-
(map #(. org.python.core.Py java2py %) args)))
120-
121-
(defn call
122-
"The first len(args)-len(keywords) members of args[]
123-
are plain arguments. The last len(keywords) arguments
124-
are the values of the keyword arguments."
115+
(into-array
116+
PyObject
117+
(map #(. Py java2py %) args)))
118+
119+
(defn call
120+
"The first len(args)-len(keywords) members of args[] are plain arguments. The
121+
last len(keywords) arguments are the values of the keyword arguments."
125122
[fun args & key-args]
126123
(.__tojava__
127124
(if key-args

test/clojure_python/t_core.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns clojure-python.t-core
2-
(:use midje.sweet)
3-
(:require [clojure-python.core :as base]))
2+
(:require [midje.sweet :refer :all]
3+
[clojure-python.core :as base]))
44

55
(fact "append-paths adds the path to system path"
66
(binding [base/*interp* (org.python.util.PythonInterpreter.)]

0 commit comments

Comments
 (0)