#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ******************************************************
#   Mosel Python Example Problems
#   =============================
#
#   file init_set_from_py.py
#   ````````````````````````
#   Initialize Mosel sets from iterable Python objects.
#
#   (c) 2018 Fair Isaac Corporation
#       author: J.Müller
# *******************************************************
import sys
import platform


class SumIterable:
    def __init__(self, n):
        self.n = n
        self.sum = 0
        self.i = 1

    def __iter__(self):
        self.sum = 0
        self.i = 1
        return self

    def __next__(self):
        if self.i <= self.n:
            self.sum += self.i
            self.i += 1
            return self.sum
        else:
            raise StopIteration


def sum_generator(n):
    i = 1
    result = 0
    while i <= n:
        result += i
        yield result
        i += 1


def mosel_set_add_from_py_iterator():
    """Python test function for MoselSet_AddFromPyIterator"""

    global setOfInt
    global setOfStr
    global setOfSetOfStr
    global setFromGenerator
    global setFromGeneratorExpr
    global setFromIterableObj

    setOfInt = {3, 4}
    setOfStr = {'p3', 'p4'}
    setOfSetOfStr = {frozenset({'p1'}), frozenset({'p3', 'p4'})}
    setFromGenerator = sum_generator(5)
    setFromGeneratorExpr = (int(((i + 1) * (i + 2) / 2)) for i in range(5))
    setFromIterableObj = SumIterable(5)

    print('Values in Python:')
    print('setOfInt              = ', setOfInt)
    print('setOfStr              = ', setOfStr)
    print('setOfSetOfStr         = ', setOfSetOfStr)
    print('setFromGenerator      = ', setFromGenerator)
    print('setFromGeneratorExpr  = ', setFromGeneratorExpr)
    print('setFromIterableObj    = ', setFromIterableObj)


if __name__ == "__main__":
    print("Python version:            ", platform.python_version(), "\n")
    print("Executed main function of:  init_set_from_py.py\n")
