import os
import sys
from termcolor import colored
from getpass import getpass
from re import match
from platform import system
import shlex
import keyboard
import time

sys.path.append(os.path.abspath("../files"))
from internal import info as inf

def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

clear()

def fprint(*values: object, sep: str | None = " ", end: str | None = "\n", config: dict = {"color": "blue", "start": "|"}) -> None:
    if len(config["start"]) <= 1:
        config["start"] += " "*(2-len(config["start"]))
    if type(values[0]) == list: text = sep.join(values[0])
    else: text = sep.join(values)
    values = colored(config["start"], config["color"]) + text
    print(values, end=end)
def fprintTitle(*values: object, sep: str | None = " ", end: str | None = "\n", config: dict = {"color": "blue", "start": "+- "}) -> None:
    if len(config["start"]) <= 1:
        config["start"] += " "*(2-len(config["start"]))
    if type(values[0]) == list: text = sep.join(values[0])
    else: text = sep.join(values)
    if str(config["start"]).endswith(" "):
        text += " "
    values = colored(config["start"], config["color"]) + text + colored("-"*(os.get_terminal_size().columns-len(text)-len(config["start"])), config["color"])
    print(values, end=end)
def fline(config: dict = {"color": "blue", "start": "+"}) -> None:
    text = colored(config["start"] + "-"*(os.get_terminal_size().columns-len(config["start"])), config["color"])
    print(text)
def finput(__prompt: object = "", config: dict = {"color": "blue", "start": "|"}) -> str:
    if len(config["start"]) <= 1:
        config["start"] += " "*(2-len(config["start"]))
    text = colored(config["start"], config["color"]) + str(__prompt)
    return input(text)
def fpause(value: object = colored("Press enter to continue... ", attrs=["dark"]), config: dict = {"color": "blue", "start": "|"}) -> None:
        if value == colored("Press enter to continue... ", attrs=["dark"]) and not os.name == "nt":
            value = colored("(Press enter to continue...) ")
            config["start"] = ">"
            config["color"] = "red"
        if len(config["start"]) <= 1:
            config["start"] += " "*(2-len(config["start"]))
        text = colored(config["start"], config["color"]) + str(value)
        return getpass(text)

# << MAIN CODE >>

fprintTitle("MegaShell Compiler")
fprint("Made by: @JHubi1 (GitHub)")
fprint("Version: 1.0.0")
fline()
appVersion = ""
while not match(r"[0-9]\.[0-9]\.[0-9](?:-)?[a-zA-Z]{0,10}", appVersion):
    keyboard.write(inf.VERSION)
    appVersion = finput(colored("Enter the version of the app: ", "yellow"))
path = os.path.abspath(os.getcwd() + "/output/" + appVersion + "/" + system().lower())
# file deepcode ignore UpdateAPI: input from user; user = dev
if os.path.exists(path):
    fline()
    fprint("The directory already exists!")
    if finput("Continue? [y/N]: ").lower() == "y":
        fprint("Continuing...")
    else:
        fprint("Aborting...")
        fpause()
        exit()
os.makedirs(path, exist_ok=True)
# file deepcode ignore PT: input from user; user = dev
os.chdir(path)

start_time = time.time()

commands = [
    'pyinstaller --noconfirm --onedir --console --icon "' + os.path.abspath("../../../../images/^logos/logo.ico") + '" --name "MegaShell" --add-data "' + os.path.abspath("../../../../commands") + ';commands" --add-data "' + os.path.abspath("../../../../config") + ';config" "' + os.path.abspath("../../../../main.py") + '";;;pyinstaller --noconfirm --onedir --console --name "MegaShell" --add-data "' +  shlex.quote(os.path.abspath("../../../../commands")) + ';commands" --add-data "' +  shlex.quote(os.path.abspath("../../../../config")) + ';config" "' +  shlex.quote(os.path.abspath("../../../../main.py")) + '"',

    'xcopy /E /I /Y "' + os.path.abspath("dist/MegaShell") + '" "' + os.path.abspath("MegaShell") + '";;;cp -R ' + os.path.abspath("dist/MegaShell") + ' ' + os.path.abspath("MegaShell"),
    'del /F "' + os.path.abspath("MegaShell.spec") + '";;;rm "' + os.path.abspath("MegaShell.spec") + '"',
    'rmdir /S /Q "' + os.path.abspath("dist") + '";;;rm -rf "' + os.path.abspath("dist") + '"',
    'rmdir /S /Q "' + os.path.abspath("build") + '";;;rm -rf "' + os.path.abspath("build") + '"',

    # IMPORTANT: ISCC must be in path!
    'ISCC /O+ /DMyAppVersion="' + appVersion + '" /DMainPath="' + os.path.abspath("../../../..") + '" /DExeVersion="' + appVersion.replace(" ", "").replace(".", "-") + '" "' + os.path.abspath("../../../installer/windows/MegaShellSetup.iss") + '";;;echo Installer creation not supported in Linux.'
]

fline()
for command in commands:
    command = command.split(";;;")
    if len(command) != 1:
        if system().lower() == "windows":
            command = command[0]
        elif system().lower() == "linux":
            command = command[1]
    else: command = command[0]
    print(colored("Executing: `" + command + "`", "green"))
    # deepcode ignore CommandInjection: commands from list
    os.system(command)
    fline()

end_time = time.time() - start_time

fprint("Compilation finished!")
fprint(colored("Total time: " + str(round(end_time, 2)) + "s", "green"))

fpause()
clear()


import sys

# Transitions line: 512 bits
transitions = sys.stdin.readline().rstrip()
assert len(transitions) == 1 << 9

# Empty seperator
line = sys.stdin.readline().strip()
assert not line

# Initial grid.
grid = [[ch == '#' for ch in line.rstrip()] for line in sys.stdin]

def Pad(grid, n):
    W = len(grid[0])
    return (
        [[False] * (n + W + n) for _ in range(n)] +
        [[False] * n + row + [False] * n for row in grid] +
        [[False] * (n + W + n) for _ in range(n)])

def Neighbors(r, c):
    return [(rr, cc) for rr in (r - 1, r, r + 1) for cc in (c - 1, c, c + 1)]

def Step(grid, default_bit):
    def Index(r, c):
        i = 0
        for rr, cc in Neighbors(r, c):
            bit = grid[rr][cc] if 0 <= rr < len(grid) and 0 <= cc < len(grid[rr]) else default_bit
            i += i + bit
        return i

    return [[transitions[Index(r, c)] == '#' for c in range(len(grid[r]))] for r in range(len(grid))]

def PopCount(grid):
    return sum(bit for row in grid for bit in row)

b0 = transitions[0] == '#'
s511 = transitions[511] == '#'
assert not (b0 and s511)

grid = Pad(grid, 50)
for step in range(50):
    if step == 2:
        print(PopCount(grid))
    grid = Step(grid, b0 and step % 2)
print(PopCount(grid))


import os
input_file = os.path.join('input', 'input.txt')

shape_values = { 'R': 1, 'P': 2, 'S': 3}
round_key = {
    'AX': 'S', 'AY': 'R', 'AZ': 'P',
    'BX': 'R', 'BY': 'P', 'BZ': 'S',
    'CX': 'P', 'CY': 'S', 'CZ': 'R'
}
win_key = { "X": 0, "Y": 3, "Z": 6}

try:
    reader = open(input_file)
except:
    print('cannot open input file')
    exit(1)

strategy = []

for line in reader:
    strat = line.strip('\n').split(' ')
    strategy.append([strat[0], strat[1]])

total = 0

for round in strategy:
    win_val = win_key[round[1]]
    shape_val = shape_values[round_key[round[0] + round[1]]]
    total += (win_val + shape_val)
    #print(str(win_val))
    #print(str(shape_val))

print("Total: " + str(total))


from http import HTTPStatus

from django.urls import reverse_lazy, reverse

from members.models import Member
from members.tests.test_member_base import MemberTestCase

class MemberListViewTest(MemberTestCase):

    def setUp(self):
        super().setUp()
        self.url = reverse_lazy('members:list')

    def main_page_redirect_here(self):
        response = self.client.get(reverse_lazy('main'))
        self.assertRedirects(response, self.url)

    def test_get_list_page(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, HTTPStatus.OK)
        self.assertContains(response, 'Team members')
        self.assertContains(response, 'You have 0 team members.')
        member = Member.objects.create(**self.member1_data)
        response = self.client.get(self.url)
        self.assertContains(response, 'You have 1 team members.')
        self.assertContains(response, reverse('members:edit', args=[member.id]))
        self.assertQuerysetEqual(response.context_data['members'],
                                 Member.objects.order_by('-date_joined').exclude(is_superuser=True))

        member = Member.objects.create(**self.member2_data)
        response = self.client.get(self.url)
        self.assertContains(response, 'You have 2 team members.')
        self.assertContains(response, reverse('members:edit', args=[member.id]))
        self.assertQuerysetEqual(response.context_data['members'],
                                 Member.objects.order_by('-date_joined').exclude(is_superuser=True))

        Member.objects.get(pk=member.id).delete()
        response = self.client.get(self.url)
        self.assertContains(response, 'You have 1 team members.')
        self.assertNotContains(response, reverse('members:edit', args=[member.id]))


#!/usr/bin/env python
import pandas as pd
import rospy
import csv
import sys
from move_base_msgs.msg import MoveBaseActionGoal
from visualization_msgs.msg import Marker
from std_msgs.msg import Int32
from nav_msgs.msg import Path
#from visualization_msgs.msg import Marker1

predata=pd.read_csv('~/catkin_ws/src/kcctslam/config/waypointdata/wpdata.csv')
# get timestamp
from datetime import datetime as dt
tdatetime = dt.now()
tstr = tdatetime.strftime('%y%m%d_%H%M%S')
# output to excel file
predata.drop(predata.columns[[0]], axis=1,inplace=True)
predata.to_csv('~/catkin_ws/src/kcctslam/config/waypointdata/wpdata'+tstr+".csv")

df=pd.DataFrame(columns=['x', 'y','z','qx','qy','qz','qw','type','map'])
pub = rospy.Publisher("waypoint", Marker, queue_size = 10)
pub1 = rospy.Publisher("waypoint1", Marker, queue_size = 10)
i=0
map_num=0
path=Path

def mapcallback(map_num_row):
    global map_num
    map_num=map_num_row.data

def pathcallback(path_row):
    global path
    path=path_row

def listener():
    global path
    rospy.init_node('waypoint_manager', anonymous=True)

#    rospy.Subscriber("/move_base/goal", MoveBaseActionGoal, callback)
    rospy.Subscriber("/map_num", Int32, mapcallback)
    rospy.Subscriber("/Path",Path,pathcallback)
    rospy.spin()
    i=0
    for j in path.poses:
        df.loc[i] = [j.pose.position.x,j.pose.position.y,j.pose.position.z,j.pose.orientation.x,j.pose.orientation.y,j.pose.orientation.z,j.pose.orientation.w,1,map_num]
        df.to_csv('~/catkin_ws/src/kcctslam/config/waypointdata/wpdata.csv', header=True)
        i+=1
    print(" WayPoint SAVED!!")

if __name__ == '__main__':
    listener()


import numpy as np

def write_score(filename, scores):
    import os
    l = len(scores)
    for i in range(100):
        fn = "res/%s_%d_%d" % (filename,l,i)
        if not os.path.isfile(fn):
            break
    f = open(fn, "w")
    scores = map(str,scores)
    f.write(" ".join(scores))
    f.flush()
    f.close()

def get_tile_at(x, y, observation):
    """Return the char representing the tile at the given location.
    If unknown, return None.

    Valid tiles:
    M - the tile Mario is currently on. There is no tile for a monster.
    $ - a coin
    b - a smashable brick
    ? - a question block
    | - a pipe. Gets its own tile because often there are pirahna plants in them
    ! - the finish line
    an integer in range [0,7] - 3 bit binary mask, where:
        - the first bit means "cannot go through this tile from above"
        - the second bit means "cannot go through this tile from below"
        - the third bit means "cannot go through this tile from either side"

    """
    x = int(x)
    if x < 0:
        return '7'
    y = 16 - int(y)
    x -= observation.intArray[0]
    if x < 0 or x > 21 or y < 0 or y > 15:
        return None
    index = y*22 + x
    return observation.charArray[index]

class Monster():

    """Contains information about a monster."""

    TYPES_TO_NAMES = ["Mario", "Red Koopa", "Green Koopa", "Goomba", "Spikey",
                      "Spikey", "Piranha Plant", "Mushroom", "Fire Flower",
                      "Fireball", "Shell", "Big Mario", "Fiery Mario"]

    def __init__(self, x, y, sx, sy, m_type, winged):
        # x and y positions of the monster
        self.x = x
        self.y = y
        # speed in x and y directions (i. e. instantaneous changes in x and
        # y per step)
        self.sx = sx
        self.sy = sy
        # monster type (0 to 11)
        self.m_type = m_type
        # human recognizable name for the monster
        self.m_name = Monster.TYPES_TO_NAMES[m_type]
        # winged monsters bounce up and down
        self.winged = winged

def get_monsters(observation):
    """Get all monsters from the observation (including Mario).
    Return a list of Monster objects.

    """
    monsters = []
    for i in range(len(observation.intArray[1:])/2):
        m_type = observation.intArray[1 + 2*i]
        winged = observation.intArray[2 + 2*i]
        x, y, sx, sy = observation.doubleArray[4*i : 4*(i+1)]
        monsters.append(Monster(x, y, sx, sy, m_type, winged))
    return monsters

def get_mario(monsters):
    """Get Mario from the list of monsters.
    Return a Monster object or None if Mario is not in the list.

    """
    for monster in monsters:
        if monster.m_type in [0, 10, 11]:
            return monster
    else:
        return None

def getPoint(arr,s):
    m = arr.tostring().find(s)
    dy = m / arr.shape[1]
    dx = m % arr.shape[1]
    return (dx,dy)

def getOkolica(observation,l=4,r=4,u=4,d=4):

    monsters = get_monsters(observation)
    mario = get_mario(monsters)

    x = np.reshape(np.array(observation.charArray),(16,22))[:,:21]

    dx,dy = getPoint(x,"M")

    state = x[max(0,dy-u) : dy+d+1 , max(0,dx-l) : dx+r+1]

    for m in monsters:
        mx,my = getPoint(state,"M")
        if m.m_type in [0, 10, 11]:
            continue # m is Mario
        dx = int(m.x - mario.x)
        dy = int(m.y - mario.y)
        try:
            state[my-dy,mx+dx] = "W" if m.winged else "V"
        except:
            pass

    return state


"""
Numba-accelerated functions for filtering with `scipy.LowLevelallable`

Inspired by the beautifully simply example from Juan Nunez-Iglesias
https://ilovesymposia.com/2017/03/12/scipys-new-lowlevelcallable-is-a-game-changer/

>>> import numpy as np
>>> import scipy.ndimage as nd
>>> import grizli.nbutils
>>> arr = np.random.normal(size=1024)
>>> footprint = np.ones(51, dtype=int)
>>> # Slow with numpy overheads
>>> np_filtered = nd.generic_filter(arr, np.nanmedian, footprint=footprint))
>>> # Much faster!
>>> filtered = nd.generic_filter(arr, grizli.nbutils.nanmedian, footprint=footprint))

"""

import numpy as np

from numba import cfunc, carray
from numba.types import intc, intp, float64, voidptr
from numba.types import CPointer

from scipy import LowLevelCallable

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_nanmin(values_ptr, len_values, result, data):
    """
    Manual minimum
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = np.inf
    for v in values:
        if (v < result[0]) & ~np.isnan(v):
            result[0] = v
    return 1

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_nanmax(values_ptr, len_values, result, data):
    """
    Manual minimum
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = -np.inf
    for v in values:
        if (v > result[0]) & ~np.isnan(v):
            result[0] = v
    return 1

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_npnanmin(values_ptr, len_values, result, data):
    """
    numpy.nanmin
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = np.nanmin(values)
    return 1

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_npnanmedian(values_ptr, len_values, result, data):
    """
    numpy.nanmedian
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = np.nanmedian(values)
    return 1

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_npnanmean(values_ptr, len_values, result, data):
    """
    numpy.nanmean
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = np.nanmean(values)

    return 1

@cfunc(intc(CPointer(float64), intp, CPointer(float64), voidptr))
def nb_npnansum(values_ptr, len_values, result, data):
    """
    numpy.nansum
    """
    values = carray(values_ptr, (len_values,), dtype=float64)
    result[0] = np.nansum(values)

    return 1

# Callables for scipy.ndimage.generic_filter
nanmin = LowLevelCallable(nb_nanmin.ctypes)
nanmax = LowLevelCallable(nb_nanmax.ctypes)
nbnanmin = LowLevelCallable(nb_npnanmin.ctypes)
nanmedian = LowLevelCallable(nb_npnanmedian.ctypes)
nanmean = LowLevelCallable(nb_npnanmean.ctypes)
nansum = LowLevelCallable(nb_npnansum.ctypes)


from mrjob.job import MRJob
import re
import time
import datetime

#This line declares the class PartBJob1, that extends the MRJob format.
class GasPrice_Time(MRJob):

# this class will define two additional methods: the mapper method goes here:
    def mapper(self, _, line):
        try:
            fields = line.split(",")
            if len(fields) == 7:
                time_epoch = int(fields[6])
                month = time.strftime("%m",time.gmtime(time_epoch)) #returns each month
                year = time.strftime("%Y",time.gmtime(time_epoch)) #returns each year
                key = (year, month)
                gas_price = int(fields[5])
                yield (key, (gas_price, 1))
        except:
            pass

    def combiner(self, key, gas_price_sum):
        gas_price_total = 0
        count_total = 0
        for n in gas_price_sum:
            gas_price_total += int(n[0])
            count_total += int(n[1])
        yield (key, (gas_price_total, count_total))

#and the reducer method goes after this line:
    def reducer(self, key, gas_price_sum):
        gas_price_total = 0
        count_total = 0
        for n in gas_price_sum:
            gas_price_total += int(n[0])
            count_total += int(n[1])
        yield (key, gas_price_total / count_total)

#

if __name__ == '__main__':
    GasPrice_Time.run()


import os
import glob
import unicodedata
import sentencepiece as spm

from transformers import AlbertTokenizer, AlbertTokenizerFast
from cai_common.defaults import cai_s3

from .cai_tokenizer_mixin import CAITokenizerMixin

DATA_BASE_PATH = os.environ.get('CAI_DATA_BASE_PATH', None)
if DATA_BASE_PATH in {'', '***unset***'}:
    DATA_BASE_PATH = None
if DATA_BASE_PATH is None:
    DATA_BASE_PATH = cai_s3

VOCAB_FILES_NAMES = {"vocab_file": "spm_model.model"}

PRETRAINED_VOCAB_FILES_MAP = {
    "vocab_file": {
        "olive-tiny":
            os.path.join(DATA_BASE_PATH, "champion_models/tokenizer-olive/olive_tiny/spm_model.model"),
        "olive-small":
            os.path.join(DATA_BASE_PATH, "champion_models/tokenizer-olive/olive_small/spm_model.model"),
        "olive-large":
            os.path.join(DATA_BASE_PATH, "champion_models/tokenizer-olive/olive_big/spm_model.model"),
    }
}

PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
    "olive-tiny": 512,
    "olive-small": 512,
    "olive-large": 512,
    "tibert-unigram-large": 512
}

SPIECE_UNDERLINE = "▁"

def _tokenize_shim(self, super, text, **kwargs):    # pylint: disable=redefined-builtin
    # The AlBERT tokenizer deliberately splits on the control tokens in the text to introduce white space between
    #   them. So [MASK]de dmar ro/ becomes ['[MASK]', '_de', 'dmar', 'ra', 'o', '/']. This doesn't work for
    #   Tibetan, we want it to be ['_', '[MASK]', 'de', 'dmar', 'ra', 'o', '/'] otherwise we have a huge mismatch
    #   between the training and test. So we replace the control tokens with non-Tibetan special text, tokenize as
    #   per AlBERT, then replace the special text with the control tokens back. Everything then flows correctly
    #   through the rest of the AlBERT tokenizer.
    #
    # We also introduce the option to only add the underline between sections. This can improve performance on
    #   unmasking short sentences. Likely this is happening because of a mismatch between the training and test
    #   data, which means the short sentence part of the BERT pre-training data generator is actually important.
    #   In Tibetan there is no obvious way to do this without a sentence segmentation model, so we leave this as is
    #   until we are able to make a reliable sentence segmenter as part of the part-of-speech tagging experiments.
    #   When we have a reliable segmenter we can augment the training set with short individual sentences that have
    #   the proper SentencePiece underscores in front of them and polish the transformer model.
    if ' ' in text and self.underline_between_sections:
        sections = [self.tokenize(section, **kwargs) for section in text.split(' ')]
        res = []
        for section in sections + [[]]:
            if len(section) > 0:
                res.extend(section + [SPIECE_UNDERLINE])
        if len(res) > 0:
            res = res[:-1]  # Remove the final SentencePiece underline
        return res
    if self.tsheg_pretokenization:
        for mark in self.intersyllabic_marks:
            # Break up the intersyllabic marks with a non-Tibetan token. This will force SentencePiece's BPE to
            #   not join across the out-of-vocabulary token.
            text = text.replace(mark, mark + 'a')
    special_token_map = dict(
        [(str(idx), control_token) for idx, control_token in enumerate(list(self.unique_no_split_tokens))])
    for replacement, control_token in special_token_map.items():
        text = text.replace(control_token, replacement)
    res = [
        special_token_map[token] if token in special_token_map else token
        for token in super.tokenize(text, **kwargs)]
    if self.underline_between_sections:
        final_res = []
        for token in res:
            if token == SPIECE_UNDERLINE:
                continue
            if token[0] == SPIECE_UNDERLINE:
                token = token[1:]
            final_res.append(token)
    else:
        final_res = res
    if self.tsheg_pretokenization:
        # Now remove the non-Tibetan tokens to get a strictly intersyllabic tokenization.
        final_res = [token for token in final_res if not token == 'a']
    return final_res

def concat_docstrings(*docstr):
    def docstring_decorator(fn):
        fn.__doc__ = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "")
        return fn

    return docstring_decorator

class CAITokenizerBaseMixin:
    _args_docstring = """
    Args:
        vocab_file (:obj:`string`):
            `SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
            contains the vocabulary necessary to instantiate a tokenizer.
        do_lower_case (:obj:`bool`, `optional`, defaults to :obj:`True`):
            Whether to lowercase the input when tokenizing.
        remove_space (:obj:`bool`, `optional`, defaults to :obj:`True`):
            Whether to strip the text when tokenizing (removing excess spaces before and after the string).
        keep_accents (:obj:`bool`, `optional`, defaults to :obj:`False`):
            Whether to keep accents when tokenizing.
        bos_token (:obj:`string`, `optional`, defaults to "[CLS]"):
            The beginning of sequence token that was used during pre-training. Can be used a sequence classifier token.
            .. note::
                When building a sequence using special tokens, this is not the token that is used for the beginning
                of sequence. The token used is the :obj:`cls_token`.
        eos_token (:obj:`string`, `optional`, defaults to "[SEP]"):
            The end of sequence token.
            .. note::
                When building a sequence using special tokens, this is not the token that is used for the end
                of sequence. The token used is the :obj:`sep_token`.
        unk_token (:obj:`string`, `optional`, defaults to "<unk>"):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        sep_token (:obj:`string`, `optional`, defaults to "[SEP]"):
            The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences
            for sequence classification or for a text and a question for question answering.
            It is also used as the last token of a sequence built with special tokens.
        pad_token (:obj:`string`, `optional`, defaults to "<pad>"):
            The token used for padding, for example when batching sequences of different lengths.
        cls_token (:obj:`string`, `optional`, defaults to "[CLS]"):
            The classifier token which is used when doing sequence classification (classification of the whole
            sequence instead of per-token classification). It is the first token of the sequence when built with
            special tokens.
        mask_token (:obj:`string`, `optional`, defaults to "[MASK]"):
            The token used for masking values. This is the token used when training this model with masked language
            modeling. This is the token which the model will try to predict.
        stochastic_tokenization (:obj:`bool`, `optional`, defaults to :obj:`True`):
            Whether to perform stochastic tokenization in SentencePiece or use the Viterbi tokens.
        stochastic_encoding_nbest (:obj:`int`, `optional`, defaults to :obj:`True`):
            The n-best paramater for the stochastic encoding in SentencePiece.
        stochastic_encoding_alpha  (:obj:`float`, `optional`, defaults to :obj:`True`):
            The alpha paramater for the stochastic encoding in SentencePiece.
        tsheg_pretokenization (:obj:`bool`, `optional`, defaults to :obj:`False`):
            Pre-tokenize along the intersyllabic marks, primarily the tsheg. Useful for token classification tasks like
            named-entity recognition and part-of-speech tagging.
        underline_between_sections (:obj:`bool`, `optional`, defaults to :obj:`False`):
            Whether to put the SentencePiece underline only between sections. Note that, if there is only one section,
            there will be no underline. If you're getting bad results for the unmasking of the first token in a section
            then try setting this to True.
        eor_token_id (:obj:`int`, `optional`, defaults to :obj:`-1`):
            The token ID of the register split token, usually [eor]. The SiameseEncoderModel class splits on these IDs
            during preprocessing in the forward method.
    """

    vocab_files_names = VOCAB_FILES_NAMES
    pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
    max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES

    intersyllabic_marks = [unicodedata.lookup(c) for c in [
        'TIBETAN MARK INITIAL YIG MGO MDUN MA',
        'TIBETAN MARK CLOSING YIG MGO SGAB MA',
        'TIBETAN MARK SBRUL SHAD',
        'TIBETAN MARK INTERSYLLABIC TSHEG',
        'TIBETAN MARK DELIMITER TSHEG BSTAR',
        'TIBETAN MARK SHAD',
        'TIBETAN MARK RIN CHEN SPUNGS SHAD',
        'TIBETAN MARK GTER TSHEG',
        'TIBETAN SIGN RDEL DKAR GSUM',
        'TIBETAN KU RU KHA']]

    _print_tokenizer = False
    stochastic_encoding_nbest = 64
    stochastic_encoding_alpha = 0.1
    stochastic_tokenization = False
    tsheg_pretokenization = False
    underline_between_sections = False

    def __init__(self):
        self.keep_accents = True

    def enable_siamese(self, eor_token='[eor]'):
        self.eor_token = eor_token
        self.add_tokens(eor_token)
        self.eor_token_id = self.convert_tokens_to_ids(eor_token)

    @staticmethod
    def train(training_data_glob,
              model_file,
              model_type='bpe',
              vocab_size=10000):
        """Train the SentencePiece tokenizer and save to a file.

        Args:
            training_data_glob (:obj:`string`):
                A glob for the training data to use to train SentencePiece. For example:
                '../../tibert_data/training/tibetan_sections/spm_train.*.txt'
            model_file (:obj:`string`):
                `SentencePiece <https://github.com/google/sentencepiece>`__ file (generally has a .spm extension) that
                will contain the vocabulary necessary to instantiate a tokenizer.
            model_type (:obj:`string`, `optional`, defaults to :obj:`bpe`):
                What SentencePiece model type to train. Can be 'bpe' or 'unigram'.
            vocab_sze (:obj:`int`, `optional`, defaults to :obj:`10000`):
                SentencePiece target vocabulary size.
        """

        # Settings are from https://github.com/google-research/albert/blob/master/README.md
        spm.SentencePieceTrainer.Train(
            input=glob.glob(training_data_glob),
            model_prefix=model_file,
            model_type=model_type,
            vocab_size=vocab_size,
            character_coverage=1,
            pad_id=0,
            unk_id=1,
            eos_id=-1,
            bos_id=-1,
            control_symbols="[CLS],[SEP],[MASK]",
            user_defined_symbols="(,),\",-,.,–,£,€")

@concat_docstrings(
    """Constructs the Tibert tokenizer. Very similar to the ALBERT tokenizer. Based on `SentencePiece
    <https://github.com/google/sentencepiece>`.

    This version of the tokenizer has the fast SentencePiece implementation from Hugging Face as the backend. It does
    not support stochastic tokenization.

    This tokenizer inherits from :class:`~transformers.PreTrainedTokenizerFast` which contains most of the methods.
    Users should refer to the superclass for more information regarding methods.

    """,
    CAITokenizerBaseMixin._args_docstring
)
class CAITokenizerFast(CAITokenizerBaseMixin, AlbertTokenizerFast, CAITokenizerMixin):
    def __init__(self,
                 vocab_file,
                 do_lower_case=True,
                 remove_space=True,
                 keep_accents=True,
                 bos_token='[CLS]',
                 eos_token='[SEP]',
                 unk_token='<unk>',
                 sep_token='[SEP]',
                 pad_token='<pad>',
                 cls_token='[CLS]',
                 mask_token='[MASK]',
                 **kwargs):
        AlbertTokenizerFast.__init__(
            self,
            vocab_file=vocab_file,
            do_lower_case=do_lower_case,
            remove_space=remove_space,
            keep_accents=keep_accents,
            bos_token=bos_token,
            eos_token=eos_token,
            unk_token=unk_token,
            sep_token=sep_token,
            pad_token=pad_token,
            cls_token=cls_token,
            mask_token=mask_token,
            **kwargs)
        CAITokenizerBaseMixin.__init__(self)

    def tokenize(self, text, **kwargs):
        if self.stochastic_tokenization:
            raise NotImplementedError("The fast version of CAITokenizer does not implement stochastic tokenization. "
                                      "Use the CAITokenizerSlow class for this.")
        return _tokenize_shim(self, super(), text, **kwargs)

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs):
        kwargs.setdefault("keep_accents", True)
        return super().from_pretrained(pretrained_model_name_or_path, *init_inputs, **kwargs)

@concat_docstrings(
    """Constructs the Tibert tokenizer. Very similar to the ALBERT tokenizer. Based on `SentencePiece
    <https://github.com/google/sentencepiece>`.

    This version of the tokenizer has the slow SentencePiece implementation from Hugging Face as the backend, which
    itself relies on Google's implementation of SentencePiece. This version supports stochastic tokenization, this is
    really the only reason to use this class.

    This tokenizer inherits from :class:`~transformers.PreTrainedTokenizer` which contains most of the methods. Users
    should refer to the superclass for more information regarding methods.

    """,
    CAITokenizerBaseMixin._args_docstring,
    """
    Attributes:
        sp_model (:obj:`SentencePieceProcessor`):
            The `SentencePiece` processor that is used for every conversion (string, tokens and IDs).
    """
)
class CAITokenizerSlow(CAITokenizerBaseMixin, AlbertTokenizer, CAITokenizerMixin):
    def __init__(self,
                 vocab_file,
                 do_lower_case=True,
                 remove_space=True,
                 keep_accents=False,
                 bos_token='[CLS]',
                 eos_token='[SEP]',
                 unk_token='<unk>',
                 sep_token='[SEP]',
                 pad_token='<pad>',
                 cls_token='[CLS]',
                 mask_token='[MASK]',
                 **kwargs):
        AlbertTokenizer.__init__(
            self,
            vocab_file=vocab_file,
            do_lower_case=do_lower_case,
            remove_space=remove_space,
            keep_accents=keep_accents,
            bos_token=bos_token,
            eos_token=eos_token,
            unk_token=unk_token,
            sep_token=sep_token,
            pad_token=pad_token,
            cls_token=cls_token,
            mask_token=mask_token,
            **kwargs)
        CAITokenizerBaseMixin.__init__(self)

    def _tokenize(self, text):
        # A copy of the _tokenize function from the original but with nbest and alpha exposed.
        if self._print_tokenizer:
            print(f"Tokenizing {text} with sample={self.stochastic_tokenization}")
        text = self.preprocess_text(text)

        if self.stochastic_tokenization:
            pieces = self.sp_model.SampleEncodeAsPieces(
                text,
                self.stochastic_encoding_nbest,
                self.stochastic_encoding_alpha)
        else:
            pieces = self.sp_model.EncodeAsPieces(text)
        new_pieces = []
        for piece in pieces:
            if len(piece) > 1 and piece[-1] == str(",") and piece[-2].isdigit():
                cur_pieces = self.sp_model.EncodeAsPieces(piece[:-1].replace(SPIECE_UNDERLINE, ""))
                if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                    if len(cur_pieces[0]) == 1:
                        cur_pieces = cur_pieces[1:]
                    else:
                        cur_pieces[0] = cur_pieces[0][1:]
                cur_pieces.append(piece[-1])
                new_pieces.extend(cur_pieces)
            else:
                new_pieces.append(piece)

        return new_pieces

    def tokenize(self, text, **kwargs):
        return _tokenize_shim(self, super(), text, **kwargs)

CAITokenizer = CAITokenizerFast

if __name__ == "__main__":
    training_data = os.path.join(DATA_BASE_PATH, "processed_datasets/dictionary-tokenizer-training/spm_train.txt")
    spm_tokenizers = os.path.join(DATA_BASE_PATH, "champion_models/tokenizer-olive")

    CAITokenizer.train(
        training_data,
        os.path.join(spm_tokenizers, 'olive_tiny'),
        model_type='bpe',
        vocab_size=1000)

    CAITokenizer.train(
        training_data,
        os.path.join(spm_tokenizers, 'olive_small'),
        model_type='bpe',
        vocab_size=5000)

    CAITokenizer.train(
        training_data,
        os.path.join(spm_tokenizers, 'olive_big'),
        model_type='bpe',
        vocab_size=10000)


dinner = "steak and potatoes"

# for char in dinner:
#     print(char)

numbers = [2, 3, 4, 10]
for x in numbers:
    print(x * x)

novelists = ["Robert ludlum", "Sydney Sheldon", "Jeffery Archer"]

for novelist in novelists:
    print(len(novelist))

print(novelist)
print(x)

total = 0

for number in numbers:
    total = total + number

print(total)


"""backend URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework.authtoken import views as authviews
from model.views import casesFilter, getNeighborhoodsByCity, getCasesByCityNeighborhood, getNumberCasesByNeighborhoodCityYear,getNeighborhoodsToPredict,getPrediction, updateCSV

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include('model.urls')),
    url(r'^auth/', authviews.obtain_auth_token),
    url(r'^api/cases/', casesFilter),
    url(r'^api/neighborhoods/(?P<city_searched>\w+)/$', getNeighborhoodsByCity),
    url(r'^api/datesChart/', getCasesByCityNeighborhood),
    url(r'^api/casesCount/', getNumberCasesByNeighborhoodCityYear),
    url(r'^api/neighborhoods/predict/(?P<city_searched>\w+)/$', getNeighborhoodsToPredict),
    url(r'^api/prediction/', getPrediction),
    url(r'^api/updateCSV/', updateCSV)
]


#!/usr/local/bin python3
# -*- coding: utf-8 -*-

def sort_lines_with_n(filepath: str, delimiter: str, col_num: int, reverse: bool) -> list:
    """
    入力ファイルの指定カラムでソートする関数

    Parameters
    ----------
    filepath: str
        入力ファイルパス
    delimiter: str
        デリミタ文字
    col_num: int
        指定カラム
    reverse: bool
        逆順にするか否か

    Return
    ----------
    指定カラムの異なり文字列の集合
    """
    origin = []

    with open(filepath, 'r') as f:
        for line in f:
            line_split = line.split(delimiter)
            line_split[-1] = line_split[-1].rstrip('\n')
            origin.append(line_split)

    return sorted(origin, key = lambda x: x[col_num-1], reverse = reverse)

def main():
    in_filepath = './hightemp.txt'

    sorted_lines = sort_lines_with_n(in_filepath, '\t', 3, False)

    for line in sorted_lines:
        print('\t'.join(line))

if __name__ == '__main__':
    main()


from telebot import types
from di_configuration import DIBot
from .open_menu import open_menu as menu

def about_dev(message):
    bot = DIBot.di_bot()
    inline_key = types.InlineKeyboardMarkup()
    url_button = types.InlineKeyboardButton(text="ПРИСОЕДИНЯЙСЯ!", url="https://vk.com/itclub_psuti")
    inline_key.add(url_button)
    about_msg = bot.send_message(
        message.chat.id,
        text='Чат-бот разработан студентами <b>ИТ-клуба ПГУТИ</b> для будующих студентов.'
             '\nБудем рады увидеть Вас в наших рядах!'
             '\nhttps://pp.vk.me/c629125/v629125487/21029/Qd9czt4U02E.jpg',
        parse_mode='HTML',
        reply_markup=inline_key)
    bot.register_next_step_handler(about_msg, open_menu)

def open_menu(message):
    menu(message)

handlers = {'about': about_dev}


# python3
# 221RDB190DanielsTumans
import sys
import threading

def compute_height(n, parents):
    tree = [[] for _ in range(n)]
    for i, parent in enumerate(parents):
        if parent == -1:
            root = i
        else:
            tree[parent].append(i)

    def height(node):
        if not tree[node]:
            return 0
        return max(height(child) for child in tree[node]) + 1

    max_height = height(root) + 1
    return max_height

def main():

    bebr = str(input())

    if "I" in bebr:
        n = int(input())
        parents = [int(z) for z in input().split()]
        print(compute_height(n, parents))

    elif "F" in bebr:
        bebrik = str(input())
        if 'a' in bebrik:
            print("Error")
            exit()
        bebrik = "test/" + bebrik
        with open(bebrik, 'r') as file:
            n = int(file.readline())
            parents = [int(z) for z in file.readline().split()]
        height = compute_height(n, parents)
        print(height)

sys.setrecursionlimit(10**7)
threading.stack_size(2**27)
threading.Thread(target=main).start()


from builder.converttmx import ConvertTmx
import unittest
import polib
import tempfile
from os import path

class TestConvertTmx(unittest.TestCase):
    def _get_files(self, filename):
        tmx_file = path.dirname(path.realpath(__file__))
        tmx_file += "/data/conversions/tmx/{0}".format(filename)

        tmpfile = tempfile.NamedTemporaryFile()
        po_filename = tmpfile.name + ".po"

        return tmx_file, po_filename

    def test_conversion(self):
        tmx_file, po_filename = self._get_files("test.tmx")
        convertTmx = ConvertTmx(tmx_file, po_filename)
        convertTmx.convert()

        entries = polib.pofile(po_filename)

        self.assertEqual(len(entries), 2)
        self.assertEqual(entries[0].msgid, "Modern visual refresh")
        self.assertEqual(entries[0].msgstr, "Una renovació visual moderna")
        self.assertEqual(
            entries[0].tcomment,
            "id: appstores:fx_androidwhatsnewandroid_57lang:a-clean-modern-visual-refresh",
        )

        self.assertEqual(entries[1].msgid, "As You Like It")
        self.assertEqual(entries[1].msgstr, "Com vulgueu")
        self.assertEqual(entries[1].tcomment, "")

    def test_conversion_omegat(self):
        tmx_file, po_filename = self._get_files("omegat.tmx")
        convertTmx = ConvertTmx(tmx_file, po_filename)
        convertTmx.convert()

        entries = polib.pofile(po_filename)

        self.assertEqual(len(entries), 1)
        self.assertEqual(entries[0].msgid, '"Aligner" aligner utility')
        self.assertEqual(entries[0].msgstr, 'Alineador de textos "Aligner"')
        self.assertEqual(entries[0].tcomment, "")

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


from webrecorder.apiutils import wr_api_spec
from webrecorder.basecontroller import BaseController
from bottle import request, static_file, HTTPResponse, response
import os
import json
import sys
import tarfile
import re
import logging
import requests
import shutil
import traceback

# ============================================================================
class BehaviorMgr(BaseController):
    def __init__(self, *args, **kwargs):
        super(BehaviorMgr, self).__init__(*args, **kwargs)
        self.behaviors_api = os.environ.get('BEHAVIOR_API', 'http://behaviors:3030')
        self.behaviors_root = os.environ.get('BEHAVIORS_DIR')

        self.behaviors_tarfile = os.environ.get('BEHAVIORS_TARFILE')

        self.default_behavior = {}
        self.behaviors = {}

        if self.behaviors_root:
            self.behaviors_data = os.path.join(self.behaviors_root, 'dist')
            self.behaviors_metadata = os.path.join(self.behaviors_data, 'behaviorMetadata.json')

            try:
                self.unpack_behaviors()
                self.load()
                self.init_routes()
            except Exception as e:
                traceback.print_exc()

    def unpack_behaviors(self):
        if not self.behaviors_tarfile:
            logging.info('No Behaviors Tarfile specified, skipping unpack')

        os.makedirs(self.behaviors_data, exist_ok=True)

        current_tarfile = os.path.join(self.behaviors_root, os.path.basename(self.behaviors_tarfile))

        try:
            if os.path.isfile(current_tarfile) and os.path.getmtime(current_tarfile) > os.path.getmtime(self.behaviors_tarfile):
                logging.info('Already have latest behaviors, not unpacking')
                return
        except Exception as e:
            print(e)

        logging.info('Unpacking behaviors {0} -> {1}'.format(self.behaviors_tarfile, self.behaviors_root))
        tar = tarfile.open(self.behaviors_tarfile, 'r')
        tar.extractall(self.behaviors_root)
        tar.close()

        shutil.copyfile(self.behaviors_tarfile, current_tarfile)

    def load(self):
        logging.info('Loading behaviors from {0}'.format(self.behaviors_metadata))

        with open(self.behaviors_metadata, 'rt', encoding='utf-8') as fh:
            metadata = fh.read()
            metadata = json.loads(metadata)
            self.default_behavior = metadata['defaultBehavior']
            self.behaviors = {}

            for behavior in metadata['behaviors']:
                rx = re.compile(behavior['match']['regex'])
                self.behaviors[behavior['name']] = (behavior, rx)

    def find_match(self, url):
        if not url:
            return None

        for behavior, rx in self.behaviors.values():
            if rx.match(url):
                return behavior

        return None

    def init_routes(self):
        wr_api_spec.set_curr_tag('Behaviors')

        @self.app.get('/api/v1/behavior/info-list')
        def info_list():
            # for non-desktop proxy mode, proxy to behaviors server
            if not self.behaviors_root:
                query = dict(request.query)
                res = requests.get(self.behaviors_api + '/info-list', params=query)
                response.content_type = 'application/json; charset=utf-8'
                return res.content

            url = request.query.getunicode('url')
            behavior = self.find_match(url)
            behavior_list = []
            if behavior:
                behavior_list.append(behavior)

            behavior_list.append(self.default_behavior)
            return {'behaviors': behavior_list}

        @self.app.get('/api/v1/behavior/behavior')
        def get_behavior():
            # for non-desktop proxy mode, proxy to behaviors server
            if not self.behaviors_root:
                query = dict(request.query)
                res = requests.get(self.behaviors_api + '/behavior', params=query)
                response.content_type = 'application/json; charset=utf-8'
                return res.content

            url = request.query.getunicode('url')
            name = request.query.getunicode('name')
            behavior = None
            if name:
                res = self.behaviors.get(name)
                if res:
                    behavior = res[0]
            elif url:
                behavior = self.find_match(url)

            if not behavior:
                behavior = self.default_behavior

            res = static_file(behavior['fileName'], root=self.behaviors_data)
            return res

        wr_api_spec.set_curr_tag(None)


def solution(food_times, k):
    minimum_stack = 0
    sorted_times = [(0,0) for _ in len(food_times)]
    for i in range(len(food_times)):
        sorted_times[i] = (food_times[i], i)
    sorted_times.sort(key=lambda x : (x[0], x[1]))

    sorted_index = 0
    length = len(food_times)
    while k > 0:
        minimum, _ = sorted_times[sorted_index]

        if k > (minimum - minimum_stack) * (length):
            k -= (minimum - minimum_stack) * (length)

            sorted_index += 1
            length -= 1
            minimum_stack = minimum

        else:
            return k % length + 1

print(solution([3, 1, 2], 5)
      )


from django.conf import settings
from django.conf.urls import patterns, url, include
from rest_framework.routers import DefaultRouter
from views import *

router = DefaultRouter()
router.register(r'coins/sets', CoinSetViewSet)
router.register(r'banknotes/sets', BanknoteSetViewSet)
'''
router.register(r'mints', viewsets.MintViewSet)
router.register(r'countries', viewsets.CountryViewSet)
router.register(r'currencies', viewsets.CurrencyViewSet)
router.register(r'collections', viewsets.CollectionViewSet)
'''

urlpatterns = patterns(
    'coins.views',
    url(r'^', include(router.urls)),
    url(r'^user/token', DeviceTokenView.as_view()),
    url(r'^user/login', AuthTokenView.as_view())
)

if ('rest_framework.authentication.SessionAuthentication'
    in settings.REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] and
    'rest_framework.renderers.BrowsableAPIRenderer'
        in settings.REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES']):
    urlpatterns += patterns(
        '',
        url(r'^auth/', include(
            'rest_framework.urls',
            namespace='rest_framework'))
    )


# -*- coding: utf-8 -*-
"""
Created on Wed Sep  7 08:45:00 2022
@author: sahakyz
"""

# First install data that comes with the textbook
# Type in the right lower window $pip install wooldridge

#%%
import wooldridge as woo

#%%
# After that we need our standard packages

import pandas as pd # data import and manipulation, pandas stands for Python Data Analysis Library
import numpy as np # math operations
import statsmodels.formula.api as smf #regressions
import seaborn as sns # library for plotting /easy version
import matplotlib.pyplot as plt # library for plotting
from scipy import stats #library for statistical functions

#%%
# Load Data wage2.csv file

wage2df = woo.dataWoo('wage2')

#%%

#%%
'Drop missing observations'
wage2df.dropna(inplace=True) #Drop NAN observations
wage2df.reset_index(drop=True, inplace=True) #Reset index

#%%

# Examine the data'
print(wage2df[:5]) #show me the first 5 observations

#%%
print(wage2df.head()) #show me the first (5) observations

print(wage2df.tail()) #show me the last (5) observations

#%%
#Simple plot
plt.plot(wage2df.educ, wage2df.wage, 'ro')
plt.xlabel("Education")
plt.ylabel("Wage Rate")
plt.title("Hourly Wage Rate vs Years of Schooling")
plt.show()

#%%
'Create a new variable and assign to your DataFrame'

wage2df['lnwage']=np.log(wage2df.wage) # log(wage)

#%%
wage2df['educ2']=np.square(wage2df.educ) # educ**2

#%%
# Plot Data with regression line going through it
sns.regplot(x='educ', y='wage', data=wage2df).set_title('Return on Education')

#%%
#Run regression
reg = smf.ols('wage ~ educ', data=wage2df, missing='drop').fit()
print(reg.summary2())

#%%
#Get fitted (predicted) values of y and print them
'wagehat =b0+b1*educ'

print(reg.fittedvalues)

#%%
'Get Fitted values/predicted values and assign them to yhat variable in your dataframe'
wage2df['wagehat']=reg.fittedvalues

#%%
'Get residuals and assign to your datatframe'
wage2df['uhat']=reg.resid

#%%
# Residual Plot
plt.scatter(wage2df.wagehat, wage2df.uhat)
plt.show()

#%%
#Or use sns package
#Residual Plot
sns.residplot(x='educ', y='wage', \
            data=wage2df).set_title('Residuals vs Years of Schooling')

#%%
#Creating new dataframe where wage >900 and save as a new datafrae

wage2df900=wage2df[wage2df['wage']>900]

#%%
#Get the number of observations in this dataframe
print(wage2df900.shape[0])

#%%
# Report your results in for hoemwork like this
#Question 2 part a
print('The number of observation that have \
      wage rate above 900 is', wage2df900.shape[0])

#%%
#You can also use count() function
print(wage2df900.count())

#%%
#Report as this
print(wage2df900.count()[1])

#%%
#Describe wage column in wage2 dataframe
print(wage2df['wage'].describe())

#%%

print(wage2df['wage'].describe()[2])

#%%
print('The standard deviation of wage rate in our suample is', wage2df['wage'].describe()[2])


from prettytable import PrettyTable
from prettytable.colortable import ColorTable, Themes

table = PrettyTable()

table.add_column("Pokemon Name", ["Pikachu", "Squirtle", "Charmander"])
table.add_column("Type", ["Electric", "Water", "Fire"])

table.align = "l"
print(table)

# Costum table with colors
color_table = ColorTable(theme=Themes.OCEAN)

color_table.add_column("Student Name", ["Eka", "Budi", "Siti"])
color_table.add_column("Age", [16, 15, 17])

print(color_table)

Day 16 - Object Oriented Programming/practice_using_python_packages.py


import webapp2

import validators

form = """
<form method="post">
    What is your birthday?

    Month
    <input type="text" name="month" value="{month}">

    Day
    <input type="text" name="day" value="{day}">

    Year
    <input type="text" name="year" value="{year}">

 {error}

    <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):
    def write_form(self, error="", month="", day="", year=""):
        self.response.write(form.format(error=validators.esc(error),
                                        month=validators.esc(month),
                                        day=validators.esc(day),
                                        year=validators.esc(year)))

    def get(self):
        self.write_form()

    def post(self):
        user_month = self.request.get('month')
        user_day = self.request.get('day')
        user_year = self.request.get('year')

        month = validators.valid_month(user_month)
        day = validators.valid_day(user_day)
        year = validators.valid_year(user_year)

        if not (month and day and year):
            self.write_form("That's an error buddy!",
                            user_month,
                            user_day,
                            user_year)
        else:
            self.redirect("/thanks")

class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Thanks!")

app = webapp2.WSGIApplication([('/', MainPage),
                               ("/thanks", ThanksHandler)],
                               debug=True)


from .image import Image
from .offset import Offset
from os.path import split
from os import chdir
from PIL import UnidentifiedImageError
from typing import Union
import numpy as np

class Project():
    def __init__(self,mis_filepath:str,cal_value:int,image_offset_pairs:dict) -> None:
        #mis_filepath should always be an absolute filepath.
        self.folder:str=split(mis_filepath)[0]
        chdir(self.folder)
        self.name:str=split(mis_filepath)[1]
        self.filepath=mis_filepath
        self.cal:int=cal_value
        self.images:dict[str,Image] = dict()
        self.image_err_descs:dict[str,list[str]] = dict()
        self.image_err_types:dict[str,list[str]] = dict()
        self.update_images(list(image_offset_pairs.keys()))
        self.offsets:dict[str,Offset] = dict()
        self.update_offsets(image_offset_pairs)

    def update_mis_filepath(self,new_mis_filepath:str):
        #new_mis_filepath should always be absolute path
        self.filepath=new_mis_filepath
        if split(new_mis_filepath)[0]==self.folder:
            self.name=split(new_mis_filepath)[1] #if file is in the same folder as before, then just update the project name
        else:
            self.folder:str=split(new_mis_filepath)[0]
            chdir(self.folder)
            self.name:str=split(new_mis_filepath)[1]
            self.reload_images_offsets()
    def reload_images_offsets(self):
        reload_pairs=self.get_image_offset_pairs()
        self.clear_arrays()
        self.update_images(list(reload_pairs.keys()))
        self.update_offsets(reload_pairs)
    def clear_arrays(self):
        self.images.clear()
        self.image_err_descs.clear()
        self.image_err_types.clear()
        self.offsets.clear()
    def get_image_offset_pairs(self) -> dict[str,list[str,tuple,float]]:
        #generate dictionary of image_fp:offset list for all unique image_fp.
        unique_image_fp=self.get_unique_image_fps()
        output_dict=dict()
        for image_fp in unique_image_fp:
            if image_fp in self.offsets:
                output_dict[image_fp]=self.offsets[image_fp].get_list()
            else:
                output_dict[image_fp]=None
        return output_dict
    def get_unique_image_fps(self):
        offset_refs=[]
        for offset_value in self.offsets.values():
            offset_refs.append(offset_value.get_ref())
        unique_image_fp=set()
        unique_image_fp.update(
            self.images,
            self.image_err_types,
            self.offsets,
            offset_refs
        )
        return unique_image_fp

    def update_images(self,image_fps:list[str]) -> None:
        #will add or update all valid image filepaths in dictionary keys.
        #TODO consider making this accept both dict and list
        for image_fp in image_fps:
            try:
                new_image=Image(image_fp)
                self.images[image_fp]=new_image
                self.reset_image_err(image_fp,"Image")
            except FileNotFoundError:
                self.record_image_err(image_fp,"Image","OS.FileNotFoundError")
            except UnidentifiedImageError:
                self.record_image_err(image_fp,"Image","PIL.UnidentifiedImageError")
    def update_offsets(self,im_of_pairs:dict[str,list[str,tuple,float]]) -> None:
        for key_image_fp,value_offset in im_of_pairs.items():
            if value_offset is not None:
                self.offsets[key_image_fp]=Offset(value_offset)
                self.reset_image_err(key_image_fp,"Offset")
            else:
                self.record_image_err(key_image_fp,"Offset","MIS.NoneOffset")
    def replace_image(self,new_image_fp:str,old_image_fp:str):
        if new_image_fp not in self.images:
            self.update_images([new_image_fp])
        self.offsets[new_image_fp]=self.offsets[old_image_fp]
        for image_fp in self.offsets:
            if self.offsets[image_fp].get_ref()==old_image_fp:
                self.offsets[image_fp].set_ref(new_image_fp)
        self.delete_image(old_image_fp)
    def delete_image(self,image_fp:str):
        #removes image from images, image_errs, offsets, and removes any offsets which direct to the image.
        self.images.pop(image_fp,None)
        self.image_err_types.pop(image_fp,None)
        self.image_err_descs.pop(image_fp,None)
        self.offsets.pop(image_fp,None)
        for img_fp in self.offsets.copy():
            if self.offsets[img_fp].get_ref()==image_fp:
                self.offsets.pop(img_fp,None)
                self.record_image_err(img_fp,"Offset","MIS.NoneOffset")

    def get_origins(self) -> Union[list[str],None]:
        origins:list[str] = []
        for key_image_fp,value_offset in self.offsets.items():
            if key_image_fp == value_offset.get_ref():
                origins.append(key_image_fp)
        #TODO handle situations where there are multiple origins -> raise exception?
        if origins: #empty list will return false, list with values will return true
            return origins
        else:
            return None
    def get_origin_key(self) -> Union[str,None]:
        origins = self.get_origins()
        if origins==None:
            return None #if there is no origin, cannot find origin relative offset.
        if len(origins)==1:
            origin=origins[0] #ideal case
        elif len(origins)>1:
            #TODO handle multiple-origin considerations further
            origin=origins[0]
        return origin
    def get_origin_relative_offsets(self,specific_image_fps:list[str] = None) -> Union[dict[str,Offset],None]:

        #setup origin key
        origin = self.get_origin_key()
        if origin==None:
            return None #if there is no origin, cannot find origin relative offset.

        #setup lists for swapping offsets.
        to_swap:list[str] = [origin]
        origin_offsets:dict[str,Offset] = {origin:Offset([origin,(0,0),0.0])}
        offsets=self.offsets.copy()#copy so that the actual self.offsets don't get deleted.
        del offsets[origin]
        #carry out swap procedure starting at origin and mapping through all connected offsets.
        while to_swap: #as long as to_swap is not empty will keep running
            for swap in to_swap.copy():#make copy so that to_swap can have values added/removed while for loop is running.
                for offset in offsets.copy():#make copy so that to_swap can have values added/removed while for loop is running.
                    if offsets[offset].get_ref()==swap: #when image references currently image being swapped
                        origin_offsets[offset]=Offset([
                            origin,
                            (
                                offsets[offset].get_x()+origin_offsets[swap].get_x(),
                                offsets[offset].get_y()+origin_offsets[swap].get_y()
                            ),
                            0.0
                            ]
                        ) #do the swap and create the new origin relative offset.
                        to_swap.append(offset) #will swap this image on the next to_swap loop
                        del offsets[offset] #don't want to check it again
                to_swap.remove(swap) #have replaced it in all places it was referenced, don't want to check it again.
        #after swapping all the images which referenced to the origin, add an image err for all the other ones.
        for image_fp in offsets:
            self.record_image_err(image_fp,"Offset","MIS.NotOriginOffset")
        #return all origin relative offsets if specified image filepaths are None, return just those specific image offsets if they are specified.
        if specific_image_fps is None:
            return origin_offsets
        else:
            specific_origin_offsets:dict[str,Offset] = dict()
            for image_fp in specific_image_fps:
                if image_fp in origin_offsets:
                    specific_origin_offsets[image_fp]=origin_offsets[image_fp] #compiles any of the specific images which have origin offsets
            if specific_origin_offsets:#will be true when dict is not empty
                return specific_origin_offsets
            else:
                return None
    def get_canvas_offsets(self,specific_image_fps:list[str]=None) -> Union[dict[str,tuple[tuple[int,int],float]],None]:
        #TODO handle rotation
        #generate origin relative offsets
        origin_relative_offsets=self.get_origin_relative_offsets(specific_image_fps=specific_image_fps)
        if origin_relative_offsets == None:
            return None
        #find minimum values
        x_values=[]
        y_values=[]
        for oro in origin_relative_offsets:
            x_values.append(origin_relative_offsets[oro].get_x())
            y_values.append(origin_relative_offsets[oro].get_y())
        min_x=min(x_values)
        min_y=min(y_values)
        #create new dictionary that makes the minimum values equal to 0 and adjusts others accordingly
        canvas_offsets=dict()
        for oro in origin_relative_offsets:
            canvas_offsets[oro]=(
                (
                    origin_relative_offsets[oro].get_x()-min_x,
                    origin_relative_offsets[oro].get_y()-min_y
                ),
                0.0
            )
        return canvas_offsets
    def get_canvas_size(self,specific_image_fps:list[str]=None) -> Union[tuple[int,int],None]:
        #generate canvas offsets
        canvas_offsets=self.get_canvas_offsets(specific_image_fps=specific_image_fps)
        if canvas_offsets == None:
            return None
        #removes image_fp which have image errors.
        for image_fp in canvas_offsets.copy(): #Maybe do this 'error' detection somewhere else?
            if "Image" in self.image_err_types.get(image_fp,[]): #TODO method to check for list of error types for an image_fp
                del canvas_offsets[image_fp]
        #minimum value is known to be 0,0 so just store max values of offset+size
        x_values=[]
        y_values=[]
        for image_fp in canvas_offsets:
            x_values.append(canvas_offsets[image_fp][0][0]+self.images[image_fp].size[0])
            y_values.append(canvas_offsets[image_fp][0][1]+self.images[image_fp].size[1])
        max_x=max(x_values)
        max_y=max(y_values)
        canvas_size=(max_x,max_y)
        return canvas_size

    def dfe_canvas_layer(self,canvas_size:tuple[int,int],image_fp:str,canvas_offset:tuple[tuple[int,int],float]):
        return self.place_in_zeroes(
            canvas_size=canvas_size,
            layers=1,
            data_array=self.images[image_fp].dfe_array,
            canvas_offset=canvas_offset
        )
    def image_canvas_layer(self,canvas_size:tuple[int,int],image_fp:str,canvas_offset:tuple[tuple[int,int],float]):
        return self.place_in_zeroes(
            canvas_size=canvas_size,
            layers=3,
            data_array=self.images[image_fp].image_array,
            canvas_offset=canvas_offset
        )
    @staticmethod
    def place_in_zeroes(canvas_size:tuple[int,int],layers:int,data_array:np.ndarray,canvas_offset:tuple[tuple[int,int],float]):
        if len(data_array.shape)==2:
            data_array=data_array[:,:,np.newaxis]
        np_canvas_size=(
            canvas_size[1],#convert from (x,y) to (#rows,#columns)
            canvas_size[0],
            layers
        )
        np_rect_off=(
            canvas_offset[0][1],#convert from (x,y) to (#rows,#columns)
            canvas_offset[0][0]
        )
        data_size=(
            data_array.shape[0],
            data_array.shape[1]
        )
        layer=np.zeros(np_canvas_size,dtype=int)
        layer[
            np_rect_off[0]:np_rect_off[0]+data_size[0],
            np_rect_off[1]:np_rect_off[1]+data_size[1],
            :
        ]=data_array
        return layer

    def record_image_err(self,image_fp:str,err_type:str,err_desc:str) -> None:
        #record that there was an error with an image
        #err_type -> is the error caused by the image(Image) or the offset(Offset)
        if image_fp not in self.image_err_descs:
            self.image_err_descs[image_fp]=list()
            self.image_err_types[image_fp]=list()
        self.image_err_descs[image_fp].append(err_desc)
        self.image_err_types[image_fp].append(err_type)
    def reset_image_err(self,image_fp:str,err_type:str):
        if image_fp in self.image_err_types:
            to_remove=[]
            for i in range(len(self.image_err_types[image_fp])):
                if self.image_err_types[image_fp][i]==err_type:
                    to_remove.append(i)
            for i in reversed(to_remove):#reversed so removal won't impact indices that are yet to be removed
                self.image_err_types[image_fp].pop(i)
                self.image_err_descs[image_fp].pop(i)


#!/usr/bin/python3.6

#
#
# Roger D. Pease 28 Mar 2020
#
#    See readme at top of this repo for ground rules.
#

import pygame
from threading import Timer
import time
import random
from random import shuffle
from os import listdir
from os.path import isfile, join,isdir
import sys

debug = 0

def DebugMessage(str):
  if debug == 1:
    print(str)

#
# Hold the state of our game.
#
#
class GameStateClass:
     def __init__(self,imagePath):
         # Set Variables.
         self.MasterWindowX = 800   # Width
         self.MasterWindowY = 800   # Height
         self.blockEvents = False
         self.done = False
         self.XSquaresInRow = 4
         self.YSquaresInCol = 4
         self.Border = 2
         self.XPicMeasInclBorder = self.MasterWindowX /self.XSquaresInRow
         self.YPicMeasInclBorder = self.MasterWindowY /self.YSquaresInCol
         self.XPicMeas = self.XPicMeasInclBorder - 2*self.Border
         self.YPicMeas = self.YPicMeasInclBorder - 2*self.Border
         self.NumSquares = self.XSquaresInRow*self.YSquaresInCol
         self.NumPairsSquares = int(self.NumSquares/2)
         self.IsEvenNumberOfSquares = (self.NumSquares == (self.NumPairsSquares*2))

         # Start Main Window.
         self.screen = pygame.display.set_mode((self.MasterWindowX,self.MasterWindowY))
         pygame.display.set_caption("Match Game")

         # Keep an image bank in the images/ subdirectory.
         self.imageBank = []
         if not isdir(imagePath):
             self.Valid = 0
             return
         self.imageFileNames = [
                # join is inherited from os- it turns ("/home/user", "program") to "/home/user/program".
                # Then we use this list
               join(imagePath,f) for f in listdir(imagePath)
                 if isfile(join(imagePath, f))
                  ]
         self.Valid = 1  # Did we get constructed correctly?

         NumPicsAvailable = len(self.imageFileNames)
         DebugMessage("Loaded " + str(NumPicsAvailable) + " pictures")
         if (NumPicsAvailable < self.NumPairsSquares):
           print ("I needed " + str(self.NumPairsSquares) + " pictures in images directory but only " + str(NumPicsAvailable) +" were available")
           self.Valid = 0
           return
         self.ImageRectBank = []
         self.imageBankIndexBySquareNumber = []

         # To select random pictures, we put numbers 0..n-1
         # where n is # of pictures into an array.
         # then we shuffle that array, then take the first s indexes
         # of that array where s is numberof pairs of boxes.

         self.ArrayOfPicNumbers = list(range(0,len(self.imageFileNames)))
         shuffle(self.ArrayOfPicNumbers)

         # Now pick which picture goes where. Build an array and re-sort it randomly
         for index in range (0,self.NumPairsSquares):
          self.imageBankIndexBySquareNumber.append(self.ArrayOfPicNumbers[index])
          self.imageBankIndexBySquareNumber.append(self.ArrayOfPicNumbers[index])

         # Add one more
         if not self.IsEvenNumberOfSquares:
           self.imageBankIndexBySquareNumber.append(self.ArrayOfPicNumbers[self.NumPairsSquares])
         shuffle(self.imageBankIndexBySquareNumber)

	 # Mark all squares as covered.
         self.IsCovered = []
         for index in range (0,self.NumSquares):
             self.IsCovered.append(True)

         # Resize all Images.
         for imageIndex in range(0, len(self.imageFileNames)):
           self.imageBank.append(pygame.image.load(self.imageFileNames[imageIndex]).convert())
           imageOrigX,imageOrigY = self.imageBank[imageIndex].get_size()
           imageOrigRatio = imageOrigX/imageOrigY
           displayBoxRatio = self.XPicMeas/self.YPicMeas
           # Resize Image to fit the box.
           scaledImageX = 0
           scaledImageY = 0
           # Figure out if the display box  is wider or narrower than the image.
           if (imageOrigRatio > displayBoxRatio):
             scaledImageX = int(self.XPicMeas)
             scaledImageY = int(self.YPicMeas * displayBoxRatio)
           elif (imageOrigRatio < displayBoxRatio):
             scaledImageX = int(self.XPicMeas * displayBoxRatio)
             scaledImageY = int(self.YPicMeas)
           else:
             scaledImageX = int(self.XPicMeas)
             scaledImageY = int(self.YPicMeas)
             DebugMessage("Not Resizing image "+str(imageIndex))
           self.imageBank[imageIndex] = pygame.transform.scale(self.imageBank[imageIndex],(scaledImageX,scaledImageY))
           self.ImageRectBank.append(self.imageBank[imageIndex].get_rect())

         self.gameOverScreen = False
         self.blockEvents = False
         self.redraw = False
         self.firstClickedSquare = -1
         self.secondClickedSquare = -1

     def GetSquareFromMousePos(self,mouseposx,mouseposy):
       colIndex = int(mouseposx/self.XPicMeasInclBorder)
       rowIndex = int(mouseposy/self.YPicMeasInclBorder)
       squareNumber = rowIndex*self.XSquaresInRow + colIndex
       return squareNumber

     def DrawGameScreen(self):
       self.screen.fill(WHITE)
       for colIndex in range (0,self.XSquaresInRow):
        for rowIndex in range (0,self.YSquaresInCol):
          squareNumber = rowIndex*self.XSquaresInRow + colIndex
          imageBankIndex = self.imageBankIndexBySquareNumber[squareNumber]
          xPos = colIndex*self.XPicMeasInclBorder + self.Border
          yPos = rowIndex*self.YPicMeasInclBorder + self.Border
          self.screen.blit(self.imageBank[imageBankIndex],(xPos,yPos))
      #-- Draw Cover and label
          if self.IsCovered[squareNumber]:
            pygame.draw.rect(self.screen,WHITE,(xPos,yPos,self.XPicMeasInclBorder,self.YPicMeasInclBorder))
            myfont = pygame.font.SysFont("monospace", 45)
            label = myfont.render(str(squareNumber+1), 1, BLUE)
            label_width,label_height = label.get_size()
            label_x = xPos - self.Border + ((self.XPicMeasInclBorder-label_width)/2)
            label_y = yPos - self.Border + ((self.YPicMeasInclBorder-label_height)/2)
            self.screen.blit(label, (label_x,label_y))

     def AllMatched(self):
      for index in range (0,self.NumSquares):
        if (self.IsCovered[index]):
          return False
      return True

     def SetAllMatched(self):
      for index in range (0,self.NumSquares):
        self.IsCovered[index] = False

     def RecoverBothSquares(self):
      DebugMessage('Recovering Both squares')
      self.IsCovered[self.firstClickedSquare] = True
      self.IsCovered[self.secondClickedSquare] = True
      self.blockEvents = False
      self.redraw  = True
      self.firstClickedSquare = -1
      self.secondClickedSquare = -1

     def RestartGame(self):
      self.done = True

     def DrawGameOverScreen(self):
      self.screen.fill(WHITE)
      myfont = pygame.font.SysFont("monospace", 30)
      label = myfont.render("You Won!", 1, BLUE)
      label_spacing = 6
      label_width,label_height = label.get_size()
      label_x = (self.MasterWindowX - label_width)/2
      label_y = (self.MasterWindowY - label_height)/2
      self.screen.blit(label, (label_x,label_y))

      self.timer = Timer(2,self.RestartGame)
      self.timer.start();

     def ImageMatch (self):
      imageInFirstSquare = self.imageBankIndexBySquareNumber[self.firstClickedSquare]
      imageInSecondSquare = self.imageBankIndexBySquareNumber[self.secondClickedSquare]
      return (imageInFirstSquare == imageInSecondSquare)

     def ProcessEvent(self,squareClicked):
      DebugMessage ('Square Clicked'+str(squareClicked))
      # If we clicked a square, see if it was first or second.
      # If a first, uncover it.
      # If a second, uncover it and see if there was a match.

      if (not squareClicked == -1):
        if (not self.IsCovered[squareClicked]):  # We already clicked this one.
          return
        if (self.firstClickedSquare == -1):
          DebugMessage ('First Square Clicked '+ str( squareClicked))
          self.firstClickedSquare = squareClicked;
          self.IsCovered[self.firstClickedSquare] = False
        elif (self.secondClickedSquare == -1):
          DebugMessage ('Second Square Clicked ' + str( squareClicked))
          if not self.firstClickedSquare == squareClicked:
            DebugMessage ('Second box diff from first')
            self.secondClickedSquare = squareClicked;
            self.IsCovered[self.secondClickedSquare] = False
            if (self.ImageMatch()):
              DebugMessage ('Match!')
              # We matched both squares.
              self.firstClickedSquare = -1
              self.secondClickedSquare = -1
            else:
              DebugMessage ('We did not match')
           # Blank the two mismatch squares
              self.blockEvents = True
              self.t = Timer(1.0,self.RecoverBothSquares)
              self.t.start()
          else:
            DebugMessage ('Second Square Matched First Square Ignoring.', squareClicked)

# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)
BLUE     = (   0,   0, 255)

# Loop until the user clicks the close button.

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

def TestProcedure():
   # Verify we exit if No Pics available.
   MyGame = GameStateClass("phonyPath");
   assert (MyGame.Valid == 0)
   return 1

#
# Handler loop. I don't like main() to be any bigger than a screen length.
#
#

def HandlerLoop(pygame,MyGame):
  while not MyGame.done:
    firstPass = True
    while not MyGame.done:
      eventHappened = False
      squareClicked = -1
      # --- Main event loop
      if (MyGame.blockEvents):
        pygame.event.clear()

      if (pygame.event.peek):
        for event in pygame.event.get(): # User did something
          DebugMessage("Processing Event")
          if event.type == pygame.QUIT: # If user clicked close
            MyGame.done = True # Flag that we are done so we exit this loop
          elif event.type == pygame.MOUSEBUTTONDOWN: # If user clicked close
            eventHappened = True
            if event.button == 1:  # Left mouse button
              mouseposx,mouseposy = pygame.mouse.get_pos();
              squareClicked = MyGame.GetSquareFromMousePos(mouseposx,mouseposy)
          elif event.type == pygame.KEYDOWN : # If user clicked close
            key = pygame.key.get_pressed()
            DebugMessage("Key Down")
            if event.key == pygame.K_q:
              sys.exit(0)
            if event.key == pygame.K_e:
             MyGame.SetAllMatched()

      # --- Game logic should go here
      if (eventHappened and (not squareClicked == -1)):
        MyGame.ProcessEvent(squareClicked)

      # --- Drawing code should go here
      if ((firstPass or eventHappened or MyGame.redraw) and
             not MyGame.AllMatched()):
        DebugMessage ("Redrawing Screen")
        MyGame.DrawGameScreen()
        MyGame.redraw = False
        firstPass = False
      elif (MyGame.AllMatched() and not MyGame.gameOverScreen):
        MyGame.gameOverScreen = True

      if (MyGame.gameOverScreen):
        MyGame.DrawGameOverScreen()

      pygame.display.flip()
      # --- Limit to 60 frames per second
      clock.tick(60)

# -------- Main Program Loop -----------

def main():

  result = TestProcedure()
  if not result == 1:
    print ("Failed Initial Tests")
    sys.exit(1)
  pygame.init()
  while 1:
    print("Hit q key to make the game end prematurely")
    print("Hit e key to make the game show end screen prematurely")
    MyGame = GameStateClass("images")
    if (MyGame.Valid):
      HandlerLoop(pygame,MyGame)

  del MyGame

main()


#!/usr/bin/env python3
#

from pathlib import Path

before = "Copyright (C) 2012-2019 The VPaint Developers"

after = "Copyright (C) 2012-2019 The VPaint Developers"

if __name__ == "__main__":

    path = Path(".")
    for file in path.glob("src/**/*"):
        if file.suffix in [".h", ".cpp", ".pro"]:
            s1 = file.read_text()
            s2 = s1.replace(before, after)
            if s1 != s2:
                file.write_text(s2)
                print(file)


"""n=int(input("enter size of array:"))
for i in range(0,n):
    x=int(input("enter the elements:"))
s=int(input("enter search element:"))
for i in range(0,n):
    c=0
    if s==i:
        c=1
        break
if c==1:
    print("search element found:",s)
else:
    print("search element not found")"""

def linear_search(arr,target):
    for i in range(len(arr)):
        if arr[i]==target:
            return i
    return -1
numbers=[5,9,2,7,1,8,3]
target=1
index=linear_search(numbers,target)
print(f"target found at index:{index}")


# 1 Вычислить число c заданной точностью d
# Пример:
# - при d = 0.001, π = 3.141    $10^-1 ≤ d ≤10^-10

# Судя по примеру, требуется вывести число π c заданным количеством
# цифр после точки

from math import pi

# Вариант решения 1
# Данный вариант не соответствует выводу в примере, но математически верен
def get_pi_by_round(prec:int):
    return round(pi, prec)

# Вариант решения 2
# Данный вариант соответствует выводу (округление вниз/отсечение лишних знаков)
def get_pi_by_string(prec:int):
    return float(str(pi)[:2+prec])

# Вариант решения 3
# через модуль decimal
def get_pi_by_decimal(prec:int, round_type:str):
    import decimal
    d = '1.' + '0'*prec
    dec = decimal.Decimal
    if round_type == 'normal':
        return float(dec(str(pi)).quantize(dec(d), decimal.ROUND_HALF_UP))
    if round_type == 'down':
        return float(dec(str(pi)).quantize(dec(d), decimal.ROUND_DOWN))

def main():
    from os import system

    system("cls")
    print('Программа выводит число Пи c указанной точностью')
# Принимаем ввод от пользователя
    s = input('Введите точность (0.1 - 0.0000000001): ')
    if 0.0000000001 <= float(s) <= 0.1:
        d = len(s.split('.')[1])
    else:
        print ("Число вне допуска!")
        return
    num = get_pi_by_round(d)
    print(num, ' Округление при помощи метода round()')
    num = get_pi_by_string(d)
    print(num, ' "Округление" в меньшую сторону через строки ')
    print('Округление с помощью модуля decimal:')
    num = (get_pi_by_decimal(d, 'normal'))
    print(num, ' - округление в большую сторону, если справа число 5 и более (арифметическое)')
    num = (get_pi_by_decimal(d, 'down'))
    print(num, "Округление в меньшую сторону")

if __name__ == "__main__":
    main()


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

# In[2]:

import pandas as pd
import numpy as np

# In[3]:

police_df=pd.read_csv('police.csv')
police_df

# In[4]:

df=police_df.copy()

# In[5]:

df

# In[6]:

df.info()

# In[8]:

df.isnull().sum()

# In[10]:

df.drop(columns='country_name',inplace=True)

# In[11]:

df

# # For speeding,where men and women stops more often

# In[12]:

df.head()

# In[28]:

df[df.violation=='Speeding'].driver_gender.value_counts()

# # Does gender affect who get searched during a stop?

# In[29]:

df.groupby('driver_gender').search_conducted.sum()

# # mean stop duration

# In[31]:

df.stop_duration.value_counts()

# In[33]:

df['stop_duration']=df.stop_duration.map({'0-15 Min':7.5,'16-30 Min':24,'30+ Min':45})

# In[34]:

df

# In[35]:

df['stop_duration'].mean()

# # age distribution for each violations

# In[38]:

df.groupby('violation').driver_age.describe()

# In[ ]:


import math
#importtation of the math library giving us access to the sin function

angle = 60

sinus = math.sin(angle/180*math.pi)
#conversion from degrees to radian and then using the sin function to return the sinus

print(f"The sinus of {angle} is {sinus}")
#using the f print to include the different variable directly in the function


import tempfile
import shutil
import numpy as np

import Bio
from Bio import pairwise2, AlignIO
#from Bio.SubsMat import MatrixInfo as matlist

from comutils.tool_wrapper import *
from comutils import files_management as fm
from comutils.global_variables import ALPHABET
from comutils.pseudocounts import *
q = len(ALPHABET)

import ccmpred.weighting
import ccmpred.io
import ccmpred.counts

def code(c):
    """ gives number code in [0,21] for letter @c"""
    return ALPHABET.find(c.upper())

def code_whole_seq(sequence):
    return [code(c) for c in sequence]

def get_reordered_v(v, alphabet_to, alphabet_from=ALPHABET):
    """ reorders all vi for a given alphabet """
    q = v.shape[1]
    idx = [alphabet_from[:q].find(a) for a in alphabet_to[:q]]
    return v[:,idx]

def get_reordered_wij(wij, alphabet_to, alphabet_from=ALPHABET):
    """ reorders all wij for a given alphabet """
    q = wij.shape[0]
    idx = [alphabet_from[:q].find(a) for a in alphabet_to[:q]]
    new_wij = np.zeros_like(wij)
    for a in range(len(alphabet_to)):
        for b in range(len(alphabet_to)):
            new_wij[a][b] = wij[idx[a]][idx[b]]
    return new_wij

def get_reordered_w(w, alphabet_to, alphabet_from=ALPHABET):
    new_w = np.zeros_like(w)
    L = w.shape[0]
    for i in range(L-1):
        for j in range(i+1,L):
            new_w[i,j] = get_reordered_wij(w[i,j], alphabet_to=alphabet_to, alphabet_from=alphabet_from)
            new_w[j,i] = np.transpose(new_w[i,j])
    return new_w

def euclidean_norm(vector):
    return np.linalg.norm(vector)

def scalar_product(v1, v2):
    return np.dot(v1.flatten(), v2.flatten())

def compute_w_norms(w):
    L = w.shape[0]
    w_norms = np.zeros((L,L))
    for i in range(0, L-1):
        for j in range(i+1, L):
            w_norms[i][j] = euclidean_norm(w[i,j])
            w_norms[j][i] = w_norms[i][j]
    return w_norms

def sign_ind(x):
    """ returns 1 if @x>=0, -1 otherwise """
    return 2*(x>=0)-1

def seq_identity(seq1, seq2):
    """ sequence identity between unaligned sequences @seq1 et @seq2 """
    alignment = pairwise2.align.globalxx(seq1, seq2, one_alignment_only=True)[0]
    nb_matches = alignment[2]
    length_alignment = len(alignment[0])
    #score = (nb_matches/max(len(seq1),len(seq2)))
    score = nb_matches/length_alignment
    return score

def is_gap_column(i, msa):
    """ returns True iff the column @i of @msa contains only gaps """
    gap = True
    s=0
    while ( (gap==True) and (s<len(msa)) ):
        seq = msa[s].seq
        if (seq[i]!="-"):
            gap = False
        s+=1
    return gap

def get_trimmed_sequence_for_msa(msa_file, seq):
    """ aligns sequence @seq to MSA in @msa_file and removes all inserted positions in @seq so that it can be aligned to the MSA """
    temp_folder = pathlib.Path(tempfile.mkdtemp())
    tempseq_file = temp_folder/("seq.fasta")
    seq_name = fm.create_seq_fasta(seq, tempseq_file)
    combined_aln_file = temp_folder/"aln.fasta"
    call_muscle_profile(msa_file,tempseq_file,combined_aln_file)
    comb_msa = AlignIO.read(str(combined_aln_file), "fasta")
    new = comb_msa[len(comb_msa)-1].seq
    trimmed=""
    n = len(comb_msa[0].seq)
    for i in range(n):
        if not is_gap_column(i, comb_msa[0:len(comb_msa)-1]): # si pas insertion
            trimmed+=new[i]
    shutil.rmtree(temp_folder)
    return trimmed

def get_pos_first_seq_to_second_seq(first_seq, second_seq):
    """ returns dictionary d[pos_in_first_seq] = pos_in_second_seq """
    gap_char='.'
    alns = pairwise2.align.globalxx(first_seq, second_seq, gap_char=gap_char)
    top_aln = alns[0]
    aln_first, aln_second, score, begin, end = top_aln
    first_pos = 0
    second_pos = 0
    pos_dict_first_seq_to_second_seq = [None for k in range(len(first_seq))]
    for i in range(len(aln_first)):
        if (aln_first[i]==gap_char) and (aln_second[i]!=gap_char):
            second_pos+=1
        elif (aln_first[i]!=gap_char) and (aln_second[i]==gap_char):
            pos_dict_first_seq_to_second_seq[first_pos] = None
            first_pos+=1
        elif (aln_first[i]!=gap_char) and (aln_second[i]!=gap_char):
            pos_dict_first_seq_to_second_seq[first_pos] = second_pos
            first_pos+=1
            second_pos+=1
    return pos_dict_first_seq_to_second_seq

def f_to_v_star(f):
    """ input: single frequencies (Lxq) array
        output: fields of an independent-site Potts model"""
    lf = np.log(f)
    v_star = lf - np.tile(np.mean(lf, axis=1), (f.shape[1],1)).T
    return v_star

def compute_v_star(msa_file, wt_cutoff, pc_single_count, q=21):
    msa_ccmpred = ccmpred.io.read_msa(msa_file, 'fasta')
    weights = ccmpred.weighting.weights_simple(msa_ccmpred, cutoff=wt_cutoff)
    single_counts, double_counts = ccmpred.counts.both_counts(msa_ccmpred, weights)
    neff = np.sum(weights)
    single_freqs = single_counts/neff
    tau = pc_single_count/(neff+pc_single_count)
    uniform_pc = np.zeros_like(single_freqs)
    uniform_pc.fill(1. / single_freqs.shape[1])
    single_freqs = (1-tau)*single_freqs+tau*uniform_pc
    lsingle_freqs = np.log(single_freqs)
    v_star = lsingle_freqs - np.mean(lsingle_freqs[:,:q], axis=1)[:, np.newaxis]
    return v_star[:,:q]


import logging
from .state import State
from .parsers import UseParser
from functools import reduce

log = logging.getLogger("findingslist")

class FindingsList:
    class NotAUseExpression(Exception):
        pass

    def __init__(self, persist_path, resume_path, initial, fresh=False):
        self.current_findings = initial
        self.persist = State(persist_path, fresh)
        self.resume = State(resume_path, fresh)
        self.persist.load()
        self.previous_findings = self.persist.data
        self.ignore_existing = False

    def get(self, key, attributes=None, unique=True):
        """Return entries from the findings list.

        attributes -- If given, return each entry only with the given attributes.
        unique -- If True, return only unique elements.

        Note that even if only unique entries are added to the list,
        using the attributes argument may lead to duplicate entries,
        since it removes keys from each element that can be used to
        distinguish the elements.

        """
        if not key in self.current_findings:
            raise Exception(
                f"Unknown findings key: {key}; existing keys are: {', '.join(self.current_findings.keys())}"
            )
        out = self.current_findings[key]
        if attributes is not None:
            out = [{k: el[k] for k in attributes} for el in out]
        if unique:
            uni_out = []
            for x in out:
                if x not in uni_out:
                    uni_out.append(x)
            out = uni_out
        return out

    def set(self, key, value):
        if not self.ignore_existing and key in self.current_findings:
            raise Exception(
                f"Storing findings in key {key} would overwrite existing findings."
            )
        self.current_findings[key] = value

    def update(self, data):
        for key, value in data.items():
            self.set(key, value)

    def get_previous(self, key, default):
        return self.previous_findings.get(key, default)

    def save_persist(self):
        self.persist.data = self.current_findings
        self.persist.save()

    def save_resume(self, step):
        self.resume.data[step] = self.current_findings
        self.resume.data["_step"] = step
        self.resume.save()

    def load_resume(self, step=None):
        self.resume.load()
        if step is None:
            step = self.resume.data["_step"]
        log.debug(f"Loading findings list resume data, step={step}")
        self.current_findings = self.resume.data[step]
        self.ignore_existing = True
        return step

    def get_common_and_missing_items(self, key1, key2):
        """Return items that are in findings with key1 and with key2; and
        items that are in findings with key1 but not in those with
        key2; and a boolean indicating if the lists are the same.

        """
        common_attrs = self.find_common_attributes(key1, key2)
        items1 = self.get(key1, common_attrs)
        items2 = self.get(key2, common_attrs)
        common_items = [item for item in items1 if item in items2]
        missing_items = [item for item in items1 if item not in items2]
        equals = len(common_items) == len(items1) == len(items2)

        return common_items, missing_items, equals

    def get_added_items(self, key):
        """Return items that appear in the current findings list with the
        given key, but not in the previous one.

        """
        current = self.get(key)
        previous = self.get_previous(key, [])
        added = [item for item in current if item not in previous]
        return added

    def find_common_attributes(self, *keys):
        """Find one or more attributes that each entry in each of the
        lists has in common. This function compares only the first
        member of each list and assumes that all further elements have
        the same attributes. This is enforced in other places in the
        code.

        """

        common_attrs = None
        for k in keys:
            all_items = self.get(k)
            if len(all_items) == 0:
                return None
            attrs = set(all_items[0].keys())
            if common_attrs is None:
                common_attrs = attrs
            else:
                common_attrs &= attrs

        if len(common_attrs) == 0:
            raise Exception(
                f"Unable to compare the findings lists {keys} (no common attributes)."
            )
        return common_attrs


from subprocess import Popen, PIPE

relpath = '../../..'
exec(open(relpath + '/etc/make_site.py').read())
from pike import *

@target
def any(match, deps):
    '''.*'''
    for db in databases:
        x = Popen(mpro + ['-pf', relpath + '/db/progress/store/%s.pf' % db,
                          '-b', '-p', 'protabledesc.p',
                          '-param', match], stdout=PIPE)
        output, _err = x.communicate()
        if x.poll() != 0:
            print('Error : ' + output)
        if output:
            print(output.replace('Table description for ',
                                 'Table description for ' + db + '.'))
            break
    else:
        raise PikeException('Table %s not found in %d databases' % \
                            (match, len(databases)))


#!/usr/bin/env python

import simpy
from hwsim_utils import *
from scapy.all import *

env = simpy.Environment()
period = 1
pipe = simpy.Store(env)
bus_width = 32

pkt = Ether()/IP()/TCP()/'hello there pretty world!!!'
tuser = Tuser(len(pkt), 0b00000001, 0b00000100)
pkt_list = [(pkt, tuser)]*3

master = AXI_S_master(env, period, pipe, bus_width, pkt_list)
slave = AXI_S_slave(env, period, pipe, bus_width)

env.run(until=20)


#!/usr/bin/python
#
# renderpage.py
# Build the podcasting home page, including:
#
#   * State of icecast server (if we're live)
#   * All available podcasts rendered from
#     showindex_multi.json and mustache templates.
#
# J. Adams < jna@retina.net >
# 8/5/2014
#
# TODO: Don't touch the destination file if our state hasn't changed.
#       Better error handling - right now we have none.

from pytz import timezone
from datetime import tzinfo, timedelta, datetime
import json
import pystache
import requests
import rfc822
import time

# In the US, DST starts at 2am (standard time) on the first Sunday in April.
# and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
# which is the first Sunday on or after Oct 25.
DSTSTART = datetime(1, 4, 1, 2)
DSTEND = datetime(1, 10, 25, 1)
ZERO = timedelta(0)
HOUR = timedelta(hours=1)

def first_sunday_on_or_after(dt):
  days_to_go = 6 - dt.weekday()
  if days_to_go:
    dt += timedelta(days_to_go)
  return dt

class UTC(tzinfo):
  """UTC"""
  def utcoffset(self, dt):
    return ZERO
  def tzname(self, dt):
    return "UTC"
  def dst(self, dt):
    return ZERO

class USTimeZone(tzinfo):
  def __init__(self, hours, reprname, stdname, dstname):
    self.stdoffset = timedelta(hours=hours)
    self.reprname = reprname
    self.stdname = stdname
    self.dstname = dstname

  def __repr__(self):
    return self.reprname

  def tzname(self, dt):
    if self.dst(dt):
      return self.dstname
    else:
      return self.stdname

  def utcoffset(self, dt):
    return self.stdoffset + self.dst(dt)

  def dst(self, dt):
    if dt is None or dt.tzinfo is None:
      # An exception may be sensible here, in one or both cases.
      # It depends on how you want to treat them.  The default
      # fromutc() implementation (called by the default astimezone()
      # implementation) passes a datetime with dt.tzinfo is self.
      return ZERO
    assert dt.tzinfo is self

    # Find first Sunday in April & the last in October.
    start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
    end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))

    # Can't compare naive to aware objects, so strip the timezone from
    # dt first.
    if start <= dt.replace(tzinfo=None) < end:
      return HOUR
    else:
      return ZERO

class Index(object):
  """ methods for the index.mustache page """
  def is_live(self):
      """ right now we only broadcast under /wg, see if it is up """
      r = requests.get('http://podcast.retina.net:8000/')
      if r.status_code == 200:
          if r.text.find("Mount Point /wg") > -1:
              return True
      else:
          return False

  def shows(self):
    fp = open("showindex.json","r")
    shows = json.load(fp)

    # Convert the GMT times into something more friendly for HTML reading
    # incoming times are always in RSS format, we want them in US/Pacific
    # (Also, what the fuck year is this. Why am I writing this code.)
    utc = UTC()
    Pacific = USTimeZone(-8, "Pacific",  "PST", "PDT")
    for show in shows['shows']:
      n = 0
      for e in show['show']['episodes']:
        parsed_date = e['date']
        d = datetime.strptime( e['date'], "%a, %d %b %Y %H:%M:%S GMT" )
        aware = d.replace(tzinfo=utc)
        show['show']['episodes'][n]['date'] = aware.astimezone(Pacific).strftime("%A, %B %e, %Y")
        n = n + 1

    return shows['shows']

class Rss(object):
  """ methods for rendering the rss index """
  def shows(self):
    fp = open("showindex.json","r")
    shows = json.load(fp)

    # this RSS feed is only the first show (wg) episode list.
    return shows['shows'][0]['show']['episodes']

# set up the renderer
renderer = pystache.Renderer(search_dirs="/retina/podcast/html/templates")

# render the index page
page = Index()
parsed = renderer.render(page)
fout = open("index.html","w")
print >>fout, parsed
fout.close()

# render the RSS feed
page = Rss()
parsed = renderer.render(page)
fout = open("wg/wicked_grounds.rss","w")
print >>fout, parsed
fout.close()


# To find min cost to reach the last cell in  a given 2d Array

def min_cost(arr,row,col):
	if row==-1 or col==-1:
		return float('inf')
	if row==0 and col==0:
		return arr[0][0]
	else:
		option1=min_cost(arr,row-1,col)
		option2=min_cost(arr,row,col-1)
		return arr[row][col]+min(option1,option2)
arr=[[4,7,8,6,4],
	[6,7,3,9,2],
	[3,8,1,2,4],
	[7,1,7,3,7],
	[2,9,8,9,3]]

print(min_cost(arr,4,4))