Sunday, May 23, 2010

Cancel turning email address into links in zim

find file /usr/lib/pymodules/python2.6/zim/parsing.py

delete @ pattern

url_re = Re(r'''(
    \b \w[\w\+\-\.]+:// %(c)s* \[ %(c)s+ \] (?: %(c)s+ [\w/] )?  |
    \b \w[\w\+\-\.]+:// %(c)s+ [\w/]                             |
    \b mailto: %(c)s+ \@ %(c)s* \[ %(c)s+ \] (?: %(c)s+ [\w/] )? |
    \b mailto: %(c)s+ \@ %(c)s+ [\w/]
)''' % _classes, re.X)

#url_re = Re(r'''(
#    \b \w[\w\+\-\.]+:// %(c)s* \[ %(c)s+ \] (?: %(c)s+ [\w/] )?  |
#    \b \w[\w\+\-\.]+:// %(c)s+ [\w/]                             |
#    \b mailto: %(c)s+ \@ %(c)s* \[ %(c)s+ \] (?: %(c)s+ [\w/] )? |
#    \b mailto: %(c)s+ \@ %(c)s+ [\w/]                            |
#    \b %(c)s+ \@ %(c)s+ \. \w+ \b
#)''' % _classes, re.X)

Wednesday, May 19, 2010

pylons middleware to strip HTML comments

import re
from webob import Request

class StripHtmlCommentsMiddleware:
    """
    Strips all html comments from response content.
    """
    def __init__(self, app):
        self.app = app
        self.htmlcomments = re.compile(
            '\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>')

    def __call__(self, environ, start_response):
        req = Request(environ)
        rsp = req.get_response(self.app)
        if "text/html" == rsp.content_type:
            new_content = self.htmlcomments.sub('', rsp.unicode_body)
            rsp.unicode_body = new_content
        return rsp(environ, start_response)

Thursday, May 13, 2010

when the default value of args assigned?

>>> def getid():
... print "in getid"
... return uuid.uuid1()
...
>>> class test(object):
... def __init__(self, id = getid()):
... self.id = id
...
in getid
>>>
>>> a = test()
>>>
>>> a.id
UUID('f20f40b2-5e8a-11df-be97-001f161eed5b')
>>>
>>> b = test()
>>>
>>> b.id
UUID('f20f40b2-5e8a-11df-be97-001f161eed5b')

Saturday, May 1, 2010

little tip of associating amule to firefox

sudo apt-get install amule-gnome-support

"network.protocol-handler" items take no  effect on my firefox 3.5...

Tuesday, April 27, 2010

persistent_id of cPickle

    cPickle’s persistent_id must return a string in one line, or load will raise a truncated exception. So you can use base64.b64encode to encode your result in to one line string. Base64.encode returns multi-line result.

Sunday, April 25, 2010

Little tip of Mako template

    “selected” is always used to indicate selected state of option in SELECT HTML component. So it’s useful to write “selected” in one line as its option tag. Following code will generate this:

<%def name="optionselected(x)"><%if x: __M_writer(u"selected")%></%def>

<option value="python" ${optionselected(c.id == "python")}></option>

Confusion about amqplib properties

    Amqplib 0.6.1 introduces some properties when sending/receiving message in Message class. But when serializing “longstr” type properties, it did not use “utf-8”. So be careful to use its “application_headers” property, all keys in this diction will encode into “utf-8”, and values remain plain string.

Code snippets from amqplib 0.6.1:

def read_shortstr(self):
        """
        Read a utf-8 encoded string that's stored in up to
        255 bytes.  Return it decoded as a Python unicode object.

        """
        self.bitcount = self.bits = 0
        slen = unpack('B', self.input.read(1))[0]
        return self.input.read(slen).decode('utf-8')


    def read_longstr(self):
        """
        Read a string that's up to 2**32 bytes, the encoding
        isn't specified in the AMQP spec, so just return it as
        a plain Python string.

        """
        self.bitcount = self.bits = 0
        slen = unpack('>I', self.input.read(4))[0]
        return self.input.read(slen)