import argparse
import numpy as np

from maml import MAML
from Dynamics import Dynamics
import gym
from cost_functions import cheetah_cost_fn, ant_cost_fn
from my_envs.mujoco import *

import tensorflow as tf

import utils
from loginfo import log
import time
from controllers import MPCcontroller
from utils import configure_log_dir, Logger
from dataset.Gymdata import dataset

def argsparser():
    parser = argparse.ArgumentParser("Tensorflow Implementation of MAML")
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--env_name', type=str,
                        default='HalfCheetahEnvDisableEnv-v0')  # HalfCheetahEnvDisableEnv   HalfCheetahVaryingEnv
    # Dataset
    parser.add_argument('--dataset', help='environment ID', choices=['sin,gym'], default='gym',
                        required=False)
    # MAML
    parser.add_argument('--K', type=int, default=32)  # horizon num_paths
    parser.add_argument('--num_paths', type=int, default=1)  # num_paths
    parser.add_argument('--model_type', type=str, default='fc')
    parser.add_argument('--loss_type', type=str, default='MSE')
    parser.add_argument('--num_updates', type=int, default=1)
    parser.add_argument('--norm', choices=['None', 'batch_norm'], default='batch_norm')
    # Train
    parser.add_argument('--is_train', action='store_true', default=True)

    parser.add_argument('--beta', type=float, default=0.01)  # learning rate for new taks during update
    parser.add_argument('--max_epochs', type=int, default=5)  # for update_training using experience

    # Test
    parser.add_argument('--restore_checkpoint', type=str)
    parser.add_argument('--restore_dir', type=str,
                        default='Hyper/log-files/HalfCheetahEnvDisableEnv-v0/Jun-03_21:18:39Meta_Train_train_meta_KXX_alXX_LabExp1/Jun-03_21:18:39Meta_Train_train_meta_K32_up1_pa1_al0.01_be0.01_batch500_LabExp1/checkpoint/MAML.HalfCheetahEnvDisableEnv-v0_gym_32-shot_1-updates_500-batch_norm-batch_norm-EXP_Meta_Train_train_meta_K32_up1_pa1_al0.01_be0.01_batch500_LabExp1')

    parser.add_argument('--NumOfExp', type=int, default=32)
    parser.add_argument('--horizon',  type=int, default=1000)
    parser.add_argument('--num_itr', type=int, default=5)
    parser.add_argument('--task_goal', type=float, default=5)

    # MPC Controller
    parser.add_argument('--mpc_horizon', '-m', type=int, default=10)  # mpc simulation H  10
    parser.add_argument('--simulated_paths', '-sp', type=int, default=5000)  # mpc  candidate  K 10000

    parser.add_argument('--note', type=str, default='Control_MPC_EXP02')
    args = parser.parse_args()
    print(args)
    return args

def rollout(env,
            controller,
            dyn_model,
            task_goal,
            experiences,
            NumOfExp = 100,
            horizon=1000,
            cost_fn=cheetah_cost_fn,
            render=False,
            verbose=False,
            save_video=False,
            ignore_done=True,
            ):
    """
        Write a sampler function which takes in an environment, a controller (either random or the MPC controller),
        and returns rollouts by running on the env.
        Each path can have elements for observations, next_observations, rewards, returns, actions, etc.
    """
    paths = []

    """ YOUR CODE HERE """
    obs = env.reset(reset_args=task_goal)
    observations, actions, next_observations, rewards ,meta_train_losses= [], [], [], [],[]
    done = False
    t = 0

    start = time.time()  # caculate run time --start time point

    while not done:
        if render:
            env.render()
        obs = obs.astype(np.float64).reshape((1, -1))
        meta_train_loss = dyn_model.update(experiences)
        if meta_train_loss is not None:
            meta_train_losses.append(meta_train_loss)
        start = time.time()
        action = controller.get_action5(dyn_model, obs)  # highly time-consuming
        action = action.astype(np.float64).reshape((1, -1))
        end = time.time()
        runtime = end - start
        next_obs, reward, done, _ = env.step(action)

        end2 = time.time()
        runtime1 = end2 - end

        next_obs = next_obs.astype(np.float64).reshape((1, -1))
        current_experience = np.concatenate((obs, action, next_obs - obs), axis=1)
        obs = next_obs

        experiences.append(current_experience)

        if not isinstance(reward, float):
            reward = np.asscalar(reward)
        rewards.append(reward)

        if len(experiences) >  NumOfExp:
            del experiences[0]

        end3 = time.time()
        runtime2 = end3 - end2

        t += 1
        if not ignore_done:
            if done:
                print("Episode finished after {} timesteps".format(t + 1))
                break
        else:
            if t >= horizon:
                break

    end = time.time()
    runtime1 = end - start

    return sum(rewards), sum(meta_train_losses)/horizon

def main(args):
    tf.set_random_seed(args.seed)
    np.random.seed(args.seed)

    env_name = args.env_name  # HalfCheetah-v2  My3LineDirect-v1
    print(env_name)

    if args.env_name == 'HalfCheetahEnvDisableEnv-v0':
        cost_fn = cheetah_cost_fn
        sample_task_fun = np.random.randint
    elif args.env_name == 'HalfCheetahVaryingEnv-v0':
        cost_fn = cheetah_cost_fn
        sample_task_fun = np.random.uniform
    else:
        print('env is error!!! ')

    env = gym.make(env_name)
    dim_input = env.observation_space.shape[0]+env.action_space.shape[0]
    dim_output = env.observation_space.shape[0]

    logdir = configure_log_dir(logname=env_name, txt=args.note)
    # save args prameters
    with open(logdir + '/info.txt', 'wt') as f:
        print('Hello World!\n', file=f)
        print(args, file=f)

    mpc_horizon = args.mpc_horizon
    num_simulated_paths = args.simulated_paths #10000

    dyn_model = Dynamics(args.env_name,

                 args.NumOfExp,
                 args.model_type,
                 args.loss_type,
                 dim_input,
                 dim_output,
                 beta = args.beta,#args.beta,
                 max_epochs=args.max_epochs,
                 is_train =args.is_train,
                 norm = args.norm,
                 task_Note=args.note,
                 restore_checkpoint=args.restore_checkpoint,
                 restore_dir=args.restore_dir,
                 logdir = logdir
                 )

    mpc_controller = MPCcontroller(env=env,
                                   dyn_model=dyn_model,
                                   horizon=mpc_horizon,
                                   cost_fn=cost_fn,
                                   num_simulated_paths=num_simulated_paths,
                                   )
    logger = Logger(logdir, csvname='log')

    num_itr = args.num_itr
    experiences,costs =[],[]
    print('MPC is beginning...' )
    for itr in range(num_itr):
        reward, model_loss_mean = rollout(env, mpc_controller, task_goal = args.task_goal,
                       dyn_model=dyn_model,experiences=experiences, NumOfExp= args.NumOfExp,horizon=args.horizon, cost_fn=cheetah_cost_fn,
                render=False, verbose=False, save_video=False, ignore_done=True, )

        #print(time.asctime( time.localtime(time.time()) ), ' itr :', itr, 'Average reward :' , cost)
        log.infov("Itr {}/{} Accumulated Reward: {:.4f}  Model loss mean:{:.4f}".format(itr, num_itr,reward, model_loss_mean))

        logger.log({'itr': itr,
                    'Accumulated Reward': reward,
                    'Model loss mean': model_loss_mean,
                    })

    print('MPC is over....')

    logger.write(display=False)

if __name__ == '__main__':
    args = argsparser()
    main(args)


import socket
import threading

HEADER = 64
PORT = 5050
SERVER_IP_ADDRESS = socket.gethostbyname(socket.gethostname())
ADDRESS = (SERVER_IP_ADDRESS, PORT)
FORMAT = 'UTF-8'
DISCONNECT_MESSAGE = 'DISCONNECT'

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDRESS)

def handle_client(connection, address):
    print(f"New connection: {address} connected.")
    connected = True
    while connected:
        message_length = connection.recv(HEADER).decode(FORMAT)
        if message_length:
            message_int = int(message_length)
            message = connection.recv(message_int).decode(FORMAT)
            if message == DISCONNECT_MESSAGE:
                connected = False
                connection.send("You've been disconnected".encode(FORMAT))
            else:
                print(f"{address}: {message}")
                connection.send("Message Received".encode(FORMAT))

    connection.close()

def start():
    print("Server is starting...")
    server.listen()
    print(f"Server is listening on {SERVER_IP_ADDRESS}")
    while True:
        connection, address = server.accept()
        thread = threading.Thread(target=handle_client, args=(connection, address))
        thread.start()
        print(f"Active connections: {threading.activeCount() - 1}")

start()


#!/usr/bin/env python
# (c) Stefan Countryman, 2017

DESC = """Find all frame files of a given frame type in a given time range on
a remote LIGO server and download them to the specified output directory,
skipping any files that have already been downloaded."""
PROG_BAR_WIDTH = 40
VERBOSE = True  # default to verbose for interactive sessions
DEFAULT_H_FRAMETYPES = []
DEFAULT_L_FRAMETYPES = []
DEFAULT_V_FRAMETYPES = []
DEFAULT_FRAME_LENGTH = 64
DEFAULT_SERVER = 'ldas-pcdev2.ligo.caltech.edu'
DEFAULT_OUTDIR = '.'
_TARGETED_SEARCH_FRAMETYPE_DICT_CIT = {
    "H1_HOFT_C02":  "hoft_C02/H1",
    "L1_HOFT_C02":  "hoft_C02/L1",
    "H1_R":         "raw/H1",
    "L1_R":         "raw/L1"
}
_TARGETED_SEARCH_SERVERS_CIT = ["ldas-pcdev{}.ligo.caltech.edu".format(i)
                                for i in range(1,6)]
# map tuples of (observing run, frametype, server) to directories
# containing frames on the remote server for fallback frame searches.
_TARGETED_SEARCH_DIRECTORIES = dict()
_TARGETED_SEARCH_DIR_FMT_CIT = "/hdfs/frames/{}/{}"
for run in ["O1", "O2"]:
    for server in _TARGETED_SEARCH_SERVERS_CIT:
        for frametype in _TARGETED_SEARCH_FRAMETYPE_DICT_CIT:
            key = (run, frametype, server)
            path = _TARGETED_SEARCH_DIR_FMT_CIT.format(
                run,
                _TARGETED_SEARCH_FRAMETYPE_DICT_CIT[frametype]
            )
            _TARGETED_SEARCH_DIRECTORIES[key] = path
_BLUE = '\033[94m'
_CLEAR = '\033[0m'
_COMPLAINT = "{}---[{{}}]---{}\n{{}}\n".format(_BLUE, _CLEAR)

# all other imports listed after argument parsing, allowing for fast help
# documentation printing.
import sys
from subprocess import Popen, PIPE
if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description=DESC)
    parser.add_argument(
        "-T",
        "--times",
        action="store_true",
        help="""
            If provided, {} will read a list of (start, stop) times from STDIN
            and will attempt to download frames for these time intervals
            (instead of time intervals specified by the ``--start`` and
            ``--deltat`` arguments, which are ignored if ``--times`` is
            specified).
        """.format(sys.argv[0])
    )
    parser.add_argument(
        "-r",
        "--retries",
        type=int,
        default=0,
        help="""
            In the event of an error, retry an additional ``retries`` number of
            times. By default, do not retry (i.e. ``retries = 0``). To keep
            retrying an infinite number of times, specify any negative number.
        """
    )
    parser.add_argument(
        "-t",
        "--start",
        type=int,
        help="""
            The starting GPS time for this dump. Will be rounded down to the
            nearest even multiple of ``--length``, since the frame files all
            start on multiples of ``--length`` seconds GPS time.
        """
    )
    parser.add_argument(
        "-l",
        "--length",
        default=DEFAULT_FRAME_LENGTH,
        type=int,
        help="""
            The duration of each frame file. The script is dumb and will only
            try to find files assuming this length. DEFAULT: {}
        """.format(DEFAULT_FRAME_LENGTH)
    )
    parser.add_argument(
        "-p",
        "--progress",
        action="store_true",
        help="""
            Don't bother downloading; instead, just try to show how much
            progress has been made on this job based on the files it finds.
        """
    )
    parser.add_argument(
        "-d",
        "--deltat",
        type=int,
        help="""
            The length of the time window in which to seek frame files,
            measured in seconds. For example, 86400 seconds is a whole day.
        """
    )
    parser.add_argument(
        "-s",
        "--server",
        default=DEFAULT_SERVER,
        help="""
            The URL of the server on which to perform a ``gw_data_find`` query
            followed by a frame file download. DEFAULT: {}
            """.format(DEFAULT_SERVER)
    )
    parser.add_argument(
        "-o",
        "--outdir",
        default=DEFAULT_OUTDIR,
        help="""
            The default directory in which to save downloaded frame files.
            DEFAULT: {}
            """.format(DEFAULT_OUTDIR)
    )
    parser.add_argument(
        "-H",
        "--hanford-frametypes",
        nargs="*",
        default=DEFAULT_H_FRAMETYPES,
        help="""
            The frametypes to download for the Hanford detector (LHO). These
            can be things like raw files (H1_R) or increasingly calibrated
            strain files (H1_HOFT_C00, H1_HOFT_C01, or H1_HOFT_C02). DEFAULT:
            {}
            """.format(DEFAULT_H_FRAMETYPES)
    )
    parser.add_argument(
        "-L",
        "--livingston-frametypes",
        nargs="*",
        default=DEFAULT_L_FRAMETYPES,
        help="""
            The frametypes to download for the Livingston detector (LLO). These
            can be things like raw files (L1_R) or increasingly calibrated
            strain files (L1_HOFT_C00, L1_HOFT_C01, or L1_HOFT_C02). DEFAULT:
            {}
            """.format(DEFAULT_L_FRAMETYPES)
    )
    parser.add_argument(
        "-V",
        "--virgo-frametypes",
        nargs="*",
        default=DEFAULT_V_FRAMETYPES,
        help="""
            The frametypes to download for Virgo. DEFAULT:
            {}
            """.format(DEFAULT_V_FRAMETYPES)
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="Print diagnostic information to STDERR."
    )
    args = parser.parse_args()
    VERBOSE = args.verbose
if VERBOSE:  # if verbose, print everything
    _PIPE_ARGS = {"stdout": PIPE}
else:
    _PIPE_ARGS = {"stdout": PIPE, "stderr": PIPE}

import filecmp
import numpy as np
import collections
from datetime import datetime
import time
import os

class GWDataFindException(Exception):
    """An error thrown when ``gw_data_find`` on the remote server fails."""

class GWDataDownloadException(Exception):
    """An error thrown when ``gsiscp`` fails to download the desired data."""

class GWRemoteSha256Exception(Exception):
    """An error thrown when ``sha256sum`` fails on the remote server."""

class GWLocalSha256Exception(Exception):
    """An error thrown when ``sha256sum`` fails locally."""

class FileNameParsingError(Exception):
    """An error thrown when a filename that must be parsed does not follow the
    expected format. The message should contain some information about the
    unparsable filename to aid in debugging."""

class TargetedSearchException(Exception):
    """An exception raised when a targeted search cannot be performed due to a
    lack of target directories."""

def time_since_file_modified(filename):
    """Get the elapsed time in seconds since a file was modified."""
    return time.time() - os.path.getmtime(filename)

def complain(*messages):
    """Write a message to stderr if running in interactive mode or if the
    ``--verbose`` flag is set. Otherwise, throw away the message. If multiple
    messages are provided, join them with newlines."""
    msg = '\n'.join([format(m) for m in messages])
    if VERBOSE:
        formatted_message = _COMPLAINT.format(datetime.now().isoformat(), msg)
        sys.stderr.write(formatted_message)

class RemoteFileInfo(object):
    """A container holding data about a remote frame file (based on its
    filename as returned by ``gw_data_find``) along with convenience methods
    for generating a proper local file name.

    It is assumed that URLs returned by ``gw_data_find`` look like:

    file://localhost/hdfs/frames/O2/hoft_C02/H1/H-H1_HOFT_C02-11869/H-H1_HOFT_C02-1186959360-4096.gwf

    """

    def __init__(self, gw_data_find_response):
        """Initialize using the ``gw_data_find`` file URL string. Strips
        surrounding whitespace from the remote filename."""
        self.gw_data_find_response = gw_data_find_response.strip()

    FILE_URL_PREFIX = "file://localhost"

    @property
    def fullpath(self):
        """Return the full path on the remote server (removing the
        "file://localhost" prefix from the response string)."""
        return self.gw_data_find_response.replace(self.FILE_URL_PREFIX, '')

    @property
    def filename(self):
        """Get the remote filename without the containing directory."""
        return os.path.basename(self.fullpath)

    @property
    def gps_start_time(self):
        """Get the GPS start time of this frame file."""
        try:
            return self.filename.split('.')[0].split('-')[2]
        except IndexError as e:
            msg = 'Cannot get GPS start time from filename: ' + self.filename
            complain(msg)
            raise FileNameParsingError(msg)

    @property
    def frame_duration(self):
        """Get the duration in seconds of this frame file."""
        try:
            return self.filename.split('.')[0].split('-')[3]
        except IndexError as e:
            msg = 'Cannot get frame duration from filename: ' + self.filename
            complain(msg)
            raise FileNameParsingError(msg)

class GWFrameQuery(object):
    """An object specifying the detector, frametype, frame start time, output
    directory, and server where remote data is stored for a GW frame.  Used to
    check if the frame file exists, and, if it doesn't, to find the file on a
    remote server and download it."""

    def __init__(self, detector, frametype, gpstime,
                 framelength=DEFAULT_FRAME_LENGTH, server=DEFAULT_SERVER,
                 outdir=DEFAULT_OUTDIR):
        self.detector = detector
        self.frametype = frametype
        self.gpstime = gpstime
        self.framelength = framelength
        self.server = server
        self.outdir = outdir

    _FILENAME_FORMAT = '{}-{}-{}-{}.gwf'

    _GW_DATA_FIND_QUERY_FMT = """gw_data_find \\
        --observatory {} \\
        --type {} \\
        --gps-start-time {} \\
        --gps-end-time {} \\
        --url-type file
    """

    _TARGETED_SEARCH_QUERY_FMT = "find {} -name {}"

    _TARGETED_SEARCH_DIRECTORIES = _TARGETED_SEARCH_DIRECTORIES

    _OBSERVING_RUNS = {
        "ER8": (1126051217, 1126627217),
        "O1": (1126627217, 1137258496),
        "O2": (1164556817, 1187733618)
    }

    _SHA256_SUM_FMT = "sha256sum '{}'"

    def execute_cmd_over_ssh(self, cmd, exception=Exception):
        """SSH into this Query's server and run ``cmd``, capturing and
        returning a tuple containing (stdout, stderr) and throwing the
        specified ``exception`` type in the event of a nonzero return code."""
        fullcmd = ['gsissh', self.server, cmd]
        proc = Popen(fullcmd, stdout=PIPE, stderr=PIPE)
        res, err = proc.communicate()
        complain("RETVAL:", proc.returncode, "STDOUT:", res, "STDERR:", err)
        if proc.returncode != 0:
            raise exception("Something went wrong: {}".format(err))
        return (res, err)

    @property
    def observing_run(self):
        """Get the observing run for this query by looking at the time window
        it belonged to. Returns ``None`` if no matching run is found."""
        for run in self._OBSERVING_RUNS:
            interval = self._OBSERVING_RUNS[run]
            if (interval[0] < self.gpstime) and (self.gpstime < interval[1]):
                return run
        return None

    @property
    def estimated_filename(self):
        """What the filename *should* be assuming that the frame length of
        each frame file on the *remote server* is the same as the
        ``framelength`` specified in this query object."""
        return self._FILENAME_FORMAT.format(
            self.detector,
            self.frametype,
            self.gpstime,
            self.framelength
        )

    @property
    def estimated_fullpath(self):
        """Again, what the full path *should* be. See
        ``estimated_filename``."""
        return os.path.join(self.outdir, self.estimated_filename)

    def estimated_fullpath_exists(self):
        """Has this frame file already been downloaded? Do a naive check for
        what we *think* the filename is, though this might actually be
        different if the frames on the remote server have different frame
        lengths than what we estimated for this query in ``framelength``."""
        return os.path.isfile(self.estimated_fullpath)

    def local_filename_from_remote(self, remote_url):
        """Get the local filename based on the filename of the remote URL. This
        might not be the expected filename, so we need to check."""
        remote_file_info = RemoteFileInfo(remote_url)
        return self._FILENAME_FORMAT.format(
            self.detector,
            self.frametype,
            remote_file_info.gps_start_time,
            remote_file_info.frame_duration
        )

    def local_fullpath_from_remote(self, remote_url):
        """Get the local full path based on the filename of the remote URL.
        This might not be the expected full path, so we need to check."""
        return os.path.join(
            self.outdir,
            self.local_filename_from_remote(remote_url)
        )

    LOCAL_RIDER_TYPES = [
        'remote_sha256',
        'local_sha256',
        'query_repr',
        'error_msg',
        'remote_url'
    ]
    RIDER_FORMAT = '.{}.{}.txt'
    LocalRiders = collections.namedtuple('LocalRiders', LOCAL_RIDER_TYPES)

    def estimated_rider_fullpaths(self):
        """Get a ``LocalRiders`` namedtuple specifying the file paths to
        rider files for this query. These files contain metadata about the
        remote download, specifically, the remote and local sha256 sums, the
        file query used to generate the file, and the remote_url originally
        returned by gw_data_find. This is based on the *expected* filename, not
        the actual filename as determined by ``gw_data_find``."""
        filename = self.estimated_filename
        rider_filenames = [
            self.RIDER_FORMAT.format(
                filename,
                rider_type
            ) for rider_type in self.LOCAL_RIDER_TYPES
        ]
        rider_fullpaths = [
            os.path.join(self.outdir, fname) for fname in rider_filenames
        ]
        return self.LocalRiders(*rider_fullpaths)

    def local_rider_fullpaths_from_remote(self, remote_url):
        actual_local_filename = self.local_filename_from_remote(remote_url)
        rider_filenames = [
            self.RIDER_FORMAT.format(
                actual_local_filename,
                rider_type
            ) for rider_type in self.LOCAL_RIDER_TYPES
        ]
        rider_fullpaths = [
            os.path.join(self.outdir, fname) for fname in rider_filenames
        ]
        return self.LocalRiders(*rider_fullpaths)

    def local_fullpath_from_remote_exists(self, remote_url):
        """Check whether the local file corresponding to the remote_url exists.
        The remote_url might have an unexpected filename due to e.g. differing
        frame durations, so we need to check this."""
        return os.path.isfile(self.local_fullpath_from_remote(remote_url))

    def remote_url_targeted_search(self):
        """If the remote_url search fails, we can try a targetted search
        instead for certain frame types and time periods."""
        run = self.observing_run
        if run == None:
            raise TargetedSearchException("Cannot determine run.")
        key = (run, self.frametype, self.server)
        try:
            searchdir = self._TARGETED_SEARCH_DIRECTORIES[key]
        except KeyError:
            raise TargetedSearchException("No search target dir defined.")
        query = self._TARGETED_SEARCH_QUERY_FMT.format(
            searchdir,
            self.estimated_filename
        )
        res, err = self.execute_cmd_over_ssh(query, TargetedSearchException)
        try:
            return res.split('\n')[0].strip()
            complain("Targeted search found file for {}".format(self))
        except IndexError:
            raise TargetedSearchException("No remote file found.")

    def remote_url(self):
        """Get the path to this frame file on the remote server. First, try a
        search using ``gw_data_find``; if this fails, as often happens, do a
        manually-targetted search."""
        query = self._GW_DATA_FIND_QUERY_FMT.format(
            self.detector,
            self.frametype,
            self.gpstime,
            self.gpstime
        )
        res, err = self.execute_cmd_over_ssh(query, GWDataFindException)
        remote_url = res.strip().replace('file://localhost', '')
        if remote_url == '':
            msg = '{} not found using gw_data_find. Trying targetted search.'
            complain(msg.format(self))
            try:
                remote_url = self.remote_url_targeted_search()
            except TargetedSearchException as e:
                msg = 'Targeted search failed, returning "" for {}: {}'
                complain(msg.format(self, e))
                return ''
        return remote_url

    def remote_sha256(self, remote_url=None):
        """Get the sha256 sum for the file specified in ``self.remote_url()``
        and write it to a rider file (if the rider file does not already
        exist). Optionally override the ``remote_url`` argument, for example if
        the remote URL (as returned by ``gw_data_find``) has already been
        fetched."""
        # if no remote URL specified, find it automatically
        if remote_url == None:
            remote_url = self.remote_url()
        remote_fullpath = RemoteFileInfo(remote_url).fullpath
        sha256cmd = self._SHA256_SUM_FMT.format(remote_fullpath)
        try:
            res, err = self.execute_cmd_over_ssh(sha256cmd,
                                                 GWRemoteSha256Exception)
        except GWRemoteSha256Exception:
            errtime = datetime.utcnow().isoformat()
            errfmt = "REMOTE_SHA256 ERROR at {}. STDERR: \n{}\n"
            errmsg = errfmt.format(errtime, err)
            raise GWRemoteSha256Exception(errmsg)
        return res.split()[0]

    def local_sha256(self, remote_url=None):
        """Get the sha256 sum for the *local* file specified by
        ``self.remote_url()`` and write it to a rider file (if the rider file
        does not already exist). Optionally override the ``remote_url``
        argument, for example if the remote URL (as returned by
        ``gw_data_find``) has already been fetched."""
        # if no remote URL specified, find it automatically
        if remote_url == None:
            remote_url = self.remote_url()
        fullpath = self.local_fullpath_from_remote(remote_url)
        cmd = ['sha256sum', fullpath]
        complain("Running command in subprocess:", cmd)
        proc = Popen(cmd, **_PIPE_ARGS)
        res, err = proc.communicate()
        complain("RETVAL:", proc.returncode, "STDOUT:", res, "STDERR:", err)
        if proc.returncode != 0:
            errtime = datetime.utcnow().isoformat()
            errfmt = "LOCAL_SHA256 ERROR at {}. STDERR: \n{}\n"
            errmsg = errfmt.format(errtime, err)
            raise GWLocalSha256Exception(errmsg)
        return res.split()[0]

    def local_file_corrupted(self, remote_url=None):
        """Check whether the local downloaded file is corrupt, e.g. due to an
        interrupted download or disk corruption. Returns ``True`` if the file
        is corrupted, ``False`` otherwise. Figures out the local filename
        from the ``self.remote_url()``. Optionally, override this check by
        providing the remote URL returned by ``gw_data_find`` on the remote
        server (to save time if it has already been calculated)."""
        if remote_url == None:
            remote_url = self.remote_url()
        riders = self.local_rider_fullpaths_from_remote(remote_url)
        try:
            with open(riders.remote_sha256, 'r') as f:
                remote_sha256 = f.read()
            with open(riders.local_sha256, 'r') as f:
                local_sha256 = f.read()
            if local_sha256 != remote_sha256:
                return True
        except IOError:
            return True
        # if we made it to the end, checks have passed and the file is not
        # corrupt
        return False

    def download(self):
        """Download the file specified in ``self.remote_url()`` from the
        remote server. The remote file might actually have a different filename
        than what is expected, particularly if the user has incorrectly
        guessed the frame duration, so an extra check is made to see if the
        local filename differs. Also write the ``self.remote_url()`` to a rider
        file for future reference."""
        remote_url = self.remote_url()
        # we might not be able to parse the path if the remote file does not
        # exist. in this case, log the error and move on to the next file.
        try:
            local_fullpath = self.local_fullpath_from_remote(remote_url)
        except FileNameParsingError as e:
            estimated_riders = self.estimated_rider_fullpaths()
            with open(estimated_riders.error_msg, 'a') as f:
                f.write(e.args[0] + '\n')
            raise e
        riders = self.local_rider_fullpaths_from_remote(remote_url)
        # Check whether the file has been partially downloaded or is corrupt.
        # If it is corrupt, delete it so that it can be redownloaded.
        if os.path.isfile(local_fullpath):
            if self.local_file_corrupted(remote_url):
                #  check whether the file was modified in the last minute.  If
                #  so, then it might still be an active download and should be
                #  left alone.
                if time_since_file_modified(local_fullpath) > 60:
                    errtime = datetime.utcnow().isoformat()
                    errfmt = "CORRUPT FILE at {}. DELETING AND PROCEEDING.\n"
                    errmsg = errfmt.format(errtime)
                    with open(riders.error_msg, 'a') as f:
                        f.write(errmsg)
                    os.remove(local_fullpath)
        # only download the file if it does not exist locally.
        if not os.path.isfile(local_fullpath):
            download_url = '{}:{}'.format(self.server, remote_url)
            # record the remote url for debugging and record-keeping
            with open(riders.remote_url, 'w') as f:
                f.write(remote_url)
            # record a representation of this query
            with open(riders.query_repr, 'w') as f:
                f.write(repr(self))
            cmd = [
                'gsiscp',
                download_url,
                local_fullpath
            ]
            complain("Running command in subprocess:", cmd)
            proc = Popen(cmd, **_PIPE_ARGS)
            res, err = proc.communicate()
            complain("RETVAL:", proc.returncode, "STDOUT:", res, "STDERR:",
                     err)
            if proc.returncode == 0:
                # get the remote sha256 sum
                try:
                    remote_sha256 = self.remote_sha256(remote_url)
                    with open(riders.remote_sha256, 'w') as f:
                        f.write(remote_sha256)
                except GWRemoteSha256Exception as e:
                    with open(riders.error_msg, 'a') as f:
                        f.write(e.args[0])
                    raise e
                # get the local sha256 sum
                try:
                    local_sha256 = self.local_sha256(remote_url)
                    with open(riders.local_sha256, 'w') as f:
                        f.write(local_sha256)
                except GWLocalSha256Exception as e:
                    with open(riders.error_msg, 'a') as f:
                        f.write(e.args[0])
                    raise e
            else:
                errtime = datetime.utcnow().isoformat()
                errfmt = "DOWNLOAD ERROR at {}. STDERR: \n{}\n"
                errmsg = errfmt.format(errtime, err)
                with open(riders.error_msg, 'a') as f:
                    f.write(errmsg)
                raise GWDataDownloadException(errmsg)

    def __repr__(self):
        fmt="{}('{}', '{}', '{}', framelength='{}', server='{}', outdir='{}')"
        return fmt.format(
            type(self).__name__,
            self.detector,
            self.frametype,
            self.gpstime,
            self.framelength,
            self.server,
            self.outdir
        )

def get_times(start, deltat, frlength):
    """Get a list of start times for frame files based on in initial starting
    time, ``start``, and a specified length of time, ``deltat``. The initial
    starting time will be rounded down to the nearest multiple of
    ``frlength``, the default length of a frame file, since these are
    the customary starting times for LVC frame files. Similarly, the time
    window ``deltat`` will be rounded up to the nearest multiple of
    ``frlength``."""
    start  = (
        int(np.floor(start // frlength)) * frlength
    )
    deltat = (
        int(np.ceil(deltat // frlength)) * frlength
    )
    return range(
        start,
        start + deltat + frlength,
        frlength
    )

def get_queries(
        start,
        deltat,
        length=DEFAULT_FRAME_LENGTH,
        server=DEFAULT_SERVER,
        outdir=DEFAULT_OUTDIR,
        h_frametypes=DEFAULT_H_FRAMETYPES,
        l_frametypes=DEFAULT_L_FRAMETYPES,
        v_frametypes=DEFAULT_V_FRAMETYPES
):
    """Get all GWFrameQuery objects in the given time window for each
    combination of detector and frametype provided.

    Args:

        ``start``       The GPS time at which the time window starts.
        ``deltat``      The width of the time window in seconds.
        ``length``      The expected duration in seconds of each frame file.
        ``server``      The server on which to look for frame files.
        ``outdir``      The output directory where downloaded files should be
                        saved.

    Additionally, one can specify the frame file types to be downloaded for
    each detector:

        ``h_frametypes``    Frametypes for LIGO Hanford.
        ``l_frametypes``    Frametypes for LIGO Livingston.
        ``v_frametypes``    Frametypes for Virgo.
    """
    queries = list()
    # add queries for each detector/frametype combination
    detector_frametypes_dict = {
        "H": h_frametypes,
        "L": l_frametypes,
        "V": v_frametypes
    }
    for detector in detector_frametypes_dict.keys():
        for frametype in detector_frametypes_dict[detector]:
            queries += [GWFrameQuery(detector, frametype, t,
                                     framelength=length, server=server,
                                     outdir=outdir)
                        for t in get_times(start, deltat, length)]
    return queries

def check_progress(queries):
    """Return a dictionary of lists of queries, where the keys of the
    dictionary indicate the status of each query."""
    status = {
        'all_queries':     queries,
        'downloaded':      [],
        'maybe_corrupt':   [],
        'corrupted':       [],
        'remote_found':    [],
        'remote_hashed':   [],
        'name_guessed':    [],
        'no_remote_found': [],
        'not_started':     [],
        'error':           []
    }
    for query in queries:
        riders = query.estimated_rider_fullpaths()
        if os.path.isfile(riders.local_sha256):
            if os.path.isfile(riders.remote_sha256):
                if filecmp.cmp(riders.local_sha256, riders.remote_sha256):
                    status['downloaded'].append(query)
                else:
                    status['corrupted'].append(query)
            else:
                status['maybe_corrupt'].append(query)
        elif os.path.isfile(riders.remote_sha256):
            status['remote_hashed'].append(query)
        elif os.path.isfile(riders.remote_url):
            status['remote_found'].append(query)
        elif os.path.isfile(riders.query_repr):
            status['name_guessed'].append(query)
        else:
            if os.path.isfile(riders.error_msg):
                status['error'].append(query)
                with open(riders.error_msg) as f:
                    lasterrmsg = f.readlines()[-1]
                if lasterrmsg == 'Cannot get GPS start time from filename: \n':
                    status['no_remote_found'].append(query)
            else:
                status['not_started'].append(query)
    return status

def display_progress(status):
    """Print how much progress has been made so far."""
    total_q = len(status['all_queries'])
    fmt = '{0: <16} {1} {2: >4}/{3: <4} ({4:>7.3f}%)'
    for key in status:
        number_of_queries = len(status[key])
        percent = (100.0 * number_of_queries) / total_q
        barticks = int(percent / 100. * PROG_BAR_WIDTH)
        bar = "#"*barticks + "-"*(PROG_BAR_WIDTH-barticks)
        if number_of_queries == 0:
            bar = "-" + bar
        else:
            bar = "#" + bar
        print(fmt.format(key, bar, number_of_queries, total_q, percent))

def read_starts_and_deltats(infile):
    """Read ``start`` times and ``deltat`` values from an ``infile``, e.g.
    ``sys.stdin``.

    Returns
    -------

    ``times``   A list of ``(start, deltat)`` tuples, where ``start`` and
                ``deltat`` are times of the format expected by ``get_queries``.

    Arguments
    ---------

    ``infile``  A ``fileobj`` with one space-separated pair of (start, stop)
                times on each line (indicating the time intervals that should
                be downloaded).
    """
    start_stops = [l.split() for l in infile.readlines()]
    return [(int(start), int(stop)-int(start)) for start, stop in start_stops]

def main():
    complain("Arguments:", args)
    if args.times:
        times = read_starts_and_deltats(sys.stdin)
    elif args.start and args.deltat:
        times = [(args.start, args.deltat)]
    else:
        raise ValueError("Must provide either ``times`` or both of ``start`` "
                         "and ``deltat``.")
    queries = sum(
        [
            get_queries(
                start=start,
                deltat=deltat,
                length=args.length,
                server=args.server,
                outdir=args.outdir,
                h_frametypes=args.hanford_frametypes,
                l_frametypes=args.livingston_frametypes,
                v_frametypes=args.virgo_frametypes
            ) for start, deltat in times
        ],
        list()
    )
    complain("Queries:", *[format(q) for q in queries])
    complain("Total queries: ", len(queries))
    if args.progress:
        display_progress(check_progress(queries))
    else:
        if VERBOSE:
            complain("Checking progress before starting.")
            display_progress(check_progress(queries))
        tries_left = args.retries
        if tries_left >= 0:
            tries_left += 1
        while tries_left != 0:
            try:
                for query in queries:
                    if not query.estimated_fullpath_exists():
                        try:
                            query.download()
                        except FileNameParsingError:
                            complain('Filename parse error, skipping:', query)
            except Exception as err:
                complain("Exception caught:", err)
                if tries_left < 0:
                    complain("Retrying. Will keep trying till interrupted.")
                else:
                    tries_left -= 1
                    if tries_left != 0:
                        complain("Retrying. Tries left: {}".format(tries_left))
        if VERBOSE:
            complain("Done. Checking progress at end:")
            display_progress(check_progress(queries))

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
	OpenScrapers Module
"""

import os.path
import xbmc
import xbmcaddon
import xbmcgui

def get():
		addonInfo = xbmcaddon.Addon().getAddonInfo
		addonPath = xbmc.translatePath(addonInfo('path'))
		changelogfile = os.path.join(addonPath, 'changelog.txt')
		r = open(changelogfile)
		text = r.read()
		id = 10147
		xbmc.executebuiltin('ActivateWindow(%d)' % id)
		xbmc.sleep(500)
		win = xbmcgui.Window(id)
		retry = 50
		while (retry > 0):
			try:
				xbmc.sleep(10)
				retry -= 1
				win.getControl(1).setLabel('[COLOR red]OpenScrapers[/COLOR] -  v%s - ChangeLog' % (xbmcaddon.Addon().getAddonInfo('version')))
				win.getControl(5).setText(text)
				while xbmcgui.getCurrentWindowDialogId() == id:
					xbmc.sleep(100)
				return
			except:
				pass


#Coded by Sumanjay on 29th Feb 2020
from flask import Flask, request, jsonify
from inshorts import getNews
from flask_cors import CORS

app = Flask(__name__)
app.secret_key = "i_am_not_feeling_sleepy_so_i_am_coding_this"
CORS(app)

@app.route('/')
def home():
    return 'News API is UP!

A part of Sj Projects'

@app.route('/news')
def news():
    if request.method == 'GET':
        category = request.args.get("category")
        if not category:
            return jsonify({
                "error": "please add category in query params"
            }), 404
        return jsonify(getNews(category)), 200

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0',port=5000,use_reloader=True)


from setuptools import setup, find_packages
import os
# import pkg_resources
# installed_pkg = {pkg.key for pkg in pkg_resources.working_set}

# # Determine version on branch name
# active_branch = os.environ.get('CI_COMMIT_REF_NAME')
# if active_branch is None:
#     if 'git' in installed_pkg:
#         import git
#         local_repo = git.Repo('./')
#         active_branch = local_repo.active_branch.name

# if not(active_branch is None) and ('packaging' in installed_pkg):
#     import packaging.version  # noqa: 401

#     # CI case
#     try:
#         version = str(packaging.version.Version(active_branch))

#     except packaging.version.InvalidVersion:
#         if active_branch in ['devel', 'master']:
#             version = active_branch
#         else:
#             version = "unstable"
# else:
#     # No CI
#     version = None

version = "0.0.23"

setup(name='databayes',
      version=version,
      url='https://github.com/alphabayes/databayes',
      author='Roland Donat',
      author_email='roland.donat@gmail.com, roland.donat@edgemind.fr, roland.donat@alphabayes.fr',
      maintainer='Roland Donat',
      maintainer_email='roland.donat@gmail.com',
      keywords='pronostic datascience machine-learning ',
      classifiers=[
          'Development Status :: 3 - Alpha',
          'Intended Audience :: Science/Research',
          'License :: OSI Approved :: MIT License',
          'Operating System :: POSIX :: Linux',
          'Programming Language :: Python :: 3.7',
          'Topic :: Scientific/Engineering :: Artificial Intelligence'
      ],
      packages=find_packages(
          exclude=[
              "*.tests",
              "*.tests.*",
              "tests.*",
              "tests",
              "log",
              "log.*",
              "*.log",
              "*.log.*"
          ]
      ),
      description='Model as a service machine learning library',
      license='GPL V3',
      platforms='ALL',
      python_requires='>=3.6',
      install_requires=[
          "typing-extensions",
          "pandas",
          "PyYAML",
          "pydantic",
          "tqdm",
          "scikit-learn",
          "pyAgrum",
          "colored",
          "gspread_pandas",
          "openpyxl",
          "XlsxWriter",
          # "psycopg2", # not needed for now
          "intervals",  # Really needed ?
          "deepmerge",
      ],
      zip_safe=False,
      )


# 더하기와 나누기
# Gold 5, math, constructive

n = int(input())

if n == 2:
    print(2,4)
else:
    s = n*n-n
    for i in range(3,10**6,2):
        if s%i == 0:
            print(i,*list(range(2,n*2,2)))
            break


"""added decision_by_id fields to works

Revision ID: 622cb040a90e
Revises: 2b164207f899
Create Date: 2023-02-02 11:38:06.597732

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '622cb040a90e'
down_revision = '2b164207f899'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('TRUNCATE works RESTART IDENTITY CASCADE')
    op.add_column('work_statuses', sa.Column('work_status_notes', sa.Text(), nullable=True))
    op.add_column('works', sa.Column('eac_decision_by_id', sa.Integer(), nullable=False))
    op.add_column('works', sa.Column('decision_by_id', sa.Integer(), nullable=False))
    op.create_foreign_key(None, 'works', 'staffs', ['decision_by_id'], ['id'])
    op.create_foreign_key(None, 'works', 'staffs', ['eac_decision_by_id'], ['id'])
    op.drop_column('works', 'work_status_notes')
    op.drop_column('works', 'work_short_status')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('works', sa.Column('work_short_status', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('works', sa.Column('work_status_notes', sa.TEXT(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'works', type_='foreignkey')
    op.drop_constraint(None, 'works', type_='foreignkey')
    op.drop_column('works', 'decision_by_id')
    op.drop_column('works', 'eac_decision_by_id')
    op.drop_column('work_statuses', 'work_status_notes')
    # ### end Alembic commands ###


# Author: Steven C. Dang

# Main script for importing provided D3M dataset schemas for operation on datasets

import logging
import os.path as path
import os
import csv

# Workflow component specific imports
from ls_utilities.ls_logging import setup_logging
from ls_utilities.cmd_parser import get_default_arg_parser
from ls_utilities.ls_wf_settings import *
from ls_dataset.d3m_dataset import D3MDataset

__version__ = '0.1'

if __name__ == '__main__':

    # Parse argumennts
    parser = get_default_arg_parser("Import List of available D3M Datasets")
    args = parser.parse_args()

    if args.is_test is not None:
        is_test = args.is_test == 1
    else:
        is_test = False

    # Get config file
    config = SettingsFactory.get_settings(path.join(args.programDir, 'program', 'settings.cfg'),
                                          program_dir=args.programDir,
                                          working_dir=args.workingDir,
                                          is_test=is_test
                                          )

    # Setup Logging
    setup_logging(config)
    logger = logging.getLogger('datasets_importer')

    ### Begin Script ###
    logger.info("Importing List of available datasets")
    logger.debug("Running Dataset Importer with arguments: %s" % str(args))

    # Read in the dataset json
    ds_root = config.get_dataset_path()
    datasets = set()
    for root, dirs, files in os.walk(ds_root):
        for f in files:
            if f == 'datasetDoc.json':
                logger.debug("Found dataset in directory: %s" % root)
                try:
                    ds = D3MDataset.from_dataset_json(path.join(root, f))
                    if ds.name not in datasets:
                        logger.info("Found dataset name: %s\nAt path: %s" % (ds.name,  ds.dpath))
                        datasets.add(ds.name)
                except:
                    # Don't choke on unsupported dataset jsons
                    logger.warning("Encountered unsupported dataset: %s" % str(path.join(root, f)))

    logger.debug("Found datasets: %s" % str(datasets))

    # # Write dataset info to output file
    out_file_path = path.join(args.workingDir, config.get('Dataset', 'out_file'))
    logger.info("Writing dataset list of %i datasets to file: %s" % (len(datasets), out_file_path))
    with open(out_file_path, 'w') as out_file:
        out_csv = csv.writer(out_file, delimiter='\t')
        out_csv.writerow(datasets)


# exercise 1.1
# use function 'bisect' to realize
# looking for the root of equation by bisect

from math import log2, log, e
from math import ceil, floor
from math import cos, sin
import matplotlib.pyplot as plt
import numpy as np

def bisect(f, a, b, tol):
    '''
    calculate the approximate solution of f(x)=0

    Input:
    - f : quation f
    - a : interval lower bound
    - b : interval upper bound  f(a)*f(b)<0
    - tol : tolerance

    Output:
    - xc : approximate solution of f
    '''
    if (f(a) < 0) == (f(b) < 0):
        raise Exception('Incorrect interval!')

    fa, fb = f(a), f(b)

    while (b-a)/2 > tol:
        c = (a+b)/2
        fc = f(c)

        if fc == 0:
            return c

        if (fa < 0) == (fc < 0):
            a, fa = c, fc

        else:
            b, fb = c, fc

    return (a+b) / 2

def bisect_digits(f, a, b, p):
    '''
    calculate the approximate solution of f(x) to
    the nearest p decimal places

    Input:
    - f : quation f
    - a : interval lower bound
    - b : interval upper bound  f(a)*f(b)<0
    - p : solution to the nearest p decimal places

    Output:
    - xc : approximate solution
    '''
    if (f(a) < 0) == (f(b) < 0):
        raise Exception('Incorrect interval!')

    n = ceil(log2((b-a) / (0.5 * 10**(-p))) - 1)
    print('Original Interval is [{},{}]'.format(a, b))
    print('To get {} decimal, we need {} steps.'.format(p, n))

    fa, fb = f(a), f(b)

    for i in range(n):
        c = (a+b) / 2
        fc = f(c)

        #print('Before {} loop, a={}, b={}, c={}'.format(i, a, b, c))

        if (fa < 0) == (fc < 0):
            a, fa = c, fc

        else:
            b, fb = c, fc

        #print('After this loop, a={}, b={}, c={}'.format(a, b, c))

    return round((a+b) / 2, p)

def plot_functions(f1, f2, f3):
    x = np.linspace(0,1,1000)
    plt.subplot(311)
    plt.plot(x, f1(x))
    plt.xlim(-2,2)
    plt.ylim(-1, 1)
    plt.subplot(312)
    plt.plot(x, f2(x))
    plt.xlim(-2,2)
    plt.ylim(-1, 1)
    plt.subplot(313)
    plt.plot(x, f3(x))
    plt.xlim(-2,2)
    plt.ylim(-1, 1)

    plt.show()

    return plt

if __name__ == '__main__':
    print('Test result:')
    f1 = lambda x: x**3 + x - 1
    print(bisect(f1, 0, 1, tol=0.00005))
    print('='*30)

    print('Problem 1.1-1')
    f1a = lambda x: x**3 - 9
    print('(a):', bisect_digits(f1a, 2, 3, 6))
    f1b = lambda x: 3*x**3 + x**2 - x - 5
    print('(b):', bisect_digits(f1b, 1, 2, 6))
    f1c = lambda x: cos(x)**2 -x + 6
    print('(c):', bisect_digits(f1c, 6, 7, 6))
    print('='*30)

    print('Problem 1.1-2')
    f2a = lambda x: x**5 + x - 1
    print('(a):', bisect_digits(f2a, 0, 1, 8))
    f2b = lambda x:sin(x) -6*x -5
    print('(b):', bisect_digits(f2b, -2, 0, 8))
    f2c = lambda x: log(x) + x**2 - 3
    print('(c):', bisect_digits(f2c, 1, 10, 8))
    print('='*30)

    print('Problem 1.1-3')
    f3a = lambda x: 2*x**3 - 6*x - 1
    f3b = lambda x: e**(x-2) + x**3 - x
    f3c = lambda x: 1 + 5*x - 6*x**3 - e*(2*x)
    #plot_functions(f3a, f3b, f3c)
    print('='*30)

    ziplist = [(2, 'a'), (3, 'b'), (5, 'c')]
    print('Problem 1.1-4')
    for i, j in ziplist:
        f4 = lambda x: x**2 - i
        print(j, bisect_digits(f4, 1, 3, 8))
    print('='*30)

    print('Problem 1.1-5')
    for i, j in ziplist:
        f5 = lambda x: x**3 - i
        print(j, bisect_digits(f5, 1, 3, 8))
    print('='*30)

    print('Problem 1.1-6')
    f6 = lambda x: cos(x)-sin(x)
    print('Approximate solution is:', bisect_digits(f6, 0, 1, 6))
    print('='*30)

    print('Problem 1.1-7')
    f7 = lambda x: np.linalg.det([[1,2,3,x],[4,5,x,6],[7,x,8,9],[x,10,11,12]]) - 1000
    x1 = bisect_digits(f7, -18, -17, 6)
    det1 = f7(x1)
    acc1 = ceil(log(abs(det1*2), 10))
    print('First root is:', x1)
    print('The det is: {} with {} decimal acc.'.format(det1 + 1000, -acc1))
    x2 = bisect_digits(f7, 9, 10, 6)
    det2 = f7(x2)
    acc2 = ceil(log(abs(det2*2), 10))
    print('Second root is:', x2)
    print('The det is:', det2)
    print('The det is: {} with {} decimal acc.'.format(det2 + 1000, -acc2))
    print('='*30)

    print('Problem 1.1-8')
    hibm = 1 / (np.arange(0, 5) + np.arange(1, 6)[:,np.newaxis])
    def f8(x):
        hibm[0][0] = x
        return np.linalg.eig(hibm)[0].max() - np.pi
    print('A_11 is:', bisect_digits(f8, 2, 3, 6))
    print('='*30)

    print('Problem 1.1-9')
    # v=4/3*pi*r^3 > 1*2
    f9 = lambda x: np.pi * x**2 * (1 - 1/3 * x) - 1
    print('The height is:', bisect(f9, 0, 1, 1e-3))
    print('='*30)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGraphicsDropShadowEffect, QWidget

class EffectTool:
    @staticmethod
    def setBlur(widget: QWidget, shadowRadius: int = 12, shadowColor: int = Qt.gray):
        # 去除默认边框
        widget.setWindowFlags(Qt.FramelessWindowHint)
        # 背景透明（就是ui中黑色背景的那个控件）
        widget.setAttribute(Qt.WA_TranslucentBackground, True)

        # 添加阴影
        effect = QGraphicsDropShadowEffect(widget)
        effect.setBlurRadius(shadowRadius)
        effect.setOffset(0, 0)
        effect.setColor(shadowColor)
        widget.setGraphicsEffect(effect)

    @staticmethod
    def setBlurs(widgets: [QWidget], blurRadius: int = 12, color: int = Qt.gray):
        for widget in widgets:
            EffectTool.setBlur(widget, blurRadius, color)


from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic import ListView, DetailView
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
# Import the mixin for class-based views
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Dove, Toy
from .forms import FeedingForm
# Create your views here.
# Define the home view
def signup(request):
  error_message = ''
  if request.method == 'POST':
    # This is how to create a 'user' form object
    # that includes the data from the browser
    form = UserCreationForm(request.POST)
    if form.is_valid():
      # This will add the user to the database
      user = form.save()
      # This is how we log a user in via code
      login(request, user)
      return redirect('index')
    else:
      error_message = 'Invalid credentials - try again'
  # A bad POST or a GET request, so render signup.html with an empty form
  form = UserCreationForm()
  context = {'form': form, 'error_message': error_message}
  return render(request, 'registration/signup.html', context)

class DoveCreate(LoginRequiredMixin, CreateView):
  model = Dove
  fields = '__all__'

  def form_valid(self, form):
    # Assign the logged in user
    form.instance.user = self.request.user
    # Let the CreateView do its job as usual
    return super().form_valid(form)

class DoveUpdate(UpdateView):
  model = Dove
  # Let's disallow the renaming of a dove by excluding the name field!
  fields = ['breed', 'description', 'age']

class DoveDelete(DeleteView):
  model = Dove
  success_url = '/doves/'

def home(request):
  return render(request, 'home.html')

def about(request):
  return render(request, 'about.html')
#   return HttpResponse('<h1>About Dove Collector<h1>')

@login_required
def doves_index(request):
  doves = Dove.objects.filter(user=request.user)
  # You could also retrieve the logged in user's doves like this
  # doves = request.user.dove_set.all()
  return render(request, 'doves/index.html', { 'doves': doves })
  # doves = Dove.objects.all()
  # return render(request, 'doves/index.html', { 'doves': doves})

@login_required
def doves_detail(request, dove_id):
  dove = Dove.objects.get(id=dove_id)
  # Get the toys the cat doesn't have
  toys_dove_doesnt_have = Toy.objects.exclude(id__in = dove.toys.all().values_list('id'))
  # Instantiate FeedingForm to be rendered in the template
  feeding_form = FeedingForm()
  return render(request, 'doves/detail.html', {
    # Pass the cat and feeding_form as context
    'dove': dove, 'feeding_form': feeding_form,
    # Add the toys to be displayed
    'toys': toys_dove_doesnt_have
    })

@login_required
def add_feeding(request, dove_id):
  # create a ModelForm instance using the data in request.POST
  form = FeedingForm(request.POST)
  # validate the form
  if form.is_valid():
    # don't save the form to the db until it
    # has the cat_id assigned
    new_feeding = form.save(commit=False)
    new_feeding.dove_id = dove_id
    new_feeding.save()
  return redirect('detail', dove_id=dove_id)

@login_required
def assoc_toy(request, dove_id, toy_id):
  Dove.objects.get(id=dove_id).toys.add(toy_id)
  return redirect('detail', dove_id=dove_id)

@login_required
def unassoc_toy(request, dove_id, toy_id):
  Dove.objects.get(id=dove_id).toys.remove(toy_id)
  return redirect('detail', dove_id=dove_id)

class ToyList(ListView):
  model = Toy

class ToyDetail(DetailView):
  model = Toy

class ToyCreate(CreateView):
  model = Toy
  fields = '__all__'

class ToyUpdate(UpdateView):
  model = Toy
  fields = ['name', 'color']

class ToyDelete(DeleteView):
  model = Toy
  success_url = '/toys/'


import re
import json
import string
from bs4 import BeautifulSoup
from selenium import webdriver
import csv

def get_soup(url):
    driver = webdriver.Chrome(executable_path=r'C:\Users\Ashish Mishra\Desktop\chromedriver.exe')
    driver.get(url)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')

    driver.close()
    return soup

def get_job_links(soup):
    urls = []
    # Loop thru all the posting links

    for link in soup.find_all('h2', {'class': 'jobtitle'}):
        # Since sponsored job postings are represented by "a target" instead of "a href", no need to worry here
        partial_url = link.a.get('href')
        # This is a partial url, we need to attach the prefix
        url = 'https://www.indeed.co.in' + partial_url
        # Make sure this is not a sponsored posting
        urls.append(url)
    return urls

def get_urls(query, num_pages, location, state):
    # # h  https: // www.indeed.co. in / jobs?q ={}& l ={} + % 2C + Karnataka & start = 10
    #
    hey_url = 'https: // www.indeed.co.in / jobs?q = {}  & l = {}+,+{}'.format(query, location, state)

    base_url = hey_url.translate(({ord(c): None for c in string.whitespace}))
    print(base_url)
    soup = get_soup(base_url)
    urls = get_job_links(soup)
    # https: // www.indeed.co.in / jobs?q = [ % 27 % 22data + scientist % 22 % 27] & l = Bengaluru, +Karnataka
    # Get the total number of postings found
    # # https: // www.indeed.co. in / jobs?q = % 22data + engineer % 22 & l = Bengaluru % 2C + Karnataka
    posting_count_string = soup.find(name='div', attrs={'id': "searchCount"}).get_text()
    posting_count_string = posting_count_string[posting_count_string.find('of') + 2:].strip()
    try:
        posting_count = int(posting_count_string)
    except ValueError:  # deal with special case when parsed string is "360 jobs"
        posting_count = int(re.search('\d+', posting_count_string).group(0))
    finally:
        posting_count = 330  # setting to 330 when unable to get the total
        pass
    max_pages = round(posting_count / 10) - 3
    if num_pages > max_pages:
        print('returning max_pages!!')
        return max_pages
    if num_pages >= 2:
        for i in range(2, num_pages + 1):
            num = (i - 1) * 10
            # https: // www.indeed.co. in / jobs?q = % 22
            # etl + developer % 22 & l = Bengaluru, +Karnataka & start = 0
            # https: // www.indeed.co. in / jobs?q = % 22data + engineer % 22 & l = Bengaluru, +Karnataka & start = 0
            hey_url = 'https: // www.indeed.co.in / jobs?q = {}  & l = {}+,+{}&start={}'.format(query, location, state, num)

            base_url = hey_url.translate(({ord(c): None for c in string.whitespace}))
            try:
                soup = get_soup(base_url)
                urls += get_job_links(soup)
            except:
                continue

    return urls

def get_posting(url):
    soup = get_soup(url)
#h3 tag
    title = soup.find(name='h3').getText().lower()
    posting = soup.find(name='div', attrs={'class': "jobsearch-JobComponent"}).get_text()

    return title, posting.lower()

def get_data(query, num_pages, location):
    postings_dict = {}
    urls = get_urls(query, num_pages, location, state)

    if isinstance(urls, list):
        num_urls = len(urls)
        for i, url in enumerate(urls):
            try:
                title, posting = get_posting(url)
                postings_dict[i] = {}
                postings_dict[i]['title'], postings_dict[i]['posting'], postings_dict[i]['url'] = \
                    title, posting, url
            except:
                continue

        # with open('file_name'.csv, 'w') as file_name:
        #   csv.writer(file_name, postings_dict)

        file_name = query.replace('"', "") + '.json'
        with open(file_name, 'w+') as f:
            print('hey')
            print((file_name))
            json.dump(postings_dict, f)

        print('{} JobPostings have been scraped : '.format(num_urls))

if __name__ == "__main__":

    fs = open('watching_list2.csv', 'r+')
    lines = fs.readlines()

    i = 0

    while i < len(lines):
        lines[i] = lines[i].lower().rstrip()
        queries = lines
        i = i + 1
        d = queries[0]
    query = '"'+d+'"'
    print(query)
    num_pages = 1
    location = 'Bengaluru'
    state = 'Karnataka'

    get_data(query, num_pages, location)


"""
jpg66
"""
from __future__ import division
from __future__ import print_function

from builtins import input
from builtins import str
from past.utils import old_div
from nplab.instrument.spectrometer.Triax import Triax
import numpy as np
from nplab.instrument.camera.Andor import Andor, AndorUI
import types
import future

Calibration_Arrays=[]

Calibration_Arrays.append([])
Calibration_Arrays.append([])
Calibration_Arrays.append([])

#Calibration_Arrays[0].append([546.,614.,633.,708.19,785.,880.])
#Calibration_Arrays[0].append([-1.54766997e-05, 3.12560316e-06, 7.48822074e-06, 1.26048704e-05 , 4.21666043e-06, 2.43945244e-05])
#Calibration_Arrays[0].append([-1.63503325e+00, -1.94141892e+00, -2.02516092e+00, -2.17431238e+00, -1.87265593e+00, -2.60909451e+00])
#Calibration_Arrays[0].append([1.44780946e+04, 1.75785703e+04, 1.85922568e+04, 2.17816013e+04, 1.10551705e+04, 3.03183653e+04])

Calibration_Arrays[1].append([546.,614.,633.,708.19,785.,880.])
Calibration_Arrays[1].append([-5.65248674e-06, -3.01056645e-06, -1.99047684e-06, -2.24562594e-05, 2.02336235e-06,  1.15803962e-05])
Calibration_Arrays[1].append([-1.75115459, -1.79100089, -1.79852965, -1.59951646, -1.85506506,-1.98678881])
Calibration_Arrays[1].append([7601.12273137,  8563.71071556,  8849.60197977,  9361.94861663,11023.31118393, 12621.76193336])

Calibration_Arrays[2].append([546.,614.,633.,708.19,785.,880.])
Calibration_Arrays[2].append([-3.93887893e-05,-6.91910954e-06,-3.18089126e-06,-6.25746333e-06, -1.60444375e-06, -1.57248823e-05])
Calibration_Arrays[2].append([-1.67741919,-1.73940226,-1.74976480,-1.74174632, -1.75474092, -1.71174797])
Calibration_Arrays[2].append([2.53324771e+03,2.76268959e+03,2.83115874e+03,3.06497573e+03, 3.30744597e+03, 3.56084766e+03])

Calibration_Arrays=np.array(Calibration_Arrays)

CCD_Size=1600 #Size of ccd in pixels

#Make a deepcopy of the andor capture function, to add a white light shutter close command to if required later
# Andor_Capture_Function=types.FunctionType(Andor.capture.__code__, Andor.capture.__globals__, 'Unimportant_Name',Andor.capture.__defaults__, Andor.capture.__closure__)

class Trandor(Andor):#Andor
    ''' Wrapper class for the Triax and the andor
    '''
    def __init__(self, white_shutter=None, triax_address = 'GPIB0::1::INSTR', use_shifts = False, laser = '_633'):
        print ('Triax Information:')
        super(Trandor,self).__init__()
        self.triax = Triax(triax_address, Calibration_Arrays,CCD_Size) #Initialise triax
        self.white_shutter = white_shutter
        self.triax.ccd_size = CCD_Size
        self.use_shifts = use_shifts
        self.laser = laser

        print ('Current Grating:'+str(self.triax.Grating()))
        print ('Current Slit Width:'+str(self.triax.Slit())+'um')
        self.metadata_property_names += ('slit_width', 'wavelengths')

    def Grating(self, Set_To=None):
        return self.triax.Grating(Set_To)

    def Generate_Wavelength_Axis(self, use_shifts=None):

        if use_shifts is None:
            use_shifts = self.use_shifts
        if use_shifts:
            if self.laser == '_633': centre_wl = 632.8
            elif self.laser == '_785': centre_wl = 784.81
            wavelengths = np.array(self.triax.Get_Wavelength_Array()[::-1])
            return ( 1./(centre_wl*1e-9)- 1./(wavelengths*1e-9))/100
        else:
            return self.triax.Get_Wavelength_Array()[::-1]
    x_axis = property(Generate_Wavelength_Axis)

    @property
    def wavelengths(self):
        return self.Generate_Wavelength_Axis(use_shifts=False)
    @property
    def slit_width(self):
        return self.triax.Slit()

    def Test_Notch_Alignment(self):
        	Accepted=False
        	while Accepted is False:
        		Input=input('WARNING! A slight misalignment of the narrow band notch filters could be catastrophic! Has the laser thoughput been tested? [Yes/No]')
        		if Input.upper() in ['Y','N','YES','NO']:
        			Accepted=True
        			if len(Input)>1:
        				Input=Input.upper()[0]
        	if Input.upper()=='Y':
        		print ('You are now free to capture spectra')
        		self.Notch_Filters_Tested=True
        	else:
        		print ('The next spectrum capture will be allowed for you to test this. Please LOWER the laser power and REDUCE the integration time.')
        		self.Notch_Filters_Tested=None
    def Set_Center_Wavelength(self, wavelength):
        ''' backwards compatability with lab codes that use trandor.Set_Center_Wavelength'''
        self.triax.Set_Center_Wavelength(wavelength)

def Capture(_AndorUI):
    if _AndorUI.Andor.white_shutter is not None:
        isopen = _AndorUI.Andor.white_shutter.is_open()
        if isopen:
            _AndorUI.Andor.white_shutter.close_shutter()
        _AndorUI.Andor.raw_image(update_latest_frame = True)
        if isopen:
            _AndorUI.Andor.white_shutter.open_shutter()
    else:
        _AndorUI.Andor.raw_image(update_latest_frame = True)
setattr(AndorUI, 'Capture', Capture)