import os

from sqlalchemy.exc import OperationalError

from metgem.workers.base import BaseWorker
from metgem.database import DataBaseBuilder

class ConvertDatabasesWorker(BaseWorker):

    def __init__(self, ids: list, output_path: str, input_path: str = None):
        super().__init__()
        self.ids = ids
        self.input_path = input_path if input_path is not None else output_path
        self.output_path = output_path
        self.iterative_update = False
        self.max = len(self.ids)
        self.desc = 'Converting databases...'

    def run(self):
        converted = {}
        for k in self.ids.keys():
            converted[k] = set()

        with DataBaseBuilder(os.path.join(self.input_path, 'spectra')) as db:
            for origin in self.ids:
                for i, (name, ids) in enumerate(self.ids[origin].items()):
                    filenames = []
                    for id_ in ids:
                        path = os.path.join(self.input_path, os.path.basename(id_)) if not os.path.isabs(id_) else id_
                        if os.path.exists(path):
                            filenames.append(path)

                    if not filenames:
                        continue

                    try:
                        db.add_bank(filenames, bank_name=origin + ' - ' + name)
                    except (OperationalError, NotImplementedError) as e:
                        self.error.emit(e)
                        return

                    self.updated.emit(i)
                    converted[origin].add(name)

        return converted


# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:24:54 2019

@author: Santosh
"""
from scipy.spatial import distance as dist
import cv2
import numpy as np
import imutils
def viewImage(image):
    cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
    cv2.imshow('Display', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

image = cv2.imread('Programs\Image\image10.jpeg')
image1 = image

hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
viewImage(hsv_img) ## 1

green_low = np.array([45 , 100, 50] )
green_high = np.array([75, 255, 255])
curr_mask = cv2.inRange(hsv_img, green_low, green_high)
hsv_img[curr_mask > 0] = ([75,255,200])
viewImage(hsv_img) ## 2
## converting the HSV image to Gray inorder to be able to apply
## contouring

RGB_again = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2RGB)
gray = cv2.cvtColor(RGB_again, cv2.COLOR_RGB2GRAY)
viewImage(gray) ## 3
gray = cv2.GaussianBlur(gray, (5, 5), 0)
viewImage(gray)
ret, threshold = cv2.threshold(gray, 100, 255, 0)
#viewImage(threshold)
#threshold = cv2.Canny(threshold, 150, 255)
#viewImage(threshold)
#threshold = cv2.dilate(threshold, None, iterations=1)
#viewImage(threshold)
#threshold = cv2.erode(threshold, None, iterations=1)

#viewImage(threshold) ## 4

contours =  cv2.findContours(threshold.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(contours)
count=1
for c in cnts:
    if cv2.contourArea(c) < 3000:
        continue
    epsilon = 0.02*cv2.arcLength(c,True)
    approx = cv2.approxPolyDP(c,epsilon,True)

    print(approx)
    for i in range(1, len(approx)):
        print(dist.euclidean(approx[i-1], approx[i]))
    print(dist.euclidean(approx[0], approx[-1]))
    print("Hey")
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    M = cv2.moments(c)
    cX = int((M["m10"] / M["m00"]))
    cY = int((M["m01"] / M["m00"]))
    #cv2.putText(image, count, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
    count=count+1
    viewImage(image)
    #cv2.waitKey(0)
#cv2.drawContours(image, contours, -1, (0, 0, 255), 3)
#viewImage(image) ## 5


import requests

def search_avatar(user):
    """
    Busca o avatar de um usuário no GitHub.
    :param user: str com o nome do usuário no GitHub.
    :return: str com o link do avatar.
    """
    url = f'https://api.github.com/users/{user}'
    resp = requests.get(url)
    return resp.json()['avatar_url']

if __name__ == '__main__':
    print(search_avatar('rousuy'))


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
import pandas as pd
import time

website_url = 'https://twitter.com/TwitterSupport/status/1578008801357209601'
chrome_driver_path = 'G:\\Downloads\\chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_path)
driver.get(website_url)
driver.maximize_window()

def get_tweet(element):
    try:
        user = element.find_element_by_xpath(".//span[contains(text(), '@')]").text
        text = element.find_element_by_xpath(".//div[@lang]").text
        tweets_data = [user, text]
    except:
        tweets_data = ['user', 'text']
    return tweets_data

user_data = []
text_data = []
tweet_ids = set()

scrolling = True

while scrolling:
    tweets = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.XPATH, '//article['
                                                                                '@role="article"]')))
    for tweet in tweets:
        tweet_list = get_tweet(tweet)
        tweet_id = ''.join(tweet_list)

        user_data.append(tweet_list[0])
        text_data.append(" ".join(tweet_list[1].split()))

    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
        time.sleep(2)
        new_height = driver.execute_script("return document.body.scrollHeight")
        # if new_height == last_height:
        #     scrolling = False
        #     break
        if len(user_data) > 100:
            scrolling = False
            break
        else:
            last_height = new_height
            break

driver.quit()

df_tweets = pd.DataFrame({'User': user_data, 'Tweet': text_data})
df_tweets.to_csv('tweets-infinite.csv', index=False)


import torch

class PadFunction(object):
    def __init__(self, pad_id=0):
        self.pad_id = pad_id

    def __call__(self, batch):
        return self._pad_fn(batch)

    def merge(self, sequences, pad_size=None):
        device = sequences[0].device
        lengths = [len(seq) for seq in sequences]
        if pad_size is None:
            pad_size = max(lengths)
        padded_seqs = torch.full((len(lengths), pad_size), self.pad_id, dtype=torch.long, device=device)
        for i, seq in enumerate(sequences):
            end = lengths[i]
            padded_seqs[i, :end] = seq[:end]
        return padded_seqs, lengths

    def make_mask(self, inputs, inputs_length):
        device = inputs.device
        max_len = inputs.shape[1]
        inputs_mask = (torch.arange(max_len).expand(len(inputs_length), max_len) < torch.tensor(inputs_length).unsqueeze(1)).to(device)
        return inputs_mask

    def _pad_fn(self, batch):
        # sort a list by sequence length (descending order) to use pack_padded_sequence
        # batch.sort(key=lambda x: len(x[0]), reverse=True)

        # seperate source and target sequences
        src_seqs, trg_seqs = tuple(zip(*batch))

        # merge sequences (from tuple of 1D tensor to 2D tensor)
        # pad_size = max([len(seq) for seq in src_seqs] + [len(seq) for seq in trg_seqs])
        pad_size = None
        src_seqs, src_lengths = self.merge(src_seqs, pad_size)
        trg_seqs, trg_lengths = self.merge(trg_seqs, pad_size)

        source_tokens = {
            'token_ids': src_seqs,
            'mask': self.make_mask(src_seqs, src_lengths)
        }

        target_tokens = {
            'token_ids': trg_seqs,
            'mask': self.make_mask(trg_seqs, trg_lengths)
        }
        return source_tokens, target_tokens


import re
import gzip
import numpy as np
from collections import OrderedDict
from astropy.io import registry
from textwrap import dedent

from .photosphere import Photosphere
from grok.utils import periodic_table, safe_open

def parse_meta(contents):
    # Documented at https://marcs.astro.uu.se/krz_format.html

    # Check if spherical or not. Could also do this by checking for model type:
    # MODEL TYPE = 3 (spherical)
    # MODEL TYPE = 0 (plane parallel)
    spherical_pattern = ".+SPHERICAL, RADIUS=\s(?P<radius>[\d\.E\+]+)\s+(?P<radius_unit>.+)"
    is_spherical = any(re.findall(spherical_pattern, contents))

    pattern = (
        "(?P<header>^.+)\n"# V[turb|TURB]\s+(?P<microturbulence>[\d\.]+) km/s.*\n"
        "T EFF=\s?(?P<teff>[\d\.]+)\s+GRAV=\s?(?P<logg>[\-\d\.]+)\s+MODEL TYPE= (?P<model_type>\d) WLSTD= (?P<standard_wavelength>\d+)"
    )
    if is_spherical:
        pattern += spherical_pattern + ".+\n"
    else:
        pattern += ".+\n"

    pattern += "\s*(?P<absorption_by_H>[01]) (?P<absorption_by_H2_plus>[01]) (?P<absorption_by_H_minus>[01]) (?P<rayleigh_scattering_by_H>[01]) (?P<absorption_by_He>[01]) (?P<absorption_by_He_plus>[01]) (?P<absorption_by_He_minus>[01]) (?P<rayleigh_scattering_by_He>[01]) (?P<absorption_by_metals_for_cool_stars_Si_Mg_Al_C_Fe>[01]) (?P<absorption_by_metals_for_intermediate_stars_N_O_Si_plus_Mg_plus_Ca_plus>[01]) (?P<absorption_by_metals_for_hot_stars_C_N_O_Ne_Mg_Si_S_Fe>[01]) (?P<thomson_scattering_by_electrons>[01]) (?P<rayleigh_scattering_by_h2>[01]).+OPACITY SWITCHES\n"

    #normalized abundances for the first 99 elements in the periodic table: N_atom/N_all where N are number densities. The sum of all abundances is 1. Positive numbers are fractions while negative numbers are log10 of fractions. The last number in line 13 is the number of depth layers in the model.
    for i, element in enumerate(periodic_table[:99], start=1):
        pattern += f"\s*(?P<log10_normalized_abundance_{element}>[\-\d\.]+) "
        if i % 10 == 0:
            pattern = pattern.rstrip() + "\n"

    pattern += "\s*(?P<n_depth>\d+)"

    meta = OrderedDict(re.match(pattern, contents).groupdict())
    if not is_spherical:
        meta.update(radius=0, radius_unit="cm")

    # Assign dtypes.
    dtype_patterns = [
        ("teff$", float),
        ("flux$", float),
        ("logg$", float),
        ("radius$", float),
        ("standard_wavelength", float),
        ("log10_normalized_abundance_\w+", float),
        ("n_depth", int),
        ("absorption_by", lambda v: bool(int(v))),
        ("scattering_by", lambda v: bool(int(v))),
    ]
    for key in meta.keys():
        for pattern, dtype in dtype_patterns:
            if re.findall(pattern, key):
                meta[key] = dtype(meta[key])
                break

    # Correct the normalized abundance columns so they are all log10 base.
    # Numbers that are positive are not yet log10
    for element in periodic_table[:99]:
        key = f"log10_normalized_abundance_{element}"
        if meta[key] > 0:
            meta[key] = np.log10(meta[key])

    # Add overall metallicity keyword, taking Fe as representative of metallicity.
    # Remember that the normalized_abundance keywords are number densities, so to convert to the "12 scale" we need:
    #   log_10(X) = log_10(N_X/N_H) + 12
    # and for [X/H] we need:
    #   [X/H] = log_10(X) - log_10(X_Solar)

    # TODO: Assuming 7.45 Fe solar composition. Check this.
    meta["m_h"] = meta["log10_normalized_abundance_Fe"] - meta["log10_normalized_abundance_H"] + 12 - 7.45

    # TODO: Should we just calculate abundances for ease?
    meta["grid_keywords"] = ("teff", "logg", "m_h")
    return meta

def read_kurucz(fp_or_path, structure_start=13, __include_extra_columns=True):

    filename, contents, content_stream = safe_open(fp_or_path)

    meta = parse_meta(contents)
    model_type = int(meta["model_type"])
    if model_type in (0, 1):
        # Plane-parallel.
        usecols = (0, 1, 2, 3, 4)
    elif model_type in (3, 4):
        # Spherical geometry.
        usecols = (0, 1, 2, 3, 4, 5)
    else:
        raise ValueError(f"Unknown model_type: {model_type}")

    if model_type in (0, 3):
        # depth scale - column mass in g/cm^2
        column_locations = [
            (structure_start, ("RHOX", "T", "XNE", "numdens_other", "Density", "Depth"))
        ]
    elif model_type in (1, 4):
        # depth scale - tau at std wavelength
        column_locations = [
            (structure_start, ("tau", "T", "XNE", "numdens_other", "Density", "Depth"))
        ]

    data = {}
    for skiprows, column_names in column_locations:
        values = np.loadtxt(
            content_stream,
            skiprows=skiprows,
            max_rows=meta["n_depth"],
            delimiter=",",
            usecols=usecols
        )
        assert values.shape[0] == meta["n_depth"]
        data.update(dict(zip(column_names, values.T)))

    column_descriptions = {
        "RHOX": ("Mass column density", "g/cm^2"),
        "tau": ("Optical depth at standard wavelength", "-"),
        "T": ("Temperature", "K"),
        "XNE": ("Number density of free electrons", "1/cm^3"),
        "numdens_other": ("Number density of all other particles (except electrons)", "1/cm^3"),
        "Density": ("Density", "g/cm^3"),
        "Depth": ("Height relative to the reference radius given in the metadata", "cm")
    }
    descriptions = { k: desc for k, (desc, unit) in column_descriptions.items() if k in data }
    units = { k: unit for k, (desc, unit) in column_descriptions.items() if k in data  }

    photosphere = Photosphere(
        data=data,
        units=units,
        descriptions=descriptions,
        meta=meta
    )

    # See marcs.py comment on this.
    # MOOG needs more things than the krz format can offer.

    # TODO: This is an awful hack that relies on you having both MARCS formats.
    #       It's only necessary for MOOG. God it's all a hack.
    if __include_extra_columns:
        alt_filename = filename.replace("marcs_krz", "marcs_mod").replace(".krz", ".mod")
        alt_photosphere = Photosphere.read(alt_filename, __include_extra_columns=False)
        for key in ("KappaRoss", "P"):
            photosphere[key] = alt_photosphere[key]

    return photosphere

def write_kurucz(photosphere, path, **kwargs):
    """
    Write a photosphere in Kurucz format, documented at:
    https://marcs.astro.uu.se/krz_format.html.

    :param photosphere:
        The photosphere.

    :path:
        The local path.
    """
    meta = photosphere.meta
    # Always use either model_type of 0 or 3, which outputs column mass as first column
    first_column_name = "RHOX"
    if photosphere.is_spherical_geometry:
        model_type = 3
        geometry_desc = f"SPHERICAL,  RADIUS= {meta['radius']:.3e} cm"
    else:
        model_type = 0
        geometry_desc = "PLANE-PARALLEL."

    opacity_keys = (
        "absorption_by_H",
        "absorption_by_H2_plus",
        "absorption_by_H_minus",
        "rayleigh_scattering_by_H",
        "absorption_by_He",
        "absorption_by_He_plus",
        "absorption_by_He_minus",
        "rayleigh_scattering_by_He",
        "absorption_by_metals_for_cool_stars_Si_Mg_Al_C_Fe",
        "absorption_by_metals_for_intermediate_stars_N_O_Si_plus_Mg_plus_Ca_plus",
        "absorption_by_metals_for_hot_stars_C_N_O_Ne_Mg_Si_S_Fe",
        "thomson_scattering_by_electrons",
        "rayleigh_scattering_by_h2"
    )
    opacity_switches = [2 if meta.get(k, None) is None else int(meta.get(k, None)) for k in opacity_keys]

    contents = dedent(
    f"""
    {meta.get('header', 'NO HEADER')}
    T EFF={meta['teff']:.0f}. GRAV= {meta['logg']:.2f} MODEL TYPE= {model_type} WLSTD= {meta.get('standard_wavelength', 5000):.0f} {geometry_desc}
    {' '.join(map(str, opacity_switches))} - OPACITY SWITCHES
    """).lstrip()

    N_elements = 99
    normalized_abundances = []
    for i, element in enumerate(periodic_table[:N_elements]):
        value = meta.get(f'log10_normalized_abundance_{element}', -20)
        if 10**value > 0.01:
            value = 10**value
        if element in ("H", "He"):
            normalized_abundances.append(f"{value:1.3f}")
        else:
            normalized_abundances.append(f"{value:1.2f}")

    n_per_line = 10
    for i in range(int(np.ceil(N_elements / n_per_line))):
        si, ei = (i * n_per_line, (i + 1) * n_per_line)
        contents += " ".join(normalized_abundances[si:ei]) + "\n"

    contents = contents.rstrip() + f" {len(photosphere):.0f}\n"

    for row in photosphere:

        contents += f"{row[first_column_name]:1.9e},{row['T']: >7.1f},{row['XNE']: >12.5e},{row['numdens_other']: >12.5e},{row['Density']: >12.5e},"
        if photosphere.is_spherical_geometry:
            contents += f"{row['Depth']: >12.5e},"
        contents += "\n"

    with open(path, "w") as fp:
        fp.write(contents)

    return None

def identify_kurucz(origin, *args, **kwargs):
    return (isinstance(args[0], str) and \
            args[0].lower().endswith((".krz", ".krz.gz")))

registry.register_reader("kurucz", Photosphere, read_kurucz)
registry.register_writer("kurucz", Photosphere, write_kurucz)
registry.register_identifier("kurucz", Photosphere, identify_kurucz)


import random
from pygame.sprite import Sprite

from game.utils.constants import SCREEN_WIDTH

class PowerUps(Sprite):
    def __init__(self, image, type):
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.y = 0
        self.rect.x = random.randint(120, SCREEN_WIDTH - 120)
        self.type = type
        self.strat_time = 0

    def update(self, game_speed):
        self.rect.y += game_speed

    def draw(self, screen):
        screen.blit(self.image, self.rect)


import cv2
import os
import shutil

if os.path.exists("result"):
    shutil.rmtree("result")
os.mkdir("result")
# https://www.spriters-resource.com/fullview/64342/
img = cv2.imread("mon_icon.png", -1)

names = [
    "mon",
    "bird",
    "water",
    "fairy",
    "grass",
    "bug",
    "snake",
    "quadruped",
    "ball",
    "helix",
]

for i in range(10):
    name = names[i]
    if i < 8:
        for j in range(2):
            x0 = j*16
            y0 = i*16
            x1 = x0 + 16
            y1 = y0 + 16

            tile = img[y0:y1, x0:x1]
            cv2.imwrite("./result/{}_mon_icon_{}.png".format(name, j), tile)
    else:
        x0 = 0
        y0 = i*16
        x1 = x0 + 16
        y1 = y0 + 16

        tile = img[y0:y1, x0:x1]
        cv2.imwrite("./result/{}_mon_icon_{}.png".format(name, 0), tile)

        x0 = 0
        y0 = i*16-1
        x1 = x0 + 16
        y1 = y0 + 16

        tile = img[y0:y1, x0:x1]
        cv2.imwrite("./result/{}_mon_icon_{}.png".format(name, 1), tile)


import wave
import numpy as np
import matplotlib.pyplot as plt
import sys
import scipy.signal.signaltools as sigtool
from scipy.signal import find_peaks

def normalization(sig):
    env = (sigtool.hilbert(sig)) #przeksztalcenie hilberata
    threshold = 200
    square_sig = (env > threshold)
    return square_sig

def wav2morse(filename):
    wav = wave.open(filename, 'r')

    raw = wav.readframes(-1)
    raw = list(np.frombuffer(raw, "Int16"))
    plt.figure('raw')
    plt.title("raw")
    plt.plot(raw)
    # plt.show()

    if wav.getnchannels()==2:
        print("Stereo files are not supported!")
        sys.exit(0)

    thresh = normalization(raw)
    # print(thresh)

    plt.figure('thresh')
    plt.title('thresh')
    plt.plot(thresh)
    # plt.show()

    peaks, _ = find_peaks(thresh)
    print(peaks)

    plt.figure('peaks')
    plt.title('peaks')
    plt.plot(peaks, thresh[peaks], '.')
    # plt.show()

    #####################################
    delta = 0
    start_value = peaks[0]
    prev_value = 0
    spaces = []
    characters = []

    for i in range(len(peaks)):
        delta = peaks[i] - prev_value#peaks[i-1]
        # print(delta)
        if delta > 20:
            spaces.append(delta)
            characters.append(prev_value-start_value)
            start_value=peaks[i]
        prev_value = peaks[i]

    rew_peaks = peaks[::-1]
    start_value = rew_peaks[0]
    prev_value = rew_peaks[0]

    for i in range(len(rew_peaks)):
        delta = abs(rew_peaks[i] - prev_value)
        if delta > 20:
            spaces.append(delta)
            characters.append(abs(prev_value-start_value))
            start_value=peaks[i]
            break
        prev_value = rew_peaks[i]

    print(spaces)
    print(characters)
    print(len(spaces)-len(characters))

    returned_vals = []
    for i in range(len(characters)):
        returned_vals.append(characters[i])
        if spaces[i] > 5000:
            returned_vals.append(0)

    print(returned_vals)

    returned = ''
    for i in range(len(returned_vals)):
        if returned_vals[i] == 0:
            returned +=' '
        elif returned_vals[i] < 500:
            returned +='.'
        else:
            returned += '-'

    print(returned)
    plt.show()
    return returned


from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = '''
---
module: azure_rm_openshiftmanagedclusterkubeconfig_info
version_added: '1.17.0'
short_description: Get admin kubeconfig of Azure Red Hat OpenShift Managed Cluster
description:
    - get kubeconfig of Azure Red Hat OpenShift Managed Cluster instance.
options:
    resource_group:
        description:
            - The name of the resource group.
        required: true
        type: str
    name:
        description:
            - Resource name.
        required: true
        type: str
    path:
        description:
            - Destination filepath of kubeconfig file
        required: false
        type: str
extends_documentation_fragment:
    - azure.azcollection.azure
author:
    - Maxim Babushkin (@maxbab)
'''

EXAMPLES = '''
- name: Obtain kubeconfig file of ARO cluster
  azure_rm_openshiftmanagedclusterkubeconfig_info:
    name: myCluster
    resource_group: myResourceGroup
  register: kubeconf

- name: Print registered kubeconfig file
  debug:
    msg: "{{ kubeconf['kubeconfig'] }}"

- name: Fetch kubeconfig and save it as mycluster_kubeconfig filename
  azure_rm_openshiftmanagedclusterkubeconfig_info:
    name: myCluster
    resource_group: myResourceGroup
    path: ./files/mycluster_kubeconfig

- name: Fetch kubeconfig and save it to specified directory (file will be named as kubeconfig by default)
  azure_rm_openshiftmanagedclusterkubeconfig_info:
    name: myCluster
    resource_group: myResourceGroup
    path: ./files/
'''

RETURN = '''
kubeconfig:
    description:
        - kubeconfig value
    returned: always
    type: str
'''

import base64
import filecmp
import json
import os
import tempfile
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient

class Actions:
    NoAction, Create, Update, Delete = range(4)

class AzureRMOpenShiftManagedClustersKubeconfigInfo(AzureRMModuleBaseExt):
    def __init__(self):
        self.module_arg_spec = dict(
            resource_group=dict(
                type='str', required=True
            ),
            name=dict(
                type='str', required=True
            ),
            path=dict(
                type='str', required=False
            )
        )

        self.resource_group = None
        self.name = None
        self.path = None

        self.results = dict(changed=False)
        self.mgmt_client = None
        self.state = None
        self.url = None
        self.status_code = [200]

        self.query_parameters = {}
        self.query_parameters['api-version'] = '2021-09-01-preview'
        self.header_parameters = {}
        self.header_parameters['Content-Type'] = 'application/json; charset=utf-8'

        self.mgmt_client = None
        super(AzureRMOpenShiftManagedClustersKubeconfigInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False)

    def exec_module(self, **kwargs):

        for key in self.module_arg_spec:
            setattr(self, key, kwargs[key])

        self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
                                                    base_url=self._cloud_environment.endpoints.resource_manager)
        self.results = self.get_kubeconfig()
        if self.path and self.path_is_valid():
            self.write_kubeconfig_to_file()
        return self.results

    def get_kubeconfig(self):
        response = None
        results = {}
        # prepare url
        self.url = ('/subscriptions' +
                    '/{{ subscription_id }}' +
                    '/resourceGroups' +
                    '/{{ resource_group }}' +
                    '/providers' +
                    '/Microsoft.RedHatOpenShift' +
                    '/openShiftClusters' +
                    '/{{ cluster_name }}' +
                    '/listAdminCredentials')
        self.url = self.url.replace('{{ subscription_id }}', self.subscription_id)
        self.url = self.url.replace('{{ resource_group }}', self.resource_group)
        self.url = self.url.replace('{{ cluster_name }}', self.name)
        self.log("Fetch for kubeconfig from the cluster.")
        try:
            response = self.mgmt_client.query(self.url,
                                              'POST',
                                              self.query_parameters,
                                              self.header_parameters,
                                              None,
                                              self.status_code,
                                              600,
                                              30)
            results = json.loads(response.text)
        except Exception as e:
            self.log('Could not get info for @(Model.ModuleOperationNameUpper).')
        return self.format_item(results)

    def format_item(self, item):
        d = {
            'kubeconfig': item['kubeconfig'],
        }
        return d

    def path_is_valid(self):
        if not os.path.basename(self.path):
            if os.path.isdir(self.path):
                self.log("Path is dir. Appending file name.")
                self.path += "kubeconfig"
            else:
                try:
                    self.log('Attempting to makedirs {0}'.format(self.path))
                    os.makedirs(self.path)
                except IOError as exc:
                    self.fail("Failed to create directory {0} - {1}".format(self.path, str(exc)))
                self.path += "kubeconfig"
        else:
            file_name = os.path.basename(self.path)
            path = self.path.replace(file_name, '')
            self.log('Checking path {0}'. format(path))
            # If the "path" is not defined, it's cwd.
            if path and not os.path.isdir(path):
                try:
                    self.log('Attempting to makedirs {0}'. format(path))
                    os.makedirs(path)
                except IOError as exc:
                    self.fail("Failed to create directory {0} - {1}".format(path, str(exc)))
        self.log("Validated path - {0}". format(self.path))
        return True

    def write_kubeconfig_to_file(self):
        decoded_bytes = base64.b64decode(self.results['kubeconfig'])
        decoded_string = decoded_bytes.decode("utf-8")

        if os.path.exists(self.path):
            self.log('Existing kubeconfig file found. Compare, to decide if needs to override')
            # If kubeconfig file already exists, compare it with the new file
            # If equal, do nothing, otherwise, override.
            tmp_kubeconfig = tempfile.TemporaryFile(mode='w')
            tmp_kubeconfig.write(decoded_string)
            tmp_kubeconfig.seek(0)

            if filecmp.cmp(tmp_kubeconfig.name, self.path):
                self.log("Files are identical. No need to override.")
                self.results['changed'] = False
                return

        self.log("Create {0} kubeconfig file.".format(self.path))
        try:
            with open(self.path, "w") as file:
                file.write(decoded_string)
        except Exception as exc:
            self.fail("Failed to write kubeconfig output to file - {0} to {1} - {2}".format(self.results['kubeconfig'],
                                                                                self.path, exc))
        self.log("The {0} kubeconfig file has been created.")
        self.results['changed'] = True
        return

def main():
    AzureRMOpenShiftManagedClustersKubeconfigInfo()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
#
# Birdsong classificatione in noisy environment with convolutional neural nets in Keras
# Copyright (C) 2017 Báint Czeba, Bálint Pál Tóth (toth.b@tmit.bme.hu)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>. (c) Balint Czeba, Balint Pal Toth
#
# Please cite the following paper if this code was useful for your research:
#
# Bálint Pál Tóth, Bálint Czeba,
# "Convolutional Neural Networks for Large-Scale Bird Song Classification in Noisy Environment",
# In: Working Notes of Conference and Labs of the Evaluation Forum, Évora, Portugália, 2016, p. 8

# this script generates images from the preprocessed spectograms

from scipy import io
import pandas as pd
import numpy as np
import time
import pickle
import os
import h5py

hdf5path = '../birdclef_data/data_top999_nozero.hdf5' # it takes long if we save all the spectorgram, maybe it is better to call processNMostCommon from loadData.py with a small N (eg. =3)
todirrootpath = '../images_bw_3/'

f = h5py.File(hdf5path, 'r')
X = f.get('X')
y = f.get('y')
mediaId = f.get('MediaId')
classId = f.get('ClassId')
print(mediaId[0])

y_class = np.empty((0,1))
for row in y:
    for i in range(len(row)):
        if row[i]==1:
            y_class=np.vstack((y_class,i))

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.ioff()

fig = plt.figure(frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')

i=0
lenX=X.shape[0]
img_artist = ax.imshow(np.flipud(X[0][0]), cmap=plt.cm.binary)

if not os.path.exists(todirrootpath):
    os.makedirs(todirrootpath)

for i in range(X.shape[0]):
    directory=os.path.join(todirrootpath,'{}'.format(classId[i][0]))
    if not os.path.exists(directory):
            os.makedirs(directory)
    print('{}/{}'.format(i, lenX))
    img_artist.set_data(np.flipud(X[i][0]))
    fileNumber=0
    while(os.path.isfile(os.path.join(directory, '{}_{}.png'.format(int(mediaId[i][0]), fileNumber)))):
        fileNumber=fileNumber+1;
        print(fileNumber)

    with open(os.path.join(directory, '{}_{}.png'.format(int(mediaId[i][0]), fileNumber)), 'w') as outfile:
        fig.canvas.print_png(outfile)

f.close()


import zmq
import numpy as np
import socket as socket0

class Server:

    def __init__(self, tcp_port=14100):

        # Connection to CM Simulation in rt
        self.tcp_port = tcp_port
        z_context = "ipc:///tmp/%d" % tcp_port
        context = zmq.Context()
        self.zsocket = context.socket(zmq.PAIR)
        self.zsocket.bind(z_context)

        self.not_recieved_attempts = 0
        self.zsocket.setsockopt( zmq.LINGER,      0 )  # ____POLICY: set upon instantiations
        self.zsocket.setsockopt( zmq.AFFINITY,    1 )  # ____POLICY: map upon IO-type thread
        self.zsocket.setsockopt( zmq.RCVTIMEO,  250 )

        # Connection to CM Simulation non rt/block
        self.tcp_port_nrt = tcp_port + 100
        z_context_nrt = "ipc:///tmp/%d" % self.tcp_port_nrt
        context_nrt = zmq.Context()
        self.zsocket_nrt = context_nrt.socket(zmq.PAIR)
        self.zsocket_nrt.bind(z_context_nrt)

    def server_nrt(self,msg):
        self.zsocket_nrt.send_string(msg)

    def server_step(self, action = [0,0]):

        #  Wait for next request from client
        #log.server("Waiting for request...")

        while self.not_recieved_attempts < 400:
            try:
                message = self.zsocket.recv_string()
                self.not_recieved_attempts = 0
                #  Send reply back to client
                s_ctrl = str(action[0]) + " " + str(action[1])
                self.zsocket.send_string(s_ctrl)
                break
            except:
                #log.server("No request. Retry...")
                #print("No request. Retry...")
                self.not_recieved_attempts += 1

        if self.not_recieved_attempts == 400:
            Server(14100).send_gui("StartSim")
            Server(14100).send_gui("SessionLogMsg {Lost connection}")
            self.not_recieved_attempts = 0
            return None, 4.04, None

        # Convert message to np array
        self.state = np.fromstring(message, dtype=np.float32, sep=' ')

        array_sum = np.sum(self.state)
        array_has_nan = np.isnan(array_sum)
        array_has_inf = np.isinf(array_sum)

        if array_has_nan or array_has_inf:
            Server(14100).send_gui("SessionLogWarn {Obs array has nan or inf}")
            Server(14100).send_gui("StopSim")
            Server(14100).send_gui("StartSim")
            self.not_recieved_attempts = 0
            return None, 4.04, None

        self.sim_time = self.state[0]
        self.s_road = self.state[1]
        self.state = np.delete(self.state, [0, 1])

        return self.state, self.sim_time, self.s_road

    def send_gui (self, msg):

        # Connect to CM GUI
        TCP_IP = socket0.gethostname()
        TCP_PORT = self.tcp_port

        self.s = socket0.socket(socket0.AF_INET, socket0.SOCK_STREAM)
        self.s.connect((TCP_IP, TCP_PORT))

        MESSAGE = "eval { %s }\n" % msg
        self.s.send(MESSAGE.encode())
        ans = self.s.recv(8192).decode().split('\r')[0]
        self.s.close()

        return "%s" % ans[1:]


__author__ = 'Administrator'

import MySQLdb
"this is MySQLdb library demonatrate reference http://mysql-python.sourceforge.net/MySQLdb.html and https://www.python.org/dev/peps/pep-0249/#connection"

def main():
    connection = MySQLdb.connect('localhost', 'root', 'luowen', 'haha')
    cursorObj = connection.cursor()  # get cursor object

    try:
        cursorObj.execute('select * from pc_wx_fans')
        print(cursorObj.rowcount)  # print the result row count
        fields = tuple(name[0] for name in cursorObj.description)
        resultSet = cursorObj.fetchall()
        arrayList = [dict(zip(fields, result)) for result in resultSet]
        print(arrayList)
    finally:
        connection.close()

if __name__ == '__main__':
    main()


import cloudscraper
import pandas as pd
from multiprocessing.dummy import Pool as ThreadPool

def scrape(s : int=0, e : int=10000) -> pd.DataFrame:
    scraper = cloudscraper.create_scraper()

    # CNN
    news = {"platform": [],
            "title": [],
            "date": [],
            "url": []}

    # Thread
    def foo(i : int):
        try:
            res = scraper.get(f"https://search.api.cnn.io/content?q=news&size=50&from={i}")
            news["title"] += [x["headline"] for x in res.json()["result"]]
            news["url"] += [x["url"] for x in res.json()["result"]]
            news["date"] += [x["lastPublishDate"] for x in res.json()["result"]]

            print("Scrap CNN...")
        except:
            pass

    with ThreadPool(16) as pool:
        pool.map(foo, range(s, e, 11))

    news["platform"] = "CNN"

    df = pd.DataFrame(news)

    return df


import requests
import json

# 1. 텍스트 준비 & 개행문자 처리
with open('text.txt', 'r', encoding='utf-8') as f:
    text = f.read()
text = text.replace('\n', '\r\n')

# 2. 맞춤법 검사 요청 (requests)
response = requests.post('http://164.125.7.61/speller/results', data={'text1': text})

# 3. 응답에서 필요한 내용 추출 (html 파싱)
data = response.text.split('data = [', 1)[-1].rsplit('];', 1)[0]

# 4. 파이썬 딕셔너리 형식으로 변환
data = json.loads(data)

# 5. 교정 내용 출력
for err in data['errInfo']:
    print(f"입력 내용 : {err['orgStr']}")
    print(f"대치어 : {err['candWord']}")
    print(f"도움말 : {err['help']}\n")


import json

from tagalog.parsing import get_tokens_and_labels

def test_json_parsing():
    test_file = "tests/data/data.json"
    reference_file = "tests/data/data.conll"

    # Parse the reference CONLL file
    reference_tokens = []
    reference_labels = []

    with open(reference_file) as i:
        for line in i:
            line = line.strip().split("\t")

            if len(line) == 2 and line[0] != "-DOCSTART-":
                reference_tokens[-1].append(line[0])
                reference_labels[-1].append(line[1])
            elif line[0] == "-DOCSTART-":
                reference_tokens.append([])
                reference_labels.append([])

    # Parse the Montague JSON file
    with open(test_file) as i:
        montague_json = json.load(i)

    document_tokens = []
    document_labels = []
    for text, tokens, labels in get_tokens_and_labels(montague_json):
        document_tokens.append(tokens)
        document_labels.append(labels)

    # Test for equality
    assert document_tokens == reference_tokens
    assert document_labels == reference_labels


import streamlit as st
st.set_page_config(layout="wide", page_title="Socialis", page_icon="")
import cv2
import numpy as np
import pandas as pd
from PIL import Image

st.title("Object Detection on Cloud")
confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5, 0.01)

def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

def icon(icon_name):
    st.markdown(f'<i class="material-icons">{icon_name}</i>', unsafe_allow_html=True)

local_css("style.css")

@st.cache(show_spinner=False)
def read_img(img):
    input_image = cv2.imread(img, cv2.IMREAD_COLOR)
    input_image = input_image[:, :, [2, 1, 0]]
    return input_image

def yolo_v3(ip_image, conf=0.5, overlap_threshold=0.3):
    net = cv2.dnn.readNetFromDarknet("config.cfg", "model.weights")
    output_layer_names = net.getLayerNames()
    output_layer_names = [output_layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

    blob = cv2.dnn.blobFromImage(ip_image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
    net.setInput(blob)
    layer_outputs = net.forward(output_layer_names)

    boxes, confidences, class_ids = [], [], []
    h, w = ip_image.shape[:2]

    for output in layer_outputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > conf:
                box = detection[0:4] * np.array([w, h, w, h])
                center_x, center_y, width, height = box.astype("int")
                x, y = int(center_x - (width / 2)), int(center_y - (height / 2))
                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                class_ids.append(class_id)

    f = open("coco.names", "r")
    f = f.readlines()
    f = [line.rstrip('\n') for line in list(f)]
    try:
        st.subheader("Detected Objects: " + ', '.join(list(set([f[obj] for obj in class_ids]))))
    except IndexError:
        st.write("Nothing Detected D:")

    indices = cv2.dnn.NMSBoxes(boxes, confidences, conf, overlap_threshold)
    xmin, xmax, ymin, ymax, labels = [], [], [], [], []

    if len(indices) > 0:
        for i in indices.flatten():
            x, y, w, h = boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]
            xmin.append(x)
            ymin.append(y)
            xmax.append(x + w)
            ymax.append(y + h)
    boxes = pd.DataFrame({"xmin": xmin, "ymin": ymin, "xmax": xmax, "ymax": ymax})

    label_colors = [0, 255, 0]
    image_with_boxes = ip_image.astype(np.float64)
    for _, (xmin, ymin, xmax, ymax) in boxes.iterrows():
        image_with_boxes[int(ymin):int(ymax), int(xmin):int(xmax), :] += label_colors
        image_with_boxes[int(ymin):int(ymax), int(xmin):int(xmax), :] /= 2
    st.image(image_with_boxes.astype(np.uint8), use_column_width=True)

img_type = st.sidebar.selectbox("Select image type", ['Browse', 'Cars', 'Person', 'Animals'], 1)
st.sidebar.write("Made by Lavin, Parth and Dharmik  \n\n Group 42 BE-C2")
image_url = ""
if img_type == 'Browse':
    st.set_option('deprecation.showfileUploaderEncoding', False)
    image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg'])

    if image_file is not None:
        our_image = Image.open(image_file)
        new_img = np.array(our_image.convert('RGB'))
        image = cv2.cvtColor(new_img, 1)
        yolo_v3(image, confidence_threshold)
    else:
        st.markdown("Please upload an image")

elif img_type == 'Person':
    image_url = "images/person.jpg"
    image = read_img(image_url)
    yolo_v3(image, confidence_threshold)
elif img_type == 'Cars':
    image_url = "images/cars.jpg"
    image = read_img(image_url)
    yolo_v3(image, confidence_threshold)
elif img_type == 'Animals':
    image_url = "images/animals.jpg"
    image = read_img(image_url)
    yolo_v3(image, confidence_threshold)


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time    : 2019-01-14 11:40
@Author  : red
@Site    :
@File    : data_process.py
@Software: PyCharm
"""
from utils import sql_util as sql
import time
import uuid

def get_all_data():
    # start = time.time()
    title_lst = sql.queryall("select title from origin_data GROUP BY title")
    # print("time is %s" % (time.time() - start))
    # print("data num is %s" % (sql.queryone("select count(*) from origin_data")))
    article = []
    comment = []
    for title in title_lst:
        array1 = []
        lst = sql.queryall("select * from origin_data where title = %s", title)
        article_id = str(uuid.uuid4())
        array1.append(article_id)
        array1.append(lst[0].get('title'))
        array1.append(lst[0].get('time'))
        array1.append(lst[0].get('content'))
        array1.append(lst[0].get('read_num'))
        article.append(array1)

        comment_lst = []
        for item in lst:
            if item.get('comment') in comment_lst:
                continue
            else:
                comment_lst.append(item.get('comment'))
                array2 = []
                array2.append(item.get('user'))
                array2.append(item.get('comment'))
                array2.append(item.get('comment_time'))
                array2.append(item.get('like_num'))
                array2.append(article_id)
                array2.append(str(uuid.uuid4()))
                comment.append(array2)
    return article, comment

def insert_data(article, comment):
    sql_str1 = "truncate table article"
    sql_str2 = "truncate table comment"
    sql.execute(sql_str1)
    sql.execute(sql_str2)
    sql_article = "insert into article(id, title, time, content, read_num) " \
                  "values (%s, %s, %s, %s, %s)"
    sql_comment = "insert into comment(user, comment, comment_time, like_num, article_id, id) " \
                  "values (%s, %s, %s, %s, %s, %s)"
    sql.insertmany(sql_article, article)
    sql.insertmany(sql_comment, comment)

if __name__ == '__main__':
    article, comment = get_all_data()
    print("数据获取完毕。。。。。。")
    insert_data(article, comment)


from pathlib import Path
import Pyxsim
import pytest

num_in_out_args = {"4ch_in,4ch_out": (4, 4),
                   "1ch_in,1ch_out": (1, 1),
                   "4ch_in,0ch_out": (4, 0),
                   "0ch_in,4ch_out": (0, 4),
                   "2ch_in,2ch_out": (2, 2),
                   "3ch_in,3ch_out": (3, 3)}

@pytest.mark.parametrize(("num_in", "num_out"), num_in_out_args.values(), ids=num_in_out_args.keys())
def test_index_sequence(capfd, request, nightly, num_in, num_out):
    if (num_in in (1,2,3) or num_out in (1,2,3)) and not nightly:
        pytest.skip("Only test 4ch modes if not nightly")

    testlevel = '0' if nightly else '1'
    id_string = f"{num_in}_{num_out}"
    id_string += "_smoke" if testlevel == '1' else ""

    cwd = Path(request.fspath).parent
    binary = f'{cwd}/test_i2s_callback_sequence/bin/master_{id_string}/test_i2s_callback_sequence_master_{id_string}.xe'

    tester = Pyxsim.testers.AssertiveComparisonTester(
        f'{cwd}/expected/sequence_check_{num_out}{num_in}.expect',
        regexp = True,
        ordered = True,
        suppress_multidrive_messages=True,
        ignore=["CONFIG:.*"]
    )

    Pyxsim.run_on_simulator(
        binary,
        tester=tester,
        build_env = {"NUMS_IN_OUT":f'{num_in};{num_out}', "SMOKE":testlevel, "MASTER":"1"},
        simargs=[],
        capfd=capfd
    )