from molinterrogator.target import _target_df
from molinterrogator.target import _compound_df
from molinterrogator.target import _compound_from_target_df

def _target_id_2_card_dict(db_id=None, client=None):

        result = client.target.filter(target_chembl_id__in=db_id)[0]
        tmp_dict = {}
        tmp_dict['Name'] = result['pref_name']
        tmp_dict['Type'] = result['target_type']
        tmp_dict['Organism'] = result['organism']
        tmp_dict['ChEMBL'] = result['target_chembl_id']
        if len(result['target_components'])>1:
            print('El target ',tmp_dict['ChemBL'],'tiene más de un target_components y no se que\
                  es.')
        for xref in result['target_components'][0]['target_component_xrefs']:
            src_db = xref['xref_src_db']
            id_db = xref['xref_id']
            if src_db == 'PDBe':
                try:
                    tmp_dict['PDB'].append(id_db)
                except:
                    tmp_dict['PDB']=[id_db]
            elif src_db == 'UniProt':
                try:
                    tmp_dict['UniProt'].append(id_db)
                except:
                    tmp_dict['UniProt']=[id_db]
            elif src_db == 'IntAct':
                try:
                    tmp_dict['IntAct'].append(id_db)
                except:
                    tmp_dict['IntAct']=[id_db]
            elif src_db == 'InterPro':
                try:
                    tmp_dict['InterPro'].append(id_db)
                except:
                    tmp_dict['InterPro']=[id_db]

        tmp_dict['BindingDB']=tmp_dict['UniProt']

        del(result)

        return tmp_dict

def _compound_from_target_2_card_dict(compound_result=None, client=None):

        tmp_dict = {}
        tmp_dict['Name'] = compound_result['molecule_pref_name']
        tmp_dict['Smile'] = compound_result['canonical_smiles']
        tmp_dict['Compound ChemBL'] = compound_result['molecule_chembl_id']
        tmp_dict['Assay ChemBL'] = compound_result['assay_chembl_id']
        tmp_dict['Document ChemBL'] = compound_result['document_chembl_id']

        if compound_result['standard_type']=='IC50':
            tmp_dict['IC50']=compound_result['standard_value']+' '+compound_result['standard_units']
        else:
            print('Type of compound value not known for molecule_chembl_id:', compound_result['molecule_chembl_id'])

        return tmp_dict

class _target_query():

    def __init__(self, query=None):

        self.string = query
        self.query = None
        self.card = _target_df.copy()

        self.run_query()
        self.update_results(index_result=0)

    def run_query(self):

        from chembl_webresource_client.new_client import new_client
        self.query = new_client.target.filter(target_synonym__icontains=self.string)
        del(new_client)

    def info_results(self):

        from chembl_webresource_client.new_client import new_client

        tmp_df = _target_df.copy()
        for result in self.query:
            chembl_id = result['target_chembl_id']
            tmp_df =tmp_df.append(_target_id_2_card_dict(chembl_id, new_client),ignore_index=True)

        del(new_client)

        return tmp_df

    def update_results(self,index_result=0):

        from chembl_webresource_client.new_client import new_client

        chembl_id = self.query[index_result]['target_chembl_id']

        self.card = _target_df.copy()
        self.card =self.card.append(_target_id_2_card_dict(chembl_id, new_client),ignore_index=True)

        self.activity_filter = new_client.molecule.filter(target_chembl_id__in=chembl_id)
        self.molecule_filter = new_client.activity.filter(target_chembl_id__in=chembl_id)

        self.compounds = _compound_from_target_df.copy()

        for result in self.molecule_filter:
            tmp_dict = _compound_from_target_2_card_dict(result, new_client)
            self.compounds = self.compounds.append(tmp_dict,ignore_index=True)


print ("Addition - 1")
print ("Subtraction - 2")
print ("Multiplication - 3")
print ("Division - 4")

i = int(input("Choose operation - "))
num_1 = int(input("First digit "))
num_2 = int(input("Second digit "))

def calc(i):
        if i == 1:
            result = num_1 + num_2
            print(f"{num_1} addition by {num_2} is {result}")
        elif i == 2:
            result = (num_1 - num_2)
            print(f"{num_1} subtraction by {num_2} is {result}")
        elif i == 3:
            result = (num_1 * num_2)
            print(f"{num_1} multiplication by {num_2} is {result}")
        elif i == 4:
            result = (num_1 / num_2)
            print(f"{num_1} subtraction by {num_2} is {result}")
        else:
            print("Choose again")
        return i

calc(i)
print(__name__)


import sys
import numpy as np
import pandas as pd

if __name__ == "__main__":
    print("Filter peaks by intensities")
    input_fname = sys.argv[1]
    df = pd.read_csv(input_fname, sep = "\t")
    print("Peak number", len(df))
    inte = df['intensity'].to_numpy()
    best_bin_cnt = len(df)
    best_bin_min = np.min(inte)
    best_bin_max = np.max(inte)
    while best_bin_cnt > 100:
        hist = np.histogram(inte)
        best_bin_idx = np.argmax(hist[0])
        best_bin_cnt = hist[0][best_bin_idx]
        best_bin_min = hist[1][best_bin_idx]
        best_bin_max = hist[1][best_bin_idx+1]
        new_inte = []
        print(best_bin_idx, best_bin_min, best_bin_max)
        for item in inte:
            if item >= best_bin_min and item <= best_bin_max:
                new_inte.append(item)
        print("new intensity size", len(new_inte))
        inte = new_inte
    cutoff = (best_bin_min + best_bin_max)/2
    sn_ratio = float(sys.argv[2])
    print("cutoff", cutoff, "sn_ratio", sn_ratio)
    cutoff = cutoff * sn_ratio
    df = df.drop(df[df.intensity < cutoff].index)
    print("Peak number after filtering", len(df))

    output_fname = sys.argv[3]
    df.to_csv(output_fname, sep="\t", index=False)
    #np_data = df.to_numpy()
    #file_header = "scan_id" + "\t" + "scan_num" + "\t" + "retention_time" + "\t" + "mz" + "\t" + "intensity"
    #np.savetxt(output_fname, np_data, delimiter = "\t", header = file_header)


"""
2016 Day 14
https://adventofcode.com/2016/day/14
"""

from dataclasses import dataclass
from hashlib import md5
from typing import Iterator, List
import re
import aocd  # type: ignore

RE_THREEPEATS = re.compile(r"(\w)\1{2,2}")

def first_threepeat(text: str) -> str:
    """
    Find the first 'threepeated' (i.e. repeated 3 times in a row) character in a string.
    """
    search = RE_THREEPEATS.search(text)
    if search:
        return search.group()[0]
    return ""

def md5hex(text: str) -> str:
    """
    Get the hex digest of the MD5 hash of the given string.
    """
    return md5(text.encode("utf-8")).hexdigest()

def stretched_hash(text: str) -> str:
    """
    Produce the stretched hash of a string by recursively running the MD5 hash algorithm 2017
    times.
    """
    for _ in range(2017):
        text = md5hex(text)
    return text

@dataclass
class KeyCandidate:
    """
    A candidate which could be a key (due to a triple character), but is potentially awaiting the
    examination of the following 1000 hashes.
    """

    hash: str
    char: str
    index: int
    matched: int

def find_keys(
    salt: str, quantity: int, stretched: bool = False
) -> Iterator[KeyCandidate]:
    """
    Search for an appropriate quantity of keys (i.e. exploring candidates with a triple character
    and checking for a hash in the next 1000 in the stream containing five of the character).
    """
    returned = 0
    index = 0
    candidates: List[KeyCandidate] = []
    create_hash = stretched_hash if stretched else md5hex

    while returned < quantity:
        hsh = create_hash(salt + str(index))
        candidates = [
            candidate for candidate in candidates if candidate.index >= (index - 1000)
        ]
        paired = [candidate for candidate in candidates if candidate.char * 5 in hsh]

        if paired:
            for candidate in paired:
                candidate.matched = index

        char = first_threepeat(hsh)
        if char:
            candidates.append(KeyCandidate(hsh, char, index, -1))

        if len(candidates) > 0 and candidates[0].matched > -1:
            candidate, *candidates = candidates
            yield candidate
            returned += 1

        index += 1

def main() -> None:
    """
    Calculate and output the solutions based on the real puzzle input.
    """
    data = aocd.get_data(year=2016, day=14)

    *_, solution1 = find_keys(data, 64)
    print(f"Part 1: {solution1.index}")

    *_, solution2 = find_keys(data, 64, True)
    print(f"Part 2: {solution2.index}")

if __name__ == "__main__":
    main()


#
# @lc app=leetcode id=191 lang=python3
#
# [191] Number of 1 Bits
#

# @lc code=start
class Solution:
    def hammingWeight(self, n: int) -> int:
        """
        LSB
        T : O(1) 37.23% | 52ms
        S : O(1) 50.21% | 13.8mb
        """
        count = 0
        while n:
            count += n & 1  # LSB
            n >>= 1  # Shift right by 1
        return count

    def hammingWeight(self, n: int) -> int:
        """
        T : O(1) 96.28% | 29ms
        S : O(1) 7.78% | 13.9mb
        """
        count = 0
        while n:
            n &= n-1  # this will take out the right-most 1 of n
            count += 1
        return count
# @lc code=end


"""
A custom Python format is a subclass of Formatter, with the logic split into
OutputFormatter for outputting to a format, and FormatExtractor for reading
from a format

The parameters it expects are specified in the format.json file.

Note: the name of the class itself is not relevant.

"""

from dataiku.customformat import Formatter, OutputFormatter, FormatExtractor

import base64
import datetime
import json
import logging
import os
import pandas

from tableau_table_reader import TableauTableReader

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='Plugin: Tableau Hyper API | %(levelname)s - %(message)s')

class MyFormatter(Formatter):

    def __init__(self, config, plugin_config):
        """
        The configuration parameters set up by the user for the formatter instance
        are passed as a json object 'config' to the constructor.
        The static configuration parameters set up by the developer in the optional
        file settings.json at the root of the plugin directory are passed as a json
        object 'plugin_config' to the constructor
        """
        Formatter.__init__(self, config, plugin_config)

    def get_output_formatter(self, stream, schema):
        """
        Return a OutputFormatter for this format
        :param stream: the stream to write the formatted data to
        :param schema: the schema of the rows that will be formatted (never None)
        """
        return MyOutputFormatter(stream, schema)

    def get_format_extractor(self, stream, schema=None):
        """
        Return a FormatExtractor for this format
        :param stream: the stream to read the formatted data from
        :param schema: the schema of the rows that will be extracted. None when the extractor is used to detect the format.
        """
        table_name = self.config['table_name']
        schema_name = self.config['schema_name']
        return MyFormatExtractor(stream, schema, table_name=table_name, schema_name=schema_name)

class MyOutputFormatter(OutputFormatter):
    """
    Writes a stream of rows to a stream in a format. The calls will be:

    * write_header()
    * write_row(row_1)
      ...
    * write_row(row_N)
    * write_footer()

    """

    def __init__(self, stream, schema):
        """
        Initialize the formatter
        :param stream: the stream to write the formatted data to
        """
        OutputFormatter.__init__(self, stream)
        self.schema = schema

    def write_header(self):
        """
        Write the header of the format (if any)
        """
        if self.schema is not None:
            self.stream.write(' ' + base64.b64encode(json.dumps(self.schema['columns'])) + '\n')

    def write_row(self, row):
        """
        Write a row in the format
        :param row: array of strings, with one value per column in the schema
        """
        clean_row = []
        for column_value in row:
            if isinstance(column_value, datetime.datetime):
                clean_row.append(column_value.isoformat())
            elif isinstance(column_value, pandas.Timestamp):
                clean_row.append(column_value.isoformat())
            else:
                clean_row.append(column_value)
        self.stream.write(base64.b64encode(json.dumps(clean_row)) + '\n')

    def write_footer(self):
        """
        Write the footer of the format (if any)
        """
        pass

class MyFormatExtractor(FormatExtractor):
    """
    Read the input format
    """

    def __init__(self, stream, schema, table_name=None, schema_name=None):
        """
        Initialize the extractor
        :param stream: the stream to read the formatted data from
        """
        FormatExtractor.__init__(self, stream)
        self.tableau_reader = TableauTableReader(table_name=table_name, schema_name=schema_name)
        self.tableau_reader.create_tmp_hyper_file()
        self.tableau_reader.read_buffer(stream)
        self.tableau_reader.open_connection()
        self.tableau_reader.read_hyper_columns()

    def read_schema(self):
        """
        Get the schema of the data in the stream, if the schema can be known upfront.
        """
        return self.tableau_reader.read_schema()

    def read_row(self):
        """
        Read one row from the formatted stream
        :returns: a dict of the data (name, value), or None if reading is finished
        """
        return self.tableau_reader.read_row()


from functools import lru_cache

from google.oauth2 import service_account

from issue_tracker_bot.settings import CREDENTIALS_PATH

SCOPES = [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive",
]

@lru_cache()
def get_credentials():
    credentials = service_account.Credentials.from_service_account_file(
        str(str(CREDENTIALS_PATH))
    )
    return credentials.with_scopes(SCOPES)


file1 = open('reportRepair_input.txt', 'r')
lines_read = file1.readlines()

def calculate_answer(lines):
    for i in range(len(lines)):
        number = int(lines[i])
        if number >= 2020:
            continue
        for j in range(i, len(lines)):
            second_number = int(lines[j])
            if number + second_number == 2020:
                print(number * second_number)
                return

calculate_answer(lines_read)


import time
import math
import torch
import torch.nn as nn
import numpy as np
from data.utils import *
from data.datasets import STGODEDataset
from torch.utils.data import DataLoader
from trainer.base_trainer import BaseTrainer
from util.logging import *
from logger.logger import Logger

class STGODETrainer(BaseTrainer):
    def __init__(self, cls, config, args):
        self.config = config
        self.device = self.config.device
        self.cls = cls
        self.setup_save(args)
        self.logger = Logger()

    def load_dataset(self):
        if os.path.exists("{}/{}_train_{}_{}.npy".format(self.config.dataset_dir, self.config.dataset_name, self.config.train_ratio, self.config.test_ratio)) and \
            os.path.exists("{}/{}_test_{}_{}.npy".format(self.config.dataset_dir, self.config.dataset_name, self.config.train_ratio, self.config.test_ratio)) and \
                os.path.exists("{}/{}_val_{}_{}.npy".format(self.config.dataset_dir, self.config.dataset_name, self.config.train_ratio, self.config.test_ratio)) and \
                os.path.exists("{}/{}_dtw_distance.npy".format(self.config.dataset_dir, self.config.dataset_name)) and \
                os.path.exists("{}/{}_spatial_distance.npy".format(self.config.dataset_dir, self.config.dataset_name)):
            print(toGreen('Found generated dataset in '+self.config.dataset_dir))
        else:
            print(toGreen('Generating dataset...'))
            generate_data_matrix(self.config.dataset_dir, self.config.dataset_name, self.config.train_ratio,
             self.config.test_ratio, self.config.sigma1, self.config.sigma2, self.config.thres1, self.config.thres2, self.config.data_format)
        if self.config.data_format == 'h5':
            data = pd.read_hdf(os.path.join(self.config.dataset_dir, self.config.dataset_name)+".h5").values
            data = np.expand_dims(data, -1)
        elif self.config.data_format == 'npz':
            data = np.load(os.path.join(self.config.dataset_dir, self.config.dataset_name)+".npz")['data']
        self.config.num_nodes = data.shape[1]
        self.config.num_features = data.shape[2]
        datasets = {}

        for category in ['train', 'val', 'test']:
            data = np.load("{}{}_{}_{}_{}.npy".format(self.config.dataset_dir, self.config.dataset_name, category, self.config.train_ratio, self.config.test_ratio))
            #x, y = seq2instance(data, self.config.num_his, self.config.num_pred)
            '''
            if category == 'train':
                self.mean, self.std = np.mean(x), np.std(x)
            x = (x - self.mean) / self.std
            '''
            datasets[category] = data

        return datasets

    def get_matrices(self):
        # distance matrix
        dist_matrix = np.load("{}/{}_dtw_distance.npy".format(self.config.dataset_dir, self.config.dataset_name))
        mean = np.mean(dist_matrix)
        std = np.std(dist_matrix)
        dist_matrix = (dist_matrix - mean) / std
        sigma = self.config.sigma1
        dist_matrix = np.exp(-dist_matrix ** 2 / sigma ** 2)
        dtw_matrix = np.zeros_like(dist_matrix)
        dtw_matrix[dist_matrix > self.config.thres1] = 1
        # spatial matrix
        dist_matrix = np.load("{}/{}_spatial_distance.npy".format(self.config.dataset_dir, self.config.dataset_name))
        # normalization
        std = np.std(dist_matrix[dist_matrix != np.float('inf')])
        mean = np.mean(dist_matrix[dist_matrix != np.float('inf')])
        dist_matrix = (dist_matrix - mean) / std
        sigma = self.config.sigma2
        sp_matrix = np.exp(- dist_matrix**2 / sigma**2)
        sp_matrix[sp_matrix < self.config.thres2] = 0

        #print(f'average degree of spatial graph is {np.sum(sp_matrix > 0)/2/self.config.num_nodes}')
        #print(f'average degree of semantic graph is {np.sum(dtw_matrix > 0)/2/self.config.num_nodes}')

        A_sp_wave = get_normalized_adj(sp_matrix).to(self.device)
        A_se_wave = get_normalized_adj(dtw_matrix).to(self.device)
        return A_sp_wave, A_se_wave

    def setup_model(self):
        A_sp_wave, A_se_wave = self.get_matrices()
        print(self.config.num_nodes, self.config.num_features)
        self.model = self.cls(self.config, A_sp_wave, A_se_wave).to(self.device)

    def compose_dataset(self):
        datasets = self.load_dataset()
        for category in ['train', 'val', 'test']:
            datasets[category] = STGODEDataset(datasets[category], self.config.num_his, self.config.num_pred)
        self.num_train_iteration_per_epoch = math.ceil(len(datasets['train']) / self.config.batch_size)
        self.num_val_iteration_per_epoch = math.ceil(len(datasets['val']) / self.config.test_batch_size)
        self.num_test_iteration_per_epoch = math.ceil(len(datasets['test']) / self.config.test_batch_size)
        return datasets['train'], datasets['val'], datasets['test']

    def compose_loader(self):
        train_dataset, val_dataset, test_dataset = self.compose_dataset()
        print(toGreen('Dataset loaded successfully ') + '| Train [{}] Test [{}] Val [{}]'.format(toGreen(len(train_dataset)), toGreen(len(test_dataset)), toGreen(len(val_dataset))))
        self.train_loader = DataLoader(train_dataset, batch_size=self.config.batch_size, shuffle=True)
        self.val_loader = DataLoader(val_dataset, batch_size=self.config.test_batch_size, shuffle=False)
        self.test_loader = DataLoader(test_dataset, batch_size=self.config.test_batch_size, shuffle=False)

    def train_epoch(self, epoch):
        self.model.train()
        start_time = time.time()
        total_loss = 0
        total_metrics = np.zeros(len(self.metrics))

        for batch_idx, (data, target) in enumerate(self.train_loader):
            label = target[..., :self.config.output_dim]  # (..., 1)  supposed to be numpy array
            data, target = data.to(self.device), target.to(self.device)
            self.optimizer.zero_grad()

            output = self.model(data)

            output = output.cpu()
            loss = self.loss(output, label)  # loss is self-defined, need cpu input
            loss.backward()

            torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.config.max_grad_norm)
            self.optimizer.step()
            training_time = time.time() - start_time
            start_time = time.time()

            total_loss += loss.item()
            this_metrics = self._eval_metrics(output, label)
            total_metrics += this_metrics

            print_progress('TRAIN', epoch, self.config.total_epoch, batch_idx, self.num_train_iteration_per_epoch, training_time, self.config.loss, loss.item(), self.config.metrics, this_metrics)

        return total_loss, total_metrics

    def validate_epoch(self, epoch, is_test):
        self.model.eval()
        total_loss = 0
        total_metrics = np.zeros(len(self.metrics))
        start_time = time.time()

        for batch_idx, (data, target) in enumerate(self.test_loader if is_test else self.val_loader):
            label = target[..., :self.config.output_dim]
            data, target = data.to(self.device), target.to(self.device)
            self.optimizer.zero_grad()

            with torch.no_grad():
                output = self.model(data)

            output = output.cpu()
            loss = self.loss(output, label)  # loss is self-defined, need cpu input

            valid_time = time.time() - start_time
            start_time = time.time()

            total_loss += loss.item()
            this_metrics = self._eval_metrics(output, label)
            total_metrics += this_metrics

            print_progress('TEST' if is_test else 'VALID', epoch, self.config.total_epoch, batch_idx, \
                self.num_test_iteration_per_epoch if is_test else self.num_val_iteration_per_epoch, valid_time, \
                self.config.loss, loss.item(), self.config.metrics, this_metrics)

        return total_loss, total_metrics

    def _eval_metrics(self, output, target):
        acc_metrics = np.zeros(len(self.metrics))
        for i, metric in enumerate(self.metrics):
            acc_metrics[i] += metric(output, target)
            # break
        return acc_metrics


from math import gcd, isqrt
def pollards_p1(n, lim = 10**2):
    a = 2
    for j in range(2, lim):
        a = pow(a, j, n)
        if 1 < gcd(a-1, n) < n:
            return gcd(a-1, n)
    return -1
def factor(n):
    f = []
    for i in range(2, isqrt(n) + 1):
        while n%i==0:
            n//=i
            f.append(i)
    return (f + [n]) if n!= 1 else f

for i in range(1, 101):
    print(i, factor(i*i-2*i+2), factor(i*i+2*i+2))


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
    path('finches/', views.finches_index, name='index'),
    path('finches/<int:finch_id>/', views.finches_detail, name='detail'),
    path('finches/create/', views.FinchCreate.as_view(), name='finches_create'),
    path('finches/<int:pk>/update/', views.FinchUpdate.as_view(), name='finches_update'),
    path('finches/<int:pk>/delete/', views.FinchDelete.as_view(), name='finches_delete'),
    path('finches/<int:finch_id>/add_sighting/', views.add_sighting, name='add_sighting'),
    path('finches/<int:finch_id>/assoc_food/<int:food_id>/', views.assoc_food, name='assoc_food'),
    path('finches/<int:finch_id>/unassoc_food/<int:food_id>/', views.unassoc_food, name='unassoc_food'),
    path('food/create/', views.FoodCreate.as_view(), name='food_create'),
    path('food/<int:pk>/update/', views.FoodUpdate.as_view(), name='food_update'),
    path('food/<int:pk>/delete/', views.FoodDelete.as_view(), name='food_delete'),
    path('food/', views.food_index, name='food_index'),
    path('food/<int:food_id>/', views.food_detail, name='food_detail'),
]


import json
import re

from django.http import JsonResponse
from django.views import View
from django.utils import timezone

from .models import MetroRegion, LocalRegion, PlaceType, Place

REGEX_ROAD_ADDRESS = '(([가-힣A-Za-z·\d~\-\.]{2,}(로|길).[\d]+)|([가-힣A-Za-z·\d~\-\.]+(읍|동)\s)[\d]+)'

class PlaceCreateView(View):
    def post(self, request):
        try:
            data = json.loads(request.body)
            place_type = PlaceType.objects.get(name = data['place_type'])
            metro_region = MetroRegion.objects.get(name=data['metro_region'])
            local_region = LocalRegion.objects.get(name=data['local_region'], metro_region = metro_region)
            road_address = data['road_address']
            name = data['name']

            assert re.match(REGEX_ROAD_ADDRESS, road_address), "INVALID_ROAD_ADDRESS_FORMAT"

            if Place.objects.filter(name = name, place_type = place_type, road_address = road_address, region = local_region, deleted_at = None).exists():
                return JsonResponse({"message": "EXIST_PLACE"}, status = 400)

            Place.objects.create(
                    name         = name,
                    place_type   = place_type,
                    road_address = road_address,
                    region       = local_region
            )
            return JsonResponse({"message": "PLACE_CREATED"}, status = 201)

        except json.JSONDecodeError as e:
            return JsonResponse({"message": f"{e}"}, status = 400)
        except KeyError:
            return JsonResponse({"message": "KEY_ERROR"}, status = 400)
        except AssertionError as e:
            return JsonResponse({"message": f"{e}"}, status = 400)
        except PlaceType.DoesNotExist:
            return JsonResponse({"message": "INVALID_PLACE_TYPE"}, status = 401)
        except MetroRegion.DoesNotExist:
            return JsonResponse({"message": "INVALID_REGION"}, status = 401)
        except LocalRegion.DoesNotExist:
            return JsonResponse({"message": "INVALID_REGION"}, status = 401)

    def get(self, request):
        places = Place.objects.select_related('place_type', 'region', 'region__metro_region').filter(deleted_at__isnull = True)

        result = [
                    {
                        "id"           : place.id,
                        "name"         : place.name,
                        "type"         : place.place_type.name,
                        "road_address" : place.road_address,
                        "local_region" : place.region.name,
                        "metro_region" : place.region.metro_region.name
                    } for place in places
                ]
        return JsonResponse({"result": result}, status = 200)

class PlaceView(View):
    def patch(self, request, place_pk):
        try:
            data = json.loads(request.body)

            data_keys = data.keys()

            place_type = PlaceType.objects.get(name = data['place_type'])
            metro_region = MetroRegion.objects.get(name=data['metro_region'])
            local_region = LocalRegion.objects.get(name=data['local_region'], metro_region = metro_region)
            road_address = data['road_address']
            name = data['name']

            assert re.match(REGEX_ROAD_ADDRESS, road_address), "INVALID_ROAD_ADDRESS_FORMAT"

            patch_object = Place.objects.get(id = place_pk)

            if patch_object.deleted_at == None:
                patch_object.place_type   = place_type
                patch_object.region       = local_region
                patch_object.road_address = road_address
                patch_object.name         = name
                patch_object.save()
                return JsonResponse({"message": "PLACE_UPDATED"}, status = 200)
            else:
                return JsonResponse({"message": "DELETED_PLACE"}, status = 400)

        except json.JSONDecodeError as e:
            return JsonResponse({"message": f"{e}"}, status = 400)
        except AssertionError as e:
            return JsonResponse({"message": f"{e}"}, status = 400)
        except Place.DoesNotExist:
            return JsonResponse({"message": "INVALID_PLACE"}, status = 401)
        except PlaceType.DoesNotExist:
            return JsonResponse({"message": "INVALID_PLACE_TYPE"}, status = 401)
        except MetroRegion.DoesNotExist:
            return JsonResponse({"message": "INVALID_REGION"}, status = 401)
        except LocalRegion.DoesNotExist:
            return JsonResponse({"message": "INVALID_REGION"}, status = 401)

    def delete(self, request, place_pk):
        try:
            delete_object = Place.objects.get(id = place_pk)

            if delete_object.deleted_at == None:
                delete_object.deleted_at = timezone.now()
                delete_object.save()
                return JsonResponse({"message": "PLACE_DELETED"}, status = 200)
            else:
                return JsonResponse({"message": "PLACE_IS_ALREADY_DELETED"}, status = 400)
        except Place.DoesNotExist:
            return JsonResponse({"message": "INVALID_PLACE"}, status = 401)


#!/usr/bin/env python3
# _*_ coding: utf-8 _*_

from collections import namedtuple
from xml.dom.minidom import Document

def readFile(filename, lines):
    with open(filename, 'r') as f:
        for line in f:
            line = line.rstrip(' \n')
            if line.startswith('//') or len(line) == 0:
                continue
            lines.append(line)

def writeXml(filename, lines, tagNames):
    # 创建doc
    doc = Document()
    # 创建根节点
    root = doc.createElement(tagNames[0])
    doc.appendChild(root)

    # 记录每层节点的最新元素
    nodes = {0: root}

    for line in lines:
        index = line.rfind(' ')
        level = (index + 1) / 4 + 1
        line = line.lstrip(' ')

        node = doc.createElement(tagNames[level])
        node.setAttribute('name', line)

        nodes[level - 1].appendChild(node)
        nodes[level] = node

    with open(filename, 'w') as f:
        f.write(doc.toprettyxml(indent='\t'))

def display(lines):
    for line in lines:
        print(line)

if __name__ == '__main__':
    lines = []
    readFile('./file/sector.txt', lines)

    tagNames = ['SectorFile', 'Sectors', 'sector', 'sector_second']
    writeXml('./file/sector.xml', lines, tagNames)


import subprocess

players = ["Simon", "Hugo", "Hebbian_Simon", "LIFELSE"]

for player in players:
    command = [
        "python",
        "PongUI.py",
        "--player",
        player,
        "--num_repeat",
        "5",
        "--num_episodes",
        "70",
        "--verbose",
        "2",
        "--fps",
        "24",
        "--simulation_only",
    ]

    subprocess.run(command)


#list
mylist = [1,2,3,4,5]
print(mylist[0])
#print(mylist[5])
print(mylist[-2])

# Append a new element to the end of the list using the append() method
mylist.append(6)
print(mylist)  # prints [1, 2, 3, 4, 5, 6]

# Remove the first occurrence of a given element from the list using the remove() method
mylist.remove(3)
print(mylist)  # prints [1, 2, 4, 5, 6]

# Get the length of the list using the len() function
length = len(mylist)
print(length)  # prints 6

# Count the number of occurrences of a given element in the list using the count() method
count = mylist.count(5)
print(count)  # prints 1

# Sort the elements of the list in ascending order using the sort() method
mylist.sort()
mylist.sort(reverse = True)
print(mylist)  # prints [1, 2, 4, 5, 6]

# Insert a new element at a specific index in the list using the insert() method
mylist.insert(2, 3)
print(mylist)  # prints [1, 2, 3, 4, 5, 6]

#string
mystring = 'ICSM Python'
mystring_lower = mystring.lower()
print(len(mystring))
print(mystring_lower[3])

#slicing
#start - inclusive
#stop - exclusive
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Get a sublist containing the 3rd, 4th, and 5th elements of the list
sublist = numbers[2:5]
print(sublist)  # prints [3, 4, 5]

# Get a sublist containing every other element of the list, starting from the 2nd element
sublist = numbers[1::2]
print(sublist)

# Get a sublist containing the last 3 elements of the list in reverse order
sublist = numbers[-1:-4:-1]
print(sublist)

#reverse

print(numbers[::-1])

#dictionary

mydict = {'name': 'BZ', 'age': 20, 'course': 'MBBS'}

# Get the length of the dictionary using the len() function
length = len(mydict)
print(length)  # prints 3

# Check if a key exists in the dictionary using the in keyword
print('name' in mydict)  # prints True
print('uni' in mydict)  # prints False

# Get a list of the keys in the dictionary using the keys() method
keys = mydict.keys()
print(keys)

# Get a list of the values in the dictionary using the values() method
values = mydict.values()
print(values)

# Get a list of the key-value pairs in the dictionary using the items() method
items = mydict.items()
print(items)

# Remove a key-value pair from the dictionary using the pop() method
my_dict_2 = mydict.pop('course')
print(my_dict_2)

# Add a new key-value pair to the dictionary
mydict['university'] = 'ICL'

# Modify the value associated with an existing key in the dictionary
mydict['age'] = 21

# Print the entire dictionary
print(mydict)

#EXAMPLE DATABASE
patient_dict = {
    'patient_id': 12345,
    'first_name': 'Zeric',
    'last_name': 'Zmith',
    'date_of_birth': '1970-01-01',
    'gender': 'male',
    'ethnicity':'black asian',
    'address': '52 Harley St',
    'phone_number': '079',
    'emergency_contact': {
        'name': 'Zemily Zmith',
        'relationship': 'sister',
        'phone_number': '078'
    },
    'medical_history': [
        {
            'condition': 'hypertension',
            'diagnosed_date': '2010-01-01',
            'medication': 'amlodipine 5mg'
        },
        {
            'condition': 'T2D',
            'diagnosed_date': '2012-01-01',
            'treatment': 'metformin 500mg'
        }
    ]
}

# Get the relationship of the emergency contact
relationship = patient_dict['emergency_contact']['relationship']
print(relationship)

# Get the treatment for the first medical condition
treatment = patient_dict['medical_history'][0]['medication']
print(treatment)

#for loop - star sign
# starsignlist = ['Capricorn', 'Aquarius', 'Pisces', 'Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius']
# monthlist = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

# newlist = []
# #range() (start,stop,step)
# for i in range(len(starsignlist)):
#     month = monthlist[i]
#     sign = starsignlist[i]
#     newlist.append((month, sign))

# print(newlist)
#len(starsignlist)

import random

names = ["Zephyrine Zinnober", "Xanthe Xylaria", "Boroumand Zeidaabadi"]

for name in names:
    first_name, last_name = name.split()
    first_initial = first_name[0].lower()
    second_initial = last_name[0].lower()
    random_number = str(random.randint(1,9))
    email = first_initial + second_initial + random_number + "20@ic.ac.uk"
    print(email)

#while vs for loop
# Using a while loop to print the numbers from 1 to 10
i = 1
while i <= 10:
    print(i)
    i += 1

# Using a for loop to print the numbers from 1 to 10
for i in range(1, 11):
    print(i)

# Initialize a list of names
names = ['John', 'Alice', 'Bob', 'Charlie', 'Eve']

# Use a while loop to iterate over the list of names
i = 0
while i < len(names):
    name = names[i]
    print(name)
    #i = i + 1
    i += 1

# Use a for loop to iterate over the list of names
for name in names:
    print(name)

#can be used together but I will show this in week 4


# q2 Loop over your list from Part 1, and use requests to download the CSV files from.
import requests
#loop over a file
for line in open("ELECTION_ID"):
    addr=addr="http://historical.elections.virginia.gov/elections/download/{}/precincts_include:0".format(line[5:10])
    resp=requests.get(addr)
    s=resp.status_code
    print(s)
#print contents of response
    file_name = line[0:4] +".csv" #saving it as year.csv as it saves extra line of codes for me in the next question. For naming it per the question the code:file_name = "president_general_"+line[0:4] +".csv"
    with open(file_name, "w") as out:
        out.write(resp.text)


from enum import Enum
import numpy as np
from pyqtgraph import AxisItem
from time import strftime, localtime
import time
import datetime
import os

import openQCM
from openQCM.common.architecture import Architecture,OSType

###############################################################################
# Enum for the types of sources. Indices MUST match app_sources constant
###############################################################################
class SourceType(Enum):
    serial = 0
    calibration = 1
    SocketClient = 2

###############################################################################
# Specifies the minimal Python version required
###############################################################################
class MinimalPython:
    major = 3
    minor = 2
    release = 0

###############################################################################
# Common constants and parameters for the application.
###############################################################################
class Constants:

    ##########################
    # Application Parameters #
    ##########################
    app_title = "Real-Time openQCM GUI"
    app_version = openQCM.__version__
    app_sources = ["Measurement openQCM Q-1 Device", "Calibration openQCM Q-1 Device"]#, "Socket Client"]
    app_encoding = "utf-8"

    ###################
    # PLOT parameters #
    ###################
    plot_update_ms = 200#16
    plot_colors = ['#ff0000', '#0072bd', '#00ffff', '#edb120', '#7e2f8e', '#77ac30', '#4dbeee', '#a2142f']
    plot_max_lines = len(plot_colors)

    ####################
    #  SAMPLES NUMBER  #
    ####################
    argument_default_samples = 501#1001

    ####################################
    # FILTERING and FITTING parameters #
    ####################################
    # Notes:
    # left and right frequencies in the area of the resonance frequency
    # Savitzky-Golay size of the data window
    # Savitzky-Golay order of the polynomial fit
    # Number of spline points: same as the frequency band +1 (es.5001)
    # Spline smoothing factor

    # Savitzky-Golay order of the polynomial fit (common for all)
    SG_order = 3

    #--------------
    # 5MHz
    #--------------
    # left and right frequencies
    L5_fundamental = 5500
    R5_fundamental = 2500
    # Savitzky-Golay size of the data window
    SG_window_size5_fundamental = 9
    # Spline smoothing factor
    Spline_factor5_fundamental = 0.05
    # left and right frequencies
    L5_3th_overtone = 7500
    R5_3th_overtone = 2500
    # Savitzky-Golay size of the data window
    SG_window_size5_3th_overtone = 11
    # Spline smoothing factor
    Spline_factor5_3th_overtone = 0.01

    # left and right frequencies
    L5_5th_overtone = 10000
    R5_5th_overtone = 2500
    # Savitzky-Golay size of the data window
    SG_window_size5_5th_overtone = 11
    # Spline smoothing factor
    Spline_factor5_5th_overtone = 0.01

    # left and right frequencies
    L5_7th_overtone = 50000
    R5_7th_overtone = 2500
    # Savitzky-Golay size of the data window
    SG_window_size5_7th_overtone = 33
    # Spline smoothing factor
    Spline_factor5_7th_overtone = 0.01

    # TODO
    # left and right frequencies
    L5_9th_overtone = 5000000
    R5_9th_overtone = 100000
    # Savitzky-Golay size of the data window
    SG_window_size5_9th_overtone = 5
    # Spline smoothing factor
    Spline_factor5_9th_overtone = 0.5

    #--------------
    # 10MHz
    #--------------
    # left and right frequencies
    L10_fundamental = 7500
    R10_fundamental = 2500
    # Savitzky-Golay size of the data window
    SG_window_size10_fundamental = 11
    # Spline smoothing factor
    Spline_factor10_fundamental = 0.01

    # left and right frequencies
    L10_3th_overtone = 13500
    R10_3th_overtone = 2500
    # Savitzky-Golay size of the data window
    SG_window_size10_3th_overtone = 11
    # Spline smoothing factor
    Spline_factor10_3th_overtone = 0.01

    # left and right frequencies
    L10_5th_overtone = 23000
    R10_5th_overtone = 3000
    # Savitzky-Golay size of the data window
    SG_window_size10_5th_overtone = 19
    # Spline smoothing factor
    Spline_factor10_5th_overtone = 0.01

    ##########################
    # SERIAL PORT Parameters #
    ##########################
    serial_default_speed = 115200
    serial_default_overtone = None
    serial_default_QCS = "@10MHz"
    serial_writetimeout_ms = 0
    serial_timeout_ms = None#0.01

    ######################
    # Process parameters #
    ######################
    process_join_timeout_ms = 2000
    simulator_default_speed = 0.1 # not used
    parser_timeout_ms = 0.005

    ##################
    # Log parameters #
    ##################
    log_export_path = "logged_data"
    log_filename = "{}.log".format(app_title)
    log_max_bytes = 5120
    log_default_level = 1
    log_default_console_log = False

    ######################################
    # File parameters for exporting data #
    ######################################
    # sets the slash depending on the OS types
    if Architecture.get_os() in [OSType.macosx, OSType.linux]:
       slash="/"
    else:
       slash="\\"

    csv_delimiter = "," # for splitting data of the serial port and CSV file storage
    csv_default_prefix = "%Y-%b-%d_%H-%M-%S"#"%H-%M-%S-%d-%b-%Y" # Hour-Minute-Second-month-day-Year
    csv_extension = "csv"
    txt_extension = "txt"
    csv_export_path = "logged_data"
    csv_filename = (strftime(csv_default_prefix, localtime()))#+'_DataLog')
    csv_sweeps_export_path = "{}{}{}".format(csv_export_path,slash,csv_filename)
    csv_sweeps_filename = "sweep"

    # Calibration: scan (WRITE for @5MHz and @10MHz QCS) path: 'common\'
    csv_calibration_filename    = "Calibration_5MHz"
    csv_calibration_filename10  = "Calibration_10MHz"
    csv_calibration_export_path = os.path.dirname(openQCM.__file__) #"common"

    ##################
    # Calibration: baseline correction (READ for @5MHz and @10MHz QCS) path: 'common\'
    csv_calibration_path   = "{}{}{}.{}".format(csv_calibration_export_path,slash,csv_calibration_filename,txt_extension)
    csv_calibration_path10 = "{}{}{}.{}".format(csv_calibration_export_path,slash,csv_calibration_filename10,txt_extension)

    # Frequencies: Fundamental and overtones (READ and WRITE for @5MHz and @10MHz QCS)
    csv_peakfrequencies_filename   = "PeakFrequencies"
    #csv_peakfrequencies_filename   = "PeakFrequencies_5MHz"
    #csv_peakfrequencies_filename10 = "PeakFrequencies_10MHz"
    cvs_peakfrequencies_path    = "{}{}{}.{}".format(csv_calibration_export_path,slash,csv_peakfrequencies_filename,txt_extension)
    #cvs_peakfrequencies_path10 = "{}{}{}.{}".format(csv_calibration_export_path,slash,csv_peakfrequencies_filename10,txt_extension)
    #########################
    '''
    # Calibration: baseline correction (READ for @5MHz and @10MHz QCS) path: 'common\'
    csv_calibration_path   = "{}\{}.{}".format(csv_calibration_export_path,csv_calibration_filename,txt_extension)
    csv_calibration_path10 = "{}\{}.{}".format(csv_calibration_export_path,csv_calibration_filename10,txt_extension)

    # Frequencies: Fundamental and overtones (READ and WRITE for @5MHz and @10MHz QCS)
    csv_peakfrequencies_filename   = "PeakFrequencies"
    #csv_peakfrequencies_filename   = "PeakFrequencies_5MHz"
    #csv_peakfrequencies_filename10 = "PeakFrequencies_10MHz"
    cvs_peakfrequencies_path    = "{}\{}.{}".format(csv_calibration_export_path,csv_peakfrequencies_filename,txt_extension)
    #cvs_peakfrequencies_path10 = "{}\{}.{}".format(csv_calibration_export_path,csv_peakfrequencies_filename10,txt_extension)
    '''

    ##########################
    # CALIBRATION PARAMETERS #
    ##########################

    # Peak Detection - distance in samples between neighbouring peaks
    dist5  =  8000 # for @5MHz
    dist10 =  10000 # for @10MHz
    calibration_default_samples = 50001
    calibration_frequency_start =  1000000
    calibration_frequency_stop  = 51000000
    calibration_fStep = (calibration_frequency_stop - calibration_frequency_start) / (calibration_default_samples-1)
    calibration_readFREQ  = np.arange(calibration_default_samples) * (calibration_fStep) + calibration_frequency_start
    #-------------------
    calib_fStep = 1000
    calib_fRange = 5000000 #
    calib_samples = 5001
    calib_sections = 10

    ###########################
    # Ring Buffers Parameters #
    ###########################
    ring_buffer_samples = 16363

    ##############################
    # Parameters for the average #
    ##############################
    environment = 50 #######################################################################################################
    SG_order_environment = 1
    SG_window_environment = 3

    ###################
    class SocketClient: #unused
        timeout = 0.01
        host_default = "localhost"
        port_default = [5555, 8080, 9090]
        buffer_recv_size = 1024
    ###################

'''
###############################################################################
#  Provides a date-time aware axis
###############################################################################
class DateAxis(AxisItem):

    """
    A tool that provides a date-time aware axis. It is implemented as an AxisItem
    that interprets positions as UNIX timestamps (i.e. seconds since 1970).
    The labels and the tick positions are dynamically adjusted depending on the range.
    """

    def __init__(self, *args, **kwargs):
        AxisItem.__init__(self, *args, **kwargs)
        self._oldAxis = None

    def tickStrings(self, values, scale, spacing):
        ret = []
        ep = datetime.datetime(1970,1,1,0,0,0)
        tonow = (datetime.datetime.utcnow()- ep).total_seconds()
        if not values:
            return []
        if spacing >= 31622400:  #366days
            fmt = "%Y"
        elif spacing >= 2678400: #31days
            fmt = "%Y %b"
        elif spacing >= 86400:   #1day
            fmt = "%b/%d"
        elif spacing >= 3600:    #1h
            fmt = "%b/%d-%Hh"
        elif spacing >= 60:      #1m
            fmt = "%H:%M"
        elif spacing >= 1:       #1s
            fmt = "%H:%M:%S"
        else: # less than 2s (show microseconds)
            #fmt = "%S.%f"""
            fmt = '[+%fms]'  # explicitly relative to last second
        for x in values:
            try:
                ret.append(time.strftime(fmt, time.localtime(x*.1+tonow))) #time.localtime(x*.1+tonow)
            except ValueError:  # Windows can't handle dates before 1970
                ret.append('')
            except:
                ret.append('')
        return ret
'''
###############################################################################
#  Provides a date-time aware axis
###############################################################################
class DateAxis(AxisItem):
    def __init__(self, *args, **kwargs):
        super(DateAxis, self).__init__(*args, **kwargs)

    def tickStrings(self, values, scale, spacing):
        TS_MULT_us = 1e6
        try:
            z= [(datetime.datetime.utcfromtimestamp(float(value)/TS_MULT_us)).strftime("%b-%d %H:%M:%S") for value in values]
        except:
            z= ''
        return z
        #return [(datetime.datetime.utcfromtimestamp(float(value)/TS_MULT_us)).strftime("%b-%d %H:%M:%S") for value in values]

###############################################################################
#  Provides a non scientific axis notation
###############################################################################
class NonScientificAxis(AxisItem):
    def __init__(self, *args, **kwargs):
        super(NonScientificAxis, self).__init__(*args, **kwargs)

    def tickStrings(self, values, scale, spacing):
        return [int(value*1) for value in values]


from cs1robots import *

load_world("worlds/rain2.wld")

hubo = Robot(beepers=10, avenue=3, street=6, orientation="E")
hubo.set_trace("red")

def turn_right():
    for i in range(3):
        hubo.turn_left()

hubo.turn_left()
hubo.move()

while (True):

    if (hubo.front_is_clear() and hubo.left_is_clear()):
        hubo.drop_beeper()
        hubo.move()

    elif (hubo.front_is_clear()):
        hubo.move()

    elif (not hubo.front_is_clear() and not hubo.left_is_clear()):
        turn_right()
        if (hubo.facing_north() and not hubo.left_is_clear()):
            hubo.move()
            hubo.turn_left()

    if (hubo._x == 3 and hubo._y == 5):
        turn_right()
        hubo.move()
        hubo.turn_left()
        break


from datetime import datetime

from pydantic import BaseModel, Field, field_validator

class ContactPersonCreateSchema(BaseModel):
    fio: str
    phone: str = Field(max_length=10, min_length=10)

    @field_validator("fio")
    @classmethod
    def validate_fio(cls, value: str):
        try:
            surname, name, father_name = value.split()
        except IndexError:
            raise ValueError("Incorrect fio format!")
        else:
            return value

class ContactPersonSchema(ContactPersonCreateSchema):
    id: str

class ContactPersonUpdateSchema(ContactPersonSchema):
    fio: str | None = None
    phone: str | None = None

    @field_validator("phone")
    @classmethod
    def validate_phone(cls, value: str):
        if len(value) != 10:
            raise ValueError("Phone number must be length of 10!")
        return value

class WatchListSchema(BaseModel):
    id: str
    address: str
    time_start: datetime
    time_finished: datetime | None = None
    is_watched: bool
    is_finished: bool
    is_refused: bool

class WatchSchema(BaseModel):
    id: str
    address: str
    time_start: datetime
    time_finished: datetime | None = None
    is_watched: bool
    is_finished: bool
    is_refused: bool
    refuse_purpose: str | None = None
    note: str
    contact_persons: list[ContactPersonSchema] = Field(default_factory=list)

class WatchCreateSchema(BaseModel):
    address: str
    time_start: datetime
    time_finished: datetime | None = None

class WatchUpdateSchema(BaseModel):
    address: str | None = None
    time_start: datetime | None = None
    time_finished: datetime | None = None
    note: str | None = None

class WatchRefuseSchema(BaseModel):
    refuse_purpose: str

class WatchFinishSchema(BaseModel):
    finish_comment: str


# "pip install keras scikit-learn" if you have not already done so
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.datasets import boston_housing
from sklearn.metrics import mean_absolute_error

# load boston housing data. Uses 80%/20% train/test split by default.
(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

# create our neural network. Will have two hidden layers of 64 units each
# and an output layer with one neuron because we are predicting a single
# scalar value. Use relu activation functions.
model = Sequential()
# add a fully-connected layer with 64 units
model.add(Dense(64, input_shape=(13,)))
# apply a relu activation function to "bend" the linear output of
# the fully connected layer
model.add(Activation('relu'))
# repeat...
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))

# use RMSProp for optimization and Mean Squared Error (MSE) as loss function.
model.compile(optimizer='rmsprop', loss='mse')
model.summary()

print('[*] training model...')
model.fit(x_train, y_train, epochs=150, verbose=2)

print('[*] predicting from test set...')
y_hat = model.predict(x_test)

for i in range(len(y_hat)):
	print ('[+] predicted: {:.1f}    real: {}     error: {:.1f}'\
		   .format(y_hat[i][0], y_test[i], abs(y_hat[i][0] - y_test[i])))

print('[+] the mean absolute error is {:.1f}'.format(mean_absolute_error(y_hat, y_test)))


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

import os
import re
import shutil
import typing

from PIL import Image

from preview_generator.manager import PreviewManager
from tests import test_utils

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
CACHE_DIR = "/tmp/preview-generator-tests/cache"
IMAGE_FILE_PATH = os.path.join(CURRENT_DIR, "test_sketch.sketch")

def setup_function(function: typing.Callable) -> None:
    shutil.rmtree(CACHE_DIR, ignore_errors=True)

def test_sketch_to_jpeg() -> None:
    manager = PreviewManager(cache_folder_path=CACHE_DIR, create_folder=True)
    assert manager.has_jpeg_preview(file_path=IMAGE_FILE_PATH) is True
    path_to_file = manager.get_jpeg_preview(
        file_path=IMAGE_FILE_PATH, height=256, width=512, force=True
    )

    assert os.path.exists(path_to_file) is True
    assert os.path.getsize(path_to_file) > 0
    assert re.match(test_utils.CACHE_FILE_PATH_PATTERN__JPEG, path_to_file)

    with Image.open(path_to_file) as jpeg:
        assert jpeg.height == 256
        assert jpeg.width == 482


import boto3
import json

class SqsManager:
    def __init__(self):
        self.sqs = boto3.client(
            service_name='sqs',
            region_name='eu-central-1'
        )

    def create_sqs_schedule_notifications_event(self, message: dict) -> None:
        self.sqs.send_message(
            QueueUrl='https://sqs.eu-central-1.amazonaws.com/007108578073/schedule-notifications-queue.fifo',
            MessageAttributes=message,
            MessageBody=json.dumps(message),
            MessageGroupId=(
                'CreateScheduleNotificationsEvent'
            )
        )


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.core.validators

class Migration(migrations.Migration):

    dependencies = [
        ('database', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='databaseentry',
            name='related_incidents',
            field=models.ManyToManyField(to='database.DatabaseEntry', blank=True),
        ),
        migrations.AddField(
            model_name='databaseentry',
            name='reliability_score',
            field=models.IntegerField(default=1, validators=[django.core.validators.MaxValueValidator(100), django.core.validators.MinValueValidator(1)]),
        ),
    ]


from django.contrib.auth import get_user_model
from django.test import TestCase, Client
from django.urls import reverse
from http import HTTPStatus
import unittest

from ..models import Group, Post

User = get_user_model()

class PostsURLTests(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        user = User.objects.create_user(username='user')
        cls.group = Group.objects.create(
            title='Test case',
            slug='test-slug',
        )
        cls.post = Post.objects.create(
            text='Create new post',
            group=cls.group,
            author=user,
        )

    def setUp(self):
        self.guest_client = Client()
        self.user = User.objects.create_user(username='Test_user')
        self.authorized_client = Client()
        self.authorized_client.force_login(self.user)
        self.author_client = Client()
        self.author_client.force_login(self.post.author)

        self.templates_urls = {
            'index.html': reverse('index'),
            'group.html': reverse(
                'group', kwargs={'slug': PostsURLTests.group.slug}
            ),
            'new_post.html': reverse('new_post'),
            'profile.html': reverse(
                'profile', kwargs={'username': PostsURLTests.post.author}
            ),
            'post.html': reverse(
                'post', kwargs={
                    'username': PostsURLTests.post.author,
                    'post_id': PostsURLTests.post.id,
                })
        }

    def test_status_web_pages_authorized(self):
        for page in self.templates_urls.values():
            with self.subTest(page=page):
                response = self.authorized_client.get(page)
                self.assertEqual(response.status_code, HTTPStatus.OK)

    @unittest.expectedFailure
    def test_status_web_pages_unauthorized(self):
        for page in self.templates_urls.values():
            with self.subTest(page=page):
                response = self.guest_client.get(page)
                self.assertEqual(response.status_code, HTTPStatus.OK)

    def test_urls_uses_correct_template(self):
        for template, url in self.templates_urls.items():
            with self.subTest(template=template):
                response = self.authorized_client.get(url)
                self.assertTemplateUsed(response, template)

    def test_status_post_edit_page_unauthorized(self):
        response = self.guest_client.get(reverse('post_edit', kwargs={
            'username': PostsURLTests.post.author,
            'post_id': PostsURLTests.post.id}))
        self.assertRedirects(response, reverse('post', kwargs={
            'username': PostsURLTests.post.author,
            'post_id': PostsURLTests.post.id}))

    def test_status_post_edit_page_authorized(self):
        response = self.authorized_client.get(reverse('post_edit', kwargs={
            'username': PostsURLTests.post.author,
            'post_id': PostsURLTests.post.id}))
        self.assertRedirects(response, reverse('post', kwargs={
            'username': PostsURLTests.post.author,
            'post_id': PostsURLTests.post.id}))

    def test_status_post_edit_page_author(self):
        response = self.author_client.get(reverse('post_edit', kwargs={
            'username': PostsURLTests.post.author,
            'post_id': PostsURLTests.post.id}))
        self.assertEqual(response.status_code, HTTPStatus.OK)


import time

from django.conf import settings

from dashboard.utils import get_web3
from gas.utils import recommend_min_gas_price_to_confirm_in_time
from kudos.utils import kudos_abi
from web3 import Web3

#setup
w3 = get_web3('mainnet')
kudos_contract_address = Web3.toChecksumAddress(settings.KUDOS_CONTRACT_MAINNET)
kudos_owner_address = Web3.toChecksumAddress(settings.KUDOS_OWNER_ACCOUNT)
nonce = w3.eth.getTransactionCount(kudos_owner_address)

# pk range to re-send
min_pk = 1684
max_pk = 1740
TIME_SLEEP = 1

for kt in KudosTransfer.objects.filter(pk__gte=min_pk, pk__lte=max_pk):

    token_id = kt.kudos_token_cloned_from.token_id
    address = kt.receive_address
    price_finney = kt.kudos_token_cloned_from.price_finney

    contract = w3.eth.contract(Web3.toChecksumAddress(kudos_contract_address), abi=kudos_abi())
    tx = contract.functions.clone(address, token_id, 1).buildTransaction({
        'nonce': nonce,
        'gas': 500000,
        'gasPrice': int(recommend_min_gas_price_to_confirm_in_time(2) * 10**9),
        'value': int(price_finney / 1000.0 * 10**18),
    })

    signed = w3.eth.account.signTransaction(tx, settings.KUDOS_PRIVATE_KEY)
    time.sleep(TIME_SLEEP)

    try:
        txid = w3.eth.sendRawTransaction(signed.rawTransaction).hex()
        nonce += 1
        print(f'sent tx {nonce}')
        kt.txid = txid
        kt.save()

    except Exception as e:
        print(e)
        error = "Could not redeem your kudos.  Please try again soon."


import unittest
import random

from src.modelo.Competidor import Competidor
from src.modelo.Carrera import Carrera
from src.logica.Logica_mock import Logica_mock
from src.modelo.declarative_base import Session
from faker import Faker

class CompetidorTestCase(unittest.TestCase):

        def setUp(self):

                self.logica = Logica_mock()
                self.session = Session()
                self.data_factory = Faker()
                Faker.seed(1000)

                self.data = []
                self.datacar = []
                self.competidores = []
                self.carreras = []

                for j in range(0,4):
                        self.datacar.append((self.data_factory.name(),True))
                        self.carreras.append(Carrera(nombre = self.datacar[-1][0],abierta =True))
                        self.session.add(self.carreras[-1])
                self.session.add(self.carreras[-1])

                for i in range(0,10):
                        self.data.append((self.data_factory.unique.name(),
                        self.data_factory.pyfloat(0,1,positive=True),
                        self.data_factory.random_int(1,3)))
                        self.competidores.append(
                                Competidor(
                                        nombre = self.data[-1][0],probabilidad=self.data[-1][1],cuota=self.data[-1][1]/(1-self.data[-1][1]),ganador=False,id_carrera=self.data[-1][2]))
                        self.session.add(self.competidores[-1])

                self.session.commit()

        def test_constructor(self):
                for competidores, dato in zip(self.competidores, self.data):
                        self.assertEqual(competidores.nombre, dato[0])
                        self.assertEqual(competidores.probabilidad, dato[1])
                        self.assertEqual(competidores.id_carrera, dato[2])

        def tearDown(self):
                self.session = Session()
                busqueda = self.session.query(Competidor).all()
                busquedaCar = self.session.query(Carrera).all()
                for competidor in busqueda:
                        self.session.delete(competidor)
                for carrera in busquedaCar:
                        self.session.delete(carrera)
                self.session.commit()
                self.session.close()

        def test_aniadir_competidor(self):
                self.data.append((self.data_factory.unique.name(), self.data_factory.pyfloat(0,1,positive=True), self.data_factory.random_int(1,3)))
                carrera = self.session.query(Carrera).filter(Carrera.id == self.data[-1][2]).first()
                self.assertEqual(self.logica.aniadir_competidor(self.data[-1][2],self.data[-1][0],self.data[-1][1],carrera.nombre),True)

        def test_aniadir_competidor_repetida(self):
                self.data.append((self.data_factory.unique.name(), self.data_factory.pyfloat(0,1,positive=True), self.data_factory.random_int(1,3)))
                carrera = self.session.query(Carrera).filter(Carrera.id == self.data[-1][2]).first()
                self.logica.aniadir_competidor(self.data[-1][2],self.data[-1][0],self.data[-1][1],carrera.nombre)
                resultado=self.logica.aniadir_competidor(self.data[-1][2],self.data[-1][0],self.data[-1][1],carrera.nombre)
                self.assertNotEqual(resultado, True)

        def test_verificar_almacenamiento_aniadir_competidor(self):
                self.data.append((self.data_factory.unique.name(), self.data_factory.pyfloat(0,1,positive=True), self.data_factory.random_int(1,3)))
                carrera = self.session.query(Carrera).filter(Carrera.id == self.data[-1][2]).first()
                self.logica.aniadir_competidor(self.data[-1][2],self.data[-1][0],self.data[-1][1],carrera.nombre)
                self.session = Session()
                competidor = self.session.query(Competidor).filter(Competidor.nombre == self.data[-1][0]).first()
                self.assertEqual(competidor.nombre, self.data[-1][0])
                self.assertEqual(competidor.probabilidad, self.data[-1][1])


from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
import numpy as np

loaded_model = load_model('./static/models/model_wildanimal.h5')    # load the previouly saved model

def predict_result(full_path):
    img = image.load_img(full_path, target_size = (200, 200, 3))
    img = np.expand_dims(img, axis=0)
    img = img.astype('float')/255

    result = loaded_model.predict(img)
    result = result.item()

    if result > 0.5:
        return round(result*100, 2), 'Wild'
    else:
        return round((1-result)*100, 2), 'No Wild'


from numpy import dtype
import tensorflow as tf
tf.set_random_seed(66)

#1. 데이터
x_data = [[1,2], [2,3], [3,1], [4,3], [5,3], [6,2]]    # (6,2)
y_data = [[0],[0],[0],[1],[1],[1]]                     # (6,1)

x = tf.placeholder(tf.float32, shape=[None, 2])
y = tf.placeholder(tf.float32, shape=[None, 1])
w = tf.compat.v1.Variable(tf.random.normal([2,1]), name = 'weight1')   # (2,1)
b = tf.compat.v1.Variable(tf.random.normal([1]), name = 'bias')

#2. 모델구성
# hypothesis = x * w + b
# hypothesis = tf.matmul(x, w) + b
# hypothesis = tf.sigmoid(hypothesis)
hypothesis = tf.sigmoid(tf.matmul(x, w) + b)
# model.add(Dense(1, activation='sigmoid'))

#3-1. 컴파일
# loss = tf.reduce_mean(tf.square(hypothesis - y))    # mse
loss = -tf.reduce_mean(y*tf.log(hypothesis)+(1-y)*tf.log(1-hypothesis))
# binary_crossentropy

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.04)
train = optimizer.minimize(loss)

#3-2. 훈련
sess = tf.compat.v1.Session()
sess.run(tf.compat.v1.global_variables_initializer())

for epoch in range(2001):
    loss_val, hypothesis_val, _ = sess.run([loss, hypothesis, train], feed_dict={x:x_data, y:y_data})
    if epoch % 200 == 0:
        print(epoch, 'loss : ', loss_val, '\n', hypothesis_val)

#4. 평가, 예측
y_predict = tf.cast(hypothesis > 0.5, dtype=tf.int32)  # tf.cast('조건') 함수 : True이면 1, False이면 0을 출력한다. 텐서를 새로운 형태로 캐스팅하는데 사용한다.부동소수점형에서 정수형으로 바꾼 경우 소수점 버린을 한다.
# print(y_predict)   # Tensor("Cast:0", shape=(?, 1), dtype=float32)
# print(sess.run(hypothesis > 0.5, feed_dict={x:x_data, y:y_data}))

accuracy = tf.reduce_mean(tf.cast(tf.equal(y, y_predict), dtype=tf.float32))

pred, acc = sess.run([y_predict, accuracy], feed_dict={x:x_data, y:y_data})

print("===============================================================================")
print("예측값 : \n", hypothesis_val)
print("예측결과 : ", pred)
print("accuracy : ", acc)

sess.close()

# ===============================================================================
# 예측값 :
#  [[0.04392562]
#  [0.17360151]
#  [0.36277783]
#  [0.7560311 ]
#  [0.92249364]
#  [0.9746191 ]]
# 예측결과 :  [[0.]
#  [0.]
#  [0.]
#  [1.]
#  [1.]
#  [1.]]
# accuracy :  1.0

# predict = tf.matmul(x, w_val) + b   # predict = model.predict

# y_predict = sess.run(predict, feed_dict={x:x_data, y:y_data})
# print("예측 : " , y_predict)

# sess.close()

# from sklearn.metrics import r2_score, mean_absolute_error
# r2 = r2_score(y_data, y_predict)
# print('r2스코어 : ', r2)

# mae = mean_absolute_error(y_data, y_predict)
# print('mae : ', mae)


import logging
from typing import Any, Dict, Tuple

import numpy as np
from scipy.sparse import csr_matrix

import cvxpy.settings as s
from cvxpy import Zero
from cvxpy.constraints import NonNeg
from cvxpy.reductions.dcp2cone.cone_matrix_stuffing import ParamConeProg
from cvxpy.reductions.solution import Solution, failure_solution
from cvxpy.reductions.solvers import utilities
from cvxpy.reductions.solvers.conic_solvers.conic_solver import ConicSolver
from cvxpy.utilities.versioning import Version

log = logging.getLogger(__name__)

class PDLP(ConicSolver):
    """An interface to PDLP via OR-Tools."""

    SUPPORTED_CONSTRAINTS = ConicSolver.SUPPORTED_CONSTRAINTS

    # The key that maps to the pdlp.QuadraticProgram in the data returned by
    # apply().
    PDLP_MODEL = "pdlp_model"

    def name(self) -> str:
        """The name of the solver."""
        return 'PDLP'

    def import_solver(self) -> None:
        """Imports the solver."""
        import ortools  # noqa F401
        if Version(ortools.__version__) < Version('9.7.0'):
            raise RuntimeError(f'Version of ortools ({ortools.__version__}) '
                               f'is too old. Expected >= 9.7.0.')
        if Version(ortools.__version__) >= Version('9.8.0'):
            raise RuntimeError('Unrecognized new version of ortools '
                               f'({ortools.__version__}). Expected < 9.8.0. '
                               'Please open a feature request on cvxpy to '
                               'enable support for this version.')

    def apply(self, problem: ParamConeProg) -> Tuple[Dict, Dict]:
        """Returns a new problem and data for inverting the new solution."""
        from ortools.pdlp.python import pdlp

        # Create data and inv_data objects
        data = {}
        inv_data = {self.VAR_ID: problem.x.id}
        if not problem.formatted:
            problem = self.format_constraints(problem, None)
        data[s.PARAM_PROB] = problem
        data[self.DIMS] = problem.cone_dims

        constr_map = problem.constr_map
        inv_data["constraints"] = constr_map[Zero] + constr_map[NonNeg]

        # Min c'x + d such that Ax + b = s, s \in cones.
        c, d, A, b = problem.apply_parameters()
        A = csr_matrix(A)
        data["num_constraints"], data["num_vars"] = A.shape

        model = pdlp.QuadraticProgram()
        model.objective_offset = d.item() if isinstance(d, np.ndarray) else d
        model.objective_vector = c
        model.variable_lower_bounds = np.full_like(c, -np.inf)
        model.variable_upper_bounds = np.full_like(c, np.inf)

        model.constraint_matrix = A
        constraint_lower_bounds = np.full_like(b, -np.inf)
        constraint_upper_bounds = np.full_like(b, np.inf)
        # Ax + b = 0
        num_eq = problem.cone_dims.zero
        constraint_lower_bounds[:num_eq] = -b[:num_eq]
        constraint_upper_bounds[:num_eq] = -b[:num_eq]
        # Ax + b >= 0
        constraint_lower_bounds[num_eq:] = -b[num_eq:]

        model.constraint_lower_bounds = constraint_lower_bounds
        model.constraint_upper_bounds = constraint_upper_bounds

        data[self.PDLP_MODEL] = model
        return data, inv_data

    def invert(self, solution: Dict[str, Any],
               inverse_data: Dict[str, Any]) -> Solution:
        """Returns the solution to the original problem."""
        status = solution["status"]

        if status in s.SOLUTION_PRESENT:
            primal_vars = {
                inverse_data[self.VAR_ID]: solution["primal"]
            }
            dual_vars = utilities.get_dual_values(
                result_vec=solution["dual"],
                parse_func=utilities.extract_dual_value,
                constraints=inverse_data["constraints"],
            )
            return Solution(status, solution["value"], primal_vars, dual_vars,
                            {})
        else:
            return failure_solution(status)

    def solve_via_data(
            self,
            data: Dict[str, Any],
            warm_start: bool,
            verbose: bool,
            solver_opts: Dict[str, Any],
            solver_cache: Dict = None,
    ) -> Solution:
        """Returns the result of the call to the solver."""
        from ortools.pdlp import solvers_pb2
        from ortools.pdlp.python import pdlp

        parameters = solvers_pb2.PrimalDualHybridGradientParams()
        parameters.verbosity_level = 3 if verbose else 0
        # CVXPY reductions can leave a messy problem (e.g., no variable
        # bounds), so we turn on presolving by default.
        parameters.presolve_options.use_glop = True
        if "parameters_proto" in solver_opts:
            proto = solver_opts["parameters_proto"]
            if not isinstance(proto, solvers_pb2.PrimalDualHybridGradientParams):
                log.error("parameters_proto must be a PrimalDualHybridGradientParams")
                return {"status": s.SOLVER_ERROR}
            parameters.MergeFrom(proto)
        if "time_limit_sec" in solver_opts:
            limit = float(solver_opts["time_limit_sec"])
            parameters.termination_criteria.time_sec_limit = limit

        result = pdlp.primal_dual_hybrid_gradient(data[self.PDLP_MODEL],
                                                  parameters)
        solution = {}
        solution["primal"] = result.primal_solution
        solution["dual"] = result.dual_solution
        solution["status"] = self._status_map(result.solve_log)

        convergence_info = self._get_convergence_info(
            result.solve_log.solution_stats,
            result.solve_log.solution_type
        )
        if convergence_info is not None:
            solution["value"] = convergence_info.primal_objective
        else:
            solution["value"] = -np.inf

        return solution

    @staticmethod
    def _get_convergence_info(stats, candidate_type):
        for convergence_info in stats.convergence_information:
            if convergence_info.candidate_type == candidate_type:
                return convergence_info
        return None

    def _status_map(self, solve_log):
        from ortools.pdlp import solve_log_pb2
        TerminationReason = solve_log_pb2.TerminationReason
        status = solve_log.termination_reason
        if status == TerminationReason.TERMINATION_REASON_OPTIMAL:
            return s.OPTIMAL
        elif status == TerminationReason.TERMINATION_REASON_PRIMAL_INFEASIBLE:
            return s.INFEASIBLE
        elif status == TerminationReason.TERMINATION_REASON_DUAL_INFEASIBLE:
            # Not technically correct, but this seems to be the convention.
            return s.UNBOUNDED
        elif status == TerminationReason.TERMINATION_REASON_TIME_LIMIT:
            return s.USER_LIMIT
        elif status == TerminationReason.TERMINATION_REASON_ITERATION_LIMIT:
            return s.USER_LIMIT
        elif status == TerminationReason.TERMINATION_REASON_KKT_MATRIX_PASS_LIMIT:
            return s.USER_LIMIT
        elif status == TerminationReason.TERMINATION_REASON_NUMERICAL_ERROR:
            log.warning('PDLP reported a numerical error.')
            return s.USER_LIMIT
        elif status == TerminationReason.TERMINATION_REASON_INVALID_PROBLEM:
            log.error('Invalid problem: %s', solve_log.termination_string)
            return s.SOLVER_ERROR
        elif status == TerminationReason.TERMINATION_REASON_INVALID_PARAMETER:
            log.error('Invalid parameter: %s', solve_log.termination_string)
            return s.SOLVER_ERROR
        elif status == TerminationReason.TERMINATION_REASON_PRIMAL_OR_DUAL_INFEASIBLE:
            return s.INFEASIBLE_OR_UNBOUNDED
        else:
            log.error("Unexpected status: %s Message: %s",
                      TerminationReason.Name(status),
                      solve_log.termination_string)
            return s.SOLVER_ERROR


from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores.faiss import FAISS
import os,argparse
from dotenv import load_dotenv
from os import getenv
from util import get_answer

load_dotenv()
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')

def main():

    """
    The main function that runs the program.

    This function takes a query from the command line arguments, uses OpenAI's GPT-3 language model to search for similar
    text in a pre-indexed dataset, and generates an answer based on the search results.

    Parameters: None
    Returns: None
    """
    # Parse command line arguments
    parser = argparse.ArgumentParser(description='Process arguments.')
    parser.add_argument('query', type=str, help='The query to search for.')
    args = parser.parse_args()
    query = args.query

    # Initialize OpenAIEmbeddings object with API key and chunk size
    embed = OpenAIEmbeddings(openai_api_key=os.getenv('OPENAI_API_KEY'), chunk_size=1000)

    # Load pre-indexed dataset using FAISS
    index = FAISS.load_local('index v2', embed)

    # Use FAISS to search for similar text in the dataset
    search = index.similarity_search(query, k=5)

    # Generate a prompt for the GPT-3 language model to use for generating an answer
    prompt = f"The text below is a group conversation in English language and some part is in pidgin english. From the text answer this question: \
    {query}: Based on the text, what are some of the mentions in the conversations, \
    make a list of the answers and you must provide links if they appear in the conversation where necessary.\
    Ignore adding source to the answer"
    answer = get_answer(search,prompt)
    print(answer['output_text'])
    return answer['output_text']

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

import codecs
import io
import logging
import re

from contextlib import contextmanager

from flask import send_file
from gridfs import GridFS
from pymongo import MongoClient

from . import BaseBackend

log = logging.getLogger(__name__)

class GridFsBackend(BaseBackend):
    '''
    A Mongo GridFS backend

    Expect the following settings:

    - `mongo_url`: The Mongo access URL
    - `mongo_db`: The database to store the file in.
    '''
    def __init__(self, name, config):
        super(GridFsBackend, self).__init__(name, config)

        self.client = MongoClient(config.mongo_url)
        self.db = self.client[config.mongo_db]
        self.fs = GridFS(self.db, self.name)

    def exists(self, filename):
        return self.fs.exists(filename=filename)

    @contextmanager
    def open(self, filename, mode='r', encoding='utf8'):
        if 'r' in mode:
            f = self.fs.get_last_version(filename)
            yield f if 'b' in mode else codecs.getreader(encoding)(f)
        else:  # mode == 'w'
            f = io.BytesIO() if 'b' in mode else io.StringIO()
            yield f
            params = {'filename': filename}
            if 'b' not in mode:
                params['encoding'] = encoding
            self.fs.put(f.getvalue(), **params)

    def read(self, filename):
        f = self.fs.get_last_version(filename)
        return f.read()

    def write(self, filename, content):
        kwargs = {
            'filename': filename
        }

        if hasattr(content, 'content_type') and content.content_type is not None:
            kwargs['content_type'] = content.content_type

        return self.fs.put(self.as_binary(content), **kwargs)

    def delete(self, filename):
        regex = '^{0}'.format(re.escape(filename))
        for version in self.fs.find({'filename': {'$regex': regex}}):
            self.fs.delete(version._id)

    def copy(self, filename, target):
        src = self.fs.get_last_version(filename)
        self.fs.put(src, filename=target, content_type=src.content_type)

    def list_files(self):
        for f in self.fs.list():
            yield f

    def serve(self, filename):
        file = self.fs.get_last_version(filename)
        return send_file(file, mimetype=file.content_type)

    def get_metadata(self, filename):
        f = self.fs.get_last_version(filename)
        return {
            'checksum': 'md5:{0}'.format(f.md5),
            'size': f.length,
            'mime': f.content_type,
            'modified': f.upload_date,
        }


from django import forms
from django.db.models import Q, Max, Count
from allauth.account.models import EmailAddress
from .base_helper import get_current_client
from .forms import (LoginForm, FullSearchForm, GoodLuckSearchForm, SignupForm,
                    ChangePasswordForm, UploadFilesForm, ResetPasswordKeyForm)
from .models.client import Client
from .models.catalog import Feeling
from .models.notification import Notification
from .models.service import SubService
from .models.chat import Room, Message
from .models.purchase import Purchase
from .models.inbox import Inbox

def signup_form_processor(request):
    context = {
        'signup_form': SignupForm()
    }
    return context

def login_form_processor(request):
    context = {
        'login_form': LoginForm()
    }
    return context

def goodluck_form_processor(request):
    context = {
        'goodluck_form': GoodLuckSearchForm()
    }
    return context

def changepass_form_processor(request):
    context = {
        'changepass_form': ChangePasswordForm()
    }
    return context

def changepasskey_form_processor(request):
    context = {
        'changepasskey_form': ResetPasswordKeyForm()
    }
    return context

def user_verified_processor(request):
    context = {
        'verified': False
    }
    if not request.user.is_anonymous():
        if EmailAddress.objects.filter(
                user=request.user,
                verified=True).exists():
            context['verified'] = True

    return context

def search_form_processor(request):
    context = {
        'search_form': FullSearchForm()
    }
    return context

def feeling_choices_processor(request):
    context = {
        'feeling_choices': Feeling.objects.filter(feeactive=True),
        'feel': 'heart-icon.png',
    }

    if Client.objects.filter(clicode=request.user.pk).exists():
        client = Client.objects.get(clicode=request.user.pk)
        if client.feecode:
            context['feel'] = str(client.feecode.feeiconfile)

    return context

def profile_menu_processor(request):
    context = {}
    if Client.objects.filter(clicode=request.user.pk).exists():
        current_client = Client.objects.get(clicode=request.user.pk)
        context['client'] = current_client
        context['username'] = current_client.cliusername
        context['name'] = current_client.cliname
        context['city'] = current_client.citcode
        context['marital_status'] = current_client.marcode
        context['height'] = current_client.heicode
        context['profile_picture'] = current_client.profile_picture
        context['profile_percentage'] = profile_percentage_processor(request)
        context['upload_profile_menu'] = UploadFilesForm()

    return context

def notifications_processor(request):
    notifications = Notification.objects.filter(
        clicoderecieved=get_current_client(request),
        notread=False).order_by('-notcode')
    quan = len(notifications)
    notifications = Notification.objects.filter(
        clicoderecieved=get_current_client(request)).order_by('-notcode')[:30]
    context = {
        'notifications': notifications,
        'quantity_ntf': quan
    }
    return context

def profile_percentage_processor(request):
    percentage = 0
    if Client.objects.filter(clicode=request.user.pk).exists():
        client = Client.objects.get(clicode=request.user.pk)
        if client.cliname:
            percentage += 1
        if client.clidescription:
            percentage += 1
        if client.clibirthdate:
            percentage += 1
        if client.inccode:
            percentage += 1
        if client.gencode:
            percentage += 1
        if client.citcode:
            percentage += 1
        if client.marcode:
            percentage += 1
        if client.educode:
            percentage += 1
        if client.lancodefirst:
            percentage += 1
        if client.lancodesecond:
            percentage += 1
        if client.langlevel:
            percentage += 1
        if client.heicode:
            percentage += 1
        if client.weicode:
            percentage += 1
        if client.ocucode:
            percentage += 1
        if client.ethcode:
            percentage += 1
        if client.bodycode:
            percentage += 1
        if client.eyecode:
            percentage += 1
        if client.haicode:
            percentage += 1
        if client.hlecode:
            percentage += 1
        if client.frecodesmoke:
            percentage += 1
        if client.frecodedrink:
            percentage += 1
        if client.zodcode:
            percentage += 1
        if client.relcode:
            percentage += 1
        if client.chicode:
            percentage += 1

        return int(percentage * 100) / 24
    return percentage

def my_chats_processor(request):
    context = {}

    if Client.objects.filter(clicode=request.user.pk).exists():
        context['is_client'] = True
        result = Room.getChatsRequest(request.user.pk)
        if len(result) > 0:
            if "mychats" in result:
                context['mychats'] = result["mychats"]
            if (("myrequest" in result) & (len(result["myrequest"]) > 0)):
                context['myrequest'] = result["myrequest"]
            else:
                context['mysuggestions'] = Client.suggestions(
                                                        request.user.pk, 4)
    return context

def upload_files_processor(request):
    context = {
        'upload_files_form': UploadFilesForm()
    }
    return context

def quantity_profile_processor(request):
    try:
        # Obtenemos la cantidad de creditos e inbox nuevos del cliente
        client = Client.objects.get(clicode=request.user.pk)
        context = {
            'new_inbox': Inbox.quantityNotRead(client.clicode)
        }
    except Client.DoesNotExist:
        context = {
            'new_inbox': 0
        }
    return context


#!/usr/bin/env python
import argparse
import sys
import time
from bcc import BPF

kstime = time.time_ns() - time.monotonic_ns()

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--pid", default=None)
args = parser.parse_args()

bpf_text = """
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>

struct event {
        int pad;
        int size;
        u64 timestamp_ns;
};
BPF_RINGBUF_OUTPUT(events, 1 << 12);

int kretprobe__tcp_sendmsg(struct pt_regs *ctx)
{
    u64 id = bpf_get_current_pid_tgid();
    if (id >> 32 != __PID__) { return 0; }
    int size = PT_REGS_RC(ctx);
    u64 time = bpf_ktime_get_ns();
    struct event event = {
        .size = size,
        .timestamp_ns = time
    };
    events.ringbuf_output(&event, sizeof(event), 0);
    return 0;
}
"""

if args.pid is None:
    print("PID must set")
    exit(0)

bpf_text = bpf_text.replace("__PID__", args.pid)

bpf = BPF(text=bpf_text)

def callback(ctx, data, size):
    event = bpf['events'].event(data)
    print("%d,%d" % (event.timestamp_ns + kstime, event.size))

bpf['events'].open_ring_buffer(callback)

while True:
    try:
        bpf.ring_buffer_consume()
    except KeyboardInterrupt:
        exit()
    sys.stdout.flush()


import quandl
from omar_utils.data.files import File  # https://github.com/Omega97/omar_utils

def load_data(path, name):
    try:
        # try to load data locally
        return File(path=path)()
    except FileNotFoundError:
        # download data from web
        df = quandl.get(name)
        f = File()
        f.load_var(df)
        f.save(path)
        return df

if __name__ == '__main__':

    # load data
    Df = load_data(path='datasets/GOOGL', name='WIKI/GOOGL')
    print(Df)

    # clean data
    print(Df['Open'])


class Solution:
    def equalFrequency(self, word: str) -> bool:

        dic = Counter(word)
        value_dic = Counter(dic.values())
        max_value = max(dic.values())
        min_value = min(dic.values())

        if max_value == 1 or len(dic.keys()) == 1:
            return(True)
        if len(set(value_dic.keys())) != 2:
            return (False)
        if min_value == 1 and value_dic[min_value] == 1:
            return(True)
        if value_dic[max_value] != 1:
            return(False)
        if max_value-1 not in value_dic.keys():
            return(False)
        return(True)

#Time: O(N)
#Space: O(1)


import logging
import shutil
import time
from typing import Optional

import requests
from colorlog import ColoredFormatter
from urllib3.exceptions import ReadTimeoutError

LOGGER = logging.getLogger("MAIN")
# formatter = ColoredFormatter("  %(log_color)s%(levelname)s:%(reset)s %(message)s")
# stream = logging.StreamHandler()
# stream.setFormatter(formatter)
# LOGGER.addHandler(stream)
LOGGER.setLevel(logging.INFO)

def download_file(url: str, local_filename: str, timeout: Optional[int] = 5) -> int:
    read_counter = 0
    break_read_error_loop = False

    while (not break_read_error_loop) and (read_counter < 5):
        count = 0
        break_connection_error_loop = False
        while (not break_connection_error_loop) and (count < 5):
            try:
                resp = requests.get(url, stream=True, timeout=timeout)
                status_code = resp.status_code
            except requests.exceptions.ReadTimeout:
                LOGGER.warn("ReadTimeout from cmip6_utils.file.download_file")
                status_code = 408
            except requests.exceptions.ConnectTimeout:
                LOGGER.warn("ConnectTimeout from cmip6_utils.file.download_file")
                status_code = 408
            except requests.exceptions.ConnectionError:
                LOGGER.warn("ConnectionError from cmip6_utils.file.download_file")
                status_code = 491
            except Exception:
                LOGGER.warn("Unknown error from cmip6_utils.file.download_file")
                status_code = 499

            if status_code != 200:
                count += 1
                LOGGER.warn(f"Failed opening URL.... Retrying {count}")
                time.sleep(timeout)
            else:
                break_connection_error_loop = True

        if count == 5:
            break_read_error_loop = True

        if status_code == 200:
            try:
                with open(local_filename, "wb") as f:
                    shutil.copyfileobj(resp.raw, f)
                break_read_error_loop = True
            except ReadTimeoutError:
                read_counter += 1
                LOGGER.warn(f"Reading from stream failed.... Retrying {read_counter}")
                status_code = -1
                time.sleep(timeout)

    return status_code


import tempfile
import shutil

from nose.plugins.attrib import attr
from w3af.plugins.attack.payloads.payloads.tests.payload_test_helper import PayloadTestHelper
from w3af.plugins.attack.payloads.payload_handler import exec_payload

class test_get_source_code(PayloadTestHelper):

    EXPECTED_RESULT = {"https://moth/w3af/audit/local_file_read/local_file_read.php":
                       (
                       '/var/www/moth/w3af/audit/local_file_read/local_file_read.php',
                       'tmp__random__/var/www/moth/w3af/audit/local_file_read/local_file_read.php')
                       }

    CONTENT = "echo file_get_contents( $_REQUEST['file'] );"

    @attr('ci_fails')
    def test_get_source_code(self):
        temp_dir = tempfile.mkdtemp()
        result = exec_payload(self.shell, 'get_source_code', args=(temp_dir,),
                              use_api=True)

        self.assertEqual(len(self.EXPECTED_RESULT.keys()), 1)

        expected_url = self.EXPECTED_RESULT.keys()[0]
        downloaded_url = result.items()[0][0].url_string
        self.assertEquals(expected_url, downloaded_url)

        downloaded_file_path = result.items()[0][1][1]
        downloaded_file_content = file(downloaded_file_path).read()
        self.assertTrue(self.CONTENT in downloaded_file_content)

        shutil.rmtree(temp_dir)