Python对象类型定义

曲辕RPA已经把下面的类型定义添加到了path中,可以直接使用,无需import

ExcelApp

class ExcelApp:
    pass

WordApp

class WordApp:
    pass

SQLConnection


class SQLConnection:
    pass

MyList


class MyList(list):
    @property
    def length(self):
        return len(self)

    @property
    def first(self):
        if len(self) == 0:
            raise Exception("列表没有数据")
        return self[0]

    @property
    def last(self):
        if len(self) == 0:
            raise Exception("列表没有数据")
        return self[-1]

    @property
    def center(self):
        count = len(self)
        if count == 0:
            raise Exception("列表没有数据")
        return self[count // 2]


    def __str__(self):
        s = f"""[列表]"""
        for index, item in enumerate(self):
            s = f"""{s}
    {index} {item}"""
        return s


Map

class Map(dict):
    @property
    def length(self):
        return len(self)


    def __str__(self):
        s = f"""[映射]"""
        for index, (key, value) in enumerate(self.items()):
            s = f"""{s}
    {index} {key}:{value}"""
        return s


    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(f"没有 {key}")


    def __setattr__(self, key, value):
        self[key] = value

原文链接:https://help.qyrpa.com/docs/api/python-others-class-define

更多推荐