Tag: python

从网页抓取文本

我想编写一个程序,可以找到公共汽车站的时间并相应地更新我的个人网页。 如果我手动这样做,我会的 访问www.calgarytransit.com 输入一个停止号码。 即)9510 点击“下一class巴士”按钮 结果可能如下所示: 10:16p 154号公路 10:46p 154号公路 11:32p 154号公路 一旦我抓住了时间和路线,我就会相应地更新我的网页。 我不知道从哪里开始。 我知道他们对网络编程很沮丧,但可以编写一些C和Python。 我可以研究哪些主题/库?

如何从C访问Python全局变量?

我在Python脚本中有一些全局变量。 该脚本中的某些函数调用C – 是否可以在C中设置其中一个变量,如果是,如何? 我很欣赏这首先不是一个非常好的设计,但是我需要对现有代码进行一些小改动,我不想对现有脚本进行重大的重构。

Python逐行从子进程捕获stdout

我已经阅读了很多与此相关的问题并且学到了很多,但我仍然无法解决我的问题。 我正在构建一个运行c ++可执行文件的wxPython应用程序,并实时显示该可执行文件中的stdout。 我试图让这项工作遇到几个奇怪的结果。 这是我目前的设置/问题: //test.cc (compiled as test.out with gcc 4.5.2) #include int main() { FILE* fh = fopen(“output.txt”, “w”); for (int i = 0; i < 10000; i++) { printf("Outputting: %d\n", i); fprintf(fh, "Outputting: %d\n", i); } fclose(fh); return 0; } #wxPythonScript.py (running on 2.7 interpreter) def run(self): self.externalBinary = subprocess.Popen(['./test.out'], shell=False, stdout=subprocess.PIPE) […]

Cython和fortran – 如何在没有f2py的情况下一起编译

最终更新 这个问题是关于如何编写一个setup.py ,它将编译一个直接访问FORTRAN代码的cython模块,就像C一样。 这是一个相当漫长而艰巨的解决方案之旅,但下面包含了完整的混乱。 原始问题 我有一个扩展,它是一个Cython文件,它设置一些堆内存并将其传递给fortran代码,以及一个fortran文件,这是一个古老的模块,我想避免重新实现,如果可以的话。 .pyx文件可以很好地编译为C,但是cython编译器会对.f90文件进行.f90 ,并出现以下错误: $ python setup.py build_ext –inplace running build_ext cythoning delaunay/__init__.pyx to delaunay/__init__.c building ‘delaunay’ extension error: unknown file type ‘.f90’ (from ‘delaunay/stripack.f90’) 这是我的安装文件(上半部分): from distutils.core import setup, Extension from Cython.Distutils import build_ext ext_modules = [ Extension(“delaunay”, sources=[“delaunay/__init__.pyx”, “delaunay/stripack.f90”]) ] setup( cmdclass = {‘build_ext’: build_ext}, ext_modules = ext_modules, … […]

为什么我的python / numpy示例比纯C实现更快?

我在python和C.中有几乎相同的代码.Python示例: import numpy nbr_values = 8192 n_iter = 100000 a = numpy.ones(nbr_values).astype(numpy.float32) for i in range(n_iter): a = numpy.sin(a) C示例: #include #include int main(void) { int i, j; int nbr_values = 8192; int n_iter = 100000; double x; for (j = 0; j < nbr_values; j++){ x = 1; for (i=0; i<n_iter; i++) x = […]

numpy数组C api

我有一个C ++函数返回一个std :: vector,我想在python中使用它,所以我使用的是C numpy api: static PyObject * py_integrate(PyObject *self, PyObject *args){ … std::vector integral; cpp_function(integral); // This changes integral npy_intp size = {integral.size()}; PyObject *out = PyArray_SimpleNewFromData(1, &size, NPY_DOUBLE, &(integral[0])); return out; } 这是我从python中调用它的方式: import matplotlib.pyplot as plt a = py_integrate(parameters) print a fig = plt.figure() ax = fig.add_subplot(111) ax.plot(a) print a 会发生什么:第一次打印没问题,值是正确的。 […]

Python版本的freopen()

python中有什么东西可以在C或C ++中复制freopen()的function吗? 确切地说,我想复制以下function: freopen(“input.txt”,”r”,stdin); 和 freopen(“output.txt”,”w”,stdout); 然后对文件I / O使用相同(标准)的控制台I / O函数。 有任何想法吗?

包含数组的ctypes结构

我正在尝试使用ctypes 。 我对操纵包含数组的C结构感兴趣。 请考虑以下my_library.c #include typedef struct { double first_array[10]; double second_array[10]; } ArrayStruct; void print_array_struct(ArrayStruct array_struct){ for (int i = 0; i < 10; i++){ printf("%f\n",array_struct.first_array[i]); } } 并假设我已经在共享库中编译它my_so_object.so从Python我可以做这样的事情 import ctypes from ctypes import * myLib = CDLL(“c/bin/my_so_object.so”) class ArrayStruct(ctypes.Structure): _fields_ = [(‘first_array’, ctypes.c_int * 10), (‘second_array’, ctypes.c_int * 10)] def __repr__(self): return ‘ciaone’ […]

使用ctypes模块访问用C编写的DLL时出错

我有一个DLL,只有一个函数,它有五个双精度数和一个整数: __declspec(dllexport) struct res ITERATE(double z_r,double z_i,double c_r, double c_i, int iterations, double limit) 它返回一个自定义的struct caled res,它由一个三双数组组成: struct res { double arr[3]; }; 要返回值,我这样做: struct res result; /*earlier in the code */ result.arr[0] = z_real; /*Just three random doubles*/ result.arr[1] = z_imag; result.arr[2] = value; return result; 我用MinGW编译它,我试图在python中使用它来做这样的事情: form ctypes import * z = [0.0,0.0] […]

将Cython中的C结构包装/转换为Python类

我刚刚开始熟悉Cython,试图将一些C语言结构包装到Python方法和类中。 我真正不了解的是如何从(初始化的)C结构转换到相应的Python类应该是有效的。 我在这里缺少什么: 来自C头文件的片段: struct test_struct { int _something; complex_struct* _another; }; typedef struct test_struct test; test *test_new(void); int some_method(test **n, int irrelevant); 来自我.pxd的相应片段: cdef struct test_struct: pass ctypedef test_struct test test* test_new() int some_method(test **n, int irrelevant) 我的.pyx: def do_something(int irrelevant): cdef test* t = test_new() ret = some_method(&t, irrelevant) # Here comes the […]