Giter Club home page Giter Club logo

Comments (5)

deads avatar deads commented on July 3, 2024 1

Hi.

I went with SWIG because it does a lot of the hard work of parsing and validating input for you. If you write your own wrapper, you have to write a lot of boilerplated code yourself. Given a large enough codebase (100K+ lines), custom wrappers become very hard to maintain.

The Python typemap lives here: https://github.com/wiseio/paratext/blob/master/src/python. The interface file (python.i) is pretty rudimentary. It converts std::vector<int/double/string> to NumPy vectors.

%typemap(out) std::vector<int> {
  $result = (PyObject*)::build_array<std::vector<int>>($1);
}

%typemap(out) std::vector<double> {
  $result = (PyObject*)::build_array<std::vector<double>>($1);
}

%typemap(out) std::vector<size_t> {
  $result = (PyObject*)::build_array<std::vector<size_t>>($1);
}

%typemap(out) const std::vector<std::string> & {
  { auto range = std::make_pair($1->begin(), $1->end());
   $result = (PyObject*)::build_array_from_range(range);
  }
}

%typemap(out) std::vector<std::string> {
  $result = (PyObject*)::build_array<std::vector<std::string>>($1);
}

%typemap(out) ParaText::CSV::ColBasedPopulator {
  $result = (PyObject*)::build_populator<ParaText::CSV::ColBasedPopulator>($1);
}

You need to write template functions to convert from C++ containers to Ruby arrays.

Ruby::Value build_array<ContainerType>(const ContainerType &container)

You also need to write a function to convert CSV columns to Ruby arrays:

Ruby::Value build_populator<ParaText::CSV::ColBasedPopulator>(ParaText::CSV::ColBasedPopulator &populator)

This is needed to call the loader.get_column(column_index) function.

Best,
Damian

from paratext.

deads avatar deads commented on July 3, 2024

I am not very familiar with the internals of Ruby, but I am happy to provide guidance for the effort.

from paratext.

v0dro avatar v0dro commented on July 3, 2024

Works :)

Should we use SWIG or is it ok to use the Ruby C API directly? Any specific reasons why you used SWIG for the python bindings?

from paratext.

wesm avatar wesm commented on July 3, 2024

SWIG will likely make your life easier if you have straightforward type mappings. I may write a Cython binding for paratext (mainly to simplify talking to other Python modules that use Cython -- you can define an internal C API via pxd files) but either way it makes for less work than a hand-coded C extension for Python at least.

from paratext.

v0dro avatar v0dro commented on July 3, 2024

Alright. I'll explore all the options and post them here soon. Thanks :)

from paratext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.