Last active
December 21, 2020 12:49
-
-
Save nyckmaia/2335ca1f5bf0d34b3056201654651179 to your computer and use it in GitHub Desktop.
Python return 'None'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -Wall -I ./include/ -L ./build/ app.cpp -o app -lmaiaslib | |
#include <iostream> | |
#include "classes.h" | |
int main(void) | |
{ | |
Score score(10); | |
Note note("A4"); | |
score.getPart(0).getStave(0).getMeasure(0).addNote(note); | |
std::string pitch = score.getPart(0).getStave(0).getMeasure(0).getNote(0).getPitch(); | |
std::cout << "Pitch = " << pitch << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
class Note | |
{ | |
private: | |
std::string _pitch; | |
public: | |
Note(const std::string& pitch) { | |
_pitch = pitch; | |
} | |
std::string getPitch() const { | |
return _pitch; | |
} | |
}; | |
class Measure | |
{ | |
private: | |
std::vector<Note> _note; | |
public: | |
Measure() { } | |
void addNote(const Note& note) { | |
_note.push_back(note); | |
} | |
Note& getNote(const size_t noteId) { | |
return _note[noteId]; | |
} | |
}; | |
class Stave | |
{ | |
private: | |
std::vector<Measure> _measure; | |
public: | |
Stave(const size_t numMeasures) { | |
_measure.resize(numMeasures); | |
} | |
Measure& getMeasure(const size_t measureId) { | |
return _measure[measureId]; | |
} | |
}; | |
class Part | |
{ | |
private: | |
std::vector<Stave> _stave; | |
public: | |
Part(const size_t numMeasures) { | |
_stave.emplace_back(numMeasures); | |
} | |
Stave& getStave(const size_t staveId) { | |
return _stave[staveId]; | |
} | |
}; | |
class Score | |
{ | |
private: | |
std::vector<Part> _part; | |
public: | |
Score(const size_t numMeasures) { | |
_part.emplace_back(numMeasures); | |
} | |
Part& getPart(const size_t partId) { | |
return _part[partId]; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.0) | |
project("Maia Library") | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") | |
option(STATIC_LIB "Generate a static libray" OFF) | |
option(PYBIND_LIB "Generate a pybind libray" OFF) | |
if(NOT STATIC_LIB AND NOT PYBIND_LIB) | |
message("--------------------") | |
message("=====> You didn't choose any library type, like: ") | |
message("=> STATIC_LIB") | |
message("=> PYBIND_LIB") | |
message("") | |
message("USAGE: cmake .. -DSTATIC_LIB=ON") | |
message("") | |
message("Setting the default configuration: STATIC_LIB") | |
message("--------------------") | |
set(STATIC_LIB ON) | |
endif() | |
# Add all maialib source files | |
file(GLOB maialib_src | |
"./*.cpp" | |
) | |
# Generate static library | |
if(STATIC_LIB) | |
message("=====> GENERATING STATIC LIBRARY <=====") | |
add_library(maiaslib STATIC ${maialib_src}) | |
target_include_directories(maiaslib PUBLIC include) | |
endif() | |
# Generate python module library | |
if(PYBIND_LIB) | |
message("=====> GENERATING PYBIND LIBRARY <=====") | |
add_definitions(-DPYBIND) | |
find_package(PythonLibs) | |
include_directories(${PYTHON_INCLUDE_DIRS}) | |
add_subdirectory(pybind11) | |
pybind11_add_module(maialib ${maialib_src}) | |
target_include_directories(maialib PUBLIC include) | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef PYBIND | |
#include <pybind11/pybind11.h> | |
#include <pybind11/stl.h> | |
#include <pybind11/iostream.h> | |
namespace py = pybind11; | |
void ScoreClass(py::module &); | |
void PartClass(py::module &); | |
void StaveClass(py::module &); | |
void MeasureClass(py::module &); | |
void NoteClass(py::module &); | |
PYBIND11_MODULE(maialib, m) { | |
ScoreClass(m); | |
PartClass(m); | |
StaveClass(m); | |
MeasureClass(m); | |
NoteClass(m); | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maialib as maia | |
myScore = maia.Score(20) | |
myNote = maia.Note("A") | |
print(myNote) # ===> Print "A" | |
myScore.getPart(0).getStave(0).getMeasure(0).addNote(myNote) | |
x = myScore.getPart(0).getStave(0).getMeasure(0).getNote(0) | |
print(x) # ===> Print "None" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "classes.h" | |
#ifdef PYBIND | |
#include <pybind11/pybind11.h> | |
#include <pybind11/stl.h> | |
#include <pybind11/iostream.h> | |
namespace py = pybind11; | |
void ScoreClass(py::module &m) { | |
py::class_<Score> cls(m, "Score"); | |
cls.def(py::init<const size_t>()); | |
cls.def("getPart", &Score::getPart, py::return_value_policy::reference); | |
} | |
void PartClass(py::module &m) { | |
py::class_<Part> cls(m, "Part"); | |
cls.def(py::init<size_t>()); | |
cls.def("getStave", &Part::getStave, py::return_value_policy::reference); | |
} | |
void StaveClass(py::module &m) { | |
py::class_<Stave> cls(m, "Stave"); | |
cls.def(py::init<size_t>()); | |
cls.def("getMeasure", &Stave::getMeasure, py::return_value_policy::reference); | |
} | |
void MeasureClass(py::module &m) { | |
py::class_<Measure> cls(m, "Measure"); | |
cls.def(py::init<>()); | |
cls.def("addNote", &Measure::addNote); | |
cls.def("getNote", &Measure::getNote, py::return_value_policy::reference); | |
} | |
void NoteClass(py::module &m) { | |
py::class_<Note> cls(m, "Note"); | |
cls.def(py::init<std::string>()); | |
cls.def("getPitch", &Note::getPitch); | |
// Default Python 'print' function: | |
cls.def("__repr__", [](Note& note) { return note.getPitch(); }); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With these 5 files, you can see that the C++ side (
app.cpp
) add aNote
and read back thepitch
value =A
(correct!)But, if I try to do the same process in Python environment (Jupyter-Lab) I got a return =
None
(wrong!)What is wrong?