Money

EuroPython 2005

Facundo Batista

facundo@taniquetil.com.ar

www.taniquetil.com.ar

Graphic art: Diana Batista

www.python.org/ar

Index

Why?

Currency data type

Behaviour

How you can help

Why?

Why a Money module?

Using Decimal directly

Some helpers

Why a Money module?

Decimal is an obvious base for Money

developers will use it directly

Do not use Decimal directly

traps

decisions to make

Helpers

Fancy printing ($1,334.20)

General currency information

Routines to deal with Money

We won't include a roulette prediction system!

Using Decimal Directly?

Decimal will solve a lot of problems

But not all of them

there're issues to be aware

example: when to round?

When do you round?

at the very end only?

(34.65 * 1.21) / 2 =

41.9265 / 2 =

20,96325 which rounded is 20,96

at every operation?

(34.65 * 1.21) / 2 =

41.93 / 2 = 20.97

Some Helpers

Fancy printing

$1,234.50 ; -1234,50 ; (USD 1234.50)

will use Raymond Hettinger's recipe: moneyfmt()

defaults first in context, then in locale

Currency information

ISO-4217 list of currency codes

we're still defining the structure

Routines

Money Present Value

Compound interests

Mortgage calculator

...

Currency Data Type

Properties

Methods

Currency Properties

Currency will hold some information

c = Currency("2.20", exchrate={'USD':'1.3'})

Value

>>> str(c)

'2.20'

Type

>>> c.type()

'EUR'

Creation date & time

>>> c.timestamp()

1114372559.921

Exchange rate at creation time:

>>> c.exchrate()

{'USD': '1.3'}

Currency Methods

formatted_str(...): returns the value in a nice format

(...) means that can accept several arguments

basically all that are also adjustable by context

>>> Currency('1.3').formatted_str()

$1.30

>>> Currency('1.3').formatted_str(decimal_sep=',')

$1,30

distrib(n): returns the amount distributed in n

diff between max and min no more than 1

the order is not assured

>>> Currency(1).distrib(3)

(Currency('0.33'), Currency('0.33'), Currency('0.34'))

>>> Currency(1).distrib(7)

(Currency('0.15'), Currency('0.14'), Currency('0.14'), Currency('0.15'), Currency('0.14'), Currency('0.14'), Currency('0.14'))

Behaviour

General

Arithmetic

Configuring Decimal

Currency Context

General Behaviour

Use decimal

surprised?

Have a context

configuration parameters

thread aware

explicit & implicit construction will follow Decimal

context will be applied in Currency instantation

Pythonic behaviour

support the built-in functions: min, hash, float, etc...

repr() should round trip

should be immutable

Arithmetic Behaviour

Of course, we'll use Decimal

supports same arithmetic

unary and binary operators

not binary floating point issues

Currency won't have NaNs or Infs

Comparison will check for currency types

>>> a = Currency('2.3', type='USD')

>>> b = Currency('1.2', type='EUR')

>>> a == b

Traceback (most recent call last):

...

Configuring Decimal

prec: 28

rounding: ROUND_HALF_EVEN

Emin: -999999999

Emax: 999999999

capitals: True (meaning uppercase E)

traps: ("On" means exception)

Clamped:	On

DivisionByZero:	On

Overflow:	On

Subnormal:	On

Underflow:	On

InvalidOperation:	On

Rounded:	Off

Inexact:	On

Green is Decimal's default

Italic is a fixed config

Currency Context

The Context will have the following attributes:

which are configurable

the value presented is the context default

rounding: rounding method (round_half_even)

prec: precision (28)

inexact_trap: if we'll have an exception on inexact (True)

curr_type: currency type ('EUR')

decimal_sep: decimal separator ('.')

curr_symbol: currency symbol ('$')

thousand_sep: thousands separator (',')

dec_quant: quantity of difgits after the decimal point (2)

always_round: if will round after each operation (False).

How You Can Help

Project in SourceForge:

http://sourceforge.net/projects/pymoney/

Mailing list:

pymoney-general (through SF)

don't want to bloat python-list or python-dev

Need discussion in the list

to define ideas

take decisions

Test cases

Show us what you want to do...

...how you want to do it...

...through examples!

Questions? Suggestions?

Sprint Thu & Fri

Join us!

Thank you!

Facundo Batista

facundo@taniquetil.com.ar

www.taniquetil.com.ar

Decimal Structure: Finite numbers, Special values

Decimal Methods: as_tuple, quantize, to_eng_strings, to_integral, adjusted, same_quantum, normalize, compare, max and min, sqrt

Using Context: Structure of a Context, Getting the current context, Setting a new context, Standard contexts, DefaultContext

Context Methods: Standard arithmetic, Specific I, Specific II

Signals: What are Signals?, Dealing with Flags, Dealing with Traps, Clamped and DivisionByZero, Inexact and Rounded, InvalidOperation and Overflow, Subnormal and Underflow

Rounding: When rounding happens, Up & down, Ceiling & floor, Half up, down & even

Usage Cases: Do use Decimal: Financial, Do use Decimal: Easier life, Don't use Decimal: Speed / Accuracy, Don't use Decimal: Directly as Money

Decimal Structure: Finite numbers, Special values

Decimal Methods: as_tuple, quantize, to_eng_strings, to_integral, adjusted, same_quantum, normalize, compare, max and min, sqrt

Using Context: Structure of a Context, Getting the current context, Setting a new context, Standard contexts, DefaultContext

Context Methods: Standard arithmetic, Specific I, Specific II

Signals: What are Signals?, Dealing with Flags, Dealing with Traps, Clamped and DivisionByZero, Inexact and Rounded, InvalidOperation and Overflow, Subnormal and Underflow

Rounding: When rounding happens, Up & down, Ceiling & floor, Half up, down & even

Usage Cases: Do use Decimal: Financial, Do use Decimal: Easier life, Don't use Decimal: Speed / Accuracy, Don't use Decimal: Directly as Money

Decimal Structure: Finite numbers, Special values

Decimal Methods: as_tuple, quantize, to_eng_strings, to_integral, adjusted, same_quantum, normalize, compare, max and min, sqrt

Using Context: Structure of a Context, Getting the current context, Setting a new context, Standard contexts, DefaultContext

Context Methods: Standard arithmetic, Specific I, Specific II

Signals: What are Signals?, Dealing with Flags, Dealing with Traps, Clamped and DivisionByZero, Inexact and Rounded, InvalidOperation and Overflow, Subnormal and Underflow

Rounding: When rounding happens, Up & down, Ceiling & floor, Half up, down & even

Usage Cases: Do use Decimal: Financial, Do use Decimal: Easier life, Don't use Decimal: Speed / Accuracy, Don't use Decimal: Directly as Money

Decimal Structure: Finite numbers, Special values

Decimal Methods: as_tuple, quantize, to_eng_strings, to_integral, adjusted, same_quantum, normalize, compare, max and min, sqrt

Using Context: Structure of a Context, Getting the current context, Setting a new context, Standard contexts, DefaultContext

Context Methods: Standard arithmetic, Specific I, Specific II

Signals: What are Signals?, Dealing with Flags, Dealing with Traps, Clamped and DivisionByZero, Inexact and Rounded, InvalidOperation and Overflow, Subnormal and Underflow

Rounding: When rounding happens, Up & down, Ceiling & floor, Half up, down & even

Usage Cases: Do use Decimal: Financial, Do use Decimal: Easier life, Don't use Decimal: Speed / Accuracy, Don't use Decimal: Directly as Money

2005-03-24T12:12:38