from rest_framework import serializers, status
from metrics_api.models import Metric
from metrics_api.utils.constants.response import ResponseMessages
from metrics_api.utils.constants import common as Constant
from metrics_api.utils.response import CustomResponse

class SelectorSerializer(serializers.Serializer):
    """
    Validate selectors in a query
    """
    field = serializers.CharField(required=False, allow_blank=True)
    operation = serializers.CharField(required=False, allow_blank=True)

    class Meta:
        fields = ['field', 'operation']

    def validate_field(self, attr):
            if not hasattr(Metric, attr) and attr not in Constant.non_models_fields:
                raise CustomResponse(
                    message=ResponseMessages.INVALID_DATA.format(object=attr),
                    code=status.HTTP_400_BAD_REQUEST
                ).error()
            return attr

    def validate_operation(self, attr):
        attr = str(attr)
        if attr and attr.lower() not in Constant.AGGREGATOR_DICT.keys():
            raise CustomResponse(
                message=ResponseMessages.INVALID_DATA.format(object=attr),
                code=status.HTTP_400_BAD_REQUEST
            ).error()
        return attr
    def validate(self, attrs):
        _field = str(attrs.get('field'))
        _operation = str(attrs.get('operation'))

        if not hasattr(Metric, _field) and _field not in Constant.non_models_fields:
            raise CustomResponse(
                message=ResponseMessages.INVALID_DATA.format(object=_field),
                code=status.HTTP_400_BAD_REQUEST
            ).error()

        if _operation and _operation.lower() not in Constant.AGGREGATOR_DICT.keys():
            raise CustomResponse(
                message=ResponseMessages.INVALID_DATA.format(object=_operation),
                code=status.HTTP_400_BAD_REQUEST
            ).error()

        if _operation and _field in Constant.non_models_fields:
            if not Constant.AGGREGATOR_DICT.get(_operation):
                raise CustomResponse(
                    message=ResponseMessages.OPERATION_NOT_ALLOWED.format(object=_field),
                    code=status.HTTP_400_BAD_REQUEST
                ).error()

        return attrs

class ValidateRequestSerializer(serializers.Serializer):
    """
    Validate incoming data from request and show errors if data is not correct
    """
    selectors = SelectorSerializer(many=True, required=False)
    filters = serializers.DictField(required=False)
    sort_by = serializers.CharField(required=False, allow_blank=True)
    sort_order = serializers.CharField(required=False, allow_blank=True)
    group_by = serializers.ListField(required=False)

    class Meta:
        fields = ['selectors', 'filters', 'sort_by', 'sort_order', 'group_by']

    def validate(self, attrs):
        _filters = attrs.get('filters', {})
        _sorting = attrs.get('sort_order')
        _group_by = attrs.get('group_by', [])
        is_error = False
        error_field = ''
        for _field in _filters:
            if not (hasattr(Metric, _field) or _field in Constant.ALLOW_DATE_FORMATS):
                is_error = True
                error_field = _field
                break

        if _sorting and _sorting.lower() not in Constant.SORTING.keys():
            is_error = True
            error_field = _sorting

        for _field in _group_by:
            if not (_field in Constant.model_fields_dict.keys() or _field in Constant.non_models_fields):
                is_error = True
                error_field = _field

        if is_error:
            raise CustomResponse(
                message=ResponseMessages.INVALID_DATA.format(object=error_field),
                code=status.HTTP_400_BAD_REQUEST
            ).error()
        return attrs

class MetricSerializer(serializers.ModelSerializer):
    """
    Metric Serializer to perform object related actions
    """
    cpi = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Metric
        fields = ['id', 'spend', 'cpi', 'channel', 'installs', 'country', 'impressions', 'clicks', 'revenue']

    def to_representation(self, instance):
        _fields = self.context.get('_fields')
        data = {}
        for item, value in instance.items():
            if Constant.model_fields_dict[item] in _fields:
                data.update({Constant.model_fields_dict[item]: value})
        return data


# https://binarysearch.com/problems/Unique-People-in-Contact-List
class Solution:
    def solve(self, contacts):
        emails = set()
        unique = True
        res = 0

        for contact in contacts:
            unique = True
            for email in contact:
                if email in emails:
                    unique = False
                else:
                    emails.add(email)
            if unique:
                res += 1

        return res


#KnearestneighboursAlgorith
#knearest neighbour depends on te Euclidean Distance it is the distance between two data points
'''
Formula Eucliden distance squre root of [sigma n where i=1 * (Qi - pi)**2]

example if q =(1,2) and p =(3,5)
q = co ordinates for data point
p = quardinates in 2 dymination the points of x and y

squre root of (1-3)+(2-5)
Euclidean_diatance = sqrt((plot1[0] -plot2[0])**2+(plot1[1]-plot2[1])**2)
'''
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
import warnings
from collections import Counter
from matplotlib import style
style.use('fivethirtyeight')

dataset = {'k':[[1,2],[2,3],[3,1]],'r':[[6,4],[7,7],[8,6]]}
new_features = [6,3]

def k_nearest_neighbour(data,predict,k=3):
	if len(data) >= k:
		warnings.warn('k is less than value less than total voting group')

	distance = []
	for group in data:
		for features in data[group]:
			# Euclidean_diatance = np.sqrt(np.sum((np.array(features))**2 - (np.array(predict))**2))
			Euclidean_diatance = np.linalg.norm(np.array(features)- np.array(predict)) # faster than  the above line
			distance.append([Euclidean_diatance,group])

	votes = [i[1] for i in sorted(distance)[:k]]
	vote_result = Counter(votes).most_common(1)[0][0]
	return vote_result

results = k_nearest_neighbour(dataset,new_features)
print(results)

for i in dataset:
	for ii in dataset[i]:
		plt.scatter(ii[0],ii[1],s=100,color=i)

plt.scatter(new_features[0],new_features[1],s=100,color =results)
plt.show()


# -*- coding: utf-8 -*-
# Created on 2010-6-8
# @author: Yefe
# $Id$
from datetime import datetime
from django import forms
from django.db.models import F
from website.apps.book.models import Book, Category, Chapter

class BookForm(forms.ModelForm):
    category = forms.ModelChoiceField(queryset=Category.objects.select_related('subarea').order_by('subarea'),
                                      cache_choices=True, label=u'分类')

    class Meta:
        model = Book
        fields = ("category", 'title', 'author_name', 'series', 'part', 'is_complete')

    def save(self):
        self.instance.subarea = self.instance.category.subarea
        return super(BookForm, self).save()

class ChpaterForm(forms.ModelForm):
    content = forms.CharField(label=u'内容', max_length=1024*512, widget=forms.Textarea)

    class Meta:
        model = Chapter
        fields = ("vol_number", 'vol_name', 'title')

    def __init__(self, *args, **kwargs):
        if kwargs.has_key('instance'):
            self.base_fields['content'].initial = kwargs['instance'].content
        else:
            self.base_fields['content'].initial = ''
        super(ChpaterForm, self).__init__(*args, **kwargs)

    def save(self):
        o = super(ChpaterForm, self).save()
        org_len = len(o.content)
        o.set_content(self.cleaned_data['content'])
        o.created_at = datetime.now()
        diff_length = o.length - org_len
        Book.objects.filter(pk=o.book.pk).update(length=F('length')+diff_length, updated_at=o.created_at)
        return o.save()


#quick note: all ciphers below do follow the rules of their origin,
#That being said, they cannot be decrypted with anything other than this program
#What makes these special is it contains charecters, and both capital and lower case
#so any substitution cipher would inherently be using a different alphabet than this one

import random, string, math
#math.ceil(4.2) -> 5
def randomAlphabet(incomingList):#Creates a randomized alphabet any time I need
    for i in range(26):
        tempLocation = random.randint(0,25)
        tempLetter = incomingList[tempLocation]
        incomingList[tempLocation] = incomingList[i]
        incomingList[i] = tempLetter
    return incomingList

def handleMessage(message):
    alphabet = getAlphabet()
    for i in range(len(message)):
        if (message[i] not in (alphabet)):
            message = message.replace(message[i], " ")
    return message

def getAlphabet():
    letterAlphabet = list(string.ascii_uppercase)#Creates alphabet
    LetterAlphabet = list(string.ascii_lowercase)
    additionalCharecters = ['!','"'," ",',','#','.', "'"]
    for i in range(0,10):
        additionalCharecters.append(str(i))
    alphabet = [*LetterAlphabet, *letterAlphabet, *additionalCharecters]
    return alphabet

def getAtBashAlphabet():
    alphabet = getAlphabet()
    convertedAlphabet = []
    for i in range(len(alphabet)):
        convertedAlphabet.append(alphabet[-i-1])
    return convertedAlphabet
def getCaesarAlphabet(shifts):
    alphabet = getAlphabet()
    if shifts > 0:
        newAlphabet = alphabet
        for i in range(shifts):
            movingLetter = newAlphabet[0]
            newAlphabet = newAlphabet[1:]
            newAlphabet.append(movingLetter)
    return newAlphabet

def caesarEncrypt(message, shifts):
    convertedAlphabet = getCaesarAlphabet(shifts)
    alphabet = getAlphabet()
    newText = ''
    for i in range(len(message)):
        newText += convertedAlphabet[alphabet.index(message[i])]
    return newText

def caesarDecrypt(message, shifts):#alphabet is organized, string is what you want decrypted, shifts is rotations on caeser cipher used
    convertedAlphabet = getCaesarAlphabet(shifts)
    alphabet = getAlphabet()
    newText = ''
    for i in range(len(message)):
        newText += alphabet[convertedAlphabet.index(message[i])]
    return newText

def atBashEncrypt(message):
    alphabet = getAlphabet()
    convertedAlphabet = getAtBashAlphabet()
    newText = ''
    for i in range(len(message)):
        newText += alphabet[convertedAlphabet.index(message[i])]
    return newText

def atBashDecrypt(message):
    alphabet = getAlphabet()
    convertedAlphabet = getAtBashAlphabet()
    newText = ""
    for i in range(len(message)):
        newText += convertedAlphabet[alphabet.index(message[i])]
    return newText

def caesarAtBashEncrypt(plainText):
    message = handleMessage(plainText)
    encryptedMessage = caesarEncrypt(atBashEncrypt(caesarEncrypt(message, len(message))), (len(message)*2))
    return encryptedMessage

def caesarAtBashDecrypt(encryptedText):
    message = caesarDecrypt(atBashDecrypt(caesarDecrypt(encryptedText, (len(encryptedText)*2))), len(encryptedText))
    return message

def buildBox(message, key):
    box = [[]]
    counter = 0
    for i in range(len(key)):
        box[0].append([])
    for i in range(len(message)):
        column = (i % len(key))
        #print("" + str(counter) + "  " + str(column) + " " + str(message[i]))
        box[counter][column].append(message[i])
        if (column == (len(key)-1)):
            counter += 1
            box.append([])
            for j in range(len(key)):
                box[counter].append([])
    x = len(key) - ((len(message)%len(key)))
    for i in range(x):
        box[counter][len(key)-i-1].append(' ')
    return box

def destroyBox(box, key):#At some point, go back and delete the extra spaces from the message
    message = ''
    for i in range(len(box)):
        for j in box[i]:
            message += str(j)
    weirdMessage = message
    message = ''
    message += weirdMessage[2]
    counter = 0
    for i in range(len(weirdMessage)):
        if i > 1:
            if counter == 5:
                message += weirdMessage[i]
                counter = 0
            counter +=1
    return message

def printBox(box):
    for i in range(len(box)):
        print(box[i])

def columnaryTransferEncrypt(message, key):#Shifts everything horizontally
    box = buildBox(message, key)
    for i in range(len(key)):#Repeats a set number of times
         for j in range(len(box)):#loops through lists of lists
             print(box[j][0])
             #print(box[j][len(box[j]-1)])
             #print("hi")
             #box[j].insert(movingLetter, 0)
    box = destroyBox(box,key)
    return box
def mainMethod():
    plainText = "I came, I saw, I conquered!"
    '''x = buildBox(plainText, "Lemon")
    printBox(x)
    y = destroyBox(x, "lemon")
    print(y)'''
    print(columnaryTransferEncrypt(plainText, "Lemom"))

mainMethod()


# Para poder utilizar Selenium
from selenium import webdriver
from selenium.webdriver.common.by import By

# Para poder crear test unitarios
import unittest

class TestCaseTemperaturaBarcelona(unittest.TestCase):

    def test_titulo(self):
        # Si no estuviera el fichero chromedriver.exe en la carpeta en la que se encuentra este proyecto,
        # debería colocarse la ruta dentro de los paréntesis de la línea inferior
        driver = webdriver.Chrome()

        # Abrimos un navegador con la url indicada dentro de los paréntesis de la línea inferior
        driver.get('https://weather.com/es-ES/tiempo/hoy/l/0ce8d487db0a25631ee2017ddbe068bfba0de2b24de23f0b82c95863724f3a86')

        # Buscamos el elemento de la página que tiene la temperatura de la ciudad
        temperatura = driver.find_element(By.CSS_SELECTOR, '#WxuSavedLocations-header-9aea3e61-fbf8-4da4-9e07-f96abf18cdf1 > div > div > div > div.styles--cards--2hNpO.styles--cardCarousel--3xhQl > div > div > div > a.styles--weatherData--5tCnE.Button--default--3zkvy > span')

        # Comprobamos que la temperatura está entre 0 y 100 grados
        self.assertGreaterEqual(int(temperatura.text[0:-1]), 0)
        self.assertLessEqual(int(temperatura.text[0:-1]), 100)

        # Cerramos la ventana del navegador
        driver.quit()

# Para lanzar el test unitario
if __name__ == '__main__':
    unittest.main()


import pandas as pd
import numpy as np
import os
import xarray as xr

# Compare inexact matches of WP and NODC data from UBC 1951-1954

wp_dir = 'C:\\Users\\HourstonH\\Documents\\charles\\' \
         'ubc_historical_cruises\\ios_wp_ubc_1951_1954\\'
wp_file = os.path.join(wp_dir, 'wp_ubc_1951_1954_data.csv')

nodc_dir = 'C:\\Users\\HourstonH\\Documents\\charles\\' \
           'ubc_historical_cruises\\'
nodc_file = os.path.join(nodc_dir, 'ocldb1660928666.22434_OSD.nc')

# Comparison file
comp_file = os.path.join(nodc_dir, 'nodc_wp_comparison.csv')

wp_df = pd.read_csv(wp_file)
nodc_ds = xr.open_dataset(nodc_file)
comp_df = pd.read_csv(comp_file)

# Get nodc profile start and end indices
# Depth (z) increases with time (assume only downcasts)
nodc_start_idx = np.concatenate(
    (np.array([0]), np.where(np.diff(nodc_ds.z.data) < 0)[0] + 1))

nodc_end_idx = np.concatenate(
    (nodc_start_idx[1:], np.array([len(nodc_ds.z.data)])))

wp_start_idx = np.unique(
    wp_df.loc[:, 'Profile number'], return_index=True)[1]

# Pandas indexing is inclusive of end!
wp_end_idx = np.concatenate((wp_start_idx[1:]-1, np.array([len(wp_df)])))

print(wp_df.loc[wp_start_idx[0]:wp_end_idx[0], 'Depth'])

comp_df['Confirmed match'] = np.repeat('N', len(comp_df))

for i in range(len(comp_df)): #len(comp_df)
    nodc_idx = comp_df.loc[i, 'Indices from NODC']
    # Find WP index based on the source .UBC file matching
    wp_idx = np.where(
        wp_df.loc[wp_start_idx, 'Source file name'] ==
        os.path.basename(comp_df.loc[i, 'WP url']))[0]
    print('NODC index', nodc_idx)
    print('WP index', wp_idx)
    # wp_idx = comp_df.loc[i, 'Indices from WP']
    # print(comp_df.loc[i, 'WP url'])
    # print(wp_df.loc[wp_start_idx[wp_idx], 'Source file name'])
    # Locate the data in the dataset or dataframe
    nodc_z = nodc_ds.z.data[
             nodc_start_idx[nodc_idx]:nodc_end_idx[nodc_idx]]
    wp_z = wp_df.loc[
           wp_start_idx[wp_idx[0]]:wp_end_idx[wp_idx[0]], 'Depth'].to_numpy()
    # print(nodc_ds.time.data[nodc_idx], nodc_z,
    #       nodc_ds.Temperature.data[nodc_start_idx[nodc_idx]:nodc_end_idx[nodc_idx]],
    #       wp_df.loc[wp_start_idx[wp_idx[0]], 'Time'],
    #       wp_z,
    #       wp_df.loc[wp_start_idx[wp_idx[0]]:wp_end_idx[wp_idx[0]], 'Temperature [C]'].to_numpy(), sep='\n')
    # # If it's a match, then the TSO values would be the same
    # BUT not necessarily the depth and time values

    # Try TSO separately
    nodc_T = nodc_ds.Temperature.data[
             nodc_start_idx[nodc_idx]:nodc_end_idx[nodc_idx]].astype('float32')
    wp_T = wp_df.loc[
           wp_start_idx[wp_idx[0]]:wp_end_idx[wp_idx[0]], 'Temperature [C]'
           ].to_numpy().astype('float32')
    # nodc_T = nodc_ds.Salinity.data[
    #          nodc_start_idx[nodc_idx]:nodc_end_idx[nodc_idx]].astype('float32')
    # wp_T = wp_df.loc[
    #        wp_start_idx[wp_idx[0]]:wp_end_idx[wp_idx[0]], 'Salinity [PSU]'
    #        ].to_numpy().astype('float32')
    # TODO nodc oxygen in umol/kg, would have to convert
    # nodc_T = nodc_ds.Oxygen.data[
    #          nodc_start_idx[nodc_idx]:nodc_end_idx[nodc_idx]].astype('float32')
    # wp_T = wp_df.loc[
    #        wp_start_idx[wp_idx[0]]:wp_end_idx[wp_idx[0]], 'Oxygen [mL/L]'
    #        ].to_numpy().astype('float32')
    # TODO use np.allclose() with a tolerance? or np.equal()
    # What absolute and relative tolerances to use?
    if len(wp_T) == len(nodc_T):
        if np.allclose(wp_T, nodc_T, rtol=1e-03, atol=1e-03, equal_nan=True):
            comp_df.loc[i, 'Confirmed match'] = 'Y'
    else:
        # Check if all elements in smaller array are in the bigger array
        bigger = wp_T if len(wp_T) > len(nodc_T) else nodc_T
        smaller = wp_T if len(wp_T) < len(nodc_T) else nodc_T
        if all([a in bigger for a in smaller]):
            comp_df.loc[i, 'Confirmed match'] = 'Y'

print(len(comp_df))
print(comp_df.loc[:, 'Confirmed match'])
print(sum(comp_df.loc[:, 'Confirmed match'] == 'Y'))

msk = comp_df.loc[:, 'Confirmed match'] == 'Y'

comp_df.to_csv(comp_file.replace('.csv', '_check_profiles.csv'), index=False)


from symbolic.args import symbolic, concrete
@symbolic(s="abcdefg")
def canWin( s):
    if s is None or len(s) < 2:
        return False
    list_s = list(s)
    return canWin_helper(list_s)
def canWin_helper( s):
    for i in range(len(s) - 1):
        if s[i] == '+' and s[i + 1] == '+':
            s[i] = '-'
            s[i + 1] = '-'
            res = canWin_helper(s)
            s[i] = '+'
            s[i + 1] = '+'
            if not res:
                return True
    return False


import logging

import cupy as cp
import numpy.typing as npt

import tike.communicators
import tike.linalg
import tike.operators
import tike.opt
import tike.ptycho.object
import tike.ptycho.position
import tike.ptycho.probe
import tike.ptycho.exitwave
import tike.precision
import tike.random

from .options import *
from .lstsq import _momentum_checked

logger = logging.getLogger(__name__)

def rpie(
    op: tike.operators.Ptycho,
    comm: tike.communicators.Comm,
    data: typing.List[npt.NDArray],
    batches: typing.List[typing.List[npt.NDArray[cp.intc]]],
    *,
    parameters: PtychoParameters,
) -> PtychoParameters:
    """Solve the ptychography problem using regularized ptychographical engine.

    The rPIE update direction can be shown to be equivalent to a conventional
    gradient descent direction but rescaled by the preconditioner term. i.e. If
    the rPIE step size (alpha) is 0 and the preconditioner is zero, we have the
    vanilla gradient descent direction.

    Parameters
    ----------
    op : :py:class:`tike.operators.Ptycho`
        A ptychography operator.
    comm : :py:class:`tike.communicators.Comm`
        An object which manages communications between GPUs and nodes.
    data : list((FRAME, WIDE, HIGH) float32, ...)
        A list of unique CuPy arrays for each device containing
        the intensity (square of the absolute value) of the propagated
        wavefront; i.e. what the detector records. FFT-shifted so the
        diffraction peak is at the corners.
    batches : list(list((BATCH_SIZE, ) int, ...), ...)
        A list of list of indices along the FRAME axis of `data` for
        each device which define the batches of `data` to process
        simultaneously.
    parameters : :py:class:`tike.ptycho.solvers.PtychoParameters`
        An object which contains reconstruction parameters.

    Returns
    -------
    result : :py:class:`tike.ptycho.solvers.PtychoParameters`
        An object which contains the updated reconstruction parameters.

    References
    ----------
    Maiden, Andrew M., and John M. Rodenburg. 2009. “An Improved
    Ptychographical Phase Retrieval Algorithm for Diffractive Imaging.”
    Ultramicroscopy 109 (10): 1256–62.
    https://doi.org/10.1016/j.ultramic.2009.05.012.

    Andrew Maiden, Daniel Johnson, and Peng Li, "Further improvements to the
    ptychographical iterative engine," Optica 4, 736-745 (2017)
    https://doi.org/10.1364/OPTICA.4.000736

    .. seealso:: :py:mod:`tike.ptycho`

    """
    probe = parameters.probe
    scan = parameters.scan
    psi = parameters.psi
    algorithm_options = parameters.algorithm_options
    exitwave_options = parameters.exitwave_options
    probe_options = parameters.probe_options
    position_options = parameters.position_options
    object_options = parameters.object_options
    eigen_probe = parameters.eigen_probe
    eigen_weights = parameters.eigen_weights

    if eigen_probe is None:
        beigen_probe = [None] * comm.pool.num_workers
    else:
        beigen_probe = eigen_probe

    if eigen_weights is None:
        beigen_weights = [None] * comm.pool.num_workers
    else:
        beigen_weights = eigen_weights

    if parameters.algorithm_options.batch_method == 'compact':
        order = range
    else:
        order = tike.random.randomizer_np.permutation

    psi_update_numerator = [None] * comm.pool.num_workers
    probe_update_numerator = [None] * comm.pool.num_workers
    position_update_numerator = [None] * comm.pool.num_workers
    position_update_denominator = [None] * comm.pool.num_workers

    batch_cost: typing.List[float] = []
    for n in order(algorithm_options.num_batch):

        (
            cost,
            psi_update_numerator,
            probe_update_numerator,
            position_update_numerator,
            position_update_denominator,
            beigen_weights,
        ) = (list(a) for a in zip(*comm.pool.map(
            _get_nearplane_gradients,
            data,
            scan,
            psi,
            probe,
            exitwave_options.measured_pixels,
            psi_update_numerator,
            probe_update_numerator,
            position_update_numerator,
            position_update_denominator,
            beigen_probe,
            beigen_weights,
            batches,
            comm.streams,
            n=n,
            op=op,
            object_options=object_options,
            probe_options=probe_options,
            position_options=position_options,
            exitwave_options=exitwave_options,
        )))

        batch_cost.append(comm.Allreduce_mean(cost, axis=None).get())

        if algorithm_options.batch_method != 'compact':
            (
                psi,
                probe,
            ) = _update(
                comm,
                psi,
                probe,
                psi_update_numerator,
                probe_update_numerator,
                object_options,
                probe_options,
                algorithm_options,
            )
            psi_update_numerator = [None] * comm.pool.num_workers
            probe_update_numerator = [None] * comm.pool.num_workers

    algorithm_options.costs.append(batch_cost)

    if position_options is not None:
        (
            scan,
            position_options,
        ) = (list(a) for a in zip(*comm.pool.map(
            _update_position,
            scan,
            position_options,
            position_update_numerator,
            position_update_denominator,
            max_shift=probe[0].shape[-1] * 0.1,
            alpha=algorithm_options.alpha,
        )))

    if algorithm_options.batch_method == 'compact':
        (
            psi,
            probe,
        ) = _update(
            comm,
            psi,
            probe,
            psi_update_numerator,
            probe_update_numerator,
            object_options,
            probe_options,
            algorithm_options,
            errors=list(np.mean(x) for x in algorithm_options.costs[-3:]),
        )

    if eigen_weights is not None:
        eigen_weights = comm.pool.map(
            _normalize_eigen_weights,
            beigen_weights,
        )

    parameters.probe = probe
    parameters.psi = psi
    parameters.scan = scan
    parameters.algorithm_options = algorithm_options
    parameters.probe_options = probe_options
    parameters.object_options = object_options
    parameters.position_options = position_options
    parameters.eigen_weights = eigen_weights
    return parameters

def _normalize_eigen_weights(eigen_weights):
    return eigen_weights / tike.linalg.mnorm(
        eigen_weights,
        axis=(-3),
        keepdims=True,
    )

def _update(
    comm: tike.communicators.Comm,
    psi: npt.NDArray[cp.csingle],
    probe: npt.NDArray[cp.csingle],
    psi_update_numerator: npt.NDArray[cp.csingle],
    probe_update_numerator: npt.NDArray[cp.csingle],
    object_options: ObjectOptions,
    probe_options: ProbeOptions,
    algorithm_options: RpieOptions,
    errors: typing.Union[None, typing.List[float]] = None,
):
    if object_options:
        psi_update_numerator = comm.Allreduce_reduce_gpu(
            psi_update_numerator)[0]
        dpsi = psi_update_numerator
        deno = (
            (1 - algorithm_options.alpha) * object_options.preconditioner[0] +
            algorithm_options.alpha * object_options.preconditioner[0].max(
                axis=(-2, -1),
                keepdims=True,
            ))
        psi[0] = psi[0] + dpsi / deno
        if object_options.use_adaptive_moment:
            if errors:
                (
                    dpsi,
                    object_options.v,
                    object_options.m,
                ) = _momentum_checked(
                    g=dpsi,
                    v=object_options.v,
                    m=object_options.m,
                    mdecay=object_options.mdecay,
                    errors=errors,
                    memory_length=3,
                )
            else:
                (
                    dpsi,
                    object_options.v,
                    object_options.m,
                ) = tike.opt.adam(
                    g=dpsi,
                    v=object_options.v,
                    m=object_options.m,
                    vdecay=object_options.vdecay,
                    mdecay=object_options.mdecay,
                )
            psi[0] = psi[0] + dpsi / deno
        psi = comm.pool.bcast([psi[0]])

    if probe_options:
        probe_update_numerator = comm.Allreduce_reduce_gpu(
            probe_update_numerator)[0]
        b0 = tike.ptycho.probe.finite_probe_support(
            probe[0],
            p=probe_options.probe_support,
            radius=probe_options.probe_support_radius,
            degree=probe_options.probe_support_degree,
        )
        b1 = probe_options.additional_probe_penalty * cp.linspace(
            0, 1, probe[0].shape[-3], dtype='float32')[..., None, None]
        dprobe = (probe_update_numerator - (b1 + b0) * probe[0])
        deno = (
            (1 - algorithm_options.alpha) * probe_options.preconditioner[0] +
            algorithm_options.alpha * probe_options.preconditioner[0].max(
                axis=(-2, -1),
                keepdims=True,
            ) + b0 + b1)
        probe[0] = probe[0] + dprobe / deno
        if probe_options.use_adaptive_moment:
            # ptychoshelves only applies momentum to the main probe
            mode = 0
            if errors:
                (
                    dprobe[0, 0, mode, :, :],
                    probe_options.v,
                    probe_options.m,
                ) = _momentum_checked(
                    g=(dprobe)[0, 0, mode, :, :],
                    v=probe_options.v,
                    m=probe_options.m,
                    mdecay=probe_options.mdecay,
                    errors=errors,
                    memory_length=3,
                )
            else:
                (
                    dprobe[0, 0, mode, :, :],
                    probe_options.v,
                    probe_options.m,
                ) = tike.opt.adam(
                    g=(dprobe)[0, 0, mode, :, :],
                    v=probe_options.v,
                    m=probe_options.m,
                    vdecay=probe_options.vdecay,
                    mdecay=probe_options.mdecay,
                )
            probe[0] = probe[0] + dprobe / deno
        probe = comm.pool.bcast([probe[0]])

    return psi, probe

def _get_nearplane_gradients(
    data: npt.NDArray,
    scan: npt.NDArray,
    psi: npt.NDArray,
    probe: npt.NDArray,
    measured_pixels: npt.NDArray,
    psi_update_numerator: typing.Union[None, npt.NDArray],
    probe_update_numerator: typing.Union[None, npt.NDArray],
    position_update_numerator: typing.Union[None, npt.NDArray],
    position_update_denominator: typing.Union[None, npt.NDArray],
    eigen_probe: typing.Union[None, npt.NDArray],
    eigen_weights: typing.Union[None, npt.NDArray],
    batches: typing.List[typing.List[int]],
    streams: typing.List[cp.cuda.Stream],
    *,
    n: int,
    op: tike.operators.Ptycho,
    object_options: typing.Union[None, ObjectOptions] = None,
    probe_options: typing.Union[None, ProbeOptions] = None,
    position_options: typing.Union[None, PositionOptions],
    exitwave_options: ExitWaveOptions,
) -> typing.List[npt.NDArray]:

    cost = 0.0
    psi_update_numerator = cp.zeros_like(
        psi) if psi_update_numerator is None else psi_update_numerator
    probe_update_numerator = cp.zeros_like(
        probe) if probe_update_numerator is None else probe_update_numerator
    position_update_numerator = cp.empty_like(
        scan
    ) if position_update_numerator is None else position_update_numerator
    position_update_denominator = cp.empty_like(
        scan
    ) if position_update_denominator is None else position_update_denominator

    def keep_some_args_constant(
        ind_args,
        mod_args,
        indices,
    ):
        (data,) = ind_args
        (
            cost,
            psi_update_numerator,
            probe_update_numerator,
            position_update_numerator,
            position_update_denominator,
            eigen_weights,
            scan,
        ) = mod_args

        unique_probe = tike.ptycho.probe.get_varying_probe(
            probe,
            eigen_probe,
            eigen_weights[indices] if eigen_weights is not None else None,
        )

        farplane = op.fwd(probe=unique_probe, scan=scan[indices], psi=psi)
        intensity = cp.sum(
            cp.square(cp.abs(farplane)),
            axis=list(range(1, farplane.ndim - 2)),
        )
        each_cost = getattr(
            tike.operators,
            f'{exitwave_options.noise_model}_each_pattern',
        )(
            data[:, measured_pixels][:, None, :],
            intensity[:, measured_pixels][:, None, :],
        )
        cost += cp.sum(each_cost)

        if exitwave_options.noise_model == 'poisson':

            xi = (1 - data / intensity)[:, None, None, :, :]
            grad_cost = farplane * xi

            step_length = cp.full(
                shape=(farplane.shape[0], 1, farplane.shape[2], 1, 1),
                fill_value=exitwave_options.step_length_start,
            )

            if exitwave_options.step_length_usemodes == 'dominant_mode':

                step_length = tike.ptycho.exitwave.poisson_steplength_dominant_mode(
                    xi,
                    intensity,
                    data,
                    measured_pixels,
                    step_length,
                    exitwave_options.step_length_weight,
                )

            else:

                step_length = tike.ptycho.exitwave.poisson_steplength_all_modes(
                    xi,
                    cp.square(cp.abs(farplane)),
                    intensity,
                    data,
                    measured_pixels,
                    step_length,
                    exitwave_options.step_length_weight,
                )

            farplane[..., measured_pixels] = (-step_length *
                                              grad_cost)[..., measured_pixels]

        else:

            # Gaussian noise model for exitwave updates, steplength = 1
            # TODO: optimal step lengths using 2nd order taylor expansion

            farplane[..., measured_pixels] = -getattr(
                tike.operators, f'{exitwave_options.noise_model}_grad')(
                    data,
                    farplane,
                    intensity,
                )[..., measured_pixels]

        unmeasured_pixels = cp.logical_not(measured_pixels)
        farplane[..., unmeasured_pixels] *= (
            exitwave_options.unmeasured_pixels_scaling - 1.0)

        pad, end = op.diffraction.pad, op.diffraction.end
        diff = op.propagation.adj(farplane, overwrite=True)[..., pad:end,
                                                            pad:end]

        if object_options:
            grad_psi = (cp.conj(unique_probe) * diff / probe.shape[-3]).reshape(
                scan[indices].shape[0] * probe.shape[-3], *probe.shape[-2:])
            psi_update_numerator = op.diffraction.patch.adj(
                patches=grad_psi,
                images=psi_update_numerator,
                positions=scan[indices],
                nrepeat=probe.shape[-3],
            )

        if position_options or probe_options:

            patches = op.diffraction.patch.fwd(
                patches=cp.zeros_like(diff[..., 0, 0, :, :]),
                images=psi,
                positions=scan[indices],
            )[..., None, None, :, :]

        if probe_options:
            probe_update_numerator += cp.sum(
                cp.conj(patches) * diff,
                axis=-5,
                keepdims=True,
            )
            if eigen_weights is not None:
                m: int = 0
                OP = patches * probe[..., m:m + 1, :, :]
                eigen_numerator = cp.sum(
                    cp.real(cp.conj(OP) * diff[..., m:m + 1, :, :]),
                    axis=(-1, -2),
                )
                eigen_denominator = cp.sum(
                    cp.abs(OP)**2,
                    axis=(-1, -2),
                )
                eigen_weights[indices, ..., 0:1, m:m+1] += (
                    0.1 * (eigen_numerator / eigen_denominator)
                )  # yapf: disable

        if position_options:

            grad_x, grad_y = tike.ptycho.position.gaussian_gradient(patches)

            position_update_numerator[indices, ..., 0] = cp.sum(
                cp.real(cp.conj(grad_x * unique_probe) * diff),
                axis=(-4, -3, -2, -1),
            )
            position_update_denominator[indices, ..., 0] = cp.sum(
                cp.abs(grad_x * unique_probe)**2,
                axis=(-4, -3, -2, -1),
            )
            position_update_numerator[indices, ..., 1] = cp.sum(
                cp.real(cp.conj(grad_y * unique_probe) * diff),
                axis=(-4, -3, -2, -1),
            )
            position_update_denominator[indices, ..., 1] = cp.sum(
                cp.abs(grad_y * unique_probe)**2,
                axis=(-4, -3, -2, -1),
            )

        return (
            cost,
            psi_update_numerator,
            probe_update_numerator,
            position_update_numerator,
            position_update_denominator,
            eigen_weights,
            scan,
        )

    (
        cost,
        psi_update_numerator,
        probe_update_numerator,
        position_update_numerator,
        position_update_denominator,
        eigen_weights,
        scan,
    ) = tike.communicators.stream.stream_and_modify(
        f=keep_some_args_constant,
        ind_args=[
            data,
        ],
        mod_args=[
            cost,
            psi_update_numerator,
            probe_update_numerator,
            position_update_numerator,
            position_update_denominator,
            eigen_weights,
            scan,
        ],
        streams=streams,
        indices=batches[n],
    )

    return (
        cost / len(batches[n]),
        psi_update_numerator,
        probe_update_numerator,
        position_update_numerator,
        position_update_denominator,
        eigen_weights,
    )

def _update_position(
    scan: npt.NDArray,
    position_options: PositionOptions,
    position_update_numerator: npt.NDArray,
    position_update_denominator: npt.NDArray,
    *,
    alpha=0.05,
    max_shift=1,
):
    step = (position_update_numerator) / (
        (1 - alpha) * position_update_denominator +
        alpha * max(position_update_denominator.max(), 1e-6))

    if position_options.use_adaptive_moment:
        (
            step,
            position_options.v,
            position_options.m,
        ) = tike.opt.adam(
            step,
            position_options.v,
            position_options.m,
            vdecay=position_options.vdecay,
            mdecay=position_options.mdecay,
        )

    scan -= step

    return scan, position_options


import math
import cv2
import numpy as np
from scipy import ndimage
import os
import matplotlib.pyplot as plt
import random

def __rotate_img(image, page_no):
    edges = cv2.Canny(image, 400, 500, apertureSize=3)
    lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
    main_lines = []

    display_img = image.copy()
    display_img = cv2.line(display_img, (0, 300), (image.shape[0], 300), (0, 0, 255), 2)
    display_img = cv2.line(display_img, (0, 1800), (image.shape[0], 1800), (0, 0, 255), 2)

    median_angle = 0
    angles = []
    if lines is not None:
        for i in range(0, len(lines)):
            for rho, theta in lines[i]:
                a = np.cos(theta)
                b = np.sin(theta)
                x0 = a * rho
                y0 = b * rho
                x1 = int(x0 + 1000 * (-b))
                y1 = int(y0 + 1000 * a)
                x2 = int(x0 - 1000 * (-b))
                y2 = int(y0 - 1000 * a)
                angle = math.degrees(math.atan2(y2 - y1, x2 - x1))
                if -70 > angle > -90:
                    angle = 90 + angle
                elif 70 < angle < 90:
                    angle = angle - 90
                if 1800 > y1 > 300 and 1800 > y2 > 300 and 0 < abs(angle) < 35:
                    angles.append(angle)
                    display_img = cv2.line(display_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                    main_lines.append((x1, y1, x2, y2, angle))

                    if len(angles) >= 50:
                        break

            # if i % 100 == 0:
            #     cv2.namedWindow('image', cv2.WINDOW_NORMAL)  # Create window with freedom of dimensions
            #     cv2.resizeWindow('image', 750, 900)
            #     cv2.imshow("image", display_img)  # Show image
            #     cv2.waitKey(0)
            #     cv2.destroyAllWindows()
        mean = (np.mean(angles)) if len(angles) > 0 else 0
        std = np.std(angles) if len(angles) > 0 else 0

        # print(std, mean)
        # if std > mean:
        #     std = std / 2

        # import matplotlib.pyplot as plt
        # import seaborn as sns
        # sns.distplot(angles, hist=True, kde=True,
        #              bins=int(180 / 5), color='darkblue',
        #              hist_kws={'edgecolor': 'darkblue'},
        #              kde_kws={'linewidth': 1})
        # sns.distplot([mean - std, mean + std], hist=True, kde=True,
        #              bins=int(180 / 5), color='green',
        #              hist_kws={'edgecolor': 'green'},
        #              kde_kws={'linewidth': 1})
        # plt.show()

        if len(angles) > 0:
            min_angle, max_angle = mean - std, mean + std
            considerable_lines = [line for line in main_lines if max_angle >= line[4] >= min_angle]
            # print(len([line[4] for line in considerable_lines]))
            # print(len([line[4] for line in considerable_lines if line[4] >= 0]))
            # print(len([line[4] for line in considerable_lines if line[4] <= 0]))
            median_angle = (np.mean([line[4] for line in considerable_lines])) if len(considerable_lines) > 0 else 0
        # print(median_angle)
        if median_angle < -45:
            median_angle = 90 + median_angle
        print(f"Angle of rotation for page {page_no} is {round(median_angle, 2)}\N{DEGREE SIGN}.")
        img_rotated = ndimage.rotate(image, median_angle)
    else:
        img_rotated = image.copy()
    return img_rotated, median_angle

def find_files_recursively(dir_path):
    files_to_process = list()
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if not root.endswith("_ocr"):
                files_to_process.append(os.path.join(root, file))
    return files_to_process

path = r"Reports\extracted_png"
images = find_files_recursively(path)
print(f"Found {len(images)} images")
random.shuffle(images)
for img in images:
    print(img)

    img_obj = cv2.imread(img)
    rot_img_obj, angle = __rotate_img(img_obj, 0)

    # cv2.imshow('Original', img_obj)
    # cv2.imshow('Rotated', rot_img_obj)
    #
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    if abs(angle) >= 1:
        f, axarr = plt.subplots(1, 2)
        axarr[0].imshow(img_obj)
        axarr[0].grid()
        axarr[1].imshow(rot_img_obj)
        axarr[1].grid()

        mng = plt.get_current_fig_manager()
        mng.window.state('zoomed')
        plt.show()


import json
import pandas as pd
import pika
import requests
from bs4 import BeautifulSoup
from s3_utils import *

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))

channel = connection.channel()
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
result = channel.queue_declare('', exclusive=False, durable=True)
queue_name = result.method.queue

print("queue name----", queue_name)
binding_key = "example_scraper"

channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key)

def sample_scraper(url):
    # Set the URL of the website you want to scrape

    # Send an HTTP GET request to the website and retrieve the HTML page
    response = requests.get(url)

    # Parse the HTML page
    soup = BeautifulSoup(response.text, "html.parser")

    # Find all the elements with the specified class
    elements = soup.find("h1")
    return elements

def on_request(ch, method, props, body):
    print("[x] received task message...")
    task_details = json.loads(body)
    context = task_details["context"]
    try:
        response = sample_scraper(context["url"])
        if isinstance(response, pd.core.frame.DataFrame):
            response_msg = response.to_csv()
        else:
            response_msg = response
            print(response_msg)
            with open("sample_scraper.txt", "wb") as f:
                f.write(str(response_msg.text))
            s3_link = upload_result("sample_scraper.txt")
        ch.basic_publish(exchange="",
                         routing_key=props.reply_to,
                         properties=pika.BasicProperties(correlation_id=props.correlation_id,delivery_mode=2),
                         body=str(s3_link))
        ch.basic_ack(delivery_tag=method.delivery_tag)
        print("[x] sent the response to the client..")
    except Exception as e:
        raise e
channel.basic_qos(prefetch_count=2)
channel.basic_consume(queue=queue_name, on_message_callback=on_request)

print(" [x] Awaiting RPC requests")

channel.start_consuming()


import numpy as np
from Kinematics_Delta import PDelta

def rotate(origin, point, angle):
    """
    Rotate a point counterclockwise by a given angle around a given origin.

    The angle should be given in degrees.
    """
    ox, oy = origin
    px, py = point
    angle = np.radians(angle)

    qx = ox + np.cos(angle) * (px - ox) - np.sin(angle) * (py - oy)
    qy = oy + np.sin(angle) * (px - ox) + np.cos(angle) * (py - oy)
    return [qx, qy]

class DeltaHand():
    def __init__(self, num_finger=4, center_len=15, ee_len=6, base_len=15, leg_len=40):
        self.delta_centers = []
        self.delta_fingers = []

        self.num_finger = num_finger
        self.center_len = center_len
        self.ee_len = ee_len
        self.base_len = base_len
        self.leg_len = leg_len

        # starting from top -> left -> bottom -> right
        # all deltas facing downwards
        # delta in order bottom -> top right -> top left

        self.init_center = [0, 1*(self.center_len + self.base_len)]
        self.angles = np.linspace(0, -360, self.num_finger, endpoint=False) + 135
        self.init_centers = []
        for i in range(self.num_finger):
            finger = PDelta(ee_len=self.ee_len, base_len=self.base_len, leg_len=self.leg_len)
            center = rotate([0,0], self.init_center, self.angles[i])
            self.delta_centers.append(center)
            self.delta_fingers.append(finger)
            self.init_centers.append([finger.ix, finger.iy, finger.iz])

        # make it positive!!
        self.hand_init_center = np.mean(np.array(self.init_centers),axis=0)
        # self.hand_init_center[2] *= -1.0

    def go_to_FK(self, distances):
        # distances are actuation distances, and it has to be in shape (num_finger x 3)
        ee_positions = []
        for i in range(self.num_finger):
            x, y, z = self.delta_fingers[i].go_to_distance(distances[i][0],distances[i][1], distances[i][2])
            # rotate to the world coordinate
            x_t = x + self.init_center[0]
            y_t = y + self.init_center[1]
            x_r, y_r = rotate([0,0], [x_t, y_t], self.angles[i])
            ee_positions.append([x_r,y_r,z])
        return ee_positions

    def go_to_IK(self, positions):
        # positions are ee's positions, and it has to be in the shape (num_finger x 3 [x y z])
        actuations = []
        err = False
        for i in range(self.num_finger):
            pos = positions[i]
            # rotate to delta's coordinate
            pos_d = rotate([0,0], [pos[0], pos[1]], -1*self.angles[i])
            pos_x = pos_d[0] - self.init_center[0]
            pos_y = pos_d[1] - self.init_center[1]
            #print("Go to: ", pos_x, pos_y, pos[2])
            a1, a2, a3 = self.delta_fingers[i].go_to(pos_x, pos_y, pos[2])
            #print("Actuation to: ", [a1, a2, a3])
            # if a1 >= 20.0 or a2 >= 20.0 or a3 >= 20.0 or a1 <= 0 or a2 <= 0 or a3 <= 0:
            if a1 <= 0 or a2 <= 0 or a3 <= 0:
                # print("Outrange in IK!")
                # pause = input("pause for err")
                err = True
            if a1 >= 20:
                a1 = 20.0
            if a2 >= 20:
                a2 = 20.0
            if a3 >= 20:
                a3 = 20.0
            actuations.append([a1, a2, a3])
        return actuations, err

if __name__ == "__main__":
    hand = DeltaHand()
    print(hand.delta_centers)
    # actuations = [[0,0,0], [20,20,20], [5,5,5], [10,10,10]]
    # ee_pos = hand.go_to_FK(actuations)
    # print(ee_pos)
    steps = 10
    middle_steps = 4
    middle_range = np.linspace(0, 20, middle_steps, endpoint=True)
    for motor1_act in middle_range:
        print(motor1_act)
        motor1_range = [motor1_act]
        motor2_range = np.linspace(0, 20, steps, endpoint=True)
        motor3_range = np.linspace(0, 20, steps, endpoint=True)
        x_solutions = [[],[],[],[]]
        y_solutions = [[],[],[],[]]
        z_solutions = [[],[],[],[]]
        errs = []

        for i, dst1 in enumerate(motor1_range):
            for j, dst2 in enumerate(motor2_range):
                for k, dst3 in enumerate(motor3_range):
                    # print("# Distance: ", dst1, dst2, dst3)
                    actuations = [[dst1, dst2, dst3], [dst1, dst2, dst3], [dst1, dst2, dst3], [dst1, dst2, dst3]]
                    ee_pos = hand.go_to_FK(actuations)
                    # print(ee_pos)
                    for e in range(hand.num_finger):
                        x_solutions[e].append(ee_pos[e][0])
                        y_solutions[e].append(ee_pos[e][1])
                        z_solutions[e].append(ee_pos[e][2])

        x_solutions = np.array(x_solutions) #* 0.001
        y_solutions = np.array(y_solutions) #* 0.001
        z_solutions = np.array(z_solutions) * -1 #* 0.001 * -1
        z_solutions -= np.min(z_solutions)

        print("Min/Max on x axis: ", np.min(x_solutions), np.max(x_solutions))
        print("Min/Max on y axis: ", np.min(y_solutions), np.max(y_solutions))
        print("Min/Max on z axis: ", np.min(z_solutions), np.max(z_solutions))


import tensorflow
import pickle
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.models import load_model
app = FastAPI()

with open('covid_token.pickle', 'rb') as handle:
  tokenizer = pickle.load(handle)

class Data(BaseModel):
    text : str

def loadModel():
    global predict_model

    predict_model = load_model('model.h5')

loadModel()

async def predict(data: Data):
    word_en=tokenizer.texts_to_sequences(data.text)
    X_new = pad_sequences(word_en, truncating='post',padding='post',maxlen =200)

    res = (predict_model.predict(X_new)>0.5).astype('int32')[:,0]
    function_to_np = lambda x : "Positive" if x == '0' else "Negative"
    output = function_to_np(res)
    return output

@app.post('/getclass/')
async def get_class(data: Data):
    category= await predict(data)
    res = {'sentiment': category}
    return {'results': res}

try:
    get_class()
except:
    print("can not post")


# given list of ints, return list of products of every int except the one at that index
# first, brute force solution-- O(n^2) time, O(n) space
def prod_all_brute(A):
    P = []
    for i in range(0, len(A)):
        # calculate product to pivot
        L = 1 # setup
        R = 1
        for x in range(0, i):
            L=L*A[x]
        # calculate product after pivot
        for y in range(i+1, len(A)):
            R=R*A[y]
        # append product of L, R products to P
        P.append(L*R)
    print(P)

#prod_all_brute([1,7,3,4])
#prod_all_brute([1,0,3,4])

# now do this in O(n) time, O(n) space
def prod_all_linear(A):
    P = [None] * len(A)

    accum = 1
    # go forwards (prod_all before index i )
    for i in range(0, len(A)):
        P[i] = accum #accum starts at 1 -- QUEUE UP
        accum *= A[i] #now for the next iteration, accum is the element before me

    accum = 1 #reset accum
    #go backwards (prod_all after index i )
    for i in range(len(A)-1, -1, -1):
        P[i] *= accum
        accum *= A[i]

    print(P)

prod_all_linear([1,7,3,4])


#!/usr/bin/env python

import os

from integrationtest import IntegrationTest, main
import unittest
import sys

class TestIntersect(IntegrationTest, unittest.TestCase):

    def test_intersect(self):
        data = {
            "--exact-allele --output-both": "expected-exact-allele-both.bed",
            "--exact-allele": "expected-exact-allele.bed",
            "--exact-pos --output-both": "expected-exact-pos-both.bed",
            "--exact-pos": "expected-exact-pos.bed",
            "--output-both": "expected-noargs-both.bed",
            "": "expected-noargs.bed",
        }

        for args, expected in data.items():
            output_file = self.tempFile("output.bed")

            params = [ "intersect", args, "-o", output_file ]
            params.extend(self.inputFiles("a.bed", "b.bed"))

            rv, err = self.execute(params)
            self.assertEqual(0, rv)
            self.assertEqual('', err)
            expected_file = self.inputFiles(expected)[0]
            self.assertFilesEqual(expected_file, output_file)

    def test_adjacent_insertions(self):
        output_file = self.tempFile("output.bed")
        params = [
            "intersect", "--full",
            "--adjacent-insertions",
            "-o", output_file
        ]
        params.extend(self.inputFiles(
            "adjacent-insertions-a.bed",
            "adjacent-insertions-b.bed"
        ))
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = self.inputFiles("expected-adjacent-insertions.bed")[0]
        self.assertFilesEqual(expected_file, output_file)

    def test_no_adjacent_insertions(self):
        output_file = self.tempFile("output.bed")
        params = [
            "intersect", "--full",
            "-o", output_file
        ]
        params.extend(self.inputFiles(
            "adjacent-insertions-a.bed",
            "adjacent-insertions-b.bed"
        ))
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = self.inputFiles("expected-no-adjacent-insertions.bed")[0]
        self.assertFilesEqual(expected_file, output_file)

    def test_exact_pos_regions(self):
        output_file = self.tempFile("output.bed")
        params = [
            "intersect",
            "--exact-pos",
            "-o", output_file
        ]
        a, b = self.inputFiles("a.bed", "a-regions.bed")
        params.extend([a, b])
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = a
        self.assertFilesEqual(expected_file, output_file)

    def test_partial_match(self):
        output_file = self.tempFile("output.bed")
        params = [
            "intersect",
            "--exact-allele --iub-match --output-both",
            "-o", output_file
        ]
        params.extend(self.inputFiles("iub-a.bed", "iub-b.bed"))
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = self.inputFiles("expected-iub-both.bed")[0]
        self.assertFilesEqual(expected_file, output_file)

    def test_position_only(self):
        output_file = self.tempFile("output.bed")
        params = [ "intersect", "-o", output_file ]
        params.extend(self.inputFiles("a.bed", "posonly.bed"))
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = self.inputFiles("a.bed")[0]
        self.assertFilesEqual(expected_file, output_file)

    def test_file_not_found(self):
        rv, err = self.execute(["intersect", "qwert", "djfsoidjfdj"])
        self.assertEqual(2, rv)
        self.assertEqual("Failed to open file qwert\n", err)

    def test_invalid_arguments(self):
        rv, err = self.execute(["the bear went over the mountain"])
        self.assertEqual(1, rv)
        self.assertTrue(err.startswith("Invalid subcommand 'the'"))

    def test_unsorted_data(self):
        params = ["intersect"]
        params.extend(self.inputFiles("a.bed", "sort/unsorted0.bed"))
        rv, err = self.execute(params)
        self.assertEqual(3, rv)
        self.assertTrue(err.startswith("Unsorted data found in stream"))

    def test_output_format_extra_fields(self):
        output_file = self.tempFile("output.bed")
        params = [
            "intersect",
            "--format 'I A3-4'",
            "-o", output_file
        ]
        params.extend(self.inputFiles("a.bed", "b.bed"))
        rv, err = self.execute(params)
        self.assertEqual(0, rv)
        self.assertEqual('', err)
        expected_file = self.inputFiles("expected-format-I,A3-4.bed")[0]
        self.assertFilesEqual(expected_file, output_file)

if __name__ == "__main__":
    main()


import random
import json

from src.helper.files import get_files, read_lines, write_file
from src.helper.pickle import dump_object
from src.data.wiki.benchmark_file_name_generator import BenchmarkFileNameGenerator
from src.data.preprocessing import preprocess_sequence
from src.data.wikipedia import Sequence, DatasetSplit
from src.settings import paths

FILE_ENCODING = "utf8"

def get_files_depth_two(directory):
    """
    Returns the paths to all files from the directories in the given directory.

    :param directory: a directory with directories containing the wanted files
    :return: list of full paths to all files at depth two
    """
    subdirs = get_files(directory)
    files = []
    for subdir in sorted(subdirs):
        path = directory + "/" + subdir + "/"
        subdir_files = get_files(path)
        for file in sorted(subdir_files):
            files.append(path + file)
    return files

def get_article_jsons(wiki_text_directory):
    """
    Reads all articles as jsons from an extracted Wikipedia dump.

    :param wiki_text_directory: Link to a directory created by the WikiExtractor script.
        Assumes subdirectories to contain files where each line corresponds to an article json.
    :return: iterator over article jsons
    """
    json_files = get_files_depth_two(wiki_text_directory)
    for file in json_files:
        lines = read_lines(file)
        for line in lines:
            article = json.loads(line)
            yield article

def split_article_ids(wiki_text_directory, n_split):
    """
    Runs through the extracted jsons, collects all article IDs and splits them into training, development and test sets.

    :param wiki_text_directory: Link to a directory created by the WikiExtractor script.
        Assumes subdirectories to contain files where each line corresponds to an article json.
    :param n_split: number of articles in the development and test sets
    :return: three lists containing the article IDs of the training, development and test sets
    """
    article_ids = [article["id"] for article in get_article_jsons(wiki_text_directory)]
    assert(len(article_ids) > 2 * n_split)
    random.shuffle(article_ids)
    development_ids = article_ids[:n_split]
    test_ids = article_ids[n_split:(2 * n_split)]
    training_ids = article_ids[(2 * n_split):]
    return training_ids, development_ids, test_ids

def split_dataset(wiki_text_directory, out_directory, n_split):
    """
    Reads all article IDs from an extracted wikipedia dump, splits them into training, development and test sets and
    pickles the three sets as lists.
    :param wiki_text_directory: Link to a directory created by the WikiExtractor script.
        Assumes subdirectories to contain files where each line corresponds to an article json.
    :param out_directory: directory where the ID lists are stored
    :param n_split: number of articles of the training and test sets
    """
    training_ids, development_ids, test_ids = split_article_ids(wiki_text_directory, n_split)
    training_ids_path = out_directory + "training_article_ids.pkl"
    development_ids_path = out_directory + "development_article_ids.pkl"
    test_ids_path = out_directory + "test_article_ids.pkl"
    dump_object(training_ids, training_ids_path)
    dump_object(development_ids, development_ids_path)
    dump_object(test_ids, test_ids_path)
    print("Split dataset into %i training articles, %i development articles, %i test articles." %
          (len(training_ids), len(development_ids), len(test_ids)))

def get_paragraphs(article_json):
    """
    Retrieve paragraphs from an article.
    Paragraphs get preprocessed and filtered,

    :param article_json: the article as json string
    :return: list of paragraphs as strings
    """
    text = article_json["text"]
    lines = text.split("\n")
    lines = [preprocess_sequence(line) for line in lines]
    lines = [line for line in lines if len(line) > 0]
    lines = [line for line in lines if line != "<onlyinclude></onlyinclude>"]
    return lines

def write_clean_articles(wiki_text_directory, out_directory, dev_ids, test_ids):
    """
    Reads the articles from an extracted wikipedia dump and writes each paragraph to a separate file,
    with the following structure:

    out_directory
    ---| training
    -------| correct
    -----------| texts
    ---------------| 0000
    -------------------| <sequence_id>_<article_id>_<title>_<paragraph>.txt
    -------------------| ... (max 5000 files)
    ---------------| ...
    ---| development
    ---| test

    :param wiki_text_directory: directory of the wikipedia dump, containing folders with files containing articles
        as jsons
    :param out_directory: path where the paragraph files will be stored
    :param dev_ids: set of article IDs for the development set
    :param test_ids: set of article IDs for the test set
    """
    articles = get_article_jsons(wiki_text_directory)
    file_generator = BenchmarkFileNameGenerator(out_directory, "correct", dev_ids, test_ids)
    file_generator.prepare_directories()
    for article in articles:
        paragraphs = get_paragraphs(article)
        files = file_generator.get_sequence_files(article, len(paragraphs))
        for paragraph, file in zip(paragraphs, files):
            write_file(file, paragraph)

def write_dataset_split_files(wiki_text_directory, dev_ids, test_ids):
    """
    Reads the articles from an extracted wikipedia dump and writes three files, each containing the paragraphs
    of one partition.
    Also dumps three lists containing the sequences as Sequence-objects.
    The output file names are defined in src.settings.paths.

    :param wiki_text_directory: directory of the wikipedia dump, containing folders with files containing articles
        as jsons
    :param dev_ids: set of article IDs for the development set
    :param test_ids: set of article IDs for the test set
    :return:
    """
    articles = get_article_jsons(wiki_text_directory)

    training_file = open(paths.WIKI_TRAINING_FILE, 'wb')
    development_file = open(paths.WIKI_DEVELOPMENT_FILE, 'wb')
    test_file = open(paths.WIKI_TEST_FILE, 'wb')

    training_sequences = []
    development_sequences = []
    test_sequences = []

    sequence_id = 0
    for article in articles:
        article_id = article["id"]
        paragraphs = get_paragraphs(article)
        for paragraph in paragraphs:
            char_len = len(paragraph)
            bytes = (paragraph + '\n').encode(FILE_ENCODING)
            if article_id in dev_ids:
                byte_offset = development_file.tell()
                development_file.write(bytes)
                byte_len = development_file.tell() - byte_offset
                sequence = Sequence(sequence_id, DatasetSplit.TRAINING, byte_offset, byte_len, char_len)
                development_sequences.append(sequence)
            elif article_id in test_ids:
                byte_offset = test_file.tell()
                test_file.write(bytes)
                byte_len = test_file.tell() - byte_offset
                sequence = Sequence(sequence_id, DatasetSplit.DEVELOPMENT, byte_offset, byte_len, char_len)
                test_sequences.append(sequence)
            else:
                byte_offset = training_file.tell()
                training_file.write(bytes)
                byte_len = training_file.tell() - byte_offset
                sequence = Sequence(sequence_id, DatasetSplit.TEST, byte_offset, byte_len, char_len)
                training_sequences.append(sequence)
            sequence_id += 1

    training_file.close()
    development_file.close()
    test_file.close()
    del articles

    dump_object(training_sequences, paths.WIKI_TRAINING_SEQUENCES)
    dump_object(development_sequences, paths.WIKI_DEVELOPMENT_SEQUENCES)
    dump_object(test_sequences, paths.WIKI_TEST_SEQUENCES)

def count_paragraphs(wiki_text_directory):
    """
    Counts and prints the number of paragraphs of the dataset and number of paragraphs of the article with the most
    paragraphs. The latter is needed to define the sequence file naming convention, i.e. number of digits for the
    paragraph ID.

    :param wiki_text_directory: directory of the wikipedia dump, containing folders with files containing articles
        as jsons
    """
    n_paragraphs = 0
    n_articles = 0
    max_paragraphs_per_article = 0
    for article in get_article_jsons(wiki_text_directory):
        a_paragraphs = len(get_paragraphs(article))
        n_paragraphs += a_paragraphs
        n_articles += 1
        if a_paragraphs > max_paragraphs_per_article:
            max_paragraphs_per_article = a_paragraphs
    print("%i paragraphs in %i articles. Longest article has %i paragraphs." %
          (n_paragraphs, n_articles, max_paragraphs_per_article))

def detect_tables(wiki_text_directory):
    """
    Prints all paragraphs containing '|'.

    :param wiki_text_directory: directory of the wikipedia dump, containing folders with files containing articles
        as jsons
    """
    for article in get_article_jsons(wiki_text_directory):
        paragraphs = get_paragraphs(article)
        for para in paragraphs:
            if '|' in para:
                print(para)


# Pete Alonzi (lpa2a@virginia.edu)
# A script to make a pie chart of student laptops for the IT Director
# 2021-06-15
# Evidence to support the need for CEDS

# imports and figure setup
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, aspect=1)

# read in data
df = pd.read_csv('../data/survey.csv')

# calculate counts for OS
myPlot = df.groupby(['Operating System']).size().plot.pie(y='Operating System', figsize=(10,10),colormap='Accent',autopct='%.1i')

# make the plot
ax.set_title("MSDS Student Operating System", fontsize=20, verticalalignment='bottom')
ax.set(ylabel=None)

# show the plot
plt.show()


#!/usr/bin/python3
# 0-safe_print_list.py

def safe_print_list(my_list=[], x=0):
    """Prints x elements in a list.

    Args:
        my_list (list): List of elemets to be printed
        x (int): number of elements to be printed

    Returns: The number of elements printed
    """
    ans = 0

    for i in range(x):
        try:
            print("{}".format(my_list[i]), end="")
            ans += 1
        except IndexError:
            break

    print("")

    return ans


from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 2,
    'retry_delay': timedelta(minutes=1),
}

dag = DAG('ReportGeneration', default_args=default_args, start_date=datetime.now()-timedelta(minutes=1))

t1 = BashOperator(
    task_id='runLoadMySQLData',
    bash_command='/usr/local/bin/python2.7 /home/cloudera/airflow/dags/UserReportGeneration/bin/practical_exercise_data_generator.py --load_data ',
    dag=dag)

t2 = BashOperator(
    task_id='runCreateCSV',
    bash_command='/usr/local/bin/python2.7 /home/cloudera/airflow/dags/UserReportGeneration/bin/practical_exercise_data_generator.py --create_csv ',
    dag=dag)

t3 = BashOperator(
    task_id='runUserTableBackup',
    bash_command='source /home/cloudera/airflow/dags/UserReportGeneration/bin/users_backup.sh ',
    dag=dag)

t4 = BashOperator(
    task_id='runActivityLogTableBackup',
    bash_command='source /home/cloudera/airflow/dags/UserReportGeneration/bin/activity_log_backup.sh ',
    dag=dag)

t5 = BashOperator(
    task_id='runUserUploadDumpBackup',
    bash_command='source /home/cloudera/airflow/dags/UserReportGeneration/bin/user_upload_dump_backup.sh ',
    dag=dag)

t6 = BashOperator(
    task_id='runUserReport',
    bash_command='source /home/cloudera/airflow/dags/UserReportGeneration/bin/user_report.sh ',
    dag=dag)

t7 = BashOperator(
    task_id='runUserTotal',
    bash_command='source /home/cloudera/airflow/dags/UserReportGeneration/bin/user_total.sh ',
    dag=dag)

t1.set_downstream([t2,t3,t4])
t2.set_downstream(t5)
t3.set_downstream([t7,t6])
t4.set_downstream(t6)
t5.set_downstream(t6)


leftMission = 3
leftCannibals = 3
rightMissions=0
rightCannibals=0
userMission = 0
userCannibals = 0
k = 0
print("\nM M M C C C |	 --- | \n")
try:
	while(True):
		while(True):
			print("Left side -> right side river travel")
			userMission = int(input("Enter number of Missionaries travel => "))
			userCannibals = int(input("Enter number of Cannibals travel => "))

			if((userMission==0)and(userCannibals==0)):
				print("Empty travel not possible")
				print("Re-enter : ")
			elif(((userMission+userCannibals) <= 2)and((leftMission-userMission)>=0)and((leftCannibals-userCannibals)>=0)):
				break
			else:
				print("Wrong input re-enter : ")
		leftMission = (leftMission-userMission)
		leftCannibals = (leftCannibals-userCannibals)
		rightMissions += userMission
		rightCannibals += userCannibals

		print("\n")
		for i in range(0,leftMission):
			print("M ",end="")
		for i in range(0,leftCannibals):
			print("C ",end="")
		print("| --> | ",end="")
		for i in range(0,rightMissions):
			print("M ",end="")
		for i in range(0,rightCannibals):
			print("C ",end="")
		print("\n")

		k +=1

		if(((leftCannibals==3)and (leftMission == 1))or((leftCannibals==3)and(leftMission==2))or((leftCannibals==2)and(leftMission==1))or((rightCannibals==3)and (rightMissions == 1))or((rightCannibals==3)and(rightMissions==2))or((rightCannibals==2)and(rightMissions==1))):
			print("Cannibals eat missionaries:\nYou lost the game")

			break

		if((rightMissions+rightCannibals) == 6):
			print("You won the game : \n\tCongrats")
			print("Total attempt")
			print(k)
			break
		while(True):
			print("Right side -> Left side river travel")
			userightMissions = int(input("Enter nuserMissionber of Missionaries travel => "))
			userightCannibals = int(input("Enter nuserMissionber of Cannibals travel => "))

			if((userightMissions==0)and(userightCannibals==0)):
					print("Empty travel not possible")
					print("Re-enter : ")
			elif(((userightMissions+userightCannibals) <= 2)and((rightMissions-userightMissions)>=0)and((rightCannibals-userightCannibals)>=0)):
				break
			else:
				print("Wrong input re-enter : ")
		leftMission += userightMissions
		leftCannibals += userightCannibals
		rightMissions -= userightMissions
		rightCannibals -= userightCannibals

		k +=1
		print("\n")
		for i in range(0,leftMission):
			print("M ",end="")
		for i in range(0,leftCannibals):
			print("C ",end="")
		print("| <-- | ",end="")
		for i in range(0,rightMissions):
			print("M ",end="")
		for i in range(0,rightCannibals):
			print("C ",end="")
		print("\n")

		if(((leftCannibals==3)and (leftMission == 1))or((leftCannibals==3)and(leftMission==2))or((leftCannibals==2)and(leftMission==1))or((rightCannibals==3)and (rightMissions == 1))or((rightCannibals==3)and(rightMissions==2))or((rightCannibals==2)and(rightMissions==1))):
			print("Cannibals eat missionaries:\nYou lost the game")
			break
except EOFError as e:
	print("\nInvalid input please retry !!")


# https://hyunsix.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%ED%81%B0%EC%88%98%EB%A7%8C%EB%93%A4%EA%B8%B0-python
def solution(number, k):
    answer = [] #stack
    for i in number:
        while answer and k>0 and answer[-1]<i:
            answer.pop()
            k-=1
        answer.append(i)
    answer = ''.join(answer[:len(answer)-k]) #77777같은게 들어오면 pop이 안되니까
    return answer


from wxPython import wx
f=wx.wxFrame(None,-1,'test')
s1=wx.wxBoxSizer()
s2=wx.wxBoxSizer()
s1.Add(s2)
f.SetSizer(s1)
#
s1.Add(wx.wxButton(f,-1,'button sizer1'),0,0)
s2.Add(wx.wxButton(f,-1,'button sizer2'),0,0)
#
def hideS2():
    s1.Show(s2,False)
    f.Layout()
def showS2():
    s1.Show(s2,True)
    f.Layout()
#
f.Show(True)


"""
HTTP route handlers for TiddlySpace.

The extensions and modifications to the default TiddlyWeb routes.
"""

import simplejson

try:
    from urlparse import parse_qs
except ImportError:
    from cgi import parse_qs

from tiddlyweb.filters import parse_for_filters
from tiddlyweb.model.bag import Bag
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.store import NoBagError, StoreError, NoTiddlerError
from tiddlyweb import control
from tiddlyweb.web.handler.recipe import get_tiddlers
from tiddlyweb.web.handler.tiddler import get as get_tiddler
from tiddlyweb.web.http import HTTP403
from tiddlyweb.web.util import get_serialize_type

from tiddlywebplugins.utils import require_any_user

from tiddlywebplugins.tiddlyspace.web import (determine_host,
        determine_space, determine_space_recipe)
from tiddlywebplugins.tiddlyspace.space import Space

SPACE_SERVER_SETTINGS = 'ServerSettings'
DEFAULT_NEWUSER_APP = 'apps'

def friendly_uri(environ, start_response):
    """
    Transform a not alread mapped request at the root of a space
    into a request for a tiddler in the public or private recipe
    of the current space.
    """
    _setup_friendly_environ(environ)
    return get_tiddler(environ, start_response)

@require_any_user()
def get_identities(environ, start_response):
    """
    Get a list of the identities associated with the named user.
    That named user must match the current user or the current
    user must be an admin.
    """
    store = environ['tiddlyweb.store']
    username = environ['wsgiorg.routing_args'][1]['username']
    usersign = environ['tiddlyweb.usersign']['name']
    roles = environ['tiddlyweb.usersign']['roles']

    if username != usersign and 'ADMIN' not in roles:
        raise HTTP403('Bad user for action')

    identities = []
    try:
        mapped_bag = store.get(Bag('MAPUSER'))
        tiddlers = store.list_bag_tiddlers(mapped_bag)
        matched_tiddlers = control.filter_tiddlers(tiddlers,
            'select=mapped_user:%s' % username, environ)
        identities = [tiddler.title for tiddler in matched_tiddlers]
    except NoBagError:
        pass

    start_response('200 OK', [
        ('Content-Type', 'application/json; charset=UTF-8')])
    return [simplejson.dumps(identities)]

def get_space_tiddlers(environ, start_response):
    """
    Get the tiddlers that make up the current space in
    whatever the reqeusted representation is. Choose recipe
    based on membership status.
    """
    space_name = _setup_friendly_environ(environ)
    serializer, _ = get_serialize_type(environ)
    # If we are a wiki read ServerSettings, but ignore index
    if ('betaserialization' in serializer
            or 'betalazyserialization' in serializer):
        _, lazy = update_space_settings(environ, space_name)
        if lazy:
            environ['tiddlyweb.type'] = 'text/x-ltiddlywiki'
    return get_tiddlers(environ, start_response)

def home(environ, start_response):
    """
    handles requests at /, serving either the front page or a space (public or
    private) based on whether a subdomain is used.

    relies on tiddlywebplugins.virtualhosting
    """
    http_host, host_url = determine_host(environ)
    if http_host == host_url:
        http_host = 'frontpage.' + http_host
    return serve_space(environ, start_response, http_host)

def serve_space(environ, start_response, http_host):
    """
    Serve a space determined from the current virtual host and user.
    The user determines whether the recipe uses is public or private.
    """
    space_name = determine_space(environ, http_host)
    recipe_name = determine_space_recipe(environ, space_name)
    environ['wsgiorg.routing_args'][1]['recipe_name'] = recipe_name.encode(
            'UTF-8')
    _, mime_type = get_serialize_type(environ)
    index, lazy = update_space_settings(environ, space_name)
    if index:
        environ['wsgiorg.routing_args'][1]['tiddler_name'] = index.encode(
                'UTF-8')
        return get_tiddler(environ, start_response)
    if 'text/html' in mime_type:
        if lazy:
            environ['tiddlyweb.type'] = 'text/x-ltiddlywiki'
        else:
            environ['tiddlyweb.type'] = 'text/x-tiddlywiki'
    return get_tiddlers(environ, start_response)

def _figure_default_index(environ, bag_name, space, index=None):
    """
    if an app hasn't been specified, check whether the space is new or old.
    old users should get the old default: TiddlyWiki, new users should get
    the new default: the app switcher. The presence of a spaceSetupFlag
    tiddler is the test for this.
    """
    store = environ['tiddlyweb.store']
    if not index and space.name != 'frontpage':
        setupFlag = Tiddler('%sSetupFlag' % space.name, bag_name)
        try:
            setupFlag = store.get(setupFlag)
        except NoTiddlerError:
            setupFlag.bag = space.private_bag()
            try:
                setupFlag = store.get(setupFlag)
            except NoTiddlerError:
                index = DEFAULT_NEWUSER_APP

    return index

def update_space_settings(environ, name):
    """
    Read a tiddler named by SPACE_SERVER_SETTINGS in the current
    space's public bag. Parse each line as a key:value pair which
    is then injected tiddlyweb.query. The goal here is to allow
    a space member to force incoming requests to use specific
    settings, such as alpha or externalized.
    """
    store = environ['tiddlyweb.store']
    space = Space(name)
    bag_name = space.public_bag()
    tiddler = Tiddler(SPACE_SERVER_SETTINGS, bag_name)
    data_text = ''
    try:
        tiddler = store.get(tiddler)
        data_text = tiddler.text
    except StoreError:
        return _figure_default_index(environ, bag_name, space), False

    query_strings = []
    index = ''
    lazy = False
    for line in data_text.split('\n'):
        try:
            key, value = line.split(':', 1)
            key = key.rstrip().lstrip()
            value = value.rstrip().lstrip()
            if key == 'index':
                index = value
            elif key == 'lazy':
                if value.lower() == 'true':
                    lazy = True
            else:
                query_strings.append('%s=%s' % (key, value))
        except ValueError:
            pass

    index = _figure_default_index(environ, bag_name, space, index)

    query_string = ';'.join(query_strings)

    filters, leftovers = parse_for_filters(query_string, environ)
    environ['tiddlyweb.filters'].extend(filters)
    query_data = parse_qs(leftovers, keep_blank_values=True)
    environ['tiddlyweb.query'].update(dict(
        [(key, [value for value in values])
            for key, values in query_data.items()]))

    return index, lazy

def _setup_friendly_environ(environ):
    """
    Manipulate the environ so that we appear to be in the context of the
    recipe appropriate for this current space and the current membership
    status. Return space_name to caller.
    """
    http_host, host_url = determine_host(environ)
    if http_host == host_url:
        space_name = "frontpage"
    else:
        space_name = determine_space(environ, http_host)

    recipe_name = determine_space_recipe(environ, space_name)
    environ['wsgiorg.routing_args'][1]['recipe_name'] = recipe_name.encode(
        'UTF-8')
    return space_name


#7. С помощью sklearn обучить модель распознавать части речи в предложении.
# Для этого необходимо разбить данные на обучающую и тестовую выборки, а в конце вывести на экран предсказание модели и степень его точности
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

X = ["I like to run", "He plays the piano", "She is reading a book"]
y = ["NOUN", "VERB", "NOUN"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

classifier = MultinomialNB()
classifier.fit(X_train_vec, y_train)

y_pred = classifier.predict(X_test_vec)
accuracy = accuracy_score(y_test, y_pred)
print(y_pred)
print("Accuracy:", accuracy)


#!/usr/bin/env python3
#
# Step two in building the messageboard server:
# A server that handles both GET and POST requests.
#

from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs

form = '''<!DOCTYPE html>
  <title>Message Board</title>
  <form method="POST" action="http://localhost:8000/">
    <textarea name="message"></textarea>
    <br>
    <button type="submit">Post it!</button>
  </form>
'''

class MessageHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        # How long was the message?
        length = int(self.headers.get('Content-length', 0))

        # Read the correct amount of data from the request.
        data = self.rfile.read(length).decode()

        # Extract the "message" field from the request data.
        message = parse_qs(data)["message"][0]

        # Send the "message" field back as the response.
        self.send_response(200)
        self.send_header('Content-type', 'text/plain; charset=utf-8')
        self.end_headers()
        self.wfile.write(message.encode())

    def do_GET(self):
        # First, send a 200 OK response.
        self.send_response(200)

        # Then send headers.
        self.send_header('Content-type', 'text/html; charset=utf-8')
        self.end_headers()

        # Then encode and send the form.
        self.wfile.write(form.encode())

if __name__ == '__main__':
    server_address = ('', 8000)
    httpd = HTTPServer(server_address, MessageHandler)
    httpd.serve_forever()