| 说明:斜体表示in place
 第10章10.1 string.rstrip() 删除字符串末尾的空白string.strip() 删除字符串前后的空白
 string.replace(a,b)把a替换为b
 fileobject.readlines()返回行的列表
 10.2 open(filename,mode)mode常见的包括:
 “r”:表示只写
 “w”:表示只读,如果同名文件存在,那么久的文件会被覆盖
 “a”:表述在文件末尾附加
 “r+”:表示既可以读也可以写
 在省去mode后,默认为"w"
 10.4json.dump(content,fileobj)#fileobj是经过open的文件对象
 dump会覆盖旧文件内容
 content=json.load(fileobj)
 第11章课后作业1:函数测试
 def get_formulate_country_city(city,country,population=''):
    if population:
        mstr = city.title() + "," + country.title() + "-population" + str(population)
    else:
        mstr= city.title() + "," + country.title()
    return mstr
 import unittest
from city_function import get_formulate_country_city
class NamesTestCase(unittest.TestCase):
    def test_city_county_population(self):
        mstr=get_formulate_country_city("santiago","chile",100)
        self.assertEqual(mstr,"Santiago,Chile-population100")
    def test_city_county(self):
        mstr=get_formulate_country_city("santiago","chile")
        self.assertEqual(mstr,"Santiago,Chile")
unittest.main
 测试类注意点:1、测试单元必需以test_开头
 2、使用unittest.main而非unittest.mian(),py36会报no tests were found
 在unittest.TestCase类下的各种断言方法 assertEqual(a,b) 
assertNotEqual(a,b) 
assertTrue(x) 
assertFalse(x) 
assertIn(item,list) 
assertNotIn(item,list) 
 课后作业2:类测试
 class Employee():
    def __init__(self,last_name,first_name,employ_raise):
        self.name=first_name+last_name
        self.employ_raise=employ_raise
    def give_raise(self,default=50):
        self.employ_raise+=default
 import unittest
from employ import Employee
import random
class Test_employ_raise(unittest.TestCase):
    def setUp(self):
        self.emp=Employee("zp","s",100000)
    def test_give_default_raise(self):
        self.emp.give_raise(50000)
        self.assertEqual(self.emp.employ_raise,150000)
    def test_give_random_raise(self):
        g_raise=random.randint(50000,100000)
        self.emp.give_raise(g_raise)
        self.assertEqual(self.emp.employ_raise,100000+g_raise)
unittest.main
 |