# -*- coding: utf-8 -*-
#
# test_prep.py
#
# This module is part of dstools.
#

"""
Testing of data pre-processing tools.
"""

__author__ = 'Severin E. R. Langberg'
__email__ = 'Langberg91@gmail.no'
__status__ = 'Passed 07.28.2018'

import pytest

import numpy as np
import pandas as pd

from . import mocking

from pytest import approx
from dstools import prep
from pandas.util.testing import assert_frame_equal

class TestFeatureScaling:

    # Arbitrary initiator to random number generator.
    SEED = 123

    THRESH = 1.e-10

    @pytest.fixture
    def data(self):

        np.random.seed(self.SEED)

        num_samples, num_features = 100, 10
        return np.random.random((num_samples, num_features))

    @pytest.fixture
    def scaler(self):

        return prep.FeatureScaling

    def test_default_scaler(self, scaler):

        assert isinstance(scaler(), prep.FeatureScaling)

    def test_custom_scaler(self, scaler):

        assert isinstance(scaler(scaler=mocking.MockScaler), prep.FeatureScaling)

    def test_fit(self, scaler, data):

        scaler().fit(data)

    def test_transform(self, scaler, data):

        scaler = scaler()
        scaler.fit(data)
        scaler.transform(data)

    def test_mean(self, data, scaler):

        scaler = scaler()
        scaler.fit(data)
        data_trans = scaler.transform(data)

        data_avg = np.mean(data_trans, axis=0)
        assert data_avg == approx(np.zeros((data_avg.shape)), rel=self.THRESH)

    def test_std(self, data, scaler):

        scaler = scaler()
        scaler.fit(data)
        data_trans = scaler.transform(data)

        data_std = np.std(data_trans, axis=0)
        assert data_std == approx(np.ones((data_std.shape)), rel=self.THRESH)

    def test_fit_transform(self, data, scaler):

        # Stepwise fitting and tarnsforming of data.
        data_trans = scaler().fit(data).transform(data)
        # Direct fitting and transforming of data.
        data_direct_trans = scaler().fit_transform(data)

        assert data_direct_trans == approx(data_trans, rel=self.THRESH)

class TestTrainTestScaling:

    # Arbitrary initiator to random number generator.
    SEED = 123

    THRESH = 1.e-10

    TEST_SIZE = 0.2

    @pytest.fixture
    def data(self):

        np.random.seed(self.SEED)

        num_samples, num_features = 100, 10
        X = np.random.random((num_samples, num_features))
        y = np.random.random((num_samples, 1))

        return X, y

    def test_mean(self, data):

        # NOTE: Test data is standardized from train data which excludes mean
        # and std test properties.

        X, y = data
        X_train, _, _, _ = prep.train_test_scaling(
            X, y, self.TEST_SIZE, self.SEED
        )
        train_avg = np.mean(X_train, axis=0)
        assert train_avg == approx(np.zeros((train_avg.shape)), rel=self.THRESH)

    def test_std(self, data):

        # NOTE: Test data is standardized from train data which excludes mean
        # and std test properties.

        X, y = data
        X_train, _, _, _ = prep.train_test_scaling(
            X, y, self.TEST_SIZE, self.SEED
        )
        train_std = np.std(X_train, axis=0)
        assert train_std == approx(np.ones((train_std.shape)), rel=self.THRESH)

    def test_split_shapes(self, data):

        X, y = data
        org_num_rows, org_num_cols = X.shape

        X_train, X_test, y_train, y_test = prep.train_test_scaling(
            X, y, test_size=self.TEST_SIZE, random_state=self.SEED
        )
        assert X_train.shape[1] == org_num_cols
        assert X_test.shape[1] == org_num_cols

        joined_X_rows = int(X_train.shape[0]) + int(X_test.shape[0])
        assert joined_X_rows == org_num_rows

        joined_y_rows = int(y_train.shape[0]) + int(y_test.shape[0])
        assert joined_y_rows == org_num_rows

    def test_split_size(self, data):

        X, y = data
        org_num_rows, org_num_cols = X.shape

        X_train, X_test, y_train, y_test = prep.train_test_scaling(
            X, y, test_size=self.TEST_SIZE, random_state=self.SEED
        )
        assert int(X_test.shape[0]) / int(X.shape[0]) == self.TEST_SIZE
        assert int(X_train.shape[0]) / int(X.shape[0]) == 1 - self.TEST_SIZE

class TestClone:

    SEED = 123

    @pytest.fixture
    def data(self):

        np.random.seed(self.SEED)

        num_samples, num_features = 100, 10
        X = np.random.random((num_samples, num_features))

        return X

    @pytest.fixture
    def cloner(self):

        return prep.Clone()

    def test_init(self, cloner):

        assert isinstance(cloner, prep.Clone)

    def test_fit(self, data, cloner):

        cloner.fit(data)

    def test_transform(self, data, cloner):

        cloner.fit(data)
        cloner.transform(data)

    def test_fit_transform(self, data, cloner):

        cloner.fit_transform(data)

    def test_copying(self, data, cloner):

        copy = cloner.fit_transform(data)

        assert np.array_equal(data, copy)
        assert copy is not data

class TestFeatureImputer:

    SEED = 123

    IMPUTE_VAL = np.nan

    @pytest.fixture
    def data(self):

        np.random.seed(self.SEED)

        df = pd.DataFrame(np.random.random((10, 4)))
        df.iloc[3:5, 0] = self.IMPUTE_VAL
        df.iloc[6:8, 2] = self.IMPUTE_VAL
        df.iloc[2:4, 3] = self.IMPUTE_VAL

        return df

    @pytest.fixture
    def imputer(self):

        imputer = prep.FeatureImputer

        return imputer

    def test_default_imputer(self, imputer):

        assert isinstance(imputer(), prep.FeatureImputer)

    def test_custom_imputer(self, imputer):

        _imputer = imputer(imputer=mocking.MockImputer)
        assert isinstance(_imputer, prep.FeatureImputer)

    def test_fit(self, imputer, data):

        _imputer = imputer()
        assert _imputer.targets is None

        _imputer.fit(data)
        assert isinstance(_imputer.targets, list)

    def test_transform(self, imputer, data):

        _imputer = imputer()
        _imputer.fit(data)
        imputed_data = _imputer.transform(data)

        assert np.all(imputed_data.isnull().sum() == 0)

    def test_fit_transform(self, imputer, data):

        imputed_data_direct = imputer().fit_transform(data)

        _imputer = imputer()
        _imputer.fit(data)
        imputed_data = _imputer.transform(data)

        assert np.array_equal(imputed_data_direct, imputed_data)

    def test_impute_strategies(self, imputer, data):

        strategies = ['mean', 'median', 'most_frequent']
        for strategy in strategies:
            _imputer = imputer(method=strategy)
            _imputer.fit(data)
            imputed_data = _imputer.transform(data)

            assert isinstance(imputed_data, pd.DataFrame)

class TestRemoveOutliers:

    SEED = 123

    @pytest.fixture
    def data(self):

        np.random.seed(self.SEED)

        return pd.DataFrame(np.random.random((10, 4)))

    @pytest.fixture
    def filter(self):

        return prep.RemoveOutliers

    def test_init(self, filter):

        assert isinstance(filter(), prep.RemoveOutliers)

    def test_fit(self, data, filter):

        _filter = filter()

        assert _filter.quantiles is None
        _filter.fit(data)
        assert isinstance(_filter.quantiles, pd.DataFrame)

    def test_transform(self, data, filter):

        _filter = filter()

        _filter.fit(data)
        data_clean = _filter.transform(data)

        assert np.any(data_clean.isnull().sum())

    def test_fit_transform(self, data, filter):

        direct_trans = filter().fit_transform(data)

        _filter = filter()
        _filter.fit(data)
        trans = _filter.transform(data)

        assert_frame_equal(direct_trans, trans, check_dtype=True)

class TestFeatureEncoder:

    @pytest.fixture
    def data(self):

        data = pd.DataFrame(
            {
                'patient': [1, 1, 1, 2, 2],
                'treatment': [0, 1, 0, 1, 0],
                'score': ['strong', 'weak', 'normal', 'weak', 'strong']
            },
            columns=['patient', 'treatment', 'score']
        )

        return data

    @pytest.fixture
    def encoder(self):

        return prep.FeatureEncoder

    def test_default_encoder(self, encoder):

        assert isinstance(encoder(), prep.FeatureEncoder)

    def test_custom_encoder(self, encoder):

        _encoder = encoder(encoder=mocking.MockEncoder)
        assert isinstance(_encoder, prep.FeatureEncoder)

    def test_fit(self, data, encoder):

        _encoder = encoder()

        assert _encoder.targets is None
        assert _encoder.target_labels is None

        _encoder.fit(data)

        assert isinstance(_encoder.targets, list)
        assert isinstance(_encoder.target_labels, dict)

        assert len(_encoder.targets) == len(list(_encoder.target_labels.keys()))

    def test_transform(self, data, encoder):

        _encoder = encoder()

        _encoder.fit(data)
        data_trans = _encoder.transform(data)

        assert any(data_trans.dtypes != 'object')

    def test_fit_transform(self, data, encoder):

        direct_trans = encoder().fit_transform(data)

        _encoder = encoder()
        _encoder.fit(data)
        trans = _encoder.transform(data)

        assert_frame_equal(direct_trans, trans, check_dtype=True)

class TestDropFeatures:

    DROP_FEATURES = ['score']

    @pytest.fixture
    def data(self):

        data = pd.DataFrame(
            {
                'patient': [1, 1, 1, 2, 2],
                'treatment': [0, 1, 0, 1, 0],
                'score': ['strong', 'weak', 'normal', 'weak', 'strong']
            },
            columns=['patient', 'treatment', 'score']
        )

        return data

    @pytest.fixture
    def dropper(self):

        return prep.DropFeatures(features=self.DROP_FEATURES)

    def test_init(self, dropper):

        assert isinstance(dropper, prep.DropFeatures)

    def test_fit(self, data, dropper):

        assert dropper.targets is None
        dropper.fit(data)
        assert isinstance(dropper.targets, list)

    def test_transform(self, data, dropper):

        dropper.fit(data)
        trans = dropper.transform(data)

        target_num_features = data.columns.size - len(self.DROP_FEATURES)
        assert trans.columns.size == target_num_features

    def test_fit_transform(self, data, dropper):

        direct_trans = dropper.fit_transform(data)

        dropper.fit(data)
        trans = dropper.transform(data)

        assert_frame_equal(direct_trans, trans, check_dtype=True)


"""
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/

Union Find.

Time complexity: O(E)
"""
class DSU:
    def __init__(self, n):
        self.rank = [0] * n
        self.parent = [x for x in range(n)]

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        px, py = self.find(x), self.find(y)
        if px != py:
            if self.rank[px] > self.rank[py]:
                self.parent[py] = px
            elif self.rank[px] < self.rank[py]:
                self.parent[px] = py
            else:
                self.parent[py] = px
                self.rank[px] += 1

class Solution:
    def countComponents(self, n: int, edges: List[List[int]]) -> int:
        dsu = DSU(n)
        for x, y in edges:
            dsu.union(x, y)
        res = set()
        for i in range(n):
            res.add(dsu.find(i))
        return len(res)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 12 12:31:15 2019

@author: harshwardhan
"""

print("Enter your weight and height in kilo and meter:")
w = float(input())
h = float(input())
x = float(w/h)
t= float(x/h)
print("Your BMI is :",t)


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

import os
import argparse
import pickle
import math
import numpy as np
import copy

def distance(list1, list2):
    assert len(list1) == len(list2)
    dis_sqr = 0
    for d1, d2 in zip(list1, list2):
        dis_sqr += (d1 - d2) * (d1 - d2)
    return math.sqrt(dis_sqr)

def extract_app_feature(op_list, op_centers, max_locality):
    total_time = 0.0
    each_center_time = []

    for i in range(len(op_centers)):
        each_center_time.append(0.0)

    op_perf_dict = {}
    op_center_dict = {}
    for each_op in op_list:
        if (not each_op.is_aid_op) and (each_op.comp_instrs != 0):

            locality = (each_op.mem_trans / each_op.comp_instrs)
            norm_loc = locality / max_locality

            dis_array = [distance([each_op.parallelism, norm_loc], c)
                         for c in op_centers]
            min_i = np.argmin(dis_array)

            op_key_list = copy.deepcopy(sorted(each_op.input_tensor_name))
            op_key_list.extend(sorted(each_op.output_tensor_name))
            op_key = ','.join(op_key_list)
            elapsed_time = float(each_op.elapsed_time)

            if (op_key not in op_perf_dict) or (elapsed_time < op_perf_dict[op_key]):
                op_perf_dict[op_key] = elapsed_time
                op_center_dict[op_key] = min_i

    for op_key in op_perf_dict.keys():
        total_time += op_perf_dict[op_key]
        each_center_time[op_center_dict[op_key]] += op_perf_dict[op_key]

    app_feature = [(t / total_time) for t in each_center_time]
    return app_feature

def merge_two_clusters(label_a, label_b, app_label):
    assert label_a != label_b
    for app_name in app_label.keys():
        if app_label[app_name] == label_a:
            app_label[app_name] = label_b
    return

def update_label_center(app_dict, app_label):
    label_center = {}
    num_points = {}

    for app_name in app_label.keys():
        ndims = len(app_dict[app_name])
        label = app_label[app_name]
        label_center[label] = np.zeros(ndims).tolist()
        num_points[label] = 0

    for app_name in app_label.keys():
        label = app_label[app_name]
        ndims = len(app_dict[app_name])
        for i in range(ndims):
            label_center[label][i] += app_dict[app_name][i]
        num_points[label] += 1

    for label in label_center.keys():
        ndims = len(label_center[label])
        for i in range(ndims):
            label_center[label][i] /= num_points[label]

    return label_center

def comp_cohesion(app_dict, app_label, label_center):
    dis_array = [distance(app_dict[x], label_center[app_label[x]])
                 for x in app_dict.keys()]
    return np.max(dis_array)

def main():

    parser = argparse.ArgumentParser(
        description="cluster applications according to operator cluster results",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('input_dir', help="specify the directory under which "
                        "profiling results are saved")
    parser.add_argument('--op_centers', default="op_centers.txt",
                        help="specify the file of op centers")
    parser.add_argument('--suffix', default=".pickle", help="specify the suffix of "
                        "result files")
    parser.add_argument('--max_locality', default="max_locality.txt",
                        help="specify the value of max locality for normalization")
    parser.add_argument('--output_dir', default=None,
                        help="specify the directory where output results are saved")
    parser.add_argument('--output_file', default="app_cluster.pickle",
                        help="specify the filename of application cluster outputs")

    args = parser.parse_args()

    print('Loading Operator Centers...')
    op_centers = []
    with open(args.op_centers, 'r') as f:
        lines = f.readlines()
        for line in lines:
            center = []
            locs = line.split(',')
            for each_loc in locs:
                each_loc.strip('\n ')
                center.append(float(each_loc))
            op_centers.append(center)

    print('Loading Max Locality...')
    max_locality = 25.0
    with open(args.max_locality, 'r') as f:
        line = f.readline()
        line.strip('\n ')
        max_locality = float(line)

    print('Loading and Extracting Operator Features...')
    app_dict = {}
    for dirpath, dirnames, filenames in os.walk(args.input_dir):
        for input_file in filenames:
            if args.suffix in input_file:
                with open(os.path.join(dirpath, input_file), 'rb') as f:
                    op_list = pickle.load(f)
                index = input_file.find(args.suffix)
                app_name = input_file[:index]
                app_dict[app_name] = extract_app_feature(op_list, op_centers,
                                                         max_locality)

    for app_name in app_dict.keys():
        print('App: {}, feature: {}'.format(app_name, app_dict[app_name]))

    print('Starting App Clustering...')
    app_label = {}
    label_center = {}
    for app_name in app_dict.keys():
        label = len(app_label) + 1
        app_label[app_name] = label
        label_center[label] = app_dict[app_name]

    result_list = []
    iter_num = 0
    tmp_app_label = copy.deepcopy(app_label)
    result_list.append([iter_num, 0.0, tmp_app_label])
    while len(label_center) > 1:
        iter_num += 1

        dis_dict = {}
        for i in label_center.keys():
            for j in label_center.keys():
                if not i == j:
                    dis_dict[(i, j)] = distance(label_center[i], label_center[j])

        label_a, label_b = min(dis_dict.items(), key=lambda x: x[1])[0]

        merge_two_clusters(label_a, label_b, app_label)
        label_center = update_label_center(app_dict, app_label)

        cohesion = comp_cohesion(app_dict, app_label, label_center)
        tmp_app_label = copy.deepcopy(app_label)
        result_list.append([iter_num, cohesion, tmp_app_label])
        print('Iteration: {}, Cohesion: {}'.format(iter_num, cohesion))

    if args.output_dir is None:
        args.output_dir = os.path.join(os.getenv('LOG_OUTPUT_DIR'), 'outputs')

    with open(os.path.join(args.output_dir, args.output_file), 'wb') as f:
        output_tuple = (app_dict, result_list)
        pickle.dump(output_tuple, f)

if __name__ == '__main__':
    main()


import os
from random import randint
from libqtile import widget
from libqtile.config import Key, Match
from libqtile.lazy import lazy

home = os.path.expanduser('~')

#### Groups (Workspace like) ####
group_names = [
    ("WWW", {
        'label': '\uf269',
        'layout': 'monadtall',
        'key':'1'}),
    ("WWW.", {
        'label': '\uf267',
        'layout': 'monadtall',
        'key':'2'}),
    ("DEV", {
        'label': '\uf121',
        'layout': 'monadtall',
        'key':'3',
        'matches': Match(wm_class='code')}),
    ("DEV.", {
        'label': '\uf3b2',
        'layout': 'max',
        'key':'4'}),
    ("TERM", {
        'label': '\uf120',
        'layout': 'bsp',
        'key':'q'}),
    ("DOCS", {
        'label': '\uf07b',
        'layout': 'monadtall',
        'key':'w'}),
    ("ALT", {
        'label': '\uf12e',
        'layout': 'monadtall',
        'key':'e'}),
    ("ALT.", {
        'label': '\uf086',
        'layout': 'monadtall',
        'key':'r',
        'matches': Match(wm_class='discord')})
]

def widgets():
    #### Colors for the widgets ####
    fixed_colors = {
        "panel_bg"		    :["#2a292c", "#2a292c"], 	# panel background
        "panel_group"       :["#695f7c", "#695f7c"],    # panel grouping background
        "current_tab_bg"	:["#ffffff", "#ffffff"], 	# background for current screen tab
        "foreground"		:["#2a292c", "#2a292c"], 	# font color for group names
        "active"		    :["#ffffff", "#ffffff"],	# font color for group names when active (with opened windows)
        "current_border"	:["#a7a7a7", "#a7a7a7"], 	# border line color for current tab (selected group)
        "inactive"          :["#49484b", "#49484b"],    # inactive group color when inactive (no windows opened)
        "screen_border"		:["#ffffff", "#ffffff"], 	# border line color for windows
    }
    rnd_colors = [
        ["#ffa0be", "#ffa0be"],
        ["#fffcc7", "#fffcc7"],
        ["#b6e2d3", "#b6e2d3"],
        ["#fae8e0", "#fae8e0"],
        ["#fbe7c6", "#fbe7c6"],
        ["#f5b2aa", "#f5b2aa"],
        ["#ef7c8e", "#ef7c8e"],
        ["#95c2d4", "#95c2d4"],
        ["#da5699", "#da5699"],
        ["#f8c835", "#f8c835"],
        ["#e34569", "#e34569"],
        ["#babc71", "#babc71"],
    ]
    separator_cfg = {
        'linewidth': 5,
        'padding': 0,
        'foreground': fixed_colors["foreground"],
        'background': fixed_colors["panel_bg"]
    }
    image_sep_open = {
        'margin': 0,
        'background': fixed_colors["panel_bg"],
        'filename': f"{home}/.config/qtile/themes/img/cg_open_group.png",
        'scale': True
    }
    image_sep_close = {
        'margin': 0,
        'background': fixed_colors["panel_bg"],
        'filename': f"{home}/.config/qtile/themes/img/cg_close_group.png",
        'scale': True
    }

    widgets_wanted = {
        'GroupBox': {
            'font': "Font Awesome 5 Free",
            'fontsize': 15,
            'margin_y': 3.5,
            'margin_x': 0,
            'padding_y': 7,
            'padding_x': 5,
            'borderwidth': 5,
            'active': fixed_colors["active"],
            'inactive': fixed_colors["inactive"],
            'rounded': False,
            'highlight_color': fixed_colors["current_tab_bg"],
            'highlight_method': "block",
            'this_current_screen_border': fixed_colors["current_border"],
            'this_screen_border': fixed_colors ["screen_border"],
            'other_current_screen_border': fixed_colors["panel_bg"],
            'other_screen_border': fixed_colors["panel_bg"],
            'foreground': fixed_colors["foreground"],
            'background': fixed_colors["panel_group"]
        },
        'WindowName': {
            'fmt': "{}",
            'font': 'Ubuntu Bold',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 0
        },
        'CheckUpdates': {
            'font': 'Ubuntu Bold',
            'distro': 'Arch_checkupdates',
            'display_format': '{updates}',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1
        },
        'Memory': {
            'font': 'Ubuntu Bold',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1
        },
        'PulseVolume': {
            'font': 'Ubuntu Bold',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1,
        },
        'ThermalSensor': {
            'font': 'Ubuntu Bold',
            'tag_sensors': 'Core 0',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1,
        },
        'CurrentLayout': {
            'font': 'Ubuntu Bold',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1
        },
        'Clock': {
            'font': 'Ubuntu Bold',
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'format': "%d/%m/%Y - %H:%M",
            'padding': 1,
        },
        'Systray': {
            'foreground': rnd_colors[randint(0, len(rnd_colors)-1)],
            'background': fixed_colors["panel_group"],
            'padding': 1,
        },
    }
    widget_list = [
        widget.Sep(**separator_cfg),
        widget.Image(
                margin=0,
                background = fixed_colors["panel_bg"],
                filename = f"{home}/.config/qtile/icons/arch-logo.png",
                scale = True),
        widget.Sep(**separator_cfg),
    ]

    for widget_name in widgets_wanted:
        widget_list.append(widget.Sep(**separator_cfg)),
        widget_list.append(widget.Image(**image_sep_open))
        if widget_name == 'CheckUpdates':
            widget_list.append(widget.TextBox(
                text='↺',
                foreground=fixed_colors['active'],
                font='Ubuntu Mono',
                background=fixed_colors["panel_group"],
                padding=0,
                fontsize=18))
        widget_tmp = getattr(widget, '%s' % widget_name)
        widget_list.append(widget_tmp(**widgets_wanted[widget_name]))
        widget_list.append(widget.Image(**image_sep_close))

    widget_list.append(widget.Sep(**separator_cfg))

    return widget_list

def keys(mod):
    keys = list()

    # Add the key shortcut to switch to any group
    for (name, kwargs) in group_names:
        keys.append(Key([mod], str(kwargs['key']), lazy.group[name].toscreen()))        # Switch to another group
        keys.append(Key([mod, "shift"], str(kwargs['key']), lazy.window.togroup(name))) # Send current window to another group

    return keys

def groups():
    group_names_copy = group_names.copy()
    # Remove the 'key' index from the dict
    # it's not needed to add the group name to the GroupBox
    for (name, kwargs) in group_names_copy:
        del kwargs['key']

    return group_names_copy


import datetime
from typing import Any, Dict, List, Type, TypeVar, Union, cast

import attr
from dateutil.parser import isoparse

from ..models.creation_text_classification_record_explanation import (
    CreationTextClassificationRecordExplanation,
)
from ..models.creation_text_classification_record_inputs import (
    CreationTextClassificationRecordInputs,
)
from ..models.creation_text_classification_record_metadata import (
    CreationTextClassificationRecordMetadata,
)
from ..models.task_status import TaskStatus
from ..models.text_classification_annotation import TextClassificationAnnotation
from ..types import UNSET, Unset

T = TypeVar("T", bound="CreationTextClassificationRecord")

@attr.s(auto_attribs=True)
class CreationTextClassificationRecord:
    """Text classification record

    Attributes:
    -----------

    inputs: Dict[str, Any]
        The input data text

    multi_label: bool
        Enable text classification with multiple predicted/annotated labels.
        Default=False

    explanation: Dict[str, List[TokenAttributions]]
        Token attribution list explaining predicted classes per token input.
        The dictionary key must be aligned with provided record text. Optional"""

    inputs: CreationTextClassificationRecordInputs
    id: Union[Unset, int, str] = UNSET
    metadata: Union[CreationTextClassificationRecordMetadata, Unset] = UNSET
    event_timestamp: Union[Unset, datetime.datetime] = UNSET
    status: Union[Unset, TaskStatus] = UNSET
    prediction: Union[TextClassificationAnnotation, Unset] = UNSET
    annotation: Union[TextClassificationAnnotation, Unset] = UNSET
    multi_label: Union[Unset, bool] = False
    explanation: Union[CreationTextClassificationRecordExplanation, Unset] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        inputs = self.inputs.to_dict()

        id: Union[Unset, int, str]
        if isinstance(self.id, Unset):
            id = UNSET
        else:
            id = self.id

        metadata: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.metadata, Unset):
            metadata = self.metadata.to_dict()

        event_timestamp: Union[Unset, str] = UNSET
        if not isinstance(self.event_timestamp, Unset):
            event_timestamp = self.event_timestamp.isoformat()

        status: Union[Unset, TaskStatus] = UNSET
        if not isinstance(self.status, Unset):
            status = self.status

        prediction: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.prediction, Unset):
            prediction = self.prediction.to_dict()

        annotation: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.annotation, Unset):
            annotation = self.annotation.to_dict()

        multi_label = self.multi_label
        explanation: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.explanation, Unset):
            explanation = self.explanation.to_dict()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "inputs": inputs,
            }
        )
        if id is not UNSET:
            field_dict["id"] = id
        if metadata is not UNSET:
            field_dict["metadata"] = metadata
        if event_timestamp is not UNSET:
            field_dict["event_timestamp"] = event_timestamp
        if status is not UNSET:
            field_dict["status"] = status
        if prediction is not UNSET:
            field_dict["prediction"] = prediction
        if annotation is not UNSET:
            field_dict["annotation"] = annotation
        if multi_label is not UNSET:
            field_dict["multi_label"] = multi_label
        if explanation is not UNSET:
            field_dict["explanation"] = explanation

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        inputs = CreationTextClassificationRecordInputs.from_dict(d.pop("inputs"))

        def _parse_id(data: Any) -> Union[Unset, int, str]:
            data = None if isinstance(data, Unset) else data
            id: Union[Unset, int, str]
            return cast(Union[Unset, int, str], data)

        id = _parse_id(d.pop("id", UNSET))

        metadata: Union[CreationTextClassificationRecordMetadata, Unset] = UNSET
        _metadata = d.pop("metadata", UNSET)
        if not isinstance(_metadata, Unset):
            metadata = CreationTextClassificationRecordMetadata.from_dict(_metadata)

        event_timestamp: Union[Unset, datetime.datetime] = UNSET
        _event_timestamp = d.pop("event_timestamp", UNSET)
        if not isinstance(_event_timestamp, Unset):
            event_timestamp = isoparse(_event_timestamp)

        status: Union[Unset, TaskStatus] = UNSET
        _status = d.pop("status", UNSET)
        if not isinstance(_status, Unset):
            status = TaskStatus(_status)

        prediction: Union[TextClassificationAnnotation, Unset] = UNSET
        _prediction = d.pop("prediction", UNSET)
        if not isinstance(_prediction, Unset):
            prediction = TextClassificationAnnotation.from_dict(_prediction)

        annotation: Union[TextClassificationAnnotation, Unset] = UNSET
        _annotation = d.pop("annotation", UNSET)
        if not isinstance(_annotation, Unset):
            annotation = TextClassificationAnnotation.from_dict(_annotation)

        multi_label = d.pop("multi_label", UNSET)

        explanation: Union[CreationTextClassificationRecordExplanation, Unset] = UNSET
        _explanation = d.pop("explanation", UNSET)
        if not isinstance(_explanation, Unset):
            explanation = CreationTextClassificationRecordExplanation.from_dict(
                _explanation
            )

        creation_text_classification_record = cls(
            inputs=inputs,
            id=id,
            metadata=metadata,
            event_timestamp=event_timestamp,
            status=status,
            prediction=prediction,
            annotation=annotation,
            multi_label=multi_label,
            explanation=explanation,
        )

        creation_text_classification_record.additional_properties = d
        return creation_text_classification_record

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import cm
from scipy.spatial import Voronoi

from points_distributions import points as data_set
from SOM.functions import voronoi_finite_polygons_2d
from SOM.K_means import K_means as KM
from SOM.Kohonen_network import Kohonen_network as KN
from SOM.Neuron_gas import Neuron_gas as NG

def plot(black, redPointsInTime, out, title):
    colors = iter(cm.rainbow(np.linspace(0, 1, len(redPointsInTime))))
    # Set up plot
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    l, = plt.plot([], [], 'r-')
    plt.grid()
    # split black points into x and y
    black_x, black_y = zip(*black)
    ax.scatter(black_x, black_y, color='black', s=7)
    plt.xlim([-10, 10])
    plt.ylim([-10, 10])
    plt.title(title)

    i1 = iter(redPointsInTime)
    i2 = iter(redPointsInTime)
    next(i2)
    # print("redPointsInTime", redPointsInTime)
    for nr, (xy_cur, xy_next) in enumerate(zip(i1, i2)):
        xs_cur, ys_cur = zip(*xy_cur)
        xs_next, ys_next = zip(*xy_next)
        color = next(colors)
        plt.plot([], [], color=color, label="iteracja {}-{}".format(nr, nr + 1))
        plt.scatter(xs_cur, ys_cur, color="red", s=20)
        plt.scatter(xs_next, ys_next, color="red", s=20)
        for x_cur, y_cur, x_next, y_next in zip(xs_cur, ys_cur, xs_next, ys_next):
            dx = x_next - x_cur
            dy = y_next - y_cur
            if(dx != 0 or dy != 0):
                plt.arrow(x_cur, y_cur, dx, dy,
                          color=color, head_width=0.15, head_length=0.25,
                          length_includes_head=True)
    lgd = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
                    fancybox=True, shadow=True, ncol=5)
    plt.savefig(out + ".png", bbox_extra_artists=(lgd,), bbox_inches='tight')

def plotVoronoi(black, redPointsInTime, out, title, koh_line_swt):

    red_at_last = list(redPointsInTime[-1])
    vor = Voronoi(red_at_last)
    # plot
    fig = plt.figure(figsize=(7, 7))
    # https://stackoverflow.com/questions/20515554/colorize-voronoi-diagram
    regions, vertices = voronoi_finite_polygons_2d(vor)
    # colorize
    for region in regions:
        polygon = vertices[region]
        plt.fill(*zip(*polygon), alpha=0.4)
    # plot lines beteeen kononen network
    if koh_line_swt:
        print("koh plt")
        red_iter = iter(red_at_last)
        red_iter_next = iter(red_at_last)
        next(red_iter_next)
        for point, point_next in zip(red_iter, red_iter_next):
            plt.plot([point[0], point_next[0]], [
                     point[1], point_next[1]], 'r--')

    for p in red_at_last:
        plt.scatter(p[0], p[1], color='red', s=10)

    plt.grid()
    # split black points into x and y
    black_x, black_y = zip(*black)
    plt.title(title)
    plt.scatter(black_x, black_y, color='black', s=1)
    plt.xlim([-10, 10])
    plt.ylim([-10, 10])
    plt.savefig(out + ".png")

Neuron_number = 10
som = {"Gas_Neuronowy": NG,
       "Siec_kohonena": KN,
       "k-Srednie": KM}
iter_number = 10
for som_name, SOM_type in som.items():
    for points_name, points in data_set.items():
        map = SOM_type(Neuron_number, points)
        redInTime = []
        for _ in range(iter_number):
            neuron_pos = list(map.getNeurons())
            redInTime.append(neuron_pos)
            map.iter_once()
        koh = False
        if som_name == "Siec_kohonena":
            koh = True
            print("kon " + som_name)
        plotVoronoi(points, redInTime, "./network_evo/" +
                    som_name + "_" + points_name + "_vor", som_name, koh)
        plot(points, redInTime, "./network_evo/" +
             som_name + "_" + points_name + "_in_time", som_name)


import random

lst = [2]

for i in range(1, random.randint(5, 8)):
    lst.append(random.randint(lst[i-1], (lst[i-1]*2)-1))

if len(lst) % 2 == 0:
    med = (lst[(int((len(lst)-1)/2))] + lst[(int((len(lst)-1)/2))+1])/2
else:
    med = lst[(int((len(lst)-1)/2))]

#  I spent several hours on just trying to get the for loop to print in a fstring. I couldn't get it to work.
for i in range(len(lst)):
    print(lst[i], end=" ")
print(f"Sum: {sum(lst)}, Median: {med}, Average: {sum(lst)/len(lst)}")


#A matrix is said to be a singular matrix if its determinant is equal to zero.

#Function to get cofactor of matrix[i][j].
def cofactor(matrix, x, y):
    return [row[: y] + row[y+1:] for row in (matrix[: x] + matrix[x+1:])]

#Recursive function to check if matrix is singular or not.
def check_for_singular(matrix):
    n=len(matrix)
    if (n== 2):
        result = matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]
        return result

    determinant = 0
    for i in range(data):
        val= (-1)**i
        dt = check_for_singular(cofactor(matrix, 0, i))
        determinant += (val*matrix[0][i]*dt)
    return determinant


import string

def main():
    N = int(input())
    alphabet = string.ascii_lowercase[:N]

    height = N * 2 - 1
    width = N * 4 - 3
    lines = [None] * height
    for i in range(N):
        sub_alphabet = alphabet[(-i - 1):]
        letters = ''.join(reversed(sub_alphabet)) + sub_alphabet[1:]
        lines[i] = lines[-i - 1] = '-'.join(letters).center(width, '-')
    print(*lines, sep='\n')

if __name__ == '__main__':
    main()

# import sys
#
# stdin = sys.stdin
#
# n = int(stdin.readline())
# for i in range(2*n-1):
#     d = n-1-abs(n-1-i)
#     for j in range(4*n-3):
#         if j % 2 == 0 and abs((j-(2*n-2))//2) <= d:
#             sys.stdout.write(chr(97+n-1-(d-abs((j-(2*n-2))//2))))
#         else:
#             sys.stdout.write("-")
#     sys.stdout.write("\n")

# def getRangoli(n):
#     r = []
#     letters = [chr(ord('a') + x) for x in range(26)]
#     hold = curr = n - 1
#     for i in range(n):
#         line = ''
#         for j in range(2 * n - 1):
#             if j % 2 == 1:
#                 line += '-'
#             else:
#                 num = (2 * n - 2 - j) // 2 + curr
#                 if num <= hold:
#                     line += letters[num]
#                 else:
#                     line += '-'
#         line = line + line[:-1][::-1]
#         curr -= 1
#         r.append(line)
#     for i in range(n - 2, -1, -1):
#         r.append(r[i][:])
#     return r


from flask_restful import Resource
from flask import request
from models import db, User
import random
import string

class DeleteAccount(Resource):
    def post(self):
        print(request.data)
        json_data = request.get_json(force=True)

        header = request.headers["Authorization"]

        if not header:
            return {"status": 'Failed'}, 403
        else:
            print(header)
            if header == "HPU9mKF2A8J4MwyXqQmiuvdwXaxnFT2ASnOvWiIOrZ9X3qWkrp":
                user = db.session.query(User).filter_by(api_key = json_data["api_key"]).first()
                if user:
                    db.session.delete(user)
                    db.session.commit()
                else:
                    return {"status": 'Failed because no User was found'}, 401

            else:
                return {"status": 'Failed because theres the wrong authentication'}, 402

        return {"status": 'success'}, 201


# -*- coding: utf-8 -*-
import scrapy
import pdb
from transfer_crawl.items import idMapEnglish
from scrapy_redis.spiders import RedisSpider
import traceback
import time, datetime
from compute_algorithm.chinese_2_english import Chinese_2_english

c_2_e = Chinese_2_english()
# scrapy crawl china2code
# class China2codeSpider(scrapy.Spider):
class China2codeSpider(RedisSpider):
    name = 'china2code'
    allowed_domains = ['http://liansai.500.com']
    start_urls = []
    redis_key = 'china2code:start_urls'

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url)

    def parse(self, response):
        trs = response.xpath('//table[@class="lstable1 ljifen_top_list_s jTrHover"]/tr')
        for tr in trs:
            if len(tr.xpath('td')) < 7:
                continue
            tds = tr.xpath('td')
            team_id = tds[1].xpath('a')[0].xpath('@href').extract()[0].split('/')[2]
            team_title = tds[1].xpath('a')[0].xpath('@title').extract()[0]
            english_name = c_2_e.get(team_title)
            single_item = idMapEnglish()
            single_item['team_id'] = team_id
            single_item['english_name'] = english_name
            yield single_item


import urllib.request
import re
req = urllib.request.urlopen('http://www.imooc.com/')
buf = req.read()
webstr = buf.decode('utf-8')
urlList = re.findall(r'(http:[^\s]*?(jpg|png))', str(webstr));
i = 0
filename = "D:\python_practices\picture\\"
for url, po in urlList:
    f = open(filename+str(i)+'.jpg','wb') #注意第二个参数要写成wb，写成w会报错
    req = urllib.request.urlopen(url)
    buf = req.read()
    #bufstr = buf.decode('utf-8','ignore')
    f.write(buf)
    i += 1
    if i == 30:
        break


from django.urls import include, path, re_path

def build_integration_urlpatterns(list_view_cls, config_form_view_cls,
                                  namespace=None):
    """Build URL patterns for integration pages.

    This will produce a set of URL patterns for the integration administration
    pages that take advantage of the specialized views provided. These can
    be used in a :file:`urls.py`, either returned as a ``urlpatterns``
    variable or included using :py:func:`~django.conf.urls.include`.

    Args:
        list_view_cls (type, optional):
            The view responsible for listing available integrations. This
            should be a subclass of
            :py:class:`~djblets.integrations.views.BaseIntegrationListView`.

            If ``None``, the integration list URL will not be provided in the
            URL patterns. Consumers should define their own
            ``integration-list`` URL with the appropriate namespace in order to
            handle any URL reversing that may occur.

        config_form_view_cls (type):
            The view responsible for configuring an integration. This should
            be a subclass of
            :py:class:`~djblets.integrations.views.
            BaseIntegrationConfigFormView`.

            If ``None``, the integration configuration URLs will not be
            provided in the URL patterns. This *will* break the integrations
            list URL unless consumers provide their own
            ``integration-add-config`` and ``integration-change-config`` URL
            registrations.

        namespace (unicode, optional):
            The namespace to use for all URL names.

            For legacy reasons, this is an empty string. If embedding into
            other parts of the app, this should be changed. For instance,
            you may want the admin version to use ``admin``.
    """
    urlpatterns = []

    if list_view_cls is not None:
        urlpatterns.append(
            path('', list_view_cls.as_view(), name='integration-list'))

    if config_form_view_cls is not None:
        urlpatterns.append(
            re_path(r'^(?P<integration_id>[A-Za-z0-9_\.]+)/configs/', include([
                path('add/',
                     config_form_view_cls.as_view(),
                     name='integration-add-config'),
                path('<int:config_id>/',
                     config_form_view_cls.as_view(),
                     name='integration-change-config'),
            ])))

    return [
        path('', include(urlpatterns, namespace=namespace)),
    ]


# -*- coding: utf-8 -*-
"""
Creating Word Clouds for highest scoring job posts
"""

##### PREAMBLE ######

import pandas as pd
import string
from collections import defaultdict
import re

import nltk
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer, SnowballStemmer

import numpy as np
from pprint import pprint

#Load the library with the CountVectorizer method
from sklearn.feature_extraction.text import CountVectorizer

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')

from wordcloud import WordCloud

from yellowbrick.text.freqdist import FreqDistVisualizer

##### Load data #####

### Step 1. read in data
#read in text for job postings
df_text= pd.read_csv('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/applied_models/applied_model_leader_batch_1-3.csv')

#Step 2. simple clean- remove punctuations & count number of words in each review
#first lets get rid of these punctuations
punctuations= ['#', '?', ':', '*', '!', '-', '.', ',', ';', "\\", "@", "&","|",")", "("]
#create a regular expression to get rid of the punctuations
rx= '[' + re.escape(''.join(punctuations)) + ']'

#Convert the titles to lowercase
df_text['jobtext_processed']= df_text['JobText'].map(lambda x: str(x).lower())
#Remove punctuation
df_text['jobtext_processed']= df_text['jobtext_processed'].map(lambda x: re.sub(rx, '', str(x)))

# Remove "job description" and "united states" [[[ADD MORE]]]
df_text['jobtext_processed']= df_text['jobtext_processed'].map(lambda x: x.replace('united states', ' '))
df_text['jobtext_processed']= df_text['jobtext_processed'].map(lambda x: x.replace('job description', ' '))

# Drop "dummy" postings
df_text = df_text[df_text['jobtext_processed'] != 'jtext dummybgt' ]

#Print out the first rows of simple-cleaned reviews
df_text['jobtext_processed'].head()

#Count number of words in jobtext_processed using space as separator
df_text['num_words']= df_text['jobtext_processed'].map(lambda x: len(x.split()))

#Step 3: word tokenization with nltk's word_tokenize function
#first need to remove stopwords
stop_words= set(stopwords.words('english'))

#Tokenize
def preprocess(text):
    '''tokenize and remove stopwords keep only tokens with > 2 characters'''
    result= []
    for token in word_tokenize(text):
        if token not in stop_words and len(token) > 2:
            result.append(token)
    return result

#Run the preprocess function above on review_text_processed1
df_text['jobtext_tokens']= df_text['jobtext_processed'].map(lambda x: preprocess(str(x)))
#Print out the first rows of tokenized reviews
df_text['jobtext_tokens'].head()

#Count number of tokens
df_text['num_tokens']= df_text['jobtext_tokens'].map(lambda x: len(x))

#write df to csv
jobtext_outf= open('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/word_clouds_jobtext.csv', 'w+', encoding= 'utf-8')
df_text.to_csv(jobtext_outf)

#Step 4: visualize the number of words & number of tokens
#freq distribution of word counts in docs
#how big the docs (job texts) are as a whole
jobtext_lens_words= df_text['num_words']
jobtext_lens_tokens= df_text['num_tokens']

#plot 1- number of words
plt.figure(figsize= (10,7), dpi= 160)
plt.hist(jobtext_lens_words, bins= 100, color= 'purple')
plt.text(600, 1150, "Mean   : " + str(round(np.mean(jobtext_lens_words))))
plt.text(600, 1120, "Median : " + str(round(np.median(jobtext_lens_words))))
plt.text(600, 1090, "Stdev   : " + str(round(np.std(jobtext_lens_words))))
plt.text(600, 1060, "1%ile    : " + str(round(np.percentile(jobtext_lens_words, 1))))
plt.text(600, 1030, "99%ile  : " + str(round(np.percentile(jobtext_lens_words, 99))))
plt.text(600, 1000, "Max : " + str(round(np.max(jobtext_lens_words))))
plt.gca().set(xlim= (0, 800), ylabel= 'Number of Job Postings', xlabel= 'Job Posting Word Count')
plt.tick_params(size= 16)
plt.xticks(np.linspace(0, 800, 11))
plt.title('Distribution of Job Posting Word Counts', fontdict= dict(size= 14))
plt.show()
plt.savefig('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/distr_jobtext_words.png')

#plot 2- number of tokens
plt.figure(figsize= (10,7), dpi= 160)
plt.hist(jobtext_lens_tokens, bins= 100, color= 'purple')
plt.text(600, 1150, "Mean   : " + str(round(np.mean(jobtext_lens_tokens))))
plt.text(600, 1120, "Median : " + str(round(np.median(jobtext_lens_tokens))))
plt.text(600, 1090, "Stdev   : " + str(round(np.std(jobtext_lens_tokens))))
plt.text(600, 1060, "1%ile    : " + str(round(np.percentile(jobtext_lens_tokens, 1))))
plt.text(600, 1030, "99%ile  : " + str(round(np.percentile(jobtext_lens_tokens, 99))))
plt.text(600, 1000, "Max : " + str(round(np.max(jobtext_lens_tokens))))
plt.gca().set(xlim= (0, 800), ylabel= 'Number of Job Postings', xlabel= 'Job Posting Token Count')
plt.tick_params(size= 16)
plt.xticks(np.linspace(0, 800, 11))
plt.title('Distribution of Job Posting Token Counts', fontdict= dict(size= 14))
plt.show()
plt.savefig('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/distr_jobtext_tokens.png')

##### Wordcloud ######
lemmatizer = WordNetLemmatizer()

### Predicted top 10 %
criterion = 'leaderComplexity'
cutoff = np.percentile(df_text[criterion], 90)

top10predict_smalldf = df_text[df_text[criterion] >= cutoff ]
top10predict_padded= [' '.join(w) for w in top10predict_smalldf['jobtext_tokens']]
top10predict_temp_string= ''
for t in top10predict_padded:
    top10predict_temp_string += "\n" + t

# LEMMATIZE
top10predict_lem_words = [lemmatizer.lemmatize(x) for x in top10predict_temp_string.split()]

top10predict_long_string =  ''
for t in top10predict_lem_words:
    top10predict_long_string += "\n" + t

#Create a WordCloud object
wcloud= WordCloud(background_color="white", max_words= 5000, contour_width= 3, contour_color='steelblue')
#Generate a word cloud
wcloud.generate(top10predict_long_string)
#Visualize the word cloud
wcloud.to_image()
wcloud.to_file('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/wcloud_top10predict.png')

### Actual Top 10%
criterion = 'leaderComplexityOCC'
cutoff = np.percentile(df_text[criterion], 90)

top10act_smalldf = df_text[df_text[criterion] >= cutoff ]
top10act_padded= [' '.join(w) for w in top10act_smalldf['jobtext_tokens']]
top10act_temp_string= ''
for t in top10act_padded:
    top10act_temp_string += "\n" + t

# LEMMATIZE
top10act_lem_words = [lemmatizer.lemmatize(x) for x in top10act_temp_string.split()]

top10act_long_string =  ''
for t in top10act_lem_words:
    top10act_long_string += "\n" + t

#Create a WordCloud object
wcloud= WordCloud(background_color="white", max_words= 5000, contour_width= 3, contour_color='steelblue')
#Generate a word cloud
wcloud.generate(top10act_long_string)
#Visualize the word cloud
wcloud.to_image()
wcloud.to_file('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/wcloud_top10act.png')

### Predicted Bottom 10 %
criterion = 'leaderComplexity'
cutoff = np.percentile(df_text[criterion], 10)

low10predict_smalldf = df_text[df_text[criterion] < cutoff ]
low10predict_padded= [' '.join(w) for w in low10predict_smalldf['jobtext_tokens']]
low10predict_temp_string= ''
for t in low10predict_padded:
    low10predict_temp_string += "\n" + t

# LEMMATIZE
low10predict_lem_words = [lemmatizer.lemmatize(x) for x in low10predict_temp_string.split()]

low10predict_long_string =  ''
for t in low10predict_lem_words:
    low10predict_long_string += "\n" + t

#Create a WordCloud object
wcloud= WordCloud(background_color="white", max_words= 5000, contour_width= 3, contour_color='steelblue')
#Generate a word cloud
wcloud.generate(low10predict_long_string)
#Visualize the word cloud
wcloud.to_image()
wcloud.to_file('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/wcloud_low10predict.png')

### Actual Bottom 10 %
criterion = 'leaderComplexityOCC'
cutoff = np.percentile(df_text[criterion], 10)

low10act_smalldf = df_text[df_text[criterion] < cutoff ]
low10act_padded= [' '.join(w) for w in low10act_smalldf['jobtext_tokens']]
low10act_temp_string= ''
for t in low10act_padded:
    low10act_temp_string += "\n" + t

# LEMMATIZE
low10act_lem_words = [lemmatizer.lemmatize(x) for x in low10act_temp_string.split()]

low10act_long_string =  ''
for t in low10act_lem_words:
    low10act_long_string += "\n" + t

#Create a WordCloud object
wcloud= WordCloud(background_color="white", max_words= 5000, contour_width= 3, contour_color='steelblue')
#Generate a word cloud
wcloud.generate(low10act_long_string)
#Visualize the word cloud
wcloud.to_image()
wcloud.to_file('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/wcloud_low10act.png')

###  DISTINCT WORDS (difference top to bottom decile) ####

    ### Train VOcabulary on full data set ###

#Initialise the count vectorizer with the English stop words
count_vectorizer= CountVectorizer(stop_words='english')
#Fit and transform the processed job postings
#Fit= Tokenize the collection of reviews and form a vocabulary with it
#Transform= Encode all the job postings based on the vocabulary
#By default CountVectorizer removes punctuation and lowers text

# Lemmatize and combine full data
all_padded= [' '.join(w) for w in df_text['jobtext_tokens']]
all_temp_string= ''
for t in all_padded:
    all_temp_string += "\n"  + t

# LEMMATIZE
lem_words = [lemmatizer.lemmatize(x) for x in all_temp_string.split()]

all_long_string =  ''
for t in lem_words:
    all_long_string += "\n" + t

all_vector= count_vectorizer.fit_transform(lem_words)
#features here is the built vocabulary words in a list

all_features= count_vectorizer.get_feature_names()

    ### Count frequencies in top and bottom postings using full vocabulary ###

# Top 10 predicted
test = [0]
test[0] = top10predict_long_string
top10predict_vector = count_vectorizer.transform(test)
top10predict_mat = top10predict_vector.toarray()

# Bottom 10 predicted
test = [0]
test[0] = low10predict_long_string
low10predict_vector = count_vectorizer.transform(test)
low10predict_mat = low10predict_vector.toarray()

# Top 10 actual
test = [0]
test[0] = top10act_long_string
top10act_vector = count_vectorizer.transform(test)
top10act_mat = top10act_vector.toarray()

# Bottom 10 actual
test = [0]
test[0] = low10act_long_string
low10act_vector = count_vectorizer.transform(test)
low10act_mat = low10act_vector.toarray()

    ### Difference count arrays top vs. bottom PREDICTED and plot ###

top_m_low_predict_mat = top10predict_mat - low10predict_mat
top_m_low_predict_mat = [max(0,x) for x in top_m_low_predict_mat[0]]

count_dict= (zip(all_features, top_m_low_predict_mat))
count_dict= sorted(count_dict, key=lambda x:x[1], reverse=True)[0:20]
words= [w[0] for w in count_dict]
counts= [w[1] for w in count_dict]
x_pos= np.arange(len(words))

plt.figure(2, figsize=(15, 15/1.6180))
plt.subplot(title='20 most distinct words: top vs. bottom predicted score decile')
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sns.barplot(x_pos, counts, palette='husl')
plt.xticks(x_pos, words, rotation=20)
plt.xlabel('words')
plt.ylabel('counts')
plt.savefig('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/top_v_low_pred_freq.png')
plt.show()

    ### Difference count arrays top vs. bottom ACTUAL and plot ###

top_m_low_act_mat = top10act_mat - low10act_mat
top_m_low_act_mat = [max(0,x) for x in top_m_low_act_mat[0]]

count_dict= (zip(all_features, top_m_low_act_mat))
count_dict= sorted(count_dict, key=lambda x:x[1], reverse=True)[0:20]
words= [w[0] for w in count_dict]
counts= [w[1] for w in count_dict]
x_pos= np.arange(len(words))

plt.figure(2, figsize=(15, 15/1.6180))
plt.subplot(title='20 most distinct words: top vs. bottom actual score decile')
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sns.barplot(x_pos, counts, palette='husl')
plt.xticks(x_pos, words, rotation=20)
plt.xlabel('words')
plt.ylabel('counts')
plt.savefig('/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/top_v_low_act_freq.png')
plt.show()

#### UNUSED

#visualize with FreqDistVisualizer specifying for top 10 words
#default is 50 words
visualizer= FreqDistVisualizer(features= features, n= 20, orient= "v")
visualizer.fit(jobtext_vector)
visualizer.poof()
visualizer.show(outpath='/Users/gschubert/Dropbox/PhD Course Notes/6.862 Machine Learning/complexity_project/Project proposal/freq_top20_tokens_A.png')

#Top 10 most frequent words - using sklearn's Count_Vecotrizer function & yellowbrick's FreqDistVisualizer function
#Helper function
def plot_10_most_common_words(count_data, count_vectorizer):
     import matplotlib.pyplot as plt
     words= count_vectorizer.get_feature_names()
     total_counts= np.zeros(len(words))
     for t in count_data:
        total_counts+=t.toarray()[0]

     count_dict= (zip(words, total_counts))
     count_dict= sorted(count_dict, key=lambda x:x[1], reverse=True)[0:10]
     words= [w[0] for w in count_dict]
     counts= [w[1] for w in count_dict]
     x_pos= np.arange(len(words))

     plt.figure(2, figsize=(15, 15/1.6180))
     plt.subplot(title='10 most common words')
     sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
     sns.barplot(x_pos, counts, palette='husl')
     plt.xticks(x_pos, words, rotation=20)
     plt.xlabel('words')
     plt.ylabel('counts')
     plt.show()

# Features
jobtext_corpus= df_text['jobtext_tokens'].map(lambda x: str(x))
jobtext_vector= count_vectorizer.fit_transform(jobtext_corpus)

#features here is the built vocabulary words in a list
features= count_vectorizer.get_feature_names()


ip=input('Введите IP адрес:')
ip_t=ip
ip_l=[]
ip_correct=True
##Если в строке IP нет 3х точек, это не IP адрес.
if ip.count('.') != 3:
	ip_correct=False
##Получаем список чисел из строки IP,попутно проверяя на попадание в необходимый интервал
##А так же проверяем,являются ли октеты числами
else:
	for i in range(0,5):
		try:
			oct=int(ip_t[0:ip_t.find('.')])
			ip_t=ip_t[ip_t.find('.')+1:]
			if oct>255 or oct<0:
				ip_correct=False
			ip_l.append(oct)
		except ValueError:
			ip_correct=False
##Запускаем вывод,в случае если IP соответствует заданным требованиям
if ip_correct:
	if ip_l[0] > 0 and ip_l[0] <= 223:
		print('unicast')
	elif ip_l[0] >= 224 and ip_l[0]<=239:
		print('multicast')
	elif ip == '255.255.255.255':
		print('local broadcast')
	elif ip == '0.0.0.0':
		print('unassigned')
	else:
		print('unused')
else:
	print('Это не IP адрес!')


from markupsafe import Markup
from fanboi2.helpers.formatters import format_markdown
from fanboi2.models import DBSession, Page
from fanboi2.cache import cache_region as cache_region_

def _get_internal_page(slug, cache_region=cache_region_):
    """Returns a content of internal page.

    :param slug: An internal page slug.
    :type slug: String
    :rtype: String or None
    """
    def _creator():
        page = DBSession.query(Page).filter_by(
                namespace='internal',
                slug=slug).\
            first()
        if page:
            return page.body
    return cache_region.get_or_create(
        'partial:%s' % (slug,),
        _creator,
        expiration_time=43200)

def global_css(context, request, cache_region=cache_region_):
    """Returns a string of inline global custom CSS for site-wide CSS override.
    This custom CSS is the content of ``internal:global/css`` page.

    :param context: A :class:`mako.runtime.Context` object.
    :param request: A :class:`pyramid.request.Request` object.
    :param cache_region: Optional cache region to cache this partial.

    :type context: mako.runtime.Context or None
    :type request: pyramid.request.Request
    :type cache_region: dogpile.cache.region.CacheRegion
    :rtype: Markup or None
    """
    page = _get_internal_page('global/css', cache_region)
    if page:
        return Markup(page)

def global_appendix(context, request, cache_region=cache_region_):
    """Returns a HTML of global appendix content. This appendix content is the
    content of ``internal:global/appendix`` page.

    :param context: A :class:`mako.runtime.Context` object.
    :param request: A :class:`pyramid.request.Request` object.
    :param cache_region: Optional cache region to cache this partial.

    :type context: mako.runtime.Context or None
    :type request: pyramid.request.Request
    :type cache_region: dogpile.cache.region.CacheRegion
    :rtype: Markup or None
    """
    page = _get_internal_page('global/appendix', cache_region)
    if page:
        return format_markdown(context, request, page)

def global_footer(context, request, cache_region=cache_region_):
    """Returns a HTML of global footer content. This footer content is the
    content of ``internal:global/footer`` page.

    :param context: A :class:`mako.runtime.Context` object.
    :param request: A :class:`pyramid.request.Request` object.
    :param cache_region: Optional cache region to cache this partial.

    :type context: mako.runtime.Context or None
    :type request: pyramid.request.Request
    :type cache_region: dogpile.cache.region.CacheRegion
    :rtype: Markup or None
    """
    page = _get_internal_page('global/footer', cache_region)
    if page:
        return Markup(page)


# Ref: http://www.voidcn.com/article/p-dqtajpmz-qp.html

class Solution(object):
	def isStrobogrammatic(self, num):

		"""
		:type num: str
		:type: bool

		"""

		print('range(len(num)//2 + 1): ', range(len(num)//2 + 1))

		for i in range(len(num)//2 + 1):

			if num[i] + num[-i - 1] not in "696001188":

				print('num[i]: ', num[i])
				print('num[-i-1]: ', num[-i-1])
				print('num[i] + num[-i - 1]', num[i] + num[-i - 1])
				print('Flase')

				return False
		print('True')
		return True

num = "666999"

strobogrammaticObj = Solution()
strobogrammaticObj.isStrobogrammatic(num)


# -*-coding:utf-8-*-
from multiprocessing import Process
import time

NUM = 99

def task(name):
    print(f'{name} is running')
    time.sleep(1)
    global NUM
    NUM += 1
    print(f'{name} is done, {NUM}')

if __name__ == '__main__':
    p = Process(target=task, args=('zh',)) # create subprocess

    p.start() # send a message to os and prepare subprocess resources
    # p.join()
    print(f'main process {NUM}')


#
# koji hub plugin unit tests
#

import configparser
import json
import os
import sys
import tempfile
import urllib.parse
import uuid
import unittest.mock
from flexmock import flexmock

import koji
import httpretty

from plugintest import PluginTest

API_BASE = "api/composer-koji/v1/"

class MockComposer:
    def __init__(self, url, *, architectures=None):
        self.url = urllib.parse.urljoin(url, API_BASE)
        self.architectures = architectures or ["x86_64"]
        self.composes = {}
        self.errors = []
        self.build_id = 1
        self.status = "success"
        self.routes = {}

    def httpretty_regsiter(self):
        httpretty.register_uri(
            httpretty.POST,
            urllib.parse.urljoin(self.url, "compose"),
            body=self.compose_create
        )

    def next_build_id(self):
        build_id = self.build_id
        self.build_id += 1
        return build_id

    def compose_create(self, request, _uri, response_headers):
        content_type = request.headers.get("Content-Type")
        if content_type != "application/json":
            return [400, response_headers, "Bad Request"]

        js = json.loads(request.body)
        ireqs = js.get("image_requests")
        if not ireqs:
            return [400, response_headers, "Bad Request"]

        for it in ireqs:
            arch = it.get("architecture")
            if arch not in self.architectures:
                return [400, response_headers, "Unsupported Architrecture"]

        compose_id = str(uuid.uuid4())
        build_id = self.next_build_id()
        compose = {
            "id": compose_id,
            "koji_build_id": build_id,
        }

        self.composes[compose_id] = {
            "request": js,
            "result": compose,
            "status": self.status,
            "routes": {
                "logs": 200
            }
        }

        httpretty.register_uri(
            httpretty.GET,
            urllib.parse.urljoin(self.url, "compose/" + compose_id),
            body=self.compose_status
        )

        httpretty.register_uri(
            httpretty.GET,
            urllib.parse.urljoin(self.url, "compose/" + compose_id + "/logs"),
            body=self.compose_logs
        )

        return [201, response_headers, json.dumps(compose)]

    def compose_status(self, _request, uri, response_headers):
        target = os.path.basename(uri)
        compose = self.composes.get(target)
        if not compose:
            return [400, response_headers, f"Unknown compose: {target}"]

        ireqs = compose["request"]["image_requests"]
        result = {
            "status": compose["status"],
            "koji_task_id": compose["request"]["koji"]["task_id"],
            "image_statuses": [
                {"status": compose["status"]} for _ in ireqs
            ]
        }
        return [200, response_headers, json.dumps(result)]

    def compose_logs(self, _request, uri, response_headers):
        route = self.routes.get("logs")
        if route and route["status"] != 200:
            return [route["status"], response_headers, "Internal error"]

        target = os.path.basename(os.path.dirname(uri))
        compose = self.composes.get(target)
        if not compose:
            return [400, response_headers, f"Unknown compose: {target}"]

        ireqs = compose["request"]["image_requests"]
        result = {
            "image_logs": [
                {"osbuild": "log log log"} for _ in ireqs
            ]
        }
        return [200, response_headers, json.dumps(result)]

class UploadTracker:
    """Mock koji file uploading and keep track of uploaded files

    This assumes that `fast_incremental_upload` will be imported
    directly into the plugin namespace.
    """

    def __init__(self):
        self.uploads = {}

    def patch(self, plugin):
        setattr(plugin,
                "fast_incremental_upload",
                self._fast_incremental_upload)

    def _fast_incremental_upload(self, _session, name, fd, path, _tries, _log):
        upload = self.uploads.get(name, {"path": path})
        fd.seek(0, os.SEEK_END)
        upload["pos"] = fd.tell()
        self.uploads[name] = upload

    def assert_upload(self, name):
        if name not in self.uploads:
            raise AssertionError(f"Upload {name} missing")

class MockHost:
    """Mock for the HostExport koji class

    HostExport has the builder specific XML-RPC methods. This mocks a
    small subset of it. Currently the methods to support tagging a
    build are supported.
    The `tags` property, a mapping from build it to a list of tag ids,
    can be used see what tags were applied to a build id.
    """
    def __init__(self):
        self.tasks = {}
        self.waitset = {}
        self.count = 0
        self.tags = {}

    def subtask(self, method, arglist, parent, **opts):
        if method != "tagBuild":
            raise ValueError(f"{method} not mocked")

        task = {
            "method": method,
            "parent": parent,
            "arglist": arglist,
            "opts": opts,
            "result": True
        }

        self._tag_build(task)

        self.count += 1
        task_id = self.count
        self.tasks[task_id] = task

    def taskSetWait(self, parent, tasks):
        if tasks is None:
            tasks = [k for k, v in self.tasks.items() if v["parent"] == parent]
        self.waitset[parent] = tasks

    def taskWait(self, parent):
        tasks = self.waitset[parent]
        return tasks, []

    def taskWaitResults(self, parent, tasks, canfail=None):
        if canfail is None:
            canfail = []
        waitset = self.waitset[parent]
        selected = [t for t in waitset if t in tasks]
        res = {t: self.tasks[t]["result"] for t in selected}
        return res

    def _tag_build(self, task):
        assert task["parent"], "tagBuild: need parent"
        args = task["arglist"]
        assert 2 < len(args) < 6, "tagBuild: wrong argument number"
        tag = args[0]
        build = args[1]
        assert isinstance(tag, int), "tagBuild: tag id not int"
        assert isinstance(build, int), "tagBuild: build id not int"

        tags = self.tags.get(build, [])
        tags += [tag]
        self.tags[build] = tags

@PluginTest.load_plugin("builder")
class TestBuilderPlugin(PluginTest):

    def setUp(self):
        super().setUp()
        self.uploads = UploadTracker()
        self.uploads.patch(self.plugin)

    @staticmethod
    def mock_session():

        host = MockHost()
        session = flexmock(host=host)

        build_target = {
            "build_tag": 23,
            "build_tag_name": "fedora-build",
            "dest_tag": 42,
            "dest_tag_name": "fedora-dest"
        }

        tag_info = {
            "id": build_target["dest_tag"],
            "name": build_target["dest_tag_name"],
            "locked": False
        }

        build_config = {
            "arches": "s390x aarch64 ppc64le x86_64"
        }

        repo_info = {
            "id": 20201015,
            "tag": build_target["build_tag_name"],
            "tag_id": build_target["build_tag"],
            "event_id": 2121,
        }

        session.should_receive("getBuildTarget") \
               .with_args("fedora-candidate", strict=True) \
               .and_return(build_target)

        session.should_receive('getBuildConfig') \
               .with_args(build_target["build_tag"]) \
               .and_return(build_config)

        session.should_receive("getTag") \
               .with_args(build_target["build_tag"], strict=True) \
               .and_return(tag_info)

        session.should_receive('getRepo') \
               .with_args(build_target["build_tag"]) \
               .and_return(repo_info)

        session.should_receive('getNextRelease') \
               .with_args(dict) \
               .and_return("20201015")

        return session

    @staticmethod
    def mock_options():
        options = flexmock(
            allowed_scms='pkg.osbuild.org:/*:no',
            workdir="/tmp",
            topurl="http://localhost/kojifiles"
        )
        return options

    def make_handler(self, *, config=None, session=None, options=None):
        if not session:
            session = self.mock_session()

        if not options:
            options = self.mock_options()

        def creator():
            return self.plugin.OSBuildImage(1,
                                            "osbuildImage",
                                            "params",
                                            session,
                                            options)

        if not config:
            return creator()

        with tempfile.TemporaryDirectory() as tmp:
            cfgfile = os.path.abspath(os.path.join(tmp, "ko.cfg"))
            with open(cfgfile, 'w') as f:
                config.write(f)

            self.plugin.DEFAULT_CONFIG_FILES = [cfgfile]
            return creator()

    def test_plugin_config(self):

        composer_url = "https://image-builder.osbuild.org:2323"
        koji_url = "https://koji.osbuild.org/kojihub"
        certs = ["crt", "key"]
        ssl_cert = ", ".join(certs)
        ssl_verify = False

        cfg = configparser.ConfigParser()
        cfg["composer"] = {
            "server": composer_url,
            "ssl_cert": ssl_cert,
            "ssl_verify": ssl_verify
        }
        cfg["koji"] = {
            "server": koji_url
        }

        handler = self.make_handler(config=cfg)

        self.assertEqual(handler.composer_url, composer_url)
        self.assertEqual(handler.koji_url, koji_url)
        session = handler.client.http
        self.assertEqual(session.cert, certs)
        self.assertEqual(session.verify, ssl_verify)

        # check we can handle a path in ssl_verify
        ssl_verify = "/a/path/to/a/ca"
        cfg["composer"]["ssl_verify"] = ssl_verify

        handler = self.make_handler(config=cfg)

        session = handler.client.http
        self.assertEqual(session.verify, ssl_verify)

        # check we can handle a plain ssl_cert string
        ssl_cert = "/a/path/to/a/cert"
        cfg["composer"]["ssl_cert"] = ssl_cert

        handler = self.make_handler(config=cfg)

        session = handler.client.http
        self.assertEqual(session.cert, ssl_cert)

        # check we handle detect wrong cert configs, i.e.
        # three certificate components
        cfg["composer"]["ssl_cert"] = "1, 2, 3"

        with self.assertRaises(ValueError):
            self.make_handler(config=cfg)

    def test_unknown_build_target(self):
        session = flexmock()

        session.should_receive("getBuildTarget") \
            .with_args("target", strict=True) \
            .and_return(None)

        options = flexmock(allowed_scms='pkg.osbuild.org:/*:no',
                           workdir="/tmp")
        handler = self.plugin.OSBuildImage(1,
                                           "osbuildImage",
                                           "params",
                                           session,
                                           options)

        args = ["name", "version", "distro",
                ["image_type"],
                "target",
                ["arches"],
                {}]

        with self.assertRaises(koji.BuildError):
            handler.handler(*args)

    def test_unsupported_architecture(self):
        session = flexmock()

        build_target = {
            "build_tag": "fedora-build",
            "name": "fedora-candidate",
            "dest_tag_name": "fedora-updates"
        }

        session.should_receive("getBuildTarget") \
               .with_args("fedora-candidate", strict=True) \
               .and_return(build_target) \
               .once()

        session.should_receive('getBuildConfig') \
               .with_args(build_target["build_tag"]) \
               .and_return({"arches": "x86_64"})

        options = flexmock(allowed_scms='pkg.osbuild.org:/*:no',
                           workdir="/tmp")

        handler = self.plugin.OSBuildImage(1,
                                           "osbuildImage",
                                           "params",
                                           session,
                                           options)

        args = ["name", "version", "distro",
                ["image_type"],
                "fedora-candidate",
                ["s390x"],
                {}]

        with self.assertRaises(koji.BuildError) as err:
            handler.handler(*args)
            self.assertTrue(str(err).startswith("Unsupported"))

    @httpretty.activate
    def test_bad_request(self):
        # Simulate a bad request by asking for an unsupported architecture
        handler = self.make_handler()

        args = ["name", "version", "distro",
                ["image_type"],
                "fedora-candidate",
                ["x86_64"],
                {}]

        url = self.plugin.DEFAULT_COMPOSER_URL
        composer = MockComposer(url, architectures=["s390x"])
        composer.httpretty_regsiter()

        with self.assertRaises(koji.GenericError):
            handler.handler(*args)

        self.uploads.assert_upload("compose-request.json")

    @httpretty.activate
    def test_compose_success(self):
        # Simulate a successful compose, check return value
        session = self.mock_session()
        handler = self.make_handler(session=session)

        arches = ["x86_64", "s390x"]
        repos = ["http://1.repo", "https://2.repo"]
        args = ["name", "version", "distro",
                ["image_type"],
                "fedora-candidate",
                arches,
                {"repo": repos}]

        url = self.plugin.DEFAULT_COMPOSER_URL
        composer = MockComposer(url, architectures=arches)
        composer.httpretty_regsiter()

        res = handler.handler(*args)
        assert res, "invalid compose result"
        compose_id = res["composer"]["id"]
        compose = composer.composes.get(compose_id)
        self.assertIsNotNone(compose)

        ireqs = compose["request"]["image_requests"]

        # Check we got all the requested architectures
        ireq_arches = [i["architecture"] for i in ireqs]
        diff = set(arches) ^ set(ireq_arches)
        self.assertEqual(diff, set())

        for ir in ireqs:
            have = [r["baseurl"] for r in ir["repositories"]]
            self.assertEqual(have, repos)

        # check uploads: logs, compose request
        for arch in arches:
            self.uploads.assert_upload(f"{arch}-image_type.log.json")
        self.uploads.assert_upload("compose-request.json")
        self.uploads.assert_upload("compose-status.json")

        build_id = res["koji"]["build"]
        # build should have been tagged
        self.assertIn(build_id, session.host.tags)

    @httpretty.activate
    def test_compose_failure(self):
        # Simulate a failed compose, check exception is raised
        session = self.mock_session()
        handler = self.make_handler(session=session)

        args = ["name", "version", "distro",
                ["image_type"],
                "fedora-candidate",
                ["x86_64"],
                {}]

        url = self.plugin.DEFAULT_COMPOSER_URL
        composer = MockComposer(url, architectures=["x86_64"])
        composer.httpretty_regsiter()

        composer.status = "failure"

        with self.assertRaises(koji.BuildError):
            handler.handler(*args)

        self.uploads.assert_upload("compose-request.json")
        self.uploads.assert_upload("x86_64-image_type.log.json")
        self.uploads.assert_upload("compose-status.json")
        # build must not have been tagged
        self.assertEqual(len(session.host.tags), 0)

    @httpretty.activate
    def test_compose_no_logs(self):
        # Simulate fetching the logs fails, a non-fatal issue
        session = self.mock_session()
        handler = self.make_handler(session=session)

        url = self.plugin.DEFAULT_COMPOSER_URL
        composer = MockComposer(url)
        composer.httpretty_regsiter()

        args = ["name", "version", "distro",
                ["image_type"],
                "fedora-candidate",
                composer.architectures,
                {}]

        composer.routes["logs"] = {
            "status": 500
        }

        res = handler.handler(*args)
        assert res, "invalid compose result"

        self.uploads.assert_upload("compose-request.json")
        with self.assertRaises(AssertionError):
            self.uploads.assert_upload("x86_64-image_type.log.json")

    @httpretty.activate
    def test_cli_compose_success(self):
        # Check the basic usage of the plugin as a stand-alone client
        # for the osbuild-composer API
        url = self.plugin.DEFAULT_COMPOSER_URL
        composer = MockComposer(url, architectures=["x86_64"])
        composer.httpretty_regsiter()

        certs = [
            "test/data/example-crt.pem",
            "test/data/example-key.pem"
        ]

        args = [
            "plugins/builder/osbuild.py",
            "compose",
            "Fedora-Cloud-Image",
            "32",
            "20201015.0",
            "fedora-32",
            "http://download.localhost/pub/linux/$arch",
            "x86_64",
            "--cert", ", ".join(certs),
            "--ca", "test/data/example-ca.pem"
        ]

        with unittest.mock.patch.object(sys, 'argv', args):
            res = self.plugin.main()
            self.assertEqual(res, 0)


import sys, pygame, math, argparse

import matrix_ops, obj_file, draw3d_ops

parser = argparse.ArgumentParser()
parser.add_argument("filename", help="path to .obj or .dat (Bezier data) file")
args = parser.parse_args()

#####

pygame.init()

#####

size = width, height = 1000, 800
o_x = width/2
o_y = height/2

# Define the colors we will use in RGB format
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE =  (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =   (255,   0,   0)

screen = pygame.display.set_mode(size)

####

pygame.display.set_caption("File: %s" % args.filename)

originalObj = obj_file.loadFile(args.filename)

#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()

sc = 0.3
scaleM = matrix_ops.getScaleMatrix(sc, sc, sc)

tx = 0.0
ty = 0.0
tz = 0.0
translM = matrix_ops.getTranslationMatrix(tx, ty, tz)

rx = 0.0
rotxM = matrix_ops.getRotateXMatrix(rx)

ry = 0.0
rotyM = matrix_ops.getRotateYMatrix(ry)

rz = 0.0
rotzM = matrix_ops.getRotateZMatrix(rz)

drot = math.pi / 180.0 * 5

fontSize = 20
font = pygame.font.Font(None, fontSize)

while not done:
        # This limits the while loop to a max of 10 times per second.
        # Leave this out and we will use all CPU we can.
        clock.tick(10)

        viewChanged = False
        for event in pygame.event.get(): # User did something
                if event.type == pygame.QUIT: # If user clicked close
                        done=True # Flag that we are done so we exit this loop
                elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_LEFT:
                                viewChanged = True
                                tx += -1.0
                        elif event.key == pygame.K_RIGHT:
                                viewChanged = True
                                tx += 1.0

                        elif event.key == pygame.K_UP:
                                viewChanged = True
                                ty += 1.0
                        elif event.key == pygame.K_DOWN:
                                viewChanged = True
                                ty += -1.0

                        elif event.key == pygame.K_q:
                                viewChanged = True
                                tz += 1.0
                        elif event.key == pygame.K_a:
                                viewChanged = True
                                tz += -1.0

                        elif event.key == pygame.K_w:
                                viewChanged = True
                                sc += 0.1
                        elif event.key == pygame.K_s:
                                if sc >= 0.2:
                                        viewChanged = True
                                        sc += -0.1

                        elif event.key == pygame.K_e:
                                viewChanged = True
                                rx += drot
                        elif event.key == pygame.K_d:
                                viewChanged = True
                                rx += -drot

                        elif event.key == pygame.K_r:
                                viewChanged = True
                                ry += drot
                        elif event.key == pygame.K_f:
                                viewChanged = True
                                ry += -drot

                        elif event.key == pygame.K_t:
                                viewChanged = True
                                rz += drot
                        elif event.key == pygame.K_g:
                                viewChanged = True
                                rz += -drot

        if viewChanged:
                scaleM = matrix_ops.getScaleMatrix(sc, sc, sc)
                translM = matrix_ops.getTranslationMatrix(tx, ty, tz)
                rotxM = matrix_ops.getRotateXMatrix(rx)
                rotyM = matrix_ops.getRotateYMatrix(ry)
                rotzM = matrix_ops.getRotateZMatrix(rz)

        screen.fill(BLACK)

        legend = "T: %.1f, %.1f, %.1f" % (tx, ty, tz)
        text = font.render(legend, True, (255,255,255))
        screen.blit(text, (0,0))

        legend = "S: %.1f" % (sc)
        text = font.render(legend, True, (255,255,255))
        screen.blit(text, (0,fontSize))

        oneDeg = math.pi / 180.0
        legend = "R: %.1f, %.1f, %.1f" % (rx / oneDeg, ry / oneDeg, rz / oneDeg)
        text = font.render(legend, True, (255,255,255))
        screen.blit(text, (0,fontSize * 2))

        pygame.draw.line(screen, RED, (width/2, 0), (width/2, height), 1)
        pygame.draw.line(screen, RED, (0, height/2), (width, height/2), 1)

        transformedObj = originalObj

        transformedObj = obj_file.applyTransform(transformedObj, scaleM)

        transformedObj = obj_file.applyTransform(transformedObj, rotxM)
        transformedObj = obj_file.applyTransform(transformedObj, rotyM)
        transformedObj = obj_file.applyTransform(transformedObj, rotzM)

        transformedObj = obj_file.applyTransform(transformedObj, translM)

        obj_file.drawObj(screen, o_x, o_y, transformedObj)

        pygame.display.flip()


# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 11:11:51 2015

@author: Richard
"""

import itertools

from cls_integer_base_fixed import IntegerBaseFixed
from config import BASE

class ProductFixed(object):
    ''' The product of two IntegerBasedFixed classes
    '''

    def __init__(self, p=0, q=0):
        if not isinstance(p, IntegerBaseFixed):
            p = IntegerBaseFixed(p, 1, 1)
        if not isinstance(q, IntegerBaseFixed):
            q = IntegerBaseFixed(q, 1, 1)

        if p.num_digits != q.num_digits:
            raise ValueError('Invalid inputs: {} and {}'.format(p, q))

        self._p = p
        self._q = q

        self._as_int = None
        self._as_integer_base = None

    @property
    def p(self):
        return self._p

    @property
    def q(self):
        return self._q

    @property
    def as_int(self):
        if self._as_int is None:
            self._as_int = self.p.as_int * self.q.as_int
        return self._as_int

    @property
    def as_integer_base(self):
        if self._as_integer_base is None:
            self._as_integer_base = IntegerBaseFixed(self.as_int, None, None)
        return self._as_integer_base

    def __repr__(self):
        factor_str = 'x'.join([self.p.__repr__(), self.q.__repr__()])
        prod_str = self.as_integer_base.__repr__()
        #return prod_str
        return '='.join([factor_str, prod_str])

    def copy(self):
        return ProductFixed(self.p.copy(), self.q.copy())

    def _generate_children(self, side='l'):

        if self.p.num_fixed_digits_left + self.p.num_fixed_digits_right == self.p.num_digits:
            return []
        if self.q.num_fixed_digits_left + self.q.num_fixed_digits_right == self.q.num_digits:
            return []

        #import warnings
        #warnings.warn('Duplicating children')
        if self.p.digits == self.q.digits:
            digits_to_add = list(itertools.combinations_with_replacement(xrange(BASE), 2))
        else:
            digits_to_add = list(itertools.product(xrange(BASE), repeat=2))

        p_digits = self.p.digits
        q_digits = self.q.digits

        children = []
        for dta in digits_to_add:
            new_p_digits = p_digits[:]

            if side == 'l':
                p_ind = self.p.num_fixed_digits_left
                q_ind = self.q.num_fixed_digits_left

                p_f_l = self.p.num_fixed_digits_left + 1
                p_f_r = self.p.num_fixed_digits_right
                q_f_l = self.q.num_fixed_digits_left + 1
                q_f_r = self.q.num_fixed_digits_right
            elif side == 'r':
                p_ind = - self.p.num_fixed_digits_right - 1
                q_ind = - self.q.num_fixed_digits_right - 1

                p_f_l = self.p.num_fixed_digits_left
                p_f_r = self.p.num_fixed_digits_right + 1
                q_f_l = self.q.num_fixed_digits_left
                q_f_r = self.q.num_fixed_digits_right + 1
            else:
                raise ValueError(side)

            new_p_digits[p_ind] = dta[0]
            new_p = IntegerBaseFixed(new_p_digits, p_f_l, p_f_r)

            new_q_digits = q_digits[:]
            new_q_digits[q_ind] = dta[1]
            new_q = IntegerBaseFixed(new_q_digits, q_f_l, q_f_r)

            child = ProductFixed(new_p, new_q)

            # Old code
            #child = self.copy()
            #child.p._digits.append(dta[0])
            #child.q._digits.append(dta[1])
            children.append(child)
        return children

    def generate_children_left(self):
        assert self.p.num_fixed_digits_left == self.q.num_fixed_digits_left
        return self._generate_children('l')

    def generate_children_right(self):
        assert self.p.num_fixed_digits_right == self.q.num_fixed_digits_right
        return self._generate_children('r')

    @property
    def num_digits(self):
        return self.as_integer_base.num_digits

    @property
    def factor_num_digits(self):
        assert self.p.num_digits == self.q.num_digits
        return self.p.num_digits

    @property
    def num_fixed_digits(self):
        return self.num_fixed_digits_left + self.num_fixed_digits_right

    @property
    def num_fixed_digits_left(self):
        assert self.p.num_fixed_digits_left == self.q.num_fixed_digits_left
        return self.p.num_fixed_digits_left

    @property
    def num_fixed_digits_right(self):
        assert self.p.num_fixed_digits_right == self.q.num_fixed_digits_right
        return self.p.num_fixed_digits_right


import sys
import Cluster

#sys.argv to access the args from command line
#file name, dimension, size, cluster num, iterator

if len(sys.argv) < 6:
	print('input argument as below:')
	print('fileName, dimension, size, cluster number, iterator')
	exit(1)

run_times = 30
fileName = sys.argv[1]
dimension = int(sys.argv[2])
size = int(sys.argv[3])
num_cluster = int(sys.argv[4])
iterator = int(sys.argv[5])
dataSet = []
fp = open(fileName, 'r')

#iris data preprocess
for lines in fp:
	line = lines.split(',')
	line.pop()
	dataSet.append(line)

#run 30 times
#begin by random assign
sse_km = []
km = Cluster.KMeans(dataSet, dimension, size, num_cluster)
for i in range(run_times):
	km.run(iterator)
	sse_km.append( km.get_sse() )
	print(km.get_cluster())

#begin by clustering with definer
#define an interval [a, b, c, ...]
# x < a : x = 0,
# a < x < b : x = 1, ...
definerSet = [[5, 6],[2.8, 3.5],[2, 5],[1, 1.5]]
sse_definer = []
km_definer = Cluster.KMeans(dataSet, dimension, size, num_cluster)
for i in range(run_times):
	km_definer.run(iterator, definerSet)
	sse_definer.append( km_definer.get_sse() )
	print(km_definer.get_cluster())

#bad definer
bad_definerSet = [[1, 8],[2, 3],[4, 5],[1, 2]]
sse_bad_definer = []
km_bad_definer = Cluster.KMeans(dataSet, dimension, size, num_cluster)
for i in range(run_times):
	km_bad_definer.run(iterator, bad_definerSet)
	sse_bad_definer.append( km_bad_definer.get_sse() )
	print(km_bad_definer.get_cluster())

for i in range(run_times):
	print(sse_km[i])

for i in range(run_times):
	print(sse_definer[i])

for i in range(run_times):
	print(sse_bad_definer[i])


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.font_manager import *
import matplotlib
myfont = FontProperties(fname='/usr/share/fonts/truetype/arphic/ukai.ttc')
matplotlib.rcParams['axes.unicode_minus'] = False
matplotlib.rcParams['axes.unicode_minus'] = False

show_number=3
pf = pd.read_csv("ls_results.csv")
data_a = pf.values.T.tolist()
data_b = pf.values.T.tolist()
data_c = pf.values.T.tolist()
ticks = pf.columns.tolist()

def set_box_color(bp, color):
    plt.setp(bp['boxes'], color=color)
    plt.setp(bp['whiskers'], color=color)
    plt.setp(bp['caps'], color=color)
    plt.setp(bp['medians'], color=color)

plt.figure()

bpl = plt.boxplot(data_a, positions=np.array(range(len(data_a)))*show_number-0.2*show_number, sym='', widths=0.6)
bpr = plt.boxplot(data_b, positions=np.array(range(len(data_b)))*show_number, sym='', widths=0.6)
bpc = plt.boxplot(data_c, positions=np.array(range(len(data_c)))*show_number+0.2*show_number, sym='', widths=0.6)
set_box_color(bpl, 'r') # colors are from http://colorbrewer2.org/
set_box_color(bpr, 'b')
set_box_color(bpc, 'g')
# draw temporary red and blue lines and use them to create a legend
plt.plot([], c='r', label=u'ls算法')
plt.plot([], c='b', label=u'wls算法')
plt.plot([], c='g', label=u'cnn算法')
plt.legend(prop =myfont)

plt.xticks(range(0, len(ticks) * show_number, show_number), ticks)
plt.xlim(-show_number, len(ticks)*show_number)
plt.yticks([0.05 * temp for temp in range(10)])
plt.tight_layout()
plt.xlabel(u'高斯噪声的方差值', fontproperties=myfont)
plt.ylabel(u'MAPE值(x100%)', fontproperties=myfont)
plt.grid(True)
plt.savefig("results/boxcompare.png",dpi=300)
plt.show()