1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
A. R. Shajii d5ce1f8ff9
Update docs (#28)
* Update docs

* Update docs

* Update docs

* GitBook: [#4] Add hint

* Update primer

* Re-organize docs

* Fix table

* Fix link

* GitBook: [#5] No subject

* GitBook: [#6] No subject

* Cleanup and doc fix

* Add IR docs

* Add ir docs

* Fix spelling error

* More IR docs

* Update README.md

* Update README.md

* Fix warning

* Update intro

* Update README.md

* Update docs

* Fix table

* Don't build docs

* Update docs

* Add Jupyter docs

* FIx snippet

* Update README.md

* Fix images

* Fix code block

* Update docs, update cmake

* Break up tutorial

* Update pipeline.svg

* Update docs for new version

* Add differences with Python docs
2022-07-26 16:08:42 -04:00

59 lines
1.8 KiB
Markdown

Codon can seamlessly call functions from C and Python:
``` python
from C import pow(float, float) -> float
pow(2.0, 2.0) # 4.0
# Import and rename function
# cobj is a C pointer (void*, char*, etc.)
from C import puts(cobj) -> void as print_line
print_line("hello".c_str()) # prints "hello"; c_str() converts Codon str to C string
```
`from C import` only works if the symbol is available to the program. If
you are running your programs via `codon`, you can link dynamic
libraries with `-l`: `codon run -l /path/to/library.so ...`.
You can also load shared libraries with `dlopen`:
``` python
LIBRARY = "somelib.so"
from C import LIBRARY.mymethod(int, float) -> cobj
from C import LIBRARY.myothermethod(int, float) -> cobj as my2
foo = mymethod(1, 2.2)
foo2 = my2(4, 3.2)
```
{% hint style="warning" %}
When importing external non-Codon functions, you must explicitly specify
argument and return types.
{% endhint %}
How about Python? If you have set the `CODON_PYTHON` environment
variable to point to the Python library, you can do:
``` python
from python import mymodule.myfunction(str) -> int as foo
print(foo("bar"))
```
You might want to execute more complex Python code within Codon. To that
end, you can use Codon's `@python` annotation:
``` python
@python
def scipy_eigenvalues(i: List[List[float]]) -> List[float]:
# Code within this block is executed by the Python interpreter,
# so it must be valid Python code.
import scipy.linalg
import numpy as np
data = np.array(i)
eigenvalues, _ = scipy.linalg.eig(data)
return list(eigenvalues)
print(scipy_eigenvalues([[1.0, 2.0], [3.0, 4.0]])) # [-0.372281, 5.37228]
```
Codon will automatically bridge any object that implements the
`__to_py__` and `__from_py__` magic methods. All standard Codon types
already implement these methods.