# Licensed under a 3-clause BSD style license - see LICENSE.rst

from astropy import units
import numpy as np
import pytest

from sofia_redux.scan.filters.filter import Filter
from sofia_redux.scan.filters.kill_filter import KillFilter

class NoFilter(Filter):  # pragma: no cover

    def __init__(self, integration=None, data=None):
        super().__init__(integration=integration, data=data)
        self.variable = False

    def get_id(self):
        return 'N'

    def get_config_name(self):
        return 'filter.none'

    def response_at(self, fch):
        if not self.variable:
            return np.ones_like(fch).astype(float)
        else:
            return np.ones((self.channel_size, np.asarray(fch).size))

    def get_mean_point_response(self):
        if not self.variable:
            return 1.0
        else:
            return np.ones(self.channel_size)

class VariedKill(KillFilter):

    def response_at(self, fch):
        response = super().response_at(fch)
        if response.ndim == 2:
            return response.T
        return response

@pytest.fixture
def initialized_filter(populated_integration):
    return NoFilter(integration=populated_integration)

@pytest.fixture
def configured_filter(initialized_filter):
    f = initialized_filter
    f.set_integration(f.integration.copy())
    f.configuration.parse_key_value(f.get_config_name(), 'True')
    f.configuration.parse_key_value(f'{f.get_config_name()}.level', '2.0')
    return f

@pytest.fixture
def kill_filter(populated_integration):
    integration = populated_integration.copy()
    f = VariedKill(integration=integration)
    integration.configuration.parse_key_value('filter.kill', 'True')
    integration.configuration.parse_key_value('filter.kill.bands', '0.5:0.6')
    f.update_config()
    f.make_temp_data()
    f.load_time_streams()
    return f

def test_init(populated_integration):

    f = NoFilter()
    for attr in ['integration', 'channels', 'parms', 'frame_parms', 'data',
                 'points']:
        assert getattr(f, attr) is None

    for attr in ['is_sub_filter', 'dft', 'pedantic', 'enabled']:
        assert not getattr(f, attr)

    for attr in ['nt', 'nf', 'df']:
        assert getattr(f, attr) == 0

    data = np.ones(1024)
    f = NoFilter(data=data)
    assert f.data is data

    integration = populated_integration
    f = NoFilter(integration=integration)
    assert f.integration is integration
    assert f.nt == 2048
    assert f.nf == 1024
    assert np.isclose(f.df, 0.0048828125)
    assert f.channels.data is integration.channels.data

def test_copy(initialized_filter):
    f = initialized_filter
    f.data = np.zeros(1024)
    f2 = f.copy()
    assert f2 is not f
    assert np.allclose(f2.data, f.data)
    assert f2.integration is f.integration
    assert f2.channels is f.channels

def test_referenced_attributes():
    f = NoFilter()
    assert 'integration' in f.referenced_attributes
    assert 'channels' in f.referenced_attributes

def test_channel_dependent_attributes():
    f = NoFilter()
    assert 'points' in f.channel_dependent_attributes

def test_size(initialized_filter):
    assert NoFilter().size == 0
    assert initialized_filter.size > 0

def test_channel_size(initialized_filter):
    assert NoFilter().channel_size == 0
    assert initialized_filter.channel_size > 0

def test_frames(initialized_filter):
    assert NoFilter().frames is None
    assert initialized_filter.frames.__class__.__name__.endswith('Frames')

def test_info(initialized_filter):
    assert NoFilter().info is None
    assert initialized_filter.info.__class__.__name__.endswith('Info')

def test_configuration(initialized_filter):
    assert NoFilter().configuration is None
    assert initialized_filter.configuration.__class__.__name__.endswith(
        'Configuration')

def test_flagspace(initialized_filter):
    assert NoFilter().flagspace is None
    assert initialized_filter.flagspace.__name__.endswith('FrameFlags')

def test_channel_flagspace(initialized_filter):
    assert NoFilter().channel_flagspace is None
    assert initialized_filter.channel_flagspace.__name__.endswith(
        'ChannelFlags')

def test_valid_filtering_frames(initialized_filter):
    f = initialized_filter.copy()
    f.set_integration(f.integration.copy())
    f.integration.frames.set_flags('MODELING_FLAGS', np.arange(5))
    f.integration.frames.valid[5] = False
    v = f.valid_filtering_frames
    assert not v[:6].any()
    assert v[6:].all()
    assert NoFilter().valid_filtering_frames.size == 0

def test_reindex(initialized_filter):
    f = NoFilter()
    f.reindex()
    assert f.channels is None

    f = initialized_filter.copy()
    # Set a copy of the integration
    f.set_integration(f.integration.copy())
    # Set dependents to actual values
    f.parms = f.integration.get_dependents(f.get_config_name())
    f.points = np.arange(f.channels.data.size)

    # Set some dead channels
    f.integration.channels.data.set_flags('DEAD', np.arange(5))
    # Slim the integration channels, but not the filter
    s1 = f.integration.channels.size
    f.integration.slim()
    s2 = f.integration.channels.size
    assert s1 - s2 == 5

    f.reindex()
    assert f.data is None
    assert f.parms.for_channel.size == s2
    assert np.allclose(f.points, np.arange(5, s1))
    assert np.allclose(f.channels.fixed_index, np.arange(5, s1))

    # Try again with no points
    f.points = None
    f.reindex()
    assert f.points is None
    assert f.channels.size == s2

def test_has_option(configured_filter):
    assert not NoFilter().has_option('level')
    assert configured_filter.has_option('level')

def test_option(configured_filter):
    assert NoFilter().option('level') is None
    assert configured_filter.option('level') == '2.0'

def test_make_temp_data(initialized_filter):
    f = initialized_filter
    f.make_temp_data()
    assert f.data.shape == (121, 2048) and np.allclose(f.data, 0)
    assert f.points.shape == (121,) and np.allclose(f.points, 0)
    f.make_temp_data()  # Do again with existing data and points...
    assert f.data.shape == (121, 2048) and np.allclose(f.data, 0)
    assert f.points.shape == (121,) and np.allclose(f.points, 0)

def test_discard_temp_data(initialized_filter):
    f = initialized_filter
    f.make_temp_data()
    f.discard_temp_data()
    assert f.data is None and f.points is None

def test_is_enabled(initialized_filter):
    f = initialized_filter
    assert not f.is_enabled()
    f.enabled = True
    assert f.is_enabled()

def test_get_temp_data(initialized_filter):
    f = initialized_filter
    f.make_temp_data()
    assert f.get_temp_data() is f.data

def test_set_temp_data(initialized_filter):
    f = initialized_filter
    x = np.zeros(20)
    f.set_temp_data(x)
    assert f.data is x

def test_rejection_at():
    f = NoFilter()
    assert np.allclose(f.rejection_at(np.arange(10)), 0)

def test_count_parms(initialized_filter):
    f = initialized_filter
    assert f.count_parms() == 0
    f.variable = True
    assert np.allclose(f.count_parms(), np.zeros(f.channel_size))

def test_get_channels(initialized_filter):
    f = initialized_filter
    assert f.get_channels().data is f.integration.channels.data

def test_set_channels(initialized_filter):
    channels = initialized_filter.integration.channels.copy()
    f = NoFilter()
    assert f.channels is None
    f.set_channels(None)
    assert f.channels is None

    f.set_channels(channels)
    assert f.channels.data is channels.data
    f.set_integration(initialized_filter.integration.copy())

    f.set_channels(channels.data)
    assert f.channels.data is channels.data
    group = f.channels

    f.set_channels(group)
    assert f.channels is not group and f.channels.data is channels.data

    with pytest.raises(ValueError) as err:
        f.set_channels(1)
    assert "Channels must be" in str(err.value)

def test_set_integration(initialized_filter):
    f = NoFilter()
    integration = initialized_filter.integration.copy()
    f.set_integration(integration)
    assert f.integration is integration
    assert f.nt == 2048
    assert f.nf == 1024
    assert f.df == 0.0048828125
    assert f.channels.data is integration.channels.data

def test_update_config(configured_filter):
    f = NoFilter()
    f.update_config()
    assert not f.enabled and not f.pedantic
    f = configured_filter.copy()
    assert not f.enabled
    assert not f.pedantic
    f.update_config()
    assert f.enabled
    assert not f.pedantic
    f.configuration.parse_key_value('filter.mrproper', 'True')
    f.update_config()
    assert f.enabled
    assert f.pedantic

def test_apply(configured_filter):
    f = NoFilter()
    assert not f.apply()
    assert f.frame_parms is None
    f = configured_filter
    assert f.apply(report=True)
    assert f.integration.comments == ['N', '(1.0)']

def test_apply_to_channels(configured_filter):
    integration = configured_filter.integration.copy()
    f = configured_filter.copy()
    f.set_integration(integration.copy())
    d1 = f.integration.frames.data.copy()
    f.apply_to_channels()
    d2 = f.integration.frames.data.copy()
    assert not np.allclose(d1, d2)
    f = configured_filter.copy()
    f.set_integration(integration.copy())
    f.pedantic = True
    f.dft = True
    f.apply_to_channels()
    d3 = f.integration.frames.data.copy()
    assert np.allclose(d3, d2)
    f = configured_filter.copy()
    f.set_integration(integration.copy())
    f.data = np.zeros((0, 0))
    f.apply_to_channels()
    assert f.data.shape == (121, 2048)

def test_pre_filter(configured_filter):
    f = configured_filter.copy()
    integration = f.integration.copy()
    i1 = integration.copy()
    f.set_integration(i1)
    parms = i1.get_dependents(f.get_config_name())
    parms.for_channel[:] += 1
    parms.for_frame[:] += 2
    assert np.allclose(i1.frames.dependents, 0)
    assert np.allclose(i1.channels.data.dependents, 0)
    f.pre_filter()
    assert np.allclose(i1.frames.dependents, -2)
    assert np.allclose(parms.for_frame, 0)
    assert np.allclose(i1.channels.data.dependents, -1)
    assert np.allclose(parms.for_channel, 0)

    i2 = integration.copy()
    f = configured_filter.copy()
    f.set_integration(i2)
    f.is_sub_filter = True
    parms = i2.get_dependents(f.get_config_name())
    parms.for_channel[:] += 1
    parms.for_frame[:] += 2
    assert np.allclose(i2.frames.dependents, 0)
    assert np.allclose(i2.channels.data.dependents, 0)
    f.pre_filter()
    assert np.allclose(i2.frames.dependents, 0)
    assert np.allclose(parms.for_frame, 0)
    assert np.allclose(i2.channels.data.dependents, 0)
    assert np.allclose(parms.for_channel, 0)

def test_post_filter(configured_filter):
    f = configured_filter.copy()
    integration = f.integration.copy()
    f.set_integration(integration)
    parms = integration.get_dependents(f.get_config_name())
    parms.for_frame[:] = 1
    parms.for_channel[:] = 2
    f.parms = parms
    f.is_sub_filter = True
    f.post_filter()
    assert np.allclose(integration.frames.dependents, 0)
    assert np.allclose(integration.channels.data.dependents, 0)
    f.is_sub_filter = False
    f.post_filter()
    assert np.allclose(integration.frames.dependents, 1)
    assert np.allclose(integration.channels.data.dependents, 2)

def test_remove(configured_filter):
    f = configured_filter.copy()
    f.make_temp_data()
    f.data.fill(1.0)
    d0 = f.integration.frames.data.copy()
    f.remove()
    assert np.allclose(d0, f.integration.frames.data + 1)

def test_remove_from_frames(configured_filter):
    f = configured_filter.copy()
    frames = f.integration.frames
    channels = f.channels
    f.make_temp_data()
    f.data.fill(1)
    d0 = frames.data.copy()
    NoFilter.remove_from_frames(
        rejected_signal=f.data,
        frames=frames,
        channels=channels)
    assert np.allclose(d0, frames.data + 1)

def test_report(configured_filter):
    f = configured_filter.copy()
    integration = f.integration.copy()
    assert len(integration.comments) == 0
    i1 = integration.copy()
    f.set_integration(i1)
    f.report()
    assert i1.comments == ['(1.0)']
    i2 = integration.copy()
    i2.channels.n_mapping_channels = 0
    f.set_integration(i2)
    f.report()
    assert i2.comments == ['(---)']

def test_load_time_streams(configured_filter):
    f = configured_filter.copy()
    assert f.data is None
    f.load_time_streams()
    df = f.data[:, :1100].T
    df0 = f.data.copy()
    di = f.integration.frames.data
    diff = di - df
    assert np.allclose(diff, diff[0, None])  # Just the mean
    assert np.allclose(f.points, 1100)  # no invalid points

    f.data = np.zeros((0, 0))
    f.load_time_streams()
    assert np.allclose(f.data, df0)

def test_fft_filter(kill_filter, configured_filter):
    f = configured_filter
    f.load_time_streams()
    d0 = f.integration.frames.data.copy()
    f0 = f.data.copy()
    f.fft_filter()
    # Nothing happens because no rejection...
    assert np.allclose(f.integration.frames.data, d0)
    assert np.allclose(f.data, f0)

    f = kill_filter.copy()
    integration = f.integration.copy()
    d0 = integration.frames.data.copy()
    f0 = f.data.copy()
    f.fft_filter()
    assert not np.allclose(f.data, f0)
    assert np.allclose(f.integration.frames.data, d0)
    assert np.allclose(f.data[f.integration.size:], 0)  # Zeroed
    f1 = f.data.copy()

    reject = f.reject.copy()
    varied_reject = np.empty((reject.size, f0.shape[0]), dtype=bool)
    varied_reject[...] = reject[:, None].copy()
    f.reject = varied_reject
    f.load_time_streams()
    f.fft_filter()
    assert np.allclose(f.data, f1)  # Should be the same, but 2-D processing.

def test_dft_filter(kill_filter, configured_filter):
    f = configured_filter
    f.load_time_streams()
    d0 = f.integration.frames.data.copy()
    f0 = f.data.copy()
    f.dft_filter()
    # Nothing happens because no rejection...
    assert np.allclose(f.integration.frames.data, d0)
    assert np.allclose(f.data, f0)

    f = kill_filter.copy()
    f0 = f.integration.frames.data.copy()
    freq_channels = np.arange(f.nf + 1)
    f.rejection_at(freq_channels)
    f.dft_filter()
    d1 = f.data.copy()
    # Check that the integration data is unmodified
    assert np.allclose(f0, f.integration.frames.data)

    f = kill_filter.copy()
    f.load_time_streams()
    f.fft_filter()
    d2 = f.data.copy()
    # Check that the DFT is consistent with the FFT
    assert np.allclose(d1, d2)

def test_calc_point_response(kill_filter):
    f = kill_filter.copy()
    # Infinite integration filter time scale
    assert np.isclose(f.calc_point_response(), 0.9723746209)

    # About half way through the filter rejection block
    f.integration.filter_time_scale = 0.9 * units.Unit('second')
    assert np.isclose(f.calc_point_response(), 0.9874574061)

def test_get_high_pass_index(kill_filter):
    f = kill_filter.copy()
    ts = f.integration.filter_time_scale
    assert np.isinf(ts) and ts > 0
    assert f.get_high_pass_index() == 0

    f.integration.filter_time_scale = 0.9 * units.Unit('second')
    assert f.get_high_pass_index() == 114

    f.integration.filter_time_scale = np.nan * units.Unit('second')
    assert f.get_high_pass_index() == 1

def test_level_data_for_channels(kill_filter):
    f = kill_filter.copy()
    f.data += 1
    d0 = f.data.copy()
    f.level_data_for_channels()
    d1 = f.data.copy()
    diff = d0 - d1
    n = f.integration.size
    assert np.allclose(diff[:, :n], 1)
    assert np.allclose(diff[:, n:], 0)

def test_level_for_channels(kill_filter):
    f = kill_filter.copy()
    f.data += 1
    d0 = f.data.copy()
    d1 = d0.copy()
    f.level_for_channels(d1, channels=f.channels)
    diff = d0 - d1
    n = f.integration.size
    assert np.allclose(diff[:, :n], 1)
    assert np.allclose(diff[:, n:], 0)

    d1 = d0.copy()
    f.level_for_channels(d1, channels=f.channels.copy())
    diff = d0 - d1
    assert np.allclose(diff[:, :n], 1)
    assert np.allclose(diff[:, n:], 0)

def test_level_data(kill_filter):
    f = kill_filter.copy()
    f.data += 1
    d0 = f.data.copy()
    f.level_data()
    d1 = f.data.copy()
    diff = d0 - d1
    n = f.integration.size
    assert np.allclose(diff[:, :n], 1)
    assert np.allclose(diff[:, n:], 0)

def test_level(kill_filter):
    f = kill_filter.copy()
    signal = np.ones(f.integration.size)
    f.level(signal)
    assert np.allclose(signal, 0)
    signal = (np.arange(3) + 1)[:, None] * np.ones(f.integration.size)[None]
    f.level(signal)
    assert np.allclose(signal, 0) and signal.shape == (3, f.integration.size)

def test_set_dft(kill_filter):
    f = kill_filter.copy()
    assert f.dft
    f.set_dft(False)
    assert not f.dft


import os.path
import re
import time
import urllib.request

import pandas as pd
from selenium.webdriver.common.by import By

from Crawling._001_extract_numbers._001_sm3.v1.BrowserModule import Browser, user_path
from Crawling._001_extract_numbers._001_sm3.v1._001_setup_train_6_numbers import CaptchaModelSix

service = Browser(view_mode=True)
d = service.d
all_info_df = pd.read_csv(user_path("gb_url.csv"))
all_account_df = pd.read_csv(user_path("account.csv"))
info_df = all_info_df.iloc[0]
account_df = all_account_df.iloc[0]
url = info_df.access_url

def get_captcha_img():
    d.get(url)

    opener = urllib.request.URLopener()
    opener.addheader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 '
                                   'Firefox/108.0')
    opener.addheader('Referer', url)
    opener.addheader('Host', info_df.host)
    get = d.get_cookie("PHPSESSID").get("value")
    print(get)
    opener.addheader('Cookie',
                     f'PHPSESSID={get}')
    kaptcha_img = d.find_element(By.CSS_SELECTOR, info_df.img_selector)
    opener.retrieve(kaptcha_img.get_attribute("src"),
                    "delete_me.png")
    return "delete_me.png"

def login():
    captcha_model = CaptchaModelSix()

    d.get(info_df.access_url)

    while True:
        img = get_captcha_img()
        _result = captcha_model.predict(img)
        print(_result)
        if len(re.sub("[^0-9]", "", _result)) == 6:
            break

    print(_result)
    service.wait_selector("#userid").send_keys(account_df.id)
    service.wait_selector("#userpw").send_keys(account_df.pw)
    service.wait_selector(".codes").send_keys(_result)
    service.wait_selector(".btn_login > span:nth-child(1)").click()
    time.sleep(1)

    # d.find_element(By.TAG_NAME, "body").send_keys(Keys.ENTER)
    # time.sleep(1)

    if os.path.exists('delete_me.png'):
        os.system("rm -f delete_me.png")

if __name__ == '__main__':
    login()


"""Grid generators for calendars"""

import math
from dataclasses import dataclass
from datetime import date, timedelta
import calendar
from typing import Optional, List, Set, Dict

HEADERMAPPING = {
    0: "Monday",
    1: "Tuesday",
    2: "Wednesday",
    3: "Thursday",
    4: "Friday",
    5: "Saturday",
    6: "Sunday",
}

@dataclass
class GridItem:
    """A date/letter pair in a grid"""

    date: date
    letter: Optional[str] = None
    label: Optional[str] = None

@dataclass
class CalendarGrid:
    """Everything that is needed to draw a calendar grid"""

    title: str
    headers: List[str]  # Monday, Tuesday, Wednesday
    grid: List[List[Optional[GridItem]]]  # 2d calendar view of grid items

@dataclass
class CalendarGridGenerator:
    """A generator that makes a calendar grid"""

    date_letter_map: Dict[date, Optional[str]]
    label_map: Dict[date, str]

    start_date: date
    end_date: date

    week_start: int = 6

    custom_title: Optional[str] = None

    def get_used_weekdays(self) -> Set[int]:
        """The weekdays that have been used by all the dates together"""

        out = set()

        for letter_date in self.date_letter_map.keys():
            out.add(letter_date.weekday())

        for label_date in self.label_map.keys():
            out.add(label_date.weekday())

        return out

    def get_grid(self) -> CalendarGrid:
        """The 2d grid calendar view of days and times"""

        used_weekdays = self.get_used_weekdays()

        # All grid items and headers will be referenced off the calendar object, for start of week consistency
        cal = calendar.Calendar(firstweekday=self.week_start)

        # The weekdays as they should be in columns across our calendar
        ordered_weekdays = [
            weekday for weekday in cal.iterweekdays() if weekday in used_weekdays
        ]

        # Monday, Tuesday, Wednesday - just those weekdays that were used in this calendar
        used_headers = [HEADERMAPPING[weekday] for weekday in ordered_weekdays]

        out = CalendarGrid(headers=used_headers, grid=[], title=self.title)

        # Walk backwards until we get our first weekday
        internal_start_date = self.start_date

        # This is a fix to the Jan 2022 issue - the first week was all None because
        # Jan 1st was on a Saturday, which then got walked back to December 27, which
        # then all got excluded because none of the dates were in range

        # Walk forwards until we hit a day of the week we care about
        while internal_start_date.weekday() not in ordered_weekdays:
            internal_start_date += timedelta(days=1)

        # Walk backwards until we hit the start of the week
        target_weekday = ordered_weekdays[0]
        while internal_start_date.weekday() != target_weekday:
            internal_start_date -= timedelta(days=1)

        # The plus one is for the Jan 2022 edge condition, and the extra row gets
        # excluded at the end if it was added
        total_weeks = math.ceil((self.end_date - internal_start_date).days / 7) + 1
        full_grid = []
        for week_index in range(total_weeks):
            week_start = internal_start_date + timedelta(days=week_index * 7)
            row: List[Optional[date]] = []
            for day_index in range(len(used_weekdays)):
                cell_date = week_start + timedelta(days=day_index)

                if cell_date >= self.start_date and cell_date <= self.end_date:
                    row.append(cell_date)
                else:
                    row.append(None)

            if row != [None] * len(row):
                # Make sure we don't dump complete nothing rows in - account
                # for the Jan 2022 special case
                full_grid.append(row)

        # Now we have a date corresponding to the actual first day that might be on our calendar,
        # even though we might not display that date because it's outside of our range

        # Create the grid we're going to be using

        def get_entry(date_val: Optional[date]) -> Optional[GridItem]:
            """
            Get the entry for this item in the calendar grid
            Will either be a grid item, or a None if it's out of our month range
            """

            if not date_val:
                return None

            letter = self.date_letter_map.get(date_val)
            label = self.label_map.get(date_val)

            return GridItem(date_val, letter, label)

        for week in full_grid:
            week_entries = [get_entry(d) for d in week]
            if week_entries != [None] * len(used_weekdays):
                # The calendar grid can generate a phantom unused row over an unused day - don't keep it
                out.grid.append(week_entries)

        return out

    @property
    def title(self) -> str:
        """Get the title of a calendar with this grid"""

        if self.custom_title:
            return self.custom_title

        if (
            self.start_date.year == self.end_date.year
            and self.start_date.month == self.end_date.month
        ):
            sample_date = date(self.start_date.year, self.end_date.month, 1)

            return sample_date.strftime("%B %Y")

        start_date_str = self.start_date.strftime("%b %d, %Y")
        end_date_str = self.end_date.strftime("%b %d, %Y")

        return f"{start_date_str} to {end_date_str}"


import sys
import os
sys.path.append(os.getcwd() + "/..")
from Intcode_computer import *
from Geometry import *

class Tile(IntEnum):
    empty = 0
    wall = 1
    block = 2
    paddle = 3
    ball = 4

class State(IntEnum):
    read_x = 0
    read_y = 1
    read_tile_id = 2

def next_state(s):
    return State((s + 1) % 3)

grid = {}
s = State.read_x

def out_func(out):
    global grid, x, y, s
    if s == State.read_x:
        x = out
    elif s == State.read_y:
        y = out
    elif s == State.read_tile_id:
        grid[Point2D(x, y)] = Tile(out)
    else:
        raise ValueError("Unknown state!")
    s = next_state(s)

# reading input
with open("input.txt", "r") as file:
    A = [int(a) for a in file.read().split(',')]

# solving Part 1
p = program(A, None, out_func)
p.exec()
print("Part 1:", sum(1 for val in grid.values() if val == Tile.block))

# solving Part 2
paddlex = next(key for key, val in grid.items() if val == Tile.paddle).x
def sgn(x):
    if x > 0:
        return 1
    elif x == 0:
        return 0
    else:
        return -1

# moving paddle towards the ball
def in_func2():
    global ball, paddlex
    direction = sgn(x - paddlex)
    paddlex += direction
    return direction

def out_func2(out):
    global s, x, y, score
    if s == State.read_x:
        x = out
    elif s == State.read_y:
        y = out
    elif s == State.read_tile_id:
        score = out
    else:
        raise ValueError("Unknown state!")
    s = next_state(s)

s = State.read_x
A[0] = 2
p = program(A, in_func2, out_func2)
p.exec()
print("Part 2:", score)


# -*- coding: utf-8 -*-
import json
import re
import os

DEFULT_WORD_FILES = [r"./he-data/nouns.txt", r"./he-data/verbs.txt"]

def create_dict(files):
    word_dict = {}
    key_words = set()
    CHARS_PATTERN = re.compile(r"""[^אבגדהוזחטיכלמנסעפצקרשתןףץםך"]""")
    for file_name in files:
        with open(file_name, 'r', encoding="utf-8") as file:
            file_text = file.read()
            word_sections = [l for l in file_text.split("\n-") if l != []]
            for word_types in word_sections:
                word_types_arr = word_types.split("\n")
                word_types_arr = [CHARS_PATTERN.sub('', word) for word in word_types_arr if CHARS_PATTERN.sub('', word) != ""]
                if len(word_types_arr):
                    base_word = word_types_arr[0]
                    key_words.add(base_word)
                    for word in word_types_arr:
                        if word not in key_words and word not in word_dict or word == base_word:
                            word_dict[word] = base_word

    return word_dict

def write_dict_to_file(dict_name, files):
    with open(dict_name, "w") as outfile:
        word_dict = create_dict(files)
        json.dump(word_dict, outfile)

class HebrewProcessor:
    def __init__(self, files = DEFULT_WORD_FILES):
        self.word_dict = create_dict(files)

    def process_word(self, word):
        PREFIXES = ["ו", "ה", "ב", "ש", "כ", "ל", "מ", "מה", "מש", "וה", "וש", "ומ", "ול", "וב"]

        if word in self.word_dict:
            return self.word_dict[word]

        for pref in PREFIXES:
            no_pref = word[word.startswith(pref) and len(pref):]
            if no_pref in self.word_dict:
                return self.word_dict[no_pref]

        return word


from django.contrib import messages
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, redirect
from .forms import UnitCreateForm
from .models import Unit

# Create your views here.
def create(request):
    form = UnitCreateForm(request.POST or None)
    context = {}
    if request.method == "POST":
        if form.is_valid():
            form.save()
            return redirect("units:list")

    context['form'] = form
    return render(request, 'unit/create.html', context)

def update(request, pk):
    context = {}
    try:
        data = Unit.objects.get(id=pk)
    except Unit.DoesNotExist:
        messages.error(request, "Object not found.")
        return redirect("units:list")

    form = UnitCreateForm(instance=data)
    if request.method == "POST":
        form = UnitCreateForm(request.POST, instance=data)
        if form.is_valid():
            form.save()
            return redirect("units:list")

    context['form'] = form
    return render(request, 'unit/create.html', context)

def list(request):
    context = {}
    data = Unit.objects.all()

    per_page = 2
    paginator = Paginator(data, per_page)
    page = request.GET.get('page')
    try:
        data = paginator.page(page)
    except PageNotAnInteger:
        data = paginator.page(1)
    except EmptyPage:
        data = paginator.page(paginator.num_pages)
    context['data'] = data
    return render(request, 'unit/list.html', context)

# def detail(request, pk):
#     context = {}
#     try:
#         data = Unit.objects.get(id=pk)
#     except Unit.DoesNotExist:
#         messages.error(request, "Details not found")
#         return redirect("units:create")
#
#     context['data'] = data
#     return render(request, 'unit/detail.html', context)

def delete(request, pk):
    try:
        data = Unit.objects.get(id=pk)
    except Unit.DoesNotExist:
        messages.error(request, "Data not found")
        return redirect("units:list")
    data.delete()
    messages.success(request, 'Deleted successfully.')
    return redirect("units:list")


from lxml import etree
import openerp.addons.decimal_precision as dp

from openerp.osv import osv
from openerp.osv import fields
from openerp.tools.translate import _
import time
from openerp.addons.workflow_info import workflow_func

class designer_order(osv.osv):
    """ 工单"""
    _name = 'designer.order'
    _description = "designer_order"
    _inherit = ['mail.thread']

    def _get_seq(self, cr, uid, ids, context=None):
        return self.pool.get('ir.sequence').get(cr, uid, 'designer.order')

    _columns = {
        'file_id': fields.many2one('ir.attachment', '客户签字', required=False, select=1),
        'work_id': fields.many2one('designer.card', '所属工作卡', change_default=True, select=True, track_visibility='always'),
        'name': fields.char('工单编号', required=True,track_visibility='always'),
        'order_line': fields.one2many('designer.order.line', 'order_id', '制作明细',track_visibility='always' ),
        'project_id': fields.many2one('designer.project', string='项目简报',track_visibility='always'),

        #取自工作卡
        'partner_id': fields.related(
            'work_id', #关联字段
            'partner_id', #工作卡对象字段
            string='客户',
            type='many2one',
            relation='res.partner',
            readonly=True,
            store=True
        ),
        # 'partner_id':fields.related(
        #     'project_id',#关联字段
        #     'partner_id',#项目简报的
        #     string='客户',
        #     type='many2one',
        #     relation='res.partner',
        #     readonly=True,
        #     store=True
        #  ),
        'state': fields.selection([
            ('draft', '草稿中'),
            ('open', '已批准'),
            ('verify1', '一次确认'),
            ('verify2', '二次确认'),
            ('cancel', '已拒绝')],
            '状态', readonly=True, track_visibility='onchange',
        ),
        'project_type': fields.selection([('print', '印刷'),
            ('logo', 'LOGO'),
            ('ad', '广告'),],
            '项目类型', track_visibility='onchange',
        ),
        #工作流审批以及记录
        'wkf_logs':fields.function(
            workflow_func._get_workflow_logs,
            string='审批记录',
            type='one2many',
            relation="workflow.logs",
            readonly=True),
    }
    _rec_name = 'name'
    _sql_constraints = [
        ('order_no', 'unique(order_no)', u'工单编号必须唯一')
    ]
    _defaults = {
        'state': lambda *a: 'draft',
        'name':_get_seq
    }
    _order = 'name asc'

    def designer_order_draft(self, cr, uid, ids, context={}):
        return self.write(cr, uid, ids, {'state': 'draft'}, context=context)

    def designer_order_cancel1(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'cancel'}, context=context)

    def designer_order_cancel2(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'cancel'}, context=context)

    def designer_order_open(self, cr, uid, ids, context={}):
        return self.write(cr, uid, ids, {'state': 'open'}, context=context)

    def designer_order_verify1(self, cr, uid, ids, context={}):
        return self.write(cr, uid, ids, {'state': 'verify1'}, context=context)

    def designer_order_verify2(self, cr, uid, ids, context={}):
        return self.write(cr, uid, ids, {'state': 'verify2'}, context=context)

class designer_order_line(osv.osv):
    """ 工单制作明细"""
    _name = 'designer.order.line'
    _inherit = ['mail.thread']
    _columns = {
        'order_id': fields.many2one('designer.order', '工单', ondelete='cascade', select=True),#关联id
        'line_no': fields.char('编号', required=True,change_default=True, select=True, track_visibility='always'),
        #'project_request': fields.text('项目要求', size=64, required=True, change_default=True, select=True, track_visibility='always'),
        'number': fields.integer('数量', required=True, change_default=True, select=True, track_visibility='always'),
        'p_number': fields.integer('P数', required=True, change_default=True, select=True, track_visibility='always'),
        'size': fields.float('尺寸',digits_compute= dp.get_precision('Price'), required=True, change_default=True, select=True, track_visibility='always'),
        'face_page': fields.float('封面纸张(g)',digits_compute= dp.get_precision('Price'), required=True, change_default=True, select=True, track_visibility='always'),
        'inside_page': fields.float('内页纸张(g)',digits_compute= dp.get_precision('Price'), required=True, change_default=True, select=True, track_visibility='always'),
        'note': fields.text('备注',size=64,change_default=True, select=True, track_visibility='always'),
        'content': fields.text('项目内容',size=64,change_default=True, select=True, track_visibility='always'),
        'binding_style': fields.char('装订方式',size=64,change_default=True, select=True, track_visibility='always'),
        'other_craft': fields.char('其他工艺',size=64,change_default=True, select=True, track_visibility='always'),
        'date':fields.datetime('交货时间',required=True),
        'print_type': fields.selection([
            ('one', '单'),
            ('both', '双'),
            ('no', '否'),],
            '封面印刷', track_visibility='onchange',
        )
    }
    _sql_constraints = [
       # ('line_no', 'unique(line_no)', 'The name of the idea must be unique')
    ]
    _defaults = {
    }
    _order = 'line_no asc'


# Functional test for uart module
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import Timer,RisingEdge,FallingEdge,ClockCycles,ReadWrite
from cocotb.result import TestFailure
import random
from cocotb_coverage.coverage import CoverPoint,coverage_db

covered_valued = []

g_sys_clk = int(cocotb.top.g_sys_clk)
period_ns = 10**9 / g_sys_clk
g_word_width = int(cocotb.top.g_data_width)

full = False
def notify():
	global full
	full = True

async def connect_tx_rx(dut):
	while full != True:
		await RisingEdge(dut.i_clk)
		dut.i_rx.value = dut.o_tx.value

# at_least = value is superfluous, just shows how you can determine the amount of times that
# a bin must be hit to considered covered
@CoverPoint("top.i_data",xf = lambda x : x.i_data.value, bins = list(range(2**g_word_width)), at_least=1)
def number_cover(dut):
	covered_valued.append(int(dut.i_data.value))

async def reset(dut,cycles=1):
	dut.i_arstn.value = 0
	dut.i_we.value = 0
	dut.i_stb.value = 0
	dut.i_data.value = 0
	dut.i_rx.value = 0

	await ClockCycles(dut.i_clk,cycles)
	dut.i_arstn.value = 1
	await RisingEdge(dut.i_clk)
	dut._log.info("the core was reset")

@cocotb.test()
async def test(dut):
	"""Check results and coverage for UART"""

	cocotb.start_soon(Clock(dut.i_clk, period_ns, units="ns").start())
	await reset(dut,5)
	cocotb.start_soon(connect_tx_rx(dut))

	expected_value = 0
	rx_data = 0

	# configure UART core via interface
	# set databits(8), stopbits(1), parity_en(1), parity_type etc..
	dut.i_stb.value = 1
	dut.i_addr.value = 1
	dut.i_we.value = 1
	dut.i_data.value = 11

	await RisingEdge(dut.i_clk)

	while(full != True):
		data = random.randint(0,2**g_word_width-1)
		while(data in covered_valued):
			data = random.randint(0,2**g_word_width-1)
		expected_value = data

		dut.i_stb.value = 1
		dut.i_we.value = 1
		dut.i_addr.value = 0
		dut.i_data.value = data

		await RisingEdge(dut.i_clk)
		dut.i_stb.value = 0
		await FallingEdge(dut.o_rx_done)

		dut.i_stb.value = 1
		dut.i_we.value = 0
		dut.i_addr.value = 0

		await ClockCycles(dut.i_clk,2)	#1 cycle to register read rd_rbr command, 1 cycle to copy rbr to o_data

		assert not (expected_value != int(dut.o_data.value)),"Different expected to actual read data"
		coverage_db["top.i_data"].add_threshold_callback(notify, 100)
		number_cover(dut)

	coverage_db.report_coverage(cocotb.log.info,bins=True)
	coverage_db.export_to_xml(filename="coverage.xml")


import numpy as np
import cv2
import matplotlib.pyplot as plt
import helpers
import time

def detect_face(img):
  # img = img.copy()

  face_cas = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')

  face_recs = face_cas.detectMultiScale(img, scaleFactor=1.2 , minNeighbors=9)

  for (x,y,w,h) in face_recs:
    # cv2.rectangle(img, (x,y), (x+w,y+h), (255), 1)
    # separate face to blur
    # face_rec = img[y:y+h,x:x+w]
    # blur face
    # face_rec = cv2.medianBlur(face_rec, 19)
    # replace face with blurred face
    # img[y:y+h,x:x+w] = face_rec

    return [x,y,x+w,y+h]

tracker = cv2.TrackerTLD_create()
tracker_name = str(tracker).split()[0][1:]

# Read video
cap = cv2.VideoCapture(0)

# Read first frame.
ret, frame = cap.read()

# Special function allows us to draw on the very first frame our desired ROI
roi = cv2.selectROI(frame, False)
roi2 = tuple(detect_face(frame))
print(roi)
print(roi2)
# roi = tuple(detect_face(frame))

# Initialize tracker with first frame and bounding box
ret = tracker.init(frame, roi)

print(type(roi))

while True:
    # Read a new frame
    ret, frame = cap.read()

    # Update tracker
    success, roi = tracker.update(frame)

    # roi variable is a tuple of 4 floats
    # We need each value and we need them as integers
    (x,y,w,h) = tuple(map(int,roi))

    # Draw Rectangle as Tracker moves
    if success:
        # Tracking success
        p1 = (x, y)
        p2 = (x+w, y+h)
        cv2.rectangle(frame, p1, p2, (0,255,0), 3)
    else :
        # Tracking failure
        cv2.putText(frame, "Cannot see a face :(", (100,200), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),3)

    # Display tracker type on frame
    cv2.putText(frame, tracker_name, (20,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),3)

    # Display result
    cv2.imshow(tracker_name, frame)

    # Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27 :
        break

cap.release()
cv2.destroyAllWindows()


# https://www.codewars.com/kata/5bb904724c47249b10000131?utm_source=newsletter&utm_medium=email&utm_campaign=weekly_coding_challenges&utm_term=2020-04-28

def points(games):
    points = 0
    for match in games:
        x = int(match[0])
        y = int(match[2])
        points += points_match(x, y)
    return points

def points_match(x, y):
    points = 1
    if x>y:
        points=3
    if x<y:
        points=0
    return points


from collections import namedtuple

import torch
from torch import nn
import lpips
import torch.nn.functional as F

def make_image_processor(config):
    type = config.get("type", "RGB").lower()
    if type == "rgb":
        ip = RGBProcessor()
    elif type == "perceptual":
        ip = PerceptualProcessor(config.get("layers", 1))
    elif type == "patch":
        ip = PatchProcessor(config.get("patch_size", 3))
    else:
        raise NotImplementedError(f"Unsupported image processor type: {type}")
    return ip

class RGBProcessor(nn.Module):
    def __init__(self):
        super().__init__()
        self.channels = 3

    def forward(self, images):
        images = images * .5 + .5
        return images

class PerceptualProcessor(nn.Module):
    def __init__(self, layers=1) -> None:
        super().__init__()
        self.lpips_module = lpips.LPIPS(net="vgg")
        self._layers = layers
        self.channels = sum(self.lpips_module.chns[:self._layers])

    def forward(self, images):
        n, v, c, h, w = images.shape
        images = images.view(n*v, c, h, w)

        in_input = self.lpips_module.scaling_layer(images)

        x = self.lpips_module.net.slice1(in_input)
        h_relu1_2 = x
        x = self.lpips_module.net.slice2(x)
        h_relu2_2 = x
        x = self.lpips_module.net.slice3(x)
        h_relu3_3 = x

        vgg_outputs = namedtuple("VggOutputs", ['relu1_2', 'relu2_2', 'relu3_3'])
        outs = vgg_outputs(h_relu1_2, h_relu2_2, h_relu3_3)

        feats = []

        for kk in range(self._layers):
            f = lpips.normalize_tensor(outs[kk])
            f = F.interpolate(f, (h, w))
            feats.append(f)

        feats = torch.cat(feats, dim=1)

        feats = feats.view(n, v, self.channels, h, w)

        return feats

class PatchProcessor(nn.Module):
    def __init__(self, patch_size) -> None:
        super().__init__()
        self.patch_size = patch_size
        self.channels = 3 * (patch_size ** 2)

        self._hps = self.patch_size // 2

    def forward(self, images):
        n, v, c, h, w = images.shape
        images = images.view(n*v, c, h, w) * .5 + .5

        images = F.pad(images, pad=(self.patch_size // 2,)*4, mode="replicate")
        h_, w_ = images.shape[-2:]

        parts = []

        for y in range(0, self.patch_size):
            for x in range(0, self.patch_size):
                parts.append(images[:, :, y:h_-(self.patch_size - y - 1), x:w_-(self.patch_size - x - 1)])

        patch_images = torch.cat(parts, dim=1)
        patch_images = patch_images.view(n, v, self.channels, h, w)

        return patch_images

class AutoMaskingWrapper(nn.Module):

    # Adds the corresponding color from the input frame for reference
    def __init__(self, image_processor):
        super().__init__()
        self.image_processor = image_processor

        self.channels = self.image_processor.channels + 1

    def forward(self, images, threshold):
        n, v, c, h, w = images.shape
        processed_images = self.image_processor(images)
        thresholds = threshold.view(n, 1, 1, h, w).expand(n, v, 1, h, w)
        processed_images = torch.stack((processed_images, thresholds), dim=2)
        return processed_images


from tkinter import *
import random

c=['green','yellow','blue','black','red','orange','purple','pink','brown']
sc=0
tleft=30
n=0
def startGame(event):

    if tleft==30:
        countdown()
    nextcolour()

def nextcolour():

    global n,sc,tleft
    if tleft>0:
         n+=1
         b1.focus_set()

         if b1.get()==c[1]:

             sc+=1
         b1.delete(0,'end')
         random.shuffle(c)
         word.config(fg=str(c[1]),text=str(c[0]))
         print(c[1],c[0])
         s.config(text="score:"+str(sc))
         t1.config(text="total number of attempts:"+str(n))

def countdown():
    global tleft
    if tleft>0:
        tleft-=1
        t.config(text="time left:"+str(tleft))
        t.after(1000,countdown)

w=Tk()
w.geometry("400x400")
w.configure(bg="grey")
w.title("colour game")

l=Label(w,text="write the colour of word not the word in given space",font=('Helvetica',12))
l.pack()
s=Label(w,text="press enter to start",font=('Arial',12))
s.pack()
t=Label(w,text="time left:"+str(tleft),font=('Arial',12))
t.pack()
t1=Label(w,font=('Arial',12))
t1.pack()
word=Label(w,font=('Arial',20))
word.pack()
b2=Button(w,text="press",command=lambda:startGame(True))
b2.pack()

b1=Entry(w)

b1.pack()
b1.focus_set()

w.mainloop()


import rospy
import rospkg
import os
import sys
import traceback
import json
from stable_baselines3 import PPO
from stable_baselines3.common.utils import get_device

from rosnav.srv import GetAction, GetActionResponse
from rosnav.rosnav_space_manager.rosnav_space_manager import RosnavSpaceManager

from rosnav import *
sys.modules["rl_agent"] = sys.modules["rosnav"]
sys.modules["rl_utils.rl_utils.utils"] = sys.modules["rosnav.utils"]

class RosnavNode:
    def __init__(self):
        # Agent name and path
        self.agent_name = rospy.get_param("agent_name")
        self.agent_path = self._get_model_path(self.agent_name)

        assert os.path.isdir(self.agent_path), f"Model cannot be found at {self.agent_path}"

        # Load hyperparams
        self._hyperparams = self._load_hyperparams(self.agent_path)
        # rospy.set_param("/actions_in_obs", self._hyperparams.get("actions_in_observationspace", False))

        self._obs_structure = self._get_observation_space_structure(self._hyperparams)

        # Set RosnavSpaceEncoder as Middleware
        self._encoder = RosnavSpaceManager()

        # Load the model
        self._agent = self._get_model(self.agent_path)

        self._get_next_action_srv = rospy.Service(
            "rosnav/get_action", GetAction, self._handle_next_action_srv
        )

    def _handle_next_action_srv(self, request):
        observation = self._encoder.encode_observation({
            "goal_in_robot_frame": request.goal_in_robot_frame,
            "laser_scan": request.laser_scan,
            "last_action": request.last_action
        }, self._obs_structure)

        action = self._agent.predict(observation, deterministic=True)[0]

        decoded_action = self._encoder.decode_action(action)

        response = GetActionResponse()
        response.action = decoded_action

        return response

    def _get_model(self, agent_path):
        action_state_sizes = [0, 3]

        for size in action_state_sizes:
            rospy.set_param(rospy.get_namespace() + "action_state_size", size)
            try:
                return PPO.load(os.path.join(agent_path, "best_model.zip")).policy
            except:
                pass

        rospy.signal_shutdown("")

    def _get_model_path(self, model_name):
        return os.path.join(
            rospkg.RosPack().get_path("rosnav"),
            "agents",
            model_name
        )

    def _load_hyperparams(self, agent_path):
        with open(os.path.join(agent_path, "hyperparameters.json")) as file:
            return json.load(file)

    def _get_observation_space_structure(self, hyperparams):
        structure = hyperparams.get("observation_space", ["laser_scan", "goal_in_robot_frame"])

        return structure

if __name__ == "__main__":
    rospy.init_node("rosnav_node")

    node = RosnavNode()

    while not rospy.is_shutdown():
        rospy.spin()


import pytest

_ = pytest.importorskip("duckdb.experimental.spark")

from duckdb.experimental.spark.sql.types import (
    LongType,
    StructType,
    BooleanType,
    StructField,
    StringType,
    IntegerType,
    LongType,
    Row,
    ArrayType,
    MapType,
)
from duckdb.experimental.spark.sql.functions import col, struct, when, lit, array_contains
import duckdb
import re

class TestDataFrameFilter(object):
    def test_dataframe_filter(self, spark):
        data = [
            (("James", "", "Smith"), ["Java", "Scala", "C++"], "OH", "M"),
            (("Anna", "Rose", ""), ["Spark", "Java", "C++"], "CA", "F"),
            (("Julia", "", "Williams"), ["CSharp", "VB"], "OH", "F"),
            (("Maria", "Anne", "Jones"), ["CSharp", "VB"], "NY", "M"),
            (("Jen", "Mary", "Brown"), ["CSharp", "VB"], "NY", "M"),
            (("Mike", "Mary", "Williams"), ["Python", "VB"], "OH", "M"),
        ]

        schema = StructType(
            [
                StructField(
                    'name',
                    StructType(
                        [
                            StructField('firstname', StringType(), True),
                            StructField('middlename', StringType(), True),
                            StructField('lastname', StringType(), True),
                        ]
                    ),
                ),
                StructField('languages', ArrayType(StringType()), True),
                StructField('state', StringType(), True),
                StructField('gender', StringType(), True),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # --- Tests ---

        # Using equals condition
        df2 = df.filter(df.state == "OH")
        res = df2.collect()
        assert res[0].state == 'OH'

        # not equals condition
        df2 = df.filter(df.state != "OH")
        df2 = df.filter(~(df.state == "OH"))
        res = df2.collect()
        for item in res:
            assert item.state == 'NY' or item.state == 'CA'

        df2 = df.filter(col("state") == "OH")
        res = df2.collect()
        assert res[0].state == 'OH'

        df2 = df.filter("gender == 'M'")
        res = df2.collect()
        assert res[0].gender == 'M'

        df2 = df.filter("gender != 'M'")
        res = df2.collect()
        assert res[0].gender == 'F'

        df2 = df.filter("gender <> 'M'")
        res = df2.collect()
        assert res[0].gender == 'F'

        # Filter multiple condition
        df2 = df.filter((df.state == "OH") & (df.gender == "M"))
        res = df2.collect()
        assert len(res) == 2
        for item in res:
            assert item.gender == 'M' and item.state == 'OH'

        # Filter IS IN List values
        li = ["OH", "NY"]
        df2 = df.filter(df.state.isin(li))
        res = df2.collect()
        for item in res:
            assert item.state == 'OH' or item.state == 'NY'

        # Filter NOT IS IN List values
        # These show all records with NY (NY is not part of the list)
        df2 = df.filter(~df.state.isin(li))
        res = df2.collect()
        for item in res:
            assert item.state != 'OH' and item.state != 'NY'

        df2 = df.filter(df.state.isin(li) == False)
        res2 = df2.collect()
        assert res2 == res

        # Using startswith
        df2 = df.filter(df.state.startswith("N"))
        res = df2.collect()
        for item in res:
            assert item.state == 'NY'

        # using endswith
        df2 = df.filter(df.state.endswith("H"))
        res = df2.collect()
        for item in res:
            assert item.state == 'OH'

        # contains
        df2 = df.filter(df.state.contains("H"))
        res = df2.collect()
        for item in res:
            assert item.state == 'OH'

        data2 = [(2, "Michael Rose"), (3, "Robert Williams"), (4, "Rames Rose"), (5, "Rames rose")]
        df2 = spark.createDataFrame(data=data2, schema=["id", "name"])

        # like - SQL LIKE pattern
        df3 = df2.filter(df2.name.like("%rose%"))
        res = df3.collect()
        assert res == [Row(id=5, name='Rames rose')]

        # rlike - SQL RLIKE pattern (LIKE with Regex)
        # This check case insensitive
        df3 = df2.filter(df2.name.rlike("(?i)^*rose$"))
        res = df3.collect()
        assert res == [Row(id=2, name='Michael Rose'), Row(id=4, name='Rames Rose'), Row(id=5, name='Rames rose')]

        df2 = df.filter(array_contains(df.languages, "Java"))
        res = df2.collect()
        assert res == [
            Row(
                name={'firstname': 'James', 'middlename': '', 'lastname': 'Smith'},
                languages=['Java', 'Scala', 'C++'],
                state='OH',
                gender='M',
            ),
            Row(
                name={'firstname': 'Anna', 'middlename': 'Rose', 'lastname': ''},
                languages=['Spark', 'Java', 'C++'],
                state='CA',
                gender='F',
            ),
        ]

        df2 = df.filter(df.name.lastname == "Williams")
        res = df2.collect()
        assert res == [
            Row(
                name={'firstname': 'Julia', 'middlename': '', 'lastname': 'Williams'},
                languages=['CSharp', 'VB'],
                state='OH',
                gender='F',
            ),
            Row(
                name={'firstname': 'Mike', 'middlename': 'Mary', 'lastname': 'Williams'},
                languages=['Python', 'VB'],
                state='OH',
                gender='M',
            ),
        ]


# -*- coding: utf-8 -*-
"""
Created on Fri May  1 10:48:30 2020

@author: Rushi
"""

import tkinter as tk
window=tk.Tk()
window.geometry('300x300')
window.resizable(0,0)

l1=tk.Label(window,text='a1= ')
l1.grid(column=0,row=0)
a1=tk.Entry(window)
a1.grid(column=1,row=0)
l2=tk.Label(window,text='a2= ')
l2.grid(column=0,row=1)
a2=tk.Entry(window)
a2.grid(column=1,row=1)
l3=tk.Label(window,text="Result")
l3.grid(row=2,column=0)
t3=tk.Entry(window)
t3.grid(row=2,column=1)
a1.insert(0,0)
a2.insert(0,0)
def clr():
    a1.delete(0,"end")
    a2.delete(0,"end")
    t3.delete(0,"end")
tk.Button(window,text='C',command=clr).grid(column=2,row=2)
def add():
    t3.delete(0,"end")
    a=int(a1.get())
    b=int(a2.get())
    c=a+b
    t3.insert(1,c)

def minu():
    t3.delete(0,"end")
    a=int(a1.get())
    b=int(a2.get())
    c=a-b
    t3.insert(1,c)

def mult():
    t3.delete(0,"end")
    a=int(a1.get())
    b=int(a2.get())
    c=a*b
    t3.insert(1,c)

def div():
    t3.delete(0,"end")
    a=int(a1.get())
    b=int(a2.get())
    c=a/b
    t3.insert(1,c)

def pow1():
    t3.delete(0,"end")
    a=int(a1.get())
    b=int(a2.get())
    c=a**b
    t3.insert(1,c)

l=tk.Label(window,text='').grid(column=0,row=3)
tk.Button(window,text='+',command=add).grid(column=0,row=4)
tk.Button(window,text='-',command=minu).grid(column=1,row=4)
tk.Button(window,text='*',command=mult).grid(column=2,row=4)
l=tk.Label(window,text='').grid(column=0,row=5)
tk.Button(window,text='/',command=div).grid(column=0,row=6)
tk.Button(window,text='^',command=pow1).grid(column=1,row=6)
window.mainloop()


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

try:
    driver.get(url='https://www.youtube.com')
    input_element = driver.find_element(By.XPATH, '//input[@id="search"]')
    time.sleep(3)
    input_element.send_keys('запрос')
    time.sleep(3)
    search_button = driver.find_element(By.XPATH, '//button[@id="search-icon-legacy"]')
    search_button.click()
    time.sleep(3)
    driver.get(url='https://ru.wikipedia.org/')
    wiki_search = driver.find_element(By.CLASS_NAME, 'vector-search-box-input').send_keys('Польша')
    # search_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'searchButton')))
    time.sleep(3)
    search_button = driver.find_element(By.CLASS_NAME, 'searchButton').click()
    time.sleep(3)
    driver.get(url='https://www.google.com')
    google_search = driver.find_element(By.CSS_SELECTOR, '#input')
except Exception as ex:
    print(ex)
finally:
    driver.close()
    driver.quit()


def parse(data):
    ssbb = []
    for i in range(len(data)):
        line = data[i].replace("Sensor at ", "").replace("x=","").replace("y=","").replace(" closest beacon is at ", "")
        line = line.split(":")
        s_b = []
        for j in line:
            r = eval(f"({j})")
            s_b.extend(r)
        s_b.extend([abs(s_b[0]-s_b[2])+abs(s_b[1]-s_b[3])])
        ssbb.append(tuple(s_b))
    ssbb_sorted = sorted(ssbb, key=lambda x: (x[1], x[0]))
    return ssbb_sorted

def ssbb_dict(sensors_beacons_list):
    ssbb, notin = sensors_beacons_list, dict()
    for var in ssbb:
        if var[1] not in notin:
            notin[var[1]] = dict()
        if var[3] not in notin:
            notin[var[3]] = dict()
        notin[var[1]][(var[0],var[0])] = "S"
        notin[var[3]][(var[2],var[2])] = "B"
    return notin

def not_in_query(sensors_beacons_list, not_in_dict, y_coord):
    ssbb, notin, query = sensors_beacons_list, not_in_dict, y_coord
    if query not in notin:
        notin[query] = dict()
    for var in ssbb:
        i = abs(query - var[1])
        if i <= var[4]:
            notin = update_notin(notin, query, (var[0] - var[4] + i, var[0] + var[4] - i))
    return notin

def update_notin(not_in_dict, y_coord, candidate_range):
    notin, y, r = not_in_dict, y_coord, candidate_range
    nin_rngs = list(k for k,v in notin[y].items() if v == False)
    nrngs = get_ranges(notin, y, r)
    if len(nin_rngs) == 0 and len(nrngs) > 0:
        notin[y].update({k: False for k in nrngs})
    else:
        mrngs = merge(nin_rngs + [r])
        for ninr in nin_rngs:
            for m in mrngs:
                if m[1] < ninr[0] or ninr[1] < m[0]:
                    nrngs = get_ranges(notin, y, (m[0], m[1]))
                    notin[y].update({k: False for k in nrngs})
                elif ninr[0] <= m[0] <= m[1] <= ninr[1]:
                    continue
                else:
                    del notin[y][ninr]
                    nmin, nmax = min(m[0], ninr[0]), max(m[1], ninr[1])
                    nrngs = get_ranges(notin, y, (nmin, nmax))
                    notin[y].update({k: False for k in nrngs})
    notin = dict(sorted(notin.items()))
    return notin

def get_ranges(notin_dict, y_coord, range_tuple):
    notin, y, rng = notin_dict, y_coord, range_tuple
    rngs, candidates, sandb = [], [rng], get_sb(notin, y)
    if len(sandb) == 0:
        return [rng]
    for sb in sandb:
        while candidates:
            rng = candidates[-1]
            if (sb,sb) == rng:
                candidates.pop()
            elif sb not in range(rng[0], rng[1]+1):
                rngs.append((rng[0], rng[1]))
                candidates.pop()
            elif sb == rng[0]:
                candidates = [(rng[0]+ 1, rng[1])] + candidates
                candidates.pop()
            elif sb == rng[1]:
                candidates = [(rng[0], rng[1]-1)] + candidates
                candidates.pop()
            elif sb in range(rng[0]+1, rng[1]):
                candidates = [(rng[0], sb-1), (sb+1, rng[1])] + candidates
                candidates.pop()
    return rngs

def get_sb(notin_dict, y_coord):
    notin, y = notin_dict, y_coord
    return sorted([j[0] for j in [k for k,v in notin[y].items() if isinstance(v,str)] if j != []])

def merge(range_list):
    rngs = sorted([list(r) for r in range_list])
    newr = [rngs[0]]
    for r in rngs:
        if r[0] <= newr[-1][1]:
            newr[-1][1] = max(newr[-1][1], r[1])
        else:
            newr.append(r)
    return [tuple(nr) for nr in newr]

def count_not_in(notin_dict, y_coord):
    notin, y = notin_dict, y_coord
    nin_rngs = list(k for k,v in notin[y].items() if v == False)
    count = 0
    for nr in nin_rngs:
        count += nr[1] - nr[0] +1
    return count

def locate_distress(sensors_beacons_list, min_query, max_query):
    ssbb, minq, maxq = sensors_beacons_list, min_query, max_query
    for query in range(maxq+1,minq-1,-1):
        rngs = []
        for var in ssbb:
            i = abs(query - var[1])
            if i <= var[4]:
                rngs.append((var[0] - var[4] + i, var[0] + var [4] - i))
        mrngs = merge(rngs)
        if len(mrngs) == 2:
            if mrngs[1][0] - mrngs[0][1] == 2:
                x = mrngs[0][1] + 1
                frecuency = 4000000*x + query
                return frecuency
    return None

with open('day15.txt') as myfile:
    data = myfile.read().strip().split("\n")
    #print(data)

ssbb = parse(data)

# PART 1
notin = ssbb_dict(ssbb)
query = 2000000
notin = not_in_query(ssbb, notin, query)
print(count_not_in(notin, query))

# PART 2
maxq = 4000000
minq = 0
print(locate_distress(ssbb, minq, maxq))

'''
p1: 4861076
p2: 10649103160102
'''


import sys
import time
import string
import random
import requests
import html.parser
from bs4 import BeautifulSoup

class IndeedSalaryFinder(object):
    '''
    Scrapes Indeed to find the Salary associated with the job in question.
    '''

    def __init__(self, query):
        '''
        query - a query to find the job, better queries are better
        '''
        self.query = query

    def find(self, job_id, city='Seattle', state='WA'):
        '''
        Actual scrape function
        '''
        self.no = []
        self.yes = []
        salary = 100
        go = 'yes'
        while go == 'yes':
            new = int(salary/2)
            if new == 0:
                new = 1
            time.sleep(random.randint(0,1))
            if salary <= 25:
                # poverty level...
                go = 'no'
            n = self.get_search(salary, job_id, city, state)
            # print(salary, n)
            if n >= 0:
                try:
                    self.yes.index(salary)
                    go = 'no'
                except:
                    self.yes.append(salary)
                    if len(self.no) >= 1:
                        new = int((min(self.no) - salary)/2)
                    if new <= 0:
                        new = 1
                    salary = salary + new
            else:
                try:
                    self.no.index(salary)
                    go = 'no'
                except:
                    self.no.append(salary)
                    if len(self.yes) >= 1:
                        new = int((salary - max(self.yes))/2)
                    if new <= 0:
                        new = 1
                    salary = salary - new
        print('{}K to {}k by Indeed'.format(max(self.yes), min(self.no)))

    def get_search(self, salary, job_id, city='Seattle', state='WA'):
        '''
        Searches for the job in Indeed... using the search function,
        finds the job_id in the html, or not.
        '''
        r = requests.get('https://www.indeed.com/jobs?q=' + self.query + '+' +
                         '%24' + str(salary) + '%2C000&l=' + city + '%2C+' +
                         state)
        soup = BeautifulSoup(r.content, 'html.parser')
        tags  = soup.find_all()
        return str(tags).find(job_id)

if __name__ == '__main__':
    if len(sys.argv) <= 1:
        print('Type in a query, such as: "data+science+galvanize" and ')
        print('the job_id.')
        print()
        print('job_id -> Make sure the search reveals 2-10 jobs.' )
        print('view source')
        print('find: jobmap')
        print('grab the long string after rd for your job.')
    else:
        finder = IndeedSalaryFinder(sys.argv[1])
        finder.find(sys.argv[2])


from flask import Flask, render_template, request
from os import listdir
from os.path import isfile, join

app = Flask(__name__)

@app.route('/')
def index():
    deck_themes = get_img_dirs()
    return render_template('index.html', deck_themes=deck_themes)

def get_files(deck_theme):
    my_path = f'static/images/{deck_theme}'
    files = [join(my_path, f) for f in listdir(my_path) if isfile(join(my_path, f))]
    return files

def get_img_dirs():
    my_path = 'static/images/'
    decks_data = []
    for dir_name in listdir(my_path):
        icons = get_files(dir_name)
        icons.sort()
        first_img = icons[0]
        deck_data = {'name': dir_name, 'img': first_img}
        decks_data.append(deck_data)
    return decks_data

@app.route('/game', methods=['POST', 'GET'])
def game():
    deck_themes = request.form.getlist('deck_theme')
    if not deck_themes:
        deck_themes = [d for d in listdir('static/images/')]

    files = []
    for deck_theme in deck_themes:
        img_list = get_files(deck_theme)
        for img in img_list:
            files.append(img)

    player_1_keys = ["Q", "W", "E", "R", "F"]
    player_2_keys = ["U", "I", "O", "P", "J"]

    difficulty = int(request.form.get('difficulty', 4))
    player_1_keys_by_difficulty = player_1_keys[:difficulty]
    player_2_keys_by_difficulty = player_2_keys[:difficulty]

    player_1 = request.form.get('player_1', "Player 1")
    player_2 = request.form.get('player_2', "Player 2")

    return render_template('game.html',
                           player_1=player_1,
                           player_2=player_2,
                           player_1_keys=player_1_keys_by_difficulty,
                           player_2_keys=player_2_keys_by_difficulty,
                           files=files)

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


# import ordering
# import pytest
#
# @pytest.mark.run(order=2)
# def test_foo():
#     print("用例11111111111")
#     assert True
#
# @pytest.mark.run(order=1)
# def test_bar():
#     print("用例22222222222")
#     assert True
#
# @pytest.mark.run(order=3)
# def test_g():
#     print("用例333333333333333")
#     assert True
import sys, os

from contextlib import ExitStack

profileList = {}

def PropValue(envfile):

    with open(envfile) as profile:

        new_profile = profile.readlines()

        # print(new_profile)

    for line in new_profile:

        line_key = line.strip().split(":", 1)[0]

        profileList[line_key] = line.strip().split(":", 1)[1]
        print(line_key)

    print(profileList)
def EnvReplaceYaml(yamlfile, newyamlfile):

    try:

        with ExitStack() as stack:

            yml_file = stack.enter_context(open(yamlfile,'r+'))

            yml_output = stack.enter_context(open(newyamlfile,'w'))

            yml_file_lines = yml_file.readlines()

        for line in yml_file_lines:

            new_line = line

            if (new_line.find('') > 0):

                env_list = new_line.split(':')

                env_name = env_list[0].strip()

                replacement = ""

                if env_name in profileList.keys():

                    replacement = profileList[env_name]

                    new_line = new_line.replace('', replacement)

                    yml_output.write(new_line)

    except IOError as e:

        print("Error: " + format(str(e)))

        # raise

if __name__ == "__main__":

    PropValue('./temp.yaml')

    EnvReplaceYaml('./temp.yaml', 'newtemap.yaml')

# temp.yaml

# ---
#
# applications:
#
# - name: --
# memory: 2048m
#
# instances: 1
#
# disk_quota: 2048m


from selenium import webdriver
import platform

class BrowserInt():

    def test(self):
        baseUrl = "https://learn.letskodeit.com/p/practice"
        os_platform = platform.system()
        if os_platform == 'Linux':
            driver_path = webdriver.Firefox(executable_path='./geckodriver')
        elif os_platform == 'Darwin':
            driver_path = webdriver.Chrome()

        driver = driver_path

        driver.maximize_window()
        driver.get(baseUrl)

        title = driver.title
        print('Title of webpage is {}'.format(title))
        currentUrl = driver.current_url
        print('Current URL of page is {}'.format(currentUrl))

        driver.refresh()
        print('Browser refreshed 1st time')
        driver.get(driver.current_url)
        print('Browser refreshed 2nd time')

        driver.get('https://www.google.com')
        print('Current URL of page is {}'.format(driver.current_url))
        driver.back()
        print('Browser went one step back in history')
        print('Current URL of page is {}'.format(driver.current_url))
        driver.forward()
        print('Browser went one step forward in history')

        source = driver.page_source
        driver.close()
        driver.quit()

ff = BrowserInt()
ff.test()


import dataset

if __name__ == "__main__":
    print("DataSet version: ", dataset.__version__)
    db = dataset.connect('sqlite:///:memory:')  # use 'sqlite:///sensors.db' to persist ...

    sensor_table = db['sensors']

    sensor_table.insert(dict(name='SHT711', type="spi", bus_no=3, cs_num=6, alias="RHT-1A"))
    sensor_table.insert(dict(name='SHT711', type="i2c", bus_no=1, i2c_addr=78, alias="RHT-1B"))
    sensor_table.insert(dict(name='SHT711', type="i2c", bus_no=1, i2c_addr=71, alias="RHT-1C"))

    rht1b = sensor_table.find_one(alias='RHT-1B')
    print(repr(rht1b))
    rht1a = sensor_table.find_one(alias='RHT-1A')
    print(repr(rht1a))
    # Find many
    i2c_sensors = sensor_table.find(type="i2c")
    print("I2C-sensors:")
    print("============")
    for sensor in i2c_sensors:
        print(repr(sensor))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""MergeResults.py:
    Gather all the information from the json files obtained in the tests and create a table in a csv file
"""
__author__ = "Jorge de la Peña García"
__version__ = "1.0"
__maintainer__ = "Jorge"
__email__ = "jpena@ucam.edu"
__status__ = "Production"

import glob
import json
import sys
from os.path import splitext, basename, join

class MergeResults:
    F_RESUME_CSV = 'Resume_analysis.csv'
    SEPARATOR_CSV = ';'
    MAX_TEST = 100  # maximum number of tests allowed per model

    def __init__(self, folder_experiment):
        self.folder = folder_experiment

        self.results = {}
        self.create_csv()

    def get_key_name(self, filename):
        key_name = splitext(basename(filename))[0].split('_')[0]

        if key_name in self.results:
            for i in range(2, self.MAX_TEST):
                if key_name+"_"+str(i) not in self.results:
                    key_name = key_name+"_"+str(i)
                    break;
        return key_name

    def read_data(self):

        for i in glob.glob(join(self.folder, '*.json')):
            key_name = self.get_key_name(i)
            with open(i) as file_in:
                self.results[key_name] = json.load(file_in)

    def create_csv(self):
        self.read_data()
        csv_header = list(self.results.keys())
        csv_header.sort()

        lst_lines = [self.SEPARATOR_CSV + '{} '.format(self.SEPARATOR_CSV).join(csv_header)]
        lst_lines += self.analysis(csv_header)
        lst_lines.append("")
        lst_lines += self.config(csv_header)

        f = open(join(self.folder, self.F_RESUME_CSV), 'w')
        for i in lst_lines:
            f.write('{}\n'.format(i))
        f.close()

    def config(self, csv_header):

        lst_keys_config = []
        lst_lines = []
        for i in csv_header:
            for k, v in self.results[i]['Config']['Model_params']['params'].items():
                if k not in lst_keys_config:
                    lst_keys_config.append(k)

        for k in lst_keys_config:
            line = k
            for key in csv_header:
                if k in self.results[key]['Config']['Model_params']['params']:
                    line += self.SEPARATOR_CSV+" " + str(self.results[key]['Config']['Model_params']['params'][k])
                else:
                    line += self.SEPARATOR_CSV + "-"
            lst_lines.append(line)
        return lst_lines

    def analysis(self, csv_header):
        lst_lines = []
        keys_analysis = self.results[csv_header[0]]['Analysis'].keys()
        for key in keys_analysis:
            if key != "Confusion matrix":
                line = key
                for header in csv_header:
                    line += self.SEPARATOR_CSV + ' ' + str(self.results[header]['Analysis'][key]).replace('.',',')
                lst_lines.append(line)
        return lst_lines

if __name__ == '__main__':
    folder = sys.argv[1]
    MergeResults(folder)


from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import numpy as np

iris = load_iris()
atributos = iris.data
classes = iris.target

atributos_do_conjunto = atributos[classes != 0]
classes_conjunto = classes[classes != 0]
classes_conjunto = np.where(classes_conjunto == 1, 1, -1)

#População inicial
def inicializar_populacao(tamanho_populacao, dimensao_individuo):
    populacao = []
    for j in range(tamanho_populacao):
        individuo = np.random.uniform(-1, 1, dimensao_individuo)
        populacao.append(individuo)
    return populacao

# Mutação do filho
def mutacao(individuo, taxa_mutacao):
    #Verifica numero aleatório gerado e compara com a taxa de mutação para mutar o filho
    for i in range(len(individuo)):
        if np.random.uniform(-1, 1) < taxa_mutacao:
            individuo[i] = np.random.uniform(-1, 1)
    return individuo

# Para cada individuo calcula a aptidao EMQ
def aptidao(individuo):
    pesos = individuo[:-1]
    viés = individuo[-1]
    erro_quadrado_total = 0
    for i in range(atributos_treinamento.shape[0]):
        entrada = atributos_treinamento[i]
        alvo = classes_treinamento[i]
        soma_ponderada = np.dot(entrada, pesos) + viés
        previsao = 1 if soma_ponderada >= 0 else 0
        erro = alvo - previsao
        erro_quadrado_total += erro ** 2
    emq = erro_quadrado_total / atributos_treinamento.shape[0]
    return emq

# Cruzamento dos pais selecionados
def crossover(pai1, pai2):
    ponto_corte = np.random.randint(0, len(pai1))
    #Une pais de acordo com o ponto de corte definido
    filho = np.concatenate((pai1[:ponto_corte], pai2[ponto_corte:]))
    return filho

for cont in range(3):
    if cont == 0:
        proporcao_conn_teste = 0.1
    if cont == 1:
        proporcao_conn_teste = 0.3
    if cont == 2:
        proporcao_conn_teste = 0.5
    # Divisão entre treinamento e teste
    atributos_treinamento, atributos_teste, classes_treinamento, classes_teste = train_test_split(atributos_do_conjunto, classes_conjunto, test_size=proporcao_conn_teste, random_state=42)
    tamanho_conjunto = atributos_teste.shape[0]
    #Inicialização pesos/bias
    np.random.seed(0)
    pesos = np.random.rand(atributos_treinamento.shape[1])
    viés = np.random.rand()

    #Genético
    tamanho_populacao = 100
    dimensao_individuo = atributos_treinamento.shape[1] + 1  #Adiciona o viés na ultima posição
    taxa_mutacao = 0.2
    num_geracoes = 200
    populacao = inicializar_populacao(tamanho_populacao, dimensao_individuo)

    for geracao in range(num_geracoes):
        aptidoes = []
        for individuo in populacao:
            apt = aptidao(individuo)
            aptidoes.append(apt);
        melhor_individuo = populacao[np.argmin(aptidoes)]
        # populacao que contem apenas o melhor indivíduo, será a próxima populacao
        populacao_selecionada = [melhor_individuo]
        #Gera filhos
        while len(populacao_selecionada) < tamanho_populacao:
            #seleciona dois indices da populacao para serem os pais
            indices = np.random.choice(len(populacao), size=2, replace=False)
            pai1 = populacao[indices[0]]
            pai2 = populacao[indices[1]]
            filho = crossover(pai1, pai2)
            filho = mutacao(filho, taxa_mutacao)
            populacao_selecionada.append(filho)
        populacao = populacao_selecionada

    melhor_pesos = melhor_individuo[:-1]
    melhor_bias = melhor_individuo[-1]

    pesos = melhor_pesos;
    viés = melhor_bias;

    # Execução de testes do classificador
    corretos = 0
    for i in range(tamanho_conjunto): #Tamanho conjunto de testes
        entrada = atributos_teste[i]
        objetivo = classes_teste[i]
        soma_ponderada = np.dot(entrada, pesos) + viés
        ativação = np.sign(soma_ponderada)
        if ativação == objetivo:
            corretos += 1

    acuracia = (corretos / tamanho_conjunto) * 100 #Quantidade de acertos/tamanho do conjunto

    print("Teste "+str(cont+1))
    print("     Tamanho conjunto de teste:", proporcao_conn_teste)
    print("     Acurácia obtida no conjunto de teste %:", acuracia)


#!/usr/bin/env python3
# daemon.py

"""
originally from the Python Cookbook (version 3)
  11.8. Implementing Remote Procedure Calls
  12.2. Determining If a Thread Has Started
  12.14. Launching a Daemon Process on Unix
  (perhaps more)
"""

import os
import sys

import atexit
import signal

import json

from datetime import datetime, timedelta

from multiprocessing.connection import Listener
from threading import Thread

import pyplayer
from inspect import getmembers, isfunction, ismethod

#debuglog = StringIO()
def TRACE(msg):
    print(
        "%s: %s"
        % (datetime.now().strftime("%Y-%m-%d_%H:%M:%S.%f")[0:-3], msg),
        file=sys.stderr)
    #debuglog.write("%s\n" % m)
    #logging.info(m)
    #print('%s' % m, file=sys.stderr)

class RPCHandler:

    def __init__(self):
        self._functions = { }
        self.mplif = pyplayer.MPlayerIF()

    def register_function(self, func):
        print(
            "adding function %s (type = %s) isfunction = %s"
            % (str(func.__name__), str(type(func)), str(isfunction(func)))
        )
        self._functions[func.__name__] = func

    def handle_connection(self, connection):
        try:
            while True:
                # Receive a message
                func_name, args, kwargs = json.loads(connection.recv())
                sys.stdout.write(
                    "RPCHandler.handle_connection: (%s) called\n"
                    % (func_name)
                )
                sys.stdout.flush()
                # Run the RPC and send a response
                try:
                    r = self._functions[func_name](*args,**kwargs)
                    sres = json.dumps(r)
                    sys.stdout.write(
                        "RPCHandler.handle_connection: "
                        "function %s result = %s, str(result) = %s type = %s\n"
                        % (func_name, str(r), str(sres), str(type(r)))
                    )
                    connection.send(sres)
                except Exception as e:
                    connection.send(json.dumps(str(e)))
        except EOFError:
            pass

# test function #1
def add(x, y):
    sys.stdout.write("add(%d, %d)\n" % (x, y))
    sys.stdout.flush()
    return x + y

# test function #2
def sub(x, y):
    sys.stdout.write("sub(%d, %d)\n" % (x, y))
    sys.stdout.flush()
    return x - y

def daemonize(pidfile, *,
              stdin='/dev/null',
              stdout='/dev/null',
              stderr='/dev/null'):

    # TODO: check for existence of stdin, stdout and stderr files and
    #       the directories containing them

    # TODO: verify the directory that contains the PID file, both the existence
    #       and the writability for the current user

    if os.path.exists(pidfile):
        raise RuntimeError('Already running')

    # First fork (detaches from parent)
    try:
        if os.fork() > 0:
            raise SystemExit(0) # Parent exit
    except OSError as e:
        raise RuntimeError('fork #1 failed.')

    os.chdir('/')
    os.umask(0)
    os.setsid()
    # Second fork (relinquish session leadership)
    try:
        if os.fork() > 0:
            raise SystemExit(0)
    except OSError as e:
        raise RuntimeError('fork #2 failed.')

    TRACE('%s started' % DAEMON)

    # Flush I/O buffers
    sys.stdout.flush()
    sys.stderr.flush()

    # Replace file descriptors for stdin, stdout, and stderr
    with open(stdin, 'rb', 0) as f:
        os.dup2(f.fileno(), sys.stdin.fileno())
    with open(stdout, 'ab', 0) as f:
        os.dup2(f.fileno(), sys.stdout.fileno())
    with open(stderr, 'ab', 0) as f:
        os.dup2(f.fileno(), sys.stderr.fileno())
    # Write the PID file
    with open(pidfile,'w') as f:
        print(os.getpid(),file=f)

    # Arrange to have the PID file removed on exit/signal
    atexit.register(lambda: os.remove(pidfile))
    # Signal handler for termination (required)

    def sigterm_handler(signo, frame):
        raise SystemExit(1)

    signal.signal(signal.SIGTERM, sigterm_handler)

def rpc_server(handler, address, authkey):
    sock = Listener(address, authkey=authkey)
    while True:
        client = sock.accept()
        t = Thread(target=handler.handle_connection, args=(client,))
        t.daemon = True
        t.start()
        # Some remote functions

def daemon_start():
    try:
        daemonize(
            PIDFILE,
            stdout=pyplayer.config.stdout_file,
            stderr=pyplayer.config.stderr_file
        )
        TRACE('%s started' % PIDFILE)
    except RuntimeError as e:
        TRACE(e)
        raise SystemExit(1)
    main()

def daemon_stop():
    if os.path.exists(PIDFILE):
        with open(PIDFILE) as f:
            TRACE('stopping %s...' % DAEMON)
            os.kill(int(f.read()), signal.SIGTERM)
            # FIXME: don't exit until the process is confirmed dead
        TRACE('%s stopped' % DAEMON)
    else:
        TRACE('Not running')
        raise SystemExit(1)

def daemon_status():
    if not os.path.exists(PIDFILE):
        TRACE('Not running.  %s does not exist.' % PIDFILE)
        raise SystemExit(1)

    pid = int(open(PIDFILE).read())
    # example: cmdlinefile="/projects/6124/cmdline"
    cmdlinefile = "/proc/%d/cmdline" % pid
    if not os.path.exists(cmdlinefile):
        TRACE(
            'Not running.  cmdline file %s does not exist in /proc tree.'
            % cmdlinefile
        )
        raise SystemExit(1)

    s = open(cmdlinefile).readline()
    # example: s='python3\x00./pyplayerd.py\x00start\x00'
    s2 = s.split("\0")[1]
    # example: s2='./pyplayerd.py'
    if not s2.endswith(DAEMON):
        TRACE(
            'Not running.  cmdline file %s indicates %s; expected %s'
            % (cmdlinefile, s2, DAEMON)
        )
        raise SystemExit(1)
    TRACE('%s is running (pid=%d)' % (DAEMON, pid))

def daemon_restart():
    try:
        daemon_stop()
    except:
        pass
    daemon_start()

def main():
    import time
    sys.stdout.write('Daemon started with pid {}\n'.format(os.getpid()))
    # Register with a handler
    handler = RPCHandler()
    handler.register_function(add)
    handler.register_function(sub)
    for f in getmembers(handler.mplif):
        if f[1] is None: continue
        print(
            "handler.mplif: %s (type = %s)"
            % (str(f[1]), str(type(f[1])))
        )
        if ismethod(f[1]) and f[0][0] != '_':
            handler.register_function(f[1])
    # Run the server
    rpc_server(handler, ('localhost', 17000), authkey=b'super_secret_auth_key__CHANGEME')

if __name__ == '__main__':
    PIDFILE = '/run/pyplayer/pyplayer.pid'
    DAEMON = sys.argv[0]
    if len(sys.argv) != 2:
        print(
            'Usage: {} [start|stop|restart|status]'.format(sys.argv[0])
        )
        raise SystemExit(1)
    if sys.argv[1] == 'start':
        daemon_start()
    elif sys.argv[1] == 'stop':
        daemon_stop()
    elif sys.argv[1] == 'status':
        daemon_status()
    elif sys.argv[1] == 'restart':
        daemon_restart()
    else:
        TRACE('Unknown command {!r}'.format(sys.argv[1]))
        raise SystemExit(1)


# got from https://gist.github.com/enigmaticape/4254054
#!/usr/bin/env python

import sys
import collections

# Bag em
# cipher_file  = open( sys.argv[ 1 ], 'rb')

def get_IC(c_text):
    cipher_text  = c_text

    # remove all non alpha and whitespace and force uppercase
    # SOTHATCIPHERTEXTLOOKSLIKETHIS
    cipher_flat  = "".join(
                            [x.upper() for x in cipher_text.split() \
                                       if  x.isalpha() ]
                         )

    # Tag em
    N            = len(cipher_flat)
    freqs        = collections.Counter( cipher_flat )
    alphabet     =  map(chr, range( ord('A'), ord('Z')+1))
    freqsum      = 0.0

    # Do the math
    for letter in alphabet:
        freqsum += freqs[ letter ] * ( freqs[ letter ] - 1 )

    IC = freqsum / ( N*(N-1) )

    print("IC of the cipher text %.3f" % IC, "({})".format( IC ))
    return IC


from arcs import *

arrows = []

def setup():
    size(500, 500)
    colorMode(HSB)
    strokeJoin(ROUND)
    create_arrows()

def create_arrows():
    arrows[:] = []
    arrows.append((125,  # radius
                   PI,  # start
                   PI,  # sweep
                   100,  # thickness
                   color(255),  # color
                   ))

def draw():
    background(200)
    for rad, start, sweep, thick, cor in arrows:
        thick = mouseY - 250
        sweep = radians(1 + mouseX)
        noFill()
        strokeWeight(5)
        stroke(180)
        circle(width / 2, height / 2, rad * 2)
        stroke(cor)
        arc_arrow(width / 2, height / 2, rad, start, sweep, thick)
        stroke(0)
        arc_arrow_b(width / 2, height / 2, rad, start, sweep, thick-10)

def keyPressed():
    if key == ' ':
        create_arrows()
    if key == 's':
        saveFrame('#####.png')

def arc_arrow_b(x, y, radius, start_ang, sweep_ang, thickness=None):
    """
    Draws an arrow in a circular arc shape
    """
    if thickness is None:
        thickness = radius / 2.
    if radius * .75 < abs(thickness / 2):
        thickness = radius * 1.5 if thickness > 0 else -radius * 1.5
    half_thickness = thickness / 2.
    offset = half_thickness / radius
    beginShape()
    b_circle_arc(x, y, radius + half_thickness,
                 start_ang, sweep_ang, mode=2)
    vertex(*point_on_arc(x, y, radius, start_ang + sweep_ang + offset))
    b_circle_arc(x, y, radius - half_thickness,
                 start_ang + sweep_ang, -sweep_ang, mode=2)
    vertex(*point_on_arc(x, y, radius, start_ang + offset))
    endShape(CLOSE)

def arc_arrow(x, y, radius, start_ang, sweep_ang, thickness=None):
    """
    Draws an arrow in a circular arc shape
    """
    if thickness is None:
        thickness = radius / 2
    # off = radius / 2
    # if radius - off / 2 < thickness / 2:
    # fill(255, 0, 0) # debug
    #     thickness = radius * 2 - off
    # else:
    # fill(255)
    if radius * .75 < abs(thickness / 2):
        thickness = radius * 1.5 if thickness > 0 else -radius * 1.5

    beginShape()
    b_circle_arc(x, y, radius + thickness / 2,
                 start_ang, sweep_ang, mode=2)
    mid_ending(x, y, radius, start_ang + sweep_ang, thickness)
    b_circle_arc(x, y, radius - thickness / 2,
                 start_ang + sweep_ang, -sweep_ang, mode=2)
    mid_ending(x, y, radius, start_ang, thickness)
    endShape(CLOSE)

def mid_ending(x, y, radius, angle, thickness):
    pa = point_on_arc(x, y, radius + thickness / 2, angle)
    pb = point_on_arc(x, y, radius - thickness / 2, angle)
    mp = mid_point(pa, pb)
    ep = point_on_arc(mp[0], mp[1], thickness / 2, angle + HALF_PI)
    vertex(*ep)
    # # debug
    # pushStyle()
    # stroke(180)
    # line(mp[0], mp[1], ep[0], ep[1]) # debug
    # popStyle()

def mid_point(pa, pb):
    if len(pa) == 3 and len(pb) == 3:
        return ((pa[0] + pb[0]) / 2,
                (pa[1] + pb[1]) / 2,
                (pa[2] + pb[2]) / 2)
    else:
        return ((pa[0] + pb[0]) / 2,
                (pa[1] + pb[1]) / 2)

def point_on_arc(cx, cy, radius, angle):
    x = cx + radius * cos(angle)
    y = cy + radius * sin(angle)
    return (x, y)


import torch
import torch.nn as nn
from src.utils.torch_utils import FLOAT

class BaseCNN(nn.Module):

    def __init__(self, price_features, num_stocks, window_length):
        super().__init__()

        self.conv1 = nn.Conv2d(price_features, 2, kernel_size=(1,3))
        self.conv2 = nn.Conv2d(in_channels=2, out_channels=20, kernel_size=(1,window_length-2))
        self.conv3 = nn.Conv2d(in_channels=20+1, out_channels=1, kernel_size=1)

        self.fc1 = nn.Linear(num_stocks, num_stocks+1)
        self.relu = nn.ReLU()

    def forward(self, x, w):
        # x = state
        # w = past action

        if len(x.shape) == 3:
            # single data point - no batches
            x = x[None,:,:,:]
            w = w[None,None,1:,None] # remove cash value
        else:
            w = w[:,None,1:,None]

        x = self.conv1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x = self.relu(x)

        x = torch.cat((x, w), dim=-3)
        x = self.conv3(x)

        # remove unused dimensions
        x = x.squeeze(3)
        x = x.squeeze(1)

        # cash_bias = torch.ones(x.shape[0], 1, dtype=FLOAT)
        # if x.is_cuda:
        #     cash_bias = cash_bias.cuda()

        # x = torch.cat((x,cash_bias), dim=-1)

        x = self.fc1(x)
        x = self.relu(x)

        return x

    def requires_grad(self, req):
        for param in self.parameters():
            param.requires_grad = req

    def init_weights(self):
        for _, module in self.named_modules():
            if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
                nn.init.kaiming_normal_(module.weight, nonlinearity='relu')

class DeterministicCNNActor(nn.Module):
    # represents the policy function

    def __init__(self, price_features, num_stocks, window_length):
        super().__init__()

        self.common = BaseCNN(price_features, num_stocks, window_length)
        self.fc = nn.Linear(num_stocks+1, num_stocks+1)

        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x, w):

        x = self.common(x, w)

        x = self.fc(x)
        x = self.softmax(x)

        return x.squeeze()

    def requires_grad(self, req, pretrained):
        for name, param in self.named_parameters():
            # if I have to unfreeze and network was pretraiend, keep common part frozen
            if req and pretrained and 'common' in name:
                continue
            param.requires_grad = req

    def init_weights(self):
        for _, module in self.named_modules():
            if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
                nn.init.kaiming_normal_(module.weight, nonlinearity='relu')

class CNNCritic(nn.Module):
    # represents the policy function

    def __init__(self, price_features, num_stocks, window_length):
        super().__init__()

        self.common = BaseCNN(price_features, num_stocks, window_length)
        self.fc = nn.Linear((num_stocks+1)*2, 1)

    def forward(self, x, w, action):

        x = self.common(x, w)

        x = torch.cat((x,action), dim=-1)
        x = self.fc(x)

        return x

    def requires_grad(self, req, pretrained):
        for name, param in self.named_parameters():
            # if I have to unfreeze and network was pretraiend, keep common part frozen
            if req and pretrained and 'common' in name:
                continue
            param.requires_grad = req

    def init_weights(self):
        for _, module in self.named_modules():
            if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
                nn.init.kaiming_normal_(module.weight, nonlinearity='relu')


from copy import copy
from math import factorial

def permutation_ids(length):
    """
    >>> list(permutation_ids(4))
    [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (0, 2, 0), (0, 2, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (1, 2, 0), (1, 2, 1), (2, 0, 0), (2, 0, 1), (2, 1, 0), (2, 1, 1), (2, 2, 0), (2, 2, 1), (3, 0, 0), (3, 0, 1), (3, 1, 0), (3, 1, 1), (3, 2, 0), (3, 2, 1)]
    """
    perm_id = [0] * (length - 1)
    while perm_id[0] < length:
        yield tuple(perm_id)
        perm_id[-1] += 1 # increment the last item
        # carry overflow, and cascade
        for rindex in range(1, length - 1):
            if perm_id[-rindex] > rindex:
                perm_id[-rindex] = 0
                perm_id[-rindex - 1] += 1

def perm_from_id(sequence, id):
    """
    >>> perm_from_id('abc', (0, 0))
    ('a', 'b', 'c')
    >>> perm_from_id('abc', (2, 1))
    ('c', 'b', 'a')
    """
    sequence = copy(list(sequence))
    perm = []
    for index in id:
        perm.append(sequence.pop(index))
    perm.append(sequence.pop())
    return tuple(perm)

def permutations(sequence):
    """
    >>> [''.join(p) for p in permutations('abc')]
    ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    >>> s = 'abcdefg'
    >>> perms = [''.join(p) for p in permutations(s)]
    >>> perms == sorted(perms)
    True
    >>> len(perms) == factorial(len(s))
    True
    """
    for id in permutation_ids(len(sequence)):
        yield perm_from_id(sequence, id)

def nth_perm_id(length, n):
    """
    >>> list(permutation_ids(4))[10]
    (1, 2, 0)
    >>> nth_perm_id(4, 10)
    (1, 2, 0)
    >>> list(permutation_ids(6)) == [nth_perm_id(6, i) for i in range(factorial(6))]
    True
    """
    perm_id = [0] * (length - 1)
    for rindex in range(1, length):
        perm_id[-rindex] = n // factorial(rindex) % (rindex + 1)
    return tuple(perm_id)

if __name__ == '__main__':
    id = nth_perm_id(10, 1000000 - 1)
    perm = perm_from_id(range(10), id)
    number = ''.join(str(d) for d in perm)
    print(number)


import spacy
from spacy.matcher import Matcher
from spacy.lang.en import English
from lib.custom_nlp.patterns import cse_pattern, tsx_pattern, stock_phrases
import re
from spacytextblob.spacytextblob import SpacyTextBlob
try:
    from lib.custom_nlp.curr_tickers import stocks as stock_names
except ImportError as e:
    print(e)
    stock_names = []

# All NLP logic is encompassed in this object
class NLPLogic:
    def __init__(self):
        nlp = spacy.load('en_core_web_sm')
        nlp.add_pipe("spacytextblob")
        self.nlp = nlp

    def get_text_matches(self, text):
        matches = []
        matched_strings = []

        temp_matched_strings, temp_matches = self.phrases_of_interest(text)
        matched_strings = [*matched_strings, *temp_matched_strings]
        matches = [*matches, *temp_matches]

        temp_matched_strings, temp_matches = self.stocks_of_interest(text)
        matched_strings = [*matched_strings, *temp_matched_strings]
        matches = [*matches, *temp_matches]

        temp_matched_strings, temp_matches = self.stocks_from_exchange(text)
        matched_strings = [*matched_strings, *temp_matched_strings]
        matches = [*matches, *temp_matches]

        return matched_strings, matches

    def phrases_of_interest(self, text):
        matcher = Matcher(self.nlp.vocab, validate=True)
        matcher.add(
            "Phrases",
            stock_phrases
        )
        doc = self.nlp(text)
        matches = matcher(doc)
        # Iterate and add stocks to the matcher, one by one
        matched_strings = []
        for match_id, start, end in matches:
            string_id = self.nlp.vocab.strings[match_id]  # Get string representation
            span = doc[start:end]  # The matched span
            matched_strings.append(span.text)
        return matched_strings, matches

    def stocks_of_interest(self, text):
        """
        Description: Checks a given sentence for stocks of interest

        Returns:
          matched_strings: String matches from text
          matches: spacy matches
        """
        stock_patterns = [{"TEXT": {"REGEX": f"{stock}"}} for stock in stock_names if len(stock) > 1]
        matcher = Matcher(self.nlp.vocab)
        matcher.add("stocks_patterns", [stock_patterns])
        doc = self.nlp(text)

        matches = matcher(doc)
        # Iterate and add stocks to the matcher, one by one
        matched_strings = []
        # for match_id, start, end in matches:
        #     string_id = self.nlp.vocab.strings[match_id]  # Get string representation
        #     span = doc[start:end]  # The matched span
        #     print(match_id, string_id, start, end, span.text)

        # https://spacy.io/usage/rule-based-matching
        for stock in stock_names:
            for match in re.finditer(stock, doc.text, flags=re.IGNORECASE):
                start, end = match.span()
                span = doc.char_span(start, end)
                # This is a Span object or None if match doesn't map to valid token sequence
                if span is not None:
                    matched_strings.append(span.text)
        # for match_id, start, end in matches:
        #     string_id = self.nlp.vocab.strings[match_id]  # Get string representation
        #     span = doc[start:end]  # The matched span
        #     matched_strings.append(span.text)
        return matched_strings, matches

    def stocks_from_exchange(self, text):
        """
        Description: Checks a given sentence for tickers from exchanges

        Returns:
          matched_strings: String matches from text
          matches: spacy matches
        """
        matcher = Matcher(self.nlp.vocab)
        doc = self.nlp(text)
        matcher.add("CSE_TICKERS", [cse_pattern])
        matcher.add("TSX_TICKERS", [tsx_pattern])
        matches = matcher(doc)

        matched_strings = []
        for match_id, start, end in matches:
            string_id = self.nlp.vocab.strings[match_id]  # Get string representation
            span = doc[start:end]  # The matched span
            matched_strings.append(span.text)
            print(matches)
        print(matched_strings)
        return matched_strings, matches

    def __exit__(self, exc_type, exc_value, traceback):
        self.nlp = None

if __name__ == "__main__":
    nlpLogic = NLPLogic()


"""Redact it yaml module.

This module contains the core logic to redact data from yaml files.
"""
import glob
from typing import Any

from redact_it.redact_it import RedactIt

class RedactItYaml(RedactIt):
    """Redact it yaml class."""

    def __init__(self, config_file: str, file_path: str, dry_run: bool) -> None:
        """Constructor.

        :param config_file: the redact it configuration file
        :param file_path: the file path of where to glob for files
        :param dry_run: toggle on/off dry run
        """
        super().__init__(config_file, file_path, dry_run)

    def redact(self) -> int:
        """Redact data from files."""
        self.load_config_file()
        if not self.config:
            return 1

        for file in glob.glob(self.file_path, recursive=True):
            if file == self.config_file:
                continue

            try:
                with open(file) as f:
                    file_content: dict[str, Any] = self.yaml.load(f)
            except FileNotFoundError as e:
                print(f"Failed to load {file}, error: {e}")
                continue

            count: int = 0

            for key, value in self.config.items():
                if isinstance(value, dict):
                    for nested_key, nested_value in value.items():
                        if (
                            nested_key in file_content[key]
                            and file_content[key][nested_key] != nested_value
                        ):
                            file_content[key][nested_key] = nested_value
                            count += 1
                else:
                    if key in file_content and file_content[key] != value:
                        file_content[key] = value
                        count += 1

            if count > 0:
                if self.dry_run:
                    print(
                        f"File {file} would be redacted, disable --dry-run to redact it"
                    )
                    continue

                print(f"Redacted {file}")

                with open(file, "w") as f:
                    self.yaml.dump(file_content, f)
        return 0