首页 文章 精选 留言 我的

精选列表

搜索[副语言],共10000篇文章
优秀的个人博客,低调大师

使用C语言扩展Python(五)

上一篇中我们在python端的做法是每次读取一个数据块,然后将这个数据块传递进C扩展模块中去,但对于目标文件的数据写入是在C扩展模块中完成的,但其实可以更面向对象一点,不是吗?原来outfp是一个文件指针, 不如改成一个从Python中传递一个文件对象到C模块里去,这个文件对象有自己的write方法,这样在C扩展模块中你就可以回调文件对象的write方法来完成数据的写入。 首先来看Python端的代码,我们定义了一个file类继承下来的MyFile子类,其中的write方法就是为在C扩展模块中回调而专门准备的。 复制代码 代码 #!/usr/bin/env python import clame INBUFSIZE = 4096 class MyFile(file): def __init__(self, path, mode): file.__init__(self, path, mode) self.n = 0 def write(self, s): file.write(self, s) self.n += 1 output = MyFile('test3.mp3', 'wb') encoder = clame.Encoder(output) input = file('test.raw', 'rb') data = input.read(INBUFSIZE) while data != '': encoder.encode(data) data = input.read(INBUFSIZE) input.close() encoder.close() output.close() print 'output.write was called %d times' % output.n 复制代码 再来看C模块的代码,clame_EncoderObject结构体中的outfp改成了PyObject类型的指针,相应的dealloc方法也做了调整,由于outfp是一个对象,因此需要对其引用计数进行减1操作,而以前的代码是直接调用fclose来直接关闭打开的目标文件。但现在我们只需要对其引用计数做减1操作,等到其为0的时候,在外部的python代码中就可以关闭这个文件对象。同样可以看到在init函数中有相应的引用计数加1的操作。在encode和close两个函数中,通过PyObject_CallMethod实现了对Python对象中指定方法的回调。 复制代码 代码 #include <Python.h> #include <lame.h> /* * On Linux: * gcc -shared -I/usr/include/python2.6 -I/usr/local/include/lame clame.c \ * -lmp3lame -o clame.so * * */ typedef struct { PyObject_HEAD PyObject *outfp; lame_global_flags *gfp; } clame_EncoderObject; static PyObject *Encoder_new(PyTypeObject *type, PyObject *args, PyObject *kw) { clame_EncoderObject *self = (clame_EncoderObject *)type->tp_alloc(type, 0); self->outfp = NULL; self->gfp = NULL; return (PyObject *)self; } static void Encoder_dealloc(clame_EncoderObject *self) { if (self->gfp) { lame_close(self->gfp); } Py_XDECREF(self->outfp); self->ob_type->tp_free(self); } static int Encoder_init(clame_EncoderObject *self, PyObject *args, PyObject *kw) { PyObject *outfp; if (!PyArg_ParseTuple(args, "O", &outfp)) { return -1; } if (self->outfp || self->gfp) { PyErr_SetString(PyExc_Exception, "__init__ already called"); return -1; } self->outfp = outfp; Py_INCREF(self->outfp); self->gfp = lame_init(); lame_init_params(self->gfp); return 0; } static PyObject *Encoder_encode(clame_EncoderObject *self, PyObject *args) { char *in_buffer; int in_length; int mp3_length; char *mp3_buffer; int mp3_bytes; if (!(self->outfp && self->gfp)) { PyErr_SetString(PyExc_Exception, "encoder not open"); return NULL; } if (!PyArg_ParseTuple(args, "s#", &in_buffer, &in_length)) { return NULL; } in_length /= 2; mp3_length = (int)(1.25 * in_length) + 7200; mp3_buffer = (char *)malloc(mp3_length); if (in_length > 0) { mp3_bytes = lame_encode_buffer_interleaved( self->gfp, (short *)in_buffer, in_length / 2, mp3_buffer, mp3_length ); if (mp3_bytes > 0) { PyObject* write_result = PyObject_CallMethod( self->outfp, "write", "(s#)", mp3_buffer, mp3_bytes); if (!write_result) { free(mp3_buffer); return NULL; } Py_DECREF(write_result); } } free(mp3_buffer); Py_RETURN_NONE; } static PyObject *Encoder_close(clame_EncoderObject *self) { int mp3_length; char *mp3_buffer; int mp3_bytes; if (!(self->outfp && self->gfp)) { PyErr_SetString(PyExc_Exception, "encoder not open"); return NULL; } mp3_length = 7200; mp3_buffer = (char *)malloc(mp3_length); mp3_bytes = lame_encode_flush(self->gfp, mp3_buffer, sizeof(mp3_buffer)); if (mp3_bytes > 0) { PyObject* write_result = PyObject_CallMethod( self->outfp, "write", "(s#)", mp3_buffer, mp3_bytes); if (!write_result) { free(mp3_buffer); return NULL; } Py_DECREF(write_result); } free(mp3_buffer); lame_close(self->gfp); self->gfp = NULL; self->outfp = NULL; Py_RETURN_NONE; } static PyMethodDef Encoder_methods[] = { { "encode", (PyCFunction)Encoder_encode, METH_VARARGS, "Encodes and writes data to the output file." }, { "close", (PyCFunction)Encoder_close, METH_NOARGS, "Closes the output file." }, { NULL, NULL, 0, NULL } }; static PyTypeObject clame_EncoderType = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ "clame.Encoder", /* tp_name */ sizeof(clame_EncoderObject), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)Encoder_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ "My first encoder object.", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ Encoder_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)Encoder_init, /* tp_init */ 0, /* tp_alloc */ Encoder_new, /* tp_new */ 0, /* tp_free */ }; static PyMethodDef clame_methods[] = { { NULL, NULL, 0, NULL } }; PyMODINIT_FUNC initclame() { PyObject *m; if (PyType_Ready(&clame_EncoderType) < 0) { return; } m = Py_InitModule3("clame", clame_methods, "My third LAME module."); Py_INCREF(&clame_EncoderType); PyModule_AddObject(m, "Encoder", (PyObject *)&clame_EncoderType); } 复制代码 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2010/05/23/1742344.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

libnetwork —— 容器网络模型(Go 语言)

Libnetwork 提供一个原生 Go 实现的容器连接,是容器的网络。libnetwork 的目标是定义一个健壮的容器网络模型(Container Network Model),提供一个一致的编程接口和应用程序的网络抽象。 Libnetwork一开始的代码只是 libcontainer 和 Docker Engine 中网络部分代码的合并,Docker 官方的愿景是希望 libnetwork 能像 libcontainer 一样,成为一个多平台的容器网络基础包。 受之前的一个 GitHub issue 启 发,libnetwork 引入了容器网络模型(CNM)的概念,CNM 定义了三个新的术语,分别是网络沙箱、Endpoint、Network。网络沙箱 指的是在每一个容器中,将会有一个隔离的用于网络配置的环境。Endpoint 是一个网络接口,可用于某一网络上的交流。Network 是一个唯一的且可识别的 Endpoint组。 接下来,Docker 公司将会把 libnetwork 集成到 Docker Engine,并在 Docker CLI 中使用新的网络命令。具体的项目路线图读者可以参考 GitHub。 注意:libnetwork 项目正在大力开发中,还不适合日常使用! 使用示例: // Create a new controller instance controller := libnetwork.New() // Select and configure the network driver networkType := "bridge" driverOptions := options.Generic{} genericOption := make(map[string]interface{}) genericOption[options.GenericData] = driverOptions err := controller.ConfigureNetworkDriver(networkType, genericOption) if err != nil { return } // Create a network for containers to join. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make of network, err := controller.NewNetwork(networkType, "network1") if err != nil { return } // For each new container: allocate IP and interfaces. The returned network // settings will be used for container infos (inspect and such), as well as // iptables rules for port publishing. This info is contained or accessible // from the returned endpoint. ep, err := network.CreateEndpoint("Endpoint1") if err != nil { return } // A container can join the endpoint by providing the container ID to the join // api which returns the sandbox key which can be used to access the sandbox // created for the container during join. // Join acceps Variadic arguments which will be made use of by libnetwork and Drivers _, err = ep.Join("container1", libnetwork.JoinOptionHostname("test"), libnetwork.JoinOptionDomainname("docker.io")) if err != nil { return } 文章转载自 开源中国社区 [http://www.oschina.net]

优秀的个人博客,低调大师

自然语言处理哪家强?

语音交互事关未来,国内外已经不少公司在抢蛋糕了,大公司收购、投资、合作不断,就可见一斑。目前,基本上所有的巨头都有涉足。 苹果收购Siri、Novauris,组建基于神经网络算法的语音识别团队 2014 年 7 月,有消息称,苹果正在联系多位语音识别技术的专家,正在招聘经理、小组负责人、研究员,很可能将成立一支强大的语音识别团队,并最终取代现在的 Siri 技术服务商 Nuance。过去几年中,苹果招聘了多名此前供职于 Nuance 的的高层次语音技术研究员,包括前研究副总裁拉里·吉利克 (Larry Gillick)、Siri 的项目经理贡纳尔·艾弗曼(Gunnar Evermann)、微软语音识别项目高管亚历克斯·阿赛洛(Alex Acero)。 在 2013 年,苹果收购了自动语音识别公司 Novauris Technologie

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码