Add docs
This commit is contained in:
@@ -46,6 +46,10 @@ CurrencyType
|
||||
|
||||
.. autoclass:: CurrencyType
|
||||
|
||||
.. module:: sqlalchemy_utils.primitives.currency
|
||||
|
||||
.. autoclass:: Currency
|
||||
|
||||
|
||||
EncryptedType
|
||||
^^^^^^^^^^^^^
|
||||
|
@@ -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
|
||||
|
@@ -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.
|
||||
|
||||
|
@@ -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')
|
||||
|
Reference in New Issue
Block a user