-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathPyCall.c
165 lines (136 loc) · 3.31 KB
/
PyCall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "main.h"
#include "PyCall.h"
#include "stdio.h"
#include <sys/stat.h>
#include <pthread.h>
#include <python3.5m/ImDib.h>
#include <python3.5m/ImPlatform.h>
#include <python3.5m/Imaging.h>
#define CSV_MAX_LINE_SIZE 4096
#define PYSHOW 1
int FileSize(char *filename)
{
struct stat statbuf;
int ret;
ret = stat(filename, &statbuf);
if(ret != 0)
return -1;
return statbuf.st_size;
}
/*init python*/
int PyInit()
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
//pyThreadRun = 1; //enable pyThread
return 0;
}
/*let pythin read .csv fiel*/
int PyReadcsv(char *m)
{
PyObject *pModule = NULL, *pArg = NULL, *result = NULL, *pFunc =NULL;
pModule = PyImport_ImportModule(m);//import *.py
pFunc = PyObject_GetAttrString(pModule, "testprint");
pArg = Py_BuildValue("(s)","just for show!");
PyObject_CallObject(pFunc,pArg);
// pFunc = PyObject_GetAttrString(pModule, "xprint");//get .py function
// pArg = Py_BuildValue("(filename)","testfile.csv");//build arg
// result = PyObject_CallFunction(pFunc,NULL);//execute py function
int ret;
PyArg_Parse(result, "i", &ret);
printf(">>>%d\n",ret);
return 0;
}
/*
the function to call py function
module: *.py name
fun: the function of .py
arg:the arguement of fun Attention:must be str/char*
*/
int PyCallfun(char* module, char* fun, char* arg)
{
PyObject *pModule = NULL, *pArg = NULL, *result = NULL, *pFunc =NULL;
int ret;
pModule = PyImport_ImportModule(module);//import *.py
pFunc = PyObject_GetAttrString(pModule, fun);
pArg = Py_BuildValue("(s)",arg);
result = PyObject_CallObject(pFunc,pArg);
PyArg_Parse(result, "i", &ret);
return ret;
}
double readCsv(char* csvName, char* indexName)
{
int counter = 0;
char line[CSV_MAX_LINE_SIZE];
FILE * f;
if(NULL == (f = fopen(csvName,"r+")))
{
printf("csv open error!\n");
return -1;
}
fgets(line, CSV_MAX_LINE_SIZE, f);
char *p = strtok(line, ",");
if(p)
{
//printf(">>>%s\n",p);
counter++;
if(!strcmp(p,indexName))
goto process;
}
else
{
printf("open csv err\n");
return -1;
}
while(1)
{
p = strtok(NULL,",");
if(p)
{
counter++;
//printf(">>>%d",counter);
//printf("%s\n",p);
if(!strcmp(p,indexName))
break;
}
else
{
break;
}
}
process:
memset(line,0,CSV_MAX_LINE_SIZE);
fgets(line, CSV_MAX_LINE_SIZE, f);
p = strtok(line, ",");
if(p)
counter--;
int i ;
for(i = 0;i<counter;i++)
{
p = strtok(NULL,",");
//printf(">>%s\n",p);
}
double ret = 0;
ret = atof(p);
fclose(f);
return ret;
}
int readTrain()
{
printf(">>>P:%lf\n",readCsv("train.csv","P"));
printf(">>>I:%lf\n",readCsv("train.csv","I"));
printf(">>>D:%lf\n",readCsv("train.csv","D"));
return 0;
}
void * PyProcessThread(void* arg)
{
pyCallTask_t *pytask = (pyCallTask_t*)arg;
int ret = PyCallfun(pytask->module, pytask->fun, pytask->fileName);
#ifdef PYSHOW
printf("ret is %d\n",ret);
printf("PY done!\n");
#endif // PYSHOW
readTrain();
return NULL;
}