Skip to content

pyclass #124

Closed
Closed
@yakir12

Description

@yakir12

I'm trying to implement a Python class using PythonCall.pyclass.

The following works as expected:

using PythonCall

Person = pyclass("Person", (); print_random = print  rand)
@pyexec (Person = Person) => """
p1 = Person()
p1.print_random() 
"""
# prints a random number

But any attempt to include methods that accept arguments referencing the instance of the class itself:

Person = pyclass("Person", (); __init__ = (self) -> (self.state = 0))
@pyexec (Person = Person) => """
p1 = Person()
"""

fails:

ERROR: Python: TypeError: Julia: MethodError: no method matching (::var"#5#6")()
Closest candidates are:
  (::var"#5#6")(!Matched::Any) at REPL[15]:1
Python stacktrace:
 [1] __call__
   @ /home/yakir/.julia/packages/PythonCall/Z6DIG/src/jlwrap/any.jl:167:30
 [2] <module>
   @ REPL[16]:1:1
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/Z6DIG/src/err.jl:10 [inlined]
 [3] pycallargs(f::Py, args::Py)
   @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/abstract/object.jl:153
 [4] pycall(::Py, ::Py, ::Vararg{Py}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/abstract/object.jl:171
 [5] pycall
   @ ~/.julia/packages/PythonCall/Z6DIG/src/abstract/object.jl:161 [inlined]
 [6] #_#11
   @ ~/.julia/packages/PythonCall/Z6DIG/src/Py.jl:330 [inlined]
 [7] Py
   @ ~/.julia/packages/PythonCall/Z6DIG/src/Py.jl:330 [inlined]
 [8] pyexec(::Type{Nothing}, code::Py, globals::Module, locals::NamedTuple{(:Person,), Tuple{Py}})
   @ PythonCall ~/.julia/packages/PythonCall/Z6DIG/src/concrete/code.jl:98
 [9] top-level scope
   @ ~/.julia/packages/PythonCall/Z6DIG/src/concrete/code.jl:146

More context

I'm trying to use the picamera Python module and process each frame in Julia. The following MWE however segfaults:

ENV["JULIA_PYTHONCALL_EXE"] = "/usr/bin/python3" # I have to include this due to https://github.com/cjdoris/PythonCall.jl/issues/120

using PythonCall

SideEffect = pyclass("SideEffect", (), 
                   __init__ = () -> nothing,
                   write = s -> println(rand()),
                   flush = () -> print("done")
                  )

@pyexec (SideEffect = SideEffect) => """
import picamera
with picamera.PiCamera() as camera:
    camera.start_recording(SideEffect(), format='h264') # start_recording calls SideEffect.write for each frame and SideEffect.flush at the end
    camera.wait_recording(5)
    camera.stop_recording()
"""

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions