From cd5977271d72cca570be7cdcff5cd412b578fec6 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Mon, 22 Apr 2013 18:39:48 -0300 Subject: [PATCH] Obtem lista de municipios e verifica CEP Obtem lista de municipios por UF e verifica se CEP se encontra no intervalo de CEPs para aquele municipio. --- municipios.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 municipios.py diff --git a/municipios.py b/municipios.py new file mode 100644 index 0000000..080a0e3 --- /dev/null +++ b/municipios.py @@ -0,0 +1,55 @@ +# +# Copyright 2013 Thadeu Lima de Souza Cascardo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -*- mode: python; encoding: utf-8; -*- +import xml.dom.minidom + +class Municipios: + def __init__(self, UF): + self.xml = xml.dom.minidom.parse("res/%s.xml" % (UF,)) + self.l = [] + self._list() + def _list(self): + for i in self.xml.childNodes[0].childNodes: + if "COL3" in i.attributes.keys(): + self.l.append((i.attributes["COL1"].nodeValue, \ + i.attributes["COL2"].nodeValue, \ + i.attributes["COL3"].nodeValue)) + def list(self): + return self.l + def get_municipio(self, code): + for i in self.l: + if i[0] == code: + return i + return None + def verify_cep(self, m, cep): + l = m[2][0:7] + h = m[2][9:16] + if cep >= l and cep <= h: + return True + return False + +if __name__ == '__main__': + municipios = Municipios('MG') + m = municipios.get_municipio('4877') + print m[1] + print municipios.verify_cep(m, '36880000') + print municipios.verify_cep(m, '05020000') + + municipios = Municipios('SP') + m = municipios.get_municipio('7107') + print m[1] + print municipios.verify_cep(m, '05020000') + print municipios.verify_cep(m, '36880000') -- 2.20.1