利用 Python 实现文档计数器功能

利用 Python 实现文档计数器功能

1. 阿拉伯数字计数器

class Carabic():
    def __init__(self):
        self.i = 0

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        while True:
            self.i = self.i+1
            return self.i

    def __call__(self):
        while True:
            self.i = self.i+1
            return self.i
    def show(self):
        self.i = self.i-1
        temp = self.__call__()
        return temp

引用方式:

count = Carabic()
count()
next(count)

2. 大写英文字母计数器

class CAlph(object):
    def __init__(self):
        self.abc = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') # 初始化计数序列
        self.i = -1

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        self.i = self.i+1
        if self.i > 25: # 退出循环的条件
            raise StopIteration("Out of alphabet limits, please check the count value %s!" % self.abc[self.i])
        return self.abc[self.i]

    def __call__(self):
        self.i = self.i+1
        if self.i > 25: # 退出循环的条件
            raise StopIteration("Out of alphabet limits, please check the count value %s!" % self.abc[self.i])
        return self.abc[self.i]

注意: 其计数值应小于 27。
引用方式:

count = CAlph()
count()
next(count)

3. 小写英文字母计数器

class Calph(object):
    def __init__(self):
        self.abc = list('abcdefghijklmnopqrstuvwxyz') # 初始化计数序列
        self.i = -1

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        self.i = self.i+1
        if self.i > 25: # 退出循环的条件
            raise StopIteration("Out of alphabet limits, please check the count value %s!" % self.abc[self.i])
        return self.abc[self.i]

    def __call__(self):
        self.i = self.i+1
        if self.i > 25: # 退出循环的条件
            raise StopIteration("Out of alphabet limits, please check the count value %s!" % self.abc[self.i])
        return self.abc[self.i]

注意: 其计数值应小于 27。
引用方式:

count = Calph()
count()
next(count)

3. 大写罗马数字计数器

class CRoman():
    def __init__(self):
        self.i = 0 # 迭代初始值
        self.c = [["","I","II","III","IV","V","VI","VII","VIII","IX"],
             ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"],
             ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"],
             ["","M","MM","MMM"]]

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        if self.i > 3000: # 退出循环的条件
            raise StopIteration("Out of number limits, please check the count value %s!" % self.__Roman__(self.i))
        self.i = self.i+1
        return self.__Roman__(self.i)

    def __call__(self):
        if self.i > 3000: # 退出循环的条件
           raise StopIteration("Out of number limits, please check the count value %s!" % self.__Roman__(self.i))
        self.i = self.i+1
        return self.__Roman__(self.i)

    def __Roman__(self, num):
        m = ''
        m = m+self.c[3][int(num / 1000 % 10)]
        m = m+self.c[2][int(num / 100 % 10)]
        m = m+self.c[1][int(num / 10 % 10)]
        m = m+self.c[0][int(num % 10)]
        return m

注意: 其计数值应小于 3000。
引用方式:

count = CRoman()
count()
next(count)

4. 小写罗马数字计数器

class Croman():
    def __init__(self):
        self.i = 0 # 迭代初始值
        self.c = [["","I","II","III","IV","V","VI","VII","VIII","IX"],
             ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"],
             ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"],
             ["","M","MM","MMM"]]

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        if self.i > 3000: # 退出循环的条件
            raise StopIteration("Out of number limits, please check the count value %s!" % self.__Roman__(self.i))
        self.i = self.i+1
        return self.__Roman__(self.i).lower()

    def __call__(self):
        if self.i > 3000: # 退出循环的条件
           raise StopIteration("Out of number limits, please check the count value %s!" % self.__Roman__(self.i))
        self.i = self.i+1
        return self.__Roman__(self.i).lower()

    def __Roman__(self, num):
        m = ''
        m = m+self.c[3][int(num / 1000 % 10)]
        m = m+self.c[2][int(num / 100 % 10)]
        m = m+self.c[1][int(num / 10 % 10)]
        m = m+self.c[0][int(num % 10)]
        return m

注意: 其计数值应小于 3000。
引用方式:

count = Croman()
count()
next(count)