Tag: 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

  • 从6.7到0.01

    编程果然不能偷懒!我这一偷懒,CPU就不知道要受多少累、冒多少热气了。一开始,我采用了很无脑的穷举方法,复杂度是O(n**2),结果肯定正确的,但是长度一万的数组就可以耗去6.7秒的CPU时间。 raymond@raymond-laptop:~/_Work/Dev/python$ python le.py time:  0:00:06.708924 return:  1 0 0 0 之后,在高手提示下将复杂度降低到n (log n + 1) ,结果不错了。 raymond@raymond-laptop:~/_Work/Dev/python$ python le.py time:  0:00:00.010430 return:  1 0 0 0 对应改进后的代码: def leader3(array): t = len(array) //2 array.sort() p = -1 c = 1 for i in array: if p == i : c+=1 if c > t:…

  • 偷闲,写了个初级二叉树排序

    二叉树是计算机算法中普遍采用的数据结构。在所有排序算法中具有高效率的二叉树排序就是基于此结构。我抽空用新学的Python写了一个试试: class node: left = None right = None value = 0 def __init__(self, v): self.value = v def walk(self): r = [] if self.left: r.extend(self.left.walk()) r.append(self.value) if self.right: r.extend(self.right.walk()) return r def insert(self, n): if self.value < n: if self.right: self.right.insert(n) else: self.right = node(n) else: if self.left: self.left.insert(n) else: self.left = node(n) if…