Source

pyd / examples / pyd_unittests / embedded.d

import pyd.pyd, pyd.embedded;

import std.stdio;

static this() {
    on_py_init({
    add_module!(ModuleName!"testing")();
    });
    py_init();
}
// py_def
unittest {
    alias py_def!(
            "def func1(a):\n"
            " return a*2+1",
            "testing", 
            int function(int)) func1;
    assert(func1(1) == 3);    
    assert(func1(2) == 5);    
    assert(func1(3) == 7);    
}

// py_stmts
unittest {
    // futures do not persist across py_stmts calls
    py_stmts(
            "a = 3 / 4;"
            ,
            "testing");
    assert(py_eval!double("a", "testing") == 0);
    py_stmts(
            "from __future__ import division\n"
            "b = 3 / 4;"
            ,
            "testing");
    assert(py_eval!double("b", "testing") == 0.75);
    py_stmts(
            "a = 3 / 4;"
            ,
            "testing");
    assert(py_eval!double("a", "testing") == 0);

    // but they do across contextual py_stmts calls.
    InterpContext c = new InterpContext();
    c.py_stmts(
            "import testing\n"
            "a = 3 / 4;"
            );
    assert(c.py_eval!double("a") == 0);
    c.py_stmts(
            "from __future__ import division\n"
            "b = 3 / 4;"
            );
    assert(c.py_eval!double("b") == 0.75);
    c.py_stmts(
            "a = 3 / 4;"
            );
    assert(c.py_eval!double("a") == 0.75);
}
unittest {
    // py_stmts with modulename executes within that module

    py_stmts(
            "a = \"doctor!\""
            ,
            "testing");
    py_stmts(
            "import testing\n"
            "assert testing.a == \"doctor!\""
            );

    // however, py_stmts contextualized or without modulename does not.

    py_stmts(
            "import testing\n"
            "a = \"nurse!\""
            );
    py_stmts(
            "import testing\n"
            "assert testing.a == \"doctor!\""
            );
    InterpContext c = new InterpContext();
    c.py_stmts(
            "import testing\n"
            "a = \"nurse!\""
            );
    py_stmts(
            "import testing\n"
            "assert testing.a == \"doctor!\""
            );

}

void main() {}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.