Permalink
Browse files

cleanup + lein & clojure version updates

  • Loading branch information...
1 parent 4c552cb commit 9ec6761515386507fb1ca113da294a506ba4f3bf @rplevy committed Oct 26, 2012
Showing with 43 additions and 46 deletions.
  1. +3 −3 project.clj
  2. +38 −41 src/clojure_python/core.clj
  3. +2 −2 test/clojure_python/t_core.clj
View
@@ -1,6 +1,6 @@
(defproject clojure-python "0.2.0"
:description "Improve seamlessness of Clojure Jython interop."
- :dependencies [[org.clojure/clojure "1.3.0"]
+ :dependencies [[org.clojure/clojure "1.4.0"]
[org.python/jython-standalone "2.5.2"]]
- :dev-dependencies [[midje "1.3.0"]]
- :main clojure-python.core)
+ :profiles {:dev {:dependencies [[midje "1.4.0"]]}}
+ :plugins [[lein-midje "2.0.0"]])
@@ -1,44 +1,42 @@
(ns clojure-python.core
- (:require (clojure [string :as s]))
- (:import (org.python.util PythonInterpreter)
- (org.python.core.*)))
+ (:require [clojure [string :as str]])
+ (:import [org.python.util PythonInterpreter]
+ [org.python.core PyObject Py]))
(declare ^:dynamic *interp*)
(defn append-paths
- "appends a vector of paths to the python system path"
+ "Appends a vector of paths to the python system path."
[libpaths]
(.exec *interp* "import sys")
(doseq [p libpaths]
(.exec *interp* (str "sys.path.append('" p "')")))
*interp*)
(defn init
- "Establish a global python interpreter.
- The init function is only usefully called once.
- Alternatively, only use with-interpreter."
+ "Establish a global python interpreter. The init function is only usefully
+ called once. Alternatively, only use with-interpreter."
[{:keys [libpaths] :as options}]
(defonce ^:dynamic
^{:doc "root binding serves as global python interpreter"}
*interp*
- (org.python.util.PythonInterpreter.))
+ (PythonInterpreter.))
(append-paths libpaths))
(defmacro with-interpreter
"Dynamically bind a new python interpreter for the calling context."
[{:keys [libpaths] :as options} & body]
- `(binding [*interp* (org.python.util.PythonInterpreter.)]
+ `(binding [*interp* (PythonInterpreter.)]
(append-paths ~libpaths)
~@body))
(defmacro py-import-lib
- "import lib
- defaults to use same name it has in python
- if it something like foo.bar, the name is bar."
+ "Import lib. Defaults to use same name it has in python. If it is something
+ like foo.bar, the name is bar."
[lib & libs]
(let [lib-sym (or (last libs) lib)
lib-strs (map name (cons lib libs))
- py-name (s/join "." lib-strs)]
+ py-name (str/join "." lib-strs)]
`(do (.exec *interp* (str "import " ~py-name))
(def ~lib-sym
(-> *interp*
@@ -49,28 +47,28 @@
.__dict__)))))
(defmacro py-import-obj
- "import objects from lib"
+ "Import objects from lib."
[lib obj & objs]
(cons 'do
(map
(fn [o#]
`(def ~o# (.__finditem__ ~lib ~(name o#)))))
(cons obj objs)))
-(defmacro py-fn
- "create a native clojure function applying the python
- wrapper calls on a python function at the top level of the library
- use this where lambda is preferred over named function"
+(defmacro py-fn
+ "Create a native clojure function applying the python wrapper calls on a python
+ function at the top level of the library use this where lambda is preferred
+ over named function."
[lib fun]
`(let [f# (.__finditem__
~lib
~(name fun))]
(fn [& args#]
(call f# args#))))
-(defmacro import-fn
- "this is like import but it defines the imported item
- as a native function that applies the python wrapper calls"
+(defmacro import-fn
+ "This is like import but it defines the imported item as a native function that
+ applies the python wrapper calls."
[lib fun & funs]
(cons 'do
(map
@@ -79,49 +77,48 @@
(cons fun funs))))
(defmacro __
- "access attribute of class or attribute of attribute of (and so on) class"
+ "Access attribute of class or attribute of attribute of (and so on) class."
([class attr]
`(.__findattr__ ~class ~(name attr)))
([class attr & attrs]
`(__ (__ ~class ~attr) ~@attrs)))
-(defmacro _>
- "call attribute as a method
- basic usage: (_> [class attrs ...] args ...)
- usage with keyword args: (_> [class attrs ...] args ... :key arg :key arg)
- keyword args must come after any non-keyword args"
+(defmacro _>
+ "Call attribute as a method.
+ Basic usage: (_> [class attrs ...] args ...)
+ Usage with keyword args: (_> [class attrs ...] args ... :key arg :key arg)
+ Keyword args must come after any non-keyword args"
([[class & attrs] & args]
(let [keywords (map name (filter keyword? args))
non-keywords (filter (fn [a] (not (keyword? a))) args)]
`(call (__ ~class ~@attrs) [~@non-keywords] ~@keywords))))
-(defn dir
- "it's slightly nicer to call the dir method in this way"
+(defn dir
+ "It's slightly nicer to call the dir method in this way."
[x] (seq (.__dir__ x)))
(defn pyobj-nth
- "nth item in a 'PyObjectDerived'"
+ "Nth item in a 'PyObjectDerived'."
[o i] (.__getitem__ o i))
(defn pyobj-range
- "access 'PyObjectDerived' items as non-lazy range"
+ "Access 'PyObjectDerived' items as non-lazy range."
[o start end] (for [i (range start end)] (pyobj-nth o i)))
(defn pyobj-iterate
- "access 'PyObjectDerived' items as Lazy Seq"
+ "Access 'PyObjectDerived' items as Lazy Seq."
[pyobj] (lazy-seq (.__iter__ pyobj)))
(defn java2py
- "to wrap java objects for input as jython, and unwrap Jython output as java"
+ "To wrap java objects for input as jython, and unwrap Jython output as java."
[args]
- (into-array
- org.python.core.PyObject
- (map #(. org.python.core.Py java2py %) args)))
-
-(defn call
- "The first len(args)-len(keywords) members of args[]
- are plain arguments. The last len(keywords) arguments
- are the values of the keyword arguments."
+ (into-array
+ PyObject
+ (map #(. Py java2py %) args)))
+
+(defn call
+ "The first len(args)-len(keywords) members of args[] are plain arguments. The
+ last len(keywords) arguments are the values of the keyword arguments."
[fun args & key-args]
(.__tojava__
(if key-args
@@ -1,6 +1,6 @@
(ns clojure-python.t-core
- (:use midje.sweet)
- (:require [clojure-python.core :as base]))
+ (:require [midje.sweet :refer :all]
+ [clojure-python.core :as base]))
(fact "append-paths adds the path to system path"
(binding [base/*interp* (org.python.util.PythonInterpreter.)]

0 comments on commit 9ec6761

Please sign in to comment.