Giter Site home page Giter Site logo

san4ezy / moneylib Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 5 KB

Python library for easy deal with money and currencies. The main point is that any operations with money will be processed with the amount in the particular currency. You cannot use an amount without currency, because it could confuse you. You no longer have to watch so that the amounts and currencies move together.

License: MIT License

Python 100.00%

moneylib's Introduction

money

Python library for easy deal with money and currencies. The main point is that any operations with money will be processed with the amount in the particular currency. You cannot use an amount without currency, because it could confuse you. You no longer have to watch so that the amounts and currencies move together.

Installation

pip install moneylib - in your environment.

How to use?

Firstly, you have to specify needed currencies. Where first argument is name, second argument is a currency code, third argument is a decimal places. You also can define prefix and suffix for your currency. Them will be used for building of string representation of Money.

from money import Currency, update_currencies

UAH = Currency('UAH', 'uah', 2, suffix=' uah')
USD = Currency('USD', 'usd', 2, prefix='$')
BTC = Currency('BTC', 'btc', 8, suffix=' btc')
update_currencies([UAH, USD, BTC])

Now, you can use defined variables or also you can use money.Currency.get method. Notice, if you try to get undefined Currency, will be raised money.exception.CurrencyDoesNotExist exception. It you have two or more defined currencies with the same codes, will be raised 'money.exceptions.MultipleCurrenciesFound' exception. For example:

from money import Currency
from money.exceptions import CurrencyDoesNotExist

try:
    LTC = Currency.get('ltc')
except CurrencyDoesNotExist as e:
    print(e)
    
# output: Currency with code "ltc" does not exist

Keep in mind, that Money is a class which contains integer representation of amount and Currency. You can apply str function to get the string representation of Money with the defined suffix and prefix; float for getting the amount without currency; int for getting an amount in minimum units.

a = Money(95, USD)

str(a)
# output: $95
float(a)
# output: 95.0
int(a)
# output: 9500

If you need to define Money using minimum units, you can use int_to_money function.

Money.int_to_money(1000, USD)
# output: $10

All arithmetic operations are available. But keep in mind, that you can use amounts with the same currencies only. Otherwise will be raised TypeError. Division is available between Money and int or float (to divide some amount between 2 persons) and between Money and Money (how many money parts can be divided).

Money(10, UAH) + Money(10, BTC)
# output: TypeError

a = Money(100, USD)
b = Money(45, USD)

a + b
# output: $145
a - b
# output: $45
a * 2
# output: $190
a // 2
# output: $47
a % 2
# output: $1
a // b
# output: 1.0
a % b
# output: 45.0

And comparison operations:

a = Money(100, USD)
b = Money(45, USD)

a > b
# output: True
a < b
# output: False
a >= b
# output: True
a <= b
# output: False
a == b
# output: False
a != b
# output: True
a is b
# output: False

And one more example, which shows a real case of using this library.

from money import Money, Currency

USD = Currency('USD', 'usd', 2, prefix='$')

# Let's imaging that you're a multimillionaire
balance = Money(5000000, USD)

print("Balance: {}".format(balance))

moneylib
amount = Money(1000000, USD)
# And you should pay a fee of 2%.
fee = amount * 0.02  # 2%

while True:
    print("Withdraw {} with fee {}".format(amount, fee))
    if balance >= amount + fee:
        balance -= amount + fee
        print("Balance: {}".format(balance))
    else:
        print("Balance too small")
        print("Balance: {}".format(balance))
        break
        
# output:
# Balance: $5000000
# Withdraw $1000000 with fee $20000
# Balance: $3980000
# Withdraw $1000000 with fee $20000
# Balance: $2960000
# Withdraw $1000000 with fee $20000
# Balance: $1940000
# Withdraw $1000000 with fee $20000
# Balance: $920000
# Withdraw $1000000 with fee $20000
# Balance too small
# Balance: $920000

moneylib's People

Contributors

san4ezy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.