-
Use Python to Check Difference between Directories Recursively
I needed to track differences between software release packages, so that if anything changed dramatically, eg. some file missing or much smaller than expected, I can then get a notification to review the new potentially flawed package. I found that filecmp.dircmp class in Python is spot on for this job. Here’s my snippet to compare…
-
Manage AWS EBS Snapshot Life Cycle with Lambda
The timing is not so great. The AWS Data Lifecycle Manager has been announced but I can’t wait for its release. So I decided to use AWS Lambda to do some snapshot lifecycle management. First a role for Lambda having full access to snapshots can be created via the console. To create snapshot with Python…
-
Saving Images in Chrome with Selenium
Here’s how to save images in Chrome browser using Selenium. The API has element.screenshot_as_png() method but apparently it’s not implemented at the moment. With some minor changes to this answer I can save a image via the browser: 🙂
-
A Simple Python IO Trick
I thought using Python’s gzip is quite straight forward, however the IO performance almost doubled with an extra BufferedWriter: import io import gzip def export_to_json_gz(schema): json_file = “test.json.gz” with io.BufferedWriter( gzip.open(temp_dir + json_file, ‘wb’) ) as gzfile: for row in stream_table_data(schema): ujson.dump(row, gzfile, ensure_ascii=False) gzfile.write(‘\n’) Now the bottle neck…
-
MySQL/ Aurora to Google BigQuery
Google BigQuery(BQ) is Google’s column storage, it’s super fast when you need to query over many gigabytes of data. BQ defaults to utf8, so it makes sense the tables in MySQL/ Aurora are also utf8 encoded. I use Python and SQL Alchemy to load data to BQ. It’s a waste for SA’s ORM features but…
-
学TurboGears 2顺便还得学习一下Genshi
Genshi是Python环境下的一个比较出名的HTML/XML/…模板框架. 作为TurboGears2.1缺省的模板组件, Genshi得到了很多好评. 我也正是因为TG粘合了如此优秀的组件而决心学习TG的. TG2.1样本项目里的主模板文件master.html的完整解释: http://www.turbogears.org/2.1/docs/main/master_html.html#master-html Genshi模板基础: http://genshi.edgewall.org/wiki/Documentation/templates.html 首先, 在TG2中通过@expose decorator来为一个View(V in MVC)指定Genshi模板. 如果不需要模板, 就是: @expose() def hello(self): return “Hello world from controller.” 这样在缺省的开发环境下, 访问http://127.0.0.1:8080/hello, 将得到简单的 Hello world from controller. 当然, 这除了测试似乎没有什么实际意义. 然后是使用模板的版本: @expose(‘example.templates.index’) def new_hello(self): return dict(hello=”New text from controller + template “, page=”new_hello”) 这样, TG会寻找example项目下的example/templates/index.html作为new_hello的模板. 同时, 将两个key传递给模板: hello和page. 而在index模板内, 可以很随意的调用这两个获得的key. <h1 py:content=”hello”>old Hello, world.</h1> Now…
-
学习Python + TurboGears 2
前一阵子我着实的困惑了几分钟: 下半辈子做什么呢? 当别人努力的stand out时, 我却在不停的试图blend in. 好在依旧自信, 我索性说, who cares, 我要继续学习我想学的东西: 互联网技术+摄影, 不管有没有搞头, 应该不至于挨饿. 废话到此为止. 选择TG而淘汰Django的原因是TG的耦合度较低, 而且学习难度要大些. 你了解我的, 我喜欢做难度大的事情, 例如投胎到中国. TG目前的版本是2.1, 安装方法可以参考: http://www.turbogears.org/2.1/docs/main/DownloadInstall.html 在Ubuntu环境下, 先安装必要基础: $ sudo aptitude install build-essential python-dev python-setuptools python-virtualenv 然后在CLI里如下方法开始第一个TG项目: $ virtualenv –no-site-packages -p python2.6 tg2env $ cd tg2env/ $ source bin/activate (tg2env)$ easy_install -i http://www.turbogears.org/2.1/downloads/current/index tg.devtools (tg2env)$ paster quickstart example…
-
Unicode编码中的简繁中文互转
现在简繁中文的转换已经不像从前那样,是gbk/ big5两种文字编码之间的转换,而是在同一个编码,Unicode,当中不同编码的映射。当然,简繁中文绝对不是简单的一一对应关系,有一对多的个别情况,例如简体的“后” 对应繁体的 “后”和“後”。行业词汇也有差别,例如大陆说“宽带”,而港台说“寬頻”;大陆说“操作系统”,而港台说“作業系統” 等等。 因此当我决定用Python程序将森海塞尔简体中文网站转换为繁体中文网站时,我的计划分三个步骤: 提取行业高频关键字,找到简体和繁体的对应,编成词典 利用现有的Python中文本地化工具包,将关键字之外的文字转换 请港台Freelancer人工校对,确保本土化 第一步,我的词典替换程序在此: 第二步,我利用的是pyzh中文工具包中的fanjian.py完成。特别感谢pyzh项目组的全部成员和fanjian.py程序作者ne.manman和zsp007! 下面就交给香港的朋友了,希望不会累坏他:) 顺便说,Songbird很不错,有点山寨iTunes,那又怎样呢?毕竟Linux下面还没iTunes。 Songbird