Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue3285] Fraction.from_any()

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Jul 4, 2008, 12:36 PM

Post #1 of 2 (292 views)
Permalink
[issue3285] Fraction.from_any()

New submission from Raymond Hettinger <rhettinger [at] users>:

After exercising the fractions module, I've found it problematic that
there isn't a unified constructor to handle mixed data input types.

For example, when updating the accurate summation recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090 to show
how it could be done with rational arithmetic. Handling mixed-type
inputs required writing a factory function:

def makefrac(x):
if isinstance(x, Decimal):
return Fraction.from_decimal(x)
if isinstance(x, float):
return Fraction.from_float(x)
return Fraction(x)

That function supported a clean version of the recipe:

def frsum(iterable):
return float(sum(map(makefrac, iterable)))

It would have been much better if that functionality were built into
the class definition. See attached patch.

----------
components: Library (Lib)
files: from_any.diff
keywords: patch
messages: 69264
nosy: rhettinger
severity: normal
status: open
title: Fraction.from_any()
type: feature request
versions: Python 2.6, Python 3.0
Added file: http://bugs.python.org/file10810/from_any.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue3285>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Jul 10, 2008, 7:36 AM

Post #2 of 2 (244 views)
Permalink
[issue3285] Fraction.from_any() [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

Adopted the solution used by functions in the math module. Functions
that accept floats also accept integral inputs. This improves
usability in face of mixed float/int data or decimal/int data.

See r64846.

----------
resolution: -> fixed
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue3285>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.