Conditions | 3 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Copyright 2017 Starbot Discord Project |
||
43 | def __init__(self): |
||
44 | """ |
||
45 | expop :: '^' |
||
46 | multop :: 'x' | '/' |
||
47 | addop :: '+' | '-' |
||
48 | integer :: ['+' | '-'] '0'..'9'+ |
||
49 | atom :: PI | E | real | fn '(' expr ')' | '(' expr ')' |
||
50 | factor :: atom [ expop factor ]* |
||
51 | term :: factor [ multop factor ]* |
||
52 | expr :: term [ addop term ]* |
||
53 | """ |
||
54 | point = Literal(".") |
||
55 | exp = CaselessLiteral("E") |
||
56 | fnumber = Combine(Word("+-" + nums, nums) + |
||
57 | Optional(point + Optional(Word(nums))) + |
||
58 | Optional(exp + Word("+-" + nums, nums))) |
||
59 | ident = Word(alphas, alphas+nums+"_$") |
||
60 | plus = Literal("+") |
||
61 | minus = Literal("-") |
||
62 | mult = Literal("x") |
||
63 | div = Literal("/") |
||
64 | lpar = Literal("(").suppress() |
||
65 | rpar = Literal(")").suppress() |
||
66 | addop = plus | minus |
||
67 | multop = mult | div |
||
68 | powop = Literal("^") |
||
69 | pi = CaselessLiteral("PI") |
||
70 | expr = Forward() |
||
71 | atom = ((Optional(oneOf("- +")) + |
||
72 | (pi|exp|fnumber|ident+lpar+expr+rpar).setParseAction(self.push_first)) |
||
73 | | Optional(oneOf("- +")) + Group(lpar+expr+rpar) |
||
74 | ).setParseAction(self.push_unary_minus) |
||
75 | # by defining exponentiation as "atom [ ^ factor ]..." instead of |
||
76 | # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right |
||
77 | # that is, 2^3^2 = 2^(3^2), not (2^3)^2. |
||
78 | factor = Forward() |
||
79 | factor << atom + ZeroOrMore((powop + factor).setParseAction(self.push_first)) |
||
80 | term = factor + ZeroOrMore((multop + factor).setParseAction(self.push_first)) |
||
81 | expr << term + ZeroOrMore((addop + term).setParseAction(self.push_first)) |
||
82 | # addop_term = ( addop + term ).setParseAction( self.pushFirst ) |
||
83 | # general_term = term + ZeroOrMore( addop_term ) | OneOrMore( addop_term) |
||
84 | # expr << general_term |
||
85 | self.bnf = expr |
||
86 | # map operator symbols to corresponding arithmetic operations |
||
87 | epsilon = 1e-12 |
||
88 | self.opn = {"+" : operator.add, |
||
89 | "-" : operator.sub, |
||
90 | "x" : operator.mul, |
||
91 | "/" : operator.truediv, |
||
92 | "^" : operator.pow} |
||
93 | self.function = {"sin" : math.sin, |
||
94 | "cos" : math.cos, |
||
95 | "tan" : math.tan, |
||
96 | "abs" : abs, |
||
97 | "trunc" : lambda a: int(a), |
||
98 | "round" : round, |
||
99 | "sgn" : lambda a: abs(a) > epsilon and cmp(a, 0) or 0} |
||
100 | |||
162 |