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