prices
raw JSON → 1.1.1 verified Fri May 01 auth: no python
Python price handling for humans. Provides a Price class with arithmetic operations, currency support, tax calculation, and discount application. Current version: 1.1.1. Release cadence: infrequent, last release 2021.
pip install prices Common errors
error TypeError: __init__() missing 1 required positional argument: 'currency' ↓
cause Creating a Price or Money without specifying the currency argument (it is positional, often incorrectly omitted).
fix
Provide currency: Price(currency='USD', net=Money(10, 'USD'), gross=Money(12, 'USD')).
error AttributeError: 'Price' object has no attribute 'amount' ↓
cause Confusion with other libraries; Price does not have an 'amount' attribute. Use Price.net or Price.gross.
fix
Use Price.net or Price.gross to get the Money object, then .amount on that Money: price.net.amount
error ImportError: cannot import name 'Discount' ↓
cause Incorrect import path for discount classes; they are in a submodule.
fix
Use 'from prices.discounts import FixedDiscount' instead of 'from prices import Discount'.
Warnings
gotcha Currency is required on Price and Money. If you omit currency, you'll get a TypeError. ↓
fix Always specify currency, e.g., Price(currency='USD', net=..., gross=...).
breaking In version 0.5, Price and PriceRange became named tuples. Accessing attributes like .currency still works, but they are immutable. ↓
fix Use the named tuple interface: price = Price(currency='USD', net=10, gross=12); price.net works, but price.net = 5 raises AttributeError.
Imports
- Price
from prices import Price - Money
from prices import Money - PriceRange
from prices import PriceRange - TaxedMoney
from prices import TaxedMoney - FixedDiscount
from prices.discounts import FixedDiscount - PercentageDiscount
from prices.discounts import PercentageDiscount
Quickstart
from prices import Price, Money, TaxedMoney, PriceRange
# Create a price
price = Price(currency='USD', net=Money(10, 'USD'), gross=Money(12, 'USD'))
print(price)
# Price arithmetic
p1 = Price(currency='USD', net=Money(5, 'USD'), gross=Money(6, 'USD'))
p2 = Price(currency='USD', net=Money(3, 'USD'), gross=Money(4, 'USD'))
print(p1 + p2)
# Create a price range
pr = PriceRange(start=p1, stop=p2)
print(pr)
# TaxedMoney
tm = TaxedMoney(net=Money(100, 'USD'), gross=Money(123, 'USD'))
print(tm)