Knife is a simple, elegant, very convenient C++11 Syntactic-Sugar Lib, for mixed-language programming with Python, SQLite and Bash. It was developed with a focus on enabling simple and natural way to mixed-language programming. enjoy the advantages of those languages directly.
Use Knife if you need a mixed-language programming library that:
- Can write those languages directly without trivial work like initialization and finalization
- Supports variable number of arguments and formatted string mechanism to avoid
sprintf(...)
works - Manipulate variables nicely, as long as variable names are same in C++ and those languages
- Cross platform support for multithreading acceleration with a handy speed-up interface
- Extendible to write your own language environment like Lua, R, Matlab, etc.
Documentation are still writing(sorry for my busy life).
Knife is compatible with: Python / Anaconda, Bash / Bourne shell.
The basic interfaces $bash, $py, and $sql are singletons and functors, which restrict the instantiation of a class to one object and overload the function operator()
. They can act like a function, for example void $bash(const char* cmd, ...)
, but not a function.
They take a command string (or format_placeholder) as input and execute it, we call them Environments, which is extendible to other languages(we will talk about it later). There is a demo shows the basic usage of knife.
Relax~ That is just the first impression. We'll explain this demo in detail, which is very simple:
#include <knife.hpp>
int main() {
printf("\nDemo of $bash\n");
$bash("mkdir hello"); // execute commands in the bash environment
$bash("ls | grep ell");// "ell" in "hello"
$bash("rm -r hello");
printf("\nDemo of $py\n");
$py("msg, pi = %s, %f", "\'hello\'", 3.1415926f); // Formatted String
$py("print (msg)");
$py_get(float, pi); // Same Variable Name: pi
std::cout << pi << std::endl;
printf("\nDemo of $sql\n");
$sql("CREATE TABLE Foo(ID INTEGER, Msg TEXT);");
int ID = 1;
const char *Msg = "hello";
$sql.insert_easily("Foo", ID, Msg); // Same Variable Name:ID, Msg
$sql("SELECT * FROM Foo;");
std::cout << $sql.query_result()[0][1] << std::endl;
$sql("DROP TABLE Foo;");
return 0;
}
Runnig this demo, we'll get:
Notice that:
- Outputs of C++(like ptintf / std::cout) are white
- Outputs of Environments are yellow
- Prompts have different color corespond to the language
- Commands that the Environments execute are blue
$bash("mkdir hello"); // execute commands in the bash environment
$bash("ls | grep ell");// "ell" in "hello"
$bash("rm -r hello");
- Firstly,
$bash("mkdir hello");
means the $bash singletons functors takes the string"mkdir hello"
as input, then mkdir will be executed in the your Bash Environment, which makes a directory in your./
path. - Then the ls command lists the files in the current working directory, grep searchs the "ell" from the output of ls.
- At last rm removes the 'hello' directory you have just made.
Yeah, the code of bash, python and sql can directly be executed in $bash, $py and $sql without any other works, and it's just a start.
$py("msg, pi = %s, %f", "\'hello\'", 3.1415926f); // Formatted String
$py("print (msg)");
$py_get(float, pi); // Same Variable Name: pi
std::cout << pi << std::endl;
We want to define two variables in the python environment named msg and pi. However the value of those variables are in C++. Maybe you know sprintf can compose a command string, but kinfe can just take inputs like printf
.
- We use
%s
as the placeholder of msg, and%f
for pi, then put the value"\'hello\'"
and3.1415926f
right after the command template. - You can manipulate them in python such as
print (msg)
by putting it into$py(...)
, the output of environments will be yellow, to distinguish the output of C++ which is default white. - The marco
$py_get(<type>, <name>)
can easily get variable value in python,<type>
is the type in C++(e.g.float
), and<name>
is the variable name both in C++ and Python(e.g.pi
). - Finally, the
std::cout
is just to emphasize that variablefloat pi;
is defined in C++ with the value ofpi
defined in Python.
$sql("CREATE TABLE Foo(ID INTEGER, Msg TEXT);");
int ID = 1;
const char *Msg = "hello";
$sql.insert_easily("Foo", ID, Msg); // Same Variable Name:ID, Msg
$sql("SELECT * FROM Foo;");
std::cout << $sql.query_result()[0][1] << std::endl;
$sql("DROP TABLE Foo;");
TODO
TODO
We should notice that, the %
in C++ formatted string means placeholder, such as %d
is the placeholder of int
, however the modulo operation in most languages including python, bahs and sql is also operator %
, as the commands of those languages are actually strings in C++, so we use mod
to replace %
in those language.
For example, if you want to execute c = a%b
in python, you need to write $py("c = a mod b"")
instead of $py("c= a%b")
because in C++ strings %
means placeholder.
TODO gcc 4.9 clang x.x C++ 11
TODO 527407514@qq.com
TODO Why bash sqlite and python? lua matlab, may be supported later
this style of readme file imitates the readme of Keras, which is one of my favorite project for it's easy, friendly, handy usage ^_^