import cv2

yuz_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
goz_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
agiz_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_smile.xml')

def detection(grayscale, img):
    yuz = yuz_cascade.detectMultiScale(grayscale, 1.3, 5)
    for (x_yuz, y_yuz, w_yuz, h_yuz) in yuz:
        cv2.rectangle(img, (x_yuz, y_yuz), (x_yuz+w_yuz, y_yuz+h_yuz), (255, 130, 0), 2)
        ri_grayscale = grayscale[y_yuz:y_yuz+h_yuz, x_yuz:x_yuz+w_yuz]
        ri_color = img[y_yuz:y_yuz+h_yuz, x_yuz:x_yuz+w_yuz]
        cv2.putText(img, 'yuz', (x_yuz, y_yuz-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 130, 0), 2)
        goz = goz_cascade.detectMultiScale(ri_grayscale, 1.2, 18)
        for (x_goz, y_goz, w_goz, h_goz) in goz:
            cv2.rectangle(ri_color,(x_goz, y_goz),(x_goz+w_goz, y_goz+h_goz), (0, 180, 60), 2)
            cv2.putText(ri_color, 'goz', (x_goz, y_goz-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 180, 60), 2)
        agiz = agiz_cascade.detectMultiScale(ri_grayscale, 1.7, 20)
        for (x_agiz, y_agiz, w_agiz, h_agiz) in agiz:
            cv2.rectangle(ri_color,(x_agiz, y_agiz),(x_agiz+w_agiz, y_agiz+h_agiz), (255, 0, 130), 2)
            cv2.putText(ri_color, 'agiz', (x_agiz, y_agiz-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 130), 2)
    return img

vc = cv2.VideoCapture(0)

while True:
    _,img = vc.read()
    grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    final = detection(grayscale, img)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img,'TEZCAN BABA',(10,450), font, 1,(255,255,255),2)
    cv2.imshow('Video', final)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

vc.release()
cv2.destroyAllWindows()


import numpy as np, os, cv2, csv, pandas as pd
from extract_face_landmarks import extract_fl as efl

subjects = (pd.read_csv('dataset/list_emo.csv'))['names']

os.system('rm -rf lm_dataset/*')

csv.register_dialect('md', delimiter=',', lineterminator='\n')
headers = ('x', 'y')

for subj in subjects:
	for emo in ('Neutral', 'Anger', 'Comtempt', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise'):
		direct = f'dataset/{subj}/{emo}'
		if os.path.isdir(direct):
			os.system(f'mkdir -p lm_{direct}')
			for doss in range(12):
				for num_im in range(72):
					im = f'{direct}/{subj}_{emo}_{doss:03d}_{num_im:08d}'
					if os.path.isfile(f'{im}.png'):
						img = cv2.imread(f'{im}.png', 0)
						lm = efl(img)
						with open(f'lm_{im}.csv', 'w') as f:
							writer = csv.writer(f, dialect='md')
							writer.writerow(headers)
							for coor in lm:
								writer.writerow(coor)
							f.close()
						print(f'{im}.png -> csv: done')


#!/usr/bin/env python

import psycopg2
import os
import re
import subprocess
from configparser import ConfigParser

if "check_output_noretval" not in dir( subprocess ): # duck punch it in!
    def f(*popenargs, **kwargs):
        if 'stdout' in kwargs:
            raise ValueError('stdout argument not allowed, it will be overridden.')
        process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
        output, unused_err = process.communicate()
        retcode = process.poll()
        return output
    subprocess.check_output_noretval = f

def record_event( node_id, event ):
  c = database.cursor()
  c.execute( "INSERT INTO node_event ( node_id, event ) VALUES (%s, %s)", ( node_id, event ) )
  database.commit()
  c.close()

def mark_offline_if_errors( node_id ):
	c = database.cursor()
	c.execute(" SELECT count(*) FROM ecc_error_history WHERE node_id=%s AND time > now()-'2 days'::interval", (node_id,))
	r = c.fetchone()
	if( int(r[0]) > 2 ): # accept a couple of errors
		print("MArking node %s offline due to ECC errors" % ( node_id ) )
		c.execute( "UPDATE node SET ecc_offline = TRUE WHERE node_id=%s", (node_id,) )
#	else:
#		c.execute( "UPDATE node SET ecc_offline = FALSE WHERE node_id=%s AND ecc_offline = FALSE", (node_id,) )
	database.commit()

def check_nodes():
	c = database.cursor()

	c.execute("SELECT hostname, node_id, bmc_ip FROM node", () )
	ret = c.fetchall()
	for r in ret:
		hostname = r[0]
		node_id= r[1]
		bmc_ip = r[2]
		check_sel( hostname, node_id, bmc_ip )
		mark_offline_if_errors( node_id )

def check_sel( hostname, node_id, bmc_ip ):
	ssh_key  = config['ssh']['key']
	ssh_user = config['ssh']['user']
	ssh_host = config['ssh']['host']
	ssh_ssh  = config['ssh']['ssh']

	check_sel = config['bmc']['check_sel']
	cmd = [ ssh_ssh, "-i", ssh_key, ssh_user + "@" + ssh_host, check_sel, bmc_ip  ]

	try:
		print( hostname )
		ret = subprocess.check_output( cmd )
		ret = ret.split("\n")
		c = database.cursor()
		for r in ret:
				if len(r.split("|"))==3:
					r = r.split("|")
					necc = int(r[0].strip())
					error= r[1].strip()
					print( "[%d][%s]" % ( necc, error ) )
					if re.search( "Uncor", error, re.IGNORECASE ):
						c.execute("INSERT INTO ecc_error_history ( node_id, error_count, error ) VALUES ( %s, %s, %s )", ( node_id, necc, error ) )
		database.commit()
		c.close()
	except:
		print("FAILED TO CHECK %s - BMC OFFLINE?" % ( hostname ) )

if __name__ == "__main__":

	config = ConfigParser()

	config.read( os.path.join( "/opt/cx1configmanagement/cx1configmanagement/app/config.ini") )

	database = psycopg2.connect(
      "dbname='" + config['database']['name'] + "'"
      + " user='" + config['database']['user'] + "'"
      + " host='" + config['database']['host'] + "'"
      + " password='" + config['database']['password'] + "'"
    )

	check_nodes()


from docx import Document
from docx.shared import Cm,Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_UNDERLINE
from docx.shared import RGBColor

document = Document()

#wyrównywanie tekstu w poziomie
paragraph = document.add_paragraph('Hi, Im a paragraph')
paragraph2 = document.add_paragraph('Hi, Im a paragraph the II')
paragraph_format = paragraph.paragraph_format #definiujemy mienną paragraph_format i przypisujemy do naszego paragrafu
paragraph2_format = paragraph2.paragraph_format
# indicating alignment is inherited from the style hierarchy - "None"
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
paragraph2_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT

#wcięcia
paragraph_normal = document.add_paragraph('Normal text')
paragraph3 = document.add_paragraph('Nice to meet u, I have a indent of 0.5cm')
paragraph3_format = paragraph3.paragraph_format
paragraph3_format.left_indent = Cm(0.5)
document.add_page_break()

#nadawanie czcionki
run = document.add_paragraph('It is 14:12 ').add_run('That is Nice')
font = run.font
font.name = 'Calibri'
font.size = Pt(18)

#podkreślenie
run2 = document.add_paragraph('Im not underlined ').add_run('Im undelined but value NONE')
font2 = run2.font
font2.underline = WD_UNDERLINE.NONE

#kolorowu tekst
run3 = document.add_paragraph('Im black ').add_run('I have a color on me')
font3 = run3.font
font3.color.rgb = RGBColor(0x42,0x24,0xE9)

document.save('text.docx')


import tensorflow as tf
import numpy as np

epochs = 100000
learning_rate = 0.1

INPUT = 2
HIDDEN = 16
OUTPUT = 1

int2binary = {}
binary_dim = 8

largest_number = 2 ** binary_dim

for i in range(largest_number):
    bin_carr = [int(x) for x in list('{0:0b}'.format(i))]
    length = len(bin_carr)
    zeros = [0]*(8-length)
    int2binary[i] = zeros + bin_carr

W_Hidd = tf.Variable(tf.random_normal(shape = [HIDDEN+INPUT,HIDDEN], mean = 0, stddev = 0.1, dtype =tf.float32), name="hidden_weight")#propagates previous state to current state plus one section for the inputs. Effectively it makes a giant matrix
B_Hidd = tf.Variable(tf.zeros(shape=[1,HIDDEN]), dtype =tf.float32,name="hidden_bias") #this bias adds onto the hidden next state

W_Out = tf.Variable(tf.random_normal(shape = [HIDDEN, OUTPUT], mean = 0 ,stddev = 0.1),dtype =tf.float32,name="Out_weight") #propagates to the end, with output, which is only one thing.
B_Out = tf.Variable(tf.zeros(shape = [1,1]), dtype =tf.float32, name="Out_bias")

states_list = []
output_list = []

X = tf.placeholder(tf.float32, shape=[2,binary_dim],name="input")

iterable_X = tf.unstack(X,axis=1)

init_hid_layer = tf.placeholder(tf.float32, shape = [1,HIDDEN],name="hidden_layer_state")
current_hid_layer = init_hid_layer
first = True
for current in iterable_X:

    current_flat = tf.reshape(current,[1,INPUT])
    concat_mat = tf.concat([current_flat,current_hid_layer],axis=1)
    next_hid_layer = tf.sigmoid(tf.matmul(concat_mat,W_Hidd) + B_Hidd)

    if first:
        next_states_mat = tf.transpose(next_hid_layer)
        first = False
    else:
        next_states_mat = tf.concat([current_states_mat,tf.transpose(next_hid_layer)],axis = 1)
    current_states_mat = next_states_mat
    current_hid_layer = next_hid_layer

logit_outputs = tf.add(tf.matmul(tf.transpose(current_states_mat),W_Out),B_Out,name = "prediction_outputs")

saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, "models/test-95000")

    a_int = np.random.randint(largest_number/2)
    a = int2binary[55]
    a_np = np.matrix(a)
    #print(a_np)
    b_int = np.random.randint(largest_number / 2)
    b = int2binary[99]
    b_np = np.matrix(b)
    c_int = 55+99
    c = int2binary[c_int]
    c_np = np.matrix(c)

    pseudo_curr = np.zeros((1,HIDDEN))
    x = np.concatenate((a_np,b_np), axis=0)
    x = np.flip(x, axis = 1)
    c_np = np.flip(c_np, axis = 1)

    prediction = sess.run(logit_outputs,feed_dict= {X:x,init_hid_layer:pseudo_curr})

    print(c_np)
    print(prediction)


"""
    Downloads the data of the URL: http://wise-tt.com/wtt_um_feri/

    The entire browser process is done by using Selenium and Firefox

"""
import time
import os
import sys
import glob

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options

class Browser:
    browser = None
    destination = None

    sleep = 2

    def __init__(self, destination: str, hide_browser=True) -> None:
        """
        A Browser class used for accessing the website from which it download the .ics file or schedule.

        Uses the 'geckodriver' browser web engine.

        @param destination: Folder path where the browsers saves downloaded files.
        @param hide_browser: Should the object display the browser. Useful for debugging.
        """
        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.folderList", 2)  # Browser will download to "browser.browser.dir"
        profile.set_preference("browser.download.manager.showWhenStarting", False)
        profile.set_preference("browser.download.dir", destination)
        profile.set_preference("webdriver.log.file", "/tmp/urnik_firefox_log")
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                               "application/octet-stream, text/calendar,application/vnd.sus-calendar,text/x-vcalendar")

        options = Options()
        if hide_browser:
            options.add_argument("--headless")  # Hide browser window

        self.browser = webdriver.Firefox(firefox_profile=profile, firefox_options=options)
        self.destination = destination

    def download_schedule(self, url: str) -> None:
        """
        Download the .ics file from the site using the url parameter.

        @param url: Url of the site to download from
        """

        # First removes any ics file specified in the destination folder
        for i in glob.iglob(str(self.destination) + "/*.ics"):
            os.remove(i)

        print("Starting browser")

        self.browser.get(url)

        wait = WebDriverWait(self.browser, 4)

        print("Loading site")
        time.sleep(self.sleep)

        script = """return $("span:contains('iCal-teden')").parent()["""
        download_button_id = self.browser.execute_script(script + "0].id")
        if download_button_id == "":
            download_button_id = self.browser.execute_script(script + "1].id")

        element_type = wait.until(EC.element_to_be_clickable((By.ID, download_button_id)))
        element_type.click()

        # A basic progress bar loop till the file has downloaded
        for i in range(1, 10):

            # Checks if the file hasn't been downloaded in in the directory self.downloadPath
            if (not any("calendar" in file for file in
                        os.listdir(self.destination))):
                time.sleep(0.4)
                sys.stdout.write("\rDownloading {}".format("." * i))
                sys.stdout.flush()
            else:
                print("\nDownloaded file.")
                break

        self.browser.quit()


f1var=open(r'C:\Users\Admin\Desktop\assigment\file_assignments\Python_Assignment_1\data.txt','r')
data=f1var.readlines()
new=data[0:]
l1=[]
for i in new:
    res=i.split('#')
    l1.append(res)
l2=[]

for j in l1:                        #unique
    if j not in l2:
        (l2.append(j))
print(l2)

la=len(l2)
print(la)
for d in range(1,la):
    for e in range(1,la-d):
        if(l2[e][4]>l2[e+1][4]):
            temp=l2[e]              #ascending salary
            l2[e]=l2[e+1]
            l2[e+1]=temp
for k in l2:
    m='#'.join(k)
    print(m)
f1var.close()


'''
Automate experiment launching
'''
from launch_common import *

GPU_FULLNAME = {
        'dam': 'Damascus',
        'pre': 'Pretoria',
        'kat': 'Kathmandu',
        'bag': 'Baghdad',
        'bud': 'Budapest',
        'lis': 'Lisbon'
}

SPEC = {}

all_specs = {}
HYPER_NOISES = [0.05, 0.1, 0.2]
for key in [100, 1000, 60000]:
    all_specs[key] = [('original', key, 9,
                       'sp_{},{}'.format(noise, noise),
                       0.002, scale_decosts(w))
                     for noise in HYPER_NOISES
    #                  for sup_noise in HYPER_NOISES
                     for w in [1, 2, 5]
                     ]

# allocate specs for each machine
def alloc(key, n):
    global all_specs
    if n == -1:
        specs = all_specs[key]
        all_specs[key] = []
    else:
        specs = all_specs[key][:n]
        del all_specs[key][:n]
    return specs

SECTION = 'pre' # pretoria ===============================
SPEC[SECTION] = [
                ('gx_2.2_leakyrelu_0.05', 100, 4, 0.2, 0.002,
                 scale_decosts(2)),
                ()]
# SPEC[SECTION] = [('gx_2.2.2_leakyrelu_0.025', 100, i, 0.2, 0.002,
#               scale_decosts(10))
#              for i in remove_range(10, 9)]

SECTION = 'bud' # budapest ===============================
SPEC[SECTION] = [
               ()]
# SPEC[SECTION] = [('gx_2.2_leakyrelu_0.05', 100, i, 0.2, 0.002,
#               scale_decosts(2))
#              for i in range(10)]
SPEC[SECTION] = alloc(60000, 1)

SECTION = 'kat' # kathmandu ===============================
SPEC[SECTION] = [
                ('gx_2.2_leakyrelu_0.05', 100, 8, 0.2, 0.002,
                 scale_decosts(2)),
               ()]
# SPEC[SECTION] = [('gx_2.2_leakyrelu_0.0125', 1000, i, 0.3, 0.002,
#               scale_decosts(5))
#              for i in remove_range(10, 6)]

SECTION = 'dam' # damasucs ===============================
SPEC[SECTION] = [

               ()]
SPEC[SECTION] = alloc(60000, 2)
# SPEC[SECTION] = [('gx_2.2.2_leakyrelu_{}'.format(init), 100, 9, noise, 0.002,
#               scale_decosts(w))
#              for noise in [0.3]
#              for w in [2, 5, 10, 1]
#              for init in ['0.006', '0.0125', '0.025', '0.05']
#              ]

SECTION = 'bag' # baghdad ===============================
SPEC[SECTION] = [
               ()]
SPEC[SECTION] = alloc(60000, -1)

# lisbon
SPEC['lis'] = []

if __name__ == '__main__':
    launch_main('CLIC', GPU_FULLNAME, SPEC, concur_email=6)


# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import itertools
from abc import ABC, ABCMeta
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePath
from typing import Dict, Iterable, List, Optional, Tuple, Type, TypeVar, cast

from pants.base.exiter import PANTS_FAILED_EXIT_CODE, PANTS_SUCCEEDED_EXIT_CODE
from pants.core.util_rules.filter_empty_sources import (
    FieldSetsWithSources,
    FieldSetsWithSourcesRequest,
)
from pants.engine import desktop
from pants.engine.addresses import Address
from pants.engine.collection import Collection
from pants.engine.console import Console
from pants.engine.engine_aware import EngineAware
from pants.engine.fs import Digest, MergeDigests, Workspace
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.process import FallibleProcessResult, InteractiveProcess, InteractiveRunner
from pants.engine.rules import Get, MultiGet, collect_rules, goal_rule, rule
from pants.engine.target import (
    FieldSet,
    Sources,
    TargetsToValidFieldSets,
    TargetsToValidFieldSetsRequest,
)
from pants.engine.unions import UnionMembership, union
from pants.util.logging import LogLevel

class Status(Enum):
    SUCCESS = "SUCCESS"
    FAILURE = "FAILURE"

class CoverageReportType(Enum):
    CONSOLE = ("console", "report")
    XML = ("xml", None)
    HTML = ("html", None)
    RAW = ("raw", None)

    _report_name: str

    def __new__(cls, value: str, report_name: Optional[str] = None) -> "CoverageReportType":
        member: "CoverageReportType" = object.__new__(cls)
        member._value_ = value
        member._report_name = report_name if report_name is not None else value
        return member

    @property
    def report_name(self) -> str:
        return self._report_name

@dataclass(frozen=True)
class TestResult(EngineAware):
    status: Status
    stdout: str
    stderr: str
    coverage_data: Optional["CoverageData"]
    xml_results: Optional[Digest]
    address_ref: str = ""

    # Prevent this class from being detected by pytest as a test class.
    __test__ = False

    @staticmethod
    def from_fallible_process_result(
        process_result: FallibleProcessResult,
        *,
        coverage_data: Optional["CoverageData"] = None,
        xml_results: Optional[Digest] = None,
        address_ref: str = "",
    ) -> "TestResult":
        return TestResult(
            status=Status.SUCCESS if process_result.exit_code == 0 else Status.FAILURE,
            stdout=process_result.stdout.decode(),
            stderr=process_result.stderr.decode(),
            coverage_data=coverage_data,
            xml_results=xml_results,
            address_ref=address_ref,
        )

    def level(self):
        if self.status == Status.FAILURE:
            return LogLevel.ERROR
        return None

    def message(self):
        result = "succeeded" if self.status == Status.SUCCESS else "failed"
        return f"tests {result}: {self.address_ref}"

@dataclass(frozen=True)
class TestDebugRequest:
    process: InteractiveProcess

    # Prevent this class from being detected by pytest as a test class.
    __test__ = False

@union
class TestFieldSet(FieldSet, metaclass=ABCMeta):
    """The fields necessary to run tests on a target."""

    sources: Sources

    __test__ = False

# NB: This is only used for the sake of coordinator_of_tests. Consider inlining that rule so that
# we can remove this wrapper type.
@dataclass(frozen=True)
class WrappedTestFieldSet:
    field_set: TestFieldSet

@dataclass(frozen=True)
class AddressAndTestResult:
    address: Address
    test_result: TestResult

class CoverageData(ABC):
    """Base class for inputs to a coverage report.

    Subclasses should add whichever fields they require - snapshots of coverage output, XML files,
    etc.
    """

_CD = TypeVar("_CD", bound=CoverageData)

@union
class CoverageDataCollection(Collection[_CD]):
    element_type: Type[_CD]

class CoverageReport(ABC):
    """Represents a code coverage report that can be materialized to the terminal or disk."""

    def materialize(self, console: Console, workspace: Workspace) -> Optional[PurePath]:
        """Materialize this code coverage report to the terminal or disk.

        :param console: A handle to the terminal.
        :param workspace: A handle to local disk.
        :return: If a report was materialized to disk, the path of the file in the report one might
                 open first to start examining the report.
        """
        ...

@dataclass(frozen=True)
class ConsoleCoverageReport(CoverageReport):
    """Materializes a code coverage report to the terminal."""

    report: str

    def materialize(self, console: Console, workspace: Workspace) -> None:
        console.print_stderr(f"\n{self.report}")
        return None

@dataclass(frozen=True)
class FilesystemCoverageReport(CoverageReport):
    """Materializes a code coverage report to disk."""

    result_digest: Digest
    directory_to_materialize_to: PurePath
    report_file: Optional[PurePath]
    report_type: CoverageReportType

    def materialize(self, console: Console, workspace: Workspace) -> Optional[PurePath]:
        workspace.write_digest(
            self.result_digest, path_prefix=str(self.directory_to_materialize_to)
        )
        console.print_stderr(
            f"\nWrote {self.report_type.report_name} coverage report to `{self.directory_to_materialize_to}`"
        )
        return self.report_file

@dataclass(frozen=True)
class CoverageReports:
    reports: Tuple[CoverageReport, ...]

    def materialize(self, console: Console, workspace: Workspace) -> Tuple[PurePath, ...]:
        report_paths = []
        for report in self.reports:
            report_path = report.materialize(console, workspace)
            if report_path:
                report_paths.append(report_path)
        return tuple(report_paths)

class ShowOutput(Enum):
    """Which tests to emit detailed output for."""

    ALL = "all"
    FAILED = "failed"
    NONE = "none"

class TestSubsystem(GoalSubsystem):
    """Run tests."""

    name = "test"

    required_union_implementations = (TestFieldSet,)

    # Prevent this class from being detected by pytest as a test class.
    __test__ = False

    @classmethod
    def register_options(cls, register) -> None:
        super().register_options(register)
        register(
            "--debug",
            type=bool,
            default=False,
            help=(
                "Run a single test target in an interactive process. This is necessary, for "
                "example, when you add breakpoints to your code."
            ),
        )
        register(
            "--force",
            type=bool,
            default=False,
            help="Force the tests to run, even if they could be satisfied from cache.",
        )
        register(
            "--output",
            type=ShowOutput,
            default=ShowOutput.FAILED,
            help="Show stdout/stderr for these tests.",
        )
        register(
            "--use-coverage",
            type=bool,
            default=False,
            help="Generate a coverage report if the test runner supports it.",
        )
        register(
            "--open-coverage",
            type=bool,
            default=False,
            help=(
                "If a coverage report file is generated, open it on the local system if the "
                "system supports this."
            ),
        )

    @property
    def debug(self) -> bool:
        return cast(bool, self.options.debug)

    @property
    def force(self) -> bool:
        return cast(bool, self.options.force)

    @property
    def output(self) -> ShowOutput:
        return cast(ShowOutput, self.options.output)

    @property
    def use_coverage(self) -> bool:
        return cast(bool, self.options.use_coverage)

    @property
    def open_coverage(self) -> bool:
        return cast(bool, self.options.open_coverage)

class Test(Goal):
    subsystem_cls = TestSubsystem

    __test__ = False

@goal_rule
async def run_tests(
    console: Console,
    test_subsystem: TestSubsystem,
    interactive_runner: InteractiveRunner,
    workspace: Workspace,
    union_membership: UnionMembership,
) -> Test:
    if test_subsystem.debug:
        targets_to_valid_field_sets = await Get(
            TargetsToValidFieldSets,
            TargetsToValidFieldSetsRequest(
                TestFieldSet,
                goal_description="`test --debug`",
                error_if_no_valid_targets=True,
                expect_single_field_set=True,
            ),
        )
        field_set = targets_to_valid_field_sets.field_sets[0]
        request = await Get(TestDebugRequest, TestFieldSet, field_set)
        debug_result = interactive_runner.run(request.process)
        return Test(debug_result.exit_code)

    targets_to_valid_field_sets = await Get(
        TargetsToValidFieldSets,
        TargetsToValidFieldSetsRequest(
            TestFieldSet,
            goal_description=f"the `{test_subsystem.name}` goal",
            error_if_no_valid_targets=False,
        ),
    )
    field_sets_with_sources = await Get(
        FieldSetsWithSources, FieldSetsWithSourcesRequest(targets_to_valid_field_sets.field_sets)
    )

    results = await MultiGet(
        Get(AddressAndTestResult, WrappedTestFieldSet(field_set))
        for field_set in field_sets_with_sources
    )

    # Print details.
    for result in results:
        if test_subsystem.options.output == ShowOutput.NONE or (
            test_subsystem.options.output == ShowOutput.FAILED
            and result.test_result.status == Status.SUCCESS
        ):
            continue
        has_output = result.test_result.stdout or result.test_result.stderr
        if has_output:
            status = (
                console.green("✓")
                if result.test_result.status == Status.SUCCESS
                else console.red("𐄂")
            )
            console.print_stderr(f"{status} {result.address}")
        if result.test_result.stdout:
            console.print_stderr(result.test_result.stdout)
        if result.test_result.stderr:
            console.print_stderr(result.test_result.stderr)
        if has_output and result != results[-1]:
            console.print_stderr("")

    # Print summary
    console.print_stderr("")
    for result in results:
        color = console.green if result.test_result.status == Status.SUCCESS else console.red
        # The right-align logic sees the color control codes as characters, so we have
        # to account for that. In f-strings the alignment field widths must be literals,
        # so we have to indirect via a call to .format().
        right_align = 19 if console.use_colors else 10
        format_str = f"{{addr:80}}.....{{result:>{right_align}}}"
        console.print_stderr(
            format_str.format(
                addr=result.address.spec, result=color(result.test_result.status.value)
            )
        )

    merged_xml_results = await Get(
        Digest,
        MergeDigests(
            result.test_result.xml_results for result in results if result.test_result.xml_results
        ),
    )
    workspace.write_digest(merged_xml_results)

    if test_subsystem.use_coverage:
        all_coverage_data: Iterable[CoverageData] = [
            result.test_result.coverage_data
            for result in results
            if result.test_result.coverage_data is not None
        ]

        coverage_types_to_collection_types: Dict[
            Type[CoverageData], Type[CoverageDataCollection]
        ] = {
            collection_cls.element_type: collection_cls
            for collection_cls in union_membership.get(CoverageDataCollection)
        }
        coverage_collections: List[CoverageDataCollection] = []
        for data_cls, data in itertools.groupby(all_coverage_data, lambda data: type(data)):
            collection_cls = coverage_types_to_collection_types[data_cls]
            coverage_collections.append(collection_cls(data))
        # We can create multiple reports for each coverage data (console, xml and html)
        coverage_reports_collections = await MultiGet(
            Get(CoverageReports, CoverageDataCollection, coverage_collection)
            for coverage_collection in coverage_collections
        )

        coverage_report_files: List[PurePath] = []
        for coverage_reports in coverage_reports_collections:
            report_files = coverage_reports.materialize(console, workspace)
            coverage_report_files.extend(report_files)

        if coverage_report_files and test_subsystem.open_coverage:
            desktop.ui_open(console, interactive_runner, coverage_report_files)

    exit_code = (
        PANTS_FAILED_EXIT_CODE
        if any(res.test_result.status == Status.FAILURE for res in results)
        else PANTS_SUCCEEDED_EXIT_CODE
    )

    return Test(exit_code)

@rule(desc="Run test target")
async def coordinator_of_tests(wrapped_field_set: WrappedTestFieldSet) -> AddressAndTestResult:
    field_set = wrapped_field_set.field_set
    result = await Get(TestResult, TestFieldSet, field_set)
    return AddressAndTestResult(field_set.address, result)

def rules():
    return collect_rules()


def listinput():
    l=[]
    n=int(input('Enter the number of inputs you want in the list:'))
    i=0
    f=1
    while i<n:
        print('Enter the element number', f )
        k=input('')
        l.append(k)
        i=i+1
        f=f+1
    return l


def pseudo_random():
    import random
    map_size = 10
    map = []
    first_random_list = [1,0]
    last_random_list = [1,2]
    first_lines = []
    ending_lines = []
    while len(first_lines) < map_size:
        first_lines.append(random.choice(first_random_list))
        for num in first_lines:
            if num == 0:
                while len(first_lines) < map_size:
                    first_lines.append(1)

    while len(ending_lines) < map_size:
        ending_lines.append(random.choice(last_random_list))
        for num in ending_lines:
            if num == 2:
                while len(ending_lines) < map_size:
                    ending_lines.append(1)

    between_line = []

    map.append(first_lines)
    while True:
        between_line.append(1)
        while True:
            between_line.append(random.randint(0,1))
            if len(between_line) == map_size-1:
                break
        between_line.append(1)
        map.append(between_line)
        between_line = []
        if len(map) == map_size-1:
            break
    map.append(ending_lines)

    return map


import json
import os
import unittest
from decimal import Decimal

import ddt

from common.excel_handler import ExcelHandler
from common.requests_handler import visit
from middleware import handler

env_data = handler.MiddleHandler()
cases = env_data.excel.read_data("invest")
logger = env_data.logger

@ddt.ddt
class TestInvest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # 登录
        cls.token = env_data.token
        cls.member_id = env_data.member_id

    def setUp(self):
        self.db = env_data.db_class()
        self.loan_id = env_data.loan_id
        self.pass_loan_id = env_data.audit()
        env_data.recharge()

    def tearDown(self):
        self.db.close()

    @ddt.data(*cases)
    def test_invest(self, case_info):
        """测试投资接口"""
        # 查询投资用户在invest表中的投资记录
        before_data = self.db.query(
            "SELECT COUNT(*) as berfore_num FROM invest WHERE member_id={};".format(self.member_id))
        before_num = before_data["berfore_num"]

        # 查询投资用户的余额
        before_info = self.db.query(
            "SELECT * FROM member WHERE id = {}".format(self.member_id)
        )
        before_amount = before_info["leave_amount"]

        # 查询投资用户的交易记录
        before_log_info = self.db.query(
            "SELECT COUNT(*) as before_log_count  FROM financelog WHERE pay_member_id = {}".format(self.member_id)
        )
        before_log_count = before_log_info["before_log_count"]

        logger.info("正在执行第{}条用例：{}".format(case_info["case_id"], case_info["title"]))

        # data = case_info["data"]
        # if "#member_id#" in data:
        #     data = data.replace("#member_id#", str(self.member_id))
        #
        # headers = case_info["headers"]
        # if "#token#" in headers:
        #     headers = headers.replace("#token#", self.token)
        #
        # if "#loan_id#" in data:
        #     data = data.replace("#loan_id#", str(self.pass_loan_id))
        data = env_data.replace_data(case_info["data"])
        headers = env_data.replace_data(case_info["headers"])
        # 访问接口
        resp = visit(
            url=env_data.yaml_data["host"] + case_info["url"],
            method=case_info["method"],
            json=json.loads(data),
            headers=json.loads(headers)
        )
        # 测试用例的路径
        data_path = os.path.join(env_data.config.DATA_PATH, "testcases.xlsx")
        # 开始断言
        try:
            for k, v in json.loads(case_info["expected"]).items():
                self.assertTrue(v == resp[k])
            if resp["code"] == 0:
                # 判断invest表是否增加一条记录
                after_data = self.db.query(
                    "SELECT COUNT(*) as after_num FROM invest WHERE member_id={};".format(self.member_id))
                after_num = after_data["after_num"]
                self.assertTrue(before_num + 1 == after_num)
                # 判断投资之前余额 - 投资金额 == 投资后余额
                after_info = self.db.query(
                    "SELECT * FROM member WHERE id = {}".format(self.member_id)
                )
                after_amount = after_info["leave_amount"]
                self.assertTrue(before_amount - Decimal(str(json.loads(data)["amount"])) == after_amount)
                # 判断financelog表是否新增一条交易记录
                after_log_info = self.db.query(
                    "SELECT COUNT(*) as after_log_count  FROM financelog WHERE pay_member_id = {}".format(self.member_id)
                )
                after_log_count = after_log_info["after_log_count"]
                self.assertTrue(before_log_count + 1 == after_log_count)
            # 断言成功设置result属性为pass
            self.result = "PASS"
            logger.info("第{}条测试用例通过".format(case_info["case_id"]))

        except AssertionError as e:
            self.result = "FAIL"
            logger.error("第{}条测试用例无法通过：{}".format(case_info["case_id"], e))
            raise e
        # 将测试结果写入excel
        finally:
            ExcelHandler(data_path).write(sheet_name="add", row=case_info["case_id"] + 1, column=9,
                                          data=self.result)

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


"""Tests for the in Memory Content Store with exact matching"""

import multiprocessing
import unittest

from PiCN.Layers.ICNLayer.PendingInterestTable.PendingInterestTableMemoryExact import PendingInterstTableMemoryExact
from PiCN.Layers.ICNLayer.ForwardingInformationBase import ForwardingInformationBaseEntry
from PiCN.Packets import Name

class test_PendingInterstTableMemoryExact(unittest.TestCase):
    def setUp(self):
        self.manager = multiprocessing.Manager()
        self.pit: PendingInterstTableMemoryExact = PendingInterstTableMemoryExact()

    def tearDown(self):
        pass

    def test_add_data_to_pit(self):
        """Test adding data to PIT"""
        fid = 1
        fake_outgoing_face = 15
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid, fake_outgoing_face)
        data = self.pit._container[0]
        self.assertEqual(data.name, name)

    def test_find_data_in_pit(self):
        """Test finding data in PIT exact"""
        fid = 1
        fake_outgoing_face = 15
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid, fake_outgoing_face)
        data = self.pit._container[0]
        self.assertEqual(data.name, name)
        res = self.pit.find_pit_entry(name)
        self.assertEqual(res.name, name)
        self.assertEqual(res.face_id, [fid])

    def test_find_data_in_pit_no_match(self):
        """Test finding data in PIT exact, with no match"""
        fid = 1
        fake_outgoing_face = 15
        name1 = Name("/test/data")
        name2 = Name("/data/test")
        self.pit.add_pit_entry(name1, fid, fake_outgoing_face)
        data = self.pit._container[0]
        self.assertEqual(data.name, name1)
        res = self.pit.find_pit_entry(name2)
        self.assertEqual(res, None)

    def test_find_data_to_pit_deduplication(self):
        """Test finding data in PIT with multiple fids"""
        fid1 = 1
        fid2 = 2
        outgoing_face = 15
        outgoing_face2 = 16
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid1, outgoing_face)
        self.pit.add_pit_entry(name, fid2, outgoing_face2)
        data = self.pit._container[0]
        self.assertEqual(data.name, name)
        res = self.pit.find_pit_entry(name)
        self.assertEqual(res.name, name)
        self.assertEqual(res.face_id, [fid1, fid2])
        self.assertEqual(res.outgoing_faces, [outgoing_face, outgoing_face2])

    def test_find_data_to_pit_deduplication_samefid(self):
        """Test finding data in PIT with two time same fids"""
        fid = 1
        outgoing_face = 15
        outgoing_face2 = 16
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid, outgoing_face)
        self.pit.add_pit_entry(name, fid, outgoing_face2)
        self.pit.add_pit_entry(name, fid, outgoing_face2)
        data = self.pit._container[0]
        self.assertEqual(data.name, name)
        res = self.pit.find_pit_entry(name)
        self.assertEqual(res.name, name)
        self.assertEqual(res.face_id, [fid])
        self.assertEqual(res.outgoing_faces, [outgoing_face, outgoing_face2])

    def test_remove_data_from_pit(self):
        """Test removing data from PIT"""
        fid = 1
        outgoing_face = 15
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid, outgoing_face)

        data = self.pit._container[0]
        self.assertEqual(data.name, name)
        self.assertEqual(len(self.pit._container), 1)
        self.pit.remove_pit_entry(name)
        self.assertEqual(len(self.pit._container), 0)

    #TODO: Check HERE
    def test_add_already_used_fib_face(self):
        """Test adding an already used FIB face"""
        n1 = Name("/test/data")
        fib_entry = ForwardingInformationBaseEntry(n1, 2, False)
        self.pit.add_pit_entry(n1, 1, 2, None, False)
        self.pit.add_used_fib_face(n1, [fib_entry.faceid])
        #self.assertEqual(self.pit.get_already_used_pit_entries(n1)[0], fib_entry.faceid )

    def test_set_number_of_forwards(self):
        """Test setting the number of forwards used in parallel, important for nack handling"""
        n1 = Name("/test/data")
        outgoing_face = 15
        self.pit.add_pit_entry(n1, [1], outgoing_face, None, False)
        self.pit.set_number_of_forwards(n1, 3)
        entry = self.pit.find_pit_entry(n1)

        self.assertEqual(entry.number_of_forwards, 3)

    def test_add_interested_face(self):
        """Test the addition of an interested face to an existing PIT entry"""
        fid = 1
        fid2 = 2
        outgoing_face = 15
        name = Name("/test/data")
        self.pit.add_pit_entry(name, fid, outgoing_face)
        self.pit.add_interested_face(name, fid2)
        pit_entry = self.pit.find_pit_entry(name)
        self.assertEqual(pit_entry.faceids, [fid, fid2])

    def test_occupancy_available_faces_per_name(self):
        name = Name("/a/b")
        fib_entry = ForwardingInformationBaseEntry(name, 1, False)
        fib_entry_faces = [1,2,3]
        fib_entry.faceid = fib_entry_faces

        pit_name1 = Name("/a/b")
        pit_name2 = Name("/x/y/z")
        pit_name3 = Name("/m/n")
        pit_name4 = Name("/a/b/c")
        pit_name5 = Name("/l/o")
        pit_name6 = Name("/a/b/c/d")

        self.pit.add_pit_entry(pit_name1, 7, 1, None, False)
        self.pit.add_pit_entry(pit_name2, 5, 1, None, False)
        self.pit.add_pit_entry(pit_name3, 6, 3, None, False)
        self.pit.add_pit_entry(pit_name4, 8, 1, None, False)
        self.pit.add_pit_entry(pit_name5, 9, 2, None, False)
        self.pit.add_pit_entry(pit_name6, 4, 2, None, False)

        result = self.pit.occupancy_available_faces_per_name(fib_entry)
        print(result[1])
        print(result[2])
        print(result[3])
        self.assertEqual(result[1], 2)
        self.assertEqual(result[2], 1)
        self.assertEqual(result[3], 0)
        self.assertEqual(len(result), 3)


from rq import Worker

class ScraperWorker(Worker):

    def request_stop(self, *args, **kwargs):
        """When SIGINT is sent to the worker (eg, if the Supervisor process
        group is restarted), immediately fail the running job and stop the
        worker. This avoids a scenario in which the worker gets shut down but
        not unregistered in Redis, causing it to get "marooned" in the admin.
        """
        job = self.get_current_job()

        if job:
            self.handle_job_failure(job)
            self.failed_queue.quarantine(job, 'Worker shutdown.')

        self.request_force_stop(*args, **kwargs)


from typing import Union, List

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import lines
from matplotlib.font_manager import FontProperties
from scipy import stats
from seaborn.utils import remove_na

from statannot.StatResult import StatResult
from statannot.comparisons_corrections.ComparisonsCorrection import ComparisonsCorrection
from statannot.comparisons_corrections.utils import assert_valid_correction_name
from statannot.utils import assert_is_in

DEFAULT = object()

def stat_test(
        box_data1,
        box_data2,
        test_name,
        comparisons_correction=None,
        num_comparisons=1,
        verbose=1,
        **stats_params
):
    """Get formatted result of two sample statistical test.

    Arguments
    ---------
    box_data1, box_data2
    test_name: str
        Statistical test to run. Must be one of:
        - `Levene`
        - `Mann-Whitney`
        - `Mann-Whitney-gt`
        - `Mann-Whitney-ls`
        - `t-test_ind`
        - `t-test_welch`
        - `t-test_paired`
        - `Wilcoxon`
        - `Kruskal`
    comparisons_correction: ComparisonCorrection (type 0) or None, default None
        Method to use for multiple comparisons correction.
    num_comparisons: int, default 1
        Number of comparisons to use for multiple comparisons correction.
    verbose: int
        Verbosity parameter, see add_stat_annotation
    stats_params
        Additional keyword arguments to pass to scipy stats functions.

    Returns
    -------
    StatResult object with formatted result of test.

    """
    # Check arguments.
    if comparisons_correction is not None:
        if not isinstance(comparisons_correction, ComparisonsCorrection):
            raise ValueError("comparisons_correction must be a "
                             "ComparisonsCorrection instance")

    # Switch to run scipy.stats hypothesis test.
    if test_name == 'Levene':
        stat, pval = stats.levene(box_data1, box_data2, **stats_params)
        result = StatResult(
            'Levene test of variance', 'levene', 'stat_value', stat, pval
        )
    elif test_name == 'Mann-Whitney':
        u_stat, pval = stats.mannwhitneyu(
            box_data1, box_data2, alternative='two-sided', **stats_params
        )
        result = StatResult(
            'Mann-Whitney-Wilcoxon test two-sided',
            'M.W.W.',
            'U_stat',
            u_stat,
            pval,
        )
    elif test_name == 'Mann-Whitney-gt':
        u_stat, pval = stats.mannwhitneyu(
            box_data1, box_data2, alternative='greater', **stats_params
        )
        result = StatResult(
            'Mann-Whitney-Wilcoxon test greater',
            'M.W.W.',
            'U_stat',
            u_stat,
            pval,
        )
    elif test_name == 'Mann-Whitney-ls':
        u_stat, pval = stats.mannwhitneyu(
            box_data1, box_data2, alternative='less', **stats_params
        )
        result = StatResult(
            'Mann-Whitney-Wilcoxon test smaller',
            'M.W.W.',
            'U_stat',
            u_stat,
            pval,
        )
    elif test_name == 't-test_ind':
        stat, pval = stats.ttest_ind(a=box_data1, b=box_data2, **stats_params)
        result = StatResult(
            't-test independent samples', 't-test_ind', 'stat_value', stat, pval
        )
    elif test_name == 't-test_welch':
        stat, pval = stats.ttest_ind(
            a=box_data1, b=box_data2, equal_var=False, **stats_params
        )
        result = StatResult(
            'Welch\'s t-test independent samples',
            't-test_welch',
            'stat_value',
            stat,
            pval,
        )
    elif test_name == 't-test_paired':
        stat, pval = stats.ttest_rel(a=box_data1, b=box_data2, **stats_params)
        result = StatResult(
            't-test paired samples', 't-test_rel', 'stat_value', stat, pval
        )
    elif test_name == 'Wilcoxon':
        zero_method_default = len(box_data1) <= 20 and "pratt" or "wilcox"
        zero_method = stats_params.get('zero_method', zero_method_default)
        if verbose >= 1:
            print("Using zero_method ", zero_method)
        stat, pval = stats.wilcoxon(
            box_data1, box_data2, zero_method=zero_method, **stats_params
        )
        result = StatResult(
            'Wilcoxon test (paired samples)', 'Wilcoxon', 'stat_value', stat, pval
        )
    elif test_name == 'Kruskal':
        stat, pval = stats.kruskal(box_data1, box_data2, **stats_params)
        result = StatResult(
            'Kruskal-Wallis paired samples', 'Kruskal', 'stat_value', stat, pval
        )
    else:
        result = StatResult(None, '', None, None, np.nan)

    # Optionally, run multiple comparisons correction
    if comparisons_correction is not None and comparisons_correction.type == 0:
        result.pval = comparisons_correction(result.pval, num_comparisons)
        result.correction_method = comparisons_correction.name

    return result

def pval_annotation_text(result: Union[List[StatResult], StatResult], pvalue_thresholds):
    single_value = False

    if isinstance(result, list):
        x1_pval = np.array([res.pval for res in result])
        x1_corr_signif = [res.corrected_significance for res in result]
    else:
        x1_pval = np.array([result.pval])
        x1_corr_signif = [result.corrected_significance]
        single_value = True

    # Sort the threshold array
    pvalue_thresholds = pd.DataFrame(pvalue_thresholds).sort_values(by=0, ascending=False).values
    x_annot = pd.Series(["" for _ in range(len(x1_pval))])

    for i in range(0, len(pvalue_thresholds)):

        if i < len(pvalue_thresholds) - 1:
            condition = (x1_pval <= pvalue_thresholds[i][0]) & (pvalue_thresholds[i + 1][0] < x1_pval)
            x_annot[condition] = pvalue_thresholds[i][1]

        else:
            condition = x1_pval < pvalue_thresholds[i][0]
            x_annot[condition] = pvalue_thresholds[i][1]

    x_annot = pd.Series(["ns" if corr_signif is False else star
                         for star, corr_signif in zip(x_annot, x1_corr_signif)])

    return x_annot if not single_value else x_annot.iloc[0]

def simple_text(result: StatResult, pvalue_format, pvalue_thresholds, test_short_name=None):
    """
    Generates simple text for test name and pvalue
    :param result: StatResult instance
    :param pvalue_format: format string for pvalue
    :param test_short_name: Short name of test to show
    :param pvalue_thresholds: String to display per pvalue range
    :return: simple annotation
    """
    # Sort thresholds
    thresholds = sorted(pvalue_thresholds, key=lambda x: x[0])

    # Test name if passed
    text = test_short_name and test_short_name + " " or ""

    for threshold in thresholds:
        if result.pval < threshold[0]:
            pval_text = "p ≤ {}".format(threshold[1])
            break
    else:
        pval_text = "p = {}".format(pvalue_format).format(result.pval)

    return text + pval_text + result.significance_suffix

def add_stat_annotation(ax, plot='boxplot',
                        data=None, x=None, y=None, hue=None, units=None, order=None,
                        hue_order=None, box_pairs=None, width=0.8,
                        perform_stat_test=True,
                        pvalues=None, test_short_name=None,
                        test=None, text_format='star', pvalue_format_string=DEFAULT,
                        text_annot_custom=None,
                        loc='inside', show_test_name=True,
                        pvalue_thresholds=DEFAULT, stats_params: dict = None,
                        comparisons_correction='bonferroni', num_comparisons='auto',
                        use_fixed_offset=False, line_offset_to_box=None,
                        line_offset=None, line_height=0.02, text_offset=1,
                        color='0.2', linewidth=1.5,
                        fontsize='medium', verbose=1):
    """
    Optionally computes statistical test between pairs of data series, and add statistical annotation on top
    of the boxes/bars. The same exact arguments `data`, `x`, `y`, `hue`, `order`, `width`,
    `hue_order` (and `units`) as in the seaborn boxplot/barplot function must be passed to this function.

    This function works in one of the two following modes:
    a) `perform_stat_test` is True: statistical test as given by argument `test` is performed.
       The `test_short_name` argument can be used to customize what appears before the pvalues
    b) `perform_stat_test` is False: no statistical test is performed, list of custom p-values `pvalues` are
       used for each pair of boxes. The `test_short_name` argument is then used as the name of the
       custom statistical test.

    :param plot: type of the plot, one of 'boxplot' or 'barplot'.
    :param line_height: in axes fraction coordinates
    :param text_offset: in points
    :param box_pairs: can be of either form: For non-grouped boxplot: `[(cat1, cat2), (cat3, cat4)]`. For boxplot grouped by hue: `[((cat1, hue1), (cat2, hue2)), ((cat3, hue3), (cat4, hue4))]`
    :param pvalue_format_string: defaults to `"{.3e}"`
    :param pvalue_thresholds: list of lists, or tuples. Default is: For "star" text_format: `[[1e-4, "****"], [1e-3, "***"], [1e-2, "**"], [0.05, "*"], [1, "ns"]]`. For "simple" text_format : `[[1e-5, "1e-5"], [1e-4, "1e-4"], [1e-3, "0.001"], [1e-2, "0.01"]]`
    :param pvalues: list or array of p-values for each box pair comparison.
    :param stats_params: Parameters for statistical test functions.
    :param comparisons_correction: Method for multiple comparisons correction.
        One of `statsmodel` `multipletests` methods (w/ default FWER), or a ComparisonCorrection instance.
    :param num_comparisons: Override number of comparisons otherwise calculated with number of box_pairs
    """

    def find_x_position_box(b_plotter, boxName):
        """
        boxName can be either a name "cat" or a tuple ("cat", "hue")
        """
        if b_plotter.plot_hues is None:
            cat = boxName
            hue_offset = 0
        else:
            cat = boxName[0]
            hue_level = boxName[1]
            hue_offset = b_plotter.hue_offsets[
                b_plotter.hue_names.index(hue_level)]

        group_pos = b_plotter.group_names.index(cat)
        box_pos = group_pos + hue_offset
        return box_pos

    def get_box_data(b_plotter, boxName):
        """
        boxName can be either a name "cat" or a tuple ("cat", "hue")

        Here we really have to duplicate seaborn code, because there is not
        direct access to the box_data in the BoxPlotter class.
        """
        cat = b_plotter.plot_hues is None and boxName or boxName[0]

        index = b_plotter.group_names.index(cat)
        group_data = b_plotter.plot_data[index]

        if b_plotter.plot_hues is None:
            # Draw a single box or a set of boxes
            # with a single level of grouping
            box_data = remove_na(group_data)
        else:
            hue_level = boxName[1]
            hue_mask = b_plotter.plot_hues[index] == hue_level
            box_data = remove_na(group_data[hue_mask])

        return box_data

    # Set default values if necessary
    if pvalue_format_string is DEFAULT:
        pvalue_format_string = '{:.3e}'
        simple_format_string = '{:.2f}'
    else:
        simple_format_string = pvalue_format_string

    if pvalue_thresholds is DEFAULT:
        if text_format == "star":
            pvalue_thresholds = [[1e-4, "****"], [1e-3, "***"],
                                 [1e-2, "**"], [0.05, "*"], [1, "ns"]]
        else:
            pvalue_thresholds = [[1e-5, "1e-5"], [1e-4, "1e-4"],
                                 [1e-3, "0.001"], [1e-2, "0.01"]]

    if stats_params is None:
        stats_params = dict()

    fig = plt.gcf()

    # Validate arguments
    if perform_stat_test:
        if test is None:
            raise ValueError("If `perform_stat_test` is True, `test` must be specified.")
        if pvalues is not None or test_short_name is not None:
            raise ValueError("If `perform_stat_test` is True, custom `pvalues` "
                             "or `test_short_name` must be `None`.")
        valid_list = ['t-test_ind', 't-test_welch', 't-test_paired',
                      'Mann-Whitney', 'Mann-Whitney-gt', 'Mann-Whitney-ls',
                      'Levene', 'Wilcoxon', 'Kruskal']
        if test not in valid_list:
            raise ValueError("test value should be one of the following: {}."
                             .format(', '.join(valid_list)))
    else:
        if pvalues is None:
            raise ValueError("If `perform_stat_test` is False, custom `pvalues` must be specified.")
        if test is not None:
            raise ValueError("If `perform_stat_test` is False, `test` must be None.")
        if len(pvalues) != len(box_pairs):
            raise ValueError("`pvalues` should be of the same length as `box_pairs`.")

    if text_annot_custom is not None and len(text_annot_custom) != len(box_pairs):
        raise ValueError("`text_annot_custom` should be of same length as `box_pairs`.")

    assert_is_in(
        loc, ['inside', 'outside'], label='argument `loc`'
    )
    assert_is_in(
        text_format,
        ['full', 'simple', 'star'],
        label='argument `text_format`'
    )

    # Comparisons correction
    if comparisons_correction is None:
        pass
    elif isinstance(comparisons_correction, str):
        assert_valid_correction_name(comparisons_correction)
        comparisons_correction = ComparisonsCorrection(comparisons_correction)
    elif not(isinstance(comparisons_correction, ComparisonsCorrection)):
            raise ValueError("comparisons_correction must be a statmodels "
                             "method name or a ComparisonCorrection instance")

    if verbose >= 1 and text_format == 'star':
        print("p-value annotation legend:")
        pvalue_thresholds = pd.DataFrame(pvalue_thresholds).sort_values(by=0, ascending=False).values
        for i in range(0, len(pvalue_thresholds)):
            if i < len(pvalue_thresholds) - 1:
                print('{}: {:.2e} < p <= {:.2e}'.format(pvalue_thresholds[i][1],
                                                        pvalue_thresholds[i + 1][0],
                                                        pvalue_thresholds[i][0]))
            else:
                print('{}: p <= {:.2e}'.format(pvalue_thresholds[i][1], pvalue_thresholds[i][0]))
        print()

    ylim = ax.get_ylim()
    yrange = ylim[1] - ylim[0]

    if line_offset is None:
        if loc == 'inside':
            line_offset = 0.05
            if line_offset_to_box is None:
                line_offset_to_box = 0.06
        # 'outside', see valid_list
        else:
            line_offset = 0.03
            if line_offset_to_box is None:
                line_offset_to_box = line_offset
    else:
        if loc == 'inside':
            if line_offset_to_box is None:
                line_offset_to_box = 0.06
        elif loc == 'outside':
            line_offset_to_box = line_offset
    y_offset = line_offset * yrange
    y_offset_to_box = line_offset_to_box * yrange

    if plot == 'boxplot':
        # Create the same plotter object as seaborn's boxplot
        box_plotter = sns.categorical._BoxPlotter(
            x, y, hue, data, order, hue_order, orient=None, width=width, color=None,
            palette=None, saturation=.75, dodge=True, fliersize=5, linewidth=None)

    elif plot == 'barplot':
        # Create the same plotter object as seaborn's barplot
        box_plotter = sns.categorical._BarPlotter(
            x, y, hue, data, order, hue_order,
            estimator=np.mean, ci=95, n_boot=1000, units=None,
            orient=None, color=None, palette=None, saturation=.75,
            errcolor=".26", errwidth=None, capsize=None, dodge=True)

    # Build the list of box data structures with the x and ymax positions
    group_names = box_plotter.group_names
    hue_names = box_plotter.hue_names
    if box_plotter.plot_hues is None:
        box_names = group_names
        labels = box_names
    else:
        box_names = [(group_name, hue_name) for group_name in group_names for hue_name in hue_names]
        labels = ['{}_{}'.format(group_name, hue_name) for (group_name, hue_name) in box_names]

    box_structs = [
        {
            'box':      box_names[i],
            'label':    labels[i],
            'x':        find_x_position_box(box_plotter, box_names[i]),
            'box_data': get_box_data(box_plotter, box_names[i]),
            'ymax':     (np.amax(get_box_data(box_plotter, box_names[i]))
                         if len(get_box_data(box_plotter, box_names[i])) > 0
                         else np.nan)
        } for i in range(len(box_names))]

    # Sort the box data structures by position along the x axis
    box_structs = sorted(box_structs, key=lambda a: a['x'])
    # Add the index position in the list of boxes along the x axis
    box_structs = [dict(box_struct, xi=i) for i, box_struct in enumerate(box_structs)]
    # Same data structure list with access key by box name
    box_structs_dic = {box_struct['box']: box_struct for box_struct in box_structs}

    # Build the list of box data structure pairs
    box_struct_pairs = []
    for i_box_pair, (box1, box2) in enumerate(box_pairs):
        valid = box1 in box_names and box2 in box_names
        if not valid:
            raise ValueError("box_pairs contains an invalid box pair.")
            pass
        # i_box_pair will keep track of the original order of the box pairs.
        box_struct1 = dict(box_structs_dic[box1], i_box_pair=i_box_pair)
        box_struct2 = dict(box_structs_dic[box2], i_box_pair=i_box_pair)
        if box_struct1['x'] <= box_struct2['x']:
            pair = (box_struct1, box_struct2)
        else:
            pair = (box_struct2, box_struct1)
        box_struct_pairs.append(pair)

    # Draw first the annotations with the shortest between-boxes distance, in order to reduce
    # overlapping between annotations.
    box_struct_pairs = sorted(box_struct_pairs, key=lambda a: abs(a[1]['x'] - a[0]['x']))

    # Build array that contains the x and y_max position of the highest annotation or box data at
    # a given x position, and also keeps track of the number of stacked annotations.
    # This array will be updated when a new annotation is drawn.
    y_stack_arr = np.array([[box_struct['x'] for box_struct in box_structs],
                            [box_struct['ymax'] for box_struct in box_structs],
                            [0 for _ in range(len(box_structs))]])
    if loc == 'outside':
        y_stack_arr[1, :] = ylim[1]
    ann_list = []
    test_result_list = []
    ymaxs = []
    y_stack = []

    # fetch results of all tests
    for box_struct1, box_struct2 in box_struct_pairs:
        box1 = box_struct1['box']
        box2 = box_struct2['box']
        box_data1 = box_struct1['box_data']
        box_data2 = box_struct2['box_data']
        i_box_pair = box_struct1['i_box_pair']

        if perform_stat_test:
            result = stat_test(
                box_data1,
                box_data2,
                test,
                comparisons_correction,
                num_comparisons != "auto" and num_comparisons or len(box_struct_pairs),
                verbose,
                **stats_params
            )
        else:
            test_short_name = test_short_name if test_short_name is not None else ''
            result = StatResult(
                'Custom statistical test',
                test_short_name,
                None,
                None,
                pvalues[i_box_pair]
            )
        result.box1 = box1
        result.box2 = box2

        test_result_list.append(result)

    # Perform other types of correction methods for multiple testing
    if comparisons_correction is not None:
        corr_name = comparisons_correction.name

        # If correction is applied to a set of pvalues
        if comparisons_correction.type == 1:
            original_pvalues = [result.pval for result in test_result_list]

            significant_pvalues = comparisons_correction(original_pvalues)

            for is_significant, result in zip(significant_pvalues, test_result_list):
                result.correction_method = corr_name
                result.corrected_significance = is_significant

        # If correction is applied per pvalue, just compare with alpha
        else:
            alpha = comparisons_correction.alpha
            for result in test_result_list:
                result.correction_method = corr_name
                result.corrected_significance = result.pval < alpha

    # Then annotate
    for box_structs, result in zip(box_struct_pairs, test_result_list):
        x1 = box_structs[0]['x']
        x2 = box_structs[1]['x']
        xi1 = box_structs[0]['xi']
        xi2 = box_structs[1]['xi']
        label1 = box_structs[0]['label']
        label2 = box_structs[1]['label']
        # ymax1 = box_structs[0]['ymax']  # Not used, so do not assign them
        # ymax2 = box_structs[1]['ymax']
        i_box_pair = box_structs[0]['i_box_pair']
        if verbose >= 1:
            print("{} v.s. {}: {}".format(label1, label2, result.formatted_output))

        if text_annot_custom is not None:
            text = text_annot_custom[i_box_pair]
        else:
            if text_format == 'full':
                text = "{} p = {}{}".format('{}', pvalue_format_string, '{}').format(
                    result.test_short_name, result.pval, result.significance_suffix)
            elif text_format is None:
                text = None
            elif text_format is 'star':
                text = pval_annotation_text(result, pvalue_thresholds)
            elif text_format is 'simple':
                test_short_name = result.test_short_name if show_test_name else None
                text = simple_text(result, simple_format_string,
                                   pvalue_thresholds, test_short_name)

        # Find y maximum for all the y_stacks *in between* the box1 and the box2
        i_ymax_in_range_x1_x2 = xi1 + np.nanargmax(
            y_stack_arr[1, np.where((x1 <= y_stack_arr[0, :])
                                    & (y_stack_arr[0, :] <= x2))])
        ymax_in_range_x1_x2 = y_stack_arr[1, i_ymax_in_range_x1_x2]

        yref = ymax_in_range_x1_x2
        yref2 = yref

        # Choose the best offset depending on whether there is an annotation below
        # at the x position in the range [x1, x2] where the stack is the highest
        if y_stack_arr[2, i_ymax_in_range_x1_x2] == 0:
            # there is only a box below
            offset = y_offset_to_box
        else:
            # there is an annotation below
            offset = y_offset
        y = yref2 + offset
        h = line_height * yrange
        line_x, line_y = [x1, x1, x2, x2], [y, y + h, y + h, y]
        if loc == 'inside':
            ax.plot(line_x, line_y, lw=linewidth, c=color)
        elif loc == 'outside':
            line = lines.Line2D(line_x, line_y, lw=linewidth, c=color, transform=ax.transData)
            line.set_clip_on(False)
            ax.add_line(line)

        # why should we change here the ylim if at the very end we set it to the correct range????
        # ax.set_ylim((ylim[0], 1.1*(y + h)))

        if text is not None:
            ann = ax.annotate(
                text, xy=(np.mean([x1, x2]), y + h),
                xytext=(0, text_offset), textcoords='offset points',
                xycoords='data', ha='center', va='bottom',
                fontsize=fontsize, clip_on=False, annotation_clip=False)
            ann_list.append(ann)

            plt.draw()
            y_top_annot = None
            got_mpl_error = False
            if not use_fixed_offset:
                try:
                    bbox = ann.get_window_extent()
                    bbox_data = bbox.transformed(ax.transData.inverted())
                    y_top_annot = bbox_data.ymax
                except RuntimeError:
                    got_mpl_error = True

            if use_fixed_offset or got_mpl_error:
                if verbose >= 1:
                    print("Warning: cannot get the text bounding box. Falling back to a fixed"
                          " y offset. Layout may be not optimal.")
                # We will apply a fixed offset in points,
                # based on the font size of the annotation.
                fontsize_points = FontProperties(size='medium').get_size_in_points()
                offset_trans = mtransforms.offset_copy(
                    ax.transData, fig=fig, x=0,
                    y=1.0 * fontsize_points + text_offset, units='points')
                y_top_display = offset_trans.transform((0, y + h))
                y_top_annot = ax.transData.inverted().transform(y_top_display)[1]
        else:
            y_top_annot = y + h

        y_stack.append(y_top_annot)  # remark: y_stack is not really necessary if we have the stack_array
        ymaxs.append(max(y_stack))
        # Fill the highest y position of the annotation into the y_stack array
        # for all positions in the range x1 to x2
        y_stack_arr[1, (x1 <= y_stack_arr[0, :]) & (y_stack_arr[0, :] <= x2)] = y_top_annot
        # Increment the counter of annotations in the y_stack array
        y_stack_arr[2, xi1:xi2 + 1] = y_stack_arr[2, xi1:xi2 + 1] + 1

    y_stack_max = max(ymaxs)
    if loc == 'inside':
        ax.set_ylim((ylim[0], max(1.03 * y_stack_max, ylim[1])))
    elif loc == 'outside':
        ax.set_ylim((ylim[0], ylim[1]))

    return ax, test_result_list


# Version of Python 2.7.16
# Function for add 1.0 to list if is minor than 5

def add_note(largo,lista):
    while largo < 5 :
        lista.append(1.0)
        largo += 1
    return lista

def main():
    # Open file with notes, this file is a .txt and is open read mode "r"
    file = open("/Users/rootdrigo/Repositories/python_scripts/Script_02/notes.txt", "r")
    # Open o create file for save results, mode "w"
    file_result = open("/Users/rootdrigo/Repositories/python_scripts/Script_02/result.txt", "w")

    # loops for read line by line
    for line in file:
        # Separate elements of list by ","
        valores=line.strip().split(",")
        # trasform to float
        valoresInt=list(map(float,valores[1:]))
        # calculate lenght of list
        largo=len(valoresInt)
        # check lenght of list, call to function "add_note"
        listaInt = add_note(largo,valoresInt)
        # sum notes
        sumaNotas=sum(listaInt)
        # media
        media=sumaNotas/len(listaInt)
        # validate approved or reprobate
        aprobado="Aprobado" if media>=4 else "Reprobado"
        # Imprime result to file result.txt
        file_result.write("Alumno: {} - Nota media: {} - {} \n".format(valores[0], media, aprobado))

    # close both files
    file_result.close()
    file.close()

if __name__ == "__main__":
    main()


import socket
import pickle
import argparse
from tictactoe import TicTacToe
from minmax import MinMax
import struct
from msg_utils import send_msg, recv_msg

def setup(socket, game):
    send_msg(socket, pickle.dumps(game))
    send_msg(socket, pickle.dumps('AI or Player (A/P)?'))
    send_msg(socket, pickle.dumps('Choose X or O'))

    ai = pickle.loads(recv_msg(socket))
    player = pickle.loads(recv_msg(socket))
    return (ai, player)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-P', '--PORT', type=int, default=8888)
    args = parser.parse_args()

    HOST = socket.gethostbyname('0.0.0.0')
    PORT = args.PORT

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        print(HOST, PORT)
        s.listen()
        conn, addr = s.accept()
        with conn:
            print("Connecting address: ", addr)
            game = TicTacToe()
            ai, player2 = setup(conn, game)
            pc = None
            print(player2)

            if player2.upper() == 'X':
                player1 = 'O'
                position = pickle.loads(recv_msg(conn))

                game.move(position, 'X')
                print(game)
            else:
                player1 = 'X'

            if ai.upper() == 'A':
                pc = MinMax(game, (player1, player2))

            while not game.is_over():
                print(game)
                if pc is not None:
                    position_player = pc.get_decision(game, pc.players)
                    game.move(position_player, player1)
                else:
                    position_player = game.turn(player1)

                send_msg(conn, pickle.dumps(position_player))
                print(game)
                if game.is_over():
                    break

                position_enemy = pickle.loads(recv_msg(conn))
                game.move(position_enemy, player2)

            print(game.result())


import pygame
from bar import *
import random

class Bars(object):
    def __init__(self, num_bars):
        self.bars = []
        self.start_x = 20
        self.start_y = 0
        self.num_bars = num_bars
        self.color = None
        self.create_bars()
        self.current = None
        self.clock = pygame.time.Clock()
        self.fps = 60
        self.sorted = []

    def create_bars(self):
        for i in range(self.num_bars):
            self.bars.append(Bar(self.start_x, random.randrange(100,720,10), 2))
            self.start_x += 20

    def bubble_sort(self, window):
        for i in range(len(self.bars)):

            for j in range(len(self.bars)-i-1):
                if self.bars[j].height > self.bars[j+1].height:
                    # highlight the current bars
                    self.bars[j].highlighted = True
                    self.bars[j+1].highlighted = True
                    # swap current height if greater
                    temp = self.bars[j].height
                    self.bars[j].height = self.bars[j+1].height
                    self.bars[j+1].height = temp
                self.clock.tick(self.fps)
                window.fill((0,0,0))
                for bar in self.bars:
                    bar.render(window)
                pygame.display.update()
                # reset current highlighted bars
                self.bars[j].highlighted = False
                self.bars[j+1].highlighted = False
        # pause after completed to see sorted list
        pygame.time.delay(3000)

def main():
    pygame.init()
    window = pygame.display.set_mode((1280, 720)) # changed from 1280 width
    pygame.display.set_caption("Bubble Sort Visualization")
    clock = pygame.time.Clock()
    test_bars = Bars(64)
    test_bars.bubble_sort(window)
    pygame.quit()

if __name__ == '__main__':
    main()


from pyx import *
import datetime
import math

c = canvas.canvas()

czarna_obwodka = path.circle(0, 0, 10.3)
szara_obwodka = path.circle(0, 0, 11)
wypelnienie_srodka = path.circle(0, 0, 9.4)
os_srodka = path.circle(0, 0, 0.5)
przedzialek = path.line(0, 0, 0, 9.9)

for tick in range(0,60):
    if tick % 5 == 0:
        c.stroke(przedzialek, [style.linewidth(0.3), trafo.rotate(6*tick)])
    else:
        c.stroke(przedzialek, [trafo.rotate(6*tick)])

c.stroke(wypelnienie_srodka,[ deco.filled([color.grey(1)]), color.cmyk.White])

katy = [x*(360/12)+60 for x in range(12,0,-1)]
cyfry = [x for x in range(1,13)]

radius = 8
for cy, k  in zip(cyfry, katy):
    c.text(0, 0, str(cy), [trafo.translate(radius*math.cos(math.radians(k))-0.5,
                            radius*math.sin(math.radians(k))-0.5),
                            trafo.scale(sx=5.5, sy=5.5) ])

aktualny_czas = datetime.datetime.now().time()
wskazowka_godzina = path.line(0, 0, 0, 8.5)
wskazowka_minuta = path.line(0, 0, 0, 10.5)

c.stroke(wskazowka_godzina,[style.linewidth(0.6), trafo.translate(0, -2),
                   trafo.rotate(-30*(aktualny_czas.hour-12))])
c.stroke(wskazowka_minuta,[style.linewidth(0.2), trafo.translate(0,-2),
                  trafo.rotate(-5*aktualny_czas.minute)])
c.stroke(os_srodka, [deco.filled([color.rgb.black])])
c.stroke(szara_obwodka, [style.linewidth(1.6), color.cmyk.Gray])
c.stroke(czarna_obwodka, [style.linewidth(0.2)])

c.writePDFfile('zegar.pdf');


import string

from sklearn.base import BaseEstimator, TransformerMixin
from src.twitter.preprocessing.common_regex import re_number, re_url

class TextCleaner(BaseEstimator, TransformerMixin):

    def __init__(self):
        nones = [None for _ in string.punctuation]
        d = dict(zip(string.punctuation, nones))
        self.trans_table = string.punctuation.maketrans(d)

    def clean_text(self, tokens):
        for i, w in enumerate(tokens):
            if re_number.match(w):
                tokens[i] = w.translate(self.trans_table)
            elif re_url.match(w):
                tokens[i] = '_LINK_'
            else:
                continue
        return tokens

    def transform(self, X):
        for i, item in enumerate(X):
            tokens = item.split()
            X[i] = " ".join(self.clean_text(tokens))

        return X

    def fit(self, X, y=None):
        # self.transform(X)
        return self


#Import tensorlflow and keras libraries that are needed
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, EarlyStopping
tf.__version__

#the size of the image is assigned to the varibale size
size = 150

#the modifications for the images in the training set
train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)

#assign the variable training_set with the data stored from this path
training_set = train_datagen.flow_from_directory('3Class/training_set', target_size = (size, size), batch_size = 2, class_mode = 'categorical', color_mode='grayscale')

#No modifications are performed on the test_set
test_datagen = ImageDataGenerator(rescale = 1./255)

#assign the variable test_set with the data stored from this path
test_set = test_datagen.flow_from_directory('3Class/test_set', target_size = (size, size), batch_size = 2, class_mode = 'categorical', color_mode='grayscale')

#Initialize the model with the Sequential model
model = tf.keras.models.Sequential()

#Convolutional Layer #1
model.add(tf.keras.layers.Conv2D(filters=24, kernel_size=4, activation='relu', input_shape=[size, size,1]))
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

#Convolutional Layer #2
model.add(tf.keras.layers.Conv2D(filters=24, kernel_size=4, activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

#Array is Flattened
model.add(tf.keras.layers.Flatten())

#The number of nodes in the ANN
model.add(tf.keras.layers.Dense(units=32, activation='relu'))

#Output Nodes
model.add(tf.keras.layers.Dense(units=3, activation='softmax'))

#Optimizer is determined
opt = tf.keras.optimizers.Adamax()

#Compiling the model
model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy', 'AUC', 'Precision', 'Recall', 'TruePositives', 'TrueNegatives', 'FalsePositives', 'FalseNegatives',])

#Print the Paramaters and Summary of the Model
print(model.summary())

#Early Stopping is assigned to the variable "es"
es = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=4)

#The model is fitted to the data
model.fit(x = training_set, validation_data = test_set, epochs = 54, verbose=2, callbacks=[es])

#The model is saved for further use with prediction or statistics
model.save('saved_model/3Class')


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        ans, level = [], [root]
        while root and level:
            ans.append([n.val for n in level])
            next_level = [(n.left, n.right) for n in level]
            level = [leaf for LR in next_level for leaf in LR if leaf]
        return ans[::-1]

LeetCode/107. Binary Tree Level Order Traversal II/solution.py


import os
import numpy as np
import xgboost as xgb
from imblearn.over_sampling import SMOTE
from metrics_list import metric_list

path = "High_IR_Data_cross_folder"
dirs = os.listdir(path) #Get files in the folder
First_line = True

for Dir in dirs:
    print("Data Set Name: ", Dir)
    dir_path = path + "/" + Dir
    files = os.listdir(dir_path)  # Get files in the folder

    methods = ["xGBoost", "SMOTE", "SMOTE-SMOTE"]
    for m in methods:
        Num_Cross_Folders = 5
        ml_record = metric_list(np.array([1]), np.array([1]), Num_Cross_Folders)
        i = 0
        for file in files:
            name = dir_path + '/' + file
            r = np.load(name)

            Positive_Features_train = r["P_F_tr"]
            Num_Positive_train = Positive_Features_train.shape[0]
            Positive_Labels_train = np.linspace(1, 1, Num_Positive_train)

            Positive_Features_test = r["P_F_te"]
            Num_Positive_test = Positive_Features_test.shape[0]
            Positive_Labels_test = np.linspace(1, 1, Num_Positive_test)

            Negative_Features_train = r["N_F_tr"]
            Num_Negative_train = Negative_Features_train.shape[0]
            Negative_Labels_train = np.linspace(0, 0, Num_Negative_train)

            Negative_Features_test = r["N_F_te"]
            Num_Negative_test = Negative_Features_test.shape[0]
            Negative_Labels_test = np.linspace(0, 0, Num_Negative_test)

            print(i, " folder; ", "Po_tr: ", Num_Positive_train, "Ne_tr: ", Num_Negative_train,
                  "Po_te: ", Num_Positive_test, "Ne_te: ", Num_Negative_test)

            Features_train_o = np.concatenate((Positive_Features_train, Negative_Features_train))
            Labels_train_o = np.concatenate((Positive_Labels_train, Negative_Labels_train))
            #                print(Labels_train_o)
            Feature_test = np.concatenate((Positive_Features_test, Negative_Features_test))
            Label_test = np.concatenate((Positive_Labels_test, Negative_Labels_test))
            #                print(Label_test)

            if m == "xGBoost":
                Feature_train = Features_train_o
                Label_train = Labels_train_o
            elif m == "SMOTE":
                sm = SMOTE()
                Feature_train, Label_train = sm.fit_sample(Features_train_o, Labels_train_o)
            elif m == "SMOTE-SMOTE":
                expand_rate_for_Majority = 0.5
                num_create_samples = int(np.ceil(Num_Negative_train * expand_rate_for_Majority))
                em = SMOTE(ratio={0: num_create_samples + Num_Negative_train})
                Re_Features_o, Labels_o = em.fit_sample(Features_train_o, Labels_train_o)
                sm = SMOTE()
                Feature_train, Label_train = sm.fit_sample(Re_Features_o, Labels_o)

            clf = xgb.XGBClassifier()
            clf.fit(Feature_train, Label_train)
            Label_predict = clf.predict(Feature_test)
            ml_record.measure(0, 0, i, Label_test, Label_predict)
            Label_score = clf.predict_proba(Feature_test)
            ml_record.auc_measure(0, 0, i, Label_test, Label_score[:,1])

            i += 1

        file_wirte = "Result_xGBoost.txt"
        ml_record.output(file_wirte, m, Dir)


# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return a list of lists of integers

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

    def dfs(self, node, depth):
        if node is None:
            return
        if depth in self.mapper:
            self.mapper[depth].append(node.val)
        else:
            self.mapper[depth] = [ node.val ]
        self.dfs(node.left, depth + 1)
        self.dfs(node.right, depth + 1)

    def zigzagLevelOrder(self, root):
        self.dfs(root, 0)
        res = []
        for key, level in self.mapper.items():
            if key % 2 == 0:
                res.append(level)
            else:
                res.append(list(reversed(level)))
        return res


#This program parse through the database and pick up the arguments which occurs 30-3000 times
from kyotocabinet import *
DBn = DB()
DBn.open('DBn.kch',DB.OREADER)
lines = open('test.tsv')
words = open('words.txt','w')

s = set()
for line in lines:
    word = line.split('\t')[2] #get the argument
    if word == '':
        word = '[B]'
    s.add(word)

for word in s:
    value = int(DBn.get(word))
    if value > 30 and value < 3000:
        words.write(word + '\n')


import time
import math
from mpi4py import MPI
import numpy as np
import sys

def remove_multiples(sieve, k):
	n = len(sieve) # Max number to calculate
	i = sieve.index(1) # Iterator starting at first 1

	# --------------------------------
	# TODO: optimized version
	# if rank==0:
	# 	data = np.array_split(sieve, comm.size)
	# else:
	# 	data = None
	#
	# b_data = comm.scatter(data, root=0)
	# print(rank,b_data)
	# sys.stdout.flush()
	#
	# while i*(rank+1)*k<n:
	# 	b_data[i*(rank+1)*k] = 0
	# 	i+=1
	# --------------------------------

	# --------------------------------
	# # Working prototype 1 (non-optimized)

	if rank==0:
		data = sieve
	else:
		data = None
	b_data = comm.bcast(data, root=0)
	while i*(rank+1)*k<n:
		b_data[i*(rank+1)*k] = 0
		i+=1
	# --------------------------------

	return b_data

def run(n):
	sieve = [1]*(n+1) # Initialize sieve
	sieve[0], sieve[1] = 0, 0 # 0 & 1 are not prime numbers
	k = 2
	while k<=math.sqrt(n)+1:
		# print(rank,"executing run",k)
		sieve = remove_multiples(sieve, k)
		k+=1
		try:
			k = sieve[k:].index(1)+k
		except:
			break
	return sieve

def next_k(sieve, k):
	if k==0:
		return 2
	k+=1
	k = sieve[k:].index(1)+k
	return k

def print_sieve(sieve):
	result = []
	for i, x in enumerate(sieve):
		if(x==1):
			result.append(i)
	print('result:',result)

# Driver code
if __name__ == '__main__':
	n = int(sys.argv[1])
	sieve = [1]*(n+1) # Initialize sieve
	sieve[0], sieve[1] = 0, 0 # 0 & 1 are not prime numbers
	tt = 0

	comm = MPI.COMM_WORLD
	size = comm.Get_size()
	rank = comm.Get_rank()

	# Initialize sieve
	k = 0

	if rank==0:
		print("Running with n =", n)
		time_start = time.time()
		sys.stdout.flush()

	while k<math.sqrt(n):
		if rank==0:
			k = next_k(sieve,k)
		k = comm.bcast(k, root=0)
		# print(rank,k)
		# print(rank,k)
		# sys.stdout.flush()
		# comm.Barrier()

		result = remove_multiples(sieve,k)
		# comm.Barrier()

		gathered = comm.gather(result, root=0)

		if rank == 0:
			ts = time.time()
			result = []
			for x in range(0, len(gathered[0])):
				add = 1
				for t in range(0, size):
					if gathered[t][x] == 0:
						add = 0
					if (add == 0):
						break
				result.insert(len(result), add)
			tt += time.time() - ts

		sieve = comm.bcast(result, root=0)

	if rank==0:
		# sieve = run(n)
		time_taken = round(time.time() - time_start, 2)
		print("Total execution time:",str(time_taken)+'s')
		print("Waarvan domme functie time:", round(tt,2),'s')
		print('result:',(sieve))
		print_sieve(sieve)


import json
import socket
import time
from io import BytesIO

from .analyzer import Analyzer

try:
    import cPickle as pickle
    USING_CPICKLE = True
except:
    import pickle
    USING_CPICKLE = False

# This whole song & dance is due to pickle being insecure
# yet performance critical for carbon. We leave the insecure
# mode (which is faster) as an option (USE_INSECURE_UNPICKLER).
# The SafeUnpickler classes were largely derived from
# http://nadiana.com/python-pickle-insecure
if USING_CPICKLE:
    class SafeUnpickler(object):
        PICKLE_SAFE = {
            'copy_reg': set(['_reconstructor']),
            '__builtin__': set(['object']),
        }

        @classmethod
        def find_class(cls, module, name):
            if module not in cls.PICKLE_SAFE:
                raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module)
            __import__(module)
            mod = sys.modules[module]
            if name not in cls.PICKLE_SAFE[module]:
                raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name)
            return getattr(mod, name)

        @classmethod
        def loads(cls, pickle_string):
            pickle_obj = pickle.Unpickler(BytesIO(pickle_string))
            pickle_obj.find_global = cls.find_class
            return pickle_obj.load()

else:
    class SafeUnpickler(pickle.Unpickler):
        PICKLE_SAFE = {
            'copy_reg': set(['_reconstructor']),
            '__builtin__': set(['object']),
        }

        def find_class(self, module, name):
            if module not in self.PICKLE_SAFE:
                raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module)
            __import__(module)
            mod = sys.modules[module]
            if name not in self.PICKLE_SAFE[module]:
                raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name)
            return getattr(mod, name)

        @classmethod
        def loads(cls, pickle_string):
            return cls(BytesIO(pickle_string)).load()
# //SafeUnpickler

def send_graphite_metric(graphite_host, graphite_port, name, value):
    if graphite:
        try:
            sock = socket.socket()
            sock.connect((graphite_host, graphite_port))
            sock.sendall('%s %s %i\n' % (name, value, time.time()))
            sock.close()
            return True
        except socket.error:
            logger.error("Can't connect to Graphite at {}:{}".format(graphite_host, graphite_port))
    return False

def check_alert(api, args, metric, trigger):
    print("Verifying alerts for: {}".format(metric))

    print("rules: {}".format(api.get_alerts_rules()))
    print("settings: {}".format(api.get_alerts_settings()))

    a = Analyzer(api, args)
    for t in a.alert(metric, (time.time(), 0), {}, check=True, trigger=trigger):
        print(t)

def check_anomalies(api, timestamp=True):
    for anomaly in api.get_anomalies(timestamp):
        if timestamp:
            print("{0}\t{1}".format(time.ctime(anomaly[1]), anomaly[0]))
        else:
            print("{0}".format(anomaly[0]))

def settings(api, import_file=None, export=True):
    if import_file:
        with open(import_file) as f:
            api.import_settings(json.load(f))
    if export:
        print(json.dumps(api.export_settings(), indent=4, sort_keys=True))

def print_metric_data(api, metric, interval):
    data = api.get_metric_data(metric)
    if not data:
        print("data not found for {0}".format(args.metric))
        return

    length = len(data)
    start = time.ctime(int(data[0][0]))
    end = time.ctime(int(data[-1][0]))
    duration = (float(data[-1][0]) - float(data[0][0]))

    last = int(data[0][0]) - 10
    bad = 0
    missing = 0
    for d in data:
        delta = int(d[0]) - last
        if delta > interval:
            bad += 1
            missing += delta
        last = d[0]

    print("Metric Data")
    print("  Stats for {} (interval={}):".format(metric, interval))
    print("  Start time:         {}".format(start))
    print("  End time:           {}".format(end))
    print("  Duration:           {} hours".format(duration / 3600))
    print("  Missing datapoints: {}".format(bad))
    print("  Missing time:       {} seconds".format(missing))
    print("  Datapoints:         {}".format(length))
    print("  Min Datapoint:      {}".format(min(d[1] for d in data)))
    print("  Max Datapoint:      {}".format(max(d[1] for d in data)))
    print("  Ave Datapoint:      {}".format(sum(d[1] for d in data) / length))

def print_metric_info(api, metric):
    info = api.get_metric_info(metric)
    if info:
        print("Metric Info")
        for k, v in info.items():
            print("  {0}: {1}".format(k, v))

def print_metric_last_analyzed_results(api, metric):
    results = api.get_last_analyzed_results(metric)
    if results:
        print("Last Analyzed Results")
        for k, v in results.items():
            print("  {0}: {1}".format(k, v))

def print_metric_last_anomaly_results(api, metric):
    results = api.get_last_analyzed_results(metric)
    if results:
        print("Last Anomaly Results")
        for k, v in results.items():
            print("  {0}: {1}".format(k, v))

def check_metric(api, metric, interval):
    print_metric_data(api, metric, interval)
    print("")
    print_metric_info(api, metric)
    print("")
    print_metric_last_analyzed_results(api, metric)
    print("")
    print_metric_last_anomaly_results(api, metric)


#!/usr/local/bin/python3

# coding:utf-8

import time
import datetime
import requests
from scrapy.selector import Selector
import re

nums = 0
error_times = 0
lasttime = ''

def spider(erectDate,nothing,fex_sl,page):
	try:
		input = requests.post('http://srh.bankofchina.com/search/whpj/search.jsp', data = {'erectDate':erectDate, 'nothing':nothing, 'pjname':fex_sl, 'page':page})
	except:
		global error_times
		error_times += 1
		print("出现错误, 5秒后重试(第%i次)"%(error_times))
		time.sleep(5)
		spider(erectDate,nothing,fex_sl,page)

	else:
		body = input.text
		for row in range(2,22):
			rate_output = Selector(text=body).xpath('//tr[%i]/td[4]/text()' %(row)).extract()
			time_output = Selector(text=body).xpath('//tr[%i]/td[7]/text()' %(row)).extract()
			global lasttime
			try:
				if time_output[0] == lasttime:
					#print("重复数据")
					continue
				outrow = time_output[0]+","+rate_output[0]+"\n"
				out.write(outrow)
				#print(time_output[0]+','+rate_output[0])
				global nums
				nums += 1
				lasttime = time_output[0]
			except IndexError:
				break
		print("已抓取至第%i/%i页, %i条数据" %(page,pages,nums))

today_noFormat = datetime.date.today()
lastweek_noFormat = today_noFormat - datetime.timedelta(days=6)
today = str(today_noFormat)
lastweek = str(lastweek_noFormat)
print("---------------------------------------------------")
print("| 中行外汇牌价抓取脚本 | Made by Bob | 2018.09.19 |")
print("---------------------------------------------------")
fex = input("请选择要抓取的外汇种类:\n1.英镑\n2.美元\n3.澳元\n4.欧元\n")

while True:
	if fex == '1':
		fex_sl = 1314
		fex_name = 'GBP'
		break
	elif fex == '2':
		fex_sl = 1316
		fex_name = 'USD'
		break
	elif fex == '3':
		fex_sl = 1325
		fex_name = 'AUD'
		break
	elif fex == '4':
		fex_sl = 1326
		fex_name = 'EUR'
		break
	else:
		fex = input("输入有误,请重新选择:\n1.英镑\n2.美元\n3.澳元\n4.欧元\n")
if __name__ == '__main__':
	print("输入抓取汇率的起始日期(回车默认7天前:%s):"%(lastweek))
	erectDate = str(input())
	if not len(erectDate.strip()):
		erectDate = str(lastweek)

	print("输入抓取汇率的结束日期(回车默认今天:%s):"%(today))
	nothing = str(input())
	if not len(nothing.strip()):
		nothing = str(today)

	input = requests.post('http://srh.bankofchina.com/search/whpj/search.jsp', data = {'erectDate':erectDate, 'nothing':nothing, 'pjname':fex_sl, 'page':'1'})
	body = input.text
	searchOBJ = re.search(r'var m_nRecordCount = (.*);',body)
	pages = (int(searchOBJ.group(1))//20)+1

	out = open('/Users/bob/Desktop/[%s]%s_%s.csv'%(fex_name,erectDate,nothing),'a',newline='')
	for page in range(1,(pages+1)):
		spider(erectDate,nothing,fex_sl,page)

	out.close()
	print("")
	print('-' * 28,"\n")
	print("抓取完毕, 共%i条数据"%(nums))
	print("共出现错误%i次"%(error_times))
	print("结果已保存至 ~/Desktop/boc-s.csv")


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 15 09:40:10 2018

@author: ml17kfma
 """
#hello Khalaf Alrowily
y0 = 57
x0 = 57
print ("x0", x0)
print ("y0", y0)
y1 = 90
x1 = 90
print ("y1", y1)
print ("x1", x1)
import random
random_number = random.random()
print ("random_number", random_number)

if random_number < 0.5:
    y0 = y0 + 1
else:
    y0 = y0 - 1
print (y0)
if random_number < 0.5:
    x0 = x0 + 1
else:
    x0 = x0 - 1
print (x0)

#move x, y randomly to change values

dist = (((y0 - y1)**2 + (x0 - x1)**2)**0.5)
print (dist)


# -----------------------------------------------------------------------------
# Name:        wordstats
# Purpose:
#
# Author:
# Date:
# -----------------------------------------------------------------------------
"""
Docstring: Enter your one-line summary here

and your detailed description
"""
import string
# The following imports are needed for the draw_cloud function.
import tkinter
import tkinter.font
import random

# The draw_cloud function is only needed for the optional part:
# to generate a word cloud.
# You don't need to change it.
def draw_cloud(input_count, min_length = 0):
    """
    Generate a word cloud based on the input count dictionary specified.

    Parameters:
    input_count (dict): represents words and their corresponding counts.
    min_length (int):  optional - defaults to 0.
         minimum length of the words that will appear
         in the cloud representation.
    Only the 20 most common words (that satisfy the minimum length criteria)
    are included in the generated cloud.
    """
    root = tkinter.Tk()
    root.title("Word Cloud Fun")
    # filter the dictionary by word length
    filter_count = {
            word: input_count[word] for word in input_count
            if len(word) >= min_length}
    max_count = max(filter_count.values())
    ratio = 100 / max_count
    frame=tkinter.Frame(root)
    frame.grid()
    my_row = 0
    for word in sorted(filter_count, key=filter_count.get, reverse=True)[0:20]:
        color = '#' + str(hex(random.randint(256,4095)))[2:]
        word_font = tkinter.font.Font(size=int(filter_count[word]* ratio))
        label = tkinter.Label(frame, text = word, font = word_font, fg = color)
        label.grid(row = my_row % 5, column = my_row // 5 )
        my_row +=  1
    root.mainloop()

# Enter your own helper function definitions here

def count_words (filename):
    # build and return the dictionary for the given filename
    count_dict = {}
    with open(filename, 'r', encoding='utf-8') as my_file:
        for line in my_file:
            line = line.lower()
            for word in line.split():
                word = word.strip(string.punctuation + string.digits)
                if word in count_dict:
                    count_dict[word] += 1
                else:
                    count_dict[word] = 1
    return count_dict

def report(word_dict):
    # report on various statistics based on the given word count dictionary
    print("The longest word is: {}".format(max(word_dict, key=len)))

    print("The 5 most common words are:")
    sort_by_freq = sorted(word_dict, key=word_dict.get, reverse=True)
    for word in sort_by_freq[0:5]:
        if word in word_dict:
            print(word + ': ' + str(word_dict[word]))

    with open('out.txt', 'w', encoding='utf-8') as my_file:
        alpha_list = sorted(word_dict)
        for word in alpha_list:
            my_file.write('{}: {}'.format(word, word_dict[word]))

def main():
    # get the input filename and save it in a variable
    filename = input("Please enter your file name: ")
    # call count_words to build the dictionary for the given file
    # save the dictionary in the variable word_count
    word_count = count_words(filename)
    # call report to report on the contents of the dictionary
    report(word_count)
    # If you want to generate a word cloud, uncomment the line below.
    # draw_cloud(word_count)

if __name__ == '__main__':
    main()