Comments (10)
How about
(py/get-slice my-array [py/... [1 nil] [2 nil]])
where py/...
acts like the builtins.Ellipsis
or ...
object.
In Python, that would be equivalent to:
my_array[..., 1:, 2:]
If I could, I'd just have the following:
(py[] my-array [py/... [1 nil] [2 nil]])
But I don't know if py[]
is a valid function or macro name. I don't think clojure allows that name. My Spacemacs config automatically puts a space in between the py
and the []
, so probably get-slice is okay.
from libpython-clj.
There aren't any docs on np slicing and indexing syntax. Would you please share how you wrote this example in libpython-clj?
from libpython-clj.
@zendevil - Now there is one very short document:
https://github.com/clj-python/libpython-clj/blob/master/docs/slicing.md
from libpython-clj.
There ought to be a concise way of indexing lists and numpy arrays. So far I haven't found any.
from libpython-clj.
from libpython-clj.
How about
(py/get-item ary [(builtins/slice 1 nil) (builtins/slice 1 nil)])
;; becomes
(py/get-item ary [[1 nil] [1 nil]])
Slicing is very common in python, and my guess is that most people don't know that it works with builtins.slice behind the scenes. We can keep it that way in this great library.
The slicing functionality can be built into py/get-item or py/get-slice can be added. If this functionality had already existed, you'd not want to go back to not having it, so how about adding it?
We can always have an experimental branch with things like these and the kebab case that I'd re-ignited interest in a few weeks ago.
Another pain point is having to do:
(do
(require-python 'x)
(require-python '[y :as foo])
(require-python 'z)
)
It would be better if we could do:
(require-python 'x '[y :as foo] 'z)
Or something along those lines, although I admit that talking is way easier than implementing all this.
from libpython-clj.
I agree that better support of slicing would be very cool. Potentially this would just a be a macro that took input that looked exactly like the python slice syntax and then produced a vector of slice objects.
from libpython-clj.
Regarding the legailty of py[]
as a symbol name, I believe the claim that it is not valid is correct -- at least it's not likely to work in a straight-forward way for use with def
, defn
, and friends.
Accorrding to: https://clojure.org/reference/reader#_symbols:
Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually).
Although these docs can be slightly off in some cases, I think they are correct in this case.
Also a repl started via clj
:
user=> (def py[] nil)
Syntax error compiling def at (REPL:1:1).
Too many arguments to def
def
doesn't succeed, and:
user=> (defn py[] [] nil)
#'user/py
defn
ends up defining a function with a different name.
On a side note, there is this:
user=> (symbol "py[]")
py[]
user=> (type (symbol "py[]"))
clojure.lang.Symbol
But it's probably better to pretend we didn't see that :)
from libpython-clj.
But it's probably better to pretend we didn't see that :)
🤣
from libpython-clj.
Are we sure a recipe wouldn't be better? Otherwise I suppose it could go in the sugar section.
(defn get-slice [x & args]
(letfn [(slice
([] (python/slice nil nil))
([x] (if (vector? x)
(apply python/slice x)
x))
([x y & xs]
(let [xs (into [x y] xs)]
(if (every? int? xs)
(apply python/slice xs)
(mapv slice xs)))))]
(py/get-item x (apply slice args))))
(get-slice (python/list [1 2 3]) 1 2)
=> [2]
(require-python '[numpy :as np])
(get-slice (np/array [[1 2] [2 3] [3 4] [4 5] [6 5]]) [1 2] python/Ellipsis)
=> [[2 3]]
(get-slice (np/array [[1 2] [2 3] [3 4] [4 5] [6 5]]) [1 2] [1])
=> [[2]]
I don't use numpy
that often so I don't know if this is super useful but it seems to work syntactically.
from libpython-clj.
Related Issues (20)
- Subprocess failed (exit code: 139)
- Cannot run the example in the tutorial in Cider with JDK 17 HOT 8
- process hanging in embeded mode on exception HOT 2
- from-import does not support "string"
- newbie: I get: `dir already refers to: #'clojure.repl/dir in namespace: user` HOT 2
- Newbie: Lein Clojure Windows Anaconda VSCode Cava REPL Returns (NoClassDefFoundError) HOT 5
- Create Python Module then Import Module and Call Function Inside Clojure? HOT 4
- Fix ubuntu version in dockerfiles HOT 6
- Consider releasing returned values from instance fns
- JDK-20 support HOT 3
- builtins/eval throws 'frame does not exist' HOT 2
- Unable to require libraries from within docker using conda HOT 6
- Failed to find a valid python library on MS-Windows with the official python distributions
- Extend CI coverage to all the mainstream architectures
- require-python missing functions with annotation HOT 3
- Make polyglot development easier by allow require-python to be path-informed HOT 1
- Data transformation pipelines? HOT 1
- NSWindow main thread error when trying to setup Gymnasium (openapi gym) HOT 5
- JVM crashes when requiring `libpython-clj2.require` on 64bit Raspberry Pi 4 HOT 1
- error of unhashable type HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libpython-clj.