#!/usr/bin/env python3

import subprocess
import sys
import os
import shutil
import time

TMP_DIR = '/tmp/quine-chameleon'
try: os.mkdir(TMP_DIR)
except: pass

#entry: compile-command, run-command
#see also compile_file() and run_file()
languages = {
  'awk': ['', 'gawk -f {0}'],
  'c': ['gcc -O3 -fsanitize=undefined -pedantic -Wall -Wno-overlength-strings -Werror -std=c99 -o {1} {0}', '{0}'],
  'coffee': ['', 'coffee {0}'],
  'clj': ['', 'clojure {0}'],
  'cpp': ['g++ -O3 -fsanitize=undefined -pedantic -Wall -Werror -std=c++11 -o {1} {0}', '{0}'],
  'cs': ['mcs -out:{1} {0}', 'mono {0}'],
  'd': ['gdc -O3 -Werror -o {1} {0}', '{0}'],
  'el': ['', 'emacs --batch --quick --script {0}'],
  'fs': ['fsharpc --out:{1}.exe {0}', 'mono {0}.exe'],
  'go': ['', 'go run {0}'],
  'hs': ['ghc -O3 -Wall -Werror -o {1} {0}', '{0}'],
  'hx': ['haxe -cp {src_dir} -main QC -neko {1}.n', 'neko {0}.n'],
  'lua': ['', 'lua {0}'],
  'm': ['gcc -O3 -fsanitize=undefined -pedantic -Wall -Wno-overlength-strings -Werror -std=c99 -o {1} {0}', '{0}'],
  'ml': ['', 'ocaml {0}'],
  'java': ['javac {0} -d {dst_dir}', 'java -cp {1} QC'],
  'js': ['', 'nodejs {0}'],
  'octave': ['', 'octave --quiet --norc {0}'],
  'pl': ['', 'perl6 {0}'],
  'py': ['', 'python3 {0}'],
  'R': ['', 'R --slave -f {0} --args'],
  'rb': ['', 'ruby {0}'],
  'rkt': ['', 'racket --no-init-file {0}'],
  'scala': ['scalac {0}', 'scala QC'],
  'vala': ['valac -o {1} {0}', '{0}'],
}

signatures = {}

def get_src_filename(lang):
  return os.path.join('build', 'QC.' + lang)

def compile_file(lang, src_fn, bin_fn, logfile=None):
  compile_cmd = languages[lang][0]
  if compile_cmd:
    subprocess.check_call(compile_cmd.format(src_fn, bin_fn, src_dir=os.path.dirname(src_fn), dst_dir=TMP_DIR), shell=True, stdout=logfile, stderr=logfile)
    return True
  else:
    return False

def run_file(lang, src_fn, bin_fn, target_lang='', logfile=None):
  compile_cmd, run_cmd = languages[lang]
  fn = bin_fn if compile_cmd else src_fn
  return subprocess.check_output(run_cmd.format(fn, os.path.dirname(fn)) + ' ' + target_lang, shell=True, stderr=logfile)

def test(languages_used):
  for lang in languages_used:
    if lang not in languages:
      print('Unknown language: '+lang)
      sys.exit(-1)

  all_good = True
  with open('log.txt', 'w') as logf:
    for lang in languages_used:
      compile_cmd, run_cmd = languages[lang]
      print('Testing ' + lang + ' ...', end='', flush=True)
      src_fn = get_src_filename(lang)
      bin_fn = os.path.join(TMP_DIR, 'qc-bin')

      try:
        compiled = compile_file(lang, src_fn, bin_fn, logf)
      except:
        print('cannot compile!')
        all_good = False
        continue

      all_output_good = True
      if CMD == 'multiquines':
        for target_lang in languages.keys():
          try:
            output = run_file(lang, src_fn, bin_fn, target_lang, logf)
          except:
            print('execution failed with:' + target_lang)
            all_output_good = False
            break
          with open(get_src_filename(target_lang), 'rb') as srcf:
            if output != srcf.read():
              print('output not identical:' + target_lang)
              logf.write('output of ' + lang + ':\n')
              logf.write(output.decode('latin-1') + '\n\n')
              all_output_good = False
              break

      else:
        sorted_langs = sorted(languages.keys())
        repeat = 3 if CMD == 'chameleon' else 1
        for i in range(repeat):
          try:
            output = run_file(lang, src_fn, bin_fn, '', logf)
          except:
            print('execution failed')
            all_output_good = False
            break
          try:
            target_lang = detect_language(output, False)
            if CMD == 'ouroboros':
              i1 = sorted_langs.index(lang)
              i2 = sorted_langs.index(target_lang)
              if i2 != (i1+1)%len(sorted_langs):
                print('output not in alphabetical order: ' + target_lang)
                logf.write('output of ' + lang + ':\n')
                logf.write(output.decode('latin-1') + '\n\n')
                all_output_good=False
                break
          except:
            print('output with unknown language')
            logf.write('output of ' + lang + ':\n')
            logf.write(output.decode('latin-1') + '\n\n')
            all_output_good = False
            break

      if all_output_good: print('ok!')
      else: all_good = False

  if not all_good:
    print('See log.txt for more detail')
    sys.exit(-1)

def detect_language(s, quick_check):
  sig_len = 20
  if len(signatures) == 0:
    for lang in languages:
      with open(get_src_filename(lang), 'rb') as f:
        sig = f.read(sig_len)
        f.seek(-sig_len, 2)
        sig += f.read(sig_len)
        if sig in signatures:
          print('signature is not long enough:',sig_len*2,lang,signatures[sig])
        signatures[sig]=lang

  try:
    lang = signatures[s[:sig_len] + s[-sig_len:]]
    if quick_check:
      return lang
    with open(get_src_filename(lang), 'rb') as f:
      if f.read() != s:
        raise Exception('File content not identical')
    return lang
  except KeyError:
    raise Exception('Cannot detect type')

def run_through(filename, max_iteration):
  stats = {}
  cur_iteration = 0
  with open(filename, 'rb') as f:
    cur_source = f.read()
  bin_fn = os.path.join(TMP_DIR, 'qc-bin')
  with open(os.path.join(TMP_DIR, 'log.txt'), 'w') as logf:
    try:
      while max_iteration == 0 or cur_iteration < max_iteration:
        start_time = time.time()
        filename = os.path.join(TMP_DIR, 'QC')
        try:
          lang = detect_language(cur_source, True)
        except:
          with open(filename, 'wb') as f:
            f.write(cur_source)
          raise
          sys.exit(-1)
        stats[lang] = stats.get(lang,0) + 1
        print(str(cur_iteration) + ': ' + lang)
        filename += '.' + lang
        with open(filename, 'wb') as f:
          f.write(cur_source)
        compile_file(lang, filename, bin_fn, logf)
        cur_source = run_file(lang, filename, bin_fn, '', logf)
        cur_iteration += 1
        # some languages' rand seed is based on seconds, need to refresh it
        while time.time() < start_time + 1:
          time.sleep(0.1)

    except KeyboardInterrupt:
      # we could be interrupted at anywhere, so re-calculate the sum
      total_run = sum(stats.values())
      runs = sorted(stats.items(), key=lambda x:x[1])
      h1 = 'Language'
      h2 = 'Count'
      w1 = max(len(i[0]) for i in runs)
      w1 = max(w1, len(h1))
      w2 = max(len(str(i[1])) for i in runs)
      w2 = max(w2, len(h2))
      print()
      print('Statistics:')
      print('{:{w1}} {:{w2}}     %'.format(h1, h2, w1=w1, w2=w2))
      for l,c in runs:
        print('{:>{w1}} {:^{w2}} {:4.1f}%'.format(l, c, c/total_run*100, w1=w1, w2=w2))
      print()

"""
Usage:

  ./test.py chameleon|ouroboros|random-ouroboros|multiquines [lang1 [lang2 ...]]

    Test the specified mode with specified languages
    Need to prepare the source files first, for language detection (see Makefile)

  ./test.py dance [file [max_iteration]]

    Run the file, execute the output, and repeat
    Need to prepare the source files first, for language detection (see Makefile)

"""

if len(sys.argv) < 2:
  print('need an argument')

CMD = sys.argv[1]
if CMD in ['chameleon', 'ouroboros', 'random-ouroboros', 'multiquines']:
  languages_used = sorted(languages.keys()) if len(sys.argv) == 2 else sys.argv[2:]
  test(languages_used)
elif CMD == 'dance':
  filename = sys.argv[2] if len(sys.argv) > 2 else 'chameleon.py'
  if not os.path.isfile(filename):
    print('Cannot find file: ' + filename)
    sys.exit(-1)
  max_iteration = 0
  if len(sys.argv) > 3:
    try:
      max_iteration = int(sys.argv[3])
    except ValueError:
      print('Bad number:' + sys.argv[3])
      sys.exit(-1)
  run_through(filename, max_iteration)
else:
  print('Unknown argument: ' + CMD)
  sys.exit(-1)


#!/usr/bin/env python3.7
# -*- coding=utf-8 -*-
# for self-exercise
# email:xueatian@cisco.com

def main():

    T = ('a','v','b','c','d')
    d = list(T)
    d.sort()
    T = tuple(d)
    e = sorted(T)
    T1 = tuple(e)

    print(d)
    print (T)
    print(T1)

if __name__ == '__main__':
    main()


import os
import time
import json
import datetime
from slackclient import SlackClient
import pyowm
import nltk
from nltk import word_tokenize, pos_tag, ne_chunk

BOT_NAME = 'weatherbot'

SLACK_BOT_TOKEN = 'SLACK TOKEN'  # got off of slack page
owm = pyowm.OWM('Open Weather Map Token') # available after creating account at https://home.openweathermap.org

slack_client = SlackClient(SLACK_BOT_TOKEN)

api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
    # retrieve all users so we can find our bot
    users = api_call.get('members')
    for user in users:
        if 'name' in user and user.get('name') == BOT_NAME:
            print("Bot ID for '" + user['name'] + "' is " + user.get('id'))
            BOT_ID = user.get('id')
    print('loop completed')
else:
    print("could not find bot user with the name " + BOT_NAME)

# starterbot's ID as an environment variable
#BOT_ID = os.environ.get("BOT_ID")

# constants
AT_BOT = "<@" + BOT_ID + ">"
EXAMPLE_COMMAND = "do"

# instantiate Slack & Twilio clients
slack_client = SlackClient(SLACK_BOT_TOKEN)

def parse_slack_output(slack_rtm_output):
    """
        The Slack Real Time Messaging API is an events firehose.
        this parsing function returns None unless a message is
        directed at the Bot, based on its ID.
    """
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                print('>>>' + str(output))
                return output['text'].split(AT_BOT)[1].strip(), \
                       output['channel']
    return None, None

class StateMachine:

    def __init__(self):
        self.guess_history = []
        self.current_state = InitialState()
        self.READ_WEBSOCKET_DELAY = 1 # 1 second delay

    def run(self):
        if slack_client.rtm_connect():
            print("WeatherBot connected and running!")
            while True:
                command, channel = parse_slack_output(slack_client.rtm_read())
                if command and channel:
                    print('com:' + str(command))
                    print('chan:' + str(channel))
                    self.guess_history.append(command)
                    self.current_state = self.current_state.handle_command(command, channel)
                    if self.current_state == None:
                        print('Quitting')
                        break
                time.sleep(self.READ_WEBSOCKET_DELAY)
        else:
            print("Connection failed. Invalid Slack token or bot ID?")

class State:
    observation = ""
    city = ""
    resp_dict = ""
    def handle_command(self, command, channel):
        assert 0, 'handle command not implemented'

class InitialState(State):

    def __init__(self):
        print('creating InitalState')

    def handle_command(self, command, channel):
        print('InitialState receiving ' + command)

        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(command))):
            print(chunk)
            if hasattr(chunk, 'label'):
                State.city = ' '.join(c[0] for c in chunk.leaves()) # Find the city and return it

        try:
            State.observation = owm.weather_at_place(State.city) # observation is for the current weather only
            return basicWeather.handle_command(self, command, channel) # Go to next state with the weather observation
        except:
            response = ("Sorry, I can't find a city in this sentence: "
                        + command
                        + ". Please rephrase it!")
            slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)
            return InitialState() # Wasn't possible to get a city, return to initial state, offer second try

class basicWeather(State):
    def handle_command(self, command, channel):
        print('BasicWeatherState receiving ' + State.city)

        weather = State.observation.get_weather() # Get the current weather
        weather_beautify = weather.to_JSON()
        State.resp_dict = json.loads(weather_beautify) # Translate JSON object into python diction
        print(State.resp_dict)

        ###
        # Get fields and add description
        ###

        # Basic information
        reference_timestamp = State.resp_dict['reference_time']
        # Temperatures are returned in Kelvin
        status = "Overall status: " + State.resp_dict['status']
        temperature_min_k = float(State.resp_dict['temperature']['temp_min'])
        temperature_avg_k = float(State.resp_dict['temperature']['temp'])
        temperature_max_k = float(State.resp_dict['temperature']['temp_max'])

        # Temperatures in Celsius, rounded to 1/10
        temperature_min_c = round((temperature_min_k - 273.15),1)
        temperature_avg_c = round((temperature_avg_k - 273.15),1)
        temperature_max_c = round((temperature_max_k - 273.15),1)

        # Temperatures in Fahrenheit, rounded to 1/10
        temperature_min_f = round((temperature_min_k * (9/5) - 459.67),1)
        temperature_avg_f = round((temperature_avg_k * (9/5) - 459.67),1)
        temperature_max_f = round((temperature_max_k * (9/5) - 459.67),1)

        # Temperatures Final Output
        temperature_min_out = ("The minimum temperature is: "
                               + str(temperature_min_f)
                               + "F ("
                               + str(temperature_min_c)
                               + "C)")
        temperature_avg_out = ("The average temperature is: "
                               + str(temperature_avg_f)
                               + "F ("
                               + str(temperature_avg_c)
                               + "C)")
        temperature_max_out = ("The maximum temperature is: "
                               + str(temperature_max_f)
                               + "F ("
                               + str(temperature_max_c)
                               +"C)")

        # Turn reference timestamp into readable time
        reference_datetime = (datetime.datetime.fromtimestamp(int(reference_timestamp)))
        current_time = datetime.datetime.now()
        difference = new_time = current_time - reference_datetime
        time_diff_seconds = difference.seconds
        time_out = int(time_diff_seconds / 60)

        #Build response out of given information
        response = ("The current weather in "
                    + State.city
                    + " :\n"
                    + status
                    + "\n"
                    + temperature_min_out
                    + "\n"
                    + temperature_avg_out
                    + "\n"
                    + temperature_max_out
                    + "\n"
                    + "The last time we checked the weather here was "
                    + str(time_out)
                    + " minutes ago. \n"
                    + "Do you want more detailed information? Type @weatherbot yes for more, or @weatherbot no to return!")

        slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True) # Respond with current weather
        return followUp() # Return followUp state, give choice of more info or return to InitialState

class advancedWeather(State):
    def handle_command(self, command, channel):
        print('advancedWeather receiving ' + State.city)

        ###
        # Get fields and add description
        ###

        # Advanced Information
        sunset_timestamp = State.resp_dict['sunset_time']
        sunrise_timestamp = State.resp_dict['sunrise_time']
        reference_timestamp = State.resp_dict['reference_time']
        visibility = State.resp_dict['visibility_distance']
        status_detail = State.resp_dict['detailed_status']
        humidity = State.resp_dict['humidity']
        wind_degree = State.resp_dict['wind']['deg']
        wind_speed = State.resp_dict['wind']['speed']
        pressure = State.resp_dict['pressure']['press']

        # Turn timestamps into readable time
        sunset_datetime = (" sets tonight at: "
                          + datetime.datetime.fromtimestamp(int(sunset_timestamp)).strftime('%H:%M:%S'))
        sunrise_datetime = ("Sun rises today at: "
                           + datetime.datetime.fromtimestamp(int(sunrise_timestamp)).strftime('%H:%M:%S'))
        reference_datetime = (datetime.datetime.fromtimestamp(int(reference_timestamp)))
        current_time = datetime.datetime.now()
        difference = new_time = current_time - reference_datetime
        time_diff_seconds = difference.seconds
        time_out = int(time_diff_seconds / 60)

        # Convert visibility distance to km and mi
        visibility_km = round((visibility / 1000),1)
        visibility_mi = round((visibility * 0.00062137),1)
        visibility_out = ("The current visibility distance is: "
                         + str(visibility_mi)
                         + " miles ("
                         + str(visibility_km)
                         + " km)")
        # Build answer
        response = ("The sun in "
                    + State.city
                    + sunset_datetime
                    + "\n"
                    + sunrise_datetime
                    + "\n"
                    + visibility_out
                    + ". \n"
                    + "The humidity is: "
                    + str(humidity)
                    + "%. \n"
                    + "The wind blows with a speed of: "
                    + str(wind_speed)
                    + " m/s. \n"
                    + "Currently the air pressure is: "
                    + str(pressure)
                    + " hPa. \n"
                    + "Overall: "
                    + status_detail
                    + ". \n"
                    + "The last time we checked was "
                    + str(time_out)
                    + " minutes ago. \n"
                    + "If you want weather information for another city, you can ask me right away!")

        slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)

        State.city = ""
        State.obswervation = ""
        State.resp_dict = ""

        return InitialState() # All done, go back to InitialState and wait for new input

class followUp(State):
    def handle_command(self, command, channel):
        print('FollowUpState receiving ' + command)
        print(State.observation)

        answerYes = ['Yes', 'yes', 'yep', 'yup', 'yea', 'y', 'Yep']
        answerNo = ['No', 'no', 'nope', 'naw', 'Nope', 'n']
        if command in answerYes:
            return advancedWeather()
        if command in answerNo:
            response_no = 'Okay! Do you want to check the weather somewhere else?'
            slack_client.api_call('chat.postMessage', channel=channel, text=response_no, as_user=True)
            return InitialState()
        if (command not in answerYes or command not in answerNo):
            response_unknown = 'Sorry, I did not understand that answer. Can you rephrase that?'
            slack_client.api_call('chat.postMessage', channel=channel, text=response_unknown, as_user=True)
            return followUp()

if __name__ == "__main__":
    weather = StateMachine()
    weather.run()


'''This class is built to handle the concept of a "network." If the requested
scan is just a ping sweep then main calls the ping sweep method, and if a port
scan is requested then this class created a Host object for each address in the
provided network, then calls upon the methods in host to conduct a scan on each
host.'''

import time
import ipaddress
from host import Host
from scan_threader import ScanThreader

class Network:
    '''Host ips is parsed using the clean up function, and hosts is initialized as
    an empty list, to be later populated as a list of Host objects. Similarly, up_hosts
    is initialized to represent the hosts that responded to pings so that Host objects
    aren't created for nonexistent hosts.'''
    def __init__(self, args):
        self.args = args
        self.host_ips = clean_up_ips(args.hosts)
        self.hosts = []
        self.up_hosts = set()

    def scan_hosts(self):
        '''Resets hosts and up_hosts so if the scanning is continuous, repeats
        aren't created, then conducts a ping sweep (if user didn't request to skip
        it) and creates a Host object for all alive hosts, then calls the function
        to conduct port scans, and reports various pieces of useful information
        about the status of the scans'''
        self.hosts = []
        self.up_hosts = set()
        if not self.args.skip:
            print('Initiating ping sweep...')
            if self.args.verbosity >= 1:
                print('Using ping sweep to reduce hosts for port scan.'
                    + 'Use -s or --skip to force port scans on all provided hosts.\n')
            self.ping_sweep()
        else:
            self.up_hosts = self.host_ips
            print('Skipping ping sweep...')

        for addr in self.up_hosts:
            self.hosts.append(Host(addr, self.args))

        if self.up_hosts:
            print(f'Initiating port scan on {len(self.up_hosts)} host(s)...')
            print('-' * 30 + '\n')
            start_time = time.time()

            for host in self.hosts:
                host.scan_host()

            elapsed = time.time() - start_time
            print(f'Scan on {len(self.hosts)} host(s) complete in {elapsed:.2f} seconds\n')

    def ping_sweep(self):
        '''This makes use of the ScanThreader class to push out pings to the hosts
        provided by the user, then reports on status and send the results back'''
        start_time = time.time()
        if self.args.verbosity >= 1 or not self.args.ports:
            print(f'Beginning ping sweep on {len(self.host_ips)} hosts...\n')

        scan = ScanThreader('Ping Sweep', self.host_ips, self.args.num_threads, self.args.verbosity)
        self.up_hosts = scan.scan()

        elapsed = time.time() - start_time
        return_str = f'\nPing sweep complete on {len(self.host_ips)} host\n'
        if self.up_hosts:
            for host in self.up_hosts:
                return_str += f'[+] Host {host} is up\n'
        else:
            return_str += 'All hosts scanned are either down or not responding to pings\n'

        if self.args.verbosity >= 1 and self.args.ports:
            print(f'Scanned {len(self.host_ips)} host(s) in {elapsed:.2f} seconds\n {"-" * 30}\n')
            print('-' * 30 + '\n')
            print(return_str)

        return return_str

    def __str__(self):
        '''Turn information about the network into pretty formatting'''
        return_str = ''
        if self.up_hosts:
            return_str += f'{len(self.host_ips)} host(s) scanned:\n'
        else:
            return_str += 'No hosts detected, cancelling port scan...\n'
        return_str += '-' * 30 + '\n'
        for each_host in self.hosts:
            return_str += str(each_host)
        return return_str

def clean_up_ips(input_ips):
    '''This function is used recursively to parse user input for ips into a usable format.
    A set is used so that duplicates can't be added to the final list and therefore unecessarily
    scanned twice. On each loop the item in the set is searched for a comma, dash, or backslash,
    and if any are found then it's split on the comma, or the range is expanded, or
    a CIDR network is provided using the ipaddress libary. IF any of these indicators are found,
    then a subset is created and recursively sent through the function until finally no items in
    the list flag on any of these indicators and the recursion folds into itself back up to the
    original function call'''
    if isinstance(input_ips, str):
        input_ips = set([input_ips])

    output_ips = set()
    for host in input_ips:
        if ',' in host:
            for item in host.split(','):
                output_ips.update(clean_up_ips(item))
        elif '-' in host:
            octets = host.split('.')
            subnet_request = False
            for octet in octets:
                if '-' in octet:
                    index = octets.index(octet)
                    start, end = octet.split('-')
                    if '/' in end:
                        end, subnet = end.split('/')
                        subnet_request = True
                    octet_range = range(int(start), int(end)+1)
            for octet in octet_range:
                octets[index] = (str(octet) + '/' + subnet) if subnet_request else octet
                output_ips.update(clean_up_ips(f'{octets[0]}.{octets[1]}.{octets[2]}.{octets[3]}'))
        elif '/' in host:
            for addr in ipaddress.IPv4Network(host, strict=False):
                output_ips.add(str(addr))
        else:
            output_ips.add(host)
    return output_ips


import numpy as np
import torch
from torch import nn

# Function to apply the solver method
def euler(f, x0, t0, tf, dt, u, *kargs):

    """
    Solve a system of differential equations using the Euler method.

    Parameters:
    f (function): The derivative function.
    x0 (ndarray): The initial state.
    t0 (float): The initial time.
    tf (float): The final time.
    dt (float): The time step.
    u (function): The input function.

    Returns:
    ndarray: The state at each time step.
    """
    t = np.arange(t0, tf, dt)
    x = np.zeros((len(t), len(x0)))
    x[0, :] = x0
    for i in range(1, len(t)):
        # print(x[i,:])
        x[i, :] = x[i-1, :] + dt * f(x[i-1, :], t[i-1], u(t[i-1]), *kargs)
    return x

#@title solver
class SolverTemplate(nn.Module):
    def __init__(self, order, min_factor: float = 0.2, max_factor: float = 10, safety: float = 0.9):
        super().__init__()
        self.order = order
        self.min_factor = torch.tensor([min_factor])
        self.max_factor = torch.tensor([max_factor])
        self.safety = torch.tensor([safety])
        self.tableau = None

    def sync_device_dtype(self, x, t_span):
        "Ensures `x`, `t_span`, `tableau` and other solver tensors are on the same device with compatible dtypes"

        if isinstance(x, dict):
            proto_arr = x[list(x.keys())[0]]
        elif isinstance(x, torch.Tensor):
            proto_arr = x
        else:
            raise NotImplementedError(
                f"{type(x)} is not supported as the state variable")

        device = proto_arr.device

        if self.tableau is not None:
            c, a, bsol, berr = self.tableau
            self.tableau = c.to(proto_arr), [a.to(proto_arr) for a in a], bsol.to(
                proto_arr), berr.to(proto_arr)
        t_span = t_span.to(device)
        self.safety = self.safety.to(device)
        self.min_factor = self.min_factor.to(device)
        self.max_factor = self.max_factor.to(device)
        return x, t_span

    def step(self, f, x, t, dt, k1=None, args=None):
        pass

class DirectSolver(SolverTemplate):
    def __init__(self, dtype=torch.float32):
        super().__init__(order=1)
        self.dtype = dtype
        self.stepping_class = "fixed"

    def step(self, f, x, t, dt, k1=None, args=None):

        x_sol = f(t, x)
        return None, x_sol, None

class ResnetSolver(SolverTemplate):
    def __init__(self, step_size=None, dtype=torch.float32):
        super().__init__(order=1)
        self.dtype = dtype
        self.stepping_class = "fixed"

        self.step_size = 1 if step_size is None else step_size

    def step(self, f, x, t, dt, k1=None, args=None):

        x_sol = x + f(t, x) * self.step_size
        return None, x_sol, None


from typing import Any, Optional, List
from app.dependencies.auth import TokenUser

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy import String, bindparam
from sqlalchemy.orm import Session
from app.db.deps import get_db
from app.db.utils import save_model
from app.dependencies.auth import JWTService, credential_check
from app.models.user import User, UserIn, UserOut

router = APIRouter()

@router.get("/user", response_model=UserOut)
async def get_user(db: Session = Depends(get_db),user : TokenUser = Depends(credential_check)):
    user = db.query(User).filter(User.id == user.id).one_or_none()
    return user

@router.get("/", response_model=List[UserOut])
async def get_users(
    search: Optional[str] = None,
    limit: Optional[int] = 100,
    skip: Optional[int] = 0,
    db: Session = Depends(get_db),
):
    db_result = db.query(User)

    if search:
        db_result = db_result.filter(User.name.like(bindparam("search", type_=String))).params(search=f"%{search}%")

    return db_result.limit(limit).offset(skip).all()

class JwtResponse(BaseModel):
    jwt: str

@router.post(
    "/login",
    response_model=JwtResponse,
)
def post_user(user: UserIn, db: Session = Depends(get_db)):

    model = db.query(User).filter(User.username == user.username).one_or_none()
    if model == None:
        model = save_model(db, User(username=user.username))

    token = JWTService().encode({"id": str(model.id)})

    return {"jwt": str(token)}


# -*- coding:utf-8 -*-
import os
import random
import string

if __name__ == '__main__':
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_add_rbac.settings")
    import django
    django.setup()

    # 插入数据
    from blog import models

    category_choices = [4,2,3]
    dates = ['2011-12-12','2012-3-5','2018-12-5']
    blog_lst = [1,2,3]

    lst = []
    # 插入5个数据
    for i in range(7):

        random_title = ''.join(random.choices(string.ascii_letters, k=5))

        random_category = random.choice(category_choices)

        random_content = ''.join(random.choices(string.ascii_letters, k=9))

        random_date = random.choice(dates)

        random_blog = random.choice(blog_lst)

        customer_obj = models.Article(title=random_title,category=random_category,content=random_content,create_at=random_date,blog_id=random_blog)
        lst.append(customer_obj)

    models.Article.objects.bulk_create(lst)


from .predictor import Predictor
from dacapo.experiments import Model
from dacapo.experiments.arraytypes import EmbeddingArray
from dacapo.experiments.datasplits.datasets.arrays import NumpyArray

import numpy as np
import torch

class DummyPredictor(Predictor):
    def __init__(self, embedding_dims):
        self.embedding_dims = embedding_dims

    def create_model(self, architecture):
        head = torch.nn.Conv3d(
            architecture.num_out_channels, self.embedding_dims, kernel_size=3
        )

        return Model(architecture, head)

    def create_target(self, gt):
        # zeros
        return NumpyArray.from_np_array(
            np.zeros((self.embedding_dims,) + gt.data.shape[-gt.dims :]),
            gt.roi,
            gt.voxel_size,
            ["c"] + gt.axes,
        )

    def create_weight(self, gt, target, mask, moving_class_counts=None):
        # ones
        return (
            NumpyArray.from_np_array(
                np.ones(target.data.shape),
                target.roi,
                target.voxel_size,
                target.axes,
            ),
            None,
        )

    @property
    def output_array_type(self):
        return EmbeddingArray(self.embedding_dims)


# Third party libs
import unittest
# Custom libs
from tests.test_globals import ROOT_DIR
from exchange_parsers.bolsa_de_santiago import get_tickers_from_pdf, clean_line

class TestChileParser(unittest.TestCase):

    def test_clean_line_basic(self):
        title = 'ACCIONES, CFI Y CFM AUTORIZADAS DESDE EL 03.04.2023 '
        sub_title = 'ACCIONES >=25% '
        header = 'Nº  ACCIÓN '
        ticker_one = 'AGUAS-A '
        number_one = '1 '
        ticker_two = 'ANTARCHILE '
        number_two = '5 '
        ticker_three = '38  MALLPLAZA '

        self.assertIsNone(clean_line(title))
        self.assertIsNone(clean_line(sub_title))
        self.assertIsNone(clean_line(header))
        self.assertEqual('AGUAS-A', clean_line(ticker_one))
        self.assertIsNone(clean_line(number_one))
        self.assertEqual('ANTARCHILE', clean_line(ticker_two))
        self.assertIsNone(clean_line(number_two))
        self.assertEqual('MALLPLAZA', clean_line(ticker_three))

    def test_get_tickers_from_pdf(self):
        expected_tickers = ['AGUAS-A', 'ALMENDRAL', 'ANDINA-A', 'ANDINA-B', 'ANTARCHILE', 'BCI', 'BESALCO', 'BICECORP', 'BLUMAR', 'BSANTANDER',
                            'CAP', 'CCU', 'CENCOSHOPP', 'CENCOSUD', 'CHILE', 'CMPC', 'COLBUN', 'CONCHATORO', 'COPEC', 'ECL', 'EMBONOR-B', 'ENELAM',
                            'ENELCHILE', 'ENELGXCH', 'ENJOY', 'ENTEL', 'FALABELLA', 'IAM', 'ILC', 'INDISA', 'INGEVEC', 'INVERCAP', 'ITAUCORP',
                            'FORUS', 'HF', 'HITES', 'LTM', 'MALLPLAZA', 'MASISA', 'MULTI X', 'NORTEGRAN', 'ORO BLANCO', 'PARAUCO', 'QUINENCO',
                            'RIPLEY', 'SALFACORP', 'SECURITY', 'SK', 'SMSAAM', 'SMU', 'SONDA', 'SQM-A', 'SQM-B', 'VAPORES', 'AAISA', 'AESANDES',
                            'BANVIDA', 'CAMANCHACA', 'CINTAC', 'CRISTALES', 'EISA', 'ENAEX', 'GASCO', 'HABITAT', 'LAS CONDES', 'LIPIGAS',
                            'MANQUEHUE', 'MINERA', 'NAVIERA', 'NITRATOS', 'NUEVAPOLAR', 'PASUR', 'PAZ', 'PUCOBRE', 'SALMOCAM', 'SOCOVESA',
                            'SOQUICOM', 'TRICOT', 'WATTS', 'ZOFRI']
        actual_tickers = get_tickers_from_pdf(f'{ROOT_DIR}/tests/resources/chile/short_selling_list_chile.pdf')

        expected_dict = {
            'CENCOSHOPP': {
                'short_sell': 'Y'
            }
        }

        self.assertEqual(expected_tickers, list(actual_tickers.keys()))
        self.assertDictEqual(expected_dict['CENCOSHOPP'], actual_tickers['CENCOSHOPP'])


from libcommon.octavo_api_client import (
    OctavoEccoClient
    )
from libcommon.metadata_readers import (
    load_good_metadata
    )
# from nltk.tag.stanford import StanfordPOSTagger
from nltk.tokenize import word_tokenize
# from nltk.stem.porter import PorterStemmer
# from nltk.stem import LancasterStemmer
from nltk.stem import SnowballStemmer
import random
# import sys
import csv
import re
from collections import OrderedDict

def get_doctext_tokens(doctext):
    text_tokens = word_tokenize(doctext)
    return text_tokens

def get_stemmed_tokens(doctext_tokens, stemmer):
    stemmed_tokens = []
    for token in doctext_tokens:
        stemmed_tokens.append(stemmer.stem(token))
    return stemmed_tokens

def get_stemmed_token(token, stemmer):
    stemmed_token = (stemmer.stem(token))
    return stemmed_token

def get_stemmed_baseline(wordfile):
    with open(wordfile) as word_file:
        english_words = set(word.strip().lower() for word in word_file)
    stemmed_baseline = set()
    for word in english_words:
        stemmed_baseline.add(stemmer.stem(word))
    return stemmed_baseline

def get_stemmed_baseline2(wordfile):
    with open(wordfile) as word_file:
        english_words = (
            set(word.split("\t")[0].strip().lower() for word in word_file))
    stemmed_baseline = set()
    for word in english_words:
        stemmed_baseline.add(stemmer.stem(word))
    return stemmed_baseline

def evaluate_text_ocr(doctext, baseline_set, stemmer, min_length=3):
    retdict = {'recognised': [], 'unrecognised': []}
    tokens = get_doctext_tokens(doctext)
    for i in range(0, len(tokens)):
        token = tokens[i]
        if len(token) < min_length:
            continue
        if re.search('[a-zA-Z]', token) is None:
            continue
        stemmed_token = get_stemmed_token(token, stemmer)
        if stemmed_token in baseline_set:
            retdict['recognised'].append(token)
        else:
            retdict['unrecognised'].append(token)
    return retdict

def get_metadata_yearsubset(good_metadata, year):
    results_metadata = {}
    for key, value in good_metadata.items():
        if value.get('estc_publication_year') == str(year):
            results_metadata[key] = value
    return results_metadata

def get_metadata_langsubset(good_metadata, lang):
    results_metadata = {}
    for key, value in good_metadata.items():
        if value.get('estc_language') == lang:
            results_metadata[key] = value
    return results_metadata

def get_metadata_sample(metadata, sample_size):
    metadata_list = list(metadata.values())
    if len(metadata_list) < sample_size:
        sample_size = len(metadata)
    random_sample = random.sample(metadata_list, sample_size)
    return random_sample

def write_wordset(words, outputfile):
    output = set(words)
    outdict = {}
    for word in output:
        outdict[word] = words.count(word)
    sorteddict = OrderedDict(
        sorted(outdict.items(), key=lambda t: t[1], reverse=True))
    with open(outputfile, 'w') as outfile:
        csvwriter = csv.writer(outfile)
        for key, value in sorteddict.items():
            csvwriter.writerow([key, value])
    # with open(outputfile, 'w') as outfile:
    #     for word in output:
    #         outfile.write(word + "\n")

def get_lensums(wordlist):
    words = wordlist
    lens = []
    for word in words:
        lens.append(len(word))
    max_len = max(lens)
    lensums = {}
    for word_length in range(2, max_len + 1):
        lensums[word_length] = lens.count(word_length)
    return lensums

def get_yearly_results(good_metadata, year, sample_size, eccoclient):
    year_metadata_subset = get_metadata_yearsubset(
        good_metadata, year)
    year_metadata_subset = get_metadata_langsubset(
        year_metadata_subset, "English")
    year_metadata_sample = get_metadata_sample(
        year_metadata_subset, sample_size)

    yearly_sample_eccoids = []
    for item in year_metadata_sample:
        yearly_sample_eccoids.append(item.get('ecco_id'))

    print("Fetching n eccoids: " + str(len(yearly_sample_eccoids)))

    yearly_results_by_doc = []
    for docid in yearly_sample_eccoids:
        doctext = eccoclient.get_text_for_document_id(docid).get('text')
        text_evaluated = evaluate_text_ocr(doctext, stemmed_baseline, stemmer)
        good_words = text_evaluated.get('recognised')
        bad_words = text_evaluated.get('unrecognised')
        ratio = len(good_words) / (len(good_words) + len(bad_words))
        yearly_results_by_doc.append({'rec_words': good_words,
                                      'unk_words': bad_words,
                                      'good_ratio': ratio})

    rec_words = []
    unk_words = []
    print("Combining yearly results.")
    for yearly_result in yearly_results_by_doc:
        rec_words.extend(yearly_result.get('rec_words'))
        unk_words.extend(yearly_result.get('unk_words'))
    ratio = len(rec_words) / (len(rec_words) + len(unk_words))

    rec_words_lensum = get_lensums(rec_words)
    unk_words_lensum = get_lensums(unk_words)

    yearly_results_all = {'rec_words': rec_words,
                          'unk_words': unk_words,
                          'ratio': ratio,
                          'rec_sums': rec_words_lensum,
                          'unk_sums': unk_words_lensum}
    return yearly_results_all

def write_yeartable_header(yeartable_csvfile):
    with open(yeartable_csvfile, 'w') as csvfile:
        csvwriter = csv.writer(csvfile)
        header_row = ['year', 'ratio', 'rec', 'unk', ]
        rec_len_list = []
        unk_len_list = []
        for i in range(2, 51):
            rec_len_list.append("rec-" + str(i))
            unk_len_list.append("unk-" + str(i))
        header_row.extend(rec_len_list)
        header_row.extend(unk_len_list)
        csvwriter.writerow(header_row)

# script start

eccoclient = OctavoEccoClient()
# stemmer = PorterStemmer()
# stemmer = LancasterStemmer()
stemmer = SnowballStemmer('english')
# wordfile = "data/wordsEn.txt"
# wordfile = "data/words.txt"
wordfile = "data/count_1w.txt"
# http://norvig.com/ngrams/
stemmed_baseline = get_stemmed_baseline2(wordfile)
good_metadata = load_good_metadata(
    "../data-own-common/good_metadata.json")
yeartable_csvfile = "output/allyears_newest.csv"
start_new = True
yearly_sample_size = 1000000
years_to_evaluate = (list(range(1700, 1799)))

if start_new:
    write_yeartable_header(yeartable_csvfile)

for year in years_to_evaluate:
    print("processing year: " + str(year))
    yearly_results = get_yearly_results(
        good_metadata, year, yearly_sample_size, eccoclient)

    with open(yeartable_csvfile, 'a') as csvfile:
        csvwriter = csv.writer(csvfile)
        outrow = [year,
                  yearly_results.get('ratio'),
                  len(yearly_results.get('rec_words')),
                  len(yearly_results.get('unk_words')),
                  ]
        rec_len_list = []
        unk_len_list = []
        for i in range(2, 51):
            rec_len = yearly_results.get('rec_sums').get(i)
            if rec_len is None:
                rec_len = 0
            rec_len_list.append(rec_len)
            unk_len = yearly_results.get('unk_sums').get(i)
            if unk_len is None:
                unk_len = 0
            unk_len_list.append(unk_len)
        outrow.extend(rec_len_list)
        outrow.extend(unk_len_list)
        csvwriter.writerow(outrow)

    print("finished with year: " + str(year))
    # print("Writing word totals for year " + str(year))
    outfile = "output/" + str(year)
    # write_wordset(yearly_results['rec_words'], (outfile + "-rec.txt"))
    # write_wordset(yearly_results['unk_words'], (outfile + "-unk.txt"))

# rec_words_outfile = "output/recset.txt"
# write_wordset(yearly_results.get('rec_words'), rec_words_outfile)
# unk_words_outfile = "output/unkset.txt"
# write_wordset(yearly_results.get('unk_words'), unk_words_outfile)

# docid = "0145100106"
# doctext = eccoclient.get_text_for_document_id(docid).get('text')

# print("good: " + str(len(good_words)))
# print("bad:  " + str(len(bad_words)))


from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
import newgen.models as mo
from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.forms.models import model_to_dict
from django.contrib import admin

#from top.objects import *
from newgen.tools import *
from newgen.functions import *
from newgen.background_tasks import *
#from top.open_func import *
from django.contrib.auth.models import Group

from django.template.loader import render_to_string
from django.db.models import Q

from cryptography.fernet import Fernet
cipher_key = Fernet.generate_key()
cipher = Fernet(cipher_key)

@login_required
def mainworker(request):

	if request.user.is_superuser:
		return HttpResponseRedirect(reverse('admin:index'))
	if str(request.user) == 'tools':
		return HttpResponseRedirect(reverse('tools'))
	if request.user.groups.first().id == 3:
		return HttpResponseRedirect(reverse('it'))

	user = request.user
	objects = []
	orig_notes =[]
	shedule_list = []

	#print(request.user.groups.first().id)
	#1 - ГРУППА БРИГАДИРОВ

	workers = engineers = GetGroupMembers("Монтажники")
	worker = GetWorker(request)
	history = GetWorkersResults(worker.worker_id)
	#print(request)
	#TOOL NOTE
	for tool_note in mo.ToolsReport.objects.filter(~Q(worker_id = worker.worker_id)):
		orig_notes.append(tool_note)

	#OBJECTS
	for objectx in mo.Object.objects.filter(is_active = True):
		objects.append(objectx.name)

	#SHEDULES
	shedules = mo.Shedule.objects.filter(date__gte = datetime.date.today())

	if shedules.exists():
		for shedule in shedules:
			if str(shedule.leader) == str(worker.fullname):
				shedule_list.append(shedule)
				continue
			if worker in shedule.workers.all():
				shedule_list.append(shedule)

	return render(request,"mainworker.html",{'objects': objects,'history':history, 'worker':worker,
		'user': user, 'workers': workers, 'orig_notes':orig_notes,
	  'shedule':shedule_list})

def notice_work(request):
	workers=[]

	#3 fields + token + object
	workers_to_notice = round((len(dict(request.POST.lists()))-2)/3)

	object_name = GetObject(request,"object")

	for i in range(workers_to_notice):
		worker_short_name = GetObject(request,"worker_" + str(i+1))
		time = GetObject(request,"time_" + str(i+1))
		comment = GetObject(request,"comment_" + str(i+1))
		worker = GetWorkerByName(worker_short_name)
		obj = GetFirstFilteredNote(mo.Object,"name",object_name)

		AddWorkNote(worker, obj, time, comment)
		AddFullReport(worker.fullname, obj.name, time)

	#bg_task()
	messages.success(request, "Всё записано, спасибо")
	return HttpResponseRedirect("../")

def tool_transfers(request):

	user = GetFirstFilteredNote (User, "username",request.user)
	workername = Worker.objects.filter(user = user.id).first().fullname

	try:
		temp_transfers_id = request.POST.getlist(workername)
		for note_id in temp_transfers_id:
			temp_note = GetFirstFilteredNote(mo.TempToolTransfers,"note_id",note_id)
			if request.POST.get(note_id) == "+":
				try:
					RefreshToolReportNote(temp_note.tool_name,temp_note.worker_id)
					AddToolForward(temp_note.tool_name,temp_note.worker_id,"")
				except Exception as e:
					print(e)
			temp_note.delete()
	except Exception as e:
		messages.warning(request,"Гавнина с обновой записей")
		return HttpResponseRedirect("../")
	messages.success(request, 'Данные по оборудованию обновлены')
	return HttpResponseRedirect("../")

def edit_tool(request):
	tool = GetTool(request.POST.get("tool"))
	if tool is None:
		messages.warning(request, "Такого инструмента нет")
		return HttpResponseRedirect("../")
	new_tool_name = request.POST.get("new_tool_name")
	try:
		tool.name = request.POST.get("new_tool_name")
		tool.save()
		messages.success(request, "Название изменено")
	except Exception as e:
		print(e)
		messages.warning(request,e)
		return HttpResponseRedirect("../")
	return HttpResponseRedirect("../")

def delete_tool(request):
	if DeleteTool(request.POST.get("tool")):
		messages.success(request, "Инструмент удалён из БД")
	else:
		messages.warning(request, "Что-то не то")
	return HttpResponseRedirect("../")

def tools(request):
	worker = GetWorker(request)
	tools =[]
	workers = []
	tools_notes = []
	transfers = []
	temp_transfers = []
	orig_notes =[]
	#TOOLS
	for tool in mo.Tools.objects.all():
		if  not mo.TempToolTransfers.objects.filter(tool_id = tool.tool_id).exists():
			tools.append(tool)

	#WORKER
	for another_worker in mo.Worker.objects.all():
		workers.append(another_worker)

	#TOOL NOTE
	for tool_note in mo.ToolsReport.objects.all():
		if str(tool_note.worker_id) != str(worker.fullname):
			orig_notes.append(tool_note)
		tools_notes.append(tool_note)
	#TRANSFERS
	for transfer in mo.ToolsForwarding.objects.order_by('-note_id'):
		transfers.append(transfer)

	#TEMP TRANSFERS
	for temp_transfer in mo.TempToolTransfers.objects.all():
		temp_transfers.append(temp_transfer)

	return render(request,"tools/tools.html",{'tools_notes': tools_notes, 'tools': tools, 'workers': workers,
	 'transfers':transfers, 'temp_transfers':temp_transfers, 'orig_notes':orig_notes})

def add_tool(request):
	if 'submit' in request.POST:
		print(111)
	if AddTool(request.POST.get("tool_name")):
		AddToolReportNote(request.POST.get("tool_name"))
		messages.success(request, "Новый инструмент добавлен")
	else:
		messages.warning(request,"Инструмент с таким названием уже есть")
	return HttpResponseRedirect("../")

def tool_forwarding(request):
	try:
		user = User.objects.filter(username = request.user).first()
		worker = Worker.objects.filter(user = user.id).first()
		tool = Tools.objects.filter(name = request.POST.get("tool")).first()
		comments = request.POST.get("comments")
		AddToolForward(tool, worker, comments)
		RefreshToolReportNote(tool, worker)
		messages.success(request, "Вы взяли инструмент")
	except Exception as e:
		print(e)
		messages.warning(request, "Инструмент не был оформлен на Вас")
	if str(request.user) == "tools":
		AddTempToolTransfer(tool, worker, comments)
	return HttpResponseRedirect("../")

def delete_temp_note(request):
	user = User.objects.filter(username = request.user).first()
	tool = mo.Tools.objects.filter(name = request.POST.get("tool_name")).first()
	worker = mo.Worker.objects.filter(fullname = request.POST.get("worker_name")).first()
	print(request)
	try:
		mo.ToolsForwarding.objects.filter(worker_id = worker.worker_id, tool_name = tool.tool_id).first().delete()
		mo.TempToolTransfers.objects.filter(worker_id = worker.worker_id, tool_id = tool.tool_id).first().delete()
		messages.success(request,"Передача отменена")
	except Exception as e:
		messages.error(request,str(e) +" temp")

	#ВЕРНУТЬ ПРОШЛОГО ХОЗЯИНА
	old_transfer = mo.ToolsForwarding.objects.filter(tool_name = tool.tool_id).first()
	old_worker = mo.Worker.objects.filter(fullname = old_transfer.worker_id).first()
	RefreshToolReportNote(tool,old_worker)
	return HttpResponseRedirect("../")

####################################################################################################################
def organisations_requests(request):
	if not request.session or not request.session.session_key:
		request.session.save()
	statuses = []
	for status in mo.UrgencyStatus.objects.all().order_by('-status_id'):
		statuses.append(status.status)
	return render(request,"organisations_requests.html",{'statuses':statuses})

def new_request(request):
	AddNewRequestNote(request)
	return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

def confirm_email(request):
	code = GetObject(request,"code")
	verification_obj =  GetFirstFilteredNote(EmailVerification,"activation_code",code)

	if verification_obj == None:
		messages.error(request, "Недействительный ключ активации")
	else:
		if datetime.date.today() > verification_obj.expiry_date:
			messages.error(request, "Данный ключ больше не активен")
		else:

			#ACTIVATE REQUEST
			related_request = mo.OpenRequest.objects.filter(note_id = verification_obj.open_request.note_id).first()
			related_request.status =  GetFirstFilteredNote(RequestStatus,"status_id",1)
			related_request.save()

			#ACTIVATE EMAIL
			related_email =GetFirstFilteredNote(mo.Email,"email",verification_obj.email)
			related_email.is_active = True
			related_email.save()

			#DELETE CODE
			GetFirstFilteredNote(EmailVerification,"activation_code",code).delete()
			messages.success(request, "Ваш Email подтверждён")

	return render(request,"inserting_auth_code.html")

def inserting_auth_code(request):
	return render(request,"inserting_auth_code.html")

def it(request):

	engineers = GetGroupMembers("Инженеры")
	active_requests = []
	accepted_requests = []

	for closed_request in ClosedRequest.objects.filter(is_accepted = True):
		accepted_requests.append(closed_request)

	for active_request in mo.OpenRequest.objects.filter(status = 1):
		if not len(ClosedRequest.objects.filter(open_request = active_request.note_id)):
			active_requests.append(active_request)

	return render(request,"it/it.html",{'active_requests':active_requests, 'engineers':engineers,'accepted_requests':accepted_requests})

def add_temp_close_request(request):
	open_request_id = GetObject(request,"request_id")
	photos = GetPhotos(request)
	responsible_list = request.POST.getlist("responsible")
	comment = GetObject(request,"comment")
	close_request_id = CloseRequest(open_request_id,responsible_list,photos,comment)
	messages.success(request, "Всё отправлено, спасибо")
	#encode id -> str -> bytes -> str -> remove "==" -> send email
	encrypted_text = cipher.encrypt(f'{close_request_id}'.encode('utf-8'))
	request_is_closed(close_request_id,encrypted_text.decode('utf-8').split("==")[0])

	return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

def job_evaluation(request, encrypted_text):
	#get decrypted request id from request url^ ****'/****/content' and add '=='
	text = str(request.build_absolute_uri).split("'")[1].split("/")[2]
	text = text +"=="
	try:
		decoded_id = cipher.decrypt(text.encode('utf-8')).decode('utf-8')
	except:
		return HttpResponseRedirect(reverse('unknown_page'))
	#ЕСЛИ ЗАЯВКА УЖЕ ОБРАБОТАНА
	closed_request_note = GetFirstFilteredNote(ClosedRequest,"note_id",decoded_id)
	try:
		if closed_request_note.is_accepted is False and not (closed_request_note is None):
			open_request_note = closed_request_note.open_request
			return render(request,"it/job_evaluation.html",{"closed_note":closed_request_note,"opened_note":open_request_note})
		else:
			return HttpResponseRedirect(reverse('organisations_requests'))
	except:
		return HttpResponseRedirect(reverse('unknown_page'))

def evaluation(request):

	result = GetObject(request,"result")
	client_comment = GetObject(request,"client_comment")
	request_id = GetObject(request,"close_request_id")
	close_note = GetFirstFilteredNote(ClosedRequest,"note_id",request_id)
	close_note.client_comment = client_comment
	if result == "Принять":
		close_note.is_accepted = True
		close_note.save()
	else:
		close_note.delete()
		AddDeclinedRequest(close_note.open_request,close_note.responsible, close_note.client_comment)
	messages.success(request, "Всё отправлено, спасибо")
	return HttpResponseRedirect(reverse('organisations_requests'))

def unknown_page(request):
	return render(request,"404/404.html")


import serial
import requests
from bs4 import BeautifulSoup

form_url = str(input('Enter URL to Google Form: '))
serial_port = str(input('Enter your Arduino serial port address: '))
data_rate_baud = str(input('Enter your data rate from Arduino in baud: '))

def get_entry():
    page = requests.get(form_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    return (soup.input['name'])

def get_form_response():
    page = requests.get(form_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    return (soup.form['action'])

# print(get_formResponse())
# print(get_entry())

entry = get_entry()
formResponse_url = get_form_response()

url = formResponse_url
form_data = {entry: '', 'draftResponse':[], 'pageHistory':0}
user_agent = {'Referer': form_url, 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0"}

ser = serial.Serial(serial_port, data_rate_baud)

n = 0
while n < 10:
    data = float(ser.readline())
    form_data[entry] = str(data)
    print("add datapoint", data, "to google forms")
    r = requests.post(url, data=form_data, headers=user_agent)
    n = n+1


def names():
    f = open('names.txt', 'r')
    dic = {}
    for nomes in f:
        nomes = nomes.split()
        lname= nomes[-1].strip()
        fname = nomes[0].strip()
        if lname not in dic:
            dic[lname] = set()
        dic[lname].add(fname)

    for i in dic:
        print(i,':', dic[i])

print(names())


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.linear_model import LinearRegression,LogisticRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split,KFold,GridSearchCV,cross_val_score
from sklearn.metrics import accuracy_score,confusion_matrix,r2_score,mean_squared_error,precision_recall_curve,f1_score,auc,roc_auc_score,roc_curve,make_scorer
from sklearn.externals import joblib

np.random.seed(9)

train_data=pd.read_csv('Data/AdvWorksCusts.csv')
print('\t\t\tCustomer Spending Beheviour Prediction\n\n\t\tTraining Data\n',train_data.head(),'\n\n\t\tData Information\n')
train_data.info()

label_data1=pd.read_csv('Data/AW_AveMonthSpend.csv')
label_data2=pd.read_csv('Data/AW_BikeBuyer.csv')

train_data.drop_duplicates(subset='CustomerID',keep='last',inplace=True)
label_data1.drop_duplicates(subset='CustomerID',keep='last',inplace=True)
label_data2.drop_duplicates(subset='CustomerID',keep='last',inplace=True)

label1=label_data1['AveMonthSpend']
label2=label_data2['BikeBuyer']

train_data['age']=(train_data['BirthDate'].apply(lambda x:x[:4])).apply(lambda x:2019-np.int64(x))

train_data.drop(columns=['CustomerID','Title','FirstName','MiddleName','LastName','City','StateProvinceName','Suffix','AddressLine2','AddressLine1','PostalCode','PhoneNumber','BirthDate'],inplace=True)

num_col=['YearlyIncome','age']
cat_col=['CountryRegionName','Education','Occupation','Gender','MaritalStatus','HomeOwnerFlag','NumberCarsOwned','NumberChildrenAtHome','TotalChildren']

def vizual(data,columns,label=label2,type='bar'):
    if type=='bar':
        for col in columns:
            sns.countplot(y=data[col])
            plt.show()
    if type=='hist':
        for col in columns:
            sns.distplot(data[col],bins=30)
            plt.show()
    if type=='scatter':
        for col in columns:
            sns.scatterplot(col,label,data=data,s=10)
            plt.show()
    if type=='violin':
        for col in columns:
            if(data[col].dtype=='object'):
                sns.violinplot(col,label,data=data)
                plt.show()
            else:
                sns.violinplot(label,col,data=data)
                plt.show()

print('\n\t\tData Vizualization\n\n\tBar Plot')
vizual(train_data,cat_col,type='bar',label=label2[label2==1])
print('\tHistogram Plot')
vizual(train_data,num_col,type='hist',label=label2[label2==1])
print('\tScatter Plot')
vizual(train_data,num_col,type='scatter',label=label1)
print('\tViolin Plot')
vizual(train_data,train_data.columns,type='violin')

def scale(data,columns,type='min_max'):
    for col in columns:
        if(type=='z score'):
            data[col]=(data[col]-np.mean(data[col]))/(np.std(data[col]))
        if(type=='min_max'):
            data[col]=(data[col]-np.min(data[col]))/(np.max(data[col])-np.min(data[col]))
        if(type=='log'):
            data[col]=np.log(data[col])
        if(type=='inverse'):
            data[col]=(1/data[col])
    return data

def preperation(data):
    data['Gender'].replace({'M':0.9,'F':0.1},inplace=True)
    data['NumberChildrenAtHome'].replace({0:0.1,1:0.3,2:0.5,3:0.7,4:0.9,5:1.1},inplace=True)

    data['s_age']=data['Gender']*data['YearlyIncome']/data['age']*data['NumberChildrenAtHome']
    data['old']=data['age'].apply(lambda x:0 if x<=60 else 1)
    data['gnch']=data['Gender']*data['NumberChildrenAtHome']

    data['NumberChildrenAtHome'].replace({1:0,2:1,3:1,4:1,5:1},inplace=True)
    data['NumberCarsOwned'].replace({1:0,2:0,3:1,4:1},inplace=True)
    data['TotalChildren'].replace({1:0,2:0,3:1,4:1,5:1},inplace=True)

    data=pd.get_dummies(data)
    data=scale(data,columns=['s_age'],type='log')
    data=scale(data,data.select_dtypes('int64','float64'))

    return data

train_data=preperation(train_data)

print('\n\t\tData Vizualization After Data Modification\n\n\tHistogram Plot')
vizual(train_data,['s_age'],type='hist')
print('\tScatter Plot')
vizual(train_data,['s_age'],type='scatter',label=label1)
print('\tBar Plot')
vizual(train_data,['gnch','old'],type='bar')
print('\tViolin Plot')
vizual(train_data,['gnch','old'],type='violin')

def regression(x,y=label1,test_size=0.2):
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=test_size)
    model=make_pipeline(PolynomialFeatures(degree=2),LinearRegression())
    model.fit(x_train,y_train)
    return model,x_test,y_test

def classify(x,y=label2,test_size=0.2):
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=test_size)
    model=make_pipeline(PolynomialFeatures(degree=2),LogisticRegression(C=0.7,penalty='l1',class_weight='balanced',solver='liblinear'))
    model.fit(x_train,y_train)
    return model,x_test,y_test

def roc_pr_f1(model,x,y,show=False,plot=False,show_all=False):
    no_skill_probs=[0 for i in range(len(y))]
    lr_probs=model.predict_proba(x)[:,1]
    pred=model.predict(x)

    ns_fpr,ns_tpr,_=roc_curve(y,no_skill_probs)
    lr_fpr,lr_tpr,_=roc_curve(y,lr_probs)

    ns_precision,ns_recall,_=precision_recall_curve(y,no_skill_probs)
    lr_precision,lr_recall,_=precision_recall_curve(y,lr_probs)
    ns_f1,ns_pr_auc,ns_roc_auc=0.0,auc(ns_recall,ns_precision),roc_auc_score(y,no_skill_probs)
    lr_f1,lr_pr_auc,lr_roc_auc=f1_score(y,pred),auc(lr_recall,lr_precision),roc_auc_score(y,lr_probs)

    if show or show_all:
        print('Accuracy score=',accuracy_score(y,pred))
        print('No Skill: F1 Score=',ns_f1,'\t\tPR AUC=',ns_pr_auc,' ROC AUC=',ns_roc_auc)
        print('Logistic: F1 Score=',lr_f1,' PR AUC=',lr_pr_auc,' ROC AUC=',lr_roc_auc)

    if plot or show_all:
        print('\n\tPrecision VS Recall')
        plt.plot(ns_recall,ns_precision,marker='.',label='No_Skill')
        plt.plot(lr_recall,lr_precision,marker='.',label='Logistics')
        plt.xlabel('Recall')
        plt.ylabel('Precision')
        plt.legend()
        plt.show()

        print('\n\tROC Curve')
        plt.plot(ns_fpr,ns_tpr,marker='.',label='No_Skill')
        plt.plot(lr_fpr,lr_tpr,marker='.',label='Logistics')
        plt.xlabel('False positive rate')
        plt.ylabel('True positive rate')
        plt.legend()
        plt.show()

    return lr_roc_auc,lr_pr_auc,lr_f1

def conf_mat(y_true,y_pred):
    cfm = confusion_matrix(y_true,y_pred)
    sns.heatmap(cfm, annot=True)
    print('\n\tConfusion Matrix')
    plt.xlabel('Predicted classes')
    plt.ylabel('Actual classes')
    plt.show()

def evaluate(model,x_test,y_test,type='class',conf=True,show=False,plot=False,show_all=False):
    if type=='class':
        pred=model.predict(x_test)
        roc_auc,pr_auc,f1=roc_pr_f1(model,x_test,y_test,show,plot,show_all)
        if conf or show_all:
            conf_mat(y_test.values,pred)

        return (1-roc_auc)

    if type=='reg':
        pred=model.predict(x_test)
        rmse=np.sqrt(mean_squared_error(y_test,pred))

        if show or show_all:
            print('R2 score = ',r2_score(y_test,pred),'\nRoot Mean Squared Error=',rmse)

        if plot or show_all:
            error=pred-y_test
            plt.scatter(y_test,error,s=1)
            plt.xlabel('Actual value')
            plt.ylabel('Error')
            plt.show()

            error.hist(figsize=(5,5),bins=30)
            plt.xlabel('Error')
            plt.ylabel('Count')
            plt.show()

            plt.scatter(y_test,pred,s=1)
            plt.xlabel('Actual value')
            plt.ylabel('Predicted Value')
            plt.show()

        return rmse

def cv(model,data,label,scoring='roc_auc',fold=5):
    kfold=KFold(n_splits=fold,shuffle=True,random_state=6)
    score=cross_val_score(model,data,label,scoring=scoring,cv=kfold)
    return (score.mean())

def nested_cv(model,data,label,scoring='roc_auc',fold=10):
    inside=KFold(n_splits=fold,shuffle=True,random_state=2)
    outside=KFold(n_splits=fold,shuffle=True,random_state=7)
    clf=GridSearchCV(model,param_grid={"C":[0.1,0.7,1,10]},cv=inside,scoring=make_scorer(roc_auc_score),return_train_score=False)
    clf.fit(data,label)
    score=cross_val_score(clf,data,label,scoring=scoring,cv=outside)
    return clf.best_estimator_.C,(score.mean())

def predict(model,x_test,y_test):
    predicted=pd.DataFrame()
    predicted['Actua Value']=y_test
    predicted['Predicted Value']=model.predict(x_test)
    return predicted

reg_model,x_test,y_test=regression(train_data)
print('\n\t\tPrediction on Validation set\n\tAvarage Monthly Speding of Customers\n',predict(reg_model,x_test,y_test),'\n\n\tEvaluation\n')
evaluate(reg_model,x_test,y_test,type='reg',show_all=True)
joblib.dump(reg_model,'Models/reg_model.sav')
cv_score=cv(reg_model,train_data,label=label1,scoring='r2')
print('\nMean R2 score after cross validation = ',cv_score)

class_model,x_test,y_test=classify(train_data,label2)
print('\n\tBike Buying Behaviour of Customers\n',predict(class_model,x_test,y_test),'\n\n\tEvaluation\n')
joblib.dump(class_model,'Models/class_model.sav')

evaluate(class_model,x_test,y_test,type='class',show_all=True)
cv_score=cv(class_model,train_data,label=label2,scoring='roc_auc')
print('\nMean ROC AUC after cross validation = ',cv_score)


# 셀프 넘버 ( 정답 )
# 알고리즘 : 구현, 완전 탐색

check = [False for _ in range(10001)]

def f(n):
    ans = n
    temp = n
    while temp > 0:
        ans += temp % 10
        temp = temp // 10

    return ans

for i in range(1, 10001):
    temp = f(i)
    if temp <= 10000:
        check[temp] = True

for i in range(1, 10001):
    if not check[i]:
        print(i)


# Program to digest make depend files
import os
import re
import sys
from typing import Dict, List, Set
from collections import defaultdict

DirectoryName = str
FileName = str

def usage():
    print(""" Usage:
    python factoroptions.py input-options-filename
    """)
    sys.exit(1)

def get_includes(line: str) -> Set[str]:
    """ Return the set of Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    includes = {part for part in parts
                if (part.startswith("-I ") and not part.startswith("-I /usr"))}
    return includes

def get_usr_includes(line: str) -> Set[str]:
    """ Return the set of Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    includes = {part for part in parts if part.startswith("-I /usr")}
    return includes

def get_non_includes(line: str) -> Set[str]:
    """ Return the set of non-Include options from the -options-for-sources line. """
    parts = line.split(";")
    # parts[0] has the -options-for-sources and the source file.
    others = {part for part in parts[1:] if not part.startswith("-I ")}
    return others

class FactorOptions:
    """ Digest .depend files"""

    def __init__(self, input_filename):
        self.input_filename = input_filename
        self.output_filename = input_filename + ".factored"
        self.includes_filename = input_filename + ".includes"
        self.defines_filename = input_filename + ".defines"
        self.lines = self.read_input
        # Collect the -options-for-sources lines.
        self.ofs_lines = [line for line in self.lines if line.startswith("-options-for-sources ")]
        common_includes = get_includes(self.ofs_lines[0])
        common_usr_includes = get_usr_includes(self.ofs_lines[0])
        common_other = get_non_includes(self.ofs_lines[0])
        print(f"len(common_includes) = {len(common_includes)}")
        print(f"len(common_usr_includes) = {len(common_usr_includes)}")
        print(f"len(common_other) = {len(common_other)}")
        for i in range(1, len(self.ofs_lines)):
            this_includes = get_includes(self.ofs_lines[i])
            common_includes = common_includes.union(this_includes)
            print(f"len(common_includes) = {len(common_includes)}")
            this_usr_includes = get_usr_includes(self.ofs_lines[i])
            common_usr_includes = common_usr_includes.union(this_usr_includes)
            print(f"len(common_usr_includes) = {len(common_usr_includes)}")
            this_other = get_non_includes((self.ofs_lines[i]))
            common_other = common_other.union(this_other)
            print(f"len(common_other) = {len(common_other)}")

        self.common_includes = common_includes
        self.common_usr_includes = common_usr_includes
        self.common_other = common_other
        self.factored = self.compute_output()
        pass

    @property
    def read_input(self) -> List[str]:
        with open(self.input_filename, 'r') as f:
            lines = [line.strip("\\\n") for line in f]
            return lines

    def is_common(self, arg):
        if arg in self.common_includes:
            return True
        if arg in self.common_usr_includes:
            return True
        return arg in self.common_other

    def compute_new_ofs(self, ofs_index) -> str:
        line = self.ofs_lines[ofs_index]
        parts = line.split(";")
        args = []
        for i in range(1, len(parts)):
            arg = parts[i]
            if not self.is_common(arg):
                args.append(arg)
        if args:
            argstring = ';'.join(args)
            result = ':'.join([parts[0], argstring])
            return result
        return ''

    def get_common_usr_include_list(self) -> List[str]:
        # Preserve the order from the first.
        line = self.ofs_lines[0]
        parts = line.split(";")
        result = []
        for i in range(1, len(parts)):
            part = parts[i]
            if part in self.common_usr_includes:
                if os.path.isdir(part[3:]):
                    result.append(part)
                else:
                    print(part + " is not a directory")
        return result

    def compute_output(self) -> List[str]:
        factored_lines = []
        ofs_index = 0
        for line in self.lines:
            if line.startswith("-options-for-sources "):
                factored = self.compute_new_ofs(ofs_index)
                if factored:
                    factored_lines.append(factored)
                ofs_index += 1
                pass
            else:
                if ofs_index == len(self.ofs_lines):
                    # We have processed the last ofs line. Output common.
                    factored_lines += ["# Start of factored options"]
                    # First the non-/usr includes.
                    sorted_includes = [include for include in self.common_includes
                                       if os.path.isdir(include[3:])]
                    sorted_includes.sort()
                    factored_lines += sorted_includes
                    # Next the /usr includes.
                    factored_lines += self.get_common_usr_include_list()
                    # Now the others. Let's sort them too.
                    sorted_others = [other for other in self.common_other]
                    sorted_others.sort()
                    factored_lines += sorted_others
                    # increment ofs_index so we don't do this again.
                    ofs_index += 1
                    factored_lines += ["# End of factored options"]

                # One of the original lines.
                factored_lines.append(line)
                pass
        return factored_lines

    def output_factored(self):
        with open(self.output_filename, "w") as w:
            for line in self.factored:
                w.write(line + '\n')
        pass

    def output_defines(self):
        with open(self.defines_filename, "w") as w:
            for line in self.common_other:
                w.write(line + '\n')
        pass

    def output_includes(self):
        with open(self.includes_filename, "w") as w:
            for line in self.common_usr_includes:
                w.write(line + '\n')
            for line in self.common_includes:
                w.write(line + '\n')
        pass

if __name__ == u'__main__':
    print('Factoring PolySpace Code Prover options\n')
    options = sys.argv
    if len(sys.argv) != 2:
        usage()

    factor = FactorOptions(options[1])
    factor.output_factored()
    factor.output_defines()
    factor.output_includes()
    print('\ndone.\n')


import logging
import socket
import threading
import time
logger = logging.getLogger(__name__)
from . import onedrone as mavlink
#from pymavlink.dialects.v20 import ardupilotmega as mavlink2

class UDPListener(threading.Thread):
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1),
        # own_hostname = socket.gethostname()
        # own_ip = socket.gethostbyname(own_hostname)
        # own_ip = ''
        self.sock.setblocking(0)

        self.sock.bind(('',10777))

        self.rx_buf = bytearray()
        self.rx_lock = threading.Lock()
        self.bytes_rcvd = 0
        self.data_available = False

        self.max_buf_size = 0xffff
        threading.Thread.__init__(self)

    def __del__(self):
        pass

    def run(self):
        self.keep_running = True
        logger.info('UDP Listener | Running UDP Listener')
        while self.keep_running:
            data_received = self._receive()
            if not data_received:
                time.sleep(0.02)

        logger.info('UDP Listener |  Run function ended')

    def stop(self):
        logger.info('UDP Listener | Stopping UDP Listener')
        self.keep_running = False
        self.join()
        logger.info('UDP Listener | Stopped UDP Listener')

    def _receive(self):
        data = b''
        try:
            data, address = self.sock.recvfrom(4096)
        except BlockingIOError:
            pass
            # logger.info('Excepted BlockingIOError')
        if data:
            self.rx_lock.acquire()
            self.rx_buf +=bytearray(data)
            self.data_available = True
            self.rx_lock.release()
            return True
        else:
            return False


from .reqs import Relic
from .reqs import EventDataUpdate, EventTrigger, MaxOp

class TheBoot(Relic):
    def __init__(self):
        super().__init__("The Boot")
        self.add_effect(
            EventDataUpdate("amount", MaxOp(5)).set_trigger(
                EventTrigger("on_enemy_pre_take_damage")
            )
        )


#crear 2 listas, una con nombres y otra con apellidos
nombres = ["Lucas","Matias","Camila","Pedro","Roberto"]
apellidos = ["Dalto","Zing","Lorenzana","Herrera","Gomez"]

#Registrar esta informacion en un TXT de forma óptima

with open("Parte2\\Ejercicios2\\Resolviendo_problemas\\nombres_y_apellidos.txt","w") as archivo:
    archivo.writelines("los datos son:\n\n")
    [archivo.writelines(f"nombres: {n}\nApellidos: {a}\n-----------------\n") for n,a in zip(nombres,apellidos)]
#Buscar una forma mas óptima


# Import Python functions
import numpy as np
import matplotlib.pyplot as plt
import datetime
import pandas as pd
import random, sys, os, glob
import pvlib
from pvlib.pvsystem import PVSystem
from pvlib.location import Location
from pvlib.modelchain import basic_chain, ModelChain
from pvlib.temperature import TEMPERATURE_MODEL_PARAMETERS
import emcee
from multiprocessing import Pool
from collections import Counter
from operator import itemgetter
from IPython.display import clear_output, Image
import warnings
warnings.simplefilter("ignore")

# Import custom functions
from clustering_functions_4D import ln_p_clustering, optimalK, best_walkers
from plot_functions_4D import burnin_chains, sample_chains, autocorrelation
from data_processing_4D import overwrite_files

# Assign guess values for theta, phi and Np. These are subsequently used in get_starting_values_ball below.
def guess_values():
    #params_pred = input('input predicted values for theta, phi and Np = ').split()
    #a, b, c = params_pred.split(',')

    theta_guess, phi_guess, Np_guess, sigma_guess = [int(x) for x in input("input predicted values (separated by a space) for theta, phi, Np and sigma = : ").split()]

    if (theta_guess < 0):
        print('a negative tilt angle is not possible. Exiting. Please try again'); sys.exit()
    if (theta_guess > 90):
        print('an angle larger than 90 degrees is not possible for the tilt angle. Exiting. Please try again.'); sys.exit()
    if (phi_guess < 0):
        phi_guess += 360
        print('Warning: a negative angle is possible; this is the same as phi += 360. Adding 360 degrees.')
    if (phi_guess > 360):
        phi_guess -= 360
        print('Warning: an angle larger than 360 degrees is possible; this is the same as phi -= 360. Subtracting 360 degrees.')
    if (Np_guess > 100):
        print('Warning: you are running an MCMC simulation for a very large system size. Are you sure?')
    if (sigma_guess < 0):
        print('a negative sigma is not possible. Exiting. Please try again'); sys.exit()

    return theta_guess, phi_guess, Np_guess, sigma_guess

# Assign MCMC starting values for nwalkers in 3D parameter space (theta, phi and Np).
def get_starting_values_ball(guess_values, ndim, nwalkers, variation):
    values = [guess_values + 1e-4*np.random.randn(ndim) for i in range(nwalkers)]

    return values

# Obtain PVlib energy day series for a chosen set of PV system characteristics using the Sandia module and SAPM inverter databases.
# Here, a SunPower module and AE Solar inverter were chosen. These may be substituted with other choices.
def get_energy_day(lat, lon, theta, phi, Np, year, month, day, naive_times, sandia_modules, sapm_inverters, timezone):

    module = sandia_modules['SunPower_SPR_230_WHT__2007__E__']
    inverter = sapm_inverters['AE_Solar_Energy__AE5_0__208V_']
    system = PVSystem(surface_tilt = theta, surface_azimuth = phi, module_parameters = module, modules_per_string = int(Np), strings_per_inverter = 1, inverter_parameters = inverter)
    location = Location(lat, lon, name='Amsterdam', altitude=10, tz=timezone)
    mc = ModelChain(system, location, orientation_strategy=None)
    mc.run_model(location.get_clearsky(naive_times))

    return [0 if np.isnan(e) else int(e) for e in mc.ac]

# Define the log likelihood function to be evaluated by Emcee.
def lnlike(params, dict_observed, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone):

    theta, phi, Np, sigma = params
    Np = int(Np)
    E_mod = get_energy_day(lat, lon, theta, phi, Np, d0.year, d0.month, d0.day, times, sandia_modules, sapm_inverters, timezone)
    dict_model = dict(list(zip(hm, E_mod)))
    obsmod = [(k, dict_observed[k], dict_model[k]) for k in sorted(dict_observed)]

    # Could define the value of N*log*2pi to speed up code.
    return (((-1*np.sum([((val[1]-val[2])**2) for val in obsmod])/(2*(sigma**2))))-(len(obsmod)*np.log(sigma)))
    #-(len(obsmod)*np.log(np.sqrt(2*3.14)))

# Define the prior for the MCMC. A uniform prior is defined which allows for all orientations and tilts and number of panels ranging
# between 1 and 100. Future improvement will include allowing for the prior to be constrained.
def lnprior(params):

    theta, phi, Np, sigma = params
    if 0.0 < theta < 90.0 and 0.0 < phi < 360.0 and 1 < Np < 100 and 0 < sigma < 1000:
        return 0.0
    return -np.inf

# Define the log probability as the sum of lnprior and lnlike. If phi is smaller than 0 or larger than 360, then phi is re-calculated.
def lnprob(params, dict_observed, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone):

    theta, phi_tmp, Np, sigma = params

    if (params[1]>360.0):
        params[1]-=360.0
    elif (params[1]<0.0):
        params[1]+=360.0
    else:
        params[1]=params[1]

    lp = lnprior(params)
    if not np.isfinite(lp):
        return -np.inf

    return lp + lnlike(params, dict_observed, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone)

# This function is employed in the sampling function (below) to check whether the chains have converged. The lines of code here come from
# the Emcee manual.
def check_convergence(tau, old_tau, iteration):
    converged = np.all(tau * 50 < iteration)
    print('50*tau/iteration = ', tau * 50/iteration)
    print('(old tau - tau)/tau = ', np.abs(old_tau - tau) / tau)
    converged &= np.all(np.abs(old_tau - tau) / tau < 0.01)
    return converged

# This function performs MCMC sampling up until the burn in phase is deemed completed. This is determined by clustering the walker chains
# and checking how close to each other the chains are in parameter space. For more information, look at the individual comments in the function.
def burnin(system_meta, system_measurements, date, mcmc_config, pvlib_databases, clustering_params, paths):
    # Assign variable values from tuples.
    (sid, lon, lat, timezone) = system_meta
    (dict_measurement, times, hm) = system_measurements
    (nwalkers, ndim, nproc, iterations, values) = mcmc_config
    (sandia_modules, sapm_inverters) = pvlib_databases
    (minClusters, maxClusters) = clustering_params
    (chains_path, plots_path) = paths

    nwalkers_initial = nwalkers
    year = int(date[0:4]); month = int(date[4:6]); day = int(date[6:8])
    d0 = datetime.datetime(year, month, day)

    # Check whether the MCMC chain files already exist.
    overwrite_files(chains_path, sid, date, nwalkers_initial, iterations)

    index = 0; index_burnin = -1; niters = 50; storage = True; ln_p_lastiters = []; index_var = 0

    # Define MCMC chain file which will be used by the Emcee backend function.
    filename = chains_path + 'chain_id%s_%s_w%s_i%s.h5' %(str(sid), date, str(nwalkers_initial), str(iterations))
    backend = emcee.backends.HDFBackend(filename); backend.reset(nwalkers, ndim)

    with Pool(processes=nproc) as pool:
        sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=([dict_measurement, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone]), pool=pool, backend=backend, moves=emcee.moves.WalkMove())
        # The while loop keeps running until the end of the burn in period has been reached.
        # The end is determined on the basis of statistics applied to the remaining number of clustered walkers.
        while (index == 0):
            index_burnin += 1
            # The MCMC sampling is performed for 50 iterations afte
            for sample in sampler.sample(initial_state = values, iterations = niters, progress=True, store=storage):
                ln_p_lastiters.extend(sample.log_prob)
                # Ignore all calculations in this loop unless the number of iterations (sampler.iteration) equals niters.
                if sampler.iteration % niters:
                    continue
                clear_output(wait=True)
                # All code within this if statement is concerned with the plotting of the MCMC chains.
                # This is not performed on the first run, since no chains have been generated yet.
                if (index_burnin > 0):
                    #files = sorted(glob.glob(chains_path + '*i10000*h5'))
                    files = sorted(glob.glob(chains_path + 'chain_id%s_%s_w%s_i%s*.h5' %(str(sid), date, str(nwalkers_initial), str(iterations))))
                    samples, log_samples = get_last_sample(files)
                    # Create plot for the first set of chains and keep appending plot afterwards.
                    if (index_burnin == 1):
                        fig, axes = plt.subplots(6, figsize=(15, 11), sharex=True)
                    starter_val = index_burnin*niters
                    burnin_chains(fig, axes, starter_val, starter_val + niters, samples, log_samples, plots_path, \
                                  sid, nwalkers_initial, iterations, date)

#                    display(Image(filename = plots_path + 'mcmc_id%s_%s_w%s_i%s.png' %(str(sid), date, str(nwalkers_initial), str(iterations))))

                # Perform k means clustering. See functions ln_p_clustering and optimalK for more info.
                ln_p_mean, kmeans = ln_p_clustering(ln_p_lastiters, nwalkers, minClusters, maxClusters)
                occurrences = Counter(kmeans.labels_)
                walker_group = list(zip([i for i in range(len(kmeans.labels_))], kmeans.labels_))
                # Calculate the variance (spread) of the mean log posterior per walker.
                ln_p_var = np.var(ln_p_mean)

                # Check whether the variance is small or large.
                print(ln_p_var)
                if (ln_p_var > 5):
                    index=0
                    # Check whether the number of clustered groups is larger than 1.
                    if (len(occurrences)>1):
                        # Determine which walkers belong to which group.
                        min_key, min_count = min(occurrences.items(), key=itemgetter(1))
                        min_val = kmeans.cluster_centers_[min_key][0]
                        if (abs(np.min(kmeans.cluster_centers_)-min_val)<1e-5):
                            id_walkers = [num[0] for num in walker_group if num[1] != min_key]
                        else:
                            id_walkers = [num[0] for num in walker_group]
                    # If there is only one group of walkers, then all walkers are retained for the next 50 iterations.
                    else:
                        id_walkers = [num[0] for num in walker_group]

                # If the variance is smaller than 5, this could indicate that the chains are starting to converge towards the same point.
                # To test whether this is the case, a counter (index_var) is initialised. If the algorithm ends up five successive times
                # in this else statement, the MCMC burn in is considered finished.
                else:
                    index=0
                    id_walkers = [num[0] for num in walker_group]
                    index_var += 1
                    print('Index var equals ', index_var)
                    # Set a check index to the burn in index. The difference between the indices must be five if else statement was performed
                    # 5 successive times.
                    if index_var == 1:
                        check_first = index_burnin
                    if index_var == 6:
                        check_last = index_burnin
                        if ((check_last - check_first) == 5):
                            index = 1
                        else:
                            index_var = 0

                # Define a new filename for the new backend. Please see explanation below as to why this is necessary.
                filename = chains_path + 'chain_id%s_%s_w%s_i%s_' %(str(sid), date, str(nwalkers_initial), str(iterations)) + "{:02}".format(index_burnin) + '.h5'
                new_backend = emcee.backends.HDFBackend(filename)
                state = sampler.get_last_sample()

                # Account for larger or smaller angles for orientation and subtract or add 360 degrees.
                for i in range(len(state.coords)):
                    if state.coords[i][1]>360.0:
                        state.coords[i][1] = state.coords[i][1]-360.0
                    if state.coords[i][1]<0.0:
                        state.coords[i][1] = state.coords[i][1]+360.0

                # Change Emcee's 'state' to allow for the clustering approach above. Only want to retain the relevant walkers.
                state.log_prob = np.array([i for j, i in enumerate(state.log_prob) if j in id_walkers])
                state.coords = np.array([i for j, i in enumerate(state.coords) if j in id_walkers])
                print('Best walker value is ', np.min(state.log_prob))
                state.random_state = None
                # Determine the new number of walkers, after clustering.
                nwalkers = len(state.log_prob)
                # Reset the backend with the new number of walkers.
                backend.reset(nwalkers, ndim)
                ndim = 4
                print('new number of walkers is ', nwalkers)
                # Define the sampler with the new backend. This is necessary because it is not possible to append to the same h5 file, when the
                # number of chains decreases.
                sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=([dict_measurement, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone]), backend = new_backend, pool = pool, moves = emcee.moves.WalkMove())
                values = state; storage = True; ln_p_lastiters = []

            # Check whether the burn in phase is completed. Else continue.
            if (index == 1):
                print('Burn in phase completed')
                break
            else:
                print('Burn in phase is still happening')
                os.system("clear")

    clear_output(wait=True)
    print('MCMC sampling can now commence')

    # Having exited the while loop, the best 8 walkers are obtained with the function best_walkers. User can choose to keep more walkers
    # if desired, though bear in mind that this number also depends on the number of walkers used to start with. Here we used 40, so this number
    # should also be increased.
    nwalkers = 8
    state = best_walkers(state)
    return state

# This function is performed after the burnin function (above).
def sampling(system_meta, system_measurements, date, mcmc_config, pvlib_databases, clustering_params, paths):
    # Assign variable values from tuples.
    (sid, lon, lat, timezone) = system_meta
    (dict_measurement, times, hm) = system_measurements
    (nwalkers, ndim, nproc, iterations, values) = mcmc_config
    (sandia_modules, sapm_inverters) = pvlib_databases
    (minClusters, maxClusters) = clustering_params
    (chains_path, plots_path) = paths

    nwalkers_initial = nwalkers
    year = int(date[0:4]); month = int(date[4:6]); day = int(date[6:8])
    d0 = datetime.datetime(year, month, day)
    filename = chains_path + 'chain_id%s_%s_w%s_i%s_sample.h5' %(str(sid), date, str(nwalkers_initial), str(iterations))
    filename_plot = plots_path + 'autocorrelation_id%s_%s_w%s_i%s.png' %(str(sid), date, str(nwalkers_initial), str(iterations))
    new_backend = emcee.backends.HDFBackend(filename)
    max_n = iterations
    storage = True
    autocorr = np.empty(max_n)
    index = 0
    niters = 50
    # Necessary to initialise old tau at infinity (see Emcee documentation)
    old_tau = np.inf

    # Similar to burnin function.
    with Pool(processes=nproc) as pool:
        sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=([dict_measurement, times, hm, d0, lon, lat, sandia_modules, sapm_inverters, timezone]), pool = pool, backend = new_backend, moves = emcee.moves.WalkMove())
        for sample in sampler.sample(initial_state = values, iterations = max_n, progress = True, store = storage):
            if sampler.iteration % niters:
                continue

            # Compute the autocorrelation time so far. See Emcee documentation for this snippet.
            clear_output(wait = True)
            tau = sampler.get_autocorr_time(tol = 0)
            autocorr[index] = np.mean(tau)
            index += 1

            # Check convergence
            converged = check_convergence(tau, old_tau, sampler.iteration)

            # Plot the auto correlation time
            autocorrelation(index, autocorr, filename_plot)

            # Check if convergence has occurred
            if converged:
                break
            old_tau = tau

            sample_chains(filename, plots_path, sid, nwalkers, iterations, date)
#            display(Image(filename = plots_path + 'mcmc_id%s_w%s_i%s_%s.png' %(str(sid), str(nwalkers), str(iterations), date)))

#    display(Image(filename = plots_path + 'mcmc_id%s_w%s_i%s_%s.png' %(str(sid), str(nwalkers), str(iterations), date)))

    print("Mean acceptance fraction:{0:.3f}".format(np.mean(sampler.acceptance_fraction)))

# Obtain the last MCMC chain sample. The output of this function is used as input for the burnin_chains function.
def get_last_sample(files):
    all_samples = []; all_log_samples = []
    for file in files[-1:]:
        reader = emcee.backends.HDFBackend(file)
        total_samples = reader.get_chain()
        length = total_samples.shape[0]
        log_prob_samples = reader.get_log_prob()
        for sample in total_samples:
            all_samples.append(sample)
        for logprob in log_prob_samples:
            all_log_samples.append(logprob)

    samples = np.asarray(all_samples)
    log_samples = np.array(all_log_samples)

    return samples, log_samples


# Leetcode
# https://leetcode.com/problems/course-schedule-ii

class Solution:
    def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
        pre = {}
        done = set([i for i in range(numCourses)])

        for i in prerequisites:
            c1, c2 = i[0], i[1]
            pre[c1] = pre.get(c1, []) + [c2]
            if c1 in done:
                done.remove(c1)

        def dfs(curr, path):
            temp = []
            path.add(curr)
            for course in pre[curr]:
                if course in path:
                    return -1
                if course not in done:
                    temp2 = dfs(course, path)
                    if temp2 == -1:
                        return -1
                    temp += temp2
                    path.remove(course)

            done.add(curr)
            return temp + [curr]

        out = list(done)
        for i in range(numCourses):
            if i not in done:
                temp = dfs(i, set([]))
                if temp == -1:
                    return []
                out += temp

        return out


import requests
import json

def take_url(in_data: str) -> str:
    """
        Функция возвращающая URL для запроса JSON файла.
    """
    id = in_data.split('/')[-2]
    url = f'https://card.wb.ru/cards/detail?&nm={id}'
    return url

def search_json(url: str):
    """
        Функция запрашивает JSON файл и записывает его в файл.
    """
    headers = {'Accept': "*/*", 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
    response = requests.get(url, headers=headers)
    # opener = AppURLopener()
    # response = opener.open(url)

    data = response.json()
    with open('wb_catalogs_data.json', 'w', encoding='UTF-8') as file:
        json.dump(data, file, indent=2, ensure_ascii=False)
        # print(f'Данные сохранены в wb_catalogs_data.json')

def take_data(url: str) -> (str, str, str, int, str):
    """
        Функция вытаскивающая из JSON файла необходимые нам данные.
        :return = брэнд, наименование, артикул, цена и URL для дальнейших запросов.
    """
    with open('wb_catalogs_data.json', 'r', encoding='UTF-8') as file:
        data_file = file.read()
        my_loads = json.loads(data_file)
        data_brand = my_loads["data"]['products'][0]["brand"]
        data_name = my_loads["data"]['products'][0]['name']
        data_id = my_loads["data"]['products'][0]['id']
        data_price = int(my_loads["data"]['products'][0]['salePriceU']) // 100
        data_url = url
        return data_brand, data_name, data_id, data_price, data_url

if __name__ == "main":
    take_url()
    search_json()
    take_data()
else:
    print(f'Импортируется {__name__}')


import machine
import utime

potentiometer = machine.ADC(26) #deklarasi pin yg dipakai
#conversion_factor = 3.3 / (65535)
led = machine.Pin(6, machine.Pin.OUT)
led1 = machine.Pin(7, machine.Pin.OUT)

while True:
    RH = potentiometer.read_u16()
    kelembaban = (100-(RH/65535)*100) #perhitungan dalam persen
    print(kelembaban)
    utime.sleep(2)
    if kelembaban < 80:#kelembaban kurang dari 80% led menyala
        led.value(1)
        led1.value(1)
    else : #kelembaban lebih dari 80% led mati
        led.value(0)
        led1.value(0)


# Given two binary strings, return their sum (also a binary string).
#
# The input strings are both non-empty and contains only characters 1 or 0.
#
# Example 1:
#
# Input: a = "11", b = "1"
# Output: "100"
#
# Example 2:
#
# Input: a = "1010", b = "1011"
# Output: "10101"
#
# Constraints:
#
# Each string consists only of '0' or '1' characters.
# 1 <= a.length, b.length <= 10^4
# Each string is either "0" or doesn't contain any leading zero.

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        result = []
        if len(a) > len(b):
            a, b = b, a
        carry = 0
        for i in range(-1, -len(a) - 1, -1):
            carry, value = divmod(int(a[i]) + int(b[i]) + carry, 2)
            result.append(str(value))
        for i in range(-len(a) - 1, -len(b) - 1, -1):
            carry, value = divmod(int(b[i]) + carry, 2)
            result.append(str(value))
        if carry == 1:
            result.append('1')
        result = ''.join(result)
        return result[::-1]

print(Solution().addBinary("1010", "1011"))


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        self.flattern2(root)

    def flattern2(self, node):
        if not node:
            return None
        if not node.left and not node.right:
            return node
        left = self.flattern2(node.left)
        right = self.flattern2(node.right)
        node.right = left
        while left and left.right:
            left = left.right
        if left:
            left.right = right
        else:
            node.right = right
        node.left = None
        return node

if __name__ == '__main__':
    S = Solution()
    a1 = TreeNode(1)
    a2 = TreeNode(4)
    a3 = TreeNode(2)
    a4 = TreeNode(11)
    a5 = TreeNode(13)
    a6 = TreeNode(4)
    a7 = TreeNode(7)
    a8 = TreeNode(2)
    a9 = TreeNode(5)
    a10 = TreeNode(1)

    # a1.left = a2
    a1.left = a3
    # a2.left = a4
    # a3.left = a5
    # a3.right = a6
    # a4.left = a7
    # a4.right = a8
    # a6.left  = a9
    # a6.right = a10
    S.flatten(a1)
    print(a1)


import colorlog
from colorlog import ColoredFormatter

handler = colorlog.StreamHandler()

formatter = ColoredFormatter(
    "%(log_color)s%(asctime)s (%(module)s:%(lineno)d) %(levelname)-8s%(reset)s %(message_log_color)s%(message)s ",
    datefmt="%y/%m/%d %H:%M:%S",
    reset=True,
    log_colors={
        'DEBUG': 'white',
        'INFO': 'white',
        'WARNING': 'white',
        'ERROR': 'white',
        'CRITICAL': 'black,bg_white',
    },
    secondary_log_colors={
        'message': {
            'DEBUG': 'blue',
            'INFO': 'cyan',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red'
        }
    },
    style='%'
)
handler.setFormatter(formatter)

log = colorlog.getLogger('example')
log.addHandler(handler)
log.setLevel('DEBUG')