-
Notifications
You must be signed in to change notification settings - Fork 0
/
num2word.py
60 lines (48 loc) · 1.66 KB
/
num2word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'''
Module: num2word.py
Requires: num2word_*.py
Version: 0.2
Author:
Taro Ogawa ([email protected])
Copyright:
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
Licence:
This module is distributed under the Lesser General Public Licence.
http://www.opensource.org/licenses/lgpl-license.php
Usage:
from num2word import to_card, to_ord, to_ordnum
to_card(1234567890)
to_ord(1234567890)
to_ordnum(12)
Notes:
The module is a wrapper for language-specific modules. It imports the
appropriate modules as defined by locale settings. If unable to
load an appropriate module, an ImportError is raised.
History:
0.2: n2w, to_card, to_ord, to_ordnum now imported correctly
'''
import locale as _locale
# Correct omissions in locale:
# Bugrep these...
_locdict = { "English_Australia" : "en_AU", }
_modules = []
for _loc in [_locale.getlocale(), _locale.getdefaultlocale()]:
_lang = _loc[0]
if _lang:
_lang = _locdict.get(_lang, _lang)
_lang = _lang.upper()
_modules.append("num2word_" + _lang)
_modules.append("num2word_" + _lang.split("_")[0])
for _module in _modules:
try:
n2wmod = __import__(_module)
break
except ImportError:
pass
try:
n2w, to_card, to_ord, to_ordnum, to_year = (n2wmod.n2w, n2wmod.to_card,
n2wmod.to_ord, n2wmod.to_ordnum,
n2wmod.to_year)
except NameError:
raise ImportError("Could not import any of these modules: %s"
% (", ".join(_modules)))