# uncompyle6 version 3.7.4
# Python bytecode 3.4 (3310)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/stas/workspace/partners/flask_restframework/flask_restframework/filter_backends.py
# Compiled at: 2017-07-14 09:11:18
# Size of source mod 2**32: 2741 bytes
import json
from flask_restframework.queryset_wrapper import MongoDbQuerySet

class BaseBackend:

    def __init__(self, qs, request, resource):
        self.qs = qs
        self.request = request
        self.resource = resource

    def filter(self):
        raise NotImplementedError

class OrderingBackend(BaseBackend):
    __doc__ = '\n    Allows ordering with GET parameter.\n    For example,\n\n        GET ?ordering=name,-value\n\n    Will sort .order_by(["name", "-value"])\n\n    You can set resource attribute **ordering** if you want default ordering\n    '

    def _default_ordering(self):
        if hasattr(self.resource, 'ordering'):
            return self.resource.ordering
        return []

    def filter(self):
        try:
            ordering_fields = self.request.args.get('ordering')
            if ordering_fields:
                ordering_fields = ordering_fields.split(',')
            else:
                ordering_fields = []
        except:
            ordering_fields = []

        ordering_fields = ordering_fields or self._default_ordering()
        if ordering_fields:
            self.qs = self.qs.order_by(*ordering_fields)
        return self.qs

class JsonFilterBackend(BaseBackend):
    __doc__ = '\n    Allows custom filtration with json_filter GET parameter.\n    For example:\n\n        GET ?json_filters={"name": "blablabla"}\n\n    will filter queryset in this way:\n\n        .filter(__raw__={"name": "blablabla"})\n\n    You can manage filter logig with Resource attribute update_json_filter which accepts\n    (json_filter)->new_json_filter\n\n    .. warning::\n\n        This filter can be used only with :class:`.MongoDbQuerySet`\n\n    '
    qs = None

    def filter(self):
        try:
            json_filter = self.request.args.get('json_filters')
            json_filter = json.loads(json_filter)
        except:
            return self.qs

        assert isinstance(self.qs, MongoDbQuerySet)
        if hasattr(self.resource, 'update_json_filter'):
            json_filter = self.resource.__class__.update_json_filter(json_filter)
        self.qs = MongoDbQuerySet.from_queryset(self.qs.data.filter(__raw__=json_filter))
        return self.qs

class SearchFilterBackend(BaseBackend):
    __doc__ = '\n    Allows custom filtration with search GET parameter.\n    For example:\n\n        GET ?search="search text"\n\n    will filter queryset in this way:\n\n        .filter(__raw__={"$text": {"$search": search_text}})\n\n    '

    def filter(self):
        try:
            search_text = self.request.args.get('search')
        except:
            search_text = ''

        if search_text:
            self.qs = self.qs.filter(__raw__={'$text': {'$search': search_text}})
        return self.qs


# Common imports
import numpy as np
import numpy.random as rnd
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from functools import partial

# from tensorflow.examples.tutorials.mnist import input_data

def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

def plot_image(image):
    plt.imshow(image, cmap="gray", interpolation="nearest")
    plt.axis("off")

def plot_color_image(image):
    plt.imshow(image.astype(np.uint8),interpolation="nearest")
    plt.axis("off")

from sklearn.datasets import load_sample_image
china = load_sample_image("china.jpg")
flower = load_sample_image("flower.jpg")
image = china[150:220, 130:250]
height, width, channels = image.shape
image_grayscale = image.mean(axis=2).astype(np.float32)
images = image_grayscale.reshape(1, height, width, 1)

fmap = np.zeros(shape=(7, 7, 1, 2), dtype=np.float32)
fmap[:, 3, 0, 0] = 1
fmap[3, :, 0, 1] = 1
fmap[:, :, 0, 0]
plot_image(fmap[:, :, 0, 0])
plt.show()
plot_image(fmap[:, :, 0, 1])
plt.show()

reset_graph()

X = tf.placeholder(tf.float32, shape=(None, height, width, 1))
feature_maps = tf.constant(fmap)
convolution = tf.nn.conv2d(X, feature_maps, strides=[1,1,1,1], padding="SAME")

with tf.Session() as sess:
    output = convolution.eval(feed_dict={X: images})

plot_image(images[0, :, :, 0])
plt.show()

plot_image(output[0, :, :, 0])
plt.show()

plot_image(output[0, :, :, 1])
plt.show()

import numpy as np
from sklearn.datasets import load_sample_images

# Load sample images
china = load_sample_image("china.jpg")
flower = load_sample_image("flower.jpg")
dataset = np.array([china, flower], dtype=np.float32)
batch_size, height, width, channels = dataset.shape

# Create 2 filters
filters = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)
filters[:, 3, :, 0] = 1  # vertical line
filters[3, :, :, 1] = 1  # horizontal line

# Create a graph with input X plus a convolutional layer applying the 2 filters
X = tf.placeholder(tf.float32, shape=(None, height, width, channels))
convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME")

with tf.Session() as sess:
    output = sess.run(convolution, feed_dict={X: dataset})

plt.imshow(output[0, :, :, 1], cmap="gray") # plot 1st image's 2nd feature map
plt.show()

for image_index in (0, 1):
    for feature_map_index in (0, 1):
        plot_image(output[image_index, :, :, feature_map_index])
        plt.show()

reset_graph()
X = tf.placeholder(shape=(None, height, width, channels), dtype=tf.float32)
conv = tf.layers.conv2d(X, filters=2, kernel_size=7, strides=[2,2], padding="SAME")

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    output = sess.run(conv, feed_dict={X: dataset})
plt.imshow(output[0, :, :, 1], cmap="gray") # plot 1st image's 2nd feature map
plt.show()

# 2.2  VALID vs SAME padding

reset_graph()

filter_primes = np.array([2., 3., 5., 7., 11., 13.], dtype=np.float32)
x = tf.constant(np.arange(1, 13+1, dtype=np.float32).reshape([1, 1, 13, 1]))
filters = tf.constant(filter_primes.reshape(1, 6, 1, 1))

valid_conv = tf.nn.conv2d(x, filters, strides=[1, 1, 5, 1], padding='VALID')
same_conv = tf.nn.conv2d(x, filters, strides=[1, 1, 5, 1], padding='SAME')

with tf.Session() as sess:
    hoge = x.eval()
    print("VALID:\n", valid_conv.eval())
    print("SAME:\n", same_conv.eval())

print("VALID:")
print(np.array([1,2,3,4,5,6]).T.dot(filter_primes))
print(np.array([6,7,8,9,10,11]).T.dot(filter_primes))
print("SAME:")
print(np.array([0,1,2,3,4,5]).T.dot(filter_primes))
print(np.array([5,6,7,8,9,10]).T.dot(filter_primes))
print(np.array([10,11,12,13,0,0]).T.dot(filter_primes))


from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox

import layouts.knowledgeEditorClassFeatureDefinition as design
import connectionToDatabase as db
import knowledgeEditor.Dialog.ScalarClassFeatureDef as ScalarEdit
import knowledgeEditor.Dialog.LogicalClassFeatureDef as LogicalEdit
import knowledgeEditor.Dialog.DimensionalClassFeatureDef as DimensionalEdit

class classFeatureDefinition(QtWidgets.QMainWindow, design.Ui_classFeatureDefinition):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)  # Это нужно для инициализации нашего дизайна
        self.featureOfClass = []
        self.inst()
        self.show_ScalarEditor = ScalarEdit.Scalar(self)
        self.show_LogicalEditor = LogicalEdit.Logical(self)
        self.show_DimensionalEditor = DimensionalEdit.Dimensional(self)

        self.comboBox_classChoose_classFeatureDefinition.activated.connect(self.takeFeaturesFromClass)
        self.button_goBack_classFeatureDefinition.clicked.connect(self.goto_return)
        self.comboBox_featureChoose_classFeatureDefinition.activated.connect(self.takeTypeOfFeature)
        self.comboBox_featureChoose_classFeatureDefinition.activated.connect(self.getValueOfFeature)
        self.button_ok_classFeatureDefinition.clicked.connect(self.goto_return)
        self.comboBox_classChoose_classFeatureDefinition.activated.connect(self.showSetFeature)
        self.comboBox_classChoose_classFeatureDefinition.activated.connect(self.showUnSetFeature)
        self.button_featureSet_classFeatureDefinition_2.clicked.connect(self.setFeatureDef)
        self.button_featureUnSet_classFeatureDefinition.clicked.connect(self.unsetFeatureDef)

    def inst(self):
        self.comboBox_classChoose_classFeatureDefinition.clear()
        classes = db.getAllClasses()
        for row in classes:
            self.comboBox_classChoose_classFeatureDefinition.addItem(row["Class"])
        if self.comboBox_classChoose_classFeatureDefinition.count() > 0:
            self.comboBox_classChoose_classFeatureDefinition.setCurrentText(classes[0]["Class"])
        self.takeFeaturesFromClass()
        self.getValueOfFeature()
        self.showSetFeature()
        self.showUnSetFeature()

    def takeFeaturesFromClass(self):
        self.comboBox_featureChoose_classFeatureDefinition.clear()
        features = db.getFeautureFromClass_classExplanation(
            self.comboBox_classChoose_classFeatureDefinition.currentText())
        if not features == None:
            if len(features) > 0:
                for row in features:
                    self.comboBox_featureChoose_classFeatureDefinition.addItem(row["NameFeature"])
                self.comboBox_featureChoose_classFeatureDefinition.setCurrentText(features[0]["NameFeature"])
                self.takeTypeOfFeature()
                return features
            else:
                return []

    def setFeatureDef(self):
        featureType = self.takeTypeOfFeature()
        if featureType == 1:
            self.goto_ScalarEdit()
        if featureType == 2:
            self.goto_LogicalEdit()
        if featureType == 3:
            self.goto_DimensionalEdit()

    def goto_ScalarEdit(self):
        self.show_ScalarEditor.inst(self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                    db.getIdFeatureClass_pairByClassNameFeatureName(
                                        self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                        self.comboBox_classChoose_classFeatureDefinition.currentText()))
        self.show_ScalarEditor.show()
        self.hide()

    def goto_LogicalEdit(self):
        self.show_LogicalEditor.inst(self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                     db.getIdFeatureClass_pairByClassNameFeatureName(
                                         self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                         self.comboBox_classChoose_classFeatureDefinition.currentText()))
        self.show_LogicalEditor.show()
        self.hide()

    def goto_DimensionalEdit(self):
        self.show_DimensionalEditor.inst(self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                         db.getIdFeatureClass_pairByClassNameFeatureName(
                                             self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                             self.comboBox_classChoose_classFeatureDefinition.currentText()))
        self.show_DimensionalEditor.show()
        self.hide()

    def unsetFeatureDef(self):
        feature = self.comboBox_featureChoose_classFeatureDefinition.currentText()
        featureType = db.takeTypeOfFeature(feature)
        if featureType['Type'] == 1:
            self.show_ScalarEditor.deleteFeature(db.getIdFeatureClass_pairByClassNameFeatureName(
                                            self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                            self.comboBox_classChoose_classFeatureDefinition.currentText()))
            self.inst()
        if featureType['Type'] == 2:
            self.show_LogicalEditor.inst(self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                         db.getIdFeatureClass_pairByClassNameFeatureName(
                                             self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                             self.comboBox_classChoose_classFeatureDefinition.currentText()))
            self.inst()
        if featureType['Type'] == 3:
            self.show_DimensionalEditor.inst(self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                             db.getIdFeatureClass_pairByClassNameFeatureName(
                                                 self.comboBox_featureChoose_classFeatureDefinition.currentText(),
                                                 self.comboBox_classChoose_classFeatureDefinition.currentText()))
            self.inst()

    def showSetFeature(self):
        print()

    def showUnSetFeature(self):
        self.text_featureUnset_classFeatureDefinition.clear()
        self.text_featureSetted_classFeatureDefinition.clear()
        AllFeatures = self.takeFeaturesFromClass()
        if not AllFeatures==None:
            if len(AllFeatures) > 0:
                for row in AllFeatures:
                    if db.isSet(db.getIdFeatureClass_pairByClassNameFeatureName(
                            row["NameFeature"],
                            self.comboBox_classChoose_classFeatureDefinition.currentText())):
                        self.text_featureSetted_classFeatureDefinition.append(row["NameFeature"])
                    else:
                        self.text_featureUnset_classFeatureDefinition.append(row["NameFeature"])

    #
    # if row["Type"] == 1:
    #     return 'Scalar'
    # if row["Type"] == 2:
    #     return 'Logical'
    # if row["Type"] == 3:
    #     return 'Dimensional'

    def getValueOfFeature(self):
        self.text_Definition.clear()
        feature = self.comboBox_featureChoose_classFeatureDefinition.currentText()
        className=self.comboBox_classChoose_classFeatureDefinition.currentText()
        self.text_Definition.clear()
        featureType = db.takeTypeOfFeature(feature)
        idFeatureClass=db.getIdFeatureClass_pairByClassNameFeatureName(feature,className)
        if featureType is not None:
            if featureType['Type'] == 1:
                values = db.getClassFeatureDefById_Scalar(idFeatureClass)
                for x in values:
                    self.text_Definition.append(x["Value"])

            if featureType['Type'] == 2:
                values = db.getClassFeatureDefById_Logical(idFeatureClass)
                self.text_Definition.append("True value:=" + str(values["True_Value"]))
                self.text_Definition.append("False value:=" + str(values["False_Value"]))
            if featureType['Type'] == 3:
                values = db.getClassFeatureDefById_Dimensional(idFeatureClass)
                self.text_Definition.append(str(values["LeftValue"])+" <= Value for class <= " +str(values["RightValue"]))

    def takeTypeOfFeature(self):
        self.line_featureType_classFeatureDefinition.clear()
        feature = self.comboBox_featureChoose_classFeatureDefinition.currentText()
        featureType = db.takeTypeOfFeature(feature)
        if featureType['Type'] == 1:
            self.line_featureType_classFeatureDefinition.setText("Скалярный")
            return 1
        if featureType['Type'] == 2:
            self.line_featureType_classFeatureDefinition.setText("Логический")
            return 2
        if featureType['Type'] == 3:
            self.line_featureType_classFeatureDefinition.setText("Размерный")
            return 3
        return -1

    def goto_return(self):
        self.parent().show()
        self.close()


#!/usr/bin/env python3
"""
Copyright 2020 Conversando Na Nuvem (https://www.youtube.com/channel/UCuI2nDGLq_yjY9JNsDYStMQ/) - Leandro Damascena

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------

This script manages aws-network-discovery, a tool for analyzing VPC dependencies.
"""
import sys
import argparse

# Check version
if sys.version_info < (3, 6):
    print("Python 3.6 or newer is required", file=sys.stderr)
    sys.exit(1)

from commands.vpc import Vpc

__version__ = "0.2.1"

def show_options(args="sys.argv[1:]"):
    parser = argparse.ArgumentParser()
    parser.add_argument(
                        "-v",
                        "--vpc-id",
                        required=True,
                        help="Inform VPC to analyze"
                        )
    parser.add_argument(
                        "-r",
                        "--region-name",
                        required=False,
                        help="Inform REGION to analyze. If not informed, try to get from config file"
                        )
    options = parser.parse_args()

    return options

def main():
    """Entry point for the CLI."""

    # Load commands
    if len(sys.argv) <= 1:
        options = show_options(args=['-h'])

    args = show_options(sys.argv)

    vpc = Vpc(vpc_id=args.vpc_id, region_name=args.region_name)
    vpc.run()

if __name__ == "__main__":
    main()


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: M:\Programming\Project\django-auth\auth\CustomAuth\migrations\0002_date_joined_auto_now_add.py
# Compiled at: 2019-12-09 12:37:18
# Size of source mod 2**32: 442 bytes
from django.db import migrations, models

class Migration(migrations.Migration):
    dependencies = [
     ('CustomAuth', '0001_initial')]
    operations = [
     migrations.AlterField(model_name='user',
       name='date_joined',
       field=models.DateTimeField(auto_now_add=True, null=True, verbose_name='join date'))]


from rest_framework import generics
from rest_framework.permissions import IsAuthenticatedOrReadOnly

from posts.api.serializers import PostSerializer, CommentSerializer, UpvoteSerializer
from posts.api.premissions import IsAuthorOrAdminOrReadOnly, IsAuthenticatedOrAdmin
from posts.models import Post, Comment, Upvote

class PostListView(generics.ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

class PostDetailsView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [IsAuthorOrAdminOrReadOnly]
    lookup_field = "slug"

class CommentListView(generics.ListCreateAPIView):
    serializer_class = CommentSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get_queryset(self):
        post_slug = self.kwargs["slug"]
        return Comment.objects.filter(post__slug=post_slug)

    def perform_create(self, serializer):
        post_slug = self.kwargs["slug"]
        post = Post.objects.get(slug=post_slug)
        serializer.save(author=self.request.user, post=post)

class CommentDetailsView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer
    permission_classes = [IsAuthorOrAdminOrReadOnly]

class UpvoteListView(generics.ListCreateAPIView):
    serializer_class = UpvoteSerializer
    permission_classes = [IsAuthenticatedOrAdmin]

    def get_queryset(self):
        post_slug = self.kwargs["slug"]
        return Upvote.objects.filter(post__slug=post_slug)

    def perform_create(self, serializer):
        post_slug = self.kwargs["slug"]
        post = Post.objects.get(slug=post_slug)
        serializer.save(user=self.request.user, post=post)

class UpvoteDetailsView(generics.RetrieveDestroyAPIView):
    queryset = Upvote.objects.all()
    serializer_class = UpvoteSerializer
    permission_classes = [IsAuthenticatedOrAdmin]


# -*- coding: utf-8 -*-

# django imports
from django.conf import settings
from django.utils.translation import get_language, to_locale

def project_data(request):
    return {
        'PROJECT_NAME': settings.PROJECT_NAME,
        'GOOGLE_ANALYTICS_TRACKING_ID': settings.GOOGLE_ANALYTICS_TRACKING_ID,
    }

def languages(request):
    lang_code = get_language()
    return {
        'LANGUAGE_CODE': lang_code,
        'LOCALE': to_locale(lang_code),
        'LANGUAGES': settings.LANGUAGES,
    }


import jwt
from rest_framework.permissions import BasePermission as DRFBasePermission
from authen.models import Member
from cook_intra import settings_base

class BasePermission(DRFBasePermission):
    def has_permission(self, request, view):  # 사용자 지정 권한
        if view.action == "join":             # view.action  조인
            return True  # permission True
        else:
            token = request.data.get("token", None)
            if not token:
                return False
            try:
                user_info = jwt.decode(token, settings_base.SECRET_KEY, algorithms=['HS256'])  # jwt token decode
                user = Member.objects.get(user_info["username"])
                request.user = user
                return True
            except Exception as e:
                return False

    def has_object_permission(self, request, view, obj):
        token = request.data.get("token", None)
        if not token:
            return False
        try:
            user_info = jwt.decode(token, settings_base.SECRET_KEY, algorithms=['HS256'])  # jwt token decode
            user = Member.objects.get(user_info["username"])
            request.user = user
            return True
        except Exception as e:
            return False


# -*- coding: utf-8 -*-
"""
Created on Sun Jun 3 2018

@author: casa
"""
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.utils import to_categorical
from keras import backend as K
import numpy as np
from tensorflow.python.lib.io import file_io
import io
import PIL
import random
import matplotlib as plt
import argparse

# parámetros para redimensionar imágenes
img_width, img_height = 150, 150
epochs = 50
batch_size = 16

train_data_dir = ''
validation_data_dir = ''

classes_num = 3  # numero de clases para clasificar
nb_train_samples = 4984
nb_validation_samples = 144

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

def leer_una_imagen(uri):
    image_bytes = file_io.FileIO(uri, mode='r').read()
    image = PIL.Image.open(io.BytesIO(image_bytes)).convert('RGB')
    image = image.resize((img_width, img_height), PIL.Image.BILINEAR)
    return image

def leer_imagenes_etiquetas(ruta):
    ## variables
    x=[]
    y=[]

    ruta=ruta + "/"
    print('LA RUTA ES...' + ruta)
    ## obtenemos la lista de ficneros para la ruta pasada por parámentro
    filelist = sorted(file_io.list_directory(ruta))
    random.seed(42)
    random.shuffle(filelist)

    print('TAMAÑO DIRECTORIO...' + str(len(filelist)) + "  primero..." + filelist[1])
    ## obtenemos las imágenes y las etiquetas de cada elemento de la lista
    for imgPath in filelist:
        imagen=leer_una_imagen(ruta + imgPath)
        imagen = img_to_array(imagen) # pasa a array
        x.append(imagen)
        	# lee, redimensiona y lo añade al array

        #imagePath = ruta + imgPath
        #imagen = cv2.imread(imagePath) # lee
        #imagen = cv2.resize(imagen, (img_width, img_height))  # redimentsiona

        # extraemos la etiqueta del nombre de cada fichero
        #ruta_partes = imagePath.split("/")  # lo dividimos en las partes de la ruta
        #nombreimagen = ruta_partes[len(ruta_partes)-1]   # nos quedamos con el nombre del fichero, que es el último

        label = imgPath[imgPath.find("_")+1:imgPath.find(".")] # la etiqueta está entre _ y .
        label = int(label) -1  # las etiquetas empiezan en 1, y les restamos 1 para que empiecen en 0
        y.append(label)

    print('REGISTROS LEIDOS X...' + str(len(x)))
    print('REGISTROS LEIDOS Y...' + str(len(y)))
    return (x,y)

def preproceso_imagenes_etiquetas(x,y):
    # pasar cada pixel a una escala de [0, 1] en un array numpy
    x = np.array(x, dtype="float") / 255.0
    # pasar las etiquetas a array y luego a categórico
    y = np.array(y)
    y_cat = to_categorical(y, num_classes=classes_num)

    return (x,y_cat)

def definir_red_neuronal():
    model = Sequential()
    model.add(Conv2D(32, (3, 3), input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(32, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(64, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    #model.add(Activation('sigmoid'))
    model.add(Dense(classes_num, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['categorical_accuracy'])

    return model

def definir_data_augmentation(x_train_p, y_train_p, x_validation_p, y_validation_p):
    # this is the augmentation configuration we will use for training
    train_datagen = ImageDataGenerator(
        rescale=1. / 255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

    # this is the augmentation configuration we will use for testing:
    # only rescaling
    test_datagen = ImageDataGenerator(rescale=1. / 255)

    t_generator = train_datagen.flow(
        x_train_p,y_train_p,
        batch_size=batch_size)

    v_generator = test_datagen.flow(
        x_validation_p,y_validation_p,
        batch_size=batch_size)

    return (t_generator,v_generator)

def dibujar_grafico_accuracy_loss(H):
    plt.use("Agg")
    plt.use("ggplot")
    plt.figure()

    plt.plot(np.arange(0, epochs), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, epochs), H.history["val_loss"], label="val_loss")
    plt.plot(np.arange(0, epochs), H.history["acc"], label="train_acc")
    plt.plot(np.arange(0, epochs), H.history["val_acc"], label="val_acc")

    plt.title("Training Loss y Accuracy")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss/Accuracy")
    plt.legend(loc="lower left")
    plt.savefig(args["grafico"])

###########################################################
###########################################################
###
###    PROGRAMA PRINCIPAL
###
###########################################################
###########################################################

def Run():
    ## Leemos datos y etiquetas para train y validación, y los preprocesamos
    (x_train, y_train) = leer_imagenes_etiquetas(train_data_dir)
    (x_train, y_train) = preproceso_imagenes_etiquetas(x_train, y_train)
    (x_validation, y_validation) = leer_imagenes_etiquetas(validation_data_dir)
    (x_validation, y_validation) = preproceso_imagenes_etiquetas(x_validation, y_validation)

    if (len(x_train)==0):
        print("ALGO HA IDO MAL!!!")
        return()
    ## definimos la estrategia de aumento de datos
    (train_generator,validation_generator) = definir_data_augmentation(x_train, y_train,x_validation, y_validation)

    ## preparamos la arquitectura de la red neuronal
    modelo = definir_red_neuronal()

    ## lanzamos el entrenamiento con los datos preparados y la red definida
    history = modelo.fit_generator(
        train_generator,
        steps_per_epoch=nb_train_samples // batch_size,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=nb_validation_samples // batch_size)

    # guardamos el modelo entrenado
    modelo.save_weights('first_try.h5')

    # guardamos la gráfica de accuracy y loss
    dibujar_grafico_accuracy_loss(history)

###########################################################
###########################################################
if __name__ == '__main__':
    # declaración de argumentos de entrada
    ap = argparse.ArgumentParser()
    ap.add_argument("-t", "--rtrain", required=True, help="ruta de train")
    ap.add_argument("-v", "--rvalid", required=True, help="ruta de validation")
    ap.add_argument("-g", "--grafico", type=str, default="plot.png", help="ruta de grafico")
    # parseo de argumentos
    args = vars(ap.parse_args())

    # lanzamos el proceso primcipal
    train_data_dir=args["rtrain"]
    validation_data_dir=args["rvalid"]
    Run()


from keras.datasets import reuters
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras. layers import Dense, Embedding, LSTM, Flatten

#1. 데이터
(x_train,y_train),(x_test,y_test) = reuters.load_data(num_words=10000, test_split = 0.2)
# 가장 빈도수가 많은 것부터 1000번쨰까지

print(x_train.shape, x_test.shape)  #(8982,), (2246,)
print(y_train.shape, y_test.shape)  #(8982,), (2246,)

print(x_train[0])
print(y_train[0])

#list는 shape구할 수 없다

print(len(x_train[0])) # 87

# y의 카테고리 개수 출력
category = np.max(y_train) + 1
print("카테고리:",category) #46개

# y의 유니크한 값을 출력
y_bunpo = np.unique(y_train)
print(y_bunpo)

y_train_pd = pd.DataFrame(y_train)
bbb = y_train_pd.groupby(0)[0].count()
print(bbb)
print(bbb.shape)

# 주간과제: groupby()의 사용법 숙지할 것

########################################

from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical

####다시 해보기
x_train = pad_sequences(x_train, maxlen=100, padding='pre') #maxlen - 최대값을 100으로 잡는다. / truncating - ex)100개가 넘는 문자열 자를 때(default = 앞에서부터)
# 여기서는 87개이기 때문에 truncating 필요 없다
x_test = pad_sequences(x_test, maxlen=100, padding='pre')

print(len(x_train[0]))  #87
print(len(x_train[-1])) #105

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

print(x_train.shape, x_test.shape) #(8982,100), (2246,100)

#2. 모델
model = Sequential()
# model.add(Embedding(1000, 100, input_length=100))
model.add(Embedding(1000,100))
# parameter 개수가 너무 커져도 좋지 않고 알아서 판단해야함
# word+_size 통상적으로 들어가는 단어 개수 넣어주는 것
# output*word_size=첫번째 파라미터 개수
# input_length안 써주면 x_train의  input_length 값을 자연스럽게 가져옴
# Embedding은 3차
model.add(LSTM(100))
model.add(Dense(46, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['acc']) #sparse_categorical_crossentropy

history = model.fit(x_train,y_train,batch_size=100,epochs=10, validation_split=0.2)

acc = model.evaluate(x_test,y_test)[1]
print("acc:",acc)

y_val_loss = history.history['val_loss']
y_loss = history.history['loss']

plt.plot(y_val_loss,marker=',',c='red',label='TestSet loss')
plt.plot(y_loss,marker=',',c='blue',label='Trainset loss')
plt.legend(loc='upper right')
plt.grid()
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()

# 8000개로 훈련, 2000여개로 맞추는 것


from flask import Flask
from flask import jsonify
from flask import request
import requests
app = Flask(__name__)

start = [{}]  #metoda put do modułu czasowego
time  = [{"speed":0,"symTime" : "2019-11-18+18:40:15" }]  #metoda put do modułu czasowego
Tzco = [{"Tzco": 0}] #to będzie pobierał od nas moduł 6
Tpm = [{"Tpm": 0}] #tak samo
impuls = [{"impuls": 0}]
#methods
@app.route('/Tzco', methods=['GET'])
def getTzco():
    return jsonify({'Tzco' : Tzco})

@app.route('/Tpm', methods=['GET'])
def getTpm():
    res = requests.put("http://127.0.0.1:5000/Tzco",json={'Tzco':2137})
    return jsonify({'Tpm' : Tpm})

@app.route('/Tpm', methods=['PUT'])
def editTpm():
    new_Tpm = request.get_json()
    Tpm[0] = new_Tpm
    qs = request.get_json()
    return jsonify({})
@app.route('/Tzco', methods=['PUT'])
def editTzco():
    new_Tzco = request.get_json()
    Tzco[0] = new_Tzco
    qs = request.get_json()
    return jsonify({})

@app.route('/start', methods=['GET'])
def getStart():
    return jsonify({'start' : start})

@app.route('/time', methods=['GET'])
def getTime():
    return jsonify({'time' : time})

@app.route('/impuls', methods=['GET'])
def getimpuls():
    return jsonify({'impuls' : impuls})

@app.route('/impuls', methods=['PUT'])
def editimpuls():
    new_impuls = request.get_json()
    impuls[0] = new_impuls
    qs = request.get_json()
    return jsonify({})

@app.route('/time', methods=['PUT'])
def editTime():
    new_time = request.get_json()
    time[0] = new_time
    qs = request.get_json()
    impuls = {"impuls": 1}
    return jsonify({})

@app.route('/start', methods=['PUT'])
def editStart():
    new_start = request.get_json()
    start[0] = new_start
    qs = request.get_json()
    impuls[0] = {"impuls": 1}
    return jsonify({})

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


# coding: utf-8
# Created by Jabok @ August 14th 2014
# fetcher.py
import os
import toml
from validators import validate_specs
from comic import Comic
from linkers import findlink
from images import findimages
from getter import Getter
from finder import Identifier
from bs4 import BeautifulSoup
from utils import lastcomicfile
"""
Initiates the fetching of a comic, attempting to generate the schema if necessary.
"""
_getter = Getter(timeout=5)

def findcontent(url, link_identifier, progress_identifier):
    """Finds image url and the link to the next page at the given web address."""
    # Url should be a full url path for correct url joining!
    image, nextpage = None, None
    soup = BeautifulSoup(_getter.get(url))
    nextpage = findlink(soup, url, link_identifier)
    if not (image and newpage): raise Exception("Nothing Found!")
    return image, nextpage

def fetchcomic(request, overwrite=False, startpage=None,
    pages=-1):
    """Fetches the comic described in the given request"""
    from utils import getcomiclist, printiter
    # If it is not a comic name that is known
    if os.path.exists(request):
        with open(request) as f:
            specs = toml.load(f)
        title = specs['title']
        directory = os.path.dirname(request) or os.getcwd()
    else:
        title = request.strip()
        directory = os.getcwd()

    if (not title in getcomiclist()) or overwrite:
        if not os.path.exists(request):
            return print("Could not find any specfile or comic '{0}'!".format(request))

        # Validate the given specification
        print("Validating... ", end="")
        valid, errors = validate_specs(specs, directory=directory)
        print("OK" if valid else "FAILED")
        if not valid:
            print("Cannot fetch a comic from the given spec due to the following errors:")
            return printiter(errors)

        Comic.create(specs, directory=directory)

    print("Preparing scrape...")
    with Comic.load(title) as comic:
        print("- Comic:", comic)
        link_identifier     = Identifier.load(comic.progress['link_identifier'])
        image_identifier    = Identifier.load(comic.progress['image_identifier'])

        lastimages  = comic.progress['lastimages']
        remaining   = pages
        reconnect   = comic.progress.get('reconnect', False)
        lastpage    = comic.progress['lastpage']
        if (not lastpage) or startpage: # No previous, or start supplied
            nextpage = startpage or specs['startpage']
            print("- Starting new scrape from", nextpage)
        else:
            print("- Resuming scrape from", lastpage)
            data        = _getter.get_read(lastpage)
            soup        = BeautifulSoup(data)
            nextpage    = findlink(soup, lastpage, link_identifier)

        # Do the actual scraping
        try:
            if nextpage:
                pagestamp = "({0} pages)".format(pages) if (pages != -1) else ""
                print("Starting scrape..."+pagestamp)

            while nextpage and remaining: # There is a way!
                print("- {0:03}: {1}".format(comic.progress['lastindex']+1, nextpage))
                data        = _getter.get_read(nextpage, reconnect=reconnect)
                soup        = BeautifulSoup(data)
                imageurls   = findimages(soup, nextpage, image_identifier)
                if not imageurls:
                    print("No images found at '{0}'! Ending...".format(nextpage))
                    break
                if imageurls == lastimages:
                    print("Image duplicates found at '{0}'! Ending...".format(nextpage))
                    break
                for url in imageurls:
                    comic.add(url, reconnect=reconnect)
                comic.setscraped(nextpage, imageurls)
                lastimages  = imageurls # Don't repeat content!
                lastpage    = nextpage
                nextpage    = findlink(soup, nextpage, link_identifier)
                if nextpage == lastpage:
                    print("No further links found at '{0}'! Ending...".format(lastpage))
                    break
                remaining  -= 1
            lastpage = comic.progress['lastpage']
            print("No more comics found after '{0}', ending...".format(lastpage))
        except KeyboardInterrupt:
            print("\nInterrupted!")
            print("Progress:")
            print("- Last page: ", comic.progress['lastpage'])
            print("- Last index:", comic.progress['lastindex'])
            print("Closing...", end=" ")
        except Exception as e:
            print("CAUGHT EXCEPTION!")
            print("Closing...", end=" ")
            raise

    meta = {"lastcomic": title}
    with open(lastcomicfile, "w") as f:
        toml.dump(meta, f)
    print("Scrape ended.")


"""
Flat text button GUI element.
"""

from PIL import ImageDraw

import arcade
from .button_abstract import ButtonAbstract
from .theme import Theme

class FlatTextButton(ButtonAbstract):
    """ Flat button with text """
    def __init__(self,
                 text,
                 center_x,
                 center_y,
                 width,
                 height,
                 theme: Theme
                 ):
        super().__init__(center_x, center_y)

        assert theme.text_color is not None
        assert theme.background_color is not None
        assert theme.text_color_mouse_over is not None
        assert theme.background_color_mouse_over is not None

        text_image_normal = arcade.get_text_image(text,
                                                  theme.text_color,
                                                  width=width,
                                                  height=height,
                                                  align="center",
                                                  valign="middle",
                                                  background_color=theme.background_color,
                                                  font_size=theme.font_size,
                                                  font_name=theme.font_name)
        text_image_mouse_over = arcade.get_text_image(text,
                                                      theme.text_color_mouse_over,
                                                      width=width,
                                                      height=height,
                                                      align="center",
                                                      valign="middle",
                                                      background_color=theme.background_color_mouse_over,
                                                      font_size=theme.font_size,
                                                      font_name=theme.font_name)
        text_image_mouse_press = arcade.get_text_image(text,
                                                       text_color=theme.text_color_mouse_press,
                                                       width=width,
                                                       height=height,
                                                       align="center",
                                                       valign="middle",
                                                       background_color=theme.background_color_mouse_press,
                                                       font_size=theme.font_size,
                                                       font_name=theme.font_name)

        rect = [0, 0, text_image_normal.width - theme.border_width / 2, text_image_normal.height - theme.border_width / 2]

        if theme.border_color and theme.border_width:
            d = ImageDraw.Draw(text_image_normal)
            d.rectangle(rect, fill=None, outline=theme.border_color, width=theme.border_width)

        if theme.border_color_mouse_over:
            d = ImageDraw.Draw(text_image_mouse_over)
            d.rectangle(rect, fill=None, outline=theme.border_color_mouse_over, width=theme.border_width)

        if theme.border_color_mouse_press:
            d = ImageDraw.Draw(text_image_mouse_press)
            d.rectangle(rect, fill=None, outline=theme.border_color_mouse_press, width=theme.border_width)

        self.texture = arcade.Texture(image=text_image_normal, name=text)
        self.normal_texture = self.texture
        self.mouse_press_texture = arcade.Texture(image=text_image_mouse_press, name=text+"4")
        self.mouse_over_texture = arcade.Texture(image=text_image_mouse_over, name=text+"5")


import graphene
from django.core.exceptions import ValidationError
from django.core.exceptions import ObjectDoesNotExist

from ...checkout.models import Checkout, CheckoutLine
from ...product.models import ProductVariant
from .types import CheckoutLineType, CheckoutType

class CheckoutLineCreateInput(graphene.InputObjectType):
    quantity = graphene.Int(required=True, description="The number of items purchased.")
    variant_id = graphene.ID(required=True, description="ID of the product variant.")

class CheckoutCreateInput(graphene.InputObjectType):
    user_email = graphene.String()
    lines = graphene.List(CheckoutLineCreateInput, required=True)

class CheckoutCreate(graphene.Mutation):
    checkout = graphene.Field(CheckoutType)

    class Arguments:
        input = CheckoutCreateInput(required=True)

    @classmethod
    def mutate(cls, root, info, input):
        lines = input.pop('lines')
        checkout = Checkout.objects.create(**input)

        checkout_lines = []
        for line in lines:
            checkout_lines.append(
                CheckoutLine(checkout_id=checkout.id, **line)
            )
        checkout.lines.bulk_create(checkout_lines)
        return CheckoutCreate(checkout=checkout)

class CheckoutLineCreate(graphene.Mutation):
    checkout_line = graphene.Field(CheckoutLineType)

    class Arguments:
        input = CheckoutLineCreateInput(required=True)
        checkout_id = graphene.ID(required=True)

    @classmethod
    def mutate(cls, root, info, input, checkout_id):

        variant_id = input.pop('variant_id')
        input_quantity = input.pop('quantity')

        try:
            checkout_line = CheckoutLine.objects.get(checkout_id=checkout_id, variant=variant_id)
        except ObjectDoesNotExist():
            checkout_line = CheckoutLine.objects.create(
                checkout_id=checkout_id,
                variant_id=variant_id,
                quantity=input_quantity)

            return CheckoutLineCreate(checkout_line=checkout_line)

        quantity = checkout_line.quantity + input_quantity

        checkout_line.quantity = quantity
        checkout_line.save()

        return CheckoutLineCreate(checkout_line=checkout_line)


# Support for py 2 and 3
from __future__ import division, print_function, unicode_literals

# Common Imports
import os
import numpy as np

# ML Imports

# Graph Imports
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Config
PROJECT_ROOT_DIRECTORY = '.'

# Define Functions

def image_path(fig_id):
    if not os.path.exists('images'):
        os.makedirs('images')
    return os.path.join(PROJECT_ROOT_DIRECTORY, 'images', fig_id)

def save_fig(fig_id, tight_layout=True):
    print("Saving", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(image_path(fig_id) + ".png", format='png', dpi=300)

# Set plots
angle = np.pi / 5
stretch = 5
m = 200

np.random.seed(3)
X = np.random.randn(m, 2) / 10
X = X.dot(np.array([[stretch, 0], [0, 1]]))
X = X.dot([[np.cos(angle), np.sin(angle)], [-np.sin(angle), np.cos(angle)]])

u1 = np.array([np.cos(angle), np.sin(angle)])
u2 = np.array([np.cos(angle - 2 * np.pi / 6), np.sin(angle - 2 * np.pi / 2)])
u3 = np.array([np.cos(angle - np.pi / 2), np.sin(angle - np.pi / 2)])

X_proj1 = X.dot(u1.reshape(-1, 1))
X_proj2 = X.dot(u2.reshape(-1, 1))
X_proj3 = X.dot(u3.reshape(-1, 1))

plt.figure(figsize=(8, 4))
plt.subplot2grid((3, 2), (0, 0), rowspan=3)
plt.plot(
    [-1.4, 1.4], [-1.4 * u1[1] / u1[0], 1.4 * u1[1] / u1[0]],
    "k-",
    linewidth=1)
plt.plot(
    [-1.4, 1.4], [-1.4 * u2[1] / u2[0], 1.4 * u2[1] / u2[0]],
    "k--",
    linewidth=1)
plt.plot(
    [-1.4, 1.4], [-1.4 * u3[1] / u3[0], 1.4 * u3[1] / u3[0]],
    "k:",
    linewidth=2)
plt.plot(X[:, 0], X[:, 1], "bo", alpha=0.5)
plt.axis([-1.4, 1.4, -1.4, 1.4])
plt.arrow(
    0,
    0,
    u1[0],
    u1[1],
    head_width=0.1,
    linewidth=5,
    length_includes_head=True,
    head_length=0.1,
    fc='k',
    ec='k')
plt.arrow(
    0,
    0,
    u3[0],
    u3[1],
    head_width=0.1,
    linewidth=5,
    length_includes_head=True,
    head_length=0.1,
    fc='k',
    ec='k')
plt.text(u1[0] + 0.1, u1[1] - 0.5, r"$\mathbf{c_2}$", fontsize=22)
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$x_2$", fontsize=18, rotation=0)
plt.grid(True)

plt.subplot2grid((3, 2), (0, 1))
plt.plot([-2, 2], [0, 0], "k-", linewidth=1)
plt.plot(X_proj1[:, 0], np.zeros(m), "bo", alpha=0.3)
plt.gca().get_xaxis().set_ticks([])
plt.gca().get_yaxis().set_ticklabels([])
plt.axis([-2, 2, -1, 1])
plt.grid(True)

plt.subplot2grid((3, 2), (1, 1))
plt.plot([-2, 2], [0, 0], "k--", linewidth=1)
plt.plot(X_proj2[:, 0], np.zeros(m), "bo", alpha=0.3)
plt.gca().get_yaxis().set_ticks([])
plt.gca().get_xaxis().set_ticklabels([])
plt.axis([-2, 2, -1, 1])
plt.grid(True)

plt.subplot2grid((3, 2), (1, 1))
plt.plot([-2, 2], [0, 0], "k--", linewidth=1)
plt.plot(X_proj2[:, 0], np.zeros(m), "bo", alpha=0.3)
plt.gca().get_yaxis().set_ticks([])
plt.gca().get_xaxis().set_ticklabels([])
plt.axis([-2, 2, -1, 1])
plt.grid(True)

plt.subplot2grid((3, 2), (2, 1))
plt.plot([-2, 2], [0, 0], "k:", linewidth=1)
plt.plot(X_proj3[:, 0], np.zeros(m), "bo", alpha=0.3)
plt.gca().get_yaxis().set_ticks([])
plt.axis([-2, 2, -1, 1])
plt.xlabel("$z_1$", fontsize=18)
plt.grid(True)

save_fig("pca_best_projection")
plt.show()


from __future__ import unicode_literals

import sys
import traceback

from termcolor import colored

from .utils import download_and_metadata, get_video_entries, menu, welcome

def main() -> None:
    """
    Main function to run the pyyt script.
    """
    print(colored("Select your option:", "cyan"))
    options = [
        "Download an entire playlist as audio",
        "Download a single video as audio",
    ]
    selected_option = menu(options)

    if selected_option == 0:
        playlist_url = input(
            colored("Please insert the YouTube playlist URL: ", "magenta")
        )
        video_entries = get_video_entries(playlist_url)
        video_list = [
            f'https://www.youtube.com/watch?v={video["id"]}' for video in video_entries
        ]
        download_and_metadata(video_list)

    elif selected_option == 1:
        video_url = input(colored("Please insert the YouTube video URL: ", "magenta"))
        download_and_metadata([video_url])

    else:
        print(colored("Wrong option. Cya!", "magenta"))

if __name__ == "__main__":
    welcome()
    try:
        main()
    except KeyboardInterrupt:
        sys.exit()

    except Exception as err:
        print(traceback.format_exc())
        print("\n", err, "\n")


#!/usr/bin/python3
#
# ISC License (ISC)
#
# Copyright (c) 2016, Austin Hellyer <hello@austinhellyer.me>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# What is ISO 15924?
# | ISO 15924, Codes for the representation of names of scripts, defines two
# | sets of codes for a number of writing systems (scripts). Each script is
# | given both a four-letter code and a numeric one.[1] Script is defined as
# | "set of graphic characters used for the written form of one or more
# | languages".
# |
# | - [wikipedia](https://en.wikipedia.org/wiki/ISO_15924)
#
# Originally by zeyla on GitHub.

from bs4 import BeautifulSoup
import os
import re
import subprocess
import sys
import urllib.request

url = 'http://unicode.org/iso15924/iso15924-codes.html'
html = urllib.request.urlopen(url).read()

soup = BeautifulSoup(html, 'html.parser')

rows = soup.find_all('table')[3] \
    .find_all('tr')[1:]

text = ""

for row in rows:
    # 0: code
    # 1: num
    # 2: name
    # 3: french name
    # 4: alias [Property Value Alias]
    # 5: date
    cells = row.find_all('td')

    if len(cells) != 6:
        continue

    code = cells[0].get_text()
    num = cells[1].get_text()
    name = cells[2].get_text()
    name_french = cells[3].get_text()

    # If the alias cell contains anything more than a single space, then it has
    # a value (Rust's `Some`), otherwise it is Rust's `None`.
    if len(cells[4].get_text()) < 2:
        alias = 'None'
    else:
        alias = 'Some("{}")'.format(cells[4].get_text())

    date_data = cells[5].get_text().split("-")

    # If the date doesn't exist or incorrectly parses into other than 3 items in
    # a list, then skip this row. The date needs to parse into 3 items in the
    # list.
    if len(date_data) != 3:
        continue

    # year, month, day
    y = date_data[0]
    m = date_data[1]
    d = date_data[2]

    # Push formatted text for a `ScriptCode`.
    text += '    list.push(ScriptCode {\n'
    text += '        alias: {},\n'.format(alias)
    text += '        code: "{}",\n'.format(code)
    text += '        date: ScriptDate::new({}, {}, {}).unwrap(),\n'.format(y, m, d)
    text += '        name: "{}",\n'.format(name)
    text += '        name_french: "{}",\n'.format(name_french)
    text += '        num: "{}",\n'.format(num)
    text += '    });\n'

# Open the file with the Vec of codes.
all_path = os.path.join(os.path.dirname(__file__), '../src/codes.rs')

with open(all_path, 'r') as f:
    all_file = f.read()

# Concat in the `text` between the beginning and end of the codes file contents.
current = all_file.rsplit('// Begin.', 1)
current_e = all_file.rsplit('// End.\n', 1)

with open(all_path, 'w') as f:
    f.write(current[0] + '// Begin.\n' + text + '    // End.\n' + current_e[1])

print('Updated.')


from django.shortcuts import render
from . import forms

# Create your views here.

def indexView(request):

	context = { 'title': 'Index'}
	return render(request, 'userApp/index.html', context)

def signUpView(request):

	form = forms.myForm()

	if request.method == 'POST':
		form = forms.myForm(request.POST)

		if form.is_valid():
			form.save(commit = True)
			return indexView(request)

	context = {
	'form' : form,
	'title': 'Sign Up'
	}

	return render(request, 'userApp/signup.html', context)


import os
from sqlalchemy import exc

from src import db, create_app
from src.blueprints.admin.models import Permission, Model

app = create_app(os.getenv('APP_SETTINGS'))

def set_model_perms(model, actions=[
        'add', 'delete', 'edit', 'view'], is_table=False):

    if is_table:
        name = str(model)
    else:
        name = f'{model.__name__}s'

    try:
        db.session.query(model)
    except exc.InvalidRequestError:
        return f"Error: {name} is not a valid sqlalchemy model or table."
    else:
        with app.app_context():
            try:
                mod = Model(name=name)
                mod.save()

                for action in actions:
                    perm = Permission(name=f'can {action} {name}')
                    perm.model_id = mod.id
                    perm.save()
            except (exc.IntegrityError, ValueError) as error:
                db.session.rollback()
                return f'Error: {error}'


import pandas as pd
from numpy import*
import matplotlib.pyplot as plt
from astropy.table import Table

#Import data
Stocks = pd.read_excel(r'C:\Users\Viktor\Desktop\Finansiell ekonomi\OMXS30.xlsx', sheet_name = 0)

Stockprices = Stocks['X / SEK']
OMXS30 = Stocks['OMXS30']

def R(prices):
    length = len(prices)-1
    Rvector = []
    for i in range(length):
        R = (prices.iloc[i+1]-prices.iloc[i])/prices.iloc[i]
        Rvector.append(R)
    return Rvector

RStocks = R(Stockprices)
ROMX = R(OMXS30)
Dates = Stocks['Datum'].loc[1:]
df = pd.DataFrame()
df['Dates'] = Dates
df['R_OMX'] = ROMX
df['R_Stocks'] = RStocks

#PART2_PLOTS
"""df.plot(x = 'Dates',y = 'R_OMX')
df.plot(x = 'Dates',y = 'R_Stocks')
df.hist(column = 'R_OMX')
df.hist(column = 'R_Stocks')
Stocks.plot(x = 'Datum', y = 'OMXS30' )
Stocks.plot(x = 'Datum', y = 'X / SEK')"""
#plt.show()

#PART3_DESCRIPTIVE_STATISTICS
MAX_OMX = max(df['R_OMX'])
MAX_Stocks = max(df['R_Stocks'])
MIN_OMX = min(df['R_OMX'])
MIN_Stocks = min(df['R_Stocks'])
MED_OMX = float(median(df['R_OMX']))
MED_Stocks = float(median(df['R_Stocks']))
MEAN_OMX = mean(df['R_OMX'])
MEAN_Stocks = mean(df['R_Stocks'])
STD_OMX = std(df['R_OMX'])
STD_Stocks = std(df['R_Stocks'])
SKEWNESS_OMX = mean(((df['R_OMX']-MEAN_OMX)**3)/(STD_OMX**3))
SKEWNESS_Stocks = mean(((df['R_Stocks']-MEAN_Stocks)**3)/(STD_Stocks**3))
KURTOSIS_OMX = mean(((df['R_OMX']-MEAN_OMX)**4)/(STD_OMX**4))
KURTOSIS_Stocks = mean(((df['R_Stocks']-MEAN_Stocks)**4)/(STD_Stocks**4))

st = Table()
st['DATA'] = ['OMX', 'Stock X']
st['MEAN'] = [MEAN_OMX,MEAN_Stocks]
st['STD'] = [STD_OMX,STD_Stocks]
st['SKEWNESS'] = [SKEWNESS_OMX,SKEWNESS_Stocks]
st['KURTOSIS'] = [KURTOSIS_OMX,KURTOSIS_Stocks]
#OMXS30 is obs more like normal distribution. Higher risk in stock X
print(st)

#PART4 obligations
def ytm(FV,C,n,y,marketprice):
    n = n
    y = linspace(-0.1,0.1,10000000)
    P = (C / y) * (1 - (power((1 + y), -n))) + (FV) / (power((1 + y), n))
    Thisval = where((abs(marketprice - P) == min(abs(marketprice - P))))
    print(min(abs(marketprice - P)))
    #print(Thisval)
    yie = (y[Thisval])
    return yie

Bonds = pd.read_excel(r'C:\Users\Viktor\Desktop\Finansiell ekonomi\OMXS30.xlsx', sheet_name = 1, skiprows=4)
dfb = pd.DataFrame()
dfb['Market val'] = Bonds['Marknadsvärde']
dfb['FV'] = Bonds['Nominellt belopp']
dfb['n'] = Bonds['Löptid']
dfb['Start yield'] = Bonds['Kupongränta']/100
dfb['C'] = dfb['FV']*dfb['Start yield']

ytmlist = []
for num in range(8):
    a = (ytm(dfb['FV'].iloc[num],dfb['C'].iloc[num],dfb['n'].iloc[num],dfb['Start yield'].iloc[num], dfb['Market val'].iloc[num]))
    ytmlist.append(a)
print(ytmlist)

nlist = [dfb['n'].iloc[i] for i in range(8)]
plt.scatter(nlist,ytmlist)
plt.show()


from django.http import JsonResponse,HttpResponse
import json
from . import models

from django.db.models import Q

from django.db.models import Count, Min, Max, Sum , Avg
def job(request):
    return JsonResponse({"code": "job"})

def searchjob(request):
    try:
        qid=request.GET.get('id')
        # print(qid)
        # jobs=models.job.objects.filter(job_id=qid,title='RPA软件售前工程师（Python）').values()

        # return HttpResponse(json.dumps(list(jobs),ensure_ascii=False))
    #     删除
        affected_rows=models.job.objects.filter(job_id=qid).delete()

        print(affected_rows[0])

        if affected_rows[0]:
            return JsonResponse({"code": "808"})
        else:
            return JsonResponse({"code": "410"})
    except Exception as ex:
        return JsonResponse({"code": "409"})

def add(request):
    if request.method=='POST':
        try:
            job=json.loads(request.body)
            # print(job)
            # res就是正在插入的对象
            # res = models.job.objects.create(**job)
            # print(res.id)
            res=models.job(**job).save(force_update=True)

            print(res)
            return JsonResponse({"code": "808"})
        except Exception as ex:
            return JsonResponse({"code": "408"})

    else:
        return JsonResponse({"code": "408"})

def addall(request):
    if request.method=='POST':
        try:
            # 读取本地文件
            with open('job51python.json','r',encoding='utf-8') as fp:
                jobs=json.load(fp)
                print('****************************')
                print(len(jobs))
                for job in jobs:
                    res = models.job(**job).save()
            return JsonResponse({"code": "808"})
        except Exception as ex:
            return JsonResponse({"code": "408"})

    else:
        return JsonResponse({"code": "408"})
def getjobs(request,index,con):
    try:
        # 1. 这个时候返回的是对象集合（job对象集合）
        # jobs=models.job.objects.all()
        # for job in jobs:
        #     print(job.title)
        index=int(index)
        pagesize=20
        # 2. 获取部分列
        # jobs = models.job.objects.all().values() #取所有列
        # jobs = models.job.objects.filter(~Q(salary_min=10000)).values('title','com_name','salary_min')
        # jobs = models.job.objects.filter(com_name__in=['梅花网','上海每联每信息技术有限公司大连分公司']).values('title','com_name','salary_min')
        # jobs = models.job.objects.exclude(com_name__in=['梅花网','上海每联每信息技术有限公司大连分公司']).values('title','com_name','salary_min')
        # jobs = models.job.objects.filter(title__icontains='Python').values('title','com_name','salary_min')
        # jobs = models.job.objects.all().values('date').annotate(m=Max('salary_min'))
        if con:
            jobs = models.job.objects.filter(title__regex=con)[pagesize*(index-1):pagesize*index].values('job_id', 'title', 'com_name', 'com_address', 'date', 'salary')
        else:
            jobs = models.job.objects.all()[pagesize*(index-1):pagesize*index].values('job_id', 'title', 'com_name', 'com_address', 'date', 'salary')

        # select date, max(salary_min) as m from job group by date
        print(len(jobs))
        # 3. 获取部分列
        # jobs = models.job.objects.all().values_list('title', 'com_name')
        # print(jobs)
        return HttpResponse(json.dumps(list(jobs),ensure_ascii=False))
    except Exception as ex:
        return JsonResponse({"code": "409"})
def pagecount(request,con):
    try:
        if con:
            jobs = models.job.objects.filter(title__regex=con).values('job_id', 'title', 'com_name', 'com_address', 'date', 'salary')
        else:
            jobs = models.job.objects.all().values('job_id', 'title', 'com_name', 'com_address', 'date', 'salary')

        print(len(jobs))
        # 3. 获取部分列
        # jobs = models.job.objects.all().values_list('title', 'com_name')
        # print(jobs)
        result={
            "count":len(jobs)
        }
        return HttpResponse(json.dumps(result,ensure_ascii=False))
    except Exception as ex:
        return JsonResponse({"code": "409"})
    # pass
def query(request):
    if request.method == 'POST':
        try:
            #多对多查询
            # info={
            #     "yh_id":"138",
            #     "kw_id":"100261076"
            # }
            # res=models.toudi.objects.create(**info)
            # print(res)

            # user_id=models.toudi.objects.filter(kw_id='100261076').values('yh__id')

            # work=models.job.objects.get(job_id='100261076')
            # print(job)

            # list=[3,4,5]
            # obj=job.apply.add(*list)
            # #
            # print(obj)
            # print(work)
            # # users=work.apply.all().values('job__apply__telephone')
            # users=work.apply.all()
            #
            # print(users)

            #1. 查询3号用户投递了哪些岗位

            obj=models.toudi.objects.filter(yh_id=3).values('kw__title','kw__com_name')
            print(obj)
            return JsonResponse({"code": "808"})
        except Exception as ex:
            return JsonResponse({"code": "408"})

    else:
        return JsonResponse({"code": "408"})

# MVC model view controller


#!/usr/bin/env python
import sys
import argparse
from filehelper import getContent
from get import getUrl
from post import postUrl
sys.dont_write_bytecode = True

def checkContentType(js):
    for pair in js:
        key, value = pair.split(':')
        if key == "Content-Type":
            if value.find("text") or value.find("multipart") or value.find("message") or value.find("image") or value.find("audio") or value.find("video") or value.find("application"):
                return True
    return False

parser = argparse.ArgumentParser(prog='httpc', add_help=False,  description = 'cURL like functionality to do GET and POST requests in python',
                                    usage="httpc (get|post) [-v] (-h \"k:v\")* [-d inline-data] [-f file] URL")

parser.add_argument("request" ,default = 'get',
                    nargs='?',
                    choices={'get', 'post'},
                    help="http request chosen to use")
parser.add_argument("-v", "--verbosity", action="store_true",
                    help="increase output verbosity")
parser.add_argument('-h',"--headers", nargs='*')
parser.add_argument('-d', '--data', nargs ='*', help = "the data to pass as the post request")
parser.add_argument('-f', '--files', nargs = '*', help = "Files to pass along the post request. Please place the file into this directory to pass it along an HTTP request")
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('url',help='url to do request upon')

args = parser.parse_args()

rawUrl = args.url
requestType = args.request
verbose = False
parsed_headers = {}
parsed_data = {}
parsed_file_data = {}

if args.verbosity:
            verbose = True

if args.headers:
    headerSeparator = '='
    if requestType == 'post':
        if not checkContentType(args.headers):
            print("Headers for a post request can only be of type text / multipart / message / image/ audio/video/application")
            exit()
        headerSeparator = ':'
    for pair in args.headers:
        key, value = pair.split(headerSeparator)
        parsed_headers[key] = value

if args.data:
    if args.request == 'get':
        print("Cannot use get with data. Please try this again with a post")
        quit()
    for pair in args.data:
        print(args.data)
        key, value = pair.split(':')
        parsed_data[key] = value

if args.files:
    if args.request == 'get':
        print("Cannot use get with data. Please try this again with a post")
        quit()
    parsed_file_data = getContent(args.files)

if rawUrl.startswith("http://www."):
    rawUrl = rawUrl[11:]
if rawUrl.startswith("www."):
    rawUrl = rawUrl[4:]

if requestType == 'get':
    getUrl(verbose,rawUrl,parsed_headers)
elif requestType == 'post':
    postUrl(verbose, rawUrl, parsed_headers, parsed_data, parsed_file_data)


import json
import os

import requests

with open("cfg\\cluster_node.json") as cfg_file:
    cfg = json.load(cfg_file)

Consumers = cfg["seeds"]
DataFeedList = os.listdir("data")
try:
    DataFeedList.remove('DataLunch.json')
    DataFeedList.remove('DataFeed.json')
except Exception:
    pass
MAP = []

def data_check():
    for seed in cfg["seeds"]:
        COMPLETE = False
        for data_feed in DataFeedList:
            if '{}_{}'.format(seed.split(':')[0], seed.split(':')[1]) in data_feed:
                COMPLETE = True
        if not COMPLETE:
            return False
    return True

def data_feeder():
    for item in DataFeedList:
        with open('data\\{}'.format(item)) as feed_file:
            data = json.load(feed_file)
        MAP.append(list(map(int, data["DataFeed"].split(','))))
    pass

def merge(list1, list2):
    RESULT = []
    while list1.__len__() > 0 and list2.__len__() > 0:
        if list1[0] < list2[0]:
            RESULT.append(list1[0])
            list1 = list1[1:]
        else:
            RESULT.append(list2[0])
            list2 = list2[1:]
    for item in list1:
        RESULT.append(item)
    for item in list2:
        RESULT.append(item)
    return RESULT

def reduce(_map):
    data_feeder()
    while _map.__len__() > 2:
        _map[1] = merge(_map[0], _map[1])
        _map = _map[1:]
    if _map.__len__() == 1:
        return _map[0]
    return merge(_map[0], _map[1])

def mpi_receive(_map):
    _map = reduce(_map)
    data_feed = {'DataFeed': str(_map)[1:-1]}
    with open(
            'data\\DataFeed.json', 'w'
    ) as output_file:
        json.dump(data_feed, output_file)

if __name__ == '__main__':
    if data_check():
        mpi_receive(MAP)

        data_feed = {"DataFeed": str(MAP[1:-1]),
                     "seed": '{}_{}'.format(cfg["this"].split(':')[0], cfg["this"].split(':')[1])}

        for consumer in Consumers:
            if consumer != cfg["this"]:
                requests.post(
                    '{}{}{}'.format("http://", consumer, "/mpi_receive"),
                    json=data_feed
                )


from selenium import webdriver
from lxml import etree
import time

url = 'https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9' \
      'C%AC&enc=utf-8&wq=%E7%AC%94%E8%AE%B0%E6%9C%AC&pvid=845d019c94f6476ca5c4ffc24df6865a'
driver = webdriver.Chrome()
driver.get(url)
script =  "var q=document.body.scrollTop=0"
driver.execute_script(script)
time.sleep(2)
driver.save_screenshot('jd.png')
print(driver.page_source)
html = driver.page_source
e = etree.HTML(html)
prices = e.xpath('//*[@id="J_goodsList"]/ul/li/div/div[2]/strong/i/text()')
names = e.xpath('//*[@id="J_goodsList"]/ul/li/div/div[3]/a/em')
for p, n in zip(names, prices):
    print(n+':'+p)
print(len(prices))
driver.close()


import sqlparse
import re
import csv
from functools import cmp_to_key
from math import inf
database = {}
def fill_db_meta(meta):
    with open(meta,"r") as f:
        metadata = f.readlines()
    lines = len(metadata)
    i=0
    while(i<lines):
        if "<begin_table>" in metadata[i]:
            i=i+1
            table = {}
            database[metadata[i].strip()] = table
            table["cols"] = {}
            table["cols_order"] = []
            i=i+1
            while True:
                if "<end_table>" in metadata[i].strip():
                    break
                table["cols"][metadata[i].strip()]=[]
                table["cols_order"].append(metadata[i].strip())
                i=i+1

        i=i+1

aggrs = {"sum":0,"avg":1,"max":2,"min":3,"count":4}
def fill_db_data():
    for table in database.keys():
        with open(table+".csv","r") as f:
            data = csv.reader(f)
            for row in data:
                for i in range(len(row)):
                    database[table]["cols"][database[table]["cols_order"][i]].append(int(row[i]))

def recur(tables,newrow,newtable):
    if(len(tables)==0):
        newtable.append(newrow)
        return
    curtable = tables[0]
    temprow=newrow[:]
    for j in range(len(curtable["cols"][curtable["cols_order"][0]])):
        for i in range(len(curtable["cols_order"])):
            newrow.append(curtable["cols"][curtable["cols_order"][i]][j])
        recur(tables[1:],newrow,newtable)
        newrow=temprow[:]

def join_tables(tablelist):
    newtable={}
    newtable["cols"] = {}
    newtable["rows"] = []
    tables = []
    for table in tablelist:
        tables.append(database[table])
    i=0
    for table in tables:
        for col in range(len(table["cols_order"])):
            newtable["cols"][table["cols_order"][col]] = i
            newtable["cols"][i] = table["cols_order"][col]
            i+=1
    recur(tables,[],newtable["rows"])
    return newtable

def f_count(newtable,cmd):
        return len(newtable["rows"])

def f_sum(newtable,cmd):
    sums = 0
    pos = newtable["cols"][cmd.strip()]
    for row in newtable["rows"]:
            sums+=row[pos]
    return sums

def f_avg(newtable,cmd):
    sums = f_sum(newtable,cmd)
    n = len(newtable["rows"])
    avg = sums/n
    return avg

def f_max(newtable,cmd):
    maxs = -inf
    pos = newtable["cols"][cmd.strip()]
    for row in newtable["rows"]:
            maxs = max(maxs,row[pos])
    return maxs

def f_min(newtable,cmd):
    mins = inf
    pos = newtable["cols"][cmd.strip()]
    for row in newtable["rows"]:
            mins = min(mins,row[pos])
    return mins

def aggr(newtable,params):
    cmd = [i.strip() for i in re.split("\(|\)",params) if i.strip()!=""]
    res = 0
    if(cmd[0]=="count"):
        res= f_count(newtable,cmd[1])
    elif(cmd[0]=="sum"):
        res= f_sum(newtable,cmd[1])
    elif(cmd[0]=="avg"):
        res= f_avg(newtable,cmd[1])
    elif(cmd[0]=="max"):
        res= f_max(newtable,cmd[1])
    elif(cmd[0]=="min"):
        res= f_min(newtable,cmd[1])
    return res

def group_by_parse(group_by_part,newtable):
    cmd = group_by_part.split(",")
    pos = [newtable["cols"][i.strip()] for i in cmd]

def order_by_parse(order_by_part,newtable):
    cmd = order_by_part.split(",")
    cmd = [i.split() for i in cmd]
    print(cmd)
    def comp(val1,val2):
        for i in cmd:
            if(len(i)==1):
                pos = newtable["cols"][i[0]]
                if(val1[pos] < val2[pos]):
                    return -1
                elif(val1[pos] > val2[pos]):
                    return 1
            else:
                pos = newtable["cols"][i[0]]
                if "desc" in i:
                    if(val1[pos] > val2[pos]):
                        return -1
                    elif(val1[pos] < val2[pos]):
                        return 1
                else:
                    if(val1[pos] < val2[pos]):
                        return -1
                    elif(val1[pos] > val2[pos]):
                        return 1
        return 0
    newtable["rows"].sort(key=cmp_to_key(comp))

def show_table(newtable):
    newtable["rows"].insert(0,newtable["ord"])
    for row in newtable["rows"]:
        for els in row:
            if(els==None):
                print("",end="\t")
            else:
                print(els,end="\t")
        print("")

def where_parse(where_part,newtable):
    temp = where_part
    where_part = re.split("and|or",where_part)
    where_part = [i.strip() for row in where_part for i in re.split("<=|>=|!=|==|<|>",row) if i.strip() != ""]
    print(where_part)
    temptable = []
    for row in newtable["rows"]:
        tempstr = temp
        for els in where_part:
            if(els.isnumeric()):
                pass
            else:
                val = row[int(newtable['cols'][els])]
                tempstr = tempstr.replace(els,str(val))
        if(eval(tempstr)):
            temptable.append(row)

    newtable["rows"] = temptable

def make_distinct(newtable):
    b = []
    for row in newtable["rows"]:
        if row not in b:
            b.append(row)
    newtable["rows"] = b

def select_parse(select_part,newtable):
    isdistict = False
    isaggr = False
    if "distinct" in select_part:
        isdistict = True
    col_list = [cols.strip() for temp in re.split("distinct",select_part) for cols in temp.split(",") if (cols.strip() != "")]
    if(len(col_list)==0):
        print("No column selected")
    if(len(col_list)==1 and col_list[0] == "*"):
        newrow = []
        for i in newtable["cols"].keys():
            if(str(i).isnumeric()):
                pass
            else:
                newrow.append(i)

        if(isdistict):
            make_distinct(newtable)

        newtable["ord"]=newrow
        ord = {}
        for i in range(len(newrow)):
            ord[newrow[i]] = i
            ord[i] = newrow[i]
        newtable["cols"] = ord
        return

    temptable=[]
    for j in col_list:
        temp = re.split("\(|\)",j)[0].strip()
        try:
            aggrs[temp]
            isaggr=True
        except KeyError:
            pass
    if(isaggr):
        temprow=[]
        for j in col_list:
            temp = re.split("\(|\)",j)[0].strip()
            print(temp)
            try:
                aggrs[temp]
                temprow.append(aggr(newtable,j))
            except KeyError:
                try:
                    temprow.append(newtable["row"][0][newtable["cols"][j]])
                except KeyError:
                    temprow.append(None)

        newtable["rows"][:] = [col_list,temprow]
    else:
        for i in newtable["rows"]:
            temprow=[]
            for j in col_list:
                temprow.append(i[newtable["cols"][j]])
            temptable.append(temprow)
        newtable["rows"] = temptable
    if(isdistict):
        make_distinct(newtable)

    newtable["ord"]=col_list
    ord = {}
    for i in range(len(col_list)):
        ord[col_list[i]] = i
        ord[i] = col_list[i]
    newtable["cols"] = ord

def parse(query):
    query = sqlparse.format(query,keyword_case  = 'lower')
    if ";" not in query:
        print("No ;")
        return
    if "select" not in query or "from" not in query:
        print("No select or from")
        return
    select_part=""
    tables=[]
    from_part=""
    where_part=""
    group_by_part=""
    order_by_part=""
    query =  query.strip(";")
    q = query

    select_part = re.split("select",q)[1].strip()
    from_part = re.split("from",select_part)
    select_part = from_part[0].strip()
    from_part = from_part[1].strip()
    if "where" in from_part:
        where_part = re.split("where",from_part)[1].strip()
        where_part = re.split("group by|order by",where_part)[0].strip()
    from_part = re.split("where|group by|order by",from_part)[0].strip()
    if "order by" in q:
        order_by_part = re.split("order by",q)[1].strip()
    if "group by" in q:
        if order_by_part=="":
            group_by_part = re.split("group by",q)[1].strip()
        else:
            group_by_part = re.split("group by",q)[1]
            group_by_part = re.split("order by",group_by_part)[0].strip()

    tables = [tabs.strip() for tabs in from_part.split(",")]
    newtable = join_tables(tables)
    if(len(newtable["cols"]) == 0):
        print("No such tables")
        return
    if(where_part):
        where_parse(where_part,newtable)
    if(group_by_part):
        group_by_parse(group_by_part,newtable)
    select_parse(select_part,newtable)
    if(order_by_part):
        order_by_parse(order_by_part,newtable)

    show_table(newtable)

while True:
    fill_db_meta("metadata.txt")
    fill_db_data()
    q = input()
    parse(q)


from django import forms
from django.utils.translation import ugettext_lazy as _

from . import validators

class EasyCMSURLField(forms.CharField):

    def __init__(self, max_length=None, min_length=None, help_text=None, *args, **kwargs):
        kwargs['help_text'] = kwargs.get(
            'help_text',
            _("Example: products or /section-1/products/ or   \"some_named_url\"")
        )
        super(EasyCMSURLField, self).__init__(max_length, min_length, *args, **kwargs)
        self.validators.append(validators.EasyCMSURLValidator())


import os
from setuptools import setup, find_packages

path = os.path.abspath(os.path.dirname(__file__))
readme = open(path + "/docs/README.md")

setup(name='house_price_prediction',
      version='0.1.0',
      description='predict house price',
      url='github.',
      author='hphan',
      author_email='mliafol.phan86@gmali.com',
      license='',
      packages=find_packages(exclude=["tests", "docs", ".gitignore"]),
      classifiers = [
		  "Programming Language :: Python :: 3",
      ],
      install_requires=[
        "coloredlogs>=10.0",
        "humanfriendly>=4.18",
        "numpy>=1.17.4",
        "scipy>=1.3.2",
        "statsmodels>=0.11.1",
        "scikit-learn>=0.21.3"
      ],
      zip_safe=False,
      include_package_data=True)


# coding=utf-8
import random
import requests
import hashlib
import time

# 获取验证码图片URL
def get_verify_image_url():
    try:
        headers = {
            'Host': 'cache.neea.edu.cn',
            'Referer': 'http://cet.neea.edu.cn/cet/'
        }
        url = "http://cache.neea.edu.cn/Imgs.do?c=CET&ik=123456789123456&t=" + str(random.random())
        response = requests.get(url=url, headers=headers)
        text = response.text
        return text[text.index('http'):text.index('png') + 3]
    except Exception:
        return get_verify_image_url()

# 保存图片并返回相应MD5值
def load_image(path):
    try:
        url = get_verify_image_url()
        response = requests.get(url)
        img = response.content
        with open(path, 'wb') as f:
            f.write(img)
        # MD5
        my_hash = hashlib.md5()
        my_hash.update(img)
        return my_hash.hexdigest(), url[url.index('imgs') + 5:url.index('.png')]
    except Exception:
        return load_image(path)

# 保存图片
def load_image2(path, url):
    try:
        response = requests.get(url)
        img = response.content
        with open(path, 'wb') as f:
            f.write(img)
    except Exception:
        load_image(path)

# main
if __name__ == '__main__':
    with open('captcha.csv', 'r') as csv_file:
        path = 'dataset/train/'
        line = csv_file.readline()
        i = 0
        while line:
            names = line.split(",")
            image_path = path + str(i) + '_' + names[1].strip() + '.png'
            url = 'http://cet.neea.edu.cn/imgs/' + names[0].strip() + '.png'
            print(image_path, url)
            load_image2(image_path, url)
            line = csv_file.readline()
            i += 1


from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.models import User
from .models import Product, Category, Company, Order
from .forms_order import OrderForm
# Create your views here.

def crud(request):

    products = Product.objects.all()
    categories = Category.objects.all()
    companies = Company.objects.all()
    params = {'products': products, 'categories' : categories, 'companies' : companies}

    return render(request, 'shop/index.html', params)

def showcategory(request, cat):

    categories = Category.objects.all()
    companies = Company.objects.all()

    cats = Category.objects.get(name=cat)

    products = Product.objects.filter(category=cats).values()

    params = {'products' : products, 'category' : cat, 'categories' : categories, 'companies' : companies}

    return render(request, 'shop/category.html', params)

def showcompany(request, comp):

    categories = Category.objects.all()
    companies = Company.objects.all()

    comps = Company.objects.get(name=comp)

    products = Product.objects.filter(company=comps).values()

    params = {'products' : products, 'category' : comp, 'categories' : categories, 'companies' : companies}

    return render(request, 'shop/category.html', params)

def showproduct(request, product_id):

    product = Product.objects.get(id=product_id)
    related = Product.objects.exclude(id=product_id).values

    params = {'product' : product, 'related': related}

    return render(request, 'shop/product.html', params)

def order(request):
    if request.method == "POST":
        form = OrderForm(request.POST)
        if form.is_valid(): # All validation rules pass
            order = form.save(commit=False)
            order.user = request.user
            order.status = 'PLACED'
            order.order_id = "OrderId" + str(form.cleaned_data.get('order_id'))
            order.save()
            params = {'submitted' : '1'}
            return render(request, 'shop/order.html', params)
    else:
        form = OrderForm()

    return render(request, 'shop/order.html', {
        'form': form, 'submitted' : '0',
    })


#import dataset
import pandas as pd
#read a dataset
fraud=pd.read_csv("C:\\Users\\jeeva\\Downloads\\R assignment\\random forest\\Fraud_check.csv")
#change categorical column values to numerical data values
fraud.Undergrad[fraud.Undergrad=='YES']=1
fraud.Undergrad[fraud.Undergrad=='NO']=0
fraud.Urban[fraud.Urban=='YES']=1
fraud.Urban[fraud.Urban=='NO']=0

#Rename a taxable income column as Tax
fraud.rename(columns={'Taxable.Income':'Tax'},inplace=True)
#if tax less than 30000 we denote as Risky otherwise Good
fraud.Tax[fraud.Tax<=30000]='Risky'
fraud.Tax[fraud.Tax!='Risky']='Good'
#Rename a marital status column as marital
fraud.rename(columns={'Marital.Status':'Marital'},inplace=True)
fraud.Marital.unique()
#['Single', 'Divorced', 'Married']
#change marital categorical datas to numerical data
def tochn(i):
    if i=='Single':
        return 0
    if i=='Divorced':
        return 1
    if i=='Married':
        return 2
fraud['Marital']=fraud['Marital'].apply(tochn)
#create new column with input variables as first output as last
fraud1=fraud[['Undergrad','Marital','City.Population','Work.Experience','Urban','Tax']]
colnames=list(fraud1.columns)
#predict and target variables assigned
prde=colnames[0:5]
trge=colnames[5]
X=fraud1[prde]
Y=fraud1[trge]
from sklearn.ensemble import RandomForestClassifier#random forest classifier
rf = RandomForestClassifier(n_jobs=2,oob_score=True,n_estimators=750,criterion="entropy")
#N estimators 750 for tree better result
#### Attributes that comes along with RandomForest function
rf.fit(X,Y) # Fitting RandomForestClassifier model from sklearn.ensemble
rf.estimators_ #
rf.classes_ # class labels (output)
rf.n_classes_ # Number of levels in class labels
rf.n_features_  # Number of input features in model 8 here.

rf.n_outputs_ # Number of outputs when fit performed

rf.predict(X)

fraud1['rf_pred'] = rf.predict(X)#create column with prediction

from sklearn.metrics import confusion_matrix
confusion_matrix(fraud1['Tax'],fraud1['rf_pred']) # Confusion matrix

pd.crosstab(fraud1['Tax'],fraud1['rf_pred'])
#nesti=750 good 476 Risky 124 =600 :::::100% accuracy
rf1 = RandomForestClassifier(n_jobs=2,oob_score=True,n_estimators=60,criterion="gini")

#### Attributes that comes along with RandomForest function
rf1.fit(X,Y)
rf1.predict(X)
fraud1['rf1_pred'] = rf1.predict(X)

from sklearn.metrics import confusion_matrix
confusion_matrix(fraud1['Tax'],fraud1['rf1_pred']) # Confusion matrix

pd.crosstab(fraud1['Tax'],fraud1['rf1_pred'])
#nesti=600,300 good 476 Risky 124 =600 :::::100% accuracy
#nesti=60 good 476 Risky 121   goodRisky=3 =600 :::::97% accuracy


import netCDF4
import numpy

nc = netCDF4.Dataset('test/bad_file.nc', 'w', format='NETCDF3_CLASSIC')

nc.Conventions = 'CF-1.7'

time    = nc.createDimension('time'   ,  2)
z       = nc.createDimension('z'      ,  3)
#z2      = nc.createDimension('z2'     ,  3)
lat     = nc.createDimension('lat'    ,  4)
lon     = nc.createDimension('lon'    ,  5)
p       = nc.createDimension('p'      ,  6)
bounds2 = nc.createDimension('bounds2',  2)

# Dimension coordinate variables
time = nc.createVariable('time', 'f8', ('time',))
time.standard_name = "time"
time.units = "days since 2000-1-1"
time.bounds = 'time_bounds' # Bounds variable is not in file
time[...] = numpy.arange(time.size)

z = nc.createVariable('z', 'f8', ('z',))
z.standard_name = "atmosphere_sigma_coordinate"
z.units = "metres"
z.positive = "up"
z.bounds = 'z_bounds'
z.formula_terms = "sigma: z ps: ps ptop: ptop"
z[...] = numpy.arange(z.size)

nc.createVariable('z_bounds', 'f8', ('time', 'bounds2')) #  Bounds span incorrect dimensions

nc.createVariable('ps', 'f8', ('time', 'bounds2')) #  Bounds span incorrect dimensions

nc.createVariable('ptop', 'f8', ('lon', 'lat'))

lat = nc.createVariable('lat', 'f8', ('lat',))
lat.standard_name = 'latitude'
lat.units = "degrees_east"
lat.bounds = 'lat_bounds'
lat[...] = numpy.arange(lat.size)

nc.createVariable('lat_bounds', 'f8', ('lat',)) # Bounds span incorrect dimensions

lon = nc.createVariable('lon', 'f8', ('lon',))
lon.standard_name = 'longitude'
lon.units = "degrees_north"
lon.bounds = 'lon_bounds'
lon[...] = numpy.arange(lon.size)

nc.createVariable('lon_bounds', 'f8', ())  # Bounds span incorrect dimensions

eastward_wind = nc.createVariable('eastward_wind', 'f8', ('time', 'z', 'lat', 'lon'))
eastward_wind.coordinates = 'var1' # Auxiliary/scalar coordinate variable is not in file
eastward_wind[...] = numpy.arange(eastward_wind.size)

nc.close()


import pandas as pd
from datetime import timedelta, date, datetime

def daterange(start_date, end_date):
    for n in range(int(((end_date - start_date).days) + 1)):
        yield start_date + timedelta(n)

def scrape(start_date, end_date):
	start_date = datetime.strptime(start_date, "%Y-%m-%d").date()
	end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
	base_url   = "https://promo.betfair.com/betfairsp/prices/dwbfpricesausplace"

	column_names = ['EVENT_ID',
	                'MENU_HINT',
	                'EVENT_NAME',
	                'EVENT_DT',
	                'SELECTION_ID',
	                'SELECTION_NAME',
	                'WIN_LOSE',
	                'BSP',
	                'PPWAP',
	                'MORNINGWAP',
	                'PPMAX',
	                'PPMIN',
	                'IPMAX',
	                'IPMIN',
	                'MORNINGTRADEDVOL',
	                'PPTRADEDVOL',
	                'IPTRADEDVOL']

	bsp_data_full = pd.DataFrame(columns=column_names)

	for single_date in daterange(start_date, end_date):
	    print("Processing " + str(single_date))
	    try:
	        url = str(base_url) + str(single_date.strftime("%d%m%Y")) + ".csv"
	        current_day_df = pd.read_csv(url)
	        bsp_data_full = pd.concat([current_day_df, bsp_data_full], ignore_index=True, sort=False)
	    except:
	        pass

	return bsp_data_full


def _reset_sys_path():
    # Clear generic sys.path[0]
    import sys
    import os

    resources = os.environ["RESOURCEPATH"]
    while sys.path[0] == resources:
        del sys.path[0]

_reset_sys_path()

def _chdir_resource():
    import os

    os.chdir(os.environ["RESOURCEPATH"])

_chdir_resource()

def _disable_linecache():
    import linecache

    def fake_getline(*args, **kwargs):
        return ""

    linecache.orig_getline = linecache.getline
    linecache.getline = fake_getline

_disable_linecache()

import re
import sys

cookie_re = re.compile(br"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
    default_encoding = "ascii"
else:
    default_encoding = "utf-8"

def guess_encoding(fp):
    for _i in range(2):
        ln = fp.readline()

        m = cookie_re.search(ln)
        if m is not None:
            return m.group(1).decode("ascii")

    return default_encoding

def _run():
    global __file__
    import os
    import site  # noqa: F401

    sys.frozen = "macosx_app"
    base = os.environ["RESOURCEPATH"]

    argv0 = os.path.basename(os.environ["ARGVZERO"])
    script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT)  # noqa: F821

    path = os.path.join(base, script)
    sys.argv[0] = __file__ = path
    if sys.version_info[0] == 2:
        with open(path, "rU") as fp:
            source = fp.read() + "\n"
    else:
        with open(path, "rb") as fp:
            encoding = guess_encoding(fp)

        with open(path, "r", encoding=encoding) as fp:
            source = fp.read() + "\n"

        BOM = b"\xef\xbb\xbf".decode("utf-8")
        if source.startswith(BOM):
            source = source[1:]

    exec(compile(source, path, "exec"), globals(), globals())

def _recipes_pil_prescript(plugins):
    try:
        import Image

        have_PIL = False
    except ImportError:
        from PIL import Image

        have_PIL = True

    import sys

    def init():
        if Image._initialized >= 2:
            return

        if have_PIL:
            try:
                import PIL.JpegPresets

                sys.modules["JpegPresets"] = PIL.JpegPresets
            except ImportError:
                pass

        for plugin in plugins:
            try:
                if have_PIL:
                    try:
                        # First try absolute import through PIL (for
                        # Pillow support) only then try relative imports
                        m = __import__("PIL." + plugin, globals(), locals(), [])
                        m = getattr(m, plugin)
                        sys.modules[plugin] = m
                        continue
                    except ImportError:
                        pass

                __import__(plugin, globals(), locals(), [])
            except ImportError:
                if Image.DEBUG:
                    print("Image: failed to import")

        if Image.OPEN or Image.SAVE:
            Image._initialized = 2
            return 1

    Image.init = init

_recipes_pil_prescript(['SpiderImagePlugin', 'FitsStubImagePlugin', 'MicImagePlugin', 'PdfImagePlugin', 'WebPImagePlugin', 'PcxImagePlugin', 'XVThumbImagePlugin', 'EpsImagePlugin', 'FliImagePlugin', 'PngImagePlugin', 'ImtImagePlugin', 'PsdImagePlugin', 'XpmImagePlugin', 'PalmImagePlugin', 'XbmImagePlugin', 'FtexImagePlugin', 'TgaImagePlugin', 'PpmImagePlugin', 'PcdImagePlugin', 'CurImagePlugin', 'TiffImagePlugin', 'BlpImagePlugin', 'GifImagePlugin', 'McIdasImagePlugin', 'MpegImagePlugin', 'Jpeg2KImagePlugin', 'DcxImagePlugin', 'GbrImagePlugin', 'IptcImagePlugin', 'SunImagePlugin', 'BufrStubImagePlugin', 'ImImagePlugin', 'MspImagePlugin', 'FpxImagePlugin', 'WmfImagePlugin', 'GribStubImagePlugin', 'BmpImagePlugin', 'SgiImagePlugin', 'PixarImagePlugin', 'JpegImagePlugin', 'DdsImagePlugin', 'IcoImagePlugin', 'IcnsImagePlugin', 'Hdf5StubImagePlugin', 'MpoImagePlugin'])

def _setup_ctypes():
    from ctypes.macholib import dyld
    import os

    frameworks = os.path.join(os.environ["RESOURCEPATH"], "..", "Frameworks")
    dyld.DEFAULT_FRAMEWORK_FALLBACK.insert(0, frameworks)
    dyld.DEFAULT_LIBRARY_FALLBACK.insert(0, frameworks)

_setup_ctypes()

import os

os.environ["MATPLOTLIBDATA"] = os.path.join(os.environ["RESOURCEPATH"], "mpl-data")

DEFAULT_SCRIPT='PlantGenerations.py'
SCRIPT_MAP={}
_run()


import numpy as np
import torch
from torch.utils.data import DataLoader, TensorDataset

def get_dataset(transformed_drawings):
    transformed_drawings = sorted(transformed_drawings, key=len, reverse=True)
    n_drawings = len(transformed_drawings)
    n_dims = len(transformed_drawings[0][0])

    # A point is artificially added at the beginning of each drawing, hence
    # `len(transformed_drawing) + 1`.
    lengths = [len(transformed_drawing) + 1 for transformed_drawing in transformed_drawings]
    max_length = max(lengths)
    lengths = torch.IntTensor(lengths)

    # All drawings have the length of the longest drawing `max_length`, padded with zeros
    # after one artificial point is added at the end of each drawing, hence `max_length + 1`.
    data = torch.zeros((n_drawings, max_length + 1, n_dims)).float()

    for i, flat_drawing in enumerate(transformed_drawings):
        # Set artificial first drawing point
        data[i, 0, :] = torch.Tensor([0, 0, 1, 0, 0]).float()
        # Set drawing points
        data[i, 1:len(flat_drawing) + 1, :] = torch.Tensor(flat_drawing)
        # Set artificial last drawing point
        data[i, len(flat_drawing) + 1:, :] = torch.Tensor([0, 0, 0, 0, 1]).float()

    # For labels, discard the null point at the beginning of each drawing. Include the one
    # at the end.
    labels = data[:, 1:]

    # For data, discard the null point at the end of each drawing. Include the one
    # at the beginning.
    data = data[:, :-1]

    return TensorDataset(data, labels, lengths)

def get_train_val_idxs(n_idxs, train_ratio=0.75, sample_ratio=1.0):
    train_idxs, val_idxs = [], []
    for i in range(n_idxs):
        if np.random.rand() <= sample_ratio:
            if np.random.rand() <= train_ratio:
                train_idxs.append(i)
            else:
                val_idxs.append(i)
    return train_idxs, val_idxs

def get_batches(ds, stds, batch_size):
    dl = DataLoader(ds, batch_size)

    for data_batch, labels_batch, lengths_batch in dl:

        standarize_data(data_batch, lengths_batch, stds)
        standarize_data(labels_batch, lengths_batch, stds)

        data_batch = data_batch.transpose(0, 1)
        labels_batch = labels_batch.transpose(0, 1)

        if torch.cuda.is_available():
            data_batch = data_batch.cuda()
            labels_batch = labels_batch.cuda()

        yield data_batch, labels_batch, lengths_batch

def create_length_mask(data, lengths):
    """
    Create lengths mask for data along one dimension.
    """
    n_sequences, max_length, _ = data.shape
    lengths_mask = torch.zeros(n_sequences, max_length)
    for i, length in enumerate(lengths):
        lengths_mask[i, :length + 1] = 1
    return lengths_mask

def get_stds(ds):
    stds = []

    data, _, lengths = ds.dataset[ds.indices]
    lengths_mask = create_length_mask(data, lengths)

    for dim in (0, 1):
        dim_data = data[:, :, dim][lengths_mask == 1]
        std = dim_data.std().item()
        stds.append(std)

    return tuple(stds)

def standarize_data(data, lengths, stds):
    lengths_mask = create_length_mask(data, lengths)

    for dim, std in enumerate(stds):
        dim_data = data[:, :, dim]
        dim_data[lengths_mask == 1] /= std

def unstandarize_flat_strokes(flat_strokes, stds):
    for dim, std in enumerate(stds):
        flat_strokes[:, dim] *= std

    flat_strokes = flat_strokes.round().int().tolist()

    return flat_strokes


'''
    Simple demo that get the number of highway
'''

import osmium as o
import sys

if o:
    print('Success to load osmium!\n')
else:
    print('Failed to load osmium\n')

class HighwayInfoHandler(o.SimpleHandler):
    def __init__(self):
        o.SimpleHandler.__init__(self)
        self.way_num = 0
        self.highway_num = 0

    def count_highway(self, tags):
        if 'highway' in tags:
            self.highway_num += 1

    def way(self, w):
        self.way_num += 1
        self.count_highway(w.tags)

def main(osmfile):
    infoHandler = HighwayInfoHandler()
    infoHandler.apply_file(osmfile)
    print('Way number: %d ' %infoHandler.way_num)
    print('Highway number: %d ' %infoHandler.highway_num)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Usage: python %s <osmfile>' % sys.argv[0])
        sys.exit(-1)

    exit(main(sys.argv[1]))