import os
import sublime
import sublime_plugin
from .ccl import _ccl_core_, CCL_Variables

CUR_DIR = os.path.dirname(os.path.realpath(__file__))
data_map_folders = os.path.join(CUR_DIR, "data")
CCL_Core = None

class ViewCernerDescriptionCommand(sublime_plugin.TextCommand):

    PRETTY = False

    @property
    def default_path(self):
        _path = CCL_Core.get_setting("data_model_location", None)
        if _path is not None:
            self._default_path = os.path.join(_path, "html")
            return self._default_path
        else:
            return None

    @property
    def pretty_path(self):
        self._pretty_path = os.path.join(data_map_folders, "html")
        return self._pretty_path

    @property
    def model_path(self):
        self._model_path = None
        if os.path.isdir(self.pretty_path):
            self._model_path = self.pretty_path
            self.PRETTY = True
        elif self.default_path is not None:
            if os.path.isdir(self.default_path):
                self._model_path = self.default_path
                self.PRETTY = False
        return self._model_path

    def run(self, *args, **kwargs):
        if self.model_path is not None:
            print(self.get_selection())
            self.open_in_browser(self.get_selection())
        else:
            sublime.message_dialog(
                "CCL Tools:\nYou need to set the data map location.")

    def get_selection(self):

        for region in self.view.sel():
            if not region.empty():
                variables = CCL_Variables(self.view)
                selected = self.view.substr(region).upper()
                if "." in selected:
                    selected = selected.split(".")[0]
                alias = variables.reverse_lookup(selected)
                # print(selected)
                # octx.priority
                # print(alias)
                if self.PRETTY:
                    if selected in CCL_Core.TABLES:
                        # print("found")
                        return selected
                    if alias:
                        # print("found")
                        return alias

                else:
                    if selected in CCL_Core.TABLES:
                        # print("found")
                        return CCL_Core.TABLE_DOCS[selected]
                    if alias:
                        # print("found")
                        return CCL_Core.TABLE_DOCS[alias]

    def open_in_browser(self, table, browser='default'):
        print("running")
        if self.PRETTY:
            print("pretty")

            filename = "{}.html".format(table)
            path = os.path.join(self.model_path, filename)
        elif self.model_path is not None:
            path = os.path.join(self.model_path, table)
            print(path)

        print(path)
        url_shortcut = "[InternetShortcut]\nURL={}".format(path)
        print(url_shortcut)
        with open('help_file.URL', 'w') as file:
            file.write(url_shortcut)

        if self.PRETTY:
            self.view.window().run_command("open_url", {"url": path})
        else:
            self.view.window().run_command("open_url", {"url": 'help_file.URL'})

def plugin_loaded():
    global CCL_Core
    global data_map_folders
    data_map_folders = os.path.join(CUR_DIR, "data")

    CCL_Core = _ccl_core_()


from locust import TaskSet, HttpLocust, run_locust, parse_options
from .helpers import retry_valid_input
from .configure import make_config, save_config
from .nest import collect_tasksets, collect_locusts
from .version import __version__ as version
from .install import install
import logging
import sys
import os

import argparse
sys.path.insert(0, os.getcwd())

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)

def create_parser():
    """Create parser object used for defining all options for locust-nest.

    Returns:
        ArgumentParser: ArgumentParser object used in *parse_options*.
    """

    # Initialize
    parser = argparse.ArgumentParser(usage="locust-nest [options] Locust options")

    parser.add_argument(
        '--configure',
        action='store_true',
        dest='configure',
        default=False,
        help="Generate config file using helper."
    )

    parser.add_argument(
        '--config-file',
        action='store',
        dest='config_file',
        default='config.json',
        help="Specify config file location."
    )

    parser.add_argument(
        '-T', '--include-tasksets',
        action='store_true',
        dest='include_tasksets',
        default=False,
        help="Whether or not to include TaskSets."
    )

    parser.add_argument(
        '-d', '--model-dir',
        action='store',
        dest='model_dir',
        default=None,
        help="Specify directory containing TaskSets or Locusts."
    )

    parser.add_argument(
        '-V', '--version',
        action='store_true',
        dest='show_version',
        default=False,
        help="show program's version number and exit"
    )
    return parser

def main(args=None):
    """Load all TaskSets and Locusts from --model-dir according to --config-file.
    --configure option will guide generation of config file before launching.
    Will pass on all standard Locust arguments to run_locust.

    Keyword Arguments:
        args {list} -- Command line arguments (default: {None})
    """

    parser = create_parser()
    nest_opts, nest_args = parser.parse_known_args()
    if nest_args:
        if nest_args[0] == 'install':
            install(nest_args[1:] if len(nest_args) > 1 else [])
            sys.exit(0)
    include_tasksets = nest_opts.include_tasksets

    if nest_opts.show_version:
        print("locust-nest version {}".format(version))
        sys.exit(0)

    model_dir = nest_opts.model_dir
    if model_dir is None:
        model_dir = retry_valid_input(
                prompt='Enter the path of your model directory:',
                title='directory',
                default='models/',
                condition=os.path.exists)

    if nest_opts.configure:
        save_config(make_config(model_dir, include_tasksets), nest_opts.config_file)
        yeses = ['y', 'Y']
        nos = ['n', 'N']
        run = retry_valid_input(
            prompt="Would you like to run locust-nest now?",
            title='run',
            default='n',
            condition=lambda x: x in (yeses+nos)
        )
        if run not in yeses:
            sys.exit(0)

    # Locust classes model_dir = nest_opts.model_dir
    print("Collecting Locust classes")
    locusts = collect_locusts(model_dir)
    locust_classes = [x[0] for x in locusts]

    # Tasksets
    if include_tasksets:
        nest_tasks = collect_tasksets(dir_path=model_dir)
        if not nest_tasks:
            logger.warning('No TaskSets found in {}'.format(model_dir))

        if nest_tasks:
            class NestTaskSet(TaskSet):
                """TaskSet containing all the sub-tasksets contained
                in the specified directory.

                Arguments:
                    TaskSet {class} -- TaskSet class from Locust.

                """
                tasks = nest_tasks

            class NestLocust(HttpLocust):
                """HttpLocust using the NestTaskSet.

                Arguments:
                    HttpLocust {class} -- HttpLocust from Locust.

                """
                task_set = NestTaskSet
                weight = 1
            locust_classes.append(NestLocust)
            locusts.append([NestLocust, NestLocust.weight])

    _, locust_opts, locust_args = parse_options(nest_args)
    locust_opts.locust_classes = locust_classes
    if not locust_classes:
        print("No classes found for simulation, exiting.")
        sys.exit(1)
    print("Running Locust with locust_classes: {}".format(locusts))
    run_locust(locust_opts, locust_args)

if __name__ == "__main__":
    main()


def nested_dict_search(dictionary, key):
    """ return: first value that matches key """
    if key in dictionary:
        return dictionary[key]
    for k, v in dictionary.items():
        if isinstance(v, dict):
            item = nested_dict_search(v, key)
            if item is not None:
                return item

def nested_dict_search_list(dictionary, key):
    """ return: list of values that matches key """
    result = []
    if key in dictionary:
        result.append(dictionary[key])
    for k, v in dictionary.items():
        if isinstance(v, dict):
            item = nested_dict_search_list(v, key)
            if item is not None:
                result += item
    return result

def parse_name(data):
    return nested_dict_search(data, 'title')

def parse_description(data):
    return nested_dict_search(data, 'description')

def parse_url(servers):
    urls = []
    for server in servers:
        # Removes virtual server of swagger-hub from urls
        if 'virtserver' not in server['url']:
            urls.append(server['url'])
    return urls


# -*- coding: utf-8 -*-
import scrapy

class RrcookiespiderSpider(scrapy.Spider):
    name = 'rrcookiespider'
    allowed_domains = ['renren.com']
    start_urls = ['http://www.renren.com/242245656/profile']  # 登录的url

    """
    人人网与登录有关的一个cookie是：
    "t":"a78bef053dd62a7124a5e71fbcfbc96a6",
    """
    # 注意cookie的时效性！！！cookie是会过期的！
    cookies = {
        "t":"a78bef053dd62a7124a5e71fbcfbc96a6",
    }

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.FormRequest(url,cookies=self.cookies,callback=self.parse_new_page)

    def parse_new_page(self,response):# 写入文件
        with open("yinicheng.html","wb") as f:
            f.write(response.body)


import webapp2
import logging
import os

class Hello(webapp2.RequestHandler):
    def get(self):

        if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
            ENV="PROD"
        else:
            ENV="DEV_APPSERVER"
        self.response.write('Hello, world from Python MAIN:'+ENV+'\n')

application = webapp2.WSGIApplication([
    # In order to make dispatch.yaml work, we handle /.*, so /py/*
    # is served by Hello handler.
    ('/.*', Hello)
], debug=True)


"""this code increases the resolution of river network and huc boundary in a controlled way using original river network and huc"""
import logging
import numpy as np
import math
from scipy import interpolate
import shapely

import watershed_workflow.utils

def densify_rivers(rivers,
                   rivers_raw=None,
                   use_original=False,
                   limit=100,
                   treat_collinearity=False):
    """Returns a list for densified rivers"""
    rivers = [
        densify_river(river,
                      river_raw,
                      use_original=use_original,
                      limit=limit,
                      treat_collinearity=treat_collinearity)
        for river, river_raw in zip(rivers, rivers_raw)
    ]
    return rivers

def densify_river(river, river_raw=None, use_original=False, limit=100, treat_collinearity=False):
    """This function traverse in the river tree and densify node.segments

    Parameters:
    -----------
    river: watershed_workflow.river_tree.RiverTree object
        river tree after simplifed (sparse points) that is to be densified
    river_raw: watershed_workflow.river_tree.RiverTree object, optional
        original tree containing all the known points from NHDPlus
    limit : int
        limit on the section length above which more points are added
    use_original: boolean
        flag for whether to resample from original river tree or just do simple interpolation
    treat_collinearity: boolean
        flag for whether to enforce non-colinearity. Collinear points in the segment create problem when
        river corridor polynomial is created

    Returns
    -------
    river_densified: watershed_workflow.river_tree.RiverTree object
        a densified river tree

    """

    assert (len(river) == len(river_raw))

    river_densified = river.deep_copy()
    for node, node_ in zip(river_densified.preOrder(), river_raw.preOrder()):
        node.segment = densify_node_segments(node,
                                             node_,
                                             limit=limit,
                                             use_original=use_original,
                                             treat_collinearity=treat_collinearity)
    return river_densified

def densify_node_segments(node, node_raw, use_original=False, limit=100, treat_collinearity=False):
    """This function adds equally-spaced points in the reach-sections longer than the limit at a desired resolution
        potentially using original river tree

    Parameters:
     -----------
    node: node of a watershed_workflow.river_tree.RiverTree object
        node of a simplifed tree (sparse points) that is to be densified
    node_raw: nodeof a watershed_workflow.river_tree.RiverTree object, optional
        node from the original tree containing all the known points from NHDPlus
    limit : int
        limit on the section length above which more points are added
    use_original: boolean
        flag for whether to resample from original river tree or just do simple interpolation
    treat_collinearity: boolean
        flag for whether to enforce non-colinearity. Collinear points in the segment create problem when
        river corridor polynomial is created

    Returns
    -------
    node.segment: node.segment of a watershed_workflow.river_tree.RiverTree object
        a densified (inplace) node.segment
    """

    seg_coords = list(node.segment.coords)  # coordinates of node.segment to be densified
    seg_coords_ = list(
        node_raw.segment.coords)  # coordinates of node.segment from original river network
    seg_coords_densified = seg_coords.copy()  # segment coordinates densified
    j = 0
    for i in range(len(seg_coords) - 1):
        section_length = watershed_workflow.utils.distance(seg_coords[i], seg_coords[i + 1])
        if section_length > limit:
            number_new_points = int(section_length // limit)
            end_points = [seg_coords[i],
                          seg_coords[i + 1]]  # points betwen which more points will be added
            if use_original:
                new_points = interpolate_with_orig(end_points, seg_coords_, number_new_points)
            else:
                new_points = interpolate_simple(end_points, number_new_points)
            seg_coords_densified[j + 1:j + 1] = new_points
            j += number_new_points
        j += 1
    if treat_collinearity:
        seg_coords_densified = treat_segment_collinearity(seg_coords_densified)
    node.segment.coords = seg_coords_densified
    return node.segment

def densify_hucs(huc, huc_raw, rivers, use_original=False, limit_scales=None):
    """This function densify huc boundaries. The densification length scale either can be a constant value or a refinement
    function where huc segment refinedment is greater for huc segments closer to the river tree

    Parameters:
     -----------
      hucs: watershed_workflow.split_hucs.SplitHUCs object
        huc to be densified
      huc_raw: watershed_workflow.split_hucs.SplitHUCs object
        original huc with all the known points from NHDPlus
      river: watershed_workflow.river_tree.RiverTree object
        to check the proximity of huc and river for refinement
      use_original: boolean
        flag for whether to resample from original river tree or just do simple interpolation
      limit_scales: int or List
        limit of section length above which more points are added, either a constant value or a list for step refinement
        [near_distance, near_length_scale, far_distance, far_length_scale]

     Returns
    -------
    watershed_densified: watershed_workflow.split_hucs.SplitHUCs object
        a densified huc
    """

    # first if there are multiple segments, we define outer-ring and remove close points
    #huc_ring=huc.exterior().exterior.simplify(tolerance=1)
    coords = list(huc.exterior().exterior.coords)
    coords_raw = list(huc_raw.exterior().exterior.coords)

    if use_original:
        if type(limit_scales) is list:
            # basic refine
            coords_densified_basic = densify_hucs_(coords,
                                                   coords_raw,
                                                   rivers,
                                                   limit_scales=limit_scales[-1])
            # adaptive refine
            coords_densified = densify_hucs_(coords_densified_basic,
                                             coords_raw,
                                             rivers,
                                             limit_scales=limit_scales)

        else:
            coords_densified = densify_hucs_(coords, coords_raw, rivers, limit_scales=limit_scales)

    else:
        coords_densified = densify_hucs_(coords, coords_raw, rivers, limit_scales=limit_scales)

    return watershed_workflow.split_hucs.SplitHUCs([shapely.geometry.Polygon(coords_densified)])

def densify_hucs_(coords, coords_raw, rivers, limit_scales=None):
    """This function increases the resolution of huc boundary by adding equally spaced interpolated points

     Parameters:
     -----------
      coords: List
        coordinates of the huc segment to be densified
      coords_raw: List
        coordinates of the original huc segment from which points can be resmapled
      limit_scales: int or List
        limit of section length above which more points are added, either a constant value or a list for step refinement
        [near_distance, near_length_scale, far_distance, far_length_scale]

      Returns
    -------
    coords_densified: List
        densified coordinates of a huc segment
    """
    adaptive = type(limit_scales) is list  # setting up flag

    coords_densified = coords.copy()
    j = 0
    for i in range(len(coords) - 1):

        # calculation of limit for a set of point
        if adaptive:
            limit = limit_from_river_distance([coords[i], coords[i + 1]], limit_scales, rivers)
        else:
            limit = limit_scales

        section_length = math.dist(coords[i], coords[i + 1])

        if section_length > limit:
            number_new_points = int(section_length // limit)
            end_points = [coords[i], coords[i + 1]]  # points betwen which more points will be added

            if adaptive:
                new_points = interpolate_simple(end_points, number_new_points)
            else:
                new_points = interpolate_with_orig(end_points, coords_raw, number_new_points)

            coords_densified[j + 1:j + 1] = new_points
            j += number_new_points
        j += 1

    return coords_densified

def interpolate_with_orig(end_points, interp_data, n):
    """This function adds desired number of new points between end points a segment (huc or river)
    resampling from orinal data

    Parameters
    ----------
    end_points: List
        end points of the segment whihc needs to be densified
    interp_data: List
        coordinates in the original segment, will be used to create an interpolator
    n: int
        number of new "equally" spaced points to be added

    Returns:
    --------
    new_points: List
        coordinates of the densified segment
    """

    inds = [watershed_workflow.utils.closest_point_ind(point, interp_data) for point in end_points
            ]  # point-indices on original network slicing a section for interpolation
    if inds[1] < inds[0]:  # this is to deal with corner case of interpolation of the last segment
        inds[1] = -2
    section_interp_data = np.array(interp_data[inds[0]:inds[1] + 1])  # coordinates on section
    a = np.array(end_points)
    (dx, dy) = abs(a[0, :] - a[1, :])
    if dx > dy:  # interpolating on x axis
        f = interpolate.interp1d(section_interp_data[:, 0],
                                 section_interp_data[:, 1],
                                 fill_value='extrapolate')  # creating interpolator
        xnew = np.linspace(end_points[0][0], end_points[1][0],
                           n + 2)[1:-1]  # new xs equally space between existing points
        ynew = f(xnew)  # interpolated ys
    else:  # interpolating on y axis
        f = interpolate.interp1d(section_interp_data[:, 1],
                                 section_interp_data[:, 0],
                                 fill_value='extrapolate')  # creating interpolator
        ynew = np.linspace(end_points[0][1], end_points[1][1],
                           n + 2)[1:-1]  # new ys equally space between existing points
        xnew = f(ynew)  # interpolated xs
    new_points = [(xnew[k], ynew[k]) for k in range(n)]
    return new_points

def interpolate_simple(end_points, n):
    """This function does not use any original data, just adds new equally spaced points based on linear interpolation"""
    xnew = np.linspace(end_points[0][0], end_points[1][0],
                       n + 2)[1:-1]  # new xs equally space between existing points
    ynew = np.linspace(end_points[0][1], end_points[1][1],
                       n + 2)[1:-1]  # new ys equally space between existing points
    new_points = [(xnew[k], ynew[k]) for k in range(n)]
    return new_points

def treat_segment_collinearity(segment_coords, tol=1e-6):
    """This functions removes collinearity from a node segment by making small pertubations orthogonal to the segment"""
    col_checks = []
    for i in range(0,
                   len(segment_coords)
                   - 2):  # traversing along the segment, checking 3 consecutive points at a time
        p0 = segment_coords[i]
        p1 = segment_coords[i + 1]
        p2 = segment_coords[i + 2]
        if check_collinearity(p0, p1, p2,
                              tol=tol):  # treating collinearity through a small pertubation
            del_ortho = 10 * tol  # shift in the middle point
            m = (p2[1] - p0[1]) / (p2[0] - p0[0])
            if abs(m) == float('inf'):
                m = 1e6
            del_y = del_ortho / (1 + m**2)**0.5
            del_x = -1 * del_ortho * m / (1 + m**2)**0.5
            p1 = (p1[0] + del_x, p1[1] + del_y)
            segment_coords[i + 1] = p1
        col_checks.append(check_collinearity(p0, p1, p2))
    assert (sum(col_checks) == 0)
    return segment_coords

def check_collinearity(p0, p1, p2, tol=1e-6):
    """this fucntion checks if three points are collinear for given tolerance value"""
    x1, y1 = p1[0] - p0[0], p1[1] - p0[1]
    x2, y2 = p2[0] - p0[0], p2[1] - p0[1]
    return abs(x1*y2 - x2*y1) < tol

def limit_from_river_distance(segment_ends, limit_scales, rivers):
    """Returns a graded refinement function based upon a distance function from rivers, for use with DensifyHucs function.
    HUC segment resolution must be higher in near_distance when the HUC segment midpoint is within near_distance from the river network.
    Length must be smaller than away_length when the HUC segment midpoint is at least away_distance from the river network.
    Area must be smaller than a linear interpolant between
    """
    near_distance, near_length, away_distance, away_length = limit_scales
    p0 = shapely.geometry.Point(segment_ends[0])
    p1 = shapely.geometry.Point(segment_ends[1])
    p_mid = shapely.geometry.Point([(segment_ends[0][0] + segment_ends[1][0]) / 2,
                                    (segment_ends[0][1] + segment_ends[1][1]) / 2])
    river_multilines = [shapely.geometry.MultiLineString(list(river)) for river in rivers]
    distance = min([
        min(p0.distance(river_multiline), p_mid.distance(river_multiline),
            p1.distance(river_multiline)) for river_multiline in river_multilines
    ])

    if distance > away_distance:
        length = away_length
    elif distance < near_distance:
        length = near_length
    else:
        length = near_length + (distance-near_distance) / (away_distance-near_distance) * (
            away_length-near_length)

    return length


import sys
import math

def rotRight(value, size, amount):
    bits = bin(value)
    bits = bits[2:]
    if len(bits) < size:
        bits = '0' * (size - len(bits)) + bits
    for i in range(amount):
        lastBit = bits[size - 1]
        bits = bits[:size - 1]
        bits = lastBit + bits
    return int(bits, 2)

def rotLeft(value, size, amount):
    bits = bin(value)
    bits = bits[2:]
    if len(bits) < size:
        bits = '0' * (size - len(bits)) + bits
    for i in range(amount):
        firstBit = bits[0]
        bits = bits[1:]
        bits = bits + firstBit
    return int(bits, 2)

if(len(sys.argv) != 4):
    print('This script should be run as \'exploit.py [hex hash] [hash-email] [victim-email]\'')
    sys.exit(1);

hs = sys.argv[1]
hsmail = sys.argv[2]
vtmail = sys.argv[3]

if hs.startswith('0x'):
    hs = hs[2:]
hs = int(hs, 16)
for byte in [ord(ch) for ch in hsmail[::-1]]:
    hs = rotRight(hs, 32, 4)
    hs = int(math.fabs(hs - byte))
    if hs >= math.pow(2, 32):
        hs = int(hs % math.pow(2, 32))

for byte in [ord(ch) for ch in vtmail]:
    hs = hs + byte
    if hs >= math.pow(2, 32):
        hs = int(hs % math.pow(2, 32))
    hs = rotLeft(hs, 32, 4)

print(hex(hs))

cryptography 1/week 2/code/assignment 1/exploit.py


import pandas as pd
import numpy as np
from datetime import  datetime
import BA_DataReader.Stocks as stocks
from C_Data.configs import *

import matplotlib.pyplot as plt

plt.style.use('fivethirtyeight')

#from google.colab import files
#uploaded = files.upload()

aapl = stocks.Stocks.getQuote('AAPL')
#print (aapl)

#create simple moving average 30 days
SMA30 = pd.DataFrame()
SMA30[configs.ADJ_CLOSE] = aapl[configs.ADJ_CLOSE].rolling(window = 30).mean()
#print(SMA30)

#create simple moving 100 day average
SMA100 = pd.DataFrame()
SMA100[configs.ADJ_CLOSE] = aapl[configs.ADJ_CLOSE].rolling(window = 100).mean()
#print(SMA100)

def quote():
    plt.figure(figsize=(12.5, 4.5))
    plt.plot(aapl[configs.ADJ_CLOSE], label = 'AAPL')
    plt.plot(SMA30[configs.ADJ_CLOSE], label = 'SMA30')
    plt.plot(SMA100[configs.ADJ_CLOSE], label = 'SMA100')
    plt.title('Apple adj close price history')
    plt.xlabel('Dates')
    plt.ylabel('Adj Close Price in USD')
    plt.legend(loc ='upper left')
    plt.show()

#quote()

#create a new dataframe to store all the data
data = pd.DataFrame()
data['AAPL'] = aapl[configs.ADJ_CLOSE]
data['sma30'] = SMA30[configs.ADJ_CLOSE]
data['sma100'] = SMA100[configs.ADJ_CLOSE]

#create a buy and sell signal function
def buySell(data):
    sigPriceBuy = []
    sigPriceSell = []
    flag = -1

    for i in range(len(data)):
        if data['sma30'][i] > data['sma100'][i]:
            if flag !=1:
                sigPriceBuy.append(data['AAPL'][i])
                sigPriceSell.append(np.nan)
                flag = 1
            else:
                sigPriceBuy.append(np.nan)
                sigPriceSell.append(np.nan)
        elif data['sma30'][i] < data['sma100'][i]:
            if flag != 0:
                sigPriceBuy.append(np.nan)
                sigPriceSell.append(data['AAPL'][i])
                flag = 0
            else:
                sigPriceBuy.append(np.nan)
                sigPriceSell.append(np.nan)
        else:
            sigPriceBuy.append(np.nan)
            sigPriceSell.append(np.nan)

    return (sigPriceBuy, sigPriceSell)

#store the buy and sell data into a variable
buySellData = buySell(data)
data['BuySignalPrice'] = buySellData[0]
data['SellSignalPrice'] = buySellData[1]

#print(data)

plt.figure(figsize=(12.6, 4.6))
plt.plot(data['AAPL'], label = 'AAPL', alpha= 0.35)
plt.plot(data['sma30'], label = 'sma30', alpha= 0.35)
plt.plot(data['sma100'], label = 'sma100', alpha= 0.35)

plt.scatter(data.index, data['BuySignalPrice'], label = 'Buy', marker = '^', color = 'green')
plt.scatter(data.index, data['SellSignalPrice'], label = 'Sell', marker = 'v', color= 'red')
plt.title = ('Apple adjusted close price history buy and sell signals')
plt.xlabel('Dates')
plt.ylabel('Adj close price in usd')
plt.legend(loc='upper left')
plt.show()


# uncompyle6 version 3.7.4
# Python bytecode 3.4 (3310)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /usr/lib/python3.4/site-packages/pintail/elasticsearch/__init__.py
# Compiled at: 2016-07-29 10:39:34
# Size of source mod 2**32: 7236 bytes
import codecs, datetime, os, urllib, uuid, pintail.search, pintail.site, pintail.mallard, elasticsearch

class ElasticSearchPage(pintail.mallard.MallardPage):

    def __init__(self, directory):
        pintail.mallard.MallardPage.__init__(self, directory, 'pintail-elasticsearch.page')

    @property
    def source_path(self):
        return self.stage_path

    @property
    def searchable(self):
        return False

    def stage_page(self):
        pintail.site.Site._makedirs(self.directory.stage_path)
        from pkg_resources import resource_string
        xsl = resource_string(__name__, 'pintail-elasticsearch.page')
        fd = open(self.stage_path, 'w', encoding='utf-8')
        fd.write(codecs.decode(xsl, 'utf-8'))
        fd.close()

    @classmethod
    def get_pages_dir(cls, directory):
        if directory.path == '/':
            return [cls(directory)]
        else:
            return []

class ElasticSearchProvider(pintail.search.SearchProvider, pintail.site.ToolsProvider, pintail.site.XslProvider):
    analyzers = {'ar': 'arabic',
     'bg': 'bulgarian',
     'ca': 'catalan',
     'ckb': 'sorani',
     'cs': 'czech',
     'da': 'danish',
     'de': 'german',
     'el': 'greek',
     'en': 'english',
     'es': 'spanish',
     'eu': 'basque',
     'fa': 'persian',
     'fi': 'finnish',
     'fr': 'french',
     'ga': 'irish',
     'gl': 'galician',
     'hi': 'hindi',
     'hu': 'hungarian',
     'hy': 'armenian',
     'id': 'indonesian',
     'it': 'italian',
     'ja': 'cjk',
     'ko': 'cjk',
     'lt': 'lithuanian',
     'lv': 'latvian',
     'nb': 'norwegian',
     'nl': 'dutch',
     'nn': 'norwegian',
     'no': 'norwegian',
     'pt': 'portuguese',
     'pt-br': 'brazilian',
     'ro': 'romanian',
     'ru': 'russian',
     'sv': 'swedish',
     'th': 'thai',
     'tr': 'turkish',
     'zh': 'cjk'}

    def __init__(self, site):
        pintail.search.SearchProvider.__init__(self, site)
        self.epoch = site.config.get('search_epoch')
        if self.epoch is None:
            self.epoch = datetime.datetime.now().strftime('%Y-%m-%d') + '-' + str(uuid.uuid1())
        elhost = self.site.config.get('search_elastic_host')
        self.elastic = elasticsearch.Elasticsearch([elhost])
        self._indexes = []

    @classmethod
    def get_xsl(cls, site):
        return [os.path.join(site.tools_path, 'pintail-elasticsearch.xsl')]

    @classmethod
    def get_xsl_params(cls, output, obj):
        if not (output == 'html' and isinstance(obj, pintail.site.Page)):
            return []
        if not isinstance(obj.site.search_provider, ElasticSearchProvider):
            return []
        lang = 'en'
        ret = [
         (
          'pintail.elasticsearch.host', obj.site.config.get('search_elastic_host')),
         (
          'pintail.elasticsearch.epoch', obj.site.search_provider.epoch),
         (
          'pintail.elasticsearch.index', obj.site.search_provider.get_index(lang))]
        domains = obj.get_search_domains()
        if domains[0] != 'none':
            ret.append(('pintail.search.domains', ' '.join(domains)))
        return ret

    @classmethod
    def build_tools(cls, site):
        from pkg_resources import resource_string
        xsl = resource_string(__name__, 'pintail-elasticsearch.xsl')
        fd = open(os.path.join(site.tools_path, 'pintail-elasticsearch.xsl'), 'w', encoding='utf-8')
        fd.write(codecs.decode(xsl, 'utf-8'))
        fd.close()

    def get_index(self, lang):
        ix = self.site.config.get('search_elastic_index') or 'pintail@{lang}'
        return ix.format(lang=lang, epoch=self.epoch)

    def get_analyzer(self, lang):
        if lang.lower() in ElasticSearchProvider.analyzers:
            return ElasticSearchProvider.analyzers[lang.lower()]
        if '-' in lang:
            return ElasticSearchProvider.get_analyzer(lang[:lang.rindex('-')])
        return 'english'

    def create_index(self, lang):
        if lang in self._indexes:
            return
        self._indexes.append(lang)
        elindex = self.get_index(lang)
        if self.elastic.indices.exists(elindex):
            pass
        else:
            analyzer = self.get_analyzer(lang)
            self.elastic.indices.create(index=elindex, body={'mappings': {'page': {'properties': {'path': {'type': 'string',  'index': 'not_analyzed'},  'lang': {'type': 'string',  'index': 'not_analyzed'},  'domain': {'type': 'string',  'index': 'not_analyzed'},  'epoch': {'type': 'string',  'index': 'not_analyzed'},  'title': {'type': 'string',  'analyzer': analyzer},  'desc': {'type': 'string',  'analyzer': analyzer},  'keywords': {'type': 'string',  'analyzer': analyzer},  'content': {'type': 'string',  'analyzer': analyzer}}}}})

    def index_page(self, page):
        self.site.log('INDEX', page.site_id)
        lang = 'en'
        self.create_index(lang)
        title = page.get_title(hint='search')
        desc = page.get_desc(hint='search')
        keywords = page.get_keywords(hint='search')
        content = page.get_content(hint='search')
        elid = urllib.parse.quote(page.site_id, safe='') + '@' + self.epoch
        elindex = self.get_index(lang)
        domains = page.get_search_domains()
        self.elastic.index(index=elindex, doc_type='page', id=elid, body={'path': page.site_path,
         'lang': lang,
         'domain': domains,
         'epoch': self.epoch,
         'title': title,
         'desc': desc,
         'keywords': keywords,
         'content': content})


# authors: Graydon Hall and Jared Kraus Group 2
import math
import os

from data_import import final_df
from user_input import obtain_user_input

import pandas as pd
import numpy as np

# required so pycharm prints out pandas dataframes correctly
desired_width = 320
pd.set_option('display.width', desired_width)
np.set_printoptions(linewidth=desired_width)
pd.set_option('display.max_columns', 17)

def ind_player_stats():
    """
    Uses obtain_user_input() to obtain fantasy football league type, football position, Ranking system for players,
    and count of players to study. The program prints out a table with individual stats for the number of players
    specified. It then prints out a summary statistics table for the specified number of players. Finally, it gives a
    table of number of players taken in the first 16 rounds of a fantasy football draft, for both STD leagues and PPR
    leagues.
    :param: none
    :return: nothing
    """
    league_type, pos, rank_sys, n_rows = obtain_user_input()
    # testing values
    # league_type = 'PPR'
    # pos = 'TE'
    # rank_sys = 'ADP'
    # n_rows = 10

    # positions: 'WR', 'RB', 'QB', 'TE'
    # key = position name
    # value = list with [rows to display relevant to that position, corresponding indexes for dataframe. ]
    pos_df_cols_dict = {
        "RB": [['NAME', 'TEAM', 'ADP', 'GP', 'RK', 'TTL PTS', 'PTS/GAME', 'RUSHING YDS','Rushing Pts', 'RECEIVING YARDS', 'Receiving Pts'],
                   ['Player Info','Player Info','Projections','2020','2020','2020', '2020', '2020', '2020', '2020', '2020']],
        "WR": [['NAME', 'TEAM', 'ADP', 'GP', 'RK', 'TTL PTS', 'PTS/GAME', 'RUSHING YDS','Rushing Pts', 'RECEIVING YARDS', 'Receiving Pts'],
                       ['Player Info','Player Info','Projections','2020','2020','2020', '2020', '2020', '2020', '2020', '2020']],
        "QB": [['NAME', 'TEAM', 'ADP', 'GP', 'RK', 'TTL PTS', 'PTS/GAME', 'RUSHING YDS','Rushing Pts', 'PASSING YDS', 'Passing Pts'],
                           ['Player Info','Player Info','Projections','2020','2020','2020', '2020', '2020', '2020', '2020', '2020']],
        "TE": [['NAME', 'TEAM', 'ADP', 'GP', 'RK', 'TTL PTS', 'PTS/GAME', 'RUSHING YDS','Rushing Pts', 'RECEIVING YARDS', 'Receiving Pts'],
                           ['Player Info','Player Info','Projections','2020','2020','2020', '2020', '2020', '2020', '2020', '2020']],

    }

    # filter table to league_type
    # filter to pos
    # rank based on rank_sys
    idx = pd.IndexSlice  # index slice object used to slice df
    # display columns based on position (from pos_df_cols_dict)
    position_df = final_df[pos_df_cols_dict[pos][0]].loc[idx[league_type,pos],:].copy()

    if rank_sys == 'ADP':
        ascending = True  # sorting: want ascending if ADP is selected
    else:
        ascending = False  # sorting: want descending if 2020 pts is selected

    position_df.sort_values(by=[rank_sys], ascending=ascending, inplace=True)  # sort DF
    # add multi-indexing based on index labels provided in pos_df_cols_dict
    position_df.columns = pd.MultiIndex.from_arrays([pos_df_cols_dict[pos][1], position_df.columns])

    # header note on NaN values
    print("\n\n\n"
          "** Note: Players with NaN values for 2020 stats were outside of top 300 players based on 2020 pts. ")
    print("         Players with NaN values for ADP stats were outside of top 300 players based on 2021 ADP projections.\n ")

    print(f"------------------------------------ Individual Statistics for top {n_rows} {pos}s ({league_type})------------------------------------")
    print(position_df.head(n_rows).to_string(index=False))  # display individual player statistics data for top X players.

    print(f"\n------------------------------------- Overall Statistics for top {n_rows} {pos}s ({league_type})-------------------------------------")
    # Note for TA: .describe() is used here (required point in rubric)
    # Dr. Marasco it was okay to use .describe() on a subset of the data. s
    print(position_df.head(n_rows).describe().iloc[1:, :])  # display descriptive statistics for top X players

    final_df_mod = final_df.copy()  # get new copy of our DF
    final_df_mod.reset_index(inplace=True)  # reset index
    final_df_mod.set_index('ID', inplace=True)  # just make ID the index now.

    # define function to map ADP to a a round
    def round_mapper(x):
        return np.ceil(x['RK_ADP']/10)

    # create column giving the round each player is drafted based on ADP
    final_df_mod['Round Drafted'] = final_df_mod.apply(round_mapper, axis=1)

    # filter to position and only do first 16 rounds
    position_df_rds = final_df_mod.loc[(final_df_mod['POS'] == pos) & (final_df_mod['Round Drafted'] <= 16)]

    # use pivot to see how many of that position is taken in each round
    rounds_pivot = position_df_rds.pivot_table(values='ADP', index='League Type', columns='Round Drafted',
                                               aggfunc=np.count_nonzero, fill_value=0)

    # dipslay table and heading
    print(f"\n-----------------------------------Count of {pos}s taken in first 16 rounds (based on ADP)-----------------------------------")
    print(rounds_pivot)

Final Project - Fantasy Football Data/individual_pos_stats.py


# A Sequence contains segments which then contain the pulses. The Sequence
# provides the information for the AWGs, in which order to play the segments.
#
# author: Michael Kerschbaum
# created: 04/2019

import numpy as np
import pycqed.measurement.waveform_control.pulsar as ps
from collections import OrderedDict as odict
from copy import deepcopy
import logging

from pycqed.utilities.timer import Timer

log = logging.getLogger(__name__)

class Sequence:
    """
    A Sequence consists of several segments, which can be played back on the
    AWGs sequentially.
    """

    RENAMING_SEPARATOR = "+"
    AMPLITUDE_ROUNDING_DIGITS = 7
    """Specifies the rounding precision when processing waveform amplitudes
    in harmonize_amplitude method. If this parameter has value n, then the
    waveform amplitudes will be rounded to the n-th digit of V (volt)."""

    def __init__(self, name, segments=()):
        """
        Initializes a Sequence object
        Args:
            name: Name of the sequence
            segments (list, tuple): list of segments to add to the sequence
        """
        self.name = name
        self.timer = Timer(self.name)
        self.pulsar = ps.Pulsar.get_instance()
        self.segments = odict()
        self.awg_sequence = {}
        self.repeat_patterns = {}
        self.extend(segments)
        self.is_resolved = False
        self.awg_scaling_factors = dict()
        """A list of AWG names whose pulse amplitudes has processed with
        method 'self.harmonize_amplitude'."""

    def add(self, segment):
        if segment.name in self.segments:
            raise NameError('Name {} already exisits in the sequence!'.format(
                segment.name))
        self.segments[segment.name] = segment
        if len(self.segments) == 1:
            self.segments[segment.name].is_first_segment = True
        self.timer.children.update({segment.name: segment.timer})

    def extend(self, segments):
        """
        Extends the sequence given a list of segments
        Args:
            segments (list): segments to add to the sequence
        """
        for seg in segments:
            self.add(seg)

    def upload(self, awgs_to_upload='all'):
        """Upload the sequence to the AWG using self.pulsar instrument.
        """
        if awgs_to_upload != 'all':
            log.warning('Sequence:upload: reducing upload overhead manually '
                        'with awgs_to_upload is deprecated. Set '
                        'pulsar.use_sequence_cache to True for automatic '
                        'reduction of upload overhead.')
        self.pulsar.program_awgs(self, awgs=awgs_to_upload)

    @Timer()
    def generate_waveforms_sequences(self, awgs=None,
                                     get_channel_hashes=False,
                                     resolve_segments=None,
                                     trigger_groups=None):
        """
        Calculates and returns
            * waveforms: a dictionary of waveforms used in the sequence,
                indexed by their hash value
            * sequences: For each awg, a list of elements, each element
                consisting of a waveform-hash for each codeword and each
                channel
        :param awgs: a list of AWG names. If None, waveforms will be
            generated for all AWGs used in the sequence.
        :param get_channel_hashes: (bool, default: False) do not create
            waveforms, and instead return a dict of channel-elements indexed
            by channel names, each channel-element consisting of a
            waveform-hash for each codeword on that channel.
        :param resolve_segments: (bool, optional) whether the segments in the
            sequence still need to be resolved. If not provided,
            self.is_resolved is checked to determine whether segments
            need to be resolved.
        :return: a tuple of waveforms, sequences as described above if
            get_channel_hashes==False. Otherwise, a tuple channel_hashes,
            sequences.
            Note that the information contained in channel_hashes is the
            same as in sequences, but in a different structure, which has the
            channel name as highest-level key:
            sequences[awg][elname][cw][chid] == channel_hashes[ch][elname][cw]
        """
        waveforms = {}
        sequences = {}
        channel_hashes = {}
        if resolve_segments or (resolve_segments is None
                                and not self.is_resolved):
            for seg in self.segments.values():
                seg.resolve_segment()
                seg.gen_elements_on_awg()

        if trigger_groups is None:
            trigger_groups = set()
            for seg in self.segments.values():
                trigger_groups |= set(seg.elements_on_awg)

        if awgs is None:
            awgs = set()
            for group in trigger_groups:
                awgs.add(self.pulsar.get_awg_from_trigger_group(group))

        # Note that method 'self.generate_waveforms_sequences' will be
        # called by 'pulsar._program_awgs' multiple times, but we only
        # want to execute 'self.harmonize_amplitude' once. Otherwise,
        # we will have wrong pulse amplitudes and scaling parameters.
        for awg in awgs:
            if awg not in self.awg_scaling_factors.keys():
                self.awg_scaling_factors[awg] = \
                    self.harmonize_amplitude(awg)

        for segname, seg in self.segments.items():
            for group in trigger_groups:
                awg = self.pulsar.get_awg_from_trigger_group(group)
                if awg not in awgs:
                    continue
                scaling_factors = self.awg_scaling_factors[awg]
                sequences.setdefault(awg, odict())
                # Store name of the segment as key and None as value.
                # This is used when compiling docstrings in seqc.
                sequences[awg].setdefault(segname, None)

                # Take element metadata from the resolved segments.
                element_metadata = seg.element_metadata
                elnames = seg.elements_on_awg.get(group, [])
                for elname in elnames:
                    sequences[awg].setdefault(elname, {'metadata': {}})
                    for cw in seg.get_element_codewords(elname, trigger_group=group):
                        sequences[awg][elname].setdefault(cw, {})
                        for ch in seg.get_element_channels(elname, trigger_group=group):
                            h = seg.calculate_hash(elname, cw, ch)
                            chid = self.pulsar.get(f'{ch}_id')
                            sequences[awg][elname][cw][chid] = h
                            if get_channel_hashes:
                                if ch not in channel_hashes:
                                    channel_hashes[ch] = {}
                                if elname not in channel_hashes[ch]:
                                    channel_hashes[ch][elname] = {}
                                channel_hashes[ch][elname][cw] = h
                            else:
                                if h not in waveforms:
                                    wf = seg.waveforms(awgs={awg},
                                        elements={elname}, channels={ch},
                                        codewords={cw})
                                    waveforms[h] = wf.popitem()[1].popitem()[1]\
                                                     .popitem()[1].popitem()[1]
                    if elname in seg.acquisition_elements:
                        sequences[awg][elname]['metadata']['acq'] = \
                            seg.acquisition_mode
                    else:
                        sequences[awg][elname]['metadata']['acq'] = False
                    sequences[awg][elname]['metadata']['allow_filter'] = \
                        seg.allow_filter
                    sequences[awg][elname]['metadata'].setdefault('trigger_groups', set())
                    sequences[awg][elname]['metadata']['trigger_groups'].add(group)
                    # Write modulation and sine configuration to element
                    if elname in element_metadata.keys() \
                            and 'mod_config' in element_metadata[elname].keys()\
                                and element_metadata[elname]['mod_config']:
                        sequences[awg][elname]['metadata']['mod_config'] = \
                            element_metadata[elname]['mod_config']
                    elif seg.mod_config:
                        sequences[awg][elname]['metadata']['mod_config'] =\
                            seg.mod_config
                    if seg.sine_config:
                        sequences[awg][elname]['metadata']['sine_config'] = \
                            seg.sine_config
                    # Pass command table scaling factors to element metadata
                    if elname in scaling_factors.keys():
                        sequences[awg][elname]['metadata']['scaling_factor'] \
                            = scaling_factors[elname]
                # Experimental feature to sweep values of nodes of ZI HDAWGs
                # in a hard sweep. See the comments above the sweep_params
                # property in Segment.
                if seg.sweep_params is not None and len(seg.sweep_params):
                    sequences[awg][elnames[0]]['metadata']['loop'] = len(
                        list(seg.sweep_params.values())[0])
                    sequences[awg][elnames[0]]['metadata']['sweep_params'] = \
                        {k[len(awg) + 1:]: v for k, v in
                         seg.sweep_params.items() if k.startswith(awg + '_')}
                    sequences[awg][elnames[-1]]['metadata']['end_loop'] = True
        self.is_resolved = True
        if get_channel_hashes:
            return channel_hashes, sequences
        else:
            return waveforms, sequences

    @staticmethod
    def harmonize_element_lengths(sequences, awgs=None):
        """
        Given a list of sequences, this function ensures for all AWGs and all
        elements that the element length is the same in all sequences. This is
        done by setting the length of each AWG element to the maximum length
        across all sequences. After this, overlap is checked and the sequences
        are marked as resolved.
        :param sequences: a list of sequences
        :param awgs: a list of AWG names. If None, lengths will be harmonized
            for all AWGs.
        """
        seq_groups = []
        if awgs is None:
            awgs = sequences[0].pulsar.awgs
        # collect element lengths
        lengths = odict()
        for i, seq in enumerate(sequences):
            seq_groups.append(set())
            for seg in seq.segments.values():
                seg.resolve_segment()
                seg.gen_elements_on_awg()
            seq_groups[i] |= set(
                [group for group in seg.elements_on_awg
                 if seq.pulsar.get_awg_from_trigger_group(group) in awgs])
            for group in seq_groups[i]:
                if group not in lengths:
                    lengths[group] = odict()
                for segname, seg in seq.segments.items():
                    elnames = seg.elements_on_awg.get(group, [])
                    for elname in elnames:
                        if elname not in lengths[group]:
                            lengths[group][elname] = []
                        lengths[group][elname].append(
                            seg.element_start_end[elname][group][1])
        # set element lengths to the maximum of the collected values
        for i, seq in enumerate(sequences):
            for group in seq_groups[i]:
                for segname, seg in seq.segments.items():
                    elnames = seg.elements_on_awg.get(group, [])
                    for elname in elnames:
                        seg.element_start_end[elname][group][1] = max(
                            lengths[group][elname])
            # test for overlaps
            for segname, seg in seq.segments.items():
                seg._test_overlap()
            # mark sequence as resolved
            seq.is_resolved = True

    def harmonize_amplitude(self, awg):
        """Rescale waveform amplitudes such that the largest pulse amplitude
        in an element is the same as the largest in that sequence. The
        scaling factor is saved in the dictionary scaling_factors and
        passed to element metadata, such that the original waveform can be
        retrieved when generating command table entries. This allows reusing
        waveforms to the largest extent based on wave hashes. Note that
        the rescaling will be skipped on the target AWG modules where command
        table is not activated.

        Args:
            awg: (str) AWG name to be processed.

        Returns:
            scaling_factors: (dict) factors to be passed to the command table
                entries in order to retrieve the original pulse amplitude.
        """
        scaling_factors = dict()

        # Command table wave sequencing is only implemented for
        # AWG interfaces that have awg_modules attribute.
        awg_interface = self.pulsar.awg_interfaces[awg]
        if not hasattr(awg_interface, "awg_modules"):
            return dict()

        awg_modules = awg_interface.awg_modules
        for awg_module in awg_modules:
            # harmonize_amplitude only affects AWGs whose command table wave
            # sequencing is enabled.
            if not self.pulsar.check_channel_parameter(
                    awg=awg_module.awg.name,
                    channel=awg_module.get_i_channel(),
                    parameter_suffix="_harmonize_amplitude"
            ):
                continue

            if not self.pulsar.check_channel_parameter(
                awg=awg_module.awg.name,
                channel=awg_module.get_i_channel(),
                parameter_suffix="_use_command_table"
            ):
                logging.warning(
                    f"On {awg_module.awg.name}_"
                    f"{awg_module.analog_channel_ids[0]}: PycQED will "
                    f"not harmonize amplitude for this AWG module "
                    f"because this feature only works when command table "
                    f"is enabled. Please set \"_use_command_table\" "
                    f"parameter to true for this AWG module if you want "
                    f"to allow harmonizing amplitude.")
                continue
            # Only check analog channels
            channel_ids = awg_module.analog_channel_ids
            # Collects all elements that are relevant to this AWG module.
            # Records the maximum amplitude in each element.
            elements_on_channel = dict()
            element_max_amp = dict()
            for segname, seg in self.segments.items():
                elements_on_channel[segname] = set()
                for chid in channel_ids:
                    channel = self.pulsar._id_channel(
                        chid, awg_interface.awg.name)
                    elements_on_channel[segname] |= \
                        seg.elements_on_channel.get(channel, set())

                for elname in elements_on_channel[segname]:
                    current_max_amp = 0
                    for pulse in seg.elements[elname]:
                        # boolean parameter indicating whether one pulse
                        # overlaps with the current AWG module
                        pulse_overlaps_with_channel = any([
                            self.pulsar.get(f'{channel}_id') in channel_ids
                            for channel in pulse.channels])
                        # boolean parameter indicating whether one pulse
                        # is played solely on the current AWG module
                        pulse_only_on_channel = all([
                            self.pulsar.get(f'{channel}_id') in channel_ids
                            for channel in pulse.channels])

                        if not pulse_overlaps_with_channel:
                            continue
                        elif not pulse_only_on_channel:
                            # The pulse only partially overlaps with the
                            # current AWG module. It is currently not
                            # supported to harmonize amplitude across
                            # multiple AWG modules.
                            raise RuntimeError(
                                f"On element {elname}: Harmonizing amplitude "
                                f"does not support pulse played across "
                                f"multiple AWG modules. Please disable "
                                f"harmonizing amplitude for all AWG modules "
                                f"relevant to this pulse."
                            )

                        if not pulse.SUPPORT_HARMONIZING_AMPLITUDE:
                            # Harmonizing amplitude is not allowed for this
                            # pulse type
                            raise RuntimeError(
                                f"On {awg_module.awg.name}: pulse {pulse.name} "
                                f"does not allow harmonizing amplitude. "
                                f"Please disable \"_harmonize_amplitude\" "
                                f"parameter for this AWG module."
                            )

                        if abs(getattr(pulse, "amplitude", 0)) > \
                                abs(current_max_amp):
                            current_max_amp = getattr(pulse, "amplitude", 0.0)
                    element_max_amp[elname] = current_max_amp

            if not len(element_max_amp) or \
                    max(element_max_amp.values()) == 0:
                # There is no element played on this awg module.
                continue
            # choose the pulse amplitude that has the largest absolute value
            max_val = max(element_max_amp.values())
            min_val = min(element_max_amp.values())
            sequence_max_amp = abs(max_val) \
                if abs(max_val) >= abs(min_val) else abs(min_val)
            for segname, seg in self.segments.items():
                for elname in elements_on_channel[segname]:
                    element_scaling_factor = \
                        element_max_amp[elname] / sequence_max_amp

                    # Saves element scaling factor to element metadata.
                    # This factors will be provided to command table
                    # entries to retrieve the correct absolute amplitude.
                    scaling_factors[elname] = dict()
                    for chid in channel_ids:
                        channel = self.pulsar._id_channel(
                            chid, awg_interface.awg.name)
                        scaling_factors[elname][channel] = \
                            element_scaling_factor

                    for pulse in seg.elements[elname]:
                        if len(pulse.channels) == 0 or \
                                not all([self.pulsar.get(f'{channel}_id')
                                         in channel_ids
                                         for channel in pulse.channels]):
                            continue
                        # Round amplitude to allow reusing waveform in
                        # the presence of finite numerical precision.
                        pulse.amplitude = round(
                            pulse.amplitude / element_scaling_factor,
                            self.AMPLITUDE_ROUNDING_DIGITS
                        ) if element_scaling_factor != 0 else \
                            round(sequence_max_amp,
                                  self.AMPLITUDE_ROUNDING_DIGITS)

        return scaling_factors

    def n_acq_elements(self, per_segment=False):
        """
        Gets the number of acquisition elements in the sequence.
        Args:
            per_segment (bool): Whether or not to return the number of
                acquisition elements per segment. Defaults to False.

        Returns:
            number of acquisition elements (list (if per_segment) or int)

        """
        n_readouts = [len(seg.acquisition_elements)
                      for seg in self.segments.values()]
        if not per_segment:
            n_readouts = np.sum(n_readouts)
        return n_readouts

    def n_segments(self):
        """
        Gets the number of segments in the sequence.
        """
        return len(self.segments)

    def repeat(self, pulse_name, operation_dict, pattern,
               pulse_channel_names=('I_channel', 'Q_channel')):
        """
        Creates a repetition dictionary keyed by awg channel for the pulse
        to be repeated.
        :param pulse_name: name of the pulse to repeat.
        :param operation_dict:
        :param pattern: repetition pattern (n_repetitions, nr_elements_per_loop or another loop-specification)
                        cf. Christian
        :param pulse_channel_names: names of the channels on which the pulse is
        applied.
        :return:
        """
        if operation_dict==None:
            pulse=pulse_name
        else:
            pulse = operation_dict[pulse_name]
        if not pulse.get('disable_repeat_pattern', False):
            repeat = dict()
            for ch in pulse_channel_names:
                if pulse[ch] is None:
                    continue
                repeat[pulse[ch]] = pattern
            self.repeat_patterns.update(repeat)
        return self.repeat_patterns

    def repeat_ro(self, pulse_name, operation_dict):
        """
        Wrapper for repeated readout
        :param pulse_name:
        :param operation_dict:
        :param sequence:
        :return:
        """
        return self.repeat(pulse_name, operation_dict,
                           (self.n_acq_elements(), 1))

    @staticmethod
    def merge(sequences, segment_limit=None, merge_repeat_patterns=True):
        """
        Merges a list of sequences. See documentation of Sequence.__add__()
        for more information on the merge of two sequences.
        Args:
            sequences (list): List of sequences to merge
            segment_limit (int): maximal number of segments in the merged sequence.
                if the total number of segments is higher, a list of sequences is
                returned. Default is None (all sequences are merged)
            merge_repeat_patterns (bool): Merges the readout pattern when
                 combining the sequences. If the readout pattern already exists, it adds
                 to the number of repetition of the pattern. Note that this behavior may
                 not work for all scenarios. In that case the patterns must be updated
                  manually after the merge and merge_repeat_patterns should be set to
                  False. Default: True.

        Returns: list of merged sequences

        Examples:
            >>> # No segment_limit
            >>> seq1 = Sequence('seq1')
            >>> seq1.extend(segments_of_seq1)  # 10 segments
            >>> seq2 = Sequence('seq2')
            >>> seq2.extend(segments_of_seq2) # 15 segments
            >>> seq_comb = Sequence.merge([seq1, seq2])
            >>> # returns a list with 1 sequence with 25 segments
            >>> # i.e. [seq1 + seq2]

            >>> # 20 segments limit
            >>> seq1 = Sequence('seq1')
            >>> seq1.extend(segments_of_seq1) # 10 segments
            >>> seq2 = Sequence('seq2')
            >>> seq2.extend(segments_of_seq2) # 15 segments
            >>> seq3 = Sequence('seq3')
            >>> seq3.extend(segments_of_seq3) # 5 segments
            >>> seq_comb = Sequence.merge([seq1, seq2, seq3])
            >>> # returns list of 2 sequences with 10 and 20 segments,
            >>> # i.e. [seq1, seq2 + seq3]

        """
        if len(sequences) == 0:
            raise ValueError("merge requires at least one sequence")
        elif len(sequences) == 1:
            # special case, return current sequence:
            return sequences
        sequences = [deepcopy(s) for s in sequences]
        merged_seqs = [sequences[0]]
        if segment_limit is None:
            segment_limit = np.inf

        segment_counter = sequences[0].n_segments()
        seg_occurences = [{s: 1 for s in sequences[0].segments}]
        for seq in sequences[1:]:
            assert seq.n_segments() <= segment_limit, \
                f"Sequence {seq.name} has more segments ({seq.n_segments()})" \
                f" than the segment_limit ({segment_limit}). Cannot merge " \
                f"without cropping the sequence."
            # if over segment_limit, add another separate sequence
            # to merged sequences
            if merged_seqs[-1].n_segments() + seq.n_segments() > segment_limit:
                merged_seqs.append(seq)
                seg_occurences.append({s: 1 for s in seq.segments})
                segment_counter = seq.n_segments()
            # otherwise merge sequences
            else:
                for seg_name, segment in seq.segments.items():
                    segment.is_first_segment = False
                    try:
                        merged_seqs[-1].add(segment)
                    except NameError:  # in case segment name exists, create new name
                        seg_occurences[-1][seg_name] += 1
                        new_name =seg_name + \
                                  f"_copy_from_merge_" \
                                  f"{seg_occurences[-1][seg_name] - 1}"
                        segment.rename(new_name)
                        merged_seqs[-1].add(segment)

                segment_counter += seq.n_segments()

                # update name of merged seq
                merged_seqs[-1].rename(merged_seqs[-1].name + Sequence.RENAMING_SEPARATOR + seq.name)
                if merge_repeat_patterns:
                    for ch_name, pattern in seq.repeat_patterns.items():
                        # if channel is already present, update number of
                        # repetitions
                        if ch_name in merged_seqs[-1].repeat_patterns:
                            pattern_prev = \
                                merged_seqs[-1].repeat_patterns[ch_name]
                            if pattern_prev[1:] != pattern[1:]:
                                raise NotImplementedError(
                                    f"The repeat patterns for channel: "
                                    f"{ch_name} do not have the same "
                                    f"'outer loop' specification (see "
                                    f"docstring Sequence.repeat). Repeat "
                                    f"patterns cannot be merged automatically. "
                                    f"Set merge_repeat_patterns to False and "
                                    f"update the repeat patterns manually.")
                            pattern_updated = (pattern_prev[0] + pattern[0],
                                               *pattern_prev[1:])
                            merged_seqs[-1].repeat_patterns[ch_name] = \
                                pattern_updated
                        # add repeat pattern
                        else:
                            merged_seqs[-1].repeat_patterns.update(
                                {ch_name: pattern})
        # compress names
        for ms in merged_seqs:
            name_parts = ms.name.split(Sequence.RENAMING_SEPARATOR)
            if len(name_parts) > 2:
                ms.rename(f"compressed_{name_parts[0]}-{name_parts[-1]}")
        return merged_seqs

    @staticmethod
    def interleave_sequences(seq_list_list):
        """
        Interleave a list of Sequence instances.
        :param seq_list_list: list of lists of Sequence instances
        :return: list of interleaved Sequences
        """
        # make sure all sequence lists in seq_list_list have the same length
        if len(set([len(seq_list) for seq_list in seq_list_list])) != 1:
            raise ValueError('The sequence lists do not have the same length.')
        # make sure all sequence lists in seq_list_list have the same segments
        if len(set([seq_list[0].n_acq_elements() for
                        seq_list in seq_list_list])) != 1:
            raise ValueError('The sequence lists do not have the same number '
                             'of segments.')

        interleaved_seqs = len(seq_list_list) * len(seq_list_list[0]) * ['']
        for i in range(len(seq_list_list)):
            interleaved_seqs[i::len(seq_list_list)] = seq_list_list[i]

        # rename sequences and timers
        for i, seq in enumerate(interleaved_seqs):
            seq.rename(f"Interleaved_Sequence_{i}")

        mc_points = [np.arange(interleaved_seqs[0].n_acq_elements()),
                     np.arange(len(interleaved_seqs))]

        return interleaved_seqs, mc_points

    @staticmethod
    def compress_2D_sweep(sequences, segment_limit=None,
                          merge_repeat_patterns=True, mc_points=None):
        """
        Compresses a list of sequences to a lower number of sequences
        (if possible), each of which containing the same amount of segments
        (assumes fixed number of readout per segment) while respecting the
        segment_limit (memory limit). Note that all sequences MUST have the
        same number of segments. Wraps the Sequence.merge() by computing an
        effective segment limit that minimizes the total number of sequences
        (to reduce upload time overhead) while keeping the (new)
        number of segments per sequence constant (it currently is a limitation
        of 2D sweeps that  all sequences must have same number of readouts)
        Args:
            sequences (list): list of sequences to compress, which all have
                the same number of segments
            segment_limit (int): maximal number of segments that can be in
                a sequence
            merge_repeat_patterns (bool): see docstring of Sequence.merge.
            mc_points: mc_points array of the original hardware sweep.
                Useful in case it differs from n_acq_elements().

        Returns: list of sequences for the compressed 2D sweep,
            new hardsweep points indices,
            new soft sweeppoints indices, and the compression factor

        """
        assert len(np.unique([s.n_segments() for s in sequences])) == 1, \
            "To allow compression, all sequences must have the same number " \
            "of segments"
        from pycqed.utilities.math import factors
        n_soft_sp = len(sequences)
        n_seg = sequences[0].n_segments()
        if segment_limit is None:
            segment_limit = np.inf

        # compute possible compression factors
        compression_fact = np.sort(factors(n_soft_sp))[::-1]

        for factor in compression_fact:
            if factor * n_seg > segment_limit:
                # too many segments in sequence, check for smaller factors
                continue
            elif factor == 1:
                # no compression possible
                log.warning(f'No compression possible: \n'
                      f'segments per sequence: \t\t{n_seg} \n'
                      f'limit of segments per sequence:\t{segment_limit}\n'
                      f'number of sequences: \t\t{n_soft_sp}\n'
                      f'To enable a compression, change the '
                      f'limit of segments to {compression_fact[-2] * n_seg} '
                      f'or the number of sequences  to x such that x has a '
                      f'factor f larger than 1 for which f * '
                      f'{n_seg} < {segment_limit}, e.g. x = '
                      f'{np.floor(segment_limit / n_seg)} (full compression)')
            break
        seg_lim_eff = factor * n_seg
        compressed_2D_sweep = Sequence.merge(sequences, seg_lim_eff,
                                              merge_repeat_patterns)
        if mc_points is None:
            hard_sp_ind = np.arange(compressed_2D_sweep[0].n_acq_elements())
            soft_sp_ind = np.arange(len(compressed_2D_sweep))
        else:
            hard_sp_ind = np.arange(len(mc_points)*len(sequences) //
                                    len(compressed_2D_sweep))
            soft_sp_ind = np.arange(len(compressed_2D_sweep))

        return compressed_2D_sweep, hard_sp_ind, soft_sp_ind, factor

    def rename(self, new_name):
        self.name = new_name
        self.timer.name = new_name

    def __repr__(self):
        string_repr = f"####### {self.name} #######\n"
        for seg_name, seg in self.segments.items():
            string_repr += str(seg) + "\n"
        return string_repr

    def __deepcopy__(self, memo):
        cls = self.__class__
        new_seq = cls.__new__(cls)
        memo[id(self)] = new_seq
        for k, v in self.__dict__.items():
            if k == "pulsar": # the reference to pulsar cannot be deepcopied
                setattr(new_seq, k, v)
            else:
                setattr(new_seq, k, deepcopy(v, memo))
        return new_seq

    def __getitem__(self, i):
        """
        Return a segment by its name or index, or a list of segments
        corresponding to a slice.
        :param i: (str, int, slice, list) Name, index, slice,
            or list of names/indices identifying the segment(s).
        :return: segment or list of segments
        """
        if isinstance(i, list):
            return [self[i] for i in i]
        elif isinstance(i, str):
            if i not in self.segments:
                raise KeyError(f'No segment with name "{i}" in the sequence '
                               f'{self.name}.')
            return self.segments[i]
        else:
            try:
                return list(self.segments.values())[i]
            except IndexError:
                raise IndexError(
                    f'Segment index {i} out of range. The sequence '
                    f'{self.name} has {len(self.segments)} segments.')

    def keys(self):
        """
        Returns the segments names (keys to access segments via __getitem__).
        :return: a set-like object providing the keys
        """
        return self.segments.keys()

    def plot(self, segments=None, show_and_close=True, **segment_plot_kwargs):
        """
        :param segments: list of segment names to plot
        :param show_and_close: (bool) show and close the plots (default: True)
        :param segment_plot_kwargs:
        :return: A list of tuples of figure and axes objects if show_and_close
            is False, otherwise no return value.
        """
        plots = []
        if segments is None:
            segments = self.segments.values()
        else:
            segments = [self.segments[s] for s in segments]
        for s in segments:
            plots.append(s.plot(show_and_close=show_and_close,
                                **segment_plot_kwargs))
        if show_and_close:
            return
        else:
            return plots


# hydroVallee.py
# Serveur pour le site d'information sur l'hydrométrie des rivières bretonnes.

import http.server
import socketserver
from urllib.parse import urlparse, parse_qs, unquote
import json

import matplotlib.pyplot as plt
import datetime as dt
import matplotlib.dates as pltd

import sqlite3

import os.path
#
# Définition du nouveau handler
#
class RequestHandler(http.server.SimpleHTTPRequestHandler):

  # sous-répertoire racine des documents statiques
  static_dir = '/client'

  #
  # On surcharge la méthode qui traite les requêtes GET
  #
  def do_GET(self):

    # On récupère les étapes du chemin d'accès
    self.init_params()

    if self.path_info[0] == 'stations':
      self.send_stations()

     # le chemin d'accès commence par /time
    elif self.path_info[0] == 'time':
      self.send_time()

    elif self.path_info[0] == 'graphe':
      self.send_courbes()

    else:

      self.send_static()

  #
  # On surcharge la méthode qui traite les requêtes HEAD
  #
  def do_HEAD(self):
    self.send_static()

  #
  # On envoie le document statique demandé
  #
  def send_static(self):

    # on modifie le chemin d'accès en insérant un répertoire préfixe
    self.path = self.static_dir + self.path

    # on appelle la méthode parent (do_GET ou do_HEAD)
    # à partir du verbe HTTP (GET ou HEAD)
    if (self.command=='HEAD'):
        http.server.SimpleHTTPRequestHandler.do_HEAD(self)
    else:
        http.server.SimpleHTTPRequestHandler.do_GET(self)

  #
  # on analyse la requête pour initialiser nos paramètres
  #
  def init_params(self):
    # analyse de l'adresse
    info = urlparse(self.path)
    self.path_info = [unquote(v) for v in info.path.split('/')[1:]]  # info.path.split('/')[1:]
    self.query_string = info.query
    self.params = parse_qs(info.query)

    # récupération du corps
    length = self.headers.get('Content-Length')
    ctype = self.headers.get('Content-Type')
    if length:
      self.body = str(self.rfile.read(int(length)),'utf-8')
      if ctype == 'application/x-www-form-urlencoded' :
        self.params = parse_qs(self.body)
    else:
      self.body = ''

    # traces
    print('info_path =',self.path_info)
    print('body =',length,ctype,self.body)
    print('params =', self.params)

  #
  # On envoie un document avec l'heure
  #
  def send_time(self):

    # on récupère l'heure
    time = self.date_time_string()

    # on génère un document au format html
    body = '<!doctype html>' + \
           '<meta charset="utf-8">' + \
           '<title>l\'heure</title>' + \
           '
Voici l\'heure du serveur :
' + \
           '
{}
'.format(time)

    # pour prévenir qu'il s'agit d'une ressource au format html
    headers = [('Content-Type','text/html;charset=utf-8')]

    # on envoie
    self.send(body,headers)

  #
  # On génère et on renvoie la liste des régions et leur coordonnées (version TD3)
  #
  def send_stations(self):

    conn = sqlite3.connect('Crepes.sqlite')
    c = conn.cursor()

    c.execute("SELECT X,Y,LbStationHydro,CdStationHydroAncienRef FROM 'Stations'")
    r = c.fetchall()
    #On vérifie que des données sur la station sont diponibles dans la table de données
    c.execute("SELECT DISTINCT code_hydro FROM 'hydro_historique'")
    ac = c.fetchall()

    for i in range(len(r)):
        if (r[i][3],) in ac:
            r[i]+=(1,)
          #  print(r[i][3])

        else:
            r[i]+=(0,)

    headers = [('Content-Type','application/json')];
    body = json.dumps([{'nom':n, 'lat':lat, 'lon': lon, 'cd':cd, 'datAv': datAv} for (lon,lat,n,cd,datAv) in r])
    self.send(body,headers)

  def send_courbes(self):

    conn = sqlite3.connect('Crepes.sqlite')
    c = conn.cursor()
    anD=self.path_info[5]
    moisD=self.path_info[6]
    jourD=self.path_info[7]
    anF=self.path_info[8]
    moisF=self.path_info[9]
    jourF=self.path_info[10]
    cdeb=self.path_info[1]
    cmoy=self.path_info[2]
    cvf=self.path_info[3]
    cvF=self.path_info[4]

    fichier1 = 'courbes/'+cdeb+cmoy+cvf+cvF+self.path_info[11]+anD+moisD+jourD+anF+moisF+jourF+'.png'

    #Si le fichier est déjà présent, on ne recrée pas de nouveau graphe

    if os.path.isfile("client/"+fichier1):
        print("Image trouvée")
    else:

        d1="'{}-{}-{}'".format(anD,moisD,jourD)
        d2="'{}-{}-{}'".format(anF,moisF,jourF)

        if len(self.path_info) <= 1 or self.path_info[11] == '' :   # pas de paramètre => station par défaut
            station = [("J1711710","blue")]                         #pas utilisé en pratique parce qu'il s'agit de stations grisées
            self.path_info[11]="inconnu"
        else:
            # On teste que la région demandée existe bien
            c.execute("SELECT code_hydro FROM 'hydro_historique'")
            codes = c.fetchall()
            if (self.path_info[11],) in codes:   # Rq: codes est une liste de tuples
              station = [(self.path_info[11],"blue")]
            else:
                print ('Erreur nom')
                self.send_error(404)    # Station non trouvée -> erreur 404
                return None

        debit=bool(int(cdeb))
        moyenne=bool(int(cmoy))
        valeur_faible=bool(int(cvf))
        valeur_forte=bool(int(cvF))

       # boucle sur les régions
        for l in (station) :
            # configuration du tracé
            fig1 = plt.figure(figsize=(18,6))
            ax = fig1.add_subplot(111)
            #ax.set_ylim(bottom=0,top=10)
            ax.grid(which='major', color='#888888', linestyle='-')
            ax.grid(which='minor',axis='x', color='#888888', linestyle=':')
            #ax.xaxis.set_major_locator(pltd.YearLocator())
            #ax.xaxis.set_minor_locator(pltd.MonthLocator())
            ax.xaxis.set_major_formatter(pltd.DateFormatter('%B %Y'))
            ax.xaxis.set_tick_params(labelsize=10)
            ax.xaxis.set_label_text("Date")
            titre='Données pour la station sélectionnée'
            ax.yaxis.set_label_text('m^3/s')
            plt.title(titre,fontsize=14)

            if debit:

                code='"'+l[0]+'"'
                c.execute("SELECT  debit_donnee_validee_m3,date FROM (SELECT  debit_donnee_validee_m3,date FROM hydro_historique  WHERE code_hydro={}) WHERE date BETWEEN {} and {} ORDER BY date".format(code,d1,d2))  # ou (l[0],)
                r = c.fetchall()
                # recupération de la date (colonne 2) et transformation dans le format de pyplot
                x = [pltd.date2num(dt.date(int(a[1][:4]),int(a[1][5:7]),int(a[1][8:]))) for a in r if not a[0] == '']
                # récupération des débits (colonne 8)
                y = [float(a[0]) for a in r if not a[0] == '']
                # tracé de la courbe
                plt.plot(x,y,linewidth=1, linestyle='-', marker='o', color='blue', label="débit")
                plt.legend(loc='upper right')

            if moyenne:

                code='"'+l[0]+'"'
                c.execute("SELECT moyenne_interannuelle,date FROM (SELECT moyenne_interannuelle,date FROM hydro_historique  WHERE code_hydro={}) WHERE date BETWEEN {} and {} ORDER BY date".format(code,d1,d2))  # ou (l[0],)
                r2 = c.fetchall()
                x2=[pltd.date2num(dt.date(int(a[1][:4]),int(a[1][5:7]),int(a[1][8:]))) for a in r2 if not a[0] == '']
                # récupération des débits (colonne 8)
                z = [float(b[0]) for b in r2 if not b[0] == '']
                # tracé de la courbe

                plt.plot(x2,z,linewidth=1,linestyle='-', color='green', label="moyenne")
                plt.legend(loc='upper right')

            if valeur_faible:

                code='"'+l[0]+'"'
                c.execute("SELECT valeur_faible,date FROM (SELECT valeur_faible,date FROM hydro_historique  WHERE code_hydro={}) WHERE date BETWEEN {} and {} ORDER BY date".format(code,d1,d2))  # ou (l[0],)
                r2 = c.fetchall()
                x2=[pltd.date2num(dt.date(int(a[1][:4]),int(a[1][5:7]),int(a[1][8:]))) for a in r2 if not a[0] == '']
                # récupération des  (colonne 8)
                z = [float(b[0]) for b in r2 if not b[0] == '']
                # tracé de la courbe
                plt.plot(x2,z,linewidth=1,linestyle='-', color='orange', label="valeur faible")
                plt.legend(loc='upper right')

            if valeur_forte:

                code='"'+l[0]+'"'
                c.execute("SELECT valeur_forte,date FROM (SELECT valeur_forte,date FROM hydro_historique  WHERE code_hydro={}) WHERE date BETWEEN {} and {} ORDER BY date".format(code,d1,d2))  # ou (l[0],)
                r2 = c.fetchall()
                x2=[pltd.date2num(dt.date(int(a[1][:4]),int(a[1][5:7]),int(a[1][8:]))) for a in r2 if not a[0] == '']
                # récupération des débits (colonne 8)
                z = [float(b[0]) for b in r2 if not b[0] == '']
                # tracé de la courbe

                plt.plot(x2,z,linewidth=1,linestyle='-', color='red', label="valeur forte")
                plt.legend(loc='upper right')

            # génération de la courbe de débit dans un fichier PNG

            plt.savefig('client/{}'.format(fichier1))
            plt.close()

        #html = ''.format(fichier,self.date_time_string(),self.path)
    body = json.dumps({
            'title': 'courbe'+self.path_info[11], \
            'img': '/'+fichier1 \
             });
    # on envoie
    print("fichier1: "+fichier1)
    headers = [('Content-Type','application/json')];
    self.send(body,headers)
    print("---------------Fin requete----------------")

  def send(self,body,headers=[]):

    # on encode la chaine de caractères à envoyer
    encoded = bytes(body, 'UTF-8')

    # on envoie la ligne de statut
    self.send_response(200)

    # on envoie les lignes d'entête et la ligne vide
    [self.send_header(*t) for t in headers]
    self.send_header('Content-Length',int(len(encoded)))
    self.end_headers()

    # on envoie le corps de la réponse
    self.wfile.write(encoded)

#
# Instanciation et lancement du serveur
#bkjbb
httpd = socketserver.TCPServer(("", 8080), RequestHandler)
httpd.serve_forever()


#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
#
# 28/02/2015 - Aggiunto il parametro di timeOUT

import pprint
import socket
import os

try:
    import httplib2
except ImportError as why:
    print ("----- Loreto IMPORT ERROR ---------")
    print ("-       {0}".format(why))
    print ("-       Il modulo {0} potrebbe non funzionare correttamente!".format(__name__))
    print ("-----------------------------------")
    pass

def getHttpPage(gv, URL, URI='', dataType='TXT', timeOUT=10, removeTAG=False, saveToFile=False, authTYPE=None, SSL=False, console=True):
    logger = gv.LN.logger.setLogger(gv, package=__name__)
    calledBy    = gv.LN.sys.calledBy
    logger.info('entered - [called by:%s]' % (calledBy(1)))
    Indent = ' '*8
    TextExtensions  = ['TXT', 'INI', 'ASC', 'SH', 'HTML']

        # -----------------------------------------------------------------------------
        # - non inseriamo il contenuto della pagina nella struttura
        # - in quanto essendo molto variabile, darebbe errore il printDictionary
        # -----------------------------------------------------------------------------
    gv.HTTP.RESP        = ''                            # Risposta dell'HTTP
    gv.HTTP.PAGE        = 'vedi variabile dedicata'     # contenuto della pagina
    gv.HTTP.ERROR       = False                         # indica se abbiamo avuto un errore
    gv.HTTP.statusMSG   = ''                            # Mio messaggio per chiarire l'errore
    gv.HTTP.statusCODE  = 9999                          # Mio messaggio per chiarire l'errore

    # eliminiamo dupliacted slash
    URI = URI.strip().replace('//', '/')
    if SSL:
        myURL = 'https://' + URL.strip() + URI
        h = httplib2.Http(".cache", disable_ssl_certificate_validation=True)
    else:
        myURL = 'http://' + URL.strip() + URI
        # h = httplib2.Http(".cache", timeout=timeOUT)
        h = httplib2.Http(timeout=timeOUT)                     # Non usare la cache ma usa timeOut

    myURI = '/' + myURL.split('/',3)[3]               # prende solo la URI
    if authTYPE == 'BASIC': h.add_credentials('ciaos', 'xyz') # Basic authentication

    logger.info("dataType: {}".format(dataType))
    logger.info("URL:      {0}".format(myURL))

    if console:
        msg = "\n Renaming remote file...:" if 'wsgi?::action=rename' in myURL else "\n URL...:"
        print(gv.LN.cCYAN + msg)
        # print(Indent + gv.LN.cYELLOW + "{0}".format(myURL), end="") # NO NLine
        print(Indent + gv.LN.cYELLOW + "{0}".format(myURL))

        # ===========================
        # = Connecting
        # ===========================
    contentType = None
    content, respFMTed, ERROR, statusCODE = '', '', True, 9999
    try:
        resp, content   = h.request(myURL, "GET")

        # resp, content   = h.request(myURL, "GET")
        statusCODE      = resp.status
        pp              = pprint.PrettyPrinter(indent=4, width=10)
        respFMTed       = pp.pformat(resp).split('\n')

            # forse non è affidabile
        # if "'content-type': 'text/plain'" in str(resp): contentType = 'text/plain'

            # non tutto viene catturato dal try:
        if str(statusCODE).startswith('2'):
            statusMSG = '{} - OK'.format(statusCODE)
            ERROR = False
            for line in respFMTed: logger.debug(line)

        else:
            statusMSG = '{} - ERROR'.format(statusCODE)
            for line in respFMTed: logger.error(line)

    except socket.gaierror as e:
        statusMSG = ' {}'.format(e)

    except httplib2.ServerNotFoundError as e:
        statusMSG = ' {}'.format(e)

    except ConnectionRefusedError as e:
        statusMSG = ' {}'.format(e)

    except Exception as e:
        statusMSG = ' {}'.format(e)

    if ERROR:
        print()   # per spegnere il precedente end=''
        if statusCODE == 404:
            statusMSG = '   The server has not found anything matching the URI given.'

        logger.error(statusMSG)
        if console: print(Indent + gv.LN.cERROR + statusMSG)

    else:
        logger.info("STATUS-MSG: " + statusMSG)
        if console:
            # print(Indent + gv.LN.cYELLOW + statusMSG)
            print(Indent + '  rCode: [{}]'.format(statusMSG))

            # Se è in formato bytes convertiamola in STRIGN
        # if contentType == 'text/plain':
        #     logger.info("content-type: " + contentType)
        #     content = content.decode(encoding='UTF-8')

        if dataType.strip().upper() in TextExtensions and type(content) == bytes:
            logger.info("content-type: " + dataType)
            content = content.decode(encoding='UTF-8')

        if removeTAG:
            content = gv.LN.net.removeHtmlMarkUp(content)

        if saveToFile:
            if saveToFile == True:
                # destDir     = os.path.dirname(myURI)
                destDir     = '/tmp/getHttpPage'
                fName       = os.path.basename(myURI)
                saveToFile  = os.path.join(destDir, fName)
            else:                           #saveToFile contiene il nome:
                destDir = os.path.dirname(saveToFile)

            if not os.path.isdir(destDir): os.makedirs(destDir)
            errMSG = gv.LN.file.writeFile(gv, saveToFile, content)
            if errMSG:
                ERROR       = True
                statusCODE  = 9999
                statusMSG   = errMSG
                logger.error(errMSG)

    gv.HTTP.RESP        = respFMTed                 # Risposta dell'HTTP (in formato LIST)
    gv.HTTP.ERROR       = ERROR                     # indica se abbiamo avuto un errore
    gv.HTTP.statusMSG   = statusMSG                 # Mio messaggio per chiarire l'errore
    gv.HTTP.statusCODE  = statusCODE                # Mio messaggio per chiarire l'errore

    logger.info('exiting - [called by:{}]'.format(calledBy(1)))
    return gv.HTTP, content

# #################################################################
# #  link:  http://stackoverflow.com/questions/4998629/python-split-string-with-multiple-delimiters
# #  splitString with multiple delimiters
# #  delimiters:    LIST of delimiters
# #################################################################

from   types import *                     # per StringType, etc
def getDelimitedString(string, delimiters, maxsplit=3):
    import re

    regexPattern = '|'.join(map(re.escape, delimiters))

    return re.split(regexPattern, string, maxsplit)

import xml
import xml.etree
import xml.etree.ElementTree as XML

def remove_tags(text):
    ''.join(XML.fromstring(text).itertext())

def remove_html_markup(s):
    tag = False
    quote = False
    out = ""

    for c in s:
            if c == '<' and not quote:
                tag = True
            elif c == '>' and not quote:
                tag = False
            elif (c == '"' or c == "'") and tag:
                quote = not quote
            elif not tag:
                out = out + c

    return out

temp =''
s = ' '
def remove_strings(text):
    global temp
    if text == '':
        return temp

    start   = text.find('<')
    end     = text.find('>')
    if start == -1 and end == -1 :
        temp = temp + text
    return temp

    newstring = text[end+1:]
    fresh_start = newstring.find('<')
    if newstring[:fresh_start] != '':
        temp += s+newstring[:fresh_start]

    remove_strings(newstring[fresh_start:])

    return temp

if __name__ == "__main__":
    import sys
    string= '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\\n\\n<title>404 Not Found</title>\\n\\n
Not Found
\\n
The requested URL /xJBossWEB/management/management was not found on this server.
\\n\\n\''
    xx =  getDelimitedString(string, ['', 'Found
'], maxsplit=0)
    print(xx[1])
    # xx =  remove_tags(string)
    # xx =  remove_strings(string)
    xx =  remove_html_markup(string)
    error, page = getHttpPage('esil904.ac.bankit.it/V621/Machines/SEFALD93/')
    print(xx)
    sys.exit()

    error, page = getHttpPage('www.google.it')
    if not error: print(page)
    print()
    print('..........................')

    error, page = getHttpPage(URL='sefalf97.utenze.bankit.it', URI='/management?recursive=true&include-runtime=true')
    if not error: print(page)
    print()
    print('..........................')

    error, page = getHttpPage(URL='sefaln43.utenze.bankit.it', URI='/xJBossWEB/management/management?operation=resource&include-runtime=true&recursive&json.pretty', authTYPE='BASIC')
    if not error: print(page)
    print('..........................')
    sys.exit()


import requests,bs4

inFile = open("aflplayers.txt","w")
teamsFile = open("playersteams.txt","w")

def get_name(url):
	res=requests.get(url)
	res.raise_for_status()
	soup=bs4.BeautifulSoup(res.text,"lxml")
	for link in soup.findAll('strong'):
		link=str(link)
		x=link.find('itemprop="name"')
		if(x!=-1):
			start = link.find(">",x)
			end = link.find("<",start)
			inFile.write(link[start+1:end] + '\n')
	for link in soup.findAll('em'):
		for y in link:
			teamName=str(y)
		if(teamName.find('#')==-1 and teamName.find("Loading suggestions")==-1):
			teamsFile.write(teamName+'\n')

for x in range(1,89):
	get_name("http://www.aflplayers.com.au/playerpedia/all/page/"+str(x))


#!/usr/bin/env python
# encoding: utf-8
'''
* liangchaob@163.com
* 2015.12.5
'''
#设置中文字符
import sys
sys.path.append("./")
reload(sys)
sys.setdefaultencoding( "utf-8" )

# 引入flask模块
from flask import Flask, render_template, request, redirect

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def hello_world():
    # return 'Hello World!'
    entries = 'hehe'
    return render_template('index2.html', entries=entries)

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


# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QFormLayout, \
    QHBoxLayout, QLineEdit, QMessageBox

class Dia(QDialog):
    def __init__(self, p):
        super(Dia, self).__init__(p)
        v_layout = QVBoxLayout()
        self.setLayout(v_layout)

        f_layout = QFormLayout()
        i_user = QLineEdit()
        i_psw = QLineEdit()
        i_psw.setEchoMode(QLineEdit.Password)
        f_layout.addRow("用户名:", i_user)
        f_layout.addRow("密码:", i_psw)
        self.i_user = i_user
        self.i_psw = i_psw

        h_layout = QHBoxLayout()
        ok_btn = QPushButton("确定")
        ok_btn.clicked.connect(self.verifyInput)
        cancel_btn = QPushButton("取消")
        cancel_btn.clicked.connect(self.reject)
        h_layout.addWidget(ok_btn)
        h_layout.addWidget(cancel_btn)

        v_layout.addLayout(f_layout)
        v_layout.addLayout(h_layout)

    def verifyInput(self):
        user = self.i_user.text()
        psw = self.i_psw.text()
        print(user, psw)
        if user != 'root' or psw != '123456':
            QMessageBox.warning(self, "警告", "用户名密码错误", QMessageBox.Yes)
            self.reject()
        else:
            self.accept()


import tensorflow as tf

a = tf.Variable(2, name='scalar')
b = tf.Variable([3, 5], name='vector')
c = tf.Variable([[1, 7], [8, 8]], name='matrix')
d = tf.Variable(tf.truncated_normal([700, 10]))

e = tf.Variable(10)
e2 = e.assign(2 * e)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    writer = tf.summary.FileWriter('../../graphs/samples_02/variables', sess.graph)
    sess.run(init)

    print(a, b, c)
    print(d.eval())
    print(sess.run(e2))
    writer.close()


# -*- coding: utf-8 -*-
import subprocess
import sys
sys.path.append('/tools/endpoint')
from share.s_tool import *

def reboot_func(session):
    result = {'code':0,'msg':[]}
    subprocess.Popen('reboot' ,stdout=subprocess.PIPE, shell=True)
    result['msg'].append('重启成功')
    return obj2str(result)
if __name__ == '__main__':
    log(LOG_DEBUG,reboot_func(12))