Skip to content

Python Bindings #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ endif()
add_subdirectory(test)
add_subdirectory(benchmark)
add_subdirectory(examples)
add_subdirectory(python)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)
7 changes: 7 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
option(PYTHON_BINDINGS "Enable Python Bindings Compilation" OFF)
if(PYTHON_BINDINGS)

add_subdirectory(bindings)


endif(PYTHON_BINDINGS)
26 changes: 26 additions & 0 deletions python/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.15)
project(CXXGraphPythonBindings LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Fetch pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.12.0
)
FetchContent_MakeAvailable(pybind11)


set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES /usr/local/lib ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
# Include headers
include_directories(${CMAKE_SOURCE_DIR}/include)

# # Add library target (header-only here)
# add_library(CXXGraphLib INTERFACE)
# target_include_directories(CXXGraphLib INTERFACE ${CMAKE_SOURCE_DIR}/include)

# Add pybind11 module
pybind11_add_module(cxxgraph bindings.cpp)
53 changes: 53 additions & 0 deletions python/bindings/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <pybind11/functional.h> // for std::function support
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "CXXGraph/CXXGraph.hpp"

namespace py = pybind11;

PYBIND11_MODULE(cxxgraph, m) {
py::class_<CXXGraph::Node<int>>(m, "Node")
.def(py::init<const std::string &, const int &>(), py::arg("user_id"),
py::arg("data"))
.def(py::init<const std::string &, int &&>(), py::arg("user_id"),
py::arg("data"))

// Read-only property accessors
.def("get_id", &CXXGraph::Node<int>::getId)
.def("get_user_id", &CXXGraph::Node<int>::getUserId)
.def("get_data", static_cast<const int &(CXXGraph::Node<int>::*)() const>(
&CXXGraph::Node<int>::getData))
.def("get_data_mut", static_cast<int &(CXXGraph::Node<int>::*)()>(
&CXXGraph::Node<int>::getData))

// Mutator
.def("set_data", &CXXGraph::Node<int>::setData)

// Operators
.def(py::self == py::self)
.def(py::self < py::self)

// String representation
.def("__repr__", [](const CXXGraph::Node<int> &n) {
std::ostringstream oss;
oss << n;
return oss.str();
});

py::class_<CXXGraph::Edge<int>>(m, "Edge").def(
py::init<const CXXGraph::id_t, const CXXGraph::Node<int> &,
const CXXGraph::Node<int> &>()); // default ctor,

py::class_<CXXGraph::Graph<int>>(m, "Graph")
.def(py::init<>()) // default ctor, no bool argument
.def("add_node",
static_cast<void (CXXGraph::Graph<int>::*)(
const CXXGraph::Node<int> *)>(&CXXGraph::Graph<int>::addNode))
.def("add_edge",
static_cast<void (CXXGraph::Graph<int>::*)(
const CXXGraph::Edge<int> *)>(&CXXGraph::Graph<int>::addEdge))

.def("dfs", &CXXGraph::Graph<int>::depth_first_search);
}
19 changes: 19 additions & 0 deletions python/tests/simpleTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
sys.path.append("../../build/python/bindings")

import cxxgraph

g = cxxgraph.Graph()
node1 = cxxgraph.Node("Node_1", 1)
node2 = cxxgraph.Node("Node_2", 2)
g.add_node(node1)
g.add_node(node1)
edge1 = cxxgraph.Edge(1,node1,node2)
g.add_edge(edge1)


result = g.dfs(node1)

for node in result:
print(node.get_id(), node.get_user_id(), node.get_data())

Loading