代码改变世界

Functional Programming 资料收集

2015-01-03 16:17 by 会被淹死的鱼, 388 阅读, 0 推荐, 收藏, 编辑
摘要:书籍:Functional Programming forJava DevelopersSICP(Structure and Interpretation of Computer Programs)课程主页(有免费在线版):https://mitpress.mit.edu/sicp/视频教程:htt... 阅读全文

Python的问题解决: IOError: [Errno 32] Broken pipe

2014-10-18 21:24 by 会被淹死的鱼, 44533 阅读, 0 推荐, 收藏, 编辑
摘要:被该问题困扰的人还是挺多的,所以又对这个问题进行了一些更深入的分析,希望可以解决读者的问题新版本:Python 的 Broken Pipe 错误问题分析遇到一个很奇怪的问题, web.py代码里面报错IOError: [Errno 32] Broken pipe启动命令: nohup python 阅读全文

python的subprocess的简单使用和注意事项

2014-03-27 00:38 by 会被淹死的鱼, 9541 阅读, 0 推荐, 收藏, 编辑
摘要:subprocess是python在2.4引入的模块, 主要用来替代下面几个模块和方法:os.systemos.spawn*os.popen*popen2.*commands.*可以参考PEP324:http://legacy.python.org/dev/peps/pep-0324/这是一个用来调用外部命令的模块, 替代了一些旧的模块, 提供了更加友好统一的接口.三个封装方法使用下面三个方法的时候, 注意两个问题: 1. shell=True或False, 两种解析方式是不同的 2. 注意PIPE的使用, 可能导致卡死subprocess.call 运行命令, 等待完成, 并返回return 阅读全文

Python tricks(7) -- new-style class的__slots__属性

2014-03-21 22:37 by 会被淹死的鱼, 578 阅读, 0 推荐, 收藏, 编辑
摘要:__slots__是在python 2.2开始引入的一个新特性, 我们来看一下官方给出的解释.This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. If defined in a new-style class,__slots__reserves space for the declared variables and prevents the automatic creation of__dict__and__w 阅读全文

Python tricks(6) -- python代码执行的效率

2014-02-15 23:49 by 会被淹死的鱼, 566 阅读, 0 推荐, 收藏, 编辑
摘要:python作为一个动态语言, 本身学习曲线比较平滑, 效率相比起来会比c++和java低一些. 脚本语言都是运行时编译的, 这个对于效率的影响是非常大的.我借用参考1的代码, 加了点代码import timeimport timeclass Timer(object): def __init__(self): pass def __enter__(self): self.start = time.time() def __exit__(self, exception_type, exception_val, trace): prin... 阅读全文

Python tricks(5) -- string和integer的comparison操作

2014-02-12 23:17 by 会被淹死的鱼, 889 阅读, 0 推荐, 收藏, 编辑
摘要:我们都知道, python是一个强类型的语言, 也是一个动态类型的语言. 但是在python2.X系列中, 这个强类型是需要打折扣的, 是非常接近强类型.我们来看下面的代码片段In [1]: 'a' Bar()# 规则2a, b = Foo(), Foo()print id(a) > id(b), id(a), id(b)print a > b# 规则3print 100 > 1print 'b' > 'a'class Foo: passclass Bar(object): pass# 规则4print Foo > 阅读全文

Python tricks(4) -- with statement

2014-01-25 22:13 by 会被淹死的鱼, 1073 阅读, 0 推荐, 收藏, 编辑
摘要:简介with是从2.5版本引入的一个语法. 这个语法本身是为了解决try..finally繁琐的释放各类资源(文件句柄, Lock等)的问题.如果想在旧版本中使用这个功能, 直接引入future模块就可以.from __future__ import with_statement举例简单说明一下没有... 阅读全文

Python tricks(3) -- list和dict的遍历和方法

2014-01-23 23:11 by 会被淹死的鱼, 25528 阅读, 0 推荐, 收藏, 编辑
摘要:每个人在使用python的过程中都会遍历list和dict.List遍历最常用最简单的遍历list的方法a = ["a", "b", "c", "d"]# simple iteratefor i in a: print i但是, 如果我需要拿到list的index, 很多人可能会这样写a = ["a", "b", "c", "d"]# index & valuefor i in xrange(len(a)): print i, 阅读全文

Python tricks(2) -- method默认参数和闭包closure

2014-01-23 00:24 by 会被淹死的鱼, 627 阅读, 0 推荐, 收藏, 编辑
摘要:Python的method可以设置默认参数, 默认参数如果是可变的类型, 比如list, map等, 将会影响所有的该方法调用.下面是一个简单的例子def f(a=None, l=[]): if not a: return l l.append(a) return lif __name__ == "__main__": print f("a") print f("b") print f("b") print f(l=[]) print f()输出结果如下:['a']['a',  阅读全文

《Hadoop权威指南》(Hadoop:The Definitive Guide) 气象数据集下载脚本

2014-01-20 00:55 by 会被淹死的鱼, 784 阅读, 0 推荐, 收藏, 编辑
摘要:已过时,无法使用从网上找到一个脚本,修改了一下#!/bin/bashCURRENT_DIR=$(cd `dirname $0`; pwd)[ -e $CURRENT_DIR/ncdc ] || mkdir $CURRENT_DIR/ncdc[ -e $CURRENT_DIR/ncdc/files ]... 阅读全文