import json
from wordcloud import WordCloud
from nltk.corpus import stopwords
second_filtered_dataset=open("../crawl_result_merge.json","r")
second_filtered_dataset_str=second_filtered_dataset.read()
second_filtered_dataset_lst=second_filtered_dataset_str.split("---------------------------------\n")
total_abstract=open("total_abstract.txt","w")

#拼接每个阶段的摘要
for i in range(0,len(second_filtered_dataset_lst)-1):#len(merge_lst)
    line=json.loads(second_filtered_dataset_lst[i])
    abstract=line["abstract"]
    total_abstract.write(abstract+"\n")
    print("abstract",abstract)
total_abstract.close()
total_abstract=open("total_abstract.txt","r")

stop_words = stopwords.words('english')
stop_words.extend(['from', 'use', 'present', 'play', 'can','find','used','uses','using','design','paper','time','two'
                   ,'however','also','many','well','new','based','user','users','data','method','information','propose',
                   'provide','research','approach','problem','based','proposed','result','results','need','way','show',
                   'help','different','study','process','e g','within','become','order','finally','make','related',
                   'one','number','may','demostrate','methods','set','improve'])

abstract_wordcloud=WordCloud(width=1024,height=768,background_color="white",stopwords=stop_words,max_words=80).generate(total_abstract.read().lower()
                                                                                )
abstract_wordcloud.to_file("total_abstract.jpg")
second_filtered_dataset.close()
total_abstract.close()


from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.multiclass import unique_labels
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
import numpy as np
import skfda

def fda_longitudinal_variance_terms(logfda):
    d_subs = logfda.data_matrix[..., 0][:, 1:] - \
        logfda.data_matrix[..., 0][:, :-1]
    t_subs = np.array(
        logfda.sample_points[0][1:]) - np.array(logfda.sample_points[0][:-1])
    terms = d_subs / np.sqrt(t_subs)

    return terms**2

def fda_longitudinal_variance(logfda):
    terms = fda_longitudinal_variance_terms(logfda)
    variance = np.nanmean(terms, axis=1)
    return variance

class LongitudinalVarianceClassifier(BaseEstimator, ClassifierMixin):

    def __init__(self,
                 real_bayes_rule=False,
                 class_variances=None,
                 synthetic_covariance=False,
                 longitudinal_variances=True,
                 linear=False):
        self.class_variances = class_variances
        self.real_bayes_rule = real_bayes_rule
        self.synthetic_covariance = synthetic_covariance
        self.longitudinal_variances = longitudinal_variances
        self.linear = linear

    def fit(self, X, y):

        # Check that X and y have correct shape
        _, y = check_X_y(X.data_matrix[..., 0], y)
        # Store the classes seen during fit
        self.classes_ = unique_labels(y)
        self.class_variances_ = (np.zeros(len(self.classes_)) if
                                 self.class_variances is None else
                                 self.class_variances)

        # Data for the exact Bayes rule
        self.class_covariance_matrices_ = [None] * len(self.classes_)
        self.class_mean_ = [None] * len(self.classes_)

        for cl in self.classes_:

            if self.class_variances is None:
                if self.longitudinal_variances:
                    self.class_variances_[cl] = np.mean(
                        fda_longitudinal_variance(X[y == cl]))
                else:
                    var = skfda.exploratory.stats.var(
                        X[y == cl, 1:]) / X.sample_points[1:]
                    self.class_variances_[cl] = var.data_matrix[..., 0][0, -1]

            self.synthetic_matrix = np.minimum(
                X.sample_points[0][:, None], X.sample_points[0])

            if self.synthetic_covariance:
                self.class_covariance_matrices_[
                    cl] = self.class_variances_[cl] * self.synthetic_matrix
            else:
                self.class_covariance_matrices_[cl] = np.cov(
                    X[y == cl].data_matrix[..., 0], rowvar=False)
            self.class_mean_[cl] = skfda.exploratory.stats.mean(X[y == cl])

        self.m_ = self.class_mean_[1] - self.class_mean_[0]

        # Return the classifier
        return self

    def decision_function(self, X):
        def _scalar_product_aux(cl, x, y):
            try:
                cov_inv = np.linalg.inv(
                    self.class_covariance_matrices_[cl][1:, 1:])
            except np.linalg.LinAlgError:
                print(f'Singular matrix with points {x.sample_points}')
                return np.nan

            y = y.data_matrix[..., 0][:, 1:].T
            x = x.data_matrix[..., 0][:, 1:]

            return x @ cov_inv @ y

        def scalar_product_quadratic(cl, x, y):
            res = _scalar_product_aux(cl, x, y)

            return np.diag(res)[:, None]

        def scalar_product_mean(cl, x, y):
            return _scalar_product_aux(cl, x, y)

        def logterm():
            if self.synthetic_covariance:
                mat = self.synthetic_matrix[1:, 1:]
                dim = mat.shape[0]
                det = np.linalg.det(mat)
                det1 = self.class_variances_[1]**dim * det
                det0 = self.class_variances_[0]**dim * det

                return -0.5 * (
                    dim * np.log(self.class_variances_[1])
                    - dim * np.log(self.class_variances_[0]))

            else:
                det1 = np.linalg.det(
                    self.class_covariance_matrices_[1][1:, 1:])
                det0 = np.linalg.det(
                    self.class_covariance_matrices_[0][1:, 1:])

                return -0.5 * (np.log(det1 / det0))

        X -= self.class_mean_[0]  # Subtract first class mean

        s1 = -0.5 * (scalar_product_quadratic(1, X, X) -
                     scalar_product_quadratic(0, X, X))
        if self.linear:
            s1 *= 0
        s2 = scalar_product_mean(1, X, self.m_)
        s3 = -0.5 * scalar_product_mean(1, self.m_, self.m_)
        s4 = logterm()

        self.decision_steps_ = (s1, s2, s3, s4)

        return s1 + s2 + s3 + s4

    def predict(self, X):

        def bayes_rule(x):
            bayes_res = (self.decision_function(x) > 0).astype(int)

            return bayes_res

        # Check is fit had been called
        check_is_fitted(self, ['class_variances_'])

        # Input validation
        check_array(X.data_matrix[..., 0])

        if self.real_bayes_rule:
            return bayes_rule(X)
        else:
            var = fda_longitudinal_variance(X)

            cl = np.argmin(
                var[:, np.newaxis] / self.class_variances_
                + np.log(self.class_variances_), axis=1)

            return cl


"""
# Definition for a Node.
class Node:
    def __init__(self, val = 0, neighbors = None):
        self.val = val
        self.neighbors = neighbors if neighbors is not None else []
"""

class Solution:
    def cloneGraph(self, node: 'Node') -> 'Node':
        if not node:
            return
        di = {}
        di[node.val] = Node(node.val)
        queue = []
        queue.append(node)
        while queue:
            curr = queue.pop(0)
            for neig in curr.neighbors:
                if neig.val not in di:
                    di[neig.val] = Node(neig.val)
                    queue.append(neig)
                di[curr.val].neighbors.append(di[neig.val])
        return di[node.val]


import logging
import urllib

from django.conf import settings
from django.core.cache import cache

import simplejson

logger = logging.getLogger(__name__)

def get_contacts_metadata():
    cache_key = 'contact_meta_data'
    cache_timeout = 3600  # 1 hour
    data = cache.get(cache_key)
    if data:
        logger.debug("returning contact data from the cache")
        return data

    api_base = settings.UREPORT_API_BASE
    args = {
        'meta_only': 'true',
        'username': settings.UREPORT_API_USERNAME,
        'api_key': settings.UREPORT_API_KEY,
    }
    url = api_base + 'contacts/?' + urllib.urlencode(args)
    logger.debug("calling url: %s" % url)
    data = simplejson.load(urllib.urlopen(url))
    cache.set(cache_key, data, cache_timeout)
    return data

def ureporter_count(request):
    results = get_contacts_metadata()
    if 'Error' in results:
        total_count = 0
    else:
        total_count = results['meta']['total_count']

    return {'total_ureporters': total_count}


from flask import Blueprint, request
from flask.json import jsonify
from app.authentication.auth import Auth
from app.services import user_services
from app.controllers import require_request_json_body

user_controller = Blueprint('user_controller', __name__)

@user_controller.route('/signup/', methods=['POST'])
@require_request_json_body
def singup():
    '''
    Register new user
    '''
    user = user_services.create_new_user(request.json)
    if user is None:
        return jsonify({'error': 'Bad request'}),400
    # Generate token
    token = Auth.generate_token(user.id)
    return jsonify({'user-token': token}), 200

@user_controller.route('/signin/', methods=['POST'])
@require_request_json_body
def signin():
    """
    Login a user
    """
    user = user_services.autenticate_user(request.json)
    if user is None:
        return jsonify({'error': 'Bad request'}),400
    # Generate a user token
    token = Auth.generate_token(user.id)
    return jsonify({'user-token': token}), 200

@user_controller.route('/<id>/content/', methods=['GET'])
def get_user_content_list(id):
    """
    Get a list of content created by the user
    """
    contents = user_services.get_user_contents(id)
    if contents is None:
        return jsonify({'error_message': 'Bad request'}), 400
    return jsonify([content.serialize for content in contents]), 200


from urllib.request import urlopen, Request
import pandas as pd
import os
import time
import numpy as np
from datetime import datetime
import datetime as dt
import sys
from io import StringIO
from joblib import Parallel, delayed
import requests
from jailscrape.common import save_to_s3, get_browser, get_logger, record_error
from selenium.webdriver.common.keys import Keys
import watchtower

ROW_INDEX = 14 # Change this for each scraper
def main(roster_row):
    try:
        """
        Old URL was for wrong state

        OLD URL: https://www.co.jefferson.tx.us/Sheriff/content/documents/inmate/CURRENTINMATES.PDF
        NEW URL: http://sheriff.jccal.org/NewWorld.InmateInquiry/AL0010000

        """
        logger = get_logger(roster_row)
        browser = get_browser()
        urlAddress = roster_row['Working Link']
        logger.info('Set working link to _%s_', urlAddress)
        #Boilerplate code setting up logger, getting initial URL

        #Given the urlAddress passed to the function we will navigate to the page
        browser.get(urlAddress)

        ##########
        # Begin core specific scraping code
        page_index = 0
        assert roster_row['State'].lower() == 'alabama'
        assert roster_row['County'].lower() == 'jefferson'

        time.sleep(np.random.uniform(5,10,1))
        menu = browser.find_element_by_xpath('//*[@id="uxFacility"]')
        menu.send_keys('Jeff')

        search = browser.find_element_by_xpath('//*[@id="Inmate_Index"]/div[1]/form/div/div/input[1]')
        search.click()

        pages = []

        #Wait
        time.sleep(np.random.uniform(5,10,1))
        store_source = browser.page_source
        pages.append(store_source)

        finished = False
        while not finished:
            time.sleep(np.random.uniform(5,10,1))
            next_page = browser.find_element_by_xpath('//*[@id="Inmate_Index"]/div[2]/div[3]/a[3]')
            next_page.click()
            store_source = browser.page_source
            if store_source not in pages:
                pages.append(store_source)
                page_index += 1
                save_to_s3(browser.page_source, page_index, roster_row)
                logger.info('Saved page _%s_', page_index)
            else:
                finished = True
        # End core specific scraping code
        ##########

        #Close the browser
        logger.info('complete!')
        browser.close()

    except Exception as errorMessage:
        browser.close()
        record_error(str(errorMessage), page_index, roster_row)
        # Record error in S3
        logger.error('Error: %s', errorMessage)
        # Log error
        pass

if __name__ == "__main__":
    #This will load in the current jail roster list
    #Select the index of the roster this script is for:
    #Write the name of the county and state
    roster = pd.read_csv('/opt/jail_roster_final_rmDuplicates.csv',encoding = "utf-8")
    main(roster[roster['index'] == ROW_INDEX].iloc[0])


import asyncio
import math

#Bubble Sort
async def bubble_sort(array):
    n = len(array)

    # Traverse through all array elements
    for i in range(n):

        # Last i elements are already in place
        for j in range(0, n-i-1):

            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if array[j] > array[j+1] :
                array[j], array[j+1] = array[j+1], array[j]
        await asyncio.sleep(0.001)

#Insertion Sort
async def insertion_sort(array):
    # Traverse through 1 to len(array)
    for i in range(1, len(array)):

        key = array[i]

        # Move elements of array[0..i-1], that are
        # greater than key, to one position ahead
        # of their current position
        j = i-1
        while j >= 0 and key < array[j] :
                array[j + 1] = array[j]
                j -= 1
        array[j + 1] = key
        await asyncio.sleep(0.001)
    return True

#Selection Sort
async def selection_sort(array):
    # Traverse through all array elements
    for i in range(len(array)):

        # Find the minimum element in remaining
        # unsorted array
        min_idx = i
        for j in range(i+1, len(array)):
            if array[min_idx] > array[j]:
                min_idx = j

        # Swap the found minimum element with
        # the first element
        array[i], array[min_idx] = array[min_idx], array[i]
        await asyncio.sleep(0.001)

#Pankcake Sort
async def pancake_sort(array):
    # Reverses array[0..i] */
    def flip(array, i):
        start = 0
        while start < i:
            temp = array[start]
            array[start] = array[i]
            array[i] = temp
            start += 1
            i -= 1
    # Returns index of the maximum
    # element in array[0..n-1] */
    def findMax(array, n):
        mi = 0
        for i in range(0,n):
            if array[i] > array[mi]:
                mi = i
        return mi
    # Start from the complete
    # array and one by one
    # reduce current size
    # by one
    n = len(array)
    curr_size = n
    while curr_size > 1:
        # Find index of the maximum
        # element in
        # array[0..curr_size-1]
        mi = findMax(array, curr_size)

        # Move the maximum element
        # to end of current array
        # if it's not already at
        # the end
        if mi != curr_size-1:
            # To move at the end,
            # first move maximum
            # number to beginning
            flip(array, mi)

            # Now move the maximum
            # number to end by
            # reversing current array
            flip(array, curr_size-1)
        curr_size -= 1
        await asyncio.sleep(0.001)

#Shell Sort
async def shell_sort(array):
    # Start with a big gap, then reduce the gap
    n = len(array)
    gap = n//2

    # Do a gapped insertion sort for this gap size.
    # The first gap elements a[0..gap-1] are already in gapped
    # order keep adding one more element until the entire array
    # is gap sorted
    while gap > 0:

        for i in range(gap,n):

            # add a[i] to the elements that have been gap sorted
            # save a[i] in temp and make a hole at position i
            temp = array[i]

            # shift earlier gap-sorted elements up until the correct
            # location for a[i] is found
            j = i
            while  j >= gap and array[j-gap] >temp:
                array[j] = array[j-gap]
                j -= gap

            # put temp (the original a[i]) in its correct location
            array[j] = temp
        await asyncio.sleep(0.3)
        gap //= 2

#Cocktail sort
async def cocktail_sort(array):
    n = len(array)
    swapped = True
    start = 0
    end = n-1
    while (swapped == True):

        # reset the swapped flag on entering the loop,
        # because it might be true from array previous
        # iteration.
        swapped = False

        # loop from left to right same as the bubble
        # sort
        for i in range (start, end):
            if (array[i] > array[i + 1]) :
                array[i], array[i + 1]= array[i + 1], array[i]
                swapped = True

        await asyncio.sleep(0.001)
        # if nothing moved, then array is sorted.
        if (swapped == False):
            break

        # otherwise, reset the swapped flag so that it
        # can be used in the next stage
        swapped = False

        # move the end point back by one, because
        # item at the end is in its rightful spot
        end = end-1

        # from right to left, doing the same
        # comparison as in the previous stage
        for i in range(end-1, start-1, -1):
            if (array[i] > array[i + 1]):
                array[i], array[i + 1] = array[i + 1], array[i]
                swapped = True

        # increase the starting point, because
        # the last stage would have moved the next
        # smallest number to its rightful spot.
        start = start + 1

#HSV colour standard to HEX
def hsv2hex(h, s, v):
    h = float(h)
    s = float(s)
    v = float(v)
    h60 = h / 60.0

    h60f = math.floor(h60)
    hi = int(h60f) % 6
    f = h60 - h60f
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    r, g, b = 0, 0, 0
    if hi == 0: r, g, b = v, t, p
    elif hi == 1: r, g, b = q, v, p
    elif hi == 2: r, g, b = p, v, t
    elif hi == 3: r, g, b = p, q, v
    elif hi == 4: r, g, b = t, p, v
    elif hi == 5: r, g, b = v, p, q
    r, g, b = int(r * 255), int(g * 255), int(b * 255)
    return "#{:02x}{:02x}{:02x}".format(r, g, b)

#Check if sorted
def is_sorted(array):
    for i in range(len(array) -1):
        if array[i] > array[i+1]:
            return False
    return True

if __name__ == "__main__":
    print("Open via main.py")
    exit()


import random
import time
import configparser
import psutil
import os
import json
from collections import defaultdict

from mininet.log import setLogLevel, info

from minindn.apps.application import Application

from minindn.helpers.nfdc import Nfdc
from mininet.topo import Topo
from minindn.minindn import Minindn
from minindn.util import MiniNDNCLI
from minindn.apps.app_manager import AppManager
from minindn.apps.nfd import Nfd
from minindn.helpers.ndn_routing_helper import NdnRoutingHelper
from mininet.node import OVSController

from tqdm import tqdm

from mininet.log import setLogLevel, info
from minindn.wifi.minindnwifi import MinindnWifi
from minindn.util import MiniNDNWifiCLI, getPopen
from minindn.apps.app_manager import AppManager
from minindn.apps.nfd import Nfd
from minindn.helpers.nfdc import Nfdc
from minindn.helpers.ndnping import NDNPing
from time import sleep
from mn_wifi.link import WirelessLink, adhoc, wmediumd
from mn_wifi.wmediumdConnector import interference

# ======================= CONFIGURATION ============================
OVERALL_RUN = 47
PLATOONS = 4
NODES_PER_PLATOON = 5
LOSS_RATE_DISCONNECTED = 100
LOSS_RATE_CONNECTED = 1
SWITCH_TIME = 30
NUM_ROUNDS = 2  # (while publishing)
# 3 rounds after stop publish

STATUS_LOG_INTERVAL_MS = 500
RUN_NUMBER_VALS = range(0, 1)
DEBUG_GDB = False

NFD_SLEEP_TIME = 2

#PROTO = "svs"
#PROTO = "syncps"
PROTO = "ip"

APP_EXECUTABLE = None
UAV_EXECUTABLE = None

if PROTO == "svs":
    APP_EXECUTABLE = "/home/vagrant/svs-pubsub-eval/svs-client/SVSClient"
    UAV_EXECUTABLE = "/home/vagrant/svs-pubsub-eval/svs-client/SVSUAV"
elif PROTO == "syncps":
    APP_EXECUTABLE = "/home/vagrant/svs-pubsub-eval/svs-client/SyncpsClient"
    UAV_EXECUTABLE = "/home/vagrant/svs-pubsub-eval/svs-client/SyncpsUAV"
elif PROTO == "ip":
    APP_EXECUTABLE = "/home/vagrant/dtn7-go/dtnd"
    UAV_EXECUTABLE = "/home/vagrant/dtn7-go/uav"

LOG_MAIN_PATH = "/vagrant/logs/{}/".format(OVERALL_RUN)
# ==================================================================

RUN_NUMBER = 0
SYNC_EXEC = None
LOG_MAIN_DIRECTORY = LOG_MAIN_PATH

def getLogPath():
    LOG_NAME = "{}/{}/{}".format(LOG_MAIN_DIRECTORY, PROTO, RUN_NUMBER)
    logpath = LOG_NAME

    if not os.path.exists(logpath):
        os.makedirs(logpath)
        os.chown(logpath, 1000, 1000)

        os.makedirs(logpath + '/stdout')
        os.chown(logpath + '/stdout', 1000, 1000)
        os.makedirs(logpath + '/stderr')
        os.chown(logpath + '/stdout', 1000, 1000)

    return logpath

def unitname_to_name_prefix(nodename):
    """
    The units have a name that looks as follows:
    unit_{platoonid}_{nodeid}
    The NDN name of such a unit looks as follows:
    /ndn/platoon{platoonid}/unit{unitid}/
    """
    platoon_id = nodename.split('_')[1]
    unit_id = nodename.split('_')[2]
    return "/ndn/platoon{}/unit{}/".format(platoon_id, unit_id)

class PingServer(Application):
    """
    Wrapper class to run the NDNPingServer on a node
    """

    def __init__(self, node):
        Application.__init__(self, node)
        self.prefix = "/ndn/{}-site/".format(node.name)

    def start(self):
        run_cmd = "ndnpingserver {0} &".format(
            self.prefix)

        ret = self.node.cmd(run_cmd)
        info("[{}] running \"{}\" == {}\n".format(
            self.node.name, run_cmd, ret))

class PlatoonClient(Application):
    """
    Wrapper class to run the PlatoonClient on a node
    """

    def __init__(self, node):
        Application.__init__(self, node)
        self.prefix = unitname_to_name_prefix(node.name)

    def start(self):
        if PROTO == "ip":
            run_cmd = "{0} {3} {2}/{3}.log :8333 store_{3} > {2}/stdout/{3}.log 2> {2}/stderr/{3}.log &".format(
                APP_EXECUTABLE, self.prefix, getLogPath(), self.node.name)
        else:
            run_cmd = "{0} {1} {2}/{3}.log > {2}/stdout/{3}.log 2> {2}/stderr/{3}.log &".format(
                APP_EXECUTABLE, self.prefix, getLogPath(), self.node.name)

        ret = self.node.cmd(run_cmd)
        info("[{}] running \"{}\" == {}\n".format(
            self.node.name, run_cmd, ret))

class UAVClient(Application):
    """
    Wrapper class to run the UAVClient on a node
    """

    def __init__(self, node):
        Application.__init__(self, node)

    def start(self):
        if PROTO == "ip":
            run_cmd = "{0} {3} {2}/{3}.log :8333 store_{3} > {2}/stdout/{3}.log 2> {2}/stderr/{3}.log &".format(
                UAV_EXECUTABLE, "/uav", getLogPath(), self.node.name)
        else:
            run_cmd = "{0} > {1}/stdout/{2}.log 2> {1}/stderr/{2}.log &".format(
                UAV_EXECUTABLE, getLogPath(), self.node.name)

        ret = self.node.cmd(run_cmd)
        info("[{}] running \"{}\" == {}\n".format(
            self.node.name, run_cmd, ret))

if __name__ == '__main__':
    setLogLevel('info')

    Minindn.cleanUp()
    Minindn.verifyDependencies()

    ndn = MinindnWifi(link=wmediumd, wmediumd_mode=interference)

    uav = ndn.net["uav"]
    uav.coord = ['400,800,0',
                 '800,800,0', '800,200,0', '200,200,0', '200,800,0', '400,800,0',
                 '800,800,0', '800,200,0', '200,200,0', '200,800,0', '400,800,0',
                 '800,800,0', '800,200,0', '200,200,0', '200,800,0', '400,800,0',
                 '800,800,0', '800,200,0', '200,200,0', '200,800,0', '400,800,0',
                 '800,800,0', '800,200,0', '200,200,0', '200,800,0', '400,800,0']

    ndn.net.mobility(uav, 'start', time=0, position=uav.coord[0])
    ndn.net.mobility(uav, 'stop', time=SWITCH_TIME*4*5, position=uav.coord[-1])

    ndn.net.setPropagationModel(model="friis")

    ndn.net.stopMobility(time=SWITCH_TIME*4*5)
    ndn.startMobility(time=0, draw=False)

    info('Starting NFD on nodes\n')
    nfds = AppManager(ndn, ndn.net.stations, Nfd)
    info('Sleeping {} seconds\n'.format(NFD_SLEEP_TIME))
    time.sleep(NFD_SLEEP_TIME)

    info('Setting NFD strategy to multicast on all nodes with prefix')
    for node in tqdm(ndn.net.stations):
        Nfdc.setStrategy(node, "/ndn/", Nfdc.STRATEGY_MULTICAST)
        Nfdc.setStrategy(node, "/voice/", Nfdc.STRATEGY_MULTICAST)

        ln = [k.strip() for k in node.cmd('nfdc status report').split('\n') if 'faceid=' in k and 'wlan0' in k and 'dev://' in k][0]
        faceid = ((ln.split(' ')[0]).split('='))[1]
        print(faceid)
        node.cmd("nfdc route add prefix /ndn nexthop " + faceid)
        node.cmd("nfdc route add prefix /voice nexthop " + faceid)

    START_TIME = 0

    def writeStatus(prefix):
        for node in ndn.net.stations:
            if PROTO != 'ip':
                with open("{}/report-{}-{}.status".format(getLogPath(), prefix, node.name), "a") as f:
                    f.write("EVAL_TIME=={}\n".format(round(time.time() * 1000) - START_TIME))
                    f.write(node.cmd('nfdc status report'))
            with open("{}/report-{}-{}.ifconfig".format(getLogPath(), prefix, node.name), "a") as f:
                f.write("EVAL_TIME=={}\n".format(round(time.time() * 1000) - START_TIME))
                f.write(node.cmd('ifconfig'))

    for run_number in RUN_NUMBER_VALS:
        # Set globals
        RUN_NUMBER = run_number
        START_TIME = round(time.time() * 1000)

        # Clear content store
        for node in ndn.net.stations:
            ndn.net.addLink(node, cls=adhoc, intf=node.name+'-wlan0', ssid='adhocNet')

            if PROTO != 'ip':
                cmd = 'nfdc cs erase /'
                node.cmd(cmd)

        writeStatus('start')

        random.seed(RUN_NUMBER)

        info('UAV initially connects to AP0\n')

        # star moving
        ndn.start()

        info('Start units and UAV\n')
        AppManager(ndn, [unit for unit in ndn.net.stations if unit.name != 'uav'], PlatoonClient)
        AppManager(ndn, [ndn.net["uav"]], UAVClient)

        current_link = 1
        for i in range(0, (NUM_ROUNDS + 3) * PLATOONS + 1):
            if i == NUM_ROUNDS * PLATOONS:
                os.system('pkill -SIGINT ' + APP_EXECUTABLE.split('/')[-1])

            current_link = (current_link + 1) % PLATOONS

            sleep_time = SWITCH_TIME # if i < NUM_ROUNDS * PLATOONS else 2 * SWITCH_TIME
            sleep_start = time.time()
            while time.time() < sleep_start + sleep_time:
                time.sleep(STATUS_LOG_INTERVAL_MS / 1000)
                writeStatus('eval')

        # kill all
        os.system('killall ' + APP_EXECUTABLE.split('/')[-1])
        os.system('killall ' + UAV_EXECUTABLE.split('/')[-1])
        time.sleep(3)

        writeStatus('end')

    ndn.net.stop()
    ndn.cleanUp()


from math import pi
from sqlalchemy import func
from sqlalchemy.orm import column_property
from geoalchemy2.types import Geography
from geoalchemy2.shape import to_shape
from sih.extensions import db

STATES = (
    ('AC', u'Acre'),
    ('AL', u'Alagoas'),
    ('AP', u'Amapá'),
    ('AM', u'Amazonas'),
    ('BA', u'Bahia'),
    ('CE', u'Ceará'),
    ('DF', u'Distrito Federal'),
    ('ES', u'Espirito Santo'),
    ('GO', u'Goiás'),
    ('MA', u'Maranhão'),
    ('MT', u'Mato Grosso'),
    ('MS', u'Mato Grosso do Sul'),
    ('MG', u'Minas Gerais'),
    ('PA', u'Pará'),
    ('PB', u'Paraíba'),
    ('PR', u'Paraná'),
    ('PE', u'Pernambuco'),
    ('PI', u'Piauí'),
    ('RJ', u'Rio de Janeiro'),
    ('RN', u'Rio Grande do Norte'),
    ('RS', u'Rio Grande do Sul'),
    ('RO', u'Rondônia'),
    ('SC', u'Santa Catarina'),
    ('SP', u'São Paulo'),
    ('SE', u'Sergipe'),
    ('TO', u'Tocantins')
)

class City(db.Model):

    __tablename__ = 'cities'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    state = db.Column(db.String(2), nullable=False)
    boundary = db.Column(Geography('POLYGON'), index=True)
    center = db.Column(Geography('POINT'), index=True)

    def __str__(self):
        return u'{}/{}'.format(self.name, self.state)

    @property
    def boundary_shape(self):
        if self.boundary is not None:
            return to_shape(self.boundary)

    @property
    def center_shape(self):
        if self.center is not None:
            return to_shape(self.center)

class Basin(db.Model):

    __tablename__ = 'basins'

    ottocode = db.Column(db.String(12), primary_key=True)
    boundary = db.Column(Geography('POLYGON'), index=True)
    area = column_property(boundary.ST_Area())
    perimeter = column_property(boundary.ST_Perimeter())
    compactness_index = column_property(
        boundary.ST_Perimeter() * 0.282 /
        func.nullif(func.sqrt(boundary.ST_Area()), 0))

    @property
    def boundary_shape(self):
        if self.boundary is not None:
            return to_shape(self.boundary)


#!/usr/bin/env python
"""
Program to build vagrant box and add to vagrant boxes list

Usage:
    make.py [-hd]
    make.py [-pd] --interactive
    make.py [-pd] --config-json=CONFIG_JSON
    make.py [-pd] [--builder-name=NAME --box-name=BOX --media=ISO]
            [(--check-sum=CHECK_SUM --check-sum-type=CHECK_SUM_TYPE)]
            [--memory=MEMORY --disk-size=DISK_SIZE]
            [(--proxy=PROXY [--user-name=PROXY_USERNAME --password])]

Options:
    -h --help                                          Show this screen.
    -d --debug                                         Debug mode.
    -i --interactive                                   Take interactive input.
    -c CONFIG_JSON --config-json=CONFIG_JSON           Path to json configuration file.
    -p --print-config                                  Print a json configuration example.
    -n NAME --builder-name=NAME                        Name of the packer builder.
    -b BOX --box-name=BOX                              Name of the vagrant box.
    -m ISO --media=ISO                                 URL to ISO.
    -k CHECK_SUM --check-sum=CHECK_SUM                 Check sum for ISO.
    -t CHECK_SUM_TYPE --check-sum-type=CHECK_SUM_TYPE  Check sum type.
    -y MEMORY --memory=MEMORY                          Amount in megabytes.
    -z DISK_SIZE --disk-size=DISK_SIZE                 Size of boot disk in megabytes.
    -x PROXY --proxy=PROXY                             Proxy, with port, if needed.
    -u PROXY_USERNAME --user-name=PROXY_USERNAME       Proxy username, if needed.
    -s --password                                      Proxy password, if needed.

"""

import subprocess
import json
import sys
import getpass
import jinja2
from docopt import docopt

class VagrantBox():
    """ Class defining vagrant box """

    def __init__(self, args):
        self.args = args
        self.build_data = {
            "builder": {
                "name": "cent7",
                "iso_url": "http://centos.mirrors.tds.net/pub/linux/centos/7.7.1908/isos/x86_64/CentOS-7-x86_64-DVD-1908.iso",
                "iso_checksum": "9bba3da2876cb9fcf6c28fb636bcbd01832fe6d84cd7445fa58e44e569b3b4fe",
                "iso_checksum_type": "sha256",
                "memory": "512",
                "disksize": "16384"
                },
            "box_name": "CentOS",
            "proxy": "",
            "proxy_username": "",
            "proxy_password": ""
            }

    def collect_data(self):
        """ Collect data interactively from user """
        ans1 = input("Enter builder name [{}]: ".format(self.build_data["builder"]["name"]))
        if ans1 != "":
            self.build_data["builder"]["name"] = ans1

        ans2 = input("Enter ISO url [{}]: ".format(self.build_data["builder"]["iso_url"]))
        if ans2 != "":
            self.build_data["builder"]["iso_url"] = ans2

        ans3 = input("Enter ISO checksum [{}]: ".format(self.build_data["builder"]["iso_checksum"]))
        if ans3 != "":
            self.build_data["builder"]["iso_checksum"] = ans3

        ans4 = input("Enter ISO checksum type [{}]: ".format(self.build_data["builder"]["iso_checksum_type"]))
        if ans4 != "":
            self.build_data["builder"]["iso_checksum_type"] = ans4

        ans5 = input("Enter Vagrant box name [{}]: ".format(self.build_data["box_name"]))
        if ans5 != "":
            self.build_data["box_name"] = ans5

        ans6 = input("Enter desired amount of memory(MB) [{}]: ".format(self.build_data["builder"]["memory"]))
        if ans6 != "":
            self.build_data["builder"]["memory"] = ans6

        ans7 = input("Enter desired amount of disk(MB) [{}]: ".format(self.build_data["builder"]["disksize"]))
        if ans7 != "":
            self.build_data["builder"]["disksize"] = ans7

        ans8 = input("Enter proxy, i.e. http(s)://proxy.domain.com:port: ")
        if ans8 != "":
            self.build_data["proxy"] = ans8

        ans9 = input("Enter proxy user name: ")
        if ans9 != "":
            self.build_data["proxy_username"] = ans9

        ans10 = getpass.getpass(prompt="Enter proxy password: ")
        if ans10 != "":
            self.build_data["proxy_password"] = ans10

    def compile_data(self):
        """ Take data from arguments and create json object """
        if self.args["--builder-name"]:
            self.build_data["builder"]["name"] = self.args["--builder-name"]
        if self.args["--media"]:
            self.build_data["builder"]["iso_url"] = self.args["--media"]
        if self.args["--check-sum"]:
            self.build_data["builder"]["iso_checksum"] = self.args["--check-sum"]
        if self.args["--check-sum-type"]:
            self.build_data["builder"]["iso_checksum_type"] = self.args["--check-sum-type"]
        if self.args["--box-name"]:
            self.build_data["box_name"] = self.args["--box-name"]
        if self.args["--memory"]:
            self.build_data["builder"]["memory"] = self.args["--memory"]
        if self.args["--disk-size"]:
            self.build_data["builder"]["disksize"] = self.args["--disk-size"]
        if self.args["--proxy"]:
            self.build_data["proxy"] = self.args["--proxy"]
        if self.args["--user-name"]:
            self.build_data["proxy_username"] = self.args["--user-name"]
        if self.args["--password"]:
            self.build_data["proxy_password"] = getpass.getpass(prompt="Enter password: ")

    def make_data(self):
        """ Pull the data together into a json object """
        if self.args["--config-json"]:
            with open(self.args["--config-json"], "r") as json_file:
                self.build_data = json.load(json_file)
        elif self.args["--interactive"]:
            self.collect_data()
        self.compile_data()

        if self.args["--print-config"]:
            print(self.build_data)

    def get_data(self):
        """ Retrives build_data """
        return self.build_data

def craft_files(debug, data):
    """ Create files for build, add and test of box """
    print("Crafting files...")
    if debug:
        print(data)

    env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates/"))

    template = env.get_template("ks.cfg.j2")
    with open("ks.cfg", "w") as config_file:
        print(template.render(data=data), file=config_file)

    template = env.get_template("vbox.json.j2")
    with open("vbox.json", "w") as config_file:
        print(template.render(data=data), file=config_file)

    template = env.get_template("kitchen.yml.j2")
    with open(".kitchen.yml", "w") as config_file:
        print(template.render(data=data), file=config_file)

def build_box():
    """ Build virtualbox vagrant box image """
    try:
        subprocess.run(["packer", "build", "-color=false", "-force", "vbox.json"], check=True)
        return 0
    except subprocess.CalledProcessError as err:
        print("Error: Packer failed, {}", err)
        return 2

def add_box(box, name):
    """ Add vagrant box image to vagrant """
    try:
        subprocess.run(["vagrant", "box", "add", "--force", "--name=" + name,
                        "packer_" + box + "_virtualbox.box"], check=True)
        return 0
    except subprocess.CalledProcessError as err:
        print("Error: Vagrant box add failed, {}", err)
        return 4

def test_box():
    """ Test with test-kitchen """
    try:
        subprocess.run(["bundle", "install", "--path", "vendor/bundle"], check=True)
        subprocess.run(["kitchen", "test"], check=True)
        return 0
    except subprocess.CalledProcessError as err:
        print("Error: Kitchen testing failed, {}", err)
        return 8

def main(args):
    """ Main function """
    new_box_data = VagrantBox(args)
    new_box_data.make_data()
    json_data = new_box_data.get_data()
    if args["--debug"]:
        print(json_data)

    craft_files(args["--debug"], json_data)

    result = build_box()
    if result == 0:
        result += add_box(json_data["builder"]["name"], json_data["box_name"])
        if result == 0:
            return result + test_box()
        return result
    return result

if __name__ == "__main__":
    NO_ARGS = len(sys.argv) - 1 == 0
    ARGS = docopt(__doc__)
    if ARGS["--help"] or NO_ARGS:
        print(__doc__)
        exit(0)
    if ARGS["--debug"]:
        print(ARGS)
    exit(main(ARGS))


import gym
import time

def random_agent():
    for game in range(100):
        print("\n************************************************************")
        _ = env.reset()
        utility = 0
        for t in range(1000):
            print(f"\n--- Step #{t} - Episode #{game} ---\n")
            action = env.action_space.sample()
            _, _, reward, done = env.step(action)
            utility += reward
            print(f"\n--- Utility (sum of rewards) = {utility} ---\n")
            print(f"--- Game Over = {done} ---\n")
            env.render()
            time.sleep(1.0)
            print("\n-------------------------------------------")
            if (done):
                print("Episode done after {} timesteps.\n".format(t+1))
                break
        print("**************************************************************\n")

# Define environment
env = gym.make('MyPacman-v0')
observations = env.reset()

random_agent()


import bpy
import random
from math import radians
from mathutils import *

main = bpy.data
contxt = bpy.context

def clearCollections():
    # Delete All Collections
    for collection in bpy.data.collections:
        for obj in collection.objects:
            bpy.data.objects.remove(obj, do_unlink=True)
        bpy.data.collections.remove(collection)

def createCollections():
    # Create Collections
    defaultCollection = bpy.data.collections.new("Collection")
    cubesCollection = bpy.data.collections.new("Cubes")
    bpy.context.scene.collection.children.link(defaultCollection)
    defaultCollection.children.link(cubesCollection)

def createCubesInCollection():
    spacing = 1
    for x in range(10):
        for y in range(10):
            location = (x * spacing, y * spacing, 0)
            bpy.ops.mesh.primitive_cube_add(size=1,
                                            enter_editmode=False,
                                            align="WORLD",
                                            location=location,
                                            scale=(1, 1, 1))
            cube = bpy.context.active_object
            bpy.data.collections['Cubes'].objects.link(cube)
            bpy.context.scene.collection.objects.unlink(cube)

def addCubeKeyFrames():
    cubes = bpy.data.collections["Cubes"].objects
    timeOffset = 0
    for cube in cubes:
        cube.scale = [0, 0, 0]
        cube.keyframe_insert(data_path="scale", frame=1 + timeOffset)
        cube.scale = [1, 1, 5]
        cube.rotation_euler[0] += radians(45)
        cube.keyframe_insert(data_path="scale", frame=50 + timeOffset)
        cube.scale = [1, 1, 0.5]
        cube.rotation_euler[0] += radians(45)
        cube.keyframe_insert(data_path="scale", frame=70 + timeOffset)
        cube.scale = [1, 1, 1]
        cube.rotation_euler[0] += radians(45)
        cube.keyframe_insert(data_path="scale", frame=80 + timeOffset)
        timeOffset += random.randint(0, 1)

def addModifier():
    cubes = bpy.data.collections["Cubes"].objects
    for cube in cubes:
        cube.modifiers.new["My Modifier","SUBSURF"]
        cube.modifiers["My Modifier"].levels = 3

addModifier()


N = int(input())

for i in range(N):
    stack = []
    order = input()

    for j in order:
        if j == '(':
            stack.append(j)
        elif j == ')':
            if stack:
                stack.pop()
            else:  # 스택에 괄호가 없을경우 NO
                print("NO")
                break
    else:  # for else 문 사용, break 에 걸리지 않았을 때
        if not stack:  # break문으로 안끊기고 스택이 비어있다면 괄호가 다 맞는거다.
            print("YES")
        else:  # break안 걸렸더라도 스택에 괄호가 들어있다면 NO이다.
            print("NO")


__author__  = 'Jonatan Paschoal'
__version__ = '1.0.0'
__email__   = 'jonfisik@hotmail.com'
__status__  = 'Professor - Física & Matemática'
__date__    = '23/12/2021'
'''
Passando para json
'''
import json

#
##
#
carros_dictionary = {
    'marca': 'honda',
    'modelo': 'HRV',
    'cor': 'prata'
}
# dictionary -> objeto json

carros_list = ['honda','volkswagem','ford','fiat','chevrolet']
# list -> array json

carros_tuplas = ('honda','volkswagem','ford','fiat','chevrolet')
# tupla -> array json

print('')
carros_j = json.dumps(carros_dictionary)
print(carros_j)
print('')
carros_j = json.dumps(carros_tuplas)
print(carros_j)
print('')
carros_j = json.dumps(carros_list)
print(carros_j)
print('')
# indent: identa o dicionário, separators: troca os separadores, sort_keys=True: ordena em ordem crescente
carros_j = json.dumps(carros_dictionary,indent=6,separators=(': ', ' = '),sort_keys=True)
print(carros_j)
print('')


import os
import sys
import torch
import numpy as np
import pandas as pd
from PIL import Image
import geopandas as gpd
import geohash_hilbert as ghh
import matplotlib.pyplot as plt
torch.manual_seed(0)
np.random.seed(0)
#======
# make sure you have the right folder to the images and labels
# for reference labels here are just laser made masks
class KdTree:
    def __init__(self, bath_to_directory, parent_precision=4, child_precision=6, train_split=0.1, path_images='/Users/ridhaalkhabaz/Documents/mlds/images/', path_labels='/Users/ridhaalkhabaz/Documents/mlds/labels/'):
        ## please note that our
        self.direc = gpd.read_file(bath_to_directory).set_crs(3443, allow_override=True).to_crs(4326) #please not 3443 projection key is only becasue samples.geojson is corrupted
        self.direc['center'] = self.direc['geometry'].centroid
        self.parent_precision = parent_precision
        self.child_precision = child_precision
        self.tree = {}
        self.tree = self._init_tree()
        self.path_images = path_images
        self.path_labels = path_labels
    def _init_tree(self):
        part = {}
        n = len(self.direc)
        for i in range(n):
            ## find the record we hash
            rec = self.direc.iloc[i]
            center = rec['center']
            ## getting the lon, lat for the sample
            lng, lat = center.x, center.y
            ## geohash our sample
            hash_child = ghh.encode(lng, lat, precision=self.child_precision)
            hash_parent = ghh.encode(lng, lat, precision=self.parent_precision)
            ## index the parent node if it does not exist
            if part.get(hash_parent) is None:
                part[hash_parent] = {}
            ## index the parent node if it does not exist
            if part[hash_parent].get(hash_child) is None:
                part[hash_parent][hash_child] = i
        return part
    def _find_train_test_indxs(self, split):
        train_split = split
        trainig_input = []
        for key in self.tree:
            sub_tree = self.tree.get(key)
            subtree_n  = len(sub_tree)
            num_recs = int(train_split*subtree_n)
            keys_list = list(sub_tree.keys())
            samples_kys = np.random.choice(keys_list, num_recs)
            samples_indx = [sub_tree.get(ky) for ky in samples_kys]
            trainig_input.extend(samples_indx)
        return trainig_input
    def _get_item_idx(self, key):
        if len(key) > self.parent_precision:
            parent_key = key[:self.parent_precision]
            desired = self.tree[parent_key].get(key)
            return desired
        return 'check input'
    def _show_example(self, key, show_label=True):
        indx = self._get_item_idx(key)
        path_to_label = self.path_labels+'label_'+str(indx)+'.png'
        path_to_image = self.path_images+'imag_'+str(indx)+'.png'
        if show_label:
            label = np.array(Image.open(path_to_label))
            label[label > 0] = 255
            plt.imshow(label)
            plt.show()
        else:
            plt.imshow(Image.open(path_to_image))
            plt.show()
    def _get_subtree_indxs(self, key, breadth_search=False):
        desired_kys = [key]
        desired = []
        if breadth_search:
            desired_kys.extend(list(ghh.neighbours(key).values()))
        for key in desired_kys:
            desired.extend(self.tree[key].values())
        return desired
    def _get_key(self, idx):
        if not isinstance(idx, int):
            return None
        for key in self.tree.keys():
            if idx in list(self.tree[key].values()):
                for ky, indx in self.tree[key].items():
                    if idx == indx:
                        return ky
        return None
    def _get_subtree_sample_indx(self, key, ratio):
        indices = list(self.tree[key].values())
        n = len(indices)
        n_sams = int(ratio*n)
        return list(np.random.choice(indices, n_sams))

#-----
# make sure you have the right folder to the images and labels
class filter_data:
    def __init__(self, path_to_metadata='./buildings/samples_bld.geojson', path_to_texture='./samples_texture.geojson', path_to_images= '/Users/ridhaalkhabaz/Documents/mlds/images/'):
        self.df_meta = gpd.read_file(path_to_metadata)
        self.labels = [self._binary_map(indx) for indx in range(len(self.df_meta))]
        self.df_meta['label'] = self.labels
        self.df_text = gpd.read_file(path_to_texture)
        self.df_text['label'] = self.labels
        self.imgs_path = path_to_images
        self.num_recs = len(self.df_meta)
    def _binary_map(self, idx):
        if self.df_meta.iloc[idx]['FID']>0:
            return 1
        return 0
    def _get_label(self, idx):
        return self.df_meta.iloc[idx]['label']
    def _get_log_data(self, training, indices, indices_test):
        cols = list(self.df_text.columns)
        cols.remove('label')
        cols.remove('geometry')
        if training:
            df_train = self.df_text.loc[indices]
            df_test = self.df_text.loc[indices_test]
            x_train = np.array(df_train[cols], dtype=np.float32)
            x_test = np.array(df_test[cols], dtype=np.float32)
            y_train = np.array(df_train['label'], dtype=np.float32)
            y_test = np.array(df_test['label'], dtype=np.float32)
            return torch.tensor(x_train), torch.tensor(y_train), torch.tensor(x_test), torch.tensor(y_test)
        df_res = self.df_text.loc[indices]
        res = np.array(df_res[cols], dtype=np.float32)
        return torch.tensor(res)
    def _img_to_tensor(self, idx):
        img_path = self.imgs_path+'imag_'+str(idx)+'.png'
        img = np.array(Image.open(img_path))
        img_tensor = torch.tensor(img).to(torch.float32)
        img_tensor = torch.unsqueeze(img_tensor, 0)
        return torch.unsqueeze(img_tensor, 0)


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

import sonnet as snt

from ..long_board import white_piece as long_piece
from ..short_board import white_piece as short_piece
from ..direction import Direction, get_eight_directions
from ..naive_effect import LongEffectAllRangeLayer, ShortEffectLayer

__author__ = 'Yasuhiro'
__date__ = '2018/3/15'

class WhitePseudoOuEffect(snt.AbstractModule):
    def __init__(self, data_format, use_cudnn, name='white_ou_pseudo_effect'):
        super().__init__(name=name)
        self.data_format = data_format
        self.use_cudnn = use_cudnn

    def _build(self, board):
        """
        非手番側の王から仮想的な利きを伸ばす
        利きがどの駒に当たっているかで王手されているかを判定する

        :param board:
        :return:
        """
        outputs = {}

        # 8方向について仮想的な利きを計算する
        ou_long_piece = long_piece.select_white_ou(
            board=board, data_format=self.data_format
        )
        for direction in get_eight_directions():
            effect = LongEffectAllRangeLayer(
                direction=direction, data_format=self.data_format,
                use_cudnn=self.use_cudnn,
                name='white_pseudo_ou_long_{}'.format(direction.name)
            )(ou_long_piece)
            outputs[direction] = effect

        # 桂馬の動き
        ou_short_piece = short_piece.select_white_ou(board=board)
        for direction in (Direction.RIGHT_DOWN_DOWN, Direction.LEFT_DOWN_DOWN):
            effect = ShortEffectLayer(
                direction=direction, data_format=self.data_format,
                use_cudnn=self.use_cudnn,
                name='white_pseudo_ou_short_{}'.format(direction.name)
            )(ou_short_piece)
            outputs[direction] = effect

        return outputs


import numpy as np
from pathlib import Path
import pickle
import yaml
import sys
import scipy.optimize as syopt
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from matplotlib import rcParams

from analysis import stats
from analysis.bootstrap import bootstrap
from analysis.formatting import err_brackets
from analysis import fitfunc as ff

from common import read_pickle
from common import fit_value
from common import fit_value3
from common import read_correlators
from common import read_correlators2
from common import read_correlators3
from common import read_correlators4
from common import make_matrices
from common import gevp

from params import params

_metadata = {"Author": "Mischa Batelaan", "Creator": __file__}
_colors = [
    "#377eb8",
    "#4daf4a",
    "#f781bf",
    "#a65628",
    "#ff7f00",
    "#984ea3",
    "#999999",
    "#e41a1c",
    "#dede00",
]
_markers = ["s", "o", "^", "*", "v", ">", "<", "s", "s"]
# From the theta tuning:
m_N = 0.4179255
m_S = 0.4641829

def main():
    """ Save the summed correlators as bootstraps in pickle files for one fixed value of lamdba

    This is for the summer students so that they can practice diagonalising a matrix and fitting to a correlator to get the energy shift due to the feynman-hellmann modification.
    """

    pars = params(0) # Get the parameters for this lattice

    # Read in the directory data from the yaml file if one is given
    if len(sys.argv) == 2:
        config_file = sys.argv[1]
    else:
        config_file = "data_dir_theta2.yaml"
    print("Reading directories from: ", config_file)
    with open(config_file) as f:
        config = yaml.safe_load(f)

    # Set things to defaults defined in another YAML file
    with open("defaults.yaml") as f:
        defaults = yaml.safe_load(f)
    for key, value in defaults.items():
        config.setdefault(key, value)

    pickledir_k1 = Path(config["pickle_dir1"])
    pickledir_k2 = Path(config["pickle_dir2"])
    plotdir = Path(config["analysis_dir"]) / Path("plots")
    datadir = Path(config["analysis_dir"]) / Path("data")
    plotdir.mkdir(parents=True, exist_ok=True)
    datadir.mkdir(parents=True, exist_ok=True)

    # Read the correlator data from the pickle files
    mom_strings = ["p-1+0+0", "p+0+0+0", "p+1+0+0"]
    if "onlytwist" in config and config["onlytwist"]:
        G2_nucl, G2_sigm = read_correlators2(pars, pickledir_k1, pickledir_k2, mom_strings)
    elif "qmax" in config and config["qmax"]:
        G2_nucl, G2_sigm = read_correlators4(pars, pickledir_k1, pickledir_k2, mom_strings)
        print("qmax")
    else:
        print("else")
        G2_nucl, G2_sigm = read_correlators(pars, pickledir_k1, pickledir_k2, mom_strings)

    lambdas = np.array([0, 0.08])

    matrix4list = []
    for i, lmb_val in enumerate(lambdas):
        print(f"Lambda = {lmb_val}\n")
        # Construct a correlation matrix for each order in lambda(skipping order 0)
        matrix_1, matrix_2, matrix_3, matrix_4 = make_matrices(
            G2_nucl, G2_sigm, lmb_val
        )
        matrix4list.append(matrix_4)

    # Save the fit data to a pickle file
    all_data = {
        "nucleon_nucleon_l0": matrix4list[0][0][0],
        "sigma_sigma_l0": matrix4list[0][1][1],
        "nucleon_nucleon_lp08": matrix4list[1][0][0],
        "sigma_sigma_lp08": matrix4list[1][1][1],
        "nucleon_sigma_lp08": matrix4list[1][1][0],
        "sigma_nucleon_lp08": matrix4list[1][0][1],
    }
    with open(datadir / (f"summer_student_data.pkl"), "wb") as file_out:
        pickle.dump(all_data, file_out)

if __name__ == "__main__":
    main()


import unittest
import os
import logging
from activities.live_stream_state import LiveStreamState
from personality import Personality
from config_io import Configuration
import time

CURDIR=os.path.abspath(os.path.dirname(__file__))

class TestLiveStreamState(unittest.TestCase):
    def test_play(self):
        config = Configuration()
        p = Personality(config)
        state = LiveStreamState.create(config, p, {})
        state.on_enter();
        time.sleep(3)
        state.on_exit();

    def test_empty_list(self):
        config = Configuration()
        p = Personality(config)
        state = LiveStreamState.create(config, p, {})
        empty_playlist = {
            "playlist": []
        }
        state.json = empty_playlist
        state.on_enter();
        for attempt in range(0,2):
            time.sleep(3)
            logging.debug("attempting to move to next track in empty livestream")
            state.on_next_track_down()
            state.on_next_track_up()
        time.sleep(3)
        state.on_exit();

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


import sys, os, random, logging, shutil
import torch
from sklearn.metrics import confusion_matrix, roc_auc_score, accuracy_score, precision_score, recall_score, f1_score, cohen_kappa_score
import numpy as np

def model_validate(model, data_loader, writer, criterion, epoch, val_type):

    model.eval()
    with torch.no_grad():
        softmax = torch.nn.Softmax(dim=1)
        loss = 0
        label_list = []
        output_list = []
        pred_list = []

        for image,mask, label in data_loader:
            image = image.cuda().float()
            label = label.cuda().long()

            output = model(image)
            loss += criterion(output, label).item()

            _, pred = torch.max(output, 1)
            output_sf = softmax(output)

            label_list.append(label.cpu().data.numpy())
            pred_list.append(pred.cpu().data.numpy())
            output_list.append(output_sf.cpu().data.numpy())

        label = [item for sublist in label_list for item in sublist]
        pred = [item for sublist in pred_list for item in sublist]
        output = [item for sublist in output_list for item in sublist]

        acc = accuracy_score(label, pred)
        f1 = f1_score(label, pred, average='macro')

        # auc_ovr = roc_auc_score(label, output, average='macro', multi_class='ovr')
        auc_ovo = roc_auc_score(label, output, average='macro', multi_class='ovo')

        loss = loss / len(data_loader)

        if epoch>=0:
            writer.add_scalar('info/{}_accuracy'.format(val_type), acc, epoch)
            writer.add_scalar('info/{}_loss'.format(val_type), loss, epoch)
            # writer.add_scalar('info/{}_auc_ovr'.format(val_type), auc_ovr, epoch)
            writer.add_scalar('info/{}_auc_ovo'.format(val_type), auc_ovo, epoch)
            writer.add_scalar('info/{}_f1'.format(val_type), f1, epoch)
            logging.info('INFO:  {} - epoch: {}, loss: {}, acc: {}, auc: {}, F1: {}.'.format(val_type, epoch, loss, acc, auc_ovo, f1))

    model.train()
    return acc,auc_ovo,f1, loss


print("test")

print(100 + 200)

#name = input("Please input your name: ")
#print(name)

age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')

print(ord('A'))
print(chr(66))

classmates = ['Michael', 'Bob', 'Tracy']
print(classmates)
classmates.insert(1, 'Jack')
print(classmates)
classmates.pop()
print(classmates)
classmates.pop(1)
print(classmates)

names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)

n = 1
while n <= 100:
    if n > 3:
        break
    print(n)
    n = n + 1
print('END')

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
print(d['Michael'])

s = set([1, 2, 3])
print(s)
s.add(4)
print(s)

print(abs(-20))
print(int('123'))
def my_abs(x):
    if x >= 0:
        return x
    else:
        return -x
print(my_abs(-99))
import math
print(math.sqrt(2))

print([x * x for x in range(1, 11)])
print([x * x for x in range(1, 11) if x % 2 == 0])
print([m + n for m in 'ABC' for n in 'XYZ'])
import os
print([d for d in os.listdir('.')])
d = {'x': 'A', 'y': 'B', 'z': 'C' }
print([k + '=' + v for k, v in d.items()])
L = ['Hello', 'World', 'IBM', 'Apple']
print([s.lower() for s in L])

def f(x):
    return x * x
m = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(m)
def add(x, y):
    return x + y
r = reduce(add, [1, 3, 5, 7, 9])
print(r)

def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
        return ax
    return sum
f = lazy_sum(1, 3, 5, 7, 9)
print(f)
print(f())

def count():
    fs = []
    for i in range(1, 4):
        def f():
             return i*i
        fs.append(f)
    return fs

f1, f2, f3 = count()
print(f1())
print(f2())
print(f3())

def count1():
    def f(j):
        def g():
            return j*j
        return g
    fs = []
    for i in range(1, 4):
        fs.append(f(i))
    return fs
f1, f2, f3 = count1()
print(f1())
print(f2())
print(f3())

print(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

def log(func):
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)
    return wrapper
@log
def now():
    print('2015-3-25')
now()

import module
module.test()


import telebot
from telebot import types
import socket

telebot.apihelper.proxy = {'https': 'socks5h://14611055481:U777Vluhz8@orbtl.s5.opennetwork.cc:999'}

bot = telebot.TeleBot('1054005968:AAHyddoeLLrVqUkfUd1w02dUpoiVapwSasQ')

keyboard = types.ReplyKeyboardMarkup(True, True)
keyboard.row('1', '2', '3', '4', '5', '6')
connected_devices = 6

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id,'К вашей системе "Умный дом" подключено '+str(connected_devices)+' устройств\n'
                                    'Выберете устройтво для управления:',reply_markup=keyboard)
@bot.message_handler(content_types=['text'])
def send_text(message):
    for x in range(1,connected_devices+1):
        if message.text.lower() == str(x):
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            sock.connect(('192.168.0.177',80))
            st1='r'
            byt1=st1.encode()
            sock.send(byt1)
            st='1'
            byt=st.encode()
            sock.send(byt)
            print(x)
            print(sock.recv(1024))
            sock.close()
            #bot.send_message(message.chat.id,)

if __name__ == '__main__':
    bot.polling(none_stop=True)


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

class Solution:
    """
    Given the head of a linked list, remove the nth node from the end of the list and return its
    head.

    Constraints:
        The number of nodes in the list is sz.
        1 <= sz <= 30
        0 <= Node.val <= 100
        1 <= n <= sz
    """

    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        """
        One pass using two pointers, one with n steps delay.
        O(len(head)) / O(1)     time / space complexity
        """
        # skip the first n nodes
        curr = head
        for _ in range(n):
            curr = curr.next

        # if n == len(head) then curr == None, and we remove the first element
        if not curr:
            return head.next

        # create a pointer to the predecessor of the node to be removed, this pointer will always
        # point to n nodes to the left of curr, so when curr is pointing to the last node,
        # to_remove_pred will point to the (n+1)th last node
        to_remove_pred = head

        # keep walking down the list until curr is last node in the list
        while curr.next:
            curr = curr.next
            to_remove_pred = to_remove_pred.next

        # remove the nth node
        to_remove_pred.next = to_remove_pred.next.next
        return head

Medium/19. Remove Nth Node From End of List.py


import subprocess
import youtube_dl
import clearScreen as c

def fbVideoDownloard(link):
    c.clear_screen()
    print("Is it a public video? ")
    print("1. Yes")
    print("2. No")
    res = input()

    try:
        c.clear_screen()
        if res == '1':
            print("Downloading ...")
            with youtube_dl.YoutubeDL({}) as y:
                y.download([link])
            print("Download Complete!")

        else:
            print("Private video can't download in this version")

    except:
        print("There is an error. Please Run again")


import cv2
import numpy as np
from scipy.spatial import distance as dist
import argparse
import imutils
import copy

class robotClass:
    def __init__(self, pos = [], team = '-no team!-', ID = '-no ID!-'):
        # centre coordinates
        self.pos = pos
        self.team = team
        self.ID = ID
        # ID markings
        self.circles = [] # [x,y,color]

    def newMarking(self, circle = [0,0,[0,0,0]]):
        self.circles.append(circle)

class ballClass:
    def __init__(self, pos = []):
        self.pos = pos

roboList = []
roboIDmarks = []

# closestBot()
# x, Nov. yth, 2018
# This function identifies the nearest robot and returns the index of the robot within
# the list of robots (roboList) which the marking belongs to.
#              (input) -> (function) -> (output)
#               [x,y] -> closestBot() -> index
# v1:
# Unfinished. Needs distance calculations corrected, maybe a threshold for how close it
# needs to be...
def closestBot(circle):
    closestDist = 99999

    # Use Euclidean distance and cycle through roboList, comparing each of the
    # "pos" variables to the circle's [x,y] passed into the function
    for idx, pos in enumerate(roboList):
        newDist = dist.euclidean([circle[1],circle[2]],pos)
        if newDist < closestDist:
            closestDist = newDist
            closestIdx = idx

# colorID()
# E.H., Nov. 24th, 2018
# This function recognizes the colors of the recognized circle depending upon the BRG
# color values. Averaging of colors may also be implemented in this function.
#              (input) -> (function) -> (output)
#             (B, G, R) -> colorID() -> 'color'
# v1:
# **note: Implement some form of color averaging across the circle
#         This will improve the reliability of the system
#         (less false positive IDs on a robot)
def colorID(blue, green, red):
    color = 'X' # Default case, 'X' will print an error for an unrecognized element
    if (blue > 220 and green < 50 and red < 50):
        color = 'B' # Blue team circle
    elif (blue < 50 and green > 200 and red > 200):
        color = 'Y' # Yellow team circle
    elif (blue > 200 and green < 50 and red > 200):
        color = 'P' # Purple ID circle
    elif (blue < 50 and green > 220 and red < 50):
        color = 'G' # Green ID circle
    elif (blue < 50 and green < 200 and red > 220):
        color = 'O' # Ball!

    return color

# IDcircle()
# K.C. & E.H., Nov. 24th, 2018
# This function identifies any given circle based on its color and location. Although it
# does not return anything, it assigns the circle to its appropriate global variable
# (a robot object, ID mark list or ball object).
#              (input) -> (function) -> (output)
#               [x,y,r] -> ID_circle() -> none
# v1:
# Must implement all identifying functions. closestBot() is not fully developed, and must
# be added when completed.
def IDcircle(img, circle):
    x=int(circle[0])
    y=int(circle[1])

    blue = img[y,x,0]
    green = img[y,x,1]
    red = img[y,x,2]
    color = colorID(blue, green, red)
    print('Circle is ', color)

    roboList.append(robotClass())

    # if its blue or (if its yellow) --> Robot center/new robot
    if (color == 'B') or (color == 'Y'):
        roboList.append(robotClass([x,y],color))

    # if green or purple --> Robot ID marking
    elif (color == 'G') or (color == 'P'):
        roboIDmarks.append([x,y,color])

    # if orange --> Ball location
    elif (color == 'O'):
        ball = ballClass([x,y])

def assignIDmarks():
    if isinstance(roboList, type(None)):
        # Assign each robot its four closest marks
        for robot in roboList:
            closestMarks = [0,0,0,0] # indices of the closest four marks to the robot center
            furthestMark = [0,0] # [index, euclidean distance]

            # Assign this robot its four closest marks
            for i, mark in enumerate(list(roboIDmarks)):
                markDist = dist.euclidean(mark,robot.pos)

                # If there aren't already four marks given to the robot,
                # just give it whatever is available in order to initialize robot.circles[]
                if len(robot.circles) < 4:
                    robot.newMarking(mark)
                    closestMarks[i] = i

                # If there is a closer value than the furthest currently in robot.circles[]
                # replace the current furthest with this new one
                elif markDist < furthestMark[1]:
                    robot.circles[furthestMark[0]] = mark   # might run into issues altering values in the "robot" object,
                                                            # as it is the subject of the for loop
                    closestMarks[furthestMark[0]] = i

                # Update the furthest mark- done after the above code so no weird stuff happens
                if markDist > furthestMark:
                    furthestMark = [i, markDist]

            # Remove the marks that were assigned to a robot- this will potentially make
            # assigning the rest of the marks much quicker with larger numbers of robots
            for idx in closestMarks:
                del roboIDmarks[idx]
    else:
        print("No robots detected, but there are ID marks..?")

def main():
    #for i in range(10):
    #    new_robot = robot('B',i+2,1*i)
    #    roboList.append(new_robot)
    #    print(roboList[i].team,roboList[i].ID,roboList[i].x,roboList[i].y)

    #cball = ball([3,4])
    #print(cball.pos)

    #First part: Recognizing circles in sample image

    imgpath = "T:\\Documents\\University\\ECE 4553\\Project\\motherfucker.png"
    img = cv2.imread(imgpath)
    cv2.imshow("circles on image", img)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # smooth it, otherwise a lot of false circles may be detected
    blurred = cv2.GaussianBlur(img, (9,9), 0)
    blurred2 = cv2.GaussianBlur(gray, (9,9), 0)
    cv2.imshow("I wonder what blurred looks like...", blurred)
    cv2.imshow("I wonder what blurred looks like...2", blurred2)
    # Some notes on the HoughCircles function:
    #  - by adjusting param2, we can alter the threshold for the circles recognized
    #  - adjusting param1 does not seem to have an effect on the result as of now
    #  - the "1" scales the image for the function, "2" would scale down by 2
    circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,minDist = 20,
                               param1=50,param2=14,minRadius=15,maxRadius=50)
    cv2.waitKey()

    if isinstance(circles, type(None)) == 0:
        for circle in circles[0,:]:
            IDcircle(img, circle)

            # draw the outer circle
            cv2.circle(img,(circle[0],circle[1]),circle[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(img,(circle[0],circle[1]),2,(0,0,255),3)
            cv2.imshow("circles on image", img)
            cv2.waitKey()

        # Assign the ID marks observed to their appropriate robot
        if isinstance(roboIDmarks, type(None)) == 0:
            assignIDmarks()

    cv2.waitKey()
    cv2.destroyAllWindows()

    #Second part: recognizing circles in stream of images

    #cap = cv2.VideoCapture(0)
    #while True:
    #    ret, frame = cap.read()
    #    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #    # smooth it, otherwise a lot of false circles may be detected
    #    # ^This is likely why so many crazy circles happened- not enough
    #    # blurring of the image being received by the camera
    #    blurred = cv2.GaussianBlur(gray, (9,9), 0)
    #    circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,20,
    #                               param1=50,param2=30,minRadius=5,maxRadius=50)

    #    # so we can have streams of both original and circled version
    #    cstream = copy.deepcopy(frame)

    #    # This if statement prevents crashing when there are no circles recognized
    #    if isinstance(circles, type(None)) == 0:
    #        for circle in circles[0,:]:
    #            # circle(image,(x,y),radius,color,thickness?)
    #            # draw the outer circle
    #            cv2.circle(cstream,(circle[0],circle[1]),circle[2],(0,255,0),2)
    #            # draw the center of the circle
    #            cv2.circle(cstream,(circle[0],circle[1]),2,(0,0,255),3)

    #    cv2.imshow("circles on stream", cstream)
    #    cv2.imshow("original stream", frame)

    #    if cv2.waitKey(1) & 0xFF == ord('\r'):
    #        #cv2.destroyAllWindows()
    #        break

if __name__== "__main__":
    main()


#job 3:
#min = 5
#max = 10

#solucao x
#3list = [(1, 1), (1, 2), (1, 3), (5, 5), (3, 7), (3, 6), (2, 4), (1, 8), (3, 9), (4, 10), (5, 11), (1, 12)]

def correction(min, max, list):
    """Um algoritmo que ordena uma lista de tuplas a partir do valor min e maximo dado,
       utilizando o segundo filho da tupla

    Args:
        min (Int): primeira operacao no job
        max (Int): ultima operacao no job
        list (list): uma solucao gerada por crossover + mutacao

    Returns:
        list: uma solucao factivel
    """
    atual = min
    for key in list:
        #print(key[1])
        #ordencao sentinela
        if key[1] >= atual and key[1] <= max and key[1] != atual:
            list.sort(key=lambda tup: tup[1])
            #print('order needed: ', list)
            return list
        elif key[1] == atual and atual<max:
            atual = atual+1
    #print('order not needed: ', list)
    return list

#correction(min,max,list)


#Напишите программу, которая найдёт произведение пар чисел списка. Парой считаем первый и последний элемент, второй и предпоследний и т.д.
import random;
def result(arr):
    if(len(arr)>1):
        arr2.append(arr[0]*arr[len(arr)-1]);
        arr.pop(0);
        arr.pop(len(arr)-1);
        result(arr);
    else:
        return arr2;

arr = [];
arr2 = [];

for i in range(random.randint(5, 20)):
    rand = random.randint(2, random.randint(2, 10));
    arr.append(rand);

print(arr);
result(arr);
if(len(arr) == 1):
    print("Остаток - ", arr[0]);

print(arr2);


from django.urls import path
from . import views

app_name = 'user'

urlpatterns = [
   path('register/', views.RegisterView, name='register'),
   path('login/', views.UserLoginView, name='login'),
   path('logout/', views.LogoutView, name='logout'),
   path('profile/', views.UserProfileView, name='profile'),
   path('profile-form/', views.ProfileFormView, name='profile-form'),

]


import pulsar
import csv
f = open("Iris.csv", "rb")
reader = csv.reader(f)

client = pulsar.Client('pulsar://localhost:6650')

producer = client.create_producer('persistent://sample/standalone/ns1/my-topic')

# for i in range(10):
#     try:
#         producer.send('hello-Pulsar-%d' % i)
#     except Exception as e:
#         print("Failed to send message: %s", e)
for row in reader:
    d = json.dumps(row)
    producer.send(d)

producer.close()


def extractScatterdriftCom(item):
	'''
	Parser for 'scatterdrift.com'
	'''

	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol) or "preview" in item['title'].lower():
		return None

	tagmap = [
		('Legend of the Great Saint',       'Legend of the Great Saint',                      'translated'),
		('My Wife Is A Princess',           'My Wife Is A Princess',                      'translated'),
		('Kidnapping All Mankind',          'Kidnapping All Mankind',                      'translated'),
		('Destroyer of Ice and Fire',       'Destroyer of Ice and Fire',                   'translated'),
	]

	for tagname, name, tl_type in tagmap:
		if tagname in item['tags']:
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


from datetime import datetime
import numpy as np
import h5py
from .reader import Reader

class ReaderHDF(Reader):

    names = {
        'northward_velocity': '/Results/velocity V/velocity V_',
        'eastward_velocity': '/Results/velocity U/velocity U_',
        'spill_latitude': '/Results/spill/Latitude/Latitude_',
        'spill_longitude': '/Results/spill/Longitude/Longitude_',
        'spill_latitude_average': '/Results/spill/Latitude_average/Latitude_average_',
        'spill_longitude_average': '/Results/spill/Longitude_average/Longitude_average_',
        'spill_latitude_envelope': '/Results/spill/Latitude_envelope/Latitude_envelope_',
        'spill_longitude_envelope': '/Results/spill/Longitude_envelope/Longitude_envelope_',
        'air_concentration_2D': '/Results/spill origin 1/Data_2D/AirConcentration_2D/AirConcentration_2D_',
        'air_int_maximum_concentration_2D':
            '/Results/spill origin 1/Data_2D/AirIntMaximumConcentration_2D/AirIntMaximumConcentration_2D_'
    }

    def open(self):
        self.dataset = h5py.File(self.file)
        return self.dataset

    def close(self):
        self.dataset.close()

    def get_latitudes(self):
        lat_in = self.dataset['/Grid/Latitude']
        if len(lat_in.shape) == 1:
            self.n_latitudes = lat_in.shape[0]
            return lat_in
        elif len(lat_in.shape) == 2:
            self.n_latitudes = lat_in.shape[1]
            return lat_in[0, ]

    def get_longitudes(self):
        lon_in = self.dataset['/Grid/Longitude']
        if len(lon_in.shape) == 1:
            self.n_longitudes = lon_in.shape[0]
            return lon_in
        elif len(lon_in.shape) == 2:
            self.n_longitudes = lon_in.shape[0]
            return lon_in[:, 1]

    def get_dates(self):
        return self.dataset['/Time']

    def get_date(self, n_time):

        date_in = self.dataset['/Time/Time_' + str(n_time).zfill(5)]
        return datetime(year=int(date_in[0]), month=int(date_in[1]), day=int(date_in[2]),
                        hour=int(date_in[3]), minute=int(date_in[4]), second=int(date_in[5]))

    def get_variable(self, var_name, n_time=None):
        if n_time is None:
            return self.__get_var_without_time(var_name)
        return self.__get_var_with_time(var_name, n_time)

    def __get_var_with_time(self, var_name, n_time):
        path = self.names[var_name]
        variable = self.dataset[path + str(n_time).zfill(5)]
        if len(variable.shape) == 2:
            variable = np.transpose(variable)
        elif len(variable.shape) == 3:
            variable = np.transpose(variable, (0, 2, 1))
        return variable

    def __get_var_without_time(self, var_name):
        path = self.names[var_name]
        variable = self.dataset[path]
        if len(variable.shape) == 2:
            variable = np.transpose(variable)
        elif len(variable.shape) == 3:
            variable = np.transpose(variable, (0, 2, 1))
        return variable

    def get_ini_ntime(self):
        return 1


import threading
import urllib.request, urllib.parse, urllib.error
import time

# results - lista zawierająca liczbę wszystkich znaków podanych stron url
results = []

class thread(threading.Thread):
    def __init__(self, urls):
        super().__init__()
        self.urls = urls

    def run(self):
        self.urls = urls

        for u in urls:
            url_open = urllib.request.urlopen(u)
            results.append(len(url_open.read()))

#urls_num - liczba adresów url
#urls - lista adresów url
urls_num = 5
urls = []

# pobierz dane od użytkownika i zapisz je w liście
for i in range(urls_num):
    url = input("Podaj URL %d: " % (i+1))
    urls.append(url)

# dodatkowy wątek liczący znaki stron
aux_thread = thread(urls)

print("Rozpoczęto pobieranie.")
aux_thread.start()

while aux_thread.is_alive():
    print("Oczekiwanie na zakończenie pobierania...")
    time.sleep(1)

print("Zakończono pobieranie")

#wypisz dane użytkownikowi
for i in range(len(urls)):
    print(f"Liczba znaków dla {urls[i]}: {results[i]}")


# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pretend
import pytest

from fenrir.c.utils import Library

class TestLibrary:

    def test_doesnt_compile_on_creation(self):
        compile_module = pretend.call_recorder(lambda: None)
        ffi = pretend.stub(
            verifier=pretend.stub(
                compile_module=compile_module,
                _compile_module=compile_module,
            )
        )

        lib = Library(ffi)

        assert lib._lib is None
        assert compile_module.calls == []

    def test_prevents_compilation(self):
        compile_module = pretend.call_recorder(lambda: None)
        ffi = pretend.stub(
            verifier=pretend.stub(
                compile_module=compile_module,
                _compile_module=compile_module,
            )
        )

        lib = Library(ffi)

        with pytest.raises(RuntimeError):
            lib.ffi.verifier.compile_module()

        with pytest.raises(RuntimeError):
            lib.ffi.verifier._compile_module()

    def test_loads_lazily(self):
        foobar = pretend.stub()
        froblob = pretend.stub()
        loaded_lib = pretend.stub(foobar=foobar, froblob=froblob)
        ffi = pretend.stub(
            verifier=pretend.stub(
                load_library=pretend.call_recorder(lambda: loaded_lib),
            )
        )

        lib = Library(ffi)

        # Quick sanity check
        assert lib._lib is None
        assert lib.ffi.verifier.load_library.calls == []

        # Get some stuff from the underlying lib
        assert lib.foobar is foobar
        assert lib.froblob is froblob

        # Check that we've loaded the library (and only once)
        assert lib.lib is loaded_lib
        assert lib.ffi.verifier.load_library.calls == [pretend.call()]


import pandas as pd
import numpy as np
import sys

from os import listdir
from os.path import isfile, join, splitext
from tabulate import tabulate

def print_summary(file):
    df = pd.read_excel(file)
    df['Stream'].dropna()

    # Get month and year based on file name
    file_name = splitext(file)[0]
    date, month = file_name.split('-')
    year = date[-6:][:4]

    # Print summary table
    print("Summary as of", month, year)
    summary = get_summary(df)
    summary_columns = ['Retailer', 'Streams', 'Earnings(USD)']
    print(tabulate(summary, headers=summary_columns), '\n')

    # Print top country table
    print('Top Countries for', month, year)
    top_country = get_top_country(df)
    print(tabulate(top_country, showindex=False, headers=top_country.columns))

def get_summary(df):
    # Group by Retailer and calculate total stream and earnings
    retailer = df.groupby('Retailer').agg(Streams=('Stream','sum'), Earnings=('Earnings($)', 'sum'))
    # Add Total row at the end
    total = retailer.sum()
    total.name = "Total"
    retailer = retailer.append(total.transpose())
    # Format output
    retailer.Earnings = retailer.Earnings.round(2)
    retailer.Streams = retailer.Streams.apply(lambda x : "{:,}".format(int(x)))
    return retailer

def get_top_country(df):
    # Get rows with top streams based on retailer
    top_country = df.loc[df.groupby('Retailer')['Stream'].idxmax()]
    # Insert columns for printing into new dataframe
    df = pd.DataFrame({'Retailer':top_country['Retailer'], 'Country':top_country['Customer Territory'],
                        'Streams': top_country['Stream'].apply(lambda x : "{:,}".format(int(x)))})
    return df

if __name__ == "__main__":
    argc = len(sys.argv)
    if argc == 1:
        path = "dataset"
        files = [join(path, f) for f in listdir(path)]
        for file in sorted(files):
            print_summary(file)
            print('\n\n\n')
    else:
        for i in range(1, argc):
            print_summary(sys.argv[i])


"""
This is a simple Vision Transformer
patch-extractort -> patch-embedding +  positional embedding -> transformer encoding -> classification
"""

import tensorflow as tf

class PatchExtractor(tf.keras.layers.Layer):
    """
    suggested images of 224x224
    """
    def __init__(self):
        super(PatchExtractor, self).__init__()

    def call(self, images):
        batch_size = tf.shape(images)[0]
        patches = tf.image.extract_patches(
            images = images,
            sizes = [1, 16, 16, 1],
            strides = [1, 16, 16, 1],
            rates = [1, 1, 1, 1],
            padding = "VALID",
        )
        patch_dims = patches.shape[-1]
        patches = tf.reshape(patches, [batch_size, -1, patch_dims])
        return patches

class PatchEncoder(tf.keras.layers.Layer):
    def __init__(self, num_patches=196, projection_dim=768):
        super(PatchEncoder, self).__init__()
        self.num_patches = num_patches
        self.projection_dim = projection_dim
        w_init = tf.random_normal_initializer()
        class_token = w_init(shape=(1, projection_dim), dtype="float32")
        self.class_token = tf.Variable(initial_value=class_token, trainable=True)
        self.projection = tf.keras.layers.Dense(units=projection_dim)
        """
        here, we use embedding object, we should change to cosine-sine-based positional encoding or another one
        """
        self.position_embedding = tf.keras.layers.Embedding(input_dim=num_patches+1, output_dim=projection_dim)

    def call(self, patch):
        batch = tf.shape(patch)[0]
        # reshape the class token embedins
        class_token = tf.tile(self.class_token, multiples = [batch, 1])
        class_token = tf.reshape(class_token, (batch, 1, self.projection_dim))
        # calculate patches embeddings
        patches_embed = self.projection(patch)
        patches_embed = tf.concat([patches_embed, class_token], 1)
        # calcualte positional embeddings
        positions = tf.range(start=0, limit=self.num_patches+1, delta=1)
        positions_embed = self.position_embedding(positions)
        # add both embeddings
        encoded = patches_embed + positions_embed
        return encoded

class MLP(tf.keras.layers.Layer):
    def __init__(self, hidden_features, out_features, dropout_rate=0.1):
        super(MLP, self).__init__()
        self.dense1 = tf.keras.layers.Dense(hidden_features, activation=tf.nn.gelu)
        self.dense2 = tf.keras.layers.Dense(out_features)
        self.dropout = tf.keras.layers.Dropout(dropout_rate)

    def call(self, x):
        x = self.dense1(x)
        x = self.dropout(x)
        x = self.dense2(x)
        y = self.dropout(x)
        return y

class AttentionBlock(tf.keras.layers.Layer):
    def __init__(self, projection_dim, num_heads=4, dropout_rate=0.1):
        super(AttentionBlock, self).__init__()
        self.norm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
        self.attn = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=projection_dim, dropout=dropout_rate)
        self.norm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
        self.mlp = MLP(projection_dim * 2, projection_dim, dropout_rate)

    def call(self, x):
        # Layer normalization 1.
        x1 = self.norm1(x) # encoded_patches
        # Create a multi-head attention layer.
        attention_output = self.attn(x1, x1)
        # Skip connection 1.
        x2 = tf.keras.layers.Add()([attention_output, x]) #encoded_patches
        # Layer normalization 2.
        x3 = self.norm2(x2)
        # MLP.
        x3 = self.mlp(x3)
        # Skip connection 2.
        y = tf.keras.layers.Add()([x3, x2])
        return y

class TransformerEncoder(tf.keras.layers.Layer):
    def __init__(self, projection_dim, num_heads=4, num_blocks=12, dropout_rate=0.1, **kwargs):
        super(TransformerEncoder, self).__init__(**kwargs)
        self.blocks = [AttentionBlock(projection_dim, num_heads, dropout_rate) for _ in range(num_blocks)]
        self.norm = tf.keras.layers.LayerNormalization(epsilon=1e-6)
        self.dropout = tf.keras.layers.Dropout(0.5)

    def call(self, x ):
        # Create a [batch_size, projection_dim] tensor.
        for block in self.blocks:
            x = block(x)
        x = self.norm(x)
        y = self.dropout(x)
        return y

def create_vit(config_data, config_model):
    num_classes = config_data.get('N_CLASSES')
    num_patches = config_data.getint('CROP_SIZE') // 16
    num_patches = num_patches * num_patches
    projection_dim = config_model.getint('PROJECT_DIM')
    input_shape = (config_data.getint('CROP_SIZE'), config_data.getint('CROP_SIZE'), 3)
    inputs = tf.keras.layers.Input(shape=input_shape)
    # Patch extractor
    patches = PatchExtractor()(inputs)
    # Patch encoder
    patches_embed = PatchEncoder(num_patches, projection_dim)(patches)
    # Transformer encoder
    representation = TransformerEncoder(projection_dim, num_blocks=6, name = 'encoder')(patches_embed)
    """
    here, global average pooling is used, but the paper says using class embedding representations[:,0,:]
    """
    representation = tf.keras.layers.GlobalAveragePooling1D()(representation)
    # MLP to classify outputs
    logits = MLP(projection_dim, num_classes, 0.5)(representation)
    # Create model

    model = tf.keras.Model(inputs = inputs, outputs = tf.keras.layers.Softmax()(logits))
    return model


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 20:41:22 2019

@author: saeem
"""

import requests
import time
import json
import xlwt

workbook = xlwt.Workbook()

sheet = workbook.add_sheet("Data.xls")

while True:

    url = "https://maps.googleapis.com/maps/api/distancematrix/json"

    querystring = {"units":"metric","departure_time":str(int(time.time())),"traffic_model":"best_guess","origins":"ITPL,Bangalore","destinations":"Tin Factory,Bangalore","key":"GetYourKeyHere"}

    headers = {
        'cache-control': "no-cache",
        'postman-token': "something"
        }

    response = requests.request("GET", url, headers=headers, params=querystring)
    d = json.loads(response.text)
    #print("On", time.strftime("%I:%M:%S"),"time duration is",d['rows'][0]['elements'][0]['duration']['text'], " & traffic time is ",d['rows'][0]['elements'][0]['duration_in_traffic']['text'])

    for i  in range(0,100):
        j = 0
        sheet.write(i, j,time.strftime("%I:%M:%S"))
        j = j+1
        sheet.write(i, j,d['rows'][0]['elements'][0]['duration']['text'])
        j = j+1
        sheet.write(i, j,d['rows'][0]['elements'][0]['duration_in_traffic']['text'])

    workbook.save("sample.xls")

    #print(response.text)


from datetime import date
from fastapi import FastAPI, Depends, File, HTTPException, UploadFile, status, Query
from sqlalchemy.orm import Session

from src.repository.users import (
    user_cr_info,
    load_plans,
    check_mn_plans,
    check_yr_plans,
)
from src.database.db import get_db

app = FastAPI()

@app.get("/api/healthchecker")
def root():
    return {"message": "Welcome to Bank_API!"}

@app.get("/user_credits/{user_id}", status_code=status.HTTP_200_OK)
async def user_credits_info(user_id: int, db: Session = Depends(get_db)):

    res = await user_cr_info(user_id, db)
    if res is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
        )
    return res

@app.post("/plans_insert", status_code=status.HTTP_201_CREATED)
async def download_plans(file: UploadFile = File(None), db: Session = Depends(get_db)):
    if not file.filename.lower().endswith((".xlsx", ".xls")):
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail="Upload xlsx or xls file",
        )
    return await load_plans(file, db)

@app.get("/plans_performance", status_code=status.HTTP_200_OK)
async def check_month_plans(
    date_on: date = Query(None, description="Date in format 2022-02-22"),
    db: Session = Depends(get_db),
):
    plans = await check_mn_plans(date_on, db)
    if not plans:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Plans not found"
        )
    return plans

@app.get("/year_performance", status_code=status.HTTP_200_OK)
async def check_year_plans(
    year_on: str = Query(None, description="Year in format 2022"),
    db: Session = Depends(get_db),
):
    plans = await check_yr_plans(year_on, db)
    if not plans:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Plans not found"
        )
    return plans


import requests,re
from 模块.UA import UserAgent
from lxml import etree

class MUban():
    def __init__(self,url):
        self.html = self.request_url(url)
        self.parse_html()

    def request_url(self,url):
        headers = {
            'User-Agent': UserAgent()
        }
        html = requests.get(url,headers=headers).content.decode('utf-8')
        tree = etree.HTML(html)
        #tree = re.findall(r'',html)
        return tree

    def parse_html(self):
        diqu_link = self.html.xpath('//div[@class="sub_nav section_sub_nav"]//a/@href')
        diqu_name = self.html.xpath('//div[@class="sub_nav section_sub_nav"]//a/@title')

        with open('链家.txt','a',encoding='utf-8') as f:

            for i,j in zip(diqu_link,diqu_name):
                f.write('*************************{}**********************'.format(j)+'\n')

                if i[:5] != 'https':url = 'https://bj.lianjia.com'+i
                else:url = i

                for page in range(1,60):
                    f.write('####################第{}页#################'.format(page)+'\n')

                    urls = url + 'pg{}/'.format(page)
                    f.write(urls+'\n')

                    diqu_liebiaos = self.request_url(urls)
                    li = diqu_liebiaos.xpath('//div[@class="info clear"]')

                    for houseinfo in li:
                        housename = houseinfo.xpath('./div[@class="title"]/a/text()')[0]
                        f.write('房屋名称：'+housename+'\n')
                        houseinfo1 = houseinfo.xpath('./div[@class="address"]/div//text()')
                        houseinfo1 = '/'.join(houseinfo1)
                        f.write(houseinfo1+'\n')
                        houseinfo2 = houseinfo.xpath('./div[@class="flood"]//text()')
                        houseinfo2 = ''.join(houseinfo2)
                        f.write(houseinfo2+'\n')
                        houseinfo3 = houseinfo.xpath('./div[@class="followInfo"]//text()')
                        f.write(''.join(houseinfo3[:3])+'\n')
                        f.write('/'.join(houseinfo3[3:-5])+'\n')
                        f.write('/'.join(houseinfo3[-5:])+'\n\n\n')

if __name__ == '__main__':
    url = 'https://bj.lianjia.com/ershoufang/'
    MUban(url)


from aiogram import Dispatcher
from aiogram.dispatcher.handler import CancelHandler, current_handler
from aiogram.dispatcher.middlewares import BaseMiddleware
from aiogram.types import Message, CallbackQuery
from aiogram.utils.exceptions import Throttled

from loader import _, config

class ThrottlingMiddleware(BaseMiddleware):
    def __init__(self, limit: float = config.RATE_LIMIT, key_prefix: str = 'antiflood_'):
        self.rate_limit = limit
        self.prefix = key_prefix
        super(ThrottlingMiddleware, self).__init__()

    async def on_process_message(self, message: Message, data: dict[str]):
        await self._throttle(message, data)

    async def on_process_callback_query(self, query: CallbackQuery, data: dict[str]):
        await self._throttle(query.message, data)

    async def _throttle(self, message: Message, data: dict[str]):
        handler = current_handler.get()
        dispatcher = Dispatcher.get_current()
        if handler:
            limit = getattr(handler, 'throttling_rate_limit', self.rate_limit)
            key = getattr(handler, 'throttling_key', f'{self.prefix}_{handler.__name__}')
        else:
            limit = self.rate_limit
            key = f'{self.prefix}_message'
        try:
            await dispatcher.throttle(key, rate=limit)
        except Throttled as throttled:
            if throttled.exceeded_count <= 2:
                await message.reply(_('Too many requests!'))

            raise CancelHandler()