栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 项目基础应用封装1(实例20个可直接应用tcy)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 项目基础应用封装1(实例20个可直接应用tcy)

#!/usr/bin/env python3.x
# -*- coding: utf-8 -*-

# @Project:TcyQuantTrade
# @Module:py_op

# @Author:       tcy
# @Date:         2022/3/20  11:47
# @Emial:        3615693665@qq.com
# @Version:     1.18
# @Last Modified time:
# @City:           China Shanghai Songjiang Xiaokunshan
# @Company:  Weishi Machinery Manufacturing Priority

# from types import an
import re
from typing import Any, List, Tuple, Set, Callable, Union, Deque, FrozenSet, MappingView, KeysView, ItemsView, ValuesView, ContextManager, Dict, ClassVar, Dict, Optional
from collections import Counter, ChainMap
import copy
import math


def int_(x: Union[int, float, str], base: int = None):
    """
    int(x=0)  # x为整数浮点数
    int(x,base = 10)  # 返回字符串x构造的整数对象

    实例:
    a = int('0b100110111', 2)  # 返回int   311
    int('0b1_0011_0111', 2)  # 允许为数字增加下划线
    a = int('0o377', 8)  # 返回int   255
    a = int('0xff', 16)  # 返回int   255
    """
    if isinstance(x, (int, float)):
        return int(x)
    else:
        if base is not None:
            return int(x, base)
        base = {'0b': 2, '0o': 8, '0x': 16, '0B': 2, '0O': 8, '0X': 16}
        return int(x, base[x[:2].lower()])


def int_to_str(x: int, isbin=False, isoct=False, ishex=False):
    if isbin:
        return bin(x)
    elif isoct:
        return oct(x)
    elif ishex:
        return hex(x)
    else:
        return str(x)


def float_(x: Union[int, str], ishex=False):
    """
    float('+1.23')     # 1.23
    float('1e-003')   # 0.001
    float('+1E6')     # 1000000.0
    float('-Inf')         # inf
    float('nan')        # nan
    float.fromhex(str)  # 返回由十六进制字符串s表示float; s可有前导和尾随空白
    """
    if isinstance(x, (int, float)):
        return float(x)
    if ishex:
        return float.fromhex(x)


def float_to_str(x: float, precision: int = 0, is_pre_cent=False, ishex=False):
    if is_pre_cent:
        return ('{:.%s' % precision + '%}').format(x)
    elif ishex:
        return x.hex()
    else:
        return f'{x:.{precision}}'


def to_str(x: Union[str, int, float], precision: int = 0,
           is_pre_cent=False, isbin=False, isoct=False, ishex=False):
    """
    用途:将str,int,float转为str
    参数:
    x:str原样输出,除int,float外的类型输出str(x);忽略其余参数
    x:int忽略prectision,is_pre_cent
    x:float 启用全部参数
    prectision:float输出精度
    is_pre_cent:bool 是否尾部加百分号;仅为十进制数据使用
    isbin=False,输出2进制数据仅用于x:int
    isoct=False,输出8进制数据仅用于x:int
    ishex=False,输出16进制数据仅用于x:int,float
    """
    if isinstance(x, int):
        return int_to_str(x, isbin, isoct, ishex)
    elif isinstance(x, float):
        return float_to_str(x, precision, is_pre_cent, ishex)
    elif isinstance(x, str):
        return x
    else:
        return str(x)


def str_is_num(num: str):
    """判断int,float字符串是否是数字"""
    pattern = re.compile(r'^[-+]?[-0-9]d*.d*|[-+]?.?[0-9]d*$')
    return True if pattern.match(num) else False


def str_replace(
        text: str,
        old: List[str] = None,
        new: List[str] = None,
        items: dict = None):
    """
    用途:替换文本中映射的字符串
    参数:
    text:str要被替换的原始文本
    old: str list 旧的字符串
    new:str list 用于替换旧字符串的字符串
    items:dict 旧,新字符串的映射==dict(zip(old,new))
    你必须输入old,new or items 参数,二选一

    注意:特殊字符(除_外)必须单个字符输入
    实例:
    user_input = "Thisnstring hastsome whitespaces...rn"
    character_map = { ord('n'): ' ',ord('t'): ' ',ord('r'): None}
    user_input.translate(character_map)  # This string has some whitespaces... "
    """
    if old is not None:
        values = new if new else [''] * len(old)
        character_map = dict(zip(old, values))
    else:
        character_map = items
    for key in character_map:
        if not re.fullmatch('\w+', key):
            character_map[key] = ord(key)

    return text.translate(character_map)


def lst_to_dict(x: Union[set, tuple, list], keys=None):
    if keys is not None:
        keys = range(len(x))
    return dict(zip(keys, x))


def lst_trans(x: Union[set, tuple, list]):
    """用途:转置数据 ;T(T(x))==x"""
    if isinstance(x, set):
        return set(zip(*x))
    elif isinstance(x, tuple):
        return tuple(zip(*x))
    elif isinstance(x, list):
        return list(zip(*x))
    else:
        return x


def lst_flat(lst: Union[set, tuple, list]) -> list:
    """
    用途:序列扁平化
    实例:
    flat([[1, [[[2, 3], 4]], [5, 6]]])#[1, 2, 3, 4, 5, 6] 本函数运行11.6 µs
    --------------------------------------------------------------------------------------------
    方法2:14.6 µs
        def b(x): return isinstance(x, (set, tuple, list))

    def _flat(arg):
        ret = []
        for i in arg:
            if b(i):
                ret.extend(i)
            else:
                ret.append(i)
        return ret

    rst = []
    rst.extend(_flat(list(map(lambda x: lst_flat(x) if b(x) else x, lst))))
    return rst

    方法3:这个函数运算时间稍长 21 µs
    def _flat(rst,tmp,tail):
    if not tmp:
        tmp.append(tail.popleft())
    for i in range(len(tmp)):
        v=tmp.popleft()
        if isinstance(v,(str,int,float,bool,bytes)):
            rst.append(v)
        else:
            tmp.appendleft(v)
            break

    def flat(lst):
        rst=[]
        tmp=deque()
        tail=deque(lst)

        while True:
            if not tail and not tmp:
                break
            _flat(rst,tmp,tail)
        return rst
    """
    stack = [lst]
    flat_list = []
    while stack:
        top = stack.pop()
        if isinstance(top, (set,tuple,list)):
            for elem in reversed(top):
                stack.append(elem)
        else:
            flat_list.append(top)

    return flat_list


def lst_filter(lst: Union[set, tuple, list], func: Callable = bool):
    """
    用途:使用 fliter() 删除列表中的错误值(如:False, None, 0 和“”)
    实例:lst_filter([0, 1, False, 2 , 3,  'a' ,  '' , 34]) # [ 1, 2, 3,  a , 34 ]
    """
    return list(filter(func, lst))


def lst_chunk(lst: Union[set, tuple, list], size=2) -> list:
    """
    用途:分块-将列表分块为指定大小的较小列表
    实例:chunk([1, 2, 3, 4, 5], 2)  # [[1,2],[3,4],5]
    """
    return list(
        map(lambda x: lst[x * size:x * size + size],
            list(range(0, math.ceil(len(lst) / size)))))


def ifelse(con: bool, value1, value2):
    if con:
        return value1
    else:
        return value2


def switchcase(key, data: dict = None, default=None, **kwargs):
    """
    用途:用字典模拟switch case
    参数:
    key:str or int
    data:dict 键为条件,vaule为数据或函数-作为结果
    kwargs:dict
    kwargs['items'] dict 当data=None
    kwargs['keys'],kwargs['values']提供字典的键,值(或函数)
    kwargs的其他关键字提供函数的参数

    实例:

    """
    if data is not None:
        d = data
    elif 'items' in kwargs:
        d = kwargs['items']
    else:
        d = dict(zip(kwargs['keys'], kwargs['values']))

    obj = d.get(key, default)
    if not isinstance(d, dict):
        return obj
    else:
        if not isinstance(obj, Callable):
            return obj
        names = ['items', 'keys', 'values']
        _kwargs = {k: v for k, v in kwargs.items() if k not in names}
        return obj(**_kwargs)


def update_dict(src1: dict, src2: dict, *args):
    chain = ChainMap(src1, src2, *args) if args else ChainMap(src1, src2)
    return dict(chain)


def dicts_merge(a: dict, b: dict):
    """
    用途:合并2个字典
    方法2:
    c = a.copy()   # make a copy of a
    c.update(b)

    实例:
    a = {  x : 1,  y : 2}
    b = {  y : 3,  z : 4}
    print(merge_dictionaries(a, b)) # { y : 3,  x : 1,  z : 4}
    """
    return {**a, **b}


def swap(a, b):
    return b, a


def copy_(x, isdeep=False):
    """
    b = a				                # 赋值
    c = a[:]				                # 浅拷贝
    d = copy.copy(a)		    # 浅拷贝
    e = copy.deepcopy(a)	# 深拷贝
    """
    if isdeep:
        return copy.deepcopy(x)
    else:
        return copy.copy(x)


def factorial_sum(n=20):
    """
    用途:递归求和
    说明:1+2!+3!+…+20!=1+2(1+3(1+4(…20(1))))
    """
    rst = 1
    for i in range(n, 1, -1):
        rst = i * rst + 1
    return rst


def factorial(n):
    """递归求阶乘5!"""
    return n * factorial(n - 1) if n > 1 else 1


def test_switchcase():
    # 测试1:
    key = 1
    ids = [1, 2, 3]
    names = ['one', 'two', 'three']
    items = {1: 'one', 2: 'two', 3: 'three'}

    # get()如没找到key时返回'Unknow'相当于switch case 中的default
    assert(items.get(key, 'Unknow') == 'one')

    # 测试2:
    assert(
        switchcase(
            0,
            keys=ids,
            values=names,
            default='out range') == 'out range')
    assert(switchcase(1, keys=ids, values=names, default='out range') == 'one')
    assert(switchcase(1, items=items, default='out range') == 'one')
    assert(switchcase(1, data=items, default='out range') == 'one')

    # 测试3:
    def str_one(): return 'one'
    def str_two1(x): return 'two(%s)' % x
    def str_two2(x, y): return 'two(%s,%s)' % (x, y)
    def str_three(): return 'three'
    def unknow(): return '请输入正确的信息'

    # 字典value是函数,直接写函数名
    items = {'1': str_one, '21': str_two1, '22': str_two2, '3': str_three}
    assert(switchcase(key='1', items=items) == 'one')
    assert(switchcase(key='21', items=items, x=1) == 'two(1)')
    assert(switchcase('22', items=items, x=1, y=2) == 'two(1,2)')


def test_map():
    set_ = {'tom', 'bob', 'shanghai'}
    def format_name(s): return s[0:1].upper() + s[1:].lower()
    set(map(format_name, set_))  # {'Bob', 'Shanghai', 'Tom'}


if __name__ == '__main__':
    test_switchcase()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/892786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号