西维蜀黍

【Python】print

基础用法

print语句可以向屏幕上输出指定的文字

例如:

print 'Hello World!'
  ...


【Python】断言(assert)

Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。

断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况,例如我们的代码只能在 Linux 系统下运行,可以先判断当前系统是否符合条件。

语法格式如下:

assert expression

等价于:

if not expression:
    raise AssertionError

assert 后面也可以紧跟参数:

assert expression [, arguments]

等价于:

if not expression:
    raise AssertionError(arguments)

以下为 assert 使用实例:

>>> assert True     # 条件为 true 正常执行
>>> assert False    # 条件为 false 触发异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert 1==1    # 条件为 true 正常执行
>>> assert 1==2    # 条件为 false 触发异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

>>> assert 1==2, '1 不等于 2'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: 1 不等于 2
>>>
  ...


【WordPress】修改站点域名

  ...


【Python】常见错误

Variable

未定义的 variable

>>> len(abc)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'abc' is not defined

Dict

不存在的 Key

>>> a = {}
>>> a["s"]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: 's'

Function

未定义的函数

>>> fun()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'fun' is not defined
>>> a = None
>>> a()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'NoneType' object is not callable
  ...


【MySQL】Lock Demo(锁实验)- Read

Locking Read

If you query data and then insert or update related data within the same transaction, the regular SELECT statement does not give enough protection. Other transactions can update or delete the same rows you just queried. InnoDB supports two types of locking reads that offer extra safety:

  1. select ... for share
  2. select ... for update

It has the potential to produce a deadlock, depending on the isolation level of the transaction. The opposite of a non-locking read.

  ...


【Python】Basics - 特殊变量(Special Variables)

  ...


【Python】魔术方法(Magic Methods)

Python的魔术方法一般以__methodname__的形式命名,如:__init__(构造方法)、 __getitem____setitem____delitem__(调用del obj[key]时对应触发的函数)、 __len__(对类实例调用len(…)时对应触发的函数)等。

魔术方法具体有:

  • __init__(self):构造方法
  • __del__ :析构函数,释放对象时使用
  • __getitem__(self,key):返回键对应的值(按照索引获取值)
  • __setitem__(self,key,value):设置给定键的值(按照索引赋值)
  • __delitem__(self,key):删除给定键对应的元素
  • __len__():对类实例调用len(…)时对应触发的函数
  • __cmp_: 比较运算
  • __call_:调用
  • __add__:加运算
  • __sub__:减运算
  • __mul__:乘运算
  • __div__:除运算
  • __mod__:求余运算
  • __pow__:幂

需要注意的是,这些成员里面有些是方法,调用时要加括号,有些是属性,调用时不需要加括号。

  ...


【Python】import 问题排查

解决思路

1

使用pip list查看本项目或者系统中已经安装了未找到的模块,这里具体又分为使用了 virenv 和未使用的情况。

2

直接在 terminal 中进入到当前工作文件夹(cd <your target folder>),测试以下 import:

$ python
Python 2.7.17 (default, Oct 24 2019, 12:57:47)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import <your target module name>
  ...


【Python】pytest - 运行错误

  ...


【Python】单元测试框架 - pytest

一个实例

# content of test_sample.py
 
def func(x):
    return x+1
 
def test_func():
    assert func(3) == 5

再一个实例

当需要编写多个测试样例的时候,我们可以将其放到一个测试类当中,如:

# content of test_class.py

class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

我们可以通过执行测试文件的方法,执行上面的测试:

$ py.test -q test_class.py
.F                                                                                                                                                  [100%]
======================================================================== FAILURES =========================================================================
___________________________________________________________________ TestClass.test_two ____________________________________________________________________

self = <testAA.TestClass instance at 0x10dd68d40>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

testAA.py:8: AssertionError
1 failed, 1 passed in 0.05 seconds
  ...