#!/usr/bin/env python
# coding: utf-8

# In[1]:

from __future__ import absolute_import, division, print_function

import re
import os
from os import getcwd
from os.path import join, exists, isdir, isfile
from tqdm import tqdm

import torch
import torch.utils.data as data

import numpy as np
import pandas as pd

# In[2]:

def get_category_level_data(data_dir, category_type):
    """
    Returns a dict, with keys 'train' and 'valid' and respective values as record dataframes,
    these dataframes contain multiple columns based on the category type
    Args:
        Category_type (string): one of the several study type file names in 'train/valid/test' dataset
    """

    study_data = {}
    #BASE_FILE = category_type + '.csv'

    os.chdir(data_dir)
    K = pd.read_csv('intrinsic.csv')
    K = K.dropna(axis = 0, how = 'all')
    K = K.replace(to_replace = float('NaN'), value = 0)

    msk = np.random.rand(len(K)) < 0.8
    study_data['train'] = K[msk]
    study_data['valid'] = K[~msk]

    return study_data

# In[3]:

class ML3Dataset(data.Dataset):

    def __init__(self, df, transform=None, download=False):

        self.df = df
        self.transform = transform

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        row = self.df.iloc[idx, :]

        reconstructed_row = row.copy()

        return torch.tensor(row), torch.tensor(reconstructed_row)

# In[4]:

if __name__ == '__main__':
    import pprint

    data_dir = '/Users/bravos/Documents/CourseWork/Semester2/cs536-ML/ManyLab3/'
    files_dir = join(data_dir, 'files')
    #all_data_dir = join(data_dir, 'ML3')

    assert isdir(data_dir) and isdir(files_dir) #and isdir(all_data_dir)

    study_data = get_category_level_data(files_dir, category_type='intrinsic')
    print(len(study_data['train']), len(study_data['valid']))

    #val_csv = join(data_dir, 'valid.csv')
    val_loader = data.DataLoader(
        ML3Dataset(study_data['valid']),
        batch_size=4,
        shuffle=False,
        num_workers=1,
        pin_memory=False)

    print("What is val_loader? :", type(val_loader))

    for i, (row, reconstructed_row) in enumerate(val_loader):
#         print(i, "\n")
        pprint.pprint(row)
        if i == 10:
            break

# In[ ]:


import numpy as np
from .metrics import r2_score

class LinearRegression:

    def __init__(self):
        """初始化 LinearRegression模型"""

        self.coef_ = None
        self.interception_ = None
        self._theta = None

    def fit_normal(self, X_train, y_train):
        """根据训练集X_train和y_train训练LinearRegression模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            'the size of X_train must be equal to the size of y_train'

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]
        return self

    def fit_gd(self, X_train, y_train, eta=0.01, n_iters=1e4, epsilon=1e-8):
        """根据训练数据集 X_train y_train,使用梯度下降法训练LinearRegression模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            'the size of X_train must be equal to the size of y_train'

        def J(theta, X_b, y):
            try:
                return np.sum((y - X_b.dot(theta))**2) / len(X_b)
            except:
                return float('inf')

        def dJ(theta, X_b, y):
            # res = np.empty(len(theta))
            # res[0] = np.sum(X_b.dot(theta) - y)
            # for i in range(1, len(theta)):
            #     res[i] = (X_b.dot(theta) - y).dot(X_b[:, i])
            # return res * 2 / len(X_b)
            return X_b.T.dot(X_b.dot(theta) - y) * 2 / len(X_b)  # 使用向量化的方法

        def gradient_descent(X_b, y, initial_theta, eta, n_iters, epsilon):
            theta = initial_theta
            cur_iter = 0
            while cur_iter < n_iters:
                last_theta = theta
                theta = theta - eta * dJ(theta, X_b, y)
                if abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon:
                    break
                cur_iter += 1
            return theta

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        initial_theta = np.zeros(X_b.shape[1])
        self._theta = gradient_descent(X_b, y_train, initial_theta, eta, n_iters, epsilon)
        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]
        return self

    def fit_sgd(self, X_train, y_train, n_iters=5, t0=5, t1=50):
        """根据训练集X_train和y_train,使用梯度下降法训练Linear Regresssion模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            'the size of X_b must be equal to the size of y'
        assert n_iters >= 1, \
            'the size of n_iters must be 1 at least'

        def dJ_sgd(theta, X_b_i, y_i):
            return X_b_i.T.dot(X_b_i.dot(theta) - y_i) * 2.

        def sgd(X_b, y, initial_theta, n_iters, t0=5, t1=50):  # 如果把这里的t0=5, t1=50换成t0和t1；那么在fit_sgd()中可以修改t0和t1的值了
            def learning_rate(t):
                return t0 / (t + t1)
            theta = initial_theta
            m = len(X_b)
            for cur_iter in range(n_iters):
                indexes = np.random.permutation(m)
                X_b_new = X_b[indexes]
                y_new = y[indexes]
                for i in range(m):
                    gradient = dJ_sgd(theta, X_b_new[i], y_new[i])
                    theta = theta - learning_rate(cur_iter * m + i) * gradient  # 注意learning_rate里面的参数
            return theta

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        initial_theta = np.zeros(X_b.shape[1])
        self._theta = sgd(X_b, y_train, initial_theta, n_iters, t0, t1)
        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]
        return self

    def predict(self, X_predict):
        """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
        assert self.coef_ is not None and self.interception_ is not None, \
            'must fit before predict'
        assert X_predict.shape[1] == len(self.coef_), \
            'the feature num of X_predict must be equal to X_train'

        X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
        return X_b.dot(self._theta)

    def score(self, X_test, y_test):
        """根据测试数据集X_test,y_test,确定当前模型的准确度"""

        y_predict = self.predict(X_test)
        return r2_score(y_test, y_predict)

    def __repr__(self):
        return 'LinearRegression()'


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

from __future__ import print_function
import requests
import re
import sys
import logging
import configargparse

from queue import Queue
from threading import Thread

# ======================================================
logging.basicConfig(
    format='%(asctime)s %(levelname)s: %(message)s',
    datefmt='%H:%M:%S'
)
log = logging.getLogger(__name__)

# ======================================================
# Scrap Hyde my ass multi-pages
def scrapes_hma(pages):
    log.debug('Start getting HMA proxis, in %s page(s).', pages)

    proxies = []
    for i in range(1,pages):
            scrape_hma(str(i), proxies),

    log.debug('Ending, found %s proxies.', len(proxies))
    return(proxies)

# ======================================================
# Scrap Hyde My Ass ProxyList
def scrape_hma(page, proxies):

    r = requests.get('http://proxylist.hidemyass.com/'+page)
    bad_class="("
    for line in r.text.splitlines():
        class_name = re.search(r'\.([a-zA-Z0-9_\-]{4})\{display:none\}', line)
        if class_name is not None:
           bad_class += class_name.group(1)+'|'

    bad_class = bad_class.rstrip('|')
    bad_class += ')'

    to_remove = '([0-9]{1,3}|[0-9]{1,3}|
[0-9]{1,3}
||</?span>|)'

    junk = re.compile(to_remove, flags=re.M)
    junk = junk.sub('', r.text)
    junk = junk.replace("\n", "")

    proxy_src = re.findall('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s*	\s*\s*([0-9]{2,6}).{100,1200}(socks4/5|HTTPS?)', junk)
    found = 0
    for src in proxy_src:
        if src[2] == 'socks4/5':
            proto = 'socks5'
        else:
            proto = src[2].lower()

        if src:
            proxies.append(proto + '://' +src[0] + ':' + src[1])
            found += 1

    log.debug('Request: %s, found : %s', 'http://proxylist.hidemyass.com/'+page, found)
    """
    list = ''
    for src in proxy_src:
        if src[2] == 'socks4/5':
            proto = 'socks5'
        else:
            proto = src[2].lower()
        if src:
            list += proto + '://' +src[0] + ':' + src[1] + '\n'
    return(list)"""

# ======================================================
# Simple function to do a call to Niantic's system for testing proxy connectivity
def check_proxy(proxy_queue, timeout, proxies):

    # Update check url - Thanks ChipWolf #1282 and #1281
    proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'
    proxy = proxy_queue.get()

    if proxy and proxy[1]:

        log.debug('Checking proxy: %s', proxy[1])

        try:
            proxy_response = requests.post(proxy_test_url, '', proxies={'http': proxy[1], 'https': proxy[1]}, timeout=timeout)

            if proxy_response.status_code == 200:
                log.debug('Proxy %s is ok', proxy[1])
                proxy_queue.task_done()
                proxies.append(proxy[1])
                return True

            elif proxy_response.status_code == 403:
                proxy_error = "Proxy " + proxy[1] + " is banned - got status code: " + str(proxy_response.status_code)

            else:
                proxy_error = "Wrong status code - " + str(proxy_response.status_code)

        except requests.ConnectTimeout:
            proxy_error = "Connection timeout (" + str(timeout) + " second(s) ) via proxy " + proxy[1]

        except requests.ConnectionError:
            proxy_error = "Failed to connect to proxy " + proxy[1]

        except Exception as e:
            proxy_error = e

    else:
            proxy_error = "Empty proxy server"

    log.warning('%s', proxy_error)
    proxy_queue.task_done()

    return False

# ======================================================
# Check all proxies and return a working list with proxies
def check_proxies(proxys, proxy_timeout):

    proxy_queue = Queue()
    total_proxies = len(proxys)

    proxies = []

    for proxy in enumerate(proxys):
        proxy_queue.put(proxy)

        t = Thread(target=check_proxy,
                   name='check_proxy',
                   args=(proxy_queue, proxy_timeout, proxies))
        t.daemon = True
        t.start()

    # This is painfull but we need to wait here untill proxy_queue is completed so we have a working list of proxies
    proxy_queue.join()

    working_proxies = len(proxies)

    if working_proxies == 0:
        log.error('Proxy was configured but no working proxies was found! We are aborting!')
        sys.exit(1)
    else:
        log.info('Proxy check completed with %d working proxies of %d configured', working_proxies, total_proxies)
        return proxies

# ======================================================
"""
f = open('myfile','w')
f.write('hi there\n') # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call it
"""
if __name__ == "__main__":
    p = configargparse.ArgParser()
    p.add_argument('-cf', '--config-file', is_config_file=True, help='config file path')
    p.add_argument('-p', '--pages', help='Number of pages to scrape',type=int, default=1)
    p.add_argument('-pt', '--proxy-timeout', help='Timeout settings for proxy checker in seconds', type=int, default=5)
    p.add_argument('-f', '--file', help='Output file, default print in console')
    p.add_argument(
        '-fl',
        '--flat',
        help='Flat format like http://ip;port\n ( default :[http://ip;port, ...])',
        action='store_true',
        default=False
    )
    p.add_argument('-v', help='verbose', action='store_true', default=False)
    p.add_argument('-d', help='debug', action='store_true', default=False)

    args = p.parse_args()

    if args.v:
        log.setLevel(logging.INFO)

    if args.d:
        log.setLevel(logging.DEBUG)

    try:
        proxies = scrapes_hma(args.pages)
        working_proxies = check_proxies(proxies, args.proxy_timeout)

        if args.file is None:
            print(working_proxies)
        else:
            f = open(args.file, 'w+')
            if args.flat:
                for wp in working_proxies:
                    f.write(wp + '\n')
            else :
                f.write('[' + ', '.join(working_proxies) + ']')

            f.close()

    except Exception as e:
        log.error('%s', e)


n = int(input())
m=list(map(int,input().split()))
m.sort()
result = 0
cnt =0
for i in m:
    cnt += 1
    if(cnt>=i):
        cnt = 0
        result += 1

print(result)


import requests

class WrappedPerfSonarServiceV1:
    """
    Backend algorithm for ECS based on PerfSonar wrapper

    This algorithm is basically a proxy
    """

    def __init__(self, namespace, visualnet_api=None, **kwargs):
        self.namespace = namespace
        self.uri = visualnet_api

    def lookup(self, srcs, dsts, cost_type):
        headers = {
            'content-type': 'application/json'
        }
        req = {
            'cost-type': {
                'cost-metric': cost_type['cost-metric'],
                'anchor-alg': 'same-site',
                'interval': { 'last': '2d' }
            },
            'endpoints': [
                {
                    'srcs': srcs,
                    'dsts': dsts
                }
            ]
        }
        resp = requests.post(self.uri, json=req, headers=headers)
        data = resp.json()
        data['meta']['cost-type']['cost-mode'] = cost_type['cost-mode']
        return data


#!/usr/bin/env python
from app import create_app
from flask.ext.script import Manager
import coverage
import os

COV = None
COV = coverage.coverage(branch=True, include='app/*')
COV.start()

app = create_app("development")
manager = Manager(app)

# run unittest
@manager.command
def test(coverage=False):
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)
    if COV:
        COV.stop()
        COV.save()
        print('Coverage Summary:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'testreport/coverage')
        COV.html_report(directory=covdir)
        print('HTML version: file://%s/index.html' % covdir)
        COV.erase()

if __name__ == '__main__':
    manager.run()


import requests
import os
import json

API_KEY = os.environ['OPENWEATHER_KEY']

# Weather Messages
thunderstorm_message = "It looks like there will be thunderstorms today. Try to stay inside, but if you have to be " \
                       "outside, bring an umbrella. Stay Safe!"
drizzle_message = "It looks like it's going to drizzle today! Think about wearing a raincoat or bring an umbrella " \
                  "where ever you go!"
rain_message = "Rain is in the forecast today! Make sure to dust off your umbrella today if you don't want to get too " \
               "wet!"
snow_message = "Someone put a spoon under their pillow last night! Get ready for some snow today, and be careful on " \
               "the roads. Thing about wearing boots and a jacket when you go outside."
clear_message = "It looks to be a clear one today, so you know what that means: suns out guns out! Think about some " \
                "sunscreen if you don't want to get burnt, and spend some time outside in the sun if it's warm enough!"
clouds_message = "There looks to be some gray in the forecast today. Nothing too exciting falling out of the sky, " \
                 "but that can be a good thing."
atmospheric_message = "There is {} outside. Be careful where you go, and watch your phone for updated weather alerts."

# Temperature
zero = "Brrrrrr, it's going to be very cold today, make sure to wear plenty of layers!"
twenty = "Whew, it's going to be cold one out there today. While it could be worse, I still recommend a few layers. " \
         "Think about covering up those ears and fingers, and make sure to wear a warm jacket!"
forty = "You feel that? It's a chilly one out there today. Make sure to wear a few layers, but you probably don't " \
        "have to go overboard. A sweater and jacket will probably be enough."
sixty = "Some call it warm, some call it cold. Honestly, I think today's temperature is 'just right'. Think about " \
        "wearing a sweatshirt and pants."
eighty = "Dress light today, it's a warm one. If the sun is out, you probably won't need a sweater."
hundred = "Wow, it's hot out there today. Definitely dress light, trust me, if you spend too much time outside you " \
          "will be sweating."
above = "Nope. Too hot out there today. Stay inside if possible, and if you go outside keep your clothes light. Make " \
        "sure you bring water wherever you go to stay hydrated."

def get_weather_message(lat, lon):
    weather_response = requests.get(
        f'https://api.openweathermap.org/data/2.5/onecall?'
        f'lat={lat}&lon={lon}&exclude=current,minutely,hourly,alert&units=imperial&appid={API_KEY}'
    )
    data = json.loads(weather_response.content)

    daily = data['daily'][0]
    temp = daily['temp']['day']
    weather_id = daily['weather'][0]['id']
    weather_main = daily['weather'][0]['main']

    weather_message = ''
    temp_message = ''

    if weather_main == 'Thunderstorm':
        weather_message = thunderstorm_message
    elif weather_main == 'Drizzle':
        weather_message = drizzle_message
    elif weather_main == 'Rain':
        weather_message = rain_message
    elif weather_main == 'Snow':
        weather_message = snow_message
    elif weather_main == 'Clear':
        weather_message = clear_message
    elif weather_main == 'Clouds':
        weather_message = clouds_message
    elif 701 <= weather_id <= 781:
        weather_message = atmospheric_message.format(weather_main.lower())

    if temp < 0:
        temp_message = zero
    elif 0 <= temp < 20:
        temp_message = twenty
    elif 20 <= temp < 40:
        temp_message = forty
    elif 40 <= temp < 60:
        temp_message = sixty
    elif 60 <= temp < 80:
        temp_message = eighty
    elif 80 <= temp < 100:
        temp_message = hundred
    elif 100 <= temp:
        temp_message = above

    return temp_message + ' ' + weather_message


# this function needs to fix from the main part................. ( even code incorrect from javascript)
def typeArray(val):
    r = []
    for v in val:
        v_type = type(v)
        if not (type in r):
            r.append(v_type)
    return {'type': r}

def typeValue(val):
    if isinstance(val, list):
        return {'type': 'array', 'items': typeArray(val)}
    # check if it is an object or not!!!!!
    if isinstance(val, dict):
        properties = getProperties(val)
        return {'type': 'object', 'properties': properties, 'required': properties.keys()}

    if isinstance(val, int):
        return {'type': 'integer'}

    return {'type': type(val)}

def getProperties(j):
    print("!!!!!!!!!!!!!!!!!!! this instance is not checked yet")
    k = j.keys()
    for name in k:
        j[name] = typeValue(j[name])
    return j

def getSchema(json_object):
    schema = {}

    # print("!!!!!!!!!!!!!!!!!!!! getSchema in json_schema_infer is not completed.....")

    schema['$schema'] = 'http://json-schema.org/schema#'
    schema['title'] = 'JSON inferred schema'
    schema['description'] = 'JSON inferred schema'
    schema['type'] = 'object'
    try:
        schema['properties'] = getProperties(json_object)
        schema['required'] = schema['properties'].keys()
    except:
        schema['properties'] = {}
        schema['required'] = schema['properties'].keys()

    return schema


import os
import imapclient
from datetime import datetime
from email.utils import parsedate_to_datetime
import email
import re
from django.conf import settings
from .models import Ticket, TicketThread, Attachment
from email.header import decode_header
from django.core.files import File
import tempfile
import hashlib

def get_emails():
    # Conecta-se ao servidor de e-mail configurado e obtém os e-mails da caixa de entrada.
    # Retorna uma lista de dicionários contendo os assuntos e corpos dos e-mails.
    client = imapclient.IMAPClient(settings.MAIL_SERVER)

    client.login(settings.MAIL_USERNAME, settings.MAIL_PASSWORD)

    client.select_folder('INBOX')

    messages = client.search(['FROM', "noreply.escoladigital@min-educ.pt"])

    # messages = client.search(['FROM', "simaosousasms2006@gmail.com"])

    response = client.fetch(messages, ['BODY[]'])

    emails = []
    for msgid, data in response.items():
        raw_email = data[b'BODY[]']
        email_message = email.message_from_bytes(raw_email)
        raw_subject = email_message.get('Subject', '')
        decoded_header = decode_header(raw_subject)
        subject = ''.join([str(text, charset or 'utf-8') if isinstance(text,
                          bytes) else str(text) for text, charset in decoded_header])
        body = get_body(email_message)

        date_str = email_message.get('Date', None)
        date = parsedate_to_datetime(date_str)

        code = extract_code_from_subject(subject)

        attachments = save_attachments(email_message, code)

        emails.append({'subject': subject, 'body': body,
                      'date': date, 'attachments': attachments})
    return emails

def get_body(email_message):
    # Extrai o corpo de um e-mail.
    # Se o e-mail for multipart, retorna o corpo da parte 'text/html', se disponível.
    # Caso contrário, retorna a parte 'text/plain' ou o corpo do próprio e-mail.
    body = None
    if email_message.is_multipart():
        for part in email_message.walk():
            if part.get_content_type() == 'text/html' and part.get('Content-Disposition') is None:
                body = part.get_payload(decode=True)
                break
    else:
        body = email_message.get_payload(decode=True)
    return body.decode('utf-8')

def extract_code_from_subject(subject):
    code_regex = r"\[(.*?)\]"
    match = re.search(code_regex, subject)
    return match.group(1) if match else None

def create_or_update_ticket(subject, body, code, date):
    # Cria ou atualiza um ticket com base no código e na data.
    # Retorna a instância do ticket criado ou atualizado.
    thread, _ = TicketThread.objects.get_or_create(thread_code=code)

    ticket, created = Ticket.objects.get_or_create(
        code=code,
        date=date,
        defaults={
            'title': subject,
            'body': body,
            'thread': thread,
            'date': date
        }
    )
    if not created:
        # update the ticket only if any of the field has changed.
        if ticket.title != subject or ticket.body != body or ticket.thread != thread or ticket.date != date:
            ticket.title = subject
            ticket.body = body
            ticket.thread = thread
            ticket.date = date
            ticket.save()

    return ticket

def update_ticket_and_thread_status(ticket_instance, subject):
    # Atualiza o status do ticket e do thread com base no assunto do e-mail.
    # Se o assunto contiver "fechado" ou "resolvido", o status é definido como "closed".
    if "fechado" in subject or "resolvido" in subject:
        ticket_instance.status = "F"
        ticket_instance.save()
        thread = TicketThread.objects.get(tickets=ticket_instance)
        thread.status = "F"
        thread.save()

def save_attachments(email_message, code):
    attachments = []
    if email_message.is_multipart():
        for part in email_message.walk():
            if part.get_content_maintype() == 'multipart':
                continue
            if part.get('Content-Disposition') is None:
                continue

            file_name = part.get_filename()

            if bool(file_name):
                base_name, ext = os.path.splitext(file_name)
                counter = 1
                new_file_name = f"{base_name}_{code}{ext}"
                while os.path.exists(os.path.join(tempfile.gettempdir(), new_file_name)):
                    new_file_name = f"{base_name}_{code}({counter}){ext}"
                    counter += 1

                file_path = os.path.join(tempfile.gettempdir(), new_file_name)
                with open(file_path, 'wb') as f:
                    f.write(part.get_payload(decode=True))
                attachments.append({'filename': file_name, 'path': file_path})

    return attachments

def fetch_and_process_emails():
    print("fetching emails...")
    emails = get_emails()
    for email_data in emails:
        print(email_data)
    for email_data in emails:
        title = email_data['subject']
        body = email_data['body']
        code = extract_code_from_subject(title)
        date = email_data['date']
        attachments = email_data.get('attachments', [])

        if code is None:
            print(f"Could not extract code from email with title: {title}")
            continue
        ticket_instance = create_or_update_ticket(title, body, code, date)
        print(ticket_instance)

        for attachment in attachments:
            with open(attachment['path'], 'rb') as f:
                content = f.read()
                # create a hash of the file content
                content_hash = hashlib.md5(content).hexdigest()

                # check if a file with the same hash already exists
                try:
                    existing_attachment = Attachment.objects.get(
                        hash=content_hash)
                except Attachment.DoesNotExist:
                    # if it doesn't exist, save the file and its hash
                    att_instance = Attachment(
                        ticket=ticket_instance, hash=content_hash)
                    att_instance.file.save(
                        os.path.basename(attachment['path']), File(f))
                    att_instance.save()

            os.remove(attachment['path'])

        update_ticket_and_thread_status(ticket_instance, title)


from rpi_control_center import GPIO_engine
import json
import threading
import datetime
import time
import os
import os.path
import glob
import logging
import logzero
from logzero import setup_logger

########################################################### Global Variables
format = '%(color)s[%(levelname)1.1s %(asctime)s %(name)s :%(funcName)s %(thread)d]%(end_color)s %(message)s' # format for the logzero logger
formatter = logzero.LogFormatter(fmt=format) # format object for logzero logger
debug_mode = False #debug mode for developers

########################################################### Wrapper/decorator definition function
def threaded(func):
	"""start and return a thread of the passed in function. Threadify a function with the @threaded decorator"""
	def wrapper(*args, **kwargs):
		thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=False)
		thread.start()
		return thread
	return wrapper

class PHController():
	def __init__(self,up_pin=26, down_pin=20, ph_api_file='./api/PH.json', config_file='./ph_config.json',dose_time=10,delay_time=20, high_thresh = 8, low_thresh=7, margin= 0.5, api_dir = './api/', log_dir = './logs/', refresh_rate = 1):

		if not os.path.exists(log_dir):
			os.makedirs(log_dir)
		log_file = log_dir + 'PH_Controller.log'

		self.state = False
		self.config_file = config_file
		self.refresh_rate = refresh_rate
		self.api_dir = api_dir
		self.log_dir = log_dir
		self.high_thresh = high_thresh + margin
		self.low_thresh = low_thresh - margin
		self.up_pin = up_pin
		self.down_pin = down_pin
		self.dose_time = dose_time
		self.delay_time = delay_time
		self.relay_engine = None
		self.ph_api_file = ph_api_file
		self.ph = None
		self.logger = setup_logger(name=__name__+"_PH_Controller_logger", logfile=log_file, level=10 if debug_mode else 20, formatter = formatter, maxBytes=2e6, backupCount=3)

	@property
	def state(self):
		"""Return the state of the PHController"""
		return self._state
	@state.setter
	def state(self,value):
		"""Set the state of the PHController"""
		self._state = value

	@property
	def config_file(self):
		"""Return the config_file of the PHController"""
		return self._config_file
	@config_file.setter
	def config_file(self,value):
		"""Set the config_file of the PHController"""
		self._config_file = value

	@property
	def refresh_rate(self):
		"""Return the refresh_rate of the PHController"""
		return self._refresh_rate
	@refresh_rate.setter
	def refresh_rate(self,value):
		"""Set the refresh_rate of the PHController"""
		self._refresh_rate = value

	@property
	def api_dir(self):
		"""Return the api_dir of the PHController"""
		return self._api_dir
	@api_dir.setter
	def api_dir(self,value):
		"""Set the api_dir of the PHController"""
		self._api_dir = value

	@property
	def log_dir(self):
		"""Return the log_dir of the PHController"""
		return self._log_dir
	@log_dir.setter
	def log_dir(self,value):
		"""Set the log_dir of the PHController"""
		self._log_dir = value

	@property
	def high_thresh(self):
		"""Return the high_thresh of the PHController"""
		return self._high_thresh
	@high_thresh.setter
	def high_thresh(self,value):
		"""Set the high_thresh of the PHController"""
		self._high_thresh = value

	@property
	def low_thresh(self):
		"""Return the low_thresh of the PHController"""
		return self._low_thresh
	@low_thresh.setter
	def low_thresh(self,value):
		"""Set the low_thresh of the PHController"""
		self._low_thresh = value

	@property
	def up_pin(self):
		"""Return the up_pin of the PHController"""
		return self._up_pin
	@up_pin.setter
	def up_pin(self,value):
		"""Set the up_pin of the PHController"""
		self._up_pin = value

	@property
	def down_pin(self):
		"""Return the down_pin of the PHController"""
		return self._down_pin
	@down_pin.setter
	def down_pin(self,value):
		"""Set the down_pin of the PHController"""
		self._down_pin = value

	@property
	def dose_time(self):
		"""Return the dose_time of the PHController"""
		return self._dose_time
	@dose_time.setter
	def dose_time(self,value):
		"""Set the dose_time of the PHController"""
		self._dose_time = value

	@property
	def delay_time(self):
		"""Return the delay_time of the PHController"""
		return self._delay_time
	@delay_time.setter
	def delay_time(self,value):
		"""Set the delay_time of the PHController"""
		self._delay_time = value

	@property
	def relay_engine(self):
		"""Return the relay_engine of the PHController"""
		return self._relay_engine
	@relay_engine.setter
	def relay_engine(self,value):
		"""Set the relay_engine of the PHController"""
		self._relay_engine = value

	@property
	def ph_api_file(self):
		"""Return the ph_api_file of the PHController"""
		return self._ph_api_file
	@ph_api_file.setter
	def ph_api_file(self,value):
		"""Set the ph_api_file of the PHController"""
		self._ph_api_file = value

	@property
	def ph(self):
		"""Return the ph of the PHController"""
		return self._ph
	@ph.setter
	def ph(self,value):
		"""Set the ph of the PHController"""
		self._ph = value

	@property
	def logger(self):
		"""Return the logger of the PHController"""
		return self._logger
	@logger.setter
	def logger(self,value):
		"""Set the logger of the PHController"""
		self._logger = value

	def begin(self):
		default_config = {
							"1":{'name':'PH_up', 'pin':self.up_pin, 'state':False},
							"2":{'name':'PH_down', 'pin':self.down_pin, 'state':False}
						}
		self.relay_engine = GPIO_engine.BulkUpdater(
													config_file = self.config_file,
													api_dir = self.api_dir,
													default_config = default_config,
													refresh_rate = self.refresh_rate,
													log_dir = self.log_dir
													)
		self.relay_engine.safe_stop_all_relays()
		self.relay_engine.start()
		self.logger.info("[PH Controller]: PH Controller Set up Successful")

	def get_ph(self, manual_ph=None):
		if self.ph_api_file:
			try:
				with open(self.ph_api_file, 'r')as f:
					data = json.load(f)
				ph = data['ph']
			except:
				self.logger.warning('[PH Controller]:Error, Could not retrieve pH data from API file')
				ph = manual_ph
		else:
			ph = manual_ph

		self.ph = ph
		return ph

	@threaded
	def start(self):
		self.state = True
		self.begin()
		while self.state:
			self.get_ph()
			if not self.ph:
				self.logger.warning('[PH controller]: Please Enable PH readings')
				time.sleep(5)
			while self.state and self.ph:
				self.get_ph()
				ph = self.ph

				try:
					if ph < self.low_thresh:
						self.logger.info(f'[PH controller]:[Ph={ph}] Low threshold hit enabling PH up')
						self.relay_engine.update_config_file("1",True)
						time.sleep(self.dose_time)
						self.relay_engine.update_config_file("1",False)
						time.sleep(self.delay_time)
					if ph > self.high_thresh:
						self.logger.info(f'[PH controller]:[Ph={ph}] High threshold hit enabling PH down')
						self.relay_engine.update_config_file("2",True)
						time.sleep(self.dose_time)
						self.relay_engine.update_config_file("2",False)
						time.sleep(self.delay_time)
					else:
						time.sleep(self.refresh_rate)
				except:
					self.logger.warning('[PH controller]: Could not get proper PH value, will try again')
					time.sleep(self.refresh_rate)

		self.relay_engine.stop()
		self.logger.info("[PH controller]: Stopped")

	def stop(self):
		self.state = False

if __name__ == '__main__':
	pass


from django.db import models

class Request(models.Model):
    message = models.TextField()
    number = models.CharField(null=False, max_length=50)
    date_request = models.DateTimeField(null=False)
    request_type = models.IntegerField(null=False)
    response = models.CharField(null=True, max_length=2)
    description = models.TextField(null=True)
    code = models.IntegerField(null=True)
    date_received = models.DateTimeField(null=True)
    date_sent = models.DateTimeField(null=True)

    class Meta:
        verbose_name = u"Request"
        verbose_name_plural = u"Requests"

    def __unicode__(self):
        return "%s" % self.message

class Last_Whatsapp_Id(models.Model):
    whatsapp_id = models.IntegerField(default=0)
    class Meta:
        verbose_name = u"LastID"
        verbose_name_plural = u"LastID"

    def __unicode__(self):
        return "%s" % self.whatsapp_id


#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

# separateCerts.py inFilePath
#
# Reads a file with a number of PEM certificates, which is split into
# individual certificates and may files are written, with numbered
# correlative names prefixed with the original filename:
# inputFilename[0-9]+ .

import re
import sys

if len(sys.argv)<2 or len(sys.argv)>2:
    sys.stderr.write("Usage: separateCerts.py <inFilePath>\n")
    exit(1)

filename=sys.argv[1]

try:
    fh=open(filename,"r")
except:
    sys.stderr.write("File: "+filename+" not found.\n")
    exit(2)

try:
    crtstr=fh.read()
except:
    sys.stderr.write("File: "+filename+". Read error.\n")
    exit(3)

fh.close()

if len(crtstr)<=0:
    sys.stderr.write("File: "+filename+". Empty file.\n")
    exit(4)

crtarr=re.findall("-+.*?BEGIN.*?CERTIFICATE.*?-+.*?-+.*?END.*?CERTIFICATE.*?-+.*?",crtstr,re.S)

if len(crtarr)<=0:
    sys.stderr.write("File: "+filename+". No PEM certificates in file.\n")
    exit(5)

#if len(crtarr)<2:
#    sys.stderr.write("File: "+filename+". 2 certs expected at least.\n")
#    exit(6)

count=0
for cert in crtarr:
    fh=open(filename+"."+str(count),"w")
    fh.write(cert+"\n\n")
    fh.close()

    count+=1

exit(0)


import logging
import re
from typing import Union
from rdflib.plugins.parsers.ntriples import W3CNTriplesParser as NTripleParser, DummySink as Sink, ParseError
from rdflib.util import from_n3
from rdflib import BNode, Literal, URIRef, Variable, Graph, Namespace
from rdflib.graph import Graph

import codecs
import sys

import click
import os

Node = Union[URIRef, Literal, BNode]

class NTParser(NTripleParser):

    def skipparse(self, f):
        """Parse f as an N-Triples/N3 file."""
        if not hasattr(f, 'read'):
            raise ParseError("Item to parse must be a file-like object.")

        # since N-Triples 1.1 files can and should be utf-8 encoded
        self.file = codecs.getreader('utf-8')(f)
        self.buffer = ''

        while True:
            self.line = self.readline()
            if self.line is None:
                break
            try:
                self.parseline()
            except ParseError:
                logging.warning(f"parse error: dropping {self.line}. Reason {sys.exc_info()[0]}")
                continue
        return self.sink

class SplitSink(Sink):

    def __init__(self, outdir):
        self.currentfile = None
        os.makedirs(outdir, exist_ok=True)
        self.outdir = outdir

    def triple(self, s: Node, p: Node, o: Node):
        tofile = re.split(r"#|/", s.toPython())[-1]
        #domain = URIRef(s.toPython().rsplit('/', 1)[0])
        if self.currentfile is None:
            currentfile = open(os.path.join(self.outdir, f"{tofile}.nt"), "a")
        if (f"{tofile}.nt" != os.path.basename(currentfile.name)):
            currentfile.close()
            currentfile = open(os.path.join(self.outdir, f"{tofile}.nt"), "a")
        currentfile.write(f"{s.n3()}\t{p.n3()}\t{o.n3()}\t.\n")

@click.command()
@click.argument("infile", type=click.Path(exists=True, file_okay=True, dir_okay=False))
@click.argument("outdir", type=click.Path(dir_okay=True, file_okay=False))
def split(infile, outdir):
    sink = SplitSink(outdir=outdir)
    n = NTParser(sink)
    with open(infile, "rb") as input_file:
        n.skipparse(input_file)

if __name__ == '__main__':
    split()


#!/usr/bin/python3
""" Solution to the N queens problem """
import sys

def queens(n, i=0, a=[], b=[], c=[]):
    """
    Args:
        n (int): size of the board
        i (int): row
        a (list): columns
        b (list): diagonal
        c (list): other diagonal
    Yields:
        list: solution in the form of a list of lists of coordinates
    """
    if i < n:
        for j in range(n):
            if j not in a and i+j not in b and i-j not in c:
                # if a solution in this branch is promising, yield it
                yield from queens(n, i+1, a+[j], b+[i+j], c+[i-j])
    else:
        yield [[i, j] for i, j in enumerate(a)]

if __name__ == "__main__":
    try:
        if len(sys.argv) < 2:
            raise ValueError("Usage: nqueens N")

        N = sys.argv[1]
        if not N.isdigit():
            raise TypeError("N must be a number")

        N = int(N)
        if N < 4:
            raise ValueError("N must be at least 4")

        for solution in queens(N):
            print(solution)
    except Exception as err:
        if hasattr(err, "message"):
            print(err.message)
        else:
            print(err)
        sys.exit(1)


import re
import recipe
import requests
import unicodedata
import pandas as pd
from bs4 import BeautifulSoup

__base_url = 'https://www.homechef.com'

def __verbose_print(to_print, verbose):
    if verbose:
        print(to_print)

##### SECTION 1:  Finding all recipe links #####

def __get_all_links(soup, base_url):
    links = []
    for x in soup.find_all('a', href=True):
        if '/recipes' in x['href']:
            links.append(x['href'].replace(base_url, ''))
        elif '/meals/' in x['href']:
            links.append(x['href'].replace(base_url, ''))
    return links

def __filter_links(links):
    other_links = []
    recipe_links = []
    for link in links:
        if '/meals/' in link:
            recipe_links.append(link)
        else:
            other_links.append(link)
    return recipe_links, other_links

def find_recipe_urls(verbose=False):
    prev_extensions = []
    search_extensions = ['/recipes']
    recipe_extensions = []

    while len(search_extensions) > 0:
        curr_search_extension = search_extensions.pop(0)
        __verbose_print(str(len(search_extensions)) + '\t' + curr_search_extension, verbose)
        prev_extensions.append(curr_search_extension)

        search_url = __base_url + curr_search_extension
        search_page = requests.get(search_url)
        search_soup = BeautifulSoup(search_page.content, "html.parser")
        search_links = __get_all_links(search_soup, __base_url)

        recipe_links, other_links = __filter_links(search_links)

        recipe_extensions.extend(recipe_links)
        for link in other_links:
            if link not in prev_extensions and link not in search_extensions:
                search_extensions.append(link)

    to_return = list(set(recipe_extensions))
    __verbose_print('Found ' + str(len(to_return)) + ' recipes from Home Chef', verbose)
    for i in range(len(to_return)):
        to_return[i] = __base_url + to_return[i]
    return to_return

##### SECTION 2:  Parsing recipe links to recipe objects #####

def __get_recipe_soup(url):
    recipe_page = requests.get(url)
    recipe_soup = BeautifulSoup(recipe_page.content, "html.parser")

    return recipe_soup

def __parse_numeric(num_str):
    try:
        return float(num_str)
    except:
        if len(num_str) == 1:
            return unicodedata.numeric(num_str)
        else:
            return float(num_str[:-1]) + unicodedata.numeric(num_str[-1])

def __get_recipe_df(recipe_soup):
    recipe_elements = [x.text.replace('\n', ' ').replace('\r', '').replace('fl. oz.', 'fl.oz.').replace('Info', '').strip().split(' ') for x in recipe_soup.find_all('li', itemprop='recipeIngredient')]

    recipe_dict_list = []
    for i in range(len(recipe_elements)):
        new_item = {}
        recipe_elements[i][0] = __parse_numeric(recipe_elements[i][0])
        recipe_elements[i][1] = 'unit' if recipe_elements[i][1] == '' else recipe_elements[i][1].replace('fl.oz.', 'fluid ounce').replace('oz.', 'ounce').replace('tsp.', 'teaspoon').replace('tbsp.', 'tablespoon')
        recipe_elements[i][2] = ' '.join(recipe_elements[i][2:])
        recipe_elements[i] = recipe_elements[i][:3]

        new_item['qty'] = recipe_elements[i][0]
        new_item['unit'] = recipe_elements[i][1]
        new_item['item'] = recipe_elements[i][2]
        recipe_dict_list.append(new_item)

    df = pd.DataFrame(recipe_dict_list, columns=['qty', 'unit', 'item'])
    df = df.fillna("")

    return df

def __get_recipe_details(recipe_soup, url):
    recipe_details = {}

    recipe_details['url'] = url
    recipe_details['name'] = recipe_soup.find_all('h1')[0].text.strip()
    for soup in recipe_soup.find_all('p'):
        if soup.find('em') is not None:
            recipe_details['description'] = soup.text.strip()
            break
    recipe_details['servings'] = int(recipe_soup.find('meta', itemprop='recipeYield')['content'].split()[0])

    recipe_details['allergens'] = {}
    recipe_details['allergens']['immediate'] = [x.strip() for x in re.sub("[\(\[].*?[\)\]]", "", recipe_soup.find('p', {'class': 'textSm'}).text.strip().replace('\n', ' ').replace('\r', '').replace('Contains:', '')).split(',')]
    recipe_details['allergens']['secondary'] = []
    recipe_details['time'] = {}
    recipe_details['time']['total'] = recipe_soup.find_all('div', {'class': 'meal__overviewItem'})[0].text.strip().replace('\n', ' ').replace('\r', '').replace('Prep & Cook Time:', '').replace('.', '').strip().replace('min', 'minutes')
    recipe_details['time']['prep'] = ''
    recipe_details['difficulty'] = recipe_soup.find_all('div', {'class': 'meal__overviewItem'})[1].text.strip().replace('\n', ' ').replace('\r', '').replace('Difficulty Level:', '').strip()

    nutrition_list = [x.text.lower().strip().replace('\n', ' ').replace('\r', '').strip() for x in recipe_soup.find('ul', itemprop='nutrition').find_all('li')]
    nutrition_dict = {}
    for i in range(len(nutrition_list)):
        nutrition_dict[' '.join(nutrition_list[i].split()[:-1])] = re.sub(r"([0-9]+(\.[0-9]+)?)",r" \1 ", nutrition_list[i].split()[-1]).strip()
    if ' kcal' not in nutrition_dict['calories']:
        nutrition_dict['calories'] += ' kcal'
    recipe_details['nutrition'] = {}
    try:
        recipe_details['nutrition']['calories'] = nutrition_dict['calories']
    except:
        recipe_details['nutrition']['calories'] = ''
    try:
        recipe_details['nutrition']['fat'] = nutrition_dict['fat']
    except:
        recipe_details['nutrition']['fat'] = ''
    try:
        recipe_details['nutrition']['saturated_fat'] = nutrition_dict['saturated_fat']
    except:
        recipe_details['nutrition']['saturated_fat'] = ''
    try:
        recipe_details['nutrition']['carbohydrates'] = nutrition_dict['carbohydrates']
    except:
        recipe_details['nutrition']['carbohydrates'] = ''
    try:
        recipe_details['nutrition']['sugar'] = nutrition_dict['sugar']
    except:
        recipe_details['nutrition']['sugar'] = ''
    try:
        recipe_details['nutrition']['fiber'] = nutrition_dict['fiber']
    except:
        recipe_details['nutrition']['fiber'] = ''
    try:
        recipe_details['nutrition']['protein'] = nutrition_dict['protein']
    except:
        recipe_details['nutrition']['protein'] = ''
    try:
        recipe_details['nutrition']['cholesterol'] = nutrition_dict['cholesterol']
    except:
        recipe_details['nutrition']['cholesterol'] = ''
    try:
        recipe_details['nutrition']['sodium'] = nutrition_dict['sodium']
    except:
        recipe_details['nutrition']['sodium'] = ''

    return recipe_details

def get_recipe(url):
    recipe_soup = None
    try:
        recipe_soup = __get_recipe_soup(url)
    except:
        raise recipe.RecipeParseException('Invalid Recipe URL\n' + url)

    recipe_df = None
    try:
        recipe_df = __get_recipe_df(recipe_soup)
    except:
        raise recipe.RecipeParseException('Invalid Recipe URL\n' + url)

    for column in recipe_df.columns:
        if recipe_df[column].isnull().all():
            raise recipe.RecipeParseException('Invalid Recipe URL\n' + url)

    recipe_details = None
    try:
        recipe_details = __get_recipe_details(recipe_soup, url)
    except:
        raise recipe.RecipeParseException('Invalid Recipe URL\n' + url)

    to_return = recipe.Recipe(recipe_details, recipe_df)
    return to_return


# -*- coding: utf-8 -*-
"""
https://leetcode.com/problems/coloring-a-border/

The key is to find the border, use BFS.

Time complexity: O(mn)
"""
import collections
class Solution:
    def colorBorder(self, grid, r0, c0, color):
        m, n = len(grid), len(grid[0])
        ref_color = grid[r0][c0]
        border = []
        queue = collections.deque([(r0, c0)])
        visited = {(r0, c0)}
        while queue:
            x, y = queue.popleft()
            nei = []
            for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                new_x, new_y = x + dx, y + dy
                if 0 <= new_x < m and 0 <= new_y < n:
                    nei.append((new_x, new_y))
            cnt = 0
            for a, b in nei:
                if grid[a][b] == ref_color: # same component
                    cnt += 1
                    if (a, b) not in visited:
                        visited.add((a, b))
                        queue.append((a, b))

            if cnt != 4: # (x, y) is border
                border.append((x, y))

        for x, y in border:
            grid[x][y] = color
        return grid


import projet
import automaton

# ---------- TEST DETERMINISATION ---------- #
aut = automaton.automaton(
        epsilons = ['0'], initials = [1,3], finals = [2],
        transitions = [
            (1,'a',2), (1,'b',3), (2,'b',2), (2,'a',3), (3,'a',1)
        ]
)

aut.display("Automate de depart")
aut2 = projet.determinisation(aut)
aut2.display("Automate determinise")


import tensorflow as tf
import numpy as np
import sys
import os
from time import time
from sacred import Experiment
from sklearn.metrics import roc_auc_score, average_precision_score

from tempfile import NamedTemporaryFile
import traceback
import faulthandler

sys.path.insert(0, "/Users/felix/Coding/CNP-TCN/src")
sys.path.insert(0, '../../src')
from data_prepro.prepro_utils import load_dataset, flatten_batch, flatten_batch_query
from utils.util import count_parameters, num_grid_times_to_grid_times
from cnp.cnp import DeterministicModel, DeterministicEncoder, DeterministicDecoder
from models.cnp_tcn import CNPTCN

#TODO labels would be nice

ex = Experiment('CNP-TCN')

@ex.config
def cnp_tcn_config():
    #makes feeding params from hyperparameter optimization easier, don't need to access dicts directly
    encoder_output_size = 128 #[128, 128, 128, 128]
    decoder_output_size = 128 #[128, 128, 2]
    decoder_levels = 3
    encoder_levels = 4
    n_channels = 40
    levels = 4
    kernel_size = 4
    dropout = 0.1
    batch_size = 500
    epochs = 100
    learning_rate = 0.0001
    l2_penalty = 100
    log_prob_fraction = 0.00001

    tcn_parameters = {
    'n_channels': n_channels,
    'levels': levels,
    'kernel_size': kernel_size,
    'dropout': dropout,
    'n_classes' : 2
    }

    cnp_parameters = {
    'encoder_output_size' : encoder_output_size,
    'decoder_output_size' : encoder_output_size,
    'decoder_levels' : decoder_levels,
    'encoder_levels' : encoder_levels,
    'num_channels' : 44 #channels of the input data
    }

    training_parameters = {
    'batch_size' : batch_size,
    'epochs' : epochs,
    'data_filepath' : '../data_prepro/mgp-rnn-datadump_labs_vitals_covs_na_thres_500_min_length_7_max_length_200_horizon_0_split_0.pkl',
    'learning_rate' : learning_rate,
    'l2_penalty' : l2_penalty,
    'log_prob_fraction': log_prob_fraction #loss is fraction*log_prob_loss  + (1-fraction)*classifier_loss
    }

@ex.automain
def fit_cnp_tcn(cnp_parameters, tcn_parameters, training_parameters,  _rnd, _seed, _run):
     #Path to save model:
    if len(_run.observers) > 0:
         checkpoint_path = os.path.join(_run.observers[0].dir, 'model_checkpoints')
    else:
        checkpoint_path = 'model_checkpoints'

    #load the data
    #outputs = simplify_time(data.times_test, data.ind_times_test), data.values_test, data.ind_lvs_test, data.num_rnn_grid_times_test, data.labels_test

    times_va, values_va, channels_va, num_grid_times_va, labels_va = load_dataset(training_parameters['data_filepath'], dataset='validation')
    times_tr, values_tr, channels_tr, num_grid_times_tr, labels_tr = load_dataset(training_parameters['data_filepath'], dataset='train')

    print('data loaded')

    #Get class imbalance (for weighted loss):
    case_prev = labels_tr.sum()/float(len(labels_tr)) #get prevalence of cases in train dataset
    class_imb = 1/case_prev #class imbalance to use as class weight if losstype='weighted'

    ##test_freq     = Ntr/batch_size #eval on test set after this many batches
    test_freq = 5#int(values_tr.shape[0]/training_parameters['batch_size'] / 4)

    rs = np.random.RandomState(_seed)
    tf.set_random_seed(_seed)

    # Define the model
    model = CNPTCN(tcn_parameters, cnp_parameters,class_imb,training_parameters['l2_penalty'])
    #model = DeterministicModel(cnp_parameters['encoder_output_sizes'], cnp_parameters['decoder_output_sizes'], cnp_parameters['num_channels'])

    #encoder = DeterministicEncoder(cnp_parameters['encoder_output_sizes'], cnp_parameters['num_channels'])
    #decoder = DeterministicDecoder(cnp_parameters['decoder_output_sizes'], cnp_parameters['num_channels'])
    print('model set up')
    #setup operations
    #placeholders
    x_input = tf.placeholder("float", [None,1]) #num_data_points
    y_input = tf.placeholder("float", [None,1]) #num_data_points
    z_input = tf.placeholder(tf.int32, [None]) #num_data_points
    obs_ind = tf.placeholder(tf.int32, [None]) #num_data_points
    x_target  = tf.placeholder('float', [None, 1]) #num_query_points
    z_target = tf.placeholder(tf.int32, [None]) #num_query_points
    obs_ind_target = tf.placeholder(tf.int32, [None]) #num_query_points
    labels = tf.placeholder(tf.int32, [None]) #batch_size
    labels_one_hot =  tf.one_hot(labels, tcn_parameters['n_classes']) #batch_size x 2
    is_training = tf.placeholder("bool")

    #mu, sigma, log_prob = model.cnp(x_input,y_input,z_input,obs_ind, x_target, z_target, obs_ind_target)
    logits, cnp_log_prob, tcn_loss = model(x_input,y_input,z_input,obs_ind, x_target, z_target, obs_ind_target, labels_one_hot, is_training)
    probs = model.compute_probs(logits)

    #Z, log_prob = model._get_cnp_output(x_input,y_input,z_input,obs_ind, x_target, z_target, obs_ind_target)
    loss = tf.reduce_mean(cnp_log_prob) * -1

    #Idea to test: balance training of cnp and tcn, find sweet spot of mgp-like behaviour of cnp and best classifier performance.
    #Maybe is dumb idea and should focus on classifier only

    #get two weights that fulfill ratio condition and sum to 1

    complete_loss = training_parameters['log_prob_fraction']*loss + (1-training_parameters['log_prob_fraction'])* tcn_loss

    sess = tf.Session()
    global_step = tf.Variable(0, trainable=False)
    optimizer = tf.train.AdamOptimizer(training_parameters['learning_rate'])
    train_op = optimizer.minimize(complete_loss, global_step=global_step)
    sess.run(tf.global_variables_initializer())
    print("Graph setup!")

    #tcn_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "temporal_conv_net/")

    count_parameters()

    #setup minibatch indices for training:
    batch_size = training_parameters['batch_size']
    Ntr = labels_tr.shape[0]

    starts = np.arange(0,Ntr,batch_size)
    ends = np.arange(batch_size,Ntr+1,batch_size)
    if ends[-1]<Ntr:
        ends = np.append(ends,Ntr)
    num_batches = len(ends)

    #setup minibatch indices for validation (memory saving)
    Nva = labels_va.shape[0]
    va_starts = np.arange(0,Nva,batch_size)
    va_ends = np.arange(batch_size,Nva+1,batch_size)
    if va_ends[-1]<Nva:
        va_ends = np.append(va_ends,Nva)

    #-------------------
    # MGP training loop
    #-------------------
    saver = tf.train.Saver(max_to_keep = None)
    loss_list =[]
    total_batches = 0
    best_LL = 0
    best_val =0
    for i in range(training_parameters['epochs']):

        #-------------------
        # Train
        #-------------------
        epoch_start = time()
        print("Starting epoch "+"{:d}".format(i))
        perm = rs.permutation(Ntr)
        batch = 0
        for s,e in zip(starts,ends):

            batch_start = time()
            inds = perm[s:e]

            #prepare the data for the feed dict: model takes 1d tensors times, values, channels and obs_indicator as input
            #these are the training points for the encoder
            times_obs, values_obs, channels_obs, obs_inds_obs = flatten_batch(times_tr[inds], values_tr[inds], channels_tr[inds])
            #these are the query points for the decoder
            times_query, channels_query, obs_inds_query = flatten_batch_query(num_grid_times_tr[inds], cnp_parameters['num_channels'])

            feed_dict={x_input: times_obs ,y_input: values_obs, z_input: channels_obs,obs_ind: obs_inds_obs,
                x_target: times_query, z_target: channels_query, obs_ind_target: obs_inds_query, labels: labels_tr[inds],
                is_training: True
                }

            try:
                loss_,logits_, probs_, _ = sess.run([complete_loss,logits, probs, train_op],feed_dict)
            except Exception as e:
                traceback.format_exc()
                print('Error occured in tensorflow during training:', e)
                #In addition dump more detailed traceback to txt file:
                with NamedTemporaryFile(suffix='.csv') as f:
                    faulthandler.dump_traceback(f)
                    _run.add_artifact(f.name, 'faulthandler_dump.csv')
                break

            print("Batch "+"{:d}".format(batch)+"/"+"{:d}".format(num_batches)+\
                  ", took: "+"{:.3f}".format(time()-batch_start)+", loss: "+"{:.5f}".format(loss_))
            sys.stdout.flush()
            batch += 1; total_batches += 1

            if total_batches % test_freq == 0: #Check val set every so often for early stopping

                #--------------------------
                # Validate and write scores
                #--------------------------

                print('--> Entering validation step...')
                val_t = time()
                #Batch-wise Validation Phase:
                va_probs_tot = np.array([])
                va_perm = rs.permutation(Nva)

                va_labels_tot = labels_va[va_perm]

                perm = rs.permutation(Ntr)
                batch = 0
                for v_s,v_e in zip(va_starts,va_ends):

                    batch_start = time()
                    #inds = perm[s:e]
                    va_inds = va_perm[v_s:v_e]

                    #prepare the data for the feed dict: model takes 1d tensors times, values, channels and obs_indicator as input
                    #these are the training points for the encoder
                    times_obs, values_obs, channels_obs, obs_inds_obs = flatten_batch(times_va[va_inds], values_va[va_inds], channels_va[va_inds])
                    #these are the query points for the decoder
                    times_query, channels_query, obs_inds_query = flatten_batch_query(num_grid_times_va[va_inds], cnp_parameters['num_channels'])

                    feed_dict={x_input: times_obs ,y_input: values_obs, z_input: channels_obs,obs_ind: obs_inds_obs,
                        x_target: times_query, z_target: channels_query, obs_ind_target: obs_inds_query, labels: labels_va[va_inds],
                        is_training: False
                        }

                    try:
                        va_loss_,va_logits_, va_probs_ = sess.run([complete_loss,logits, probs],feed_dict)

                    except Exception as e:
                        traceback.format_exc()
                        print('Error occured in tensorflow during training:', e)
                        #In addition dump more detailed traceback to txt file:
                        with NamedTemporaryFile(suffix='.csv') as f:
                            faulthandler.dump_traceback(f)
                            _run.add_artifact(f.name, 'faulthandler_dump.csv')
                        break
                    va_probs_tot = np.concatenate([va_probs_tot, va_probs_])

                va_auc = roc_auc_score(va_labels_tot, va_probs_tot)
                va_prc = average_precision_score(va_labels_tot, va_probs_tot)
                best_val = max(va_prc, best_val)
                print("Epoch "+str(i)+", seen "+str(total_batches)+" total batches. Validating Took "+\
                    "{:.2f}".format(time()-val_t)+\
                    ". OOS, "+str(0)+" hours back: Loss: "+"{:.5f}".format(va_loss_)+ \
                    ", AUC: {:.5f}".format(va_auc)+", AUPR: "+"{:.5f}".format(va_prc))
                _run.log_scalar('train_loss', va_loss_, total_batches)
                _run.log_scalar('val_auprc', va_prc, total_batches)
                #_run.log_scalar('lenghts', length_, total_batches)
                sys.stdout.flush()

                #create a folder and put model checkpoints there
                saver.save(sess, checkpoint_path + "/epoch_{}".format(i), global_step=total_batches)
        print("Finishing epoch "+"{:d}".format(i)+", took "+\
                "{:.3f}".format(time()-epoch_start))

        ### Takes about ~1-2 secs per batch of 50 at these settings, so a few minutes each epoch
        ### Should converge reasonably quickly on this toy example with these settings in a few epochs
    return {'Best Validation AUPRC': best_val}


from fastapi import status, HTTPException
from sqlalchemy.orm import Session

from .. import models
from blog.schemas import Blog

def create(request: Blog, db: Session):
    new_blog = models.Blog(
        title=request.title,
        body=request.body,
        user_id=1
    )

    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)

    return new_blog

def get_all(db: Session):
    blogs = db.query(models.Blog).all()

    return blogs

def get_blog(id: int, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f'Blog with the id of {id} not found!')

    return blog

def update(id: int, request: Blog, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not blog:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'Blog with the id of {id} not found'
        )

    # update individual blog attributes
    for key, value in request.__dict__.items():
        setattr(blog, key, value)

    db.commit()

    return 'Blog details successfuly updated'

def destroy(id: int, db: Session):
    blog = db.query(models.Blog).filter(models.Blog.id == id)
    if not blog:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'blog with the id of {id} not found'
        )

    blog.delete(synchronize_session=False)

    db.commit()

    return 'Blog post successfully deleted!'


from hyperparameters import *
import torch
import numpy as np
from timeit import default_timer as timer

def test_epoch(model, testset, groundtestset, CHUNK_TEST_SIZE):
  test_loss = []
  state = None
  timer_beg = timer()
  predY = []
  # Evaluation flag
  model.eval()
  # We don't need to keep track of the gradients
  with torch.no_grad():

    # this loop slides over one chunk in windows of length SEQ_LEN
    # *first loop -> 0 : SEQ_LEN -1
    # *second loop-> SEQ_LEN : 2*SEQ_LEN
    # *...
    # *last loop -> CHUNK_SIZE - SEQ_LEN : CHUNK_SIZE - 1
    # Observe that SEQ_LEN is the length of the sequences we feed into the LSTM
    for beg_t, end_t in zip(range(0, CHUNK_TEST_SIZE - SEQ_LEN - 1, SEQ_LEN),
                            range(SEQ_LEN, CHUNK_TEST_SIZE, SEQ_LEN)):

      dataX = torch.empty([BATCH_SIZE, SEQ_LEN, 2*NUM_JOINTS], device=device)
      dataY = torch.empty([BATCH_SIZE, SEQ_LEN, NUM_JOINTS], device=device)
      # Step 2. Get our inputs ready for the network, that is, turn them into
      # Tensors of one-hot sequences.
      # for every batch
      for batch in range(BATCH_SIZE):
        # we obtain the batch
        batch_data = testset[batch] # shape = [CHUNK_SIZE, NUM_JOINTS*2]
        batch_ground = groundtestset[batch] # shape = [CHUNK_SIZE, NUM_JOINTS]

        # we obtain the sequence by applying the window [beg_t:end_t] to the batch
        seq_data = batch_data[beg_t:end_t, :] # shape = [SEQ_LEN, NUM_JOINTS*2]
        seq_ground = batch_ground[beg_t:end_t, :] # shape = [SEQ_LEN, NUM_JOINTS]

        # Add the sequences to dataX and dataY
        dataX[batch, :, :] = seq_data # create batch-dim and append
        dataY[batch, : , :] = seq_ground # create batch-dim and append

      # Step 3. Run our forward pass.
      # Forward through model and carry the previous state forward in time (statefulness)
      y_, state = model(dataX, None)
      predY.append(y_)
      # detach the previous state graph to not backprop gradients further than the BPTT span
      state = (state[0].detach(), # detach h[t]
              state[1].detach()) # detach c[t]

      # Step 4. Compute the loss, gradients, and update the parameters by
      #  calling optimizer.step()
      loss = loss_function(y_, dataY)
      test_loss.append(loss.item())
    timer_end = timer()
    return test_loss, predY


from datetime import date

class IdeFish:

    def __init__(self, name, species):
        # Establish the properties of each animal
        # with a default value
        self.name = name
        self.species = species
        self.date_added = date.today()
        self.swimming = True
Jack = IdeFish("Mr Jack", "Fish")
# Jack.name = "Mr Jack"
# Jack.species = "Fish"
print(Jack)


# A procedure, cellular_automaton, that takes three inputs:
#     a non-empty string,
#     a pattern number which is an integer between 0 and 255 that
# represents a set of rules, and
#     a positive integer, n, which is the number of generations.
# The procedure should return a string which is the result of
# applying the rules generated by the pattern to the string n times.

def cellular_automaton(string, pattern_number, iterations):
    pattern_dict = create_pattern_dict(pattern_number)
    for i in range(iterations):
        new_string = ""
        for k in range(len(string)):
            new_string = new_string + new_k(k, string, pattern_dict)
        string = new_string
    return new_string

def new_k(k, string, pattern_dict):
    post_k = k +1
    if k == len(string) -1:
        post_k = 0
    sub_string= string[k-1] + string[k] + string[post_k]
    for pattern in pattern_dict:
        if pattern == sub_string:
            return pattern_dict[pattern]
    return "Error in pattern match"

def create_pattern_dict(pattern_number):
    pattern_list = ['xxx', 'xx.', 'x.x', 'x..', '.xx', '.x.', '..x', '...']
    bin_number = bin(pattern_number)[2:].zfill(8)
    bin_pattern = bin_number.replace('0','.').replace('1','x')
    return dict(zip(pattern_list, bin_pattern))


import os
import time
import json
import dict
import shutil
import asyncio
import aiohttp
import aiofiles
import traceback
from tqdm.contrib import itertools

class Threading:
    def __init__(self, list_type):
        self.list_type = list_type
        self.tasks = []

    async def create_thread(self):
        for type in self.list_type:
            task = asyncio.to_thread(self.start_thread, type)
            self.tasks.append(task)
        await asyncio.gather(*self.tasks)

    @staticmethod
    def start_thread(type):
        parser = Parser(type)
        print(f"Старт потока _{type}_: {time.strftime('%X')}")
        asyncio.run(parser.main())
        print(f"Завершение цикла событий _{type}_: {time.strftime('%X')}")

class Parser:
    url_interpol = "https://ws-public.interpol.int/notices/v1/"

    def __init__(self, type, session=None):
        self._type = type
        self.session = session

    async def main(self):
        os.mkdir(f'{self._type}') if not os.path.exists(f"{self._type}") else shutil.rmtree(f'{self._type}')
        async with aiohttp.ClientSession() as self.session:
            iteration = Iteration(self._type, self.session)
            write_file = WriteFile(self._type, self.session)
            # построение url с фильтрами
            for country, gender, age in itertools.product(dict.country_list, dict.gender_list, range(1, 100), ncols=100, position=0, leave=True):
                try:
                    full_url = f'{self.url_interpol}{self._type}?nationality={country}&sexId={gender}&ageMin={age}&ageMax={age}'
                    #print(f"Страна:{country}. Пол:{gender}. Возраст {age}")
                    async with self.session.get(full_url) as json_response:
                        list_json = await json_response.json(content_type=None)
                        if list_json['_embedded']['notices']:
                            path, profile_json, image_link = await iteration.iteration_item_in_list(full_url, list_json)
                            await write_file.write_json(path, profile_json)
                            await write_file.write_image(path, image_link)
                            await asyncio.sleep(0.5)
                        else:
                            #print("Нет данных на запрос")
                            continue
                except Exception as error:
                    print(f"Ошибка при парсинге:\n{error}", traceback.format_exc())
                    await write_file.write_txt(full_url)
                    continue

class Iteration(Parser):
    def __init__(self, type, session):
        self.link_list = []
        super().__init__(type, session)

    async def iteration_pages(self, full_url, list_json):
        # перебор страниц
        for page in range(1, int(list_json['_links']['last']['href'][-1]) + 1):
            async with self.session.get(f'{full_url}&resultPerPage=20&page={page}') as list_response:
                pages_json = await list_response.json(content_type=None)
                return pages_json

    async def iteration_item_in_list(self, full_url, list_json):
        # перебор элементов на странице
        list_json = await self.iteration_pages(full_url, list_json)
        await asyncio.sleep(0.5)
        for item in list_json['_embedded']['notices']:
            if item['_links']['self']['href'] not in self.link_list:
                link = item['_links']['self']['href']
                self.link_list.append(link)
                async with self.session.get(link) as item_response:
                    profile_json = await item_response.json(content_type=None)
                    path = f"{item['name']}_{item['forename']}({item['entity_id'].replace('/', '.')})"
                    image_link = item['_links']['images']['href']
                    return path, profile_json, image_link

class WriteFile(Parser):
    def __init__(self, type, session):
        super().__init__(type, session)

    async def write_txt(self, link):
        # запись url с ошибкой
        with open(f"{self._type}_error_link.txt", 'w') as file:
            file.write(link + '\n')

    async def write_image(self, path, api_images):
        # запись изображений из профиля
        async with self.session.get(api_images) as res_images:
            profile_photo_data = await res_images.json(content_type=None)
            for item_image in profile_photo_data['_embedded']['images']:
                async with self.session.get(f"{api_images}/{item_image['picture_id']}") as res_image:
                    file = await aiofiles.open(f"{self._type}/{path}/{item_image['picture_id']}.jpg", mode='wb')
                    await file.write(await res_image.read())
                    await file.close()

    async def write_json(self, path, data):
        # запись данных json из профиля
        os.makedirs(f'{self._type}/{path}')
        with open(f"{self._type}/{path}/data.json", 'w') as outfile:
            json.dump(data, outfile, indent=2)

if __name__ == '__main__':
    thread = Threading(['yellow', 'red'])
    asyncio.run(thread.create_thread())


#my solution
def solution(dartResult):
    i = 0
    result = []
    cnt = 0
    while len(dartResult) > i:
        score = 0

        #점수
        if dartResult[i+1].isdigit(): #10인 경우
            score = 10
            i += 2
        else:
            score = int(dartResult[i])
            i += 1

        #보너스
        if dartResult[i] == "D":
            score = score**2
        elif dartResult[i] == "T":
            score = score**3
        i += 1

        #옵션
        if len(dartResult) > i:
            if dartResult[i] == "*":
                if i < 4:
                    score = score*2
                else:
                    result[cnt-1] = result[cnt-1]*2
                    score = score*2
                i += 1

            elif dartResult[i] == "#":
                score = 0 - score
                i += 1

        result.append(score)
        cnt += 1

    return sum(result)

#best solution: regular expression
import re

def solution(dartResult):
    bonus = {'S' : 1, 'D' : 2, 'T' : 3}
    option = {'' : 1, '*' : 2, '#' : -1}
    p = re.compile('(\d+)([SDT])([*#]?)')
    dart = p.findall(dartResult)
    for i in range(len(dart)):
        if dart[i][2] == '*' and i > 0:
            dart[i-1] *= 2
        dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]

    answer = sum(dart)
    return answer

print(solution('10S2D*3T'))


import string
def pangram(str):
     alp="abcdefghijklmnopqrstuwxyz"
     for char in alp:
            if char not in str.lower():
                  return False
     else:
            return True

string="The quick brown fox jumps over the lazy dog"
if(pangram(string)==True):
     print("yes")
else:
     print("no")


class Foo:
	def __init__(self):
		self.bar = 1
		self.num = 1
		self.word1 = 'a'
		self.word2 = 'a'

def class_manipulation1(x):
	x.bar += 1
	x.num = x.num + 1
	x.word1 += 'b'
	x.word2 = 'b'

def class_manipulation2(x):
	new_foo = Foo()
	x = new_foo
	x.bar += 1
	x.num = x.num + 1
	x.word1 += 'b'
	x.word2 = 'b'

if __name__ == '__main__':
	# example 1
	foo1 = Foo()
	class_manipulation1(foo1)

	print(f"foo1.bar = {foo1.bar}")
	print(f"foo1.num = {foo1.num}")
	print(f"foo1.word1 = {foo1.word1}")
	print(f"foo1.word2 = {foo1.word2}")

	"""
	foo1.bar = 2
	foo1.num = 2
	foo1.word1 = ab
	foo1.word2 = b
	"""
	#------------------------------------------------------
	# example 2
	foo2 = Foo()
	class_manipulation2(foo2)

	print(f"foo2.bar = {foo2.bar}")
	print(f"foo2.num = {foo2.num}")
	print(f"foo2.word1 = {foo2.word1}")
	print(f"foo2.word2 = {foo2.word2}")

	"""
	foo2.bar = 1
	foo2.num = 1
	foo2.word1 = a
	foo2.word2 = a
	"""


# Given an string in roman no format (s)  your task is to convert it to integer .

roman = 'MMMCMCCIV'

dict = {
    'I': 1,
    'II': 2,
    'III': 3,
    'IV': 4,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
}

def convert(roman, dict):
    roman_int = 0
    i = 0
    while i < len(roman):
        value1 = dict[roman[i]]

        if (i+1) < len(roman):
            value2 = dict[roman[i+1]]

            if value1 >= value2:
                roman_int += value1
                i += 1
            else:
                roman_int += value2 - value1
                i += 2
        else:
            roman_int += value1
            i += 1

    return roman_int

print(f"Given roman number: {roman}")
print(f"Roman number in integer: {convert(roman, dict)}")

180Geeks/Strings/Roman Number to Integer.py


class People:
    name = None
    age = None
    gender = None

    def personInfo(self):
        print("姓名%s —— 性别%s ——  年龄%d" % (self.name, self.gender, self.age))

    def __init__(self, a, b, c):
        self.name = a
        self.gender = b
        self.age = c

class Student(People):
    college = None
    Group = None

    def __init__(self, a, b, c, d, e):
        People.__init__(self, a, b, c)
        self.college = d
        self.Group = e

    def personInfo(self):
        People.personInfo(self)
        print("—— 学院%s ——班级%s" % (self.college, self.Group))

p = People("刘金涛", "男", 21)
p.personInfo()
s = Student("刘金涛", "男", 21, "计算机", "3班")
s.personInfo()


from pathlib import PurePosixPath
from typing import Dict, Any, Union, Sequence, Iterable, Tuple
import re

from cw.cached import cached

TreeNodeKey = Union[str, int]
TreeNodePath = Union[PurePosixPath, TreeNodeKey, Sequence[TreeNodeKey]]

int_re = re.compile(r"^\s*\[(\d+)\]\s*$")

class TreeNode:
    """

    """
    def __init__(self, *, value: Any=None, key: TreeNodePath = None, parent: "TreeNode"=None, default=None):
        self.children: Dict[TreeNodeKey, TreeNode] = {}
        if key is None and (parent is not None):
            raise ValueError("Only the root node may not have a key.")

        # At this point NoneType is allowed because it may only be the root node, which is unnamed.
        if not isinstance(key, (str, int, type(None))):
            raise TypeError("The key may only be of type str or int.")

        # Set the parent. This will be None for the root node.
        self.parent = parent

        if parent is not None:
            self.key = key
            self.root = parent.root
            self.parent.add_child(self)
        else:
            self.root = self
            self.key = ""

        self.value = value
        self.__default = default

    @classmethod
    def from_path_value_pairs(cls, path_value_pairs: Iterable[Tuple[TreeNodePath, Any]]):
        root = cls(default=lambda:  None)
        for path, value in path_value_pairs:
            root[path] = value
        root.set_default(None)
        return root

    @classmethod
    def from_dict(cls, path_value_pairs: Dict[TreeNodePath, Any]):
        return cls.from_path_value_pairs(path_value_pairs.items())

    @cached
    def default(self):
        # If __default is None, then return the default of the parent node.
        # If this is the root Node, return None.
        if self.__default is None:
            if self.is_root:
                return None
            else:
                return self.parent.default
        else:
            return self.__default

    def set_default(self, default):
        self.__default = default
        del self.default
        for _, child in self.items():
            if hasattr(child, "default"):
                del child.default

    @cached
    def path(self):
        node = self
        path_parts = []
        while node.parent is not None:
            path_parts.append(node.path_key)
            node = node.parent
        return PurePosixPath("/", *reversed(path_parts))

    @cached
    def path_key(self):
        return self.escape_key(self.key)

    @cached
    def is_root(self):
        return self is self.root

    @staticmethod
    def escape_key(key):
        if isinstance(key, int):
            return f"[{key}]"
        elif isinstance(key, str):
            return key
        else:
            raise TypeError("The key may only be of type str or int.")

    @staticmethod
    def unescape_key(escaped_key):
        match = int_re.match(escaped_key)
        if match:
            return int(match.group(1))
        else:
            return escaped_key

    @staticmethod
    def generate_path_obj(path: TreeNodePath) -> PurePosixPath:
        if isinstance(path, PurePosixPath):
            path = path
        elif isinstance(path, (list, tuple)):
            path = PurePosixPath(*(f"[{x}]" if isinstance(x, int) else x for x in path))
        else:
            path = PurePosixPath(f'[{path}]' if isinstance(path, int) else path)
        return path

    def add_child(self, child: "TreeNode"):
        self.children[child.key] = child

    def node_from_path(self, path: TreeNodePath):
        path = self.generate_path_obj(path)
        path_parts = path.parts

        # If the path_parts tuple is empty, then the path points to this object.
        if not path_parts:
            return self

        next_node_key = path_parts[0]

        # If the path is absolute set the search node it the root node.
        # Otherwise set the child node to the next child in line.
        if path.is_absolute():
            next_node = self.root
        else:
            # If the next node name is "..", go to the parent.
            if next_node_key == "..":
                next_node = self.parent or self
            else:
                if next_node_key in self.children:
                    next_node = self.children[self.escape_key(path_parts[0])]
                else:
                    if callable(self.default):
                        next_node = TreeNode(key=self.unescape_key(next_node_key), parent=self, value=self.default())
                    else:
                        raise KeyError(f"Node with key '{next_node_key}' in path '{path}' does not exist.")

        # Keep searching for the target node in at the next_node.
        return next_node.node_from_path(PurePosixPath(*path_parts[1:]))

    def __getitem__(self, path: TreeNodePath):
        return self.node_from_path(path).value

    def __setitem__(self, path: TreeNodePath, value):
        node = self.node_from_path(path)
        node.value = value

    def __iter__(self):
        yield from self.children

    def __len__(self):
        return len(self.children)

    def items(self):
        yield from self.children.items()

    def path_values(self):
        for _, node in self.children:
            yield node.path, node.weight

    def path_nodes(self):
        for _, node in self.children:
            yield node.path, node

    # It finally happened, this function ran without bugs on the first try.
    # This only means one thing. Programming karma will be striking back soon.
    #
    # Never mind, found two bugs already. Three. Four.
    # Seventh try's the charm.
    def object_hierarchy(self):
        # If all the child nodes have integers as keys and all of these keys are
        # positive ranging from 0 to the largest key value without gaps, then
        # it's a list.

        # If this node has children (and is not a list) then it's a dictionary

        # Otherwise this node is an endpoint node and contains a value.

        if len(self):
            # The node has children, so it's a container (either list or dictionary).
            key_set = set(self)
            if all((isinstance(key, int) for key in key_set)):
                # All children have integer keys, so it might be a list.
                # Check if all the keys range from 0 to max key value without gaps.
                if key_set == set(range(max(key_set) + 1)):
                    # It's a list, get the children's object_hierarchy() value, make sure
                    # they are put in the list in the correct order
                    return list((self.children[i].object_hierarchy() for i in range(max(key_set) + 1)))

            # If this code was reached, it's a dictionary.
            # Create a dictionary with the values being the return value of the
            # children's object_hierarchy() function.
            return dict(((key, child.object_hierarchy()) for key, child in self.items()))
        else:
            # The node has no children, so return the value.
            return self.value

    def __str__(self):
        return f"<TreeNode path='{self.path}' key='{self.key}' value='{self.value}'>"


def count_bases(seq):
    """"Counting number of bases in the sequence"""
    counter_a = 0
    counter_c = 0
    counter_g = 0
    counter_t = 0
    for elem in seq:
        if elem == 'A':
            counter_a += 1
        elif elem == 'C':
            counter_c += 1
        elif elem == 'G':
            counter_g += 1
        elif elem == 'T':
            counter_t += 1
    dict_bases = {'A': counter_a,'C': counter_c,'G': counter_g, 'T':counter_t}
    return dict_bases


# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head:
            return False
        pre_vis = cur_vis = head
        while pre_vis and cur_vis:
            cur_vis = cur_vis.next
            pre_vis = pre_vis.next
            if not pre_vis:
                return False
            pre_vis = pre_vis.next
            if pre_vis == cur_vis:
                return True
        return False


import numpy as np
import pandas as pd
import os
from sklearn.preprocessing import LabelEncoder  # кодирование категориальных
from sklearn.feature_selection import SelectKBest  # выбор признаков
from sklearn.feature_selection import chi2  # выбор по Хи квадрат
from sklearn.model_selection import train_test_split  # деление на тест и обучение
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from catboost import CatBoostClassifier

class Settings:

    def __init__(self):
        self.df_row_count = 2001
        self.df_subject_count = 6
        self.df_min_grade = 0
        self.df_max_grade = 99
        self.df_file = 'students.csv'
        self.model_file = 'people_university_model.cbm'
        self.model_file_eff = 'people_university_model_eff.cbm'
        self.resource_folder = 'ml'
        self.final_result_list = ['удовлетворительно', 'хорошо', 'отлично']

        if not os.path.exists(self.resource_folder):
            os.mkdir(self.resource_folder)

    def df_path(self):
        return self.resource_folder + '/' + self.df_file

    def model_path(self):
        return self.resource_folder + '/' + self.model_file

    def model_eff_path(self):
        return self.resource_folder + '/' + self.model_file_eff

class DataframeGenerator:

    def __init__(self, settings: Settings):
        self.__settings = settings

    def generate(self):
        subjects = [f"subject_{num + 1}" for num in range(self.__settings.df_subject_count)]

        grades = np.random.randint(
            self.__settings.df_min_grade,
            self.__settings.df_max_grade + 1,
            (self.__settings.df_row_count, self.__settings.df_subject_count)
        )
        student_scores = pd.DataFrame(grades, columns=subjects)

        student_scores.sum(axis=0)
        student_scores['mean_score'] = np.round((student_scores.sum(axis=1) / self.__settings.df_subject_count), 3)

        student_scores['final_lab'] = student_scores.apply(self.__generate_final, axis=1)

        return student_scores

    def __generate_final(self, row):
        score = row['mean_score']
        if score < 35:
            val = self.__settings.final_result_list[0]
        elif 35 <= score < 60:
            val = self.__settings.final_result_list[1]
        else:
            val = self.__settings.final_result_list[2]
        return val

class StudentRepository:

    def __init__(self, settings: Settings):
        self.__data_generator = DataframeGenerator(settings)
        self.__settings = settings

    def create(self):
        return self.__data_generator.generate()

    def store(self):
        df = self.create()
        df.to_csv(self.__settings.df_path(), index=False)

    def load(self):
        return pd.read_csv(self.__settings.df_path())

    def loadOrCreate(self, override=False):
        if override or not os.path.exists(self.__settings.df_path()):
            self.store()
        return self.load()

class ModelService:

    @staticmethod
    def train_model(x_train, y_train, iterations=100, depth=4, learning_rate=0.1, file_to_save=''):
        model = CatBoostClassifier(iterations=iterations, depth=depth, learning_rate=learning_rate)
        model.fit(x_train, y_train)
        model.save_model(file_to_save)

    @staticmethod
    def preprocess_dataset(
            df,
            to_drop=['final_lab', 'mean_score'],
            y_name='final_lab',
            need_to_encode_y=True,
            need_best_features=True,
            test_size=0.2,
            random_state=1234
    ):
        X = df.drop(columns=to_drop, axis=1)
        y = df[y_name]  # Series

        if need_to_encode_y:
            label_encoder = LabelEncoder()
            temp = label_encoder.fit_transform(y)
            new_result = pd.DataFrame([y, temp], columns=['y', 'encoded_y'])
            new_result.to_csv('ml/result_labels.csv', index=False)
            y = temp

        if need_best_features:
            # 2. Выберите лучшие 3 признака для обучения
            selector = SelectKBest(chi2, k=3)
            X = selector.fit_transform(X, y)

        # 3. Разбейте датасет на тестовую и обучающую выборку train_test_split (для лучших признаков)
        # 80% обучающей выборки, 20% тестовой
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=random_state)

        return X_train, X_test, y_train, y_test

    @staticmethod
    def model_report(model, x_test, y_test, average='weighted'):
        y_pred = model.predict(x_test)
        return {
            'accuracy': accuracy_score(y_test, y_pred),
            'precision': precision_score(y_test, y_pred, average=average),
            'recall': recall_score(y_test, y_pred, average=average),
            'f1': f1_score(y_test, y_pred, average=average)
        }

    @staticmethod
    def load_model(path_to_model=''):
        new_model = CatBoostClassifier()
        new_model.load_model(path_to_model)
        return new_model


# 1. 중앙 지점을 기준으로 재귀적으로 분할(left, right), size==1 일 때 분할 종료
# 2. left 와 right 두 배열값을 비교하면서 merge
#
# 시간복잡도 = O(nlg(n))

n = int(input())
num = [int(input()) for _ in range(n)]

def merge(left_list, right_list):
    left_index = 0
    right_index = 0
    left_size = len(left_list)
    right_size = len(right_list)
    ret = []

    while left_index < left_size and right_index < right_size:
        if left_list[left_index] < right_list[right_index]:
            ret.append(left_list[left_index])
            left_index += 1
        else:
            ret.append(right_list[right_index])
            right_index += 1

    if right_index == right_size:
        while left_index < left_size:
            ret.append(left_list[left_index])
            left_index += 1

    elif left_index == left_size:
        while right_index < right_size:
            ret.append(right_list[right_index])
            right_index += 1

    return ret

def divide(num_list, size):
    if size == 1:
        return num_list
    left = divide(num_list[0:size // 2], size // 2)
    right = divide(num_list[size // 2:], (size + 1) // 2)

    return merge(left, right)

ans = divide(num, n)
for i in range(len(ans)):
    print(ans[i])


def convert(number):

    factors = {3:"Pling",5:"Plang",7:"Plong"}
    drops = []

    for i in factors:
        if number % i == 0:
            drops.append(factors.get(i))

    if not drops:
        return str(number)
    else:
        separator = ''
        return separator.join(drops)


"""empty message

Revision ID: 0770e1a3baea
Revises: 6525b4aac66c
Create Date: 2018-04-13 22:40:38.476991

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '0770e1a3baea'
down_revision = '6525b4aac66c'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('logs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('subject', sa.String(length=30), nullable=False),
    sa.Column('content', sa.Text(), nullable=False),
    sa.Column('level', sa.String(length=20), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('solved_at', sa.DateTime(), nullable=False),
    sa.Column('created_by', sa.Integer(), nullable=True),
    sa.Column('solved_by', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
    sa.ForeignKeyConstraint(['solved_by'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('logs')
    # ### end Alembic commands ###


from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
import logging
from airflow.contrib.hooks.aws_hook import AwsHook
import pandas as pd
import os

class TemperaturePreprocessOperator(BaseOperator):
    """
    This class is to define the pre-processing steps for
    temperature data and puts them to pre_processed_csv folder.
    """

    ui_color = '#89DA59'

    @apply_defaults
    def __init__(self,
                 aws_credentials_id,
                 input_path,
                 output_path,
                 file_ext,
                 *args, **kwargs):

        super(TemperaturePreprocessOperator, self).__init__(*args, **kwargs)
        self.aws_credentials_id = aws_credentials_id
        self.input_path = input_path
        self.output_path = output_path
        self.file_ext = file_ext

    def execute(self, context):
        logging.info('Reading AWS Credentials. ')
        aws_hook = AwsHook(self.aws_credentials_id)
        credentials = aws_hook.get_credentials()

        logging.info('Reading the Global Temperature data in csv format.')
        df = pd.read_csv(self.input_path)

        logging.info('Correcting the data type of date.')
        df['dt'] = df['dt'].astype('datetime64[ns]')

        logging.info('Ectracting year and month columns from date.')
        df['year'] = df['dt'].dt.year
        df['month'] = df['dt'].dt.month

        logging.info('Dropping the dt column.')
        df = df.drop('dt', axis=1)

        # save pre-processed data to csv
        logging.info('Saving the pre-processed file to csv...')

        filename = os.path.basename(self.input_path)
        df.to_csv(os.path.join(self.output_path,filename), index=False)


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

p = GPIO.PWM(12,50)
p.start(0)
print("0")
time.sleep(10)

p.ChangeDutyCycle(3)
print("3")
time.sleep(10)


import torch.utils.data as data
import torchvision.transforms as transforms

import os
import random
import torch

from PIL import Image

from utils import  make_dataset

class BaseDataset(data.Dataset):
    def __init__(self):
        super(BaseDataset, self).__init__()

    def name(self):
        return 'BaseDataset'

    def initialize(self, opt):
        pass

class PairDataset(BaseDataset):
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A')
        self.dir_B = os.path.join(opt.dataroot, opt.phase + 'B')

        self.A_paths = make_dataset(self.dir_A)
        self.B_paths = make_dataset(self.dir_B)

        self.A_paths = sorted(self.A_paths)
        self.B_paths = sorted(self.B_paths)
        self.A_size = len(self.A_paths)
        self.B_size = len(self.B_paths)

        transform_list = []

        transform_list += [transforms.ToTensor(),
                           transforms.Normalize((0.5, 0.5, 0.5),
                                                (0.5, 0.5, 0.5))]
        # transform_list = [transforms.ToTensor()]

        self.transform = transforms.Compose(transform_list)
        # self.transform = get_transform(opt)

    def __getitem__(self, index): # 这个方法如何，何时调用？
        A_path = self.A_paths[index % self.A_size]
        B_path = self.B_paths[index % self.B_size]

        A_img = Image.open(A_path).convert('RGB')
        B_img = Image.open(A_path.replace("low", "normal").replace("A", "B")).convert('RGB')

        A_img = self.transform(A_img)
        B_img = self.transform(B_img)

        w = A_img.size(2)
        h = A_img.size(1)
        # w_offset = random.randint(0, max(0, w - self.opt.fineSize - 1))
        # h_offset = random.randint(0, max(0, h - self.opt.fineSize - 1))

        # A_img = A_img[:, h_offset:h_offset + self.opt.fineSize,
        #         w_offset:w_offset + self.opt.fineSize]
        # B_img = B_img[:, h_offset:h_offset + self.opt.fineSize,
        #         w_offset:w_offset + self.opt.fineSize]

        if self.opt.resize_or_crop == 'no':
            r, g, b = A_img[0] + 1, A_img[1] + 1, A_img[2] + 1
            A_gray = 1. - (0.299 * r + 0.587 * g + 0.114 * b) / 2.
            A_gray = torch.unsqueeze(A_gray, 0)
            input_img = A_img
            # A_gray = (1./A_gray)/255.
        else:

            # A_gray = (1./A_gray)/255.
            if (not self.opt.no_flip) and random.random() < 0.5:
                idx = [i for i in range(A_img.size(2) - 1, -1, -1)]
                idx = torch.LongTensor(idx)
                A_img = A_img.index_select(2, idx)
                B_img = B_img.index_select(2, idx)
            if (not self.opt.no_flip) and random.random() < 0.5:
                idx = [i for i in range(A_img.size(1) - 1, -1, -1)]
                idx = torch.LongTensor(idx)
                A_img = A_img.index_select(1, idx)
                B_img = B_img.index_select(1, idx)
            if (not self.opt.no_flip) and random.random() < 0.5:
                times = random.randint(self.opt.low_times, self.opt.high_times) / 100.
                input_img = (A_img + 1) / 2. / times
                input_img = input_img * 2 - 1
            else:
                input_img = A_img
            r, g, b = input_img[0] + 1, input_img[1] + 1, input_img[2] + 1
            A_gray = 1. - (0.299 * r + 0.587 * g + 0.114 * b) / 2.
            A_gray = torch.unsqueeze(A_gray, 0)
        return {'A': A_img, 'B': B_img, 'A_gray': A_gray, 'input_img': input_img,
                'A_paths': A_path, 'B_paths': B_path}

    def __len__(self):
        return self.A_size

    def name(self):
        return 'PairDataset'

class Structure():
    def __init__(self, opt):
        self.__dict__.update(opt)

def Get_paired_dataset(batch_size):
    data = PairDataset()
    opt = {"dataroot": "../final_dataset", "phase": "train", "resize_or_crop": False, "isTrain": True, "no_flip": True,
           "vary": 1, "lighten": True, "fineSize": 400}
    opt = Structure(opt)
    data.initialize(opt)
    dataloader = torch.utils.data.DataLoader(data, batch_size=batch_size)
    return dataloader


import csv

import openpyxl
import requests

def main():
    data = requests.get('https://www.edraak.org/api/marketing/explore/').json()
    rows = []
    for category, courses in data['courses_by_category'].items():
        for course in courses:
            row = {
                "CourseEN": course['name_en'],
                "CourseAR": course['name_ar'],
                "URL": course['course_url'],
                # "Cost": data['data']['master_course_profile']['master_course_bar_info']['price'],
                "DetailsEN": course['description_en'],
                "DetailsAR": course['description_ar'],
                "Language": course['language'],
                "Platform": "Edraak",
                "Logo": course['cover_image'],
                "Filter": f"{category} > {course['name_en']}".title(),
            }
            rows.append(row)
    for course in data['specializations']:
        row = {
            "CourseEN": course['name_en'],
            "CourseAR": course['name_ar'],
            "URL": f"https://www.edraak.org/{course['lms_url']}",
            # "Cost": data['data']['master_course_profile']['master_course_bar_info']['price'],
            "DetailsEN": course['description_en'],
            "DetailsAR": course['description_ar'],
            "Language": course['language'],
            "Platform": "Edraak",
            "Logo": course['cover_image'],
            "Filter": f"Specialization > {course['name_en']}".title(),
        }
        rows.append(row)
    with open('edraak.csv', 'w',encoding=encoding,newline='') as f:
        c = csv.DictWriter(f, fieldnames=rows[0].keys())
        c.writeheader()
        c.writerows(rows)
    convert('edraak.csv')

encoding = 'utf-8'
def convert(filename):
    wb = openpyxl.Workbook()
    ws = wb.active
    with open(filename, encoding=encoding) as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            ws.append(row)
    wb.save(filename.replace("csv", "xlsx"))

if __name__ == '__main__':
    main()