import re
import sys

def _dump_str(v):
    if sys.version_info < (3,) and hasattr(v, 'decode') and isinstance(v, str):
        v = v.decode('utf-8')
    v = "%r" % v
    if v[0] == 'u':
        v = v[1:]
    singlequote = v.startswith("'")
    if singlequote or v.startswith('"'):
        v = v[1:-1]
    if singlequote:
        v = v.replace("\\'", "'")
        v = v.replace('"', '\\"')
    v = v.split("\\x")
    while len(v) > 1:
        i = -1
        if not v[0]:
            v = v[1:]
        v[0] = v[0].replace("\\\\", "\\")
        # No, I don't know why != works and == breaks
        joinx = v[0][i] != "\\"
        while v[0][:i] and v[0][i] == "\\":
            joinx = not joinx
            i -= 1
        if joinx:
            joiner = "x"
        else:
            joiner = "u00"
        v = [v[0] + joiner + v[1]] + v[2:]
    return str('"' + v[0] + '"')

def _dump_float(v):
    return "{}".format(v).replace("e+0", "e+").replace("e-0", "e-")

class TomlEncoder:

    def __init__(self, _dict=dict, preserve=False):
        self._dict = _dict
        self.preserve = preserve
        self.dump_funcs = {
            str: _dump_str,
            list: self.dump_list,
            int: lambda v: v,
            float: _dump_float
        }

    def dump_list(self, v):
        retval = "["
        for u in v:
            retval += " " + str(self.dump_value(u)) + ","
        retval += "]"
        return retval

    def dump(self,obj,f):
        d = self.dumps(obj)
        f.write(d)
        return d

    def dumps(self,o, encoder=None):
        """Stringifies input dict as toml
        Args:
            o: Object to dump into toml
            encoder: The ``TomlEncoder`` to use for constructing the output string
        Returns:
            String containing the toml corresponding to dict
        """

        retval = ""
        if encoder is None:
            encoder = TomlEncoder(o.__class__)
        addtoretval, sections = self.dump_sections(o, "")
        retval += addtoretval
        outer_objs = [id(o)]
        while sections:
            section_ids = [id(section) for section in sections.values()]
            for outer_obj in outer_objs:
                if outer_obj in section_ids:
                    raise ValueError("Circular reference detected")
            outer_objs += section_ids
            newsections = encoder.get_empty_table()
            for section in sections:
                addtoretval, addtosections = encoder.dump_sections(
                    sections[section], section)

                if addtoretval or (not addtoretval and not addtosections):
                    if retval and retval[-2:] != "\n\n":
                        retval += "\n"
                    retval += "[" + section + "]\n"
                    if addtoretval:
                        retval += addtoretval
                for s in addtosections:
                    newsections[section + "." + s] = addtosections[s]
            sections = newsections
        return retval

    def get_empty_table(self):
        return self._dict()

    def _dump_str(v):
        if sys.version_info < (3,) and hasattr(v, 'decode') and isinstance(v, str):
            v = v.decode('utf-8')
        v = "%r" % v
        if v[0] == 'u':
            v = v[1:]
        singlequote = v.startswith("'")
        if singlequote or v.startswith('"'):
            v = v[1:-1]
        if singlequote:
            v = v.replace("\\'", "'")
            v = v.replace('"', '\\"')
        v = v.split("\\x")
        while len(v) > 1:
            i = -1
            if not v[0]:
                v = v[1:]
            v[0] = v[0].replace("\\\\", "\\")
            # No, I don't know why != works and == breaks
            joinx = v[0][i] != "\\"
            while v[0][:i] and v[0][i] == "\\":
                joinx = not joinx
                i -= 1
            if joinx:
                joiner = "x"
            else:
                joiner = "u00"
            v = [v[0] + joiner + v[1]] + v[2:]
        return str('"' + v[0] + '"')

    def dump_value(self, v):
        # Lookup function corresponding to v's type
        dump_fn = self.dump_funcs.get(type(v))
        if dump_fn is None and hasattr(v, '__iter__'):
            dump_fn = self.dump_funcs[list]
        # Evaluate function (if it exists) else return v
        return dump_fn(v) if dump_fn is not None else self.dump_funcs[str](v)

    def dump_sections(self, o, sup):
        retstr = ""
        if sup != "" and sup[-1] != ".":
            sup += '.'
        retdict = self._dict()
        arraystr = ""
        for section in o:
            section = str(section)
            qsection = section
            if not re.match(r'^[A-Za-z0-9_-]+$', section):
                qsection = _dump_str(section)
            if not isinstance(o[section], dict):
                arrayoftables = False
                if isinstance(o[section], list):
                    for a in o[section]:
                        if isinstance(a, dict):
                            arrayoftables = True
                if arrayoftables:
                    for a in o[section]:
                        arraytabstr = "\n"
                        arraystr += "[[" + sup + qsection + "]]\n"
                        s, d = self.dump_sections(a, sup + qsection)
                        if s:
                            if s[0] == "[":
                                arraytabstr += s
                            else:
                                arraystr += s
                        while d:
                            newd = self._dict()
                            for dsec in d:
                                s1, d1 = self.dump_sections(d[dsec], sup +
                                                            qsection + "." +
                                                            dsec)
                                if s1:
                                    arraytabstr += ("[" + sup + qsection +
                                                    "." + dsec + "]\n")
                                    arraytabstr += s1
                                for s1 in d1:
                                    newd[dsec + "." + s1] = d1[s1]
                            d = newd
                        arraystr += arraytabstr
                else:
                    if o[section] is not None:
                        retstr += (qsection + " = " +
                                   str(self.dump_value(o[section])) + '\n')
            elif self.preserve and isinstance(o[section], InlineTableDict):
                retstr += (qsection + " = " +
                           self.dump_inline_table(o[section]))
            else:
                retdict[qsection] = o[section]
        retstr += arraystr
        return (retstr, retdict)

class TomlDecoder:
    def get_empty_table(self):
        return dict()

    def _get_split_on_quotes(self, line):
        doublequotesplits = line.split('"')
        quoted = False
        quotesplits = []
        if len(doublequotesplits) > 1 and "'" in doublequotesplits[0]:
            singlequotesplits = doublequotesplits[0].split("'")
            doublequotesplits = doublequotesplits[1:]
            while len(singlequotesplits) % 2 == 0 and len(doublequotesplits):
                singlequotesplits[-1] += '"' + doublequotesplits[0]
                doublequotesplits = doublequotesplits[1:]
                if "'" in singlequotesplits[-1]:
                    singlequotesplits = (singlequotesplits[:-1] +
                                         singlequotesplits[-1].split("'"))
            quotesplits += singlequotesplits
        for doublequotesplit in doublequotesplits:
            if quoted:
                quotesplits.append(doublequotesplit)
            else:
                quotesplits += doublequotesplit.split("'")
                quoted = not quoted
        return quotesplits

    def bounded_string(self, s):
        if len(s) == 0:
            return True
        if s[-1] != s[0]:
            return False
        i = -2
        backslash = False
        while len(s) + i > 0:
            if s[i] == "\\":
                backslash = not backslash
                i -= 1
            else:
                break
        return not backslash

    def _load_array_isstrarray(self, a):
        a = a[1:-1].strip()
        if a != '' and (a[0] == '"' or a[0] == "'"):
            return True
        return False

    def load_array(self, a):
        atype = None
        retval = []
        a = a.strip()
        if '[' not in a[1:-1] or "" != a[1:-1].split('[')[0].strip():
            strarray = self._load_array_isstrarray(a)
            if not a[1:-1].strip().startswith('{'):
                a = a[1:-1].split(',')
            else:
                # a is an inline object, we must find the matching parenthesis
                # to define groups
                new_a = []
                start_group_index = 1
                end_group_index = 2
                open_bracket_count = 1 if a[start_group_index] == '{' else 0
                in_str = False
                while end_group_index < len(a[1:]):
                    if a[end_group_index] == '"' or a[end_group_index] == "'":
                        if in_str:
                            backslash_index = end_group_index - 1
                            while (backslash_index > -1 and
                                   a[backslash_index] == '\\'):
                                in_str = not in_str
                                backslash_index -= 1
                        in_str = not in_str
                    if not in_str and a[end_group_index] == '{':
                        open_bracket_count += 1
                    if in_str or a[end_group_index] != '}':
                        end_group_index += 1
                        continue
                    elif a[end_group_index] == '}' and open_bracket_count > 1:
                        open_bracket_count -= 1
                        end_group_index += 1
                        continue

                    # Increase end_group_index by 1 to get the closing bracket
                    end_group_index += 1

                    new_a.append(a[start_group_index:end_group_index])

                    # The next start index is at least after the closing
                    # bracket, a closing bracket can be followed by a comma
                    # since we are in an array.
                    start_group_index = end_group_index + 1
                    while (start_group_index < len(a[1:]) and
                           a[start_group_index] != '{'):
                        start_group_index += 1
                    end_group_index = start_group_index + 1
                a = new_a
            b = 0
            if strarray:
                while b < len(a) - 1:
                    ab = a[b].strip()
                    while (not self.bounded_string(ab) or
                           (len(ab) > 2 and
                            ab[0] == ab[1] == ab[2] and
                            ab[-2] != ab[0] and
                            ab[-3] != ab[0])):
                        a[b] = a[b] + ',' + a[b + 1]
                        ab = a[b].strip()
                        if b < len(a) - 2:
                            a = a[:b + 1] + a[b + 2:]
                        else:
                            a = a[:b + 1]
                    b += 1
        else:
            al = list(a[1:-1])
            a = []
            openarr = 0
            j = 0
            for i in range(len(al)):
                if al[i] == '[':
                    openarr += 1
                elif al[i] == ']':
                    openarr -= 1
                elif al[i] == ',' and not openarr:
                    a.append(''.join(al[j:i]))
                    j = i + 1
            a.append(''.join(al[j:]))
        for i in range(len(a)):
            a[i] = a[i].strip()
            if a[i] != '':
                nval, ntype = self.load_value(a[i])
                if atype:
                    if ntype != atype:
                        raise ValueError("Not a homogeneous array")
                else:
                    atype = ntype
                retval.append(nval)
        return retval

    def load_line(self, line, currentlevel, multikey, multibackslash):
        i = 1
        quotesplits = self._get_split_on_quotes(line)
        quoted = False
        for quotesplit in quotesplits:
            if not quoted and '=' in quotesplit:
                break
            i += quotesplit.count('=')
            quoted = not quoted
        pair = line.split('=', i)
        strictly_valid = _strictly_valid_num(pair[-1])
        if re.compile('([0-9])(_([0-9]))*').match(pair[-1]):
            pair[-1] = pair[-1].replace('_', '')
        while len(pair[-1]) and (pair[-1][0] != ' ' and pair[-1][0] != '\t' and
                                 pair[-1][0] != "'" and pair[-1][0] != '"' and
                                 pair[-1][0] != '[' and pair[-1][0] != '{' and
                                 pair[-1].strip() != 'true' and
                                 pair[-1].strip() != 'false'):
            try:
                float(pair[-1])
                break
            except ValueError:
                pass
            if re.compile(r"([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]{3,6}))?").match(pair[-1]):
                break
            i += 1
            prev_val = pair[-1]
            pair = line.split('=', i)
            if prev_val == pair[-1]:
                raise ValueError("Invalid date or number")
            if strictly_valid:
                strictly_valid = _strictly_valid_num(pair[-1])
        pair = ['='.join(pair[:-1]).strip(), pair[-1].strip()]
        if '.' in pair[0]:
            if '"' in pair[0] or "'" in pair[0]:
                quotesplits = self._get_split_on_quotes(pair[0])
                quoted = False
                levels = []
                for quotesplit in quotesplits:
                    if quoted:
                        levels.append(quotesplit)
                    else:
                        levels += [level.strip() for level in
                                   quotesplit.split('.')]
                    quoted = not quoted
            else:
                levels = pair[0].split('.')
            while levels[-1] == "":
                levels = levels[:-1]
            for level in levels[:-1]:
                if level == "":
                    continue
                if level not in currentlevel:
                    currentlevel[level] = self.get_empty_table()
                currentlevel = currentlevel[level]
            pair[0] = levels[-1].strip()
        elif (pair[0][0] == '"' or pair[0][0] == "'") and \
                (pair[0][-1] == pair[0][0]):
            pair[0] = _unescape(pair[0][1:-1])
        k, koffset = self._load_line_multiline_str(pair[1])
        if k > -1:
            while k > -1 and pair[1][k + koffset] == '\\':
                multibackslash = not multibackslash
                k -= 1
            if multibackslash:
                multilinestr = pair[1][:-1]
            else:
                multilinestr = pair[1] + "\n"
            multikey = pair[0]
        else:
            value, vtype = self.load_value(pair[1], strictly_valid)
        try:
            currentlevel[pair[0]]
            raise ValueError("Duplicate keys!")
        except TypeError:
            raise ValueError("Duplicate keys!")
        except KeyError:
            if multikey:
                return multikey, multilinestr, multibackslash
            else:
                currentlevel[pair[0]] = value

    def _load_line_multiline_str(self, p):
        poffset = 0
        if len(p) < 3:
            return -1, poffset
        if p[0] == '[' and (p.strip()[-1] != ']' and
                            self._load_array_isstrarray(p)):
            newp = p[1:].strip().split(',')
            while len(newp) > 1 and newp[-1][0] != '"' and newp[-1][0] != "'":
                newp = newp[:-2] + [newp[-2] + ',' + newp[-1]]
            newp = newp[-1]
            poffset = len(p) - len(newp)
            p = newp
        if p[0] != '"' and p[0] != "'":
            return -1, poffset
        if p[1] != p[0] or p[2] != p[0]:
            return -1, poffset
        if len(p) > 5 and p[-1] == p[0] and p[-2] == p[0] and p[-3] == p[0]:
            return -1, poffset
        return len(p) - 1, poffset

    def load(self,f):
        data = f.read()
        return self.loads(data)

    def loads(self,s, _dict=dict):
        """Parses string as toml
        Args:
            s: String to be parsed
            _dict: (optional) Specifies the class of the returned toml dictionary
        Returns:
            Parsed toml file represented as a dictionary
        Raises:
            TypeError: When a non-string is passed
            TomlDecodeError: Error while decoding toml
        """

        implicitgroups = []
        retval = self.get_empty_table()
        currentlevel = retval
        if not isinstance(s, str):
            raise TypeError("Expecting a string")

        if not isinstance(s, str):
            s = s.decode('utf8')

        original = s
        sl = list(s)
        openarr = 0
        openstring = False
        openstrchar = ""
        multilinestr = False
        arrayoftables = False
        beginline = True
        keygroup = False
        dottedkey = False
        keyname = 0
        key = ''
        prev_key = ''
        line_no = 1

        for i, item in enumerate(sl):
            if item == '\r' and sl[i + 1] == '\n':
                sl[i] = ' '
                continue
            if keyname:
                key += item
                if openstring:
                    if item == openstrchar:
                        oddbackslash = False
                        k = 1
                        while i >= k and sl[i - k] == '\\':
                            oddbackslash = not oddbackslash
                            k += 1
                        if not oddbackslash:
                            keyname = 2
                            openstring = False
                            openstrchar = ""
                    continue
                elif keyname == 1:
                    if item.isspace():
                        keyname = 2
                        continue
                    elif item == '.':
                        dottedkey = True
                        continue
                    elif item.isalnum() or item == '_' or item == '-':
                        continue
                    elif (dottedkey and sl[i - 1] == '.' and
                        (item == '"' or item == "'")):
                        openstring = True
                        openstrchar = item
                        continue
                elif keyname == 2:
                    if item.isspace():
                        if dottedkey:
                            nextitem = sl[i + 1]
                            if not nextitem.isspace() and nextitem != '.':
                                keyname = 1
                        continue
                    if item == '.':
                        dottedkey = True
                        nextitem = sl[i + 1]
                        if not nextitem.isspace() and nextitem != '.':
                            keyname = 1
                        continue
                if item == '=':
                    keyname = 0
                    prev_key = key[:-1].rstrip()
                    key = ''
                    dottedkey = False

            if item == "'" and openstrchar != '"':
                k = 1
                try:
                    while sl[i - k] == "'":
                        k += 1
                        if k == 3:
                            break
                except IndexError:
                    pass
                if k == 3:
                    multilinestr = not multilinestr
                    openstring = multilinestr
                else:
                    openstring = not openstring
                if openstring:
                    openstrchar = "'"
                else:
                    openstrchar = ""
            if item == '"' and openstrchar != "'":
                oddbackslash = False
                k = 1
                tripquote = False
                try:
                    while sl[i - k] == '"':
                        k += 1
                        if k == 3:
                            tripquote = True
                            break
                    if k == 1 or (k == 3 and tripquote):
                        while sl[i - k] == '\\':
                            oddbackslash = not oddbackslash
                            k += 1
                except IndexError:
                    pass
                if not oddbackslash:
                    if tripquote:
                        multilinestr = not multilinestr
                        openstring = multilinestr
                    else:
                        openstring = not openstring
                if openstring:
                    openstrchar = '"'
                else:
                    openstrchar = ""
            if item == '#' and (not openstring and not keygroup and
                                not arrayoftables):
                j = i
                comment = ""
                try:
                    while sl[j] != '\n':
                        comment += s[j]
                        sl[j] = ' '
                        j += 1
                except IndexError:
                    break
                if not openarr:
                    self.preserve_comment(line_no, prev_key, comment, beginline)
            if item == '[' and (not openstring and not keygroup and
                                not arrayoftables):
                if beginline:
                    if len(sl) > i + 1 and sl[i + 1] == '[':
                        arrayoftables = True
                    else:
                        keygroup = True
                else:
                    openarr += 1
            if item == ']' and not openstring:
                if keygroup:
                    keygroup = False
                elif arrayoftables:
                    if sl[i - 1] == ']':
                        arrayoftables = False
                else:
                    openarr -= 1
            if item == '\n':
                if openstring or multilinestr:
                    if ((sl[i - 1] == "'" or sl[i - 1] == '"') and (
                            sl[i - 2] == sl[i - 1])):
                        sl[i] = sl[i - 1]
                        if sl[i - 3] == sl[i - 1]:
                            sl[i - 3] = ' '
                elif openarr:
                    sl[i] = ' '
                else:
                    beginline = True
                line_no += 1
            elif beginline and sl[i] != ' ' and sl[i] != '\t':
                beginline = False
                if not keygroup and not arrayoftables:
                    keyname = 1
                    key += item

        s = ''.join(sl)
        s = s.split('\n')
        multikey = None
        multilinestr = ""
        multibackslash = False
        pos = 0
        for idx, line in enumerate(s):
            if idx > 0:
                pos += len(s[idx - 1]) + 1

            if not multilinestr or multibackslash or '\n' not in multilinestr:
                line = line.strip()
            if line == "" and (not multikey or multibackslash):
                continue
            if multikey:
                if multibackslash:
                    multilinestr += line
                else:
                    multilinestr += line
                multibackslash = False
                closed = False
                if multilinestr[0] == '[':
                    closed = line[-1] == ']'
                elif len(line) > 2:
                    closed = (line[-1] == multilinestr[0] and
                            line[-2] == multilinestr[0] and
                            line[-3] == multilinestr[0])
                if closed:
                    value, vtype = self.load_value(multilinestr)
                    currentlevel[multikey] = value
                    multikey = None
                    multilinestr = ""
                else:
                    k = len(multilinestr) - 1
                    while k > -1 and multilinestr[k] == '\\':
                        multibackslash = not multibackslash
                        k -= 1
                    if multibackslash:
                        multilinestr = multilinestr[:-1]
                    else:
                        multilinestr += "\n"
                continue
            if line[0] == '[':
                arrayoftables = False
                if line[1] == '[':
                    arrayoftables = True
                    line = line[2:]
                    splitstr = ']]'
                else:
                    line = line[1:]
                    splitstr = ']'
                i = 1
                quotesplits = self._get_split_on_quotes(line)
                quoted = False
                for quotesplit in quotesplits:
                    if not quoted and splitstr in quotesplit:
                        break
                    i += quotesplit.count(splitstr)
                    quoted = not quoted
                line = line.split(splitstr, i)
                groups = splitstr.join(line[:-1]).split('.')
                i = 0
                while i < len(groups):
                    groups[i] = groups[i].strip()
                    if len(groups[i]) > 0 and (groups[i][0] == '"' or
                                            groups[i][0] == "'"):
                        groupstr = groups[i]
                        j = i + 1
                        while ((not groupstr[0] == groupstr[-1]) or
                            len(groupstr) == 1):
                            j += 1
                            if j > len(groups) + 2:
                                raise Exception
                            groupstr = '.'.join(groups[i:j]).strip()
                        groups[i] = groupstr[1:-1]
                        groups[i + 1:j] = []
                    else:
                        if not re.compile(r'^[A-Za-z0-9_-]+$').match(groups[i]):
                            raise Exception
                    i += 1
                currentlevel = retval
                for i in range(len(groups)):
                    group = groups[i]
                    try:
                        currentlevel[group]
                        if i == len(groups) - 1:
                            if group in implicitgroups:
                                implicitgroups.remove(group)
                            elif arrayoftables:
                                currentlevel[group].append(self.get_empty_table()
                                                        )
                            else:
                                raise Exception
                    except TypeError:
                        currentlevel = currentlevel[-1]
                        if group not in currentlevel:
                            currentlevel[group] = self.get_empty_table()
                            if i == len(groups) - 1 and arrayoftables:
                                currentlevel[group] = [self.get_empty_table()]
                    except KeyError:
                        if i != len(groups) - 1:
                            implicitgroups.append(group)
                        currentlevel[group] = self.get_empty_table()
                        if i == len(groups) - 1 and arrayoftables:
                            currentlevel[group] = [self.get_empty_table()]
                    currentlevel = currentlevel[group]
                    if arrayoftables:
                        try:
                            currentlevel = currentlevel[-1]
                        except KeyError:
                            pass
            elif line[0] == "{":
                self.load_inline_object(line, currentlevel, multikey,
                                        multibackslash)
            elif "=" in line:
                ret = self.load_line(line, currentlevel, multikey,
                                        multibackslash)
                if ret is not None:
                    multikey, multilinestr, multibackslash = ret
        return retval

    def load_value(self, v, strictly_valid=True):
        if not v:
            raise ValueError("Empty value is invalid")
        if v == 'true':
            return (True, "bool")
        elif v.lower() == 'true':
            raise ValueError("Only all lowercase booleans allowed")
        elif v == 'false':
            return (False, "bool")
        elif v.lower() == 'false':
            raise ValueError("Only all lowercase booleans allowed")
        elif v[0] == '"' or v[0] == "'":
            quotechar = v[0]
            testv = v[1:].split(quotechar)
            triplequote = False
            triplequotecount = 0
            if len(testv) > 1 and testv[0] == '' and testv[1] == '':
                testv = testv[2:]
                triplequote = True
            closed = False
            for tv in testv:
                if tv == '':
                    if triplequote:
                        triplequotecount += 1
                    else:
                        closed = True
                else:
                    oddbackslash = False
                    try:
                        i = -1
                        j = tv[i]
                        while j == '\\':
                            oddbackslash = not oddbackslash
                            i -= 1
                            j = tv[i]
                    except IndexError:
                        pass
                    if not oddbackslash:
                        if closed:
                            raise ValueError("Found tokens after a closed " +
                                             "string. Invalid TOML.")
                        else:
                            if not triplequote or triplequotecount > 1:
                                closed = True
                            else:
                                triplequotecount = 0
            if quotechar == '"':
                escapeseqs = v.split('\\')[1:]
                backslash = False
                for i in escapeseqs:
                    if i == '':
                        backslash = not backslash
                    else:
                        if i[0] not in ['0', 'b', 'f', 'n', 'r', 't', '"'] and (i[0] != 'u' and
                                                     i[0] != 'U' and
                                                     not backslash):
                            raise ValueError("Reserved escape sequence used")
                        if backslash:
                            backslash = False
                for prefix in ["\\u", "\\U"]:
                    if prefix in v:
                        hexbytes = v.split(prefix)
                        v = _load_unicode_escapes(hexbytes[0], hexbytes[1:],
                                                  prefix)
                v = _unescape(v)
            if len(v) > 1 and v[1] == quotechar and (len(v) < 3 or
                                                     v[1] == v[2]):
                v = v[2:-2]
            return (v[1:-1], "str")
        elif v[0] == '[':
            return (self.load_array(v), "array")
        elif v[0] == '{':
            inline_object = self.get_empty_inline_table()
            self.load_inline_object(v, inline_object)
            return (inline_object, "inline_object")
        else:
            itype = "int"
            neg = False
            if v[0] == '-':
                neg = True
                v = v[1:]
            elif v[0] == '+':
                v = v[1:]
            v = v.replace('_', '')
            lowerv = v.lower()
            if '.' in v or ('x' not in v and ('e' in v or 'E' in v)):
                if '.' in v and v.split('.', 1)[1] == '':
                    raise ValueError("This float is missing digits after "
                                     "the point")
                if v[0] not in '0123456789':
                    raise ValueError("This float doesn't have a leading "
                                     "digit")
                v = float(v)
                itype = "float"
            elif len(lowerv) == 3 and (lowerv == 'inf' or lowerv == 'nan'):
                v = float(v)
                itype = "float"
            if itype == "int":
                v = int(v, 0)
            if neg:
                return (0 - v, itype)
            return (v, itype)

def _strictly_valid_num(n):
    n = n.strip()
    if not n:
        return False
    if n[0] == '_':
        return False
    if n[-1] == '_':
        return False
    if "_." in n or "._" in n:
        return False
    if len(n) == 1:
        return True
    if n[0] == '0' and n[1] not in ['.', 'o', 'b', 'x']:
        return False
    if n[0] == '+' or n[0] == '-':
        n = n[1:]
        if len(n) > 1 and n[0] == '0' and n[1] != '.':
            return False
    if '__' in n:
        return False
    return True

def _unescape(v):
    """Unescape characters in a TOML string."""
    i = 0
    backslash = False
    while i < len(v):
        if backslash:
            backslash = False
            if v[i] in ['0', 'b', 'f', 'n', 'r', 't', '"']:
                v = v[:i - 1] + _escape_to_escapedchars[v[i]] + v[i + 1:]
            elif v[i] == '\\':
                v = v[:i - 1] + v[i:]
            elif v[i] == 'u' or v[i] == 'U':
                i += 1
            else:
                raise ValueError("Reserved escape sequence used")
            continue
        elif v[i] == '\\':
            backslash = True
        i += 1
    return v

from serializer import Serializer

class TomlSerializer(Serializer):
    encoder = TomlEncoder()
    decoder = TomlDecoder()

    def dump(self,obj,f):
        data = self.to_valid_dict(obj)
        self.encoder.dump(data,f)

    def dumps(self,obj):
        data = self.to_valid_dict(obj)
        return self.encoder.dumps(data)

    def load(self,f):
        obj = self.to_valid_obj(self.decoder.load(f))
        return obj

    def loads(self,data):
        obj =self.to_valid_obj( self.decoder.loads(data) )
        return obj


# Expresiones regulares (search, findall, split, sub)

text = 'Hola mi nombre es Adrian'

import re # Modulo re(Regular Expressions)
#-------------Search-------------#
buscar = re.search('nombre',text)
# re.search('Texto a buscar', variable donde buscar)
print(buscar)
'''
Si el texto se encuentra en la variable arroja esto
<re.Match object; span=(8, 14), match='nombre'>
'''
if buscar:
	print('Texto encontrado')
else:
	print('No se enctro el texto')

buscar = re.search('Adrian',text)
'''
palabra$ indica si el texto termina con esa palabra
^palabra indica si el texto inicia con esa palabra
palabra1.palabra2 puede haber caracteres en medio
palabra1.*palabra2 hay 0 o más caracteres en medio
'''
print(buscar)
#--------------------------------#
#-------------Findall-------------#
texto_2 = '''
El coche de Luis es rojo,
coche de Antonio blanco
y el coche de Maria es rojo
'''
buscar_2 = re.findall('coche.*rojo',texto_2)
'''
['coche de Luis es rojo', 'coche de Maria es rojo']
'''
print(buscar_2)
#---------------------------------#
#--------------split--------------#
texto_3 = 'la silla es blanca y vale 80'
split = re.split('\s',texto_3)# divide el texto en las palabras que
# lo forman en cada espacio en blanco, devolviendo una lista
# de las palabras que hay en el texto
'''
\s espacio en blanco
'''
print(split)
'''
['la', 'silla', 'es', 'blanca', 'y', 'vale', '80']
'''
#---------------------------------#
#---------------sub---------------#
'''
Sustituye todas las coincidencias de una cadena
'''
sub = re.sub('silla','mesa',texto_3)
'''
re.sub('palabra a buscar','cambio',variable)
'''
print(sub)
#---------------------------------#

11. Expresiones regulares, JSON, fecha y hora/1_Expresiones_Regulares.py


from abc import abstractmethod
from functools import reduce
from typing import Callable, Any, List
import random
from .benchmark import *
from copy import copy

class PopulationBasedHeuristics(object):
    """
    Population Based Heuristic Algorithm Template Class.
    """

    def __init__(self, objective_function: Callable[..., Any], soft_constrains: List[Callable[..., Any]] = None,
                 hard_constrains: List[Callable[..., Any]] = None, ranges: list = None, population_size: int = 32,
                 smooth: bool = False, epsilon: float = 10**-4, **params):
        r"""
        Initialize Population Based Heuristic algorithm.

        e.g.,

        objective_function:

        .. code-block:: python
            def objective_function(*x):
                return x[0]**2 + x_[1]**2

        soft_constrains $g_{i} (x) <= 0$:

        .. code-block:: python
            def g1(*x) -> float or int:
                return x[0]

            def g2(*x) -> float or int:
                return x[1]

            soft_constrains = [g1, g2]

        hard_constrains $h_{i} (x) == 0$:

        .. code-block:: python
            def h1(*x) -> float or int:
                return x[0]

            def h2(*x) -> float or int:
                return x[1]

            hard_constrains = [h1, h2]

        ranges:

            variables ranges:

        .. code-block:: python
            ranges = [[0, 100], [0, 90]]

        :param FunctionType objective_function: Method to evaluate objective function
        :param list[FunctionType] soft_constrains: List of Soft constrains methods
        :param list[FunctionType] hard_constrains: List of Hard constrains methods
        :param list[list[float, float]] ranges: List of variables ranges
        :return None:
        """
        self.objective_function = objective_function
        self.soft_constrains = soft_constrains
        self.hard_constrains = hard_constrains
        self.ranges = ranges
        self.dimension = len(ranges)
        self.population_size = population_size
        self.population: List[dict] = []
        self.history: List[dict] = []
        self.smooth: bool = smooth
        self.epsilon: float = epsilon
        self.custom_population: list = []
        self.function_evaluations: int = 0
        self.max_function_evaluations: int = params.get("max_fes", 10**10)

        # Check if population has enough size
        if self.population_size <= 0:
            raise ValueError("""
            Population size is not valid. Population size
            must be greater than 0
            """)

    def set_custom_population(self, custom_population: list) -> None:
        """
        Sets custom initial population
        :custom_population list: Custom initial population
        :return None:
        """
        self.custom_population = custom_population

    def create_individual(self) -> dict:
        """
        Creates individual randomly
        :return dict: Random individual
        """
        return {
            "x": [random.uniform(min(ri), max(ri))
                  for ri in self.ranges],
            "fx": None,
            "gx": None,
            "hx": None,
        }

    def fix_ranges(self, individual: dict) -> dict:
        """
        Fix ranges of an individual

        Checks if and individual is in the search space, if not replace it with a new random one
        :param dict individual: Individual
        :return dict: Fixed Individual
        """
        for j in range(self.dimension):
            sup = max(self.ranges[j])
            inf = min(self.ranges[j])
            x_j = individual["x"][j]
            if x_j < inf or x_j > sup:
                individual["x"][j] = random.uniform(inf, sup)
        return individual

    def create_population(self) -> List[dict]:
        r"""
        Creates population randomly

        e.g.,

        [
            { "x": [0.10, 0.20], fx: None, gx: None, hx: None },
            { "x": [0.05, 0.02], fx: None, gx: None, hx: None },
            { "x": [0.85, 0.72], fx: None, gx: None, hx: None }
        ]
        :custom_population list: Custom population
        :return list[dict]: Random population
        """
        if self.custom_population:
            return self.custom_population
        return [
            self.create_individual()
            for _ in range(self.population_size)
        ]

    def evaluate_population(self, population: List[dict]) -> List[dict]:
        r"""
        Evaluates objective function and constrains for a given population

        e.g.,

        Population:
        .. code-block:: json
        [
            { "x": [0.10, 0.20], fx: None, gx: None, hx: None },
            { "x": [0.05, 0.02], fx: None, gx: None, hx: None },
            { "x": [0.85, 0.72], fx: None, gx: None, hx: None }
        ]

        Evaluated population:

        .. code-block:: json
        [
            { "x": [0.10, 0.20], fx: 10.340, gx: 0, hx: 3.282 },
            { "x": [0.05, 0.02], fx: 1.0010, gx: 0, hx: -0.01 },
            { "x": [0.85, 0.72], fx: -35.02, gx: 0, hx: 5.439 }
        ]
        :return list[dict]: Return evaluated population
        """
        return list(map(self.evaluate_individual, population))

    def update_fes(self):
        self.function_evaluations += 1

    def evaluate_individual(self, individual: dict) -> dict:
        """
        Evaluates objective function and constrains for a single individual
        :param dict individual: Individual
        :return dict: Evaluated Individual
        """
        self.update_fes()
        individual["fx"] = self.objective_function(*individual["x"])
        individual["gx"] = sum([gi(*individual["x"]) * (gi(*individual["x"]) > 0)
                               for gi in self.soft_constrains])
        if self.smooth:
            individual["hx"] = sum([(abs(hi(*individual["x"])) - self.epsilon) *
                                    (abs(hi(*individual["x"])) > self.epsilon)
                                    for hi in self.hard_constrains])
        else:
            individual["hx"] = sum([abs(hi(*individual["x"])) * (hi(*individual["x"]) != 0)
                                    for hi in self.hard_constrains])
        individual["fes"] = copy(self.function_evaluations)

        return individual

    def get_best(self, population: List[dict], exclude_list: list = []) -> dict:
        """
        Gets the best individual for a given population using DEB conditions
        :population List[dict]: Population list
        :exclude_list list: List of excluded indexes
        :return dict: Best individual
        """
        pop = [{**population[j], "index": j} for j in range(len(population))
               if j not in exclude_list]
        return reduce(self.comparison, pop)

    @staticmethod
    def comparison(xi: dict, xj: dict) -> dict:
        """
        Compares to vectors evaluating DEB conditions
        :param dict xi: Vector i
        :param dict xj: Vector j
        :return dict: Return best vector based on DEB conditions
        """
        svr_i = xi["gx"] + xi["hx"]
        svr_j = xj["gx"] + xj["hx"]
        if svr_i <= 0 and svr_j <= 0:
            if xi["fx"] < xj["fx"]:
                return xi
            else:
                return xj
        elif svr_i > 0 and svr_j > 0:
            if svr_i < svr_j:
                return xi
            else:
                return xj
        else:
            if svr_i <= 0:
                return xi
            else:
                return xj

    @abstractmethod
    def population_enhancement(self) -> None:
        pass

    @abstractmethod
    def stop_condition(self) -> None:
        pass

    def save_history(self, iteration: int) -> None:
        """
        Save search data
        :param int iteration: Current iteration
        :return None:
        """
        best_individual = self.get_best(self.population)
        self.history.append({**best_individual, "iteration": iteration + 1,
                             "fes": self.function_evaluations})

    def search(self, iterations: int = 100, save_history: bool = False):
        """
        Search method
        :param int iterations: Number of iterations
        :param bool save_history: If True algorthm will store each iteration data
        :return None:
        """
        self.history = []
        initial_population = self.create_population()
        self.population = copy(self.evaluate_population(initial_population))
        for iteration in range(iterations):
            if self.stop_condition():
                break
            self.population_enhancement()
            if save_history:
                self.save_history(iteration)
            if self.function_evaluations > self.max_function_evaluations:
                break

if __name__ == "__main__":
    pass


import random

N = 100_000

with open("test.txt", "w") as f:
    f.write(str(N) + "\n")
    for i in range(N):
        if i % 100_000 == 0:
            print(str(i / 1_000_000) + " millions")
        f.write(str(random.randint(1, 2**30)) + " ")


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  2 22:38:31 2020

@author: allen
"""

import argparse
import requests
import json
import math
import operator

def conv4326ToXY3414(Lat,Lng):
    #This function converts google's destLat & destLng to 4326 to 3414
    #More info: https://docs.onemap.sg/#4326-wgs84-to-3857
    urlOnemapHeadTo3414 = "https://developers.onemap.sg/commonapi/convert/4326to3414?"
    latlng = "latitude=" + str(Lat) + "&longitude=" + str(Lng)
    urlOnemapTo3414 = urlOnemapHeadTo3414 + latlng
    fileobj = requests.get(urlOnemapTo3414)
    convToXY3414 = json.loads(fileobj.text)
    p1 = [convToXY3414["X"],convToXY3414["Y"]]
    return p1

def getDistancefromXYcoord(p1,p2):
    dist = math.sqrt(((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2))
    return dist

def parseUserInput():
    parser = argparse.ArgumentParser()
    parser.add_argument("destLat", type=float)
    parser.add_argument("destLng", type=float)
    args = parser.parse_args()

    destLat = args.destLat
    destLng = args.destLng
    destLatLngObj = {
        "destLat" : destLat,
        "destLng" : destLng
    }
    return destLatLngObj

def httpRequestForHDBCarpark():
    #website: https://data.gov.sg/dataset/hdb-carpark-information
    urlCarpark = "https://data.gov.sg/api/action/datastore_search?resource_id=139a3035-e624-4f56-b63f-89ae28d4ae4c&limit=10000"
    fileobj = requests.get(urlCarpark)
    carparkDB = json.loads(fileobj.text) #this function should only run once every period and store in a db
    with open('HDBCarpark_DB.txt', 'w') as my_data_file:
        my_data_file.write(fileobj.text)
    return

def httpRequestForCarparkShoppingMallAndHotel():
    urlCarpark = "https://data.gov.sg/api/action/datastore_search?resource_id=85207289-6ae7-4a56-9066-e6090a3684a5&limit=10000"
    fileobj = requests.get(urlCarpark)
    carparkDB = json.loads(fileobj.text) #this function should only run once every period and store in a db
    with open('CarparkShoppingMallAndHotel(original).txt', 'w') as my_data_file:
        my_data_file.write(fileobj.text)
    return

def httpRequestForURACarpark():
    urlCarpark = "https://data.gov.sg/api/action/datastore_search?resource_id=5a8f7572-0d46-4ce5-be71-a7081d2c42b0&limit=10000"
    fileobj = requests.get(urlCarpark)
    carparkDB = json.loads(fileobj.text) #this function should only run once every period and store in a db
    with open('URACarpark_DB.txt', 'w') as my_data_file:
        my_data_file.write(fileobj.text)
    return

def fromTXTHDBCarpark():
    carparkDB = open('HDBCarpark_DB.txt', 'r')
    carparkDB = json.loads(carparkDB.read())
    return carparkDB

def fromTXTCarparkShoppingMallAndHotel():
    carparkDB = open('CarparkShoppingMallAndHotel_DB.txt', 'r')
    carparkDB = json.loads(carparkDB.read())
    return carparkDB

def fromTXTCarparkURA():
    carparkDB = open('URACarpark_DB(Motor Car).txt', 'r')
    carparkDB = json.loads(carparkDB.read())
    return carparkDB

#depreciated
def insertXYCoordToCarparkDB(carparkDB):
    #Key in your Google API key here
    apiKey = 'YOUR GOOGLE API KEY'
    geocodeRequest = 'https://maps.googleapis.com/maps/api/geocode/json?address='
    keyCode = '&key='
    noresultsIDArray = []
    for num in range(len(carparkDB["result"]["records"])):
        print(num)
        inputAddress = carparkDB["result"]["records"][num]["carpark"]
        inputAddress = inputAddress.replace(" ","+")
        googleHttpRequest = geocodeRequest + inputAddress + keyCode + apiKey
        googleMapResponseObj = requests.post(googleHttpRequest).text
        googleMapResponseObj = json.loads(googleMapResponseObj)
        if googleMapResponseObj["status"]=='ZERO_RESULTS':
            noresultsIDArray.append(carparkDB["result"]["records"][num]["_id"])
            print(noresultsIDArray)
        else:
            Lat = googleMapResponseObj["results"][0]["geometry"]["location"]["lat"]
            Lng = googleMapResponseObj["results"][0]["geometry"]["location"]["lng"]
            p1 = conv4326ToXY3414(Lat,Lng)
            xyCoordObj = {
                "x_coord": p1[0],
                "y_coord": p1[1]
            }
            carparkDB["result"]["records"][num].update(xyCoordObj)

    # print(carparkDB["result"]["records"])
    carparkDBtoStr = json.dumps(carparkDB)
    with open('CarparkShoppingMallAndHotel_DB.txt', 'w') as my_data_file:
        my_data_file.write(carparkDBtoStr)

    # loadedtxt = open('data.txt', 'r')
    # loadedtxt = json.loads(loadedtxt.read())
    # print(type(loadedtxt))

    noresultsIDArray = json.dumps(noresultsIDArray)
    with open('noresultsIDArray.txt', 'w') as my_data_file:
        my_data_file.write(noresultsIDArray)
    return

#this 2 are run together after function httpRequestForCarparkShoppingMallAndHotel() and then insertXYCoordToCarparkDB(carparkDB). CarparkDB for shopping mall is ready to be used for nearest carpark
def deleteNoGoogleResultRoutine(notepadRead,notepadWrite,delArrayID):
    carparkDB = open(notepadRead, 'r')
    carparkDB = json.loads(carparkDB.read())

    for index in range(len(delArrayID)):
        idValue = delArrayID[index]
        for num in range(len(carparkDB["result"]["records"])):
            if carparkDB["result"]["records"][num]["_id"] == idValue:
                print(num)
                print(carparkDB["result"]["records"][num])
                del carparkDB["result"]["records"][num]
                break

    carparkDB = json.dumps(carparkDB)
    with open(notepadWrite, 'w') as my_data_file:
        my_data_file.write(carparkDB)
    return
def fillXYCoordSubRoutine(carparkDB,carparkKeyValue,noresultsIDArray,notepadWrite,notepadWriteNoResult1stArray,notepadWriteNoResult2ndArray):
    apiKey = 'YOUR GOOGLE API KEY'
    geocodeRequest = 'https://maps.googleapis.com/maps/api/geocode/json?address='
    keyCode = '&key='

    noresultsIDArray2ndRound = []
    if len(noresultsIDArray) > 0:
        for index in range(len(noresultsIDArray)):
            print("this should not be running!")
            idValue = noresultsIDArray[index]
            for num in range(len(carparkDB["result"]["records"])):
                if carparkDB["result"]["records"][num]["_id"] == idValue:
                    inputAddress = carparkDB["result"]["records"][num][carparkKeyValue]
                    inputAddress = inputAddress.replace(" ","+")
                    googleHttpRequest = geocodeRequest + inputAddress + keyCode + apiKey
                    googleMapResponseObj = requests.post(googleHttpRequest).text
                    googleMapResponseObj = json.loads(googleMapResponseObj)

                    if googleMapResponseObj["status"]=='ZERO_RESULTS':
                        noresultsIDArray2ndRound.append(carparkDB["result"]["records"][num]["_id"])
                        print(noresultsIDArray2ndRound)
                    else:
                        Lat = googleMapResponseObj["results"][0]["geometry"]["location"]["lat"]
                        Lng = googleMapResponseObj["results"][0]["geometry"]["location"]["lng"]
                        p1 = conv4326ToXY3414(Lat,Lng)
                        xyCoordObj = {
                            "x_coord": p1[0],
                            "y_coord": p1[1]
                        }
                        carparkDB["result"]["records"][num].update(xyCoordObj)
    else:
        for num in range(len(carparkDB["result"]["records"])):
            inputAddress = carparkDB["result"]["records"][num][carparkKeyValue]
            inputAddress = inputAddress.replace(" ","+")
            googleHttpRequest = geocodeRequest + inputAddress + keyCode + apiKey
            googleMapResponseObj = requests.post(googleHttpRequest).text
            googleMapResponseObj = json.loads(googleMapResponseObj)

            if googleMapResponseObj["status"]=='ZERO_RESULTS':
                noresultsIDArray.append(carparkDB["result"]["records"][num]["_id"])
                print(noresultsIDArray)
            else:
                Lat = googleMapResponseObj["results"][0]["geometry"]["location"]["lat"]
                Lng = googleMapResponseObj["results"][0]["geometry"]["location"]["lng"]
                p1 = conv4326ToXY3414(Lat,Lng)
                xyCoordObj = {
                    "x_coord": p1[0],
                    "y_coord": p1[1]
                }
                carparkDB["result"]["records"][num].update(xyCoordObj)
                print(carparkDB["result"]["records"][num])

    carparkDB = json.dumps(carparkDB)
    with open(notepadWrite, 'w') as my_data_file:
        my_data_file.write(carparkDB)

    if len(noresultsIDArray) > 0:
        noresultsIDArray = json.dumps(noresultsIDArray)
        with open(notepadWriteNoResult1stArray, 'w') as my_data_file:
            my_data_file.write(noresultsIDArray)

    if len(noresultsIDArray2ndRound) > 0:
        with open(notepadWriteNoResult2ndArray, 'w') as my_data_file:
            my_data_file.write(noresultsIDArray2ndRound)

    return
def fillXYCoordMain(notepadRead,notepadWrite,notepadWriteNoResult1stArray,carparkKeyValue,notepadWriteNoResult2ndArray):
    carparkDB = open(notepadRead, 'r')
    carparkDB = json.loads(carparkDB.read())
    noresultsID1stArray = []
    #line below should be run at the start and then comment out
    # fillXYCoordSubRoutine(carparkDB,carparkKeyValue,noresultsID1stArray,notepadWrite,notepadWriteNoResult1stArray,notepadWriteNoResult2ndArray)

    #3 lines below should be run after line at the top
    delArrayID = open(notepadWriteNoResult2ndArray, 'r')
    delArrayID = json.loads(delArrayID.read())
    deleteNoGoogleResultRoutine(notepadRead,notepadWrite,delArrayID)

    return
# fillXYCoordMain("URACarpark_DB(Motor Car).txt","URACarpark_DB(Motor Car)(XY).txt","noresultsIDArrayURA1st.txt","ura_carpark_name","noresultsIDArrayURA2nd.txt")
#for running delete code
# fillXYCoordMain("URACarpark_DB(Motor Car)(XY).txt","URACarpark_DB(Motor Car)(XY)- After Delete.txt","noresultsIDArrayURA1st.txt","ura_carpark_name","noresultsIDArrayURA2nd.txt")

#this 3 function below will run together to filter motorcar carparks from original database
def loop(carparkDB,key,value):
    print(len(carparkDB["result"]["records"]))
    for index, carparks in enumerate(carparkDB["result"]["records"]):
        if type(value) == str:
            if value not in carparks[key]:
                # print(carparkDB["result"]["records"][index])
                del carparkDB["result"]["records"][index]
                bDel = 1
            else:
                bDel = 0
        else:
            if  carparks[key] == str(value):
                print(carparkDB["result"]["records"][index])
                del carparkDB["result"]["records"][index]
                bDel = 1
            else:
                bDel = 0

    print(len(carparkDB["result"]["records"]))

    obj = {
        "carparkDB": carparkDB,
        "bDel": bDel
    }
    return obj
def filterURACarpark(notepadRead,key,value,notepadWrite):
    carparkDB = open(notepadRead, 'r')
    carparkDB = json.loads(carparkDB.read())
    bDel = 1
    while bDel != 0:
        obj = loop(carparkDB,key,value)
        carparkDB = obj["carparkDB"]
        bDel = obj["bDel"]

    carparkDB = json.dumps(carparkDB)
    with open(notepadWrite, 'w') as my_data_file:
        my_data_file.write(carparkDB)
    return
def filterURACarparkOverall():
    filterURACarpark('URACarpark_DB.txt',"type_of_lot","Motor Car",'URACarpark_DB(Motor Car).txt')
    filterURACarpark('URACarpark_DB(Motor Car).txt',"number_of_lots",0,'URACarpark_DB(Motor Car).txt')
    return

def combineAllCarparksDB():
    carparkDBURA = open('URACarpark_DB(Motor Car)(XY)- After Delete.txt', 'r')
    carparkDBURA = json.loads(carparkDBURA.read())

    carparkDBHDB = open('HDBCarpark_DB.txt', 'r')
    carparkDBHDB = json.loads(carparkDBHDB.read())

    carparkDBSPM = open('CarparkShoppingMallAndHotel_DB(XY).txt', 'r')
    carparkDBSPM = json.loads(carparkDBSPM.read())

    combinedDB = {
        "result": {"records":[]}
    }

    combinedDB["result"]["records"].extend(carparkDBURA["result"]["records"])
    combinedDB["result"]["records"].extend(carparkDBHDB["result"]["records"])
    combinedDB["result"]["records"].extend(carparkDBSPM["result"]["records"])
    combinedDB["result"]["total_records"] = len(combinedDB["result"]["records"])

    combinedDB = json.dumps(combinedDB)
    with open("combinedCarparkDB.txt", 'w') as my_data_file:
        my_data_file.write(combinedDB)
    return

def returnNearestCarpark(carparkDB):
    #for testing, comment 3 lines below
    destLatLngObj = parseUserInput()
    destLat = destLatLngObj["destLat"]
    destLng = destLatLngObj["destLng"]

    # #for testing
    # destLat = 1.3602298
    # destLng = 103.8595684
    p1 = conv4326ToXY3414(destLat,destLng)

    #Long = X, Lat = Y
    #This function finds the distance between p2 (x,y coord for HDB carpark DB) with p1 (destination's x,y coord)
    distDict = {}
    for num in range(len(carparkDB["result"]["records"])):
        xCoord2 = float(carparkDB["result"]["records"][num]["x_coord"])
        yCoord2 = float(carparkDB["result"]["records"][num]["y_coord"])
        p2 = [xCoord2, yCoord2]
        dist = getDistancefromXYcoord(p1,p2)
        distDict[num] = round(dist)

    #This function sort distance (value) with their carpark (keys) with smallest distance at the top
    distDict = sorted(distDict.items(), key=operator.itemgetter(1))
    distDict = dict((x, y) for x, y in distDict)
    #distKeysArray will store only the keys to be used later to reference and get the particular carpark name
    distKeysArray = list(distDict.keys())

    nearestCarpark = {"carpark_details" : {}}
    for num in range(0,5):
        index = distKeysArray[num]
        #For getting carpark address
        if "address" in carparkDB["result"]["records"][index]:
            carparkAddress = str(carparkDB["result"]["records"][index]["address"])
            #to also additionally get HDB parking rates
            carparkRate = "$0.60 or $1.20 per half-hour* (More info here: https://www.hdb.gov.sg/cs/infoweb/car-parks/short-term-parking/short-term-parking-charges)"
        elif "carpark" in carparkDB["result"]["records"][index]:
            carparkAddress = str(carparkDB["result"]["records"][index]["carpark"])
            #to also additionally get SPM parking rates
            carparkRate1 = carparkDB["result"]["records"][index]["weekdays_rate_1"]
            carparkRate2 = carparkDB["result"]["records"][index]["weekdays_rate_2"]

            if (carparkRate2 == "-" or carparkRate2 == carparkRate1):
                carparkRate = carparkRate1
            else:
                carparkRate = carparkRate1 + "AND" + carparkRate2

        elif "ura_carpark_name" in carparkDB["result"]["records"][index]:
            carparkAddress = str(carparkDB["result"]["records"][index]["ura_carpark_name"])
            #to also additionally get URA parking rates
            carparkRate = "$0.60 or $1.20 per half-hour* (More info here: https://www.ura.gov.sg/Corporate/Car-Parks/Short-Term-Parking/Parking-coupons"

        # For calculating distance
        distanceFromDest = str(distDict.get(index))
        xCoord = float(carparkDB["result"]["records"][index]["x_coord"])
        yCoord = float(carparkDB["result"]["records"][index]["y_coord"])
        urlOnemapTo3857 = "https://developers.onemap.sg/commonapi/convert/3414to3857?"
        XandY = "X=" + str(xCoord) + "&Y=" + str(yCoord)
        urlOnemapTo3857 = urlOnemapTo3857 + XandY
        fileobj = requests.get(urlOnemapTo3857)
        convTo3857 = json.loads(fileobj.text)
        lng = convTo3857["X"]
        lat = convTo3857["Y"]
        nearestCarpark["carpark_details"][num] = {"address": str(carparkAddress), "lat": str(lat), "lng": str(lng), "distance_from_dest": str(distanceFromDest+'m'),"carpark_rates": str(carparkRate)}

    nearestCarpark = json.dumps(nearestCarpark).replace('\'', '"')
    print(nearestCarpark)
    return

carparkDB = open('combinedCarparkDB.txt','r')
carparkDB = json.loads(carparkDB.read())
returnNearestCarpark(carparkDB)

# this 2 function should be run periodically once every month to refresh data from govtech
# httpRequestForHDBCarpark()
# httpRequestForCarparkShoppingMallAndHotel()


# -*- coding: UTF-8 -*-
"""
title: 不同字符的最小子序列
Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

Example 1:
Input: s = "bcabc"
Output: "abc"

Example 2:
Input: s = "cbacdcbc"
Output: "acdb"

Constraints:
1 <= s.length <= 1000
s consists of lowercase English letters.
此题与LeetCode题316相同
"""

class Solution:
    def smallestSubsequence(self, s: str) -> str:
        """
        贪心 + 单调栈
        要想使返回结果的字典序最小，则应尽量使返回结果中的字母升序，因此可使用一个单调递增栈。
        若当前字母小于栈顶字母，且栈顶字母在之后位置还存在，则可以pop栈顶字母；若栈顶字母在之后不存在了，则此时必须保留。
        """
        # 记录每个字母最后一次出现的下标
        ch2idx = {}
        for idx, ch in enumerate(s):
            ch2idx[ch] = idx
        stack = []
        chs_in_stack = set()
        for idx, ch in enumerate(s):
            if ch not in chs_in_stack:
                while stack and stack[-1] > ch and ch2idx[stack[-1]] > idx:
                    chs_in_stack.remove(stack.pop())
                chs_in_stack.add(ch)
                stack.append(ch)
        return ''.join(stack)


from os import listdir
from skimage.morphology import disk, square
from utils import populateFiles, readImages, saveImageFile, applyCustomThreshold, applyOtsuThreshold, applyAdaptiveThreshold

# Let's apply median, gaussian and mean
# all of the morphed images

sourceDir = 'step2'
destDir = 'step3'

# get morphed images
files = populateFiles(sourceDir)

# read each file and populate a dictionary
images = readImages(sourceDir, files)

# iterate over dictionary and apply thresholds
for name, image in images.items():
    # let's derive a custom threshold from the histogram, say 75
    customImage = applyCustomThreshold(image, 75)
    saveImageFile(customImage, destDir + '/' + name + '_custom75.bmp')
    otsuImage, thr = applyOtsuThreshold(image)
    saveImageFile(otsuImage, destDir + '/' + name + '_otsu' + str(thr) + '.bmp')
    # blocksize 35 and offset 5 seems OK
    adaptiveImage = applyAdaptiveThreshold(image, 35, 3)
    saveImageFile(adaptiveImage, destDir + '/' + name + '_adaptive.bmp')


#！/usr/bin/env python
#  -*- coding:utf-8 -*-
#  author:dabai time:2019/2/22

class Settings():
    """存储《外星人入侵》的所有设置的类"""

    def __init__(self):
        """初始化游戏的静态设置"""
        # 屏幕设置
        self.screen_width=1000
        self.screen_height=600
        self.bg_color=(230,230,230) # 设置背景色:RGB

        # 飞船的设置
        self.ship_speed_factor=1.5
        self.ship_limit=3

        # 子弹设置
        self.bullet_speed_factor=3
        self.bullet_width=3
        self.bullet_height=15
        self.bullet_color=60,60,60
        self.bullets_allowed=3

        # 外星人设置
        self.alien_speed_factor=1
        self.fleet_drop_speed=10
        # fleet_direction为1表示向右移，为-1表示向左移
        self.fleet_direction=1

        # 以什么样的速度加快游戏节奏
        self.speedup_scale=1.1

        # 外星人点数的提高速度
        self.score_scale=1.5

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        """初始化随游戏进行而变化的设置"""
        self.ship_speed_factor=1.5
        self.bullet_speed_factor=3
        self.alien_speed_factor=1

        # fleet_direction为1表示向右；为-1表示向左
        self.fleet_direction=1

        # 记分
        self.alien_points=50

    def increase_speed(self):
        """提高速度设置和外星人点数"""
        self.ship_speed_factor*=self.speedup_scale
        self.bullet_speed_factor*=self.speedup_scale
        self.alien_speed_factor*=self.speedup_scale

        self.alien_points=int(self.alien_points*self.score_scale)


class Solution136:
    # Time: O(n); Space: O(n)
    def singleNumber(self, nums: List[int]) -> int:
        unique = set()
        for i in nums:
            if i in unique:
                unique.remove(i)
            else:
                unique.add(i)
        return list(unique)[0]

    # Time: O(n); Space: O(1)
    # check the same number -> XOR
    def singleNumber2(self, nums: List[int]) -> int:
        ans = nums[0]
        for i in range(1, len(nums)):
            ans ^= nums[i]
        return ans


# URI Online Judge | 1020
# Link: https://www.urionlinejudge.com.br/judge/pt/problems/view/1020

__author__ = 'darkwolf'

time = int(input())
years = months = int(0)
while time >= 365:
    years += 1
    time -= 365
while time >= 30:
    months += 1
    time -= 30
days = time
print("%i ano(s)" % years)
print("%i mes(es)" % months)
print("%i dia(s)" % days)


from typing import TYPE_CHECKING, Optional
from ortools.linear_solver import pywraplp
if TYPE_CHECKING:
    from Entity import Team
    from Enum import Position
    from Optimizer.MathModel import MathModel

class Player:
    def __init__(self, name: str, team: "Team", position: "Position", price: float, pick: float = 0.0, minutes: int = 0,
                 points: int = 0, goals: int = 0, assists: int = 0, bonus: int = 0, bps: float = 0.0):
        self.name: str = name
        self.team: "Team" = team
        self.position: "Position" = position
        self.price: float = float(str(price).replace('Â£', '').replace('M', ''))
        self.pick: float = float(str(pick).replace('%', ''))
        self.minutes: int = int(minutes)
        self.points: int = int(points)
        self.goals: int = int(goals)
        self.assists: int = int(assists)
        self.bonus: int = int(bonus)
        self.bps: float = float(bps)
        self.selectionVariable: Optional[pywraplp.Variable] = None
        self.benchVariable: Optional[pywraplp.Variable] = None

    def initializeVariable(self, model: "MathModel") -> None:
        self.selectionVariable = model.solver.IntVar(0, 1, f'main_select_{self.key()}')
        self.benchVariable = model.solver.IntVar(0, 1, f'bench_select_{self.key()}')

    def key(self, price:bool = True) -> str:
        firstP = f"{self.name}({self.team.name})({self.position.name})"
        secondP = f"({self.price})" if price else ''
        return firstP + secondP

    def secondaryValue(self) -> float:
        return 0.01 * (self.points + self.goals + self.assists + self.pick + self.minutes)


#!/usr/bin/env python

import numpy as np
import pandas as pd
import click as ck
from sklearn.metrics import classification_report
from sklearn.metrics.pairwise import cosine_similarity
import sys
from collections import deque
import time
import logging
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
from scipy.spatial import distance
from scipy import sparse
import math
from utils import FUNC_DICT, Ontology, NAMESPACES
from matplotlib import pyplot as plt
import os

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)

@ck.command()
@ck.option(
    '--data-root', '-dr', default='data',
    help='Prediction model')
@ck.option(
    '--ont', '-ont', default='mf',
    help='Prediction model')
def main(data_root, ont):
    train_data_file = f'data/{ont}/train_data.pkl'
    valid_data_file = f'{data_root}/{ont}/valid_data.pkl'
    test_data_file = f'{data_root}/{ont}/test_data.pkl'
    terms_file = f'{data_root}/{ont}/terms.pkl'

    go_rels = Ontology(f'data/go-basic.obo', with_rels=True)
    terms_df = pd.read_pickle(terms_file)
    terms = terms_df['gos'].values.flatten()
    terms_dict = {v: i for i, v in enumerate(terms)}

    train_df = pd.read_pickle(train_data_file)
    valid_df = pd.read_pickle(valid_data_file)
    train_df = pd.concat([train_df, valid_df])

    test_df = pd.read_pickle(test_data_file)

    annotations = train_df['prop_annotations'].values
    annotations = list(map(lambda x: set(x), annotations))

    prot_index = {}
    for i, row in enumerate(train_df.itertuples()):
        prot_index[row.proteins] = i

    prot_ac_index = {}
    for i, row in enumerate(train_df.itertuples()):
        prot_ac_index[row.accessions[0]] = i

    dsets = {'train': train_df, 'valid': valid_df, 'test': test_df}

    # BLAST Similarity (Diamond)
    for dset in ('test',):
        diamond_scores_file = f'{data_root}/{ont}/{dset}_diamond.res'
        diamond_scores = {}
        with open(diamond_scores_file) as f:
            for line in f:
                it = line.strip().split()
                if it[0] == it[1]: # Ignore same proteins (for train)
                    continue
                if it[0] not in diamond_scores:
                    diamond_scores[it[0]] = {}
                diamond_scores[it[0]][it[1]] = float(it[2])

        # scores_file = f'{data_root}/{ont}/{dset}_foldseek.m8'
        # scores = {}
        # with open(scores_file) as f:
        #     for line in f:
        #         it = line.strip().split()
        #         if it[0] == it[1]: # Ignore same proteins (for train)
        #             continue
        #         p1, p2 = it[0].split('.')[0], it[1].split('.')[0]
        #         if p1 not in scores:
        #             scores[p1] = {}
        #         scores[p1][p2] = float(it[2])

        # fold_preds = []
        # print(f'Preds for {dset}')
        # df = dsets[dset]
        # for i, row in enumerate(df.itertuples()):
        #     annots = {}
        #     prop_annots = {}
        #     prot_id = row.accessions[0]
        #     if prot_id in scores:
        #         sim_prots = scores[prot_id]
        #         allgos = set()
        #         total_score = 0.0
        #         for p_id, score in sim_prots.items():
        #             if p_id in prot_ac_index:
        #                 allgos |= annotations[prot_ac_index[p_id]]
        #                 total_score += score
        #         allgos = list(sorted(allgos))
        #         sim = np.zeros(len(allgos), dtype=np.float32)
        #         for j, go_id in enumerate(allgos):
        #             s = 0.0
        #             for p_id, score in sim_prots.items():
        #                 if p_id in prot_ac_index and go_id in annotations[prot_ac_index[p_id]]:
        #                     s += score
        #             sim[j] = s / total_score
        #         for go_id, score in zip(allgos, sim):
        #             annots[go_id] = score
        #         prop_annots = annots.copy()

        #         for go_id, score in annots.items():
        #             for sup_go in go_rels.get_ancestors(go_id):
        #                 if sup_go in prop_annots:
        #                     prop_annots[sup_go] = max(prop_annots[sup_go], score)
        #                 else:
        #                     prop_annots[sup_go] = score
        #     fold_preds.append(prop_annots)

        diam_preds = []
        print(f'Diamond preds for {dset}')
        df = dsets[dset]
        for i, row in enumerate(df.itertuples()):
            annots = {}
            prop_annots = {}
            prot_id = row.proteins
            # DiamondScore
            if prot_id in diamond_scores:
                sim_prots = diamond_scores[prot_id]
                allgos = set()
                total_score = 0.0
                for p_id, score in sim_prots.items():
                    allgos |= annotations[prot_index[p_id]]
                    total_score += score
                allgos = list(sorted(allgos))
                sim = np.zeros(len(allgos), dtype=np.float32)
                for j, go_id in enumerate(allgos):
                    s = 0.0
                    for p_id, score in sim_prots.items():
                        if go_id in annotations[prot_index[p_id]]:
                            s += score
                    sim[j] = s / total_score
                for go_id, score in zip(allgos, sim):
                    annots[go_id] = score

                prop_annots = annots.copy()
                for go_id, score in annots.items():
                    for sup_go in go_rels.get_ancestors(go_id):
                        if sup_go in prop_annots:
                            prop_annots[sup_go] = max(prop_annots[sup_go], score)
                        else:
                            prop_annots[sup_go] = score
            diam_preds.append(prop_annots)

        # new_preds = []
        # for dp, fp in zip(diam_preds, fold_preds):
        #     for go_id, score in fp.items():
        #         if go_id in dp:
        #             dp[go_id] = max(dp[go_id], score)
        #         else:
        #             dp[go_id] = score
        #     new_preds.append(dp)

        df['diam_preds'] = diam_preds
        # df['fold_preds'] = fold_preds
        # df['diam_fold_preds'] = new_preds
        filename = f'{data_root}/{ont}/{dset}_data_diam.pkl'
        df.to_pickle(filename)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 29 06:59:01 2016

@author: mike
"""

from utils import cutoff_funcs
from numpy import arange, array
reload(cutoff_funcs)

#### Plot of scaling factor
t = logspace(0.0, log10(10000), num=250).astype(int)
x = array([cutoff_funcs.fdr_func(arange(1.0, 1.0 +n) / float(n)) for n in t])

####

alpha_fdr = .1
beta_fdr = .05
N = 10

alpha_vec = cutoff_funcs.create_fdr_controlled_alpha(alpha_fdr,
                                                     arange(1.0, 1.0 + N))
beta_vec = cutoff_funcs.create_fdr_controlled_alpha(beta_fdr,
                                                    arange(1.0, 1.0 + N))


from os import environ, listdir
from json import load, dump
from discord import Activity, Embed, Intents, opus
from discord.ext import commands
from rich import print
from rich.console import Console
from rich.table import Table
from ctypes.util import find_library
console = Console()
__import__("dotenv").load_dotenv()

# -------
# prefix area

defaultprefixes = ["World ", "w/", "world "]
ownerprefix = ["w/", "world ", "World "]

async def get_prefix(world, message):
    if await world.is_owner(message.author):
        return commands.when_mentioned_or(*ownerprefix)(world, message)
    prefixes = load(open('prefixes.json', 'r'))
    if not str(message.guild.id) in prefixes:
        return commands.when_mentioned_or(*defaultprefixes)(world, message)
    return commands.when_mentioned_or(prefixes[str(message.guild.id)])(world, message)

# -------
# Main bot area

world = commands.Bot(
    command_prefix=get_prefix,
    description="Discord Bot Made For All",
    case_insensitive=True,
    intents=Intents.all()
)
setattr(world, "color", 0x2F3136)
world.remove_command("help")

# -------
# Command area

@world.command(help="Change the prefix for your discord guild")
@commands.has_permissions(administrator=True)
async def changeprefix(ctx, prefix):
    if len(prefix) > 7:
        return await ctx.send(f"Sorry {ctx.author.mention} a limit of `7` letters please.")
    with open('prefixes.json', 'r') as f:
        prefixes = load(f)

    prefixes[str(ctx.guild.id)] = prefix

    with open('prefixes.json', 'w') as f:
        dump(prefixes, f, indent=4)
    await ctx.send(embed=Embed(
        title="Custom prefix",
        description=f"Prefix for {ctx.guild} is now `{prefix}`.\nTry using the help command: `{prefix}help`"
    ))

@world.command()
async def enablenoprefix(ctx):
    if await world.is_owner(ctx.author):
        ownerprefix.append("")
        await ctx.send(f"Hey {ctx.author.mention} i have enabled `No Prefix mode`.")

@world.command()
async def disablenoprefix(ctx):
    if await world.is_owner(ctx.author):
        ownerprefix.remove("")
        await ctx.send(f"Hey {ctx.author.mention} i have disabled `No Prefix mode`.")

# -------
# Cogs area
for cog in filter(lambda x: x.endswith(".py"), listdir("cogs/")):
    world.load_extension(f"cogs.{cog[:-3]}")
    print(f"[bold cyan]INFO  [/bold cyan][yellow]COG:[/yellow] [purple]{cog}[/purple] [yellow]is loaded.[/yellow]")

# -------
# Blacklist area
with open("blacklisted.json") as f:
    blacklisted_people = load(f)

# -------
# Message received area
@world.event
async def on_message(message):
    """
    Dispatched every time a message is sent.
    Checks if the message wasn't send by a bot, the message wasn't send
    on DMs and the user isn't blacklisted.
    """
    if message.author.bot or (str(message.author.id) in blacklisted_people):
        return
    elif not message.guild:
        return await message.channel.send((
            "You can't use commands on DMs, invite the bot your server: "
            f"<https://discord.com/oauth2/authorize?client_id={world.user.id}&permissions=8&scope=bot>"
        ))

    await world.process_commands(message)

# -------
# Event area
@world.event
async def on_ready():
    """Dispatched when the bot has successfully connected into Discord."""
    await world.change_presence(status="dnd", activity=Activity(type=2, name="w/help"))
    table = Table()
    table.add_column("[bold white]Bot info[/bold white]", justify="left", style="bold blue", no_wrap=True)
    table.add_row(f"[bold cyan]Name:[/bold cyan] {world.user.name}")
    table.add_row(f"[bold cyan]Latency:[/bold cyan] {float(world.latency*1000):.0f}ms")
    table.add_row(f"[bold cyan]Guilds:[/bold cyan] {len(world.guilds)}")
    console.print(table)

@world.event
async def on_guild_remove(guild):
    prefixes = load(open('prefixes.json', 'r'))
    prefixes.pop(str(guild.id))

    with open('prefixes.json', 'w') as f:
        dump(prefixes, f, indent=4)

#if not opus.is_loaded():
 #   opus.load_opus(find_library("opus"))

world.run(environ["TOKEN"])


import json
from dbnew import DB

db = DB("GNCorp.db")

create_employees = """
INSERT INTO employees (
    first_name,
    last_name,
    team_id,
    leader_id,
    title,
    description
    )
VALUES (?, ?, ? ,? ,? ,?)
"""
create_team ="""
INSERT INTO teams (
    team_name,
    leader_id,
    description
)
VALUES (?, ?, ?)
"""

with open("seed_all.json", "r") as seed:
    data = json.load(seed)

    for emp in data["employees"]:
        db.call_db(create_employees,
                   emp["first_name"],
                   emp["last_name"],
                   emp["team_id"],
                   emp["leader_id"],
                   emp["title"],
                   emp["description"])

    for team in data["teams"]:
        db.call_db(create_team,
                   team["team_name"],
                   team["leader_id"],
                   team["description"])

print(emp)
print(team)


""" Username to UUID
 Converts a Minecraft username to it's UUID equivalent.

Uses the official Mojang API to fetch player data.

credit: https://github.com/EthanJoyce/MinecraftUsernameToUUID tnx buddy <3
"""
import http.client
import json

class UsernameToUUID:
    def __init__(self, username):
        self.username = username

    def get_uuid(self, timestamp=None):
        """
          Get the UUID of the player.

          Parameters
          ----------
          timestamp : long integer
              The time at which the player used this name, expressed as a Unix timestamp.
        """
        get_args = "" if timestamp is None else "?at=" + str(timestamp)

        http_conn = http.client.HTTPSConnection("api.mojang.com")
        http_conn.request("GET", "/users/profiles/minecraft/" + self.username + get_args,
            headers={'User-Agent':'Minecraft Username -> UUID', 'Content-Type':'application/json'})
        response = http_conn.getresponse().read().decode("utf-8")

        if not response and timestamp is None: # No response & no timestamp
            return self.get_uuid(0) # Let's retry with the Unix timestamp 0.
        if not response: # No response (player probably doesn't exist)
            return ""

        json_data = json.loads(response)
        try:
            uuid = json_data['id']
        except KeyError as e:
            print("KeyError raised:", e)

        return uuid


from django.conf.urls import url

from users.views import show, log_in, reg_in, findback, personpage, gladdress, address, short_msg, choice_address, \
    address_change, address_delet, set_default

urlpatterns = [
    url(r'^$',show,name='主页'),
    url(r'^reg_in/$',reg_in,name='注册'),
    url(r'^log_in/$',log_in,name='登录'),
    url(r'^findback/$',findback,name='找回密码'),
    url(r'^infor/$',personpage,name='个人首页'),
    url(r'^gladdress/$',gladdress,name='收货地址显示页'),
    url(r'^address/$',address,name='添加'),
    url(r'^short_msg/$',short_msg,name='短信验证'),
    url(r'^choice_address/$',choice_address,name='添加收货地址'),
    url(r'^address_change/',address_change,name='修改收货地址'),
    url(r'^address_delet/',address_delet,name='删除收货地址'),
    url(r'^set_default/',set_default,name='修改默认地址'),
]


import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn,seq2seq

class Chatbot(object):
	"""docstring for Chatbot"""
	def __init__(self, word_dim):
		self.layer=2
		self.lr = 0.001
		self.word_dim = word_dim
		self.embedding_size = 1000
		self.hidden_units = 256
		self.sequence_length = 15
		self.beamsearch = False
		self.bidirectional = True
		self.initializer = tf.contrib.layers.xavier_initializer()
		self.embedding_layer = tf.get_variable(name='embedding',
			shape=[self.word_dim, self.embedding_size],initializer=self.initializer)

		self.__graph__()

	def __graph__(self):
		self.init_placeholder()
		self.build_encoder()
		self.build_decoder()
		self.build_optimizer()

	def init_placeholder(self):
		self.X = tf.placeholder(tf.int32, [None, self.sequence_length])
		self.Y = tf.placeholder(tf.int32, [None, self.sequence_length])
		self.dropout = tf.placeholder(tf.float32, [])
		self.batch_size = tf.placeholder(tf.int32, [])
		self.mask = tf.placeholder(tf.float32, [None, None])
		self.sp = tf.placeholder(tf.float32, [])
		self.len = tf.placeholder(tf.int32, [None])

	def build_encoder(self):
		# print ("build encoder ....... ")
		with tf.variable_scope('encoder'):

			self.encoder_inputs = tf.nn.embedding_lookup(self.embedding_layer, self.X)
			# projection_layer = tf.layers.Dense(self.hidden_units, use_bias=False, kernel_initializer=self.initializer)
			# self.encoder_inputs = projection_layer(self.encoder_inputs)

			if self.bidirectional == True:
				encoder_fw_cell = self.build_single_cell(self.hidden_units)
				encoder_bw_cell = self.build_single_cell(self.hidden_units)
				encoder_outputs, self.encoder_last_state = tf.nn.bidirectional_dynamic_rnn(
					encoder_fw_cell,
					encoder_bw_cell,
					self.encoder_inputs,
					dtype=tf.float32)
				self.encoder_outputs = tf.concat(encoder_outputs, -1)
			else:
				self.encoder_cell = rnn.MultiRNNCell([self.build_single_cell(self.hidden_units) for _ in range(self.layer)], state_is_tuple=True)

				self.encoder_outputs, self.encoder_last_state = tf.nn.dynamic_rnn(
					cell=self.encoder_cell, inputs=self.encoder_inputs, dtype=tf.float32, time_major=False)

	def build_decoder(self):
		# print ("build decoder ....... ")
		with tf.variable_scope('decoder'):
			# build cell list & attention
			cell_list = [self.build_single_cell(self.hidden_units) for i in range(self.layer)]
			# input_layer = tf.layers.Dense(self.hidden_units, name='input_projection')
			output_layer = tf.layers.Dense(self.word_dim, use_bias=False, kernel_initializer=self.initializer, name='output_projection')
			self.decoder_inputs = tf.nn.embedding_lookup(self.embedding_layer, self.Y)
			# self.decoder_inputs = input_layer(self.decoder_inputs)
			attention_mechanism = seq2seq.BahdanauAttention(
				num_units=self.hidden_units,
				memory=self.encoder_outputs,normalize=True)
			cell_list[-1] = tf.contrib.seq2seq.AttentionWrapper(
				cell=cell_list[-1],
				attention_mechanism=attention_mechanism,
				attention_layer_size=self.hidden_units,
				initial_cell_state=self.encoder_last_state[-1],
				name = 'attention_wrapper',
			)

			decoder_cell = rnn.MultiRNNCell(cells=cell_list, state_is_tuple=True)
			# RNN initial state
			initial_state = [state for state in self.encoder_last_state]
			initial_state[-1] = cell_list[-1].zero_state(batch_size=self.batch_size , dtype=tf.float32)
			decoder_initial_state = tuple(initial_state)
			self.decoder_initial_state = decoder_initial_state

			# training helper (training)
			training_helper = seq2seq.ScheduledEmbeddingTrainingHelper(
				inputs = self.decoder_inputs,
				sequence_length = self.len,
				embedding = self.embedding_layer,
				sampling_probability=self.sp)

			training_decoder = seq2seq.BasicDecoder(
				cell=decoder_cell,
				helper=training_helper,
				initial_state=decoder_initial_state,
				output_layer=output_layer)
			decoder_outputs, decoder_state, decoder_length = seq2seq.dynamic_decode(training_decoder)
			self.logit = decoder_outputs.rnn_output
			self.loss = tf.contrib.seq2seq.sequence_loss(
				logits=self.logit,
				targets=self.Y,
				weights=self.mask)

	def build_optimizer(self):
		optimizer = tf.train.AdamOptimizer(self.lr)
		# self.train_op = optimizer.minimize(self.loss)
		gradients, variables = zip(*optimizer.compute_gradients(self.loss))
		gradients, _ = tf.clip_by_global_norm(gradients, 5.0)
		self.train_op = optimizer.apply_gradients(zip(gradients, variables))

	def train(self, sess, X, Y, mask, batch_size, sp):
		sequence_length = [self.sequence_length]*batch_size
		l, _ = sess.run([self.loss, self.train_op],
					feed_dict={self.X:X, self.Y:Y,
					self.len:sequence_length, self.sp:sp,
					self.batch_size:batch_size, self.dropout:0.1,
					self.mask: mask,
					})
		return l

	def test(self, sess, X, batch):
		Y = np.copy(X)
		slen = [self.sequence_length]*batch
		cur = sess.run(self.logit, feed_dict={self.X:X, self.Y:Y,
						self.len:slen, self.sp:0.8,
						self.batch_size:batch, self.dropout:0, })
		# cur = sess.run(self.infer_outputs, feed_dict={
		# 	self.X:X,self.batch_size:1, self.dropout:0, })
		return cur

	def build_single_cell(self, x):
		cell = rnn.LSTMCell(x, use_peepholes=True, initializer=tf.keras.initializers.glorot_normal(seed=7122))
		cell = rnn.DropoutWrapper(cell,
			input_keep_prob=1-self.dropout,
			output_keep_prob=1-self.dropout,
			state_keep_prob=1-self.dropout,
		)
		return cell

	def save(self, sess, path):
		saver = tf.train.Saver()
		saver.save(sess, save_path=path)

	def restore(self, sess, path):
		saver = tf.train.Saver()
		saver.restore(sess, save_path=path)


from biscuit.biscuit import runGame as biscuit
from memoryGame.memory_game import runGame as memory
from segmentation.coloriage import runGame as coloriage
from soleil.soleil  import runGame as soleil
from piano.glass import runGame as piano
import utils as animation
import cv2
import pygame
# lancer la sequence intro du jeu
# lancer le jeu sur lequel on cliquer

mousePos = (-1,-1)

def introDuJeu():
    global mousePos
    pygame.mixer.init()
    musicIntro = pygame.mixer.Sound('animation/intro.mp3')
    musicIntro.play()
    musicAmbiance = pygame.mixer.Sound('biscuit/assets/UnfinishedBusiness.mp3')
    action = animation.afficher_intro()
    if action == "Quitter":
        return
    musicIntro.stop()
    musicAmbiance.play()
    play = True
    while play:
        n = get_game()
        mousePos = (-1,-1)
        if n == -1:
            return
        play = lancer_le_jeu(n)
    return

def get_mouse_pos(event,x,y,flags,param):
    global mousePos
    if event == cv2.EVENT_LBUTTONDOWN:
        mousePos = (x,y)

def get_game():
    global mousePos
    mousePos = (-1,-1)
    menu = cv2.imread("animation/intro/introduction539.png")
    cv2.imshow("jeu", menu)
    while True:
        cv2.setMouseCallback('jeu',get_mouse_pos)
        key = cv2.waitKey(1)
        if mousePos[0] != -1:
            if mousePos[1] < 600:
                if mousePos[0] < 640:
                    print(1)
                    return 1
                elif mousePos[0] < 1280:
                    print(2)
                    return 2
                elif mousePos[0] < 1920:
                    print(3)
                    return 3
            elif mousePos[1]<1080:
                if mousePos[0] < 640:
                    print(4)
                    return 4
                elif mousePos[0] < 1280:
                    print(5)
                    return 5
                elif mousePos[0] < 1920:
                    return -1
        if key == ord('&') :
            return 1
        if key == ord('é') :
            return 2
        if key == ord('"') :
            return 3
        if key == ord("'") :
            return 4
        if key == ord('('):
            return 5
        if key == ord('q') or key == ord('§'):
            return -1

def lancer_le_jeu(n):
    if n == 1:
        action = soleil()
    elif n == 2:
        action = biscuit()
    elif n== 3 :
        action = memory()
    elif n== 4:
        action = coloriage()
    elif n== 5 :
        action = piano()
    else :
        return False

    return action

introDuJeu()


from time import time
from requests import Session as BaseSession
from id3.cache import LRUDictCache

class Session(BaseSession):
    """
    A normal requests.Session object which has GET requests cached for a specified duration.
    """

    def __init__(self, cache_duration: float = 3600) -> None:
        super().__init__()
        self.cache_duration = float(cache_duration)
        self.cache = LRUDictCache()

    def request(self, method: str, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=None, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None):
        method = method.upper()
        cache_key = f"{url}{params}{headers}{cookies}{files}{auth}"
        try:
            if method == "GET" and cache_key in self.cache:
                cache_item = self.cache[cache_key]
                if time() - cache_item["time"] <= self.cache_duration:
                    return cache_item["response"]
                self.cache.pop(cache_key, None)
        except Exception:
            pass
        r = super().request(method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
        if method == "GET":
            self.cache[cache_key] = {"response": r, "time": time()}
        return r


#!/usr/bin/env python
# coding: utf-8

#import and create spark session
from pyspark.sql import SparkSession
import pyspark.sql.functions as f

spark = SparkSession.builder.master("local[*]").appName("BaseisII").config("spark.jars", "spark-measure_2.12-0.17.jar").getOrCreate()

#import csv
movies = spark.read.csv("/home/administrator/Downloads/movielens/movie.csv",inferSchema =True, header=True)
ratings = spark.read.csv("/home/administrator/Downloads/movielens/rating.csv",inferSchema =True, header=True)
tags = spark.read.csv("/home/administrator/Downloads/movielens/tag.csv",inferSchema =True, header=True)

#erotima 9
ratings.alias("ra1").join(ratings.alias("ra2"), on='timestamp')\
                    .where("ra1.timestamp == ra2.timestamp") \
                    .where("ra1.movieId == ra2.movieId")\
                    .where("ra1.userId != ra2.userId")\
                    .select(f.col('ra1.userId').alias('id1')).groupBy().count().show()

spark.stop()


import requests
import tempfile
import json
from astropy.io import fits

from .utilities import Filter

class ArchiveApi(object):
    def __init__(self, api_key=None, observer_id=None):
        """Bayfordbury Archive API layer. See documentation here: https://observatory.herts.ac.uk/wiki/API

        api_key, str : your API key

        observer_id, int : your observer ID
        """

        assert api_key is not None, "You need an API key to search and download from Bayfordbury servers. Get one from https://observatory.herts.ac.uk/telescopes/myaccount.php"
        assert observer_id is not None, "You need an Observer ID to search and download from Bayfordbury servers. You can find it at https://observatory.herts.ac.uk/telescopes/myaccount.php"

        self.api_key = api_key
        self.observer_id = observer_id

        self._search_url   = "https://observatory.herts.ac.uk/api/imagesearch.php"
        self._download_url = "https://observatory.herts.ac.uk/api/getfit.php"

    def search(self, ra, dec, radius, minjd=None, maxjd=None, minexp=None, maxexp=None, solved=False, mine=False):
        """Search for images in archive around coordinates, within a certain radius.

        ra, float       : right ascention of the search center, decimal degrees, required.

        dec, float      : declination of the search center, decimal degrees, required.

        radius, float   : search radius, decimal degrees, required.

        minjd, float    : earliest Julian date of the observation, optional.

        maxjd, float    : latest Julian date of the observation, optional.

        minexp, float   : shortest exposure time of the observation, seconds, optional.

        maxexp, float   : longest exposure time of the observation, seconds, optional.

        solved, boolean : must be plate solved, optional.

        mine, boolean   : return only your images.
        """
        assert ra is not None, "ra parameter is required."
        assert dec is not None, "dec parameter is required."
        assert radius is not None, "radius parameter is required."

        # construct a query parameters dictionary
        params = { # required parameters and those with a default value
            "key"    : self.api_key,
            "id"     : self.observer_id,
            "ra"     : ra,
            "dec"    : dec,
            "dist"   : radius,
            "solved" : solved,
            "mine"   : mine
        }

        if minjd is not None:
            params["minjd"] = minjd

        if maxjd is not None:
            params["maxjd"] = maxjd

        if minexp is not None:
            params["minexp"] = minexp

        if maxexp is not None:
            params["maxexp"] = maxexp

        # send the request
        response = requests.get(self._search_url, params)
        self._handle_bad_response(response)

        return self._unpack_search_response(response.content)

    def download(self, image_id, saveto=None):
        """
        Download an image from Bayfordbury Archive.

        image_id, int : database ID in the archive.

        saveto, str : path to save the .fits file to. If None, creates a temporary file (in /tmp if you're using Linux)

        Returns an Astropy FITS image.
        """
        assert isinstance(image_id, int), "image_id must be an integer number."

        # construct a query parameters dictionary
        params = { # required parameters and those with a default value
            "key"    : self.api_key,
            "id"     : self.observer_id,
            "dbid"   : image_id
        }

        # send the request
        response = requests.get(self._download_url, params, stream=True)
        self._handle_bad_response(response)

        # create the file handle
        file_handle = tempfile.NamedTemporaryFile(mode="wb") if saveto is None else open(saveto, "wb")
        # write the bytes to the new file
        with file_handle as handle:
            for block in response.iter_content(1024):
                handle.write(block)
            file_path = file_handle.name if saveto is None else saveto
            file_instance = fits.open(file_path, ignore_missing_end=True, cache=False)

        return file_instance

    def _handle_bad_response(self, response):
        """
        Raises:
            HTTPStatus errors if any.
            PermissionError if credentials are wrong.
            FileNotFoundError if image ID is not in the Bayfordbury archive.
        """
        response.raise_for_status()
        # wrap this in try/catch in case the response is not actual JSON (could be file data, for instance)
        try:
            response_dictionary = json.loads(response.content)
        except:
            return

        if "Exception" in response_dictionary and response_dictionary["Exception"] == True:
            if "verification_failed" in response_dictionary["ExceptionReason"]:
                raise PermissionError("Verification failed for the <observer_id> and <api_key> you provided.")
            elif "image_id_not_found" in response_dictionary["ExceptionReason"]:
                raise FileNotFoundError("File with a supplie ID not found in Bayfordbury archive.")

    def _unpack_search_response(self, response):
        """
        Converts json returned by the server's 'search' API to a list of dictionaries.

        response, byte array : server response

        Returns a list of images found.
        """
        column_names = ["id", "ra", "dec", "dist", "exp", "tel", "bin", "jd", "filter"]
        response_dictionary = json.loads(response)

        if "Exception" in response_dictionary and response_dictionary["Exception"] == True:
            if "no_matching_images" in response_dictionary["ExceptionReason"]:
                return []
            else:
                raise NotImplementedError("Server returned an error <{}>, but it was not handled here ¯\_(ツ)_/¯ .".format(response_dictionary["ExceptionReason"]))

        return [value for key, value in response_dictionary["images"].items()]


# this program resize a RGB image to 160*320 in shape

import cv2
import numpy as np
img = cv2.imread('images.jpeg')
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv2.resize(img,(320, 160), interpolation = cv2.INTER_CUBIC)
cv2.imwrite("road.jpg", res)


# 파이썬 알고리즘 인터뷰 6장 3번문제
# https://leetcode.com/problems/reorder-data-in-log-files/

class Solution:

    # My solution
    def reorderLogFiles(self, logs):

        str_list = []
        num_list = []

        for log in logs:
            alnum = ''.join(log.split()[1:])
            if alnum.isalpha():
                str_list.append(log)
            else:
                num_list.append(log)

        # key 안에 정렬 순서 2가지 두는 법 까먹어서 교재 봐버림. 튜플로 하는 거였음
        return sorted(str_list, key = lambda x: (x.split()[1:], x.split()[0])) + num_list

    # Solution
    def solution(self, logs):
        letters, digits = [], []
        for log in logs:
            if log.split()[1].isdigit():
                digits.append(log)
            else:
                letters.append(log)

        letters.sort(key = lambda x: (x.split()[1:], x.split()[0]))
        return letters + digits