1///|
2pub struct Matplotlib {
3  // matplotlib: PyModule 
4  
@python.PyModule
pyplot
:
struct @python.PyModule {
  // private fields
}
PyModule
5 // color_map: PyModule 6
@python.PyModule
pylab
:
struct @python.PyModule {
  // private fields
}
PyModule
7} 8 9///| 10fn
() -> Matplotlib!Error
new
() ->
struct Matplotlib {
  pyplot: @python.PyModule
  pylab: @python.PyModule
}
Matplotlib
!
type Error[A]
Error
{
11 //guard @python.pyimport("matplotlib") is Some(matplotlib) else { 12 // raise LoadMatplotlibError 13 //} 14 15 //guard @python.pyimport("matplotlib.cm") is Some(color_map) else { 16 // raise LoadColorMapError 17 //} 18 19 guard
(name : String, print_err~ : Bool = ..) -> @python.PyModule?

Import a python module

Example

test "Py Import" {
  let os = @python.pyimport("os")

  assert_true!(os is Some(_))
}
@python.pyimport
("matplotlib.pyplot") is
(@python.PyModule) -> @python.PyModule?
Some
(
@python.PyModule
pyplot
) else {
20 raise
MatplotlibError
LoadPyPlotError
21 } 22 guard
(name : String, print_err~ : Bool = ..) -> @python.PyModule?

Import a python module

Example

test "Py Import" {
  let os = @python.pyimport("os")

  assert_true!(os is Some(_))
}
@python.pyimport
("pylab") is
(@python.PyModule) -> @python.PyModule?
Some
(
@python.PyModule
pylab
) else { raise
MatplotlibError
LoadPylabError
}
23
struct Matplotlib {
  pyplot: @python.PyModule
  pylab: @python.PyModule
}
Matplotlib
::{
24 // matplotlib, 25
@python.PyModule
pyplot
,
26 // color_map, 27
@python.PyModule
pylab
,
28 } 29} 30 31///| 32fn
(fname : String) -> @python.PyCallable!MatplotlibError
pyplot_get_func
(
String
fname
:
String
String
) ->
struct @python.PyCallable {
  // private fields
}
Pycallable
!
type! MatplotlibError {
  LoadPyPlotError
  LoadPylabError
  LoadFuncError(String)
}
MatplotlibError
{
33 let
Matplotlib
lib
=
() -> Matplotlib
singleton
()
34 guard
Matplotlib
lib
.
@python.PyModule
pyplot
.
(self : @python.PyModule, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?

Get attribute by name.

Example

test "Py Import" {
  let collections = pyimport("collections").unwrap()
  guard collections.get_attr("Counter").unwrap() is PyCallable(counter)

  let list = [1L, 2L, 2L, 3L, 3L, 3L].map(PyInteger::from) |> PyList::from
  let args = PyTuple::new(1)
  args .. set(0, list)
  guard counter.invoke(args~) is Some(PyDict(cnt))
  inspect!(cnt, content="Counter({3: 3, 2: 2, 1: 1})")

  guard cnt.obj().get_attr("total") is Some(PyCallable(total))
  inspect!(total.invoke().unwrap(), content="PyInteger(6)")
}

// The above code is equivalent to the following python code:

import collections
from collections import Counter

list = [1, 2, 2, 3, 3, 3]
cnt = Counter(list)
print(cnt) # Counter({3: 3, 2: 2, 1: 1})

total = cnt.total()
print(total) # 6
get_attr
(
String
fname
) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
f
)) else {
35 raise
(String) -> MatplotlibError
LoadFuncError
("Didn't find function : \{
String
fname
} in matplotlib.pyplot")
36 } 37
@python.PyCallable
f
38} 39 40///| 41//fn pylab_get_func(fname : String) -> Pycallable!MatplotlibError { 42// let lib = singleton() 43// guard lib.pylab.get_attr(fname) is Some(PyCallable(f)) else { 44// raise LoadFuncError("Didn't find function : \{fname} in pylab") 45// } 46// f 47//} 48 49///| 50let
() -> Matplotlib
singleton
: () ->
struct Matplotlib {
  pyplot: @python.PyModule
  pylab: @python.PyModule
}
Matplotlib
=
() -> () -> Matplotlib
get_lib
()
51 52///| 53fn
() -> () -> Matplotlib
get_lib
() -> () ->
struct Matplotlib {
  pyplot: @python.PyModule
  pylab: @python.PyModule
}
Matplotlib
{
54
() -> Unit
@python.init_py
()
55 let
Matplotlib
mlib
= match
() -> Matplotlib!Error
new
?() {
56
(Matplotlib) -> Result[Matplotlib, Error]
Ok
(
Matplotlib
mlib
) =>
Matplotlib
mlib
57
(Error) -> Result[Matplotlib, Error]
Err
(
Error
e
) => {
58
(input : Error) -> Unit

Prints any value that implements the Show trait to the standard output, followed by a newline.

Parameters:

  • value : The value to be printed. Must implement the Show trait.

Example:

test "println" {
  println(42)
  println("Hello, World!")
  println([1, 2, 3])
}
println
(
Error
e
)
59
() -> Matplotlib
panic
()
60 } 61 } 62 fn() {
Matplotlib
mlib
}
63} 64 65///| 66pub fn
(nrows : Int, ncols : Int, figsize~ : (Double, Double) = ..) -> (Figure, Array[Array[Axes]])
subplots
(
67
Int
nrows
:
Int
Int
,
68
Int
ncols
:
Int
Int
,
69
(Double, Double)
figsize
~ : (
Double
Double
,
Double
Double
) = (8, 6)
70) -> (
struct Figure {
  obj: @python.PyObject
  // private fields
}
Figure
,
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
]]) {
71 guard
Int
nrows
(self_ : Int, other : Int) -> Bool
>=
1
(Bool, Bool) -> Bool
&&
Int
ncols
(self_ : Int, other : Int) -> Bool
>=
1
72 let
Matplotlib
lib
=
() -> Matplotlib
singleton
()
73 guard
Matplotlib
lib
.
@python.PyModule
pyplot
.
(self : @python.PyModule, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?

Get attribute by name.

Example

test "Py Import" {
  let collections = pyimport("collections").unwrap()
  guard collections.get_attr("Counter").unwrap() is PyCallable(counter)

  let list = [1L, 2L, 2L, 3L, 3L, 3L].map(PyInteger::from) |> PyList::from
  let args = PyTuple::new(1)
  args .. set(0, list)
  guard counter.invoke(args~) is Some(PyDict(cnt))
  inspect!(cnt, content="Counter({3: 3, 2: 2, 1: 1})")

  guard cnt.obj().get_attr("total") is Some(PyCallable(total))
  inspect!(total.invoke().unwrap(), content="PyInteger(6)")
}

// The above code is equivalent to the following python code:

import collections
from collections import Counter

list = [1, 2, 2, 3, 3, 3]
cnt = Counter(list)
print(cnt) # Counter({3: 3, 2: 2, 1: 1})

total = cnt.total()
print(total) # 6
get_attr
("subplots") is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
f
))
74 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(2)
75
@python.PyTuple
args
76 ..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
nrows
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
77 ..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(1,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
ncols
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
78 let
@python.PyDict
kwargs
=
struct @python.PyDict {
  // private fields
}
PyDict
::
() -> @python.PyDict

Creates a new python dict object.

Example

test "PyDict::new" {
  let dict = PyDict::new()
  dict
  ..set("one", PyInteger::from(1))
  ..set("two", PyFloat::from(2.0))
  ..set("three", PyBool::from(true));

  inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}")
}

The above code is equivalent to:

d = { 'one': 1, 'two': 2.0, 'three': True }

print(d) # Output: {'one': 1, 'two': 2.0, 'three': True}
new
()
79 let (
Double
w
,
Double
h
) =
(Double, Double)
figsize
80 let
@python.PyTuple
py_figsize_arg
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(2)
81
@python.PyTuple
py_figsize_arg
..
(self : @python.PyTuple, idx : Int, item : @python.PyFloat) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyFloat {
  // private fields
}
PyFloat
::
(Double) -> @python.PyFloat

Create a PyFloat from a Double value.

Example

test "PyFloat::from" {
  let f = @python.PyFloat::from(3.5);
  inspect!(f, content="3.5")
}
from
(
Double
w
))..
(self : @python.PyTuple, idx : Int, item : @python.PyFloat) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(1,
struct @python.PyFloat {
  // private fields
}
PyFloat
::
(Double) -> @python.PyFloat

Create a PyFloat from a Double value.

Example

test "PyFloat::from" {
  let f = @python.PyFloat::from(3.5);
  inspect!(f, content="3.5")
}
from
(
Double
h
))
82
@python.PyDict
kwargs
.
(self : @python.PyDict, key : String, val : @python.PyTuple) -> Unit

Set the elements of the dict by its key (String).

Example

test "PyDict::set" {
  let dict = PyDict::new()
  dict
  ..set("one", PyInteger::from(1))
  ..set("two", PyFloat::from(2.0))
  ..set("three", PyBool::from(true));
 
  inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}")
}

The above code is equivalent to:

d = dict()
d['one'] = 1
d['two'] = 2.0
d['three'] = True
set
("figsize",
@python.PyTuple
py_figsize_arg
)
83 guard
@python.PyCallable
f
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~,
@python.PyDict
kwargs
~) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyTuple) -> @python.PyObjectEnum
PyTuple
(
@python.PyTuple
fig_ax
))
84 guard
@python.PyTuple
fig_ax
[0] is
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
fig
)
85 let
Figure
fig
=
struct Figure {
  obj: @python.PyObject
  // private fields
}
Figure
::{
@python.PyObject
obj
:
@python.PyObject
fig
}
86 guard
@python.PyTuple
fig_ax
[1] is
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
py_axes
) // It possibly matpolotlib.axes._axes.Axes or numpy.ndarray
87 let
Array[Array[Axes]]
axes
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
]] =
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int = ..) -> Array[Array[Axes]]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
()
88 match (
Int
nrows
,
Int
ncols
) {
89 (1, 1) => { 90
Array[Array[Axes]]
axes
.
(self : Array[Array[Axes]], value : Array[Axes]) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int = ..) -> Array[Axes]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
())
91
Array[Array[Axes]]
axes
[0].
(self : Array[Axes], value : Axes) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
::{
@python.PyObject
obj
:
@python.PyObject
py_axes
})
92 } 93 (
Int
n
, 1) =>
94 for
Int
i
in
Int
0
..<
Int
n
{
95
Array[Array[Axes]]
axes
.
(self : Array[Array[Axes]], value : Array[Axes]) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int = ..) -> Array[Axes]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
())
96 guard
@python.PyObject
py_axes
.
(self : @python.PyObject, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?
get_attr
("__getitem__") is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
ax_getitem
))
97 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(1)
98
@python.PyTuple
args
..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
i
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
99 guard
@python.PyCallable
ax_getitem
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
ax
))
100
Array[Array[Axes]]
axes
(Array[Array[Axes]], Int) -> Array[Axes]

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : Array[Axes], value : Axes) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
::{
@python.PyObject
obj
:
@python.PyObject
ax
})
101 } 102 (1,
Int
n
) => {
103 let
Array[Axes]
col
=
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int = ..) -> Array[Axes]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
()
104 for
Int
i
in
Int
0
..<
Int
n
{
105 guard
@python.PyObject
py_axes
.
(self : @python.PyObject, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?
get_attr
("__getitem__") is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
ax_getitem
))
106 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(1)
107
@python.PyTuple
args
..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
i
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
108 guard
@python.PyCallable
ax_getitem
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
ax
))
109
Array[Axes]
col
.
(self : Array[Axes], value : Axes) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
::{
@python.PyObject
obj
:
@python.PyObject
ax
})
110 } 111
Array[Array[Axes]]
axes
.
(self : Array[Array[Axes]], value : Array[Axes]) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
Array[Axes]
col
)
112 } 113 (
Int
r
,
Int
c
) =>
114 for
Int
i
in
Int
0
..<
Int
r
{
115
Array[Array[Axes]]
axes
.
(self : Array[Array[Axes]], value : Array[Axes]) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(capacity~ : Int = ..) -> Array[Axes]

Creates a new empty array with an optional initial capacity.

Parameters:

  • capacity : The initial capacity of the array. If 0 (default), creates an array with minimum capacity. Must be non-negative.

Returns a new empty array of type Array[T] with the specified initial capacity.

Example:

test "Array::new" {
  let arr : Array[Int] = Array::new(capacity=10)
  inspect(arr.length(), content="0")
  inspect(arr.capacity(), content="10")
}

test "Array::new/default" {
  let arr : Array[Int] = Array::new()
  inspect(arr.length(), content="0")
}
new
())
116 guard
@python.PyObject
py_axes
.
(self : @python.PyObject, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?
get_attr
("__getitem__") is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
ax_r_getitem
))
117 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(1)
118
@python.PyTuple
args
..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
i
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
119 guard
@python.PyCallable
ax_r_getitem
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
ax_r
))
120 for
Int
j
in
Int
0
..<
Int
c
{
121 guard
@python.PyObject
ax_r
.
(self : @python.PyObject, attr : String, print_err~ : Bool = ..) -> @python.PyObjectEnum?
get_attr
("__getitem__") is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyCallable) -> @python.PyObjectEnum
PyCallable
(
@python.PyCallable
ax_c_getitem
))
122 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(1)
123
@python.PyTuple
args
..
(self : @python.PyTuple, idx : Int, item : @python.PyInteger) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
j
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
124 guard
@python.PyCallable
ax_c_getitem
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~) is
(@python.PyObjectEnum) -> @python.PyObjectEnum?
Some
(
(@python.PyObject) -> @python.PyObjectEnum
PyClass
(
@python.PyObject
ax
))
125
Array[Array[Axes]]
axes
(Array[Array[Axes]], Int) -> Array[Axes]

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : Array[Axes], value : Axes) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

let v = []
v.push(3)
push
(
struct Axes {
  obj: @python.PyObject
  // private fields
}
Axes
::{
@python.PyObject
obj
:
@python.PyObject
ax
})
126 } 127 } 128 } 129 (
Figure
fig
,
Array[Array[Axes]]
axes
)
130} 131 132///| 133pub fn
() -> Unit
show
() ->
Unit
Unit
{
134 let
@python.PyCallable
func
=
(fname : String) -> @python.PyCallable!MatplotlibError
pyplot_get_func
?("show").
(self : Result[@python.PyCallable, MatplotlibError]) -> @python.PyCallable
unwrap
()
135 let _ =
@python.PyCallable
func
.
(self : @python.PyCallable, args~ : @python.PyTuple = .., kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
()
136 137} 138//pub fn save(filename : String, dpi~ : Int = 0) -> Unit { 139// let func = pylab_get_func?("save").unwrap() 140// let py_str = PyString::from(filename) 141// let args = PyTuple::new(1)..set(0, py_str) 142// let kwargs = PyDict::new() 143// if dpi > 0 { 144// kwargs.set("dpi", PyInteger::from(dpi.to_int64())) 145// } 146// let _ = func.invoke(args~, kwargs~) 147// 148//} 149 150///| 151pub fn
(filename : String, dpi~ : Int = .., quality~ : Int = ..) -> Unit
savefig
(
String
filename
:
String
String
,
Int
dpi
~ :
Int
Int
= 0,
Int
quality
~ :
Int
Int
= 0) ->
Unit
Unit
{
152 let
@python.PyCallable
func
=
(fname : String) -> @python.PyCallable!MatplotlibError
pyplot_get_func
?("savefig").
(self : Result[@python.PyCallable, MatplotlibError]) -> @python.PyCallable
unwrap
()
153 let
@python.PyString
py_str
=
struct @python.PyString {
  // private fields
}
PyString
::
(String) -> @python.PyString

Create a PyString from a string

Example

test "PyString::from" {
  let s = @python.PyString::from("hello");
  inspect(s, content="\'hello\'");
}
from
(
String
filename
)
154 let
@python.PyTuple
args
=
struct @python.PyTuple {
  // private fields
}
PyTuple
::
(UInt64) -> @python.PyTuple

Create a PyTuple object.

Notes: Tuple type must know the size of the tuple.

Example:

test "PyTuple::new" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(1, 2.0, \'three\')")
}
new
(1)
155
@python.PyTuple
args
..
(self : @python.PyTuple, idx : Int, item : @python.PyString) -> Unit

Set the item at the given index.

Notes: Although python supports negative index, in moonbit, we don't have plan to support it. Code like tuple.set(-1, item) will raise IndexOutOfBoundError.

Example:

test "PyTuple::set" {
  let tuple = PyTuple::new(3)
  tuple
  ..set(0, PyInteger::from(1))
  ..set(1, PyFloat::from(2.0))
  ..set(2, PyString::from("three"));

  inspect!(tuple, content="(10, 2.0, \'three\')")
}
set
(0,
@python.PyString
py_str
)
156 let
@python.PyDict
kwargs
=
struct @python.PyDict {
  // private fields
}
PyDict
::
() -> @python.PyDict

Creates a new python dict object.

Example

test "PyDict::new" {
  let dict = PyDict::new()
  dict
  ..set("one", PyInteger::from(1))
  ..set("two", PyFloat::from(2.0))
  ..set("three", PyBool::from(true));

  inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}")
}

The above code is equivalent to:

d = { 'one': 1, 'two': 2.0, 'three': True }

print(d) # Output: {'one': 1, 'two': 2.0, 'three': True}
new
()
157 if
Int
dpi
(self_ : Int, other : Int) -> Bool
>
0 {
158
@python.PyDict
kwargs
.
(self : @python.PyDict, key : String, val : @python.PyInteger) -> Unit

Set the elements of the dict by its key (String).

Example

test "PyDict::set" {
  let dict = PyDict::new()
  dict
  ..set("one", PyInteger::from(1))
  ..set("two", PyFloat::from(2.0))
  ..set("three", PyBool::from(true));
 
  inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}")
}

The above code is equivalent to:

d = dict()
d['one'] = 1
d['two'] = 2.0
d['three'] = True
set
("dpi",
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
dpi
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
159 } 160 if
Int
quality
(self_ : Int, other : Int) -> Bool
>
0 {
161
@python.PyDict
kwargs
.
(self : @python.PyDict, key : String, val : @python.PyInteger) -> Unit

Set the elements of the dict by its key (String).

Example

test "PyDict::set" {
  let dict = PyDict::new()
  dict
  ..set("one", PyInteger::from(1))
  ..set("two", PyFloat::from(2.0))
  ..set("three", PyBool::from(true));
 
  inspect!(dict, content="{\'one\': 1, \'two\': 2.0, \'three\': True}")
}

The above code is equivalent to:

d = dict()
d['one'] = 1
d['two'] = 2.0
d['three'] = True
set
("quality",
struct @python.PyInteger {
  // private fields
}
PyInteger
::
(Int64) -> @python.PyInteger

Create a PyInteger from an integer.

Example

test "PyInteger::from" {
  let i = @python.PyInteger::from(42);
  inspect!(i, content="42")
}
from
(
Int
quality
.
(self : Int) -> Int64

Converts a 32-bit signed integer to a 64-bit signed integer. All 32-bit integers can be represented exactly in 64-bit integer format, so the conversion is lossless.

Parameters:

  • self : The 32-bit signed integer to be converted.

Returns a 64-bit signed integer that represents the same numerical value as the input.

Example:

test "Int::to_int64" {
  let n = 42
  inspect(n.to_int64(), content="42")
  let neg = -42
  inspect(neg.to_int64(), content="-42")
}
to_int64
()))
162 } 163 let _ =
@python.PyCallable
func
.
(self : @python.PyCallable, args~ : @python.PyTuple, kwargs~ : @python.PyDict) -> @python.PyObjectEnum?
invoke
(
@python.PyTuple
args
~,
@python.PyDict
kwargs
~)
164 165} 166 167///| 168pub fn
() -> Unit
close
() ->
Unit
Unit
{
169 let
@python.PyCallable
func
=
(fname : String) -> @python.PyCallable!MatplotlibError
pyplot_get_func
?("close").
(self : Result[@python.PyCallable, MatplotlibError]) -> @python.PyCallable
unwrap
()
170 let _ =
@python.PyCallable
func
.
(self : @python.PyCallable, args~ : @python.PyTuple = .., kwargs~ : @python.PyDict = ..) -> @python.PyObjectEnum?
invoke
()
171 172} 173