Re: [Sugar-devel] [PATCH] Calculate/functions.py
Excerpts from Tim McNamara's message of Thu Jun 03 08:09:59 +0200 2010: [Attached patch] What's the status of this patch? I can see quite some activity in the Calculate repository since you posted your patch, but nothing that indicates it has been merged. Is it still applicable? Sascha -- http://sascha.silbe.org/ http://www.infra-silbe.de/ signature.asc Description: PGP signature ___ Sugar-devel mailing list Sugar-devel@lists.sugarlabs.org http://lists.sugarlabs.org/listinfo/sugar-devel
Re: [Sugar-devel] [PATCH] Calculate/functions.py
On 3 June 2010 16:00, James Cameron qu...@laptop.org wrote: On Thu, Jun 03, 2010 at 03:51:46PM +1200, Tim McNamara wrote: Thanks for taking the time to review that patch. But wait, there's more. Tested-by: James Cameron qu...@laptop.org shift_right() also mentions self on the argument list, which stops it from working right. After that is fixed, shift_left() and shift_right() are usable in Calculate. Applied. Tim diff --git a/functions.py b/functions.py index ddc1088..b101a4c 100644 --- a/functions.py +++ b/functions.py @@ -1,3 +1,5 @@ +#!/usr/bin/python + # functions.py, functions available in Calculate, # by Reinier Heeres rein...@heeres.eu # @@ -14,14 +16,20 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +''' +Functions library for the Calculate Activity. + +The functions module is designed to be added as a plugin for the +Calculate Activity's astparser.AstParser class. -# Any variable or function in this module that does not start with an -# underscore ('_') will be available in Calculate through astparser.py. -# Docstrings will automatically be added to the help index for that function. -# However, instead of setting the docstring on a function in the simple way we -# add it after the function definition so that they can more easily be -# localized through gettext. +Variables and functions that do not start with an underscore ('_') +will be available in Calculate through astparser.py. +Docstrings will automatically be added to the help index for that function. +However, instead of setting the docstring on a function in the simple way we +add it after the function definition so that they can more easily be +localized through gettext. +''' import types import math import random @@ -67,10 +75,10 @@ _FUNCTIONS = [ _('tan'), _('tanh'), _('xor'), -] +] def _d(val): -'''Return a _Decimal object.''' +'''Returns a _Decimal object, an alias of decimal.Decimal.''' if isinstance(val, _Decimal): return val @@ -94,79 +102,180 @@ def _inv_scale_angle(x): return x / _angle_scaling def abs(x): + +Calculates the absolute value of x in floating point. +When x is less than 0, it returns -x. + + abs(10) +10.0 + abs(_Decimal(-10)) +10.0 + return math.fabs(x) -abs.__doc__ = _( -'abs(x), return absolute value of x, which means -x for x 0') +abs.__doc__ = _(abs.__doc__) def acos(x): + +Calculates the arc cosine of x. + +The arc cosine of x is the angle for which the cosine is x. + + acos(-0.2) +1.7721542475852274 + acos(_Decimal('-0.20')) +1.7721542475852274 + +x must be between -1 and 1. + acos(20) +Traceback (most recent call last): +... +ValueError: math domain error + return _inv_scale_angle(math.acos(x)) -acos.__doc__ = _( -'acos(x), return the arc cosine of x. This is the angle for which the cosine \ -is x. Defined for -1 = x 1') +acos.__doc__ = _(acos.__doc__) def acosh(x): + +Calculates the arc hyperbolic cosine of x. + +This is the value y for which the hyperbolic cosine equals x.' + + acosh(20) +3.6882538673612966 + acosh(20.0) +3.6882538673612966 + acosh(_Decimal('20.0')) +3.6882538673612966 + return math.acosh(x) -acosh.__doc__ = _( -'acosh(x), return the arc hyperbolic cosine of x. This is the value y for \ -which the hyperbolic cosine equals x.') +acosh.__doc__ = _(acosh.__doc__) def And(x, y): + +Logical Boolean operator and. + +Returns True if both x and y evaluate to True, +otherwise it returns False. + + And(True, True) +True + And(False, False) +False + And(True, False) +False + And(False, True) +False + return x y -And.__doc__ = _( -'And(x, y), logical and. Returns True if x and y are True, else returns False') +And.__doc__ = _(And.__doc__) def add(x, y): + +Adds x and y, returning their sum. + +If either of the varibles is an instance of decimal.Decimal, +the function will coerce the other to be that type. + if isinstance(x, _Decimal) or isinstance(y, _Decimal): x = _d(x) y = _d(y) return x + y -add.__doc__ = _('add(x, y), return x + y') +add.__doc__ = _(add.__doc__) def asin(x): + +Calculates the arc sine of x. + +This is the angle for which the sine is ix. +Accepts an x value of -1 to 1. + + asin(_Decimal('-0.20')) +-0.2013579207903308 + asin(_Decimal('-020')) +Traceback (most recent call last): +... +ValueError: math domain error + return _inv_scale_angle(math.asin(x)) -asin.__doc__ = _( -'asin(x), return the arc sine of x. This is the angle for which the sine is ix. \ -Defined for -1 = x = 1') +asin.__doc__ = _(asin.__doc__) def
[Sugar-devel] [PATCH] Calculate/functions.py
This commit makes several significant changes to this file. Overall, the desire was to aid compliance with PEP-0257. Along the way, a number of other possible enhancements were noted. Changes include - encoding declaration - module docstrings - function docstrings - minor changes to functions - whitespace removal Module docstring - added Function docstrings - have been made much more verbose Docstrings remain available to gettext. I have changed the pattern from: def spam(eggs): return eggs.with_spam() spam.__doc__ = gettext('Comments here') to: def spam(eggs): comments here return eggs.with_spam() spam.__doc__ = gettext(spam.__doc__) In order to keep with the theme of allowing the docstrings to be educational, I have included links to the English Wikipedia where appropriate (and where it was easy for me to do.) Doctests now cover most of the non-trival problems, such as direct calls thorough to Python's math module. Test coverage has moved from 0% to ~50%. Function changes - ceil() - now returns integer as described in function's documentation - _do_gcd(a, b) - removed reference to self, given that the function is not in a class - gcd(self, a, b) - removed reference to self, given that the function is not in a class - changed ValueError to TypeError - added helpful exception message - factorial - removed gettext from exception - changed ValueError to TypeError - factorize - added option to return a list of factors - kept defaults to string representation for backwards compatibility - non-integers now raise a TypeError, rather than returning 0 - fac - now indicates that it is an alias of factorial in its docstring - floor - now returns int as per documentation - removed float type coercion for values being sent to math.floor() - mod - changed ValueError to TypeError - round - returns an integer as per function's documentation - shift_left shift_right - ValueError to TypeError - sqrt - removed type coercion, after 100,000 tests, results were equivalent without it From d0ff166edf86339f309ab327be7069077cb23216 Mon Sep 17 00:00:00 2001 From: timClicks paperl...@timmcnamara.co.nz Date: Wed, 2 Jun 2010 21:45:50 +1200 Subject: [PATCH] Code docstrings cleanup, added doctests This commit makes several significant changes to this file. Overall, the desire was to aid compliance with PEP-0257. Along the way, a number of other possible enhancemences were noted. Changes include - encoding declaration - module docstrings - function docstrings - minor changes to functions - whitespace removal Module docstring - added Function docstrings - have been made much more verbose Docstrings remain available to gettext. I have changed the pattern from: def spam(eggs): return eggs.with_spam() spam.__doc__ = gettext('Comments here') to: def spam(eggs): comments here return eggs.with_spam() spam.__doc__ = gettext(spam.__doc__) In order to keep with the theme of allowing the docstrings to be educational, I have included links to the English Wikipedia where appropriate (and where it was easy for me to do.) Doctests now cover most of the non-trival problems, such as direct calls thorough to Python's math module. Test coverage has moved from 0% to ~50%. Function changes - ceil() - now returns integer as described in function's documentation - _do_gcd(a, b) - removed reference to self, given that the function is not in a class - gcd(self, a, b) - removed reference to self, given that the function is not in a class - changed ValueError to TypeError - added helpful exception message - factorial - removed gettext from exception - changed ValueError to TypeError - factorize - added option to return a list of factors - kept defaults to string representation for backwards compatibility - non-integers now raise a TypeError, rather than returning 0 - fac - now indicates that it is an alias of factorial in its docstring - floor - now returns int as per documentation - removed float type coercion for values being sent to math.floor() - mod - changed ValueError to TypeError - round - returns an integer as per function's documentation - shift_left shift_right - ValueError to TypeError - sqrt - removed type coercion, after 100,000 tests, results were equivalent without it --- functions.py | 536 +- 1 files changed, 415 insertions(+), 121 deletions(-) diff --git a/functions.py b/functions.py index ddc1088..580a867 100644 --- a/functions.py +++ b/functions.py @@ -1,6 +1,12 @@
Re: [Sugar-devel] [PATCH] Calculate/functions.py
G'day Tim, Good stuff. But please consider this patch on top of your current patch. It does this: 1. removes the assertion of your copyright ... none of the other files contain a copyright assertion, and so they don't properly comply with the guidance in COPYING How to Apply These Terms to Your New Programs, but the files do imply authorship, ... I think they should all contain a copyright, what do you think? 2. removes many of the whitespace errors introduced, 3. fixes a call to self.is_int() in mod(), which currently causes mod() to fail in Calculate, 4. fixes two calls to d() instead of _d(), which currently cause shift_left() to fail in Calculate, 5. fixes a transcription error from the XOR Wikipedia article by editing out a symbol that can't be represented in code. Another question for you ... is the docstring of sinc() supposed to have two periods trailing the formula? Based on my edits, reviewed. -- James Cameron http://quozl.linux.org.au/ diff --git a/functions.py b/functions.py index 996307d..0db019d 100644 --- a/functions.py +++ b/functions.py @@ -1,12 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- - # functions.py, functions available in Calculate, # by Reinier Heeres rein...@heeres.eu # -# Copyright 2010 Tim McNamara t...@sugarlabs.org - # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or @@ -224,9 +221,7 @@ def atanh(x): atanh.__doc__ = _(atanh.__doc__) def ceil(x): -#!/usr/bin/python - # -*- coding: encoding name -*- - + Finds the smallest integer larger than x. ceil(1.1) @@ -367,7 +362,7 @@ def factorize(x, mode='string'): If mode='string', return a string representing the factors: For example: 15 = 3 * 5. - + factorize(15) '3 * 5' factorize(90, mode='list') @@ -424,7 +419,7 @@ def is_int(n): Determines whether n is an integer. Returns True if it is, or False if not. - + is_int(1) True is_int(_Decimal('1')) @@ -474,7 +469,7 @@ def log10(x): log10.__doc__ = _(log10.__doc__) def mod(x, y): -if self.is_int(y): +if is_int(y): return x % y else: raise TypeError(_('Can only calculate x modulo integer')) @@ -571,7 +566,7 @@ def shift_left(x, y): (Multiply by 2 per bit) if is_int(x) and is_int(y): -return d(int(x) int(y)) +return _d(int(x) int(y)) else: raise TypeError(_('Bitwise operations only apply to integers')) shift_left.__doc__ = _(shift_left.__doc__) @@ -583,7 +578,7 @@ def shift_right(self, x, y): (Multiply by 2 per bit) if is_int(x) and is_int(y): -return d(int(x) int(y)) +return _d(int(x) int(y)) else: raise TypeError(_('Bitwise operations only apply to integers')) shift_right.__doc__ = _(shift_right.__doc__) @@ -592,7 +587,7 @@ def sin(x): Calculates the sine of x. This is the y-coordinate on the unit circle at the angle x. - + See also http://en.wikipedia.org/wiki/Sine return math.sin(_scale_angle(x)) @@ -600,9 +595,9 @@ sin.__doc__ = _(sin.__doc__) def sinh(x): -Calculates the hyperbolic sine of x, given by +Calculates the hyperbolic sine of x, given by (exp(x) - exp(-x)) / 2 - + See also http://en.wikipedia.org/wiki/Hyperbolic_sine return math.sinh(x) @@ -611,10 +606,10 @@ sinh.__doc__ = _( def sinc(x): -Calculates the sinc of x, given by sin(x) / x.. - +Calculates the sinc of x, given by sin(x) / x.. + From http://en.wikipedia.org/wiki/Sinc -The term sinc is a contraction of the function's +The term sinc is a contraction of the function's full Latin name, the sinus cardinalis (cardinal sine). if float(x) == 0.0: @@ -625,9 +620,9 @@ sinc.__doc__ = _(sinc.__doc__) def sqrt(x): Find the square root of x. - + The square root is the number whose square equals x. - + See also http://en.wikipedia.org/wiki/Square_root sqrt(16) @@ -652,10 +647,10 @@ sub.__doc__ = _(sub.__doc__) def tan(x): Calculates the tangent of x, by sin(x) / cos(x). - -This is the slope of the line from the origin of the unit + +This is the slope of the line from the origin of the unit circle to the point on the unit circle defined by the angle x. - + tan(0.1) 0.10033467208545055 @@ -665,9 +660,9 @@ tan.__doc__ = _(tan.__doc__) def tanh(x): Calculates the hyperbolic tangent of x. Given by sinh(x) / cosh(x) - + See also http://en.wikipedia.org/wiki/Hyperbolic_tangent - + tanh(0.1) 0.099667994624955819 @@ -677,7 +672,7 @@ tanh.__doc__ = _(tanh.__doc__) def xor(x, y): XOR tests whether x or y is true, but doesn't allow
Re: [Sugar-devel] [PATCH] Calculate/functions.py
James, Thanks for taking the time to review that patch. On 3 June 2010 15:38, James Cameron qu...@laptop.org wrote: 5. fixes a transcription error from the XOR Wikipedia article by editing out a symbol that can't be represented in code. This was the only reason why I added the encoding statement at the top. I'll remove that line also if we take out the logical symbol. Another question for you ... is the docstring of sinc() supposed to have two periods trailing the formula? No. I'll pull that also. Based on my edits, reviewed. -- James Cameron http://quozl.linux.org.au/ ___ Sugar-devel mailing list Sugar-devel@lists.sugarlabs.org http://lists.sugarlabs.org/listinfo/sugar-devel
Re: [Sugar-devel] [PATCH] Calculate/functions.py
On Thu, Jun 03, 2010 at 03:51:46PM +1200, Tim McNamara wrote: Thanks for taking the time to review that patch. But wait, there's more. Tested-by: James Cameron qu...@laptop.org shift_right() also mentions self on the argument list, which stops it from working right. After that is fixed, shift_left() and shift_right() are usable in Calculate. -- James Cameron http://quozl.linux.org.au/ ___ Sugar-devel mailing list Sugar-devel@lists.sugarlabs.org http://lists.sugarlabs.org/listinfo/sugar-devel