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

简介:

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

  View Code

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

复制代码
#!/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()
复制代码

 

  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<|>|&|%|~|^|;|\''))

  执行结果:

复制代码
------ini------
aaa
aaa
aaa
------clear------
复制代码

 

  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字符

复制代码
    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))
复制代码

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

复制代码
------ini------
'-1'
'{'test': 'abc'}'
''
'aaa'
null
------clear------
复制代码

 

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

复制代码
    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))
复制代码

  执行结果:

复制代码
------ini------

aaa
将字符串截
aa将字符
------clear------
复制代码

 

 

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

 

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

  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,如需转载请自行联系原作者





相关文章
|
6天前
|
Python
高阶函数如`map`, `filter`, `reduce`和`functools.partial`在Python中用于函数操作
【6月更文挑战第20天】高阶函数如`map`, `filter`, `reduce`和`functools.partial`在Python中用于函数操作。装饰器如`@timer`接收或返回函数,用于扩展功能,如记录执行时间。`timer`装饰器通过包裹函数并计算执行间隙展示时间消耗,如`my_function(2)`执行耗时2秒。
14 3
|
3天前
|
测试技术 开发者 Python
Python中的装饰器:提升函数的灵活性和可重用性
在Python编程中,装饰器是一种强大的工具,它可以在不修改函数本身的情况下,动态地扩展函数的功能。本文将介绍装饰器的工作原理及其在实际开发中的应用,帮助读者更好地理解和利用这一特性。
|
3天前
|
JSON API 数据库
Python使用Quart作为web服务器的代码实现
Quart 是一个异步的 Web 框架,它使用 ASGI 接口(Asynchronous Server Gateway Interface)而不是传统的 WSGI(Web Server Gateway Interface)。这使得 Quart 特别适合用于构建需要处理大量并发连接的高性能 Web 应用程序。与 Flask 类似,Quart 也非常灵活,可以轻松地构建 RESTful API、WebSockets、HTTP/2 服务器推送等。
|
2天前
|
存储 Python
在Python中,匿名函数(lambda表达式)是一种简洁的创建小型、一次性使用的函数的方式。
【6月更文挑战第24天】Python的匿名函数,即lambda表达式,用于创建一次性的小型函数,常作为高阶函数如`map()`, `filter()`, `reduce()`的参数。lambda表达式以`lambda`开头,后跟参数列表,冒号分隔参数和单行表达式体。例如,`lambda x, y: x + y`定义了一个求和函数。在调用时,它们与普通函数相同。例如,`map(lambda x: x ** 2, [1, 2, 3, 4, 5])`会返回一个列表,其中包含原列表元素的平方。
13 4
|
3天前
|
JSON 数据格式 索引
Python内置函数如`print()`输出信息,`len()`计算长度
【6月更文挑战第23天】Python内置函数如`print()`输出信息,`len()`计算长度,`type()`识别类型,`range()`生成序列,`sum()`求和,`min()`和`max()`找极值,`abs()`取绝对值,`round()`四舍五入,`sorted()`排序,`zip()`和`enumerate()`组合及遍历,`map()`和`filter()`应用函数。标准库如`os`用于操作系统交互,`sys`处理解释器信息,`math`提供数学运算,`re`支持正则表达式,`json`处理JSON数据。学习这些能提升编程效率。
17 5
|
2天前
|
Python
在Python中,高阶函数是指那些可以接受一个或多个函数作为参数,并返回一个新的函数的函数。
【6月更文挑战第24天】Python的高阶函数简化代码,增强可读性。示例:`map()`检查用户名合法性,如`[&quot;Alice&quot;, &quot;Bob123&quot;, &quot;Charlie!&quot;, &quot;David7890&quot;]`;`reduce()`与`lambda`结合计算阶乘,如1到10的阶乘为3628800;`filter()`找出1到100中能被3整除的数,如[3, 6, 9, ..., 99]。
12 3
|
1天前
|
缓存 前端开发 JavaScript
Parcel-极速零配置Web应用打包工具
Parcel-极速零配置Web应用打包工具
5 1
|
2天前
|
分布式计算 大数据 调度
MaxCompute产品使用问题之为什么用python写的udf函数跑起来比本地还要慢
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
6天前
|
数据安全/隐私保护 Python
Python装饰器是高阶函数,用于在不修改代码的情况下扩展或修改函数行为。它们提供可重用性、模块化和无侵入性的功能增强。
【6月更文挑战第20天】Python装饰器是高阶函数,用于在不修改代码的情况下扩展或修改函数行为。它们提供可重用性、模块化和无侵入性的功能增强。例如,`@simple_decorator` 包装`my_function`,在调用前后添加额外操作。装饰器还能接受参数,如`@logged(&quot;INFO&quot;, &quot;msg&quot;)`,允许动态定制功能。
16 6