mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
Big unit reorganisation
* Renamed pynum2word to num2word * Renamed num2word_* to lang_* * Re-organised all unit headers * Made imports relative
This commit is contained in:
46
num2words/__init__.py
Normal file
46
num2words/__init__.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from . import lang_EN
|
||||
from . import lang_EN_GB
|
||||
from . import lang_FR
|
||||
from . import lang_DE
|
||||
from . import lang_ES
|
||||
from . import lang_LT
|
||||
|
||||
CONVERTER_CLASSES = {
|
||||
'en': lang_EN.Num2Word_EN(),
|
||||
'en_GB': lang_EN_GB.Num2Word_EN_GB(),
|
||||
'fr': lang_FR.Num2Word_FR(),
|
||||
'de': lang_DE.Num2Word_DE(),
|
||||
'es': lang_ES.Num2Word_ES(),
|
||||
'lt': lang_LT.Num2Word_LT(),
|
||||
}
|
||||
|
||||
def num2words(number, ordinal=False, lang='en'):
|
||||
# We try the full language first
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
# ... and then try only the first 2 letters
|
||||
lang = lang[:2]
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
raise NotImplementedError()
|
||||
converter = CONVERTER_CLASSES[lang]
|
||||
if ordinal:
|
||||
return converter.to_ordinal(number)
|
||||
else:
|
||||
return converter.to_cardinal(number)
|
||||
@@ -1,25 +1,21 @@
|
||||
'''
|
||||
Module: num2word_base.py
|
||||
Version: 1.0
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
History:
|
||||
1.1: add to_splitnum() and inflect()
|
||||
add to_year() and to_currency() stubs
|
||||
'''
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from orderedmapping import OrderedMapping
|
||||
from .orderedmapping import OrderedMapping
|
||||
|
||||
|
||||
class Num2Word_Base(object):
|
||||
@@ -1,36 +1,21 @@
|
||||
'''
|
||||
Module: num2word_DE.py
|
||||
Requires: num2word_base.py
|
||||
Version: 0.4
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
- http://german4u2know.tripod.com/nouns/10.html
|
||||
- http://www.uni-bonn.de/~manfear/large.php
|
||||
|
||||
Usage:
|
||||
from num2word_DE import to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(12)
|
||||
|
||||
History
|
||||
0.4: Use high ascii characters instead of low ascii approximations
|
||||
add to_currency() and to_year()
|
||||
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EU import Num2Word_EU
|
||||
from .lang_EU import Num2Word_EU
|
||||
|
||||
#//TODO: Use German error messages
|
||||
class Num2Word_DE(Num2Word_EU):
|
||||
@@ -1,46 +1,23 @@
|
||||
'''
|
||||
Module: num2word_EN.py
|
||||
Requires: num2word_EU.py
|
||||
Version: 1.2
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.uni-bonn.de/~manfear/large.php
|
||||
|
||||
Usage:
|
||||
from num2word_EN import n2w, to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
n2w.is_title = True
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(1234567890)
|
||||
to_year(1976)
|
||||
to_currency(dollars*100 + cents, longval=False)
|
||||
to_currency((dollars, cents))
|
||||
|
||||
|
||||
History:
|
||||
1.2: to_ordinal_num() made shorter and simpler (but slower)
|
||||
strings in merge() now interpolated
|
||||
to_year() and to_currency() added
|
||||
|
||||
1.1: to_ordinal_num() fixed for 11,12,13
|
||||
'''
|
||||
from __future__ import division, unicode_literals
|
||||
import num2word_EU
|
||||
from . import lang_EU
|
||||
|
||||
|
||||
class Num2Word_EN(num2word_EU.Num2Word_EU):
|
||||
class Num2Word_EN(lang_EU.Num2Word_EU):
|
||||
def set_high_numwords(self, high):
|
||||
max = 3 + 3*len(high)
|
||||
for word, n in zip(high, range(max, 3, -3)):
|
||||
@@ -1,38 +1,21 @@
|
||||
'''
|
||||
Module: num2word_EN_EUR.py
|
||||
Requires: num2word_EN.py
|
||||
Version: 1.0
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.uni-bonn.de/~manfear/large.php
|
||||
|
||||
Usage:
|
||||
from num2word_EN import n2w, to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
n2w.is_title = True
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(1234567890)
|
||||
to_year(1976)
|
||||
to_currency(euros*100 + cents)
|
||||
to_currency((euros,cents))
|
||||
|
||||
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EN import Num2Word_EN
|
||||
|
||||
from .lang_EN import Num2Word_EN
|
||||
|
||||
class Num2Word_EN_EUR(Num2Word_EN):
|
||||
def to_currency(self, val, longval=True, cents=True, jointxt="and"):
|
||||
@@ -1,39 +1,21 @@
|
||||
'''
|
||||
Module: num2word_EN_GB.py
|
||||
Requires: num2word_EN.py
|
||||
Version: 1.0
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.uni-bonn.de/~manfear/large.php
|
||||
|
||||
Usage:
|
||||
from num2word_EN import n2w, to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
n2w.is_title = True
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(1234567890)
|
||||
to_year(1976)
|
||||
to_currency(pounds*100 + pence)
|
||||
to_currency((pounds,pence))
|
||||
|
||||
|
||||
History:
|
||||
1.0: Split from num2word_EN with the addition of to_currency()
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EN import Num2Word_EN
|
||||
from .lang_EN import Num2Word_EN
|
||||
|
||||
|
||||
class Num2Word_EN_GB(Num2Word_EN):
|
||||
@@ -1,33 +1,21 @@
|
||||
'''
|
||||
Module: num2word_EN_GB_old.py
|
||||
Requires: num2word_EN_GB_old.py
|
||||
Version: 0.3
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. 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_EN_old import to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(12)
|
||||
|
||||
History:
|
||||
0.3: Rename from num2word_EN_old
|
||||
|
||||
Todo:
|
||||
Currency (pounds/shillings/pence)
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EN_GB import Num2Word_EN_GB
|
||||
from .lang_EN_GB import Num2Word_EN_GB
|
||||
|
||||
class Num2Word_EN_GB_old(Num2Word_EN_GB):
|
||||
def base_setup(self):
|
||||
@@ -1,35 +1,21 @@
|
||||
'''
|
||||
Module: num2word_ES.py
|
||||
Requires: num2word_EU.py
|
||||
Version: 0.3
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.smartphrase.com/Spanish/sp_numbers_voc.shtml
|
||||
|
||||
Usage:
|
||||
from num2word_ES import to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(12)
|
||||
|
||||
History:
|
||||
0.3: Use high ascii characters instead of low ascii approximations
|
||||
String interpolation where it makes things clearer
|
||||
add to_currency()
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EU import Num2Word_EU
|
||||
from .lang_EU import Num2Word_EU
|
||||
|
||||
#//TODO: correct orthographics
|
||||
#//TODO: error messages
|
||||
@@ -1,27 +1,21 @@
|
||||
'''
|
||||
Module: num2word_EU.py
|
||||
Requires: num2word_base.py
|
||||
Version: 1.1
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.uni-bonn.de/~manfear/large.php
|
||||
|
||||
History:
|
||||
1.1: add to_currency()
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_base import Num2Word_Base
|
||||
from .base import Num2Word_Base
|
||||
|
||||
class Num2Word_EU(Num2Word_Base):
|
||||
def set_high_numwords(self, high):
|
||||
@@ -1,38 +1,22 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
'''
|
||||
Module: num2word_FR.py
|
||||
Requires: num2word_EU.py
|
||||
Version: 0.5
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
|
||||
Data from:
|
||||
http://www.ouc.bc.ca/mola/fr/handouts/numbers.doc
|
||||
http://www.realfrench.net/units/Interunit_63.html
|
||||
http://www.sover.net/~daxtell/france/Euro/euro.htm
|
||||
|
||||
Usage:
|
||||
from num2word_FR import to_card, to_ord, to_ordnum
|
||||
to_card(1234567890)
|
||||
to_ord(1234567890)
|
||||
to_ordnum(12)
|
||||
|
||||
History:
|
||||
0.5: Use high ascii characters instead of low ascii approximations
|
||||
String interpolation where it makes things clearer
|
||||
to_currency() added [to_year works by default]
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
from num2word_EU import Num2Word_EU
|
||||
from .lang_EU import Num2Word_EU
|
||||
|
||||
#//TODO: error messages in French
|
||||
#//TODO: ords
|
||||
@@ -1,4 +1,19 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
u"""
|
||||
>>> from textwrap import fill
|
||||
|
||||
35
num2words/orderedmapping.py
Normal file
35
num2words/orderedmapping.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301 USA
|
||||
|
||||
class OrderedMapping(dict):
|
||||
def __init__(self, *pairs):
|
||||
self.order = []
|
||||
for key, val in pairs:
|
||||
self[key] = val
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
if key not in self:
|
||||
self.order.append(key)
|
||||
super(OrderedMapping, self).__setitem__(key, val)
|
||||
|
||||
def __iter__(self):
|
||||
for item in self.order:
|
||||
yield item
|
||||
|
||||
def __repr__(self):
|
||||
out = ["%s: %s"%(repr(item), repr(self[item])) for item in self]
|
||||
out = ", ".join(out)
|
||||
return "{%s}"%out
|
||||
@@ -1,60 +0,0 @@
|
||||
'''
|
||||
Module: num2word.py
|
||||
Requires: num2word_*.py
|
||||
Version: 0.2
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. 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
|
||||
'''
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import num2word_EN
|
||||
import num2word_EN_GB
|
||||
import num2word_FR
|
||||
import num2word_DE
|
||||
import num2word_ES
|
||||
import num2word_LT
|
||||
|
||||
CONVERTER_CLASSES = {
|
||||
'en': num2word_EN.Num2Word_EN(),
|
||||
'en_GB': num2word_EN_GB.Num2Word_EN_GB(),
|
||||
'fr': num2word_FR.Num2Word_FR(),
|
||||
'de': num2word_DE.Num2Word_DE(),
|
||||
'es': num2word_ES.Num2Word_ES(),
|
||||
'lt': num2word_LT.Num2Word_LT(),
|
||||
}
|
||||
|
||||
def num2words(number, ordinal=False, lang='en'):
|
||||
# We try the full language first
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
# ... and then try only the first 2 letters
|
||||
lang = lang[:2]
|
||||
if lang not in CONVERTER_CLASSES:
|
||||
raise NotImplementedError()
|
||||
converter = CONVERTER_CLASSES[lang]
|
||||
if ordinal:
|
||||
return converter.to_ordinal(number)
|
||||
else:
|
||||
return converter.to_cardinal(number)
|
||||
@@ -1,35 +0,0 @@
|
||||
'''
|
||||
Module: orderedmapping.py
|
||||
Version: 1.0
|
||||
|
||||
Author:
|
||||
Taro Ogawa (tso@users.sourceforge.org)
|
||||
|
||||
Copyright:
|
||||
Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
|
||||
Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
|
||||
|
||||
Licence:
|
||||
This module is distributed under the Lesser General Public Licence.
|
||||
http://www.opensource.org/licenses/lgpl-license.php
|
||||
'''
|
||||
from __future__ import generators
|
||||
class OrderedMapping(dict):
|
||||
def __init__(self, *pairs):
|
||||
self.order = []
|
||||
for key, val in pairs:
|
||||
self[key] = val
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
if key not in self:
|
||||
self.order.append(key)
|
||||
super(OrderedMapping, self).__setitem__(key, val)
|
||||
|
||||
def __iter__(self):
|
||||
for item in self.order:
|
||||
yield item
|
||||
|
||||
def __repr__(self):
|
||||
out = ["%s: %s"%(repr(item), repr(self[item])) for item in self]
|
||||
out = ", ".join(out)
|
||||
return "{%s}"%out
|
||||
Reference in New Issue
Block a user