from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pandas as pd
import argparse
import sys,os
import tensorflow as tf
from PIL import Image
from tqdm import tqdm
import csv

t=tqdm(pd.read_csv('/home/DATA/guoming/food_recog/raw_data/list/food_meituan_val.csv').values)
val_list=[]
val_labels=[]

i=0
for tt in t:
    val_list.append(tt[0])
    val_labels.append(tt[1])
    i+=1

def load_image(filename):
    #Read in the image_data to be classified."""
    return tf.gfile.FastGFile(filename, 'rb').read()

def load_labels(filename):
    #Read in labels, one label per line."""
    return [line.rstrip() for line in tf.gfile.GFile(filename)]

def load_graph(filename):
    #Unpersists graph from file as default graph."""
    with tf.gfile.FastGFile(filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')
def read_tensor_from_image_file(file_name,
                                input_height=224,
                                input_width=224,
                                input_mean=127.5,
                                input_std=127.5):
    input_name = "file_reader"
    output_name = "normalized"
    file_reader = tf.read_file(file_name, input_name)
    if file_name.endswith(".png"):
        image_reader = tf.io.decode_png(
            file_reader, channels=3, name="png_reader")
    elif file_name.endswith(".gif"):
        image_reader = tf.squeeze(
            tf.io.decode_gif(file_reader, name="gif_reader"))
    elif file_name.endswith(".bmp"):
        image_reader = tf.io.decode_bmp(file_reader, name="bmp_reader")
    else:
        image_reader = tf.image.decode_jpeg(
            file_reader, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(
        dims_expander, [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
    sess = tf.Session()
    result = sess.run(normalized)

    return result

def run_graph(src, dest, labels, input_layer_name, output_layer_name, num_top_predictions,val_img_list,output_csv):
    config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
    #config.gpu_options.per_process_gpu_memory_fraction = 0.1
    with tf.Session(config=config) as sess:
        # Feed the image_data as input to the graph.
        # predictions  will contain a two-dimensional array, where one
        # dimension represents the input image count, and the other has
        # predictions per class
        i=0
        f_csv = open(output_csv,'w',encoding='utf-8',newline="")
        csv_writer = csv.writer(f_csv)
        csv_writer.writerow(["id","predicted"])
        #with open('submit.csv','w') as outfile:
        right_num=0
        for f in tqdm(val_img_list):
            # im=Image.open(os.path.join(src,f))
            # im = im.resize((224,224),Image.ANTIALIAS)
            # img=im.convert('RGB')
            # img.save(os.path.join(dest,f))
            # image_data=load_image(os.path.join(dest,f))

            # img_data_jpg = tf.image.decode_jpeg(image_data) #图像解码
            # img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
            file_name = os.path.join(src,f)
            t = read_tensor_from_image_file(
            file_name)
            softmax_tensor=sess.graph.get_tensor_by_name(output_layer_name)
            predictions,=sess.run(softmax_tensor, {input_layer_name: t})

            # Sort to show labels in order of confidence
            top_k = predictions.argsort()[-num_top_predictions:][::-1]
            result=[]
            for node_id in top_k:
                score = predictions[node_id]
                result.append(int(labels[node_id]))
            if val_labels[i] in result:
                right_num+=1
            csv_writer.writerow([f,result])
            i+=1
            if i%100==1:
                print("processing %d"%i)
                accuracy=float(right_num)/float(i)
                print("accuracy %.4f"%accuracy)

    accuracy=float(right_num)/float(len(val_img_list))
    print("accuracy %.4f"%accuracy)

os.environ['CUDA_VISIBLE_DEVICES']='0'
src="/home/DATA/guoming/food_recog/raw_data/val"
dest="/home/DATA/guoming/food_recog/raw_data/val2"
if not os.path.exists(dest):
    os.makedirs(dest)
labels='./output/output_labels.txt'
predictions_csv="./output/guoming_val.csv"
graph='./output/intermediate_graph/intermediate_30000.pb'
input_layer='DecodeJpeg/contents:0'
input_layer='input:0'

output_layer='final_result:0'
num_top_predictions=3
labels = load_labels(labels)
print("labels")
print(labels)
load_graph(graph)
run_graph(src,dest,labels,input_layer,output_layer,num_top_predictions,val_list,predictions_csv)


# Copyright (C) 2012-2014 Geoscience Australia

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
 Title: vulnerability_model.py

Author:   Ben Cooper, ben.cooper@ga.gov.au

Description: TODO:

Copyright 2012 by Geoscience Australia

"""

import os
import sys
import numpy
import logging
from numpy import asarray, interp, where, zeros

from hazimp.validator import Validator, NRML_SCHEMA
from hazimp.xml_interface import XmlLayer

LOGGER = logging.getLogger(__name__)
DEFAULTLOSS = 0

def vuln_sets_from_xml_file(filenames: list) -> dict:
    """
    Load a GEM NRML vulnerability file in the format described in
    resources/nrml/schema/risk/vulnerability.xsd

    Args:
    :param filenames: The names of the xml files.

    :returns: A dictionary of Vulnerability Sets.
    """

    vulnerability_sets = {}

    for filename in filenames:
        if not os.path.exists(filename):
            raise RuntimeError(
                f'No vulnerability XML was loaded. Check file name {filename}'
            )

        validator = Validator(NRML_SCHEMA)
        try:
            validator.validate(filename)
        except AssertionError as err:
            LOGGER.exception(f"{filename} is not a valid vulnerability file")
            LOGGER.exception(err)
            sys.exit(1)

        xml_node = XmlLayer(filename=filename)
        set_id, vulnerability_set = vuln_sets_from_xml_node(xml_node)
        vulnerability_sets[set_id] = vulnerability_set

    return vulnerability_sets

def vuln_sets_from_xml_node(xml_node):
    """
    Load in the vulnerability sets from an xml node.

    :param vulnerability_models: Dictionary to store vulnerability models.
    :param xml_node: The root node of the vulnerability xml file.

    :returns: A dictionary of Vulnerability Sets.
    """

    xml_vuln_set = xml_node['vulnerabilityModel'][0]
    vuln_set_id = xml_vuln_set.attributes['id']
    asset_category = xml_vuln_set.attributes['assetCategory']
    loss_category = xml_vuln_set.attributes['lossCategory']
    iml = xml_vuln_set['imls'][0]
    im_level = iml.array[0]
    intensity_measure_type = iml.attributes['imt']
    try:
        default_loss = xml_vuln_set.attributes['defaultLoss']
    except KeyError:
        default_loss = DEFAULTLOSS

    vulnerability_functions = {}

    for func in xml_vuln_set['vulnerabilityFunction']:
        vuln_funct = VulnerabilityFunction.from_xml_node(func)
        vulnerability_functions[vuln_funct.function_id] = vuln_funct

    return (vuln_set_id, VulnerabilitySet(
        im_level,
        intensity_measure_type,
        vuln_set_id,
        asset_category,
        loss_category,
        vulnerability_functions,
        default_loss))

class VulnerabilitySet(object):

    """
    A set of vulnerability functions for a given intensity measure
    level.  All vulnerability functions have the same intensity
    measure (x-axis) and loss category (y-axis). Each vulnerability
    function represents a class of assets, such as brick buildings
    etc.

    Methods:
    - calc_mean - return mean loss and sigma for the given function id
    - sample - return a sample for the given function id

    Constructor input:
    - intensity_measure_level - a set of points for the x axis of the mean loss
      curve (common to all functions)
    - intensity_measure_type - type of intensity measure that the intensity
      measure level specifies ('MMI' is the only supported value)
    - vulnerability_functions - a dictionary of VulnerabilityFunction objects
      where the function id is the key

    Class method:
    - from_xml - construct a Vulnerability_Set object from a NRML vulnerability
      file

    """
    # pylint: disable=R0913

    def __init__(self,
                 intensity_measure_level,
                 intensity_measure_type,
                 vulnerability_set_id,
                 asset_category,
                 loss_category,
                 vulnerability_functions,
                 default_loss):
        """
        :params intensity_measure_level: a set of points for the x axis
                of the mean loss curve (common to all functions)
        :params intensity_measure_type: type of intensity measure that the
                intensity measure level specifies e.g. MMI
        :params asset_category: The asset typology
        :params loss_category: The type of loss suffered by the asset_category
        :params vulnerability_functons: a dictionary of VulnerabilityFunction
                 objects where the function id is the key.

        """
        self.intensity_measure_level = asarray(intensity_measure_level)
        self.intensity_measure_type = intensity_measure_type
        self.vulnerability_set_id = vulnerability_set_id
        # asset_category: The asset typology
        # e.g. population, buildings
        self.asset_category = asset_category
        # loss_category: The type of loss suffered by the asset_category
        # e.g. fatalities, collapse, building damage index,
        # contents damage index
        self.loss_category = loss_category
        self.vulnerability_functions = vulnerability_functions
        self.default_loss = default_loss

    def __repr__(self):
        return ('Discrete Vulnerability Set:\n'
                '          intensity measure type: %s\n'
                '         intensity measure level: %s\n'
                'discrete vulnerability functions: %s\n'
                % (str(self.intensity_measure_type),
                   str(self.intensity_measure_level),
                   str(list(self.vulnerability_functions.keys()))))

    def build_realised_vuln_curves(self, vulnerability_function_ids,
                                   variability_method=None):
        """
        Given a list of vulnerability_function_IDs return the
        actual vulnerability curves, as a realised vulnerabitly
        curves instance.

        Currently this just returns a mean curve.

        :parmas vulnerability_function_IDs: A list of the vuln. functions.
            The list dimension is asset.
        :parmas variability_method: How the vulnerability function is sampled.

        :returns: A realised vulnerabitly curves instance.  Use this to calc
            the loss ratio.

        """
        loss_per_asset = []  # Build a list up.
        for key in vulnerability_function_ids:
            try:
                vuln_funct = self.vulnerability_functions[key]
            except KeyError:
                msg = '[%s] does not have a vulnerability curve.\n The' % key
                msg += ' vulnerability set is %s' % self.vulnerability_set_id
                raise NotImplementedError(msg)
            loss_per_asset.append(vuln_funct.get_loss(variability_method))
        # To get dimensions (asset, loss)
        loss_per_asset = asarray(loss_per_asset)
        realised_vuln_curves = RealisedVulnerabilityCurves(
            self.intensity_measure_type,
            self.loss_category,
            self.intensity_measure_level,
            loss_per_asset,
            self.vulnerability_set_id,
            self.default_loss)
        return realised_vuln_curves

    def calc_mean(self, func_id, intensity):
        """
        A wrapper for `VulnerabilityFunction.calc_mean`. Lookup the
        specified vulnerability function in the set and run `calc_mean`
        for that function.

        """
        func = self.vulnerability_functions.get(func_id)
        if func is None:
            raise NotImplementedError(
                '%s does not have a configured vulnerability curve' % func_id)

        return func.calc_mean(intensity, self.intensity_measure_level)

    def sample(self, func_id, mean, sigma):
        """
        A wrapper for VulnerabilityFunction.sample. Lookup the
        specified vulnerability function in the set and run sample for
        that function.

        :param str func_id: Then name of the vulnerability function set
        :param float mean: Mean value of the selected vulnerability function
        :param float sigma: the coefficient of variation of the selected
          vunlerability function

        """
        func = self.vulnerability_functions.get(func_id)
        if func is None:
            raise NotImplementedError(
                '%s does not have a configured vulnerability curve' % func_id)

        return func.sample(mean, sigma)

def ratio_cutoff(ratio):
    """
    As VulnerabilityFunction.sample may return values outside of a ratio,
    ensure that the given ratio array is between 0 and 1 by setting values
    outside these bounds to the bounds.

    :param float ratio: the value to be tested.
    :returns: 1 if ratio > 1.0, 0 if ratio < 0.0, or the value of ratio
     otherwise.

    """
    # High cutoff
    ratio = where(ratio > 1.0, 1.0, ratio)
    # Low cufoff
    ratio = where(ratio < 0.0, 0.0, ratio)

    return ratio

class VulnerabilityFunction(object):

    """
    A vulnerability function defined by a specified set of points on a curve.

    Methods:
    calc_mean - return mean loss and sigma based on the given set of points
    sample - return a sample based on the specified probabilistic distribution

    """

    def __init__(self,
                 func_id,
                 mean_loss,
                 coefficient_of_variation,
                 distribution):
        """
        :param func_id: identifier for this function
        :param mean_loss: array of ratio points
        :param coefficient_of_variation: array of uncertainty points
                (shape must match shape of mean_loss)
        :param distribution: either normal ('N') or lognormal ('LN')
        :param vulnerability_set: The vulnerability set for this function.
        """

        self.function_id = func_id
        self.mean_loss = asarray(mean_loss)
        self.coefficient_of_variation = asarray(coefficient_of_variation)
        self.distribution = asarray(distribution)

    def get_loss(self, variability_method=None):

        """
        Get the actual loss for a curve.

        Currently this just returns a mean curve.

        :param variability_method: How the vulnerability function is sampled.

        :returns: The loss y-axis values, sampled using the supplied method.
        """
        if variability_method is None:
            loss_points = self.mean_loss
        elif variability_method == 'mean':
            loss_points = self.mean_loss
        elif variability_method == 'normal':
            loss_points = self.mean_loss + numpy.random.normal(0, 1) * \
                (self.mean_loss*self.coefficient_of_variation)
        elif variability_method == 'normal_uniform':
            loss_points = self.mean_loss + numpy.random.normal(0, 1) * \
                (self.coefficient_of_variation)
        else:
            raise RuntimeError(("Invalid vulnerability_method in "
                                "configuration file. vulnerability_method "
                                "must be 'normal','normal_uniform, 'mean' "
                                "or left blank"))

        # TODO: Fix!
        return ratio_cutoff(loss_points)

    def calc_mean(self, intensity):
        """
        Calculate mean loss ratio and sigma based on the specified points on
        the curve:
                        |
                        |                                +
                        |                           +
        Mean loss ratio |                     +
                        |               +
                        |            +
                        |          +
                        |         +
                        |        +
                        |       +
                        |    +
                        | +
                        +-----------------------------------
                               Intensity measure level

        For a given intensity, mean loss and sigma is determined by linearly
        interpolating the points on the curve.

        Note that sigma is calculated as cv * mean loss as cv = mean loss/sigma
        """
        mean_loss = interp(intensity,
                           self.intensity_measure_level,
                           self.mean_loss)
        cv = interp(intensity,
                    self.intensity_measure_level,
                    self.coefficient_of_variation)
        # cv = sigma / mean
        sigma = cv * mean_loss

        return (mean_loss, sigma)

    @classmethod
    def from_xml_node(cls, xml_node):
        """Load in a vulnerability function from an xml node.

        :param xml_node: The root node of the vulnerability xml file.

        :returns: A vulnerability function.
        """
        func_id = xml_node.attributes['id']
        prob_dist = xml_node.attributes['dist']
        loss = xml_node['meanLRs'][0].array[0]
        coeff_of_variation = xml_node['covLRs'][0].array[0]

        return cls(
            func_id,
            loss,
            coeff_of_variation,
            prob_dist)

class RealisedVulnerabilityCurves(object):

    """
    Respresents a collection of vulnerability curves associated with an
    exposure set.

    There is one vulnerability curve per asset
    """
    # pylint: disable=R0913

    def __init__(self,
                 intensity_measure_type,
                 loss_category_type,
                 intensity_measure_level,
                 loss_per_asset,
                 vulnerability_set_id,
                 default_loss):
        """

        :param intensity_measure_type: type of intensity measure that the
                intensity measure level specifies ('MMI' is the only supported
                 value).
        :param loss_category_type: Type of loss suffered by the asset_category.
        :param intensity_measure_level: a set of points for the x axis of
            the mean loss curve (common to all functions).
        :param loss_per_asset: 2D array of ratio points per asset.
                The loss dimension can be regarded as the y axis values.
                dimensions (asset, loss).
        """

        self.intensity_measure_type = intensity_measure_type
        self.loss_category_type = loss_category_type
        self.loss_per_asset = loss_per_asset

        self.intensity_measure_level = intensity_measure_level
        self.vulnerability_set_id = vulnerability_set_id
        self.default_loss = default_loss

    def look_up(self, intensity):
        """
        Given an intensity use the curve to determine the loss ratio.

        :param intensity: An array intensity measures.  Dimensions(asset, ...)

        :return: A loss value. loss_category_type describes type of loss.
        """
        # Note the dimensions after the asset are different in intensity
        # and loss_per_asset.

        loss = zeros(intensity.shape)
        assert self.loss_per_asset.shape[0] == intensity.shape[0]

        nan = numpy.isnan(intensity)
        for asset in range(self.loss_per_asset.shape[0]):
            loss[asset] = interp(
                intensity[asset],
                self.intensity_measure_level,
                self.loss_per_asset[asset])
        loss[nan] = self.default_loss
        return loss


import cv2
import numpy as np

print(cv2.__version__)

img = cv2.imread("IDCard-Dylan.png", cv2.IMREAD_COLOR)
cv2.imshow("original", img)

qrDecoder = cv2.QRCodeDetector()

opencvData, points, rectifiedImage = qrDecoder.detectAndDecode(img)
bbox = cv2.boundingRect(points)

if opencvData != None:
    print("QR Code Detected")
else:
    print("QR Code NOT Detected")

if opencvData != None:
    print("QR Code Detected")
else:
    print("QR Code NOT Detected")

print(type(bbox))

imgRectangle = img.copy()
cv2.rectangle(imgRectangle, (int(bbox[0]), int(bbox[1])), (int(
    bbox[0]+bbox[2]), int(bbox[1]+bbox[3])), (255, 0, 0), thickness=2, lineType=cv2.LINE_8)

print(*opencvData)

cv2.imshow("detected", imgRectangle)

cv2.waitKey(0)
cv2.destroyAllWindows()


import json
from django.shortcuts import render
from django.http import HttpResponse, QueryDict
from django.core import serializers

from models import Counter

def home(request):
    return render(request, "targets/home.html", dict())

def get_counter(request):
    counter,c = Counter.objects.get_or_create(name="main")
    c_dict = dict(counter=counter)
    if request.method == "POST":
        counter.value += 1
        counter.save()
    return render(request, "targets/counter.html", c_dict)

def get_counter_ajax(request):
    counter,c = Counter.objects.get_or_create(name="main")
    # make it serializer in the response by serializing and deserializing
    c_j = json.loads(serializers.serialize('json', [counter]))[0]['fields']
    c_dict = dict(counter=c_j)
    return HttpResponse(json.dumps(c_dict), content_type="application/json")

def incr_counter_ajax(request):
    counter,c = Counter.objects.get_or_create(name="main")
    counter.value += 1
    counter.save()
    c_j = json.loads(serializers.serialize('json', [counter]))[0]['fields']
    c_dict = dict(counter=c_j)
    return HttpResponse(json.dumps(c_dict), content_type="application/json")


import os
import re
import time
import multiprocessing

f = re.compile('\d.*\d')

for lin in [
			'python train.py --logtostderr --train_dir=./outputs/train-v7/train --pipeline_config_path=./config/train-v7.config',\
			'python train.py --logtostderr --train_dir=./outputs/train-v8/train --pipeline_config_path=./config/train-v8.config'
		]:

	while True:

		res = os.popen('nvidia-smi --query-gpu=memory.free --format=csv').readlines()
		mem = int(f.findall(res[-1])[0])

		if mem > 7000:
			print('large than 7000.', mem)
			os.system('{}'.format(lin))
			break
		else:
			pass
			print(mem)

		time.sleep(600*6*1)


import os

negative_data_path = r"D:\Dataset Skripsi Batch Final Label\Negative"
positive_data_path = r"D:\Dataset Skripsi Batch Final Label\Positive"

def create_training_csv(data_path, label, writer):
    # Policy: Discard frames that don't meet the 12 sliding window requirement
    subjects = os.listdir(data_path)
    for subject in subjects:
        subject_data_path = os.path.join(data_path, subject)
        detections = os.listdir(subject_data_path)
        for detection in detections:
            detection_data_path = os.path.join(subject_data_path, detection)
            numbers = sorted([int(f[3:-4]) for f in os.listdir(detection_data_path)])
            counter = 0
            list_filename = []
            for number in numbers:
                filename = "img" + str(number).zfill(5) + ".jpg"
                path_filename = os.path.join(detection_data_path, filename)
                list_filename.append(path_filename)

            sliding_window_12 = len(list_filename) // 12

            for index in range(0, sliding_window_12):
                for filename in list_filename[12 * index:12 * (index + 1)]:
                    writer.write(filename)
                    writer.write(',')
                writer.write(str(label))
                writer.write('\n')

if __name__ == "__main__":
    filewriter = open('training_pubspeak_face_detection.csv',  'w')
    filewriter.write("1,2,3,4,5,6,7,8,9,10,11,12,Label\n")
    create_training_csv(positive_data_path, 1, filewriter)
    create_training_csv(negative_data_path, 0, filewriter)
    filewriter.close()


# https://leetcode.com/problems/sliding-window-maximum/
from collections import deque
from typing import List

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        res = []
        left = right = 0
        q = deque()  # contain indices

        while right < len(nums):
            while q and nums[q[-1]] < nums[right]:
                q.pop()
            q.append(right)

            # remove left val from window
            if left > q[0]:
                q.popleft()

            if right + 1 >= k:
                res.append(nums[q[0]])
                left += 1

            right += 1

        return res

if __name__ == '__main__':
    pass

Sliding Window/Leetcode Problems/239. Sliding Window Maximum.py


import json
import random
import numpy as np
import os
from collections import deque

from tensorflow.python.keras.models import Sequential, model_from_json
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.optimizers import Adam

# Just disables the warning, doesn't enable AVX/FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

class DQNAgent:
    def __init__(self, model, epsilon):
        """Create an agent using a model. Typically you want to call either from_stored_model or from_dimensions."""
        self.memory = deque(maxlen=250000)
        self.gamma = 0.5  # discount rate
        self.epsilon = epsilon
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995
        self.learning_rate = 0.001
        self.batch_size = 64
        model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate))
        self.model = model
        self.input_size = int(self.model.input.shape[-1])
        self.output_size = self.model.output.shape[-1]

    @classmethod
    def from_stored_model(cls, model_file):
        model_and_settings = json.load(open(model_file))
        epsilon = model_and_settings['epsilon']
        model_json = model_and_settings['model']
        model = model_from_json(json.dumps(model_json))

        return cls(model, epsilon)

    @classmethod
    def from_dimensions(cls, state_size, layers, action_size):
        """ Neural Net for Deep-Q learning Model--->>  #Q=NN.predict(state)"""
        model = Sequential()
        # First layer
        model.add(Dense(layers[0], input_dim=state_size, activation='relu'))
        # Middle layers (if any)
        for layersize in layers[1:]:
            model.add(Dense(layersize, activation='relu'))
        # Output layer
        model.add(Dense(action_size, activation='linear'))

        return cls(model, epsilon=0.8)

    def remember(self, state, action, reward, next_state):
        """Store a memory

        Args:
            state: the state at the beginning of the action
            action: the chosen action based on this
            reward: the reward for the action
            next_state: the resulting state or None if the creature died.
        """
        self.memory.append((state, action, reward, next_state))

    def act(self, state):
        """
        Return an action given the state using the internal network.

        Args:
            state: flat array containing the state

        Returns:
            integer indicating the action

        """
        if np.random.rand() <= self.epsilon:
            return random.randrange(self.output_size)
        act_values = self.predict(state)
        return np.argmax(act_values[0])

    def predict(self, state):
        state = np.reshape(state, (1, -1))
        return self.model.predict(state)

    def fit(self, state, target_f):
        state = np.reshape(state, (1, -1))
        self.model.fit(state, target_f, epochs=1, verbose=0)

    def replay(self):
        # Sample a batch from memory uniformly at random
        batch_size = min(self.batch_size, len(self.memory))
        batch = random.sample(self.memory, batch_size)

        # Predict q_values in batches for efficiency
        none_state = np.zeros(self.input_size)  # Used in place of None for next_state
        states = np.array([sample[0] for sample in batch])
        next_states = np.array([(none_state if sample[3] is None else sample[3]) for sample in batch])
        q_values = self.model.predict(states)
        q_values_next = self.model.predict(next_states)

        loss = 0.0

        # Fill in our training batch
        X = np.zeros((batch_size, self.input_size))
        y = np.zeros((batch_size, self.output_size))
        for i in range(batch_size):
            state, action, reward, next_state = batch[i]
            # Important : target is the q_value itself for all actions except the one actually taken
            if next_state is not None:
                reward += self.gamma * np.amax(q_values_next[i])
            target = q_values[i]
            loss += abs(target[action] - reward)
            target[action] = reward
            X[i] = state
            y[i] = target

        self.model.fit(X, y, verbose=0)
        self.epsilon = min(self.epsilon_decay * self.epsilon, self.epsilon_min)

        return loss / batch_size

    def identity_test(self):
        """Run the network over inputs with each exactly one cell set to one."""
        inputs = np.identity(self.input_size)
        return self.model.predict(inputs)

    def show_weights(self):
        for idx, layer in enumerate(self.model.layers):
            print(f'\nLayer {idx} weights (rows are inputs, columns are outputs:')
            print(layer.get_weights()[0])
            print(f'Layer {idx} biases:')
            print(layer.get_weights()[1])
        print()

    def load_weights(self, name):
        self.model.load_weights(name)

    def save_weights(self, name):
        self.model.save_weights(name)

    def save_history(self, name):
        with open(name, 'w') as fout:
            for state, action, reward, next_state in self.memory:
                if next_state is not None:  # Added but not sure why this was not needed in the first place
                    state = [float(x) for x in state]
                    if next is not None:
                        next_state = [float(x) for x in next_state]
                    record = {'state': state, 'action': int(action), 'reward': float(reward), 'next_state': next_state}
                    fout.write(json.dumps(record) + '\n')

    def save_model(self, name):
        model_json = json.loads(self.model.to_json(indent=2))
        with open(name, 'w') as json_file:
            json.dump({'model': model_json, 'epsilon': self.epsilon}, json_file, indent=2)


import io
from pathlib import Path
import sys

root_dir = (Path(__file__).parent / "../").resolve()
sys.path.append(str(root_dir))

from check_response import CheckResponse
from validate import validate_url, validate_date

class TestValidateURL:
    def test_validate_url_validURL(self):
        # assemble
        url = "https://www.jwatch.org/general-medicine-online-archives"
        # act
        url_check = validate_url(url)
        # assert
        assert url_check == CheckResponse(
            True, "https://www.jwatch.org/general-medicine-online-archives"
        )

    def test_validate_url_invalidURL(self):
        # assemble
        url = "invalid_url"
        # act
        url_check = validate_url(url)
        # assert
        assert url_check == CheckResponse(False, "")

    def test_validate_url_validURL_jwatchNotInURL(self):
        # assemble
        url = "https://www.google.com/"
        # act
        url_check = validate_url(url)
        # assert
        assert url_check == CheckResponse(False, "")

class TestValidateDate:
    def test_validate_date_validDate(self):
        # assemble
        date = "02-2021"
        # act
        date_check = validate_date(date)
        # assert
        assert date_check == CheckResponse(True, ("02", "2021"))

    def test_validate_date_dateIncludesDay(self):
        # assemble
        date = "01-02-2021"
        # act
        date_check = validate_date(date)
        # assert
        assert date_check == CheckResponse(False, "")

    def test_validate_date_monthNotValid(self):
        # assemble
        date = "13-2021"
        # act
        date_check = validate_date(date)
        # assert
        assert date_check == CheckResponse(False, "")

    def test_validate_date_noHyphen(self):
        # assemble
        date = "13/2021"
        # act
        date_check = validate_date(date)
        # assert
        assert date_check == CheckResponse(False, "")

    def test_validate_date_twoDigitYear(self):
        # assemble
        date = "13-21"
        # act
        date_check = validate_date(date)
        # assert
        assert date_check == CheckResponse(False, "")


import tensorflow as tf
from tensorflow import keras
import os
import numpy as np
import random

def get_data():
    train_concentrate = os.listdir(os.getcwd() + "/active/train")
    test_concentrate = os.listdir(os.getcwd() + "/active/test")
    train_relax = os.listdir(os.getcwd() + "/relax/train")
    test_relax = os.listdir(os.getcwd() + "/relax/test")

    train_data = []
    train_labels = []
    shortest = 1100
    for file1, file2 in zip(train_concentrate, train_relax):
        d1 = (np.genfromtxt(os.getcwd() + "/active/train/" + file1,
        delimiter=",", skip_header=1))[:,1:]
        d2 = (np.genfromtxt(os.getcwd() + "/relax/train/" + file2,
        delimiter=",", skip_header=1))[:,1:]
        a = int(shortest/128) * 128
        train_data.append(d1[:a])
        train_data.append(d2[:a])
        for i in range(8):
            train_labels.append(0)
        for i in range(8):
            train_labels.append(1)

    test_data = []
    test_labels = []
    for file1, file2 in zip(test_concentrate, test_relax):
        d1 = (np.genfromtxt(os.getcwd() + "/active/test/" + file1,
        delimiter=",", skip_header=1))[:,1:]
        d2 = (np.genfromtxt(os.getcwd() + "/relax/test/" + file2,
        delimiter=",", skip_header=1))[:,1:]
        a = int(shortest/128) * 128
        test_data.append(d1[:a])
        test_data.append(d2[:a])
        for i in range(8):
            test_labels.append(0)
        for i in range(8):
            test_labels.append(1)

    train_data = np.array(train_data).reshape(-1, 128, 4)
    test_data = np.array(test_data).reshape(-1, 128, 4)
    train_labels = np.array(train_labels)
    test_labels = np.array(test_labels)

    temp1 = list(zip(train_data, train_labels))
    random.shuffle(temp1)
    train_data, train_labels = zip(*temp1)

    train_data = np.array(train_data)
    train_labels = np.array(train_labels)

    print(train_data.shape, test_data.shape, train_labels.shape)
    return train_data, test_data, train_labels, test_labels

def get_model(training_data, training_labels):
    model = keras.Sequential()
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(100, activation=tf.nn.relu))
    model.add(keras.layers.Dense(100, activation=tf.nn.relu))
    model.add(keras.layers.Dense(2, activation=tf.nn.softmax))

    model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
    model.fit(training_data, training_labels, epochs=4)

    return model

def predict(model, data):
    prediction = model.predict(data)
    print("prediction:", np.argmax(prediction))

train_data, test_data, train_labels, test_labels = get_data()

model = get_model(train_data, train_labels)
test_loss, test_acc = model.evaluate(test_data, test_labels)
print("test_accuracy:", test_acc)


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

# 猎聘的spider，爬取job和company信息

__author__ = 'bitfeng'

import re
import scrapy
from chinahr.items import JobInfoItem, ComInfoItem
from scrapy.loader import ItemLoader
from chinahr.processors import *
import urllib

# 猎聘网spider
class LiepinCrawlSpider(scrapy.Spider):
    name = 'liepin'  # spider名称
    allowed_domain = ['liepin.com']  # 限制域名
    start_urls = ['http://www.liepin.com/it/?imscid=R000000030',   # 起始urls
                  'http://www.liepin.com/realestate/?imscid=R000000031',
                  'http://www.liepin.com/financial/?imscid=R000000032',
                  'http://www.liepin.com/consumergoods/?imscid=R000000033',
                  'http://www.liepin.com/automobile/?imscid=R000000034',
                  'http://www.liepin.com/medicine/?imscid=R000000054',
                  ]

    # 处理起始urls的response
    def parse(self, response):
        urls = response.xpath('//ul[@class="sidebar float-left"]/li/dl/dd/a/@href').extract()
        for url in urls:
            yield scrapy.Request('http://www.liepin.com'+re.sub(re.compile(u'&dqs=\d*'), '', url), callback=self.parse_flip)

    def parse_flip(self, response):
        category = ''.join(re.compile(u'(?<=&key=).*?(?=&)').findall(response.url))
        urls = response.xpath('//ul[@class="sojob-result-list"]/li/a/@href').extract()
        for url in urls:
            yield scrapy.Request(url, callback=self.parse_info, meta={'category': category})
        reg_url = ''.join(response.xpath('//div[@class="pagerbar"]').
                          re(u'(?<=)\d{1,3}.*?(?=)'))
        next_url = ''.join(re.compile(u'(?<=href=")http.*?(?=")').findall(reg_url))
        if next_url:
            yield scrapy.Request(next_url, callback=self.parse_flip)

    # 抓取 职位信息 和 公司信息
    def parse_info(self, response):
        loaderJob = ItemLoader(item=JobInfoItem(), response=response)
        loaderJob.add_value('url', value=response.url)
        loaderJob.add_value('job_category', value=urllib.unquote(response.meta['category']))
        loaderJob.add_xpath('job_name', '//div[@class="title-info over"]/h1/text()')
        loaderJob.add_xpath('job_name', '//div[@class="title-info "]/h1/text()')
        loaderJob.add_xpath('job_company', '//div[@class="title-info over"]/h3/text()')
        loaderJob.add_xpath('job_company', '//div[@class="title-info "]/h3/text()')
        loaderJob.add_xpath('job_company', '//div[@class="title-info "]/h3/a/text()')
        loaderJob.add_xpath('job_miniEdu', '//div[@class="resume clearfix"]/span/text()', TakeNumL(0))
        loaderJob.add_xpath('job_experience', '//div[@class="resume clearfix"]/span/text()', TakeNumL(1))
        loaderJob.add_xpath('job_reqLan', '//div[@class="resume clearfix"]/span/text()', TakeNumL(2))
        loaderJob.add_xpath('job_reqAge', '//div[@class="resume clearfix"]/span/text()', TakeNumL(3))
        loaderJob.add_xpath('job_salary', '//p[@class="job-main-title"]/text()', TakeFirstL())
        loaderJob.add_xpath('job_location', '//p[@class="basic-infor"]/span[1]/text()', TakeFirstL())
        loaderJob.add_xpath('job_update', '//p[@class="basic-infor"]/span[2]/text()', TakeFirstL(), re=u'(?<=发布于：).*')
        loaderJob.add_xpath('job_desc', '//div[@class="content content-word"][1]', RemoveTagsL(), StripBlankL(), JoinL(''))
        loaderJob.add_xpath('job_benefits', '//div[@class="job-main main-message"]',
                            RemoveTagsL(), ReplaceBlank(), re=u'(?<=薪酬福利：)[\s\S]*')
        loaderJob.add_xpath('job_benefits', '//div[@class="tag-list clearfix"]/span/text()', JoinL('|'))
        yield loaderJob.load_item()

        if 'job.liepin.com' in response.url:
            loaderCom = ItemLoader(item=ComInfoItem(), response=response)
            loaderCom.add_value('url', value=response.url)
            loaderCom.add_value('com_name', value=loaderJob.get_collected_values('job_company'))
            loaderCom.add_xpath('com_industry', '//div[@class="right-post-top"]/div[@class="content content-word"]/a[1]/@title', TakeFirstL())
            loaderCom.add_xpath('com_size', '//div[@class="right-post-top"]/div[@class="content content-word"]', RemoveTagsL(), re=u'(?<=规模：)[\s\S]*?(?=
)')
            loaderCom.add_xpath('com_nature', '//div[@class="right-post-top"]/div[@class="content content-word"]', RemoveTagsL(), re=u'(?<=性质：)[\s\S]*?(?=
)')
            loaderCom.add_xpath('com_address', '//div[@class="right-post-top"]/div[@class="content content-word"]', RemoveTagsL(), re=u'(?<=地址：)[\s\S]*')
            loaderCom.add_xpath('com_intro', '//div[@class="job-main main-message noborder "]/div[@class="content content-word"]/text()', StripBlankL(), TakeFirstL())
            yield loaderCom.load_item()


import pandas as pd
import numpy as np

df = pd.read_csv("./data/unicorn_companies.csv")
df["valuation"] = df["valuation"].replace('[\$,]', '', regex=True).astype(float)
df["date_joined"] = df["date_joined"].replace('None', np.nan)
df["date_joined"] = pd.to_datetime(df["date_joined"].astype(str))
df = df.sort_values(by="date_joined")

# COUNTRY STATS

# number of startups by country
country_stats = df.groupby(
    by=["country"])["company"].count().reset_index()

# average valuation by country
country_stats["mean_valuations_billion_usd"] = df.groupby(
    by=["country"])["valuation"].mean().reset_index(drop=True)

# max valuation by country
country_stats["max_valuations_billion_usd"] = df.groupby(
    by=["country"])["valuation"].max().reset_index(drop=True)

print(country_stats)

# TIME SERIES STATS

# number of unicorns over time
time_series = df.groupby(
    by=["country", "date_joined"])["company"].count().reset_index()
# valuations over time by country
time_series["valuation_billion_usd"] = df.groupby(
    by=["country", "date_joined"])["valuation"].sum().reset_index(drop=True)
# cumulative count of unicorns over time
time_series["company_cumsum"] = time_series.groupby(
    by=["country"])["company"].expanding().sum().reset_index(drop=True)
# cumulative valuation of unicorns over time
time_series["valuation_cumsum"] = time_series.groupby(
    by=["country"]
    )["valuation_billion_usd"].expanding().sum().reset_index(drop=True)

# INVESTOR STATS

# split the investors in to list
df["select_investors"] = df["select_investors"].str.split(',')

# change investor lists to columns
investors = df["select_investors"].explode()

# inner merge to transform dataframe rows of unique investors per company
df = pd.merge(
    investors,
    df,
    left_index=True,
    right_index=True,
    suffixes=("_single", "_list")
    )

# count of companiess in investors portfolio
investor_stats = df.groupby(
    by=["select_investors single"])["company"].count().reset_index()

# count of industries in investors portfolio
investor_stats["industry_count"] = df.groupby(
    by=["select_investors_single"])["Industry"].count().reset_index(drop=True)

# valiation of companies in investors portfolio
investor_stats["total_valuation"] = df.groupby(
    by=["select_investors_single"])["valuation"].sum().reset_index(drop=True)

print(investor_stats)


import sys
import collections
def input(): return sys.stdin.readline().strip()

line = input()
low_op = ["+", "-"]
high_op = ["*", "/"]
parenthesis = ["(", ")"]
priority = {"-": 0, "+": 0, "/": 1, "*":1}

def toPostfix(exp):
    operator = collections.deque()
    operand = collections.deque()
    parenthesis_exp = ""
    parenthesis_ctr = 0

    for c in exp:
        update = False

        if c in parenthesis or parenthesis_ctr > 0:
            if c == "(": parenthesis_ctr += 1
            elif c == ")": parenthesis_ctr -= 1
            parenthesis_exp += c

            if parenthesis_ctr == 0:
                parenthesis_exp = parenthesis_exp[1:-1]
                operand.append(toPostfix(parenthesis_exp))
                parenthesis_exp = ""
                update = len(operator) > 1

        elif c in priority.keys():
            operator.append(c)

        else:
            operand.append(c)
            update = len(operator) > 1

        if update and priority[operator[-1]] > priority[operator[-2]]:
            sub_answer = ""
            for i in range(2):
                sub_answer = operand.pop() + sub_answer
            sub_answer += operator.pop()
            operand.append(sub_answer)

    while operator:
        sub_answer = ""
        for i in range(2): sub_answer += operand.popleft()
        sub_answer += operator.popleft()
        operand.appendleft(sub_answer)

    return operand[0].strip()

print(toPostfix(line))


from . import posts
from .forms import PostForm
from flask import request, redirect, url_for, render_template, flash
from app.models import db, Post
from flask_login import current_user, login_required

# Create a post
@posts.route('/create_post', methods=['GET', 'POST'])
def create_post():
    form = PostForm()
    if request.method == 'POST' and form.validate_on_submit():

        # Grabbing our post form data
        title = form.title.data
        caption = form.caption.data
        img_url = form.img_url.data
        user_id = current_user.id

        # Creating an instance of the Post Model
        new_post = Post(title, caption, img_url, user_id)

        # Adding new post to our database
        db.session.add(new_post)
        db.session.commit()

        flash(f'Successfully created post {title}!', 'success')
        return redirect(url_for('main.home'))
    else:
        return render_template('create_post.html', form=form)

# Viewing all posts
@posts.route('/feed')
def feed():
    posts = Post.query.all()
    return render_template('feed.html', posts=posts)

# Update a post
@posts.route('/update/<int:post_id>', methods=['GET', 'POST'])
@login_required
def update_post(post_id):
    post = Post.query.get(post_id)
    form = PostForm()

    if current_user.id != post.user_id:
        flash("You cannot update someone else's post you snake! 🐍 ", 'warning')
        return redirect(url_for('posts.feed'))

    if request.method == 'POST' and form.validate_on_submit():
        # form data
        title = form.title.data
        caption = form.caption.data
        img_url = form.img_url.data

        # update post object in db
        post.title = title
        post.caption = caption
        post.img_url = img_url

        # commit changes to db
        db.session.commit()

        return redirect(url_for('posts.feed'))
    else:
        return render_template('update_post.html', post=post, form=form)

# delete post
@posts.route('/delete/<int:post_id>')
@login_required
def delete_post(post_id):
    post = Post.query.get(post_id)

    if current_user.id != post.user_id:
        flash("You cannot delete someone else's post you snake! 🐍 ", 'warning')
        return redirect(url_for('posts.feed'))

    db.session.delete(post)
    db.session.commit()
    flash(f'Successfully deleted post {post.title}!', 'info')
    return redirect(url_for('posts.feed'))


### ONLY predictions with the choosen model..

# Importing libaries
from sklearn.preprocessing import PolynomialFeatures
import pandas as pd
import numpy as np
import GUI as g

def pred_new(model, x):
    x = np.array(x).reshape(1,-1)
    # scaling:
    x = scaler.transform(x)
    pred = model.predict(x)
    print(f'{model} prediction is: {pred}')
    return pred

def pred_new_poly(model, x, degree):
    poly_reg = PolynomialFeatures (degree = degree)
    x = np.array(x).reshape(1,-1)
    # scaling:
    x = scaler.transform(x)
    x = poly_reg.fit_transform(x)
    pred = model.predict(x)
    print(f'{model} prediction is: {pred}')
    return pred

### Preperation of data

#reading data
df = pd.read_csv(r"C:\Users\Orang\OneDrive - Ariel University\Desktop\שנה ד\סמסטר ב\Data mining\Project\ready_data.csv", sep=',')

# dropping first and second column (not relevnt)
df.drop(columns=['precise_change'], inplace=True)

# splitting target and features.
X = df.drop(columns=['j_integral', 'Model Properties change '], inplace=False)
X = X.iloc[:,0:12]
X.drop(columns=['nu_T'], inplace=True)
y = df.loc[:,['j_integral']]

# scaling:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X)
X = scaler.transform(X)

# splitting data to train and test sets:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# building the model after grid search best parameters.
from xgboost import XGBRegressor
XGboost_model = XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
             colsample_bynode=1, colsample_bytree=1, enable_categorical=False,
             gamma=0, gpu_id=-1, importance_type=None,
             interaction_constraints='', learning_rate=0.1, max_delta_step=0,
             max_depth=3, min_child_weight=1,
             monotone_constraints='()', n_estimators=400, n_jobs=16,
             num_parallel_tree=1, predictor='auto', random_state=0, reg_alpha=0,
             reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method='exact',
             validate_parameters=1, verbosity=None)
XGboost_model.fit(X, y)

### Lets Predict values!

x = g.gui()
g.gui(pred_new(XGboost_model, x))


import requests
import streamlit as st
from typing import NamedTuple

# force wide mode
st.set_page_config(layout="wide")

query = st.text_input("Query")

if query:
    r = requests.get("http://localhost:8080/v1beta/search", params=dict(q=query))
    hits = r.json()["hits"]
    for x in hits:
        st.write(x)


'''35. Python program to check if a string has at least one letter and one number
Examples:
Input: welcome2ourcountry34
Output: True
Input: stringwithoutnum
Output: False'''

# solution
s=input("Enter a string : ")
num=False
alpha=False
for i in s:
  if i.isdigit():
    num=True
  elif i.isalpha():
    alpha=True
if num==True and alpha==True:
  print("True")
else:
  print("False")


import os
import sys
import numpy as np
import pandas as pd
import torch
from torchvision import transforms
from torch.utils import data
from torch import Tensor

from datetime import datetime
import itertools
import sqlite3
import logging
import time

from dataset.db_query import LeagueTag, SeasonTag
from dataset.dataset import SingleSeasonSingleLeague, SingleSeasonSingleLeagueTimeSlices

"""
All together we have 75 seasons in different leagues (minus the ones
we don't use because they have to many defect entries)

Each Seasons has around 316 matches (~ Bundesliga) probably all together
around 24000 matches ...

We do a 80/20 Train-Test Split where we use some of the Train seasons
for Validation.

i.e.
    50 for Train
    10 for Valid
    15 for Test
"""

TRAIN_SEASONS = [(LeagueTag.NET, SeasonTag.S14_15),
                 (LeagueTag.SPA, SeasonTag.S08_09),
                 (LeagueTag.NET, SeasonTag.S13_14),
                 (LeagueTag.SCO, SeasonTag.S12_13),
                 (LeagueTag.SCO, SeasonTag.S15_16),
                 (LeagueTag.NET, SeasonTag.S10_11),
                 (LeagueTag.BEL, SeasonTag.S15_16),
                 (LeagueTag.ITA, SeasonTag.S08_09),
                 (LeagueTag.ENG, SeasonTag.S09_10),
                 (LeagueTag.BEL, SeasonTag.S09_10),
                 (LeagueTag.NET, SeasonTag.S09_10),
                 (LeagueTag.ITA, SeasonTag.S12_13),
                 (LeagueTag.FRA, SeasonTag.S14_15),
                 (LeagueTag.BEL, SeasonTag.S14_15),
                 (LeagueTag.ITA, SeasonTag.S14_15),
                 (LeagueTag.ITA, SeasonTag.S15_16),
                 (LeagueTag.GER, SeasonTag.S13_14),
                 (LeagueTag.ITA, SeasonTag.S09_10),
                 (LeagueTag.SWI, SeasonTag.S11_12),
                 (LeagueTag.FRA, SeasonTag.S12_13),
                 (LeagueTag.ENG, SeasonTag.S13_14),
                 (LeagueTag.FRA, SeasonTag.S15_16),
                 (LeagueTag.SPA, SeasonTag.S10_11),
                 (LeagueTag.SWI, SeasonTag.S15_16),
                 (LeagueTag.SPA, SeasonTag.S13_14),
                 (LeagueTag.SCO, SeasonTag.S08_09),
                 (LeagueTag.SPA, SeasonTag.S09_10),
                 (LeagueTag.NET, SeasonTag.S11_12),
                 (LeagueTag.SPA, SeasonTag.S14_15),
                 (LeagueTag.ENG, SeasonTag.S14_15),
                 (LeagueTag.SCO, SeasonTag.S10_11),
                 (LeagueTag.ITA, SeasonTag.S11_12),
                 (LeagueTag.SCO, SeasonTag.S14_15),
                 (LeagueTag.POR, SeasonTag.S10_11),
                 (LeagueTag.NET, SeasonTag.S15_16),
                 (LeagueTag.SPA, SeasonTag.S11_12),
                 (LeagueTag.FRA, SeasonTag.S13_14),
                 (LeagueTag.GER, SeasonTag.S14_15),
                 (LeagueTag.GER, SeasonTag.S12_13),
                 (LeagueTag.BEL, SeasonTag.S13_14),
                 (LeagueTag.SCO, SeasonTag.S11_12),
                 (LeagueTag.POR, SeasonTag.S15_16),
                 (LeagueTag.POR, SeasonTag.S12_13),
                 (LeagueTag.GER, SeasonTag.S09_10),
                 (LeagueTag.BEL, SeasonTag.S10_11),
                 (LeagueTag.SWI, SeasonTag.S09_10),
                 (LeagueTag.ITA, SeasonTag.S10_11),
                 (LeagueTag.ENG, SeasonTag.S15_16),
                 (LeagueTag.SWI, SeasonTag.S12_13),
                 (LeagueTag.BEL, SeasonTag.S11_12),
                 (LeagueTag.POR, SeasonTag.S09_10)]

VALID_SEASONS = [(LeagueTag.SCO, SeasonTag.S13_14),
                 (LeagueTag.SWI, SeasonTag.S10_11),
                 (LeagueTag.POR, SeasonTag.S11_12),
                 (LeagueTag.ITA, SeasonTag.S13_14),
                 (LeagueTag.SPA, SeasonTag.S12_13),
                 (LeagueTag.NET, SeasonTag.S12_13),
                 (LeagueTag.POR, SeasonTag.S14_15),
                 (LeagueTag.ENG, SeasonTag.S11_12),
                 (LeagueTag.FRA, SeasonTag.S11_12),
                 (LeagueTag.ENG, SeasonTag.S10_11)]

TEST_SEASONS = [(LeagueTag.FRA, SeasonTag.S09_10),
                (LeagueTag.GER, SeasonTag.S08_09),
                (LeagueTag.BEL, SeasonTag.S12_13),
                (LeagueTag.ENG, SeasonTag.S08_09),
                (LeagueTag.GER, SeasonTag.S11_12),
                (LeagueTag.ENG, SeasonTag.S12_13),
                (LeagueTag.SWI, SeasonTag.S14_15),
                (LeagueTag.FRA, SeasonTag.S08_09),
                (LeagueTag.GER, SeasonTag.S10_11),
                (LeagueTag.SCO, SeasonTag.S09_10),
                (LeagueTag.SPA, SeasonTag.S15_16),
                (LeagueTag.SWI, SeasonTag.S13_14),
                (LeagueTag.GER, SeasonTag.S15_16),
                (LeagueTag.FRA, SeasonTag.S10_11),
                (LeagueTag.POR, SeasonTag.S13_14)]

SMALL_TRAIN_SEASONS = [(LeagueTag.NET, SeasonTag.S14_15),
                       (LeagueTag.SPA, SeasonTag.S08_09),
                       (LeagueTag.NET, SeasonTag.S13_14),
                       (LeagueTag.SCO, SeasonTag.S12_13),
                       (LeagueTag.SCO, SeasonTag.S15_16),
                       (LeagueTag.NET, SeasonTag.S10_11),
                       (LeagueTag.BEL, SeasonTag.S15_16),
                       (LeagueTag.ITA, SeasonTag.S08_09),
                       (LeagueTag.ENG, SeasonTag.S09_10),
                       (LeagueTag.BEL, SeasonTag.S09_10)]

SMALL_VALID_SEASONS = [(LeagueTag.SCO, SeasonTag.S13_14),
                 (LeagueTag.SWI, SeasonTag.S10_11),
                 (LeagueTag.POR, SeasonTag.S11_12)]

SMALL_TEST_SEASONS = [(LeagueTag.FRA, SeasonTag.S09_10),
                (LeagueTag.GER, SeasonTag.S08_09),
                (LeagueTag.BEL, SeasonTag.S12_13)]

def make_dataset(sql_path,
                 season_list,
                 undersample_probs=(1.0, 1.0, 1.0),
                 use_ts=False,
                 ts_size=30,
                 with_odds=False):
    datasets = list()
    for league_tag, season_tag in season_list:
        if use_ts:
            d = SingleSeasonSingleLeagueTimeSlices(
                sql_path,
                league_tag,
                season_tag,
                slice_size=ts_size,
                undersample_probs=undersample_probs,
                load_odds=with_odds)
        else:
            d = SingleSeasonSingleLeague(
                sql_path,
                league_tag,
                season_tag,
                undersample_probs=undersample_probs, load_odds=with_odds)
        datasets.append(d)
    return data.ConcatDataset(datasets)

def make_train_set(sql_path, undersample_probs=(1.0, 1.0, 1.0), use_ts=False, ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        TRAIN_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size,
        with_odds=with_odds)

def make_valid_set(sql_path, undersample_probs=(1.0, 1.0, 1.0), use_ts=False, ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        VALID_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size,
        with_odds=with_odds)

def make_test_set(sql_path, undersample_probs=(1.0, 1.0, 1.0), use_ts=False, ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        TEST_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size,
        with_odds=with_odds)

def make_small_train_set(sql_path,
                         undersample_probs=(1.0, 1.0, 1.0),
                         use_ts=False,
                         ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        SMALL_TRAIN_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size,
        with_odds=with_odds)

def make_small_valid_set(sql_path,
                         undersample_probs=(1.0, 1.0, 1.0),
                         use_ts=False,
                         ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        SMALL_VALID_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size,
        with_odds=with_odds)

def make_small_test_set(sql_path,
                        undersample_probs=(1.0, 1.0, 1.0),
                        use_ts=False,
                        ts_size=30, with_odds=False):
    return make_dataset(
        sql_path,
        SMALL_TEST_SEASONS,
        undersample_probs=undersample_probs,
        use_ts=use_ts,
        ts_size=ts_size, with_odds=with_odds)


"""A module for considering the dimensions of a room."""

def calculate_room_area():
	"""Prompt user for length and width of a room and calculate area"""
	length_prompt = "Please enter the length of the room in meters:\n"
	width_prompt = "Please enter the width of the room in meters:\n"
	quit_msg = 'Enter "q" or "quit" at any time to exit.'

	print(quit_msg)
	length_str = input(length_prompt)

	if length_str == 'q' or length_str == 'quit':
		print('quitting...')
		return

	print(quit_msg)
	width_str = input(width_prompt)

	if width_str == 'q' or width_str == 'quit':
		print('quitting...')
		return

	length = int(length_str)
	width = int(width_str)
	area = length * width

	print(f'The area of the room is {area} square meters ({area * 3.281} square eet).')

if __name__ == '__main__':
	calculate_room_area()


from .debuggablesubsystem import DebuggableSubsystem
from wpilib.cantalon import CANTalon
from networktables import NetworkTable

from robotpy_ext.common_drivers.navx.ahrs import AHRS

from custom.config import Config
from commands.drivecommand import DriveCommand
import ports

class BaseDrive(DebuggableSubsystem):
    '''
    A general case drive train system. It abstracts away shared functionality of
    the various drive types that we can employ. Anything that can be done with
    knowing what type of drive system we have should be implemented here.
    '''

    def __init__(self, name):
        super().__init__(name)

        '''
        Create all motors, disable the watchdog, and turn off neutral braking
        since the PID loops will provide braking.
        '''
        self.motors = [
            CANTalon(ports.drivetrain.frontLeftMotorID),
            CANTalon(ports.drivetrain.frontRightMotorID),
            CANTalon(ports.drivetrain.backLeftMotorID),
            CANTalon(ports.drivetrain.backRightMotorID),
        ]

        for motor in self.motors:
            motor.setSafetyEnabled(False)
            motor.enableBrakeMode(False)

        '''
        Subclasses should configure motors correctly and populate activeMotors.
        '''
        self.activeMotors = []
        self._configureMotors()

        '''Initialize the navX MXP'''
        self.navX = AHRS.create_spi()
        self.resetGyro()

        '''A record of the last arguments to move()'''
        self.lastInputs = None

        self.setUseEncoders()
        self.maxSpeed = 1

        '''Allow changing CAN Talon settings from dashboard'''
        self._publishPID('Speed', 0)
        self._publishPID('Position', 1)

        '''Add items that can be debugged in Test mode.'''
        self.debugSensor('navX', self.navX)

        self.debugMotor('Front Left Motor', self.motors[0])
        self.debugMotor('Front Right Motor', self.motors[1])
        self.debugMotor('Back Left Motor', self.motors[2])
        self.debugMotor('Back Right Motor', self.motors[3])

    def initDefaultCommand(self):
        '''
        By default, unless another command is running that requires this
        subsystem, we will drive via joystick using the max speed stored in
        Config.
        '''
        self.setDefaultCommand(DriveCommand(Config('DriveTrain/maxSpeed')))

    def move(self, x, y, rotate):
        '''Turns coordinate arguments into motor outputs.'''

        '''
        Short-circuits the rather expensive movement calculations if the
        coordinates have not changed.
        '''
        if [x, y, rotate] == self.lastInputs:
            return

        self.lastInputs = [x, y, rotate]

        # Prevent drift caused by small input values
        x -= x % .01
        y -= y % .01
        rotate -= rotate % .01

        speeds = self._calculateSpeeds(x, y, rotate)
        '''Prevent speeds > 1'''
        maxSpeed = 0
        for speed in speeds:
            maxSpeed = max(abs(speed), maxSpeed)

        if maxSpeed > 1:
            speeds = [x / maxSpeed for x in speeds]

        '''Use speeds to feed motor output.'''
        if self.useEncoders:
            if all(abs(x) < 0.1 for x in speeds):
                '''
                When we are trying to stop, clearing the I accumulator can
                reduce overshooting, thereby shortening the time required to
                come to a stop.
                '''
                for motor in self.activeMotors:
                    motor.clearIaccum()

            for motor, speed in zip(self.activeMotors, speeds):
                motor.set(speed * self.speedLimit)

        else:
            for motor, speed in zip(self.activeMotors, speeds):
                motor.set(speed * self.maxPercentVBus)

    def setPositions(self, positions):
        '''
        Have the motors move to the given positions. There should be one
        position per active motor. Extra positions will be ignored.
        '''

        if not self.useEncoders:
            raise RuntimeError('Cannot set position. Encoders are disabled.')

        self._setMode(CANTalon.ControlMode.Position)
        for motor, position in zip(self.activeMotors, positions):
            motor.set(position)

    def atPosition(self, tolerance=10):
        '''
        Check setpoint error to see if it is below the given tolerance.
        '''

        error = 0
        for motor in self.activeMotors:
            error += abs(motor.getError())

        error /= len(self.activeMotors)

        return error <= tolerance

    def stop(self):
        '''A nice shortcut for calling move with all zeroes.'''

        if self.useEncoders:
            self._setMode(CANTalon.ControlMode.Speed)

        self.move(0, 0, 0)

    def resetGyro(self):
        '''Force the navX to consider the current angle to be zero degrees.'''

        self.navX.reset()
        self.gyroOffset = 0

    def setGyroAngle(self, angle):
        '''Tweak the gyro reading.'''

        heading = self.naxX.getYaw()
        self.gyroOffset = angle - heading

    def getAngle(self):
        '''Current gyro reading'''

        return (self.navX.getYaw() + self.gyroOffset) % 360

    def getSpeeds(self):
        '''Returns the speed of each active motors.'''

        if not self.useEncoders:
            raise RuntimeError('Cannot read speed. Encoders are disabled.')

        return [x.getSpeed() for x in self.activeMotors]

    def getPositions(self):
        '''Returns the position of each active motor.'''

        if not self.useEncoders:
            raise RuntimeError('Cannot read position. Encoders are disabled.')

        return [x.getPosition() for x in self.activeMotors]

    def setUseEncoders(self, useEncoders=True):
        '''
        Turns on and off encoders. As a side effect, if encoders are enabled,
        the motors will be set to speed mode. Disabling encoders should not be
        done lightly, as many commands rely on encoder information.
        '''

        self.useEncoders = useEncoders
        if useEncoders:
            self._setMode(CANTalon.ControlMode.Speed)
        else:
            self._setMode(CANTalon.ControlMode.PercentVbus)

    def setSpeedLimit(self, speed):
        '''
        Updates the max speed of the drive and changes to the appropriate
        mode depending on if encoders are enabled.
        '''

        if speed < 0:
            raise ValueError('DriveTrain speed cannot be less than 0')

        self.speedLimit = speed
        if speed > self.maxSpeed:
            self.maxSpeed = speed

        '''If we can't use encoders, attempt to approximate that speed.'''
        self.maxPercentVBus = speed / self.maxSpeed

        if self.useEncoders:
            self._setMode(CANTalon.ControlMode.Speed)
        else:
            self._setMode(CANTalon.ControlMode.PercentVbus)

    def _setMode(self, mode):
        '''
        Sets the control mode of active motors, with some intelligent changes
        depending on the mode.
        '''

        maxVoltage = self.activeMotors[0].getBusVoltage()

        for motor in self.activeMotors:
            motor.configMaxOutputVoltage(maxVoltage)

            if mode == CANTalon.ControlMode.Position:
                motor.setProfile(1)
                motor.configMaxOutputVoltage(maxVoltage / 2)

            elif mode == CANTalon.ControlMode.Speed:
                motor.setProfile(0)
                motor.clearIaccum()

            motor.setControlMode(mode)

    def _publishPID(self, table, profile):
        '''
        Read the PID value from the first active CAN Talon and publish it to the
        passed NetworkTable.
        '''

        table = NetworkTable.getTable('DriveTrain/%s' % table)

        talon = self.activeMotors[0]

        talon.setProfile(profile)
        table.putNumber('P', talon.getP())
        table.putNumber('I', talon.getI())
        table.putNumber('D', talon.getD())
        table.putNumber('F', talon.getF())
        table.putNumber('IZone', talon.getIZone())
        table.putNumber('RampRate', talon.getCloseLoopRampRate())

        table.addTableListener(self._PIDListener(profile))

    def _PIDListener(self, profile):
        '''Provides as easy way to make sure we update the right profile.'''

        def updatePID(table, key, value, isNew):
            '''
            Loops over all active motors and updates the appropriate setting. To
            avoid using a very long if structure inside the loop, we use getattr
            to access the methods of the motor by name.
            '''

            funcs = {
                'P': 'setP',
                'I': 'setI',
                'D': 'setD',
                'F': 'setF',
                'IZone': 'setIZone',
                'RampRate': 'setCloseLoopRampRate'
            }

            for motor in self.activeMotors:
                motor.setProfile(profile)
                getattr(motor, funcs[key])(value)

        return updatePID

    def _configureMotors(self):
        '''
        Make any necessary changes to the motors and define self.activeMotors.
        '''

        raise NotImplementedError()

    def _calculateSpeeds(self, x, y, rotate):
        '''Return a speed for each active motor.'''

        raise NotImplementedError()


from urllib.request import urlopen
import os
import sys
import json
import traceback

_resources_dir = 'resources'
_licenses_json_file = 'licenses.json'
_frequentLicenselist_file = 'frequentLicenselist.json'
_frequent_lic_nick_json_file = 'frequent_license_nick_list.json'

def get_license_from_nick():
    licenses = {}
    licenses_file = os.path.join(_resources_dir, _frequent_lic_nick_json_file)

    try:
        base_dir = os.path.join(sys._MEIPASS, vars(sys.modules[__name__])['__package__'])
    except Exception:
        base_dir = os.path.dirname(__file__)

    file_withpath = os.path.join(base_dir, licenses_file)
    try:
        with open(file_withpath, 'r') as f:
            licenses = json.load(f)
    except Exception as ex:
        print(f"Error to get license from json file : {ex}")

    return licenses

def get_spdx_licenses_json():
    success = True
    error_msg = ''
    licenses = ''
    # licenses : https://github.com/spdx/license-list-data/blob/v3.17/json/licenses.json
    licenses_file = os.path.join(_resources_dir, _licenses_json_file)

    try:
        base_dir = os.path.join(sys._MEIPASS, vars(sys.modules[__name__])['__package__'])
    except Exception:
        base_dir = os.path.dirname(__file__)

    file_withpath = os.path.join(base_dir, licenses_file)
    try:
        with open(file_withpath, 'r') as f:
            licenses = json.load(f)
    except Exception as e:
        success = False
        error_msg = 'Failed to open ' + file_withpath + ': ' + str(e)

    return success, error_msg, licenses

def create_frequentlicenses():
    success = True
    error_msg = ''
    licenses = ''
    frequentLicenses = {}
    _frequentLicenses_key = 'frequentLicenses'

    # spdx_txt_url = _spdx_txt_base_url + version + /text/ + licenseId + '.txt'
    _spdx_txt_base_url = 'https://raw.githubusercontent.com/spdx/license-list-data/'

    try:
        success, error_msg, licenses = get_spdx_licenses_json()
        if success:
            version = licenses['licenseListVersion']
            frequentLicenses[_frequentLicenses_key] = []
            for lic in licenses['licenses']:
                tmp_lic = {}
                tmp_lic["fullName"] = lic['name']
                tmp_lic["shortName"] = lic['licenseId']

                deprecated = lic['isDeprecatedLicenseId']
                if deprecated:
                    spdx_txt_url = _spdx_txt_base_url + 'v' + version + '/text/' + 'deprecated_' + lic['licenseId'] + '.txt'
                else:
                    spdx_txt_url = _spdx_txt_base_url + 'v' + version + '/text/' + lic['licenseId'] + '.txt'

                with urlopen(spdx_txt_url) as url:
                    licenseText = url.read().decode('utf-8')
                tmp_lic["defaultText"] = licenseText

                frequentLicenses[_frequentLicenses_key].append(tmp_lic)

    except Exception as e:
        success = False
        error_msg = 'Failed to open and parse licenses.json'
        print(error_msg, ": ", e)
        print(traceback.format_exc())

    return frequentLicenses, success, error_msg

def main():
    _frequencyLicense = os.path.join(_resources_dir, _frequentLicenselist_file)
    frequentLicenses, success, error_msg = create_frequentlicenses()
    if success:
        with open(_frequencyLicense, 'w', encoding='utf-8') as f:
            json.dump(frequentLicenses, f, sort_keys=True, indent=4)
        print("Created " + _frequentLicenselist_file + " in " + _resources_dir)

if __name__ == '__main__':
    main()


import aeidon
import contextlib
import inspect
import locale
import os
import random
import re
import shutil
import subprocess
import stat
import sys
import urllib.parse

def affirm(value):
    """Raise :exc:`aeidon.AffirmationError` if value evaluates to ``False``."""
    if not value:
        raise aeidon.AffirmationError

def atomic_open_require(path, mode="w", *args, **kwargs):
    assert "w" in mode

@contextlib.contextmanager
@aeidon.deco.contractual
def atomic_open(path, mode="w", *args, **kwargs):
    """
    A context manager for atomically writing a file.

    The file is written to a temporary file on the same filesystem, flushed and
    fsynced and then renamed to replace the existing file. This should
    (probably) be atomic on any Unix system. On Windows, it should (probably)
    be atomic if using Python 3.3 or greater.
    """
    path = os.path.realpath(path)
    chars = list("abcdefghijklmnopqrstuvwxyz0123456789")
    directory = os.path.dirname(path)
    basename = os.path.basename(path)
    while True:
        # Let's use a hidden temporary file to avoid a file
        # flickering in a possibly open file browser window.
        suffix = "".join(random.sample(chars, 8))
        temp_basename = ".{}.tmp{}".format(basename, suffix)
        temp_path = os.path.join(directory, temp_basename)
        if not os.path.isfile(temp_path): break
    try:
        if os.path.isfile(path):
            # If the file exists, use the same permissions.
            # Note that all other file metadata, including
            # owner and group, is not preserved.
            with open(temp_path, "w") as fobj: pass
            st = os.stat(path)
            os.chmod(temp_path, stat.S_IMODE(st.st_mode))
        with open(temp_path, mode, *args, **kwargs) as fobj:
            yield fobj
            fobj.flush()
            os.fsync(fobj.fileno())
        try:
            if hasattr(os, "replace"):
                # os.replace was added in Python 3.3.
                # This should be atomic on Windows too.
                os.replace(temp_path, path)
            else:
                # os.rename is atomic on Unix, but fails
                # on Windows if the file exists.
                os.rename(temp_path, path)
            # os.rename and os.replace will fail if path
            # and temp_path are not on the same device,
            # for instance they can be on two separate
            # branches of a union mount. Atomicity is not
            # possible in this case.
        except OSError:
            # Fall back to a non-atomic operation using
            # shutil.move. On Windows this requires that
            # the destination file does not exist.
            if sys.platform == "win32":
                if os.path.isfile(path):
                    os.remove(path)
            shutil.move(temp_path, path)
    finally:
        try:
            os.remove(temp_path)
        except Exception:
            pass

@aeidon.deco.once
def chardet_available():
    """Return ``True`` if :mod:`chardet` module is available."""
    try:
        import chardet
        return True
    except Exception:
        return False

def compare_versions_require(x, y):
    re_version = re.compile(r"^(\d+\.)+\d+$")
    assert re_version.match(x) is not None
    assert re_version.match(y) is not None

@aeidon.deco.contractual
def compare_versions(x, y):
    """
    Compare version strings `x` and `y`.

    Used version number formats are ``MAJOR.MINOR``, ``MAJOR.MINOR.PATCH`` and
    ``MAJOR.MINOR.PATCH.DATE/REVISION``, where all items are integers.
    Return 1 if `x` newer, 0 if equal or -1 if `y` newer.
    """
    compare = lambda a, b: (a > b) - (a < b)
    return compare(list(map(int, x.split("."))),
                   list(map(int, y.split("."))))

def connect_require(observer, observable, signal, *args):
    if observer is not observable:
        assert hasattr(observer, observable)

@aeidon.deco.contractual
def connect(observer, observable, signal, *args):
    """
    Connect `observable`'s `signal` to `observer`'s callback method.

    If `observable` is a string, it should be an attribute of `observer`.
    If `observable` is not a string it should be the same as `observer`.
    """
    method_name = signal.replace("-", "_").replace("::", "_")
    if observer is not observable:
        method_name = "_".join((observable, method_name))
    method_name = ("_on_{}".format(method_name)).replace("__", "_")
    if not hasattr(observer, method_name):
        method_name = method_name[1:]
    method = getattr(observer, method_name)
    if observer is not observable:
        observable = getattr(observer, observable)
    return observable.connect(signal, method, *args)

def copy_dict_ensure(src, value):
    assert src == value
    assert src is not value

@aeidon.deco.contractual
def copy_dict(src):
    """Copy `src` dictionary recursively and return copy."""
    dst = src.copy()
    for key, value in src.items():
        if isinstance(value, dict):
            dst[key] = copy_dict(value)
        if isinstance(value, list):
            dst[key] = copy_list(value)
        if isinstance(value, set):
            dst[key] = set(value)
    return dst

def copy_list_ensure(src, value):
    assert src == value
    assert src is not value

@aeidon.deco.contractual
def copy_list(src):
    """Copy `src` list recursively and return copy."""
    dst = list(src)
    for i, value in enumerate(src):
        if isinstance(value, dict):
            dst[i] = copy_dict(value)
        if isinstance(value, list):
            dst[i] = copy_list(value)
        if isinstance(value, set):
            dst[i] = set(value)
    return dst

def detect_format_require(path, encoding):
    assert aeidon.encodings.is_valid_code(encoding)

@aeidon.deco.contractual
def detect_format(path, encoding):
    """
    Detect and return format of subtitle file at `path`.

    Raise :exc:`IOError` if reading fails.
    Raise :exc:`UnicodeError` if decoding fails.
    Raise :exc:`aeidon.FormatError` if unable to detect format.
    Return an :attr:`aeidon.formats` enumeration item.
    """
    re_ids = [(x, re.compile(x.identifier)) for x in aeidon.formats]
    with open(path, "r", encoding=encoding) as fobj:
        for line in fobj:
            for format, re_id in re_ids:
                if re_id.search(line) is not None:
                    return format
    raise aeidon.FormatError("Failed to detect format of file {}"
                             .format(repr(path)))

def detect_newlines(path):
    """Detect and return the newline type of file at `path` or ``None``."""
    try:
        fobj = open(path, "r", newline="")
        fobj.read()
        chars = fobj.newlines
    except Exception:
        return None
    if chars is None:
        return None
    if isinstance(chars, str):
        return aeidon.newlines.find_item("value", chars)
    if isinstance(chars, tuple):
        if len(chars) == 1:
            return aeidon.newlines.find_item("value", chars[0])
        # This is not actually correct. If both CR and LF are detected,
        # it could mean a mixture of Mac and Unix newlines on separate
        # lines or one Windows newline in a mostly something else file.
        # We could count the frequencies, but it's probably not worth
        # the effort.
        return aeidon.newlines.WINDOWS
    return None

@aeidon.deco.once
def enchant_available():
    """Return ``True`` if :mod:`enchant` module is available."""
    try:
        import enchant
        return True
    except Exception:
        return False

@aeidon.deco.once
def enchant_and_dicts_available():
    """Return ``True`` if :mod:`enchant` and dictionaries are available."""
    try:
        import enchant
        return bool(enchant.list_languages())
    except Exception:
        return False

def flatten(lst):
    """
    Return a shallow version of `lst`.

    >>> aeidon.util.flatten([1, 2, 3, [4, 5, [6]]])
    [1, 2, 3, 4, 5, 6]
    """
    flat_lst = []
    for item in lst:
        if isinstance(item, list):
            flat_lst.extend(flatten(item))
        else: # Non-list item.
            flat_lst.append(item)
    return flat_lst

def get_chardet_version_ensure(value):
    if value is not None:
        assert isinstance(value, str)

@aeidon.deco.contractual
def get_chardet_version():
    """Return :mod:`chardet` version number as string or ``None``."""
    try:
        import chardet
        return chardet.__version__
    except Exception:
        return None

def get_default_encoding_ensure(value):
    assert aeidon.encodings.is_valid_code(value)

@aeidon.deco.once
@aeidon.deco.contractual
def get_default_encoding(fallback="utf_8"):
    """Return the locale encoding or `fallback`."""
    encoding = locale.getpreferredencoding()
    encoding = encoding or fallback
    re_illegal = re.compile(r"[^a-z0-9_]")
    encoding = re_illegal.sub("_", encoding.lower())
    encoding = get_encoding_alias(encoding)
    return encoding

@aeidon.deco.once
def get_default_newline():
    """Return system default newline as :attr:`aeidon.newlines` item."""
    return aeidon.newlines.find_item("value", os.linesep)

def get_enchant_version_ensure(value):
    if value is not None:
        assert isinstance(value, str)

@aeidon.deco.contractual
def get_enchant_version():
    """Return :mod:`enchant` version number as string or ``None``."""
    try:
        import enchant
        return enchant.__version__
    except Exception:
        return None

def get_encoding_alias(encoding):
    """Return proper Python alias for `encoding`."""
    from encodings.aliases import aliases
    if encoding in aliases:
        return aliases[encoding]
    return encoding

def get_ranges_require(lst):
    for item in lst:
        assert isinstance(item, int)

def get_ranges_ensure(value, lst):
    for item in value:
        assert item == list(range(item[0], item[-1] + 1))

@aeidon.deco.contractual
def get_ranges(lst):
    """
    Return a list of ranges in list of integers.

    >>> aeidon.util.get_ranges([1, 2, 3, 5, 6, 7, 9, 11, 12])
    [[1, 2, 3], [5, 6, 7], [9], [11, 12]]
    """
    if not lst: return []
    lst = get_sorted_unique(lst)
    ranges = [[lst.pop(0)]]
    for item in lst:
        if item == ranges[-1][-1] + 1:
            ranges[-1].append(item)
        else: ranges.append([item])
    return ranges

def get_sorted_unique_ensure(value, lst):
    for item in value:
        assert value.count(item) == 1
    assert sorted(value) == value

@aeidon.deco.contractual
def get_sorted_unique(lst):
    """
    Return sorted `lst` with duplicates removed.

    >>> aeidon.util.get_sorted_unique([3, 2, 1, 2, 4, 4])
    [1, 2, 3, 4]
    """
    lst = sorted(lst)
    for i in reversed(range(1, len(lst))):
        if lst[i] == lst[i - 1]:
            lst.pop(i)
    return lst

@aeidon.deco.memoize(100)
def get_template_header(format):
    """
    Read and return the template header for `format`.

    Raise :exc:`IOError` if reading global header file fails.
    Raise :exc:`UnicodeError` if decoding global header file fails.
    """
    header = None
    directory = os.path.join(aeidon.DATA_HOME_DIR, "headers")
    path = os.path.join(directory, format.name.lower())
    if os.path.isfile(path):
        try: header = read(path, None).rstrip()
        except IOError:
            print_read_io(sys.exc_info(), path)
        except UnicodeError:
            print_read_unicode(sys.exc_info(),
                               path,
                               get_default_encoding())

    if header is None:
        directory = os.path.join(aeidon.DATA_DIR, "headers")
        path = os.path.join(directory, format.name.lower())
        header = read(path, "ascii").rstrip()
    return normalize_newlines(header)

def get_unique_ensure(value, lst, keep_last=False):
    for item in value:
        assert value.count(item) == 1

@aeidon.deco.contractual
def get_unique(lst, keep_last=False):
    """
    Return `lst` with duplicates removed.

    Keep the last duplicate if `keep_last` is ``True``, else keep first.

    >>> aeidon.util.get_unique([3, 2, 1, 2, 4, 4])
    [3, 2, 1, 4]
    >>> aeidon.util.get_unique([3, 2, 1, 2, 4, 4], keep_last=True)
    [3, 1, 2, 4]
    """
    lst = lst[:]
    if keep_last:
        lst.reverse()
    for i in reversed(range(len(lst))):
        for j in range(0, i):
            if lst[j] == lst[i]:
                lst.pop(i)
                break
    if keep_last:
        lst.reverse()
    return lst

def install_module(name, obj):
    """
    Install `obj`'s module into the :mod:`aeidon` namespace.

    Typical call is of form::

        aeidon.util.install_module("foo", lambda: None)
    """
    aeidon.__dict__[name] = inspect.getmodule(obj)

def is_command(command):
    """Return ``True`` if `command` exists as a file in ``$PATH``."""
    dirs = os.environ.get("PATH", "").split(os.pathsep)
    paths = [os.path.join(x, command) for x in dirs]
    return any(map(os.path.isfile, paths))

def last(iterator):
    """Return the last value from `iterator` or ``None``."""
    value = None
    for value in iterator: pass
    return value

def makedirs_ensure(value, directory):
    assert os.path.isdir(directory)

@aeidon.deco.contractual
def makedirs(directory):
    """
    Recursively make `directory` if it does not exist.

    Raise :exc:`OSError` if unsuccessful.
    """
    if not os.path.isdir(directory):
        os.makedirs(directory)

def normalize_newlines(text):
    """
    Convert all newlines in `text` to Unix newlines.

    >>> aeidon.util.normalize_newlines("one\\r\\ntwo")
    'one\\ntwo'
    """
    re_newline_char = re.compile(r"\r\n?")
    return re_newline_char.sub("\n", text)

def path_to_uri(path):
    """Convert local filepath to URI."""
    if sys.platform == "win32":
        path = "/{}".format(path.replace("\\", "/"))
    return "file://{}".format(urllib.parse.quote(path))

def print_read_io(exc_info, path):
    """Print :exc:`IOError` message to standard error."""
    print("Failed to read file '{}': {}"
          .format(path, exc_info[1].args[1]),
          file=sys.stderr)

def print_read_unicode(exc_info, path, encoding):
    """Print :exc:`UnicodeError` message to standard error."""
    encoding = encoding or get_default_encoding()
    print("Failed to decode file '{}' with codec '{}'"
          .format(path, encoding),
          file=sys.stderr)

def print_remove_os(exc_info, path):
    """Print :exc:`OSError` message to standard error."""
    print("Failed to remove file '{}': {}"
          .format(path, exc_info[1].args[1]),
          file=sys.stderr)

def print_write_io(exc_info, path):
    """Print :exc:`IOError` message to standard error."""
    print("Failed to write file '{}': {}"
          .format(path, exc_info[1].args[1]),
          file=sys.stderr)

def print_write_unicode(exc_info, path, encoding):
    """Print :exc:`UnicodeError` message to standard error."""
    encoding = encoding or get_default_encoding()
    print("Failed to encode file '{}' with codec '{}'"
          .format(path, encoding),
          file=sys.stderr)

def read_require(path, encoding=None, fallback="utf_8"):
    if encoding is not None:
        assert aeidon.encodings.is_valid_code(encoding)
    if fallback is not None:
        assert aeidon.encodings.is_valid_code(fallback)

@aeidon.deco.contractual
def read(path, encoding=None, fallback="utf_8"):
    """
    Read file at `path` and return text.

    `fallback` should be ``None`` to try only `encoding`.
    Raise :exc:`IOError` if reading fails.
    Raise :exc:`UnicodeError` if decoding fails.
    """
    encoding = encoding or get_default_encoding()
    try:
        with open(path, "r", encoding=encoding) as fobj:
            return fobj.read().strip()
    except UnicodeError:
        if not fallback in (encoding, None, ""):
            return read(path, fallback, None)
        raise # UnicodeError

def readlines_require(path, encoding=None, fallback="utf_8"):
    if encoding is not None:
        assert aeidon.encodings.is_valid_code(encoding)
    if fallback is not None:
        assert aeidon.encodings.is_valid_code(fallback)

@aeidon.deco.contractual
def readlines(path, encoding=None, fallback="utf_8"):
    """
    Read file at `path` and return lines.

    `fallback` should be ``None`` to try only `encoding`.
    Raise :exc:`IOError` if reading fails.
    Raise :exc:`UnicodeError` if decoding fails.
    """
    text = read(path, encoding, fallback)
    text = normalize_newlines(text)
    return text.split("\n")

def replace_extension(path, format):
    """Replace possible extension in `path` with that of `format`."""
    extensions = [x.extension for x in aeidon.formats]
    if path.endswith(tuple(extensions)):
        path = path[:path.rfind(".")]
    return "".join((path, format.extension))

def shell_quote(path):
    """Quote and escape `path` for shell use."""
    if sys.platform != "win32":
        # Windows filenames can contain backslashes only as
        # directory separators and cannot contain double quotes.
        path = path.replace("\\", "\\\\")
        path = path.replace('"', '\\"')
    return '"{}"'.format(path)

def start_process(command, **kwargs):
    """
    Start `command` as a new background subprocess.

    `command` and `kwargs` are passed to :class:`subprocess.Popen`.
    Raise :exc:`aeidon.ProcessError` if something goes wrong.
    Return :class:`subprocess.Popen` instance.
    """
    # Use no environment on Windows due to a subprocess bug.
    # https://bugzilla.gnome.org/show_bug.cgi?id=605805
    try:
        return subprocess.Popen(command,
                                shell=(sys.platform != "win32"),
                                cwd=os.getcwd(),
                                env=(os.environ.copy()
                                     if sys.platform != "win32"
                                     else None),

                                universal_newlines=True,
                                **kwargs)

    except OSError as error:
        raise aeidon.ProcessError(str(error.args))

def title_to_lower_case_ensure(value, title_name):
    assert value.islower()

@aeidon.deco.memoize(100)
@aeidon.deco.contractual
def title_to_lower_case(title_name):
    """
    Convert title case name to lower case with underscores.

    >>> aeidon.util.title_to_lower_case('TitleCase')
    'title_case'
    """
    lower_name = ""
    for char in title_name:
        if char.isupper() and lower_name:
            lower_name += "_"
        lower_name += char.lower()
    return lower_name

def uri_to_path(uri):
    """Convert `uri` to local filepath."""
    uri = urllib.parse.unquote(uri)
    if sys.platform == "win32":
        path = urllib.parse.urlsplit(uri)[2]
        while path.startswith("/"):
            path = path[1:]
        return path.replace("/", "\\")
    return urllib.parse.urlsplit(uri)[2]

def write_require(path, text, encoding=None, fallback="utf_8"):
    if encoding is not None:
        assert aeidon.encodings.is_valid_code(encoding)
    if fallback is not None:
        assert aeidon.encodings.is_valid_code(fallback)

def write_ensure(value, path, text, encoding=None, fallback="utf_8"):
    assert os.path.isfile(path)

@aeidon.deco.contractual
def write(path, text, encoding=None, fallback="utf_8"):
    """
    Write `text` to file at `path`.

    `fallback` should be ``None`` to try only `encoding`.
    Raise :exc:`IOError` if writing fails.
    Raise :exc:`UnicodeError` if encoding fails.
    """
    encoding = encoding or get_default_encoding()
    try:
        with open(path, "w", encoding=encoding) as fobj:
            return fobj.write(text)
    except UnicodeError:
        if not fallback in (encoding, None, ""):
            return write(path, text, fallback, None)
        raise # UnicodeError

def writelines_require(path, lines, encoding=None, fallback="utf_8"):
    if encoding is not None:
        assert aeidon.encodings.is_valid_code(encoding)
    if fallback is not None:
        assert aeidon.encodings.is_valid_code(fallback)

def writelines_ensure(value, path, lines, encoding=None, fallback="utf_8"):
    assert os.path.isfile(path)

@aeidon.deco.contractual
def writelines(path, lines, encoding=None, fallback="utf_8"):
    """
    Write `lines` of text to file at `path`.

    `fallback` should be ``None`` to try only `encoding`.
    Raise :exc:`IOError` if writing fails.
    Raise :exc:`UnicodeError` if encoding fails.
    """
    text = "\n".join(lines) + "\n"
    return write(path, text, encoding, fallback)


from threading import Thread

import requests

from api import NETWORK_EXCEPTIONS, token, async_utils
from api.albums import _create_album_from_json
from api.tracks import _create_track_from_json
from domain.album import Album
from domain.artist import Artist
from utils import retry

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_artist(artist_id: str) -> Artist:
    json = requests.get(
        url=f'https://api.spotify.com/v1/artists/{artist_id}',
        headers={'Authorization': f'Bearer {token}'}
    ).json()
    return _create_artist_from_json(json)

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_artists(artist_ids: list[str]) -> list[Artist]:
    json = requests.get(
        url=f'https://api.spotify.com/v1/artists',
        params={'ids': ','.join(artist_ids)},
        headers={'Authorization': f'Bearer {token}'}
    ).json()
    return [_create_artist_from_json(j) for j in json['artists']]

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_artists_async(artist_ids: list[str], callback) -> Thread:
    return async_utils.async_request(
        'get',
        url=f'https://api.spotify.com/v1/artists',
        params={'ids': ','.join(artist_ids)},
        headers={'Authorization': f'Bearer {token}'},
        callback=lambda r: callback([_create_artist_from_json(j) for j in r.json()['artists']])
    )

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_artist_albums(artist_id: str, country_code: str = None) -> list[Album]:
    json = requests.get(
        url=f'https://api.spotify.com/v1/artists/{artist_id}/albums',
        params={'market': country_code, 'limit': 50},
        headers={'Authorization': f'Bearer {token}'}
    ).json()
    return [_create_album_from_json(j) for j in json['items']]

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_artist_top_tracks(artist_id: str, country_code: str):
    json = requests.get(
        url=f'https://api.spotify.com/v1/artists/{artist_id}/top-tracks',
        params={'market': country_code},
        headers={'Authorization': f'Bearer {token}'}
    ).json()
    return [_create_track_from_json(j) for j in json['tracks']]

@retry(times=3, exceptions=NETWORK_EXCEPTIONS)
def get_related_artists(artist_id: str):
    json = requests.get(
        url=f'https://api.spotify.com/v1/artists/{artist_id}/related-artists',
        headers={'Authorization': f'Bearer {token}'}
    ).json()
    return [_create_artist_from_json(j) for j in json['artists']]

def _create_artist_from_json(json: dict) -> Artist:
    return Artist(json['id'], json['name'], json['followers']['total'], json['genres'], json['popularity'])


from flask import Flask, jsonify, render_template, Blueprint, request, make_response, redirect, url_for
from . import db
from .models import People, User
from datetime import datetime as dt
routes = Blueprint('routes', __name__)

@routes.route("/")
def hello():
    return render_template('users.jinja2')

@routes.route("/person", methods=['GET'])
def person():
    """Create a user via query string parameters."""
    username = request.args.get("user")
    email = request.args.get("email")
    if username and email:
        existing_user = User.query.filter(
            User.username == username or User.email == email
        ).first()
        if existing_user:
            return make_response(f"{username} ({email}) already created!")
        new_user = User(
            username=username,
            email=email,
            created=dt.now(),
            bio="In West Philadelphia born and raised, \
            on the playground is where I spent most of my days",
            admin=False,
        )  # Create an instance of the User class
        db.session.add(new_user)  # Adds new User record to database
        db.session.commit()  # Commits all changes
        redirect(url_for("person"))
    return render_template("users.jinja2", users=User.query.all(), title="Show Users")

@routes.route("/button")
def button():
    return '<button> Click here </button>'
@routes.route("/addperson")
def addperson():
    return render_template("form.html")
@routes.route("/health")
def health():
    return jsonify({'status': 'ok'})
@routes.route("/personadd", methods=['POST'])
def personadd():
    pname = request.form["pname"]
    color = request.form["color"]
    entry = People(pname, color)
    db.session.add(entry)
    db.session.commit()
    return render_template("index.html")


r"""Curved elements.

This example solves the eigenvalue problem

.. math::
   -\Delta u = \lambda u \quad \text{in $\Omega$},
with the boundary condition :math:`u|_{\partial \Omega} = 0` using isoparametric
mapping via biquadratic basis and finite element approximation using fifth-order
quadrilaterals.

"""
from skfem import *
from skfem.models.poisson import laplace, mass
import numpy as np

# create mesh and element for the finite element approximation
m = MeshQuad()
m.refine()
e = ElementQuadP(5)

# create quadratic mesh for the local-to-global mapping
E = ElementQuad2()
mapping_basis = InteriorBasis(m, E)
M = MeshQuad.from_basis(mapping_basis)

# deform the quadratic mesh
f1 = M.facets_satisfying(lambda x: (np.abs(x[0] - .5) == .5) * (x[1] > .5))
f2 = M.facets_satisfying(lambda x: (np.abs(x[0] - .5) == .5) * (x[1] < .5))
f3 = M.facets_satisfying(lambda x: (np.abs(x[1] - .5) == .5) * (x[0] > .5))
f4 = M.facets_satisfying(lambda x: (np.abs(x[1] - .5) == .5) * (x[0] < .5))
M.p[0, mapping_basis.facet_dofs[:, f1]] += 0.1
M.p[0, mapping_basis.facet_dofs[:, f2]] -= 0.1
M.p[1, mapping_basis.facet_dofs[:, f3]] -= 0.1
M.p[1, mapping_basis.facet_dofs[:, f4]] += 0.1

# create mapping for the finite element approximation and assemble
mapping = MappingIsoparametric(M, E)
basis = InteriorBasis(m, e, mapping)

A = asm(laplace, basis)
M = asm(mass, basis)

L, x = solve(*condense(A, M, D=basis.find_dofs()), k=8)

if __name__ == '__main__':

    from os.path import splitext
    from sys import argv
    name = splitext(argv[0])[0]

    from skfem.visuals.matplotlib import *
    ax = draw(m)
    plot(basis, x[:, 6], Nrefs=6, ax=ax)
    savefig(f'{name}_eigenmode.png')


import sqlite3
import os.path

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "movies.db")
with sqlite3.connect(db_path) as db:

    cur=db.cursor()

id1 = input("1. kategori")
id2 = input("2. kategori")
id3 = input("3. kategori")
id4 = input("4. kategori")

print("******")
film2= cur.execute('SELECT movie_id from dbkategori WHERE category_id in (?,?,?,?) GROUP by movie_id HAVING COUNT(movie_id)  >=  4',
(id1,id2,id3,id4))

film = cur.fetchall()
list = []

for i in film:
    list.append(i)
    print(i)

"""print(list[0][0])"""
print(len(film))

print("-----")