首页 文章 精选 留言 我的

精选列表

搜索[扩展层],共10000篇文章
优秀的个人博客,低调大师

Python的C/C++扩展——boost_python编写Python模块

前面讲述了Python使用ctypes直接调用动态库和使用Python的C语言API封装C函数,本文概述方便封装C++类给Python使用的boost_python库。 安装boost python库: sudo aptitude install libboost-python-dev 示例 下面代码简单实现了一个普通函数maxab()和一个Student类: #include <iostream> #include <string> int maxab(int a, int b) { return a>b?a:b; } class Student { private: int age; std::string name; public: Student() {} Student(std::string const& _name, int _age) { name=_name; age=_age; } static void myrole() { std::cout << "I'm a student!" << std::endl; } void whoami() { std::cout << "I am " << name << std::endl; } bool operator==(Student const& s) const { return age == s.age; } bool operator!=(Student const& s) const { return age != s.age; } }; 使用boost.python库封装也很简单,如下代码所示: #include <Python.h> #include <boost/python.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp> #include <vector> #include "student.h" using namespace boost::python; BOOST_PYTHON_MODULE(student) { // This will enable user-defined docstrings and python signatures, // while disabling the C++ signatures scope().attr("__version__") = "1.0.0"; scope().attr("__doc__") = "a demo module to use boost_python."; docstring_options local_docstring_options(true, false, false); def( "maxab", &maxab, "return max of two numbers.\n" ); class_<Student>("Student", "a class of student") .def(init<>()) .def(init<std::string, int>()) // methods for Chinese word segmentation .def( "whoami", &Student::whoami, "method's doc string..." ) .def( "myrole", &Student::myrole, "method's doc string..." ) .staticmethod("myrole"); // 封装STL class_<std::vector<Student> >("StudentVec") .def(vector_indexing_suite<std::vector<Student> >()) ; } 上述代码还是include了Python.h文件,如果不include的话,会报错误: wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory 编译 编译以上代码有两种方式,一种是在命令行下面直接使用g++编译: g++ -I/usr/include/python2.7 -fPIC wrap_student.cpp -lboost_python -shared -o student.so 首先指定Python.h的路径,如果是Python 3的话就要修改为相应的路径,编译wrap_student.cpp要指定-fPIC参数,链接(-lboost_python)生成动态库(-shared)。 生成的student.so动态库就可以被python直接import使用了 In [1]: import student In [2]: student.maxab(2, 5)Out[2]: 5 In [3]: s = student.Student('Tom', 12) In [4]: s.whoami()I am Tom In [5]: s.myrole()I'm a student!另外一直方法是用python的setuptools编写setup.py脚本: #!/usr/bin/env python from setuptools import setup, Extension setup(name="student", ext_modules=[ Extension("student", ["wrap_student.cpp"], libraries = ["boost_python"]) ]) 然后执行命令编译: python setup.py build or sudo python setup.py install 文章版权归属于 猿人学

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

mica 中 test 扩展 mica-boot-test 单元测试插件

介绍 由于在mica-launcher组件中我们埋入了一些配置,该组件为了方便单元测试。 依赖坐标 Maven <dependency> <groupId>net.dreamlu</groupId> <artifactId>mica-boot-test</artifactId> <scope>test</scope> </dependency> Gradle testCompile "net.dreamlu:mica-boot-test" 使用方式 示例代码一 采用@RunWith(MicaSpringRunner.class)。 @SpringBootTest @MicaBootTest("appName") @RunWith(MicaSpringRunner.class) public class MicaExampleApplicationTests { @Test public void contextLoads() { } } 示例代码二 继承MicaBaseTest。 @SpringBootTest @MicaBootTest("appName") public class ApplicationTests extends MicaBaseTest { @Test public void contextLoads() { } } 说明:上面2种方式均可,@MicaBootTest注解属性有:服务名、profile和enableLoader。 开源推荐 Spring boot 微服务高效开发mica工具集:https://gitee.com/596392912/mica Avue一款基于vue可配置化的神奇框架:https://gitee.com/smallweigit/avue pig宇宙最强微服务(架构师必备):https://gitee.com/log4j/pig SpringBlade完整的线上解决方案(企业开发必备):https://gitee.com/smallc/SpringBlade IJPay 支付SDK让支付触手可及:https://gitee.com/javen205/IJPay 加入【如梦技术】Spring QQ群:479710041,了解更多。 关注我们 扫描上面二维码,更多精彩内容每天推荐!

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

Spring Cloud Gateway 数据库存储路由信息的扩展方案

动态路由背景 ​ 无论你在使用Zuul还是Spring Cloud Gateway 的时候,官方文档提供的方案总是基于配置文件配置的方式 例如: # zuul 的配置形式 routes: pig-auth: path: /auth/** serviceId: pig-auth stripPrefix: true # gateway 的配置形式 routes: - id: pigx-auth uri: lb://pigx-auth predicates: - Path=/auth/** filters: - ValidateCodeGatewayFilter 配置更改需要重启服务,不能满足实际生产过程中的动态刷新、实时变更的业

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

扩展EasyUI在页面中马上显示选中的本地图片

在编写前台页面的时候,有时须要将选中的图片夹杂着其它信息一起上传到服务端,在选着本地图片的时候,为了获得更好的效果,须要将该图片显示在页面上。 最初思路有两个。详细例如以下: 1、获取选中文件的二进制数据再传递给画板,画出图片来。 2、获取选中文件的传递给Img标签。 经过測试,得到例如以下结果: 1、Img标签的src无法指向本地路径的文件,这应该是基于安全考虑的结果吧。 2、通过File API 读取的文件二进制数据无法直接传递给Cancav画板画出选中图片,这也许也是基于安全考虑的结果。 3、经过不懈努力。最终发现了一个解决方法,那就是通过File API的FileReader对象的readAsDataURL将本地图片创建出一个虚拟URL传递给Img标签的src。 具体代码例如以下: 当前项目是基于EasyUi的。以下是页面的对话框代码,请关注id="announcePicture"的文件控件,系统要求在该文件控件选择一个图片文件时,将其显示在id="img"的Img标签中。 <div id="announceDlg" class="easyui-dialog" style="width:400px;height:550px;padding:10px 20px" closed="true" buttons="#announceDlg-buttons"> <div class="ftitle">公告信息</div> <form id="fm" method="post" enctype="multipart/form-data"> <div class="fitem"> <label>公告Id:</label> <input id="announceId" name="announceId" class="easyui-textbox" type="text" value=""> </div> <div class="fitem"> <label>标题:</label> <input type="text" id="announceTitle" name="announceTitle" class="easyui-textbox" value="" data-options="required:true"> </div> <div class="fitem"> <label>文件:</label> <input class="easyui-filebox" id="announcePicture" name="announcePicture" value="" style=""> </div> <div class="fitem"> <label>内容:</label> <input type="text" id="announceContent" name="announceContent" class="easyui-textbox" value="" data-options="iconCls:'icon-search',multiline:true,required:true" style="height:120px"> </div> <div class="fitem"> <label></label> <img id="img" style="width:160px;height:160px" /> </div> </form> </div> <div id="announceDlg-buttons"> <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="save()" style="width:90px">Save</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#announceDlg').dialog('close')" style="width:90px">Cancel</a> </div> 以下是对EasyUI进行的拓展,添加了getFile方法。该方法能够获取当前FileBox对象选中的文件的File对象。 $.extend($.fn.filebox.methods, { getFile: function (myself) { var temp = $(myself).next().children("[type='file']"); var file = document.getElementById(temp.attr("id")); if (file.files.length > 0) { // 若选中一个文件,则返回该文件的File对象 return file.files[0]; } // 若未选中不论什么文件,则返回null return null; } }); 以下是设置FileBox对象的OnChange事件,当选择一个图片后。执行当中定义的事件处理函数: $("#announcePicture").filebox({ onChange: function (event) { // 获取所选文件的File对象 var file = $(this).filebox("getFile"); if (file != null) { // 创建FileReader对象 var reader = new window.FileReader(); // 定义reader的onload事件 // 当读完文件信息后触发onload事件 reader.onload = function (e) { // reader.result保存着产生的虚拟URL $("#img").attr("src", this.result); } // 读取指定文件并形成URL reader.readAsDataURL(file); } } }); 细心的各位一定会发现,在FileBox对象的OnChange事件处理函数内调用getFile时并未传递不论什么阐述。可是在getFile函数中却有一个myself參数被运用了。要了解这点须要看一下EasyUI中处理FileBox的源码: $.fn.filebox = function (_4ed, _4ee) { if (typeof _4ed == "string") { var _4ef = $.fn.filebox.methods[_4ed]; if (_4ef) { return _4ef(this, _4ee); } else { return this.textbox(_4ed, _4ee); } } _4ed = _4ed || {}; return this.each(function () { var _4f0 = $.data(this, "filebox"); if (_4f0) { $.extend(_4f0.options, _4ed); } else { $.data(this, "filebox", { options: $.extend({}, $.fn.filebox.defaults, $.fn.filebox.parseOptions(this), _4ed) }); } _4ea(this); }); }; 看到return _4ef(this, _4ee); 这句语句了吗?EasyUI在调用指定方法时,默认第一个參数为当前对象,而实际传入的參数,则作为第二个參数来使用。 原文发布时间为: 2018 年 01 月 01 日 原文作者:会哭的鳄鱼 本文来源:开源中国 如需转载请联系原作者

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

服务器架构之性能扩展-第十一章(12)

第十一章、Linux高可用(HA)高性能(HPC) 高可用的概念 高可用:(high-availablility)是一组互相信任的计算机组织在一起,当其中的一个成员出现故障,由其它成员来接替它的工作提供服务。 它的作用是提供冗余,并没有提高系统性能。 高可用要用到数据的共享,高可用状态下数据共享有磁盘复制和共享磁盘方式,其中磁盘复制在数据量小更新较少时适用。 高可用就要用到心跳检测工具,我们这里适用heartbeat实现,Heartbeat项目是Linux-HA工程的一个组成部分,它实现了一个高可用集群系统。 实验: 环境:我们使用两台计算机做双击热备,系统为centos5.6 共享磁盘的方式做两个集群,两个主机一个是192.168.211.7和192.168.211.8,他们和测试主机windows7在同一个网段,win7ip为192.168,211,1他们之间有一根心跳线连接10.0.0.1和10.0.0.2. 配置实例 我们使用setup分别给eth0和eth1分配ip,其中eth0使用vmlan1路由和win7在同一网段,eth1使用vmlan2路由。然后,我们要使各个主机之间ping通。 heartbeat的安装 1、我们可以到Linux-ha.org下载该软件 在安装之前要先安装pkconfig,glib2-devel,e2fsprongs和libnet。其中libnet需要源码安装。 2、建立用户和组 Groupadd –g 2000 haclient Useradd –u 2000 –g haclient hacluster 3、Yum -y install heartbeat 4、配置heartbeat 配置文件,我们可以到解压的heartbeat/doc/目录复制一份到/etc/ha.d/目录下,保证两个centos主机的配置文件相同,我们配置好一台,然后scp或rsync一份给另一台主机即可。 Vi/etc/ha.d/ha.cf Bcast eth1//eth1做心跳检测 keepalive 3//3秒更新一次 deadtime 30//10秒更新服务器 warntime 5//5秒警告级别 udpport 694//端口 auto_failback on//自动切换 nodenode1//节点1,接主机名 nodenode2//节点2 logfile /var/log/ha-log//日志 respawn hacluster /usr/lib/heartbeat/ipfail ping 10.0.0.2 Haresource文件 Vi /etc/ha.d/haresource Node1192.168.211.9/24 httpd//转发的服务器地址 Vi /etc/ha.d/authkeys Auth 1 1 crc hello #Scpha.cf root@192.168.211.8:/etc/ha.d //server2需要同样的配置 #Scpharesource root@192.168.211.8:/etc/ha.d #Scp authkeysroot@192.168.211.8:/etc/ha.d Chmod 600 authkeys Service heartbeat restart 会看到多了一个eth1:0的网卡 这时对192.168.211.7开启apache,然后做解析 Vi /etc/hosts 192.168.211.7node1 192.168.211.8node2 然后分别访问一下。 这时停止node1,会看到node2会自动进行开启eth1:0网卡。 这时192.168.211.9一会就可以ping通访问了。 本文转自zsaisai 51CTO博客,原文链接:http://blog.51cto.com/3402313/1032767

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

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

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册