2017-06-28 23:55:53 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-10-15 02:30:55 -05:00
|
|
|
#~ import falcon
|
|
|
|
#~ from models.main import get_cp
|
2017-06-28 23:55:53 -05:00
|
|
|
|
|
|
|
|
2017-10-15 02:30:55 -05:00
|
|
|
#~ class AppPostalCode(object):
|
2017-06-28 23:55:53 -05:00
|
|
|
|
2017-10-15 02:30:55 -05:00
|
|
|
#~ def on_get(self, req, resp):
|
|
|
|
#~ values = req.params
|
|
|
|
#~ req.context['result'] = get_cp(values['cp'])
|
|
|
|
#~ resp.status = falcon.HTTP_200
|
|
|
|
|
|
|
|
#~ https://github.com/kennethreitz/requests/blob/v1.2.3/requests/structures.py#L37
|
|
|
|
|
|
|
|
import re
|
|
|
|
import collections
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
|
|
|
|
class CaseInsensitiveDict(collections.MutableMapping):
|
|
|
|
"""A case-insensitive ``dict``-like object.
|
|
|
|
Implements all methods and operations of
|
|
|
|
``collections.MutableMapping`` as well as dict's ``copy``. Also
|
|
|
|
provides ``lower_items``.
|
|
|
|
All keys are expected to be strings. The structure remembers the
|
|
|
|
case of the last key to be set, and ``iter(instance)``,
|
|
|
|
``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()``
|
|
|
|
will contain case-sensitive keys. However, querying and contains
|
|
|
|
testing is case insensitive::
|
|
|
|
cid = CaseInsensitiveDict()
|
|
|
|
cid['Accept'] = 'application/json'
|
|
|
|
cid['aCCEPT'] == 'application/json' # True
|
|
|
|
list(cid) == ['Accept'] # True
|
|
|
|
For example, ``headers['content-encoding']`` will return the
|
|
|
|
value of a ``'Content-Encoding'`` response header, regardless
|
|
|
|
of how the header name was originally stored.
|
|
|
|
If the constructor, ``.update``, or equality comparison
|
|
|
|
operations are given keys that have equal ``.lower()``s, the
|
|
|
|
behavior is undefined.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, data=None, **kwargs):
|
|
|
|
self._store = OrderedDict()
|
|
|
|
if data is None:
|
|
|
|
data = {}
|
|
|
|
self.update(data, **kwargs)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
# Use the lowercased key for lookups, but store the actual
|
|
|
|
# key alongside the value.
|
|
|
|
self._store[key.lower()] = (key, value)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._store[key.lower()][1]
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self._store[key.lower()]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return (casedkey for casedkey, mappedvalue in self._store.values())
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._store)
|
|
|
|
|
|
|
|
def lower_items(self):
|
|
|
|
"""Like iteritems(), but with all lowercase keys."""
|
|
|
|
return (
|
|
|
|
(lowerkey, keyval[1])
|
|
|
|
for (lowerkey, keyval)
|
|
|
|
in self._store.items()
|
|
|
|
)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, collections.Mapping):
|
|
|
|
other = CaseInsensitiveDict(other)
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
# Compare insensitively
|
|
|
|
return dict(self.lower_items()) == dict(other.lower_items())
|
|
|
|
|
|
|
|
# Copy is required
|
|
|
|
def copy(self):
|
|
|
|
return CaseInsensitiveDict(self._store.values())
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(dict(self.items()))
|
|
|
|
|
|
|
|
|
|
|
|
class NumLet(object):
|
|
|
|
|
|
|
|
def __init__(self, value, moneda, **args):
|
|
|
|
self._letras = self._letters(value, moneda)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def letras(self):
|
|
|
|
return self._letras.upper()
|
|
|
|
|
|
|
|
#~ def _letters(self, numero, moneda='peso', texto_inicial='-(',
|
|
|
|
#~ texto_final='/100 m.n.)-', fraccion_letras=False, fraccion=''):
|
|
|
|
def _letters(self, numero, moneda='peso'):
|
|
|
|
|
|
|
|
texto_inicial = ''
|
|
|
|
texto_final = '/100 m.n.)-'
|
|
|
|
fraccion_letras = False
|
|
|
|
fraccion = ''
|
|
|
|
|
|
|
|
enletras = texto_inicial
|
|
|
|
numero = abs(numero)
|
|
|
|
numtmp = '%015d' % numero
|
|
|
|
|
|
|
|
if numero < 1:
|
|
|
|
enletras += 'cero ' + self._plural(moneda) + ' '
|
|
|
|
else:
|
|
|
|
enletras += self._numlet(numero)
|
|
|
|
if numero == 1 or numero < 2:
|
|
|
|
enletras += moneda + ' '
|
|
|
|
elif int(''.join(numtmp[3:])) == 0 or int(''.join(numtmp[9:])) == 0:
|
|
|
|
enletras += 'de ' + self._plural(moneda) + ' '
|
|
|
|
else:
|
|
|
|
enletras += self._plural(moneda) + ' '
|
|
|
|
|
|
|
|
decimal = '%0.2f' % numero
|
|
|
|
decimal = decimal.split('.')[1]
|
|
|
|
#~ decimal = int((numero-int(numero))*100)
|
|
|
|
if fraccion_letras:
|
|
|
|
if decimal == 0:
|
|
|
|
enletras += 'con cero ' + self._plural(fraccion)
|
|
|
|
elif decimal == 1:
|
|
|
|
enletras += 'con un ' + fraccion
|
|
|
|
else:
|
|
|
|
enletras += 'con ' + self._numlet(int(decimal)) + self.plural(fraccion)
|
|
|
|
else:
|
|
|
|
enletras += decimal
|
|
|
|
|
|
|
|
enletras += texto_final
|
|
|
|
return enletras
|
|
|
|
|
|
|
|
def _numlet(self, numero):
|
|
|
|
numtmp = '%015d' % numero
|
|
|
|
co1=0
|
|
|
|
letras = ''
|
|
|
|
leyenda = ''
|
|
|
|
for co1 in range(0,5):
|
|
|
|
inicio = co1*3
|
|
|
|
cen = int(numtmp[inicio:inicio+1][0])
|
|
|
|
dec = int(numtmp[inicio+1:inicio+2][0])
|
|
|
|
uni = int(numtmp[inicio+2:inicio+3][0])
|
|
|
|
letra3 = self.centena(uni, dec, cen)
|
|
|
|
letra2 = self.decena(uni, dec)
|
|
|
|
letra1 = self.unidad(uni, dec)
|
|
|
|
|
|
|
|
if co1 == 0:
|
|
|
|
if (cen+dec+uni) == 1:
|
|
|
|
leyenda = 'billon '
|
|
|
|
elif (cen+dec+uni) > 1:
|
|
|
|
leyenda = 'billones '
|
|
|
|
elif co1 == 1:
|
|
|
|
if (cen+dec+uni) >= 1 and int(''.join(numtmp[6:9])) == 0:
|
|
|
|
leyenda = "mil millones "
|
|
|
|
elif (cen+dec+uni) >= 1:
|
|
|
|
leyenda = "mil "
|
|
|
|
elif co1 == 2:
|
|
|
|
if (cen+dec) == 0 and uni == 1:
|
|
|
|
leyenda = 'millon '
|
|
|
|
elif cen > 0 or dec > 0 or uni > 1:
|
|
|
|
leyenda = 'millones '
|
|
|
|
elif co1 == 3:
|
|
|
|
if (cen+dec+uni) >= 1:
|
|
|
|
leyenda = 'mil '
|
|
|
|
elif co1 == 4:
|
|
|
|
if (cen+dec+uni) >= 1:
|
|
|
|
leyenda = ''
|
|
|
|
|
|
|
|
letras += letra3 + letra2 + letra1 + leyenda
|
|
|
|
letra1 = ''
|
|
|
|
letra2 = ''
|
|
|
|
letra3 = ''
|
|
|
|
leyenda = ''
|
|
|
|
return letras
|
|
|
|
|
|
|
|
def centena(self, uni, dec, cen):
|
|
|
|
letras = ''
|
|
|
|
numeros = ["","","doscientos ","trescientos ","cuatrocientos ","quinientos ","seiscientos ","setecientos ","ochocientos ","novecientos "]
|
|
|
|
if cen == 1:
|
|
|
|
if (dec+uni) == 0:
|
|
|
|
letras = 'cien '
|
|
|
|
else:
|
|
|
|
letras = 'ciento '
|
|
|
|
elif cen >= 2 and cen <= 9:
|
|
|
|
letras = numeros[cen]
|
|
|
|
return letras
|
|
|
|
|
|
|
|
def decena(self, uni, dec):
|
|
|
|
letras = ''
|
|
|
|
numeros = ["diez ","once ","doce ","trece ","catorce ","quince ","dieci","dieci","dieci","dieci"]
|
|
|
|
decenas = ["","","","treinta ","cuarenta ","cincuenta ","sesenta ","setenta ","ochenta ","noventa "]
|
|
|
|
if dec == 1:
|
|
|
|
letras = numeros[uni]
|
|
|
|
elif dec == 2:
|
|
|
|
if uni == 0:
|
|
|
|
letras = 'veinte '
|
|
|
|
elif uni > 0:
|
|
|
|
letras = 'veinti'
|
|
|
|
elif dec >= 3 and dec <= 9:
|
|
|
|
letras = decenas[dec]
|
|
|
|
if uni > 0 and dec > 2:
|
|
|
|
letras = letras+'y '
|
|
|
|
return letras
|
|
|
|
|
|
|
|
def unidad(self, uni, dec):
|
|
|
|
letras = ''
|
|
|
|
numeros = ["","un ","dos ","tres ","cuatro ","cinco ","seis ","siete ","ocho ","nueve "]
|
|
|
|
if dec != 1:
|
|
|
|
if uni > 0 and uni <= 5:
|
|
|
|
letras = numeros[uni]
|
|
|
|
if uni >= 6 and uni <= 9:
|
|
|
|
letras = numeros[uni]
|
|
|
|
return letras
|
|
|
|
|
|
|
|
def _plural(self, palabra):
|
|
|
|
if re.search('[aeiou]$', palabra):
|
|
|
|
return re.sub('$', 's', palabra)
|
|
|
|
else:
|
|
|
|
return palabra + 'es'
|