Eli's Notes

lifelong learning


  • 首页

  • 归档

  • 标签

  • 搜索

go命令相关

发表于 2016-09-30 |
  1. godep相关
    1
    2
    3
    4
    #安装godep
    go get github.com/tools/godep
    #安装Godeps/Godeps.json中的依赖
    godep restore

使用mock进行单元测试(python)

发表于 2016-09-21 |

此代码涵盖了mock的基本用法,代码来源于mock文档 mock doc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from mock import MagicMock, Mock, patch, ANY
# 1.使用mock 替换对象中的方法
# mock.assert_called_once_with 用于验证调用参数是否正确
class ProductionClass(object):
def method(self):
self.something(1, 2, 3)
def something(self, a, b, c):
pass
real = ProductionClass()
real.something = MagicMock()
real.method()
real.something.assert_called_once_with(1, 2, 3)
# 2.使用mock 替代传入参数
# assert_called_with 用于验证依赖对像是否被正确使用
class ProductionClass(object):
def closer(self, something):
something.close()
real = ProductionClass()
mock = Mock()
real.closer(mock)
mock.close.assert_called_with()
# 3.使用Mock 替换Class
# 替换类于替换方法类似
# 例子需要构造module模块
import sys
sys.modules['module'] = Mock()
import module
def calculate():
instance = module.Helper()
return instance.getFactor() *3
with patch('module.Helper') as mock:
instance = mock.return_value
instance.getFactor.return_value = 1
result = calculate()
instance.getFactor.assert_called_once_with() #方法被正确调用
assert result == 3 #验证结果是否正确
# 4. side_effect, literate
mock = MagicMock(side_effect=[4, 5, 6])
assert mock()==4
assert mock()==5
assert mock()==6
# 5. side_effect, Exception
mock = Mock(side_effect=Exception('Boom!'))
# 6. side_effect, function
vals = {(1, 2): 1, (2, 3): 2}
def side_effect(*args):
return vals[args]
mock = MagicMock(side_effect=side_effect)
assert 1==mock(1, 2)
assert 2==mock(2, 3)
# 7. 基于类/对象为模板创建对象
# 使用模版不存在的方法或者属性会报错
class SomeClass(object):
pass
a = SomeClass()
mock = Mock(spec=a)
#mock.a
#mock.old_method() # 报错
# 8. mock 调用多次
returns = [1, 2]
def side_effect(*args):
result = returns.pop(0)
if isinstance(result, Exception):
raise result
return result
mock = Mock(side_effect=side_effect)
assert 1==mock('first')
assert 2==mock('second')
# 9. patch应用到多个
from unittest2 import TestCase
@patch('module.SomeClass')
class MyTest(TestCase):
def test_one(self, MockSomeClass):
self.assertTrue(module.SomeClass is MockSomeClass)
def test_two(self, MockSomeClass):
self.assertTrue(module.SomeClass is MockSomeClass)
def not_a_test(self):
return 'something'
MyTest('test_one').test_one()
MyTest('test_two').test_two()
MyTest('test_two').not_a_test()
class MyTest(TestCase):
def setUp(self):
self.patcher = patch('module.foo')
self.mock_foo = self.patcher.start()
def test_foo(self):
self.assertTrue(module.foo is self.mock_foo)
def tearDown(self):
self.patcher.stop()
MyTest('test_foo').run()
# 10. ANY
mock = Mock(return_value=None)
mock('foo', bar=object())
mock.assert_called_once_with('foo', bar=ANY)

命令行下格式化json

发表于 2016-08-11 |
1
python2 -m json.tool xxx.json

向python对象动态添加方法

发表于 2016-08-06 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def barFighters( self ):
print "barFighters"
a.barFighters = barFighters
a.barFighters()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: barFighters() takes exactly 1 argument (0 given)
import types
a.barFighters = types.MethodType( barFighters, a )
a.barFighters()
barFighters

>

引用:
http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance

svn常用命令

发表于 2016-07-25 |
  1. 显示前10条log

    1
    svn log -l 10
  2. 显示版本差异,只输出文件名

    1
    svn diff --summarize -r954:958
  3. 回退到历史版本

    1
    2
    3
    svn merge -r 958:954 ""
    svn diff
    svn commit -m "revert"

curl发送post数据

发表于 2016-07-21 |
1
curl -d "id=7778&name='wzp'" "http://172.16.102.208:8089/wiapi/score"

查看磁盘使用

发表于 2016-06-07 |

centos 查看磁盘空间利用大小:

df -h

查看当前目录所占空间大小:

du -sh

du命令查看一级目录

centos: du -h –max-depth=1
mac

mac: du -hd1\n
mac

ssh免密码登录

发表于 2016-06-07 |

Note: 首先任意一个机器上生成公钥(ids_rsa.pub)和私钥(ids_rsa), 然后将公钥放到登录目标机器的authorized_keys中, 然后就可以使用私钥登录了。
具体流程:http://chenlb.iteye.com/blog/211809

python入门

发表于 2016-06-06 |

python是一个简单而强大的语言,应用领域非常广泛。因此,网上的优秀的入门教程非常多。
此文档只是对入门教程的补充。

python的特点

  1. 拥有简单而优雅的语法,严格的格式要求, 因此代码很整洁。
  2. 开源软件,经过多年的积累,拥有众多的库, 因此完全不需要重复造轮子。
  3. 解释性的语言,不需要编译,能够快速执行出结果。因此,常用的调试方式,不是打断点,
    而是添加日志。
  4. 开发方便,不需要IDE支持,notepad也可以作为常用开发软件。
  5. 网上有丰富的文档,无论你遇到神马问题,使用百度/谷歌都是很好的解决方法。

安装

  1. 官方下载地址: https://www.python.org/downloads/
  2. 推荐版本2.7, 由于python3.x进行了语法的升级,很多库不支持3.x的语法。
  3. 安装ipython,ipython是python解释器的升级版,支持语法高亮,tab补全等多个高级功能。5星推荐。
  4. 安装库有两种方法, 一种是使用pip或者easy_install(命令行下安装,推荐),
    另一种是手动下载库文件(基本上用不到)https://pypi.python.org/pypi?%3Aaction=browse

语法

  1. 基本语法与其他语言大同小异,for循环、if条件判断、print 输出打印
  2. 常用数据结构有list列表、dict字典等
  3. 面向对象编程,class,支持继承

推荐教程

  1. 推荐书Dive into python, 很薄的书,可以用来查查语法。
    中文版
  2. 推荐python 官方文档,非常丰富的文档资源https://docs.python.org/2/
  3. 另外一个入门教程http://www.pythondoc.com/pythontutorial27/index.html

Hello World!

1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
"""
auther: 一个忍不住写hello world的人。
"""
def hello():
print "hello world!"
hello()

英语学习计划

发表于 2016-05-17 |

具体练习方法:

  1. 流利说APP:纠正发音
  2. 听写(沪江听力酷):增加听力词汇量
  3. 复读并录音:纠正发音,提高听力
  4. 阅读英文原版书:提高阅读词汇量,并熟悉常用描述方法。
  5. Think in English: 提高表达能力。
  6. 口语练习:十分钟描述一件事,或一个东西。
  7. 写作练习:写文章,发表到italki上校对。
1…456
Eli Wang

Eli Wang

59 日志
3 分类
22 标签
GitHub 豆瓣
© 2015 - 2018 Eli Wang
由 Hexo 强力驱动
主题 - NexT.Pisces