2021-07-05《python编程 从入门到实践》第10,11章 笔记

说明:
斜体表示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.4
json.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) #核实a==b
assertNotEqual(a,b) #核实a!=b
assertTrue(x) #核实x为True
assertFalse(x) #核实x为Flase
assertIn(item,list) #核实item在list中
assertNotIn(item,list) #核实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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值