-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_area_codes.py
69 lines (57 loc) · 1.55 KB
/
generate_area_codes.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
61
62
63
64
65
66
67
68
69
# $Id: generate_area_codes.py,v 1.4 2012-03-19 14:20:46 sshevtsov Exp $
# Scritp que lee por entrada estandar el archivo codigos de area
# y genera por salida estandar la estructura hash al estilo python
# para utilizarla en los scripts.
# Este script es de apoyo.
#
import csv
from sys import stdout
reader = csv.reader(open('/dev/stdin','rb'), delimiter='\t')
tree={}
def addValue(code,start,end,struct,value):
try:
struct[code[start]]['name']
except KeyError:
struct[code[start]]={'name' : None }
if start<end:
addValue(code,start+1,end,struct[code[start]],value)
elif start==end:
struct[code[start]]['name'] = value
def showData(struct,ident,coma):
try:
keys = list(struct.iterkeys())
except AttributeError:
if struct == None:
quote=""
utf8=""
else:
quote="'"
utf8="u"
stdout.write(utf8+quote+str(struct)+quote+coma)
if coma == ',' : print
return
stdout.write("\n"+ident+"{\n")
#stdout.write("\n"+ident+"{")
for key in keys:
#stdout.write("key="+key+" keys[-1:]="+str(keys[-1:])+" coma="+coma+"\n")
stdout.write(ident+" '"+key+"':")
if key == keys[-1:][0]:
nextcoma=""
else:
nextcoma=","
showData(struct[key],ident+" ",nextcoma)
stdout.write("\n"+ident+"}"+coma)
if coma == ',' : print
def show(struct):
stdout.write("areaCodesTree=")
showData(struct," ","")
print
for row in reader:
code=row[0]
area=row[1]
try:
prov=row[2]
except IndexError, e:
prot="no_prov"
addValue(code,0,len(code)-1,tree,area)
show(tree)