# Path: server/localfinance/models.py
# SRID = 4326
# ADMIN_LEVEL_CITY = 5
# ADMIN_LEVEL_CITY_ARR = 6
# class AdminZone(Base):
# class AdminZoneFinance(Base):
# class Stats(Base):
#     def code_insee(self):
# 
# Path: server/localfinance/cache.py



import re
import json
import os
import numpy as np
import brewer2mpl
from sqlalchemy import func, cast, Float, select, alias, and_
from sqlalchemy.sql import compiler
from psycopg2.extensions import adapt as sqlescape
from .models import DBSession, AdminZone, AdminZoneFinance, Stats, SRID, ADMIN_LEVEL_CITY
from .cache import region
    line-width: 2.0;
  }
"""

def compile_query(query):
    """Hack function to get sql query with params"""
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = {}
    for k,v in comp.params.iteritems():
        if isinstance(v, unicode):
            v = v.encode(enc)
        params[k] = sqlescape(v)
    # XXX: hack to remove ST_AsBinary put by geoalchemy, it breaks mapnik query
    # :(((
    str_query = (comp.string.encode(enc) % params).decode(enc)
    m = re.search('ST_AsBinary\(([\w\.]*)\)', str_query)
    if len(m.groups()) > 1:
        # the trick does not handle this case
        raise
    return str_query.replace(m.group(), m.groups()[0])

def quantile_scale(sql_variable, sql_filter, size):
    values = map(float, zip(*DBSession.query(sql_variable).filter(sql_filter).all())[0])
    return np.percentile(values, list(np.linspace(0, 100, size+1)))

def get_extent():

# Path: server/localfinance/models.py
# SRID = 4326
# ADMIN_LEVEL_CITY = 5
# ADMIN_LEVEL_CITY_ARR = 6
# class AdminZone(Base):
# class AdminZoneFinance(Base):
# class Stats(Base):
#     def code_insee(self):
# 
# Path: server/localfinance/cache.py



# Path: server/localfinance/maps.py
import re
import json
import os
import numpy as np
import brewer2mpl
from sqlalchemy import func, cast, Float, select, alias, and_
from sqlalchemy.sql import compiler
from psycopg2.extensions import adapt as sqlescape
from .models import DBSession, AdminZone, AdminZoneFinance, Stats, SRID, ADMIN_LEVEL_CITY
from .cache import region

    line-width: 2.0;
  }
"""

def compile_query(query):
    """Hack function to get sql query with params"""
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = {}
    for k,v in comp.params.iteritems():
        if isinstance(v, unicode):
            v = v.encode(enc)
        params[k] = sqlescape(v)
    # XXX: hack to remove ST_AsBinary put by geoalchemy, it breaks mapnik query
    # :(((
    str_query = (comp.string.encode(enc) % params).decode(enc)
    m = re.search('ST_AsBinary\(([\w\.]*)\)', str_query)
    if len(m.groups()) > 1:
        # the trick does not handle this case
        raise
    return str_query.replace(m.group(), m.groups()[0])

def quantile_scale(sql_variable, sql_filter, size):
    values = map(float, zip(*DBSession.query(sql_variable).filter(sql_filter).all())[0])
    return np.percentile(values, list(np.linspace(0, 100, size+1)))

def get_extent():

    return DBSession.query(