# Path: canon.py
# class Memoize:
#     def __init__(self, f):
#     def __call__(self, *args):
# def getpair(text):
# def chd_id_nomemo(text):
# def country_id_nomemo(rawname):
# def canon_number(f):
# def canon_period(p):
# def updatedb():
# def _ignore():


from sqlalchemy import Column, String, Boolean
from sqlalchemy import ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import Engine
from sqlalchemy import event
from canon import canonicalise, canon_number, canon_period, chd_id
import datetime
import atexit
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()


class Value(Base):
    __tablename__ = "value"
    dsID = Column(String, ForeignKey('dataset.dsID'), primary_key=True)
    region = Column(String, primary_key=True)
    indID = Column(String, ForeignKey('indicator.indID'), primary_key=True)
    period = Column(String, primary_key=True)
    value = Column(String, nullable=False)
    is_number = Column(Boolean)
    source = Column(String)

    def is_blank(self):
        if type(self.value) in [float, int]:
            return False
        return self.value is None or self.value.strip() == ''

    def save(self):
        self.region = canonicalise(self.region)
        self.period = canon_period(self.period)
        self.indID = chd_id(self.indID)
        if self.region is None:
            return
        if self.period is None or self.period == '':
            self.period = now()[:10]  # YYYY-MM-DD
        self.is_number=bool(self.is_number)
        if self.is_number:

# Path: canon.py
# class Memoize:
#     def __init__(self, f):
#     def __call__(self, *args):
# def getpair(text):
# def chd_id_nomemo(text):
# def country_id_nomemo(rawname):
# def canon_number(f):
# def canon_period(p):
# def updatedb():
# def _ignore():


# Path: orm.py
from sqlalchemy import Column, String, Boolean
from sqlalchemy import ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import Engine
from sqlalchemy import event
from canon import canonicalise, canon_number, canon_period, chd_id
import datetime
import atexit

Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()


class Value(Base):
    __tablename__ = "value"
    dsID = Column(String, ForeignKey('dataset.dsID'), primary_key=True)
    region = Column(String, primary_key=True)
    indID = Column(String, ForeignKey('indicator.indID'), primary_key=True)
    period = Column(String, primary_key=True)
    value = Column(String, nullable=False)
    is_number = Column(Boolean)
    source = Column(String)

    def is_blank(self):
        if type(self.value) in [float, int]:
            return False
        return self.value is None or self.value.strip() == ''

    def save(self):
        self.region = canonicalise(self.region)
        self.period = canon_period(self.period)
        self.indID = chd_id(self.indID)
        if self.region is None:
            return
        if self.period is None or self.period == '':
            self.period = now()[:10]  # YYYY-MM-DD
        self.is_number=bool(self.is_number)
        if self.is_number:

            self.value = canon_number(self.value)