from omegaconf import OmegaConf
import argparse

def parser():
    parser = argparse.ArgumentParser(description='Time-series representation framework')
    parser.add_argument('--model_name', type=str, help='model name for experiments')
    parser.add_argument('--default_cfg', type=str, help='configuration for default setting')
    parser.add_argument('--model_cfg', type=str, help='configuration for model')
    parser.add_argument(
        "opts",
        help="Modify config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load configs
    cfg = OmegaConf.load(args.default_cfg)

    cfg = OmegaConf.merge(cfg, {'MODEL':{'modelname' : args.model_name}})

    if args.model_cfg:
        model_cfg = OmegaConf.load(args.model_cfg)
        modelname = cfg.MODEL.modelname

        # Check if the modelname is in the model_config
        if modelname in model_cfg:
            model_setting_conf = OmegaConf.create(model_cfg[modelname])
            # Merge the specific model config with the default config
            cfg = OmegaConf.merge(cfg, {'MODELSETTING' : model_setting_conf})
        else:
            print(f"Model '{modelname}' not found in the model_config.")
            return None

    # update cfg
    for k, v in zip(args.opts[0::2], args.opts[1::2]):
        try:
            # types except for int, float, and str
            OmegaConf.update(cfg, k, eval(v), merge=True)
        except:
            OmegaConf.update(cfg, k, v, merge=True)

    return cfg


import os

from conan.internal.conan_app import ConanApp
from conan.internal.integrity_check import IntegrityChecker
from conans.errors import ConanException
from conans.model.package_ref import PkgReference
from conans.model.recipe_ref import RecipeReference
from conans.util.files import rmdir

class CacheAPI:

    def __init__(self, conan_api):
        self.conan_api = conan_api

    def export_path(self, ref: RecipeReference):
        app = ConanApp(self.conan_api.cache_folder)
        ref.revision = None if ref.revision == "latest" else ref.revision
        ref_layout = app.cache.recipe_layout(ref)
        return ref_layout.export()

    def recipe_metadata_path(self, ref: RecipeReference):
        app = ConanApp(self.conan_api.cache_folder)
        ref = _resolve_latest_ref(app, ref)
        ref_layout = app.cache.recipe_layout(ref)
        return ref_layout.metadata()

    def export_source_path(self, ref: RecipeReference):
        app = ConanApp(self.conan_api.cache_folder)
        ref.revision = None if ref.revision == "latest" else ref.revision
        ref_layout = app.cache.recipe_layout(ref)
        return ref_layout.export_sources()

    def source_path(self, ref: RecipeReference):
        app = ConanApp(self.conan_api.cache_folder)
        ref.revision = None if ref.revision == "latest" else ref.revision
        ref_layout = app.cache.recipe_layout(ref)
        return ref_layout.source()

    def build_path(self, pref: PkgReference):
        app = ConanApp(self.conan_api.cache_folder)
        pref = _resolve_latest_pref(app, pref)
        ref_layout = app.cache.pkg_layout(pref)
        return ref_layout.build()

    def package_metadata_path(self, pref: PkgReference):
        app = ConanApp(self.conan_api.cache_folder)
        pref = _resolve_latest_pref(app, pref)
        ref_layout = app.cache.pkg_layout(pref)
        return ref_layout.metadata()

    def package_path(self, pref: PkgReference):
        app = ConanApp(self.conan_api.cache_folder)
        pref = _resolve_latest_pref(app, pref)
        ref_layout = app.cache.pkg_layout(pref)
        return ref_layout.package()

    def check_integrity(self, package_list):
        """Check if the recipes and packages are corrupted (it will raise a ConanExcepcion)"""
        app = ConanApp(self.conan_api.cache_folder)
        checker = IntegrityChecker(app)
        checker.check(package_list)

    def clean(self, package_list, source=True, build=True, download=True, temp=True):
        """
        Remove non critical folders from the cache, like source, build and download (.tgz store)
        folders.
        :param package_list: the package lists that should be cleaned
        :param source: boolean, remove the "source" folder if True
        :param build: boolean, remove the "build" folder if True
        :param download: boolen, remove the "download (.tgz)" folder if True
        :param temp: boolean, remove the temporary folders
        :return:
        """

        app = ConanApp(self.conan_api.cache_folder)
        if temp:
            rmdir(app.cache.temp_folder)
            # Clean those build folders that didn't succeed to create a package and wont be in DB
            builds_folder = app.cache.builds_folder
            if os.path.isdir(builds_folder):
                for subdir in os.listdir(builds_folder):
                    folder = os.path.join(builds_folder, subdir)
                    manifest = os.path.join(folder, "p", "conanmanifest.txt")
                    info = os.path.join(folder, "p", "conaninfo.txt")
                    if not os.path.exists(manifest) or not os.path.exists(info):
                        rmdir(folder)

        for ref, ref_bundle in package_list.refs():
            ref_layout = app.cache.recipe_layout(ref)
            if source:
                rmdir(ref_layout.source())
            if download:
                rmdir(ref_layout.download_export())
            for pref, _ in package_list.prefs(ref, ref_bundle):
                pref_layout = app.cache.pkg_layout(pref)
                if build:
                    rmdir(pref_layout.build())
                if download:
                    rmdir(pref_layout.download_package())

def _resolve_latest_ref(app, ref):
    if ref.revision is None or ref.revision == "latest":
        ref.revision = None
        result = app.cache.get_latest_recipe_reference(ref)
        if result is None:
            raise ConanException(f"'{ref}' not found in cache")
        ref = result
    return ref

def _resolve_latest_pref(app, pref):
    pref.ref = _resolve_latest_ref(app, pref.ref)
    if pref.revision is None or pref.revision == "latest":
        pref.revision = None
        result = app.cache.get_latest_package_reference(pref)
        if result is None:
            raise ConanException(f"'{pref.repr_notime()}' not found in cache")
        pref = result
    return pref


#!/usr/bin/python3
"""This is the borrower class"""
from models.base_model import BaseModel, Base
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship

class Lender(BaseModel, Base):
    """This is the class for Lender
    Attributes:
    loan_size: amount looking to loan
    loan_duration: length of loan
    l_m_r_t: lowest interest rate WTA
    """
    __tablename__ = 'lenders'
    loan_size = Column(Integer, nullable=True)
    loan_duration = Column(Integer, nullable=True)
    interest = Column(Integer, nullable=False)
    email = Column(String(60), nullable=False)
    user_id = Column(String(60), ForeignKey('users.id'), nullable=False)
    user = relationship("User", back_populates="lender")


import numpy as np
import tensorflow as tf
import h5py
import sys
from scatternet.utils.data_processing import format_galaxies, check_data_processing
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils import class_weight
import random
if sys.version_info[0] == 2:
    import cPickle as pickle
else:
    import pickle

def shuffle(x,y):
    temp = list(zip(x, y))
    random.shuffle(temp)
    x,y = zip(*temp)
    x = np.array(x)
    y = np.array(y)
    return x,y

class DataSet():
    def __init__(self, add_channel = False):

        self.modified = False
        self.load_data()
        self.keys, self._unique_indices = np.unique(self.y_train, return_index = True)
        if add_channel:
            self.add_channel()
        else:
            (self._n_train, self._dim_x, self._dim_y), self.n_classes = self.x_train.shape, self.keys.size
        self._x_test_rot = np.rot90(self.x_test,axes=(1, 2))
        self._encoder = LabelBinarizer()
        self._encoder.fit(self.y_train)

    def write(self,outname):
        with open(outname, 'wb') as f:
            np.save(f, self.x_train)
            np.save(f, self.y_train)
            np.save(f, self.x_val)
            np.save(f, self.y_val)
            np.save(f, self.x_test)
            np.save(f, self.y_test)
            np.save(f,self.label_list)

    def save_original(self):
        self.__x_train    = np.copy(self._x_train)
        self.__x_test     = np.copy(self._x_test)
        self.__x_test_rot = np.copy(self._x_test_rot)
        self.__x_val      = np.copy(self._x_val)

    def restore_original(self):
        self._x_train    = self.__x_train
        self._x_test     = self.__x_test
        self._x_test_rot = self.__x_test_rot
        self._x_val      = self.__x_val

    def add_channel(self):
        self._x_train = self._x_train.reshape(*self._x_train.shape,1)
        self._x_test  = self._x_test.reshape( *self._x_test.shape,1)
        self._x_val   = self._x_val.reshape(  *self._x_val.shape,1)

        (self._n_train, self._dim_x, self._dim_y,__), self.n_classes = self.x_train.shape, self.keys.size

    def load_data(self):
        raise NotImplementedError

    def preprocess(self,f):
        self._x_train    = f(self._x_train)
        self._x_test     = f(self._x_test)
        self._x_test_rot = f(self._x_test_rot)
        self._x_val      = f(self._x_val)

    def info(self):

        for i, k in enumerate(self.keys):
            n_train = len(np.where(self.y_train == i)[0])
            n_test = len(np.where(self.y_test == i)[0])
            n_val = len(np.where(self.y_val == i)[0])
            print(self.label_list[i], "n_train =", n_train, "n_val =", n_val, "n_test =", n_test)

    def truncate_train(self,n, balance = False, randomize = False):

        if randomize:
            self._x_train, self._y_train = shuffle(self._x_train, self._y_train)

        if balance:
            n_per_class = int(n / self.n_classes)
            indices = []
            for i, k in enumerate(self.keys):
                class_indices = np.where(self.y_train == i)[0][:n_per_class]
                indices = np.append(indices,class_indices)
            indices = indices.astype(np.intc)
            self._x_train = self._x_train[indices]
            self._y_train = self._y_train[indices]

        else:
            self._x_train = self._x_train[:n]
            self._y_train = self._y_train[:n]

    def get_example_classes(self,n=1):
        indices = []
        for i, k in enumerate(self.keys):
            class_indices = np.where(self.y_train == i)[0][:n]
            indices = np.append(indices,class_indices)
        indices = indices.astype(np.intc)
        return (self._x_train[indices], self._y_train[indices])

    def augment(self):

        print('Augment',self._x_train.shape)

        x_train_flip = np.flip(self.x_train,axis=1)
        x_train = np.vstack( [self._x_train, x_train_flip])
        y_train = np.append(self._y_train,self._y_train)

        for k in range(1,4):
            x_train_rotated         = np.rot90(self._x_train,axes=(1, 2),k=k)
            x_train_rotated_flipped = np.rot90(x_train_flip,axes=(1, 2),k=k)

            x_train = np.vstack( [x_train, x_train_rotated])
            x_train = np.vstack( [x_train, x_train_rotated_flipped])
            y_train = np.append(y_train,self._y_train)
            y_train = np.append(y_train,self._y_train)

        self._x_train, self._y_train = shuffle(x_train, y_train)

        print(self._x_train.shape)

    @property
    def data_shape(self):
        return self._x_test_rot.shape[1:]

    def encode(self, y):
        return self._encoder.transform(y)

    def decode(self, y):
        return self._encoder.inverse_transform(y)

    @property
    def class_weights(self):
        return class_weight.compute_class_weight(class_weight='balanced',
                                                 classes = np.unique(self.y_train),
                                                 y=self.y_train)

    @property
    def sample_weights(self):
        return class_weight.compute_sample_weight('balanced',self.y_train)

    @property
    def label_list(self):
        raise NotImplementedError

    @property
    def unique_indices(self):
        return self._unique_indices

    @property
    def dim_x(self):
        return self._dim_x

    @property
    def dim_y(self):
        return self._dim_y

    @property
    def x_train(self):
        return self._x_train

    @property
    def y_train(self):
        return self._y_train

    @property
    def n_train(self):
        return self._n_train

    @property
    def x_val(self):
        return self._x_val

    @property
    def y_val(self):
        return self._y_val

    @property
    def x_test(self):
        return self._x_test

    @property
    def x_test_rot(self):
        return self._x_test_rot

    @property
    def y_test(self):
        return self._y_test

class DataFromFile(DataSet):
    def __init__(self, infilename, add_channel = False):
        self.infilename = infilename
    def load_data(self):
        with open(self.infilename, 'rb') as f:
            self.x_train = np.load(f)
            self.y_train = np.load(f)
            self.x_val   = np.load(f)
            self.y_val   = np.load(f)
            self.x_test  = np.load(f)
            self.y_test  = np.load(f)
            self._label_list= np.load(f)
    @property
    def label_list(self):
        return self._label_list

class RadioGalaxies(DataSet):
    '''
    This Radio Galaxy Dataset is a collection and combination of several catalogues
    using the FIRST radio galaxy survey [[1]](https://ui.adsabs.harvard.edu/abs/1995ApJ...450..559B/abstract).
    https://www.sciencedirect.com/science/article/pii/S2352340923000926
    '''

    def load_set(self,F, key, x,y):
        data_entry = F[key + "/Img"]
        label_entry = F[key + "/Label_literature"]
        d = np.array(data_entry)
        y.append(np.array(label_entry))
        x.append(d)

    def format(self,x,y, trimby = 120):

        x,y = shuffle(x,y)
        x = x.astype(np.intc)
        y = y.astype(np.intc)
        x = x[:,trimby:-trimby,trimby:-trimby]/255.

        return x,y

    def load_data(self):
        self._x_train, self._y_train, self._x_val,  self._y_val, self._x_test,  self._y_test  = [], [], [], [], [], []
        with h5py.File('data/galaxy_data_h5.h5', 'r') as F:
            for key in F.keys():
                split_entry = F[key + "/Split_literature"].asstr()[()]

                if split_entry == "train":
                    self.load_set(F,key,self._x_train, self._y_train)
                elif split_entry == "test":
                    self.load_set(F,key,self._x_test, self._y_test)
                elif split_entry == "valid":
                    self.load_set(F,key,self._x_val, self._y_val)

        self._x_train, self._y_train = self.format(self._x_train, self._y_train)
        self._x_test,  self._y_test  = self.format(self._x_test,  self._y_test)
        self._x_val,   self._y_val   = self.format(self._x_val,   self._y_val)

    @property
    def label_list(self):
        return ["FRI", "FRII", "Compact", "Bent"]

class Galaxy10(DataSet):
    '''
    Galaxy morphology classification on the Galaxy10 DECals Dataset:
    https://astronn.readthedocs.io/en/latest/galaxy10.html
    Follow the instructions at the link above to download Galaxy10.h5
    '''

    def load_data(self):

        with h5py.File('data/Galaxy10.h5', 'r') as F:
            images = np.array(F['images'])
            labels = np.array(F['ans'])
        labels = labels.astype(np.intc)
        images = images.astype(np.intc)

        split1 = 1000#int(len(labels)*9/10.)
        split2 = split1+200#-1
        split3 = split2+200

        self._x_train, self._y_train = images[0:split1,:,:], labels[0:split1]
        self._x_test,  self._y_test = images[split1:split2,:,:], labels[split1:split2]
        self._x_val,  self._y_val = images[split2:split3,:,:], labels[split2:split3]

        self._x_train = format_galaxies(self._x_train, threshold = 0.1, min_size = 100, margin = 2)
        self._x_test  = format_galaxies(self._x_test,  threshold = 0.1, min_size = 100, margin = 2)
        self._x_val   = format_galaxies(self._x_val,   threshold = 0.1, min_size = 100, margin = 2)

    @property
    def label_list(self):
        return ['Disk, Face-on, No Spiral', 'Smooth, Completely round', 'Smooth, in-between round', 'Smooth, Cigar shaped', 'Disk, Edge-on, Rounded Bulge', 'Disk, Edge-on, Boxy Bulge',
                'Disk, Edge-on, No Bulge','Disk, Face-on, Tight Spiral', 'Disk, Face-on, Medium Spiral', 'Disk, Face-on, Loose Spiral']

class MINST(DataSet):
    '''
    Classic MINST letters dataset
    '''

    def load_data(self):

        (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
        x_train, x_test = x_train / 255., x_test / 255.

        self._x_train = x_train[0:10000]
        self._y_train = y_train[0:10000]

        self._x_test = x_test[0:1000]
        self._y_test = y_test[0:1000]

        self._x_val = x_test[1000:2000]
        self._y_val = y_test[1000:2000]

    @property
    def label_list(self):
        return [0,1,2,3,4,5,6,7,8,9]

class Mirabest(DataSet):
    '''
    Classic MINST letters dataset
    '''

    def load_data(self):

        filepath = './data/Mirabest/batches/'

        with open(filepath + 'batches.meta', 'rb') as infile:
            if sys.version_info[0] == 2:
                data = pickle.load(infile)
            else:
                data = pickle.load(infile, encoding='latin1')
            self.classes = data['label_names']
        self.class_to_idx = {_class: i for i, _class in enumerate(self.classes)}
        self.idx_to_class = {i: _class for i, _class in enumerate(self.classes)}
        self._x_test,  self._y_test = self.load_file(filepath,['test_batch'])
        self._x_val,   self._y_val = self.load_file(filepath,['data_batch_1'])
        self._x_train, self._y_train = self.load_file(filepath,["data_batch_{0}".format(i) for i in range(2,8)])

    def load_file(self,file_dir, file_list):
        data = []
        targets = []
        for file_path in file_list:
            with open(file_dir+ file_path, 'rb') as f:
                if sys.version_info[0] == 2:
                    entry = pickle.load(f)
                else:
                    entry = pickle.load(f, encoding='latin1')

                data.append(entry['data'])
                if 'labels' in entry:
                    targets.extend(entry['labels'])
                else:
                    targets.extend(entry['fine_labels'])
        data = np.vstack(data).reshape(-1, 150, 150)
        targets = np.array(targets)
        #data = data.transpose((0, 2, 3, 1))
        return data,targets

    def remove_uncertain_classes(self):
        uncertain_classes = [3,4,7,9]
        certain_classes = [0, 1,2,5,6,8]

        def remove_entries(exclude_list, x, y):
            targets = np.array(y)
            exclude = np.array(exclude_list).reshape(1, -1)
            exclude_mask = ~(targets.reshape(-1, 1) == exclude).any(axis=1)
            #h1 = np.array(h1_list).reshape(1, -1)
            #h2 = np.array(h2_list).reshape(1, -1)
            #h1_mask = (targets.reshape(-1, 1) == h1).any(axis=1)
            #h2_mask = (targets.reshape(-1, 1) == h2).any(axis=1)
            #targets[h1_mask] = 0 # set all FRI to Class~0
            #targets[h2_mask] = 1 # set all FRII to Class~1
            x = x[exclude_mask]
            y = targets[exclude_mask]
            return x,y

        self._x_test, self._y_test  = remove_entries(uncertain_classes,self._x_test, self._y_test)
        self._x_train,self._y_train = remove_entries(uncertain_classes,self._x_train,self._y_train)
        self._x_val,  self._y_val   = remove_entries(uncertain_classes,self._x_val,self._y_val)

        self.keys, self._unique_indices = np.unique(self.y_train, return_index = True)
        self.n_classes = self.keys.size
        self._encoder.fit(self.y_train)

    @property
    def label_list(self):
        return [self.idx_to_class[i] for i in self.keys]

class MirabestBinary(Mirabest):
    def load_data(self):
        super().load_data()

        def merge(x, y):
            targets = np.array(y)
            fr1_classes = [0,1,2]
            fr2_classes=[5,6]
            exclude_list=[3,4,7,8,9]
            exclude = np.array(exclude_list).reshape(1, -1)
            exclude_mask = ~(targets.reshape(-1, 1) == exclude).any(axis=1)
            h1 = np.array(fr1_classes).reshape(1, -1)
            h2 = np.array(fr2_classes).reshape(1, -1)
            h1_mask = (y.reshape(-1, 1) == h1).any(axis=1)
            h2_mask = (y.reshape(-1, 1) == h2).any(axis=1)

            targets[h1_mask] = 0 # set all FRI to Class~0
            targets[h2_mask] = 1 # set all FRII to Class~1
            x = x[exclude_mask]
            y = targets[exclude_mask]
            return x,y
        self._x_test, self._y_test  = merge(self._x_test, self._y_test)
        self._x_train,self._y_train = merge(self._x_train,self._y_train)
        self._x_val,  self._y_val   = merge(self._x_val,self._y_val)

        #self.keys, self._unique_indices = np.unique(self.y_train, return_index = True)
        #self.n_classes = self.keys.size
        #self._encoder.fit(self.y_train)
    @property
    def label_list(self):
        return ["FRI","FRII"]


from django.urls import path
from . import views

urlpatterns =[
    path("",views.index, name="index"),
    path('list/',views.repolist,name='repos'),
    path('logout/', views.logout_page, name='logout'),
    path('<name>/repo_details/', views.repo_details, name='repos_details'),
    path('<name>/data/', views.data, name='data'),
    path('<name>/create_branch/', views.create_branch, name='create_branch'),
    path("<name>/file/", views.file, name="file"),
    path("<name>/file_details/", views.file_details, name='file_details'),
    path("<name>/pull_request/", views.pull_req, name="pull_req"),
    path("<name>/pull_details/", views.pull_details, name="pull_details"),
    path("<name>/merging/", views.merging, name="merging"),
    path("<name>/merge/", views.merge, name="merge"),
    path("<name>/del_branch/", views.del_branch, name="del_branch"),
    path("<name>/delete/", views.delete, name="delete"),
]


#!/usr/bin/python3
"""
Creates a BaseModel class that defines attributes and
methods for other classes
"""

import datetime
import uuid
import json
import models

class BaseModel:
    """Creates base model for other classes"""

    def __init__(self, *args, **kwargs):
        """class constructor"""
        if kwargs:
            for key, value in kwargs.items():
                if key == "__class__":
                    pass
                elif "updated_at" == key:
                    self.updated_at = datetime.datetime.strptime(
                        kwargs["updated_at"], "%Y-%m-%dT%H:%M:%S.%f")
                elif "created_at" == key:
                    self.created_at = datetime.datetime.strptime(
                        kwargs["created_at"], "%Y-%m-%dT%H:%M:%S.%f")
                else:
                    setattr(self, key, value)
        else:
            self.id = str(uuid.uuid4())
            self.created_at = datetime.datetime.now()
            self.updated_at = datetime.datetime.now()
            models.storage.new(self)

    def __str__(self):
        """string representation of the base class"""
        return "[{}] ({}) {}".format(
            self.__class__.__name__, self.id, self.__dict__)

    def save(self):
        """updates the updated_at instance attribute to current time"""
        self.updated_at = datetime.datetime.now()
        models.storage.save()

    def to_dict(self):
        """returns a dictionary containing keys/values of the instance"""
        dictionary = {}
        dictionary['__class__'] = self.__class__.__name__

        for key, value in self.__dict__.items():
            if key == "created_at" or key == "updated_at":
                dictionary[key] = value.isoformat()
            else:
                dictionary[key] = value
        return dictionary


from typing import Iterable, Mapping, Optional, Tuple

from django.http import Http404

from sentry.models import (
    Identity,
    IdentityProvider,
    IdentityStatus,
    Integration,
    Organization,
    User,
)
from sentry.types.integrations import EXTERNAL_PROVIDERS, ExternalProviders

def get_identity_or_404(
    provider: ExternalProviders,
    user: User,
    integration_id: int,
    organization_id: Optional[int] = None,
) -> Tuple[Organization, Integration, IdentityProvider]:
    """For endpoints, short-circuit with a 404 if we cannot find everything we need."""
    try:
        integration = Integration.objects.get(id=integration_id)
        idp = IdentityProvider.objects.get(
            external_id=integration.external_id, type=EXTERNAL_PROVIDERS[provider]
        )
        organization_filters = dict(
            member_set__user=user,
            organizationintegration__integration=integration,
        )
        # If provided, ensure organization_id is valid.
        if organization_id:
            organization_filters.update(dict(id=organization_id))
        organization = Organization.objects.filter(**organization_filters)[0]
    except Exception:
        raise Http404
    return organization, integration, idp

def get_identities_by_user(idp: IdentityProvider, users: Iterable[User]) -> Mapping[User, Identity]:
    identity_models = Identity.objects.filter(
        idp=idp,
        user__in=users,
        status=IdentityStatus.VALID,
    )
    return {identity.user: identity for identity in identity_models}


#!flask/bin/python
from flask import Flask
from flask import jsonify
from flask import Flask, abort, request
import logging
import json
import os
import sys
import psycopg2
from pprint import pprint
import  plpygis
import simplejson
import psycopg2.extras

app = Flask(__name__)

connection = psycopg2.connect("dbname='projecty' user='vorl' host='localhost' password='' port='5432'") #Vlad
connection.set_session(autocommit=True)
logging.basicConfig(level=logging.INFO)

# postgres
########################################################################3
# update user
########################################################################################
@app.route('/api/game/user', methods=['POST'])
def user_registration():
	try:
		datauser=request.get_json()

		logging.info('registering new user {}'.format(datauser))

		cursor = connection.cursor()

		#check if userid exist
		cursor.execute("SELECT * FROM userdata WHERE userid=%s;" , [datauser['userid'],])

		if cursor.fetchone()==None:
			i=0
			create_string = "INSERT INTO userdata("
			for key in datauser:
				if i==0:
					create_string = create_string+str(key)
					i=1
				else:
					create_string = create_string+","+str(key)
			i=0
			create_string = create_string+ ") VALUES("+"'"
			for key in datauser:
				if i==0:
					create_string = create_string+str(datauser[key])
					i=1
				else:
					create_string = create_string+"'"+","+"'"+str(datauser[key])
			create_string = create_string +"'"+ ")"
			cursor.execute(create_string)

		else:
			def type_format(v):
				if isinstance(v, float) or isinstance(v, int):
					return v
				else:
					return "'{}'".format(v)

			keys = [k for k in datauser.keys() if k != 'userid']
			sql = """update userdata set {} where userid = '{}'""".format(', '.join(map(lambda k: k + '=' + type_format(datauser[k]), keys)), datauser['userid'])
			print(sql)
			cursor.execute(sql)

	except Exception as e:
		print("Error [%r]" % (e))
		sys.exit(1)
	finally:
		if cursor:
			cursor.close()

	return ''

###################################################################################################
# sync user data
########################################################################################

@app.route('/api/game/user/<userid>', methods=[ 'GET'])
def user(userid):

	logging.info('requested data for user with ID {}'.format(userid))

	cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
	try:
		cursor.execute("SELECT * FROM userdata WHERE userid=%s" , [userid,])
		rows = cursor.fetchall()
		if not rows:
			name = 'Player 1'
			sql = """
			INSERT INTO userdata (userid, firstname)
			VALUES ('{userid}', '{firstname}')
			ON CONFLICT DO NOTHING
			""".format(userid=userid, firstname=name)

			cursor.execute(sql)

			return jsonify({'userid': userid, 'name': name})
		else:
			return jsonify({'userid': rows[0]['userid'], 'name': rows[0]['firstname']})

	except Exception as e:
		print("Error [%r]" % (e))
		sys.exit(1)
	finally:
		if cursor:
			cursor.close()

	return ''

###################################################################################################
# user current status
########################################################################################
@app.route('/api/game/user-score/<userid>', methods=[ 'GET'])
def user_info(userid):

	logging.info('requested scores of user with ID {}'.format(userid))

	global query_result
	cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
	try:

		cursor.execute("""
		SELECT scorecategory, sum(userscore) as score
				FROM 	totalgamescore
				WHERE  userid='{userid}'
				group by scorecategory""".format(userid=userid))

		rows = cursor.fetchall()
		response = {}
		for row in rows:
			response[row['scorecategory']] = row['score']
		return jsonify(response)

	except Exception as e:
		print(e)
		return '', 500
	finally:
		if cursor:
			cursor.close()

##########################################################################################################3
# location registration
########################################################################################
@app.route('/api/game/location/<userid>', methods=['POST'])
def location(userid):

	logging.info('New location for user with ID {} reported'.format(userid))

	cursor = connection.cursor()
	try:
		location_data=request.get_json()

		sql = """
		INSERT INTO templocation (userid, longtitude, latitude)
			VALUES ('{userid}', {longtitude}, {latitude})
			ON CONFLICT (userid) DO UPDATE
			  SET longtitude = excluded.longtitude,
				  latitude = excluded.latitude
		""".format(userid=userid, longtitude=location_data['longtitude'], latitude=location_data['latitude'])

		cursor.execute(sql)

	except Exception as e:
		print(e)
		return '', 500

	finally:
		if cursor:
			cursor.close()

	return '', 200

###################################################################################################
# pull list tokens around
########################################################################################
@app.route('/api/game/getlist/<userid>/<r>', methods=['GET'])
def getlist(userid,r):

	global query_result
	try:
		cursor = connection.cursor()

		cursor.execute("SELECT * FROM templocation WHERE userid=%s;" , [userid])

		query_result_location = [ dict(line)
			for line in [zip([ column[0]
				for column in cursor.description], row)
					for row in cursor.fetchall()] ]

		cursor.execute(
							"SELECT  obj.objectid, obj.objectcategory, pointlongtitude as longtitude, pointlatitude as latitude "
							"FROM 	objectlocation  as objloc, "
								"object as obj "
								"CROSS JOIN (SELECT ST_MakePoint(%s,%s)::geography AS ref_geom) AS r "
								"WHERE ST_DWithin(ST_MakePoint(pointlatitude, pointlongtitude)::geography, ref_geom, %s) "
								"and objloc.objectid=obj.objectid "
								"and obj.objectid not in (select totalgamescore.objectid from totalgamescore where userid=%s)" ,
			(query_result_location[0]['latitude'], query_result_location[0]['longtitude'],r, userid))

		query_result = [ dict(line)
			for line in [zip([ column[0]
				for column in cursor.description], row)
					for row in cursor.fetchall()] ]

	except Exception as e:
		print("Error [%r]" % (e))
		sys.exit(1)
	finally:
		if cursor:
			cursor.close()

	logging.info('Found {} objects around user with ID {}'.format(len(query_result), userid))

	return jsonify({'Token_List': query_result})

###################################################################################################
# collection acception
########################################################################################
@app.route('/api/game/collection_reg/<userid>', methods=['POST'])
def collection_reg(userid):

	global query_result
	cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
	try:
		data=request.get_json()

		objectid = data['objectid'].strip()

		logging.info('User with ID {} reported a collision with {}!'.format(userid, objectid))

		sql = """
		insert into totalgamescore(userid, userscore, scorecategory, eventtime, objectid)
		(select '{userid}', objectbasescore, objectcategory, current_timestamp, objectid from object where objectid = '{objectid}')
		ON CONFLICT DO NOTHING
		""".format(userid=userid, objectid=objectid)

		cursor.execute(sql)

		cursor.execute("""
		SELECT scorecategory, sum(userscore) as score
				FROM 	totalgamescore
				WHERE  userid='{userid}'
				group by scorecategory""".format(userid=userid))

		rows = cursor.fetchall()
		scores = {}
		for row in rows:
			scores[row['scorecategory']] = row['score']

		sql = """
		select segmentid, storyfile from segment s
			left join
			(select scorecategory, sum(userscore) as userscore
				from totalgamescore
				where userid = '{userid}'
				group by scorecategory) gs
			on s.segmentcategory = gs.scorecategory
			where segmentthreshold <= userscore
			and s.segmentid not in (select segmentid from usersegment where userid='{userid}')
			order by s.segmentid""".format(userid=userid)

		cursor.execute(sql)

		data = cursor.fetchall()
		segment_ids = list(map(lambda x: x[0], data))
		text = ' '.join(list(map(lambda x: x[1], data)))

		scores['story'] = text

		for segmentid in segment_ids:
			sql = """
			insert into usersegment(userid, segmentid) VALUES ('{userid}', '{segmentid}')
			ON CONFLICT DO NOTHING
			""".format(userid=userid, segmentid=segmentid)
			cursor.execute(sql)

		return jsonify(scores)

	except Exception as e:
		print(e)
		return '', 500
	finally:
		if cursor:
			cursor.close()

## Starts the server for serving Rest Services
if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False)


import unittest
from typing import List, Optional

from common import TreeNode, create_binary_tree

class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        return [] if not root else self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]

    def postorderTraversalIterative(self, root: Optional[TreeNode]) -> List[int]:
        res, stack = [], [(root, False)]
        while stack:
            node, visited = stack.pop()
            if node:
                if visited:
                    res.append(node.val)
                else:
                    stack.append((node, True))
                    stack.append((node.right, False))
                    stack.append((node.left, False))
        return res

class TestResult(unittest.TestCase):
    def run_test(self, data, expect):
        # result = Solution().postorderTraversal(create_binary_tree(data))
        result = Solution().postorderTraversalIterative(create_binary_tree(data))

        self.assertEqual(result, expect)

    def test_case1(self):
        data = [1, None, 2, 3]
        expect = [3, 2, 1]
        self.run_test(data, expect)

    def test_case2(self):
        data = []
        expect = []
        self.run_test(data, expect)

    def test_case3(self):
        data = [1]
        expect = [1]
        self.run_test(data, expect)

    def test_case4(self):
        data = [1, 4, 3, 2]
        expect = [2, 4, 3, 1]
        self.run_test(data, expect)

if __name__ == "__main__":
    unittest.main()


import json

from django.shortcuts import render
import execjs
from django.http import HttpResponse

def get_js():
    # f = open("D:/WorkSpace/MyWorkSpace/jsdemo/js/des_rsa.js",'r',encoding='UTF-8')
    f = open("static/decode.js", 'r', encoding='UTF-8')
    line = f.readline()
    htmlstr = ''
    while line:
        htmlstr = htmlstr + line
        line = f.readline()
    return htmlstr

def upload(request):
    jsstr = get_js()
    ctx = execjs.compile(jsstr)
    v = ctx.call('decodeMessage', request.GET['data'])

    print(v)
    return HttpResponse(json.dumps(v), content_type="application/json")
    # return render(request, 'index.html', content)


#!/usr/bin/env python3
from django.core.management.base import BaseCommand

from workshops.models import Tag

TAGS = [
    {
        'name': 'automated-email' ,
        'priority': 0,
        'details': 'Only for automated emails',
    },
    {
        'name': 'online',
        'priority': 7,
        'details': 'Events taking place entirely online.',
    },
    {
        'name': 'Instructor Training',
        'priority': 8,
        'details': 'Instructor Trainer',
    },
    {
        'name': 'cancelled',
        'priority': 9,
        'details': 'Events that were supposed to happen but due to some circumstances got cancelled',
    },
    {
        'name': 'unresponsive',
        'priority': 10,
        'details': "Events whose hosts and/or organizers aren't going to send data",
    },
    {
        'name': 'stalled',
        'priority': 11,
        'details': "Events with lost contact with the host or TTT events that aren't running.",
    },
    {
        'name': 'hackathon',
        'priority': 15,
        'details': 'Event is a hackathon',
    },
    {
        'name': 'CW',
        'priority': 16,
        'details': 'Curriculum Workshop',
    },
    {
        'name': 'HIC',
        'priority': 100,
        'details': 'Event being done in a High Income Country',
    },
    {
        'name': 'LMIC',
        'priority': 130,
        'details': 'Event occurring in a Low or Middle Income Country',
    },
    {
        'name': 'ECR',
        'priority': 140,
        'details': 'Event is a school for ECRs',
    },
    {
        'name': 'Data Steward',
        'priority': 110,
        'details': 'Event is a Data Steward training event',
    },

]

class Command(BaseCommand):

    help = "Setup tags according to what's desired"

    def handle(self, *args, **options):
        sords_tags_names = [sords_tag['name'] for sords_tag in TAGS]
        for tag in Tag.objects.all():
            if tag.name not in sords_tags_names:
                tag.delete()
            if tag.name in sords_tags_names:
                for entry in TAGS:
                    if entry['name'] == tag.name:
                        correct_tag = entry
                        tag.priority = correct_tag['priority']
                        tag.details = correct_tag['details']
                        tag.save()
        for tag_object in TAGS:
            obj, created = Tag.objects.get_or_create(**tag_object)
            if created:
                print(f"Created {obj.name}")
            if not created:
                print(f"{obj.name} already created, skipping.")


n, m, k, x = 4, 4, 1, 1
graph = [[],
         [2,3],
         [3,4],
         [],
         []]

# "모든 도로의 거리가 1" 이라는 조건 덕분에 BFS를 이용하여 간단히 해결
# 출발점으로 가까이 있는게 최단거리니까

from collections import deque

distance = [-1] * (n+1)
distance[x] = 0

q = deque([x])
while q:
    now = q.popleft()
    for next_node in graph[now]:
        if distance[next_node] == -1:
            distance[next_node] = distance[now] + 1
            q.append([next_node])

check = False
for i in range(distance):
    if i == k:
        print(i)
        check = True

if not check:
    print(-1)

[Coding Test] 이코테/기출문제/15 특정 거리의 도시 찾기.py

15 특정 거리의 도시 찾기.py


from django.contrib import admin
from django.shortcuts import reverse
from django.templatetags.static import static
from django.utils.html import format_html
from burgershop.apps.restaurants.models import Restaurant
from burgershop.apps.restaurants.admin import RestaurantMenuItemInline

from .models import Product, ProductCategory

class ProductsFilterByRestaurant(admin.SimpleListFilter):
    title = 'Доступны в Ресторане'
    parameter_name = 'restaurant_pk'

    def lookups(self, request, model_admin):
        queryset = Restaurant.objects.all()
        restaurants = {
            (
                str(restaurant.pk),
                restaurant.name,
            )
            for restaurant in queryset
        }
        return restaurants

    def queryset(self, request, queryset):
        restaurant_pk = self.value()
        if restaurant_pk:
            return queryset.filter(
                menu_items__restaurant__pk=restaurant_pk,
                menu_items__availability=True
            ).distinct()

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = (
        'get_image_list_preview',
        'name',
        'category',
        'price',
    )
    list_display_links = (
        'name',
    )
    list_filter = (
        'category',
        ProductsFilterByRestaurant,
    )
    search_fields = (
        # FIXME SQLite can not convert letter case for cyrillic words properly, so search will be buggy.
        # Migration to PostgreSQL is necessary
        'name',
        'category__name',
    )

    inlines = (
        RestaurantMenuItemInline,
    )
    fieldsets = (
        ('Общее', {
            'fields': [
                'name',
                'category',
                'image',
                'get_image_preview',
                'price',
            ]
        }),
        ('Подробно', {
            'fields': [
                'special_status',
                'description',
            ],
            'classes': [
                'wide'
            ],
        }),
    )

    readonly_fields = (
        'get_image_preview',
    )

    class Media:
        css = {
            "all": (
                static("admin/products.css")
            )
        }

    def get_image_preview(self, obj):
        if not obj.image:
            return 'выберите картинку'
        return format_html(
            '',
            url=obj.image.url
        )
    get_image_preview.short_description = 'превью'

    def get_image_list_preview(self, obj):
        if not obj.image or not obj.id:
            return 'нет картинки'
        edit_url = reverse('admin:products_product_change', args=(obj.id,))
        return format_html(
            '',
            edit_url=edit_url, src=obj.image.url
        )
    get_image_list_preview.short_description = 'превью'

@admin.register(ProductCategory)
class ProductCategoryAdmin(admin.ModelAdmin):
    pass


from django.urls import path
from .views import index, add_forum, index_json, add_api_forum
from comment.views import index as index_comment

urlpatterns = [
    path('', index, name='forum_list'),
    path('add/', add_forum, name='add'),
    path('add-api/', add_api_forum, name='add'),
    path('json/', index_json, name='json api'),
    path('<int:id>', index_comment, name='comment'),
]


# from tensorflow.keras.datasets import mnist
from keras.datasets import mnist
from sklearn.preprocessing import OneHotEncoder
import tensorflow as tf
import numpy as np
from sklearn.metrics import accuracy_score

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 28*28)
x_test = x_test.reshape(10000, 28*28)

# OneHotEncoder

y_train = y_train[:,np.newaxis]
y_test = y_test[:,np.newaxis]

enc = OneHotEncoder()
y_train = enc.fit_transform(y_train).toarray()
y_test = enc.transform(y_test).toarray()

# 모델 구성

x = tf.compat.v1.placeholder(tf.float32, shape=[None, 28*28])
y = tf.compat.v1.placeholder(tf.float32, shape=[None, 10])

# 히든 레이어

w = tf.Variable(tf.random_normal([28*28,512]), name='weight')
b = tf.Variable(tf.random_normal([512]), name='bias')

layers = tf.nn.relu(tf.matmul(x, w) + b)

# 히든 레이어

w = tf.Variable(tf.random_normal([512,256]), name='weight')
b = tf.Variable(tf.random_normal([256]), name='bias')

layers = tf.nn.relu(tf.matmul(layers, w) + b)

# 히든 레이어

w = tf.Variable(tf.random_normal([256,64]), name='weight')
b = tf.Variable(tf.random_normal([64]), name='bias')

layers = tf.nn.relu(tf.matmul(layers, w) + b)

# 아웃풋 레이어

w = tf.Variable(tf.random_normal([64,10]), name='weight')
b = tf.Variable(tf.random_normal([10]), name='bias')

# hyporthesis = x * w + b
# layers = tf.nn.relu(tf.matmul(x, w) + b)
# layers = tf.nn.elu(tf.matmul(x, w) + b)
# layers = tf.nn.selu(tf.matmul(x, w) + b)
# layers = tf.nn.dropout(layer, keep_prob=0.3)
# hypothesis = tf.sigmoid(tf.matmul(layers, w) + b)
hypothesis = tf.nn.softmax(tf.matmul(layers, w) + b)

# cost  = tf.reduce_mean(tf.square(hypothesis-y)) # mse
# cost = -tf.reduce_mean(y*tf.log(hypothesis)+(1-y)*tf.log(1-hypothesis)) # binary_crossentropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(hypothesis), axis=1))    # categorical_crossentropy

# optimizer  = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
optimizer  = tf.train.GradientDescentOptimizer(learning_rate=1e-1)
train = optimizer.minimize(cost)

# predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
# accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, y), dtype=tf.float32))

sess = tf.compat.v1.Session()
sess.run(tf.global_variables_initializer())

for epochs in range(1001):
    cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
            feed_dict={x:x_train, y:y_train})
    if epochs % 200 == 0:
        print(epochs, "cost :", cost_val, '\n', hy_val)

predict = sess.run(hypothesis, feed_dict={x:x_test})
y_pred = np.argmax(predict, axis=1)
y_test = np.argmax(y_test, axis=1)

print('acc_score :', accuracy_score(y_test, y_pred))

# acc_score : 0.098

sess.close()


from torch.utils.data import DataLoader
import torch.utils.data as data
import torch
import os
import os.path
from torchvision import transforms
import numpy as np
from PIL import Image, ImageFile
import matplotlib.pyplot as plt

ImageFile.LOAD_TRUNCATED_IMAGES = True
IMG_EXTENSIONS = [
    '.jpg', '.JPG', '.jpeg', '.JPEG',
    '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
]

class_weight = [5.603479582009111, 4.29526217910912, 33.5735969320392, 3.335498275510207, 15.485574406695266, 8.738715632316753,
              31.541973276563166, 32.01203384130015, 12.828815882297924, 38.01410671061199, 43.907386358778936]
mean = (0.41189488770418226, 0.4251328066237724, 0.432670702070482)
std = (0.3070734955953852, 0.3116110784489235, 0.3072184293428751)

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

def make_dataset(d):
    images = []
    annot = []

    for root, _, fnames in sorted(os.walk(d)):
        for fname in fnames:
            if is_image_file(fname):
                path = os.path.join(root, fname)
                item = path
                images.append(item)

    for root, _, fnames in sorted(os.walk("%sannot" % d)):
        for fname in fnames:
            if is_image_file(fname):
                path = os.path.join(root, fname)
                item = path
                annot.append(item)

    return images, annot

# transform label images to floattensor
class MaskToTensor(object):
    def __call__(self, img):
        return torch.from_numpy(np.array(img, dtype=np.int32)).float()

class Loaddata(data.Dataset):

    def __init__(self, root, transform=None, target_transform=None, joint_transform=None):

        imgs, annot = make_dataset(root)
        if len(imgs) == 0:
            raise(RuntimeError("Found 0 images in subfolders of: " + root + "\n"
                               "Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))

        self.root = root
        self.imgs = imgs
        self.annot = annot
        self.transform = transform
        self.target_transform = target_transform
        self.joint_transform = joint_transform
        self.n_classes = 12

    def __getitem__(self, index):
        path_to_img = self.imgs[index]
        path_to_target = self.annot[index]
        img = Image.open(path_to_img)
#        img = np.array(img, dtype=np.uint8)

        target = Image.open(path_to_target)

        if self.joint_transform is not None:
            img, target = self.joint_transform(img, target)
        if self.transform is not None:
            img = self.transform(img)
        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

    def __len__(self):
        return len(self.imgs)

    def decode_segmap(self, temp):
        Sky = [128, 128, 128]
        Building = [128, 0, 0]
        Pole = [192, 192, 128]
        Road = [128, 64, 128]
        Pavement = [60, 40, 222]
        Tree = [128, 128, 0]
        SignSymbol = [192, 128, 128]
        Fence = [64, 64, 128]
        Car = [64, 0, 128]
        Pedestrian = [64, 64, 0]
        Bicyclist = [0, 128, 192]
        Unlabelled = [0, 0, 0]

        label_colours = np.array([Sky, Building, Pole, Road,
                                  Pavement, Tree, SignSymbol, Fence, Car,
                                  Pedestrian, Bicyclist, Unlabelled])
        r = temp.copy()
        g = temp.copy()
        b = temp.copy()
        for l in range(0, self.n_classes):
            r[temp == l] = label_colours[l, 0]
            g[temp == l] = label_colours[l, 1]
            b[temp == l] = label_colours[l, 2]

        rgb = np.zeros((temp.shape[0], temp.shape[1], 3))
        rgb[:, :, 0] = r / 255.0
        rgb[:, :, 1] = g / 255.0
        rgb[:, :, 2] = b / 255.0
        return rgb

if __name__ == '__main__':
    local_path = 'C:/Users/mchiwml4/dataset/segmentation/camvid/train'
    joint_transform = jointtransform.Compose([
                jointtransform.RandomCrop(224),
                jointtransform.RandomHorizontallyFlip()
                ])
    transform = transforms.Compose([
                transforms.ToTensor()
                ])
    dst = Loaddata(local_path, transform=transform, target_transform=MaskToTensor(), joint_transform= joint_transform)
    bs = 4
    trainloader = DataLoader(dst, batch_size=bs)
    for i, image in enumerate(trainloader):
        imgs, labels = image
        imgs = imgs.numpy()
        imgs = np.transpose(imgs, [0,2,3,1])
        labels.squeeze_()
        f, axarr = plt.subplots(bs, 2)
        for j in range(bs):
            axarr[j][0].imshow(imgs[j])
            axarr[j][1].imshow(dst.decode_segmap(labels.numpy()[j]))
        plt.show()
        if i == 0:
            break


from flask import Flask, jsonify
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route('/get_latest_stories', methods=['GET'])
def get_latest_stories():
    try:
        url = 'https://time.com/'
        response = requests.get(url)

        if response.status_code == 200:
            soup = BeautifulSoup(response.text, 'html.parser')

            story_elements = soup.find_all('li', class_='latest-stories__item')

            stories = []

            for story in story_elements[:6]:
                title = story.text.strip()
                link = story.find('a')['href']
                stories.append({'Title': title, 'Link': link})

            return jsonify({'stories': stories})
        else:
            return jsonify({'error': 'Failed to fetch data from Time.com'}), 500
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)


from django.shortcuts import render
from todo_app.models import Todo
from django.http import HttpResponseRedirect

#CRUD operation

def todo_list(request):
    todos= Todo.objects.all()
    return render(
           request,
          "bootstrap/todo_list.html",
          {
           "todos":todos},

            )
def todo_delete(request, pk):
    todo=Todo.objects.get(pk=pk)
    todo.delete()
    return HttpResponseRedirect("/")

def todo_create(request):
    if request.method == "GET":
        return render(request, "bootstrap/todo_create.html")
    else:
       title= request.POST["title"]
       Todo.objects.create(title=title)
       return HttpResponseRedirect("/") #goto home page

def todo_update(request, pk):
    todo=Todo.objects.get(pk=pk)
    if request.method=="GET":
        return render(request, "bootstrap/todo_update.html", {"todo":todo})
    else:
        todo.title=request.POST["title"]
        todo.save()
        return HttpResponseRedirect("/") #goto homepage


import cv2
import numpy as np
from math_tool import conv2
from read_bin import BinFile

def preprocess(imageinput, bayerformat="rggb", outputformat="raw", mode=0, bitdepth=10, pedestal=64, FOV=0,
               whitebalance=True, signed=True, more_precise=False, custom_size=[0, 0], custom_encoding="H", for_SFR_test=False):

    if custom_encoding is "H":
        # 默认unsigned integer, 16位
        bin = BinFile(imageinput, datatype=np.uint16)
        header, ID = bin.get_realdata(2)
    else:
        bin = BinFile(imageinput, datatype=custom_encoding)
        header, ID = bin.get_realdata(2)

    if custom_size == [0, 0]:
        width = header[0]
        height = header[1]
    else:
        width = custom_size[0]
        height = custom_size[1]

    ID = np.asarray(ID).astype(np.double)

    if custom_size == [0, 0]:
        ID = ID[2:]

    ID = ID.reshape((height, width))

    # 按mode切片
    ID = crop_by_mode(ID, mode)

    # 增益
    ID = ID + pedestal
    if not signed:
        ID[ID < 0] = 0

    # 白平衡
    if whitebalance:
        ID = white_balance(ID, for_SFR_test)
        ID[ID > 2 ** bitdepth - 1] = 2 ** bitdepth - 1  # 防过饱和

    # 去镜头阴影
    if FOV is not 0:
        ID = lens_shading_correction(ID, 75)
        ID[ID > 2 ** bitdepth - 1] = 2 ** bitdepth - 1  # 防过饱和

    # 图像格式转换
    if outputformat is "raw":
        # 源格式输出
        if not more_precise:
            ID = np.round(ID)
        return ID

    elif outputformat is "bayer":
        # 分4层输出，强制转换到RGrGbB
        plane = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2), 4))
        R = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2)))
        Gr = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2)))
        Gb = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2)))
        B = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2)))

        plane[:, :, 0] = ID[::2, ::2]
        plane[:, :, 1] = ID[::2, 1::2]
        plane[:, :, 2] = ID[1::2, ::2]
        plane[:, :, 3] = ID[1::2, 1::2]

        if bayerformat is "rggb":
            R = plane[:, :, 0]
            Gr = plane[:, :, 1]
            Gb = plane[:, :, 2]
            B = plane[:, :, 3]
        elif bayerformat is "bggr":
            B = plane[:, :, 0]
            Gb = plane[:, :, 1]
            Gr = plane[:, :, 2]
            R = plane[:, :, 3]
        elif bayerformat is "gbrg":
            Gb = plane[:, :, 0]
            B = plane[:, :, 1]
            R = plane[:, :, 2]
            Gr = plane[:, :, 3]
        elif bayerformat is "grbg":
            Gr = plane[:, :, 0]
            R = plane[:, :, 1]
            B = plane[:, :, 2]
            Gb = plane[:, :, 3]

        bayer = np.zeros((int(ID.shape[0] / 2), int(ID.shape[1] / 2), 4))
        bayer[:, :, 0] = R
        bayer[:, :, 1] = Gr
        bayer[:, :, 2] = Gb
        bayer[:, :, 3] = B

        if not more_precise:
            bayer = np.round(bayer)
        return bayer

    elif outputformat is "rgb":
        bgr = bilinear_interpolation_opencv(ID, bayerformat)
        if not more_precise:
            bgr = np.round(bgr)
        return bgr

    elif outputformat is "yuv":
        bgr = bilinear_interpolation(ID, bayerformat)

        # opencv solution
        yuv = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV)

        # numpy solution
        # yuv = np.zeros(rgb.shape)
        # yuv[:, :, 0] = 0.299 * rgb[:, :, 0] + 0.587 * rgb[:, :, 1] + 0.114 * rgb[:, :, 2]
        # yuv[:, :, 1] = 2 ** (bitdepth - 1) - 0.1687 * rgb[:, :, 0] - 0.3313 * rgb[:, :, 1] + 0.5 * rgb[:, :, 2]
        # yuv[:, :, 2] = 2 ** (bitdepth - 1) + 0.5 * rgb[:, :, 0] - 0.4187 * rgb[:, :, 1] - 0.0813 * rgb[:, :, 2]

        if not more_precise:
            yuv = np.round(yuv)
        return yuv

    return

def crop_by_mode(raw, mode):
    width = raw.shape[1]
    height = raw.shape[0]

    # 按mode切片
    if mode is 1:
        raw = raw[8:height - 8, 8:width - 8]
    elif mode is 2:
        raw = raw[int((height - (width - 16) / (16 / 9)) / 2): height - int((height - (width - 16) / (16 / 9)) / 2),
              8:width - 8]
    elif mode is 3:
        raw = raw[8:height - 8,
              int((width - (height - 16) * (3 / 2)) / 2): width - int((width - (height - 16) * (3 / 2)) / 2)]
    elif mode is 4:
        raw = raw[8:height - 8,
              int((width - (height - 16) * (4 / 3)) / 2): width - int((width - (height - 16) * (4 / 3)) / 2)]

    return raw

def white_balance(raw, for_SFR_test=False):
    width = raw.shape[1]
    height = raw.shape[0]

    # 分4层
    plane = np.zeros((int(height / 2), int(width / 2), 4))
    plane[:, :, 0] = raw[::2, ::2]
    plane[:, :, 1] = raw[::2, 1::2]
    plane[:, :, 2] = raw[1::2, ::2]
    plane[:, :, 3] = raw[1::2, 1::2]

    block_size_R = 100
    block_size_C = 100

    if not for_SFR_test:
        center = [plane.shape[0] / 2 - 1, plane.shape[1] / 2 - 1]
    else:
        center = [plane.shape[0] / 2 - 1, plane.shape[1] / 2 - 1 - 0.08 * plane.shape[1]]  # slightly dodge the dot on the very center of the SFR chart

    center_block = plane[int(center[0] - block_size_R / 2 + 1):int(center[0] + int(block_size_R / 2 + 1)),
                   int(center[1] - int(block_size_C / 2) + 1): int(center[1] + int(block_size_C / 2) + 1), :]

    center_mean = np.mean((np.mean(center_block, axis=0)), axis=0)
    balance = np.max(center_mean) / center_mean

    for i in range(4):
        plane[:, :, i] = plane[:, :, i] * balance[i]

    # 4合1
    post_wb = np.zeros(raw.shape)
    post_wb[::2, ::2] = plane[:, :, 0]
    post_wb[::2, 1::2] = plane[:, :, 1]
    post_wb[1::2, ::2] = plane[:, :, 2]
    post_wb[1::2, 1::2] = plane[:, :, 3]

    return post_wb

# 镜头阴影纠正
# 涉及遍历全像素，使用Numba加速
# @numba.jit()
def lens_shading_correction(raw, FOV):
    width = raw.shape[1]
    height = raw.shape[0]

    # 找出中心并计算外接圆半径。加0.5的原因：一般来说，真实的像素是方形的，而真正的图像中心是四个像素中间的间隙，而不是某个像素
    centerX = width / 2 + 0.5 - 1
    centerY = height / 2 + 0.5 - 1
    circumradius = (centerX ** 2 + centerY ** 2) ** 0.5

    # 求出所有点所在的视野（半）角对应程度
    # pure python, slow "for loop"
    # Old Method, relied on Numba to accelerate
    # for j in range(height):
    #     for i in range(width):
    #         if not more_precise:
    #             FOV_scale = (FOV / 2) * ((centerX - i) ** 2 + (centerY - j) ** 2) ** 0.5 / circumradius
    #         else:
    #             FOV_scale = 180 / np.pi * np.arctan(np.tan(np.pi / 180 * (FOV / 2)) * ((centerX - i) ** 2 + (centerY - j) ** 2) ** 0.5 / circumradius)
    #
    #         # 采用4次余弦因子增益，次方越大，纠正效果越强(具体函数，是根据图像对角线分布函数拟合的)
    #         lsc_factor = 1 / (np.cos(np.pi / 180 * FOV_scale)) ** 4
    #         raw[j, i] = raw[j, i] * lsc_factor

    # New Method, no more "for loop", don't relied on Numba
    x_points = (centerX - np.arange(0, width)) ** 2
    y_points = (centerY - np.arange(0, height)) ** 2

    x_grid = np.tile(x_points, [len(y_points), 1])
    y_grid = np.transpose(np.tile(y_points, [len(x_points), 1]))

    FOV_grid = 180 / np.pi * np.arctan(np.tan(np.pi / 180 * (FOV / 2)) * np.sqrt(x_grid + y_grid) / circumradius)
    lsc_grid = 1 / np.cos(np.pi / 180 * FOV_grid) ** 4

    # Apply correction coefficient
    raw = lsc_grid * raw

    return raw

# 线性插值
def bilinear_interpolation_opencv(raw_bayer, bayerformat="rggb"):
    if str.lower(bayerformat) is "rggb":
        raw_bayer = cv2.cvtColor(raw_bayer, cv2.COLOR_BAYER_BG2BGR)
    elif str.lower(bayerformat) is "bggr":
        raw_bayer = cv2.cvtColor(raw_bayer, cv2.COLOR_BAYER_RG2BGR)
    elif str.lower(bayerformat) is "gbrg":
        raw_bayer = cv2.cvtColor(raw_bayer, cv2.COLOR_BAYER_GR2BGR)
    elif str.lower(bayerformat) is "grbg":
        raw_bayer = cv2.cvtColor(raw_bayer, cv2.COLOR_BAYER_GB2BGR)

def bilinear_interpolation(raw_bayer, bayerformat="rggb"):
    w = raw_bayer.shape[1]
    h = raw_bayer.shape[0]
    if str.lower(bayerformat) is "rggb":
        red_mask = np.tile(([1, 0], [0, 0]), [int(h / 2), int(w / 2)])
        green_mask = np.tile(([0, 1], [1, 0]), [int(h / 2), int(w / 2)])
        blue_mask = np.tile(([0, 0], [0, 1]), [int(h / 2), int(w / 2)])
    elif str.lower(bayerformat) is "bggr":
        blue_mask = np.tile(([1, 0], [0, 0]), [int(h / 2), int(w / 2)])
        green_mask = np.tile(([0, 1], [1, 0]), [int(h / 2), int(w / 2)])
        red_mask = np.tile(([0, 0], [0, 1]), [int(h / 2), int(w / 2)])
    elif str.lower(bayerformat) is "gbrg":
        green_mask = np.tile(([1, 0], [0, 0]), [int(h / 2), int(w / 2)])
        blue_mask = np.tile(([0, 1], [1, 0]), [int(h / 2), int(w / 2)])
        red_mask = np.tile(([0, 0], [0, 1]), [int(h / 2), int(w / 2)])
    elif str.lower(bayerformat) is "grbg":
        green_mask = np.tile(([1, 0], [0, 0]), [int(h / 2), int(w / 2)])
        red_mask = np.tile(([0, 1], [1, 0]), [int(h / 2), int(w / 2)])
        blue_mask = np.tile(([0, 0], [0, 1]), [int(h / 2), int(w / 2)])

    # 分离RG（Gr + Gb）B
    R = raw_bayer * red_mask
    G = raw_bayer * green_mask
    B = raw_bayer * blue_mask

    # 给缺失区插值
    R = R + conv2(R, np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]), mode="same")
    G = G + conv2(G, np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]), mode="same")
    B = B + conv2(B, np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]), mode="same")

    # 计算每个像素由多少个像素合成
    Rpix = conv2(red_mask, np.ones((3, 3)), "same")
    Gpix = conv2(green_mask, np.ones((3, 3)), "same")
    Gpix = Gpix - green_mask * Gpix + green_mask
    Bpix = conv2(blue_mask, np.ones((3, 3)), "same")

    R = R / Rpix
    G = G / Gpix
    B = B / Bpix

    rgb = np.zeros((h, w, 3))
    rgb[:, :, 0] = R
    rgb[:, :, 1] = G
    rgb[:, :, 2] = B

    return rgb


# ===================================================================
# Author(s):
#
#       Name        Email
#      Jon Perry    dcjmp90@gmail.com
#
# Notes:
#
#   This will hold subclasses of the Abstract Config class
# ===================================================================
"""A Config class for item lookup"""

import bs4 as bs
import urllib.request as url_request
from sanctuary.utils.config.base_config import BaseConfig
from sanctuary.utils.modules.search_module import ItemSearchModule
from sanctuary.utils.parser.cli_arguments import Map

__all__ = ['ItemLookupConfig']

class ItemLookupConfig(BaseConfig):
    """A processor for bot data

    Performs various bot tasks like look up
    of item stats or look up of names of runes.
    Anything that is within the diablo 2 database.

    Parameters
    ----------

    bot_name : str
        The name for the lookup bot

    prefix : str
        The type of prefix to filter in discord

    args : Values objects from builtin
        This will be all the argsparser arguments

    Notes
    -----

    currently only building a single lookup bot, but this may change
    based on needs, the lookups may need to be delegated to many.

    Usage
    -----

    ?<item_type> -- bs4.BeautifulSoup object parse of <item_type>
            will return an array-like structure of all of this <item_type>

    ?<item_type> <item_name> stats -- bs4.BeautifulSoup object parse of <item_name>
            will return stats of <item_name>

    ?<item_type> <item_name> requirements -- bs4.BeautifulSoup object parse of <item_name>
            will return requirements of <item_name>

    >>> ?runewords spirit requirements
    ... returns: lvl. 25, 4 Socket Sword/Shield, Tal->Thul->Ort->Amn

    >>> ?runewords spirit stats
    ...
    """

    def __init__(self,
                 bot_name,
                 args,
                 prefix='?',
    ):
        self.bot_name = bot_name
        self.prefix = prefix
        self.args = args
        super().__init__(self.bot_name,
                         self.prefix,
                         self.args,
        )

        self.searchables = [ v for k, v in self.SEARCH_ITEMS.items() ]
        self.bs_parser = Map({})
        self.searchable = ItemSearchModule(self.config)

        for name in self.searchables:
            url = url_request.urlopen(self.args.BASE_URL+name)
            self.bs_parser[name] = bs.BeautifulSoup(url.read(), self.config.PARSER)

    def _item_type(self, item_type):
        """Parser for all of type"""

        if item_type.lower() in self.bs_parser.keys():
            _bs_parser = self.bs_parser[item_type.lower()]

            items = _bs_parser.findAll(self.config.CONTAINER)
            return items
        else:
            return None

    def __call__(self,
                 item_type,
                 item_name = None,
                 item_spec = None,
                 **kwargs,
    ):
        """Override of the call method"""
        parent_container = self._item_type(item_type)

        if parent_container is None:
            raise ValueError('No known item of type {}. KYS.'.format(item_type))
        else:
            return self.searchable(parent_container,
                                   item_name,
                                   item_spec,
                                   **kwargs)