Merge lp:~stefanor/ibid/math-dos-382549 into lp:~ibid-core/ibid/old-trunk-pack-0.92

Proposed by Stefano Rivera
Status: Merged
Approved by: Jonathan Hitchcock
Approved revision: 647
Merged at revision: 644
Proposed branch: lp:~stefanor/ibid/math-dos-382549
Merge into: lp:~ibid-core/ibid/old-trunk-pack-0.92
Diff against target: None lines
To merge this branch: bzr merge lp:~stefanor/ibid/math-dos-382549
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve
Michael Gorven Approve
Review via email: mp+6960@code.launchpad.net
To post a comment you must log in.
lp:~stefanor/ibid/math-dos-382549 updated
647. By Stefano Rivera

factorial shouldn't allow floating point values

Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve

review: Approve
Revision history for this message
Stefano Rivera (stefanor) wrote :

Vhata: be aware that there is one extra revision, post-diff

Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/math.py'
2--- ibid/plugins/math.py 2009-05-30 16:15:04 +0000
3+++ ibid/plugins/math.py 2009-06-02 09:10:54 +0000
4@@ -71,7 +71,7 @@
5 error = unicode_output(error.strip())
6 raise Exception("BC Error: %s" % error)
7
8-help['calc'] = u'Returns the anwser to mathematical expressions'
9+help['calc'] = u'Returns the anwser to mathematical expressions. Uses Python syntax and semantics (i.e. radians)'
10 class LimitException(Exception):
11 pass
12
13@@ -81,6 +81,17 @@
14 raise LimitException
15 return pow(*args)
16
17+# Factorial is only available in 2.6:
18+try:
19+ from math import factorial
20+except ImportError:
21+ factorial = lambda x: x == 0 and 1 or reduce(lambda a, b: a * b, xrange(1, x + 1))
22+
23+def limited_factorial(x):
24+ if x >= 500:
25+ raise LimitException
26+ return factorial(x)
27+
28 # ast method
29 class PowSubstitutionTransformer(NodeTransformer):
30 def visit_BinOp(self, node):
31@@ -119,6 +130,7 @@
32 for function in extras:
33 safe[function] = eval(function)
34 safe['pow'] = limited_pow
35+ safe['factorial'] = limited_factorial
36
37 @match(r'^(?:calc\s+)?(.+?)$')
38 def calculate(self, event, expression):

Subscribers

People subscribed via source and target branches