the blog of huhuchen

记录成长.

Android资料整理

| Comments

最重要的一个地方

google developer

开源社区

开源社区大神

周报(邮件订阅 or RSS)

Blog

UI 设计

  • Android Niceties A collection of screenshots encompassing some of the most beautiful looking Android apps.

看过的书

Android studio插件

Python代码小记-效率及明确

| Comments

今天写了一段代码,结果改了好几次,特此记录一下。

功能:传入任意字符,将其decode为unicode字符。

version1
1
2
3
4
import chardet
def convert_charset_to_unicode(charset, default='utf-8'):
    charset_encoding = chardet.detect(charset).get('encoding') or default
    return charset.decode(charset_encoding, 'ignore')

问题:

  • 效率低下,通常大多数需要decode字符都是utf-8,也就是说只有少数的情况下需要detect encoding。而现在的情况是每次decode都需要detect,而detect这个操作比较重,因此需要就效率进行一次优化。
version2
1
2
3
4
5
6
7
import chardet
def convert_charset_to_unicode(charset, default='utf-8'):
    try:
        return charset.decode(default)
    except:
        charset_encoding = chardet.detect(charset).get('encoding') or default
        return charset.decode(charset_encoding, 'ignore')

问题:

  • 没有明确定义错误类型,当使用try..except..时,一般异常明确的时候需要指出其异常类型。
version3
1
2
3
4
5
6
7
import chardet
def convert_charset_to_unicode(charset, default='utf-8'):
    try:
        return charset.decode(default)
    except UnicodeDecodeError:
        charset_encoding = chardet.detect(charset).get('encoding') or default
        return charset.decode(charset_encoding, 'ignore')

小结:注重效率以及明确异常类型。

使用github Pages和octopress搭建blog

| Comments

第一篇blog简单介绍下使用octopress在github pages上搭建blog的步骤。

  • 首先参看Octopress官方文档 Octopress Setup
  • 我使用的主题是 Octoflat 具体的demo
  • 博客正文是使用 Markdown
  • comment使用的是Disqus。只需要在octopress的_config.yml里配置一下就行了。
1
2
 disqus_short_name: your_disqus_short_name
 disqus_show_comment_count: true