[python] 定义抽象基类(Abstract Base Classes)

Date Category python

抽象基类一般用于规定子类必须重新定义某些方法。比如 web 框架中的 cache 部分的基类一般类似下面这样:

class BaseCache(object):
    def get(self, key):
        raise NotImplementedError('subclasses of BaseCache must provide a get() method')

    def set(self, key, value, timeout=60):
        raise NotImplementedError('subclasses of BaseCache must provide a set() method')


class MemcachedCache(BaseCache):
    def get(self, key):
        value = self._cache ...
more ...

[database] postgresql 常用操作

安装

sudo apt-get install postgresql-client
sudo apt-get install postgresql

启动

sudo service postgresql start

进入控制台

sudo -u postgres psql

psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432

退出

postgres=# \q

创建用户

sudo -u postgres createuser dbuser

sudo -u postgres psql
postgres ...
more ...

[python] 模块间互相导入的问题

Date Category python

两个模块间互相导入时,可能会出现如下的问题:

# a.py
from b import y
print y
x = 5

# b.py
from a import x
print x
y = 10

>>> import b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "b.py", line 1, in <module>
    from a import x
  File "a.py", line ...
more ...