From c3e61756ce231be8e5ed0504fdee71a2eac72797 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Tue, 7 Apr 2015 14:27:40 +0300 Subject: [PATCH] Add docs --- docs/data_types.rst | 4 +++ sqlalchemy_utils/primitives/currency.py | 47 ++++++++++++++++++++++++- sqlalchemy_utils/types/currency.py | 4 +-- tests/primitives/test_currency.py | 4 +-- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/docs/data_types.rst b/docs/data_types.rst index a8839ea..3b6546f 100644 --- a/docs/data_types.rst +++ b/docs/data_types.rst @@ -46,6 +46,10 @@ CurrencyType .. autoclass:: CurrencyType +.. module:: sqlalchemy_utils.primitives.currency + +.. autoclass:: Currency + EncryptedType ^^^^^^^^^^^^^ diff --git a/sqlalchemy_utils/primitives/currency.py b/sqlalchemy_utils/primitives/currency.py index 46e3848..5770971 100644 --- a/sqlalchemy_utils/primitives/currency.py +++ b/sqlalchemy_utils/primitives/currency.py @@ -8,6 +8,51 @@ from sqlalchemy_utils.utils import str_coercible @str_coercible class Currency(object): + """ + Currency class wraps a 3-letter currency code. It provides various + convenience properties and methods. + + :: + + from babel import Locale + from sqlalchemy_utils import Currency, i18n + + + # First lets add a locale getter for testing purposes + i18n.get_locale = lambda: Locale('en') + + + Currency('USD').name # US Dollar + Currency('USD').symbol # $ + + Currency(Currency('USD')).code # 'USD' + + Currency always validates the given code. + + :: + + Currency(None) # raises TypeError + + Currency('UnknownCode') # raises ValueError + + + Currency supports equality operators. + + :: + + Currency('USD') == Currency('USD') + Currency('USD') != Currency('EUR') + + + Currencies are hashable. + + + :: + + len(set([Currency('USD'), Currency('USD')])) # 1 + + + """ def __init__(self, code): if isinstance(code, Currency): self.code = code @@ -54,4 +99,4 @@ class Currency(object): return '%s(%r)' % (self.__class__.__name__, self.code) def __unicode__(self): - return self.name + return self.code diff --git a/sqlalchemy_utils/types/currency.py b/sqlalchemy_utils/types/currency.py index f2e9940..d7cf130 100644 --- a/sqlalchemy_utils/types/currency.py +++ b/sqlalchemy_utils/types/currency.py @@ -8,8 +8,8 @@ from .scalar_coercible import ScalarCoercible class CurrencyType(types.TypeDecorator, ScalarCoercible): """ - Changes Currency objects to a string representation on the way in and - changes them back to Currency objects on the way out. + Changes :class:`.Currency` objects to a string representation on the way in + and changes them back to :class:`.Currency` objects on the way out. In order to use CurrencyType you need to install Babel_ first. diff --git a/tests/primitives/test_currency.py b/tests/primitives/test_currency.py index c8e6d4d..a69504f 100644 --- a/tests/primitives/test_currency.py +++ b/tests/primitives/test_currency.py @@ -55,11 +55,11 @@ class TestCurrency(object): def test_unicode(self): currency = Currency('USD') - assert six.text_type(currency) == u'US Dollar' + assert six.text_type(currency) == u'USD' def test_str(self): currency = Currency('USD') - assert str(currency) == 'US Dollar' + assert str(currency) == 'USD' def test_representation(self): currency = Currency('USD')