Mar 12, 2013

python, ajax

From Evernote:

python, ajax

ajax 的要义是异步访问,不进行 page refresh. 但归根到底,还是一次 http 访问。
Firebug 可以查看网页所访问的全部地址,当然也包含 XHR 地址。找到 net 标签,选择 XHR

有时候需要在 python 中模拟 ajax 访问,可以使用

opener = urllib2.build_opener()
header = {'User-Agent':USER_AGENT,
'X-Requested-With':'XMLHttpRequest',
'Referer':发出 ajax 访问的页面的 url}
request = urllib2.Request(url,None,header)
page = opener.open(request).read()

以苏宁为例。苏宁页面中价格是 ajax 获取的,其完整的 header 如下。cookie 也作为 header 的一部分。

Accept application/json, text/javascript, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie __utma=1.969807245.1332136632.1363074620.1363087633.5; _snma=www.suning.com%7C13321366319954164639577406%7C1332136631995%7C1363087631615%7C1363087641369%7C43%7C5; logonUserIdLastTime=apple2101%40gmail.com; nick=apple2101; WC_PERSISTENT=xw%2bWpAijt%2fF%2bDUW%2fLlBSTJIk6XA%3d%0a%3b2013%2d03%2d12+19%3a27%3a12%2e976%5f1363070404466%2d548555%5f10052; smhst=4797905a1526655a1526707a1504644a3436846; __utmz=1.1363071778.3.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); JSESSIONID=00007tdcVqwih8dRPN-WIvqKTCX:17a3mnbnj; cityId=9315; WC_SESSION_ESTABLISHED=true; WC_ACTIVEPOINTER=%2d7%2c10052; WC_USERACTIVITY_-1002=%2d1002%2c10052%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2cnull%2cHfu4AyHVyTEH5%2fLoLwRp0%2frI3cWrBquyD%2fSaNhM3UZ0lBXgogKjO%2fnlhmMBqRneXWJNymkgPcFsi%0atcL%2foBOkrTxmwXKhtjFyuSGc%2bRxhyVmbWwnE2xgfEylnuINNcTRAFLJbP0f%2bnqRLzC%2faRcNAog%3d%3d; WC_GENERIC_ACTIVITYDATA=[30000005601374689%3atrue%3afalse%3a0%3axgte8CZGlUbEzv%2fAjAUk1wdSN%2bI%3d][com.ibm.commerce.context.audit.AuditContext|1363070404466%2d548555][com.ibm.commerce.store.facade.server.context.StoreGeoCodeContext|null%26null%26null%26null%26null%26null][CTXSETNAME|Store][com.ibm.commerce.context.globalization.GlobalizationContext|%2d7%26CNY%26%2d7%26CNY][com.ibm.commerce.catalog.businesscontext.CatalogContext|10051%26null%26false%26false%26false][com.suning.commerce.context.common.SNContext|9315%26%2d1%26null%2661%2e164%2e36%2e1%26null%26null%26null%26null%26null%26null%26null%26null%26null][com.ibm.commerce.context.base.BaseContext|10052%26%2d1002%26%2d1002%26%2d1][com.ibm.commerce.context.experiment.ExperimentContext|null][com.ibm.commerce.context.entitlement.EntitlementContext|10007%2610007%26null%26%2d2000%26null%26null%26null][com.ibm.commerce.giftcenter.context.GiftCenterContext|null%26null%26null]; _snmc=1; _snsr=direct%7Cdirect%7C%7C%7C; _snmb=136308763162090554%7C1363087641376%7C1363087641371%7C2; _snmp=136308764012592196; __utmb=1.2.10.1363087633; __utmc=1
Host www.suning.com
Referer http://www.suning.com/emall/prd_10052_10051_-7_4797905_.html
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0
X-Requested-With XMLHttpRequest

Dec 25, 2012

制造业的变革,Make entrepreneur

From Evernote:

制造业的变革,Make entrepreneur

maker entrepreneur
技术对制造业的改变
技术对印刷有过巨大的改变。以前印刷是专业工人才能从事的技术活,现在你编辑一个文档,然后点一下打印,就行了。

以前有什么创意要制造出来很困难。
主要难在两部,一 prototyping 二 生产
现在 prototyping 可以由 3d printer 实现
生产就要依靠世界扁平的力量,alibaba 上有大量的厂商,有些有定制的能力

现在进入个人制造的时代,制造有三个步骤
rip mod make
rip 把现实世界的东西抓取为三维模型
mod 修改,完善
make

3d modeling app
Autodesk 有一套工具,123D

创业的一个思路
先建立一个 community,集思广益,从小东西逐渐做大,再创立 company
bottom up的过程

孩子的教育
everything is becoming easier
children who realized anything they can imagine, they can make it real, they will be the driver of modern manufacture in 21 centruy.
They can do it. don't need special permissions, special skills, speical degrees.
what remains is waht is their bigest idea?

Dec 22, 2012

随手集 信手写: MongoDB, PyMongo, BSON, django

From Evernote:

随手集 信手写: MongoDB, PyMongo, BSON, django

Clipped from: http://noteonx.blogspot.com/2011/06/mongodb-pymongo.html

mongodb 要点

参考:
不错的 Tutorial
m
inibook

MongoDB provides a simple http interface listing information of interest to administrators. This interface may be accessed at the port with numeric value 1000 more than the configured mongod port; the default port for the http interface is 28017. To access the http interface an administrator may, for example, point a browser to http://localhost:28017 if mongod is running with the default port on the local machine.


To recap, MongoDB is made up of databases which contain collections. A collection is
made up of documents. Each document is made up of fields. Collections can be indexed,
which improves lookup and sorting performance. Finally, when we get data from MongoDB we
do so through a cursor whose actual execution is delayed until necessary.
 
index 使用二叉搜索树
 
类比
collection     table
document     row
field          column
这只是类比,并不相同
Relational databases define columns at the table level whereas a document-oriented database defines its fields at the document level.
因此 document 能有自己独特的 fields
 
document-based database 是 schema-less 的。
 

配置和使用


配置:
创建一个目录,用于保存数据库
在 config-file 写入
dbpath=path-to-data-folder

mongod: server
$ mongod --config path-to-config-file
建议将这个命令存成可执行脚本

mongod 有两种方式可以连接,一种是 tcp (socket?) 连接,一种是 REST 方式
REST 方式需要再安装 REST interfaces. mongodb 安装包自带了一个简易的 REST 接口(用参数 --rest 开启),只适合admin使用.

第一种使用
mongo: shell client
pymongo python client

第二种需要再安装 REST interfaces。自带的简易服务时加参数 --rest,使用浏览器进行通信
http://localhost:28017/

django mongodb backend

关于mongodb 的思想,可见参见这个 tutorial
以 blog, comments 为例


pymongo

PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python

API Docs

pymongo 实际上由三部分组成
bson
pymongo
gridfs: location based storage?

from pymongo import Connection  connection = Connection()    db = connection.learn    collection = db.unicorns  for unicorn in collection.find():   print unicorn  

BSON

BSON is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. BSON is designed to be lightweight, traversable, and efficient.MongoDB uses BSON as the data storage and network transfer format for "documents".

BSON at first seems BLOB-like, but there exists an important difference: the Mongo database understands BSON internals. This means that MongoDB can "reach inside " BSON objects, even nested ones. Among other things, this allows MongoDB to build indexes and match objects against query expressions on both top-level and nested BSON keys.


MongoDB uses BSON documents for three things
  1. Data storage (user documents). These are the regular JSON-like objects that the database stores for us. These BSON documents are sent to the database via the INSERT operation. User documents have limitations on the "element name" space due to the usage of special characters in the JSON-like query language.
    1. A user document element name cannot begin with "$".
    2. A user document element name cannot have a "." in the name.
    3. The element name "_id" is reserved for use as a primary key id, but you can store anything that is unique in that field.
      The database expects that drivers will prevent users from creating documents that violate these constraints. 
  2. Query "Selector" Documents : Query documents (or selectors) are BSON documents that are used in QUERY, DELETE and UPDATE operations. They are used by these operations to match against documents. Selector objects have no limitations on the "element name" space, as they must be able to supply special "marker" elements, like "$where" and the special "command" operations. 
  3. "Modifier" Documents : Documents that contain 'modifier actions' that modify user documents in the case of an update (seeUpdating).




PyMongo 包含 bson 模块
bson_string = BSON.encode({"name": (1,2,3)}) # 将 python dict 编码为 bson
print BSON.decode(bson_string) # 将 bson 解码为 python dict

son:
A subclass of dict that maintains ordering of keys and provides a few extra niceties for dealing with SON. SON objects can be converted to and from BSON.

The mapping from Python types to BSON types is as follows:
Python TypeBSON TypeSupported Direction
Nonenullboth
boolbooleanboth
intnumber (int)both
floatnumber (real)both
stringstringpy -> bson
unicodestringboth
listarrayboth
dict / SONobjectboth
datetime.datetime [1] [2]dateboth
compiled reregexboth
bson.binary.Binarybinaryboth
bson.objectid.ObjectIdoidboth
bson.dbref.DBRefdbrefboth
Noneundefinedbson -> py
unicodecodebson -> py
bson.code.Codecodepy -> bson
unicodesymbolbson -> py


Http Interface - MongoDB

Source URL:http://www.mongodb.org/display/DOCS/Http+Interface

REST Interfaces

DrowsyDromedary (Ruby)

DrowsyDromedary is a REST layer for Mongo based on Ruby.

MongoDB Rest (Node.js)

MongoDB Rest is an alpha REST interface to MongoDB, which uses the MongoDB Node Native driver.

Mongodb Java REST server

Mongodb Java REST server based on Jetty.

HTTP Interfaces

Sleepy Mongoose (Python)

Sleepy Mongoose is a full featured HTTP interface for MongoDB.



 

 

Dec 11, 2012

jquery ajax

$.ajax

待添加

Dec 10, 2012

Javascript

From Evernote:

Javascript

Clipped from: http://jqfundamentals.com/chapter/javascript-basics

只有五种基本类型(primitives),其他都是对象

As it turns out, most everything we work with in JavaScript is an object — in fact, there are only five kinds of values that are not objects:

  • strings (text) : 'Aliz'
  • booleans (true/false)
  • numbers
  • undefined
  • null

对象声明和使用:

var person = {
firstName : 'Boaz',
lastName : 'Sender'
};

log( 'First name: ' + person.firstName ); // dot notation
log( 'Last name: ' + person[ 'lastName' ] ); // bracket notation,需加引号

 

函数命名有两种方法,只用第一种

var addTwoNumbers = function(a, b) {
return a + b;
};

 

function addTwoNumbers(a, b) {
return a + b;
}

Bottom line: naming functions using the function declaration approach can have unexpected results if you don't fully understand a feature of JavaScript known as hoisting. The details of hoisting are beyond the scope of this guide, but for now we'll stick to assigning function expressions to variables.

变量:

  • you should almost always declare variables with a var statement
  • variables declared inside of a function using a var statement are not available outside of that function
  • variables declared without a var statement are always global

Variables that are declared inside a function with a var statement are only available inside of the function; this is generally desirable, and all variables should be declared with a var statement unless they are intended to be global — that is, available anywhere in your code. This usually isn't what you want, unless you want it to be possible for other code to change the value of the variable.

 

对象内的函数:

var person = {
firstName : 'Boaz',
lastName : 'Sender',
greet : function() {
log( 'Hi, ' + this.firstName );
}
};

person.greet(); // logs 'Hi, Boaz'

var sayIt = person.greet; // store the method in a variable

sayIt(); // logs 'Hi, undefined' -- uh-oh 因为 this 含义改变了

this. It refers to the object that is the context in which the function was called.

When we store the .greet() method in a variable sayIt and then call sayIt(), the context object changes to the global window object, not the person object. Since the window object doesn't have a property firstName, we getundefined when we try to access it.

What's a developer to do? First, be aware that storing object methods in variables can have unintended consequences for the meaning of this. Second, be aware that you can force the meaning of this to be what you want it to be, using the.call() or .apply() method on the function itself.

sayIt.call( person ); // logs 'Hi, Boaz'

 

Array 使用方法

var myArray = [ 'a', 'b', 'c' ];
var firstItem = myArray[ 0 ];

var secondItem = myArray[ 1 ]; // access the item at index 1
log( secondItem ); // logs 'b'

var len = myArray.length;

var i;

for (i = 0; i < len; i = i + 1) {
log( 'item at index ' + i + ' is ' + myArray[ i ] );
}

 

Logic operation:

五种 false

As it turns out, most values in JavaScript are truthy — in fact, there are only five values in JavaScript that are falsy:

  • undefined (the default value of declared variables that are not assigned a value)
  • null
  • NaN ("not a number"):将非数学类型进行数学操作,会得到 NaN,如 'five' - '4'
  • 0 (the number zero)
  • '' (an empty string)
It's important to remember that all other values aside from the five values listed above are truthy. This includes empty arrays, empty objects, all non-empty strings (including the string '0'), and all numbers other than 0.

逻辑与 (&&) and 逻辑或 (||) operations.

var propertyName = ( dim === 'width' ) ? 'clientWidth' : 'clientHeight';

相等操作 === 三个等号

Dec 4, 2012

sass, compass

From Evernote:

sass, compass

compass 和 sass 的关系就像 django 和 python


compass compile path_to_project
compass watch path_to_project

使用 compass 和 sass 的主要目的就是提高效率,可以直接使用别人的 module

比如Brandon Mathis有很多不错的东西
特别是 fancy-buttons,你再也不用为按钮的样式而发愁了



ultraedit 开发 webapp

From Evernote:

ultraedit 开发 webapp

利用 project 功能,add project folder
开启 file auto update

html 可以直接用 browser 看