Package: codeoven
IntroductionIf you're working on scientific calculations using C programming, you might find it tedious to prepare an interface to call it from Python. However, Codeoven offers a simple solution to this problem. All you have to do is add some comment lines to the original C source code, and Codeoven will do the rest. By decorating your code with@ovencomment lines, the code will be compiled and executed so that it can communicate with your Python script throughstdio.Using Codeoven eliminates the need to prepare a separate interface to link your C code with Python. Instead, the@ovencomment lines act as a bridge between the two programming languages. This approach has the added advantage of allowing the original source code to be run in any IDE, making debugging easier.Here is a quick example.Assume that we have a source codemain.cthat adds up from 1 to n.#include<stdio.h>intmain(void){inti,total=0;intn=1;// @ovenfor(i=1;i<=n;i++){total+=i;}printf("answer:%d\n",total);// @ovenreturn0;}The following Python script will compile and run main.c to get the sum of 1 to 10.fromcodeovenimportOvenoven=Oven("main.c")result=oven.run({"n":10})print(result["answer"])# -> print 55Notice that the only modification required to the source code is the@ovencomments on the lines corresponding to input and output.In terms of the execution speed, the initial compilation and the communication between C and Python throughstdiowould be the major bottleneck.UsageLoading your source codeoven = Oven("main.c")Running your programparameter_dict = {"a": 1, "b": 2.0, "c": [3.0, 4.0, 5.0]}
result = oven.run(parameter_dict)Passing values from Python to CPassing a scalar value// At declarationinta=1;// @oven//At substitutiondoubleb;b=2;// @ovenPassing an arraydoublec[]={0.0};Passing value from C to PythonYou can output the results of your calculation usingprintf:printf("key_name:%lf\n",value);//@ovenThe format string must always be composed of a sequence of a key, a colon, a value, and a newline. As for the value, it can be a number, string tuple list, or dictionary, as long as it can be evaluated by Python's ast.literal_eval.printf("a_and_b:(%d, %lf)\n",a,b);// @oven// This line passesprintf("time_series:(%d, %lf)\n",0,x);// @ovenfor(i=0;i<n;i++)x+=calculation(i,x);printf("time_series:(%d, %lf)\n",i+1,x);// @ovenIf multiple outputs are made for the same key, the result is a list in the order of outputs.Accessing the resultsThe return value ofrunis a dictionary. You can access it as usual.print(result["a"])Code modifiers@ovenInputint n = 1; // @oven
double array[] = {1.0, 2.0, 3.0}; // @ovenOutputprintf("result:%d\n", n*n); // @oven(Plan)@oven.assign  (new plan)-> @oven.as: or @oven:Assign the value using a Python statement.int array[] = {2, 3, 5}; // @oven
int n = 3; // @oven.assign:array.sizedouble map[][] = {{1.0, 2.0}, {2.0, 3.0}, {3.0, 4.0}}; // @oven
int n_x = 2; // @oven.assign:map.shape[0]
int n_y = 3; // @oven.assign:map.shape[1]@oven.compileThe input specified by@oven.compileis embdedded directrly in the source code.int n = 1; // @oven.compile

double array[] = {1.0, 2.0}; // @oven.compile
int n = 2; // @oven.assign:array.sizeReferenceclasscodeoven.Ovencodeoven.Oven(file_name:str)file_name: The file name of a source code to be processedWhen an instance of Oven is created, the source code is loaded and parsed. At this stage, no new code is written out yet; the oven instance generates code whenrunorburnis executed.oven.run(parameters:dict=None) -> dictparameters: Initialization or substitution statements commented with "@oven" in the C source code will be replaced using the value of the dictionary at runtime thoughstdin. The compilation will be done once for the first execution.oven.burn(parameters:dict=None) -> dictparameters: Initialization or substitution statements commented with "@oven" in the C source code will be replaced using the value of the dictionary.Each time the methodburnis called, the new source code is output, compiled, and executed. No information exchange will be done at runtime, and all the parameters and data will be implemented in the source code.Examples#include<stdio.h>intmain(void){doubledata[]=[1,3,5];// @ovenintn=3;// @over:data.sizedoubleresult=0;inti;for(i=0;i<n;i++){result+=data[i];}printf("sum:%lf\n",result);// @ovenreturn0}#include<stdio.h>intmain(void){doubledata=[[1,2,3],[2,3,4]];// @oven.runtimeintimax=3;// @oven.assign:data.shape[0]intkmax=2;// @oven.assign:data.shape[1]inti,k;for(i=0;i<)}