Skip to content

cyclone-scheme/python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python

Index

Intro

This package provides an experimental interface to Python 3, based on Ivan Raikov's package for CHICKEN Scheme.

Dependencies

None from Cyclone, but it needs python3-dev or similar. In Debian Linux you should run:

$ sudo apt install python3-dev

Test-dependencies

None

Foreign-dependencies

None

API

The API provides a set of high-level procedures and another with low-level ones. Only high-level procedures are documented here.

(cyclone python)

[procedure] (py-start)

Mandatory to run it before any other procedure from this package. Run only once.

[procedure] (py-stop)

Mandatory to run after any other procedure from this package to avoid memory leaking. Run only once.

[procedure] (py-run-simple-string code)

This is the simplest way to run Python code. We can, for example, run:

(py-run-simple-string "print(1+2)")
;; => 3

But note that this procedure does not return data. Use py-value or py bellow to get the return value in Scheme format.

[procedure] (py-run-file file)

If we already have a Python project, we can use this procedure to run it. See the run-file.scm example.

[procedure] (py-value var)

Evaluates var and returns it. See the test file for example of usage.

[syntax] (py-def name [arg1 ...])

This macro converts a Python procedure into a Scheme one. The name can be a string or a list (not quoted) with a string (the Python procedure name) and a symbol (the Scheme name for the procedure). arg1 is optional.

[syntax] (py-def-method name [arg1])

This macro converts a Python object method into a Scheme procedure.

The name can be a string or a list (not quoted) with a string (the Python method name) and a symbol (the Scheme name for the created procedure). arg1 is optional.

The generated procedure is called with the method's parent object as the first parameter and has a rest parameter, so other arguments can be passed.

See the h5py.scm and swriter.scm for examples.

[syntax] (py-def-attribute name)

This macro converts a Python object attribute into a Scheme procedure. If you want to set the attribute into a variable, use py-value instead.

The generated procedure is called with the attribute's parent object as the first parameter. If a second parameter is given, the attribute is set to it.

The name can be a string or a list (not quoted) containing a string (the Python attribute name) and a symbol (the Scheme name for the created procedure). arg1 is optional.

See the h5py.scm and swriter.scm for examples.

[procedure] (py-call module procedure-or-attribute [arg1 ...])

This is the quickest form to retrieve Python values from module procedures or attributes. See the test file for example of usage.

[syntax] (with-python command1 ...)

A wrapper around the high-level commands above that runs (py-start) and (py-stop) automatically.

(with-python
 (py-import "math")
 (py-value "math.pi"))

is equivalent to

(py-start)
(py-import "math")
(py-value "math.pi")
(py-stop)

Examples

See the examples directories.

Author(s)

  • Ivan Raikov
  • Arthur Maciel

Maintainer(s)

Arthur Maciel

Version

"0.3.0"

License

BSD

Tags

FFI

About

A Python FFI library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages