-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcladr_db.py
99 lines (78 loc) · 2.82 KB
/
cladr_db.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
# -*- coding: utf-8 -*-
# :noTabs=true:indentSize=4:
"""CLADR database API
"""
import logging
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
CHUNK_SIZE = 10000
Base = declarative_base()
class CladrRecord(Base):
__tablename__ = 'cladr'
def __init__(self, code, code_prefix, code_suffix, actuality, name, typename, postcode, okatd, status, obj_class):
self.code = code
self.code_prefix = code_prefix
self.code_suffix = code_suffix
self.actuality = actuality
self.name = name
self.type = typename
self.postcode = postcode
self.okatd = okatd
self.status = status
self.obj_class = obj_class
self.full_code = code
if len(self.okatd) == 11 and self.okatd[8:11] == '000':
self.okatd = self.okatd[0:8]
code = Column(BigInteger, primary_key=True)
status = Column(String(2))
code_prefix = Column(String(11))
code_suffix = Column(String(4))
actuality = Column(String(2))
postcode = Column(String(6))
okatd = Column(String(11))
obj_class = Column(String(1))
name = Column(String(256))
type = Column(String(256))
is_actual = Column(Boolean)
full_code = Column(String(17))
class CladrDB:
"""CLADR database API
"""
def __init__(self, engine):
self.engine = engine
self.Session = sessionmaker(bind=engine)
self.data = []
def insert(self, cladr):
self.data.append(cladr)
if len(self.data) >= CHUNK_SIZE:
self.flush()
def flush(self):
session = self.Session()
session.add_all(self.data)
session.commit()
self.data = []
def recreate(self):
"""DROP and CREATE database
"""
Base.metadata.drop_all(self.engine)
Base.metadata.create_all(self.engine)
Index('code_prefix_idx', CladrRecord.code_prefix, CladrRecord.actuality, CladrRecord.status).create(self.engine)
def load_data(self, prepare_key, cladr):
"""Load CLADR records
"""
session = self.Session()
by_key = {}
by_code = {}
for cladr_rec in session.query(CladrRecord) \
.filter(CladrRecord.code_prefix == str(cladr)[0:11]) \
.filter(CladrRecord.actuality == '00') \
.filter(CladrRecord.status == '99'):
keys = [prepare_key(" ".join([cladr_rec.name, cladr_rec.type]))]
keys.append(prepare_key(cladr_rec.name))
for key in keys:
by_key.setdefault(key, []).append(cladr_rec)
by_code.setdefault(cladr_rec.code, []).append(cladr_rec)
session.close()
return (by_key, by_code)