3 # Python version of the CEP browser program
5 # Note: dict type in python is implemented using a hash
8 # Luiz Fernando N. Capitulino
9 # <lcapitulino@gmail.com>
15 print '%s;%s;%s;%s;%s' % \
16 (self
.__cep
, self
.__uf
, self
.__cidade
, \
17 self
.__bairro
, self
.__rua
)
23 return self
.__cep
== y
;
27 str += 'CEP: ' + self
.cep
+ '\n'
28 str += 'UF: ' + self
.uf
+ '\n'
29 str += 'Cidade: ' + self
.cidade
+ '\n'
30 str += 'Bairro: ' + self
.bairro
+ '\n'
31 str += 'Rua: ' + self
.rua
+ '\n'
34 def __init__(self
, line
):
35 items
= line
.split(";")
36 self
.__rua
= items
.pop()
37 self
.__bairro
= items
.pop()
38 self
.__cidade
= items
.pop()
39 self
.__uf
= items
.pop()
40 self
.__cep
= items
.pop()
42 class AddressList(list):
43 def lookup(self
, cep
):
49 def __init__(self
, contents
):
52 addr
= Address(line
.strip('\n\r'))
55 class AddressDict(dict):
58 def __init__(self
, contents
):
61 addr
= Address(line
.strip('\n\r'))
62 self
.setdefault(addr
.get_cep(), addr
)
66 contents
= db
.readlines()
70 def do_test(addresses
, contents
):
73 cep
= line
[:line
.find(';')]
74 ret
= addresses
.lookup(cep
)
86 sys
.stdout
.write('>>> ')
87 line
= sys
.stdin
.readline()
96 line
= line
.strip('\n')
97 entry
= addresses
.lookup(line
)
101 print line
+ ' : not found'
103 print 'usage: cep-browser.py [ -l | -d ] [ -t ] < filename >'
108 if len(sys
.argv
) < 3 or len(sys
.argv
) > 4:
112 if opt
!= "-l" and opt
!= "-d":
118 if sys
.argv
[2] == "-t":
122 contents
= read_db(file)
125 addresses
= AddressList(contents
)
127 addresses
= AddressDict(contents
)
130 do_test(addresses
, contents
)
135 if __name__
== "__main__":