Example #1
0
def calculator(data):
    try:
        if "=" in data:
            data = str_to_list(data)
            result = calc_eq(data)
            if result == "error":
                raise ValueError
            else:
                result = "output: x = " + str(result)
                return result

        input_data = data
        if "log" in input_data:
            i = input_data.index("(")
            k = input_data.index(")")
            data = float(input_data[i + 1 : k])
            result = log(data)
            return "output: %s" % result
        else:
            data = str_to_list(input_data)
            result = calculate(data)
            return "output: %s" % result

    except ZeroDivisionError:
        print("bad input")
        exit()
    except ValueError:
        print("bad input")
        exit()
    def mainp(self, period, time, days):
        per = 1
        for day in range(int(days)):
            for per in range(int(period)):
                conn = sqlite3.connect('data.sqlite')
                # r1 = room()
                # str=room.make1(r1)

                str = "0"  # hexadecimal
                print("str:", str)
                m = calculate()
                y = calculate.make(m, str)

                print("y", y)
                r2 = room()
                x = room.make(r2, y)

                # c = conn.cursor()
                # c.execute('select code from data2 where class =?', str(x[0]))
                # conn.commit()
                #
                print("x", x)
                # aa = code(x)
                # print("aa", aa)

                t = teach()
                teach.make(t, x, int(time))
                # print(t1[0])

                c = conn.cursor()
                c.execute('select * from data2 where active =?', "1")
                conn.commit()
                print("aaa", c)
                for qr in c:
                    out = conn.cursor()
                    out.execute(
                        'INSERT INTO outdata (period,room_id,teach_id,n_teach,sub_id,day) VALUES (?,?,?,?,?,?)',
                        (per + 1, qr[2], qr[1], qr[3], qr[8], day + 1))
                    conn.commit()
                conn.close()
Example #3
0
    def test_calculate(self):
        test_data_simple = [
            (['(', '7', '+', '3', ')', '*', '4'], 40),
            (['(', '2', '*', '4', '+', '8', ')', '/', '4'], 4),
            (['3', '*', 'x', '=', '6'], 'x = 2'),
            (['(', '7', '+', '1', ')', '*', 'x', '=', '6'], 'x = 0.75'),
            (['1', '+', '(', '7', '-', '2', ')'], 6),
            (['6', '+', '6'], 12),
            (['2', '*', '(', '25', '+', '2', ')'], 54),
            (['0', '*', '0.002'], 0),
            (['(', '-', '4', ')', '*', '8'], -32),
        ]

        test_data_complex = [
            (['log(', '5', ')'], 1.6094379124341003),
        ]

        test_wrong_data = [
            ['4', '+', '*', '8'],
            [('-', '4'), '(', '*', '8'],
        ]

        for input_data, expected_result in test_data_simple:
            self.assertEquals(calculate(input_data), expected_result)
Example #4
0
def code(time):
    # print("code")
    st = "0"  # hexadecimal
    print("str:", st)
    m = calculate()
    y = calculate.make(m, st)

    print("y", y)
    r2 = room()
    x = room.make(r2, y)
    conn = sqlite3.connect('data.sqlite')
    te = []
    id = []
    nd = []
    for i in x:
        c = conn.cursor()
        c.execute('select code,teach_id,n_id from data2 where room_id= ?',
                  (str(i), ))
        conn.commit()
        r = []
        d = []
        n = []
        for j in c:
            print("j", j)
            a = j[0]
            m = calculate()
            y = calculate.make(m, a)
            r = r + [y]
            d = d + [j[1]]
            n = n + [j[2]]
        # print(r)
        te = te + [r]
        id = id + [d]
        nd = nd + [n]

    print(te)
    print("id", id)
    print(nd)
    cal = len(x) * (len(max(id)) * 2)
    sr = seek(id, cal)
    print("sr", sr)
    sid_ar = []
    for ar in sr:
        print("ar", ar)
        # co=[i for i in range(len(ar))]
        n_tea = []

        for aa in range(len(ar)):
            c = conn.cursor()
            c.execute(
                'select max(n_teach) from data2 where room_id= ? and teach_id=?',
                (
                    str(x[aa]),
                    str(ar[aa]),
                ))
            conn.commit()
            l = c.fetchone()
            n_tea = n_tea + [l[0]]

        co = []
        no = []
        for aa in range(len(ar)):
            c = conn.cursor()
            c.execute(
                'select code,n_id from data2 where room_id= ? and teach_id=? and n_teach=?',
                (
                    str(x[aa]),
                    str(ar[aa]),
                    str(randint(1, n_tea[aa])),
                ))
            conn.commit()
            for j in c:
                bb = j[0]
                m = calculate()
                y = calculate.make(m, bb)
                co = co + [y]
                no = no + [j[1]]
        print("co", co)
        print("no", no)
        sum = sumit(co)
        print("sum", sum)
        print("x", x)
        sclass = []
        for i in x:
            try:
                if (sum[i - 1] == 0):
                    sclass = sclass + [i]
            except:
                sclass = sclass + [i]
        sid = []
        for i in range(len(x)):
            if (a_ar(sclass, x[i]) == 1
                ):  #searching for x[i] in sclass , returns 1 if found
                sid = sid + [no[i]]
        # try:
        #
        # except:
        #     sid = sid + []
        print("sclass", sclass)
        print("sid", sid)
        sid_ar = sid_ar + [sid]
    list = [x for x in sid_ar if x != []]  #to remove empty array
    for s in list:
        for nid in s:
            c = conn.cursor()
            c.execute('select hour from data2 where n_id = ?', (str(nid), ))
            l = c.fetchone()
            conn.commit()
            h = l[0]
            c = conn.cursor()
            c.execute('UPDATE data2 SET hour =? , active =? WHERE  n_id = ?', (
                str(h - time),
                "1",
                str(nid),
            ))
            conn.commit()
    conn.close()

    return list
Example #5
0
def calc_eq(data):
    try:
        k = data.index('=')
        left_part = data[:k]
        right_part = data[k+1:]
        if len(data) == 3 and 'x' in data and '=' in data:
            x = data.index('x')
            eq = data.index('=')
            del data[x]
            del data[eq-1]

            return data[0]

        if 'x' in left_part:
            data_with_x = left_part
            data = right_part
        else:
            data_with_x = right_part
            data = left_part
        x_index = data_with_x.index('x')

        if len(right_part) > 1:
            right_part = calculate(right_part)
        right_part = float(right_part[0])

        if x_index > 0 and data_with_x[x_index-1] == '*' or data_with_x[x_index-1] == '/':
            act_data = data_with_x[x_index-1]
            act_data_index = x_index - 1
        else:
            act_data = data_with_x[x_index+1]
            act_data_index = x_index + 1

        if data_with_x[act_data_index-1] == ')':
            for i in range((act_data_index-1), -1, -1):

                if data_with_x[i] == '(':
                    data_near_x = []
                    data_near_x.append(calculate(data_with_x[i:act_data_index]))

                    data_with_x[i:act_data_index] = data_near_x
                    data_near_x = data_near_x[0]
        else:
            if act_data_index < x_index:
                data_near_x = data_with_x[act_data_index-1]
            else:
                data_near_x = data_with_x[act_data_index+1]
        #print(data_with_x)
        data_without_x = 0

        #if len(data_with_x) > 3:
        data_with_x.remove(data_near_x)
        data_with_x.remove(act_data)

        data_with_x.remove('x')
        '''
            if data_with_x[x_index-1] == '+' and data_with_x[x_index] == '+':
                data_with_x[x_index-1:x_index+1] = '+'
                data_without_x = calculate(data_with_x)
            if data_with_x[x_index-1] == '-' and data_with_x[x_index] == '-':
                data_with_x[x_index-1:x_index+1] = '-'
                data_without_x = calculate(data_with_x)
            if data_with_x[x_index-1] == '-' and data_with_x[x_index] == '+':
                data_with_x[x_index-1:x_index+1] = '-'
                data_without_x = calculate(data_with_x)
            '''
        #print(data_with_x)
        i = 0
        for i in range(1, len(data_with_x)-1):
            if data_with_x[i-1] == '+' and data_with_x[i] == '+':
                data_with_x[i-2:i+1] = '+'
            elif data_with_x[i-1] == '+' and data_with_x[i] == '-':
                data_with_x[i-2:i+1] = '-'
            elif data_with_x[i-1] == '-' and data_with_x[i] == '+':
                data_with_x[i-2:i+1] = '-'
            elif data_with_x[i-1] == '-' and data_with_x[i] == '-':
                data_with_x[i-2:i+1] = '+'

        if data_with_x:
            data_without_x = calculate(data_with_x)
        else:
            data_without_x = calculate(data_near_x)
        print(data_with_x)
        if data_with_x:
            right_part -= data_without_x
        else:
            right_part += data_without_x


        if act_data == '*':
            result = right_part / float(data_near_x)
        elif act_data == '/':
            result = right_part * float(data_near_x)
        return result
    except Exception:
        return 'error'