Categories:Viewed: 4 - Published at: a few seconds ago

[ Show class of custom sequence type ,iter , iterables and iterator using example of tuple ]

show class of custom sequence type ,iter , iterables and iterator using example of tuple

class CustomTupleIter:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self):
        self.list_ = (1,2,3,4)

    def __len__(self):
        return len(self.list_)

    def __getitem__(self, i):
        if isinstance(i, int):
            if i<0:
                i = len(self.list_) + i

            if i<0 or i>=len(self.list_):
                raise IndexError('Invalid Input')
            else:
                return self.list_[i]

    def __iter__(self):
        return self.CustomTupleIterator(self)

    class CustomTupleIterator:
        def __init__(self, other):
            self.count = 0
            self.other = other

        def __iter__(self):
            return self

        def __next__(self):
            if self.count < len(self.other.list_):
                self.count += 1
                return self.other.list_[self.count]
            else:
                raise StopIteration

clone of orginal list with two functionality i. iterating and sequence

class CustomListIter:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self):
        self.list_ = [1,2,3,4]

    def __len__(self):
        return len(self.list_)

    def __getitem__(self, i):
        if isinstance(i, int):
            if i<0:
                i = len(self.list_) + i

            if i<0 or i>=len(self.list_):
                raise IndexError('Invalid Input')
            else:
                return self.list_[i]

    def __iter__(self):
        return self.CustomListIterator(self)

    class CustomListIterator:
        def __init__(self, other):
            self.count = 0
            self.other = other

        def __iter__(self):
            return self

        def __next__(self):
            if self.count < len(self.other.list_):
                self.count += 1
                return self.other.list_[self.count]
            else:
                raise StopIteration

Write a class that act like squares and should print the squares of values and and cuustom sequence type.

class Square:
    def __init__(self, n):
        self.n = n

    def __iter__(self):
        return self.show_sq(self.n)

    @staticmethod
    def show_sq(n):
         for i in range(n):
             yield i**2

    def __getitem__(self, i):
       if isinstance(i, int):
            if i < = self.n:
                print(i**2)
            else:
                raise ValueError('Index out of bound')

fibonaaci using generator

def fibo(n):
    x = 0
    yield x
    y = 1
    yield y
    for i in range(n-1):
        x, y = y, x+y
        yield y

show generator is faster than list

def show_gen_fast():
    from timeit import timeit
    dt = timeit("[num for num in fib(100) ]", globals = globals(), number=1)
    return dt

Add two strings

def add_str(str1,str2):
   return str1 + str2

we are dealing with multiple inheritance

class A(object):
    def foo(self):
        print("class A")

class B(object):
    def foo(self):
        print("class B")

class C(A, B):
    pass

This is how pass works in case of multiple inheritance

class A1(object):
   def foo(self):
      print("class A1")

class B1(A1):
   pass

class C1(A1):
   def foo(self):
      print("class C1")

class D1(B1,C1):
   pass