The response return by Scrapy is a Python repr() object thus Chinese character 'display' in Unicode.
Quick fix is like this:
item['name'][0].encode('utf-8')
Though some characters are still display incorrectly.
Find source code here.
More about Unicode in Python:
http://evanjones.ca/python-utf8.html
http://www.b-list.org/weblog/2007/nov/10/unicode/
Saturday, September 26, 2009
Friday, September 18, 2009
Trac quick setup
Came across Trac while checking information of Scrapy and had a quick try.
Operation System is Ubuntu.
Operation performed:
Operation System is Ubuntu.
Operation performed:
- Download package from official site
- Follow INSTALL doc in the package to complete installation
- Start Trac and visit Guide
- Auth
- basic auth
- TODO other auth of adding register feature
- Subversion
- python Subversion module (ubuntu install script: apt-get install python-subversion)
- "svnadmin create" Subversion repos and update trac.ini accordingly
- Permission (Guide section TracPermissions)
- Add permission TRAC_ADMIN to user in order to show the WebAdmin tab
- WebAdmin is an Admin Web interface to perform admin operations
- Roadmap
- Component
- TODO etc ...
Thursday, September 03, 2009
Verify Binary Search result with duplicate target in the array
Testing scenario: If testArray contains target at position n, then binarySearch (testArray, target) must return n
public void assertTheory4(int[] testArray, int target, int returnValue) {
assertEquals(getTargetPosition(testArray, target), returnValue);
}
public int getTargetPosition(int[] testArray, int target) {
for (int i = 0; i < testArray.length; i++)
if (testArray[i] == target)
return i;
return -1;
}
Array=[2, 11, 36, 66, 104, 108, 108, 108, 122, 155, 159, 161, 191]
target=108
Binary Search 108 will return 6 while getTargetPosition return 5.
Possible fix: Update the function getTargetPosition to return a int array, which contains all position of the value.
Then assert this return int array must contain binary search result.
Any others?
REFERENCE: SECTION 7.2 OF <BEAUTIFUL CODE>
public void assertTheory4(int[] testArray, int target, int returnValue) {
assertEquals(getTargetPosition(testArray, target), returnValue);
}
public int getTargetPosition(int[] testArray, int target) {
for (int i = 0; i < testArray.length; i++)
if (testArray[i] == target)
return i;
return -1;
}
Array=[2, 11, 36, 66, 104, 108, 108, 108, 122, 155, 159, 161, 191]
target=108
Binary Search 108 will return 6 while getTargetPosition return 5.
Possible fix: Update the function getTargetPosition to return a int array, which contains all position of the value.
Then assert this return int array must contain binary search result.
Any others?
REFERENCE: SECTION 7.2 OF <BEAUTIFUL CODE>
Tuesday, September 01, 2009
NameCheck of XML Validation
The beautiful part is build up a array with size 2^16, each value is 8bits, each bit indicate something like whether current character could be start of name, valid name.
For instance, number 4 is at 0x34 (decimal 52), so flags[52] = 0x0b (00001100), those 8bits 0/1 are indication.
Chapter 5 of <beautiful code> has the detail.
For instance, number 4 is at 0x34 (decimal 52), so flags[52] = 0x0b (00001100), those 8bits 0/1 are indication.
Chapter 5 of <beautiful code> has the detail.
Patterns: Factory method,Template method and Strategy
Basically, Factory method is a method that is defined in Parent class and is implemented in Child class, which usually is abstract method.
By contract, Template method is a method that implements an "algorithm". This implementation is usually defined in a "Parent" class and leveraging other methods including Factory methods. (See sample below)
public abstract class PizzaStore {
/**
* #templateMethod(String type) is an Template Method.
* <p>
* #factoryMethodOfInit() AND #factoryMethodOfCreate(String type) are both
* Factory Method.
*/
public Pizza templateMethod(String type) {
factoryMethodOfInit();
Pizza pizza = factoryMethodOfCreate(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract void factoryMethodOfInit();
protected abstract Pizza factoryMethodOfCreate(String type);
}
Here is a definition:
Factory Method:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Template Method:
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
(Here "lets subclasses redefine certain steps" could be done by leveraging Factory method)
Relationship: Factory Methods are often called by template methods.
Then the Strategy Pattern, Spring DI uses this pattern IMO somehow.
Check the updated PizzaStore version below, londonPizzaCreator and nYorkPizzaCreator are "Strategies" of PizzaCreator.
Then PizzaStore can change strategies at runtime regardless implementation detail by injecting various strategies. (via method setPizzaCreator)
public abstract class PizzaStore {
private PizzaCreator pizzaCreator;
public Pizza templateMethod(String type) {
factoryMethodOfInit();
Pizza pizza = pizzaCreator.createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract void factoryMethodOfInit();
public void setPizzaCreator(PizzaCreator pizzaCreator) {
this.pizzaCreator = pizzaCreator;
}
}
// ---------------------------------------------------------------------------------- "Strategies"
public interface PizzaCreator {
public Pizza createPizza(String type);
}
public class londonPizzaCreator implements PizzaCreator {
public Pizza createPizza(String type) {
// TODO add creat detail
return null;
}
}
public class nYorkPizzaCreator implements PizzaCreator {
public Pizza createPizza(String type) {
// TODO add creat detail
return null;
}
}
Strategy:
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Related to Template Method:
Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.
What's more?
Those patterns are actually in different category in the book by GoF, trying to understand the reason therefore to understand their diff more clearly.
By contract, Template method is a method that implements an "algorithm". This implementation is usually defined in a "Parent" class and leveraging other methods including Factory methods. (See sample below)
public abstract class PizzaStore {
/**
* #templateMethod(String type) is an Template Method.
* <p>
* #factoryMethodOfInit() AND #factoryMethodOfCreate(String type) are both
* Factory Method.
*/
public Pizza templateMethod(String type) {
factoryMethodOfInit();
Pizza pizza = factoryMethodOfCreate(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract void factoryMethodOfInit();
protected abstract Pizza factoryMethodOfCreate(String type);
}
Here is a definition:
Factory Method:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Template Method:
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
(Here "lets subclasses redefine certain steps" could be done by leveraging Factory method)
Relationship: Factory Methods are often called by template methods.
Then the Strategy Pattern, Spring DI uses this pattern IMO somehow.
Check the updated PizzaStore version below, londonPizzaCreator and nYorkPizzaCreator are "Strategies" of PizzaCreator.
Then PizzaStore can change strategies at runtime regardless implementation detail by injecting various strategies. (via method setPizzaCreator)
public abstract class PizzaStore {
private PizzaCreator pizzaCreator;
public Pizza templateMethod(String type) {
factoryMethodOfInit();
Pizza pizza = pizzaCreator.createPizza(type);
System.out.println("--- Making a " + pizza.getName() + " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract void factoryMethodOfInit();
public void setPizzaCreator(PizzaCreator pizzaCreator) {
this.pizzaCreator = pizzaCreator;
}
}
// ---------------------------------------------------------------------------------- "Strategies"
public interface PizzaCreator {
public Pizza createPizza(String type);
}
public class londonPizzaCreator implements PizzaCreator {
public Pizza createPizza(String type) {
// TODO add creat detail
return null;
}
}
public class nYorkPizzaCreator implements PizzaCreator {
public Pizza createPizza(String type) {
// TODO add creat detail
return null;
}
}
Strategy:
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Related to Template Method:
Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.
What's more?
Those patterns are actually in different category in the book by GoF, trying to understand the reason therefore to understand their diff more clearly.
Wednesday, August 19, 2009
Tail Call Optimization
Hello-Macro in Clojure
If define the unless as function like this,
(defn unless [expr form] (if expr nil form))
Does that work?
(unless false (println "this should print"))
=> this should print
(unless true (println "this should not print"))
=> this should not print
Obviously the answer is no.
The reason is Clojure evaluates all the arguments before passing them to a function,
so the println is called before unless ever sees it.
So it seems Clojure is applicative order evaluation, but not normal order evaluation. (SICP exe. 1.5)
Therefore Macro.
(defmacro unless [expr form] (list 'if expr nil form))
(defn unless [expr form] (if expr nil form))
Does that work?
(unless false (println "this should print"))
=> this should print
(unless true (println "this should not print"))
=> this should not print
Obviously the answer is no.
The reason is Clojure evaluates all the arguments before passing them to a function,
so the println is called before unless ever sees it.
So it seems Clojure is applicative order evaluation, but not normal order evaluation. (SICP exe. 1.5)
Therefore Macro.
(defmacro unless [expr form] (list 'if expr nil form))
Thursday, March 05, 2009
Uninstall Oracle DB in Windows XP
This article shows the detail.
But problem is the OUI failed to show all installed components...
After read this post, I got chance to read the inventory.xml by finding out that my Oracle DB was not appeared in the HOME LIST...
Everything went well once fix the inventory.xml manualy.
But problem is the OUI failed to show all installed components...
After read this post, I got chance to read the inventory.xml by finding out that my Oracle DB was not appeared in the HOME LIST...
Everything went well once fix the inventory.xml manualy.
Sunday, February 22, 2009
雨天
还在继续。
人们似乎都不喜欢雨天,在言语上排斥,在心里面厌烦。
空气中弥漫着雨水,打湿了漂亮的装扮,'浸湿'了衣服和被子。
水气冲到了家里的每一个角落。
落在雨棚上的雨滴是一种噪音。
人们开始头痛,身体感到不适。
打开窗,深呼吸,清心舒畅。
空气中,树叶上,街道里,
雨水洗刷了灰尘,雨滴变成了音符。
路上行人寥寥,雨点拍打湖面,声音依稀可见。
人们开始微笑,心里发现平静。
为了看到雨后的清澈,人们每天快乐。
Subscribe to:
Posts (Atom)

