Re: [Pymoney-general] Currency's distrib() method
Brought to you by:
facundobatista
From: Facundo B. <fac...@gm...> - 2005-07-01 14:51:26
|
On 7/1/05, Darek Suchojad <ds...@ti...> wrote: > Then let's add it. We can always get rid of it if it doesn't survive > the public discussion on the PEP. OK. We was forgetting another issue here. Some people don't want the distribution to be to the cent, dime, or whatever, but to a specific value. So, here is what I think will be pretty much the final version of distrib(). Take a look to the examples, because my idea is to include them as test cases. Please send other examples if you feel that is good to test other corners of the problem. ---------------------- distrib(nparts=3DNone, proportions=3DNone, LSV=3DNone) You can call distrib() with nparts XOR proportions. You can not callit with both of them, neither with none of them. In any case, you also can pass LSV. If you call it with ``nparts`` (an integer or something that can be int()'ed), the system will distribute the amount in that quantity of parts, in the same proportion:: >>> Currency(1).distrib(nparts=3D3) (Currency('0.33'), Currency('0.33'), Currency('0.34')) >>> >>> Currency(1).distrib(nparts=3D7) (Currency('0.15'), Currency('0.14'), Currency('0.15'), Currency('0.14'), Currency('0.14'), Currency('0.14'), Currency('0.14')) If you call it with ``proportions`` (a tuple or something that can be tuple()'d; and in each position of the tuple, a Decimal or something that could be Decimal()'ed), the system will distribute the amount regarding those proportions:: >>> Currency(1).distrib(proportions=3D[1,2]) [Currency("0.33"), Currency("0.67")] >>> >>> Currency(3).distrib(proportions=3D[2,1,3,3]) [Currency("0.66"), Currency("0.33"), Currency("1.01"), Currency("1.00")] ``LSV`` means "least significant value, and is the value to which the system will aproximate the finals results. The default is ``10**(-dec_places)``, so if you have ``dec_places=3D2``, ``LSV`` will be ``0.01`` (which is what we used in the examples above). It must be a Decimal or something that can be Decimal()'ed. >>> Currency(1).distrib(nparts=3D3, LSV=3DDecimal("0.1")) (Currency('0.40'), Currency('0.30'), Currency('0.30')) >>> >>> Currency(1).distrib(nparts=3D7, LSV=3D"0.05") (Currency('0.15'), Currency('0.10'), Currency('0.15'), Currency('0.15'), Currency('0.15'), Currency('0.15'), Currency('0.15')) >>> >>> Currency(1).distrib(proportions=3D[1,2], LSV=3D"0.2") [Currency("0.40"), Currency("0.60")] >>> >>> Currency(3).distrib(proportions=3D[2,1,3,3], LSV=3DDecimal("0.15")) [Currency("0.60"), Currency("0.30"), Currency("1.05"), Currency("1.05")] This is needed (at least) in Sweden, where they're used to round to 50 cents when dealing with coins. ---------------- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ |