from django.shortcuts import redirect
from django.urls import include, re_path

from olympia.addons.urls import ADDON_ID
from olympia.amo.decorators import use_primary_db
from olympia.amo.utils import partial
from olympia.lib.misc.urlconf_decorator import decorate

from . import views

# These will all start with /addon/<addon_id>/
detail_patterns = [
    # Redirect to the edit page from the base.
    re_path(
        r'^$',
        lambda r, addon_id: redirect('devhub.addons.edit', addon_id, permanent=True),
    ),
    re_path(r'^edit$', views.edit, name='devhub.addons.edit'),
    re_path(r'^delete$', views.delete, name='devhub.addons.delete'),
    re_path(r'^disable$', views.disable, name='devhub.addons.disable'),
    re_path(r'^enable$', views.enable, name='devhub.addons.enable'),
    re_path(
        r'^cancel-latest-(?P<channel>listed|unlisted)$',
        views.cancel,
        name='devhub.addons.cancel',
    ),
    re_path(r'^ownership$', views.ownership, name='devhub.addons.owner'),
    re_path(r'^invitation$', views.invitation, name='devhub.addons.invitation'),
    re_path(
        r'^edit_(?P<section>[^/]+)(?:/(?P<editable>[^/]+))?$',
        views.addons_section,
        name='devhub.addons.section',
    ),
    re_path(
        r'^upload_preview$',
        views.upload_image,
        {'upload_type': 'preview'},
        name='devhub.addons.upload_preview',
    ),
    re_path(
        r'^upload_icon$',
        views.upload_image,
        {'upload_type': 'icon'},
        name='devhub.addons.upload_icon',
    ),
    re_path(
        r'^upload-(?P<channel>listed|unlisted)$',
        views.upload_for_version,
        name='devhub.upload_for_version',
    ),
    re_path(
        r'^upload/(?P<uuid>[^/]+)$',
        views.upload_detail_for_version,
        name='devhub.upload_detail_for_version',
    ),
    re_path(r'^versions$', views.version_list, name='devhub.addons.versions'),
    re_path(r'^versions/delete$', views.version_delete, name='devhub.versions.delete'),
    re_path(
        r'^versions/reenable$', views.version_reenable, name='devhub.versions.reenable'
    ),
    re_path(r'^versions/stats$', views.version_stats, name='devhub.versions.stats'),
    re_path(
        r'^versions/(?P<version_id>\d+)$',
        views.version_edit,
        name='devhub.versions.edit',
    ),
    re_path(r'^versions/(?P<version>[^/]+)$', views.version_bounce),
    # New version submission
    re_path(
        r'^versions/submit/$', views.submit_version_auto, name='devhub.submit.version'
    ),
    re_path(
        r'^versions/submit/agreement$',
        views.submit_version_agreement,
        name='devhub.submit.version.agreement',
    ),
    re_path(
        r'^versions/submit/distribution$',
        views.submit_version_distribution,
        name='devhub.submit.version.distribution',
    ),
    re_path(
        r'^versions/submit/upload-(?P<channel>listed|unlisted)$',
        views.submit_version_upload,
        name='devhub.submit.version.upload',
    ),
    re_path(
        r'^versions/submit/(?P<version_id>\d+)/source$',
        views.submit_version_source,
        name='devhub.submit.version.source',
    ),
    re_path(
        r'^versions/submit/(?P<version_id>\d+)/details$',
        views.submit_version_details,
        name='devhub.submit.version.details',
    ),
    re_path(
        r'^versions/submit/(?P<version_id>\d+)/finish$',
        views.submit_version_finish,
        name='devhub.submit.version.finish',
    ),
    re_path(
        r'^versions/submit/wizard-(?P<channel>listed|unlisted)$',
        views.submit_version_theme_wizard,
        name='devhub.submit.version.wizard',
    ),
    re_path(
        r'^versions/submit/wizard-(?P<channel>listed|unlisted)/background$',
        views.theme_background_image,
        name='devhub.submit.version.previous_background',
    ),
    re_path(
        r'^file/(?P<file_id>[^/]+)/validation$',
        views.file_validation,
        name='devhub.file_validation',
    ),
    re_path(
        r'^file/(?P<file_id>[^/]+)/validation\.json$',
        views.json_file_validation,
        name='devhub.json_file_validation',
    ),
    re_path(
        r'^submit/$', lambda r, addon_id: redirect('devhub.submit.finish', addon_id)
    ),
    re_path(
        r'^submit/source-(?P<channel>listed|unlisted)$',
        views.submit_addon_source,
        name='devhub.submit.source',
    ),
    re_path(
        r'^submit/details$', views.submit_addon_details, name='devhub.submit.details'
    ),
    re_path(r'^submit/finish$', views.submit_addon_finish, name='devhub.submit.finish'),
    re_path(r'^request-review$', views.request_review, name='devhub.request-review'),
    re_path(r'^rmlocale$', views.remove_locale, name='devhub.addons.remove-locale'),
]
# These will all start with /ajax/addon/<addon_id>/
ajax_patterns = [
    re_path(r'^image/status$', views.image_status, name='devhub.ajax.image.status'),
]
redirect_patterns = [
    re_path(
        r'^addon/edit/(\d+)',
        lambda r, id: redirect('devhub.addons.edit', id, permanent=True),
    ),
    re_path(
        r'^addon/status/(\d+)',
        lambda r, id: redirect('devhub.addons.versions', id, permanent=True),
    ),
    re_path(
        r'^versions/(\d+)',
        lambda r, id: redirect('devhub.addons.versions', id, permanent=True),
    ),
]

urlpatterns = decorate(
    use_primary_db,
    [
        re_path(r'^$', views.index, name='devhub.index'),
        re_path(r'', include(redirect_patterns)),
        # Redirect people who have /addons/ instead of /addon/.
        re_path(
            r'^addons/\d+/.*', lambda r: redirect(r.path.replace('addons', 'addon', 1))
        ),
        # Add-on submission
        re_path(
            r'^addon/submit/(?:1)?$',
            lambda r: redirect('devhub.submit.agreement', permanent=True),
        ),
        re_path(
            r'^addon/submit/agreement$',
            views.submit_addon,
            name='devhub.submit.agreement',
        ),
        re_path(
            r'^addon/submit/distribution$',
            views.submit_addon_distribution,
            name='devhub.submit.distribution',
        ),
        re_path(
            r'^addon/submit/upload-(?P<channel>listed|unlisted)$',
            views.submit_addon_upload,
            name='devhub.submit.upload',
        ),
        # Theme-specific submission pages
        re_path(
            r'^addon/submit/theme/agreement$',
            views.submit_theme,
            name='devhub.submit.theme.agreement',
        ),
        re_path(
            r'^addon/submit/theme/distribution$',
            views.submit_theme_distribution,
            name='devhub.submit.theme.distribution',
        ),
        re_path(
            r'^addon/submit/theme/upload-(?P<channel>listed|unlisted)$',
            views.submit_theme_upload,
            name='devhub.submit.theme.upload',
        ),
        re_path(
            r'^addon/submit/wizard-(?P<channel>listed|unlisted)$',
            views.submit_addon_theme_wizard,
            name='devhub.submit.wizard',
        ),
        # Submission API
        re_path(
            r'^addon/agreement/$',
            views.developer_agreement,
            name='devhub.developer_agreement',
        ),
        re_path(r'^addon/api/key/$', views.api_key, name='devhub.api_key'),
        # Standalone validator:
        re_path(
            r'^addon/validate/?$', views.validate_addon, name='devhub.validate_addon'
        ),
        # Redirect to /addons/ at the base.
        re_path(r'^addon$', lambda r: redirect('devhub.addons', permanent=True)),
        re_path(r'^addons$', views.dashboard, name='devhub.addons'),
        re_path(
            r'^themes$', views.dashboard, name='devhub.themes', kwargs={'theme': True}
        ),
        re_path(r'^feed$', views.feed, name='devhub.feed_all'),
        # TODO: not necessary when devhub homepage is moved out of remora
        re_path(r'^feed/all$', lambda r: redirect('devhub.feed_all', permanent=True)),
        re_path(r'^feed/%s$' % ADDON_ID, views.feed, name='devhub.feed'),
        re_path(r'^upload$', views.upload, name='devhub.upload'),
        re_path(
            r'^upload/unlisted$',
            partial(views.upload, channel='unlisted'),
            name='devhub.upload_unlisted',
        ),
        re_path(
            r'^upload/([^/]+)(?:/([^/]+))?$',
            views.upload_detail,
            name='devhub.upload_detail',
        ),
        re_path(
            r'^standalone-upload$',
            partial(views.upload, is_standalone=True),
            name='devhub.standalone_upload',
        ),
        re_path(
            r'^standalone-upload-unlisted$',
            partial(views.upload, is_standalone=True, channel='unlisted'),
            name='devhub.standalone_upload_unlisted',
        ),
        re_path(
            r'^standalone-upload/([^/]+)$',
            views.standalone_upload_detail,
            name='devhub.standalone_upload_detail',
        ),
        # URLs for a single add-on.
        re_path(r'^addon/%s/' % ADDON_ID, include(detail_patterns)),
        re_path(r'^ajax/addon/%s/' % ADDON_ID, include(ajax_patterns)),
        # Old LWT Theme submission.
        re_path(
            r'^theme/submit/?$',
            lambda r: redirect('devhub.submit.theme.agreement'),
            name='devhub.submit.theme.old_lwt_flow',
        ),
        # Add-on SDK page
        re_path(r'builder$', lambda r: redirect(views.MDN_BASE)),
        # Developer docs
        re_path(
            r'docs/(?P<doc_name>[-_\w]+(?:/[-_\w]+)?)?$', views.docs, name='devhub.docs'
        ),
        # logout page
        re_path(r'^logout', views.logout, name='devhub.logout'),
    ],
)


import os
import requests
from collections import deque
from sys import argv

from bs4 import BeautifulSoup
from colorama import init, Fore, deinit

print('J)une OS N)ew F)ind R)un V)iew A)pps')
print()
print()
print('Welcome to June OS by Piper')
print()
print()
print('The aim is to create an easy-to-use CLI')
print()
print()

while True:
    command = input('juneos> ')

    if command == 'N':
        filename = input('Enter the file name to add to or create new: ')
        file_content = input('Enter the content of the file: ')

        try:
            with open('files/' + filename, 'w') as file:
                file.write(file_content)
            print('File saved')
        except:
            print('Error occurred while saving the file.')

    elif command == 'F':
        files = os.listdir('files/')
        print('Here are all your files')
        for file in files:
            print(file)

    elif command == 'J':
        print("Hi I'm June an OS designed to be simple and easy to use. Currently I'm version 0.1 whatever that means :)")

    elif command == 'V':
        filename = input('Enter the name of the file you want to view: ')
        try:
            with open('files/' + filename, 'r') as file:
                file_contents = file.read()
                print('Contents of ' + filename)
                print(file_contents)
        except FileNotFoundError:
            print('File not found.')
        except:
            print('Error occurred while reading the file.')

    elif command == 'R':
        filename = input('Enter the name of the Python program you want to run: ')
        try:
            exec(open('files/' + filename).read())
        except FileNotFoundError:
            print("I can't find that file.")
        except Exception as e:
            print('Error occurred while running the program, whoops:', e)

    elif command == 'A':
        print('Here is what I can do. Type anything else to go back.')
        print('')
        print('1. Calculate')
        print('2. Access the internet')
        print('3. Time')
        print('4. Games')
        print('5. Be a therapist / Just chat with me')
        choose = input('choose> ')
        if choose == '1':
          exec(open('calculate.py').read())
        elif choose  == '2':
          exec(open('it.py').read())
        elif choose  == '3':
          exec(open('time.py').read())
        elif choose  == '4':
          exec(open('game.py').read())
        elif choose  == '5':
          exec(open('ai.py').read())
        elif choose  == '6':
          exec(open('chat.py').read())
    else:
        print("Sorry, I can't do that.")


#!/usr/bin/env python3
#reaction_find_connections - find connections between organisms
import sys

organisms = {}
for filename in sys.argv[1:]:
    with open(filename) as current:
        organisms[filename] = {'ins': set(), 'outs': set()}
        for line in current:
            record = line.rstrip('\n').split('\t')
            organisms[filename]['ins'].update(record[1].split('+'))
            organisms[filename]['outs'].update(record[3].split('+'))
            if record[2] == "=":
                organisms[filename]['ins'].update(record[3].split('+'))
                organisms[filename]['outs'].update(record[1].split('+'))

for name, current in organisms.items():
    for cpd in current['outs']:
        for othername, other in organisms.items():
            #if other == current: continue
            if cpd in other['ins']:
                print("\t".join([name, cpd, othername]))


from commoncrawl import get_cc_docs

import os
import tarfile
import codecs
from tqdm import tqdm
import fasttext

def preprocess_for_fasttext(x):
    return x.replace('\n', ' ')

def limit(x, num):
    try:
        for _ in range(num):
            yield next(x)
    except StopIteration:
        return

if __name__ == '__main__':
    num_owt = 0

    with open('fasttext_training.txt', 'w') as fout:
        print('Processing OWT data')
        for subset in tqdm(os.listdir('openwebtext')):
            tar = tarfile.open('openwebtext/' + subset, encoding='utf-8')
            utf8reader = codecs.getreader('utf-8')
            for name in tar.getmembers():
                fp = utf8reader(tar.extractfile(name))
                contents = fp.read()
                fout.write('__label__owt ' + preprocess_for_fasttext(contents) + '\n')
                num_owt += 1

        print('Processed', num_owt, 'documents from OWT')

        print('Processing CC data')
        for doc in tqdm(limit(get_cc_docs(), num_owt), total=num_owt):
            fout.write('__label__cc ' + preprocess_for_fasttext(doc) + '\n')

    print('Training fasttext')
    model = fasttext.train_supervised(input="fasttext_training.txt")
    model.save_model('fasttext_filter.bin')


# -*- coding: utf-8 -*-
"""
    The functions for preparing the data.

:Author: Yan Zhou

:License: BSD 3-Clause, see LICENSE file.
"""

from Brian2_scripts.sim_brian_paper.sim_brian_paper_CoE.src.core import BaseFunctions

import os
import shutil, zipfile
from PIL import Image as PIL_Image
import cv2 as cv

import numpy as np
import pandas as pd

class BN_classification(BaseFunctions):
    """
    Class used to load BN dataset

    """
    def __init__(self):
        self.IMG_HEIGHT = 100
        self.IMG_WIDTH = 150
        self.NUM_FRAMES = 20

    def _get_category(self, path_category):
        """Loads the Dataframe labels from a csv and creates dictionnaries to convert the string labels to int and backwards
        # Arguments
            path_labels : path to the csv containing the labels
        """
        self.category_df = pd.read_csv(path_category, names=['category'])
        # Extracting list of labels from the dataframe
        self.category = [str(category[0]) for category in self.category_df.values]
        self.n_category = len(self.category)
        # Create dictionnaries to convert label to int and backwards
        self.CATEGORIES = dict(zip(self.category, range(self.n_category)))
        self.label_to_category = dict(enumerate(self.category))

    def _load_video_category(self, path_subset, mode="category"):
        """ Loads a Dataframe from a csv
        # Arguments
            path_subset : String, path to the csv to load
            mode        : String, (default: label), if mode is set to "label", filters rows given if the labels exists in the labels Dataframe loaded previously
        #Returns
            A DataFrame
        """
        if mode == "input":
            names = ['video_id']
        elif mode == "category":
            names = ['video_id', 'category']
        df = pd.read_csv(path_subset, sep=';', names=names)
        if mode == "category":
            df = df[df.category.isin(self.category)]
        return df

    def load_labels(self):
        self._get_category(self.path_labels)
        if self.path_train:
            self.train = self._load_video_category(self.path_train)
        if self.path_validation:
            self.validation = self._load_video_category(self.path_validation)
        if self.path_test:
            self.test = self._load_video_category(self.path_test, 'input')

    def normalize2(self, number):
        stre = str(number)
        while len(stre) < 5:
            stre = "0" + stre
        return stre

    def extract(self, z, sub_path):
        i = 1
        while True:
            try:
                z.extract(sub_path + self.normalize2(i) + '.jpg', path=self.data_path)
                i += 1
            except KeyError:
                break

    def delete(self, sub_path):
        shutil.rmtree(sub_path)

    def load_videos(self, videoIds, z):
        video_set = []
        for counterVideo, videoId in enumerate(videoIds, 1):
            self.extract(z, str(videoId) + '/')
            directory = os.path.join(self.data_path, str(videoId) + '/')
            if len(os.listdir(directory)) >= self.NUM_FRAMES:  # don't use if video length is too small
                video = []
                for counterImage, image in enumerate(os.listdir(directory), 1):
                    image_values = PIL_Image.open(os.path.join(directory, image))
                    image_values = image_values.convert('RGB')  # L: converts to greyscale
                    image_values = image_values.resize((self.IMG_WIDTH, self.IMG_HEIGHT), PIL_Image.ANTIALIAS)
                    video.append(np.asarray(image_values).reshape(-1))
                video_set.append(np.asarray(video))
            else:
                # print("skipped video # %d with length %d" % (counterVideo, len(os.listdir(directory))))
                video_set.append(None)
            self.delete(directory)
        return np.asarray(video_set)

    def optical_flow(self, frames, origin_size=(100, 150, 3)):
        frame1 = frames[0].reshape(origin_size)
        prvs = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY)
        hsv = np.zeros_like(frame1)
        hsv[..., 1] = 255
        bgrs = []
        for frame_ in frames[1:]:
            frame2 = frame_.reshape(origin_size)
            next = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)
            # 返回一个两通道的光流向量，实际上是每个点的像素位移值
            flow = cv.calcOpticalFlowFarneback(prvs, next, None,
                                               0.5, 3, 5,
                                               3, 5, 1.1, 0)
            # 笛卡尔坐标转换为极坐标，获得极轴和极角
            mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])

            hsv[..., 0] = ang * 180 / np.pi / 2  # 角度
            hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
            bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
            bgrs.append(bgr)
            prvs = next
        return bgrs

    def load_data_BN(self, videoId_df):
        frames = self.load_videos(videoId_df.video_id.values, self.z)
        videoId_df_ = videoId_df.copy()
        videoId_df_['frames'] = frames
        return videoId_df_.dropna(axis=0, subset=['frames'])

    def load_data_BN_all(self, data_path):
        self.data_path = data_path
        self.path_vid = data_path + '20bn-jester-v1.zip'
        self.path_labels = data_path + 'jester-v1-labels.csv'
        self.path_train = data_path + 'jester-v1-train.csv'
        self.path_validation = data_path + 'jester-v1-validation.csv'
        self.path_test = data_path + 'jester-v1-test.csv'
        self.load_labels()
        self.z = zipfile.ZipFile(self.path_vid, 'r', zipfile.ZIP_DEFLATED)

    def frame_diff(self, frames, origin_size=(100, 150, 3), with_optical_flow = True):
        frame_diff = []
        it = frames.__iter__()
        frame_pre = cv.cvtColor(next(it).reshape(origin_size), cv.COLOR_BGR2GRAY)
        while True:
            try:
                frame = cv.cvtColor(next(it).reshape(origin_size), cv.COLOR_BGR2GRAY)
                frame_diff.append(np.abs(frame_pre.astype(int) - frame.astype(int)).reshape(-1))
                frame_pre = frame
            except StopIteration:
                break
        if with_optical_flow:
            bgrs = self.optical_flow(frames, origin_size)
            frame_diff_masked = []
            for diff, bgr in zip(frame_diff, bgrs):
                masked = diff.reshape(origin_size[:2]) * (cv.cvtColor(bgr, cv.COLOR_BGR2GRAY).astype(int)/255)
                frame_diff_masked.append(masked)
            return np.asarray(frame_diff_masked)
        else:
            return np.asarray(frame_diff)

    def block_array(self, matrix, size):
        if int(matrix.shape[0] % size[0]) == 0 and int(matrix.shape[1] % size[1]) == 0:
            X = int(matrix.shape[0] / size[0])
            Y = int(matrix.shape[1] / size[1])
            shape = (X, Y, size[0], size[1])
            strides = matrix.itemsize * np.array([matrix.shape[1] * size[0], size[1], matrix.shape[1], 1])
            squares = np.lib.stride_tricks.as_strided(matrix, shape=shape, strides=strides)
            return squares
        else:
            raise ValueError('matrix must be divided by size exactly')

    def pooling(self, frames, origin_size=(100, 150), pool_size=(5, 5), types='max'):
        data = []
        for frame in frames:
            pool = np.zeros((int(origin_size[0] / pool_size[0]), int(origin_size[1] / pool_size[1])), dtype=np.int16)
            frame_block = self.block_array(frame.reshape(origin_size), pool_size)
            for i, row in enumerate(frame_block):
                for j, block in enumerate(row):
                    if types == 'max':
                        pool[i][j] = block.max()
                    elif types == 'average':
                        pool[i][j] = block.mean()
                    else:
                        raise ValueError('I have not done that type yet..')
            data.append(pool.reshape(-1))
        return np.asarray(data)

    def threshold_norm(self, frames, threshold):
        frames = (frames - np.min(frames)) / (np.max(frames) - np.min(frames))
        frames[frames < threshold] = 0
        frames[frames > threshold] = 1
        frames = frames.astype('<i1')
        return frames

    def select_data_BN(self, fraction, data_frame, is_order=True, **kwargs):
        try:
            selected = [self.label_to_category[x] for x in kwargs['selected']]
        except KeyError:
            selected = self.CATEGORIES.keys()
        if  fraction > 1:
            data_frame_selected = pd.DataFrame(columns=data_frame.columns)
            for s in selected:
                data_frame_selected = data_frame_selected.append(data_frame[data_frame['category'].isin([s])].sample(
                    n=fraction))
        else:
            data_frame_selected = data_frame[data_frame['category'].isin(selected)].sample(frac=fraction)

        if is_order:
            data_frame_selected = data_frame_selected.sort_index().reset_index(drop=True)
        else:
            data_frame_selected = data_frame_selected.reset_index(drop=True)
        return self.load_data_BN(data_frame_selected)

    def encoding_latency_BN(self, analog_data, origin_size=(100, 150, 3), pool_size=(5, 5), types='max', threshold=0.2,
                            with_optical_flow=True):
        data_diff = analog_data.frames.apply(self.frame_diff, origin_size=origin_size, with_optical_flow = with_optical_flow)
        data_diff_pool = data_diff.apply(self.pooling, origin_size=origin_size[:2], pool_size=pool_size, types=types)
        data_diff_pool_threshold_norm = data_diff_pool.apply(self.threshold_norm, threshold=threshold)
        label = analog_data.category.map(self.CATEGORIES).astype('<i1')
        data_frame = pd.DataFrame({'value': data_diff_pool_threshold_norm, 'label': label})
        return data_frame


from csv import writer
from searchtweets import ResultStream, gen_rule_payload, load_credentials, collect_results
from tweet_parser.getter_methods.tweet_geo import get_profile_location

## Authentication, search_tweets_fullarchive_dev or search_tweets_30_day_dev
premium_search_args = load_credentials(filename="./twitter_keys.yaml",
                 yaml_key="search_tweets_fullarchive_dev",
                 env_overwrite=False)

country_code = "mx"
out_file = "migrantes.csv"

rule = gen_rule_payload("(migrantes) place_country:"+country_code,
    results_per_call=100,
    from_date="2016-06-01",
    to_date="2017-06-01") # testing with a sandbox account

print(rule)

def fetch_tweets():

    tweets = collect_results(rule,
                         max_results=10,
                         result_stream_args=premium_search_args)

    with open(out_file, 'w') as out:
        csv = writer(out)

        row = ("user", "city/state", "country", "text", "created_at")
        csv.writerow(row)

        for tweet in tweets:
            place_name = ""

            place = tweet.get("place")
            if place != None:
                place_name = place["full_name"]

            print(tweet)
            row = (
                tweet.get("screen_name"),
                place_name,
                country_code,
                tweet.all_text,
                tweet.created_at_string ##tweet.created_at_datetime.strftime("%Y-%m-%d|%H:%M:%S")
            )

            values = [(value.encode('unicode') if hasattr(value, 'encode') else value) for value in row]
            csv.writerow(values)
    out.close()

fetch_tweets()

##print(tweets[0].get("place")["name"])
##tweet.retweeted_tweet
##print(len(tweets))


__author__ = 'Kalyan'

problem_notes = '''
For this assignment create a decorator which can be used for count profiling.

@count_calls:
- add a count attribute to the function
- count keeps track of number of calls made to the function. If the function is recursive, give a parameter
to track only top level call and not the sub calls.

You may want to give special behavior for how this decorators behave when exceptions are raised etc. but for now
just count all calls irrespective of error status.

Assume that this decorators are used in single threaded programs only for now.

I used count for testing simplicity, you could write @time_calls decorator in a very similar spirit. Try it out as a
personal exercise and see what all design issues you hit when you chain it with above!
'''
import functools

def count_calls(skip_recursion=True):
    def log(func):
        if skip_recursion:
            def inner(n):
                if inner.initial:
                    inner.count = inner.count + 1
                    inner.value = n
                    inner.greater = True
                    inner.initial = False
                result = func(n)
                if inner.value == n:
                    inner.initial = True
                    inner.value = 0
                return result

            inner.count = 0
            inner.initial = True
            inner.value = 0
            return inner
        else:
            def inner(n):
                inner.count = inner.count + 1
                return func(n)

            inner.count = 0
            return inner

    return log

def test_calls_decorator():
    @count_calls()
    def fib(n):
        if n <= 0:
            raise ValueError("n <= 0")
        if n == 1 or n == 2:
            return 1

        return fib(n-1) + fib(n-2)

    assert [1, 1, 2, 3, 5 ] == [fib(i) for i in range(1,6)]
    assert 5 == fib.count # only top calls counted.

    # with recursion, count all calls, but time only top level calls.
    @count_calls(skip_recursion=False)
    def fib(n):
        if n == 1 or n == 2:
            return 1

        return fib(n-1) + fib(n-2)

    assert [1, 1, 2, 3, 5 ] == [fib(i) for i in range(1,6)]
    assert 19 == fib.count # all calls counted.


# -*- coding: utf-8 -*-
"""
@Time ：　2020/12/31
@Auth ：　xiaer.wang
@File ：　run_test.py
@IDE 　：　PyCharm
"""
from pathlib import Path
from dataloader import DataSetDir
from method import Kmeans,GMMs,DBScan,AC,Optics, Louvain
from sklearn.cluster import OPTICS,DBSCAN
import numpy as np
from evaluate import select_evaluate_func,metrics_adjusted_randn_index,\
    metrics_normalized_mutual_info_score,metrics_fowlkes_mallows_score
from config import DataConfig,DATA_ROOT
from utils import set_random_seed
from logger import Logger
from utils import split_sub_train_set_by_dev_set, get_word_idxes_and_cluster_idxes, \
    grid_search_model_params
from args import parser
args = parser.parse_args()
cwd = Path.cwd()
SEED = 2020

def floatrange(start,stop,steps):
    return [start+float(i)*(stop-start)/(float(steps)-1) for i in range(steps)]
def format_output(data, bit = 2):

    if isinstance(data, float):
        return round(data * 100, bit)
    elif isinstance(data, list):
       return [round(d * 100, bit)  for d in data]

if __name__ == '__main__':

    DataConfig['data_name'] = args.data
    DataConfig['method_type'] = args.method
    DataConfig['word_emb_select'] = args.embed
    datadir_name = DataConfig['data_name']
    data = DATA_ROOT.joinpath(datadir_name)
    #TODO : need modify (word_emb_select) -> choice ['combined.embed', 'Tencent_combined.embed']
    word_emb_select = DataConfig['word_emb_select']
    method_type = DataConfig['method_type']

    datadir = DataSetDir(data,word_emb_select = word_emb_select)
    word2id = datadir.word2id
    embedding = datadir.embedding_vec

    train_vocab = datadir.train_dataset.vocab
    train_sets = datadir.train_dataset.raw_sets

    dev_vocab = datadir.dev_dataset.vocab
    dev_sets = datadir.dev_dataset.raw_sets

    # # for train
    # train_flat_word_list, train_word_idxes, train_cluster_idxes = get_word_idxes_and_cluster_idxes(train_sets, train_vocab, word2id)
    # train_word_embeddings = embedding[np.array(train_word_idxes)]

    run_times = DataConfig['run_times']
    seed_list = DataConfig['seed_list']
    ari_list, nmi_list, fmi_list = [], [], []
    for i in range(run_times):
        # TODO : model
        set_random_seed(seed=seed_list[i])
        dev_flat_word_list, dev_word_idxes, dev_cluster_idxes = get_word_idxes_and_cluster_idxes(dev_sets, dev_vocab,word2id)
        dev_word_embeddings = embedding[np.array(dev_word_idxes)]

        if method_type == 'kmeans':
            # set cluster number by prior
            k_cluster = len(dev_cluster_idxes.keys())
            model = Kmeans(n_cluster=k_cluster, seed=seed_list[i])
            pred_labels = model.predict(dev_word_embeddings)
        elif method_type == 'gmms':
            k_component = len(dev_cluster_idxes.keys())
            model = GMMs(n_component=k_component, seed=seed_list[i])
            pred_labels = model.predict(dev_word_embeddings)
        elif method_type == 'ac':
            k_cluster = len(dev_cluster_idxes.keys())
            model = AC(n_cluster=k_cluster)
            pred_labels = model.predict(dev_word_embeddings)

        elif method_type == 'optics':
            if i == 0:
                sub_train_sets, sub_train_vocab, _,_ = split_sub_train_set_by_dev_set(train_sets, train_vocab, dev_sets)
                min_samples_list = list(range(1, 10))
                learn_params = dict(min_samples=min_samples_list)
                model = OPTICS()
                best_params = grid_search_model_params(dev_sets, dev_vocab, word2id,
                                                       embedding,model,learn_params,score_index='ARI')
                best_min_sample = best_params['min_samples']
                del sub_train_sets, sub_train_vocab

            model = Optics(min_sample = best_min_sample)
            pred_labels = model.predict(dev_word_embeddings)

        elif method_type == 'dbscan':

            if i == 0:
                sub_train_sets, sub_train_vocab, _, _ = split_sub_train_set_by_dev_set(train_sets, train_vocab, dev_sets)
                eps_list = list(floatrange(0.1, 10, 10))
                min_samples_list = list(range(1, 10, 10))
                learn_params = dict(eps = eps_list, min_samples = min_samples_list)
                model = DBSCAN()
                best_params = grid_search_model_params(dev_sets, dev_vocab, word2id,
                                                       embedding,model,learn_params,score_index='ARI')
                best_min_sample = best_params['min_samples']
                best_eps = best_params['eps']
                del sub_train_sets, sub_train_vocab

            model = DBScan(eps = best_eps, min_sample = best_min_sample)
            pred_labels = model.predict(dev_word_embeddings)

        elif method_type == 'louvain':
            if i == 0:
                sub_train_sets, sub_train_vocab, _, _ = split_sub_train_set_by_dev_set(train_sets, train_vocab,dev_sets)
                thresold_list = list(floatrange(0.2, 0.8, 10))
                param_dict = {}
                best_thresold = 0
                best_score = 0.
                for thresold in thresold_list:

                    sub_train_flat_word_list, sub_train_word_idxes, sub_train_cluster_idxes = get_word_idxes_and_cluster_idxes(sub_train_sets,sub_train_vocab,word2id)
                    sub_train_word_embeddings = embedding[np.array(sub_train_word_idxes)]
                    sub_train_labels = np.array([sub_train_cluster_idxes[sub_train_vocab[word]] for word in sub_train_flat_word_list])
                    model = Louvain(dev_flat_word_list, measure='cosine', thresold= thresold)
                    pred_labels = model.predict(dev_word_embeddings)
                    target_dict = {word: sub_train_cluster_idxes[cluster] for word, cluster in sub_train_vocab.items()}
                    pred_dict = {word: label for word, label in zip(sub_train_flat_word_list, pred_labels)}
                    _, ARI = metrics_adjusted_randn_index(pred_dict, target_dict)
                    # _, NMI = metrics_normalized_mutual_info_score(pred_dict, target_dict)
                    # _, FMI = metrics_fowlkes_mallows_score(pred_dict, target_dict)
                    score = ARI
                    # print('calcuate thresold : {:.3f}, ARI : {:.3f}, NMI : {:.3f}, FMI : {:.3f}'.format(thresold, ARI, NMI, FMI))
                    if score > best_score:
                        best_score = score
                        best_thresold = thresold
                print('Use best thresold : {:.3f}'.format(best_thresold))
            model = Louvain(dev_flat_word_list, measure='cosine', thresold=best_thresold)
            pred_labels = model.predict(dev_word_embeddings)

        else:
            raise KeyError('Method Key Error')
        target_dict = {word : dev_cluster_idxes[cluster] for word, cluster in dev_vocab.items()}
        pred_dict = {word : label for word,label in zip(dev_flat_word_list,pred_labels)}
        # result_list = select_evaluate_func(['ARI','NMI','FMI'])
        # import pdb;pdb.set_trace()
        _, ARI = metrics_adjusted_randn_index(pred_dict, target_dict)
        _, NMI = metrics_normalized_mutual_info_score(pred_dict,target_dict)
        _, FMI = metrics_fowlkes_mallows_score(pred_dict, target_dict)
        ari_list.append(ARI)
        nmi_list.append(NMI)
        fmi_list.append(FMI)

    ari_avg, ari_std = np.mean(ari_list), np.std(ari_list)
    nmi_avg, nmi_std = np.mean(nmi_list), np.std(nmi_list)
    fmi_avg, fmi_std = np.mean(fmi_list), np.std(fmi_list)

    log_str = '\n' + '='*40 +f'\nUse method : <{method_type}> || deal with dataset : <{datadir_name}> || embedding type: <{word_emb_select}>\n'+'='*40
    log_str += f'\nRun {run_times} times experiment...'
    log_str += f'\n[ARI] : {str(format_output(ari_list))}\n' \
               f'[FMI] : {str(format_output(fmi_list))}\n' \
               f'[NMI] : {str(format_output(nmi_list))}'
    log_str += f'\n[ARI] mean : {format_output(ari_avg)}, std : {format_output(ari_std)}\n' \
               f'[FMI] mean : {format_output(fmi_avg)}, std : {format_output(fmi_std)}\n' \
               f'[NMI] mean : {format_output(nmi_avg)}, std : {format_output(nmi_std)}'

    log = Logger(DataConfig['log_file'], 'a')
    log.put(log_str)
    print(log_str)


"""Utility functions"""

import csv
import tifffile as tiff
import numpy as np
import os
from scipy.interpolate import UnivariateSpline
import numpy as np

def get_list_from_csv(csv_file_path, parse_float=True, skip_header=False):
    """Given a CSV file, converts it to list"""
    def _parse_float_array(arr):
        return [float(item) for item in arr]

    with open(csv_file_path, 'r') as f:
        reader = csv.reader(f)
        csv_list = list(reader)

    parsed_list = csv_list

    if parse_float:
        parsed_list = [_parse_float_array(item) for item in csv_list]

    return parsed_list[1:] if skip_header else parsed_list

def plot_csv_on_rgb_tif(centroids, reference_img_path, tif_output_path, color=[10000, 0, 0], dtype=np.uint16):
    """Given a CSV file, plots the co-ordinates in the CSV on a RGB TIF stack"""
    def _parse_int_array(arr):
        return [int(item) for item in arr]

    def _draw_square(image, coord, size=2):
        coord = _parse_int_array(coord)
        shape_z, shape_y, shape_x, _ = image.shape
        z_range = range(max(0, coord[0]-size), min(shape_z, coord[0]+size))
        y_range = range(max(0, coord[1]-size), min(shape_y, coord[1]+size))
        x_range = range(max(0, coord[2]-size), min(shape_x, coord[2]+size))

        for z in z_range:
            for y in y_range:
                for x in x_range:
                    image[z, y, x, :] = color

        return image

    img = tiff.imread(reference_img_path)

    if img.ndim == 3:
        shape_z, shape_y, shape_x = img.shape
        new_img = np.zeros((shape_z, shape_y, shape_x, 3))
        new_img[:, :, :, 0] = img
        new_img[:, :, :, 1] = img
        new_img[:, :, :, 2] = img
    elif img.ndim == 4:
        shape_z, shape_y, shape_x, _ = img.shape
        new_img = img

    for i, c in enumerate(centroids):
        new_img = _draw_square(new_img, c)

    tiff.imsave(tif_output_path, new_img.astype(dtype))

def plot_csv_on_tif(centroids, reference_img_path, tif_output_path, img_shape=None):
    """Given a CSV file, plots the co-ordinates in the CSV on a TIF stack"""
    def _parse_int_array(arr):
        return [int(item) for item in arr]

    def _draw_square(image, coord, size=2):
        coord = _parse_int_array(coord)
        shape_z, shape_y, shape_x = image.shape
        z_range = range(max(0, coord[0]-size), min(shape_z, coord[0]+size))
        y_range = range(max(0, coord[1]-size), min(shape_y, coord[1]+size))
        x_range = range(max(0, coord[2]-size), min(shape_x, coord[2]+size))

        for z in z_range:
            for y in y_range:
                for x in x_range:
                    image[z, y, x] = 255

        return image

    if reference_img_path:
        ref_image = tiff.imread(reference_img_path)
        shape_z, shape_y, shape_x = ref_image.shape
    else:
        shape_z, shape_y, shape_x = img_shape

    annotated_image = np.zeros((shape_z, shape_y, shape_x))

    for i, c in enumerate(centroids):
        annotated_image = _draw_square(annotated_image, c)

    tiff.imsave(tif_output_path, annotated_image.astype(np.uint8))

def write_list_to_csv(arr, csv_output_path, open_mode='w'):
    """Given a list, writes it to a CSV file"""
    with open(csv_output_path, open_mode) as csv_file:
        for item in arr:
            csv_file.write(','.join([str(x) for x in item]) + '\n')

def create_intern_config():
    """Creates intern.cfg file from BOSS_TOKEN environment variable"""
    INTERN_CFG = 'intern.cfg'
    if os.path.exists(INTERN_CFG):
        os.remove(INTERN_CFG)

    boss_token = os.environ['BOSS_TOKEN']
    with open('intern_cfg_tmpl', 'r') as cfg_file:
        cfg_str = cfg_file.read()

    with open('intern.cfg', 'w') as cfg_file:
        cfg_file.write(cfg_str.format(boss_token))


__author__ = ""
__revision__ = "$Rev$"
__version__ = "@VERSION@"

import datetime
import elixir

class Bill(elixir.Entity):
    """A relational-database-mapped entity that is used for logging
    billing information.  These (persistent) entities is also used for
    generating statistics.
    """
    timestamp = elixir.Field(elixir.DateTime,
                             default=datetime.datetime.now,
                             index=True)
    #uuid = Field(Integer) # UUIDs are 128-bit integers.
    #
    # Do not need any UUID, because the inserts are done to a
    # database, and the TIMESTAMP will be created on insertion.
    #
    amount = elixir.Field(elixir.Integer)
    interface = elixir.Field(elixir.Unicode(4))
    host = elixir.Field(elixir.Unicode(24))
    transaction_type = elixir.Field(elixir.Unicode(16))
    description = elixir.Field(elixir.Unicode(16))

    def __repr__(self):
        attrs = ['timestamp', 'amount', 'interface', 'host',
                 'transaction_type', 'description']

        return '%s(%s)' % (self.__class__.__name__,
                           ', '.join(['%s=%s' \
                                          % (attr, repr(getattr(self, attr)), )
                                      for attr in attrs]))

def initialize(db_address='sqlite:///:memory:', logger=None, db_echo=True):
    """Bind the ORM module to a DB and setup and (if necessary) create
    the tables and mappings defined in the default collection.
    """
    elixir.metadata.bind = db_address
    if logger is not None:
        elixir.metadata.bind.logger = logger

    elixir.metadata.bind.echo = db_echo # Show all SQL queries.

    #print db_address
    elixir.setup_all()
    elixir.create_all()

def log(amount, interface, host, transaction_type, description):
    """Insert a new billing log entry.
    """
    # Create a new Bill instance.  Bill entities have a timestamp
    # attribute that is automatically set to datetime.datetime.now.
    bill = Bill(amount=amount, interface=interface, host=host,
                transaction_type=transaction_type, description=description)

    # Store the new entity in the database.
    elixir.session.commit()

def main():
    """Module mainline (for standalone execution)."""
    pass

if __name__ == "__main__":
    main()


import torch
from torch import nn

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64 * 4 * 4, 64),
            nn.Linear(64, 10),
        )

    def forward(self, x):
        x = self.model(x)
        return x

print(Model())  # 查看网络模型结构
# content = torch.load('./model/best_model.pth')
# content = torch.load('./model/model.pth')
# content = torch.load('./checkpoint/ckpt.pth')
# print(content.keys())  # 输出模型中的key
# print(content)  # 输出模型所有数据
# 之后有其他需求比如要看 key 为 model 的内容有啥
# print(content['best_acc'])


import RPi.GPIO as GPIO
from calculate_angles import get_rotation
from time import sleep
from time import time
import numpy as np
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import math

HID_SIZE = 10
DT = 0.035
LEARNING_RATE = 0.001
gamma = 0.5
ENTROPY_BETA = 10  # encourage exploration
decay_ratio = 0.98

class QNet(nn.Module):
    def __init__(self, obs_size, act_size):
        super(QNet, self).__init__()

        self.base = nn.Sequential(
            nn.Linear(obs_size, HID_SIZE),
            nn.ReLU(),
        )
        self.policy = nn.Sequential(
            nn.Linear(HID_SIZE, act_size),
            nn.Softmax(),
        )
        self.value = nn.Sequential(
            nn.Linear(HID_SIZE, 1),
        )

    def forward(self, x):
        base_out = self.base(x)
        return self.policy(base_out),  self.value(base_out)

model = QNet(2, 21)
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
# comment it if no such file
model.load_state_dict(torch.load('model_best.pth.tar'))

GPIO.setmode(GPIO.BCM)

GPIO.setup(0, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)

GPIO.setup(11, GPIO.IN)  # button

GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)

motor1 = GPIO.PWM(0, 101)
motor2 = GPIO.PWM(26, 101)

motor1.start(0)
motor2.start(0)

state = prev_button = False
experience = []

def control_motor(choice):
    speed = -100 + 10 * choice
    if speed < 0:
        GPIO.output(5, GPIO.HIGH)
        GPIO.output(6, GPIO.LOW)
        GPIO.output(13, GPIO.HIGH)
        GPIO.output(19, GPIO.LOW)
    else:
        GPIO.output(5, GPIO.LOW)
        GPIO.output(6, GPIO.HIGH)
        GPIO.output(13, GPIO.LOW)
        GPIO.output(19, GPIO.HIGH)
    motor1.ChangeDutyCycle(abs(speed))
    motor2.ChangeDutyCycle(abs(speed))

def get_reward(angle):
    if abs(angle) < 10:
        return 1
    if abs(angle) > 20:
        return -1
    else:
        return 0

def calc_logprob(policy_v, actions_v):
    p = - torch.log(policy_v.gather(1, actions_v.view(-1, 1)))
    return p

def train(states, actions_):
    if len(actions_) <= 1:
        return

    # prepare inputs
    values = []
    advs = []
    for i in range(len(states)):
        if i == (len(states) - 1):
            reward = get_reward(states[i][-1])
        else:
            next_angle = states[i+1][-1]
            reward = get_reward(next_angle) + gamma * model(torch.from_numpy(np.array(states[i+1]).astype(np.float32)))[1].detach().numpy()[0]
        values.append(reward)
        advs.append(reward - model(torch.from_numpy(np.array(states[i]).astype(np.float32)))[1].detach().numpy()[0])
    states, values = torch.from_numpy(np.array(states).astype(np.float32)), torch.from_numpy(np.array(values).astype(np.float32))
    advs = torch.from_numpy(np.array(advs).astype(np.float32))

    # experience.append((advs, states, values, actions_))
    #
    # if len(experience) > 100:
    #     experience.pop(0)
    #
    # for advs, states, values, actions in experience:
        # start training

    optimizer.zero_grad()
    policy_, value_ = model(states)
    ids = torch.Tensor(actions).long()
    log_prob_v = advs * calc_logprob(policy_, ids).squeeze(-1)
    loss_policy_v = -log_prob_v.mean()
    m = torch.distributions.Categorical(policy_)
    entropy_loss_v = ENTROPY_BETA * m.entropy().mean()

    value_loss_v = F.mse_loss(value_.squeeze(-1), values)

    print(loss_policy_v, - entropy_loss_v, value_loss_v)

    loss_v = loss_policy_v - entropy_loss_v + value_loss_v
    loss_v.backward()
    optimizer.step()
    torch.save(model.state_dict(), 'model_best.pth.tar')

states = []
actions = []
trained = False

try:

    while True:

        try:

            val = GPIO.input(11)
            if val == GPIO.LOW:
                if not prev_button:
                    current_time = time()

                    state = not state
                    prev_button = True
            else:
                prev_button = False

            if not state:
                motor1.ChangeDutyCycle(0)
                motor2.ChangeDutyCycle(0)
                if not trained:
                    train(states, actions)
                    states = []
                    actions = []
                    trained = True
                    print('trained!')
                continue

            current_time = time()

            # RL learning

            y_gyro, y_angle = get_rotation()

            if abs(y_angle) > 70:
                state = False
                trained = False

            policy, _ = model(torch.from_numpy(np.array([y_gyro, y_angle]).astype(np.float32)))
            policy = policy.detach().numpy()
            choice = np.random.choice(21, 1, p=policy)[0]
            control_motor(choice)

            states.append((y_gyro, y_angle))
            actions.append(choice)
            # print(y_angle, choice, time() - current_time)

            sleep(DT - (time() - current_time))

        except Exception as e:
            print(e)

            states = []
            actions = []
            state = False
            trained = False

        finally:
            #  do nothing
            m = 1

finally:
    GPIO.cleanup()


import ipaddress
import logging
from hashlib import sha256

import geoip2.database
import user_agents
from celery import shared_task
from django.conf import settings
from django.core.cache import cache
from django.db.models import Q
from django.utils import timezone

from core.models import Service

from .models import Hit, Session

log = logging.getLogger(__name__)

_geoip2_city_reader = None
_geoip2_asn_reader = None

def _geoip2_lookup(ip):
    global _geoip2_city_reader, _geoip2_asn_reader  # TODO: is there a better way to do global Django vars? Is this thread safe?
    try:
        if settings.MAXMIND_CITY_DB == None or settings.MAXMIND_ASN_DB == None:
            return None
        if _geoip2_city_reader == None or _geoip2_asn_reader == None:
            _geoip2_city_reader = geoip2.database.Reader(settings.MAXMIND_CITY_DB)
            _geoip2_asn_reader = geoip2.database.Reader(settings.MAXMIND_ASN_DB)
        city_results = _geoip2_city_reader.city(ip)
        asn_results = _geoip2_asn_reader.asn(ip)
        return {
            "asn": asn_results.autonomous_system_organization,
            "country": city_results.country.iso_code,
            "longitude": city_results.location.longitude,
            "latitude": city_results.location.latitude,
            "time_zone": city_results.location.time_zone,
        }
    except geoip2.errors.AddressNotFoundError:
        return {}
    except FileNotFoundError as e:
        log.exception("Unable to perform GeoIP lookup: %s", e)
        return {}

@shared_task
def ingress_request(
    service_uuid,
    tracker,
    time,
    payload,
    ip,
    location,
    user_agent,
    dnt=False,
    identifier="",
):
    try:
        service = Service.objects.get(pk=service_uuid, status=Service.ACTIVE)
        log.debug(f"Linked to service {service}")

        if dnt and service.respect_dnt:
            log.debug("Ignoring because of DNT or GPC")
            return

        try:
            remote_ip = ipaddress.ip_network(ip)
            for ignored_network in service.get_ignored_networks():
                if (
                    ignored_network.version == remote_ip.version
                    and ignored_network.supernet_of(remote_ip)
                ):
                    log.debug("Ignoring because of ignored IP")
                    return
        except ValueError as e:
            log.exception(e)

        # Validate payload
        if payload.get("loadTime", 1) <= 0:
            payload["loadTime"] = None

        association_id_hash = sha256()
        association_id_hash.update(str(ip).encode("utf-8"))
        association_id_hash.update(str(user_agent).encode("utf-8"))
        if settings.AGGRESSIVE_HASH_SALTING:
            association_id_hash.update(str(service.pk).encode("utf-8"))
            association_id_hash.update(
                str(timezone.now().date().isoformat()).encode("utf-8")
            )
        session_cache_path = (
            f"session_association_{service.pk}_{association_id_hash.hexdigest()}"
        )

        # Create or update session
        session = None
        if cache.get(session_cache_path) is not None:
            cache.touch(session_cache_path, settings.SESSION_MEMORY_TIMEOUT)
            session = Session.objects.filter(
                pk=cache.get(session_cache_path), service=service
            ).first()
        if session is None:
            initial = True

            log.debug("Cannot link to existing session; creating a new one...")

            ip_data = _geoip2_lookup(ip)
            log.debug(f"Found geoip2 data...")

            ua = user_agents.parse(user_agent)
            device_type = "OTHER"
            if (
                ua.is_bot
                or (ua.browser.family or "").strip().lower() == "googlebot"
                or (ua.device.family or ua.device.model or "").strip().lower()
                == "spider"
            ):
                device_type = "ROBOT"
            elif ua.is_mobile:
                device_type = "PHONE"
            elif ua.is_tablet:
                device_type = "TABLET"
            elif ua.is_pc:
                device_type = "DESKTOP"
            if device_type == "ROBOT" and service.ignore_robots:
                return
            session = Session.objects.create(
                service=service,
                ip=ip if service.collect_ips and not settings.BLOCK_ALL_IPS else None,
                user_agent=user_agent,
                identifier=identifier.strip(),
                browser=ua.browser.family or "",
                device=ua.device.family or ua.device.model or "",
                device_type=device_type,
                start_time=time,
                last_seen=time,
                os=ua.os.family or "",
                asn=ip_data.get("asn") or "",
                country=ip_data.get("country") or "",
                longitude=ip_data.get("longitude"),
                latitude=ip_data.get("latitude"),
                time_zone=ip_data.get("time_zone") or "",
            )
            cache.set(
                session_cache_path, session.pk, timeout=settings.SESSION_MEMORY_TIMEOUT
            )
        else:
            initial = False

            log.debug("Updating old session with new data...")

            # Update last seen time
            session.last_seen = time
            if session.identifier == "" and identifier.strip() != "":
                session.identifier = identifier.strip()
            session.save()

        # Create or update hit
        idempotency = payload.get("idempotency")
        idempotency_path = f"hit_idempotency_{idempotency}"
        hit = None

        if idempotency is not None:
            if cache.get(idempotency_path) is not None:
                cache.touch(idempotency_path, settings.SESSION_MEMORY_TIMEOUT)
                hit = Hit.objects.filter(
                    pk=cache.get(idempotency_path), session=session
                ).first()
                if hit is not None:
                    # There is an existing hit with an identical idempotency key. That means
                    # this is a heartbeat.
                    log.debug("Hit is a heartbeat; updating old hit with new data...")
                    hit.heartbeats += 1
                    hit.last_seen = time
                    hit.save()

        if hit is None:
            log.debug("Hit is a page load; creating new hit...")
            # There is no existing hit; create a new one
            hit = Hit.objects.create(
                session=session,
                initial=initial,
                tracker=tracker,
                # At first, location is given by the HTTP referrer. Some browsers
                # will send the source of the script, however, so we allow JS payloads
                # to include the location.
                location=payload.get("location", location),
                referrer=payload.get("referrer", ""),
                load_time=payload.get("loadTime"),
                start_time=time,
                last_seen=time,
                service=service,
            )

            # Recalculate whether the session is a bounce
            session.recalculate_bounce()

            # Set idempotency (if applicable)
            if idempotency is not None:
                cache.set(
                    idempotency_path, hit.pk, timeout=settings.SESSION_MEMORY_TIMEOUT
                )
    except Exception as e:
        log.exception(e)
        print(e)
        raise e


import warnings

import pytest

from pytestqt.exceptions import (
    _is_exception_capture_enabled,
    _QtExceptionCaptureManager,
)
from pytestqt.logging import QtLoggingPlugin, _QtMessageCapture
from pytestqt.qt_compat import qt_api
from pytestqt.qtbot import QtBot, _close_widgets

@pytest.fixture(scope="session")
def qapp_args(pytestconfig):
    """
    Fixture that provides QApplication arguments to use.

    You can override this fixture to pass different arguments to
    ``QApplication``:

    .. code-block:: python

       @pytest.fixture(scope="session")
       def qapp_args():
           return ["prog_name", "--arg=foo"]

    Note that it can only be overridden once at session scope.
    It is not possible to override this per unit test since a QApplication
    cannot be destroyed and recreated within the same app.

    The default value is a list with one element which is determined the same
    way as for ``QApplication.applicationName()``,
    see :ref:`qapp fixture<setting-qapp-name>` for more information.

    """
    return [pytestconfig.getini("qt_qapp_name")]

@pytest.fixture(scope="session")
def qapp_cls():
    """
    Fixture that provides the QApplication subclass to use.

    You can override this fixture to use a custom QApplication subclass from
    your application for tests:

    .. code-block:: python

       @pytest.fixture(scope="session")
       def qapp_cls():
           return myapp.Application

    Or use a ``QCoreApplication`` if you want to test a non-gui Qt application:

       @pytest.fixture(scope="session")
       def qapp_cls():
           return qt_api.QtCore.QCoreApplication
    """
    return qt_api.QtWidgets.QApplication

@pytest.fixture(scope="session")
def qapp(qapp_args, qapp_cls, pytestconfig):
    """
    Fixture that instantiates the QApplication instance that will be used by
    the tests.

    You can use the ``qapp`` fixture in tests which require a ``QApplication``
    to run, but where you don't need full ``qtbot`` functionality.
    """
    app = qt_api.QtWidgets.QApplication.instance()
    if app is None:
        global _qapp_instance
        _qapp_instance = qapp_cls(qapp_args)
        name = pytestconfig.getini("qt_qapp_name")
        _qapp_instance.setApplicationName(name)
        return _qapp_instance
    else:
        if not isinstance(app, qapp_cls):
            warnings.warn(
                f"Existing QApplication {app} is not an instance of qapp_cls: "
                f"{qapp_cls}"
            )
        return app

# holds a global QApplication instance created in the qapp fixture; keeping
# this reference alive avoids it being garbage collected too early
_qapp_instance = None

@pytest.fixture
def qtbot(qapp, request):
    """
    Fixture used to create a QtBot instance for using during testing.

    Make sure to call addWidget for each top-level widget you create to ensure
    that they are properly closed after the test ends.
    """
    result = QtBot(request)
    return result

@pytest.fixture
def qtlog(request):
    """Fixture that can access messages captured during testing"""
    if hasattr(request._pyfuncitem, "qt_log_capture"):
        return request._pyfuncitem.qt_log_capture
    else:
        return _QtMessageCapture([])  # pragma: no cover

@pytest.fixture
def qtmodeltester(request):
    """
    Fixture used to create a ModelTester instance to test models.
    """
    from pytestqt.modeltest import ModelTester

    tester = ModelTester(request.config)
    yield tester
    tester._cleanup()

def pytest_addoption(parser):
    parser.addini(
        "qt_api", 'Qt api version to use: "pyside6" , "pyside2", "pyqt6", "pyqt5"'
    )
    parser.addini("qt_no_exception_capture", "disable automatic exception capture")
    parser.addini(
        "qt_default_raising",
        "Default value for the raising parameter of qtbot.waitSignal/waitCallback",
    )
    parser.addini(
        "qt_qapp_name", "The Qt application name to use", default="pytest-qt-qapp"
    )

    default_log_fail = QtLoggingPlugin.LOG_FAIL_OPTIONS[0]
    parser.addini(
        "qt_log_level_fail",
        'log level in which tests can fail: {} (default: "{}")'.format(
            QtLoggingPlugin.LOG_FAIL_OPTIONS, default_log_fail
        ),
        default=default_log_fail,
    )
    parser.addini(
        "qt_log_ignore",
        "list of regexes for messages that should not cause a tests " "to fails",
        type="linelist",
    )

    group = parser.getgroup("qt", "qt testing")
    group.addoption(
        "--no-qt-log",
        dest="qt_log",
        action="store_false",
        default=True,
        help="disable pytest-qt logging capture",
    )
    group.addoption(
        "--qt-log-format",
        dest="qt_log_format",
        default=None,
        help="defines how qt log messages are displayed.",
    )

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_setup(item):
    """
    Hook called after before test setup starts, to start capturing exceptions
    as early as possible.
    """
    capture_enabled = _is_exception_capture_enabled(item)
    if capture_enabled:
        item.qt_exception_capture_manager = _QtExceptionCaptureManager()
        item.qt_exception_capture_manager.start()
    yield
    _process_events()
    if capture_enabled:
        item.qt_exception_capture_manager.fail_if_exceptions_occurred("SETUP")

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_call(item):
    yield
    _process_events()
    capture_enabled = _is_exception_capture_enabled(item)
    if capture_enabled:
        item.qt_exception_capture_manager.fail_if_exceptions_occurred("CALL")

@pytest.hookimpl(hookwrapper=True, trylast=True)
def pytest_runtest_teardown(item):
    """
    Hook called after each test tear down, to process any pending events and
    avoiding leaking events to the next test. Also, if exceptions have
    been captured during fixtures teardown, fail the test.
    """
    _process_events()
    _close_widgets(item)
    _process_events()
    yield
    _process_events()
    capture_enabled = _is_exception_capture_enabled(item)
    if capture_enabled:
        item.qt_exception_capture_manager.fail_if_exceptions_occurred("TEARDOWN")
        item.qt_exception_capture_manager.finish()

def _process_events():
    """Calls app.processEvents() while taking care of capturing exceptions
    or not based on the given item's configuration.
    """
    app = qt_api.QtWidgets.QApplication.instance()
    if app is not None:
        app.processEvents()

def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "qt_no_exception_capture: Disables pytest-qt's automatic exception "
        "capture for just one test item.",
    )

    config.addinivalue_line(
        "markers", "qt_log_level_fail: overrides qt_log_level_fail ini option."
    )
    config.addinivalue_line(
        "markers", "qt_log_ignore: overrides qt_log_ignore ini option."
    )
    config.addinivalue_line("markers", "no_qt_log: Turn off Qt logging capture.")

    if config.getoption("qt_log") and config.getoption("capture") != "no":
        config.pluginmanager.register(QtLoggingPlugin(config), "_qt_logging")

    qt_api.set_qt_api(config.getini("qt_api"))

def pytest_report_header():
    from pytestqt.qt_compat import qt_api

    v = qt_api.get_versions()
    fields = [
        f"{v.qt_api} {v.qt_api_version}",
        "Qt runtime %s" % v.runtime,
        "Qt compiled %s" % v.compiled,
    ]
    version_line = " -- ".join(fields)
    return [version_line]


import sqlite3
from tkinter import *
from tkinter import messagebox
import os

#banco de dados e tabela
def create_database():
    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    cursor.execute('''
    CREATE TABLE IF NOT EXISTS tasks(
        id INTEGER PRIMARY KEY,
        title TEXT,
        status TEXT
    )
    ''')

    conn.commit()
    conn.close()

# Adicionar tarefa
def add_task():
    task_title = entry_task.get()
    task_status = "Pendente"

    if task_title:
        conn = sqlite3.connect('tasks.db')
        cursor = conn.cursor()

        cursor.execute('''
        INSERT INTO tasks (title, status) VALUES (?,?)
        ''', (task_title, task_status))

        conn.commit()
        conn.close()

        entry_task.delete(0, END)
        get_tasks()

    else:
        messagebox.showerror('Erro', 'Insira uma tarefa')

#tarefas
def get_tasks():
    list_tasks.delete(0, END)

    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    cursor.execute('''
    SELECT * FROM tasks
    ''')

    tasks = cursor.fetchall()

    for task in tasks:
        list_tasks.insert(END, f"{task[1]} - {task[2]}")

    conn.close()

# Atualizar tarefa
def update_task():
    selected_task = list_tasks.curselection()
    task_id = selected_task[0] + 1

    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    cursor.execute('''
    UPDATE tasks SET status = ? WHERE id = ?
    ''', ('Concluída', task_id))

    conn.commit()
    conn.close()

    get_tasks()

# Excluir tarefa
def delete_task():
    selected_task = list_tasks.curselection()
    task_id = selected_task[0] + 1

    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    cursor.execute('''
    DELETE FROM tasks WHERE id = ?
    ''', (task_id,))

    conn.commit()
    conn.close()

    get_tasks()

# Salvar dados
def save_data():
    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    cursor.execute('''
    SELECT * FROM tasks
    ''')

    tasks = cursor.fetchall()

    with open('tasks.txt', 'w') as file:
        for task in tasks:
            file.write(f"{task[1]};{task[2]}\n")

    conn.close()

# Carregar dados
def load_data():
    with open('tasks.txt', 'r') as file:
        lines = file.readlines()

    conn = sqlite3.connect('tasks.db')
    cursor = conn.cursor()

    for line in lines:
        task_title, task_status = line.strip().split(';')

        cursor.execute('''
        INSERT INTO tasks (title, status) VALUES (?,?)
        ''', (task_title, task_status))

    conn.commit()
    conn.close()

    get_tasks()

# Tela principal

def main_screen():
    global window, entry_task, list_tasks, btn_add, btn_update, btn_delete, btn_save, btn_load

    window = Tk()
    window.title('Gerenciador de Tarefas')
    window.geometry('300x300')

    entry_task = Entry(window, width=20)
    entry_task.pack(pady=10)

    btn_add = Button(window, text='Adicionar', width=20, command=add_task)
    btn_add.pack(pady=5)

    list_tasks = Listbox(window, width=50, height=15)
    list_tasks.pack(pady=5)

    btn_update = Button(window, text='Atualizar', width=20, command=update_task)
    btn_update.pack(pady=5)

    btn_delete = Button(window, text='Excluir', width=20, command=delete_task)
    btn_delete.pack(pady=5)

    btn_save = Button(window, text='Salvar', width=20, command=save_data)
    btn_save.pack(pady=5)

    btn_load = Button(window, text='Carregar', width=20, command=load_data)
    btn_load.pack(pady=5)

    get_tasks()

    window.mainloop()

create_database()
main_screen()


import unittest

class Solution:
    def validMountainArray(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        climb = True

        for i in range(1, len(A)):
            if A[i-1] == A[i]:
                return False

            if climb:
                if A[i-1] > A[i]:
                    if i == 1:
                        return False
                    else:
                        climb = False
            else:
                if A[i-1] <= A[i]:
                    return False

        return not climb

class SolutionTest(unittest.TestCase):
    def test_validMountainArray(self):
        s = Solution()
        self.assertTrue(s.validMountainArray([0, 3, 2, 1]))
        self.assertFalse(s.validMountainArray([2, 1]))
        self.assertFalse(s.validMountainArray([3, 5, 5]))

if __name__ == "__main__":
    unittest.main()


import requests

def check_value_positive(value) -> bool:

    ''' Проверка рациональности числа'''

    if value > 0:
        return True
    else:
        return False

def currency_value_balance(balance, currency):

    ''' Перевод баланса в другую валюту '''

    info = requests.get(f'https://www.cbr-xml-daily.ru/daily_json.js')
    info = info.json()
    currency_value = info['Valute'][currency]['Value']
    cur_balance = balance/currency_value
    return cur_balance


import pytest
import curio
import curio_http

@pytest.fixture
def kernel():
    kernel = curio.Kernel(log_errors=False)

    def handle_crash(current):
        raise current.exc_info[1]

    kernel._crash_handler = handle_crash

    return kernel

async def request(*args, out=None, **kwargs):
    async with curio_http.ClientSession() as session:
        response = await session.request(*args, **kwargs)
        if out:
            data = await getattr(response, out)()
            return response, data

    return response

def test_params(kernel):
    response = kernel.run(request(
        'GET', 'http://example.com/path?set=via_url'))

    assert response.url == 'example.com:80/path?set=via_url'

    response = kernel.run(request(
        'GET', 'http://example.com/path?set=via_url',
        params={'set': 'via_params'}))

    assert response.url == 'example.com:80/path?set=via_url&set=via_params'

def test_get_404(kernel):
    response, text = kernel.run(request(
        'GET', 'http://httpbin.org/status/404', out='text'))

    assert response.http_version == '1.1'
    assert response.status_code == 404

    assert response.headers['content-type'] == 'text/html; charset=utf-8'

    with pytest.raises(curio_http.HTTPError):
        response.raise_for_status()

    assert text is None

def test_get_binary(kernel):
    response, binary = kernel.run(request(
        'GET', 'https://httpbin.org/bytes/10000', out='binary'))

    # Should do nothing.
    response.raise_for_status()

    assert len(binary) == 10000

def test_get_json(kernel):
    response, json_content = kernel.run(request(
        'GET', 'http://httpbin.org/get', out='json'))

    assert response.http_version == '1.1'
    assert response.status_code == 200

    assert response.headers['content-type'] == 'application/json'

    assert json_content['headers'] == {
        'Host': 'httpbin.org'
    }

def test_post(kernel):
    response, json = kernel.run(request(
        'POST', 'http://httpbin.org/post', data='foo=bar', out='json'))

    assert response.status_code == 200

    assert json['data'] == 'foo=bar'

def test_ssl(kernel):
    response, json = kernel.run(request(
        'GET', 'https://httpbin.org/get', out='json'))

    assert response.http_version == '1.1'
    assert response.status_code == 200

    assert response.headers['content-type'] == 'application/json'

    assert json['headers'] == {
        'Host': 'httpbin.org'
    }

@pytest.mark.parametrize('redirect_type', ['absolute', 'relative'])
def test_redirect(kernel, redirect_type):
    url = 'http://httpbin.org/{}-redirect/2'.format(redirect_type)

    response = kernel.run(request('GET', url, allow_redirects=False))
    assert response.is_redirect
    assert response.url == 'httpbin.org:80/{}-redirect/2'.format(redirect_type)

    response = kernel.run(request('GET', url, allow_redirects=True))
    assert not response.is_redirect
    assert response.url == 'httpbin.org:80/get'
    assert [r.url for r in response.history] == [
        'httpbin.org:80/{}-redirect/2'.format(redirect_type),
        'httpbin.org:80/{}-redirect/1'.format(redirect_type),
    ]


# 1 load xml 2 load jpg 3 haar gray 4 (检测)detect 5 draw(绘制方框)
import cv2
import numpy as np

# 加载load xml 参数1 file name
face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')

# load jpg
img = cv2.imread('psb.jpg')
cv2.imshow('src', img)

# haar gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# detect faces 1 data 2 scale 3 5
# 检测人脸: 1 灰度图片数据 2 缩放系数 3 目标大小
faces = face_xml.detectMultiScale(gray, 1.3, 5)
# 人脸个数
print('face=', len(faces))

# draw方框
for (x, y, w, h) in faces:
    # 起始坐标。起始坐标。
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    # 我们已经找到的人脸区域
    roi_face = gray[y:y + h, x:x + w]
    roi_color = img[y:y + h, x:x + w]
    # 参数1 gray
    eyes = eye_xml.detectMultiScale(roi_face)
    print('eye=', len(eyes))
    for (e_x, e_y, e_w, e_h) in eyes:
        cv2.rectangle(roi_color, (e_x, e_y), (e_x + e_w, e_y + e_h), (0, 255, 0), 2)
cv2.imshow('dst', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


import random

answer = []
while len(answer) < 3:
    temp = random.randint(1, 9)
    if temp not in answer:
        answer.append(temp)

round = 0
while True:
    round += 1
    print(f"round {round}")
    strike = 0
    ball = 0
    for i in range(3):
        user = int(input(f"{i+1}번째 수"))
        if user == answer[i]:
            strike += 1
        elif user in answer:
            ball += 1
    if (strike == 0) & (ball == 0):
        print("out")
    else:
        print(f"strike : {strike}, ball : {ball}")

    if strike == 3:
        print("win")
        break
    if round == 9:
        print("lose")
        break


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'dynamicArray' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER n
#  2. 2D_INTEGER_ARRAY queries
#
class DynamicArray:
    def __init__(self, n):
        self.n = n
        self.lastAnswer = 0
        self.seqList = []
        self.set_sequence_list()

    def set_sequence_list(self):
        for i in range(0, self.n):
            self.seqList.append([])

    def find_sequence(self, x):
        index = (x ^ self.lastAnswer) % n

        return self.seqList[index]

    def query_type_one(self, x, y):
        sequence = self.find_sequence(x)
        sequence.append(y)

    def query_type_two(self, x, y):
        sequence = self.find_sequence(x)
        size = len(sequence)
        index = y % size
        value = sequence[index]
        self.lastAnswer = value

        return self.lastAnswer

def dynamicArray(n, queries):
    # Write your code here
    array = DynamicArray(n)
    dynamicarray_result = []

    for query in queries:
        if query[0] == 1:
            array.query_type_one(query[1], query[2])
        else:
            result_for_type_two = array.query_type_two(query[1], query[2])
            dynamicarray_result.append(result_for_type_two)

    return dynamicarray_result

if __name__ == '__main__':
    # fptr = open(os.environ['OUTPUT_PATH'], 'w')
    fptr = sys.stdout

    first_multiple_input = input().rstrip().split()

    n = int(first_multiple_input[0])

    q = int(first_multiple_input[1])

    queries = []

    for _ in range(q):
        queries.append(list(map(int, input().rstrip().split())))

    result = dynamicArray(n, queries)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()


import bpy
import importlib
from pathlib import Path
import ifcpatch
from blenderbim.bim.prop import StrProperty, Attribute
from bpy.types import PropertyGroup
from bpy.props import (
    PointerProperty,
    StringProperty,
    EnumProperty,
    BoolProperty,
    IntProperty,
    FloatProperty,
    FloatVectorProperty,
    CollectionProperty,
)

ifcpatchrecipes_enum = []

def purge():
    global ifcpatchrecipes_enum
    ifcpatchrecipes_enum = []

def get_ifcpatch_recipes(self, context):
    global ifcpatchrecipes_enum
    if len(ifcpatchrecipes_enum) < 1:
        ifcpatchrecipes_enum.clear()
        ifcpatch_path = Path(importlib.util.find_spec("ifcpatch").submodule_search_locations[0])
        for filename in ifcpatch_path.joinpath("recipes").glob("*.py"):
            f = str(filename.stem)
            if f == "__init__":
                continue
            docs = ifcpatch.extract_docs(f, "Patcher", "__init__", ("src", "file", "logger", "args"))
            ifcpatchrecipes_enum.append((f, f, docs.get("description", "") if docs else ""))
    return ifcpatchrecipes_enum

def update_ifc_patch_recipe(self, context):
    bpy.ops.bim.update_ifc_patch_arguments(recipe=self.ifc_patch_recipes)

class BIMPatchProperties(PropertyGroup):
    ifc_patch_recipes: EnumProperty(items=get_ifcpatch_recipes, name="Recipes", update=update_ifc_patch_recipe)
    ifc_patch_input: StringProperty(default="", name="IFC Patch Input IFC")
    ifc_patch_output: StringProperty(default="", name="IFC Patch Output IFC")
    ifc_patch_args: StringProperty(default="", name="Arguments")
    ifc_patch_args_attr: CollectionProperty(type=Attribute, name="Arguments")
    should_load_from_memory: BoolProperty(default=False, name="Load from Memory")


import http
import ssl

conn = http.client.HTTPSConnection("biogpt-1.cnvrg.stjude.org",
                                   443,
                                   context = ssl._create_unverified_context())

headers = {
    'Cnvrg-Api-Key': "XXXXXXXXXX",
    'Content-Type': "application/json"
    }

endpoint = "/api/v1/endpoints/XXXXXXXXXXXX"


tamanho = input("Qual o tamanho do ovo desejado\n1-Pequeno\n2-Médio\n3-Grande\nR:")

if tamanho == "1":
    valor = 7.80
elif tamanho == "2":
    valor = 12.90
elif tamanho == "3":
    valor = 23.95

sabor = input("Qual é o sabor desejado:\n1-Chocolate preto\n2-Chocolate branco\n3-Chocolate ao Leite\nR:")

if sabor == "1":
    valor= valor+9.67
elif sabor == "2":
    valor= valor+4.50
elif sabor == "3":
    valor= valor+9.32

recheio = input("Qual é o recheio desejado:\n1-Chocolate preto\n2-Chocolate branco\n3-Os dois\nR:")

if recheio == "1":
    valor = valor+4.83
elif recheio == "2":
    valor = valor+2.25
elif recheio == "3":
    valor = valor+3.46

acrescimo = input ("Qual será o acrescimo: \n1-MM'S \n2-KitKat\n3-Não quero\n4-Os dois\nR:")

if acrescimo == "1":
    valor= valor+4.67
elif acrescimo == "2":
    valor= valor+5.43
elif acrescimo == "4":
    valor= valor+10.1
elif acrescimo == "3":
    valor= valor+0.00

presente = input ("Sua compra sera presente?\n1-Sim\n2-Não\nR:")

if presente == "2":
    valor= valor+0.00
elif presente == "1":
    valor = valor+2.50

entrega = input ("Sua compra sera entrega?\n1-Sim\n2-Não\nR:")
if entrega == "2":
    valor= valor+0.00
elif entrega == "1":
    valor = valor+5.00

pagamento = input ("Informe a forma pagamento\n1-Cartão de crédito\n2-Dinheiro\n3-PIX\nR: ")
if pagamento == "1":
    valor= valor+3.30
elif pagamento == "2" or pagamento == "3":
    valor = valor/1.10

print("Seu ovo ficou R$ " + str(valor))


import contextlib
import logging
from pathlib import Path
from typing import List, Optional, Tuple, Union, Sequence

import torch
from torch import nn
import tensorrt as trt

def device_trt2torch(device: trt.TensorLocation) -> torch.device:
    if device == trt.TensorLocation.DEVICE:
        return torch.device("cuda")
    elif device == trt.TensorLocation.HOST:
        return torch.device("cpu")
    else:
        raise TypeError("invalid device: %s" % device)

def dtype_trt2torch(dtype: trt.DataType) -> torch.dtype:
    if dtype == trt.int8:
        return torch.int8
    elif dtype == trt.bool:
        assert trt_version() >= "7.0"
        return torch.bool
    elif dtype == trt.int32:
        return torch.int32
    elif dtype == trt.float16:
        return torch.float16
    elif dtype == trt.float32:
        return torch.float32
    else:
        raise TypeError("invalid dtype: %s" % dtype)

class TensorRTModule(nn.Module):
    def __init__(
        self,
        onnx_path: Union[str, Path],
        engine_path: Union[str, Path],
        max_batch_size: int = 1,
        max_workspace_size: int = 1 << 25,
        fp16_mode: bool = False,
        force_rebuild: bool = False,
    ):
        super().__init__()
        self.onnx_path: Path = Path(onnx_path)
        self.engine_path: Path = Path(engine_path)
        self.max_batch_size: int = max_batch_size
        self.max_workspace_size: int = max_workspace_size
        self.fp16_mode: bool = fp16_mode
        self.force_rebuild: bool = force_rebuild

        self._logger: trt.Logger = None
        self._engine: trt.ICudaEngine = None
        self._context: trt.IExecutionContext = None
        self._input_names: List[str] = None
        self._output_names: List[str] = None

        self._init_logger()
        self._init_engine()
        self._init_names()

    def _init_logger(self):
        self._logger = trt.Logger(trt.Logger.Severity.INFO)
        trt.init_libnvinfer_plugins(self._logger, "")

    def _init_engine(self):
        if not self.force_rebuild and self.engine_path.exists():
            with trt.Runtime(self._logger) as runtime:
                with open(self.engine_path, "rb") as f:
                    engine_data = f.read()
                self._engine = runtime.deserialize_cuda_engine(engine_data)
                self._context = self._engine.create_execution_context()
            return

        with contextlib.ExitStack() as stack:
            explicit_batch = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
            builder = stack.enter_context(trt.Builder(self._logger))
            network = stack.enter_context(builder.create_network(explicit_batch))
            parser = stack.enter_context(trt.OnnxParser(network, self._logger))

            builder.max_workspace_size = self.max_workspace_size
            builder.max_batch_size = self.max_batch_size
            builder.fp16_mode = self.fp16_mode
            builder.strict_type_constraints = True

            with open(self.onnx_path, "rb") as model:
                success = parser.parse(model.read())
                if not success:
                    err = parser.get_error(0)
                    msg = "while parsing node number %i:\n" % err.node()
                    msg += "%s:%i In function %s:\n[%i] %s" % (
                        err.file(),
                        err.line(),
                        err.func(),
                        err.code(),
                        err.desc(),
                    )
                    raise RuntimeError(msg)

            self._engine = builder.build_cuda_engine(network)
            self._context = self._engine.create_execution_context()
            with open(self.engine_path, "wb") as f:
                f.write(self._engine.serialize())

    def _init_names(self):
        self._input_names = []
        self._output_names = []
        for index in range(self._engine.num_bindings):
            name = self._engine.get_binding_name(index)
            if self._engine.binding_is_input(index):
                self._input_names.append(name)
            else:
                self._output_names.append(name)

    def forward(self, inputs: Sequence[torch.Tensor]) -> Tuple[torch.Tensor]:
        batch_size = inputs[0].shape[0]
        bindings = [None] * self._engine.num_bindings

        for i, input_name in enumerate(self._input_names):
            index = self._engine.get_binding_index(input_name)
            dtype = dtype_trt2torch(self._engine.get_binding_dtype(index))
            device = device_trt2torch(self._engine.get_location(index))
            x = inputs[i].to(dtype).to(device).contiguous()
            bindings[index] = x.data_ptr()

        outputs = []
        for output_name in self._output_names:
            index = self._engine.get_binding_index(output_name)
            shape = (batch_size, *self._engine.get_binding_shape(index)[1:])
            dtype = dtype_trt2torch(self._engine.get_binding_dtype(index))
            device = device_trt2torch(self._engine.get_location(index))
            output = torch.empty(shape, dtype=dtype, device=device)
            outputs.append(output)
            bindings[index] = output.data_ptr()

        stream = torch.cuda.current_stream()
        self._context.execute_async(
            batch_size=batch_size, bindings=bindings, stream_handle=stream.cuda_stream,
        )
        stream.synchronize()
        return outputs


import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import popular_data_skills.dashboard.helpers_filter as filter
import popular_data_skills.config.config as config

class JobFilter():

    def __init__(self, job: str, experience: str, degree: str, ):
        # Read the job data into pandas dataframe
        job_data = pd.read_csv(config.FINAL_DATA_FILE, index_col=False)
        self.background_color = "white"

        self.primary_color = "#2fa4e7"
        self.secondary_color = "#b2cddb"
        self.tertiary_color = "#f8f9fa"
        self.highlight_color = "#1e6b97"
        self._job = job
        self._experience = experience
        self._degree = degree
        self._job_list = job_data['job'].unique()
        self._degree_list = sorted(job_data['degree_level'].unique())
        self._experience_list = sorted(job_data['experience'].unique())
        self._group_list = filter.group_list()

        # Add new column no degree
        job_data['no_degree'] = job_data['degree_level'].apply(
            lambda x: 1 if x == 'not_specified' else 0)

        # Drop unuseful columns
        info_columns = ['company_name', 'country', 'job_title', 'location',
                        'date_posted', 'applicant_count', 'job_description_lines', ]
        job_data = job_data.drop(info_columns, axis=1)

        # Filter data as per profile
        filtered_df = filter.filter_df(job_data, job, experience, degree)

        # Drop catagorical columns use to filter df that are no longer needed
        drop_columns = ['job', 'experience', 'degree_level']
        filtered_df = filtered_df.drop(columns=drop_columns)

        # Wrangle df in summary df
        filtered_df = (filtered_df
                       .sum()
                       .sort_values(ascending=False).astype(int)
                       )

        filtered_df = (filtered_df
                       .to_frame()
                       .reset_index()
                       )

        filtered_df['groups'] = filtered_df['index'].apply(
            filter.add_groups).sort_values()
        filtered_df['index'] = filtered_df['index'].replace({'no_degree': 'no degree stated',
                                                            'degree': 'any degree'})

        group_total_df, filtered_df = filter.get_group_totals(filtered_df)

        def get_ratio(row, group_total_df):
            ''' Uses dfs from get_group_totals to calulate percentage of each skill relative to its parent group '''
            mask = group_total_df[group_total_df['groups'] == row['groups']]
            group_total = mask['group_total'].values[0].astype(int)
            # #return the percantage of group
            return ((row['count'] / group_total) * 100).astype(int)

        filtered_df['ratio'] = filtered_df.apply(
            lambda x: get_ratio(x, group_total_df), axis=1)

        filtered_df = filtered_df[filtered_df['ratio'] != 0]

        self.df = filtered_df

    @property
    def job(self):
        return self._job

    @property
    def experience(self):
        return self._experience

    @property
    def degree(self):
        return self._degree

    @property
    def job_list(self):
        return self._job_list

    @property
    def experience_list(self):
        return self._experience_list

    @property
    def degree_list(self):
        return self._degree_list

    @property
    def group_list(self):
        return self._group_list

    def tree_map(self):
        my_color_scale = [(0.00, "white"),
                          (0.50, self.secondary_color),
                          (0.75, self.primary_color),
                          (1.00, self.highlight_color)]

        # ["white",self.primary_color]
        ''' Return tree map of data filtered by parameters giving. '''
        # Create treemap based on filtered dfs
        self.tree_fig = px.treemap(self.df, path=[px.Constant(
            "all"), 'groups', 'index'], values='ratio', labels="images",
            color='ratio',
            color_continuous_scale=my_color_scale,
            color_continuous_midpoint=np.average(self.df["ratio"]),
        )

        self.tree_fig.update_traces(root_color=self.tertiary_color,)
        self.tree_fig.update_layout(margin=dict(t=10, l=5, r=5, b=10)),

        self.tree_fig.data[0]['textfont']['size'] = 25
        self.tree_fig.update_coloraxes(showscale=False)

        self.tree_fig.update_traces(
            textposition="middle center", selector=dict(type='treemap'))
        self.tree_fig.data[0].texttemplate = "%{label}
%{value}%"

        return self.tree_fig

    def grouped_bar_plot(self, comparision_object, comparision_group: str):
        '''Creates a grouped bar plot comparing curent class.object profile against other class.object profile  '''
        df_by_group = self.df[self.df['groups'] == comparision_group].sort_values(
            by='ratio', ascending=False).head(10)
        y_1 = df_by_group['ratio']
        x_1 = df_by_group['index'].str.capitalize()

        comparision_df1 = comparision_object.df[comparision_object.df['groups']
                                                == comparision_group]
        y_list = []
        for item in df_by_group['index']:
            new_y = comparision_df1[comparision_df1['index']
                                    == item]['ratio'].values[0]
            y_list.append(new_y)
        y_2 = y_list
        x_2 = x_1

        self.grouped_fig = go.Figure(data=[
            # Grouped bar principal plot
            go.Bar(
                name=f'{str.capitalize(self.job)}  Exp: {self.experience}  Degree: {str.capitalize(self.degree)}',
                x=y_1,
                y=x_1,
                marker=dict(
                    color=self.primary_color,
                ),
                text=y_1,
                textposition='inside',
                orientation='h',

            ),
            # Grouped bar secondary plot
            go.Bar(
                name=f'{str.capitalize(comparision_object.job)}  Exp: {comparision_object.experience}  Degree: {str.capitalize(comparision_object.degree)}',
                x=y_2,
                y=x_2,
                marker=dict(
                    color=self.secondary_color,
                ),
                text=y_2,
                textposition='inside',
                orientation='h',

            )
        ])

        self.grouped_fig.update_layout(
            barmode='group',
            # height=len(y_1) * 100,
        )

        # Title
        self.grouped_fig.update_layout(

            yaxis=dict(
                showgrid=False,
                showline=False,
                showticklabels=True,
                autorange="reversed",
                domain=[0, 0.95],
            ),
            xaxis=dict(
                domain=[0, 0.90],
            ),
            legend=dict(orientation="h",
                x=0.5,
                y=1.1, xanchor='center', yanchor="top"),
            margin=dict(l=5, r=5, t=5, b=5),
            paper_bgcolor=self.background_color,
            plot_bgcolor=self.background_color,
        )

        return self.grouped_fig

    def most_demand_compare_barplot(self, comparision_object, comparision_group):
        '''Creates a grouped bar plot comparing curent class.object profile against other class.object profile  '''
        df_by_group = self.df[self.df['groups'] == comparision_group].sort_values(
            by='ratio', ascending=False).head(10)
        y_1 = df_by_group['ratio']
        x_1 = df_by_group['index'].str.capitalize()

        comparision_df2 = comparision_object.df[comparision_object.df['groups'] == comparision_group].sort_values(
            by='ratio', ascending=False).head(10)
        y_3 = comparision_df2['ratio']
        x_3 = comparision_df2['index'].str.capitalize()
        # Create two bar plots side by side to compare most in demand skills from each profile

        # Creating two subplots
        demand_comapre_fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
                                           shared_yaxes=False,
                                           # vertical_spacing=0.0001
                                           )

        # Add bar chart for Profile 1
        demand_comapre_fig.add_trace(go.Bar(
            x=y_1,
            y=x_1,
            marker=dict(
                color=self.primary_color,
                line=dict(
                    color=self.primary_color,
                    width=1),
            ),
            name=f'{str.capitalize(self.job)}  Exp: {self.experience}  Degree: {str.capitalize(self.degree)}',
            text=y_1,
            textposition='inside',
            orientation='h',
        ), 1, 1)

        # Add bar chart for Profile 2
        demand_comapre_fig.add_trace(go.Bar(
            x=y_3,
            y=x_3,
            marker=dict(
                color=self.secondary_color,
                line=dict(
                    color=self.secondary_color,
                    width=1,)
            ),
            name=f'{str.capitalize(comparision_object.job)}  Exp: {comparision_object.experience}  Degree: {str.capitalize(comparision_object.degree)}',
            text=y_3,
            textposition='inside',
            orientation='h',
        ), 1, 2)

        # Title
        demand_comapre_fig.update_layout(
            # Axes
            yaxis=dict(
                showgrid=False,
                showline=False,
                showticklabels=True,
                autorange="reversed",
                side='right',

                ticklabelposition="inside",

                # tickfont=dict(
                #     size=15,
                #     # color='rgba(102, 102, 102, 0.8)',
                #     # family=  "Droid Sans Mono",
                # ),
                domain=[0.05, 0.95],
            ),
            yaxis2=dict(
                showgrid=False,
                showline=False,
                showticklabels=True,
                autorange="reversed",
                ticklabelposition="inside",
                # tickfont=dict(
                #     size=15,
                #     # color='rgba(102, 102, 102, 0.8)',
                #     # family=  "Droid Sans Mono",
                # ),
                linecolor='rgba(102, 102, 102, 0.8)',
                linewidth=2,
                domain=[0.05, 0.95],
            ),
            xaxis=dict(
                zeroline=False,
                showline=False,
                showticklabels=False,
                showgrid=True,
                autorange="reversed",
                domain=[0, 0.49],

            ),
            xaxis2=dict(
                zeroline=False,
                showline=False,
                showticklabels=False,
                showgrid=True,
                domain=[0.51, 1],
                # side='top',
                # dtick=25000,
            ),
            # showlegend=False,
            legend=dict(orientation="h",
                x=0.5,
                y=1.1, xanchor='center', yanchor="top"),
            margin=dict(l=5, r=5, t=5, b=5),
            paper_bgcolor=self.background_color,
            plot_bgcolor=self.background_color,
            # showlegend=False,
        )

        # Annotations
        annotations = []
        # Adding percentage labels
        # for yd, y_3d, xd, x_3d in zip(y_1, y_3, x_1, x_3):
        #     # Percentage labels for Profile 1 plot
        #     annotations.append(dict(xref='x1', yref='y1',
        #                             y=xd, x=2,
        #                             text=str(yd) + '%',
        #                             font=dict(family='Arial', size=12,
        #                                       color='rgb(0, 0, 0)'),
        #                             showarrow=False))
        #     # Percentage labels for Profile 2 plot
        #     annotations.append(dict(xref='x2', yref='y2',
        #                             y=x_3d, x=2,
        #                             text=str(y_3d) + '%',
        #                             font=dict(family='Arial', size=12,
        #                                       color='rgb(0, 0, 0)'),
        #                             showarrow=False))
        # Footnotes
        annotations.append(dict(xref='paper', yref='paper',
                                x=-0.2, y=-0.109,
                                text='Figures are number of job posts a certain skills is mentioned in. As a percentage of total of the group',
                                font=dict(family='Arial', size=10,
                                          color='rgb(150,150,150)'),
                                showarrow=False))

        demand_comapre_fig.update_layout(annotations=annotations)

        return demand_comapre_fig

#job_profile1 =  JobFilter('analyst', '5+', 'phd',)
#job_profile2 = JobFilter('scientist', '5+', 'phd',)

# job_profile1.tree_map()
#compare_df = job_profile1.df_by_group('Skills')
# job_profile1.get_x()
#job_profile1.grouped_bar_plot(job_profile2, 'Skills')
#job_profile1.most_demand_compare_barplot(job_profile2 , 'Degree')
# job_profile1.df

# TEMP

    def bar_plot(self, comparision_group: str):
        '''Creates a grouped bar plot comparing curent class.object profile against other class.object profile  '''
        if comparision_group == 'all':
            df_by_group = self.df.sort_values(
                by='ratio', ascending=False).head(10)
            y_1 = df_by_group['ratio']
            x_1 = df_by_group['index'].str.capitalize()
        else:
            df_by_group = self.df[self.df['groups'] == comparision_group].sort_values(
                by='ratio', ascending=False).head(10)
            y_1 = df_by_group['ratio']
            x_1 = df_by_group['index'].str.capitalize()

        self.bar_fig = go.Figure(data=[
            # Grouped bar principal plot
            go.Bar(
                name=f'''Job: {self.job}
            Experience: {self.experience}
            Degree: {self.degree}''',
                x=y_1,
                y=x_1,
                marker=dict(
                    color=self.primary_color,
                ),
                text=y_1,
                textposition='inside',
                orientation='h'
            ),
        ])

        # Title
        self.bar_fig.update_layout(

            yaxis=dict(
                showgrid=False,
                showline=False,
                showticklabels=True,
                autorange="reversed",
                domain=[0, 0.95],
            ),
            xaxis=dict(
                domain=[0, 0.90],
            ),
            showlegend=False,
            # legend=dict(x=0.4, y=1.1, font_size=16, ),
            margin=dict(l=5, r=5, t=5, b=5),
            paper_bgcolor=self.background_color,
            plot_bgcolor=self.background_color,
        )
        return self.bar_fig