-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more examples of implemented features (#13)
- Loading branch information
1 parent
c57300d
commit 096f3f9
Showing
8 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# Python Functions | ||
# Python Functions | ||
|
||
```zig title="src/functions.zig" | ||
--8<-- "example/functions.zig" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# Python Classes | ||
# Python Classes | ||
|
||
```zig title="src/classes.zig" | ||
--8<-- "example/classes.zig" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const std = @import("std"); | ||
const py = @import("pydust"); | ||
|
||
pub const Animal = py.class("Animal", struct { | ||
pub const __doc__ = "Animal docstring"; | ||
}); | ||
|
||
pub const Dog = py.subclass("Dog", &.{Animal}, struct { | ||
pub const __doc__ = "Adorable animal docstring"; | ||
const Self = @This(); | ||
|
||
name: [:0]const u8, | ||
|
||
pub fn __init__(self: *Self, args: *const extern struct { name: py.PyString }) !void { | ||
args.name.incref(); | ||
self.name = try args.name.asSlice(); | ||
} | ||
|
||
pub fn get_name(self: *const Self) !py.PyString { | ||
return py.PyString.fromSlice(self.name); | ||
} | ||
|
||
pub fn make_noise() !py.PyString { | ||
return py.PyString.fromSlice("Bark!"); | ||
} | ||
}); | ||
|
||
pub const Owner = py.class("Owner", struct { | ||
pub const __doc__ = "Takes care of an animal"; | ||
|
||
pub fn adopt_puppy(args: *const extern struct { name: py.PyString }) !py.PyObject { | ||
return try py.init(Dog, .{ .name = args.name }); | ||
} | ||
}); | ||
|
||
comptime { | ||
py.module(@This()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const py = @import("pydust"); | ||
|
||
pub fn double(args: *const extern struct { x: py.PyLong }) !i64 { | ||
return try args.x.as(i64) * 2; | ||
} | ||
|
||
comptime { | ||
py.module(@This()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
|
||
from example import classes | ||
|
||
|
||
def test_hierarchy(): | ||
assert issubclass(classes.Dog, classes.Animal) | ||
assert isinstance(classes.Dog("Pupper's name"), classes.Animal) | ||
|
||
|
||
def test_make_noise(): | ||
with pytest.raises(AttributeError): | ||
classes.Animal().make_noise() | ||
assert classes.Dog("Pupper's name").make_noise() == "Bark!" | ||
|
||
|
||
def test_adopt(): | ||
owner = classes.Owner() | ||
adopted = owner.adopt_puppy("Cute pupper's name") | ||
assert isinstance(adopted, classes.Dog) | ||
assert adopted.get_name() == "Cute pupper's name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import inspect | ||
|
||
import pytest | ||
|
||
from example import functions | ||
|
||
|
||
def test_double(): | ||
assert functions.double(10) == 20 | ||
with pytest.raises(TypeError, match="'float' object cannot be interpreted as an integer"): | ||
functions.double(0.1) | ||
|
||
|
||
def test_text_signature(): | ||
assert inspect.signature(functions.double) == inspect.Signature( | ||
[inspect.Parameter("x", kind=inspect._ParameterKind.POSITIONAL_ONLY)] | ||
) |