mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
209 lines
5.8 KiB
Python
209 lines
5.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Dict, List
|
|
from app.common.constant import CommonConstant
|
|
|
|
|
|
class StringUtil:
|
|
"""
|
|
字符串工具类
|
|
"""
|
|
|
|
@classmethod
|
|
def is_blank(cls, string: str) -> bool:
|
|
"""
|
|
校验字符串是否为''或全空格
|
|
|
|
参数:
|
|
- string (str): 需要校验的字符串。
|
|
|
|
返回:
|
|
- bool: 校验结果。
|
|
"""
|
|
if string is None:
|
|
return False
|
|
str_len = len(string)
|
|
if str_len == 0:
|
|
return True
|
|
else:
|
|
for i in range(str_len):
|
|
if string[i] != ' ':
|
|
return False
|
|
return True
|
|
|
|
@classmethod
|
|
def is_empty(cls, string) -> bool:
|
|
"""
|
|
校验字符串是否为''或None
|
|
|
|
参数:
|
|
- string (str | None): 需要校验的字符串。
|
|
|
|
返回:
|
|
- bool: 校验结果。
|
|
"""
|
|
return string is None or len(string) == 0
|
|
|
|
@classmethod
|
|
def is_not_empty(cls, string: str) -> bool:
|
|
"""
|
|
校验字符串是否不是''和None
|
|
|
|
参数:
|
|
- string (str): 需要校验的字符串。
|
|
|
|
返回:
|
|
- bool: 校验结果。
|
|
"""
|
|
return not cls.is_empty(string)
|
|
|
|
@classmethod
|
|
def is_http(cls, link: str):
|
|
"""
|
|
判断是否为 http(s):// 开头
|
|
|
|
参数:
|
|
- link (str): 链接。
|
|
|
|
返回:
|
|
- bool: 是否为 http(s):// 开头。
|
|
"""
|
|
return link.startswith(CommonConstant.HTTP) or link.startswith(CommonConstant.HTTPS)
|
|
|
|
@classmethod
|
|
def contains_ignore_case(cls, search_str: str, compare_str: str):
|
|
"""
|
|
查找指定字符串是否包含指定字符串同时忽略大小写
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str (str): 比对的字符串。
|
|
|
|
返回:
|
|
- bool: 查找结果。
|
|
"""
|
|
if compare_str and search_str:
|
|
return compare_str.lower() in search_str.lower()
|
|
return False
|
|
|
|
@classmethod
|
|
def contains_any_ignore_case(cls, search_str: str, compare_str_list: List[str]):
|
|
"""
|
|
查找指定字符串是否包含列表中的任意一个字符串(忽略大小写)
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str_list (List[str]): 比对的字符串列表。
|
|
|
|
返回:
|
|
- bool: 查找结果。
|
|
"""
|
|
if search_str and compare_str_list:
|
|
return any([cls.contains_ignore_case(search_str, compare_str) for compare_str in compare_str_list])
|
|
return False
|
|
|
|
@classmethod
|
|
def equals_ignore_case(cls, search_str: str, compare_str: str):
|
|
"""
|
|
比较两个字符串是否相等(忽略大小写)
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str (str): 比对的字符串。
|
|
|
|
返回:
|
|
- bool: 比较结果。
|
|
"""
|
|
if search_str and compare_str:
|
|
return search_str.lower() == compare_str.lower()
|
|
return False
|
|
|
|
@classmethod
|
|
def equals_any_ignore_case(cls, search_str: str, compare_str_list: List[str]):
|
|
"""
|
|
判断指定字符串是否与列表中任意一个字符串相等(忽略大小写)
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str_list (List[str]): 比对的字符串列表。
|
|
|
|
返回:
|
|
- bool: 比较结果。
|
|
"""
|
|
if search_str and compare_str_list:
|
|
return any([cls.equals_ignore_case(search_str, compare_str) for compare_str in compare_str_list])
|
|
return False
|
|
|
|
@classmethod
|
|
def startswith_case(cls, search_str: str, compare_str: str):
|
|
"""
|
|
查找指定字符串是否以指定字符串开头
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str (str): 比对的字符串。
|
|
|
|
返回:
|
|
- bool: 查找结果。
|
|
"""
|
|
if compare_str and search_str:
|
|
return search_str.startswith(compare_str)
|
|
return False
|
|
|
|
@classmethod
|
|
def startswith_any_case(cls, search_str: str, compare_str_list: List[str]):
|
|
"""
|
|
查找指定字符串是否以列表中任意一个字符串开头
|
|
|
|
参数:
|
|
- search_str (str): 查找的字符串。
|
|
- compare_str_list (List[str]): 比对的字符串列表。
|
|
|
|
返回:
|
|
- bool: 查找结果。
|
|
"""
|
|
if search_str and compare_str_list:
|
|
return any([cls.startswith_case(search_str, compare_str) for compare_str in compare_str_list])
|
|
return False
|
|
|
|
@classmethod
|
|
def convert_to_camel_case(cls, name: str) -> str:
|
|
"""
|
|
将下划线大写方式命名的字符串转换为驼峰式;若输入为空则返回空字符串。
|
|
|
|
参数:
|
|
- name (str): 下划线大写方式命名的字符串。
|
|
|
|
返回:
|
|
- str: 转换后的驼峰式命名的字符串。
|
|
"""
|
|
if not name:
|
|
return ''
|
|
if '_' not in name:
|
|
return name[0].upper() + name[1:]
|
|
parts = name.split('_')
|
|
result = []
|
|
for part in parts:
|
|
if not part:
|
|
continue
|
|
result.append(part[0].upper() + part[1:].lower())
|
|
return ''.join(result)
|
|
|
|
@classmethod
|
|
def get_mapping_value_by_key_ignore_case(cls, mapping: Dict[str, str], key: str) -> str:
|
|
"""
|
|
根据忽略大小写的键获取字典中的对应的值
|
|
|
|
参数:
|
|
- mapping (Dict[str, str]): 字典。
|
|
- key (str): 字典的键。
|
|
|
|
返回:
|
|
- str: 字典键对应的值,未匹配则返回空字符串。
|
|
"""
|
|
for k, v in mapping.items():
|
|
if key.lower() == k.lower():
|
|
return v
|
|
|
|
return ''
|