Python – 夏清然的日志 https://www.qingran.net Xia Qingran Geek Blog Sun, 07 Aug 2016 09:50:33 +0000 en-US hourly 1 https://wordpress.org/?v=4.6.1 112893047 删除Python easy_install安装软件 https://www.qingran.net/2011/01/%e5%88%a0%e9%99%a4python-easy_install%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/ https://www.qingran.net/2011/01/%e5%88%a0%e9%99%a4python-easy_install%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/#comments Wed, 12 Jan 2011 14:48:51 +0000 https://www.qingran.net/?p=791 python的easy_install是很方便的安装体系,可是一般来说setup.py没有deinstall的选项,那么如何删除已经安装的egg呢?

easy_install -mxN Genshi
然后下面类似的提示:

install_dir /usr/local/lib/python2.6/dist-packages/
Processing Genshi-0.6-py2.6.egg
Removing Genshi 0.6 from easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/Genshi-0.6-py2.6.egg

Because this distribution was installed --multi-version, before you can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one of
these examples, in order to select the desired version:

    pkg_resources.require("Genshi")  # latest installed version
    pkg_resources.require("Genshi==0.6")  # this exact version
    pkg_resources.require("Genshi>=0.6")  # this version or higher

然后删除目录:

rm -rf /usr/local/lib/python2.6/dist-packages/Genshi-0.6-py2.6.egg

]]>
https://www.qingran.net/2011/01/%e5%88%a0%e9%99%a4python-easy_install%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/feed/ 1 791
Python.org被墙,替换docs和code下载地址 https://www.qingran.net/2010/04/python-org%e8%a2%ab%e5%a2%99%e6%9b%bf%e6%8d%a2docs%e5%92%8ccode%e4%b8%8b%e8%bd%bd%e5%9c%b0%e5%9d%80/ https://www.qingran.net/2010/04/python-org%e8%a2%ab%e5%a2%99%e6%9b%bf%e6%8d%a2docs%e5%92%8ccode%e4%b8%8b%e8%bd%bd%e5%9c%b0%e5%9d%80/#comments Sat, 17 Apr 2010 03:32:05 +0000 https://www.qingran.net/?p=280 http://www.python.org/download/和http://www.python.org/doc/长久以来被“墙“,替换的下载地址是:

Python 2.6.x

python 2.6.5 windows installer i386

python 2.6.5 windows installer amd64

python 2.6.5 source code bzipped

Python 3.x

python 3.1.2 windows installer i386

python 3.1.2 windows installer amd64

python 3.1.2 source code bzipped tarball

Docs

python 2.6.5 Docs PDF

python 3.1.2 Docs PDF

]]>
https://www.qingran.net/2010/04/python-org%e8%a2%ab%e5%a2%99%e6%9b%bf%e6%8d%a2docs%e5%92%8ccode%e4%b8%8b%e8%bd%bd%e5%9c%b0%e5%9d%80/feed/ 1 280
GNU/Linux下更好的使用Thinkpad的键盘灯 https://www.qingran.net/2009/08/gnulinux%e4%b8%8b%e6%9b%b4%e5%a5%bd%e7%9a%84%e4%bd%bf%e7%94%a8thinkpad%e7%9a%84%e9%94%ae%e7%9b%98%e7%81%af/ https://www.qingran.net/2009/08/gnulinux%e4%b8%8b%e6%9b%b4%e5%a5%bd%e7%9a%84%e4%bd%bf%e7%94%a8thinkpad%e7%9a%84%e9%94%ae%e7%9b%98%e7%81%af/#respond Tue, 11 Aug 2009 06:37:09 +0000 https://www.qingran.net/?p=66 让广大“黑友”的thinklight更好的发挥作用。

Thinkpad屏幕顶部的键盘灯是个很有创意的设计,这个和小红点一起构成了Thinkpad的重要特色功能。

比如收到邮件后,thinklight闪烁;pidgin收到消息后,thinklight闪烁提醒。

pidgin通过pidgin-blinklight插件即可实现。

debian/ubuntu用户需要安装:

#apt-get install pidgin-blinklight

然后在pidgin的”工具” -> “插件”内启用,这样在pidgin收到消息时thinklight能闪烁三下。

同时在收到Email的时候也可以让thinklight来进行提醒,如果是evolution的,使用thinklight-notification 就可以实现,详细见:http://ubuntuforums.org/showthread.php?t=1017263

下面详细说一下thunderbird收到邮件时的thinklight闪烁。

安装thunderbird插件yamb,http://www.globs.org/download.php?lng=en。

进入thunderbird,在“工具” -> “附加软件”,进入“Yet Another Mail Biff”的配置,在”External notifier executable”内加入如下代码的执行路径:

=============================================
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import time
import sys
import signal

TL_PROC_FILE =  “/proc/acpi/ibm/light”
PID_FILE =  “/tmp/thinklightblink.pid”

def blink(second, filepath = TL_PROC_FILE):
try:
# open light
fp = open(filepath, “w”)
fp.seek(0)
fp.write(“on”)
fp.flush()

time.sleep(second)

fp.seek(0)
fp.write(“off”)
fp.close()
return
except Exception, inst:
print inst
return

def writepid(filepath = PID_FILE):
try:
pid = os.getpid()
fp = open(filepath, “w”)
fp.write(str(pid))
fp.close()
return
except Exception, inst:
print inst
return

def delpid(filepath = PID_FILE):
try:
if os.path.isfile(filepath):
os.remove(filepath)

return
except Exception, inst:
print inst
return

def getpid(filepath = PID_FILE):
try:
if os.path.isfile(filepath):
return int(open(filepath).read())
else:
return None
except Exception, inst:
print inst
return None

if __name__ == “__main__”:
try:
os.kill(getpid(), signal.SIGTERM)
except:
pass

writepid()

blink(1)
delpid()
=============================================

完成。测试一下thunderbird收到邮件时thinklight是否会闪动一下 😀

手动操作thinklight的方法:
echo ‘on’ > /proc/acpi/ibm/light
echo ‘off’ > /proc/acpi/ibm/light
echo 255 > /sys/class/leds/tpacpi::thinklight/brightness
echo 0 > /sys/class/leds/tpacpi::thinklight/brightness

参考文档:
http://www.thinkwiki.org/wiki/ThinkLight
http://blog.wahlig.eu/2008/04/thinklight-mail-notification.html

]]>
https://www.qingran.net/2009/08/gnulinux%e4%b8%8b%e6%9b%b4%e5%a5%bd%e7%9a%84%e4%bd%bf%e7%94%a8thinkpad%e7%9a%84%e9%94%ae%e7%9b%98%e7%81%af/feed/ 0 66
django model加入cache https://www.qingran.net/2009/06/django-model%e5%8a%a0%e5%85%a5cache/ https://www.qingran.net/2009/06/django-model%e5%8a%a0%e5%85%a5cache/#respond Fri, 12 Jun 2009 08:09:14 +0000 https://www.qingran.net/?p=117 近来用django开发不少,对其自带自带的”django.middleware.cache.UpdateCacheMiddleware” 和”django.middleware.cache.FetchFromCacheMiddleware”感觉很不爽,原因有两个:
0,cache的过期控制只能通过超时时间进行,而不能主动通知;
1,全页面的cache粒度太粗。

遂想到django统一的models抽象应该很方便对db的cache进行统一处理,所以就有以下的想法:

目前设计的cache存储的数据结构是按db的行进行cache,cache采用memcached作为存储方式,其数据结构是:

{pointer_key -> model_key}, {model_key -> model}。

point_key:以get方法查询的表名和查询参数为基础构建,例如:’user-{‘username’:’qingran}’或者 ‘user-{‘id’:1}’;
model_key:以数据库的表名和表的primary key为基础构建,例如:’user-1’,’user-2’;
model:就是models class的内容了,在db中就是符合primary key的数据行。

这样的结构能使不同的get参数只要是查询同一个表的同一个行,那么就对应同一个model数据。

cache的命中和过期:
在get的方法,现查{point_key -> model_key},然后查询{model_key -> model}最终得到数据。如果有一步命中失败,就从db取,然后回写cache。
在models进行save和delete方法的时候把{model_key -> model}的对应删除。

但是这样的存储结构目前只能缓存models.Model.objects的get方法请求。而无法对filter方法进行cache。filter cache的难点在于这个查询的结果是一个集合,而集合中的任何一个元素的修改或者新添加一个符合filter查询的数据,都会导致cache失效。

所以说解决filter查询的cache需要解决以下问题:

0,save()和delete()方法中发生时能够快速获知filter的cache是否需要立即更新。
可能”需要手动维护所有filter相关缓存的一个key清单,当你有相关数据发生变动时手动清理相关key。”
另外新增加的符合filter查询要求的元素也会引起cache的失效。

1,因为一个filter查询集合内一个元素的失效就清理整个filter集合可能会cache的更新过于频繁。

啰嗦了一堆,上代码:
===========================================
from django.core.cache import cache
from django.db import models
from django.conf import settings

DOMAIN_CACHE_PREFIX = settings.CACHE_MIDDLEWARE_KEY_PREFIX
CACHE_EXPIRE = settings.CACHE_MIDDLEWARE_SECONDS

def cache_key(model, id):
return (“%s-%s-%s” % (DOMAIN_CACHE_PREFIX, model._meta.db_table,
id)).replace(” “, “”)

class GetCacheManager(models.Manager):
# to reload the get method. cache -> db -> cache
def get(self, *args, **kwargs):
id = repr(kwargs)

# in mc, data are stored in {pointer_key -> model_key},{model_key -> model}
# pointer_key is object.get’s method parameters.
# pointer_key = ‘www-user-{‘username’:’qingran}’ or ‘www-user-{‘id’:1}’
pointer_key = cache_key(self.model, id)

# model_key is “<prefix>-<db tablename>-pk”
# model_key = ‘www-user-1’
model_key = cache.get(pointer_key)

if model_key != None:
model = cache.get(model_key)
if model != None:
return model

# cache MISS, get from db.
model = super(GetCacheManager, self).get(*args, **kwargs)

# write data back to cache from db.
if not model_key:
model_key = cache_key(model, model.pk)
cache.set(pointer_key, model_key, CACHE_EXPIRE)

cache.set(model_key, model, CACHE_EXPIRE)

return model

class ModelWithGetCache(models.Model):
# to reload the save method
def save(self, *args, **kwargs):
# first, delete cache {model_key -> model}
model_key = cache_key(self, self.pk)
cache.delete(model_key)

super(ModelWithGetCache, self).save()

# to reload the delete method
def delete(self, *args, **kwargs):
# first, delete cache {model_key -> model}
model_key = cache_key(self, self.pk)
cache.delete(model_key)

super(ModelWithGetCache, self).delete()

===============================================

在使用的时候定义models需要改从ModelWithGetCache继承,并且指定objects = GetCacheManager()

Sample:
class User(ModelWithGetCache):
objects = GetCacheManager()

]]>
https://www.qingran.net/2009/06/django-model%e5%8a%a0%e5%85%a5cache/feed/ 0 117