Skip to content

Commit e2f398d

Browse files
committed
Initial commit
0 parents  commit e2f398d

12 files changed

+2388
-0
lines changed

.gitignore

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
91+
# emacs recovery files
92+
*~
93+
94+
# Mac stuff
95+
.DS_Store

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Vimeo, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: install test pylint
2+
3+
install:
4+
python setup.py install
5+
pip install pylint
6+
7+
test:
8+
PYTHONPATH=PYTHONPATH:money py.test money
9+
10+
pylint:
11+
PYTHONPATH=PYTHONPATH:money python $$(which pylint) money --output-format colorized

README.rst

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
========
2+
py-money
3+
========
4+
5+
Money class for Python 3. Unlike other Python money classes, this class enforces that all monetary amounts are represented with the correct number of decimal places for the currency. For example, 3.678 USD is invalid, as is 5.5 JPY.
6+
7+
Installation
8+
============
9+
10+
Install the latest release with:
11+
12+
::
13+
14+
pip install py-money
15+
16+
Usage
17+
=====
18+
19+
A Money object can be created with an amount (specified as a string) and a currency from the Currency class.
20+
21+
.. code:: python
22+
23+
>>> from money.money import Money
24+
>>> from money.currency import Currency
25+
>>> m = Money('9.95', Currency.GBP)
26+
>>> m
27+
GBP 9.95
28+
29+
Money is immutable and supports most mathematical and logical operators.
30+
31+
.. code:: python
32+
33+
>>> m = Money('10.00', Currency.USD)
34+
>>> m / 2
35+
USD 5.00
36+
>>> m + Money('3.00', Currency.USD)
37+
USD 8.00
38+
>>> m > Money('5.55', Currency.USD)
39+
True
40+
41+
Money will automatically round to the correct number of decimal places for the currency.
42+
43+
.. code:: python
44+
45+
>>> m = Money('9.95', Currency.EUR)
46+
>>> m * 0.15
47+
EUR 1.49
48+
>>> m = Money('10', Currency.JPY)
49+
>>> m / 3
50+
JPY 3
51+
52+
Money can be formatted for different locales.
53+
54+
.. code:: python
55+
56+
>>> Money('3.24', Currency.USD).format('en_US')
57+
'$3.24'
58+
>>> Money('9.95', Currency.EUR).format('en_UK')
59+
'€5.56'
60+
>>> Money('94', Currency.JPY).format('ja_JP')
61+
'¥94'
62+
63+
Money does not support conversion between currencies and probably never will. Mathetmatical and logical operations between two money objects are only allowed if both objects are of the same currency. Otherwise, an error will be thrown.
64+
65+
Money will throw an error if you try to construct an object with an invalid amount for the currency (eg, 3.678 USD or 5.5 JPY).
66+
67+
For more examples, check out the test file!
68+
69+
Is this the money library for me?
70+
=================================
71+
72+
If you're just trying to do simple mathematical operations on money in different currencies, this library is probably perfect for you! Perhaps you're just running a store online and you need to compute sales tax.
73+
74+
.. code:: python
75+
76+
>>> subtotal = Money('9.95', Currency.USD)
77+
>>> tax = subtotal * 0.07
78+
>>> total = tax + subtotal
79+
>>> subtotal.format('en_US')
80+
'$9.95'
81+
>>> tax.format('en_US')
82+
'$0.70'
83+
>>> total.format('en_US')
84+
'$10.65'
85+
86+
All rounding will be done correctly, and you can open up in multiple countries with ease!
87+
88+
If you're doing complicated money operations that require many digits of precision for some reason (or you're running a gas station and charging that extra nine tenths of a cent), this library is not for you.
89+
90+
A word of warning: rounding is performed after each multiplication or division operation. While this is exactly what you want when computing sales tax, it may cause confusion if you're not expecting it.
91+
92+
.. code:: python
93+
94+
>>> m = Money('9.95', Currency.USD)
95+
>>> m * 0.5 * 2
96+
USD 9.96
97+
>>> m * (0.5 * 2)
98+
USD 9.95
99+
>>> m * 1
100+
USD 9.95
101+
102+
To avoid confusion, make sure you simplify your expressions!
103+
104+
Future improvements
105+
===================
106+
Support may be added one day for setting rounding modes. Foreign exchange rates will probably never be supported.
107+
108+
Contributing
109+
============
110+
Pull requests are welcome! Please include tests. You can run the tests from the root directory with
111+
112+
::
113+
114+
make test
115+
116+
You can run pylint from the root directory with
117+
118+
::
119+
120+
make pylint
121+
122+
Acknowledgements
123+
================
124+
Much of the code is borrowed from https://github.com/carlospalol/money. Much of the logic for handling foreign currencies is taken from https://github.com/sebastianbergmann/money. Money formatting is powered by `Babel <http://babel.pocoo.org/>`_.

money/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)