import re
import matplotlib.pyplot as plt
from keras.layers import Embedding, LSTM, Dense, Dropout
from keras.models import Sequential
from sklearn.metrics import classification_report
from transformers import DistilBertModel, DistilBertConfig, DistilBertTokenizer, AutoTokenizer, AutoModel
import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.preprocessing import LabelEncoder
from keras.utils.np_utils import to_categorical

def clean(text):
    REPLACE_BY_SPACE_RE = re.compile('[/(){}\[\]\|@,;]')
    text1 = text.replace('"', ' ')
    text1 = REPLACE_BY_SPACE_RE.sub('', text1)
    text1 = text1.replace('  ', ' ')
    text1 = text1.strip()
    return text1

def init_BERT():
    # Initializing a DistilBERT configuration
    configuration = DistilBertConfig()

    # Initializing a model from the configuration
    model = DistilBertModel(configuration)

    # Accessing the model configuration
    configuration = model.config
    configuration.output_hidden_states = True

    tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-multilingual-cased')
    model = DistilBertModel.from_pretrained('distilbert-base-multilingual-cased', return_dict=True)
    # tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
    # model = AutoModel.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
    return model, tokenizer

def get_average_embedding(input_embeddings):
    temp = input_embeddings[0].detach().numpy()
    return np.mean(temp, axis=0)

def get_BERT_embedding(input_seq, **kwargs):
    try:
        inputs = tokenizer(input_seq, return_tensors="pt")
        outputs = model(**inputs)
        last_hidden_states = outputs.last_hidden_state
        if kwargs.get("avg") == 'yes':
            return get_average_embedding(last_hidden_states)
        return last_hidden_states[0][0].detach().numpy()
    except:
        print("Error")

def make_BERT_embbedings_column():
    df = pd.read_csv('mtsamples-expanded-min.csv')
    df = df.dropna()
    print(df.shape)
    df = df[df.medical_specialty != 'SOAP / Chart / Progress Notes']
    df = df[df.medical_specialty != 'Consult - History and Phy.']
    df = df[df.medical_specialty != 'Surgery']
    df = df[df.medical_specialty != 'Sleep Medicine']
    df = df[df.medical_specialty != 'metabolics / Gastroenterology']
    df = df[df.medical_specialty != 'Office Notes']
    df = df[df.medical_specialty != 'Lab Medicine - Pathology']
    df = df[df.medical_specialty != 'IME-QME-Work Comp etc.']
    df = df[df.medical_specialty != 'Hospice - Palliative Care']
    df = df[df.medical_specialty != 'Emergency Room Reports']
    df = df[df.medical_specialty != 'Discharge Summary']
    df = df[df.medical_specialty != 'Diets and Nutritions']
    df = df[df.medical_specialty != 'Cosmetic / Plastic Surgery']
    df = df[df.medical_specialty != 'Chiropractic']
    df = df[df.medical_specialty != 'Letters']
    df = df[df.medical_specialty != 'Physical Medicine - Rehab']
    df = df[df.medical_specialty != 'Speech - Language']

    tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-multilingual-cased')
    model = DistilBertModel.from_pretrained('distilbert-base-multilingual-cased', return_dict=True)

    inputs = tokenizer("Ovo je kosa linija.", return_tensors="pt")
    outputs = model(**inputs)
    last_hidden_state = outputs.last_hidden_state

    print(last_hidden_state.shape)
    print(get_average_embedding(last_hidden_state).shape)

    df['embedding'] = df.apply(lambda row: get_BERT_embedding(row['keywords']), axis=1)
    # za operacije na redovima mora da se doda parametar axis=1
    df.to_csv('train_BERT_embed.csv')
    return df

# needed for converting string from loaded df to floats (numpy array)
def convert_embbedings_to_float(df):
    x = []
    for str_emb in df['embedding']:
        if isinstance(str_emb, str):
            str_emb = str_emb.replace('\'', '')
            str_emb = str_emb.replace('[', '')
            str_emb = str_emb.replace(']', '')
            str_emb = str_emb.replace('\n', '')
            vals = str_emb.split()
            # val = list(map(float, vals))
            val = [float(x) for x in vals]
            x.append(val)
        else:
            x.append(str_emb)
    return np.array(x)

def test_clf(clf):
    val_df = pd.read_csv('mtsamples-val.csv')
    val_df = val_df.dropna()
    val_df['keywords'] = val_df['keywords'].apply(clean)
    print(val_df['keywords'])
    val_df['embedding'] = val_df.apply(lambda row: get_BERT_embedding(row['keywords']), axis=1)

    val_x = [row for row in val_df['embedding']]

    result = clf.predict(val_x)

    for i in range(len(result)):
        # print(val_df.iloc[i]['Conditions'], " ---> ", result[i])
        print(val_df.iloc[i]['keywords'], " ---> ", result[i])

    for i in range(len(result)):
        val_df.loc[i, 'Prediction'] = result[i]

    del val_df['embedding']
    # header = ["NCTId", "ShortTitle", "Conditions", "Keywords", "Prediction"]
    header = ["keywords", "medical_specialty", "Prediction"]
    val_df.to_csv("medicalSpecialityBERT.csv", columns=header, index=False)

    val_y = val_df['medical_specialty'].dropna()
    print(classification_report(val_y, result))

model, tokenizer = init_BERT()

try:
    df = pd.read_csv('train_BERT_embed.csv')
    print("Loaded dataset with BERT embbedings...")
except:
    print("Making new embbedings...")
    df = make_BERT_embbedings_column()

# clf_svm = svm.SVC(kernel='linear')
# print(df['medical_specialty'].value_counts())

# labels encoding
target = df['medical_specialty'].values.tolist()
label_encoder = LabelEncoder()
df['medical_specialty_label'] = label_encoder.fit_transform(target)
n_classes = len(label_encoder.classes_)
print(n_classes)

msk = np.random.rand(len(df)) < 0.8
train_df = df[msk]
test_df = df[~msk]

train_x = convert_embbedings_to_float(train_df)
test_x = convert_embbedings_to_float(test_df)
train_y = train_df['medical_specialty_label']
test_y = test_df['medical_specialty_label']
train_labels = train_y.values
test_labels = test_y.values

Y_train = to_categorical(train_labels)
Y_test = to_categorical(test_labels)

print("Before reshape:")
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
print(test_y.shape)

# reshaping for LSTM
# (NumberOfExamples, TimeSteps, FeaturesPerStep)
# mozda ipak .reshape(train_x.shape[0], train_x.shape[1], 1)
train_x = train_x.reshape(train_x.shape[0], 1, 768)
test_x = test_x.reshape(test_x.shape[0], 1, 768)

print("After reshape:")
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
print(test_y.shape)

# clf_svm.fit(train_x, train_y)
# print(classification_report(test_y, clf_svm.predict(test_x)))
# test_clf(clf_svm)

# NEURAL NETWORK
model = Sequential()
# define LSTM model
model.add(LSTM(64, return_sequences=False, input_shape=(1, 768)))
# mozda relu
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.2))
model.add(Dense(n_classes, activation='softmax'))
# model.add(Dense(n_classes, activation='sigmoid'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

epochs = 30
hist = model.fit(train_x, Y_train, batch_size=32, epochs=epochs,
                 validation_data=(test_x, Y_test))

x = range(epochs)

plt.plot(x, hist.history['accuracy'], label='train')
plt.plot(x, hist.history['val_accuracy'], label='validation')
plt.title('Accuracy')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()

print((np.expand_dims(test_x[0], axis=0)).shape)
n = 5
predictions = model.predict(test_x[0:n + 1])
max_predictions = []

for i in range(n):
    print(test_df.iloc[i]["keywords"])
    max_predictions.append(np.argmax(predictions[i], axis=0))

print(max_predictions)

print(label_encoder.inverse_transform(max_predictions))


import tkinter as tk

from tkinter import *
from bing_image_downloader.downloader import download

window = tk.Tk()
window.geometry('600x500')
window.title("Dataset maker")
window.config(padx=10, pady=10)

f = Frame(window)

xscrollbar = Scrollbar(f, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=N+S+E+W)

yscrollbar = Scrollbar(f)
yscrollbar.grid(row=0, column=1, sticky=N+S+E+W)

text = Text(f, wrap=NONE,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set)
text.grid(row=0, column=0)

xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

title_label = tk.Label(window, text="Dataset Creator")
title_label.config(font=("Lobster", 34))
title_label.pack(padx=10, pady=10)

def title_infomration():
    inp = inptext.get()
    name_output_dir = output_box.get()
    query_string = inp
    output_dir = name_output_dir
    try:
        download(query_string, limit=100, output_dir=output_dir, adult_filter_off=True, force_replace=False, timeout=100,
                    verbose=True)
        lbl = tk.label(window, text='The images are downloading.')
        lbl.pack()
    except Exception as error:
        lbl = tk.label(window, text=f'{error} has occured.')
        lbl.pack()

names = Label(window, text='Name of serach term: ')
names.pack()
inptext = Entry(window, fg='red')
inptext.pack()

outputt = Label(window, text='Name of output directory: ')
outputt.pack()
output_box = Entry(window)
output_box.pack()

downloadButton = tk.Button(window,
                        text = "Downloads",
                        command = title_infomration)
downloadButton.pack()

lbl = tk.Label(window, text = "")
lbl.pack()

window.mainloop()


from setuptools import setup, find_packages
from setuptools.command.install import install as _install

__version__ = '0.4.0'

class NLTKInstall(_install):
    def run(self):
        _install.do_egg_install(self)

        import nltk
        nltk.download('averaged_perceptron_tagger')

setup(
    cmdclass={'install': NLTKInstall},
    name='Code base analizer',
    packages=find_packages(exclude=['tests']),
    include_package_data=True,
    version=__version__,
    install_requires=['nltk'],
    setup_requires=['nltk'],

    description='Module displays most popular words found in your codebase.',
    url='https://github.com/Grin941/codebase_analizer',
    licence='MIT',
    author='Grinenko Alexander',
    author_email='labamifi@gmail.com',
    classifiers=[
        'Development Status :: 1 - Planning',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Operating System :: MacOS',
        'Operating System :: POSIX :: Linux',
        'Operating System :: Microsoft :: Windows',
        'Programming Language :: Python :: 3 :: Only',
        'Programming Language :: Python :: Implementation :: CPython',
        'Topic :: Utilities',
    ],
    entry_points={
        'console_scripts': ['analize_codebase=bin.analize_codebase:main']
    },
)


import json

data = {"test1": 1, "test2": 2, "test3": 3, "new_key": 4} #assigns data to be used for overriding

class jsonfile(object):
    """docstring for jsonfile"""
    def __init__(self, file): #file is an actual argument
        super(jsonfile, self).__init__() #idk what this does but i feel like it might break if i remove
        self.file = file #makes the argument file a class var

        try:
            with open(file) as file: #attempts to load the file that was inputted
                self.data = file_contents = json.load(file) # parses the JSON data in the file and returns a dictionary assigned to class var data
        except: #if the file doesnt exist yet
            open(file, "x") # creates the file
            print(f'{file} was not found, creating...') #flavortext
            self.data = {} #still makes sure data exists to not error out

    def write(self, input): #overrides the json with input
        with open(self.file, "w") as file: #opens up the file for writing
            file.write(json.dumps(input, indent=4)) # formats and beautifies the JSON as a string and overwrites the file contents with it
        self.data = input #updates the data var to make sure its uptodate

foo = jsonfile('file.json')

print(foo.data)
foo.write(data)
print(foo.data)
print(foo.file)


from collections import deque
def count(n):
    b = 1
    res = []
    while n != 0:
        yield n, b
        n -= 1
        b += 1
        if True:
            pass

def test():
    n = 0
    while True:
        if n != 1:
            yield n
        n += 1
def search(list):
    a = deque(maxlen=3)
    for i in list:
        yield i, a
    a.append(i)
a = search([1, 2, 3, 4, 5, 6])
for i, j in a:
    for each in j:
        print(each)
for i in range(5):
    print(i)

print(count(2))
for i, j in count(5):
    print(i, "===>", j)


import hashlib
import logging
from playwright.sync_api import sync_playwright
from sqlalchemy.orm import Session

from app.config import settings
from app.logic.image import compress_image
from db.models.screenshot import Screenshot

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

MAX_LINKS_TO_FOLLOW = settings.max_links_to_follow

def calculate_checksum(image_path):
    with open(image_path, "rb") as image_file:
        file_contents = image_file.read()
        return hashlib.sha256(file_contents).hexdigest()

def add_screenshot_to_db(unique_id, image_path, file_name, image_url: str, db):
    # Adjust the image_path to point to the compressed file
    compressed_image_path = image_path + ".jpg"

    checksum = calculate_checksum(compressed_image_path)
    new_screenshot = Screenshot(
        unique_id=unique_id,
        image_path=compressed_image_path,
        checksum=checksum,
        file_name=file_name + ".jpg",
        image_url=image_url,
    )
    db.add(new_screenshot)
    db.commit()
    return new_screenshot

def take_screenshot(browser, url, path):
    try:
        page = browser.new_page()
        page.goto(url)
        page.screenshot(path=path)
        page.close()

        # Compress the image after taking the screenshot
        compress_image(path)

        # Return the new path with the compressed file extension
        return path + ".jpg"
    except Exception as e:
        logger.error(f"Error taking screenshot of {url}: {e}")
        return None

def crawl_and_screenshot(start_url, number_of_links, unique_id, db: Session):
    number_of_links = min(number_of_links, MAX_LINKS_TO_FOLLOW)
    if number_of_links < 1:
        raise ValueError(
            f"Number of links to follow must be greater than 0, got {number_of_links}"
        )

    if number_of_links > MAX_LINKS_TO_FOLLOW:
        raise ValueError(
            f"Number of links to follow must be less than {MAX_LINKS_TO_FOLLOW}, got {number_of_links}"
        )

    if not start_url.startswith("http"):
        raise ValueError(f"Start URL must start with http, got {start_url}")

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)

        screenshots = []
        initial_path = take_screenshot(
            browser, start_url, f"screenshots/{unique_id}_0.png"
        )
        if initial_path:
            screenshots.append(
                add_screenshot_to_db(
                    unique_id, initial_path, f"{unique_id}_0.png", start_url, db
                )
            )

        page = browser.new_page()
        page.goto(start_url)
        links = page.evaluate(
            "() => Array.from(document.querySelectorAll('a')).map(a => a.href);"
        )
        page.close()
        valid_links = list(set(filter(lambda link: link.startswith("http"), links)))
        followed_links = valid_links[:number_of_links]

        for index, url in enumerate(followed_links, start=1):
            path = f"screenshots/{unique_id}_{index}.png"
            screenshot = take_screenshot(browser, url, path)
            if screenshot:
                screenshots.append(
                    add_screenshot_to_db(
                        unique_id, screenshot, f"{unique_id}_{index}.png", url, db
                    )
                )

        browser.close()
        return [s.image_path for s in screenshots if s is not None]


from math import sqrt, sin, cos

from PIL import Image as PillowImage
from basics import Vector
from numpy import array, uint8

from class_lib.color import BLACK
from class_lib.coordinate_system import CoordinateSystem
from class_lib.light import Ray
from class_lib.useful_functions import reflected_vector
from globals import MAX_RECURSION_COUNTER

class Image:
    """2D-Array of color pixels"""

    def __init__(self, height=320, width=200):
        self.__width = width
        self.__height = height
        self.__pixels = [[None for _ in range(width)] for _ in range(height)]

    def get_pixel(self, row, column):
        """Gets pixel color from given row and column"""
        pix = self.__pixels[row][column]
        if pix:
            return pix
        return BLACK  # Default color returned if pixel has not been set

    def set_pixel(self, row, column, color):
        """Sets pixel to given color"""
        self.__pixels[row][column] = color

    def as_pillow_image(self):
        new_pixel_map = [[self.get_pixel(i, j).as_int_tuple() for j in range(self.__width)] for i in
                         range(self.__height)]
        pixel_array = array(new_pixel_map, dtype=uint8)
        return PillowImage.fromarray(pixel_array)

    def save_as_png(self, file_name):
        """Save image as png file"""
        new_image = self.as_pillow_image()
        new_image.save(file_name)

    def save_as_ppm(self, file_name):
        """Save image as PPM file"""
        with open(file_name, 'w') as img_file:
            # Write header
            # Indicate that it is a ppm file, and give width and height
            img_file.write(f'P3 {self.__width} {self.__height}\n')
            # Maximum value
            img_file.write('255\n')
            # Write values
            for i in range(self.__height):
                for j in range(self.__width):
                    img_file.write(self.get_pixel(i, j).ppm_string() + '\t')
                img_file.write('\n')

class Camera:
    """Camera positioned in 3d space"""

    def __init__(self, resolution=(20, 30), position=Vector(10, 0, 0), direction=Vector(-1, 0, 0), zoom=1,
                 tilt_angle=0):
        """Initialize new camera. Attention: Resolution is given like a matrix = height, width"""
        self.__resolution = resolution
        self.__position = position
        self.__direction = direction.unit
        self.__zoom = zoom
        self.__tilt_angle = tilt_angle

        # Auxiliary variables
        r_ver = self.__resolution[0] - 1
        r_hor = self.__resolution[1] - 1
        discriminant = r_ver * r_ver + r_hor * r_hor

        delta = 1 / (self.__zoom * sqrt(discriminant)) if discriminant > 0 else 1

        i_prime, j_prime, k_prime = CoordinateSystem.unit_base_axis(self.__direction, self.__tilt_angle)

        self.v_vertical = -delta * k_prime
        self.v_horizontal = -delta * j_prime
        self.offset_vertical = r_ver / 2
        self.offset_horizontal = r_hor / 2

    @property
    def image_height(self):
        return self.__resolution[0]

    @property
    def image_width(self):
        return self.__resolution[1]

    def __pixel_pos(self, row, column) -> Vector:
        """Pixel position given its row and column"""
        return self.__position + self.__direction + (row - self.offset_vertical) * self.v_vertical + (
                column - self.offset_horizontal) * self.v_horizontal

    def get_ray(self, row, column):
        """Ray from camera through given pixel"""
        p0 = self.__position
        direction = self.__pixel_pos(row, column) - self.__position
        return Ray(p0, direction)

class Scene:
    """Collection of objects, light sources, and a camera"""

    def __init__(self, camera, objects, illumination):
        self.__camera = camera
        self.__objects = objects
        self.__background_color = BLACK
        self.__illumination = illumination

    def ray_is_obstructed(self, ray, light_source_distance):
        """
        Returns true if there is an object obstructing the light source and causing a shadow
        """
        for obj in self.__objects:
            distance = obj.intersection_distance(ray)
            if distance and distance <= light_source_distance:
                return True
        return False

    def __nearest_object_hit_by_ray(self, ray):
        """
        Find closest object which intersects ray
        Returns object itself, and its distance to ray origin
        """
        save_object_index = None
        minimum_distance = -1
        for obj_index, obj in enumerate(self.__objects):
            distance = obj.intersection_distance(ray)
            if not distance:
                continue
            if distance < minimum_distance or minimum_distance < 0:
                minimum_distance = distance
                save_object_index = obj_index
        if save_object_index is not None:
            return self.__objects[save_object_index], minimum_distance
        return None, None

    def render_image(self):
        """Produce image"""
        image = Image(self.__camera.image_height, self.__camera.image_width)

        # Loop through all pixels
        # TODO: Split up this loop between cores, to make program more efficient.
        #  Or should I actually create different images, and then merge them at the end?
        for row in range(self.__camera.image_height):
            if self.__camera.image_height > 200 and (row + 1) % 50 == 0:
                print(f"Rendering row {row + 1}/{self.__camera.image_height}")  # To give an idea of the time left
            for col in range(self.__camera.image_width):
                ray = self.__camera.get_ray(row, col)
                pix_color = self.ray_trace(ray)
                image.set_pixel(row, col, pix_color)
        print("Done rendering.")
        return image

    def __get_ambient_light(self, chip):
        return chip.material.ambient_light_reflectivity ** self.__illumination.ambient_light.intensity

    @staticmethod
    def __get_diffuse_light(chip, ray_to_light_source, light_source):
        diffuse_multiplier = abs(ray_to_light_source.direction * chip.normal)
        diffuse_multiplier *= light_source.attenuator_by_distance_sq(chip.position)
        return diffuse_multiplier * chip.material.diffuse_light_reflectivity ** light_source.intensity

    @staticmethod
    def __get_phong_blinn_light(chip, incoming_ray, ray_to_light_source, light_source):
        h = (- incoming_ray.direction + ray_to_light_source.direction).unit
        r = reflected_vector(ray_to_light_source.direction, chip.normal)
        specular_multiplier = max(h * r, 0)

        specular_multiplier = chip.material.specular_multiplier * (
                specular_multiplier ** chip.material.specular_coefficient)

        specular_multiplier *= light_source.attenuator_by_distance_sq(chip.position)
        return specular_multiplier * light_source.intensity

    @staticmethod
    def __get_refracted_ray(chip, incoming_ray, ray_is_coming_from_outside):
        alpha = 1 / chip.material.refractive_index if ray_is_coming_from_outside else chip.material.refractive_index
        aux = alpha * incoming_ray.direction * chip.normal
        discriminant = aux * aux + 1 - alpha * alpha
        if discriminant < 0:
            return None
        beta = -aux - sqrt(discriminant) if ray_is_coming_from_outside else -aux + sqrt(discriminant)
        new_direction = alpha * incoming_ray.direction + beta * chip.normal
        return Ray(chip.position, new_direction)

    def color_at(self, chip, incoming_ray, recursion_depth):
        """Find color at given point of the scene"""
        color = BLACK
        if recursion_depth >= MAX_RECURSION_COUNTER:
            return color
        # Ambient light:
        color += self.__get_ambient_light(chip)

        # Light sources:
        for light_source in self.__illumination.light_sources:
            new_ray = light_source.ray_to_light_source(chip.position)
            # Check if light source is shining on the right side of the surface.
            cos_incoming = incoming_ray.direction * chip.normal
            cos_new_ray = new_ray.direction * chip.normal
            if cos_incoming * cos_new_ray > 0:
                # No good. They should have opposite signs, to indicate that the light is hitting the same side
                # of the surface that the camera sees. Otherwise, an opaque tube would be lit inside from a light
                # source outside, for example.
                continue
            # Check if light is obstructed, causing a shadow
            if not self.ray_is_obstructed(new_ray, light_source.distance_to_point(
                    chip.position)):  # Light source distance is infinity
                # Diffuse reflection
                color += Scene.__get_diffuse_light(chip, new_ray, light_source)
                # Specular reflection (Phong-Blinn)
                color += Scene.__get_phong_blinn_light(chip, incoming_ray, new_ray, light_source)
        # Recursive bits: Reflection and refraction
        # Reflection
        if chip.material.reflective_index > 0:
            new_direction = -reflected_vector(incoming_ray.direction, chip.normal)
            refracted_ray = Ray(chip.position, new_direction)

            # Find nearest object
            nearest_object, object_distance = self.__nearest_object_hit_by_ray(refracted_ray)
            if nearest_object is not None:
                intersection_position = refracted_ray.position_at_time(object_distance)
                new_chip = nearest_object.chip_at(intersection_position)
                attenuation = chip.material.reflective_index
                color += attenuation * self.color_at(new_chip, refracted_ray, recursion_depth + 1)
        # Refraction
        if chip.material.is_refractive:
            ray_is_coming_from_outside = chip.normal * incoming_ray.direction < 0
            refracted_ray = Scene.__get_refracted_ray(chip, incoming_ray, ray_is_coming_from_outside)
            if refracted_ray:
                # Find nearest object
                nearest_object, object_distance = self.__nearest_object_hit_by_ray(refracted_ray)
                if nearest_object is not None:
                    intersection_position = refracted_ray.position_at_time(object_distance)
                    new_chip = nearest_object.chip_at(intersection_position)
                    new_color = self.color_at(new_chip, refracted_ray, recursion_depth + 1)
                    vector_travelled = incoming_ray.initial_point - chip.position if ray_is_coming_from_outside else chip.position - new_chip.position
                    distance_travelled = vector_travelled.length
                    attenuated_color = chip.material.attenuate_by_refraction(new_color, distance_travelled)
                    color += attenuated_color

        return color

    def ray_trace(self, ray):
        """Traces ray through the scene and return a color"""
        color = BLACK

        # Find closest object which intersects ray
        nearest_object, object_distance = self.__nearest_object_hit_by_ray(ray)

        # Draw
        if nearest_object is not None:
            intersection_position = ray.position_at_time(object_distance)
            chip = nearest_object.chip_at(intersection_position)
            color += self.color_at(chip, ray, recursion_depth=0)
        else:
            # Draw background color
            return self.__background_color

        return color


from task import get_task, Inference, Training, ValidationTraining

def test_task_is_training():
    kwargs = {
        "train_data_path": "data/dummy_train_technical_test.csv",
        "validation_split": None,
    }
    task = get_task(**kwargs)
    assert isinstance(task, Training)

def test_task_is_validation_training():
    kwargs = {
        "train_data_path": "data/dummy_train_technical_test.csv",
        "validation_split": .2,
    }
    task = get_task(**kwargs)
    assert isinstance(task, ValidationTraining)

def test_task_is_inference():
    kwargs = {
        "infer_data_path": "data/dummy_test_technical_test.csv",
        "model_checkpoint_path": "path/to/some/model/checkpoint",
    }
    task = get_task(**kwargs)
    assert isinstance(task, Inference)


import tkinter as tk
from tkinter import ttk, scrolledtext, Menu, Spinbox, filedialog as fd, messagebox as mBox
from my_CalendarDialog import CalendarDialog
import location_helper as lh
import ToolTip as tt

# Creating a Window ===========================================================
win = tk.Tk()
win.title('Resume Maker')
win.iconbitmap('pyc.ico')

# Creating tabs ===============================================================
tabControl= ttk.Notebook(win)
p_info= ttk.Frame(tabControl)
tabControl.add(p_info, text='Personal Information')
e_background= ttk.Frame(tabControl)
tabControl.add(e_background, text='Education Background')
w_experience= ttk.Frame(tabControl)
tabControl.add(w_experience, text='Work Experience')
skills= ttk.Frame(tabControl)
tabControl.add(skills, text='Skills')
tabControl.pack(expand=5, fill='both', padx=4, pady=6)

# Creating a Menu Bar ==========================================================
def _quit():
      win.quit()
      win.destroy()
      exit()
def _msg():
      mBox.showinfo('About: Resume Maker', 'Resume Maker \nWritten by Theophilus Ige')
def help():
      def _quithelp():
            help_win.destroy()

      help_win= tk.Toplevel(win)
      help_win.title('Help: Resume Maker')
      help_win.iconbitmap('pyc.ico')
      texts= 'This tool can be used to download a variety of corpora and models \nthat can be used with NLTK.  Each corpus or model is distributed \nin a single zip file, known as a "package file."  You can \ndownload packages individually, or you can download pre-defined \ncollections of packages.\n\nWhen you download a package, it will be saved to the "download \ndirectory."  A default download directory is chosen when you run \nthe downloader; but you may also select a different download \ndirectory.  On Windows, the default download directory is \n"package." \n\nThe NLTK downloader can be used to download a variety of corpora, \nmodels, and other data packages. '

      ttk.Label(help_win, text= texts).pack(side=tk.TOP, padx=8, pady=4)
      tk.Button(help_win, text="Ok", command= _quithelp).pack(side=tk.BOTTOM, padx=8, pady=4)

menuBar = Menu(p_info)
win.config(menu=menuBar)

# Add menu items
fileMenu = Menu(menuBar, tearoff=0)
fileMenu.add_command(label='New           Ctrl-N')
fileMenu.add_separator()
fileMenu.add_command(label='Exit          Ctrl-X', command=_quit)
menuBar.add_cascade(label='File', menu=fileMenu)

# Add another Menu to the Menu Bar and an item
helpMenu = Menu(menuBar, tearoff=0)
helpMenu.add_command(label='About', command=_msg)
helpMenu.add_command(label='Instructions  F1', command=help)
menuBar.add_cascade(label='Help', menu=helpMenu)
###################################################################################
# personal information

# Name label frame -----------------------------------------------------------------
name_label=ttk.Labelframe(p_info, text=' Name ')
name_label.grid(column=0, columnspan=8,sticky='WE', padx=8, pady=4)
#first name
ttk.Label(name_label, text='First Name:').grid(column=0, row=0, padx=8, pady=4)
firstname= tk.StringVar()
firstname_entered= ttk.Entry(name_label, width=20, textvariable= firstname)
firstname_entered.grid(column=1, row=0, padx=8, pady=4)
tt.create_ToolTip(firstname_entered, 'This is an entry widget.')
#middle name
ttk.Label(name_label, text='Middle Name:').grid(column=2, row=0, padx=8, pady=4)
middlename= tk.StringVar()
middlename_entered= ttk.Entry(name_label, width=20, textvariable= middlename)
middlename_entered.grid(column=3, row=0, padx=8, pady=4)
#last name
ttk.Label(name_label, text='Last Name:').grid(column=4, row=0, padx=8, pady=4)
lastname= tk.StringVar()
lastname_entered= ttk.Entry(name_label, width=20, textvariable= lastname)
lastname_entered.grid(column=5, row=0, padx=8, pady=4)

# Address label frame -----------------------------------------------------------------
address_label=ttk.Labelframe(p_info, text=' Address ')
address_label.grid(column=0,row=1, columnspan=8, sticky='WE', padx=8, pady=4)
#Home address
ttk.Label(address_label, text='Home Address:').grid(column=0, row=1, padx=8, pady=4)
home_address= tk.StringVar()
home_address_entered= ttk.Entry(address_label, width=20, textvariable= home_address)
home_address_entered.grid(column=1, row=1, padx=8, pady=4)
#country
ttk.Label(address_label, text='Country:').grid(column=2, row=1, padx=8, pady=4)
country = ttk.Combobox(address_label, width=12, values = lh.countries)
country.current(0)
country.grid(column=3, row=1, padx=8, pady=4)
tt.create_ToolTip(country, 'This is a dropdown.')
# country callback
def onchange(e):
      state.config(value= lh.country_states[country.get()])
      state.current(0)
# bind country to state
country.bind("<<ComboboxSelected>>", onchange)

#State
ttk.Label(address_label, text='State:').place(x=490,y=0)
state = ttk.Combobox(address_label, width=12, values=[])
state.place(x=540,y=0)

# Contacts label frame -----------------------------------------------------------------
contacts_label=ttk.Labelframe(p_info, text=' Contacts ')
contacts_label.grid(column=0, row =2, columnspan=8, sticky='WE', padx=8, pady=4)
# Phone number
ttk.Label(contacts_label, text='Phone number:').grid(column=0, row=2, padx=8, pady=4)
phone_no= tk.StringVar()
phone_no_entered= ttk.Entry(contacts_label, width=20, textvariable= phone_no)
phone_no_entered.grid(column=1, row=2, padx=8, pady=4)
# Email
ttk.Label(contacts_label, text='Email:').grid(column=2, row=2, padx=8, pady=4)
email= tk.StringVar()
email_entered= ttk.Entry(contacts_label, width=20, textvariable= email)
email_entered.grid(column=3, row=2, padx=8, pady=4)
# LinkedIn Id
ttk.Label(contacts_label, text='LinkedIn Id:').grid(column=4, row=2, padx=8, pady=4)
linkedin_id= tk.StringVar()
linkedin_id_entered= ttk.Entry(contacts_label, width=20, textvariable= linkedin_id)
linkedin_id_entered.grid(column=5, row=2, padx=8, pady=4)

# preview skills
p_info_preview= tk.Button(p_info, text="Preview")
p_info_preview.grid(column=3,row=4, sticky='WE',padx=8, pady=4)
###################################################################################
# Education background

# Basic information labelframe -----------------------------------------------------------------
basic_info_label=ttk.Labelframe(e_background, text=' Basic information ')
basic_info_label.grid(column=0, columnspan=8, sticky='WE',padx=8, pady=4)
#Degree/Certificate Type
ttk.Label(basic_info_label, text='Degree/ certificate Type:').grid(column=0, row=0, padx=8, pady=4)
degree_type= tk.StringVar()
degree_type_entered= ttk.Entry(basic_info_label, width=20, textvariable= degree_type)
degree_type_entered.grid(column=2, row=0, padx=8, pady=4)

# Name of school/college
ttk.Label(basic_info_label, text='Name of school/college:').grid(column=3, row=0, padx=8, pady=4)
school_name= tk.StringVar()
school_name_entered= ttk.Entry(basic_info_label, width=20, textvariable= school_name)
school_name_entered.grid(column=4, row=0, padx=8, pady=4)

# Grade labelframe -----------------------------------------------------------------
grade_label=ttk.Labelframe(e_background, text=' Grade ')
grade_label.grid(column=0, row= 1, sticky='WE',columnspan=8, padx=8, pady=4)
# current Grade
ttk.Label(grade_label, text='Current Grade:').grid(column=0, row=0, padx=8, pady=4)
grade= tk.StringVar()
grade_entered= ttk.Entry(grade_label, width=20, textvariable= grade)
grade_entered.grid(column=1, row=0, padx=8, pady=4)
# Grade scale
ttk.Label(grade_label, text='Grade scale:').place(x=360,y=2)
grade_scale= tk.StringVar()
grade_scale_entered= ttk.Entry(grade_label, width=20, textvariable=grade_scale)
grade_scale_entered.place(x=450,y=2)

# Dates labelframe -----------------------------------------------------------------
Dates_label=ttk.Labelframe(e_background, text=' Dates ')
Dates_label.grid(column=0, row=2, sticky='WE', columnspan=8, padx=8, pady=4)
# Year of entry
def getentdate():
    cd = CalendarDialog(Dates_label)
    result = cd.result
    entry_date.set(result)

entry_date_button= tk.Button(Dates_label, text="Choose entry date", command=getentdate)
entry_date_button.pack(side=tk.LEFT, padx=8, pady=4)
tt.create_ToolTip(entry_date_button, 'This is a Date Picker.')
entry_date = tk.StringVar()
entry_date.set('None')
tk.Entry(Dates_label, textvariable=entry_date).pack(side=tk.LEFT, padx=8, pady=4)

# Year of graduation
def getgraddate():
    cd = CalendarDialog(Dates_label)
    result = cd.result
    graduation_date.set(result)

tk.Button(Dates_label, text="Choose graduation date", command=getgraddate).pack(side=tk.LEFT, padx=8, pady=4)
graduation_date = tk.StringVar()
graduation_date.set('None')
tk.Entry(Dates_label, textvariable=graduation_date).pack(side=tk.LEFT, padx=8, pady=4)

# preview Education background
e_background_preview= tk.Button(e_background, text="Preview")
e_background_preview.grid(column=3,row=4,sticky='WE', padx=8, pady=4)
###################################################################################
# Work experience

# company labelframe -----------------------------------------------------------------
company_label=ttk.Labelframe(w_experience, text=' Company Info ')
company_label.grid(column=0, sticky='WE', columnspan=8, padx=8, pady=4)
# Company name
ttk.Label(company_label, text='Degree/ certificate Type:').grid(column=0, row=2, padx=8, pady=4)
phone_no= tk.StringVar()
phone_no_entered= ttk.Entry(company_label, width=20, textvariable= phone_no)
phone_no_entered.grid(column=1, row=2, padx=8, pady=4)
# Position title
ttk.Label(company_label, text='Name of school/college:').grid(column=2, row=2, padx=8, pady=4)
school_name= tk.StringVar()
school_name_entered= ttk.Entry(company_label, width=20, textvariable= email)
school_name_entered.grid(column=3, row=2, padx=8, pady=4)

# location labelframe -----------------------------------------------------------------
location_label=ttk.Labelframe(w_experience, text=' Location ')
location_label.grid(column=0, sticky='WE', columnspan=8, padx=8, pady=4)
# Company address
ttk.Label(location_label, text='company Address:').grid(column=0, row=0, padx=8, pady=4)
company_address= tk.StringVar()
company_address_entered= ttk.Entry(location_label, width=20, textvariable= company_address)
company_address_entered.grid(column=1, row=0, padx=8, pady=4)
# company country
ttk.Label(location_label, text='Country:').grid(column=2, row=0, padx=8, pady=4)
company_country= ttk.Combobox(location_label, width=12, values = lh.countries)
company_country.current(0)
company_country.grid(column=3, row=0, padx=8, pady=4)

# company country callback
def onchange(e):
      company_state.config(value= lh.country_states[company_country.get()])
      company_state.current(0)
# bind country to state
company_country.bind("<<ComboboxSelected>>", onchange)

# company state
ttk.Label(location_label, text='State:').grid(column=4, row=0, padx=8, pady=4)
company_state= ttk.Combobox(location_label, width=12, values=[])
company_state.grid(column=5, row=0, padx=8, pady=4)

# Dates labelframe -----------------------------------------------------------------
WDates_label=ttk.Labelframe(w_experience, text=' Dates ')
WDates_label.grid(column=0, sticky='WE', columnspan=8, padx=8, pady=4)

# Year of entry
def getWentdate():
    cd = CalendarDialog(WDates_label)
    result = cd.result
    Wentry_date.set(result)

tk.Button(WDates_label, text="Choose entry date", command=getWentdate).pack(side=tk.LEFT, padx=8, pady=4)
Wentry_date = tk.StringVar()
Wentry_date.set('None')
tk.Entry(WDates_label, textvariable=Wentry_date).pack(side=tk.LEFT, padx=8, pady=4)

# Year of leaving
def getleavedate():
    cd = CalendarDialog(WDates_label)
    result = cd.result
    leaving_date.set(result)

tk.Button(WDates_label, text="Choose leaving date", command=getleavedate).pack(side=tk.LEFT, padx=8, pady=4)
leaving_date = tk.StringVar()
leaving_date.set('None')
tk.Entry(WDates_label, textvariable=leaving_date).pack(side=tk.LEFT, padx=8, pady=4)

# preview work experience
workXP_preview= tk.Button(w_experience, text="Preview")
workXP_preview.grid(column=3,row=4, sticky='WE',padx=8, pady=4)
###################################################################################
# skills

# skills labelframe -----------------------------------------------------------------
skills_label=ttk.Labelframe(skills, text=' Skills ')
skills_label.grid(column=0, sticky='WE', columnspan=8, padx=8, pady=4)
# skill name
ttk.Label(skills_label, text='Skill name:').grid(column=0, row=0)
skill_name= tk.StringVar()
skill_name_entered= ttk.Entry(skills_label, width=20, textvariable= skill_name)
skill_name_entered.grid(column=1, row=0, padx=8, pady=4)
# skill level
ttk.Label(skills_label, text='Skill level(%):').grid(column=3, row=0)
skill_level= tk.StringVar()
skill_level_entered= ttk.Entry(skills_label, width=20, textvariable= skill_level)
skill_level_entered.grid(column=4, row=0, padx=8, pady=4)
# company acquired
ttk.Label(skills_label, text='Company acquired:').grid(column=5, row=0, pady=16)
company_acquired= tk.StringVar()
company_acquired_entered= ttk.Entry(skills_label, width=20, textvariable= company_acquired)
company_acquired_entered.grid(column=6, row=0, padx=8, pady=16)
# preview skills
skills_preview= tk.Button(skills, text="Preview")
skills_preview.grid(column=3,row=4,sticky='WE', padx=8, pady=4)

# preview labelframe -----------------------------------------------------------------
preview_label=ttk.Labelframe(win, text=' Preview ')
preview_label.pack(expand=5, fill='both', padx=4, pady=2)
ttk.Label(preview_label, text='Company acquired:').pack(expand=5, fill='both', padx=8, pady=4)

# Download button
tk.Button(win, text="Download Resume", font=('Helvetica', 16, 'bold'), bg='black', fg='green').pack(expand=5, fill='both', padx=4, pady=2)

win.mainloop()


from re import search
import csv

# 單純 concordance function，輸入欲查上下文之關鍵字 kw，以及所有 sentences；
# return (上文, kw, 下文) 之 list。
def concordance(kw,sentences):
    concordance_list = []
    for sentence in sentences:
        m = search(pattern='(?P<left>.*)(?P<kw>' + kw + ')(?P<right>.*)', string=sentence)
        if m:
            concordance_list.append([m.group('left'), m.group('kw'), m.group('right')])
    return concordance_list

# 通用型 concordancer，輸入欲查上下文之關鍵字 kw，以及 csv 語料庫格式 row，以及欲檢索之欄位 target_column；
# return csv 格式的 rows，並於 target_column 前後 insert 上文和下文之欄位資料。
from nltk.text import ConcordanceIndex
def generic_concordancer(kw, rows, target_column_num):
    header_row = next(rows)
    header_row.insert(target_column_num+1, "right_print")
    header_row.insert(target_column_num, "left_print")

    output_rows= [header_row]
    for row in rows:
        if kw in row[target_column_num]:
            CI = ConcordanceIndex(tokens=list(row[target_column_num]))
            ConcordanceLine = CI.find_concordance(word=kw)[0]
            row.insert(target_column_num+1, ConcordanceLine.right_print)
            row[target_column_num] = kw
            row.insert(target_column_num, ConcordanceLine.left_print)
            output_rows.append(row)
    return output_rows

def segmented_concordancer(kw, rows, target_column_num):
    header_row = next(rows)
    header_row.insert(target_column_num+1, "right_print")
    header_row.insert(target_column_num, "left_print")

    output_rows= [header_row]
    for row in rows:
        if kw in row[target_column_num]:
            CI = ConcordanceIndex(tokens=row[target_column_num].split("/"))
            ConcordanceLine = CI.find_concordance(word=kw)[0]
            row.insert(target_column_num+1, ConcordanceLine.right_print)
            row[target_column_num] = kw
            row.insert(target_column_num, ConcordanceLine.left_print)
            output_rows.append(row)
    return output_rows

import sys
from klokahAPI import klokahAPI
if __name__ == "__main__":
#   csvfile = open(sys.argv[1]) #corpora_root_dir/nan/liching-v3-all.tsv
 #  kw = sys.argv[2] # 打
  # for row in generic_concordancer(kw=kw, rows=csv.reader(csvfile, delimiter="\t"), target_column_num=2):
   #    print(row)

    d = sys.argv[1]
    txt = kw = sys.argv[2]
    target_column_num = int(sys.argv[3])

    rows = klokahAPI(d=int(d), txt=txt)
    for row in generic_concordancer(kw, rows, target_column_num):
        print(row)


def main():
    f = open("./input.txt", "r")

    suml = 0
    sums = []

    for l in f:
        if l == "\n":
            sums.append(suml)
            suml = 0
        else:
            suml += int(l)

    sums.sort()
    print(sum(sums[-3:]))

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat Aug 13 15:49:14 2016

@author: Administrator
"""
'''
    adaboost方法，选择树分类器，效果没有随机森林好
    选择逻辑回归分类器，
'''
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import StratifiedShuffleSplit
from sklearn.metrics import classification_report,accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
import treeinterpreter.treeinterpreter as ti
import gc
import os
from sklearn.decomposition import PCA
from sklearn.pipeline import  Pipeline
from time import time

#%%
#load data
rootPath = r'G:\OneDrive\codes\python\RF\data'
#rootPath = r'D:\OneDrive\codes\python\RF\data'
trainPath = rootPath+r'\train'
#trainPath = rootPath+r'\train_filter'
trainlabPath = rootPath+r'\trainlab'
#testPath = rootPath+r'\test'
testPath = rootPath+r'\test'
testlabPath = rootPath+r'\testlab'

data1 = np.loadtxt(open(trainPath,"rb"),delimiter=",",skiprows=0,dtype=np.float)
lab1 = np.loadtxt(open(trainlabPath,"rb"),delimiter=",",skiprows=0,dtype=np.int)
data2 = np.loadtxt(open(testPath,"rb"),delimiter=",",skiprows=0,dtype=np.float)
lab2 = np.loadtxt(open(testlabPath,"rb"),delimiter=",",skiprows=0,dtype=np.int)
test = {'data':data2.transpose(),'lab':lab2}
train = {'data':data1.transpose(),'lab':lab1}

#ssp = StratifiedShuffleSplit(test['lab'],n_iter=1,test_size=0.9,random_state=1)
ssp = StratifiedShuffleSplit(train['lab'],n_iter=3,test_size=0.1,random_state=1)
for trainlab,testlab in ssp:
    print("train:\n%s\ntest:\n%s" % (trainlab,testlab))
    X = test['data'][trainlab];Xlab = test['lab'][trainlab]
    Y = test['data'][testlab];Ylab = test['lab'][testlab]

#%% pipeliene&cv
pca = PCA()
adaboost_tree = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_features='auto',criterion="entropy"))
adaboost_log = AdaBoostClassifier(base_estimator=LogisticRegression(C=1000,class_weight=None,penalty='l2'
        ,solver='newton-cg',max_iter=200,tol=1E-5,n_jobs=1,verbose=3,
        multi_class='multinomial', fit_intercept=True))

#%% 选择模型
"""
 By default, weak learners are decision stumps.默认的基分类器是决策树
learning_rate:controls the contribution of the weak learners in the final combination.
main parameters to tune：n_estimators and the complexity of the base estimators
"""
#pipeline = Pipeline([('pca',pca),('ada',adaboost_tree)])
pipeline = Pipeline([('pca',pca),('ada',adaboost_log)])
#paramMap = {'ada__n_estimators':[175,150,200],'ada__learning_rate':[1E-3,1E-1,1],'ada__algorithm':["SAMME",'SAMME.R'],
#            'pca__n_components':[200,50,25,16],"pca__whiten":[True]}
paramMap = {'ada__n_estimators':[50],'ada__learning_rate':[1],'ada__algorithm':["SAMME",'SAMME.R'],
            'pca__n_components':[25],"pca__whiten":[True]}

#cv 选择一个cv生成器
#refit =False,表示不用整个数据集来refit,best_estimator
#grid_search = GridSearchCV(pipeline,paramMap,refit=False,n_jobs=1,cv=ssp)
grid_search = GridSearchCV(pipeline,paramMap,refit=True,n_jobs=3,cv=ssp)
#%%
# training
print("start training···")
start = time()
#grid_search.fit(test['data'],test['lab'])
grid_search.fit(train['data'],train['lab'])
print("GridSearchCV took %.2f seconds for %d cadidate param sets"
      %(time()-start,len(grid_search.grid_scores_)))

best_score= grid_search.best_score_
best_param = grid_search.best_params_
#%%
#testing
if os.path.exists(rootPath+'\prediction'):
    os.remove(rootPath+'\prediction')#删除文件
row,col = test['data'].shape
prediction = np.array([])
for i in range(int(np.ceil(row/1000.0))):
    start = 1000*i
#    prediction,bias,contributions, =ti.predict(rf,test['data'][start:min(start+20,col)])
    prediction = np.concatenate((prediction,grid_search.predict(test['data'][start:min(start+1000,row)])),axis=1)

#追加到文件
np.savetxt(open(rootPath+'\prediction','w'),prediction,delimiter = ',',fmt ='%d')

print("calssification report:")
report = classification_report(test['lab'],prediction)
acu=accuracy_score(test['lab'],prediction)


import numpy as np
import fitsio

def dataWrangle(fits_file, save=True, name_tag="point_data", get_center=False):
    """ Extract relevant data from fits file
    Need to run only once!!!
    """
    fits_obj = fitsio.FITS(fits_file)

    # the first HDU is always an image - doesn't contain data
    n_skewers = len(fits_obj) - 1
    print("Total number of skewers are %d" % n_skewers)

    # Create arrays to store the data
    if get_center:
        ra_arr, dec_arr = [], []
    xx, yy, zz = [], [], []
    ff = []

    for i in range(1, len(fits_obj)):
        hdr = fits_obj[i].read_header()

        ra = np.deg2rad(hdr['RA'])
        dec = np.deg2rad(hdr['DEC'])

        if get_center:
            ra_arr.append(ra)
            dec_arr.append(dec)

        tb_data = fits_obj[i].read()
        r, field = tb_data['RCOMOV'], tb_data['DELTA_T']

        # Spherical to cartesian co-ordinates
        x = np.sin(dec) * np.cos(ra) * r
        y = np.sin(dec) * np.sin(ra) * r
        z = np.cos(dec) * r

        xx = np.hstack((xx, x))
        yy = np.hstack((yy, y))
        zz = np.hstack((zz, z))
        ff = np.hstack((ff, field))

    if save:
        np.savetxt(name_tag + ".dat", [xx, yy, zz, ff])
    if get_center:
        return np.mean(ra_arr), np.mean(dec_arr)

def celestial_rot_matrix(ra_point, dec_point, is_rad=True):
    """ Obtain a rotation matrix to rotate any set of
    cartesian points in such a way that the polar axes
    points along the (ra_point, dec_point) direction
    """
    if not is_rad:
        ra_point = np.deg2rad(ra_point)
        dec_point = np.deg2rad(dec_point)

    # Vector around which we rotate
    k_perp = np.array([-np.sin(ra_point), np.cos(ra_point), 0])

    # Rotation direction - Cross product matrix
    K = np.array([
        [0, -k_perp[2], k_perp[1]],
        [k_perp[2], 0, -k_perp[0]],
        [-k_perp[1], k_perp[0], 0]
        ])

    # ADOPTED FROM: https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
    # Rotation Matrix to rotate the points vectors
    Q = np.eye(3) - np.sin(dec_point) * K +\
        (1 - np.cos(dec_point)) * np.matmul(K, K)

    return Q

def rot_points(data_file, ra_point, dec_point, viz=True):
    """ Rotate the points and visualize

    Parameters:
        data_file: input file containing the cordiantes and field values
        ra_point: central RA of the plate
        dec_point: central DEC of the plate
        offset: offset between the map (starts at 0) and pixel data
                Positive offset is preferred.
    """
    if data_file is None:
        data_file = "point_data.dat"

    *XX, ff = np.loadtxt(data_file)

    # rotate the points using Rotation matrix
    rot_matrix = celestial_rot_matrix(ra_point, dec_point)
    x, y, z = rot_matrix.dot(XX)


import turtle as t
import colorsys
t.bgcolor("black")
t.tracer(127)
def design():
    h =67
    n =298
    for i in range(2000):
        c = colorsys.hsv_to_rgb(h,1,0.9)
        h +=1/n
        t.pensize(13)
        t.color("black")
        t.up()
        t.goto(0,0)
        t.down()
        t.fillcolor(c)
        t.begin_fill()
        t.circle(124,60,steps=5)
        t.fd(i)
        t.rt(79)
        t.lt(90)
        t.bk(70)
        t.end_fill()
design()
t.seth(80)
t.done()


from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey

class Adressee(MPTTModel):
    """
    Adressee Table implimented with MPTT.
    """

    name = models.CharField(
        verbose_name=_("Название адресата"),
        help_text=_("Необходимо и не должно повторяться"),
        max_length=255,
        unique=True,
    )
    slug = models.SlugField(
        max_length=255, verbose_name=_("Adressee safe URL"), unique=True
    )
    parent = TreeForeignKey(
        "self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
    )
    is_active = models.BooleanField(default=True)

    class MPTTMeta:
        order_insertion_by = ["name"]

    class Meta:
        verbose_name = _("Adressee")
        verbose_name_plural = _("Adressees")

    def get_absolute_url(self):
        return reverse("store:adressee_list", args=[self.slug])

    def __str__(self):
        return self.name

class Category(MPTTModel):
    """
    Category Table implimented with MPTT.
    """

    name = models.CharField(
        verbose_name=_("Category Name"),
        help_text=_("Required and unique"),
        max_length=255,
        unique=True,
    )
    slug = models.SlugField(
        verbose_name=_("Category safe URL"), max_length=255, unique=True
    )
    parent = TreeForeignKey(
        "self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
    )
    is_active = models.BooleanField(default=True)

    class MPTTMeta:
        order_insertion_by = ["name"]

    class Meta:
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")

    def get_absolute_url(self):
        return reverse("store:category_list", args=[self.slug])

    def __str__(self):
        return self.name

class ProductType(models.Model):
    """
    ProductType Table will provide a list of the different types
    of products that are for sale.
    """

    name = models.CharField(
        verbose_name=_("Product Name"),
        help_text=_("Required"),
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)

    class Meta:
        verbose_name = _("Product Type")
        verbose_name_plural = _("Product Types")

    def __str__(self):
        return self.name

class ProductSpecification(models.Model):
    """
    The Product Specification Table contains product
    specifiction or features for the product types.
    """

    product_type = models.ForeignKey(ProductType, on_delete=models.RESTRICT)
    name = models.CharField(
        verbose_name=_("Name"), help_text=_("Required"), max_length=255
    )

    class Meta:
        verbose_name = _("Product Specification")
        verbose_name_plural = _("Product Specifications")

    def __str__(self):
        return self.name

class Product(models.Model):
    """
    The Product table contining all product items.
    """

    adressee = models.ForeignKey(
        Adressee, on_delete=models.RESTRICT, null=True, blank=True
    )
    product_type = models.ForeignKey(ProductType, on_delete=models.RESTRICT)
    category = models.ForeignKey(Category, on_delete=models.RESTRICT)
    title = models.CharField(
        verbose_name=_("title"),
        help_text=_("Required"),
        max_length=255,
    )
    description = models.TextField(
        verbose_name=_("description"), help_text=_("Not Required"), blank=True
    )
    slug = models.SlugField(max_length=255)
    regular_price = models.DecimalField(
        verbose_name=_("Regular price"),
        help_text=_("Maximum 999.99"),
        error_messages={
            "name": {
                "max_length": _("The price must be between 0 and 999.99."),
            },
        },
        max_digits=15,
        decimal_places=2,
    )
    discount_price = models.DecimalField(
        verbose_name=_("Discount price"),
        help_text=_("Maximum 999.99"),
        error_messages={
            "name": {
                "max_length": _("The price must be between 0 and 999.99."),
            },
        },
        max_digits=15,
        decimal_places=2,
    )
    is_active = models.BooleanField(
        verbose_name=_("Product visibility"),
        help_text=_("Change product visibility"),
        default=True,
    )
    internalnumber = models.PositiveSmallIntegerField(
        blank=True, verbose_name="Внутренний номер"
    )
    inventorynumber = models.IntegerField(
        default=0, blank=True, verbose_name="Инвентарный №"
    )
    serialnumber = models.CharField(
        max_length=50, blank=True, default=0, verbose_name="Серийниый №"
    )
    rozport = models.PositiveSmallIntegerField(blank=True, verbose_name="Розетка(порт)")
    signature = models.BooleanField(default=False, verbose_name="Роспись")
    created_at = models.DateTimeField(
        _("Created at"), auto_now_add=True, editable=False
    )
    updated_at = models.DateTimeField(_("Updated at"), auto_now=True)
    users_wishlist = models.ManyToManyField(
        settings.AUTH_USER_MODEL, related_name="user_wishlist", blank=True
    )

    class Meta:
        ordering = ("created_at",)
        verbose_name = _("Product")
        verbose_name_plural = _("Products")

    def get_absolute_url(self):
        return reverse("store:product_detail", args=[self.slug])

    def __str__(self):
        return self.title

class ProductSpecificationValue(models.Model):
    """
    The Product Specification Value table holds each of the
    products individual specification or bespoke features.
    """

    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    specification = models.ForeignKey(ProductSpecification, on_delete=models.RESTRICT)
    value = models.CharField(
        verbose_name=_("value"),
        help_text=_("Product specification value (maximum of 255 words"),
        max_length=255,
    )

    class Meta:
        verbose_name = _("Product Specification Value")
        verbose_name_plural = _("Product Specification Values")

    def __str__(self):
        return self.value

class ProductImage(models.Model):
    """
    The Product Image table.
    """

    product = models.ForeignKey(
        Product, on_delete=models.CASCADE, related_name="product_image"
    )
    image = models.ImageField(
        verbose_name=_("image"),
        help_text=_("Upload a product image"),
        upload_to="images/",
        default="images/default.png",
    )
    alt_text = models.CharField(
        verbose_name=_("Alturnative text"),
        help_text=_("Please add alturnative text"),
        max_length=255,
        null=True,
        blank=True,
    )
    is_feature = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = _("Product Image")
        verbose_name_plural = _("Product Images")


import cv2
import numpy as np

def sketch(image):
    #convert image to greyscale
    img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #clean up image using Gaussian Blur
    img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)

    #Extract edges
    canny_edges = cv2.Canny(img_gray_blur, 10, 70)

    #Invert to binarize the image
    ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)

    return mask

capture = cv2.VideoCapture(0)

while True:
    ret, frame = capture.read()
    cv2.imshow('Sketchy', sketch(frame))
    if cv2.waitKey(1) == 13: #13 is the Enter waitKey
        break

capture.release()
cv2.destroyAllWindows()


""" 804 https://leetcode.com/problems/unique-morse-code-words/"""

import time
from typing import List

class Solution:
    def uniqueMorseRepresentations(self, words: List[str]) -> int:
        alphabet = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
                    "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]

        words_morse = []
        for word in words:
            word_morse = ""
            for letter in word:
                word_morse += alphabet[ord(letter)-97]
            words_morse.append(word_morse)
        return len(set(words_morse))

if __name__ == "__main__":
    start_time = time.time()
    print(Solution().uniqueMorseRepresentations(["gin", "zen", "gig", "msg"]))

    print("--- %s seconds ---" % (time.time() - start_time))


class User:
    """This class gets the email, user_id, role and name of the user in the school"""

    def __init__(self, user_id, name, email, role):
        """Initializing the instance variables of the class"""

        self.user_id = user_id
        self.name = name
        self.email = email
        self.role = role

    def get_user_id(self):
        """returns the ID of the user in the school"""
        return f"{self.user_id}"

    def get_name(self):
        """returns the name of the user"""
        return f"{self.name}"

    def get_email(self):
        """returns the email address of the user"""
        name_split = self.name.split()
        valid_email = "{}.{}@agblschool.com".format(self.name[:1].lower(), name_split[1].lower())
        if self.email != valid_email or self.email == "":
            raise Exception("Invalid email")
        return f"{self.email}"

    def get_role(self):
        """returns the role of the user in the school"""
        return f"{self.role}"

    def user_details(self):
        """displays information about the user"""
        name_split = self.name.split()
        valid_email = "{}.{}@agblschool.com".format(self.name[:1].lower(), name_split[1].lower())
        if self.email != valid_email or self.email == "":
            raise Exception("Invalid email")
            # quit()

        else:
            return "------------------------------ PERSONAL INFORMATION -----------------------" \
                   "\nUser ID: {}" \
                   "\nName: {}" \
                   "\nEmail: {}" \
                   "\nRole: {}".format(self.user_id, self.name, self.email, self.role)

class Student(User):
    """The class gets all information to register a particular student"""

    def __init__(self, user_id, name, email, role):
        super().__init__(user_id, name, email, role)
        self.dob = ""
        self.age = str
        self.gender = str
        self.parent_name = str
        self.parent_contact = str
        self.address = str
        self.doA = str

    def get_dob(self):
        """returns the date of birth of the student"""
        print(f"{self.dob}")

    def get_age(self):
        """returns the age of the student"""
        print(f"{self.age}")

    def get_gender(self):
        """returns the gender of the student"""
        print(f"{self.gender}")

    def get_parent_name(self):
        """returns the students' parents' name"""
        print(f"{self.parent_name}")

    def get_parent_contact(self):
        """returns the students' parents' contact"""
        print(f"{self.parent_contact}")

    def set_parent_contact(self, phone_num):
        """returns the updated parent contact"""
        self.parent_contact = phone_num
        return f"{phone_num}"

    def get_address(self):
        """returns the address of the student"""
        print(f"{self.address}")

    def set_address(self, new_address):
        """returns the updated address"""
        self.address = new_address
        return f"{new_address}"

    def get_doA(self):
        """returns the date of admission of the student"""
        print(f"{self.doA}")

    def user_detail(self, dob, age, gender, parent_name, parent_contact, address, doA):
        """returns the information of the student"""
        self.dob = dob
        self.age = age
        self.gender = gender
        self.parent_name = parent_name
        self.parent_contact = parent_contact
        self.address = address
        self.doA = doA
        return f"------------------------------ PERSONAL INFORMATION -----------------------" \
               f"\nUser ID: {self.get_user_id()}" \
               f"\nName: {self.get_name()}" \
               f"\nEmail: {self.get_email()}" \
               f"\nRole: {self.get_role()}" \
               f"\nDate of Birth: {self.dob}" \
               f"\nAge: {self.age}" \
               f"\nGender: {self.gender}" \
               f"\nParent name: {self.parent_name}" \
               f"\nParent contact: {self.parent_contact}" \
               f"\nAddress: {self.address}" \
               f"\nDate of Admission: {self.doA}"

class Register(Student):
    """The student class the methods to register, search, delete, update or display students information in the
    registration system"""

    def __init__(self, user_id, name, email, role):
        super(Register, self).__init__(user_id, name, email, "student")
        self.student_list = []
        self.student_details = {"IB200": ["Irene Busah", "i.busah@agblschool.com", "student", "5th May, 2000", "21",
                                          "Female", "Joseph Busah", "+233549185709", "Takoradi", "30th December"],
                                "NN233": ["Nelson Nor", "n.nor@agblschool.com", "student", "10th May 2001", "20",
                                          "Male", "Constance Nor", "+233241918971", "Wa", "30th August 2020"]}

        self.num_of_students = 2
        self.file = "student_list.txt"

    def register(self, user_id, name, emails, role, dob, age, gender, parent_name, parent_contact, address, doA):
        """Allows the user to register a new student"""
        name_split = name.split()
        valid_email = "{}.{}@agblschool.com".format(name[:1].lower(), name_split[1].lower())
        if emails != valid_email or emails == "":
            raise Exception("Invalid email")
        with open("student_list.txt", "a+") as self.file:
            """Appends the new student's information to the student list file"""

            self.file.write("--------------------------- New Student Details -----------------------\n")
            self.file.write("ID: {}\n".format(user_id))
            self.file.write("Name: {}\n".format(name))
            self.file.write("Email: {}\n".format(emails))
            self.file.write("Role: {}\n".format(role))
            self.file.write("Date of birth: {}\n".format(dob))
            self.file.write("Age: {}\n".format(age))
            self.file.write("Gender: {}\n".format(gender))
            self.file.write("Parent name: {}\n".format(parent_name))
            self.file.write("Parent contact: {}\n".format(parent_contact))
            self.file.write("Address: {}\n".format(address))
            self.file.write("Date of Admission: {}\n".format(doA))

        self.student_details[user_id] = self.student_list
        self.num_of_students += 1

        print(f"---------------------SUCCESSFULLY REGISTERED {self.num_of_students}-----------------------")

    def delete_student(self, ID):
        """removes a student from the register using the student's ID"""
        if ID not in self.student_details.keys() or ID == "":
            raise Exception("ID not found")
        elif ID in self.student_details.keys():
            del self.student_details[ID]
            self.num_of_students -= 1
            return f"You have successfully deleted {ID} from the register system\n" \
                   f"Current Number of students: {self.num_of_students}\n" \
                   "----------------------- SUCCESSFULLY DELETED ----------------------"

    def search_student(self, ID):
        """returns the details of the student with the provided ID"""
        for student in list(self.student_details.keys()):
            if student == ID:
                print("Name: {}".format(self.student_details.get(ID)[0]))
                print("Email: {}".format(self.student_details.get(ID)[1]))
                print("Role: {}".format(self.student_details.get(ID)[2]))
                print("Date of birth: {}".format(self.student_details.get(ID)[3]))
                print("Age: {}".format(self.student_details.get(ID)[4]))
                print("Gender: {}".format(self.student_details.get(ID)[5]))
                print("Parent name: {}".format(self.student_details.get(ID)[6]))
                print("Parent contact: {}".format(self.student_details.get(ID)[7]))
                print("Address: {}".format(self.student_details.get(ID)[8]))
                print("Date of Admission: {}".format(self.student_details.get(ID)[8]))

    def update_student(self, user_id, new_address, new_parent_contact):
        """updates the address and parent_contact of the student in the register system
        using the set_parent_contact and set_address methods in the Student class
        """
        for key, value in self.student_details.items():
            if key in self.student_details.keys() and key == user_id:
                value[8] = self.set_address(new_address)
                value[7] = self.set_parent_contact(new_parent_contact)
                print(self.student_details.get(user_id))
                print("")
                print(f"-------------------- SUCCESSFULLY UPDATED --------------------")
            elif user_id not in self.student_details.keys():
                raise Exception("ID not found")

    def display_register(self):
        """returns the students in the text file"""
        file_open = open("student_list.txt", "r")
        for line in file_open.readlines():
            line.strip("\n")
            print(line)

        file_open.close()  # closes the opened file

# register = Register("ML234", "Myra Lugwiri", "m.lugwiri@agblschool.com", "staff")
# register.register("16th October", "23", "female", "Micheal", "+23389097634", "Kumasi", "4th June")
# register_one = Register("IB200", "Irene Busah", "i.busah@agblschool.com", "student")
# register_one.register("5th May, 2000", "21", "Female", "Joseph Busah", "+233549185709", "Takoradi", "30th December")
# register.display_register()


"""
Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer,
replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay),
or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
"""

class Solution(object):
    def isHappy(self, n): #45 ms
        """
        :type n: int
        :rtype: bool
        """
        seen=[]
        while(n>0 and (n not in seen)):
            seen.append(n)
            if isHappyNumber(n):
                return True
            n = Happy(n)
        return False;

def Happy(n):
    res = 0
    while(n>0):
        res+=math.pow(n%10,2)
        n=n/10
    return int(res)

def isHappyNumber(n):
    return (math.log10(n).is_integer() or n==1)


import random
from .myconstants import moves_dict, user_comp

class GameBrain:

    def __init__(self):
        self.__computerAction = None
        self.__computerOptions = ['R', 'P', 'S']
        self.__gameScore = {"Player": 0, "Computer": 0}
        self.__pairs = [['R', 'P'], ['R', 'S'], ['P', 'S']]
        self.__score_dict = {'Computer': 0, 'Player': 0}
        self.result = 'Press Z for results'

    def getWinner(self, playerAction):
        if playerAction != 'None':
            self.__computerAction = self.__computerOptions[random.randint(
                0, 2)]

            # print(
            #     f"Computer Action : {self.__computerAction} |||| Player Action : {playerAction}")
            # print("-------------------------------------------")
            move_check = (self.__computerAction, playerAction)

            if moves_dict.get(move_check):
                self.__score_dict[user_comp[move_check.index(
                    moves_dict.get(move_check))]] += 1

            elif moves_dict.get(move_check[::-1]):
                self.__score_dict[user_comp[move_check.index(
                    moves_dict.get(move_check[::-1]))]] += 1

            else:
                # print('Tie')
                pass

            return self.__computerAction

        else:
            # print("Waiting for player move")
            return 'None'

    def getFinalResult(self):
        player_score = self.__score_dict['Player']
        computer_score = self.__score_dict['Computer']
        if player_score > computer_score:
            self.result = 'Player Wins'

        elif player_score < computer_score:
            self.result = 'Computer Wins'

        elif player_score == computer_score:
            self.result = "It's a tie"
        return self.result

    def getCurrentScore(self):
        return f"Player : {self.__score_dict['Player']} | Computer : {self.__score_dict['Computer']}"

    def resetScore(self):
        self.__score_dict = {'Computer': 0, 'Player': 0}

    def resetResult(self):
        self.result = 'Press Z for results'
        return self.result


import os, pytz, string, random, decimal, requests, json
from PIL import Image, ImageEnhance

from Tripalocal_V1 import settings
from unionpay.util.helper import load_config
from datetime import timedelta, date
from copy import deepcopy
from io import BytesIO
from django.core.files.storage import default_storage as storage

def isEnglish(s):
    if s is None:
        return False

    try:
        s.decode('ascii') if isinstance(s, bytes) else s.encode('ascii')
    except UnicodeDecodeError:
        return False
    except UnicodeEncodeError:
        return False
    else:
        return True

def experience_fee_calculator(price, commission_rate):
    if price is None or commission_rate is None \
        or commission_rate >=1.00 or commission_rate < 0:
        return price

    if type(price)==int or type(price) == float:
        COMMISSION_PERCENT = round(commission_rate/(1-commission_rate),3)
        return round(price*(1.00+COMMISSION_PERCENT), 0)*(1.00+settings.STRIPE_PRICE_PERCENT) + settings.STRIPE_PRICE_FIXED

    return price

def convert_currency(price, current_currency, target_currency, conversion=None):
    '''
    add the parameter of "conversion" to avoid reading local files
    '''
    if not (type(price)==int or type(price) == float or type(price) == decimal.Decimal):
        return price

    if current_currency is None or len(current_currency) == 0 \
        or target_currency is None or len(target_currency) == 0 \
        or current_currency.upper() == target_currency.upper():
        return price

    if conversion and type(conversion) != dict:
        return price

    if not conversion:
        file_name = 'experiences/currency_conversion_rate/' + current_currency.upper() + '.yaml'
        conversion = load_config(os.path.join(settings.PROJECT_ROOT, file_name).replace('\\', '/'))

    return round(float(price)*float(conversion.get(target_currency.upper(), 1.00)), 2)

def get_total_price(experience, guest_number=0, adult_number=0, child_number=0, extra_information=None, language=None):
    '''
    return total price, not including commission or service fee
    either use guest_number, or adult_number + child_number, the latter has a higher priority
    extra_information: for partner products, it indicates which optionitem is chosen, e.g., {"1243":1, "2344":2}
    '''
    if extra_information:
        price = 0
        i = 0
        items = json.loads(extra_information)
        extras = []
        for k, v in items.items():
            try:
                int(k)
            except ValueError as e:
                #"Extras" for rezdy product
                og = experience.optiongroup_set.filter(type="Extras", language="en")[0]
                oi = og.optionitem_set.filter(name = k)[0]
                if oi.price_type.upper() != "FIXED":
                    price += oi.price * v
                else:
                    price += oi.price
                extras.append(k)

        for k in extras:
            items.pop(k, None)

        language = settings.LANGUAGES[0][0] if language is None else language
        for og in experience.optiongroup_set.filter(language=language):
            for oi in og.optionitem_set.all():
                if str(oi.original_id) in items.keys():
                    price += oi.price*items.get(str(oi.original_id))
                    i += 1
                    if i == len(items.keys()):
                        break
            if i == len(items.keys()):
                break
        if i == len(items.keys()):
            if experience.currency.lower() != "aud":
                price = convert_currency(price, "aud", experience.currency)
            return price
        else:
            raise ValueError("OptionItem mismatch")

    if type(guest_number) != int or guest_number < 0:
        guest_number = 0
    if type(adult_number) != int or adult_number < 0:
        adult_number = 0
    if type(child_number) != int or child_number < 0:
        child_number = 0

    if adult_number > 0 or child_number > 0:
        guest_number = adult_number

    subtotal_price = 0.0
    if experience.children_price is not None and experience.children_price > 0:
        subtotal_price += float(experience.children_price) * float(child_number)
    else:
        guest_number += child_number

    if experience.dynamic_price and type(experience.dynamic_price) == str:
        price = experience.dynamic_price.split(',')
        if len(price)+experience.guest_number_min-2 == experience.guest_number_max:
        #these is comma in the end, so the length is max-min+2
            if guest_number <= experience.guest_number_min:
                subtotal_price += float(experience.price) * float(experience.guest_number_min)
            else:
                subtotal_price += float(price[guest_number-experience.guest_number_min]) * float(guest_number)
        else:
            #wrong dynamic settings
            subtotal_price += float(experience.price)*float(guest_number)
    else:
        if guest_number <= experience.guest_number_min:
            subtotal_price += float(experience.price) * float(experience.guest_number_min)
        else:
            subtotal_price += float(experience.price)*float(guest_number)

    return subtotal_price + experience.fixed_price

def get_timezone(city):
    timezones = load_config(os.path.join(settings.PROJECT_ROOT, 'experiences/time_zone/time_zone.yaml').replace('\\', '/'))
    return timezones.get(city.lower(), settings.TIME_ZONE)

def email_account_generator(size=10, chars=string.ascii_lowercase + string.digits):
    if size is None or type(size) != int or size < 4 or size > 20:
        size = 10
    if chars is None or not ''.join(chars).isalnum():
        chars = string.ascii_lowercase + string.digits

    return ''.join(random.choice(chars) for _ in range(size)).lower()

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

def get_weather(lat, lon, time):
    api_address = "https://api.forecast.io/forecast/0a73fa455425979752f12c2afe2441ba/" + str(lat) + "," + str(lon) + "," + str(time)
    payload = {'units': 'si', 'exclude': 'currently,minutely,hourly,alerts,flags'}
    r = requests.get(api_address, params=payload)
    if r.status_code is requests.codes.ok:
        return r.json()
    else:
        r.raise_for_status()

def static_vars(**kwargs):
    def decorate(func):
        for k in kwargs:
            setattr(func, k, kwargs[k])
        return func
    return decorate

@static_vars(dict={"Melbourne":{"max_experience_id":0,"experiences":[],\
    "max_product_id":0,"products":[],\
    "max_flight_id":0,"flights":[],\
    "max_transfer_id":0,"transfers":[],\
    "max_accommodation_id":0,"accommodations":[],\
    "max_restaurant_id":0,"restaurants":[],\
    "max_suggestion_id":0,"suggestions":[],\
    "max_price_id":0,"prices":[],\
    "itineraries_last_updated":"2000-01-01 00:00:00","daily_itineraries":[]}})
def recent_search(action, new_search=None):
    '''
    action: get, update, clear
    new_search: add the new item to the dict/update an existing item
    '''
    if action is not None:
        if action.lower()=="get":
            return recent_search.dict
        #elif action.lower()=="update" and new_search is not None:
        #    for k, v in new_search.items():
        #        if k not in recent_search.dict:
        #            recent_search.dict[k] = deepcopy(v)
        #        else:
        #            for experience in v['experiences']:
        #                if recent_search.dict[k]['max_id'] < experience['id']:
        #                    recent_search.dict[k]['experiences'].append(experience)
        #            recent_search.dict[k]['max_id'] = v['max_id']
        elif action.lower()=="clear":
            recent_search.dict.clear()

#http://code.activestate.com/recipes/362879-watermark-with-pil/
def reduce_opacity(im, opacity):
    """Returns an image with reduced opacity."""
    assert opacity >= 0 and opacity <= 1
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    return im

def watermark(im, mark, position, opacity=1):
    """Adds a watermark to an image."""
    if opacity < 1:
        mark = reduce_opacity(mark, opacity)
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    # create a transparent layer the size of the image and draw the
    # watermark in that layer.
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    if position == 'tile':
        for y in range(0, im.size[1], mark.size[1]):
            for x in range(0, im.size[0], mark.size[0]):
                layer.paste(mark, (x, y))
    elif position == 'scale':
        # scale, but preserve the aspect ratio
        # width of logo / width of image = 1/6
        # right margin of logo / width of image = 1/30
        # top margin = right margin
        ratio = float(mark.size[0]) / float(mark.size[1]) #min(float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
        w = int(im.size[0]/6)
        h = int(w/ratio)
        mark = mark.resize((w, h))
        layer.paste(mark, (int(im.size[0]*29/30 - w), int(im.size[0]/30)))
    else:
        layer.paste(mark, position)
    # composite the watermark with the layer
    return Image.composite(layer, im, layer)

#im = Image.open('test.png')
#mark = Image.open('overlay.png')
#watermark(im, mark, 'tile', 0.5).show()
#watermark(im, mark, 'scale', 1.0).show()
#watermark(im, mark, (100, 100), 0.5).show()

def add_watermark(f, im, extension, dirname, filename):
    #save a copy of the original image
    filename_org = filename.split(".")[0]+"_original."+filename.split(".")[1]
    if storage.exists(dirname + filename_org):
        storage.delete(dirname + filename_org)
    f_org = storage.open(dirname + filename_org, 'wb')
    im_out = BytesIO()
    if extension.lower() == '.jpg':
        extension = '.jpeg'
    im.save(im_out, format = extension[1:].upper(), quality=100)
    f_org.write(im_out.getvalue())
    f_org.close()

    mark = Image.open(os.path.join(settings.MEDIA_ROOT, 'img/tripalocal_watermark.png').replace('\\', '/'))
    im = watermark(im, mark, 'scale', 0.5)

    im_out = BytesIO()
    im.save(im_out, format = extension[1:].upper())
    f.close()
    f = storage.open(dirname + filename, 'wb')
    f.write(im_out.getvalue())
    f.close()

def is_datetime_naive(d):
    return (d.tzinfo is None or d.tzinfo.utcoffset(d) is None)


import time
import sys
import numpy as np
from glearn.utils.config import Configurable
from glearn.utils.reflection import get_class

class AdvancedViewer(Configurable):
    def __init__(self, config, display=None, width=None, height=None, zoom=1, modes=None,
                 fps=None):
        super().__init__(config)

        import pyglet

        self.isopen = False
        self.display = display

        self.images = {}
        self.labels = {}

        if width is None:
            width = 100
        if height is None:
            height = 100
        self.width = width
        self.height = height
        self.zoom = zoom
        self.fps = fps

        try:
            self.window = pyglet.window.Window(width=width * zoom, height=height * zoom,
                                               visible=False, display=self.display, vsync=False,
                                               resizable=True)
            self.window.push_handlers(self)
            self.isopen = True
        except Exception as e:
            self.error(f"Failed to create window: {e}")
            self.window = None

        self.last_frame = 0

        self.modes = []
        if modes is not None:
            for mode_config in modes:
                ModeClass = get_class(mode_config)
                mode = ModeClass(config)
                self.modes.append(mode)

    def close(self):
        if self.isopen:
            if sys.meta_path is not None:
                self.window.close()
            self.isopen = False

    def __del__(self):
        self.close()

    def initialize_gl(self):
        import pyglet.gl as gl

        # Set clear color
        gl.glClearColor(0, 0, 0, 0)

        # Set antialiasing
        gl.glEnable(gl.GL_LINE_SMOOTH)
        gl.glEnable(gl.GL_POLYGON_SMOOTH)
        gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)

        # Set alpha blending
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

        # gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)

        # Set viewport
        # gl.glViewport(0, 0, width, height)

    def prepare(self, trainer):
        self.trainer = trainer

        for mode in self.modes:
            mode.prepare(trainer)

    def view_results(self, query, feed_map, results):
        for mode in self.modes:
            mode.view_results(query, feed_map, results)

    def on_key_press(self, key, modifiers):
        for mode in self.modes:
            mode.on_key_press(key, modifiers)

    def set_zoom(self, zoom):
        self.zoom = zoom

        if self.window:
            self.window.set_size(self.width * self.zoom, self.height * self.zoom)
        pass

    def get_size(self):
        return (int(self.width), int(self.height))

    def set_size(self, width, height):
        self.width = width
        self.height = height

        if self.window:
            self.window.set_size(self.width * self.zoom, self.height * self.zoom)

    def on_resize(self, width, height):
        self.width = width / self.zoom
        self.height = height / self.zoom

    def on_close(self):
        self.isopen = False

    def set_main_image(self, values):
        height, width, chans = values.shape
        self.set_size(width, height)
        self.add_image("*", values, x=0, y=0, width=width, height=height)

    def imshow(self, arr):
        # backwards compatibility with Atari envs
        self.set_main_image(arr)

    def add_image(self, name, values, x=0, y=0, width=None, height=None):
        if not self.window:
            return

        import pyglet

        # get image data
        rows, cols, chans = values.shape
        if width is None:
            width = cols
        if height is None:
            height = rows

        # convert data to image format
        values = np.array(values).ravel().astype(int).tolist()
        bytes_conv = bytes(values)

        # infer format
        if chans >= 4:
            fmt = "RGBA"
        elif chans == 3:
            fmt = "RGB"
        elif chans == 1:
            fmt = "I"

        # TODO - could cache/reuse image objects and call set_data on them?
        image = pyglet.image.ImageData(cols, rows, fmt, bytes_conv, pitch=cols * -chans)

        self.images[name] = (image, x, y, width, height)

    def remove_image(self, name):
        self.images.pop(name, None)

    def remove_images(self, prefix):
        self.images = {name: image for name, image in self.images.items()
                       if not name.startswith(prefix)}

    def add_label(self, name, message, x=0, y=0, anchor_x='left', anchor_y='bottom',
                  font_name='Times New Roman', font_size=16, **kwargs):
        if not self.window:
            return

        import pyglet

        label = pyglet.text.Label(message, x=x, y=y, anchor_x=anchor_x, anchor_y=anchor_y,
                                  font_name=font_name, font_size=font_size, **kwargs)
        self.labels[name] = label

    def remove_label(self, name):
        self.labels.pop(name, None)

    def remove_labels(self, prefix):
        self.labels = {name: label for name, label in self.labels.items()
                       if not name.startswith(prefix)}

    def prepare_render(self):
        pass

    def render(self):
        if not self.window:
            return

        if len(self.images) + len(self.labels) == 0:
            return

        if not self.window.visible:
            self.window.set_visible(True)
            self.window.activate()

        # only render at desired framerate
        if self.fps is not None:
            t = time.time()
            interval = t - self.last_frame
            if interval >= 1 / self.fps:
                self.last_frame = t
            else:
                return

        self.window.clear()
        self.window.switch_to()
        self.window.dispatch_events()

        self.prepare_render()

        # TODO - HANDLE ZOOM
        # # Initialize Projection matrix
        # glMatrixMode( GL_PROJECTION )
        # glLoadIdentity()

        # # Initialize Modelview matrix
        # glMatrixMode( GL_MODELVIEW )
        # glLoadIdentity()
        # # Save the default modelview matrix
        # glPushMatrix()

        # # Clear window with ClearColor
        # glClear( GL_COLOR_BUFFER_BIT )

        # # Set orthographic projection matrix
        # glOrtho( self.left, self.right, self.bottom, self.top, 1, -1 )

        # draw custom images
        for image_data in self.images.values():
            image, x, y, width, height = image_data

            # apply zoom
            x *= self.zoom
            y *= self.zoom
            width *= self.zoom
            height *= self.zoom

            # TODO... pixel interpolation
            # gl.EnableTex2d(image.tex_id) ?
            # gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)

            # blit image
            image.blit(x, y, width=width, height=height)

        # draw custom labels
        for label in self.labels.values():
            # apply zoom
            base = (label.x, label.y, label.font_size, label.width, label.height)
            label.x *= self.zoom
            label.y *= self.zoom
            label.width = label.width * self.zoom if label.width is not None else None
            label.height = label.height * self.zoom if label.height is not None else None
            label.font_size *= self.zoom

            label.draw()

            label.x, label.y, label.font_size, label.width, label.height = base

        self.window.flip()


# PITCHSHIFTER
# This function implements the pitch shifter audio effect by using two parallel
# delay lines. The delay time for each line is modulated by a sawtooth LFO. The
# frequency of the LFO is based on the desired number of semitones for the
# pitch shifter. Both increases and decreases in pitch are possible with this
# function.
#
# Both LFOs repeat a cycle such that the delay time stays within a range of 0ms
# to 50ms. This way the processed signal is not significantly shorter or longer
# than the original signal.
#
# The cycles of the two LFOs are intentionally offset to have an overlap. An
# amplitude crossfade is applied to the delay lines to switch between the two
# during the overlap. The crossfade reduces the audibility of the relatively
# large discontinuity in the delay time at the start of the LFO cycle.
#
# See also PITCHSHIFTEREXAMPLE, CROSSFADES, LFOPITCH

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from crossfades import crossfades

def pitchShifter(x, Fs, semitones):
    Ts = 1 / Fs
    N = len(x)  # Total number of samples
    out = np.zeros(N)
    y1 = np.zeros(N)
    y2 = np.zeros(N)
    lfo1 = np.zeros(N)  # for visualizing LFOs
    lfo2 = np.zeros(N)

    maxDelay = int(Fs * .05)  # Maximum delay is 50ms
    buffer1 = np.zeros(maxDelay + 1)
    buffer2 = np.zeros(maxDelay + 1)
    wIndex1 = maxDelay
    wIndex2 = maxDelay

    tr = pow(2, semitones / 12)  # Convert semitones
    dRate = 1 - tr  # Delay rate of change

    tau = (maxDelay / abs(dRate)) * Ts  # Period of sawtooth LFO
    freq = 1 / tau  # Frequency of LFO

    fade = round((tau * Fs) / 8)  # Fade length is 1/8 of a cycle
    Hz = (freq / 2) * (8 / 7)  # Frequency crossfade due to overlap
    g1, g2 = crossfades(Fs, N, Hz, fade)  # Crossfade gains

    if dRate > 0:  # Pitch decrease
        # Initialize delay so LFO cycles line up with crossfade
        d1 = dRate * fade
        d2 = maxDelay
        d1Temp = d1  # These variables are used to control the length of
        d2Temp = d2  # each cycle of the LFO for the proper amount of overlap

    else:  # Pitch increase
        # Initialize delay so LFO cycles line up with crossfade
        d1 = maxDelay - (maxDelay / 8)
        d2 = 0
        d1Temp = d1
        d2Temp = d2

    # Loop to process input signal
    for n in range(N):
        # Parallel delay processing of the input signal
        intDelay1 = int(np.floor(d1))  # round down to get previous sample
        intDelay2 = int(np.floor(d2))

        frac1 = d1 - intDelay1  # find fractional amount
        frac2 = d2 - intDelay2

        rIndex1 = wIndex1 - intDelay1  # set location of read index
        rIndex2 = wIndex2 - intDelay2

        # Ensure read index is not exceeding the length of the buffer
        if rIndex1 < 0:
            rIndex1 += maxDelay

        if rIndex2 < 0:
            rIndex2 += maxDelay

        # Ensure read index does not go out of bounds of the buffer
        if rIndex1 == 0:
            y1 = (1 - frac1) * buffer1[rIndex1] + frac1 * buffer1[maxDelay]
        else:
            y1 = (1 - frac1) * buffer1[rIndex1] + frac1 * buffer1[rIndex1 - 1]

        if rIndex2 == 0:
            y2 = (1 - frac2) * buffer2[rIndex2] + frac2 * buffer2[maxDelay]
        else:
            y2 = (1 - frac2) * buffer2[rIndex2] + frac2 * buffer2[rIndex2 - 1]

        # Store the current output in circular buffer
        buffer1[wIndex1] = x[n]
        buffer2[wIndex2] = x[n]

        wIndex1 += 1
        if wIndex1 > maxDelay:
            wIndex1 = 1

        wIndex2 += 1
        if wIndex2 > maxDelay:
            wIndex2 = 1

        # Use crossfade gains to combine the output of each delay
        out[n] = g1[n] * y1 + g2[n] * y2

        lfo1[n] = d1  # Save the current delay times for plotting
        lfo2[n] = d2

        # The following conditions are set up to control the
        # overlap of the sawtooth LFOs
        if dRate < 0:  # Slope of LFO is negative (pitch up)
            d1 += dRate
            d1Temp += dRate
            if d1 < 0:
                d1 = 0  # Portion of LFO where delay time = 0

            if d1Temp < -maxDelay * (6 / 8):  # Start next cycle
                d1 = maxDelay
                d1Temp = maxDelay

            d2 += dRate
            d2Temp += dRate
            if d2 < 0:
                d2 = 0  # Portion of LFO where delay time = 0

            if d2Temp < -maxDelay * (6 / 8):  # Start next cycle
                d2 = maxDelay
                d2Temp = maxDelay

        else:  # Slope of LFO is positive (pitch down)
            d1Temp += dRate
            if d1Temp > maxDelay:  # Start next cycle
                d1 = 0
                d1Temp = -maxDelay * (6 / 8)

            elif d1Temp < 0:
                d1 = 0  # Portion where delay time = 0

            else:
                d1 += dRate

            d2Temp += dRate
            if d2Temp > maxDelay:  # Start next cycle
                d2 = 0
                d2Temp = -maxDelay * (6 / 8)

            elif d2Temp < 0:
                d2 = 0  # Portion where delay time = 0

            else:
                d2 += dRate

    # Plotting
    t = np.arange(0, N) * Ts
    plt.subplot(4, 1, 1)  # Waveform
    plt.plot(t, lfo1, t, lfo2)
    plt.axis([0, t[-1], -100, maxDelay])
    plt.ylabel('Delay')

    plt.subplot(4, 1, 2)
    plt.plot(t, g1, t, g2)  # Crossfade gains
    plt.axis([0, t[-1], -0.1, 1.1])
    plt.ylabel('Amplitude')

    plt.figure()
    nfft = 2048  # Length of each time frame
    window = signal.windows.hann(nfft)  # Calculated window function
    overlap = 128  # Number of samples for frame overlap
    spec, f, tSpec, imAxis = plt.specgram(out)
    plt.show()

    return out

Ch. 15 - Modulated Delay Effects/pitchShifter.py


import sqlite3
from sqlite3 import Error

# need to create a db connection
def create_db(db_file):
    """ create a database connection to a SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)
    finally:
        if conn:
            conn.close()

# where i defined the path to the database
if __name__ == '__main__':
    create_db(r"C:\Users\heath\PycharmProjects\final\commands\Courses.db")