• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Money class for Python 3

py-money

Latest PyPI version

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.

>>> from money.money import Money
>>> from money.currency import Currency
>>> m = Money('9.95', Currency.GBP)
>>> m
GBP 9.95

Money objects can also be created from and converted to sub units.

>>> m = Money.from_sub_units(499, Currency.USD)
>>> m
USD 4.99
>>> m.sub_units
499

Money is immutable and supports most mathematical and logical operators.

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

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

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

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

>>> 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 install everything needed for development with

make install

You can then run the tests from the root directory with

make test

You can run pylint from the root directory with

make pylint

This repo requires pull-request reviews for all changes on branches bound for production in accordance with Vimeo policy.

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.

More Repositories

1

psalm

A static analysis tool for finding errors in PHP applications
PHP
5,545
star
2

player.js

Interact with and control an embedded Vimeo Player.
JavaScript
1,432
star
3

graph-explorer

A graphite dashboard powered by structured metrics
Python
1,060
star
4

php-mysql-engine

A MySQL engine written in pure PHP
PHP
547
star
5

vimeo.php

Official PHP library for the Vimeo API.
PHP
450
star
6

player-api

Examples for our JavaScript and ActionScript player APIs.
449
star
7

laravel

A Vimeo bridge for Laravel
PHP
397
star
8

stag-java

Speedy Type Adapter Generation
Java
350
star
9

VIMVideoPlayer

Deprecated: Please use [PlayerKit]( https://github.com/vimeo/PlayerKit) instead.
Objective-C
281
star
10

vimeo.js

Official Node.js library for the Vimeo API.
JavaScript
267
star
11

vimeo-oembed-examples

Some examples of how to use our oEmbed endpoint
HTML
237
star
12

vimeo-unity-sdk

Easily stream your Vimeo videos into Unity or record and publish out to Vimeo.
C#
210
star
13

vimeo.py

Official Python library for the Vimeo API.
Python
210
star
14

graphite-influxdb

An influxdb backend for Graphite-web and graphite-api
Python
198
star
15

vimeo-php-lib

Our official PHP library for the Advanced API.
PHP
180
star
16

tailgate

Tailgate is a nodejs app to pipe `tail -F` into websockets. It's a very simple way to have real-time access to your logs.
JavaScript
164
star
17

PlayerKit

Swift
154
star
18

ABLincoln

A library for online experiments.
PHP
152
star
19

vimeo-networking-java

The Vimeo Java (Android) SDK
Kotlin
121
star
20

vimeo-threejs-player

A plugin for streaming video from Vimeo to WebGL/VR/AR apps
JavaScript
89
star
21

tattletale.js

A utility to send console logs over XHR for server-side processing.
JavaScript
87
star
22

VimeoNetworking

The Vimeo API iOS SDK
Swift
84
star
23

vimeo-depth-player

A WebVR volumetric video renderer that uses color-depth based videos hosted on Vimeo.
JavaScript
83
star
24

aframe-vimeo-component

Stream Vimeo videos into WebVR.
JavaScript
81
star
25

simple-black-box

A simple black-box behavior testing framework
Shell
68
star
26

pentagon

Vault <-> Kubernetes Secrets
Go
60
star
27

libvmod-boltsort

A fast Varnish module for sorting query string parameters.
C
59
star
28

VimeoUpload

The Vimeo iOS Upload SDK
Swift
59
star
29

whisper-to-influxdb

migrate (import) graphite data from whisper to influxdb
Go
58
star
30

smoketcp

Smokeping like tcp connectivity tester, reports to statsd. written in Golang
Go
58
star
31

VIMNetworking

The Vimeo iOS SDK
Objective-C
58
star
32

vimeo-depth-viewer

OpenGL application for viewing depth and color video streams from Intel RealSense cameras
C++
53
star
33

carbon-tagger

native tag-based metrics for graphite/carbon
Go
50
star
34

iris

Vimeo Design System
TypeScript
47
star
35

go-util

Small reusable Go functions.
Go
44
star
36

Blueprint

aka How We Collaborate
42
star
37

graphite-api-influxdb-docker

docker image with graphite-api and graphite-influxdb
Shell
39
star
38

go-magic

Go Bindings for libmagic and an idiomatic API for getting a file's MIME type.
Go
39
star
39

rollup-plugin-bundle-size

A rollup plugin to show the size of the generated bundle(s).
JavaScript
27
star
40

vimeo-maxmsp

Play and manipulate Vimeo videos in Max/MSP and Jitter
Max
24
star
41

dials

Dials is an extensible configuration package for Go.
Go
18
star
42

VIMDeeplink

Simple Objc and Swift wrappers around the Vimeo iOS deeplink API
Swift
17
star
43

openapi

An OpenAPI specification for the Vimeo API.
16
star
44

puppet-diamond

diamond module for puppet
Puppet
15
star
45

nagios-cloudwatch-plugin

AWS CloudWatch check Nagios plugin
Python
14
star
46

elevator

Validate and patch AV1 levels
Rust
13
star
47

puppet-statsd

statsd module for puppet
Puppet
11
star
48

VIMUpload

This library has been deprecated, use VimeoUpload instead
Objective-C
11
star
49

go-hammer

Go
10
star
50

babel-plugin-transform-i18n

A Babel transform plugin to replace strings with their translations.
JavaScript
10
star
51

VIMObjectMapper

An automatic JSON to model object converter
Objective-C
9
star
52

leaderelection

Go
9
star
53

go-clocks

A convenient package providing a Clock abstraction in Go
Go
9
star
54

go-taglog

Based on, and compatible with, the Go standard log package, but also provides additional functionality and features such as tagging.
Go
8
star
55

ios-labs-staffpicks

An iOS Labs sample project
Swift
7
star
56

grouplogger

Go
6
star
57

netstorage

go client for the new Akamai Netstorage http api
Go
6
star
58

zendesk-ticket-history

JavaScript
6
star
59

caps

Go package to read/write popular video caption formats(mostly a port of pycaption)
Go
6
star
60

Uniform

Swift
6
star
61

alog

Another Go Logging Package
Go
5
star
62

vimeo-deeplink-android

A helper library to deep link into the official Vimeo Android App
Java
5
star
63

vimeo-live-player-examples

Example application for using Vimeo Live M3U8 links with third-party players
HTML
5
star
64

graph-explorer-docker

Shell
5
star
65

eslint-config-player

ESLint config for all player team projects.
JavaScript
5
star
66

go-iccjpeg

A small utility package to extract ICC profiles from JPEGs.
Go
4
star
67

graphite-go

Go
4
star
68

go-imgparse

A small go library to efficiently parse the resolution of various image format streams.
Go
4
star
69

go-retry

A small package for doing comprehensive retries.
Go
4
star
70

omnipay-bluesnap

BlueSnap driver for the Omnipay PHP payment processing library
PHP
4
star
71

payment-gateway-logger

PHP
3
star
72

av1stats

An AV1 stream analysis CLI tool
Rust
3
star
73

omnipay-vindicia

Vindicia driver for the Omnipay PHP payment processing library
PHP
3
star
74

genepool

A golang package for building generic workpools in a standardized way
Go
2
star
75

VimeoCommon

1
star
76

k8swatcher

Convenient watching interface for Kubernetes.
Go
1
star
77

policy

1
star