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