3
0
mirror of https://github.com/triqs/dft_tools synced 2024-11-01 03:33:50 +01:00
dft_tools/pytriqs/wrap_test/my_module_desc.py
Olivier Parcollet 5105e04ac7 Add new wrapper generator
- with pytriqs.wrap_test as an example.
2014-05-11 21:47:46 +02:00

105 lines
4.7 KiB
Python

from wrap_generator import *
# The module
module = module_(full_name = "pytriqs.wrap_test.my_module", doc = " Doc of my_module ")
module.add_include("./a.hpp")
module.add_include("<triqs/arrays.hpp>")
# one class
g = class_(
py_type = "Ac",
c_type = "A",
#serializable= "boost",
serializable= "tuple",
is_printable= True,
hdf5 = True,
arithmetic = ("algebra","double")
)
# add a constructor
g.add_constructor(doc = "DOC of constructor", args = [])
# add a method m1, with 3 overloads in C++ : dispatch is done on the type of the arguments
g.add_method(py_name = "m1", c_name = "m1", signature = "double (int u, double y = 3)", doc = "DOC of m1")
g.add_method(py_name = "m1", c_name = "m1", signature = "double (int u)", doc = "DOC of m1...")
g.add_method(py_name = "m1", c_name = "m2", signature = "double (double u)", doc = "DOC of m1...")
# another version of the method, with some pre/post processing written in python
g.add_method(py_name = "m1p", c_name = "m1", signature = "double (int u, double y = 3)", doc = "DOC of mm", python_precall = "pytriqs.wrap_test.aux.ffg", python_postcall = "pytriqs.wrap_test.aux.post1")
# demo of adding a simple piece of C++ code, there is no C++ method corresponding
g.add_method(py_name = "m1_x", calling_pattern = "bool result = (self_c.x >0) && (self_c.x < 10)" , signature = "bool()", doc = "A method which did not exist in C++")
# alternative syntax
#g.add_method(py_name = "m1", python_precall = "aux.ffg", python_postcall = "aux.post1").add_overload(c_name = "m1", rtype = "double", doc = "DOC of mm", args = [("int","u"), ("double","y",3)])
# older syntax, giving rtype and args (better for automatic scripts).
g.add_method(py_name = "m1f", c_name = "m1", rtype = "double", doc = "DOC of mm", args = [("int","u"), ("double","y",3)])
# add the call operator
g.add_call(signature = "int(int u)", doc = "call op")
# add getitem/setitem ...
g.add_getitem(signature = "double(int i)", doc = " doc [] ")
g.add_setitem(signature = "void(int i, double v)", doc = " doc [] set ")
# We can also add the call to a pure python function !
g.add_pure_python_method("pytriqs.wrap_test.aux.pure_py1")
def ffg2(self, *args, **kw) :
""" my doc of the function ffg2 """
print "calling ffg2 [inline], with :"
print args
print kw
#return [2*x for x in args], kw
print dir()
return tuple(2*x for x in args), kw
g.add_pure_python_method(ffg2)
# public members -> as properties
g.add_member(c_name = "x", c_type = "double", doc = "x field of A ....")
g.add_member(c_name = "y", c_type = "double", doc = "y field of A : read_only", read_only=True)
# properties : transform a couple of methods into properties
g.add_property(name = "i", getter = cfunction(c_name="_get_i", doc = "i prop get doc", signature = "int()"),
setter = cfunction(c_name="_set_i", doc = "i prop set doc", signature = "void(int j)"))
g.add_property(name = "ii", getter = cfunction(c_name="_get_i", doc = "i prop get doc", signature = "int()"))
g.add_iterator()
module.add_class(g)
# various module functions....
module.add_function (name = "print_a", signature = "void(A a)", doc = "DOC of print_a")
module.add_function (name = "print_err", signature = "void(A a)", doc = "DOC of print_a")
module.add_function (name = "make_vector", signature = "std::vector<int>(int size)", doc = "DOC of print_a")
module.add_function (name = "make_vector2", signature = "std::vector<std::vector<int>>(int size)", doc = "DOC ....")
module.add_function (name = "vector_x2", signature = "std::vector<int>(std::vector<int> v)", doc = "DOC of print_a")
module.add_function (name = "make_matrix", signature = "matrix_view<double>(int size)", doc = "DOC ....")
module.add_function (name = "iter_on_range", signature = "void (range r)" , doc = "DOC ....")
module.add_function (name = "make_fnt_ii", rtype = "std::function<int(int,int)>", doc = "....", args = [])
module.add_function (name = "make_fnt_iid", rtype = "std::function<int(int,int,double)>", doc = "....", args = [])
module.add_function (name = "make_fnt_void", rtype = "std::function<void(int,int)>", doc = "....", args = [])
module.add_function (name = "use_fnt_ii", signature = "void(std::function<int(int,int)> f)", doc = "....")
module.add_function (name = "use_fnt_iid", signature = "void(std::function<int(int,int,double)> f)", doc = "....")
def f1(x,y):
print " I am in f1 ", x,y
print y + 1/0.2
print tuple([x])
assert x>0, "an horrible error"
# The code of f1 will be copied verbatim in the module, and compiled at the
# initialisation of the module
module.add_python_function(f1)
if __name__ == '__main__' :
module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2])