import bleach

from django import forms

from osf.models import RegistrationProvider, Subject, RegistrationSchema
from admin.base.utils import (
    get_nodelicense_choices,
    get_defaultlicense_choices,
    validate_slug,
    get_brand_choices,
)

class RegistrationProviderForm(forms.ModelForm):
    _id = forms.SlugField(
        required=True,
        help_text='URL Slug',
        validators=[validate_slug]
    )

    class Meta:
        model = RegistrationProvider
        exclude = [
            'primary_identifier_name',
            'primary_collection',
            'type',
            'advisory_board',
            'example',
            'domain',
            'domain_redirect_enabled',
            'collected_type_choices',
            'status_choices',
            'reviews_comments_private',
            'reviews_comments_anonymous',
        ]

        widgets = {
            'licenses_acceptable': forms.CheckboxSelectMultiple(),
        }

    def __init__(self, *args, **kwargs):
        nodelicense_choices = get_nodelicense_choices()
        defaultlicense_choices = get_defaultlicense_choices()
        brand_choices = get_brand_choices()
        super(RegistrationProviderForm, self).__init__(*args, **kwargs)
        self.fields['licenses_acceptable'].choices = nodelicense_choices
        self.fields['default_license'].choices = defaultlicense_choices
        self.fields['brand'].choices = brand_choices
        if kwargs.get('initial', None) and kwargs.get('initial').get('_id', None):
            provider = RegistrationProvider.load(kwargs.get('initial').get('_id'))
            self.fields['default_schema'].choices = provider.schemas.filter(visible=True, active=True).values_list('id', 'name')
        else:
            self.fields['default_schema'].choices = RegistrationSchema.objects.filter(active=True).values_list('id', 'name')

    def clean_description(self, *args, **kwargs):
        if not self.data.get('description'):
            return u''
        return bleach.clean(
            self.data.get('description'),
            tags=['a', 'br', 'em', 'p', 'span', 'strong'],
            attributes=['class', 'style', 'href', 'title', 'target'],
            styles=['text-align', 'vertical-align'],
            strip=True
        )

    def clean_footer_links(self, *args, **kwargs):
        if not self.data.get('footer_links'):
            return u''
        return bleach.clean(
            self.data.get('footer_links'),
            tags=['a', 'br', 'div', 'em', 'p', 'span', 'strong'],
            attributes=['class', 'style', 'href', 'title', 'target'],
            styles=['text-align', 'vertical-align'],
            strip=True
        )

class RegistrationProviderCustomTaxonomyForm(forms.Form):
    add_missing = forms.BooleanField(required=False)
    custom_taxonomy_json = forms.CharField(widget=forms.Textarea, initial='{"include": [], "exclude": [], "custom": {}, "merge": {}}', required=False)
    provider_id = forms.IntegerField(widget=forms.HiddenInput())
    include = forms.ChoiceField(choices=[], required=False)
    exclude = forms.ChoiceField(choices=[], required=False)
    custom_name = forms.CharField(required=False)
    custom_parent = forms.CharField(required=False)
    bepress = forms.ChoiceField(choices=[], required=False)

    merge_from = forms.ChoiceField(choices=[], required=False)
    merge_into = forms.ChoiceField(choices=[], required=False)

    def __init__(self, *args, **kwargs):
        super(RegistrationProviderCustomTaxonomyForm, self).__init__(*args, **kwargs)
        subject_choices = [(x, x) for x in Subject.objects.filter(bepress_subject__isnull=True).values_list('text', flat=True)]
        for name, field in self.fields.items():
            if hasattr(field, 'choices'):
                if field.choices == []:
                    field.choices = subject_choices


import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from PIL import Image
import os
import numpy as np
import argparse
import json
from utility import loading_data,process_image,imshow

# Create Parse using ArgumentParser
parser = argparse.ArgumentParser(description="Trainning arguments")
# Create command line arguments using add_argument() from ArguementParser method
parser.add_argument('--img_path', type = str, default = './flowers/test/74/image_01200.jpg',        help="Path to the folder of flower images")
parser.add_argument('--checkpoint', type = str, default = './checkpoint.pth',                       help="Path to the checkpoint")
parser.add_argument('--top_k', type = int, default = 5,                                           help="top K most likely classes")
parser.add_argument('--category_names', type = str, default = './cat_to_name.json',                 help="Path to the folder of category_names")
parser.add_argument('--gpu',default=True,                                                         help="Will be using GPU for traning or not")
args = parser.parse_args()

if args.gpu==True:
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
else:
    device = torch.device("cpu")
with open(args.category_names, 'r') as f:
    cat_to_name = json.load(f)

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)
    model = models.vgg11(pretrained=True)

    model.to(device)

    model.classifier = checkpoint['model_calssifier']
    model.load_state_dict(checkpoint['model_state_dict'])
    model.class_to_idx = checkpoint['model_class_to_idx']
    return model
def predict(image_path, model, topk=args.top_k):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    model.to(device)
    model.eval()
    img=process_image(image_path).view([1,3,224,224])
    img=img.to(device)
    with torch.no_grad():
        logps = model.forward(img)
    ps = torch.exp(logps)
    return ps.topk(topk)
def main():
    model=load_checkpoint(args.checkpoint)
    top_p,top_class=predict(args.img_path,model)
    my_dict = {y:x for x,y in model.class_to_idx.items()}
    array=[]
    for i in range(args.top_k):
        k=int(top_class[0][i].cpu().numpy())
        n=my_dict.get(k)
        n=int(n)
        array.append(n)
    lab=[cat_to_name[str(index)] for index in array]
    probability = top_p.cpu().numpy()
    print(f"the name of the highest probabilty : {lab[0]} with probabilty of {(probability[0][0]*100):.3f}% ")
    print(f"the names of top {args.top_k} : {lab}")
    #printing the approximation of probabilites of the top_k instade of using .astype(int)
    probability_app=[]
    for i in range(args.top_k):
        probability_app.append(f"{(probability[0][i]*100):.3f}%")
    print(f"the probabilities of top {args.top_k} : {probability_app}")
if __name__ == "__main__":
    main()


from typing import Optional, Union, Callable
import numpy as np
import warnings

from .gates import PauliX, PauliY, PauliZ
from .ment import Ment, MentOutcome

class ControlMent(Ment):
    def __init__(
        self,
        condition: Optional[Union[bool, MentOutcome]] = None,
        true_angle: Optional[Union[int, float, tuple, str]] = None,
        true_plane: Optional[str] = "XY",
        false_angle: Optional[Union[int, float, tuple, str]] = 0,
        false_plane: Optional[str] = "X",
    ):
        """Controlled measurement operator."""
        true_ment = Ment(angle=true_angle, plane=true_plane)
        super().__init__(angle=false_angle, plane=false_plane)
        self._true_ment = true_ment
        self._condition = condition

    def __repr__(self) -> str:
        return (
            f"ControlMent(False: {super().__repr__()}, True: {repr(self._true_ment)})"
        )

    @property
    def condition(self) -> bool:
        if isinstance(self._condition, bool):
            return lambda x: self._condition
        elif isinstance(self._condition, MentOutcome):
            return self._condition

    @condition.setter
    def condition(self, condition):
        if not isinstance(condition, (bool, MentOutcome)):
            raise TypeError(
                f"Invalid argument type. Expected bool or MentOutcome but got {type(condition)}"
            )
        self._condition = condition

    def angle(self, *args, **kwargs):
        if args == () and kwargs == {}:
            if isinstance(self._condition, bool):
                if self._condition:
                    return self._true_ment.angle
                else:
                    return super().angle
            elif self._true_ment.angle is None or super().angle is None:
                return None
            else:
                return super().angle
        else:
            if self.condition(*args, **kwargs):
                return self._true_ment.angle
            else:
                return super().angle

    def plane(self, *args, **kwargs):
        if args == () and kwargs == {}:
            if self._true_ment.plane is None or super().plane is None:
                return None
            else:
                return super().plane
        else:
            if self.condition(*args, **kwargs):
                return self._true_ment.plane
            else:
                return super().plane

    def is_trainable(self):
        return super().is_trainable() or self._true_ment.is_trainable()

    def copy(self):
        return ControlMent(
            self.condition,
            self._true_ment.angle,
            self._true_ment.plane,
            self._angle,
            self._plane,
        )

    def matrix(self, angle: float | None = None, *args, **kwargs):
        """Return the matrix of the controlled measurement operator."""
        if (not self.is_trainable()) and (angle is not None):
            raise ValueError("ControlledMent is not trainable, so angle must be None.")

        if self.condition(*args, **kwargs):
            if self._true_ment.is_trainable():
                return self._true_ment.matrix(angle, *args, **kwargs)
            else:
                return self._true_ment.matrix()
        else:
            if super().is_trainable():
                return super().matrix(angle, *args, **kwargs)
            else:
                return super().matrix()

    def get_povm(self, angle: float | None = None, *args, **kwargs):
        if self.condition(*args, **kwargs):
            return self._true_ment.get_povm(angle, *args, **kwargs)
        else:
            return super().get_povm(angle, *args, **kwargs)

ControlledMent = ControlMent


import gzip
import os

import numpy as np
import six
from six.moves.urllib import request

from numpy import genfromtxt
from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
import numpy as np
import dateutil.parser
import pdb
import glob
import cPickle as pickle
import shelve
import six
from six.moves.urllib import request

def prepare_data():
    supervised_data = {}
    output_data = []
    new_data_list  = []
    files = glob.glob("/home/deep/development/deep_trading/ib/ftr_csv/*.csv")
    #files = ["/home/deep/development/deep_trading/ib/csv_data/KSCL.csv"]
    for file in files:
        stock_data = genfromtxt(file, delimiter=',', dtype=None, names=True)
        output = None
        daily_gain = None
        #check 1000 can be changed accordingly
        last_n_data = []
        intial_data = stock_data[0]
        for data in stock_data[1:]:
            final_value = data['Low']
            intial_value = intial_data['High']
            #can also add transaction fee
            if (final_value > intial_value):
                output = 1 #should have bought
            elif (final_value < intial_value):
                output = 2 #should have sold
            else:
                output = 0 #should have done nothing
            #percent_gain = (final_value - intial_value) / intial_value
            #converting into 0 to 1
            #output = ((percent_gain - (-1)) / (1 - (-1)) ) * (1 - 0) + 0
            final_date = dateutil.parser.parse(data["DateTime"]).date()
            intial_date = dateutil.parser.parse(intial_data["DateTime"]).date()
            last_n_data.append([intial_data["Low"], intial_data["High"], intial_data["Close"], intial_data["Open"], intial_data["Volume"]])
            if final_date != intial_date:
                opening_price = data["Open"]
                closing_price = intial_data["Close"]
                gain = (opening_price - closing_price) / closing_price
                #converting into 0 to 1 from -1 to 1
                #new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
                daily_gain = ((gain - (-1)) / (1 - (-1)) ) * (1 - 0) + 0
                #todo 1000 can change
            if (daily_gain is not None) and (len(last_n_data) >= 1000):
                last_n_data = last_n_data[-1000:]
                input_vector = make_supervised_vector(last_n_data, daily_gain)
                #list_data = [daily_gain,intial_data["Low"], intial_data["High"], intial_data["Close"], intial_data["Open"], intial_data["Volume"]]
                output_data.append(output)
                #if daily_gain > 0.5:
                    #pdb.set_trace()
                new_data_list.append(input_vector)
            intial_data = data
    stock_data = np.asarray(new_data_list)
    output_data = np.asarray(output_data)
    supervised_data["data"] = stock_data
    supervised_data["target"] = output_data
    return supervised_data

def make_supervised_vector(last_n_data, daily_gain):
    column = len(last_n_data[0])
    standaridized_data = standardization(last_n_data)
    #apply minmax above it to change range from 0 to 1, can be changed
    min_max_scaler = preprocessing.MinMaxScaler()
    standaridized_data = min_max_scaler.fit_transform(standaridized_data)
    vector = standaridized_data[-10:].reshape(10*column,)
    vector = np.append(vector,find_average(standaridized_data[-200:]))
    vector = np.append(vector, find_average(standaridized_data[-1000:]))
    vector = np.append(vector, daily_gain)
    return vector

def standardization(data):
    #Standard standardization with mean
    return preprocessing.scale(data)

def find_average(data):
    return np.mean(data, axis=0)

def load_stock_data():
    if not os.path.exists('stock_5.pkl'):
        dictonary =  prepare_data()
        with open("stock_5.pkl", "wb") as myFile:
            six.moves.cPickle.dump(dictonary, myFile, -1)
    with open('stock_cont.pkl', 'rb') as myFile:
        data = six.moves.cPickle.load(myFile)
    #pdb.set_trace();
    return data


'''Punto en el plano'''

'''Se pide realizar un programa que ingresando el valor x e
y de un punto determine a que cuadrante
pertenece en el sistemas de coordenadas '''

variable_x = int(input('Ingrese el valor de la variable X: '))
variable_y = int(input('Ingrese el valor de la variable y: '))

primer_cuadrante = variable_x >= 0 & variable_y >=0
segundo_cuadrante = variable_x <= -1 & variable_y >= 0
tercer_cuadrante = variable_x <=0 -1 & variable_y <= -1
cuarto_cuadrante = variable_x >= 0 & variable_y <= -1

if primer_cuadrante:
    print('El punto esta en el primer cuadrante.')
elif segundo_cuadrante:
    print('El punto esta en el segundo cuadrante.')
elif tercer_cuadrante:
    print('El punto esta en el tercer cuadrante.')
else:
    print('El punto esta en el cuarto cuadrante.')


dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def short_dist(row, col, visited, min_cnt):
	answer = N*N
	if row == N-1 and col == N-1:
		return min_cnt
	visited[row][col] = True
	for i in range(4):
		new_col = col+dx[i]
		new_row = row+dy[i]
		if 0 <= new_col < N and 0 <= new_row < N and route[new_row][new_col] == 1 and not visited[new_row][new_col]:
			answer = min(answer, short_dist(new_row, new_col, visited, min_cnt+1))
	visited[row][col] = False
	return answer

N = int(input())
route = []
for _ in range(N):
	route.append(list(map(int, input().split(' '))))
visited = [[False for _ in range(N)] for _ in range(N)]
ans = short_dist(0, 0, visited, 1)
if ans != N*N:
	print(ans)


import urwid
import sys
import io
import time

transactions = [{'desc':'1'},{'desc':'2'},{'desc':'3'},{'desc':'4'},{'desc':'5'},{'desc':'6'},{'desc':'7'}]
options = {'bla': 'bla', 'cavia':'cavia', 'kantoor':'kantoor', 'kantine':'kantine'}

class UrwStdWriter(io.TextIOWrapper):

    def __init__(self, *args, **kwargs):
        self.wgt = urwid.Text("")
        self.oldout = sys.stdout
        super(UrwStdWriter, self).__init__(io.BytesIO(), sys.stdout.encoding, *args, **kwargs)
        self.maxrows = 5

    def capture_stdout(self):
        self.oldout = sys.stdout
        sys.stdout = self

    def release_stdout(self):
        sys.stdout = self.oldout

    def __del__(self):
        self.release_stdout()

    def write(self, data):
        #if self.wgt.get_text()[0].count('\n') > self.wgt.rows() and len(self.wgt.get_text()):
        if self.wgt.get_text()[0].count('\n') > self.maxrows:
            cuttxt = self.wgt.get_text()[0].split('\n')[1:]
            self.wgt.set_text("%s%s" %(self.wgt.get_text()[0].split('\n',1)[1], data))
        else:
            self.wgt.set_text("%s%s" %(self.wgt.get_text()[0], data))

    def get_widget(self):
        return self.wgt

class EditCompletion(urwid.Edit):

    def __init__(self, options, *args, **kwargs):
        self.options = options
        super(EditCompletion, self).__init__(*args, **kwargs)

    def keypress(self, size, key):
        p = self.edit_pos
        super(EditCompletion, self).keypress(size, key)

        if self.valid_char(key):
            p = self.edit_pos
            cur = self.get_edit_text()
            if len(cur) and p:
                for o in self.options:
                    if o.startswith(cur[:p]):
                        self.set_edit_text(o)
                        break
            if self.get_edit_text() in self.options:
                return key
        else:
        #elif key=="tab" and self.allow_tab:
        #    key = " "*(8-(self.edit_pos%8))
        #    self.insert_text(key)
            return key

class TransactionListBox(urwid.ListBox):
    def __init__(self, transactions, account_options, *args, **kwargs):
        tws = []
        for t in transactions:
            tws.append(urwid.Pile((urwid.Text(('title transaction', "[{0}]{1}".format(t.get('amount', 0), t['desc']))), EditCompletion(account_options))))
        #urwid.Pile()
        body = urwid.SimpleFocusListWalker(tws)

        super(TransactionListBox, self).__init__(body)

    #def keypress(self, size, key):
    #    if key != 'enter':
    #        return super(TransactionListBox, self).keypress(size, key)
    #    self.original_widget = urwid.Text(
    #        u"Nice to meet you,\n%s.\n\nPress Q to exit." %
    #        edit.edit_text)

palette = [
    (None,  'light gray', 'black'),
    ('heading', 'black', 'light gray'),
    ('line', 'black', 'light gray'),
    ('options', 'dark gray', 'black'),
    ('focus heading', 'white', 'dark red'),
    ('focus line', 'black', 'dark red'),
    ('focus options', 'black', 'light gray'),
    ('selected', 'light gray', 'dark blue'),
    ('title transaction', 'white', 'dark blue')]

def on_cancel_clicked(button):
    raise urwid.ExitMainLoop()

def gnc_urw_edit(transactions, options):

    def unhandled_key(key):
        if key == 'esc':
            raise urwid.ExitMainLoop()
        elif key == 'f2':
            on_save_clicked(None)

    def on_save_clicked(button):
        count = 0
        for pile in tlb.original_widget.body:
            acc_name = pile.contents[1][0].get_edit_text()
            try:
                transactions[count]['account'] = options[acc_name]
            except Exception as e:
                print(("Can't match accounts, please fix: %s" %e))
                break
            else:
                count += 1

        print((count, len(transactions)))
        if count == len(transactions):
            out.release_stdout()
            raise urwid.ExitMainLoop()
        else:
            print("ERROR: not all transactions are matched")

    cancel_b = urwid.Button('ESC:Cancel')
    save_b = urwid.Button('F2:Save&Exit')
    urwid.connect_signal(save_b, 'click', on_save_clicked)
    urwid.connect_signal(cancel_b, 'click', on_cancel_clicked)
    footer = urwid.AttrMap(urwid.Columns(((18, save_b), (16,cancel_b)),dividechars=5), 'heading')
    header = urwid.AttrMap(urwid.Text("Edit transactions"), 'heading')

    tlb = urwid.AttrMap(TransactionListBox(transactions, list(options.keys())), 'selected')
    acc_options = urwid.AttrMap(urwid.LineBox(urwid.Text("\n".join(options)), title='options'), 'focus options')
    out = UrwStdWriter()

    debug = urwid.Filler(urwid.LineBox(out.get_widget(), title='debug'), height='flow', min_height=10)
    frame = urwid.Frame(urwid.Columns((tlb, (20, urwid.Filler(acc_options)))), header=header, footer=footer)
    loop = urwid.MainLoop(urwid.Pile((('weight', 10, frame), (10, debug))), unhandled_input=unhandled_key, palette=palette)
    out.capture_stdout()
    loop.run()
    print("urwid finished")
    return transactions

if __name__ == '__main__':
    gnc_urw_edit(transactions, options)


import numpy as np
import pandas as pd
from  src.exception import Custom_Exception
import os
from src.logger import logging
from src.utils import save_object
import sys
from dataclasses import dataclass
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder,StandardScaler,OneHotEncoder,MinMaxScaler
from imblearn.combine import SMOTEENN
from imblearn.under_sampling import EditedNearestNeighbours
from imblearn.pipeline import Pipeline as ImbPipeline
from sklearn.pipeline import make_pipeline,Pipeline
from sklearn.compose import ColumnTransformer, make_column_selector

@dataclass
class DataTransformationConfig:
    preprocessor_obj_file_path=os.path.join('artifacts','pre_processor.pkl')

class DataTranformation:
    def __init__(self):
        self.data_transformation_config=DataTransformationConfig()

    def get_data_tranformer_object(self):
        try:
            numerical_features=['SLNO', 'Candidate Ref', 'Duration to accept offer', 'Notice period',
                                'Pecent hike expected in CTC', 'Percent hike offered in CTC',
                                'Percent difference CTC', 'Rex in Yrs', 'Age']

            categorical_features=['DOJ Extended', 'Offered band', 'Joining Bonus',
                                  'Candidate relocate actual', 'Gender', 'Candidate Source',
                                  'LOB', 'Location']

            num_pipeline=Pipeline(
            steps=[
               ( "imputer",SimpleImputer(strategy='median')),
               ("scaler",MinMaxScaler())
            ]
        )

            cat_pipeline=Pipeline(
             steps=[
               ( "imputer",SimpleImputer(strategy='most_frequent')),
               ("one_hot_encoder",OneHotEncoder())
            ]
        )
            preprocessor=ColumnTransformer(
            [
                ("num_pipeline",num_pipeline,numerical_features),
                ("cat_pipeline",cat_pipeline,categorical_features)

            ]
        )
            return preprocessor

        except Exception as e:
            raise Custom_Exception(e,sys)

    def initiate_data_transformation(self,train_path,test_path):
        try:
            train_df = pd.read_csv(train_path)
            test_df = pd.read_csv(test_path)
            logging.info('Reading of Train and Test data has been done successfully.')
            preprocessing_obj = self.get_data_tranformer_object()
            target_column = "Status"
            input_feature_train_df = train_df.drop(columns=[target_column], axis=1)
            train_df[target_column] = train_df[target_column].replace({'Joined': 1, 'Not Joined': 0})
            test_df[target_column] = test_df[target_column].replace({'Joined': 1, 'Not Joined': 0})
            target_feature_train_df = train_df[target_column]
            input_feature_test_df = test_df.drop(columns=[target_column], axis=1)
            target_feature_test_df = test_df[target_column]
            logging.info('Applying pre-processing object on training and testing dataframe')

            # Fit the preprocessor on training data
            input_feature_train_arr = preprocessing_obj.fit_transform(input_feature_train_df)

            # Apply the preprocessor on testing data
            input_feature_test_arr = preprocessing_obj.transform(input_feature_test_df)

            # Concatenate the target feature to the transformed training and testing arrays
            train_arr = np.c_[input_feature_train_arr, np.array(target_feature_train_df)]
            test_arr = np.c_[input_feature_test_arr, np.array(target_feature_test_df)]
            logging.info('Data Transformation obj created successfully')

            save_object(
                file_path=self.data_transformation_config.preprocessor_obj_file_path,
                obj=preprocessing_obj
            )

            return train_arr, test_arr, self.data_transformation_config.preprocessor_obj_file_path

        except Exception as e:
            raise Custom_Exception(e, sys)


# coding: utf-8

"""
Константы и утилитные функции.
"""

import asyncio
import os.path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from typing import Union, Optional, SupportsInt, List

#############################################################################
# COMMON CONSTANTS
#############################################################################

# Max number of postings

MAX_POSTINGS = 32758

# Encodings

ANSI = 'cp1251'
UTF = 'utf-8'
OEM = 'cp866'

# Stop marker for menus

STOP_MARKER = '*****'

# Record status

LOGICALLY_DELETED = 1
PHYSICALLY_DELETED = 2
ABSENT = 4
NON_ACTUALIZED = 8
NEW_RECORD = 16
LAST = 32
LOCKED = 64
AUTOIN_ERROR = 128
FULL_TEXT_NOT_ACTUALIZED = 256

# Paths

SYSTEM = 0
DATA = 1
MASTER_FILE = 2
INVERTED_FILE = 3
PARAMETER_FILE = 10
FULL_TEXT = 11
INTERNAL_RESOURCE = 12

# Text delimiters

IRBIS_DELIMITER = '\x1F\x1E'
SHORT_DELIMITER = '\x1E'
OTHER_DELIMITER = '\x1F'
MSDOS_DELIMITER = '\r\n'

# Common formats

ALL = "&uf('+0')"
BRIEF = '@brief'
IBIS = '@ibiskw_h'
INFORMATIONAL = '@info_w'
OPTIMIZED = '@'

# Workstation codes

ADMINISTRATOR = 'A'
CATALOGER = 'C'
ACQUISITIONS = 'M'
COMPLECT = 'M'
READER = 'R'
CIRCULATION = 'B'
BOOKLAND = 'B'
PROVISION = 'K'
JAVA_APPLET = 'J'

# Error codes

# Клиент не подключен к серверу
NOT_CONNECTED = -100500

# Command codes

# Получение признака монопольной блокировки базы данных
EXCLUSIVE_DATABASE_LOCK = '#'

# Получение списка удаленных, неактуализированных
# и заблокированных записей
RECORD_LIST = '0'

# Получение версии сервера
SERVER_INFO = '1'

# Получение статистики по базе данных
DATABASE_STAT = '2'

# Глобальная корректировка
GLOBAL_CORRECTION = '5'

# Сохранение группы записей
SAVE_RECORD_GROUP = '6'

# Печать
PRINT = '7'

# Запись параметров в ini - файл, расположенный на сервере
UPDATE_INI_FILE = '8'

IMPORT_ISO = '9'

# Регистрация клиента на сервере
REGISTER_CLIENT = 'A'

# Разрегистрация клиента
UNREGISTER_CLIENT = 'B'

# Чтение записи, ее расформатирование
READ_RECORD = 'C'

# Сохранение записи
UPDATE_RECORD = 'D'

# Разблокировка записи
UNLOCK_RECORD = 'E'

# Актуализация записи
ACTUALIZE_RECORD = 'F'

# Форматирование записи или группы записей
FORMAT_RECORD = 'G'

# Получение терминов и ссылок словаря,
# форматирование записей
READ_TERMS = 'H'

# Получение ссылок для термина (списка терминов)
READ_POSTINGS = 'I'

# Глобальная корректировка виртуальной записи
CORRECT_VIRTUAL_RECORD = 'J'

# Поиск записей с опциональным форматированием
SEARCH = 'K'

# Получение / сохранение текстового файла,
# расположенного на сервере (группы текстовых файлов)
READ_DOCUMENT = 'L'

BACKUP = 'M'

# Пустая операция. Периодическое подтверждение
# соединения с сервером
NOP = 'N'

# Получение максимального MFN для базы данных
GET_MAX_MFN = 'O'

# Получение терминов и ссылок словаря в обратном порядке
READ_TERMS_REVERSE = 'P'

# Разблокирование записей
UNLOCK_RECORDS = 'Q'

# Полнотекстовый поиск (новый!)
FULL_TEXT_SEARCH = '&'

# Опустошение базы данных
EMPTY_DATABASE = 'S'

# Создание базы данных
CREATE_DATABASE = 'T'

# Разблокирование базы данных
UNLOCK_DATABASE = 'U'

# Чтение ссылок для заданного MFN
GET_RECORD_POSTINGS = 'V'

# Удаление базы данных
DELETE_DATABASE = 'W'

# Реорганизация мастер - файла
RELOAD_MASTER_FILE = 'X'

# Реорганизация словаря
RELOAD_DICTIONARY = 'Y'

# Создание поискового словаря заново
CREATE_DICTIONARY = 'Z'

# Получение статистики работы сервера
GET_SERVER_STAT = '+1'

# Получение списка запущенных процессов
GET_PROCESS_LIST = '+3'

# Сохранение списка пользователей
SET_USER_LIST = '+7'

# Перезапуск сервера
RESTART_SERVER = '+8'

# Получение списка пользователей
GET_USER_LIST = '+9'

# Получение списка файлов на сервере
LIST_FILES = '!'

# Codes for ReadRecord command
READ_RECORD_CODES = [-201, -600, -602, -603]

# Codes for ReadTerms command
READ_TERMS_CODES = [-202, -203, -204]

#############################################################################
# COMMON FUNCTIONS
#############################################################################

def same_string(first: 'Optional[str]', second: 'Optional[str]') -> bool:
    """
    Строки совпадают с точностью до регистра?

    :param first: Первая строка
    :param second: Вторая строка
    :return: True, если совпадают
    """
    if first is None or second is None:
        return False
    return first.upper() == second.upper()

def safe_str(obj) -> str:
    """
    Простое избегание 'None' при выводе текстового представления объекта.

    :param obj: Объект
    :return: Текстовое представление
    """
    if obj is None:
        return ''
    return str(obj)

def safe_int(text: 'Union[str, bytes, SupportsInt]') -> int:
    """
    Безопасное превращение строки в целое.

    :param text: Текст
    :return: Целое число
    """
    try:
        result: int = int(text)
    except ValueError:
        result = 0
    return result

# noinspection PyUnreachableCode
def throw_value_error() -> str:
    """
    Simple raises ValueError.

    :return: None
    """
    raise ValueError()

def irbis_to_dos(text: str) -> str:
    """
    Convert IRBIS text to MS-DOS text.

    :param text: Text to convert
    :return: Converted text
    """
    return text.replace(IRBIS_DELIMITER, '\n')

def irbis_to_lines(text: str) -> 'List[str]':
    """
    Convert IRBIS text to list of strings.

    :param text: Text to convert
    :return: List of strings
    """
    return text.split(IRBIS_DELIMITER)

def short_irbis_to_lines(text: str) -> 'List[str]':
    """
    Convert IRBIS with short delimiter text to list of strings.

    :param text: Text to convert
    :return: List of strings
    """
    return text.split(SHORT_DELIMITER)

def change_extension(filename: str, new_extension: str) -> str:
    """
    Смена расширения у файла (без физического переименования!).
    :param filename:
    :param new_extension:
    :return:
    """
    return os.path.splitext(filename)[0] + new_extension

def remove_comments(text: str) -> str:
    """
    Удаление комментариев из кода.

    :param text: Код для очистки
    :return: Очищенный код
    """

    if '/*' not in text:
        return text

    result = []
    state = ''
    index = 0
    length = len(text)
    while index < length:
        char = text[index]
        if state in ("'", '"', '|'):
            if char == state:
                state = ''
            result.append(char)
        else:
            if char == '/':
                if index + 1 < length and text[index + 1] == '*':
                    while index < length:
                        char = text[index]
                        if char in ('\r', '\n'):
                            result.append(char)
                            break
                        index = index + 1
                else:
                    result.append(char)
            else:
                if char in ("'", '"', '|'):
                    state = char
                    result.append(char)
                else:
                    result.append(char)
        index = index + 1

    return ''.join(result)

def prepare_format(text: str) -> str:
    """
    Подготовка формата к отсылке на сервер.

    :param text: Неподготовленный формат
    :return: Подготовленный формат
    """
    text = remove_comments(text)
    length = len(text)
    if length == 0:
        return text

    flag = False
    for char in text:
        if char < ' ':
            flag = True
            break

    if not flag:
        return text

    result = []
    for char in text:
        if char >= ' ':
            result.append(char)

    return ''.join(result)

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

# pylint: disable=invalid-name
irbis_event_loop = None

def init_async() -> None:
    """
    Инициализация асинхронной обработки цикла сообщений.

    :return: None.
    """
    global irbis_event_loop  # pylint: disable=global-statement
    if not irbis_event_loop:
        irbis_event_loop = asyncio.get_event_loop()

def close_async() -> None:
    """
    Завершение асинхронной обработки цикла сообщений.

    :return: None.
    """
    global irbis_event_loop  # pylint: disable=global-statement
    if irbis_event_loop:
        irbis_event_loop.close()
        irbis_event_loop = None

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

class ObjectWithError:
    """
    Объект, хранящий код последней ошибки.
    """

    __slots__ = ('last_error',)

    def __init__(self):
        self.last_error: int = 0


from collections import Counter

class Supermarket():
    total = 0.0

    def get_total(self):
        return self.total

    def change_value(self, value):
        self.total += value

    def calculate_number_of_coins(self, change):
        currency = [0.01, 0.05, 0.1, 0.25, 0.5, 1.00, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0]

        n = len(currency)
        result = []
        i = n - 1
        change = round(change, 2)

        while i >= 0:
            while change >= currency[i]:
                change = round(change - currency[i],2)
                result.append(currency[i])

            i -= 1

        result = dict(Counter(result))

        for i in list(set(currency) - set(result)):
            result[i] = 0

        return result


from student_table import StudentHashRecords
import common_utils
import re

class Students:
    def __init__(self):
        self.cse_list = list()
        self.mec_list = list()
        self.ece_list = list()
        self.arc_list = list()

    def initializeHash(self):
        """ intialize Student Hash Table and perform operation like insertion, halloffame, course, average"""
        studentHashRecords = StudentHashRecords()
        self.get_inputs_from_txt(studentHashRecords)
        self.hallOfFame(studentHashRecords)
        CGPAFrom, CPGATo = self.get_course_offer_range()
        self.newCourseList(studentHashRecords, CGPAFrom, CPGATo)
        self.depAvg(studentHashRecords)
        self.destroyHash(studentHashRecords)

    def insertStudentRec(self, StudentHashRecords, studentId, CGPA):
        """Insert the Student records in the Hash Table"""
        StudentHashRecords.add(key=studentId, value=CGPA)

    def hallOfFame(self, StudentHashRecords):
        """Topper of per year and per department"""
        self.distribute_student_in_dep(StudentHashRecords)
        prompts_data = self.get_prompts_from_txt()
        for prompt_data in prompts_data:
            if 'hallOfFame' in prompt_data:
                cse_hall_of_fame_list = self.get_hall_of_fame_dept_wise(self.cse_list)
                mec_hall_of_fame_list = self.get_hall_of_fame_dept_wise(self.mec_list)
                ece_hall_of_fame_list = self.get_hall_of_fame_dept_wise(self.ece_list)
                arc_hall_of_fame_list = self.get_hall_of_fame_dept_wise(self.arc_list)
                total_quantity = len(cse_hall_of_fame_list) + len(mec_hall_of_fame_list) + len(
                    ece_hall_of_fame_list) + len(arc_hall_of_fame_list)
                self.write_halloffame_output(total_quantity, cse_hall_of_fame_list, mec_hall_of_fame_list,
                                             ece_hall_of_fame_list, arc_hall_of_fame_list)

    def newCourseList(self, StudentHashRecords, CGPAFrom, CPGATo):
        """ prints the list of all students who have a CGPA within the given range and have graduated in the
        last 5 years"""
        year_range_start = 2011
        year_range_end = 2017
        self.distribute_student_for_course(StudentHashRecords, year_range_start, year_range_end)
        cse_course_list = self.get_new_course_dept_wise(self.cse_list, CGPAFrom, CPGATo)
        mec_course_list = self.get_new_course_dept_wise(self.mec_list, CGPAFrom, CPGATo)
        ece_course_list = self.get_new_course_dept_wise(self.ece_list, CGPAFrom, CPGATo)
        arc_course_list = self.get_new_course_dept_wise(self.arc_list, CGPAFrom, CPGATo)
        total_quantity = len(cse_course_list) + len(mec_course_list) + len(ece_course_list) + len(arc_course_list)
        self.write_newcourse_output(CGPAFrom, CPGATo, total_quantity, cse_course_list,mec_course_list,ece_course_list,arc_course_list)

    def depAvg(self, StudentHashRecords):
        """ prints the list of all departments followed by the maximum CGPA and average CGPA of all students"""
        dep_cgpa_list = []
        self.distribute_student_in_dep(StudentHashRecords)
        cse_max_value, cse_avg = self.get_depavg_dept_wise(self.cse_list)
        dep_cgpa_list.append("CSE: max: {}, avg: {}".format(cse_max_value, cse_avg))
        mec_max_value, mec_avg = self.get_depavg_dept_wise(self.mec_list)
        dep_cgpa_list.append("MEC: max: {}, avg: {}".format(mec_max_value, mec_avg))
        ece_max_value, ece_avg = self.get_depavg_dept_wise(self.ece_list)
        dep_cgpa_list.append("ECE: max: {}, avg: {}".format(ece_max_value, ece_avg))
        arc_max_value, arc_avg = self.get_depavg_dept_wise(self.arc_list)
        dep_cgpa_list.append("ARC: max: {}, avg: {}".format(arc_max_value, arc_avg))
        self.write_depavg_output(dep_cgpa_list)

    def destroyHash(self, StudentHashRecords):
        """destroy hash table implementation"""
        return StudentHashRecords.delete()

    def get_course_offer_range(self):
        """get course range from prompt files"""
        prompts_data = self.get_prompts_from_txt()
        for prompt_data in prompts_data:
            if "courseOffer" in prompt_data:
                course_range = prompt_data.split(":")
                course_range[1] = course_range[1].rstrip()
                course_range[2] = course_range[2].rstrip()
                return float(course_range[1]), float(course_range[2])

    def get_hall_of_fame_dept_wise(self, dep_list):
        """ fetch student id with grade who topped in their department in respective year"""
        hall_of_fame = list()
        stud_id, max_value = self.get_key_and_value(dep_list[0])
        existing_year = self.get_student_desc(stud_id)
        for stud in dep_list:
            key, value = self.get_key_and_value(stud)
            year = self.get_student_desc(key)
            if year < 2017 and year == existing_year and value > max_value:
                max_value = value
                stud_id = key
            elif year < 2017 and year != existing_year:
                hall_of_fame.append("{} / {}".format(stud_id, max_value))
                existing_year = year
                max_value = value
                stud_id = key
        hall_of_fame.append("{} / {}".format(stud_id, max_value))
        return hall_of_fame

    def get_new_course_dept_wise(self, dep_list,  CGPAFrom, CPGATo):
        """fetch the student id and grade who falls under new course range"""
        new_course_list = list()
        for stud in dep_list:
            key, value = self.get_key_and_value(stud)
            if CGPAFrom <= value <= CPGATo:
                new_course_list.append("{} / {}".format(key, value))
        return new_course_list

    def get_depavg_dept_wise(self, dep_list):
        """ calculate the department wise max grade and average grade"""
        max_value = 0
        sum = 0
        for stud in dep_list:
            key, value = self.get_key_and_value(stud)
            sum += value
            if max_value < value:
                max_value = value
        return max_value, round(sum/len(dep_list), 1)

    def get_inputs_from_txt(self, StudentHashRecords):
        """ read the student id and grade from input file and feed into hash table"""
        input_path = common_utils.get_data_dir("inputPS12.txt")
        input_data = common_utils.get_content_from_file(input_path)
        for data in input_data:
            record = data.split('/')
            record[0] = record[0].rstrip()
            record[1] = record[1].rstrip()
            self.insertStudentRec(StudentHashRecords, record[0], float(record[1]))

    def get_key_and_value(self, data):
        """ to return key and value from the list"""
        return data[0], data[1]

    def takeFirst(self, elem):
        """return first element of the list"""
        return elem[0]

    def distribute_student_in_dep(self, StudentHashRecords):
        """categorize the students on the basis on each department"""
        self.cse_list = []
        self.mec_list = []
        self.ece_list = []
        self.arc_list = []
        for data in StudentHashRecords:
            key, value = self.get_key_and_value(data)
            if re.search("CSE", key):
                self.cse_list.append(data)
            elif re.search("MEC", key):
                self.mec_list.append(data)
            elif re.search("ECE", key):
                self.ece_list.append(data)
            elif re.search("ARC", key):
                self.arc_list.append(data)
        self.cse_list.sort(key=self.takeFirst)
        self.arc_list.sort(key=self.takeFirst)
        self.mec_list.sort(key=self.takeFirst)
        self.ece_list.sort(key=self.takeFirst)

    def distribute_student_for_course(self, StudentHashRecords, year_range_start, year_range_end):
        """categorize the graduated students on the basis on each department only for last 5 year"""
        self.cse_list = []
        self.mec_list = []
        self.ece_list = []
        self.arc_list = []
        for data in StudentHashRecords:
            key, value = self.get_key_and_value(data)
            if re.search("CSE", key):
                year = self.get_student_desc(key)
                if year_range_start < year < year_range_end:
                    self.cse_list.append(data)
            elif re.search("MEC", key):
                year = self.get_student_desc(key)
                if year_range_start < year < year_range_end:
                    self.mec_list.append(data)
            elif re.search("ECE", key):
                year = self.get_student_desc(key)
                if year_range_start < year < year_range_end:
                    self.ece_list.append(data)
            elif re.search("ARC", key):
                year = self.get_student_desc(key)
                if year_range_start < year < year_range_end:
                    self.arc_list.append(data)
            self.cse_list.sort(key=self.takeFirst)
            self.arc_list.sort(key=self.takeFirst)
            self.mec_list.sort(key=self.takeFirst)
            self.ece_list.sort(key=self.takeFirst)

    def get_prompts_from_txt(self):
        """retrieve the tags present in prompt file"""
        prompts_path = common_utils.get_data_dir("promptsPS12.txt")
        prompts_data = common_utils.get_content_from_file(prompts_path)
        return prompts_data

    def get_student_desc(self, key):
        """ fetch the year from student id"""
        year = key[:4]
        return int(year)

    def write_halloffame_output(self, total_quantity, cse_hall_of_fame_list, mec_hall_of_fame_list,
                                ece_hall_of_fame_list, arc_hall_of_fame_list):
        """ write hall of fame data to output file"""
        filename = common_utils.get_abs_path(common_utils.get_output_dir(), "outputPS12.txt")
        with open(filename, 'w+') as file:
            file.write("---------- hall of fame ----------\n")
            file.write("Total eligible students: {}\n".format(total_quantity))
            file.write("Qualified students:\n")
            for line in cse_hall_of_fame_list:
                file.write(line + "\n")
            for line in mec_hall_of_fame_list:
                file.write(line + "\n")
            for line in ece_hall_of_fame_list:
                file.write(line + "\n")
            for line in arc_hall_of_fame_list:
                file.write(line + "\n")
            file.write("-------------------------------------\n")

    def write_newcourse_output(self,CGPAFrom, CPGATo, total_quantity, cse_course_list,mec_course_list,ece_course_list,arc_course_list):
        """ write new course offer data to output file"""
        filename = common_utils.get_abs_path(common_utils.get_output_dir(), "outputPS12.txt")
        with open(filename, 'a+') as file:
            file.write("---------- new course candidates ----------\n")
            file.write("Input: {} to {}\n".format(CGPAFrom, CPGATo))
            file.write("Total eligible students: {}\n".format(total_quantity))
            file.write("Qualified students:\n")
            for line in cse_course_list:
                file.write(line + "\n")
            for line in mec_course_list:
                file.write(line + "\n")
            for line in ece_course_list:
                file.write(line + "\n")
            for line in arc_course_list:
                file.write(line + "\n")
            file.write("-------------------------------------\n")

    def write_depavg_output(self,dep_cgpa_list):
        """ write department wise average and max grade data to output file"""
        filename = common_utils.get_abs_path(common_utils.get_output_dir(), "outputPS12.txt")
        with open(filename, 'a+') as file:
            file.write("---------- department CGPA ----------\n")
            for line in dep_cgpa_list:
                file.write(line + "\n")
            file.write("-------------------------------------\n")

if __name__ == '__main__':
    student = Students()
    student.initializeHash()


#练习：这一脚本内容较多。请及时复习
#该脚本练习使用tensorboard可视化工具：包括：绘制计算图，loss曲线，参数直方图
# 问题：W是在函数中初始化的，那么W是如何更新的？？？？？？？？？？？？？？？？？？？？？？是否可以对最后训练出的w进行可视化？
# 问题2：accuracy的定义也在一个函数中（注意这个函数是在会话建立之后调用的），为什么accuracy的summary没有被加入到tensorboard中？
# 问题3：tensorflow如何进行cross-validation?????????????????????????????/
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
BATCH_SIZE = 100
LEARNING_RATE = 0.005
def add_layer(input, output_dim,layer_name,  activation = None):
    with tf.name_scope(layer_name):
        input_dim = input.get_shape().as_list()[1]
        with tf.name_scope('Weights'):
            W = tf.Variable(tf.random_normal([input_dim, output_dim]),name='Weights')
            tf.summary.histogram(layer_name + '/Weights', W)
        with tf.name_scope('biases'):
            b = tf.Variable(tf.zeros(output_dim) + 0.5,name='Biases')
            tf.summary.histogram(layer_name +'/biases', b)
        if activation is None:
            output = tf.matmul(input, W)+b
        else:
            output = activation(tf.matmul(input, W)+b)
            tf.summary.histogram(layer_name+'/output', output)
        return output

def compute_accuracy(val_Xs,val_ys):  #注意：这个函数是在会话中调用的，且不需要显示的传入一个参数代表会话sess
    # 问题：这里的accuracy能不能像loss一样在tensoborad scalar部分画出？
    #注意：使用全局变量
    global prediction
    y_pre = sess.run(prediction,feed_dict={Xs:val_Xs, ys:val_ys})
    # tf.argmax返回的是最大值所对应的索引
    correct_prediction = tf.equal(tf.argmax(y_pre,1), val_ys)
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar('accuracy',accuracy)
    result = sess.run(accuracy, feed_dict={Xs:val_Xs, ys:val_ys})
    return result

digits = load_digits()
data = digits.data
label = digits.target
X_train, X_test, y_train, y_test = train_test_split(data, label)
with tf.name_scope('Input'):
    Xs = tf.placeholder(dtype=tf.float32, shape = [None,64] , name='X')
    ys = tf.placeholder(dtype=tf.int32, shape = [None], name = 'y')
l1 = add_layer(Xs, 100, layer_name='layer_1', activation=tf.nn.tanh) #为什么使用relu效果要差很多？
prediction = add_layer(l1, 10, layer_name='layer_2', activation=tf.nn.softmax)

with tf.name_scope('Loss'):
    #注意下一句的错误①：ys 为N x 1, 而 prediction 为 N X 10,故要转换成one-hot形式
    #错误②：在使用cross_entropy = -tf.reduce_sum(y_*tf.log(y))时，y最小值有可能取到0，导致log出错
    # cross_entropy = tf.reduce_mean(-tf.reduce_sum(tf.one_hot(indices=ys, depth=10)*tf.log(prediction), axis=1))
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(tf.one_hot(indices=ys, depth=10) *
                                                  tf.log(tf.clip_by_value(prediction, 1e-10, 1.0)), axis=1))
    tf.summary.scalar('loss',cross_entropy)  #注意：以逗号分隔
with tf.name_scope('Train'):
    train_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cross_entropy)
init = tf.global_variables_initializer()
N = X_train.shape[0]
# with tf.device('/gpu:0'):
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('tf_visualizer/c1', sess.graph)

sess.run(init)
for iter in range(2000):
    indices = np.array(np.random.choice(N,BATCH_SIZE,replace=True))
    X_batch = X_train[indices]
    y_batch = y_train[indices]
    sess.run(train_step, feed_dict={Xs:X_batch,ys:y_batch})
    if iter%200 == 0:
        print('iteration: {}/2000: '.format(iter),)
        print(' 准确率为：' + repr(compute_accuracy(X_batch, y_batch)),
              'loss值为：'+repr(sess.run(cross_entropy,feed_dict={Xs:X_batch,ys:y_batch})))
        result = sess.run(merged,feed_dict={Xs:X_batch,ys:y_batch})
        writer.add_summary(result,iter)  #注意不要忘记了


import random

from org.sortition.groupselect.allocator.TAAllocator import TAAllocator

class TAAllocationsManager:
    def __init__(self, ctx):
        self.ctx = ctx

    def run(self, progress_bar=None):
        app_data = self.ctx.app_data

        tables = app_data.settings['tables']
        seats = app_data.settings['seats']

        if(tables*seats < len(app_data.peopledata_vals)):
            raise Exception("Error: Not enough space!", "There's not enough space! Please increase the number of groups or number of people per group!")

        seed = app_data.settings['seed']

        try:
            random.seed(seed)
        except:
            raise Exception("Error: Random seed incorrect!", "There was a problem setting the random seed. Please check your input!")

        nallocations = app_data.settings['nallocations']
        nattempts = app_data.settings['nattempts']

        if (nallocations < 1):
            raise Exception("Error: Wrong allocation number!", "The number of computed allocations must at least be 1!")

        peopledata_vals_used = [{} for i in range(app_data.m_data)]

        for i in range(app_data.m_data):
            for j in app_data.order_cluster+app_data.order_diverse:
                peopledata_vals_used[i][j] = self.ctx.app_data_manager.load_details(i, j)

        order_cluster_dict = self.ctx.app_data_manager.get_fields_cluster_dict()
        order_diverse_dict = self.ctx.app_data_manager.get_fields_diverse_dict()

        if not order_diverse_dict:
            raise Exception("Error: One diversification field required!", "You have to set at least one field that is used to diversify people across groups.")

        allocator = TAAllocator(tables, seats, peopledata_vals_used, order_cluster_dict, order_diverse_dict)

        manuals = app_data.manuals

        if any([len([m[0] for m in manuals if m[1] == t]) > seats for t in range(tables)]):
            raise Exception("Error: Too many manuals!", "You allocated too many people manually to one group.")
        allocator.set_manually(manuals)

        allocations = []
        pids = list(range(len(app_data.peopledata_vals)))
        for n in range(nattempts):
            if progress_bar: progress_bar.setValue(n+1)
            random.shuffle(pids)
            result = allocator.run(pids)
            allocations.append([[person[0] for person in table if person] for table in result])

        allocation_groups = []
        for n in range(nattempts):
            allocation_group = random.sample(allocations, nallocations)
            allocation_group_links = 0
            allocation_group_links_max = 0

            for pid in range(app_data.m_data):
                links = []
                for allc in allocation_group:
                    for t in range(tables):
                        if pid in allc[t]:
                            new_links = allc[t][:]
                            new_links.remove(pid)
                            links.extend(new_links)
                            allocation_group_links_max += len(allc[t])-1
                allocation_group_links += len(list(set(links)))

            allocation_groups.append([allocation_group_links, allocation_group_links_max, allocation_group])

        allcmax = max(allocation_groups, key=lambda x: x[0])
        allocation_group_outcome = allcmax[2]
        allocation_group_links_pp = allcmax[0]/app_data.m_data
        allocation_group_links_pp_max = allcmax[1]/app_data.m_data

        self.ctx.app_data.results = allocation_group_outcome

        self.links = allocation_group_links_pp
        self.links_rel = allocation_group_links_pp/allocation_group_links_pp_max


# Problem: https://codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/start/

def solution(array):
  elements_count = get_elements_count(array)
  # Find first odd value occurrence in elements count dict
  for key, value in elements_count.iteritems():
    if is_odd(value):
      return key

# Given an array, return a dictionary which contains the array values as keys
# and the number of occurrences in the array as values
def get_elements_count(array):
  count = {}
  for element in array:
    if element in count:
      count[element] += 1
    else:
      count[element] = 1
  return count

# Return true if a number is odd, false otherwise
def is_odd(num):
    return num % 2 != 0


from django.contrib import admin
from .models import Project_Status, Project, Skills, Worker_Status, Worker, Worker_Request

class Project_status_Admin(admin.ModelAdmin):
    readonly_fields = ('id', 'status')
    list_display = ['id', 'status']

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class Worker_status_Admin(admin.ModelAdmin):
    readonly_fields = ('id', 'status')
    list_display = ['id', 'status']

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class Project_Admin(admin.ModelAdmin):
    list_display = ['username', 'title', 'start_date', 'end_date', 'status', 'total_work_hours', 'point_per_hour']
    def save_model(self, request, instance, form, change):
        user = request.user
        instance = form.save(commit=False)
        instance.username = user
        instance.save()
        form.save_m2m()
        return instance

class Worker_Admin(admin.ModelAdmin):
    list_display = ['username', 'phone_number', 'status']
    def save_model(self, request, instance, form, change):
        user = request.user
        instance = form.save(commit=False)
        instance.username = user
        instance.save()
        form.save_m2m()
        return instance

admin.site.register(Skills)
admin.site.register(Project_Status, Project_status_Admin)
admin.site.register(Project, Project_Admin)
admin.site.register(Worker, Worker_Admin)
admin.site.register(Worker_Status, Worker_status_Admin)
admin.site.register(Worker_Request)
# admin.site.register(Worker_status, Worker_status_Admin)


#!/usr/bin/env python
from setuptools import setup

# django-static doesn't have a version but this setup.py does
VERSION = '1.5.6' # remember to match with django_static/__init__.py

import os
long_description = open(os.path.join(os.path.dirname(__file__),
                                     'README.rst')).read()

setup(
      name='django-static',
      version=VERSION,
      url='https://github.com/peterbe/django-static',
      download_url='https://pypi.python.org/pypi/django-static/',
      description='Template tags for better serving static files from templates in Django',
      long_description=long_description,
      author='Peter Bengtsson',
      author_email='mail@peterbe.com',
      platforms=['any'],
      license='BSD',
      packages=[
        'django_static',
        'django_static.templatetags',
        ],
      classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Application Frameworks',
        'Topic :: Software Development :: Libraries :: Python Modules',
        ],
)


# -*- coding: utf-8 -*-
import os
import ssl
import urlparse

from lib.requests_toolbelt.adapters import host_header_ssl
from lib import doh
from platformcode import logger, config
import requests

try:
    import _sqlite3 as sql
except:
    import sqlite3 as sql

db = os.path.join(config.get_data_path(), 'kod_db.sqlite')

class CustomSocket(ssl.SSLSocket):
    def __init__(self, *args, **kwargs):
        super(CustomSocket, self).__init__(*args, **kwargs)

class CustomContext(ssl.SSLContext):
    def __init__(self, protocol, hostname, *args, **kwargs):
        self.hostname = hostname
        super(CustomContext, self).__init__(protocol)

    def wrap_socket(self, sock, server_side=False,
                    do_handshake_on_connect=True,
                    suppress_ragged_eofs=True,
                    server_hostname=None):
        return CustomSocket(sock=sock, server_side=server_side,
                         do_handshake_on_connect=do_handshake_on_connect,
                         suppress_ragged_eofs=suppress_ragged_eofs,
                         server_hostname=self.hostname,
                         _context=self)

class CipherSuiteAdapter(host_header_ssl.HostHeaderSSLAdapter):

    def __init__(self, domain, CF=False, *args, **kwargs):
        self.conn = sql.connect(db)
        self.cur = self.conn.cursor()
        self.ssl_context = CustomContext(ssl.PROTOCOL_TLS if 'PROTOCOL_TLS' in ssl.__dict__ else ssl.PROTOCOL_SSLv3, domain)
        self.CF = CF  # if cloudscrape is in action
        self.cipherSuite = kwargs.pop('cipherSuite', ssl._DEFAULT_CIPHERS)

        super(CipherSuiteAdapter, self).__init__(**kwargs)

    def flushDns(self, request, domain, **kwargs):
        self.cur.execute('delete from dnscache where domain=?', (domain,))
        self.conn.commit()
        return self.send(request, flushedDns=True, **kwargs)

    def getIp(self, domain):
        ip = None
        try:
            self.cur.execute('select ip from dnscache where domain=?', (domain,))
            ip = self.cur.fetchall()[0][0]
            logger.info('Cache DNS: ' + domain + ' = ' + str(ip))
        except:
            pass
        if not ip:  # not cached
            try:
                ip = doh.query(domain)[0]
                logger.info('Query DoH: ' + domain + ' = ' + str(ip))
                self.writeToCache(domain, ip)
            except Exception:
                logger.error('Failed to resolve hostname, fallback to normal dns')
                import traceback
                logger.error(traceback.print_exc())
                ip = domain
        return ip

    def writeToCache(self, domain, ip):
        try:
            self.cur.execute('insert into dnscache values(?,?)', (domain, ip))
        except:
            self.cur.execute("""CREATE TABLE IF NOT EXISTS dnscache(
                    "domain"	TEXT NOT NULL UNIQUE,
                    "ip"	TEXT NOT NULL,
                    PRIMARY KEY("domain")
                );""")
        self.conn.commit()

    def init_poolmanager(self, *args, **kwargs):
        kwargs['ssl_context'] = self.ssl_context
        return super(CipherSuiteAdapter, self).init_poolmanager(*args, **kwargs)

    def proxy_manager_for(self, *args, **kwargs):
        kwargs['ssl_context'] = self.ssl_context
        return super(CipherSuiteAdapter, self).proxy_manager_for(*args, **kwargs)

    def send(self, request, flushedDns=False, **kwargs):
        try:
            parse = urlparse.urlparse(request.url)
        except:
            raise requests.exceptions.InvalidURL
        if parse.netloc:
            domain = parse.netloc
        else:
            raise requests.exceptions.URLRequired
        self.ssl_context = CustomContext(ssl.PROTOCOL_TLS if 'PROTOCOL_TLS' in ssl.__dict__ else ssl.PROTOCOL_SSLv3, domain)
        if self.CF:
            self.ssl_context.options |= (ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1)
        self.ssl_context.set_ciphers(self.cipherSuite)
        self.init_poolmanager(self._pool_connections, self._pool_maxsize, block=self._pool_block)
        ip = self.getIp(domain)

        realUrl = request.url

        if request.headers:
            request.headers["Host"] = domain
        else:
            request.headers = {"Host": domain}
        ret = None
        tryFlush = False

        parse = list(parse)
        parse[1] = ip
        request.url = urlparse.urlunparse(parse)
        try:
            ret = super(CipherSuiteAdapter, self).send(request, **kwargs)
        except Exception as e:
            logger.info('Request for ' + domain + ' with ip ' + ip + ' failed')
            logger.info(e)
            tryFlush = True
        if tryFlush and not flushedDns:  # re-request ips and update cache
            logger.info('Flushing dns cache for ' + domain)
            return self.flushDns(request, domain, **kwargs)
        ret.url = realUrl
        return ret


import numpy as np
import copy

class DynamicSizeRecarray:
    def __init__(self, recarray=None, dtype=None):
        """
        A dynamic version of numpy.core.records.recarray.
        Either provide an existing recarray 'recarray' or
        provide the 'dtype' to start with an empty recarray.

        Parameters
        ----------
        recarray : numpy.core.records.recarray, default=None
            The start of the dynamic recarray.
        dtype : list(tuple("key", "dtype_str")), default=None
            The dtype of the dynamic recarray.
        """
        self.size = 0
        if recarray is None and dtype == None:
            raise AttributeError("Requires either 'recarray' or 'dtype'.")
        if recarray is not None and dtype is not None:
            raise AttributeError(
                "Expected either one of 'recarray' or' dtype' to be 'None'"
            )

        if dtype:
            recarray = np.core.records.recarray(
                shape=0,
                dtype=dtype,
            )

        initial_capacity = np.max([2, len(recarray)])
        self.recarray = np.core.records.recarray(
            shape=initial_capacity,
            dtype=recarray.dtype,
        )
        self.append_recarray(recarray=recarray)

    def capacity(self):
        """
        Returns the capacity (in number of records) of the allocated memeory.
        This is the length of the internal recarray.
        """
        return len(self.recarray)

    def to_recarray(self):
        """
        Exports to a numpy.core.records.recarray.
        """
        out = np.core.records.recarray(
            shape=self.size,
            dtype=self.recarray.dtype,
        )
        out = self.recarray[0 : self.size]
        return out

    def append_record(self, record):
        """
        Append one record to the dynamic racarray.
        The size of the dynamic recarray will increase by one.

        Parameters
        ----------
        record : dict
            The values from the record-dict for the keys in the recarray will
            be appended to the recarray.
        """
        self._grow_if_needed(additional_size=1)
        for key in self.recarray.dtype.names:
            self.recarray[self.size][key] = record[key]
        self.size += 1

    def append_records(self, records):
        """
        Append the records to the dynamic racarray.
        The size of the dynamic recarray will increase by len(records).

        Parameters
        ----------
        record : list of dicts
            A list of records. Every record must have the keys of the internal,
            dynamic recarray.
        """
        for record in records:
            self.append_record(record=record)

    def append_recarray(self, recarray):
        """
        Append a recarray to the dynamic racarray.
        The size of the dynamic recarray will increase by len(recarray).

        Parameters
        ----------
        recarray : numpy.core.records.recarray
            This will be appended to the internal, dynamic recarray.
        """
        self._grow_if_needed(additional_size=len(recarray))
        start = self.size
        stop = start + len(recarray)
        self.recarray[start:stop] = recarray
        self.size += len(recarray)

    def _grow_if_needed(self, additional_size):
        assert additional_size >= 0
        current_capacity = self.capacity()
        required_size = self.size + additional_size

        if required_size > current_capacity:
            swp = copy.deepcopy(self.recarray)
            next_capacity = np.max([current_capacity * 2, required_size])
            self.recarray = np.core.records.recarray(
                shape=next_capacity,
                dtype=swp.dtype,
            )
            start = 0
            stop = self.size
            self.recarray[start:stop] = swp[0 : self.size]
            del swp

    def __len__(self):
        return self.size

    def __repr__(self):
        out = "{:s}(size={:d}, capacity={:d})".format(
            self.__class__.__name__,
            self.size,
            self.capacity(),
        )
        return out


import os
import sys
import commands

from subprocess import Popen, PIPE

mkarg = commands.mkarg

class ExecError(Exception):
    """Accessible attributes:
    command	executed command
    exitcode	non-zero exitcode returned by command
    output	error output returned by command
    """
    def __init__(self, command, exitcode, output=None):
        Exception.__init__(self, command, exitcode, output)

        self.command = command
        self.exitcode = exitcode
        self.output = output

    def __str__(self):
        str = "non-zero exitcode (%d) for command: %s" % (self.exitcode,
                                                          self.command)
        if self.output:
            str += "\n" + self.output
        return str

def fmt_command(command, *args):
    return command + " ".join([mkarg(arg) for arg in args])

def system(command, *args):
    """Executes <command> with <*args> -> None
    If command returns non-zero exitcode raises ExecError"""

    sys.stdout.flush()
    sys.stderr.flush()

    command = fmt_command(command, *args)
    error = os.system(command)
    if error:
        exitcode = os.WEXITSTATUS(error)
        raise ExecError(command, exitcode)

def getoutput(command, *args):
    """Executes <command> with <*args> -> output
    If command returns non-zero exitcode raises ExecError"""

    command = fmt_command(command, *args)
    error, output = commands.getstatusoutput(command)
    if error:
        exitcode = os.WEXITSTATUS(error)
        raise ExecError(command, exitcode, output)

    return output

def getoutput_popen(command, input=None):
    """Uses subprocess.Popen to execute <command>, piping <input> into stdin.
    If command returns non-zero exitcode raise ExecError.

    Return command output.
    """

    shell=False
    if isinstance(command, str):
        shell=True

    child = Popen(command, shell=shell, stdin=PIPE, stdout=PIPE, stderr=PIPE)

    errstr = None
    try:
        outstr, errstr = child.communicate(input)
    except OSError:
        pass

    errno = child.wait()
    if errstr is None:
        errstr = child.stderr.read()

    if errno != 0:
        raise ExecError(command, errno, errstr)

    return outstr


# From a list of measures, it returns an agregation of the coverage of each risk
class CoverageComputer:
    def __init__(self, measures_data):
        self.measures_data = measures_data

    def call(self):
        risks = self.__risks()
        risk_coverage = list(
            map(
                lambda risk: self.__risk_coverage(risk, self.measures_data),
                risks
              )
        )

        return dict(zip(risks, risk_coverage))

    def __risks(self):
        return list(map(
            lambda risk: risk["risk"],
            self.measures_data[0]["riskCoverage"]
        ))

    def __risk_coverage(self, risk, measures_data):
        measure_coverage_per_risk = list(map(
            lambda measure: self.__measure_coverage_per_risk(measure),
            measures_data
        ))

        sum_coverage = sum(map(
            lambda measure_coverage: measure_coverage[risk],
            measure_coverage_per_risk
        ))

        if sum_coverage > 100:
            return 100

        return sum_coverage

    def __measure_coverage_per_risk(self, measure):
        coverage_identifier_association =  list(map(
            lambda risk: (risk["risk"], risk["coverage"]),
            measure["riskCoverage"]
        ))

        return dict(coverage_identifier_association)


import matplotlib.pyplot as plt
import numpy as np
import math

def calc_polinom_S(A, B, X, x):
    S = 0
    for j in range(len(A)-1):
        if x >= X[j] and x <= X[j+1]:
            S = S + A[j] + B[j] * (x - X[j])
    return S

# graficul pentru determinarea graficului erorii de trunchiere
def plot_function(A, B, X, a, b, f):
    # plot-uim graficul
    # subdiviziuni
    v_func = np.linspace(a, b, 50)
    f2 = np.vectorize(f)
    plt.plot(v_func, f2(v_func), linestyle='-', linewidth=3)

    # x_grafic = np.linspace(a, b, 50)
    # x_grafic_poli = np.zeros(50)
    # for i in range(50):
    #     x_grafic_poli[i] = calc_polinom_S(A, B, X, v_func[i])

    # plt.plot(v_func, calc_polinom_S(A, B, X, v_func), linestyle='--', linewidth=3)

    plt.legend(['f(x)', 'polinom(x)'])  # Adauga legenda
    plt.axvline(0, c='black')  # Adauga axa OY
    plt.axhline(0, c='black')  # Adauga axa OX
    plt.xlabel('x')  # Label pentru axa OX
    plt.ylabel('y')  # Label pentru axa OY
    # Titlul figurii
    plt.title('Metoda lagrange')

    plt.show()  # Arata graficul

def interpolare_liniara(X, Y, N, a, b):
    # calculam coeficientii
    A = np.zeros((N, 1))
    B = np.zeros((N, 1))
    for j in range(len(Y) - 1):
        A[j] = Y[j]
        B[j] = (Y[j+1] - Y[j]) / (Y[j+1] - X[j])

    plot_function(A, B, X, a, b, f)

def f(x):
    return math.cos(2 * x) - 2 * math.sin(3 * x)

if __name__ == "__main__":
    N = 15
    a = (-1) * math.pi
    b = math.pi
    X = np.linspace(a, b, N+1)
    Y = np.zeros((N+1, 1))
    print(f"{X.shape[0]}")
    # il calculam pe y
    for i in range(len(X)):
        Y[i] = f(X[i])

    interpolare_liniara(X, Y, N, a, b)


import sys
import requests
import pandas as pd
from PyQt5.QtWidgets import QComboBox, QApplication, QWidget, QFileDialog, QLabel, QPushButton, QProgressBar, QTextEdit, QVBoxLayout, QHBoxLayout

class VINDecoder(QWidget):
    def __init__(self):
        super().__init__()

        # Set up the GUI
        self.setWindowTitle('VIN Decoder')
        self.setGeometry(100, 100, 800, 600)
        self.layout = QVBoxLayout()

        # Add a label for the file name
        self.file_label = QLabel('No file selected')
        self.layout.addWidget(self.file_label)

        # Add a button for selecting a file
        self.file_button = QPushButton('Select File')
        self.file_button.clicked.connect(self.select_file)
        self.layout.addWidget(self.file_button)

        # Add a label for the VIN column selection
        self.vin_label = QLabel('Select VIN Column:')
        self.layout.addWidget(self.vin_label)

        # Add a combo box for selecting the VIN column
        self.vin_column = QComboBox()
        self.layout.addWidget(self.vin_column)

        # Add a text edit for displaying the input file
        self.input_text = QTextEdit()
        self.input_text.setReadOnly(True)
        self.layout.addWidget(self.input_text)

        # Add a progress bar
        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0, 100)
        self.progress_bar.setValue(0)
        self.layout.addWidget(self.progress_bar)

        # Add a button for decoding the VINs
        self.decode_button = QPushButton('Decode')
        self.decode_button.clicked.connect(self.decode_vins)
        self.decode_button.setDisabled(True)
        self.layout.addWidget(self.decode_button)

        # Add the layout to the window
        self.setLayout(self.layout)

        self.show()

    def select_file(self):
        # Show a file dialog to select an Excel or CSV file
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getOpenFileName(self, 'Select File', '', 'Excel Files (*.xlsx);;CSV Files (*.csv)', options=options)

        # Update the file label and load the file into a Pandas DataFrame
        if file_name:
            self.file_label.setText(file_name)
            self.df = pd.read_excel(file_name) if file_name.endswith('.xlsx') else pd.read_csv(file_name)
            self.input_text.setPlainText(str(self.df.head()))
            self.vin_column.addItems(self.df.columns)
            self.decode_button.setEnabled(True)

    def decode_vins(self):
        # Get the VIN column from the DataFrame
        vin_col = self.df[self.vin_column.currentText()]

        # Split the VINs into batches of 50 and send to the NHTSA API
        batches = [vin_col[i:i+50] for i in range(0, len(vin_col), 50)]
        decoded_data = []
        for i, batch in enumerate(batches):
            response = requests.post('https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/',
                                     json={'format': 'json', 'data': batch.tolist()})
            if response.status_code == 200:
                data = response.json()['Results']
                for d in data:
                    decoded_data.append(d)
            else:
                print('Error:', response.status_code, response.text)
                break

            # Update the progress bar
            self.progress_bar.setValue((i + 1) * 100 // len(batches))

        # Create a new DataFrame with the decoded data and selected columns
        decoded_df = pd.DataFrame(decoded_data)
        selected_cols = ['VIN'] + [self.df.columns[i] for i in range(len(self.df.columns)) if self.df.columns[i] in decoded_df.columns]
        output_df = pd.merge(self.df[selected_cols], decoded_df, on='VIN')

        # Show a file dialog to select where to save the output file
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getSaveFileName(self, 'Save File', '', 'CSV Files (*.csv)', options=options)

        # Save the output file
        if file_name:
            output_df.to_csv(file_name, index=False)

        # Reset the progress bar
        self.progress_bar.setValue(0)

    def view_sheet(self):
        # Open the input file in the default application
        file_name = self.file_label.text()
        if file_name:
            import os
            os.startfile(file_name)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    decoder = VINDecoder()
    sys.exit(app.exec_())


'''TESTCASE
4 2
-
0 1
-
1 0
-
0 0
-
10 0
-
10 10
'''
from functools import reduce

def fprint(x):
    print('{:.4f}'.format(x))

def f(a, b):
    return reduce(int.__mul__, (i for i in range(a, b+1)), 1)

def A(n, m):
    if n and m:
        return f(n-m+1, n)
    return 0

def C(n, m):
    if m == 0:
        return 0
    return A(n, m) // f(2, m)

n, m = map(int, input().split())

if n == 0:
    fprint(0)
    fprint(0)
elif m == 0:
    fprint(n > 0)
    fprint(n > 1)
else:
    fprint((n/(n+m))**2)
    fprint(C(n, 2)/C(n+m, 2))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    project: rigamajig2
    file: deformation_widget.py
    author: masonsmigel
    date: 07/2022
    discription:

"""
# MAYA
import maya.cmds as cmds
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets

# RIGAMAJIG2
from rigamajig2.shared import common
from rigamajig2.ui.widgets import pathSelector, collapseableWidget
from rigamajig2.ui.builder_ui import constants
from rigamajig2.maya.builder.constants import SKINS, PSD, SHAPES, DEFORM_LAYERS
from rigamajig2.maya.rig import deformLayer
from rigamajig2.maya import skinCluster

class DeformationWidget(QtWidgets.QWidget):
    """ Deformation layout for the builder UI """

    def __init__(self, builder=None):
        """
       Constructor for the deformation widget
       :param builder: builder to connect to the ui
       """
        super(DeformationWidget, self).__init__()

        self.builder = builder

        self.createWidgets()
        self.createLayouts()
        self.createConnections()

    def createWidgets(self):
        """ Create Widgets"""
        self.mainCollapseableWidget = collapseableWidget.CollapsibleWidget('Deformations', addCheckbox=True)

        self.deformLayerPathSelector = pathSelector.PathSelector(
            "layers:",
            caption='Select a deformationLayer file',
            fileFilter=constants.JSON_FILTER,
            fileMode=1)

        self.loadDeformLayersButton = QtWidgets.QPushButton("Load Deform Layers")
        self.loadDeformLayersButton.setIcon(QtGui.QIcon(common.getIcon("loadDeformLayers.png")))
        self.saveDeformLayersButton = QtWidgets.QPushButton("Save Deform Layers")
        self.saveDeformLayersButton.setIcon(QtGui.QIcon(common.getIcon("saveDeformLayers.png")))

        self.addDeformLayersWidget = collapseableWidget.CollapsibleWidget('Add deformation Layers')
        self.addDeformLayersWidget.setHeaderBackground(constants.EDIT_BG_HEADER_COLOR)
        self.addDeformLayersWidget.setWidgetBackground(constants.EDIT_BG_WIDGET_COLOR)
        self.suffixLineEdit = QtWidgets.QLineEdit()
        self.suffixLineEdit.setPlaceholderText("suffix")

        self.combineMthodComboBox = QtWidgets.QComboBox()
        for item in deformLayer.CONNECTION_METHOD_LIST:
            self.combineMthodComboBox.addItem(item)
        self.addDeformLayerButton = QtWidgets.QPushButton("Add Deform Layer")
        self.connectToMainMeshButton = QtWidgets.QPushButton("Connect to Model")

        self.skinPathSelector = pathSelector.PathSelector(
            "skin:",
            caption="Select the skin weight folder",
            fileFilter=constants.JSON_FILTER,
            fileMode=2)
        self.loadAllSkinButton = QtWidgets.QPushButton("Load All Skins")
        self.loadAllSkinButton.setIcon(QtGui.QIcon(common.getIcon("loadSkincluster.png")))
        self.loadSingleSkinButton = QtWidgets.QPushButton("Load Skin")
        self.loadSingleSkinButton.setIcon(QtGui.QIcon(common.getIcon("loadSkincluster.png")))
        self.saveSkinsButton = QtWidgets.QPushButton("Save Skin")
        self.saveSkinsButton.setIcon(QtGui.QIcon(common.getIcon("saveSkincluster.png")))

        self.loadDeformLayersButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.saveDeformLayersButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.loadAllSkinButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.loadSingleSkinButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.saveSkinsButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)

        self.loadAllSkinButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)
        self.loadSingleSkinButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)
        self.saveSkinsButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)
        self.loadDeformLayersButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)
        self.saveDeformLayersButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)

        self.skinEditWidget = collapseableWidget.CollapsibleWidget('Edit Skin Cluster')
        self.skinEditWidget.setHeaderBackground(constants.EDIT_BG_HEADER_COLOR)
        self.skinEditWidget.setWidgetBackground(constants.EDIT_BG_WIDGET_COLOR)

        self.copySkinWeightsButton = QtWidgets.QPushButton("Copy Skin Weights and Influences")
        self.copySkinWeightsButton.setIcon(QtGui.QIcon(":copySkinWeight"))
        self.connectBpmsButton = QtWidgets.QPushButton("Connect BPMs on Skins")

        self.SHAPESPathSelector = pathSelector.PathSelector(
            "SHAPES:",
            caption="Select a SHAPES Node Setup",
            fileFilter=constants.MEL_FILTER,
            fileMode=2)
        self.saveSHAPESButton = QtWidgets.QPushButton("Save SHAPES Setup")
        self.saveSHAPESButton.setIcon(QtGui.QIcon(common.getIcon("saveShapesSetup.png")))
        self.loadSHAPESButton = QtWidgets.QPushButton("Load SHAPES Setup")
        self.loadSHAPESButton.setIcon(QtGui.QIcon(common.getIcon("loadShapesSetup.png")))

        self.saveSHAPESButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.saveSHAPESButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)
        self.loadSHAPESButton.setFixedHeight(constants.LARGE_BTN_HEIGHT)
        self.loadSHAPESButton.setIconSize(constants.LARGE_BTN_ICON_SIZE)

    def createLayouts(self):
        """ Create Layouts"""
        # setup the main layout.
        self.mainLayout = QtWidgets.QVBoxLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(0)

        deformLayerLayout = QtWidgets.QHBoxLayout()
        deformLayerLayout.setContentsMargins(0, 0, 0, 0)
        deformLayerLayout.setSpacing(4)
        deformLayerLayout.addWidget(self.loadDeformLayersButton)
        deformLayerLayout.addWidget(self.saveDeformLayersButton)

        # add the deformation layers back to the collapseable widget
        self.mainCollapseableWidget.addWidget(self.deformLayerPathSelector)
        self.mainCollapseableWidget.addLayout(deformLayerLayout)
        self.mainCollapseableWidget.addWidget(self.addDeformLayersWidget)

        addDeformLayersLayout = QtWidgets.QHBoxLayout()
        addDeformLayersLayout.addWidget(self.suffixLineEdit)
        addDeformLayersLayout.addWidget(self.combineMthodComboBox)
        addDeformLayersLayout.addWidget(self.addDeformLayerButton)

        self.addDeformLayersWidget.addLayout(addDeformLayersLayout)
        self.addDeformLayersWidget.addWidget(self.connectToMainMeshButton)
        self.addDeformLayersWidget.addSpacing(4)

        self.mainCollapseableWidget.addSpacing(10)

        skinButtonLayout = QtWidgets.QHBoxLayout()
        skinButtonLayout.setContentsMargins(0, 0, 0, 0)
        skinButtonLayout.setSpacing(4)
        skinButtonLayout.addWidget(self.loadAllSkinButton)
        skinButtonLayout.addWidget(self.loadSingleSkinButton)
        skinButtonLayout.addWidget(self.saveSkinsButton)

        # add the skin layers back to the collapseable widget
        self.mainCollapseableWidget.addWidget(self.skinPathSelector)
        self.mainCollapseableWidget.addLayout(skinButtonLayout)

        self.mainCollapseableWidget.addWidget(self.skinEditWidget)
        self.skinEditWidget.addWidget(self.copySkinWeightsButton)
        self.skinEditWidget.addWidget(self.connectBpmsButton)
        self.skinEditWidget.addSpacing(4)

        self.mainCollapseableWidget.addSpacing(10)
        self.mainCollapseableWidget.addWidget(self.SHAPESPathSelector)

        # SHAPES layout
        SHAPESButtonLayout = QtWidgets.QHBoxLayout()
        SHAPESButtonLayout.setContentsMargins(0, 0, 0, 0)
        SHAPESButtonLayout.setSpacing(4)
        SHAPESButtonLayout.addWidget(self.loadSHAPESButton)
        SHAPESButtonLayout.addWidget(self.saveSHAPESButton)
        self.mainCollapseableWidget.addLayout(SHAPESButtonLayout)

        # add the widget to the main layout
        self.mainLayout.addWidget(self.mainCollapseableWidget)

    def createConnections(self):
        """ Create Connections"""
        self.loadDeformLayersButton.clicked.connect(self.loadDeformLayers)
        self.saveDeformLayersButton.clicked.connect(self.saveDeformLayers)
        self.addDeformLayerButton.clicked.connect(self.addDeformLayer)
        self.connectToMainMeshButton.clicked.connect(self.connectDeformMesh)

        self.loadAllSkinButton.clicked.connect(self.loadAllSkins)
        self.loadSingleSkinButton.clicked.connect(self.loadSingleSkin)
        self.saveSkinsButton.clicked.connect(self.saveSkin)
        self.copySkinWeightsButton.clicked.connect(self.copySkinWeights)
        self.connectBpmsButton.clicked.connect(self.connectBindPreMatrix)
        self.saveSHAPESButton.clicked.connect(self.saveSHAPESData)
        self.loadSHAPESButton.clicked.connect(self.loadSHAPESData)

    def setBuilder(self, builder):
        """ Set a builder for intialize widget"""
        rigEnv = builder.getRigEnviornment()
        self.builder = builder
        self.deformLayerPathSelector.setRelativePath(rigEnv)
        self.skinPathSelector.setRelativePath(rigEnv)
        self.SHAPESPathSelector.setRelativePath(rigEnv)

        # update data within the rig
        deformLayerFile = self.builder.getRigData(self.builder.getRigFile(), DEFORM_LAYERS)
        self.deformLayerPathSelector.selectPath(deformLayerFile)

        skinFile = self.builder.getRigData(self.builder.getRigFile(), SKINS)
        self.skinPathSelector.selectPath(skinFile)

        SHAPESFile = self.builder.getRigData(self.builder.getRigFile(), SHAPES)
        self.SHAPESPathSelector.selectPath(SHAPESFile)

    def runWidget(self):
        """ Run this widget from the builder breakpoint runner"""
        self.loadDeformLayers()
        self.loadAllSkins()
        self.loadSHAPESData()

    @property
    def isChecked(self):
        """ Check it the widget is checked"""
        return self.mainCollapseableWidget.isChecked()

    # CONNECTIONS
    def loadDeformLayers(self):
        """ Save load pose reader setup from json using the builder """
        self.builder.loadDeformationLayers(self.deformLayerPathSelector.getPath())

    def saveDeformLayers(self):
        """ Save pose reader setup to json using the builder """
        self.builder.saveDeformationLayers(self.deformLayerPathSelector.getPath())

    def loadAllSkins(self):
        """Load all skin weights in the given folder"""
        self.builder.loadSkinWeights(self.skinPathSelector.getPath())

    def loadSingleSkin(self):
        """Load a single skin file"""
        from rigamajig2.maya.builder import deform
        path = cmds.fileDialog2(ds=2, cap="Select a skin file", ff=constants.JSON_FILTER, okc="Select",
                                dir=self.skinPathSelector.getPath())
        if path:
            deform.loadSingleSkin(path[0])

    def saveSkin(self):
        """Save the skin weights"""
        self.builder.saveSkinWeights(path=self.skinPathSelector.getPath())

    def copySkinWeights(self):
        """ Copy Skin weights"""
        src = cmds.ls(sl=True)[0]
        dst = cmds.ls(sl=True)[1:]
        skinCluster.copySkinClusterAndInfluences(src, dst)

    def connectBindPreMatrix(self):
        """
        Connect influence joints to their respective bindPreMatrix
        """
        for mesh in cmds.ls(sl=True):
            sc = skinCluster.getSkinCluster(mesh)
            skinCluster.connectExistingBPMs(sc)

    def loadSHAPESData(self):
        self.builder.loadSHAPESData(self.SHAPESPathSelector.getPath())

    def saveSHAPESData(self):
        self.builder.saveSHAPESData(self.SHAPESPathSelector.getPath())

    def addDeformLayer(self):
        """
        add a new deformation layer to the selected object
        :return:
        """
        suffix = self.suffixLineEdit.text()
        connectionMethod = self.combineMthodComboBox.currentText()

        # create a new deformation layer
        for node in cmds.ls(sl=True):
            layers = deformLayer.DeformLayer(node)
            layers.createDeformLayer(suffix=suffix, connectionMethod=connectionMethod)

    def connectDeformMesh(self):
        """
        Connect deformation layers to the main deform layer
        :return:
        """
        for s in cmds.ls(sl=True):
            layer = deformLayer.DeformLayer(s)
            layer.connectToModel()


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

#from __future__ import unicode_literals
from ds import DataStorage

import os
import shutil
import subprocess
import urllib
import imghdr
from string import Template
import pprint

from urllib import FancyURLopener
class MyOpener(FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

class States():
    WAITING = "Waiting..."
    LOADING_REC = "Loading recipes from storage"
    LOADING_TEMP = "Loading templates"
    TEMPLATING = "Templating "
    GENERATING = "Running generation"
    DONE = "Done"
    PAGES_PREP = "Preparing page %d (country: %s)"

symbol_mapping = {
    u"°": u"$^\circ$",
    u"¼": u"1/4",
    u"⁄": u"/",
    u"½": u"1/2",
    u"¾": u"3/4",
    u"–": u"-",
    u"—": u"-",
    u"×": u"$\\times$",
    u"’": u"'",
    u"‘": u"'",
    u"“": u"\"",
    u"”": u"\"",
    u"&": u"\&"

}

class Generator(object):
    output_file = "tex/final.pdf"
    gen_cmd = "pdflatex"
    template_page = "tex/page.tex"
    template_main = "tex/main.tex"
    state = States.WAITING
    def __init__(self):
        self.ds = DataStorage()
        self.pages = []

    def bckp_current(self):
        if os.path.exists(self.output_file):
            shutil.copyfile(self.output_file, "%s.bckp" % self.output_file)

    def generate(self, book_uuid):
        self.state = States.WAITING
        self.bckp_current()
        self.state = States.LOADING_REC
        countries_content = self.ds.get_countries_content(book_uuid)
        self.pages = self.prep_pages(countries_content)
#        with open("tex/blah.tex", "w") as fp:
#            p = "\n\\newpage\n".join(self.pages)
#            fp.write(p)

        self.state = States.TEMPLATING
        book = self.template_book({u"PAGE": "\n\\newpage\n".join(self.pages)})
        with open("tex/final.tex", "w") as fp:
            fp.write(book)

        self.state = States.GENERATING
        self.call_cmd("tex/final.tex")
        self.state = States.DONE

    def compress(self, path):
        resize=500
        quality=30
        cmd = ["convert", path,  "-units", "PixelsPerInch", "-density", "300", "-resize", str(resize), "-quality", str(quality), path]
        self.call_cmd(None, cmd)

    def call_cmd(self, path, cmd=None):
        if not cmd:
            cmd = [self.gen_cmd, "-output-directory=tex/", "-interaction=nonstopmode",  path]
        print("Calling %s" % " ".join(cmd))
        subprocess.call(cmd)

    def template_book(self, data):
        with open(self.template_main, "r") as fp:
            template = fp.read()

        t = Template(template)

        result = t.substitute(data)
        return result

    def template_recipe(self, data):
        with open(self.template_page, "r") as fp:
            template = self.unicodize(fp.read())

        t = Template(template)
        result = t.substitute(data)
        return self.unicodize(result)

    def download_image(self, image_uri, image_name):
        if "." in image_uri[-4:]:
            image_name = "%s%s" % (image_name[0:-3], image_uri[-3:])

        dest = "tex/images"
        if not os.path.isdir(dest):
            os.mkdir(dest)
        path = os.path.join("tex/images", image_name).replace(" ","-").lower()

        #urllib.urlretrieve(image_uri, path)
        #myopener = MyOpener()
        #myopener.retrieve(image_uri, path)
        #resource = urllib.urlopen(image_uri)
        #with open(path, "wb") as fp:
            #fp.write(resource.read())

        what = imghdr.what(path)
        if what == "jpeg":
            what = "jpg"
        elif what == None:
            what = "jpg"

        if what != path[-3:0]:
            new_path = "%s%s" % (path[0:-3], what)
            os.rename(path, new_path)
            path = new_path

        self.compress(path)
        return path

    def prep_pages(self, content):
        pages = []
        page_num = 1
        for country in content:
            data = {}

            i = 0
            for recipe in country["recipes"]:
                #if page_num <75 or page_num > 85:
                #    page_num += 1
                #    continue
                    #break
                self.state = States.PAGES_PREP % (page_num, country["country"])
                print(self.state)

                data.update(recipe)
                data["country"] = self.replace_symbols(country["country"])
                data["country_title"] = self.prep_country(country["country"], i)
                data["image_uri"] = self.download_image(recipe["image"]["uri"], "%s%d.jpg" % (country["country"], i))
                data["image_source_uri"] = recipe["image"]["source_uri"]
                data["ingredients"] = self.prep_ingredients(recipe["ingredients"])
                data["directions"] = self.prep_directions(recipe["directions"])
                data["description"] = self.replace_symbols(recipe["description"])
                data["title"] = self.replace_symbols(recipe["title"])

                for k, v in data.iteritems():
                    data[k] = self.unicodize(v)

                page = self.template_recipe(data)
                pages.append(self.unicodize(page))
#                with open("tex/blah%s.tex" % page_num, "w") as fp:
#                    fp.write(page)
                i+=1
                page_num += 1
        return pages

    def unicodize(self, data):
        tmp = data
        try:
            if hasattr(data, 'decode'):
                tmp = str(data)
        except (UnicodeEncodeError, UnicodeDecodeError) as e:
            tmp = str(data.encode('utf-8')) #data.decode("utf-8")

        return tmp

    def prep_country(self, country, i):
        if i == 0:
            return "\section{%s}" % country
        else:
            return ""

    def prep_ingredients(self, ingredients):
        result = []

        for ing in ingredients:
            title = False
            if ing.startswith("#"):
                ing = "\\textbf{%s}" % ing[1:]
                title = True

            ing = self.replace_symbols(ing)
            #print("%s %s" % (ing, type(ing)))

            if title and len(result)>0:
                result[-1] = result[-1][:-2]+"\n\n"
            result.append("%s %s%s" % ("$\\bullet$" if not title else "", ing, "\\\\*" if title else "\\\\"))

        return "\n".join(result)

    def prep_directions(self, directions):
        result = []
        dir_list = directions.split("\n")
        for direction in dir_list:
            if direction.startswith(u"§"):
                direction = direction[1:]

            direction = direction.strip()
            if len(direction) == 0:
                continue

            if direction[-1:] != ".":
                direction += "."
            if len(direction) > 0:
                direction = self.replace_symbols(direction)

            if direction.startswith("#"):
                dash = direction.find(" -")
                direction = u"\\textbf{%s}%s" % (direction[1:dash], direction[dash:])

            result.append(direction)

        return "\n\n".join(result)

    def replace_symbols(self, text):
        for frm, to in symbol_mapping.iteritems():
            text = text.replace(frm, to)

        return text


import pytest

from api.base.settings.defaults import API_BASE
from osf.models.metaschema import FileMetadataSchema
from osf_tests.factories import (
    AuthUserFactory,
)

@pytest.mark.django_db
class TestFileMetadataSchemaList:

    def test_schema_list_crud(self, app):

        user = AuthUserFactory()
        url = '/{}schemas/files/'.format(API_BASE)

        # test_authenticated_user_can_view_schemas
        res = app.get(url, auth=user.auth)
        assert res.status_code == 200
        assert (len(res.json['data']) == FileMetadataSchema.objects.filter(active=True).count())

        # test_cannot_create_schemas
        res = app.post_json_api(url, auth=user.auth, expect_errors=True)
        assert res.status_code == 405

        # test_unauthenticated_user_can_view_schemas
        res = app.get(url)
        assert res.status_code == 200


# Clustering(隠れマルコフモデル)

import tensorflow_probability as tfp #regression と classificationと違うモジュール
import tensorflow as tf

"""
Cold days are encoded by a 0 and hot days are encoded by a 1.

The first day in our sequence has an 80% chance of being cold.

A cold day has a 30% chance of being followed by a hot day.

A hot day has a 20% chance of being followed by a cold day.

On each day the temperature is normally distributed with mean
and standard deviation 0 and 5 on a cold day and mean and standard
deviation 15 and 10 on a hot day.
"""

#今日の天気から明日の天気を予想する。隠れマルコフモデル
#昨日以降のデータは関係ない。

#ショートカット作成
tfd = tfp.distributions

#モデル設定
initial_distribution = tfd.Categorical(probs=[0.2, 0.8]) #初日20%で寒くて、80%で暑い
transition_distribution = tfd.Categorical(probs=[[0.5, 0.5],
                                                 [0.2, 0.8]])
# 今日が寒い日なら、50%で明日も寒い。今日が暑い日なら20％で明日は寒い。
observation_distribution = tfd.Normal(loc=[0., 15.], scale=[15., 10.])  # refer to point 5 above
#寒い日の分布は平均0度、標準偏差15、暑い日の分布は平均15度、標準偏差10

#モデル適用
model = tfd.HiddenMarkovModel(
    initial_distribution=initial_distribution,
    transition_distribution=transition_distribution,
    observation_distribution=observation_distribution,
    num_steps=7)
# num_steps=7は7日先まで予測するということ

# 場合分けの確立の平均値をとって、明日以降の7日の気温を予測
mean = model.mean()
print(mean.numpy())