mirror of
https://github.com/phodal/github
synced 2026-05-24 01:28:30 +00:00
update
This commit is contained in:
parent
af5238451a
commit
ecabb55d1f
10 changed files with 387 additions and 1861 deletions
2
Makefile
2
Makefile
|
|
@ -46,7 +46,7 @@ pdf: markdown
|
||||||
--normalize \
|
--normalize \
|
||||||
--smart \
|
--smart \
|
||||||
--toc \
|
--toc \
|
||||||
--latex-engine=xelatex
|
--latex-engine=`which xelatex`
|
||||||
|
|
||||||
mobi: epub
|
mobi: epub
|
||||||
# Symlink bin: ln -s /path/to/kindlegen /usr/local/bin
|
# Symlink bin: ln -s /path/to/kindlegen /usr/local/bin
|
||||||
|
|
|
||||||
|
|
@ -65,23 +65,25 @@ jQuery[^jQuery]在发布版本``2.1.3``,一共有152个commit。我们可以
|
||||||
|
|
||||||
通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?
|
通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?
|
||||||
|
|
||||||
it("specifying response when you need it", function (done) {
|
```javascript
|
||||||
var doneFn = jasmine.createSpy("success");
|
it("specifying response when you need it", function (done) {
|
||||||
|
var doneFn = jasmine.createSpy("success");
|
||||||
|
|
||||||
lettuce.get('/some/cool/url', function (result) {
|
lettuce.get('/some/cool/url', function (result) {
|
||||||
expect(result).toEqual("awesome response");
|
expect(result).toEqual("awesome response");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
|
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
|
||||||
expect(doneFn).not.toHaveBeenCalled();
|
expect(doneFn).not.toHaveBeenCalled();
|
||||||
|
|
||||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||||
"status": 200,
|
"status": 200,
|
||||||
"contentType": 'text/plain',
|
"contentType": 'text/plain',
|
||||||
"responseText": 'awesome response'
|
"responseText": 'awesome response'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -97,17 +99,19 @@ jQuery[^jQuery]在发布版本``2.1.3``,一共有152个commit。我们可以
|
||||||
|
|
||||||
也许你已经知道了``Selenium``、``Jasmine``、``Cucumber``等等的框架,看到过类似于下面的测试
|
也许你已经知道了``Selenium``、``Jasmine``、``Cucumber``等等的框架,看到过类似于下面的测试
|
||||||
|
|
||||||
Ajax
|
```
|
||||||
✓ specifying response when you need it
|
Ajax
|
||||||
✓ specifying html when you need it
|
✓ specifying response when you need it
|
||||||
✓ should be post to some where
|
✓ specifying html when you need it
|
||||||
Class
|
✓ should be post to some where
|
||||||
✓ respects instanceof
|
Class
|
||||||
✓ inherits methods (also super)
|
✓ respects instanceof
|
||||||
✓ extend methods
|
✓ inherits methods (also super)
|
||||||
Effect
|
✓ extend methods
|
||||||
✓ should be able fadein elements
|
Effect
|
||||||
✓ should be able fadeout elements
|
✓ should be able fadein elements
|
||||||
|
✓ should be able fadeout elements
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -123,17 +127,18 @@ lettuce.js | 98.58% (209 / 212)| 82.98%(78 / 94) | 100.00% (54 / 54) | 98.58% (2
|
||||||
|
|
||||||
虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的``.travis.yml``示例:
|
虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的``.travis.yml``示例:
|
||||||
|
|
||||||
language: node_js
|
```yml
|
||||||
node_js:
|
language: node_js
|
||||||
- "0.10"
|
node_js:
|
||||||
|
- "0.10"
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
before_install: npm install -g grunt-cli
|
|
||||||
install: npm install
|
|
||||||
after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc411680e8f4569936ac8ffbb0ab codeclimate < coverage/lcov.info
|
|
||||||
|
|
||||||
|
before_install: npm install -g grunt-cli
|
||||||
|
install: npm install
|
||||||
|
after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc411680e8f4569936ac8ffbb0ab codeclimate < coverage/lcov.info
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -153,28 +158,30 @@ CI对于一个开发者在不同城市开发同一项目上来说是很重要的
|
||||||
|
|
||||||
先看看上面的ajax类:
|
先看看上面的ajax类:
|
||||||
|
|
||||||
Lettuce.get = function (url, callback) {
|
```javascript
|
||||||
Lettuce.send(url, 'GET', callback);
|
Lettuce.get = function (url, callback) {
|
||||||
};
|
Lettuce.send(url, 'GET', callback);
|
||||||
|
};
|
||||||
|
|
||||||
Lettuce.send = function (url, method, callback, data) {
|
Lettuce.send = function (url, method, callback, data) {
|
||||||
data = data || null;
|
data = data || null;
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
if (callback instanceof Function) {
|
if (callback instanceof Function) {
|
||||||
request.onreadystatechange = function () {
|
request.onreadystatechange = function () {
|
||||||
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
||||||
callback(request.responseText);
|
callback(request.responseText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
request.open(method, url, true);
|
request.open(method, url, true);
|
||||||
if (data instanceof Object) {
|
if (data instanceof Object) {
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
request.setRequestHeader('Content-Type', 'application/json');
|
request.setRequestHeader('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
request.send(data);
|
request.send(data);
|
||||||
};
|
};
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,24 @@
|
||||||
|
|
||||||
如果是第一次使用Git,你需要设置署名和邮箱:
|
如果是第一次使用Git,你需要设置署名和邮箱:
|
||||||
|
|
||||||
$ git config --global user.name "用户名"
|
```
|
||||||
$ git config --global user.email "电子邮箱"
|
$ git config --global user.name "用户名"
|
||||||
|
$ git config --global user.email "电子邮箱"
|
||||||
|
```
|
||||||
|
|
||||||
将代码仓库clone到本地,其实就是将代码复制到你的机器里,并交由Git来管理:
|
将代码仓库clone到本地,其实就是将代码复制到你的机器里,并交由Git来管理:
|
||||||
|
|
||||||
$ git clone git@github.com:someone/symfony-docs-chs.git
|
```
|
||||||
|
$ git clone git@github.com:someone/symfony-docs-chs.git
|
||||||
|
```
|
||||||
|
|
||||||
你可以修改复制到本地的代码了(symfony-docs-chs项目里都是rst格式的文档)。当你觉得完成了一定的工作量,想做个阶段性的提交:
|
你可以修改复制到本地的代码了(symfony-docs-chs项目里都是rst格式的文档)。当你觉得完成了一定的工作量,想做个阶段性的提交:
|
||||||
|
|
||||||
向这个本地的代码仓库添加当前目录的所有改动:
|
向这个本地的代码仓库添加当前目录的所有改动:
|
||||||
|
|
||||||
$ git add .
|
```
|
||||||
|
$ git add .
|
||||||
|
```
|
||||||
|
|
||||||
或者只是添加某个文件:
|
或者只是添加某个文件:
|
||||||
|
|
||||||
|
|
@ -49,16 +55,19 @@
|
||||||
|
|
||||||
> …or create a new repository on the command line
|
> …or create a new repository on the command line
|
||||||
|
|
||||||
echo "# github-roam" >> README.md
|
```
|
||||||
git init
|
echo "# github-roam" >> README.md
|
||||||
git add README.md
|
git init
|
||||||
git commit -m "first commit"
|
git add README.md
|
||||||
git remote add origin git@github.com:phodal/github-roam.git
|
git commit -m "first commit"
|
||||||
git push -u origin master
|
git remote add origin git@github.com:phodal/github-roam.git
|
||||||
|
git push -u origin master
|
||||||
|
```
|
||||||
|
|
||||||
> …or push an existing repository from the command line
|
> …or push an existing repository from the command line
|
||||||
|
|
||||||
git remote add origin git@github.com:phodal/github-roam.git
|
```
|
||||||
git push -u origin master
|
git remote add origin git@github.com:phodal/github-roam.git
|
||||||
|
git push -u origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
|
@ -19,7 +19,7 @@ $time python handle.py
|
||||||
sys 0m0.618s
|
sys 0m0.618s
|
||||||
```
|
```
|
||||||
|
|
||||||
##line_profiler python
|
###line_profiler python
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future" easy_install line_profiler
|
sudo ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future" easy_install line_profiler
|
||||||
|
|
@ -82,7 +82,7 @@ Line # Hits Time Per Hit % Time Line Contents
|
||||||
|
|
||||||
于是我们就发现我们的瓶颈就是从读取``created_at``,即创建时间。。。以及解析json,反而不是我们关心的IO,果然``readline``很强大。
|
于是我们就发现我们的瓶颈就是从读取``created_at``,即创建时间。。。以及解析json,反而不是我们关心的IO,果然``readline``很强大。
|
||||||
|
|
||||||
##memory_profiler
|
###memory_profiler
|
||||||
|
|
||||||
首先我们需要install memory_profiler:
|
首先我们需要install memory_profiler:
|
||||||
|
|
||||||
|
|
@ -121,7 +121,7 @@ Line # Mem usage Increment Line Contents
|
||||||
27 return datacount, dataarray
|
27 return datacount, dataarray
|
||||||
```
|
```
|
||||||
|
|
||||||
##objgraph python
|
###objgraph python
|
||||||
|
|
||||||
安装objgraph
|
安装objgraph
|
||||||
|
|
||||||
|
|
|
||||||
BIN
github-roam.epub
BIN
github-roam.epub
Binary file not shown.
327
github-roam.md
327
github-roam.md
|
|
@ -144,23 +144,25 @@ jQuery[^jQuery]在发布版本``2.1.3``,一共有152个commit。我们可以
|
||||||
|
|
||||||
通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?
|
通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?
|
||||||
|
|
||||||
it("specifying response when you need it", function (done) {
|
```javascript
|
||||||
var doneFn = jasmine.createSpy("success");
|
it("specifying response when you need it", function (done) {
|
||||||
|
var doneFn = jasmine.createSpy("success");
|
||||||
|
|
||||||
lettuce.get('/some/cool/url', function (result) {
|
lettuce.get('/some/cool/url', function (result) {
|
||||||
expect(result).toEqual("awesome response");
|
expect(result).toEqual("awesome response");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
|
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
|
||||||
expect(doneFn).not.toHaveBeenCalled();
|
expect(doneFn).not.toHaveBeenCalled();
|
||||||
|
|
||||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||||
"status": 200,
|
"status": 200,
|
||||||
"contentType": 'text/plain',
|
"contentType": 'text/plain',
|
||||||
"responseText": 'awesome response'
|
"responseText": 'awesome response'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -176,17 +178,19 @@ jQuery[^jQuery]在发布版本``2.1.3``,一共有152个commit。我们可以
|
||||||
|
|
||||||
也许你已经知道了``Selenium``、``Jasmine``、``Cucumber``等等的框架,看到过类似于下面的测试
|
也许你已经知道了``Selenium``、``Jasmine``、``Cucumber``等等的框架,看到过类似于下面的测试
|
||||||
|
|
||||||
Ajax
|
```
|
||||||
✓ specifying response when you need it
|
Ajax
|
||||||
✓ specifying html when you need it
|
✓ specifying response when you need it
|
||||||
✓ should be post to some where
|
✓ specifying html when you need it
|
||||||
Class
|
✓ should be post to some where
|
||||||
✓ respects instanceof
|
Class
|
||||||
✓ inherits methods (also super)
|
✓ respects instanceof
|
||||||
✓ extend methods
|
✓ inherits methods (also super)
|
||||||
Effect
|
✓ extend methods
|
||||||
✓ should be able fadein elements
|
Effect
|
||||||
✓ should be able fadeout elements
|
✓ should be able fadein elements
|
||||||
|
✓ should be able fadeout elements
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -202,17 +206,18 @@ lettuce.js | 98.58% (209 / 212)| 82.98%(78 / 94) | 100.00% (54 / 54) | 98.58% (2
|
||||||
|
|
||||||
虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的``.travis.yml``示例:
|
虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的``.travis.yml``示例:
|
||||||
|
|
||||||
language: node_js
|
```yml
|
||||||
node_js:
|
language: node_js
|
||||||
- "0.10"
|
node_js:
|
||||||
|
- "0.10"
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
before_install: npm install -g grunt-cli
|
|
||||||
install: npm install
|
|
||||||
after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc411680e8f4569936ac8ffbb0ab codeclimate < coverage/lcov.info
|
|
||||||
|
|
||||||
|
before_install: npm install -g grunt-cli
|
||||||
|
install: npm install
|
||||||
|
after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc411680e8f4569936ac8ffbb0ab codeclimate < coverage/lcov.info
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -232,28 +237,30 @@ CI对于一个开发者在不同城市开发同一项目上来说是很重要的
|
||||||
|
|
||||||
先看看上面的ajax类:
|
先看看上面的ajax类:
|
||||||
|
|
||||||
Lettuce.get = function (url, callback) {
|
```javascript
|
||||||
Lettuce.send(url, 'GET', callback);
|
Lettuce.get = function (url, callback) {
|
||||||
};
|
Lettuce.send(url, 'GET', callback);
|
||||||
|
};
|
||||||
|
|
||||||
Lettuce.send = function (url, method, callback, data) {
|
Lettuce.send = function (url, method, callback, data) {
|
||||||
data = data || null;
|
data = data || null;
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
if (callback instanceof Function) {
|
if (callback instanceof Function) {
|
||||||
request.onreadystatechange = function () {
|
request.onreadystatechange = function () {
|
||||||
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
||||||
callback(request.responseText);
|
callback(request.responseText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
request.open(method, url, true);
|
request.open(method, url, true);
|
||||||
if (data instanceof Object) {
|
if (data instanceof Object) {
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
request.setRequestHeader('Content-Type', 'application/json');
|
request.setRequestHeader('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
request.send(data);
|
request.send(data);
|
||||||
};
|
};
|
||||||
|
```
|
||||||
|
|
||||||
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
代码来源: [https://github.com/phodal/lettuce](https://github.com/phodal/lettuce)
|
||||||
|
|
||||||
|
|
@ -301,18 +308,24 @@ CI对于一个开发者在不同城市开发同一项目上来说是很重要的
|
||||||
|
|
||||||
如果是第一次使用Git,你需要设置署名和邮箱:
|
如果是第一次使用Git,你需要设置署名和邮箱:
|
||||||
|
|
||||||
$ git config --global user.name "用户名"
|
```
|
||||||
$ git config --global user.email "电子邮箱"
|
$ git config --global user.name "用户名"
|
||||||
|
$ git config --global user.email "电子邮箱"
|
||||||
|
```
|
||||||
|
|
||||||
将代码仓库clone到本地,其实就是将代码复制到你的机器里,并交由Git来管理:
|
将代码仓库clone到本地,其实就是将代码复制到你的机器里,并交由Git来管理:
|
||||||
|
|
||||||
$ git clone git@github.com:someone/symfony-docs-chs.git
|
```
|
||||||
|
$ git clone git@github.com:someone/symfony-docs-chs.git
|
||||||
|
```
|
||||||
|
|
||||||
你可以修改复制到本地的代码了(symfony-docs-chs项目里都是rst格式的文档)。当你觉得完成了一定的工作量,想做个阶段性的提交:
|
你可以修改复制到本地的代码了(symfony-docs-chs项目里都是rst格式的文档)。当你觉得完成了一定的工作量,想做个阶段性的提交:
|
||||||
|
|
||||||
向这个本地的代码仓库添加当前目录的所有改动:
|
向这个本地的代码仓库添加当前目录的所有改动:
|
||||||
|
|
||||||
$ git add .
|
```
|
||||||
|
$ git add .
|
||||||
|
```
|
||||||
|
|
||||||
或者只是添加某个文件:
|
或者只是添加某个文件:
|
||||||
|
|
||||||
|
|
@ -326,18 +339,21 @@ CI对于一个开发者在不同城市开发同一项目上来说是很重要的
|
||||||
|
|
||||||
> …or create a new repository on the command line
|
> …or create a new repository on the command line
|
||||||
|
|
||||||
echo "# github-roam" >> README.md
|
```
|
||||||
git init
|
echo "# github-roam" >> README.md
|
||||||
git add README.md
|
git init
|
||||||
git commit -m "first commit"
|
git add README.md
|
||||||
git remote add origin git@github.com:phodal/github-roam.git
|
git commit -m "first commit"
|
||||||
git push -u origin master
|
git remote add origin git@github.com:phodal/github-roam.git
|
||||||
|
git push -u origin master
|
||||||
|
```
|
||||||
|
|
||||||
> …or push an existing repository from the command line
|
> …or push an existing repository from the command line
|
||||||
|
|
||||||
git remote add origin git@github.com:phodal/github-roam.git
|
```
|
||||||
git push -u origin master
|
git remote add origin git@github.com:phodal/github-roam.git
|
||||||
|
git push -u origin master
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#Github项目分析一
|
#Github项目分析一
|
||||||
|
|
@ -618,7 +634,7 @@ $time python handle.py
|
||||||
sys 0m0.618s
|
sys 0m0.618s
|
||||||
```
|
```
|
||||||
|
|
||||||
##line_profiler python
|
###line_profiler python
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future" easy_install line_profiler
|
sudo ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future" easy_install line_profiler
|
||||||
|
|
@ -681,7 +697,7 @@ Line # Hits Time Per Hit % Time Line Contents
|
||||||
|
|
||||||
于是我们就发现我们的瓶颈就是从读取``created_at``,即创建时间。。。以及解析json,反而不是我们关心的IO,果然``readline``很强大。
|
于是我们就发现我们的瓶颈就是从读取``created_at``,即创建时间。。。以及解析json,反而不是我们关心的IO,果然``readline``很强大。
|
||||||
|
|
||||||
##memory_profiler
|
###memory_profiler
|
||||||
|
|
||||||
首先我们需要install memory_profiler:
|
首先我们需要install memory_profiler:
|
||||||
|
|
||||||
|
|
@ -720,7 +736,7 @@ Line # Mem usage Increment Line Contents
|
||||||
27 return datacount, dataarray
|
27 return datacount, dataarray
|
||||||
```
|
```
|
||||||
|
|
||||||
##objgraph python
|
###objgraph python
|
||||||
|
|
||||||
安装objgraph
|
安装objgraph
|
||||||
|
|
||||||
|
|
@ -1518,7 +1534,7 @@ C | 2
|
||||||
|
|
||||||
最近萌发了一个想法写游戏引擎,之前想着做一个JavaScript前端框架。看看,这个思路是怎么来的。
|
最近萌发了一个想法写游戏引擎,之前想着做一个JavaScript前端框架。看看,这个思路是怎么来的。
|
||||||
|
|
||||||
##一、[Lettuce](https://github.com/phodal/lettuce)构建过程
|
##[Lettuce](https://github.com/phodal/lettuce)构建过程
|
||||||
|
|
||||||
> Lettuce是一个简约的移动开发框架。
|
> Lettuce是一个简约的移动开发框架。
|
||||||
|
|
||||||
|
|
@ -1579,66 +1595,70 @@ C | 2
|
||||||
|
|
||||||
但是显然,他们都太重了。事实上,对于一个库来说,80%的人只需要其中20%的代码。于是,找到了[https://github.com/stackp/promisejs](https://github.com/stackp/promisejs),看了看用法,这就是我们需要的功能:
|
但是显然,他们都太重了。事实上,对于一个库来说,80%的人只需要其中20%的代码。于是,找到了[https://github.com/stackp/promisejs](https://github.com/stackp/promisejs),看了看用法,这就是我们需要的功能:
|
||||||
|
|
||||||
function late(n) {
|
```javascript
|
||||||
var p = new promise.Promise();
|
function late(n) {
|
||||||
setTimeout(function() {
|
var p = new promise.Promise();
|
||||||
p.done(null, n);
|
setTimeout(function() {
|
||||||
}, n);
|
p.done(null, n);
|
||||||
return p;
|
}, n);
|
||||||
}
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
late(100).then(
|
late(100).then(
|
||||||
function(err, n) {
|
function(err, n) {
|
||||||
return late(n + 200);
|
return late(n + 200);
|
||||||
}
|
}
|
||||||
).then(
|
).then(
|
||||||
function(err, n) {
|
function(err, n) {
|
||||||
return late(n + 300);
|
return late(n + 300);
|
||||||
}
|
}
|
||||||
).then(
|
).then(
|
||||||
function(err, n) {
|
function(err, n) {
|
||||||
return late(n + 400);
|
return late(n + 400);
|
||||||
}
|
}
|
||||||
).then(
|
).then(
|
||||||
function(err, n) {
|
function(err, n) {
|
||||||
alert(n);
|
alert(n);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
```
|
||||||
|
|
||||||
接着打开看看Promise对象,有我们需要的功能,但是又有一些功能超出我的需求。接着把自己不需要的需求去掉,这里函数最后就变成了
|
接着打开看看Promise对象,有我们需要的功能,但是又有一些功能超出我的需求。接着把自己不需要的需求去掉,这里函数最后就变成了
|
||||||
|
|
||||||
function Promise() {
|
```javascript
|
||||||
this._callbacks = [];
|
function Promise() {
|
||||||
|
this._callbacks = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.prototype.then = function(func, context) {
|
||||||
|
var p;
|
||||||
|
if (this._isdone) {
|
||||||
|
p = func.apply(context, this.result);
|
||||||
|
} else {
|
||||||
|
p = new Promise();
|
||||||
|
this._callbacks.push(function () {
|
||||||
|
var res = func.apply(context, arguments);
|
||||||
|
if (res && typeof res.then === 'function') {
|
||||||
|
res.then(p.done, p);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
return p;
|
||||||
|
};
|
||||||
|
|
||||||
Promise.prototype.then = function(func, context) {
|
Promise.prototype.done = function() {
|
||||||
var p;
|
this.result = arguments;
|
||||||
if (this._isdone) {
|
this._isdone = true;
|
||||||
p = func.apply(context, this.result);
|
for (var i = 0; i < this._callbacks.length; i++) {
|
||||||
} else {
|
this._callbacks[i].apply(null, arguments);
|
||||||
p = new Promise();
|
}
|
||||||
this._callbacks.push(function () {
|
this._callbacks = [];
|
||||||
var res = func.apply(context, arguments);
|
};
|
||||||
if (res && typeof res.then === 'function') {
|
|
||||||
res.then(p.done, p);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
};
|
|
||||||
|
|
||||||
Promise.prototype.done = function() {
|
var promise = {
|
||||||
this.result = arguments;
|
Promise: Promise
|
||||||
this._isdone = true;
|
};
|
||||||
for (var i = 0; i < this._callbacks.length; i++) {
|
```
|
||||||
this._callbacks[i].apply(null, arguments);
|
|
||||||
}
|
|
||||||
this._callbacks = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
var promise = {
|
|
||||||
Promise: Promise
|
|
||||||
};
|
|
||||||
|
|
||||||
需要注意的是: ``License``,不同的软件有不同的License,如MIT、GPL等等。最好能在遵循协议的情况下,使用别人的代码。
|
需要注意的是: ``License``,不同的软件有不同的License,如MIT、GPL等等。最好能在遵循协议的情况下,使用别人的代码。
|
||||||
|
|
||||||
|
|
@ -1646,34 +1666,37 @@ C | 2
|
||||||
|
|
||||||
由于,现有的一些Ajax库都比较,最后只好参照着别人的代码自己实现。
|
由于,现有的一些Ajax库都比较,最后只好参照着别人的代码自己实现。
|
||||||
|
|
||||||
Lettuce.get = function (url, callback) {
|
```javascript
|
||||||
Lettuce.send(url, 'GET', callback);
|
Lettuce.get = function (url, callback) {
|
||||||
};
|
Lettuce.send(url, 'GET', callback);
|
||||||
|
};
|
||||||
|
|
||||||
Lettuce.load = function (url, callback) {
|
Lettuce.load = function (url, callback) {
|
||||||
Lettuce.send(url, 'GET', callback);
|
Lettuce.send(url, 'GET', callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Lettuce.post = function (url, data, callback) {
|
Lettuce.post = function (url, data, callback) {
|
||||||
Lettuce.send(url, 'POST', callback, data);
|
Lettuce.send(url, 'POST', callback, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Lettuce.send = function (url, method, callback, data) {
|
||||||
|
data = data || null;
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
if (callback instanceof Function) {
|
||||||
|
request.onreadystatechange = function () {
|
||||||
|
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
||||||
|
callback(request.responseText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
request.open(method, url, true);
|
||||||
|
if (data instanceof Object) {
|
||||||
|
data = JSON.stringify(data);
|
||||||
|
request.setRequestHeader('Content-Type', 'application/json');
|
||||||
|
}
|
||||||
|
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
|
request.send(data);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Lettuce.send = function (url, method, callback, data) {
|
|
||||||
data = data || null;
|
|
||||||
var request = new XMLHttpRequest();
|
|
||||||
if (callback instanceof Function) {
|
|
||||||
request.onreadystatechange = function () {
|
|
||||||
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
|
||||||
callback(request.responseText);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
request.open(method, url, true);
|
|
||||||
if (data instanceof Object) {
|
|
||||||
data = JSON.stringify(data);
|
|
||||||
request.setRequestHeader('Content-Type', 'application/json');
|
|
||||||
}
|
|
||||||
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
|
||||||
request.send(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
|
||||||
BIN
github-roam.mobi
BIN
github-roam.mobi
Binary file not shown.
BIN
github-roam.pdf
BIN
github-roam.pdf
Binary file not shown.
1513
github-roam.rtf
1513
github-roam.rtf
File diff suppressed because one or more lines are too long
248
index.html
248
index.html
|
|
@ -104,10 +104,11 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
|
||||||
</ul></li>
|
</ul></li>
|
||||||
</ul></li>
|
</ul></li>
|
||||||
<li><a href="#github项目分析二">Github项目分析二</a><ul>
|
<li><a href="#github项目分析二">Github项目分析二</a><ul>
|
||||||
<li><a href="#time-python分析">Time Python分析</a></li>
|
<li><a href="#time-python分析">Time Python分析</a><ul>
|
||||||
<li><a href="#line_profiler-python">line_profiler python</a></li>
|
<li><a href="#line_profiler-python">line_profiler python</a></li>
|
||||||
<li><a href="#memory_profiler">memory_profiler</a></li>
|
<li><a href="#memory_profiler">memory_profiler</a></li>
|
||||||
<li><a href="#objgraph-python">objgraph python</a></li>
|
<li><a href="#objgraph-python">objgraph python</a></li>
|
||||||
|
</ul></li>
|
||||||
<li><a href="#python-sqlite3-查询数据">python SQLite3 查询数据</a><ul>
|
<li><a href="#python-sqlite3-查询数据">python SQLite3 查询数据</a><ul>
|
||||||
<li><a href="#数据导入">数据导入</a></li>
|
<li><a href="#数据导入">数据导入</a></li>
|
||||||
</ul></li>
|
</ul></li>
|
||||||
|
|
@ -145,7 +146,7 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
|
||||||
</ul></li>
|
</ul></li>
|
||||||
</ul></li>
|
</ul></li>
|
||||||
<li><a href="#如何在github寻找灵感fork">如何在Github“寻找灵感(fork)”</a><ul>
|
<li><a href="#如何在github寻找灵感fork">如何在Github“寻找灵感(fork)”</a><ul>
|
||||||
<li><a href="#一lettuce构建过程">一、<a href="https://github.com/phodal/lettuce">Lettuce</a>构建过程</a><ul>
|
<li><a href="#lettuce构建过程"><a href="https://github.com/phodal/lettuce">Lettuce</a>构建过程</a><ul>
|
||||||
<li><a href="#需求">需求</a></li>
|
<li><a href="#需求">需求</a></li>
|
||||||
<li><a href="#计划">计划</a></li>
|
<li><a href="#计划">计划</a></li>
|
||||||
<li><a href="#实现第一个需求">实现第一个需求</a></li>
|
<li><a href="#实现第一个需求">实现第一个需求</a></li>
|
||||||
|
|
@ -259,23 +260,23 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
|
||||||
<p>当只有一个人的时候,你只需要明确知道自己想要什么就够了。我们还需要的是CI、测试,以来提升代码的质量。</p>
|
<p>当只有一个人的时候,你只需要明确知道自己想要什么就够了。我们还需要的是CI、测试,以来提升代码的质量。</p>
|
||||||
<h3 id="测试">测试</h3>
|
<h3 id="测试">测试</h3>
|
||||||
<p>通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?</p>
|
<p>通常我们都会找Document,如果没有的话,你会找什么?看源代码,还是看测试?</p>
|
||||||
<pre><code>it("specifying response when you need it", function (done) {
|
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="at">it</span>(<span class="st">"specifying response when you need it"</span><span class="op">,</span> <span class="kw">function</span> (done) <span class="op">{</span>
|
||||||
var doneFn = jasmine.createSpy("success");
|
<span class="kw">var</span> doneFn <span class="op">=</span> <span class="va">jasmine</span>.<span class="at">createSpy</span>(<span class="st">"success"</span>)<span class="op">;</span>
|
||||||
|
|
||||||
lettuce.get('/some/cool/url', function (result) {
|
<span class="va">lettuce</span>.<span class="at">get</span>(<span class="st">'/some/cool/url'</span><span class="op">,</span> <span class="kw">function</span> (result) <span class="op">{</span>
|
||||||
expect(result).toEqual("awesome response");
|
<span class="at">expect</span>(result).<span class="at">toEqual</span>(<span class="st">"awesome response"</span>)<span class="op">;</span>
|
||||||
done();
|
<span class="at">done</span>()<span class="op">;</span>
|
||||||
});
|
<span class="op">}</span>)<span class="op">;</span>
|
||||||
|
|
||||||
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
|
<span class="at">expect</span>(<span class="va">jasmine</span>.<span class="va">Ajax</span>.<span class="va">requests</span>.<span class="at">mostRecent</span>().<span class="at">url</span>).<span class="at">toBe</span>(<span class="st">'/some/cool/url'</span>)<span class="op">;</span>
|
||||||
expect(doneFn).not.toHaveBeenCalled();
|
<span class="at">expect</span>(doneFn).<span class="va">not</span>.<span class="at">toHaveBeenCalled</span>()<span class="op">;</span>
|
||||||
|
|
||||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
<span class="va">jasmine</span>.<span class="va">Ajax</span>.<span class="va">requests</span>.<span class="at">mostRecent</span>().<span class="at">respondWith</span>(<span class="op">{</span>
|
||||||
"status": 200,
|
<span class="st">"status"</span><span class="op">:</span> <span class="dv">200</span><span class="op">,</span>
|
||||||
"contentType": 'text/plain',
|
<span class="st">"contentType"</span><span class="op">:</span> <span class="st">'text/plain'</span><span class="op">,</span>
|
||||||
"responseText": 'awesome response'
|
<span class="st">"responseText"</span><span class="op">:</span> <span class="st">'awesome response'</span>
|
||||||
});
|
<span class="op">}</span>)<span class="op">;</span>
|
||||||
});</code></pre>
|
<span class="op">}</span>)<span class="op">;</span></code></pre></div>
|
||||||
<p>代码来源: <a href="https://github.com/phodal/lettuce" class="uri">https://github.com/phodal/lettuce</a></p>
|
<p>代码来源: <a href="https://github.com/phodal/lettuce" class="uri">https://github.com/phodal/lettuce</a></p>
|
||||||
<p>上面的测试用例,清清楚楚地写明了用法,虽然写得有点扯。</p>
|
<p>上面的测试用例,清清楚楚地写明了用法,虽然写得有点扯。</p>
|
||||||
<p>等等,测试是用来干什么的。那么,先说说我为什么会想去写测试吧:</p>
|
<p>等等,测试是用来干什么的。那么,先说说我为什么会想去写测试吧:</p>
|
||||||
|
|
@ -322,7 +323,7 @@ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Inf
|
||||||
<p>本地测试都通过了,于是我们添加了<code>Travis-CI</code>来跑我们的测试</p>
|
<p>本地测试都通过了,于是我们添加了<code>Travis-CI</code>来跑我们的测试</p>
|
||||||
<h3 id="ci">CI</h3>
|
<h3 id="ci">CI</h3>
|
||||||
<p>虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的<code>.travis.yml</code>示例:</p>
|
<p>虽然node.js不算是一门语言,但是因为我们用的node,下面的是一个简单的<code>.travis.yml</code>示例:</p>
|
||||||
<pre><code>language: node_js
|
<pre class="yml"><code>language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "0.10"
|
- "0.10"
|
||||||
|
|
||||||
|
|
@ -344,28 +345,28 @@ after_success: CODECLIMATE_REPO_TOKEN=321480822fc37deb0de70a11931b4cb6a2a3cc4116
|
||||||
</ul>
|
</ul>
|
||||||
<p><code>Code Climate</code>是一个与github集成的工具,我们不仅仅可以看到测试覆盖率,还有代码质量。</p>
|
<p><code>Code Climate</code>是一个与github集成的工具,我们不仅仅可以看到测试覆盖率,还有代码质量。</p>
|
||||||
<p>先看看上面的ajax类:</p>
|
<p>先看看上面的ajax类:</p>
|
||||||
<pre><code>Lettuce.get = function (url, callback) {
|
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">Lettuce</span>.<span class="at">get</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> callback) <span class="op">{</span>
|
||||||
Lettuce.send(url, 'GET', callback);
|
<span class="va">Lettuce</span>.<span class="at">send</span>(url<span class="op">,</span> <span class="st">'GET'</span><span class="op">,</span> callback)<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
Lettuce.send = function (url, method, callback, data) {
|
<span class="va">Lettuce</span>.<span class="at">send</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> method<span class="op">,</span> callback<span class="op">,</span> data) <span class="op">{</span>
|
||||||
data = data || null;
|
data <span class="op">=</span> data <span class="op">||</span> <span class="kw">null</span><span class="op">;</span>
|
||||||
var request = new XMLHttpRequest();
|
<span class="kw">var</span> request <span class="op">=</span> <span class="kw">new</span> <span class="at">XMLHttpRequest</span>()<span class="op">;</span>
|
||||||
if (callback instanceof Function) {
|
<span class="cf">if</span> (callback <span class="kw">instanceof</span> Function) <span class="op">{</span>
|
||||||
request.onreadystatechange = function () {
|
<span class="va">request</span>.<span class="at">onreadystatechange</span> <span class="op">=</span> <span class="kw">function</span> () <span class="op">{</span>
|
||||||
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
<span class="cf">if</span> (<span class="va">request</span>.<span class="at">readyState</span> <span class="op">===</span> <span class="dv">4</span> <span class="op">&&</span> (<span class="va">request</span>.<span class="at">status</span> <span class="op">===</span> <span class="dv">200</span> <span class="op">||</span> <span class="va">request</span>.<span class="at">status</span> <span class="op">===</span> <span class="dv">0</span>)) <span class="op">{</span>
|
||||||
callback(request.responseText);
|
<span class="at">callback</span>(<span class="va">request</span>.<span class="at">responseText</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
request.open(method, url, true);
|
<span class="va">request</span>.<span class="at">open</span>(method<span class="op">,</span> url<span class="op">,</span> <span class="kw">true</span>)<span class="op">;</span>
|
||||||
if (data instanceof Object) {
|
<span class="cf">if</span> (data <span class="kw">instanceof</span> Object) <span class="op">{</span>
|
||||||
data = JSON.stringify(data);
|
data <span class="op">=</span> <span class="va">JSON</span>.<span class="at">stringify</span>(data)<span class="op">;</span>
|
||||||
request.setRequestHeader('Content-Type', 'application/json');
|
<span class="va">request</span>.<span class="at">setRequestHeader</span>(<span class="st">'Content-Type'</span><span class="op">,</span> <span class="st">'application/json'</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
<span class="va">request</span>.<span class="at">setRequestHeader</span>(<span class="st">'X-Requested-With'</span><span class="op">,</span> <span class="st">'XMLHttpRequest'</span>)<span class="op">;</span>
|
||||||
request.send(data);
|
<span class="va">request</span>.<span class="at">send</span>(data)<span class="op">;</span>
|
||||||
};</code></pre>
|
<span class="op">};</span></code></pre></div>
|
||||||
<p>代码来源: <a href="https://github.com/phodal/lettuce" class="uri">https://github.com/phodal/lettuce</a></p>
|
<p>代码来源: <a href="https://github.com/phodal/lettuce" class="uri">https://github.com/phodal/lettuce</a></p>
|
||||||
<p>在<a href="https://codeclimate.com/github/phodal/lettuce/src/ajax.js">Code Climate</a>在出现了一堆问题</p>
|
<p>在<a href="https://codeclimate.com/github/phodal/lettuce/src/ajax.js">Code Climate</a>在出现了一堆问题</p>
|
||||||
<ul>
|
<ul>
|
||||||
|
|
@ -431,8 +432,7 @@ git push -u origin master</code></pre>
|
||||||
<p>…or push an existing repository from the command line</p>
|
<p>…or push an existing repository from the command line</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<pre><code>git remote add origin git@github.com:phodal/github-roam.git
|
<pre><code>git remote add origin git@github.com:phodal/github-roam.git
|
||||||
git push -u origin master
|
git push -u origin master</code></pre>
|
||||||
</code></pre>
|
|
||||||
<h1 id="github项目分析一">Github项目分析一</h1>
|
<h1 id="github项目分析一">Github项目分析一</h1>
|
||||||
<h2 id="生成图表">生成图表</h2>
|
<h2 id="生成图表">生成图表</h2>
|
||||||
<p>如何分析用户的数据是一个有趣的问题,特别是当我们有大量的数据的时候。除了<code>matlab</code>,我们还可以用<code>numpy</code>+<code>matplotlib</code></p>
|
<p>如何分析用户的数据是一个有趣的问题,特别是当我们有大量的数据的时候。除了<code>matlab</code>,我们还可以用<code>numpy</code>+<code>matplotlib</code></p>
|
||||||
|
|
@ -634,7 +634,7 @@ draw_date(<span class="st">"data/2014-01-01-0.json"</span>)</code></pr
|
||||||
<pre><code> real 0m43.411s
|
<pre><code> real 0m43.411s
|
||||||
user 0m39.226s
|
user 0m39.226s
|
||||||
sys 0m0.618s</code></pre>
|
sys 0m0.618s</code></pre>
|
||||||
<h2 id="line_profiler-python">line_profiler python</h2>
|
<h3 id="line_profiler-python">line_profiler python</h3>
|
||||||
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> ARCHFLAGS=<span class="st">"-Wno-error=unused-command-line-argument-hard-error-in-future"</span> easy_install line_profiler</code></pre></div>
|
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> ARCHFLAGS=<span class="st">"-Wno-error=unused-command-line-argument-hard-error-in-future"</span> easy_install line_profiler</code></pre></div>
|
||||||
<p>然后在我们的<code>parse_data.py</code>的<code>handle_json</code>前面加上<code>@profile</code></p>
|
<p>然后在我们的<code>parse_data.py</code>的<code>handle_json</code>前面加上<code>@profile</code></p>
|
||||||
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="at">@profile</span>
|
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="at">@profile</span>
|
||||||
|
|
@ -680,7 +680,7 @@ Line # Hits Time Per Hit % Time Line Contents
|
||||||
28 19 349 18.4 0.0 f.close()
|
28 19 349 18.4 0.0 f.close()
|
||||||
29 19 20 1.1 0.0 return datacount, dataarray</code></pre>
|
29 19 20 1.1 0.0 return datacount, dataarray</code></pre>
|
||||||
<p>于是我们就发现我们的瓶颈就是从读取<code>created_at</code>,即创建时间。。。以及解析json,反而不是我们关心的IO,果然<code>readline</code>很强大。</p>
|
<p>于是我们就发现我们的瓶颈就是从读取<code>created_at</code>,即创建时间。。。以及解析json,反而不是我们关心的IO,果然<code>readline</code>很强大。</p>
|
||||||
<h2 id="memory_profiler">memory_profiler</h2>
|
<h3 id="memory_profiler">memory_profiler</h3>
|
||||||
<p>首先我们需要install memory_profiler:</p>
|
<p>首先我们需要install memory_profiler:</p>
|
||||||
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pip</span> install -U memory_profiler
|
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">pip</span> install -U memory_profiler
|
||||||
$ <span class="kw">pip</span> install psutil</code></pre></div>
|
$ <span class="kw">pip</span> install psutil</code></pre></div>
|
||||||
|
|
@ -706,7 +706,7 @@ Line # Mem usage Increment Line Contents
|
||||||
25
|
25
|
||||||
26 f.close()
|
26 f.close()
|
||||||
27 return datacount, dataarray</code></pre>
|
27 return datacount, dataarray</code></pre>
|
||||||
<h2 id="objgraph-python">objgraph python</h2>
|
<h3 id="objgraph-python">objgraph python</h3>
|
||||||
<p>安装objgraph</p>
|
<p>安装objgraph</p>
|
||||||
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">pip</span> install objgraph</code></pre></div>
|
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">pip</span> install objgraph</code></pre></div>
|
||||||
<p>我们需要调用他</p>
|
<p>我们需要调用他</p>
|
||||||
|
|
@ -1368,7 +1368,7 @@ pipe.execute()</code></pre></div>
|
||||||
<p>重造轮子是重新创造一个已有的或是已被其他人优化的基本方法。</p>
|
<p>重造轮子是重新创造一个已有的或是已被其他人优化的基本方法。</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<p>最近萌发了一个想法写游戏引擎,之前想着做一个JavaScript前端框架。看看,这个思路是怎么来的。</p>
|
<p>最近萌发了一个想法写游戏引擎,之前想着做一个JavaScript前端框架。看看,这个思路是怎么来的。</p>
|
||||||
<h2 id="一lettuce构建过程">一、<a href="https://github.com/phodal/lettuce">Lettuce</a>构建过程</h2>
|
<h2 id="lettuce构建过程"><a href="https://github.com/phodal/lettuce">Lettuce</a>构建过程</h2>
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<p>Lettuce是一个简约的移动开发框架。</p>
|
<p>Lettuce是一个简约的移动开发框架。</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
@ -1419,97 +1419,97 @@ pipe.execute()</code></pre></div>
|
||||||
<li><a href="https://github.com/cujojs/when" class="uri">https://github.com/cujojs/when</a></li>
|
<li><a href="https://github.com/cujojs/when" class="uri">https://github.com/cujojs/when</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>但是显然,他们都太重了。事实上,对于一个库来说,80%的人只需要其中20%的代码。于是,找到了<a href="https://github.com/stackp/promisejs" class="uri">https://github.com/stackp/promisejs</a>,看了看用法,这就是我们需要的功能:</p>
|
<p>但是显然,他们都太重了。事实上,对于一个库来说,80%的人只需要其中20%的代码。于是,找到了<a href="https://github.com/stackp/promisejs" class="uri">https://github.com/stackp/promisejs</a>,看了看用法,这就是我们需要的功能:</p>
|
||||||
<pre><code>function late(n) {
|
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">late</span>(n) <span class="op">{</span>
|
||||||
var p = new promise.Promise();
|
<span class="kw">var</span> p <span class="op">=</span> <span class="kw">new</span> <span class="va">promise</span>.<span class="at">Promise</span>()<span class="op">;</span>
|
||||||
setTimeout(function() {
|
<span class="at">setTimeout</span>(<span class="kw">function</span>() <span class="op">{</span>
|
||||||
p.done(null, n);
|
<span class="va">p</span>.<span class="at">done</span>(<span class="kw">null</span><span class="op">,</span> n)<span class="op">;</span>
|
||||||
}, n);
|
<span class="op">},</span> n)<span class="op">;</span>
|
||||||
return p;
|
<span class="cf">return</span> p<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
|
|
||||||
late(100).then(
|
<span class="at">late</span>(<span class="dv">100</span>).<span class="at">then</span>(
|
||||||
function(err, n) {
|
<span class="kw">function</span>(err<span class="op">,</span> n) <span class="op">{</span>
|
||||||
return late(n + 200);
|
<span class="cf">return</span> <span class="at">late</span>(n <span class="op">+</span> <span class="dv">200</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
).then(
|
).<span class="at">then</span>(
|
||||||
function(err, n) {
|
<span class="kw">function</span>(err<span class="op">,</span> n) <span class="op">{</span>
|
||||||
return late(n + 300);
|
<span class="cf">return</span> <span class="at">late</span>(n <span class="op">+</span> <span class="dv">300</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
).then(
|
).<span class="at">then</span>(
|
||||||
function(err, n) {
|
<span class="kw">function</span>(err<span class="op">,</span> n) <span class="op">{</span>
|
||||||
return late(n + 400);
|
<span class="cf">return</span> <span class="at">late</span>(n <span class="op">+</span> <span class="dv">400</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
).then(
|
).<span class="at">then</span>(
|
||||||
function(err, n) {
|
<span class="kw">function</span>(err<span class="op">,</span> n) <span class="op">{</span>
|
||||||
alert(n);
|
<span class="at">alert</span>(n)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
);</code></pre>
|
)<span class="op">;</span></code></pre></div>
|
||||||
<p>接着打开看看Promise对象,有我们需要的功能,但是又有一些功能超出我的需求。接着把自己不需要的需求去掉,这里函数最后就变成了</p>
|
<p>接着打开看看Promise对象,有我们需要的功能,但是又有一些功能超出我的需求。接着把自己不需要的需求去掉,这里函数最后就变成了</p>
|
||||||
<pre><code>function Promise() {
|
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">Promise</span>() <span class="op">{</span>
|
||||||
this._callbacks = [];
|
<span class="kw">this</span>.<span class="at">_callbacks</span> <span class="op">=</span> []<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
|
|
||||||
Promise.prototype.then = function(func, context) {
|
<span class="va">Promise</span>.<span class="va">prototype</span>.<span class="at">then</span> <span class="op">=</span> <span class="kw">function</span>(func<span class="op">,</span> context) <span class="op">{</span>
|
||||||
var p;
|
<span class="kw">var</span> p<span class="op">;</span>
|
||||||
if (this._isdone) {
|
<span class="cf">if</span> (<span class="kw">this</span>.<span class="at">_isdone</span>) <span class="op">{</span>
|
||||||
p = func.apply(context, this.result);
|
p <span class="op">=</span> <span class="va">func</span>.<span class="at">apply</span>(context<span class="op">,</span> <span class="kw">this</span>.<span class="at">result</span>)<span class="op">;</span>
|
||||||
} else {
|
<span class="op">}</span> <span class="cf">else</span> <span class="op">{</span>
|
||||||
p = new Promise();
|
p <span class="op">=</span> <span class="kw">new</span> <span class="at">Promise</span>()<span class="op">;</span>
|
||||||
this._callbacks.push(function () {
|
<span class="kw">this</span>.<span class="va">_callbacks</span>.<span class="at">push</span>(<span class="kw">function</span> () <span class="op">{</span>
|
||||||
var res = func.apply(context, arguments);
|
<span class="kw">var</span> res <span class="op">=</span> <span class="va">func</span>.<span class="at">apply</span>(context<span class="op">,</span> arguments)<span class="op">;</span>
|
||||||
if (res && typeof res.then === 'function') {
|
<span class="cf">if</span> (res <span class="op">&&</span> <span class="kw">typeof</span> <span class="va">res</span>.<span class="at">then</span> <span class="op">===</span> <span class="st">'function'</span>) <span class="op">{</span>
|
||||||
res.then(p.done, p);
|
<span class="va">res</span>.<span class="at">then</span>(<span class="va">p</span>.<span class="at">done</span><span class="op">,</span> p)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
});
|
<span class="op">}</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
return p;
|
<span class="cf">return</span> p<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
Promise.prototype.done = function() {
|
<span class="va">Promise</span>.<span class="va">prototype</span>.<span class="at">done</span> <span class="op">=</span> <span class="kw">function</span>() <span class="op">{</span>
|
||||||
this.result = arguments;
|
<span class="kw">this</span>.<span class="at">result</span> <span class="op">=</span> arguments<span class="op">;</span>
|
||||||
this._isdone = true;
|
<span class="kw">this</span>.<span class="at">_isdone</span> <span class="op">=</span> <span class="kw">true</span><span class="op">;</span>
|
||||||
for (var i = 0; i < this._callbacks.length; i++) {
|
<span class="cf">for</span> (<span class="kw">var</span> i <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> i <span class="op"><</span> <span class="kw">this</span>.<span class="va">_callbacks</span>.<span class="at">length</span><span class="op">;</span> i<span class="op">++</span>) <span class="op">{</span>
|
||||||
this._callbacks[i].apply(null, arguments);
|
<span class="kw">this</span>.<span class="at">_callbacks</span>[i].<span class="at">apply</span>(<span class="kw">null</span><span class="op">,</span> arguments)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
this._callbacks = [];
|
<span class="kw">this</span>.<span class="at">_callbacks</span> <span class="op">=</span> []<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
var promise = {
|
<span class="kw">var</span> promise <span class="op">=</span> <span class="op">{</span>
|
||||||
Promise: Promise
|
<span class="dt">Promise</span><span class="op">:</span> Promise
|
||||||
};</code></pre>
|
<span class="op">};</span></code></pre></div>
|
||||||
<p>需要注意的是: <code>License</code>,不同的软件有不同的License,如MIT、GPL等等。最好能在遵循协议的情况下,使用别人的代码。</p>
|
<p>需要注意的是: <code>License</code>,不同的软件有不同的License,如MIT、GPL等等。最好能在遵循协议的情况下,使用别人的代码。</p>
|
||||||
<h3 id="实现第二个需求">实现第二个需求</h3>
|
<h3 id="实现第二个需求">实现第二个需求</h3>
|
||||||
<p>由于,现有的一些Ajax库都比较,最后只好参照着别人的代码自己实现。</p>
|
<p>由于,现有的一些Ajax库都比较,最后只好参照着别人的代码自己实现。</p>
|
||||||
<pre><code>Lettuce.get = function (url, callback) {
|
<div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">Lettuce</span>.<span class="at">get</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> callback) <span class="op">{</span>
|
||||||
Lettuce.send(url, 'GET', callback);
|
<span class="va">Lettuce</span>.<span class="at">send</span>(url<span class="op">,</span> <span class="st">'GET'</span><span class="op">,</span> callback)<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
Lettuce.load = function (url, callback) {
|
<span class="va">Lettuce</span>.<span class="at">load</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> callback) <span class="op">{</span>
|
||||||
Lettuce.send(url, 'GET', callback);
|
<span class="va">Lettuce</span>.<span class="at">send</span>(url<span class="op">,</span> <span class="st">'GET'</span><span class="op">,</span> callback)<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
Lettuce.post = function (url, data, callback) {
|
<span class="va">Lettuce</span>.<span class="at">post</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> data<span class="op">,</span> callback) <span class="op">{</span>
|
||||||
Lettuce.send(url, 'POST', callback, data);
|
<span class="va">Lettuce</span>.<span class="at">send</span>(url<span class="op">,</span> <span class="st">'POST'</span><span class="op">,</span> callback<span class="op">,</span> data)<span class="op">;</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
|
|
||||||
Lettuce.send = function (url, method, callback, data) {
|
<span class="va">Lettuce</span>.<span class="at">send</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> method<span class="op">,</span> callback<span class="op">,</span> data) <span class="op">{</span>
|
||||||
data = data || null;
|
data <span class="op">=</span> data <span class="op">||</span> <span class="kw">null</span><span class="op">;</span>
|
||||||
var request = new XMLHttpRequest();
|
<span class="kw">var</span> request <span class="op">=</span> <span class="kw">new</span> <span class="at">XMLHttpRequest</span>()<span class="op">;</span>
|
||||||
if (callback instanceof Function) {
|
<span class="cf">if</span> (callback <span class="kw">instanceof</span> Function) <span class="op">{</span>
|
||||||
request.onreadystatechange = function () {
|
<span class="va">request</span>.<span class="at">onreadystatechange</span> <span class="op">=</span> <span class="kw">function</span> () <span class="op">{</span>
|
||||||
if (request.readyState === 4 && (request.status === 200 || request.status === 0)) {
|
<span class="cf">if</span> (<span class="va">request</span>.<span class="at">readyState</span> <span class="op">===</span> <span class="dv">4</span> <span class="op">&&</span> (<span class="va">request</span>.<span class="at">status</span> <span class="op">===</span> <span class="dv">200</span> <span class="op">||</span> <span class="va">request</span>.<span class="at">status</span> <span class="op">===</span> <span class="dv">0</span>)) <span class="op">{</span>
|
||||||
callback(request.responseText);
|
<span class="at">callback</span>(<span class="va">request</span>.<span class="at">responseText</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
};
|
<span class="op">};</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
request.open(method, url, true);
|
<span class="va">request</span>.<span class="at">open</span>(method<span class="op">,</span> url<span class="op">,</span> <span class="kw">true</span>)<span class="op">;</span>
|
||||||
if (data instanceof Object) {
|
<span class="cf">if</span> (data <span class="kw">instanceof</span> Object) <span class="op">{</span>
|
||||||
data = JSON.stringify(data);
|
data <span class="op">=</span> <span class="va">JSON</span>.<span class="at">stringify</span>(data)<span class="op">;</span>
|
||||||
request.setRequestHeader('Content-Type', 'application/json');
|
<span class="va">request</span>.<span class="at">setRequestHeader</span>(<span class="st">'Content-Type'</span><span class="op">,</span> <span class="st">'application/json'</span>)<span class="op">;</span>
|
||||||
}
|
<span class="op">}</span>
|
||||||
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
<span class="va">request</span>.<span class="at">setRequestHeader</span>(<span class="st">'X-Requested-With'</span><span class="op">,</span> <span class="st">'XMLHttpRequest'</span>)<span class="op">;</span>
|
||||||
request.send(data);
|
<span class="va">request</span>.<span class="at">send</span>(data)<span class="op">;</span>
|
||||||
};</code></pre>
|
<span class="op">};</span></code></pre></div>
|
||||||
<section class="footnotes">
|
<section class="footnotes">
|
||||||
<hr />
|
<hr />
|
||||||
<ol>
|
<ol>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue