f19813ff6126c86bc27df4eef8daf241ded65000
[ejpi] / src / plugins / builtins.py
1 from __future__ import division
2
3 import os
4 import operator
5 import math
6
7 import operation
8
9 import sys
10 sys.path.append("../")
11 import plugin_utils
12
13 _NAME = "Builtins"
14 _MAP = {
15         "name": _NAME,
16         "keys": {
17                 (0, 0): {
18                         "CENTER": {"action": "7", "type": "text", "text": "7", },
19                         "showAllSlices": True,
20                 },
21                 (0, 1): {
22                         "CENTER": {"action": "8", "type": "text", "text": "8", },
23                         "SOUTH": {"action": "[**]", "type": "text", "text": "**", },
24                         "EAST": {"action": "[sq]", "type": "text", "text": "sq", },
25                         "WEST": {"action": "[sqrt]", "type": "text", "text": "sqrt", },
26                         "showAllSlices": False,
27                 },
28                 (0, 2): {
29                         "CENTER": {"action": "9", "type": "text", "text": "9", },
30                         "showAllSlices": True,
31                 },
32                 (1, 0): {
33                         "CENTER": {"action": "4", "type": "text", "text": "4", },
34                         "showAllSlices": True,
35                 },
36                 (1, 1): {
37                         "CENTER": {"action": "5", "type": "text", "text": "5", },
38                         "EAST": {"action": "[+]", "type": "text", "text": "+", },
39                         "WEST": {"action": "[-]", "type": "text", "text": "-", },
40                         "NORTH": {"action": "[*]", "type": "text", "text": "*", },
41                         "SOUTH": {"action": "[/]", "type": "text", "text": "/", },
42                         "showAllSlices": True,
43                 },
44                 (1, 2): {
45                         "CENTER": {"action": "6", "type": "text", "text": "6", },
46                         "showAllSlices": True,
47                 },
48                 (2, 0): {
49                         "CENTER": {"action": "1", "type": "text", "text": "1", },
50                         "EAST": {"action": "0", "type": "text", "text": "0", },
51                         "showAllSlices": True,
52                 },
53                 (2, 1): {
54                         "CENTER": {"action": "2", "type": "text", "text": "2", },
55                         "EAST": {"action": "[abs]", "type": "text", "text": "abs", },
56                         "NORTH": {"action": ".", "type": "text", "text": ".", },
57                         "WEST": {"action": "[+-]", "type": "text", "text": "+/-", },
58                         "showAllSlices": True,
59                 },
60                 (2, 2): {
61                         "CENTER": {"action": "3", "type": "text", "text": "3", },
62                         "NORTH": {"action": "[!]", "type": "text", "text": "!", },
63                         "WEST": {"action": "j", "type": "text", "text": "j", },
64                         "showAllSlices": True,
65                 },
66         },
67 }
68 PLUGIN = plugin_utils.PieKeyboardPluginFactory(_NAME, _MAP, [])
69
70 addition = operation.generate_function(operator.add, "+", operation.Function.REP_INFIX, 2)
71 subtraction = operation.generate_function(operator.sub, "-", operation.Function.REP_INFIX, 2)
72 multiplication = operation.generate_function(operator.mul, "*", operation.Function.REP_INFIX, 2)
73 trueDivision = operation.generate_function(operator.truediv, "/", operation.Function.REP_INFIX, 2)
74
75 PLUGIN.register_operation("+", addition)
76 PLUGIN.register_operation("-", subtraction)
77 PLUGIN.register_operation("*", multiplication)
78 PLUGIN.register_operation("/", trueDivision)
79
80 exponentiation = operation.generate_function(operator.pow, "**", operation.Function.REP_INFIX, 2)
81 abs = operation.generate_function(operator.abs, "abs", operation.Function.REP_FUNCTION, 1)
82 try:
83         fact_func = math.factorial
84 except AttributeError:
85         def fact_func(self, num):
86                 if num <= 0:
87                         return 1
88                 return num * fact_func(self, num - 1)
89 factorial = operation.generate_function(fact_func, "!", operation.Function.REP_POSTFIX, 1)
90 negate = operation.generate_function(operator.neg, "+-", operation.Function.REP_PREFIX, 1)
91 square = operation.generate_function((lambda self, x: x ** 2), "sq", operation.Function.REP_FUNCTION, 1)
92 square_root = operation.generate_function((lambda self, x: x ** 0.5), "sqrt", operation.Function.REP_FUNCTION, 1)
93
94 # @todo Possibly make a graphic for this of x^y
95 PLUGIN.register_operation("**", exponentiation)
96 PLUGIN.register_operation("abs", abs)
97 PLUGIN.register_operation("!", factorial)
98 PLUGIN.register_operation("+-", negate)
99 PLUGIN.register_operation("sq", square)
100 PLUGIN.register_operation("sqrt", square_root)