def generate(fp): mod = pybindgen.Module('classes') mod.add_include('"classes.hpp"') namespace = mod.add_cpp_namespace('classes') class1 = namespace.add_class('Class1') class1.add_enum('DIRECTION', ['UP', 'DOWN', 'LEFT', 'RIGHT']) # class1.add_function('AcceptEnum', None, [param('MyEnum_e', 'value')]) class1.add_instance_attribute('m_flag', 'int') class1.add_constructor([param('int', 'flag')]) class1.add_constructor([]) class1.add_method('Method1', None, []) sclass = namespace.add_class("Singleton", is_singleton=True) sclass.add_method("getReference", retval("classes::Singleton&", caller_owns_return=True), [], is_static=True) # mod.add_class('Class1', # memory_policy=cppclass.ReferenceCountingMethodsPolicy( # incref_method='Ref', # decref_method='Unref', # peekref_method='PeekRef') # ) # mod.add_function('DoSomething', retval('Class1 *', caller_owns_return=False), []) mod.generate(fp)
def Generate(cxxRootNode): from ..cppmodel.CxxNode import Generator from ..cppmodel.CxxFunction import CxxFunction from ..cppmodel.CxxClass import CxxClass from ..cppmodel.CxxVariable import CxxVariable filePath = str(cxxRootNode) commonPrefix = os.path.commonprefix([filePath, __file__]) relativePath = os.path.relpath(filePath, commonPrefix) fileBasePath, fileExt = os.path.splitext(relativePath) fileModuleName = fileBasePath.replace(os.path.sep, "_") pybindMod = pybindgen.Module(fileModuleName) # pybindMod.add_include('"shared/cube.h"') amountOfSymbolsAdded = 0; for node in cxxRootNode.forEachChild(): if type(node) is CxxFunction: if node.generateFor & Generator.Python: amountOfSymbolsAdded += GenerateFunction(pybindMod, node) if type(node) is CxxClass: if node.generateFor & Generator.Python: amountOfSymbolsAdded += GenerateClass(node) if type(node) is CxxVariable: if node.generateFor & Generator.Python: amountOfSymbolsAdded += GenerateVariable(node) if amountOfSymbolsAdded > 0: output = io.StringIO() pybindMod.generate(output) return output.getvalue() else: return ""
def generate(file_): mod = pybindgen.Module('_rabinkarprh') mod.add_include('"rabinkarp.h"') mod.add_container('std::list<unsigned int>', 'unsigned int', 'list') mod.add_container('std::list<double>', 'double', 'list') cls = mod.add_class('RabinKarpHash') cls.add_constructor([ pybindgen.param('int', 'my_window_size'), pybindgen.param('int', 'seed') ]) cls.add_method('set_threshold', None, [pybindgen.param('double', 'my_threshold')]) cls.add_method('next_chunk_boundaries', pybindgen.retval('std::list<unsigned int>'), [ pybindgen.param('const std::string*', 'content'), pybindgen.param('const unsigned int', 'prepend_bytes') ]) cls = mod.add_class('RabinKarpMultiThresholdHash') cls.add_constructor([ pybindgen.param('int', 'my_window_size'), pybindgen.param('int', 'seed'), pybindgen.param('std::list<double>', 'my_thresholds') ]) cls.add_method('next_chunk_boundaries_with_thresholds', pybindgen.retval('std::list<unsigned int>'), [ pybindgen.param('const std::string*', 'content'), pybindgen.param('unsigned int', 'prepend_bytes') ]) mod.generate(file_)
def generate(fp): mod = pybindgen.Module('tutorial') mod.add_include('"tutorial.hpp"') namespace = mod.add_cpp_namespace('tutorial') namespace.add_enum('Color', ['RED', 'BLUE', 'WHITE']) # mod.add_function('AcceptEnum', None, [param('MyEnum_e', 'value')]) # default arguments namespace.add_function('UseDefaultArguments', 'double', [ param('double', 'arg1', default_value='3.1415'), param('bool', 'arg2', default_value='true') ]) # overloaded namespace.add_function('OverloadedFunction', None, [param('const std::string &', 'name')]) namespace.add_function('OverloadedFunction', None, [param('int', 'index')]) # overloaded with default arguments namespace.add_function('UseDefaultOverload', 'int', [ param('int', 'num'), param('int', 'offset', default_value='0'), param('int', 'stride', default_value='1') ]) namespace.add_function('UseDefaultOverload', 'int', [ param('double', 'type'), param('int', 'num'), param('int', 'offset', default_value='0'), param('int', 'stride', default_value='1') ]) mod.generate(fp)
def generate(file_): print(os.getcwd()) mod = pybindgen.Module('pythonCPPinterop') mod.add_include('"CppPythonCrossModule.h"') mod.add_function('PythonCPPRunAlphabeta', pybindgen.retval('double'), [ pybindgen.param('std::string', 'boardstring'), pybindgen.param('int', 'depth'), pybindgen.param('std::string', 'player') ]) mod.generate(file_)
def generate(fp): mod = pybindgen.Module('strings') mod.add_include('"strings.hpp"') mod.add_function('passChar', None, [pybindgen.param('char', 'status')]) mod.add_function('returnChar', retval('char'), []) mod.add_function('returnStrings', None, [param('std::string &', 'arg1', direction=Parameter.DIRECTION_OUT), param('std::string &', 'arg2', direction=Parameter.DIRECTION_OUT)]) mod.add_function('getConstStringAlloc', retval('const std::string'), []) mod.add_function('getConstStringRefAlloc', retval('const std::string &'), []) # mod.add_function('getConstStringPtrAlloc', # retval('const std::string *'), []) #pybindgen.typehandlers.base.TypeLookupError: ['std::string *'] mod.generate(fp)
# from pybindgen/examples/buffer/modulegen.py class BufferReturn(pybindgen.ReturnValue): CTYPES = [] def __init__(self, ctype, length_expression): super().__init__(ctype, is_const=False) self.length_expression = length_expression def convert_c_to_python(self, wrapper): pybuf = wrapper.after_call.declare_variable("PyObject*", "pybuf") wrapper.after_call.write_code("%s = PyBuffer_FromReadWriteMemory(retval, %s);" % (pybuf, self.length_expression)) wrapper.build_params.add_parameter("N", [pybuf], prepend=True) mod = pybindgen.Module('discretization') mod.add_include('"discretization.hh"') mod.add_container('std::vector<double>', 'double', 'vector') # declare a container only once Vector = mod.add_class('Vector') Vector.add_constructor([pybindgen.param('int', 'size'), pybindgen.param('double', 'value')]) Vector.add_constructor([pybindgen.param('const Vector&', 'othter')]) Vector.add_instance_attribute('dim', 'int', is_const=True) Vector.add_method('scal', None, [pybindgen.param('double', 'val')]) Vector.add_method('axpy', None, [pybindgen.param('double', 'a'), pybindgen.param('const Vector&', 'x')]) Vector.add_method('dot', pybindgen.retval('double'), [pybindgen.param('const Vector&', 'other')], is_const=True) Vector.add_method('data', BufferReturn('double*', 'self->obj->dim * sizeof(double)'), []) DiffusionOperator = mod.add_class('DiffusionOperator')
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. # # # \file # Python bindings for RnnLM. These only correspond # to basic evaluation functions, not training. By default # the evaluations utilizes the -independent convention from # the original rnnlm tool. This is all we are interested in # for G2P evaluations. # Specifically this is the generator code for pybindgen. import pybindgen from pybindgen import param, retval import sys mod = pybindgen.Module('RnnLM') mod.add_container('std::vector<std::string>', 'std::string', 'vector') mod.add_container('std::vector<double>', 'double', 'vector') struct = mod.add_struct('UttResult') struct.add_constructor([]) struct.add_instance_attribute('sent_prob', 'double') struct.add_instance_attribute('word_probs', 'std::vector<double>') struct.add_instance_attribute('words', 'std::vector<std::string>') mod.add_include('"RnnLMPy.h"') klass = mod.add_class('RnnLMPy') klass.add_constructor([param('std::string', 'rnnlm_file')]) klass.add_method('EvaluateSentence', retval('UttResult'), [param('std::vector<std::string>', 'words')]) mod.generate(sys.stdout)
#the following conditions: # #The above copyright notice and this permission notice shall be #included in all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF #MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE #LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION #WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import pybindgen, sys pycastC = pybindgen.Module("pycastC") ## create the submodules of pycast pycastC_errors = pycastC.add_cpp_namespace("errors") pycastC_common = pycastC.add_cpp_namespace("common") pycastC_methods = pycastC.add_cpp_namespace("methods") pycastC_optimization = pycastC.add_cpp_namespace("optimization") ## pycast.errors pycastC_errors.add_include('"pycast/errors/opt/baseerrormeasure.h"') pycastC_errors_baseerrormeasure = pycastC_errors.add_cpp_namespace( "baseerrormeasure") pycastC_errors_baseerrormeasure_BaseErrorMeasure = pycastC_errors_baseerrormeasure.add_cpp_namespace( "BaseErrorMeasure") pycastC_errors_baseerrormeasure_BaseErrorMeasure.add_function(
import pybindgen from pybindgen import param, retval import sys mod = pybindgen.Module('Phonetisaurus') ################################################ #PhonetisaurusOmega decoder wrapper mod.add_include('"include/PhonetisaurusScript.h"') #Build up the basic bits for the PathData return object mod.add_container('std::vector<int>', 'int', 'vector' ) mod.add_container('std::vector<float>', 'float', 'vector' ) #Register the PathDataPy struct struct = mod.add_struct('PathData') struct.add_constructor([]) struct.add_instance_attribute ('PathWeight', 'float') struct.add_instance_attribute ('PathWeights', 'std::vector<float>') struct.add_instance_attribute ('ILabels', 'std::vector<int>') struct.add_instance_attribute ('OLabels', 'std::vector<int>') struct.add_instance_attribute ('Uniques', 'std::vector<int>') #Register the vector<PathData> container mod.add_container('std::vector<PathData>', 'PathData', 'vector' ) g2pklass = mod.add_class('PhonetisaurusScript') std_exception = mod.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()') g2pklass.add_constructor([param('std::string', 'model')],
class BufferReturn(pybindgen.ReturnValue): CTYPES = [] def __init__(self, ctype, length_expression): super().__init__(ctype, is_const=False) self.length_expression = length_expression def convert_c_to_python(self, wrapper): memview = wrapper.after_call.declare_variable("PyObject*", "memview") wrapper.after_call.write_code( "%s = PyMemoryView_FromMemory((char*)retval, %s, PyBUF_WRITE);" % (memview, self.length_expression)) wrapper.build_params.add_parameter("N", [memview], prepend=True) mod = pybindgen.Module('model') mod.add_include('"model.hh"') mod.add_container('std::vector<double>', 'double', 'vector') # declare a container only once Vector = mod.add_class('Vector') Vector.add_constructor( [pybindgen.param('int', 'size'), pybindgen.param('double', 'value')]) Vector.add_constructor([pybindgen.param('const Vector&', 'othter')]) Vector.add_instance_attribute('dim', 'int', is_const=True) Vector.add_method('scal', None, [pybindgen.param('double', 'val')]) Vector.add_method( 'axpy', None,
import pybindgen import sys mod = pybindgen.Module('TestModule') mod.add_include('"test.h"') ## no params no return # mod.add_function('test_func', None, []) ## return value and multiple params # modd.add_function('test_func', pybindgen.retval('int'), [pybindgen.param('int', 'a'), pybindgen.param('int', 'b')]) ## param and return pointer mod.add_function('test_func', pybindgen.retval('char*', caller_owns_return=False), [pybindgen.param('char*', 'txt', transfer_ownership=False)]) mod.generate(sys.stdout)
import pybindgen import sys mod = pybindgen.Module('MyModule') mod.add_include('"my-module.h"') mod.add_function('MyModuleDoAction', None, []) mod.generate(sys.stdout)
import pybindgen import sys mod = pybindgen.Module('Maestro') mod.add_include('"maestro.h"') mod.add_function('maestroGetPosition', pybindgen.retval('int'), [ pybindgen.param('int', 'fd'), pybindgen.param('unsigned char', 'channel') ]) mod.add_function('maestroSetTarget', pybindgen.retval('int'), [ pybindgen.param('int', 'fd'), pybindgen.param('unsigned char', 'channel'), pybindgen.param('unsigned short', 'target') ]) mod.add_function('maestroSetSpeed', pybindgen.retval('int'), [ pybindgen.param('int', 'fd'), pybindgen.param('unsigned char', 'channel'), pybindgen.param('unsigned short', 'target') ]) mod.add_function('maestroSetAccel', pybindgen.retval('int'), [ pybindgen.param('int', 'fd'), pybindgen.param('unsigned char', 'channel'), pybindgen.param('unsigned short', 'target') ]) mod.add_function('maestroConnect', pybindgen.retval('int'), [pybindgen.param('const char *', 'device')]) mod.generate(sys.stdout)