You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-23Lines changed: 18 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ without copying them.
18
18
## Installation
19
19
20
20
Within Julia, just use the package manager to run `Pkg.add("PyCall")` to
21
-
install the files. Julia 0.7 or later is required.
21
+
install the files. Julia 0.7 or later is required.
22
22
23
23
The latest development version of PyCall is available from
24
24
<https://github.com/JuliaPy/PyCall.jl>. If you want to switch to
@@ -139,7 +139,7 @@ we could call the Newton solver in scipy.optimize via:
139
139
A macro exists for mimicking Python's "with statement". For example:
140
140
141
141
@pywith pybuiltin("open")("file.txt","w") as f begin
142
-
f[:write]("hello")
142
+
f.write("hello")
143
143
end
144
144
145
145
The type of `f` can be specified with `f::T` (for example, to override automatic
@@ -162,18 +162,13 @@ example, using [Biopython](http://biopython.org/wiki/Seq) we can do:
162
162
@pyimport Bio.Seq as s
163
163
@pyimport Bio.Alphabet as a
164
164
my_dna = s.Seq("AGTACACTGGT", a.generic_dna)
165
-
my_dna[:find]("ACT")
166
-
167
-
whereas in Python the last step would have been `my_dna.find("ACT")`.
165
+
my_dna.find("ACT")
168
166
169
167
## Troubleshooting
170
168
171
169
Here are solutions to some common problems:
172
170
173
-
* As mentioned above, use `foo[:bar]` and `foo[:bar](...)` rather than `foo.bar` and `foo.bar(...)`,
174
-
respectively, to access attributes and methods of Python objects.
175
-
176
-
* By default, PyCall [doesn't include the current directory in the Python search path](https://github.com/JuliaPy/PyCall.jl/issues/48). If you want to do that (in order to load a Python module from the current directory), just run `pushfirst!(PyVector(pyimport("sys")["path"]), "")`.
171
+
* By default, PyCall [doesn't include the current directory in the Python search path](https://github.com/JuliaPy/PyCall.jl/issues/48). If you want to do that (in order to load a Python module from the current directory), just run `pushfirst!(PyVector(pyimport("sys")."path"), "")`.
177
172
178
173
## Python object interfaces
179
174
@@ -199,9 +194,9 @@ dates/periods, and functions, along with tuples and arrays/lists
199
194
thereof, but more are planned. (Julia symbols are converted to Python
200
195
strings.)
201
196
202
-
Given `o::PyObject`, `o[:attribute]` is equivalent to `o.attribute`
197
+
Given `o::PyObject`, `o.attribute` in Julia is equivalent to `o.attribute`
203
198
in Python, with automatic type conversion. To get an attribute as a
204
-
`PyObject` without type conversion, do `o["attribute"]` instead.
199
+
`PyObject` without type conversion, do `o."attribute"` instead.
205
200
The `keys(o::PyObject)` function returns an array of the available
206
201
attribute symbols.
207
202
@@ -338,10 +333,10 @@ and also by providing more type information to the Julia compiler.
338
333
339
334
*`pyimport(s)`: Import the Python module `s` (a string or symbol) and
340
335
return a pointer to it (a `PyObject`). Functions or other symbols
341
-
in the module may then be looked up by `s[name]` where `name` is a
336
+
in the module may then be looked up by `s.name` where `name` is a
342
337
string (for the raw `PyObject`) or symbol (for automatic
343
338
type-conversion). Unlike the `@pyimport` macro, this does not
344
-
define a Julia module and members cannot be accessed with `s.name`.
339
+
define a Julia module.
345
340
346
341
*`py"..."` evaluates `"..."` as a Python string, equivalent to
347
342
Python's [`eval`](https://docs.python.org/2/library/functions.html#eval) function, and returns the result
@@ -377,7 +372,7 @@ and also by providing more type information to the Julia compiler.
377
372
instead use `w.pymember(:member)` (for the `PyAny` conversion) or
378
373
`w.pymember("member")` (for the raw `PyObject`). `pywrap` is rather
379
374
inefficient since it converts *every* member of `o` at once; you
380
-
are generally encouraged to simply access members via `o[:member]`
375
+
are generally encouraged to simply access members via `o.member`
381
376
rather than using `pywrap`.
382
377
383
378
Occasionally, you may need to pass a keyword argument to Python that
@@ -411,15 +406,15 @@ For instance,
411
406
@pyimport numpy.polynomial as P
412
407
@pydef mutable struct Doubler <: P.Polynomial
413
408
function __init__(self, x=10)
414
-
self[:x] = x
409
+
self.x = x
415
410
end
416
411
my_method(self, arg1::Number) = arg1 + 20
417
-
x2.get(self) = self[:x] * 2
412
+
x2.get(self) = self.x * 2
418
413
function x2.set!(self, new_val)
419
-
self[:x] = new_val / 2
414
+
self.x = new_val / 2
420
415
end
421
416
end
422
-
Doubler()[:x2]
417
+
Doubler().x2
423
418
424
419
is essentially equivalent to the following Python code:
425
420
@@ -450,22 +445,22 @@ Here's another example using [Tkinter](https://wiki.python.org/moin/TkInter):
0 commit comments