使用Docker+VS Code搭建Go+Python的开发环境

因为项目需要,可能使用Go调用Python 3的接口,趁这个机会实践了一下,如何用Docker+VS Code迅速地搭建开发环境,真的非常非常非常香!

准备Docker Image和Container

先使用Docker迅速的Build出包含了Go,Python 3.7的Image,同时也下载了DataDog/go-python3。这是一个挺不错的Go Module,可以用来调用Python 3的C API,进而调用Python的API(后续写篇文章介绍一下用法)。

有了Dockerfile,再使用docker build生成Image

Image生成后,就可以启动Container了,

Container的名字叫go_dev, 同时挂载了H:\go_dev/go_dev上。Container里的go, python3都已经安装完成。

使用VSCode连接到Docker container

首先需要安装Remote Container的插件,真的是个神器!

安装完成后,可以在左下角一个新的张铎图标,点击后就可以连接刚才启动的Container了。

在菜单中选择,“Attach to Running Container

在后面的菜单中,选择Container “go_dev”。第一次连接时,需要选择一个目录来写代码了。连接后,打开Terminal也默认调整在Container里的目录。非常方便!

在Remote模式下,代码的很多分析是在Container里完成的,所以在第一次连接完成后,你需要把一些需要的Plugin再安装一下,比如Go语言的支持,Python的支持等等。这个要根据实际的需要。

好了,开发环境搞定,继续板砖写代码吧!

 

 

Use Atlassian Confluence API to Copy and Create New Pages

For team management, we have a Confluence space to do weekly reporting. In that space, usually, one page is for a week and each page has the same template. So to copy these pages becomes a very boring work. No worries, there are Confluence APIs to help.

Installation and Connection to Confluence

Very simple and pip will help to finish everything.

After installation, use the following code to connect to Confluence.

Copy and Create New Page

Before creating new page, you need to get the body content of the template page.

Pay attention to the expand=’body.storage’. Without it, the returned page only contains the basic information, no body HTML.

Now you have the body and the rest work is a just for-loop.

Also you can use confluence.update_page() to update some existing pages.

References

Create Python Virtual Environment with virtualenv

There are a lot of pros to using a virtual environment to manage different Python environment, which will be independent of each other. The following are the steps to create one under Ubuntu 16.04 with Python 2.7.x

Install the virtualenv with the necessary packages

Create a folder for the virtualenv

Activate & Deactivate the virtual environment

After sourcing the activate script, you can find a “(myenv)” in front of the folder prompt, which is the virtual environment’s name and it has been activated.

Now, the pip can be used to install/update the python modules belongs to this virtual env only.

To deactivate is also very simple.

 

对Python脚本做简单的profiling

最近事情好多,Blog好久没有更新了。今天上来写写最近解决的一个Python里的性能优化问题。

起因

之前为项目写过一个Sqlite数据库预处理的Python脚本,里面主要做了张新表,把其他表的数据填进去。当时主要考虑到维护性,条理清楚,就没太考虑Performance。之后QA发现模块的运行比原来慢了20倍,因为还是挺快,所以没有当时马上修正。

Profiling

这次Release要修掉这个问题。我的原则是,改进Performance一定要做Profiling,做到有的放矢才。

和Java的VirtualVM类似,Python 2.7也内置了几个Module做Profiling,我选择了cProfile。基本就是如下命令:

“-s tottime”是让结果用总执行时间排序。

优化之前的执行结果,

可见罪魁祸首就是sqlite3的Cursor的execute()方法,和原本猜测的也是一样的。优化的手法也很明确就是减少execute()的调用次数,使用batch和合并SQL语句的办法,很容易就用空间换回了时间。

优化之后的结果,

优化之后只是原来的9%的Runtime。

总结

  • 继续坚持用Profiler来做Performance的改进。
  • 边改边用Profiler查看Performance有没有提升。
  • 不要过分优化,否则代码没法看了。😀

References

强大的XPath

最近的项目开发中常常要分析XML,深切体会到XPath的强大和方便。Java可以使用DOM4j,Python 2.5以后可以使用了etree。

举个小例子,如果有如下XML:

要去掉所有<B>的节点,XPath可以如何做呢?

XPath的灵活之处就在于,可以用简单明了的Path控制想要读取的节点。

其实还有很多有趣的特性,可以看看XPath Tutorial

关于Perl语言基本知识的学习总结

老大走了,同时留了大约7,8千行的Perl脚本给我们维护。以前Perl没学过,这两天大概看了一下,现在做下简单的总结。

总的感觉是:Perl语言真的很简洁!

变量

Perl的变量类型分成了几种:标量类型,数组类型,联合类型(理解成Map比较好)。

比如:

NOTE:其实还有引用类型,只是现在还没有用到,先不总结了。

流程控制

这个部分和其他语言很像,但有些地方会再简洁,Larry果然真的确实”很懒”。

条件控制

循环控制

和C很像,就是有的地方别忘了加上$, 如:

函数

基本语法

包(Package)

package定义一个包,如:

最后的”1;”非常重要,不然别的脚本就无法使用这个包。Perl里的True/False很像Python里定义的。

那如何使用呢,也很简单。

有几个小问题:

  1. 包的搜索路径在@INC中,如有需要,要在开始加

总结

这些就是Perl的基本内容,再把正则表达式看一下就可以开始读代码的了。