This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: give round() kwargs (mostly for PFAs)
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, tim.peters, wesc
Priority: normal Keywords: patch

Created on 2006-03-29 07:30 by wesc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bltinmodule.zip wesc, 2006-03-29 07:30 ZIP file with pre-patch and patched file, C and U diffs, and test output
Messages (3)
msg49878 - (view) Author: Wesley J. Chun (wesc) Date: 2006-03-29 07:30
1. WHAT
this is a short non-critical patch to give round()
keyword arguments for Partial Function Application
(PEP309) purposes.

2. WHY
arguments that appear on the RHS of function
declarations are hard to call when using
functional.partial because the "variable" argument is
to the left (since PFAs pretty much favor only variable
arguments to the right). if fixed arguments are to the
right (are "curried"), then we need to support keyword
args for them as much as possible to encourage their use.

3. MOTIVATION
similar to the example in the Python Lib Ref doc page
for the functional module and its use with int()...

http://docs.python.org/dev/lib/module-functional.html

>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18

...we should also allow round() to be used in the same
way...

>>> from functional import partial
>>>
>>> roundTwo = partial(round, ndigits=2)
>>> print '%.2f' % roundTwo(1.5555555)
1.56
>>> print '%.2f' % roundTwo(1.5545555)
1.55
>>> print '%.2f' % roundTwo(1.5445555)
1.54

without this patch, users current get this:

>>> from functional import partial
>>> roundTwo = partial(round, ndigits=2)
>>> '%.2f' % roundTwo(1.5555555)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: round() takes no keyword arguments

4. THE FIX
the number variable in bltinmodule.c is renamed from
'x' to 'number', and the keyword is named 'ndigits'
after the doc string:

>>> print round.__doc__
round(number[, ndigits]) -> floating point number

this patch consists of a single file,
Python/bltinmodule.c.  the original CVS version from
Mar 28, the patched version, output from running
test/test_builtin.py, and both the C and U diff files
are included in the attached ZIP file.

5. TESTING
as mentioned above, we ran the test/test_builtin.py
script and there are no changes in the execution of
test_round:

    :
test_repr (test.test_builtin.BuiltinTest) ... ok
test_round (test.test_builtin.BuiltinTest) ... ok
test_setattr (test.test_builtin.BuiltinTest) ... ok
    :

the full output file is in the ZIP file.
msg49879 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-03-31 18:32
Logged In: YES 
user_id=31435

Marked as Accepted.  Thanks!

Note that, in the future, a unified diff is sufficient.  The
other stuff doesn't really help, and "hiding" the patch in
an archive file actually makes it harder for people to review.

Ah!  That all obscured something important:  the patch
doesn't add test cases.  When you add new features, you must
also add new tests.

Set the Resolution back to None.  When new tests are added,
the patch can be applied.
msg49880 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-03-31 18:55
Logged In: YES 
user_id=849994

Added tests and checked in in rev. 43496.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43108
2006-03-29 07:30:07wesccreate