鱼C论坛

 找回密码
 立即注册
查看: 823|回复: 1

[作品展示] 可变字符串

[复制链接]
发表于 2023-8-29 20:45:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
"""
Are you still suffering from inefficiency caused by frequent changes to strings?
This module can help you solve that problem!
"""
class char(str):
    def __init__(self, string):
        if isinstance(string, str):
            if len(string) == 1:
                self.char = string
            else:
                raise ValueError("the length outside 1")
        else:
            raise TypeError("input is a not string")

class MutableString(object):
    
    def __init__(self, string):
        '''
        input: A string object
        output: None
        init a new object
        '''
        if isinstance(string, str):
            self.string = list(string)
        elif isinstance(string, list):
            self.string = string
        else:
            raise TypeError("arg can only be a list or string")
        pass
    
    def __add__(self, other):
        if isinstance(other, MutableString):
            return MutableString(self.string + other.string)
        elif isinstance(other, str):
            return MutableString(self.string + list(other))
        else:
            raise TypeError("arg can only be a MutableString object or a string")
        pass

    __radd__ = __add__

    def __mul__(self, other):
        if isinstance(other, int):
            return MutableString(self.string * other)
    
    def __getattr__(self, name):
        if name == "str":
            return self.get_string()
        else:
            return super().__getattr__(name)
        pass

    def __setattr__(self, name, value):
        if name == "str":
            if isinstance(value, str):
                self.string = list(value)
            elif isinstance(value, list):
                self.string = value
            else:
                raise TypeError("arg can only be a list or string")
        else:
            super().__setattr__(name, value)
        pass

    def __getitem__(self, index):
        return self.string[index]

    def __setitem__(self, index, value):
        self.string[index] = value
        pass

    def __delitem__(self, index):
        del self.string[index]
        pass

    def __repr__(self):
        return """+"".join(self.string)+"""

    __str__ = __repr__

    # new function
    
    def append(self, string: char):
        '''
        input: A string object
        output: None
        append value
        '''
        if isinstance(string, char):
            self.string.extend(list(string))
            pass
        else:
            raise TypeError("arg can only be a char")
        pass
    
    def pop(self, index: int):
        '''
        input: A index value (int type)
        output: a string object
        delete the element corresponding to the index
        '''
        try:
            return self.string.pop(index)
        except IndexError:
            raise IndexError("string index out of range")
        except (ValueError, TypeError):
            raise TypeError("input is a not number")
        pass

    def insert(self, index: int, value: char):
        '''
        input: Insert index, value
        output: None
        insert value in index
        '''
        if isinstance(value, char):
            self.string.insert(index, value)
        else:
            raise TypeError("arg can only be a char")
        pass

    def delete_paragraph(self, start: int=None, stop: int=None):
        '''
        input: Start index, stop index
        output: None
        delete all char in start - stop
        '''
        if start == None:
            start = 0
        if stop == None:
            stop = len(self.string)
        if isinstance(start, int) and isinstance(stop, int):
            del self.string[start:stop]
        pass
    
    def get_string(self):
        '''
        input: string function name, all args (tuple type)
        output: string function return value 
        run string function
        '''
        return ''.join(self.string)

优点:
进行操作时不用创建一个新字符串。

缺点:
参数必须是char类型。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-9 09:20:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 13:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表