YAML

这阵子磨磨蹭蹭的, 联合着师妹, 把YAML的一份说明文档翻译了下, 但还没有翻译完. 想年后再继续了...

YAML英文原文:
YAML(1): http://docs.google.com/Doc?docid=0ARpLl8CU4NijZGY3Z3p0ZG1fODA5aDhka2NzY2c&hl=en
YAML(2-3): http://docs.google.com/Doc?docid=0ARpLl8CU4NijZGY3Z3p0ZG1fODEwNTU0aGs0Z2I&hl=en
YAML(4): http://docs.google.com/Doc?docid=0ARpLl8CU4NijZGY3Z3p0ZG1fODE2ZmN3Mm1jZnQ&hl=en, 这个还没完全翻译完~

Tags:      

SIKULI

今天上午大集中, 和xxh聊了会, 无意间找到SIKULI这个项目http://groups.csail.mit.edu/uid/sikuli/index.shtml, 看了下demo, http://groups.csail.mit.edu/uid/sikuli/demo.shtml, 原来这个"小玩意儿"挺酷! 于是乎这边简单介绍下.

== 什么是SIKULI ==
SIKULI, 名字读起来像Sakura(樱花), 她是一种可视化搜索技术并且可以自动识别GUI上的图像...
她包含自己的脚本, 可视化的API(for Jython), 本身的IDE和相关的集成环境. 可运行于windows, Linux, Mac OSx(示例中的就是在Mac上), 甚至是iphone中.
哈哈,,,说了这么多, 是不是还是不知道她到底是做什么的? ----- 按照我的理解, 用户写一个sikuli脚本, 定义一些操作, 这些操作是模拟你在当前桌面上的实际操作, 比如说, 你点击Click了一个图标, 这个图标可以由图像形式嵌入到你的脚本中, 当这个脚本运行时, 就能自动从当前整个桌面图像上识别这个图标, 然后自动执行click操作并反映出来, 这个模拟操作就像是你手动执行的一样.

== 如何使用 ==

step1: 安装java 6和sikuli, 基于Java, 但可用Jython也
step2: 运行sikuli, 编写脚本, 她提供了很多命令, 比如click(), 中间的括号中会出现让你截图的按钮, 点击一下过了一会, 能让你截图你想要点击的图标, 这个图标作为图片嵌入到脚本中.
step3: 运行这个脚本, 就能看到效果了....

我做了个小小小示例, 就是运行后, 自动开启桌面上的picasa. 太简单以至于不好意思拿出来晒了. 而更多的功能得靠个人想象力来了.

官方更多的demo: http://groups.csail.mit.edu/uid/sikuli/demo.shtml

== 点点分析 ==
瞎想想她的实现原理. 估计是充分利用图像匹配技术, 找到相关的按钮, 调用系统api进行各种操作, 比如点击图标, 在输入框中输入. 以此实现全自动.... 那我们可以事先写好一个脚本, 比如一开机之后, 打开音乐, 进入某个文件夹, 打开浏览器, 打开编辑器....哈哈, 全自动!

最后, 顺便推荐一个latex画图的包, PGF TiKz, 据xxh说功能强大, 嗯嗯, 下次试试~
TeX

Tags:      

AttributesAndMethods

http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html

== 什么是属性 ==
属性是从一个对象访问另一个对象的方式(挺怪的解释), 使用点号, 如obj.attribute..(从obj访问attribute, 这样看貌似前面的解释有点道理)

== 属性的访问 ==
class.__dict__以字典对象列出class的所有属性, 包含一些Python-provided(内置)属性(__dict__并不是所有对象都具有的).
obj.att 依次查找的顺序


  1. The object itself (objectname.__dict__ or any Python-provided attribute of objectname). --- obj本身

  2. The object's type (objectname.__class__.__dict__). Observe that only __dict__ is searched, which means only user-provided attributes of the class. In other words objectname.__bases__ may not return anything even though objectname.__class__.__bases__ does exist. ---obj的类中的属性, 即和obj.__class__有关, 不和obj.__base__有关

  3. The bases of the object's class, their bases, and so on. (__dict__ of each of objectname.__class__.__bases__). More than one base does not confuse Python, and should not concern us at the moment. The point to note is that all bases are searched until an attribute is found.obj的类的__base__, 即各级父类

如果没有从这些中找到, 会抛出AttributeError异常. 注意, 属性的查找不会从objectname.__class__.__class__(obj的类型的类型)...中找

dir() 以列表方式列出obj的所有属性
类属性的查找有点特殊, 是先class的__class__(类型, type)中找, 再从class.__base__(父类)中找.

== 函数/方法 ==

>>> cobj.f is C.__dict__['f'] 3
False
>>> cobj.f 4
<bound method C.f of <__main__.C instance at 0x008F9850>>
>>> C.__dict__['f'].__get__(cobj, C) 5
<bound method C.f of <__main__.C instance at 0x008F9850>>

使用__dict__来查找时需要调用这个__get__()方法并返回结果. 这个__get__()是将普通函数转成bound method(绑定方法?其实是和普通函数一样的)
Anyone can put objects with a __get__() method inside the class __dict__ and get away with it.
Such objects are called descriptors and have many uses.

== descriptor ==
__get__() 读取属性时调用(eg. print objectname.attrname)
__set__() 写属性值时调用(eg. objectname.attrname = 12)
__delete__() 删除属性时调用(eg. del objectname.attrname)
Descriptors work only when attached to classes. Sticking a descriptor in an object that is not a class gives us nothing.

上面的都是指data descriptor, 还有一种叫做non-data descriptor

Data descriptors are useful for providing full control over an attribute. 
This is what one usually wants for attributes used to store some piece
of data. For example an attribute that gets transformed and saved
somewhere on setting, would usually be reverse-transformed and
returned when read. When you have a data descriptor, it controls
all access (both read and write) to the attribute on an instance.
Of course, you could still directly go to the class and replace the
descriptor, but you can't do that from an instance of the class.

Non-data descriptors, in contrast, only provide a value when an
instance itself does not have a value. So setting the attribute on
an instance hides the descriptor. This is particularly useful in the
case of functions (which are non-data descriptors) as it allows
one to hide a function defined in the class by attaching one to
an instance.

大概意思是Data descriptor完全控制属性的读和写. 而Non-data descriptor 只提供对象不包含值时的值, 当给属性设置时会隐藏这个descriptor. 这可隐藏类的函数f, 而对象的f仍然可以另外定义)

查找obj.attribute的顺序, 再一次:


  1. If attrname is a special (i.e. Python-provided) attribute for objectname, return it.

  2. Check objectname.__class__.__dict__ for attrname. If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname.__class__ for the same case.

  3. Check objectname.__dict__ for attrname, and return if found. If objectname is a class, search its bases too. If it is a class and a descriptor exists in it or its bases, return the descriptor result.

  4. Check objectname.__class__.__dict__ for attrname. If it exists and is a non-data descriptor, return the descriptor result. If it exists, and is not a descriptor, just return it. If it exists and is a data descriptor, we shouldn't be here because we would have returned at point 2. Search all bases of objectname.__class__ for same case.

  5. Raise AttributeError

a = property(get_a, set_a, del_a, "docstring") # property的get, set, del, docstring, 可以这样定义

== Method Resolution Order ==
method order
针对继承中的相同方法的执行顺序问题. 如A, B, C, D中都有do_your_stuff()方法, 那么D中的do_your_stuff()中会执行A.do_your_stuff()两次....
解决一:

# 使用自定义的next列表
D.next_class_list = [D,B,C,A]

class B(A):
def do_your_stuff(self):
next_class = self.find_out_whos_next()
next_class.do_your_stuff(self)
# do stuff with self for B

def find_out_whos_next(self):
l = self.next_class_list # l depends on the actual instance
mypos = l.index(B) 1 # Find this class in the list
return l[mypos+1] # Return the next one

解决二:
provides a class attribute __mro__ for each class, and a type called super. The __mro__ attribute is a tuple containing the class itself and all of its superclasses without duplicates in a predictable order. A super object is used in place of the find_out_whos_next() method.

class B(A): 1
def do_your_stuff(self):
super(B, self).do_your_stuff() 2
# do stuff with self for B

#...more super
class B(A):

def do_your_stuff(self):
self.__super.do_your_stuff()
# do stuff with self for B

B._B__super = super(B)

__mro__的优先顺序:


  1. If A is a superclass of B, then B has precedence over A. Or, B should always appear before A in all __mro__s (that contain both). In short let's denote this as B > A.

  2. If C appears before D in the list of bases in a class statement (eg. class Z(C,D):), then C > D.

  3. If E > F in one scenario (or one __mro__), then it should be that E > F in all scenarios (or all __mro__s).


这个优先顺序的计算按照下图中的例子就很容易算了:
mro
从左到右, 都要满足各串上的约束条件, 各串上"节点"的相对位置不能冲突, 互换某串上的节点位置, 可以此来决定类的继承顺序. 注: 得到的结果并不唯一.

下面还有些关于特殊方法的特殊使用, 包括如何自定义这样特殊方法, 如果继承内置类...详见原文.

== Lizzer ==
最后介绍个小工具. 叫做Lizzer, 因为和我英文名字类似. 所以这里介绍一下.
这个是一个js bookmarklet小工具,
lizzer
"""当你在当前页面中激活这款小产品, 他可以方便地位用户在几乎所有的Web邮件系统, 博客平台以及 Twitter 等任何支持富媒体编辑功能的站点上引用外站的内容(Google, Yahoo!, Flickr, Delicious, Docstoc ,Youtube etc.)到编辑器中.""" 可貌似这些大多都不能访问(如上图所示, 汗死也)...

更多js bookmarklet工具可另见http://www.showeb20.com/?p=2606

Tags: