timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Аритметични изрази от Десислава Цветкова

Обратно към всички решения

Към профила на Десислава Цветкова

Резултати

  • 10 точки от тестове
  • 2 бонус точки
  • 12 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import operator


class MathOperation:
    SYMBOLS_FUNCS = {
        '__add__': '+', '__radd__': '+', '__sub__': '-', '__rsub__': '-',
        '__mul__': '*', '__rmul__': '*', '__truediv__': '/',
        '__rtruediv__': '/', '__div__': '/', '__rdiv__': '/',
        '__mod__': '%', '__rmod__': '%', '__pow__': '**', '__rpow__': '**',
        '__floordiv__': '//', '__lshift__': '<<', '__rshift__': '>>',
        '__and__': '&', '__xor__': '^', '__or__': '|',
        '__rfloordiv__': '//', '__rlshift__': '<<', '__rrshift__': '>>',
        '__rand__': '&', '__rxor__': '^', '__ror__': '|',
    }
    FUNCTIONS = ['__add__', '__sub__', '__mul__', '__truediv__', '__div__',
                 '__mod__', '__pow__', '__floordiv__', '__lshift__',
                 '__rshift__', '__and__', '__xor__', '__or__']
    RFUNCTIONS = ['__radd__', '__rsub__', '__rmul__', '__rtruediv__',
                  '__rdiv__', '__rmod__', '__rpow__', '__rfloordiv__',
                  '__rlshift__', '__rrshift__', '__rand__',
                  '__rxor__', '__ror__']

    def _add_functions(self):
        for func_name in type(self).FUNCTIONS:
            setattr(type(self), func_name, self.__make_func(func_name,
                                                            func_name))

        for index, func_name in enumerate(type(self).RFUNCTIONS, start=0):
            setattr(type(self), func_name,
                    self.__make_func(type(self).FUNCTIONS[index],
                                     func_name, -1))

    def __make_func(self, func_name, func_named, way=1):
        def func(self, other):
            args = [self, Operator(type(self).SYMBOLS_FUNCS[func_name],
                                   getattr(operator, func_name)),
                    other]
            return Expression(tuple(args[::way]))
        func.__name__ = func_named
        return func


class Constant(MathOperation):
    def __init__(self, value):
        self.VALUE = value
        self._add_functions()

    def evaluate(self, **kwargs):
        return self.VALUE

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.VALUE)


class Expression(MathOperation):

    def __init__(self, expression):
        self.expression = self.__make_expresion(expression)
        self.variable_names = self.__collect_variable_names()
        self._add_functions()

    def __str__(self):
        if isinstance(self.expression, tuple):
            return "({} {} {})".format(str(self.expression[0]),
                                       str(self.expression[1]),
                                       str(self.expression[2]))
        return str(self.expression)

    def __repr__(self):
        return str(self)

    def __make_expresion(self, expression):
        if isinstance(expression, tuple):
            return (Expression(expression[0]),
                    expression[1],
                    Expression(expression[2]))
        return expression

    def __collect_variable_names(self):
        VAR_NAMES = {
            Constant: lambda ex: [],
            Variable: lambda ex: ex.variable_names,
            tuple: lambda ex: ex[0].variable_names + ex[2].variable_names,
            Expression: lambda ex: ex.variable_names
        }
        function = VAR_NAMES.get(type(self.expression), lambda ex: [])
        return function(self.expression)

    def evaluate(self, **kwargs):
        DEFINED_TYPES = (Expression, Variable, Constant, tuple)
        if not type(self.expression) in DEFINED_TYPES:
            return self.expression
        elif not isinstance(self.expression, tuple):
            return self.expression.evaluate(**kwargs)
        return (self.expression[1]((self.expression[0]).evaluate(**kwargs),
                                   (self.expression[2]).evaluate(**kwargs)))


class Variable(MathOperation):

    def __init__(self, name):
        self.NAME = name
        self._add_functions()

    @property
    def variable_names(self):
        return [self.NAME]

    def __str__(self):
        return self.NAME

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return kwargs[self.NAME]


class Operator:
    def __init__(self, symbol, logic):
        self.symbol = symbol
        self.logic = logic

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol

    def __call__(self, lhs, rhs):
        return self.logic(lhs, rhs)


def create_operator(symbol, logic):
    return Operator(symbol, logic)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

Лог от изпълнението

...................
----------------------------------------------------------------------
Ran 19 tests in 0.133s

OK

История (4 версии и 2 коментара)

Десислава обнови решението на 17.03.2016 18:22 (преди почти 2 години)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import operator


class MathOperation:

    def __add__(self, other):
        return Expression((self, Operator('+', operator.__add__), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', operator.__add__), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', operator.__sub__), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', operator.__sub__), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', operator.__mul__), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', operator.__mul__), self))

    def __truediv__(self, other):
        return Expression((self, Operator('//', operator.__truediv__), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('//', operator.__truediv__), self))

    def __div__(self, other):
        return Expression((self, Operator('/', operator.__div__), other))

    def __rdiv__(self, other):
        return Expression((other, Operator('/', operator.__div__), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', operator.__mod__), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', operator.__mod__), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', operator.__pow__), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', operator.__pow__), self))


class Constant(MathOperation):
    def __init__(self, value):
        self.VALUE = value

    def evaluate(self, **kwargs):
        return self.VALUE

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.VALUE)


class Expression(MathOperation):

    def __init__(self, expression):
        self.expression = self.__make_expresion(expression)
        self.variable_names = self.__collect_variable_names()

    def __str__(self):
        if type(self.expression) == tuple:
            return "({} {} {})".format(str(self.expression[0]),
                                       str(self.expression[1]),
                                       str(self.expression[2]))
        return str(self.expression)

    def __repr__(self):
        return str(self)

    def __make_expresion(self, expression):
        if type(expression) == tuple:
            return (Expression(expression[0]),
                    expression[1],
                    Expression(expression[2]))
        return expression

    def __collect_variable_names(self):
        VAR_NAMES = {
            Constant: lambda ex: [],
            Variable: lambda ex: ex.variable_names,
            tuple: lambda ex: ex[0].variable_names + ex[2].variable_names,
            Expression: lambda ex: ex.variable_names
        }
        function = VAR_NAMES.get(type(self.expression), lambda ex: [])
        return function(self.expression)

    def evaluate(self, **kwargs):
        DEFINED_TYPES = (Expression, Variable, Constant, tuple)
        if not type(self.expression) in DEFINED_TYPES:
            return self.expression
        elif type(self.expression) != tuple:
            return self.expression.evaluate(**kwargs)
        return (self.expression[1]((self.expression[0]).evaluate(**kwargs),
                                   (self.expression[2]).evaluate(**kwargs)))


class Variable(MathOperation):

    def __init__(self, name):
        self.NAME = name

    @property
    def variable_names(self):
        return [self.NAME]

    def __str__(self):
        return self.NAME

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return kwargs[self.NAME]


# TO DO динамично дефиниране на операторите


class Operator:
    def __init__(self, symbol, logic):
        self.symbol = symbol
        self.logic = logic

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol

    def __call__(self, lhs, rhs):
        return self.logic(lhs, rhs)


def create_operator(symbol, logic):
    return Operator(symbol, logic)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

Десислава обнови решението на 18.03.2016 00:22 (преди почти 2 години)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import operator


class MathOperation:
    SYMBOLS_FUNCS = {
        '__add__': '+', '__radd__': '+', '__sub__': '-', '__rsub__': '-',
        '__mul__': '*', '__rmul__': '*', '__truediv__': '/',
        '__rtruediv__': '/', '__div__': '/', '__rdiv__': '/',
        '__mod__': '%', '__rmod__': '%', '__pow__': '**', '__rpow__': '**'
    }
    FUNCTIONS = ['__add__', '__sub__', '__mul__', '__truediv__', '__div__',
                 '__mod__', '__pow__']
    RFUNCTIONS = ['__radd__', '__rsub__', '__rmul__', '__rtruediv__',
                  '__rdiv__', '__rmod__', '__rpow__']

    def _add_functions(self):
        for func_name in type(self).FUNCTIONS:
            setattr(type(self), func_name, self.__make_func(func_name))

        for index, func_name in enumerate(type(self).RFUNCTIONS, start=0):
            setattr(type(self), func_name,
                    self.__make_rfunc(type(self).FUNCTIONS[index], func_name))

    def __make_func(self, func_name):
        def func(self, other):
            return Expression((self,
                               Operator(type(self).SYMBOLS_FUNCS[func_name],
                                        getattr(operator, func_name)),
                               other))
        func.__name__ = func_name
        return func

    def __make_rfunc(self, func_name, rfunc_name):
        def func(self, other):
            return Expression((other,
                               Operator(type(self).SYMBOLS_FUNCS[func_name],
                                        getattr(operator, func_name)),
                               self))
        func.__name__ = rfunc_name
        return func


class Constant(MathOperation):
    def __init__(self, value):
        self.VALUE = value
        self._add_functions()

    def evaluate(self, **kwargs):
        return self.VALUE

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.VALUE)


class Expression(MathOperation):

    def __init__(self, expression):
        self.expression = self.__make_expresion(expression)
        self.variable_names = self.__collect_variable_names()
        self._add_functions()

    def __str__(self):
        if type(self.expression) == tuple:
            return "({} {} {})".format(str(self.expression[0]),
                                       str(self.expression[1]),
                                       str(self.expression[2]))
        return str(self.expression)

    def __repr__(self):
        return str(self)

    def __make_expresion(self, expression):
        if type(expression) == tuple:
            return (Expression(expression[0]),
                    expression[1],
                    Expression(expression[2]))
        return expression

    def __collect_variable_names(self):
        VAR_NAMES = {
            Constant: lambda ex: [],
            Variable: lambda ex: ex.variable_names,
            tuple: lambda ex: ex[0].variable_names + ex[2].variable_names,
            Expression: lambda ex: ex.variable_names
        }
        function = VAR_NAMES.get(type(self.expression), lambda ex: [])
        return function(self.expression)

    def evaluate(self, **kwargs):
        DEFINED_TYPES = (Expression, Variable, Constant, tuple)
        if not type(self.expression) in DEFINED_TYPES:
            return self.expression
        elif type(self.expression) != tuple:
            return self.expression.evaluate(**kwargs)
        return (self.expression[1]((self.expression[0]).evaluate(**kwargs),
                                   (self.expression[2]).evaluate(**kwargs)))


class Variable(MathOperation):

    def __init__(self, name):
        self.NAME = name
        self._add_functions()

    @property
    def variable_names(self):
        return [self.NAME]

    def __str__(self):
        return self.NAME

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return kwargs[self.NAME]


class Operator:
    def __init__(self, symbol, logic):
        self.symbol = symbol
        self.logic = logic

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol

    def __call__(self, lhs, rhs):
        return self.logic(lhs, rhs)


def create_operator(symbol, logic):
    return Operator(symbol, logic)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

Десислава обнови решението на 19.03.2016 17:27 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import operator


class MathOperation:
    SYMBOLS_FUNCS = {
        '__add__': '+', '__radd__': '+', '__sub__': '-', '__rsub__': '-',
        '__mul__': '*', '__rmul__': '*', '__truediv__': '/',
        '__rtruediv__': '/', '__div__': '/', '__rdiv__': '/',
        '__mod__': '%', '__rmod__': '%', '__pow__': '**', '__rpow__': '**',
        '__floordiv__': '//', '__lshift__': '<<', '__rshift__': '>>',
        '__and__': '&', '__xor__': '^', '__or__': '|',
        '__rfloordiv__': '//', '__rlshift__': '<<', '__rrshift__': '>>',
        '__rand__': '&', '__rxor__': '^', '__ror__': '|',
    }
    FUNCTIONS = ['__add__', '__sub__', '__mul__', '__truediv__', '__div__',
                 '__mod__', '__pow__', '__floordiv__', '__lshift__',
                 '__rshift__', '__and__', '__xor__', '__or__']
    RFUNCTIONS = ['__radd__', '__rsub__', '__rmul__', '__rtruediv__',
                  '__rdiv__', '__rmod__', '__rpow__', '__rfloordiv__',
                  '__rlshift__', '__rrshift__', '__rand__',
                  '__rxor__', '__ror__']

    def _add_functions(self):
        for func_name in type(self).FUNCTIONS:
            setattr(type(self), func_name, self.__make_func(func_name))

        for index, func_name in enumerate(type(self).RFUNCTIONS, start=0):
            setattr(type(self), func_name,
                    self.__make_rfunc(type(self).FUNCTIONS[index], func_name))

    def __make_func(self, func_name):
        def func(self, other):
            return Expression((self,
                               Operator(type(self).SYMBOLS_FUNCS[func_name],
                                        getattr(operator, func_name)),
                               other))
        func.__name__ = func_name
        return func

    def __make_rfunc(self, func_name, rfunc_name):
        def func(self, other):
            return Expression((other,
                               Operator(type(self).SYMBOLS_FUNCS[func_name],
                                        getattr(operator, func_name)),
                               self))
        func.__name__ = rfunc_name
        return func


class Constant(MathOperation):
    def __init__(self, value):
        self.VALUE = value
        self._add_functions()

    def evaluate(self, **kwargs):
        return self.VALUE

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.VALUE)


class Expression(MathOperation):

    def __init__(self, expression):
        self.expression = self.__make_expresion(expression)
        self.variable_names = self.__collect_variable_names()
        self._add_functions()

    def __str__(self):
        if type(self.expression) == tuple:
            return "({} {} {})".format(str(self.expression[0]),
                                       str(self.expression[1]),
                                       str(self.expression[2]))
        return str(self.expression)

    def __repr__(self):
        return str(self)

    def __make_expresion(self, expression):
        if type(expression) == tuple:
            return (Expression(expression[0]),
                    expression[1],
                    Expression(expression[2]))
        return expression

    def __collect_variable_names(self):
        VAR_NAMES = {
            Constant: lambda ex: [],
            Variable: lambda ex: ex.variable_names,
            tuple: lambda ex: ex[0].variable_names + ex[2].variable_names,
            Expression: lambda ex: ex.variable_names
        }
        function = VAR_NAMES.get(type(self.expression), lambda ex: [])
        return function(self.expression)

    def evaluate(self, **kwargs):
        DEFINED_TYPES = (Expression, Variable, Constant, tuple)
        if not type(self.expression) in DEFINED_TYPES:
            return self.expression
        elif type(self.expression) != tuple:
            return self.expression.evaluate(**kwargs)
        return (self.expression[1]((self.expression[0]).evaluate(**kwargs),
                                   (self.expression[2]).evaluate(**kwargs)))


class Variable(MathOperation):

    def __init__(self, name):
        self.NAME = name
        self._add_functions()

    @property
    def variable_names(self):
        return [self.NAME]

    def __str__(self):
        return self.NAME

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return kwargs[self.NAME]


class Operator:
    def __init__(self, symbol, logic):
        self.symbol = symbol
        self.logic = logic

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol

    def __call__(self, lhs, rhs):
        return self.logic(lhs, rhs)


def create_operator(symbol, logic):
    return Operator(symbol, logic)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)
  • Начинът, по който генерираш имплементация на операторите е хитър, но __make_func и __make_rfunc са ми твърде еднакви.
  • Когато вземаш решение какво да правиш въз основа типа на аргумент, използвай isinstance() вместо type()

Десислава обнови решението на 19.03.2016 21:15 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import operator


class MathOperation:
    SYMBOLS_FUNCS = {
        '__add__': '+', '__radd__': '+', '__sub__': '-', '__rsub__': '-',
        '__mul__': '*', '__rmul__': '*', '__truediv__': '/',
        '__rtruediv__': '/', '__div__': '/', '__rdiv__': '/',
        '__mod__': '%', '__rmod__': '%', '__pow__': '**', '__rpow__': '**',
        '__floordiv__': '//', '__lshift__': '<<', '__rshift__': '>>',
        '__and__': '&', '__xor__': '^', '__or__': '|',
        '__rfloordiv__': '//', '__rlshift__': '<<', '__rrshift__': '>>',
        '__rand__': '&', '__rxor__': '^', '__ror__': '|',
    }
    FUNCTIONS = ['__add__', '__sub__', '__mul__', '__truediv__', '__div__',
                 '__mod__', '__pow__', '__floordiv__', '__lshift__',
                 '__rshift__', '__and__', '__xor__', '__or__']
    RFUNCTIONS = ['__radd__', '__rsub__', '__rmul__', '__rtruediv__',
                  '__rdiv__', '__rmod__', '__rpow__', '__rfloordiv__',
                  '__rlshift__', '__rrshift__', '__rand__',
                  '__rxor__', '__ror__']

    def _add_functions(self):
        for func_name in type(self).FUNCTIONS:
            setattr(type(self), func_name, self.__make_func(func_name,
                                                            func_name))

        for index, func_name in enumerate(type(self).RFUNCTIONS, start=0):
            setattr(type(self), func_name,
                    self.__make_func(type(self).FUNCTIONS[index],
                                     func_name, -1))

    def __make_func(self, func_name, func_named, way=1):
        def func(self, other):
            args = [self, Operator(type(self).SYMBOLS_FUNCS[func_name],
                                   getattr(operator, func_name)),
                    other]
            return Expression(tuple(args[::way]))
        func.__name__ = func_named
        return func


class Constant(MathOperation):
    def __init__(self, value):
        self.VALUE = value
        self._add_functions()

    def evaluate(self, **kwargs):
        return self.VALUE

    def __repr__(self):
        return str(self)

    def __str__(self):
        return str(self.VALUE)


class Expression(MathOperation):

    def __init__(self, expression):
        self.expression = self.__make_expresion(expression)
        self.variable_names = self.__collect_variable_names()
        self._add_functions()

    def __str__(self):
        if isinstance(self.expression, tuple):
            return "({} {} {})".format(str(self.expression[0]),
                                       str(self.expression[1]),
                                       str(self.expression[2]))
        return str(self.expression)

    def __repr__(self):
        return str(self)

    def __make_expresion(self, expression):
        if isinstance(expression, tuple):
            return (Expression(expression[0]),
                    expression[1],
                    Expression(expression[2]))
        return expression

    def __collect_variable_names(self):
        VAR_NAMES = {
            Constant: lambda ex: [],
            Variable: lambda ex: ex.variable_names,
            tuple: lambda ex: ex[0].variable_names + ex[2].variable_names,
            Expression: lambda ex: ex.variable_names
        }
        function = VAR_NAMES.get(type(self.expression), lambda ex: [])
        return function(self.expression)

    def evaluate(self, **kwargs):
        DEFINED_TYPES = (Expression, Variable, Constant, tuple)
        if not type(self.expression) in DEFINED_TYPES:
            return self.expression
        elif not isinstance(self.expression, tuple):
            return self.expression.evaluate(**kwargs)
        return (self.expression[1]((self.expression[0]).evaluate(**kwargs),
                                   (self.expression[2]).evaluate(**kwargs)))


class Variable(MathOperation):

    def __init__(self, name):
        self.NAME = name
        self._add_functions()

    @property
    def variable_names(self):
        return [self.NAME]

    def __str__(self):
        return self.NAME

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return kwargs[self.NAME]


class Operator:
    def __init__(self, symbol, logic):
        self.symbol = symbol
        self.logic = logic

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol

    def __call__(self, lhs, rhs):
        return self.logic(lhs, rhs)


def create_operator(symbol, logic):
    return Operator(symbol, logic)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)