且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的第一个python web开发框架(13)——工具函数包说明(四)

更新时间:2022-05-26 02:34:53

 string_helper.py是字符串操作包,主要对字符串进行检查、过滤和截取等处理。

我的第一个python web开发框架(13)——工具函数包说明(四) View Code

  check_string()函数主要是用来检查字符串是否符合指定规则用的,它被is_开头的各个函数所调用。is_开头的几个函数怎么使用,请看测试用例。

我的第一个python web开发框架(13)——工具函数包说明(四)
#!/usr/bin/evn python
# coding=utf-8

import unittest
from common import string_helper


class StringHelperTest(unittest.TestCase):
    """字符串操作包测试类"""

    def setUp(self):
        """初始化测试环境"""
        print('------ini------')

    def tearDown(self):
        """清理测试环境"""
        print('------clear------')

    def test_is_email(self):
        self.assertEqual(string_helper.is_email('aaaaa'), False)
        self.assertEqual(string_helper.is_email('aaaa@xxx.com'), True)
        self.assertEqual(string_helper.is_email('xxx@xxx.com.xx'), True)

    def test_is_phone(self):
        self.assertEqual(string_helper.is_phone('aaaaa'), False)
        self.assertEqual(string_helper.is_phone('12345678'), False)
        self.assertEqual(string_helper.is_phone('01012345678'), True)
        self.assertEqual(string_helper.is_phone('010-123456'), False)
        self.assertEqual(string_helper.is_phone('010-12345678'), True)
        self.assertEqual(string_helper.is_phone('010 12345678'), True)
        self.assertEqual(string_helper.is_phone('0757 12345678'), True)

    def test_is_mobile(self):
        self.assertEqual(string_helper.is_mobile('aaaaa'), False)
        self.assertEqual(string_helper.is_mobile('123456789'), False)
        self.assertEqual(string_helper.is_mobile('13012345678'), True)
        self.assertEqual(string_helper.is_mobile('14012345678'), False)

    def test_is_letters(self):
        self.assertEqual(string_helper.is_letters('123456'), False)
        self.assertEqual(string_helper.is_letters('1ds2f12sdf'), False)
        self.assertEqual(string_helper.is_letters('absbdsf'), True)
        self.assertEqual(string_helper.is_letters('ADdfFSds'), True)

    def test_is_idcard(self):
        self.assertEqual(string_helper.is_idcard('123456789'), False)
        self.assertEqual(string_helper.is_idcard('aaaaaaaaa'), False)
        self.assertEqual(string_helper.is_idcard('340223190008210470'), False)
        self.assertEqual(string_helper.is_idcard('34022319000821047X'), True)
        

if __name__ == '__main__':
    unittest.main()
我的第一个python web开发框架(13)——工具函数包说明(四)

 

  filter_str()函数用来将指定的特殊字符全部过滤掉

    def test_filter_str(self):
        print(string_helper.filter_str('aaa'))
        print(string_helper.filter_str('aaa<>&\''))
        print(string_helper.filter_str('aaa<|>|&|%|~|^|;|\''))

  执行结果:

我的第一个python web开发框架(13)——工具函数包说明(四)
------ini------
aaa
aaa
aaa
------clear------
我的第一个python web开发框架(13)——工具函数包说明(四)

 

  filter_tags函数将代码上的全部html标签过滤掉(网上找到来的代码)

    def test_filter_tags(self):
        print(string_helper.filter_tags('<html><body><b>aaa</b></body></html>'))

  执行结果:

------ini------
aaa
------clear------

 

  string()函数主要用于拼接sql语句用的,用于在字符串的两边添加 ' 这个单撇号,如果is_return_null这个参数为True时,输入内容为空则返回null字符

我的第一个python web开发框架(13)——工具函数包说明(四)
    def test_string(self):
        print(string_helper.string(-1))
        print(string_helper.string({'test': 'abc'}))
        print(string_helper.string(''))
        print(string_helper.string('aaa'))
        print(string_helper.string('', True))
我的第一个python web开发框架(13)——工具函数包说明(四)

  执行结果:(使用print打印到控制台的结果,字符串不输出""双引号,实际上存储到变量中时,下面内容都会加上双引号

我的第一个python web开发框架(13)——工具函数包说明(四)
------ini------
'-1'
'{'test': 'abc'}'
''
'aaa'
null
------clear------
我的第一个python web开发框架(13)——工具函数包说明(四)

 

  cut_str()函数会将输入的字符串按指定长度截取

我的第一个python web开发框架(13)——工具函数包说明(四)
    def test_cut_str(self):
        print(string_helper.cut_str('', 5))
        print(string_helper.cut_str('aaa', 5))
        print(string_helper.cut_str('将字符串截取指定长度', 5))
        print(string_helper.cut_str('aa将字符串截取指定长度', 5))
我的第一个python web开发框架(13)——工具函数包说明(四)

  执行结果:

我的第一个python web开发框架(13)——工具函数包说明(四)
------ini------

aaa
将字符串截
aa将字符
------clear------
我的第一个python web开发框架(13)——工具函数包说明(四)

 

 

  verify_helper.py是验证码生成包,调用比较简单,这里就不再详细说明,到后面章节会有详细例子。

 

  web_helper.py是web操作包,主要是对web服务进行相关处理。它需要启动web服务后基于web服务下才行进行测试操作,不能直接运行测试用例进行测试,大家可以先了解一下里面函数的功能。

我的第一个python web开发框架(13)——工具函数包说明(四) View Code

  get_ip():获取当前客户端ip地址

  get_session():获取当前客户的session

  return_msg():生成统一的返回给客户端的内容(json格式)。输出内容有state:状态码,一般使用-1表示出现错误,0表示正常,可以根据需要进行修改或添加更多的状态码;msg:状态文说明,出错时返回出错内容提示;data:需要返回的其他内容全部会放在这里。

  return_raise():当调用这个函数时,会直接终于代码的执行,直接将结果输出到客户端。

  get_form():获取客户端Form方式提交的参数值

  get_query():获取客户端Get方式提交的参数值


    本文转自 AllEmpty 博客园博客,原文链接:http://www.cnblogs.com/EmptyFS/p/7687691.html,如需转载请自行联系原作者