Archive

Archive for March, 2009

分析日志常用到的几个shell工具

March 31st, 2009

一个不错的例子: http://nunojob.wordpress.com/2008/04/12/history-awk-print-2-sort-uniq-c-sort-rn-head/

httpd.log | awk '{print $2}' \  | sort | uniq -c | sort -rn | head

解释:
httpd.log : 要分析的日志
awk : 用来取出某一特定的列。简单的也可以用cut来代替
sort : 用来排序(第一次排序是用来为后面的uniq服务的。uniq对于没有排序的内容工作不正确)
uniq : 用来uniq有序的内容,-c参数会把重复次数带上
sort -rn : 用来安重复次数倒序排列
head : 用来去前几条数据,默认是10.

Python ,

Satchmo 新体验

March 30th, 2009

今天checkout最新的satchmo看了一下,发现变化还是很大的。看样子我们的BE是没法升级到最新的satchmo了…

还没有时间仔细研究,不过发现有几个satchmo用到的第三方的app值得注意一下:
1. django-registration

http://code.google.com/p/django-registration/

这个已经用过几次,感觉不错,而且感觉它的生命力很强。
2. django-values

http://code.google.com/p/django-values/

satchmo用这个替换了原来自己写的configuration模块。我一直认为satchmo的configuration模块有设计上的缺陷,结果这次被彻底换掉了,不错。有时间研究一下这个。
3. threaded_multihost

http://gosatchmo.com/apps/django-threaded-multihost/

multi-site aware features, 不是很懂,还得看看代码。

Python ,

[八卦] google music, 爽!

March 30th, 2009

今天看新闻发现google出了music搜索,于是试用了一把。 (http://www.google.cn/music/homepage)

搜歌,听歌,下载,一应俱全。
尤其是它的web播放器,用起来很舒服。

对于google的music搜索,有几个疑问:
1. 歌曲的下载地址不是链接到其他网站,而是统一链接到top100.cn
2. 上面可以下载的歌曲很新,不知道版权问题是如何解决的?

Python

Normal method, @staticmethod, @classmethod of python

March 18th, 2009

Reference:
http://snippets.dzone.com/posts/show/1679
http://www.geocities.com/foetsch/python/new_style_classes.htm

Check the doctest in these code bottom.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test the variable methods in class
"""
 
class K(object):
    """
    Test the variable methods in class
    >>> k_obj = K()
    >>> k_obj.method1()
    Normal method: obj.method1() becomes method1(obj)
    >>> K.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.K'>)
    >>> k_obj.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.K'>)
    >>> K.method3()
    staticmethod: K.method3() become just method3(None)
    >>> k_obj.method3()
    staticmethod: K.method3() become just method3(None)
    """
    # normal method (instance method)
    def method1(self):
        print 'Normal method: obj.method1() becomes method1(obj)'
 
    # class method
    @classmethod
    def method2(cls):
        print 'classmethod: K.method2() becomes method2(klass: %s)' % cls
 
    # static method
    @staticmethod
    def method3():
        print 'staticmethod: K.method3() become just method3(None)'
 
class KK(K):
    """
    Test the variable methods' behavior in the subclass.
    >>> kk_obj = KK()
    >>> kk_obj.method1()
    Normal method: obj.method1() becomes method1(obj)
    >>> KK.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.KK'>)
    >>> kk_obj.method2()
    classmethod: K.method2() becomes method2(klass: <class '__main__.KK'>)
    >>> KK.method3()
    staticmethod: K.method3() become just method3(None)
    >>> kk_obj.method3()
    staticmethod: K.method3() become just method3(None)
    """
    pass 
 
if __name__ == "__main__":
    import doctest
    doctest.testmod()

Python

Convert MP3 Id3 tag encoding

March 12th, 2009

It’s a long time pain for my mp3 files. I tried a lot of kinds way to convert my mp3 files’ id3 tag encoding. But every time when I need to convert the tag, I can’t remembered what I did last time. Now I’m going to record some way to do this.

I used amarok. Since the developers os amarok hate to deal with various kinds of encoding, they determined to use unicode which required id3v2.4 tag. This decision makes a lot of Chinese music displayed like a mess in the amarok because most of Chinese music used old version, different encode id3 tags.

I found some good ways to convert your music id3 tags here: http://wiki.linuxmce.org/index.php/Converting_MP3_Tags_To_UTF-8

The most way I like is mid3iconv

I used it like this:

For a single file

$mid3iconv -e gb2312 a.mp3 --remove-v1 -dp

For a directory

$find . -name "*.mp3" -exec mid3iconv -e gb2312 --remove-v1 -dp "{}" \;

-p option means dry-run, so if you really want to convert, remove this option.

Linux , ,

Install google toolbar in archlinux’s firefox

March 12th, 2009

I can’t install google toolbar in archlinux firefox3 for a long time. Today I resolved this issue by searched archlinux forum.

Reference:

http://bugs.archlinux.org/task/13759

Why I can’t install google toolbar?

Google can’t recogonized my firefox version correct, that’s because archlinux mark firefox as “GranParadiso”.

How to change that?
1. Type “about:config” in the url bar of firefox.
2. Search “general.useragent.extra.firefox” key, and change the value from “GranParadiso/3.0.x” to “Firefox/3.0.x”
3. Then go to google toolbar install page and click install.

Linux ,

A good example to explain *args and **kwargs in python

March 10th, 2009

I read this wonderful example here: http://www.megasolutions.net/python/-args-and—kwargs-78766.aspx

>>> def a(*stuff):
                 print repr(stuff)
>>> def b(**stuff):
                 print repr(stuff)
>>> def c(*args, **kwargs):
                 print 'args', repr(args)
                 print 'kwargs', repr(kwargs)
>>> a(1,2,3)
(1, 2, 3)
>>> b(hello='world', lingo='python')
{'hello': 'world', 'lingo': 'python'}
>>> c(13,14,thenext=16,afterthat=17)
args (13, 14)
kwargs {'afterthat': 17, 'thenext': 16}
>>> args = [1,2,3,4]
>>> kwargs = {'no-way': 23, 'yet-anotherInvalid.name': 24}
>>> c(*args, **kwargs)
args (1, 2, 3, 4)
kwargs {'no-way': 23, 'yet-anotherInvalid.name': 24}

Here is some simple explaination about *args and **kwargs

Basically ‘args’ is a tuple with all the positional arguments, kwargs is a dictionary with all the named arguments.
Likewise you can pass a tuple to a function like func(*tuple), or a dict like func(**dictionary) or both, where the zuple has to come first.

Python

FireStats icon Powered by FireStats