@@ -13,6 +13,10 @@ use super::*;
13
13
/// Interface for [NumPy ndarray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html).
14
14
pub struct PyArray < T > ( PyObject , PhantomData < T > ) ;
15
15
16
+ pub fn get_array_module ( py : Python ) -> PyResult < & PyModule > {
17
+ PyModule :: import ( py, npyffi:: array:: MOD_NAME )
18
+ }
19
+
16
20
pyobject_native_type_convert ! (
17
21
PyArray <T >,
18
22
* npyffi:: PY_ARRAY_API . get_type_object( npyffi:: ArrayType :: PyArray_Type ) ,
@@ -75,10 +79,9 @@ impl<T> PyArray<T> {
75
79
/// # Example
76
80
/// ```
77
81
/// # extern crate pyo3; extern crate numpy; fn main() {
78
- /// use numpy::{ PyArray, PyArrayModule} ;
82
+ /// use numpy::PyArray;
79
83
/// let gil = pyo3::Python::acquire_gil();
80
- /// let np = PyArrayModule::import(gil.python()).unwrap();
81
- /// let arr = PyArray::<f64>::new(gil.python(), &np, &[4, 5, 6]);
84
+ /// let arr = PyArray::<f64>::new(gil.python(), &[4, 5, 6]);
82
85
/// assert_eq!(arr.ndim(), 3);
83
86
/// # }
84
87
/// ```
@@ -94,10 +97,9 @@ impl<T> PyArray<T> {
94
97
/// # Example
95
98
/// ```
96
99
/// # extern crate pyo3; extern crate numpy; fn main() {
97
- /// use numpy::{ PyArray, PyArrayModule} ;
100
+ /// use numpy::PyArray;
98
101
/// let gil = pyo3::Python::acquire_gil();
99
- /// let np = PyArrayModule::import(gil.python()).unwrap();
100
- /// let arr = PyArray::<f64>::new(gil.python(), &np, &[4, 5, 6]);
102
+ /// let arr = PyArray::<f64>::new(gil.python(), &[4, 5, 6]);
101
103
/// assert_eq!(arr.strides(), &[240, 48, 8]);
102
104
/// # }
103
105
/// ```
@@ -117,10 +119,9 @@ impl<T> PyArray<T> {
117
119
/// # Example
118
120
/// ```
119
121
/// # extern crate pyo3; extern crate numpy; fn main() {
120
- /// use numpy::{ PyArray, PyArrayModule} ;
122
+ /// use numpy::PyArray;
121
123
/// let gil = pyo3::Python::acquire_gil();
122
- /// let np = PyArrayModule::import(gil.python()).unwrap();
123
- /// let arr = PyArray::<f64>::new(gil.python(), &np, &[4, 5, 6]);
124
+ /// let arr = PyArray::<f64>::new(gil.python(), &[4, 5, 6]);
124
125
/// assert_eq!(arr.shape(), &[4, 5, 6]);
125
126
/// # }
126
127
/// ```
@@ -152,11 +153,10 @@ impl<T: TypeNum> PyArray<T> {
152
153
/// # Example
153
154
/// ```
154
155
/// # extern crate pyo3; extern crate numpy; fn main() {
155
- /// use numpy::{ PyArray, PyArrayModule} ;
156
+ /// use numpy::PyArray;
156
157
/// let gil = pyo3::Python::acquire_gil();
157
- /// let np = PyArrayModule::import(gil.python()).unwrap();
158
158
/// let slice = vec![1, 2, 3, 4, 5].into_boxed_slice();
159
- /// let pyarray = PyArray::from_boxed_slice(gil.python(), &np, slice);
159
+ /// let pyarray = PyArray::from_boxed_slice(gil.python(), slice);
160
160
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
161
161
/// # }
162
162
/// ```
@@ -169,12 +169,11 @@ impl<T: TypeNum> PyArray<T> {
169
169
/// # Example
170
170
/// ```
171
171
/// # extern crate pyo3; extern crate numpy; fn main() {
172
- /// use numpy::{ PyArray, PyArrayModule} ;
172
+ /// use numpy::PyArray;
173
173
/// use std::collections::BTreeSet;
174
174
/// let gil = pyo3::Python::acquire_gil();
175
- /// let np = PyArrayModule::import(gil.python()).unwrap();
176
175
/// let set: BTreeSet<u32> = [4, 3, 2, 5, 1].into_iter().cloned().collect();
177
- /// let pyarray = PyArray::from_iter(gil.python(), &np, set);
176
+ /// let pyarray = PyArray::from_iter(gil.python(), set);
178
177
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
179
178
/// # }
180
179
/// ```
@@ -187,10 +186,9 @@ impl<T: TypeNum> PyArray<T> {
187
186
/// # Example
188
187
/// ```
189
188
/// # extern crate pyo3; extern crate numpy; fn main() {
190
- /// use numpy::{ PyArray, PyArrayModule} ;
189
+ /// use numpy::PyArray;
191
190
/// let gil = pyo3::Python::acquire_gil();
192
- /// let np = PyArrayModule::import(gil.python()).unwrap();
193
- /// let pyarray = PyArray::from_vec(gil.python(), &np, vec![1, 2, 3, 4, 5]);
191
+ /// let pyarray = PyArray::from_vec(gil.python(), vec![1, 2, 3, 4, 5]);
194
192
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
195
193
/// # }
196
194
/// ```
@@ -206,13 +204,12 @@ impl<T: TypeNum> PyArray<T> {
206
204
/// # Example
207
205
/// ```
208
206
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
209
- /// use numpy::{ PyArray, PyArrayModule} ;
207
+ /// use numpy::PyArray;
210
208
/// let gil = pyo3::Python::acquire_gil();
211
- /// let np = PyArrayModule::import(gil.python()).unwrap();
212
209
/// let vec2 = vec![vec![1, 2, 3]; 2];
213
- /// let pyarray = PyArray::from_vec2(gil.python(), &np, & vec2).unwrap();
210
+ /// let pyarray = PyArray::from_vec2(gil.python(), &vec2).unwrap();
214
211
/// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2, 3], [1, 2, 3]].into_dyn());
215
- /// assert!(PyArray::from_vec2(gil.python(), &np, & vec![vec![1], vec![2, 3]]).is_err());
212
+ /// assert!(PyArray::from_vec2(gil.python(), &vec![vec![1], vec![2, 3]]).is_err());
216
213
/// # }
217
214
/// ```
218
215
pub fn from_vec2 < ' py > ( py : Python < ' py > , v : & Vec < Vec < T > > ) -> Result < & ' py Self , ArrayCastError > {
@@ -236,16 +233,15 @@ impl<T: TypeNum> PyArray<T> {
236
233
/// # Example
237
234
/// ```
238
235
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
239
- /// use numpy::{ PyArray, PyArrayModule} ;
236
+ /// use numpy::PyArray;
240
237
/// let gil = pyo3::Python::acquire_gil();
241
- /// let np = PyArrayModule::import(gil.python()).unwrap();
242
238
/// let vec2 = vec![vec![vec![1, 2]; 2]; 2];
243
- /// let pyarray = PyArray::from_vec3(gil.python(), &np, & vec2).unwrap();
239
+ /// let pyarray = PyArray::from_vec3(gil.python(), &vec2).unwrap();
244
240
/// assert_eq!(
245
241
/// pyarray.as_array().unwrap(),
246
242
/// array![[[1, 2], [1, 2]], [[1, 2], [1, 2]]].into_dyn()
247
243
/// );
248
- /// assert!(PyArray::from_vec3(gil.python(), &np, & vec![vec![vec![1], vec![]]]).is_err());
244
+ /// assert!(PyArray::from_vec3(gil.python(), &vec![vec![vec![1], vec![]]]).is_err());
249
245
/// # }
250
246
/// ```
251
247
pub fn from_vec3 < ' py > (
@@ -273,10 +269,9 @@ impl<T: TypeNum> PyArray<T> {
273
269
/// # Example
274
270
/// ```
275
271
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
276
- /// use numpy::{ PyArray, PyArrayModule} ;
272
+ /// use numpy::PyArray;
277
273
/// let gil = pyo3::Python::acquire_gil();
278
- /// let np = PyArrayModule::import(gil.python()).unwrap();
279
- /// let pyarray = PyArray::from_ndarray(gil.python(), &np, array![[1, 2], [3, 4]]);
274
+ /// let pyarray = PyArray::from_ndarray(gil.python(), array![[1, 2], [3, 4]]);
280
275
/// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2], [3, 4]].into_dyn());
281
276
/// # }
282
277
/// ```
@@ -385,10 +380,9 @@ impl<T: TypeNum> PyArray<T> {
385
380
/// # Example
386
381
/// ```
387
382
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
388
- /// use numpy::{ PyArray, PyArrayModule} ;
383
+ /// use numpy::PyArray;
389
384
/// let gil = pyo3::Python::acquire_gil();
390
- /// let np = PyArrayModule::import(gil.python()).unwrap();
391
- /// let pyarray = PyArray::<i32>::new(gil.python(), &np, &[4, 5, 6]);
385
+ /// let pyarray = PyArray::<i32>::new(gil.python(), &[4, 5, 6]);
392
386
/// assert_eq!(pyarray.shape(), &[4, 5, 6]);
393
387
/// # }
394
388
/// ```
@@ -404,10 +398,9 @@ impl<T: TypeNum> PyArray<T> {
404
398
/// # Example
405
399
/// ```
406
400
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
407
- /// use numpy::{ PyArray, PyArrayModule} ;
401
+ /// use numpy::PyArray;
408
402
/// let gil = pyo3::Python::acquire_gil();
409
- /// let np = PyArrayModule::import(gil.python()).unwrap();
410
- /// let pyarray = PyArray::zeros(gil.python(), &np, &[2, 2], false);
403
+ /// let pyarray = PyArray::zeros(gil.python(), &[2, 2], false);
411
404
/// assert_eq!(pyarray.as_array().unwrap(), array![[0, 0], [0, 0]].into_dyn());
412
405
/// # }
413
406
/// ```
@@ -433,12 +426,11 @@ impl<T: TypeNum> PyArray<T> {
433
426
/// # Example
434
427
/// ```
435
428
/// # extern crate pyo3; extern crate numpy; fn main() {
436
- /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
429
+ /// use numpy::{PyArray, IntoPyArray};
437
430
/// let gil = pyo3::Python::acquire_gil();
438
- /// let np = PyArrayModule::import(gil.python()).unwrap();
439
- /// let pyarray = PyArray::<f64>::arange(gil.python(), &np, 2.0, 4.0, 0.5);
431
+ /// let pyarray = PyArray::<f64>::arange(gil.python(), 2.0, 4.0, 0.5);
440
432
/// assert_eq!(pyarray.as_slice().unwrap(), &[2.0, 2.5, 3.0, 3.5]);
441
- /// let pyarray = PyArray::<i32>::arange(gil.python(), &np, -2.0, 4.0, 3.0);
433
+ /// let pyarray = PyArray::<i32>::arange(gil.python(), -2.0, 4.0, 3.0);
442
434
/// assert_eq!(pyarray.as_slice().unwrap(), &[-2, 1]);
443
435
/// # }
444
436
pub fn arange < ' py > ( py : Python < ' py > , start : f64 , stop : f64 , step : f64 ) -> & ' py Self {
@@ -452,15 +444,14 @@ impl<T: TypeNum> PyArray<T> {
452
444
/// # Example
453
445
/// ```
454
446
/// # extern crate pyo3; extern crate numpy; fn main() {
455
- /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
447
+ /// use numpy::{PyArray, IntoPyArray};
456
448
/// let gil = pyo3::Python::acquire_gil();
457
- /// let np = PyArrayModule::import(gil.python()).unwrap();
458
- /// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
459
- /// let mut pyarray_i = PyArray::<i64>::new(gil.python(), &np, &[3]);
460
- /// assert!(pyarray_f.copy_to(&np, &mut pyarray_i).is_ok());
449
+ /// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
450
+ /// let pyarray_i = PyArray::<i64>::new(gil.python(), &[3]);
451
+ /// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
461
452
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
462
453
/// # }
463
- pub fn copy_to < U : TypeNum > ( & self , other : & mut PyArray < U > ) -> Result < ( ) , ArrayCastError > {
454
+ pub fn copy_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ArrayCastError > {
464
455
let self_ptr = self . as_array_ptr ( ) ;
465
456
let other_ptr = other. as_array_ptr ( ) ;
466
457
let result = unsafe { PY_ARRAY_API . PyArray_CopyInto ( other_ptr, self_ptr) } ;
@@ -478,15 +469,14 @@ impl<T: TypeNum> PyArray<T> {
478
469
/// # Example
479
470
/// ```
480
471
/// # extern crate pyo3; extern crate numpy; fn main() {
481
- /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
472
+ /// use numpy::{PyArray, IntoPyArray};
482
473
/// let gil = pyo3::Python::acquire_gil();
483
- /// let np = PyArrayModule::import(gil.python()).unwrap();
484
- /// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
485
- /// let mut pyarray_i = PyArray::<i64>::new(gil.python(), &np, &[3]);
486
- /// assert!(pyarray_f.move_to(&np, &mut pyarray_i).is_ok());
474
+ /// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
475
+ /// let pyarray_i = PyArray::<i64>::new(gil.python(), &[3]);
476
+ /// assert!(pyarray_f.move_to(pyarray_i).is_ok());
487
477
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
488
478
/// # }
489
- pub fn move_to < U : TypeNum > ( & self , other : & mut PyArray < U > ) -> Result < ( ) , ArrayCastError > {
479
+ pub fn move_to < U : TypeNum > ( & self , other : & PyArray < U > ) -> Result < ( ) , ArrayCastError > {
490
480
let self_ptr = self . as_array_ptr ( ) ;
491
481
let other_ptr = other. as_array_ptr ( ) ;
492
482
let result = unsafe { PY_ARRAY_API . PyArray_MoveInto ( other_ptr, self_ptr) } ;
@@ -504,11 +494,10 @@ impl<T: TypeNum> PyArray<T> {
504
494
/// # Example
505
495
/// ```
506
496
/// # extern crate pyo3; extern crate numpy; fn main() {
507
- /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
497
+ /// use numpy::{PyArray, IntoPyArray};
508
498
/// let gil = pyo3::Python::acquire_gil();
509
- /// let np = PyArrayModule::import(gil.python()).unwrap();
510
- /// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
511
- /// let pyarray_i = pyarray_f.cast::<i32>(gil.python(), &np, false).unwrap();
499
+ /// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
500
+ /// let pyarray_i = pyarray_f.cast::<i32>(gil.python(), false).unwrap();
512
501
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
513
502
/// # }
514
503
pub fn cast < ' py , U : TypeNum > (
0 commit comments