py-moneyed-0.6.0/0000750000175000017500000000000012562351743013530 5ustar lukeluke00000000000000py-moneyed-0.6.0/setup.py0000750000175000017500000000302012562351720015233 0ustar lukeluke00000000000000#!/usr/bin/env python from setuptools import setup from setuptools.command.test import test as TestCommand import sys class Tox(TestCommand): def finalize_options(self): TestCommand.finalize_options(self) self.test_args = [] self.test_suite = True def run_tests(self): # import here, cause outside the eggs aren't loaded import tox errno = tox.cmdline(self.test_args) sys.exit(errno) setup( name='py-moneyed', packages=['moneyed'], version='0.6.0', description='Provides Currency and Money classes for use in your Python code.', author='Kai', author_email='k@limist.com', url='http://github.com/limist/py-moneyed', download_url='', keywords="money currency class abstraction", license='BSD', install_requires=[], classifiers=[ 'Programming Language :: Python', 'Programming Language :: Python :: 3', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Development Status :: 3 - Alpha', 'Environment :: Other Environment', 'Intended Audience :: Developers', 'Topic :: Office/Business :: Financial', 'Topic :: Software Development :: Libraries :: Python Modules'], long_description=open('README.rst', 'r').read(), extras_require={ 'tests': [ 'pytest>=2.3.0', 'tox>=1.6.0' ]}, tests_require=['tox>=1.6.0', 'pytest>=2.3.0'], cmdclass={'test': Tox}, include_package_data=True, ) py-moneyed-0.6.0/LICENSE0000640000175000017500000000274312347517533014546 0ustar lukeluke00000000000000Copyright (c) 2011 Kai Wu, k@limist.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the names of the copyright holders of this software, nor its contributors, may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. py-moneyed-0.6.0/PKG-INFO0000640000175000017500000001055012562351743014627 0ustar lukeluke00000000000000Metadata-Version: 1.1 Name: py-moneyed Version: 0.6.0 Summary: Provides Currency and Money classes for use in your Python code. Home-page: http://github.com/limist/py-moneyed Author: Kai Author-email: k@limist.com License: BSD Description: Overview ======== The need to represent instances of money frequently arises in software development, particularly any financial/economics software. To address that need, the py-moneyed package provides the classes of Money and Currency, at a level more useful than just using Python's Decimal class, or ($DEITY forbid) the float primitive. The package is meant to be stand-alone and easy to either use directly, or subclass further. py-moneyed is BSD-licensed. Some of the py-moneyed code was first derived from python-money available via this URL: http://code.google.com/p/python-money/ Because that Google Code version has been inactive since May 2008, I forked it and modified it for my needs in 2010. Compared to python-money, major changes here in py-moneyed include separating it from Django usage, tightening types handling in operators, a complete suite of unit tests, PEP8 adherence, providing a setup.py, and local currency formatting/display. Usage ----- On to the code! The Money class is instantiated with: - An amount which can be of type string, float, or Decimal. - A currency, which usually is specified by the three-capital-letters ISO currency code, e.g. USD, EUR, CNY, and so on. For example, .. sourcecode:: python from moneyed.classes import Money sale_price_today = Money(amount='99.99', currency='USD') The Money class also provides operators with type checking, matching currency checking, and sensible dimensional behavior, e.g. you cannot multiply two Money instances, nor can you add a Money instance to a non-Money number; dividing a Money instance by another results in a Decimal value, etc. The Currency class is provided with a complete dictionary of ISO 4217 currencies data, each key (e.g. 'USD') mapping to a Currency instance with ISO numeric code, canonical name in English, and countries using the currency. Thanks to the python-money developers for their (possibly tedious) data-entry of the ISO codes! Testing ------- Unit-tests have been provided, and can be run with tox_ (recommended) or just py.test. If you don't have tox installed on your system, it's a modern Python tool to automate running tests and deployment; install it to your global Python environment with: :: sudo pip install tox Then you can activate a virtualenv (any will do - by design tox will not run from your globally-installed python), cd to the py-moneyed source directory then run the tests at the shell: :: cd where/py-moneyed-source/is tox If you do not have all versions of Python that are used in testing, you can use pyenv_. After installing pyenv, install the additional plugin pyenv-implict_. The py-moneyed package has been tested with Python 2.6, 2.7, 3.2, 3.3 and PyPy 2.1. .. _tox: http://tox.testrun.org/latest/ .. _pyenv: https://github.com/yyuu/pyenv .. _pyenv-implict: https://github.com/concordusapps/pyenv-implict Future ------ Future versions of py-moneyed may provide currency conversions or other capabilities, dependent on feedback and usage. Keywords: money currency class abstraction Platform: UNKNOWN Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Development Status :: 3 - Alpha Classifier: Environment :: Other Environment Classifier: Intended Audience :: Developers Classifier: Topic :: Office/Business :: Financial Classifier: Topic :: Software Development :: Libraries :: Python Modules py-moneyed-0.6.0/moneyed/0000750000175000017500000000000012562351743015170 5ustar lukeluke00000000000000py-moneyed-0.6.0/moneyed/classes.py0000640000175000017500000004674712562341742017220 0ustar lukeluke00000000000000# -*- coding: utf-8 -*- from __future__ import division from __future__ import unicode_literals from decimal import Decimal, ROUND_DOWN import sys import warnings # Default, non-existent, currency DEFAULT_CURRENCY_CODE = 'XYZ' PYTHON2 = sys.version_info[0] == 2 class Currency(object): """ A Currency represents a form of money issued by governments, and used in one or more states/countries. A Currency instance encapsulates the related data of: the ISO currency/numeric code, a canonical name, and countries the currency is used in. """ def __init__(self, code='', numeric='999', name='', countries=None): if countries is None: countries = [] self.code = code self.countries = countries self.name = name self.numeric = numeric def __eq__(self, other): return type(self) is type(other) and self.code == other.code def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return self.code class MoneyComparisonError(TypeError): # This exception was needed often enough to merit its own # Exception class. def __init__(self, other): assert not isinstance(other, Money) self.other = other def __str__(self): # Note: at least w/ Python 2.x, use __str__, not __unicode__. return "Cannot compare instances of Money and %s" \ % self.other.__class__.__name__ class CurrencyDoesNotExist(Exception): def __init__(self, code): super(CurrencyDoesNotExist, self).__init__( "No currency with code %s is defined." % code) class Money(object): """ A Money instance is a combination of data - an amount and a currency - along with operators that handle the semantics of money operations in a better way than just dealing with raw Decimal or ($DEITY forbid) floats. """ def __init__(self, amount=Decimal('0.0'), currency=DEFAULT_CURRENCY_CODE): if not isinstance(amount, Decimal): amount = Decimal(str(amount)) self.amount = amount if not isinstance(currency, Currency): currency = get_currency(str(currency).upper()) self.currency = currency def __repr__(self): return "%s %s" % (self.amount.to_integral_value(ROUND_DOWN), self.currency) def __unicode__(self): from moneyed.localization import format_money return format_money(self) def __str__(self): from moneyed.localization import format_money return format_money(self) def __pos__(self): return self.__class__( amount=self.amount, currency=self.currency) def __neg__(self): return self.__class__( amount=-self.amount, currency=self.currency) def __add__(self, other): if other == 0: # This allows things like 'sum' to work on list of Money instances, # just like list of Decimal. return self if not isinstance(other, Money): raise TypeError('Cannot add or subtract a ' + 'Money and non-Money instance.') if self.currency == other.currency: return self.__class__( amount=self.amount + other.amount, currency=self.currency) raise TypeError('Cannot add or subtract two Money ' + 'instances with different currencies.') def __sub__(self, other): return self.__add__(-other) def __mul__(self, other): if isinstance(other, Money): raise TypeError('Cannot multiply two Money instances.') else: if isinstance(other, float): warnings.warn("Multiplying Money instances with floats is deprecated", DeprecationWarning) return self.__class__( amount=(self.amount * Decimal(str(other))), currency=self.currency) def __truediv__(self, other): if isinstance(other, Money): if self.currency != other.currency: raise TypeError('Cannot divide two different currencies.') return self.amount / other.amount else: if isinstance(other, float): warnings.warn("Dividing Money instances by floats is deprecated", DeprecationWarning) return self.__class__( amount=self.amount / Decimal(str(other)), currency=self.currency) def __abs__(self): return self.__class__( amount=abs(self.amount), currency=self.currency) def __bool__(self): return bool(self.amount) if PYTHON2: __nonzero__ = __bool__ def __rmod__(self, other): """ Calculate percentage of an amount. The left-hand side of the operator must be a numeric value. Example: >>> money = Money(200, 'USD') >>> 5 % money USD 10.00 """ if isinstance(other, Money): raise TypeError('Invalid __rmod__ operation') else: if isinstance(other, float): warnings.warn("Calculating percentages of Money instances using floats is deprecated", DeprecationWarning) return self.__class__( amount=(Decimal(str(other)) * self.amount / 100), currency=self.currency) __radd__ = __add__ __rsub__ = __sub__ __rmul__ = __mul__ __rtruediv__ = __truediv__ # _______________________________________ # Override comparison operators def __eq__(self, other): return (isinstance(other, Money) and (self.amount == other.amount) and (self.currency == other.currency)) def __ne__(self, other): result = self.__eq__(other) return not result def __lt__(self, other): if not isinstance(other, Money): raise MoneyComparisonError(other) if (self.currency == other.currency): return (self.amount < other.amount) else: raise TypeError('Cannot compare Money with different currencies.') def __gt__(self, other): if not isinstance(other, Money): raise MoneyComparisonError(other) if (self.currency == other.currency): return (self.amount > other.amount) else: raise TypeError('Cannot compare Money with different currencies.') def __le__(self, other): return self < other or self == other def __ge__(self, other): return self > other or self == other # ____________________________________________________________________ # Definitions of ISO 4217 Currencies # Source: http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm CURRENCIES = {} CURRENCIES_BY_ISO = {} def add_currency(code, numeric, name, countries): global CURRENCIES CURRENCIES[code] = Currency( code=code, numeric=numeric, name=name, countries=countries) CURRENCIES_BY_ISO[numeric] = CURRENCIES[code] return CURRENCIES[code] def get_currency(code=None, iso=None): try: if iso: return CURRENCIES_BY_ISO[str(iso)] return CURRENCIES[code] except KeyError: raise CurrencyDoesNotExist(code) DEFAULT_CURRENCY = add_currency(DEFAULT_CURRENCY_CODE, '999', 'Default currency.', []) AED = add_currency('AED', '784', 'UAE Dirham', ['UNITED ARAB EMIRATES']) AFN = add_currency('AFN', '971', 'Afghani', ['AFGHANISTAN']) ALL = add_currency('ALL', '008', 'Lek', ['ALBANIA']) AMD = add_currency('AMD', '051', 'Armenian Dram', ['ARMENIA']) ANG = add_currency('ANG', '532', 'Netherlands Antillian Guilder', ['NETHERLANDS ANTILLES']) AOA = add_currency('AOA', '973', 'Kwanza', ['ANGOLA']) ARS = add_currency('ARS', '032', 'Argentine Peso', ['ARGENTINA']) AUD = add_currency('AUD', '036', 'Australian Dollar', ['AUSTRALIA', 'CHRISTMAS ISLAND', 'COCOS (KEELING) ISLANDS', 'HEARD ISLAND AND MCDONALD ISLANDS', 'KIRIBATI', 'NAURU', 'NORFOLK ISLAND', 'TUVALU']) AWG = add_currency('AWG', '533', 'Aruban Guilder', ['ARUBA']) AZN = add_currency('AZN', '944', 'Azerbaijanian Manat', ['AZERBAIJAN']) BAM = add_currency('BAM', '977', 'Convertible Marks', ['BOSNIA AND HERZEGOVINA']) BBD = add_currency('BBD', '052', 'Barbados Dollar', ['BARBADOS']) BDT = add_currency('BDT', '050', 'Taka', ['BANGLADESH']) BGN = add_currency('BGN', '975', 'Bulgarian Lev', ['BULGARIA']) BHD = add_currency('BHD', '048', 'Bahraini Dinar', ['BAHRAIN']) BIF = add_currency('BIF', '108', 'Burundi Franc', ['BURUNDI']) BMD = add_currency('BMD', '060', 'Bermudian Dollar (customarily known as Bermuda Dollar)', ['BERMUDA']) BND = add_currency('BND', '096', 'Brunei Dollar', ['BRUNEI DARUSSALAM']) BRL = add_currency('BRL', '986', 'Brazilian Real', ['BRAZIL']) BSD = add_currency('BSD', '044', 'Bahamian Dollar', ['BAHAMAS']) BTN = add_currency('BTN', '064', 'Bhutanese ngultrum', ['BHUTAN']) BWP = add_currency('BWP', '072', 'Pula', ['BOTSWANA']) BYR = add_currency('BYR', '974', 'Belarussian Ruble', ['BELARUS']) BZD = add_currency('BZD', '084', 'Belize Dollar', ['BELIZE']) CAD = add_currency('CAD', '124', 'Canadian Dollar', ['CANADA']) CDF = add_currency('CDF', '976', 'Congolese franc', ['DEMOCRATIC REPUBLIC OF CONGO']) CHF = add_currency('CHF', '756', 'Swiss Franc', ['LIECHTENSTEIN']) CLP = add_currency('CLP', '152', 'Chilean peso', ['CHILE']) CNY = add_currency('CNY', '156', 'Yuan Renminbi', ['CHINA']) COP = add_currency('COP', '170', 'Colombian peso', ['COLOMBIA']) CRC = add_currency('CRC', '188', 'Costa Rican Colon', ['COSTA RICA']) CUC = add_currency('CUC', '931', 'Cuban convertible peso', ['CUBA']) CUP = add_currency('CUP', '192', 'Cuban Peso', ['CUBA']) CVE = add_currency('CVE', '132', 'Cape Verde Escudo', ['CAPE VERDE']) CZK = add_currency('CZK', '203', 'Czech Koruna', ['CZECH REPUBLIC']) DJF = add_currency('DJF', '262', 'Djibouti Franc', ['DJIBOUTI']) DKK = add_currency('DKK', '208', 'Danish Krone', ['DENMARK', 'FAROE ISLANDS', 'GREENLAND']) DOP = add_currency('DOP', '214', 'Dominican Peso', ['DOMINICAN REPUBLIC']) DZD = add_currency('DZD', '012', 'Algerian Dinar', ['ALGERIA']) EGP = add_currency('EGP', '818', 'Egyptian Pound', ['EGYPT']) ERN = add_currency('ERN', '232', 'Nakfa', ['ERITREA']) ETB = add_currency('ETB', '230', 'Ethiopian Birr', ['ETHIOPIA']) EUR = add_currency('EUR', '978', 'Euro', ['AKROTIRI AND DHEKELIA', 'ANDORRA', 'AUSTRIA', 'BELGIUM', 'CYPRUS', 'ESTONIA', 'FINLAND', 'FRANCE', 'GERMANY', 'GREECE', 'GUADELOUPE', 'IRELAND', 'ITALY', 'KOSOVO', 'LATVIA', 'LITHUANIA', 'LUXEMBOURG', 'MALTA' 'MARTINIQUE', 'MAYOTTE', 'MONACO', 'MONTENEGRO', 'NETHERLANDS', 'PORTUGAL', 'RÉUNION', 'SAN MARINO', 'SAINT BARTHÉLEMY', 'SAINT PIERRE AND MIQUELON', 'SAN MARINO', 'SLOVAKIA', 'SLOVENIA', 'SPAIN', 'VATICAN CITY']) FJD = add_currency('FJD', '242', 'Fiji Dollar', ['FIJI']) FKP = add_currency('FKP', '238', 'Falkland Islands Pound', ['FALKLAND ISLANDS (MALVINAS)']) GBP = add_currency('GBP', '826', 'Pound Sterling', ['UNITED KINGDOM']) GEL = add_currency('GEL', '981', 'Lari', ['GEORGIA']) GHS = add_currency('GHS', '936', 'Ghana Cedi', ['GHANA']) GIP = add_currency('GIP', '292', 'Gibraltar Pound', ['GIBRALTAR']) GMD = add_currency('GMD', '270', 'Dalasi', ['GAMBIA']) GNF = add_currency('GNF', '324', 'Guinea Franc', ['GUINEA']) GTQ = add_currency('GTQ', '320', 'Quetzal', ['GUATEMALA']) GYD = add_currency('GYD', '328', 'Guyana Dollar', ['GUYANA']) HKD = add_currency('HKD', '344', 'Hong Kong Dollar', ['HONG KONG']) HNL = add_currency('HNL', '340', 'Lempira', ['HONDURAS']) HRK = add_currency('HRK', '191', 'Croatian Kuna', ['CROATIA']) HTG = add_currency('HTG', '332', 'Haitian gourde', ['HAITI']) HUF = add_currency('HUF', '348', 'Forint', ['HUNGARY']) IDR = add_currency('IDR', '360', 'Rupiah', ['INDONESIA']) ILS = add_currency('ILS', '376', 'New Israeli Sheqel', ['ISRAEL']) IMP = add_currency('IMP', 'Nil', 'Isle of Man pount', ['ISLE OF MAN']) INR = add_currency('INR', '356', 'Indian Rupee', ['INDIA']) IQD = add_currency('IQD', '368', 'Iraqi Dinar', ['IRAQ']) IRR = add_currency('IRR', '364', 'Iranian Rial', ['IRAN']) ISK = add_currency('ISK', '352', 'Iceland Krona', ['ICELAND']) JMD = add_currency('JMD', '388', 'Jamaican Dollar', ['JAMAICA']) JOD = add_currency('JOD', '400', 'Jordanian Dinar', ['JORDAN']) JPY = add_currency('JPY', '392', 'Yen', ['JAPAN']) KES = add_currency('KES', '404', 'Kenyan Shilling', ['KENYA']) KGS = add_currency('KGS', '417', 'Som', ['KYRGYZSTAN']) KHR = add_currency('KHR', '116', 'Riel', ['CAMBODIA']) KMF = add_currency('KMF', '174', 'Comoro Franc', ['COMOROS']) KPW = add_currency('KPW', '408', 'North Korean Won', ['KOREA']) KRW = add_currency('KRW', '410', 'Won', ['KOREA']) KWD = add_currency('KWD', '414', 'Kuwaiti Dinar', ['KUWAIT']) KYD = add_currency('KYD', '136', 'Cayman Islands Dollar', ['CAYMAN ISLANDS']) KZT = add_currency('KZT', '398', 'Tenge', ['KAZAKHSTAN']) LAK = add_currency('LAK', '418', 'Kip', ['LAO PEOPLES DEMOCRATIC REPUBLIC']) LBP = add_currency('LBP', '422', 'Lebanese Pound', ['LEBANON']) LKR = add_currency('LKR', '144', 'Sri Lanka Rupee', ['SRI LANKA']) LRD = add_currency('LRD', '430', 'Liberian Dollar', ['LIBERIA']) LSL = add_currency('LSL', '426', 'Lesotho loti', ['LESOTHO']) LTL = add_currency('LTL', '440', 'Lithuanian Litas', ['LITHUANIA']) LVL = add_currency('LVL', '428', 'Latvian Lats', ['LATVIA']) LYD = add_currency('LYD', '434', 'Libyan Dinar', ['LIBYAN ARAB JAMAHIRIYA']) MAD = add_currency('MAD', '504', 'Moroccan Dirham', ['MOROCCO', 'WESTERN SAHARA']) MDL = add_currency('MDL', '498', 'Moldovan Leu', ['MOLDOVA']) MGA = add_currency('MGA', '969', 'Malagasy Ariary', ['MADAGASCAR']) MKD = add_currency('MKD', '807', 'Denar', ['MACEDONIA']) MMK = add_currency('MMK', '104', 'Kyat', ['MYANMAR']) MNT = add_currency('MNT', '496', 'Tugrik', ['MONGOLIA']) MOP = add_currency('MOP', '446', 'Pataca', ['MACAO']) MRO = add_currency('MRO', '478', 'Ouguiya', ['MAURITANIA']) MUR = add_currency('MUR', '480', 'Mauritius Rupee', ['MAURITIUS']) MVR = add_currency('MVR', '462', 'Rufiyaa', ['MALDIVES']) MWK = add_currency('MWK', '454', 'Malawian Kwacha', ['MALAWI']) MXN = add_currency('MXN', '484', 'Mexican peso', ['MEXICO']) MYR = add_currency('MYR', '458', 'Malaysian Ringgit', ['MALAYSIA']) MZN = add_currency('MZN', '943', 'Metical', ['MOZAMBIQUE']) NAD = add_currency('NAD', '516', 'Namibian Dollar', ['NAMIBIA']) NGN = add_currency('NGN', '566', 'Naira', ['NIGERIA']) NIO = add_currency('NIO', '558', 'Cordoba Oro', ['NICARAGUA']) NOK = add_currency('NOK', '578', 'Norwegian Krone', ['BOUVET ISLAND', 'NORWAY', 'SVALBARD AND JAN MAYEN']) NPR = add_currency('NPR', '524', 'Nepalese Rupee', ['NEPAL']) NZD = add_currency('NZD', '554', 'New Zealand Dollar', ['COOK ISLANDS', ', prefix=None, suffix=NoneNEW ZEALAND', 'NIUE', 'PITCAIRN', 'TOKELAU']) OMR = add_currency('OMR', '512', 'Rial Omani', ['OMAN']) PEN = add_currency('PEN', '604', 'Nuevo Sol', ['PERU']) PGK = add_currency('PGK', '598', 'Kina', ['PAPUA NEW GUINEA']) PHP = add_currency('PHP', '608', 'Philippine Peso', ['PHILIPPINES']) PKR = add_currency('PKR', '586', 'Pakistan Rupee', ['PAKISTAN']) PLN = add_currency('PLN', '985', 'Zloty', ['POLAND']) PYG = add_currency('PYG', '600', 'Guarani', ['PARAGUAY']) QAR = add_currency('QAR', '634', 'Qatari Rial', ['QATAR']) RON = add_currency('RON', '946', 'New Leu', ['ROMANIA']) RSD = add_currency('RSD', '941', 'Serbian Dinar', ['SERBIA']) RUB = add_currency('RUB', '643', 'Russian Ruble', ['RUSSIAN FEDERATION']) RWF = add_currency('RWF', '646', 'Rwanda Franc', ['RWANDA']) SAR = add_currency('SAR', '682', 'Saudi Riyal', ['SAUDI ARABIA']) SBD = add_currency('SBD', '090', 'Solomon Islands Dollar', ['SOLOMON ISLANDS']) SCR = add_currency('SCR', '690', 'Seychelles Rupee', ['SEYCHELLES']) SDG = add_currency('SDG', '938', 'Sudanese Pound', ['SUDAN']) SEK = add_currency('SEK', '752', 'Swedish Krona', ['SWEDEN']) SGD = add_currency('SGD', '702', 'Singapore Dollar', ['SINGAPORE']) SHP = add_currency('SHP', '654', 'Saint Helena Pound', ['SAINT HELENA']) SLL = add_currency('SLL', '694', 'Leone', ['SIERRA LEONE']) SOS = add_currency('SOS', '706', 'Somali Shilling', ['SOMALIA']) SRD = add_currency('SRD', '968', 'Surinam Dollar', ['SURINAME']) STD = add_currency('STD', '678', 'Dobra', ['SAO TOME AND PRINCIPE']) SYP = add_currency('SYP', '760', 'Syrian Pound', ['SYRIAN ARAB REPUBLIC']) SZL = add_currency('SZL', '748', 'Lilangeni', ['SWAZILAND']) THB = add_currency('THB', '764', 'Baht', ['THAILAND']) TJS = add_currency('TJS', '972', 'Somoni', ['TAJIKISTAN']) TMM = add_currency('TMM', '795', 'Manat', ['TURKMENISTAN']) TND = add_currency('TND', '788', 'Tunisian Dinar', ['TUNISIA']) TOP = add_currency('TOP', '776', 'Paanga', ['TONGA']) TRY = add_currency('TRY', '949', 'Turkish Lira', ['TURKEY']) TTD = add_currency('TTD', '780', 'Trinidad and Tobago Dollar', ['TRINIDAD AND TOBAGO']) TVD = add_currency('TVD', 'Nil', 'Tuvalu dollar', ['TUVALU']) TWD = add_currency('TWD', '901', 'New Taiwan Dollar', ['TAIWAN']) TZS = add_currency('TZS', '834', 'Tanzanian Shilling', ['TANZANIA']) UAH = add_currency('UAH', '980', 'Hryvnia', ['UKRAINE']) UGX = add_currency('UGX', '800', 'Uganda Shilling', ['UGANDA']) USD = add_currency('USD', '840', 'US Dollar', ['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', 'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', 'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', 'UNITED STATES', 'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', 'VIRGIN ISLANDS (U.S.)']) UYU = add_currency('UYU', '858', 'Uruguayan peso', ['URUGUAY']) UZS = add_currency('UZS', '860', 'Uzbekistan Sum', ['UZBEKISTAN']) VEF = add_currency('VEF', '937', 'Bolivar Fuerte', ['VENEZUELA']) VND = add_currency('VND', '704', 'Dong', ['VIET NAM']) VUV = add_currency('VUV', '548', 'Vatu', ['VANUATU']) WST = add_currency('WST', '882', 'Tala', ['SAMOA']) XAF = add_currency('XAF', '950', 'CFA franc BEAC', ['CAMEROON', 'CENTRAL AFRICAN REPUBLIC', 'REPUBLIC OF THE CONGO', 'CHAD', 'EQUATORIAL GUINEA', 'GABON']) XAG = add_currency('XAG', '961', 'Silver', []) XAU = add_currency('XAU', '959', 'Gold', []) XBA = add_currency('XBA', '955', 'Bond Markets Units European Composite Unit (EURCO)', []) XBB = add_currency('XBB', '956', 'European Monetary Unit (E.M.U.-6)', []) XBC = add_currency('XBC', '957', 'European Unit of Account 9(E.U.A.-9)', []) XBD = add_currency('XBD', '958', 'European Unit of Account 17(E.U.A.-17)', []) XCD = add_currency('XCD', '951', 'East Caribbean Dollar', ['ANGUILLA', 'ANTIGUA AND BARBUDA', 'DOMINICA', 'GRENADA', 'MONTSERRAT', 'SAINT KITTS AND NEVIS', 'SAINT LUCIA', 'SAINT VINCENT AND THE GRENADINES']) XDR = add_currency('XDR', '960', 'SDR', ['INTERNATIONAL MONETARY FUND (I.M.F)']) XFO = add_currency('XFO', 'Nil', 'Gold-Franc', []) XFU = add_currency('XFU', 'Nil', 'UIC-Franc', []) XOF = add_currency('XOF', '952', 'CFA Franc BCEAO', ['BENIN', 'BURKINA FASO', 'COTE D\'IVOIRE', 'GUINEA-BISSAU', 'MALI', 'NIGER', 'SENEGAL', 'TOGO']) XPD = add_currency('XPD', '964', 'Palladium', []) XPF = add_currency('XPF', '953', 'CFP Franc', ['FRENCH POLYNESIA', 'NEW CALEDONIA', 'WALLIS AND FUTUNA']) XPT = add_currency('XPT', '962', 'Platinum', []) XTS = add_currency('XTS', '963', 'Codes specifically reserved for testing purposes', []) YER = add_currency('YER', '886', 'Yemeni Rial', ['YEMEN']) ZAR = add_currency('ZAR', '710', 'Rand', ['SOUTH AFRICA']) ZMK = add_currency('ZMK', '894', 'Zambian Kwacha', []) # historical ZMW = add_currency('ZMW', '967', 'Zambian Kwacha', ['ZAMBIA']) ZWD = add_currency('ZWD', '716', 'Zimbabwe Dollar A/06', ['ZIMBABWE']) ZWL = add_currency('ZWL', '932', 'Zimbabwe dollar A/09', ['ZIMBABWE']) ZWN = add_currency('ZWN', '942', 'Zimbabwe dollar A/08', ['ZIMBABWE']) py-moneyed-0.6.0/moneyed/localization.py0000640000175000017500000003121512562341742020233 0ustar lukeluke00000000000000# -*- coding: utf-8 -*- from __future__ import unicode_literals from decimal import Decimal, ROUND_HALF_EVEN import moneyed DEFAULT = "default" class CurrencyFormatter(object): sign_definitions = {} formatting_definitions = {} def add_sign_definition(self, locale, currency, prefix='', suffix=''): locale = locale.upper() currency_code = currency.code.upper() if locale not in self.sign_definitions: self.sign_definitions[locale] = {} self.sign_definitions[locale][currency_code] = (prefix, suffix) def add_formatting_definition(self, locale, group_size, group_separator, decimal_point, positive_sign, trailing_positive_sign, negative_sign, trailing_negative_sign, rounding_method): locale = locale.upper() self.formatting_definitions[locale] = { 'group_size': group_size, 'group_separator': group_separator, 'decimal_point': decimal_point, 'positive_sign': positive_sign, 'trailing_positive_sign': trailing_positive_sign, 'negative_sign': negative_sign, 'trailing_negative_sign': trailing_negative_sign, 'rounding_method': rounding_method} def get_sign_definition(self, currency_code, locale): currency_code = currency_code.upper() if locale.upper() not in self.sign_definitions: locale = DEFAULT local_set = self.sign_definitions.get(locale.upper()) if currency_code in local_set: return local_set.get(currency_code) else: return ('', " %s" % currency_code) def get_formatting_definition(self, locale): locale = locale.upper() if locale in self.formatting_definitions: return self.formatting_definitions.get(locale) else: return self.formatting_definitions.get(DEFAULT) def format(self, money, include_symbol=True, locale=DEFAULT, decimal_places=None, rounding_method=None): locale = locale.upper() code = money.currency.code.upper() prefix, suffix = self.get_sign_definition(code, locale) formatting = self.get_formatting_definition(locale) if rounding_method is None: rounding_method = formatting['rounding_method'] if decimal_places is None: # TODO: Use individual defaults for each currency decimal_places = 2 q = Decimal(10) ** -decimal_places # 2 places --> '0.01' quantized = money.amount.quantize(q, rounding_method) negative, digits, e = quantized.as_tuple() result = [] digits = list(map(str, digits)) build, next = result.append, digits.pop # Trailing sign if negative: build(formatting['trailing_negative_sign']) else: build(formatting['trailing_positive_sign']) # Suffix if include_symbol: build(suffix) # Decimals for i in range(decimal_places): build(next() if digits else '0') # Decimal points if decimal_places: build(formatting['decimal_point']) # Grouped number if not digits: build('0') else: i = 0 while digits: build(next()) i += 1 if i == formatting['group_size'] and digits: i = 0 build(formatting['group_separator']) # Prefix if include_symbol: build(prefix) # Sign if negative: build(formatting['negative_sign']) else: build(formatting['positive_sign']) return ''.join(reversed(result)) _FORMATTER = CurrencyFormatter() format_money = _FORMATTER.format _sign = _FORMATTER.add_sign_definition _format = _FORMATTER.add_formatting_definition # FORMATTING RULES _format(DEFAULT, group_size=3, group_separator=",", decimal_point=".", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("en_US", group_size=3, group_separator=",", decimal_point=".", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("de_DE", group_size=3, group_separator=" ", decimal_point=",", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("de_AT", group_size=3, group_separator=" ", decimal_point=",", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("de_CH", group_size=3, group_separator=" ", decimal_point=".", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("sv_SE", group_size=3, group_separator=" ", decimal_point=",", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) _format("pl_PL", group_size=3, group_separator=" ", decimal_point=",", positive_sign="", trailing_positive_sign="", negative_sign="-", trailing_negative_sign="", rounding_method=ROUND_HALF_EVEN) # CURRENCY SIGNS # Default currency signs. These can be overridden for locales where # foreign or local currency signs for one reason or another differ # from the norm. # There may be errors here, they have been entered manually. Please # fork and fix if you find errors. # Code lives here (2011-05-08): https://github.com/limist/py-moneyed _sign(DEFAULT, moneyed.AED, prefix='د.إ') _sign(DEFAULT, moneyed.AFN, suffix='؋') _sign(DEFAULT, moneyed.ALL, prefix='L') _sign(DEFAULT, moneyed.AMD, prefix='Դ') _sign(DEFAULT, moneyed.ANG, prefix='ƒ') _sign(DEFAULT, moneyed.AOA, prefix='Kz') _sign(DEFAULT, moneyed.ARS, prefix='ARS$') _sign(DEFAULT, moneyed.AUD, prefix='A$') _sign(DEFAULT, moneyed.AWG, prefix='ƒ') _sign(DEFAULT, moneyed.BAM, prefix='КМ') _sign(DEFAULT, moneyed.BBD, prefix='Bds$') _sign(DEFAULT, moneyed.BDT, prefix='৳') _sign(DEFAULT, moneyed.BGN, prefix='лв') _sign(DEFAULT, moneyed.BHD, prefix='.د.ب') _sign(DEFAULT, moneyed.BIF, prefix='FBu') _sign(DEFAULT, moneyed.BMD, prefix='BD$') _sign(DEFAULT, moneyed.BND, prefix='B$') _sign(DEFAULT, moneyed.BRL, prefix='R$') _sign(DEFAULT, moneyed.BSD, prefix='B$') _sign(DEFAULT, moneyed.BTN, prefix='Nu.') _sign(DEFAULT, moneyed.BWP, prefix='P') _sign(DEFAULT, moneyed.BYR, prefix='Br') _sign(DEFAULT, moneyed.BZD, prefix='BZ$') _sign(DEFAULT, moneyed.CAD, prefix='C$') _sign(DEFAULT, moneyed.CDF, prefix='C₣') _sign(DEFAULT, moneyed.CHF, prefix='Fr.') _sign(DEFAULT, moneyed.CLP, prefix='CLP$') _sign(DEFAULT, moneyed.CNY, prefix='¥') _sign(DEFAULT, moneyed.COP, prefix='COL$') _sign(DEFAULT, moneyed.CRC, prefix='₡') _sign(DEFAULT, moneyed.CUC, prefix='CUC$') _sign(DEFAULT, moneyed.CUP, prefix='$MN') _sign(DEFAULT, moneyed.CVE, prefix='Esc') _sign(DEFAULT, moneyed.CZK, prefix='Kč') _sign(DEFAULT, moneyed.DJF, prefix='D₣') _sign(DEFAULT, moneyed.DKK, suffix=' Dkr') _sign(DEFAULT, moneyed.DOP, prefix='RD$') _sign(DEFAULT, moneyed.DZD, prefix='دج') _sign(DEFAULT, moneyed.EGP, prefix='ج.م.') _sign(DEFAULT, moneyed.ERN, prefix='Nfk') _sign(DEFAULT, moneyed.ETB, prefix='Br') _sign(DEFAULT, moneyed.EUR, suffix=' €') _sign(DEFAULT, moneyed.FJD, prefix='FJ$') _sign(DEFAULT, moneyed.FKP, prefix='FK£') _sign(DEFAULT, moneyed.GBP, prefix='GB£') _sign(DEFAULT, moneyed.GEL, prefix='ლ') _sign(DEFAULT, moneyed.GHS, prefix='₵') _sign(DEFAULT, moneyed.GIP, prefix='GIP£') _sign(DEFAULT, moneyed.GMD, prefix='D') _sign(DEFAULT, moneyed.GNF, prefix='G₣') _sign(DEFAULT, moneyed.GTQ, prefix='Q') _sign(DEFAULT, moneyed.GYD, prefix='G$') _sign(DEFAULT, moneyed.HKD, prefix='HK$') _sign(DEFAULT, moneyed.HNL, prefix='L') _sign(DEFAULT, moneyed.HRK, suffix=' kn') _sign(DEFAULT, moneyed.HTG, prefix='G') _sign(DEFAULT, moneyed.HUF, prefix='Ft') _sign(DEFAULT, moneyed.IDR, prefix='Rp') _sign(DEFAULT, moneyed.ILS, prefix='₪') _sign(DEFAULT, moneyed.IMP, prefix='IM£') _sign(DEFAULT, moneyed.INR, prefix='₹') _sign(DEFAULT, moneyed.IQD, prefix='ع.د') _sign(DEFAULT, moneyed.IRR, prefix='ریال') _sign(DEFAULT, moneyed.ISK, suffix=' Íkr') _sign(DEFAULT, moneyed.JMD, prefix='J$') _sign(DEFAULT, moneyed.JOD, prefix='JD') _sign(DEFAULT, moneyed.JPY, prefix='¥') _sign(DEFAULT, moneyed.KES, prefix='Ksh') _sign(DEFAULT, moneyed.KGS, prefix='лв') _sign(DEFAULT, moneyed.KHR, prefix='៛') _sign(DEFAULT, moneyed.KMF, prefix='C₣') _sign(DEFAULT, moneyed.KPW, prefix='₩') _sign(DEFAULT, moneyed.KRW, prefix='₩') _sign(DEFAULT, moneyed.KWD, prefix='د.ك') _sign(DEFAULT, moneyed.KYD, prefix='CI$') _sign(DEFAULT, moneyed.LAK, prefix='₭') _sign(DEFAULT, moneyed.LBP, prefix='LL') _sign(DEFAULT, moneyed.LKR, prefix='₨') _sign(DEFAULT, moneyed.LRD, prefix='LD$') _sign(DEFAULT, moneyed.LSL, prefix='M') _sign(DEFAULT, moneyed.LTL, prefix='Lt') _sign(DEFAULT, moneyed.LVL, prefix='Ls') _sign(DEFAULT, moneyed.LYD, prefix='ل.د') _sign(DEFAULT, moneyed.MAD, prefix='د.م.') _sign(DEFAULT, moneyed.MGA, prefix='Ar') _sign(DEFAULT, moneyed.MKD, prefix='ден') _sign(DEFAULT, moneyed.MMK, prefix='K') _sign(DEFAULT, moneyed.MNT, prefix='₮') _sign(DEFAULT, moneyed.MOP, prefix='MOP$') _sign(DEFAULT, moneyed.MRO, prefix='UM') _sign(DEFAULT, moneyed.MUR, prefix='₨') _sign(DEFAULT, moneyed.MVR, prefix='Rf.') _sign(DEFAULT, moneyed.MWK, prefix='MK') _sign(DEFAULT, moneyed.MXN, prefix='Mex$') _sign(DEFAULT, moneyed.MYR, prefix='RM') _sign(DEFAULT, moneyed.MZN, prefix='MT') _sign(DEFAULT, moneyed.NAD, prefix='N$') _sign(DEFAULT, moneyed.NGN, prefix='₦') _sign(DEFAULT, moneyed.NIO, prefix='C$') _sign(DEFAULT, moneyed.NOK, suffix=' Nkr') _sign(DEFAULT, moneyed.NPR, prefix='₨') _sign(DEFAULT, moneyed.NZD, prefix='NZ$') _sign(DEFAULT, moneyed.OMR, prefix='ر.ع.') _sign(DEFAULT, moneyed.PEN, prefix='S/.') _sign(DEFAULT, moneyed.PGK, prefix='K') _sign(DEFAULT, moneyed.PHP, prefix='₱') _sign(DEFAULT, moneyed.PKR, prefix='₨') _sign(DEFAULT, moneyed.PLN, suffix=' zł') _sign(DEFAULT, moneyed.PYG, prefix='₲') _sign(DEFAULT, moneyed.QAR, prefix='ر.ق') _sign(DEFAULT, moneyed.RSD, prefix='RSD ') _sign(DEFAULT, moneyed.RUB, prefix='руб.') _sign(DEFAULT, moneyed.RWF, prefix='FRw') _sign(DEFAULT, moneyed.SAR, prefix='ر.س') _sign(DEFAULT, moneyed.SBD, prefix='SI$') _sign(DEFAULT, moneyed.SCR, prefix='SRe') _sign(DEFAULT, moneyed.SDG, prefix='S£') _sign(DEFAULT, moneyed.SEK, suffix=' Skr') _sign(DEFAULT, moneyed.SGD, prefix='S$') _sign(DEFAULT, moneyed.SHP, prefix='SH£') _sign(DEFAULT, moneyed.SLL, prefix='Le') _sign(DEFAULT, moneyed.SOS, prefix='Sh.So.') _sign(DEFAULT, moneyed.SRD, prefix='SRD$') _sign(DEFAULT, moneyed.STD, prefix='Db') _sign(DEFAULT, moneyed.SYP, prefix='£S') _sign(DEFAULT, moneyed.SZL, prefix='E') _sign(DEFAULT, moneyed.THB, prefix='฿') _sign(DEFAULT, moneyed.TND, prefix='د.ت') _sign(DEFAULT, moneyed.TOP, prefix='TOP$') _sign(DEFAULT, moneyed.TRY, prefix='₺') _sign(DEFAULT, moneyed.TTD, prefix='TT$') _sign(DEFAULT, moneyed.TVD, prefix='$T') _sign(DEFAULT, moneyed.TWD, prefix='NT$') _sign(DEFAULT, moneyed.UAH, prefix='₴') _sign(DEFAULT, moneyed.UGX, prefix='USh') _sign(DEFAULT, moneyed.USD, prefix='US$') _sign(DEFAULT, moneyed.UYU, prefix='$U') _sign(DEFAULT, moneyed.VEF, prefix='Bs.') _sign(DEFAULT, moneyed.VND, prefix='₫') _sign(DEFAULT, moneyed.VUV, prefix='VT') _sign(DEFAULT, moneyed.WST, prefix='WS$') _sign(DEFAULT, moneyed.XAF, prefix='FCFA') _sign(DEFAULT, moneyed.XCD, prefix='EC$') _sign(DEFAULT, moneyed.XDR, prefix='SDR') _sign(DEFAULT, moneyed.XOF, prefix='CFA') _sign(DEFAULT, moneyed.ZAR, prefix='R') _sign(DEFAULT, moneyed.ZMK, prefix='ZK') _sign(DEFAULT, moneyed.ZMW, prefix='ZK') _sign(DEFAULT, moneyed.ZWL, prefix='Z$') _sign('en_US', moneyed.USD, prefix='$') _sign('en_UK', moneyed.GBP, prefix='£') _sign('sv_SE', moneyed.SEK, prefix=' kr') _sign('pl_PL', moneyed.PLN, suffix=' zł') _sign('de_DE', moneyed.EUR, suffix=' €') _sign('de_AT', moneyed.EUR, suffix=' €') _sign('de_CH', moneyed.CHF, prefix='Fr.') # Adding translations for missing currencies _sign('en_US', moneyed.KWD, prefix='KD') _sign('en_US', moneyed.BHD, prefix='BD') _sign('en_US', moneyed.SAR, prefix='SR') _sign('en_US', moneyed.DZD, prefix='DA') _sign('en_US', moneyed.LYD, prefix='LD') _sign('en_US', moneyed.TND, prefix='DT') _sign('en_US', moneyed.AED, prefix='Dhs') _sign('en_US', moneyed.EGP, prefix='L.E.') _sign('en_US', moneyed.QAR, prefix='QR') py-moneyed-0.6.0/moneyed/test_moneyed_classes.py0000640000175000017500000002645612562341742021772 0ustar lukeluke00000000000000# -*- encoding: utf-8 -*- from __future__ import division from __future__ import unicode_literals from copy import deepcopy from decimal import Decimal import warnings import pytest # Works with less code, more consistency than unittest. from moneyed.classes import Currency, Money, MoneyComparisonError, CURRENCIES, DEFAULT_CURRENCY, USD, get_currency from moneyed.localization import format_money class TestCurrency: def setup_method(self, method): self.default_curr_code = 'XYZ' self.default_curr = CURRENCIES[self.default_curr_code] def test_init(self): usd_countries = CURRENCIES['USD'].countries US_dollars = Currency( code='USD', numeric='840', name='US Dollar', countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', 'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', 'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', 'UNITED STATES', 'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', 'VIRGIN ISLANDS (U.S.)']) assert US_dollars.code == 'USD' assert US_dollars.countries == usd_countries assert US_dollars.name == 'US Dollar' assert US_dollars.numeric == '840' def test_repr(self): assert str(self.default_curr) == self.default_curr_code def test_compare(self): other = deepcopy(self.default_curr) # equality assert self.default_curr == CURRENCIES['XYZ'] assert self.default_curr == other # non-equality other.code = 'USD' assert self.default_curr != other assert self.default_curr != CURRENCIES['USD'] def test_fetching_currency_by_iso_code(self): assert get_currency('USD') == USD assert get_currency(iso='840') == USD assert get_currency(iso=840) == USD class TestMoney: def setup_method(self, method): self.one_million_decimal = Decimal('1000000') self.USD = CURRENCIES['USD'] self.one_million_bucks = Money(amount=self.one_million_decimal, currency=self.USD) def test_init(self): one_million_dollars = Money(amount=self.one_million_decimal, currency=self.USD) assert one_million_dollars.amount == self.one_million_decimal assert one_million_dollars.currency == self.USD def test_init_string_currency_code(self): one_million_dollars = Money(amount=self.one_million_decimal, currency='usd') assert one_million_dollars.amount == self.one_million_decimal assert one_million_dollars.currency == self.USD def test_init_default_currency(self): one_million = self.one_million_decimal one_million_dollars = Money(amount=one_million) # No currency given! assert one_million_dollars.amount == one_million assert one_million_dollars.currency == DEFAULT_CURRENCY def test_init_float(self): one_million_dollars = Money(amount=1000000.0) assert one_million_dollars.amount == self.one_million_decimal def test_repr(self): assert repr(self.one_million_bucks) == '1000000 USD' assert repr(Money(Decimal('2.000'), 'PLN')) == '2 PLN' m_1 = Money(Decimal('2.000'), 'PLN') m_2 = Money(Decimal('2.000000'), 'PLN') assert repr(m_1) == repr(m_2) def test_str(self): assert str(self.one_million_bucks) == 'US$1,000,000.00' def test_format_money(self): # Two decimal places by default assert format_money(self.one_million_bucks) == 'US$1,000,000.00' # No decimal point without fractional part assert format_money(self.one_million_bucks, decimal_places=0) == 'US$1,000,000' # locale == pl_PL one_million_pln = Money('1000000', 'PLN') # Two decimal places by default assert format_money(one_million_pln, locale='pl_PL') == '1 000 000,00 zł' assert format_money(self.one_million_bucks, locale='pl_PL') == '1 000 000,00 USD' # No decimal point without fractional part assert format_money(one_million_pln, locale='pl_PL', decimal_places=0) == '1 000 000 zł' def test_add(self): assert (self.one_million_bucks + self.one_million_bucks == Money(amount='2000000', currency=self.USD)) def test_add_non_money(self): with pytest.raises(TypeError): Money(1000) + 123 def test_sub(self): zeroed_test = self.one_million_bucks - self.one_million_bucks assert zeroed_test == Money(amount=0, currency=self.USD) def test_sub_non_money(self): with pytest.raises(TypeError): Money(1000) - 123 def test_mul(self): x = Money(amount=111.33, currency=self.USD) assert 3 * x == Money(333.99, currency=self.USD) assert Money(333.99, currency=self.USD) == 3 * x def test_mul_float_warning(self): # This should be changed to TypeError exception after deprecation period is over. with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") Money(amount="10") * 1.2 assert "Multiplying Money instances with floats is deprecated" in [w.message.args[0] for w in warning_list] with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") 1.2 * Money(amount="10") assert "Multiplying Money instances with floats is deprecated" in [w.message.args[0] for w in warning_list] def test_mul_bad(self): with pytest.raises(TypeError): self.one_million_bucks * self.one_million_bucks def test_div(self): x = Money(amount=50, currency=self.USD) y = Money(amount=2, currency=self.USD) assert x / y == Decimal(25) def test_div_mismatched_currencies(self): x = Money(amount=50, currency=self.USD) y = Money(amount=2, currency=CURRENCIES['CAD']) with pytest.raises(TypeError): assert x / y == Money(amount=25, currency=self.USD) def test_div_by_non_Money(self): x = Money(amount=50, currency=self.USD) y = 2 assert x / y == Money(amount=25, currency=self.USD) def test_div_float_warning(self): # This should be changed to TypeError exception after deprecation period is over. with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") Money(amount="10") / 1.2 assert "Dividing Money instances by floats is deprecated" in [w.message.args[0] for w in warning_list] def test_rmod(self): assert 1 % self.one_million_bucks == Money(amount=10000, currency=self.USD) def test_rmod_bad(self): with pytest.raises(TypeError): assert (self.one_million_bucks % self.one_million_bucks == 1) def test_rmod_float_warning(self): # This should be changed to TypeError exception after deprecation period is over. with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter("always") 2.0 % Money(amount="10") assert "Calculating percentages of Money instances using floats is deprecated" in [w.message.args[0] for w in warning_list] def test_convert_to_default(self): # Currency conversions are not implemented as of 2/2011; when # they are working, then convert_to_default and convert_to # will need to be tested. pass # Note: no tests for __eq__ as it's quite thoroughly covered in # the assert comparisons throughout these tests. def test_ne(self): x = Money(amount=1, currency=self.USD) assert self.one_million_bucks != x def test_equality_to_other_types(self): x = Money(amount=0, currency=self.USD) assert x != None # NOQA assert x != {} def test_not_equal_to_decimal_types(self): assert self.one_million_bucks != self.one_million_decimal def test_lt(self): x = Money(amount=1, currency=self.USD) assert x < self.one_million_bucks def test_lt_mistyped(self): x = 1.0 with pytest.raises(MoneyComparisonError): assert x < self.one_million_bucks def test_gt(self): x = Money(amount=1, currency=self.USD) assert self.one_million_bucks > x def test_gt_mistyped(self): x = 1.0 with pytest.raises(MoneyComparisonError): assert self.one_million_bucks > x def test_abs(self): abs_money = Money(amount=1, currency=self.USD) x = Money(amount=-1, currency=self.USD) assert abs(x) == abs_money y = Money(amount=1, currency=self.USD) assert abs(y) == abs_money def test_sum(self): assert (sum([Money(amount=1, currency=self.USD), Money(amount=2, currency=self.USD)]) == Money(amount=3, currency=self.USD)) def test_arithmetic_operations_return_real_subclass_instance(self): """ Arithmetic operations on a subclass instance should return instances in the same subclass type. """ extended_money = ExtendedMoney(amount=2, currency=self.USD) operated_money = +extended_money assert type(extended_money) == type(operated_money) operated_money = -extended_money assert type(extended_money) == type(operated_money) operated_money = ExtendedMoney(amount=1, currency=self.USD) + ExtendedMoney(amount=1, currency=self.USD) assert type(extended_money) == type(operated_money) operated_money = ExtendedMoney(amount=3, currency=self.USD) - Money(amount=1, currency=self.USD) assert type(extended_money) == type(operated_money) operated_money = (1 * extended_money) assert type(extended_money) == type(operated_money) operated_money = (extended_money / 1) assert type(extended_money) == type(operated_money) operated_money = abs(ExtendedMoney(amount=-2, currency=self.USD)) assert type(extended_money) == type(operated_money) operated_money = (50 % ExtendedMoney(amount=4, currency=self.USD)) assert type(extended_money) == type(operated_money) def test_can_call_subclass_method_after_arithmetic_operations(self): """ Calls to `ExtendedMoney::do_my_behaviour` method throws AttributeError: 'Money' object has no attribute 'do_my_behaviour' if multiplication operator doesn't return subclass instance. """ extended_money = ExtendedMoney(amount=2, currency=self.USD) # no problem extended_money.do_my_behaviour() # throws error if `__mul__` doesn't return subclass instance (1 * extended_money).do_my_behaviour() def test_bool(self): assert bool(Money(amount=1, currency=self.USD)) assert not bool(Money(amount=0, currency=self.USD)) class ExtendedMoney(Money): def do_my_behaviour(self): pass py-moneyed-0.6.0/moneyed/__init__.py0000640000175000017500000000003712562341742017300 0ustar lukeluke00000000000000from .classes import * # NOQA py-moneyed-0.6.0/setup.cfg0000640000175000017500000000007312562351743015352 0ustar lukeluke00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 py-moneyed-0.6.0/py_moneyed.egg-info/0000750000175000017500000000000012562351743017372 5ustar lukeluke00000000000000py-moneyed-0.6.0/py_moneyed.egg-info/dependency_links.txt0000640000175000017500000000000112562351743023441 0ustar lukeluke00000000000000 py-moneyed-0.6.0/py_moneyed.egg-info/top_level.txt0000640000175000017500000000001012562351743022114 0ustar lukeluke00000000000000moneyed py-moneyed-0.6.0/py_moneyed.egg-info/SOURCES.txt0000640000175000017500000000050412562351743021256 0ustar lukeluke00000000000000CONTRIBUTORS LICENSE MANIFEST.in README.rst setup.py tox.ini moneyed/__init__.py moneyed/classes.py moneyed/localization.py moneyed/test_moneyed_classes.py py_moneyed.egg-info/PKG-INFO py_moneyed.egg-info/SOURCES.txt py_moneyed.egg-info/dependency_links.txt py_moneyed.egg-info/requires.txt py_moneyed.egg-info/top_level.txtpy-moneyed-0.6.0/py_moneyed.egg-info/PKG-INFO0000640000175000017500000001055012562351743020471 0ustar lukeluke00000000000000Metadata-Version: 1.1 Name: py-moneyed Version: 0.6.0 Summary: Provides Currency and Money classes for use in your Python code. Home-page: http://github.com/limist/py-moneyed Author: Kai Author-email: k@limist.com License: BSD Description: Overview ======== The need to represent instances of money frequently arises in software development, particularly any financial/economics software. To address that need, the py-moneyed package provides the classes of Money and Currency, at a level more useful than just using Python's Decimal class, or ($DEITY forbid) the float primitive. The package is meant to be stand-alone and easy to either use directly, or subclass further. py-moneyed is BSD-licensed. Some of the py-moneyed code was first derived from python-money available via this URL: http://code.google.com/p/python-money/ Because that Google Code version has been inactive since May 2008, I forked it and modified it for my needs in 2010. Compared to python-money, major changes here in py-moneyed include separating it from Django usage, tightening types handling in operators, a complete suite of unit tests, PEP8 adherence, providing a setup.py, and local currency formatting/display. Usage ----- On to the code! The Money class is instantiated with: - An amount which can be of type string, float, or Decimal. - A currency, which usually is specified by the three-capital-letters ISO currency code, e.g. USD, EUR, CNY, and so on. For example, .. sourcecode:: python from moneyed.classes import Money sale_price_today = Money(amount='99.99', currency='USD') The Money class also provides operators with type checking, matching currency checking, and sensible dimensional behavior, e.g. you cannot multiply two Money instances, nor can you add a Money instance to a non-Money number; dividing a Money instance by another results in a Decimal value, etc. The Currency class is provided with a complete dictionary of ISO 4217 currencies data, each key (e.g. 'USD') mapping to a Currency instance with ISO numeric code, canonical name in English, and countries using the currency. Thanks to the python-money developers for their (possibly tedious) data-entry of the ISO codes! Testing ------- Unit-tests have been provided, and can be run with tox_ (recommended) or just py.test. If you don't have tox installed on your system, it's a modern Python tool to automate running tests and deployment; install it to your global Python environment with: :: sudo pip install tox Then you can activate a virtualenv (any will do - by design tox will not run from your globally-installed python), cd to the py-moneyed source directory then run the tests at the shell: :: cd where/py-moneyed-source/is tox If you do not have all versions of Python that are used in testing, you can use pyenv_. After installing pyenv, install the additional plugin pyenv-implict_. The py-moneyed package has been tested with Python 2.6, 2.7, 3.2, 3.3 and PyPy 2.1. .. _tox: http://tox.testrun.org/latest/ .. _pyenv: https://github.com/yyuu/pyenv .. _pyenv-implict: https://github.com/concordusapps/pyenv-implict Future ------ Future versions of py-moneyed may provide currency conversions or other capabilities, dependent on feedback and usage. Keywords: money currency class abstraction Platform: UNKNOWN Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Development Status :: 3 - Alpha Classifier: Environment :: Other Environment Classifier: Intended Audience :: Developers Classifier: Topic :: Office/Business :: Financial Classifier: Topic :: Software Development :: Libraries :: Python Modules py-moneyed-0.6.0/py_moneyed.egg-info/requires.txt0000640000175000017500000000004212562351743021767 0ustar lukeluke00000000000000 [tests] pytest>=2.3.0 tox>=1.6.0 py-moneyed-0.6.0/README.rst0000640000175000017500000000573512547221520015222 0ustar lukeluke00000000000000Overview ======== The need to represent instances of money frequently arises in software development, particularly any financial/economics software. To address that need, the py-moneyed package provides the classes of Money and Currency, at a level more useful than just using Python's Decimal class, or ($DEITY forbid) the float primitive. The package is meant to be stand-alone and easy to either use directly, or subclass further. py-moneyed is BSD-licensed. Some of the py-moneyed code was first derived from python-money available via this URL: http://code.google.com/p/python-money/ Because that Google Code version has been inactive since May 2008, I forked it and modified it for my needs in 2010. Compared to python-money, major changes here in py-moneyed include separating it from Django usage, tightening types handling in operators, a complete suite of unit tests, PEP8 adherence, providing a setup.py, and local currency formatting/display. Usage ----- On to the code! The Money class is instantiated with: - An amount which can be of type string, float, or Decimal. - A currency, which usually is specified by the three-capital-letters ISO currency code, e.g. USD, EUR, CNY, and so on. For example, .. sourcecode:: python from moneyed.classes import Money sale_price_today = Money(amount='99.99', currency='USD') The Money class also provides operators with type checking, matching currency checking, and sensible dimensional behavior, e.g. you cannot multiply two Money instances, nor can you add a Money instance to a non-Money number; dividing a Money instance by another results in a Decimal value, etc. The Currency class is provided with a complete dictionary of ISO 4217 currencies data, each key (e.g. 'USD') mapping to a Currency instance with ISO numeric code, canonical name in English, and countries using the currency. Thanks to the python-money developers for their (possibly tedious) data-entry of the ISO codes! Testing ------- Unit-tests have been provided, and can be run with tox_ (recommended) or just py.test. If you don't have tox installed on your system, it's a modern Python tool to automate running tests and deployment; install it to your global Python environment with: :: sudo pip install tox Then you can activate a virtualenv (any will do - by design tox will not run from your globally-installed python), cd to the py-moneyed source directory then run the tests at the shell: :: cd where/py-moneyed-source/is tox If you do not have all versions of Python that are used in testing, you can use pyenv_. After installing pyenv, install the additional plugin pyenv-implict_. The py-moneyed package has been tested with Python 2.6, 2.7, 3.2, 3.3 and PyPy 2.1. .. _tox: http://tox.testrun.org/latest/ .. _pyenv: https://github.com/yyuu/pyenv .. _pyenv-implict: https://github.com/concordusapps/pyenv-implict Future ------ Future versions of py-moneyed may provide currency conversions or other capabilities, dependent on feedback and usage. py-moneyed-0.6.0/tox.ini0000640000175000017500000000041412562342146015040 0ustar lukeluke00000000000000[tox] # Add to .travis.yml when you add to this envlist = py26,py27,py32,py33,pypy,flake,checkmanifest [testenv] deps= pytest commands = py.test [testenv:flake] deps = flake commands = flake8 [testenv:checkmanifest] deps = check-manifest commands = check-manifest py-moneyed-0.6.0/CONTRIBUTORS0000640000175000017500000000050212547221520015376 0ustar lukeluke00000000000000py-moneyed was started by Kai Wu in July 2010 and publicly released in February 2011. Jacob Hansson (jacob@voltvoodoo.com) contributed a better currency formatting implementation, and various patches, in May 2011. Wojciech Banas added compatibility with multiple versions of Python, including Python 3.x, in August 2013.py-moneyed-0.6.0/MANIFEST.in0000640000175000017500000000011012562341742015255 0ustar lukeluke00000000000000include README.rst include CONTRIBUTORS include LICENSE include tox.ini