Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nickyr committed Apr 7, 2017
0 parents commit e2f398d
Show file tree
Hide file tree
Showing 12 changed files with 2,388 additions and 0 deletions.
95 changes: 95 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

# emacs recovery files
*~

# Mac stuff
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Vimeo, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: install test pylint

install:
python setup.py install
pip install pylint

test:
PYTHONPATH=PYTHONPATH:money py.test money

pylint:
PYTHONPATH=PYTHONPATH:money python $$(which pylint) money --output-format colorized
124 changes: 124 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
========
py-money
========

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.

Installation
============

Install the latest release with:

::

pip install py-money

Usage
=====

A Money object can be created with an amount (specified as a string) and a currency from the Currency class.

.. code:: python
>>> from money.money import Money
>>> from money.currency import Currency
>>> m = Money('9.95', Currency.GBP)
>>> m
GBP 9.95
Money is immutable and supports most mathematical and logical operators.

.. code:: python
>>> m = Money('10.00', Currency.USD)
>>> m / 2
USD 5.00
>>> m + Money('3.00', Currency.USD)
USD 8.00
>>> m > Money('5.55', Currency.USD)
True
Money will automatically round to the correct number of decimal places for the currency.

.. code:: python
>>> m = Money('9.95', Currency.EUR)
>>> m * 0.15
EUR 1.49
>>> m = Money('10', Currency.JPY)
>>> m / 3
JPY 3
Money can be formatted for different locales.

.. code:: python
>>> Money('3.24', Currency.USD).format('en_US')
'$3.24'
>>> Money('9.95', Currency.EUR).format('en_UK')
'€5.56'
>>> Money('94', Currency.JPY).format('ja_JP')
'¥94'
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.

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).

For more examples, check out the test file!

Is this the money library for me?
=================================

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.

.. code:: python
>>> subtotal = Money('9.95', Currency.USD)
>>> tax = subtotal * 0.07
>>> total = tax + subtotal
>>> subtotal.format('en_US')
'$9.95'
>>> tax.format('en_US')
'$0.70'
>>> total.format('en_US')
'$10.65'
All rounding will be done correctly, and you can open up in multiple countries with ease!

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.

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.

.. code:: python
>>> m = Money('9.95', Currency.USD)
>>> m * 0.5 * 2
USD 9.96
>>> m * (0.5 * 2)
USD 9.95
>>> m * 1
USD 9.95
To avoid confusion, make sure you simplify your expressions!

Future improvements
===================
Support may be added one day for setting rounding modes. Foreign exchange rates will probably never be supported.

Contributing
============
Pull requests are welcome! Please include tests. You can run the tests from the root directory with

::

make test

You can run pylint from the root directory with

::

make pylint

Acknowledgements
================
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/>`_.
Empty file added money/__init__.py
Empty file.
Loading

0 comments on commit e2f398d

Please sign in to comment.