[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 ...

[python] 函数陷阱

Date Category python

本文说的这几个问题适用于 python 2.6+

UnboundLocalError

In [1]: a = 1
In [2]: def func():
   ...:     print(a)
   ...:     a = 2
   ...:
In [3]: func()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-08a2da4138f6> in <module>()
----> 1 func()
<ipython-input-2-9e6cf545bc05> in func()
      1 def func():
----> 2     print(a)
      3     a ...
more ...

[linux]配置 vsftp 启用被动模式(passive mode)解决客户端"no route to host"错误

系统: CentOS

  • 修改 vsftpd.conf,启用被动模式,指定端口:

    pasv_min_port=12000
    pasv_max_port=12199
    pasv_enable=YES
    
  • 配置防火墙 iptables,允许端口访问:

    iptables -I INPUT -p tcp --dport 12000:12199 -j ACCEPT
    service iptables save
    

参考资料

more ...