Skip to content

Commit 760bf8f

Browse files
committed
add python
1 parent 296d508 commit 760bf8f

54 files changed

Lines changed: 1296 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

binascii.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
import binascii
4+
5+
def order_list():
6+
a = 'aa0902630000bb'
7+
a_list = []
8+
for i in a.split():
9+
a_list.append(binascii.a2b_hex(i))
10+
return a_list;
11+
12+
13+
print order_list()

c2python_swig/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
all:
3+
swig -c++ -python c2python_swig.i
4+
g++ -c -fpic c2python_swig.cpp c2python_swig_wrap.cxx -I /usr/include/python2.7/
5+
g++ -shared c2python_swig.o c2python_swig_wrap.o -o _c2python_swig.so
6+
7+
clean:
8+
rm -f *.o *.cxx *.so *.py *.pyc
9+

c2python_swig/c2python_swig.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/*
3+
* =========================================================================
4+
*
5+
* FileName: c2python_swig.cpp
6+
*
7+
* Description:
8+
*
9+
* Version: 1.0
10+
* Created: 2018-02-26 13:31:15
11+
* Last Modified: 2018-02-27 10:14:02
12+
* Revision: none
13+
* Compiler: gcc
14+
*
15+
* Author: zt ()
16+
* Organization:
17+
*
18+
* =========================================================================
19+
*/
20+
21+
22+
#include "c2python_swig.h"
23+
24+
c2python_swig::c2python_swig()
25+
{
26+
}
27+
28+
c2python_swig::~c2python_swig()
29+
{
30+
}
31+
32+
void c2python_swig::test ( std::string a )
33+
{
34+
std::cout << a << std::endl;
35+
}
36+

c2python_swig/c2python_swig.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/*
3+
* =========================================================================
4+
*
5+
* FileName: c2python_swig.h
6+
*
7+
* Description:
8+
*
9+
* Version: 1.0
10+
* Created: 2018-02-27 10:12:18
11+
* Last Modified: 2018-02-27 10:12:37
12+
* Revision: none
13+
* Compiler: gcc
14+
*
15+
* Author: zt ()
16+
* Organization:
17+
*
18+
* =========================================================================
19+
*/
20+
21+
22+
#ifndef C2PYTHON_SWIG_INC
23+
#define C2PYTHON_SWIG_INC
24+
25+
#include <stdio.h>
26+
#include <stdlib.h>
27+
28+
#include <iostream>
29+
#include <string>
30+
31+
32+
class c2python_swig
33+
{
34+
public:
35+
c2python_swig();
36+
virtual ~c2python_swig();
37+
void test ( std::string a );
38+
39+
private:
40+
41+
};
42+
43+
#endif /* C2PYTHON_SWIG_INC */
44+

c2python_swig/c2python_swig.i

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%module c2python_swig
2+
%include "std_string.i"
3+
%{
4+
#include "c2python_swig.h"
5+
%}
6+
%include "c2python_swig.h"

class_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
#-*- coding: UTF-8 -*-
3+
4+
5+
class Test:
6+
def prt(self):
7+
print(self)
8+
print(self.__class__)
9+
10+
11+
12+
t = Test()
13+
14+
t.prt()
15+
16+
print "你好"
17+

cpp_callpython.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* Filename: python.cpp
5+
*
6+
* Description:
7+
*
8+
* Version: 1.0
9+
* Created: 2017年08月11日 14时46分32秒
10+
* Last Modified: 2017年08月11日 14时46分32秒
11+
* Revision: none
12+
* Compiler: gcc
13+
*
14+
* Author: zt (),
15+
* Organization:
16+
*
17+
* =====================================================================================
18+
*/
19+
20+
//g++ python.cpp -o a -g -lpython2.7
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <iostream>
24+
#include <string>
25+
26+
#include <python2.7/Python.h>
27+
28+
void Test_Add ( int a, int b )
29+
{
30+
Py_Initialize();
31+
32+
PyRun_SimpleString ( "import sys" );
33+
/* */
34+
PyRun_SimpleString ( "sys.path.append('./')" );
35+
36+
/* 模块不能用test之类常见的名字,python内置test模块优先级最高 */
37+
PyObject* pModule = PyImport_ImportModule ( "test2" );
38+
39+
if ( !pModule )
40+
std::cout << "pModule error\n";
41+
42+
PyObject* pv = PyObject_GetAttrString ( pModule, "Add" );
43+
44+
if ( !pv )
45+
std::cout << "pv error\n";
46+
47+
PyObject* pArg = Py_BuildValue ( "(i,i)", a, b );
48+
49+
if ( !pArg )
50+
std::cout << "pArg error\n";
51+
52+
PyEval_CallObject ( pv, pArg );
53+
54+
Py_Finalize();
55+
}
56+
57+
void Test_Class ( std::string name )
58+
{
59+
Py_Initialize();
60+
61+
PyRun_SimpleString ( "import sys" );
62+
PyRun_SimpleString ( "sys.path.append('./')" );
63+
64+
PyObject* pModule = PyImport_ImportModule ( "test2" );
65+
66+
if ( !pModule )
67+
std::cout << "pModule !\n";
68+
69+
PyObject* pClass = PyObject_GetAttrString ( pModule, "TestClass" );
70+
71+
if ( !pClass )
72+
std::cout << "pClass!\n";
73+
74+
PyObject* pArg = PyTuple_New ( 1 );
75+
PyTuple_SetItem ( pArg, 0, Py_BuildValue ( "s", name.c_str() ) );
76+
77+
if ( !pArg )
78+
std::cout << "pArg!\n";
79+
80+
PyObject* pClassObj = PyEval_CallObject ( pClass, pArg );
81+
82+
if ( !pClassObj )
83+
std::cout << "pClassObj!\n";
84+
85+
PyObject* pFunc = PyObject_GetAttrString ( pClassObj, "printName" );
86+
87+
if ( !pFunc )
88+
std::cout << "pFunc!\n";
89+
90+
PyEval_CallObject ( pFunc, NULL );
91+
92+
Py_Finalize();
93+
}
94+
95+
96+
int main ( int argc, char* argv[] )
97+
{
98+
//Test_Add ( 10, 22 );
99+
Test_Class ( "test class" );
100+
return 0;
101+
}
102+
103+

guangtou.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
#-*- coding:utf-8 -*-
3+
###############################################################################
4+
# File Name: guangtou.py
5+
# Author:
6+
# Mail:
7+
# Created Time: 2017-10-31 22:28:00
8+
###############################################################################
9+
10+
from sympy import Circle
11+
from scipy import integrate
12+
from math import sqrt
13+
14+
c1=Circle((1,0),1)
15+
c2=Circle((1/2,1),1/2)
16+
jiaodian = c1.intersection(c2)
17+
print(jiaodian)
18+
#求出交点:[Point2D(1/5, 3/5), Point2D(1, 1)]
19+
20+
def func(x):
21+
return sqrt(2*x-x**2)-1+sqrt(x-x**2)
22+
23+
p,err = integrate.quad(func,0.2,1.0)
24+
print(p)
25+

kalman2d.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
# FileName: kalman2d.py
4+
#
5+
# Description:
6+
#
7+
# Version: 1.0
8+
# Created: 2018-01-16 13:46:56
9+
# Last Modified: 2018-01-17 14:12:46
10+
# Revision: none
11+
# Compiler: gcc
12+
#
13+
# Author: zt ()
14+
# Organization:
15+
16+
'''
17+
kalman2d - 2D Kalman filter using OpenCV
18+
19+
Based on http://jayrambhia.wordpress.com/2012/07/26/kalman-filter/
20+
21+
Copyright (C) 2014 Simon D. Levy
22+
23+
This code is free software: you can redistribute it and/or modify
24+
it under the terms of the GNU Lesser General Public License as
25+
published by the Free Software Foundation, either version 3 of the
26+
License, or (at your option) any later version.
27+
This code is distributed in the hope that it will be useful,
28+
29+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+
GNU General Public License for more details.
31+
32+
You should have received a copy of the GNU Lesser General Public License
33+
along with this code. If not, see <http://www.gnu.org/licenses/>.
34+
'''
35+
36+
#from cv2 import cv
37+
import cv2
38+
import numpy
39+
40+
class Kalman2D(object):
41+
'''
42+
A class for 2D Kalman filtering
43+
'''
44+
45+
def __init__(self, processNoiseCovariance=1e-4, measurementNoiseCovariance=1e-1, errorCovariancePost=0.1):
46+
'''
47+
For explanation of the error covariances see
48+
http://en.wikipedia.org/wiki/Kalman_filter
49+
'''
50+
# 状态空间:位置--2d,速度--2d
51+
self.kalman = cv2.KalmanFilter(4, 2, 0)
52+
#self.kalman_state = cv2.CreateMat(4, 1, cv.CV_32FC1)
53+
self.kalman_state = numpy.zeros((4,1),numpy.float32)
54+
self.kalman_process_noise = numpy.zeros((4,1),numpy.float32)
55+
#self.kalman_measurement = cv2.CreateMat(2, 1, cv.CV_32FC1)
56+
self.kalman_measurement = numpy.zeros((2,1),numpy.float32)
57+
58+
for j in range(4):
59+
for k in range(4):
60+
self.kalman.transitionMatrix[j,k] = 0
61+
self.kalman.transitionMatrix[j,j] = 1
62+
# 加入速度 x = x + vx, y = y + vy
63+
# 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1
64+
# 如果把下面两句注释掉,那么位置跟踪kalman滤波器的状态模型就是没有使用速度信息
65+
# self.kalman.transition_matrix[0, 2]=1
66+
# self.kalman.transition_matrix[1, 3]=1
67+
68+
cv2.setIdentity(self.kalman.measurementMatrix)
69+
#初始化带尺度的单位矩阵
70+
cv2.setIdentity(self.kalman.processNoiseCov, cv2.RealScalar(processNoiseCovariance))
71+
cv2.setIdentity(self.kalman.measurementNoiseCov, cv2.RealScalar(measurementNoiseCovariance))
72+
cv2.setIdentity(self.kalman.error_cov_post, cv.RealScalar(errorCovariancePost))
73+
74+
self.predicted = None
75+
self.esitmated = None
76+
77+
def update(self, x, y):
78+
'''
79+
Updates the filter with a new X,Y measurement
80+
'''
81+
self.kalman_measurement[0, 0] = x
82+
self.kalman_measurement[1, 0] = y
83+
84+
self.predicted = cv2.KalmanFilter.predict(self.kalman)
85+
self.corrected = cv2.KalmanFilter.correct(self.kalman, self.kalman_measurement)
86+
87+
def getEstimate(self):
88+
'''
89+
Returns the current X,Y estimate.
90+
'''
91+
return self.corrected[0,0], self.corrected[1,0]
92+
93+
def getPrediction(self):
94+
'''
95+
Returns the current X,Y prediction.
96+
'''
97+
return self.predicted[0,0], self.predicted[1,0]
98+

package_runoob/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python
2+
3+
4+
if __name__ == '__main__':
5+
print "as main"
6+
else:
7+
print "package_oob init"
8+

0 commit comments

Comments
 (0)