from time import sleep

try:
    import pyglet
    from pyglet.gl import *
except ImportError:
    print("GET PYGLET!!!")
    input()
from pyglet.window import key
from manual_player import *

GRID_SIZE = np.array([192, 128])  # Number of grids per screen
POS_MATRIX = np.zeros(GRID_SIZE, dtype=np.int8)  # Create 2D collision-position matrix
SCREEN_SIZE = np.array([1920, 1024])  # Designate a screen-size
GRID_QUANTIZATION = SCREEN_SIZE / GRID_SIZE  # Size of each square
KEY_DICT = {}
NUM_PLAYERS = 3
current_players = NUM_PLAYERS

window = pyglet.window.Window(SCREEN_SIZE[0], SCREEN_SIZE[1], resizable=True)
players_list = []

reset_text = pyglet.text.Label('Resetting game state', \
                               font_name='Times New Roman', \
                               font_size=36, \
                               color=(255, 255, 255, 255), \
                               x=window.width // 2, \
                               y=window.height // 2, \
                               anchor_x='center', anchor_y='center')

def bind_key(obj, symbol_list):
    for symbol in symbol_list:
        KEY_DICT[symbol] = obj

players_list.append(Player(96, 96, 1, ('c3B', (255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0)), \
                           ('c3B', (120, 0, 0, 120, 0, 0, 120, 0, 0, 120, 0, 0)), \
                           GRID_QUANTIZATION, GRID_SIZE, "Player 1"))

players_list.append(Player(36, 33, 2, ('c3B', (0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0)), \
                           ('c3B', (0, 120, 0, 0, 120, 0, 0, 120, 0, 0, 120, 0)), \
                           GRID_QUANTIZATION, GRID_SIZE, "Player 2"))

players_list.append(Player(156, 33, 3, ('c3B', (0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255)), \
                           ('c3B', (0, 0, 120, 0, 0, 120, 0, 0, 120, 0, 0, 120)), \
                           GRID_QUANTIZATION, GRID_SIZE, "Player 3"))

for player in players_list:
    player.set_initial_direction(GRID_SIZE)

players_list[1].set_controls((key.W, key.S, key.A, key.D))
bind_key(players_list[1], (key.W, key.S, key.A, key.D))
players_list[2].set_controls((key.UP, key.DOWN, key.LEFT, key.RIGHT))
bind_key(players_list[2], (key.UP, key.DOWN, key.LEFT, key.RIGHT))
players_list[0].set_controls((key.I, key.K, key.J, key.L))
bind_key(players_list[0], (key.I, key.K, key.J, key.L))

code_batch = pyglet.graphics.Batch()

@window.event
def on_key_press(symbol, modifier):
    if symbol in KEY_DICT:
        KEY_DICT[symbol].change_dir(symbol)

@window.event
def on_draw():
    window.clear()
    code_batch.draw()
    batch = pyglet.graphics.Batch()
    for player_inst in players_list:
        if player_inst.active:
            batch.add(4, gl.GL_QUADS, None, ('v2f', player.draw_pos), player.color)
    batch.draw()

def update(dt=0):
    global POS_MATRIX, code_batch, current_players, NUM_PLAYERS
    try:
        for player in players_list:
            if POS_MATRIX[player.x][player.y] == 0:
                POS_MATRIX[player.x][player.y] = player.code
                current_draw_position = [player.x * GRID_QUANTIZATION[0], player.y * GRID_QUANTIZATION[1], \
                                         player.x * GRID_QUANTIZATION[0], (player.y + 1) * GRID_QUANTIZATION[1], \
                                         (player.x + 1) * GRID_QUANTIZATION[0], (player.y + 1) * GRID_QUANTIZATION[1], \
                                         (player.x + 1) * GRID_QUANTIZATION[0], player.y * GRID_QUANTIZATION[1]]
                if player.active:
                    code_batch.add(4, gl.GL_QUADS, None, ('v2f', current_draw_position), player.trail_color)
            else:
                player.death()
            player.update(GRID_QUANTIZATION, GRID_SIZE)
    except PlayerDeath as death_tag:
        if current_players > 2:
            current_players -= 1
            player.new_set(GRID_QUANTIZATION, GRID_SIZE)
            player.set_initial_direction(GRID_SIZE)
            death_tag.player_name.dx = 0
            death_tag.player_name.dy = 0
            death_tag.player_name.active = False
        else:
            current_players = NUM_PLAYERS
            game_state = pyglet.text.Label('%s lost.' % death_tag.player_name.name, \
                                           font_name='Times New Roman', \
                                           font_size=36, \
                                           color=(0, 255, 255, 0), \
                                           x=SCREEN_SIZE[0] / 2, \
                                           y=SCREEN_SIZE[1] / 2, \
                                           anchor_x='center', anchor_y='center')
            print(death_tag.player_name.name)
            game_state.draw()
            sleep(0.5)
            reset_text.draw()
            for player in players_list:
                player.new_set(GRID_QUANTIZATION, GRID_SIZE)
                player.set_initial_direction(GRID_SIZE)
                player.active = True
            POS_MATRIX = np.zeros(GRID_SIZE, dtype=np.int8)
            code_batch = pyglet.graphics.Batch()
            sleep(0.5)

window.schedule = pyglet.clock.schedule_interval(update, 1 / 45.0)  # update at 60Hz
pyglet.app.run()


import time
import os
from selenium import webdriver
import math
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--remote-debugging-port=9222")

def calc(x):
    return str(math.log(abs(12*math.sin(int(x)))))

try:
    browser = webdriver.Chrome(chrome_options=chrome_options)
    browser.get("http://suninjuly.github.io/alert_accept.html")
    button = browser.find_element_by_xpath('//button[contains(text(), "I want to go on a magical journey!")]')
    button.click()
    confirm = browser.switch_to.alert
    confirm.accept()
    num = browser.find_element_by_xpath('//span[@id="input_value"]').text
    calc_result = calc(num)
    element = browser.find_element_by_xpath('//input[@id="answer"]')
    element.send_keys(calc_result)

    button = browser.find_element_by_xpath('//button[contains(text(), "Submit")]')
    button.click()

finally:
    print(browser.switch_to.alert.text)
    browser.quit()


import sys
import collections
from thirdparty import six
from lib.core.settings import UNICODE_ENCODING, STDIN_PIPE_DASH
from lib.core.data import kb,conf,paths
from lib.core.exception import SqlmapSystemException
from lib.core.bigarray import BigArray
from termcolor import colored
import os

def dataToStdout(data, forceOutput=False, bold=False, content_type=None):
    """
    Writes text to the stdout (console) stream
    """

    message = data
    sys.stdout.write(colored(message, "cyan"))
    try:
        sys.stdout.flush()
    except IOError:
        pass

def setPaths(rootPath):
    """
    Sets absolute paths for project directories and files
    """

    paths.ROOT_PATH = rootPath
    # paths
    paths.LIB_PATH = os.path.join(paths.ROOT_PATH, "lib")
    paths.PLUGIN_PATH = os.path.join(paths.ROOT_PATH, "plugin")
    paths.SETTINGS_PATH = os.path.join(paths.ROOT_PATH, "lib", "core", "settings.py")
    paths.DICS_PATH = os.path.join(paths.ROOT_PATH, "dics")
    # print(paths.values())
    for path in paths.values():
        if any(path.endswith(_) for _ in (".txt", ".xml", ".zip")):
            checkFile(path)

def checkFile(filename, raiseOnError=True):
    """
    Checks for file existence and readability

    >>> checkFile(__file__)
    True
    """

    valid = True

    if filename:
        filename = filename.strip('"\'')

    if filename == STDIN_PIPE_DASH:
        return checkPipedInput()
    else:
        try:
            if filename is None or not os.path.isfile(filename):
                valid = False
        except:
            valid = False

        if valid:
            try:
                with open(filename, "rb"):
                    pass
            except:
                valid = False

    if not valid and raiseOnError:
        raise SqlmapSystemException("unable to read file '%s'" % filename)

    return valid

def weAreFrozen():
    """
    Returns whether we are frozen via py2exe.
    This will affect how we find out where we are located.

    # Reference: http://www.py2exe.org/index.cgi/WhereAmI
    """

    return hasattr(sys, "frozen")

def getUnicode(value, encoding=None, noneToNull=False):
    """
    Return the unicode representation of the supplied value:

    >>> getUnicode('test') == u'test'
    True
    >>> getUnicode(1) == u'1'
    True
    """

    if noneToNull and value is None:
        return "NULL"

    if isinstance(value, six.text_type):
        return value
    elif isinstance(value, six.binary_type):
        # Heuristics (if encoding not explicitly specified)
        candidates = filterNone((encoding, kb.get("pageEncoding") if kb.get("originalPage") else None, conf.get("encoding"), UNICODE_ENCODING, sys.getfilesystemencoding()))
        if all(_ in value for _ in (b'<', b'>')):
            pass
        elif any(_ in value for _ in (b":\\", b'/', b'.')) and b'\n' not in value:
            candidates = filterNone((encoding, sys.getfilesystemencoding(), kb.get("pageEncoding") if kb.get("originalPage") else None, UNICODE_ENCODING, conf.get("encoding")))
        elif conf.get("encoding") and b'\n' not in value:
            candidates = filterNone((encoding, conf.get("encoding"), kb.get("pageEncoding") if kb.get("originalPage") else None, sys.getfilesystemencoding(), UNICODE_ENCODING))

        for candidate in candidates:
            try:
                return six.text_type(value, candidate)
            except UnicodeDecodeError:
                pass

        try:
            return six.text_type(value, encoding or (kb.get("pageEncoding") if kb.get("originalPage") else None) or UNICODE_ENCODING)
        except UnicodeDecodeError:
            return six.text_type(value, UNICODE_ENCODING, errors="reversible")
    elif isListLike(value):
        value = list(getUnicode(_, encoding, noneToNull) for _ in value)
        return value
    else:
        try:
            return six.text_type(value)
        except UnicodeDecodeError:
            return six.text_type(str(value), errors="ignore")  # encoding ignored for non-basestring instances

def filterNone(values):
    """
    Emulates filterNone([...]) functionality

    >>> filterNone([1, 2, "", None, 3])
    [1, 2, 3]
    """

    retVal = values

    if isinstance(values, collections.Iterable):
        retVal = [_ for _ in values if _]

    return retVal

def isListLike(value):
    """
    Returns True if the given value is a list-like instance

    >>> isListLike([1, 2, 3])
    True
    >>> isListLike('2')
    False
    """

    return isinstance(value, (list, tuple, set, BigArray))

def checkPipedInput():
    """
    Checks whether input to program has been provided via standard input (e.g. cat /tmp/req.txt | python sqlmap.py -r -)
    # Reference: https://stackoverflow.com/a/33873570
    """

    return not os.isatty(sys.stdin.fileno())


import numpy as np
import cv2
cam = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cv2.namedWindow("test")
img_counter = 0
while True:
    ret, frame = cam.read()
    cv2.imshow("test", frame)
    if not ret:
        break
    k = cv2.waitKey(1)
    if k%256 == 27:
        print("Escape hit, closing...")
        break
    elif k%256 == 32:
        img_name = "opencv_frame_0.png"
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
cam.release()
cv2.destroyAllWindows()


import json
import argparse

args = argparse.ArgumentParser()
args.add_argument("questions",type=str)
args.add_argument("exp",type=str)
args.add_argument("output",type=str)
args = args.parse_args()

ques = open(args.questions)
exp = open(args.exp)
out = open(args.output,'w')

exp_map = dict()

for lines in exp:
    line = json.loads(lines)
    exp_map[line['id']] = line

ques_map = dict()

for lines in ques:
    line = json.loads(lines)
    line['question']['cose'] = exp_map[line['id']]["explanation"]["open-ended"]
    ques_map[line['id']] = line

for key in ques_map:
    st = json.dumps(ques_map[key])
    out.write(st+'\n')

out.close()


# -*- coding: utf-8 -*-
# Copyright 2018 Mobicage NV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @@license_version:1.3@@

import datetime

from plugins.limburg_net_trash.plugin_utils import get_epoch_from_datetime
from mcfw.properties import long_property, unicode_property, typed_property
from mcfw.serialization import register, deserializer, ds_unicode, serializer, s_unicode, s_long, get_list_serializer, \
    ds_long, get_list_deserializer, List

class StreetTO(object):
    number = long_property('1')
    name = unicode_property('2')

    @classmethod
    def fromObj(cls, obj):
        to = cls()
        to.number = obj["nr"]
        to.name = obj["s"]
        return to

@serializer
def _s_street_to_stream(stream, to):
    s_long(stream, 1)
    s_long(stream, to.number)
    s_unicode(stream, to.name)

@deserializer
def _ds_street_to_stream(stream):
    to = StreetTO()
    version = ds_long(stream)
    to.number = ds_long(stream)
    to.name = ds_unicode(stream)
    return to

register(StreetTO, _s_street_to_stream, _ds_street_to_stream)

@deserializer
def _ds_street_to(stream, version):
    to = StreetTO()
    to.number = ds_long(stream)
    to.name = ds_unicode(stream)
    return to

@serializer
def _s_street_to(stream, to):
    s_long(stream, to.number)
    s_unicode(stream, to.name)

def _s_street_list(stream, l):
    s_long(stream, 1)
    f = get_list_serializer(_s_street_to)
    f(stream, l)

def _ds_street_list(stream):
    l = list()
    version = ds_long(stream)
    f = get_list_deserializer(_ds_street_to, True)
    for to in f(stream, version):
        l.append(to)
    return l

register(List(StreetTO), _s_street_list, _ds_street_list)

class HouseTO(object):
    number = long_property('1')
    bus = unicode_property('2')

    @classmethod
    def fromObj(cls, obj):
        to = cls()
        to.number = obj["h"]
        to.bus = obj["t"]
        return to

@deserializer
def _ds_house_to(stream, version):
    to = HouseTO()
    to.number = ds_long(stream)
    to.bus = ds_unicode(stream)
    return to

@serializer
def _s_house_to(stream, to):
    s_long(stream, to.number)
    s_unicode(stream, to.bus)

def _s_house_list(stream, l):
    s_long(stream, 1)
    f = get_list_serializer(_s_house_to)
    f(stream, l)

def _ds_house_list(stream):
    l = list()
    version = ds_long(stream)
    f = get_list_deserializer(_ds_house_to, True)
    for to in f(stream, version):
        l.append(to)
    return l

register(List(HouseTO), _s_house_list, _ds_house_list)

CURRENT_ACTIVITY_VERSION = 1
class ActivityTO(object):
    number = long_property('1')
    name = unicode_property('2')

    @classmethod
    def fromObj(cls, obj):
        to = cls()
        to.number = obj["nr"]
        to.name = obj["s"]
        if to.name == u"grofvuil":
            to.name = u"grofvuil (enkel op aanvraag)"

        return to

@deserializer
def _ds_activity_to(stream, version):
    to = ActivityTO()
    to.number = ds_long(stream)
    to.name = ds_unicode(stream)
    return to

@serializer
def _s_activity_to(stream, to):
    s_long(stream, to.number)
    s_unicode(stream, to.name)

def _s_activity_list(stream, l):
    s_long(stream, CURRENT_ACTIVITY_VERSION)
    f = get_list_serializer(_s_activity_to)
    f(stream, l)

def _ds_activity_list(stream):
    l = list()
    version = ds_long(stream)
    f = get_list_deserializer(_ds_activity_to, True)
    for to in f(stream, version):
        l.append(to)
    return l

register(List(ActivityTO), _s_activity_list, _ds_activity_list)

class CollectionTO(object):
    epoch = long_property('1')
    year = long_property('2')
    month = long_property('3')
    day = long_property('4')
    activity = typed_property('5', ActivityTO, False)

    @classmethod
    def fromObj(cls, obj, activity):
        to = cls()
        d = obj["d"].split("/")
        to.year = long(d[2])
        to.month = long(d[1])
        to.day = long(d[0])
        d = datetime.date(to.year, to.month, to.day)
        to.epoch = get_epoch_from_datetime(d)
        to.activity = activity
        return to

@deserializer
def _ds_collection_to(stream, version):
    to = CollectionTO()
    to.epoch = ds_long(stream)
    to.year = ds_long(stream)
    to.month = ds_long(stream)
    to.day = ds_long(stream)
    _version = ds_long(stream)
    to.activity = _ds_activity_to(stream, _version)
    return to

@serializer
def _s_collection_to(stream, to):
    s_long(stream, to.epoch)
    s_long(stream, to.year)
    s_long(stream, to.month)
    s_long(stream, to.day)
    s_long(stream, CURRENT_ACTIVITY_VERSION)
    _s_activity_to(stream, to.activity)

def _s_collection_list(stream, l):
    s_long(stream, 1)
    f = get_list_serializer(_s_collection_to)
    f(stream, l)

def _ds_collection_list(stream):
    l = list()
    version = ds_long(stream)
    f = get_list_deserializer(_ds_collection_to, True)
    for to in f(stream, version):
        l.append(to)
    return l

register(List(CollectionTO), _s_collection_list, _ds_collection_list)


import numpy as np
from scipy import stats
class neuroCorrelation:
    """
    This class is for the correlation between trait similarity matrix and neural correlation matrix
    """
    # include the flattened trait similarity matrix into the class
    def __init__(self, traitMatrix):
        """
        :param traitMatrix: this is a vector of the lower triangle of trait similarity matrix of a given valence.
        """
        self.traitMatrix = traitMatrix

    def correlate(self, brainData):
        """
        generate a function which generate the coefficient of a person correlation between the neural matrix and the trait similarity matrix
        :param brainData: this is a fmri_dataset of a given valence after z-scoring within runs
        :return: the correlation coefficient between neural correlation matrix and trait similarity matrix of a given valence
        """
        # generate a similarity matrix across each pair of samples
        neuro_sim = np.corrcoef(brainData.samples)

        # take lower triangle of the neuro similarity matrix and flatten
        neuro_sim_flat = np.tril(neuro_sim, k=-1).flatten()

        # generage a mask matrix of 1
        ones_matrix = np.ones(np.shape(neuro_sim))

        # take lower triangle of the ones matrix and flatten to generate a mask
        onesflat = np.tril(ones_matrix, k=-1).flatten()

        # calculate the correlation between neuro similarity matrix and trait similarity matrix, and apply the mask to take the lower triangle
        corr_output = stats.pearsonr(neuro_sim_flat[onesflat != 0], self.traitMatrix[onesflat != 0])

        # only return the correlation coefficient, but not the p-value
        return corr_output[0]


from django.shortcuts import render
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
from dashboard.models import Area
from dashboard.models import Data
from dashboard.models import RawData
from manage_devices.models import Node
from django.http import JsonResponse
from django.http import HttpResponse
from datetime import datetime
from django.db.models.functions import TruncMonth
from django.db.models import Avg
from django.template.loader import get_template
from django.template.loader import render_to_string
from api.serializers import DataSerialization, NodeSerialization, RawDataSerialization, AreaSerialization

class ReportIndex(LoginRequiredMixin, View):
    def get(self, request):
        areas = Area.objects.all()
        return render(request, template_name='report/index.html', context={
            'areas': areas,
        })

class ReportShowOnView(LoginRequiredMixin, View):
    def get(self, request):
        area = request.GET.get('area')
        date_start = datetime.strptime(request.GET.get('month_start'), '%a %b %d %Y %H:%M:%S %Z%z')
        date_end = datetime.strptime(request.GET.get('month_end'), '%a %b %d %Y %H:%M:%S %Z%z')

        if area == 'all':
            areas = Area.objects.all()
            areas_serialized = AreaSerialization(data=areas, many=True)
            areas_serialized.is_valid()
            data_to_send = {}
            nodes_to_send = {}
            for area in areas:
                data_by_area_and_day = Data.objects.filter(measuring_date__range=[date_start.date(), date_end.date()],
                                                           area_id=area).order_by('measuring_date')
                nodes_by_area = Node.objects.filter(area=area)
                data_by_area = {}
                for node in nodes_by_area:
                    data_by_node = data_by_area_and_day.filter(node=node)
                    data_serialized = DataSerialization(data=data_by_node, many=True)
                    data_serialized.is_valid()
                    data_by_area[node.id] = data_serialized.data
                data_to_send[area.id] = data_by_area
                nodes_serialized = NodeSerialization(data=nodes_by_area, many=True)
                nodes_serialized.is_valid()
                nodes_to_send[area.id] = nodes_serialized.data
            dict_data = {
                'data': data_to_send,
                'areas': areas_serialized.data,
                'nodes': nodes_to_send
            }
        else:
            area_object = Area.objects.get(pk=area)
            data_by_area_and_day = Data.objects.filter(measuring_date__range=[date_start.date(), date_end.date()],
                                                       area_id=area).order_by('measuring_date')
            area_serialized = AreaSerialization(instance=area_object)
            nodes = Node.objects.filter(area=area)
            data_to_send = {}
            for node in nodes:
                data_by_node = data_by_area_and_day.filter(node=node)
                data_serialized = DataSerialization(data=data_by_node, many=True)
                data_serialized.is_valid()
                data_to_send[node.id] = data_serialized.data
            nodes_serialized = NodeSerialization(data=nodes, many=True)
            nodes_serialized.is_valid()
            dict_data = {
                'data': data_to_send,
                'area': area_serialized.data,
                'nodes': nodes_serialized.data
            }
        return JsonResponse(dict_data, safe=False)

# def aveage_data_by_month(data):
#     data

# class ReportExport(LoginRequiredMixin, View):
#     def get(self, request):
#         htmp_template = get_template('report/index.html')
#         html_string = htmp_template.render()
#         pdf_file = HTML(string=html_string).write_pdf()
#         response = HttpResponse(pdf_file, content_type='application/pdf')
#         response['Content-Disposition'] = 'attachment;filename="report.pdf"'
#         return response


#!/usr/bin/python3

import logging
import os
import time

logger = logging.getLogger("nitsi.logger")

class Logger_Exeception(BaseException):
    pass

# This function should create the necessary folders
# and touch the logging files
def init_logging(path):
    logger.debug("Init logging directory")

    try:
        path = os.path.abspath(path)
    except BaseException as e:
        logger.error("Failed to get the absolute path for: {}".format(path))

    if os.path.isdir(path):
        log_dir = "{}/log".format(path)
    elif os.path.isfile(path):
        log_dir = "{}/{}.log".format(os.path.dirname(path), os.path.basename(path))

    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    time_dir = log_dir + "/" + time.strftime("%Y-%m-%d_%H-%M-%S" ,time.gmtime(time.time()))

    if os.path.exists(time_dir):
        logger.error("Path {} alreday exist".format(time_dir))
        raise Logger_Exeception
    else:
        os.mkdir(time_dir)

    return time_dir

class TestFormatter(logging.Formatter):
    def __init__(self, start_time=None, name=None, longest_machine_name=10):
        super().__init__(fmt="[%(asctime)s] %(message)s")
        logger.debug("Initiating TestFormatter for")
        if start_time == None:
            self.starttime = time.time()
        else:
            self.starttime = start_time

        if name == None:
            self.name = ""
        else:
            self.name = name

        self.longest_machine_name = longest_machine_name

    def converter(self, recordtime):

        # This returns a timestamp relatively to the time when we started
        # the test.

        recordtime -= self.starttime

        return time.gmtime(recordtime)

    def format(self, record):
        return "[{}][{:^{align}}] {}".format(self.formatTime(record),
                                                self.name,
                                                record.getMessage(),
                                                align=self.longest_machine_name)

    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        t = time.strftime("%H:%M:%S", ct)
        s = "{}.{:03d}".format(t, round(record.msecs,None))
        return s

class Log_Formatter(logging.Formatter):
    def __init__(self):
        super().__init__(fmt="[%(asctime)s] [%(levelname)s] %(message)s")
        logger.debug("Initiating Log_Formatter")

    def format(self, record):
        # We use 8 to align the levelname, because CRITICAL ist the longest name and has 8 chars
        return "[{}][{:^}][{:^8}] {}".format(self.formatTime(record),
                                                record.name,
                                                record.levelname,
                                                record.getMessage())

    def formatTime(self, record, datefmt=None):
        t = time.strftime("%H:%M:%S", time.gmtime(record.created))
        s = "{}.{:03d}".format(t, round(record.msecs,None))
        return s


from time import time, sleep

import boto3
import requests
import simplejson

URL_TRANSACTIONS = 'https://www.bitstamp.net/api/v2/transactions/{}/?time=minute'
PAIRS = ['btcusd', 'btceur', 'eurusd', 'xrpusd', 'xrpeur', 'xrpbtc', 'ltcusd', 'ltceur', 'ltcbtc', 'ethusd', 'etheur', 'ethbtc']

def get_transactions_minute_json(pair):
    transactions = simplejson.loads(requests.get(URL_TRANSACTIONS.format(pair)).text)
    timestamp = int(time())
    if not transactions:
        return {
            'error': 'No transactions executed in the last minute.',
            'timestamp': timestamp,
            'currency_pair': pair,
            'log_type': 'transactions_minute',
        }
    prices = [float(transaction['price']) for transaction in transactions]
    return {
        'currency_pair': pair,
        'price_open': prices[-1],
        'price_close': prices[0],
        'price_low': min(prices),
        'price_high': max(prices),
        'price_minute_avg': sum(prices)/len(prices),
        'timestamp': timestamp,
        'log_type': 'transactions_minute'
    }

client = boto3.client(
    's3',
    aws_access_key_id='***',
    aws_secret_access_key='***'
)

while (True):
    multiline_json_string = '\n'.join([simplejson.dumps(get_transactions_minute_json(pair)) for pair in PAIRS])
    client.put_object(Body=b'{}'.format(multiline_json_string), Bucket='bitstamp-transaction-log', Key=str(int(time())))
    sleep(60)


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

plt.figure(figsize=(17, 5))

img = cv2.imread('hand.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height = img.shape[0]
width = img.shape[1]
plt.subplot(1, 3, 1)
plt.imshow(img)
plt.axis('off')
plt.title('original')

# OpenCV를 이용한 변환 행렬 도출
center = (width / 2, height / 2)
cv_M = cv2.getRotationMatrix2D(center, 90, 1.0)  # 회전 방향이 반시계방향(CCW; Counter Clock-Wise)
cv_result = cv2.warpAffine(img, cv_M, (width, height))
print('>> OpenCV Rotation matrix')
print(cv_M, end='\n\n')

plt.subplot(1, 3, 2)
plt.imshow(cv_result)
plt.axis('off')
plt.title('cv_result')

theta = np.deg2rad(90)
my_m = np.array([[1,0,-width/2],[0,1,-height/2],[0,0,1]])
my_m = np.array([[np.cos(theta),np.sin(theta),1],[-np.sin(theta),np.cos(theta),1],[0,0,1]]).dot(my_m)
my_m = np.array([[1,0,width/2],[0,1,height/2],[0,0,1]]).dot(my_m)

print('>> My matrix')
print(my_m[:2])
my_result = cv2.warpAffine(img, my_m[:2], (width, height))

plt.subplot(1, 3, 3)
plt.imshow(my_result)
plt.axis('off')
plt.title('my_result')

# figure 출력
plt.tight_layout()
plt.show()


"""
Filename:     calc_temporal_aggregate.py
Author:       Damien Irving, irving.damien@gmail.com
Description:  Calculate the linear trend or climatology

"""

# Import general Python modules

import sys, os, pdb
import argparse
import numpy, math
import iris
from iris.experimental.equalise_cubes import equalise_attributes
from decimal import Decimal

# Import my modules

cwd = os.getcwd()
repo_dir = '/'
for directory in cwd.split('/')[1:]:
    repo_dir = os.path.join(repo_dir, directory)
    if directory == 'ocean-analysis':
        break

modules_dir = os.path.join(repo_dir, 'modules')
sys.path.append(modules_dir)

try:
    import general_io as gio
    import timeseries
    import grids
    import convenient_universal as uconv
    import spatial_weights
except ImportError:
    raise ImportError('Must run this script from anywhere within the ocean-analysis git repo')

# Define functions

def calc_linear_trend(data, xaxis):
    """Calculate the linear trend.
    polyfit returns [a, b] corresponding to y = a + bx
    """

    if data.mask[0]:
        return data.fill_value
    else:
        return numpy.polynomial.polynomial.polyfit(xaxis, data, 1)[-1]

def get_agg_cube(cube, agg):
    """Get the temporally aggregated data.

    Args:
      cube (iris.cube.Cube)
      agg (str)

    """

    coord_names = [coord.name() for coord in cube.dim_coords]
    assert coord_names[0] == 'time'

    if agg == 'trend':
        trend_data = timeseries.calc_trend(cube, per_yr=True)
        agg_cube = cube[0, ::].copy()
        agg_cube.data = trend_data
        try:
            agg_cube.units = str(cube.units) + ' yr-1'
        except ValueError:
            agg_cube.units = 'yr-1'
    elif agg == 'clim':
        agg_cube = cube.collapsed('time', iris.analysis.MEAN)
    agg_cube.remove_coord('time')

    return agg_cube

def lat_tropics(cell):
    return -30 < cell < 30

def subtract_tropics(cube):
    """Subtract the mean tropics trend."""

    lat_constraint = iris.Constraint(latitude=lat_tropics)
    tropics_cube = cube.extract(lat_constraint)
    tropics_mean = tropics_cube.collapsed(['longitude', 'latitude'], iris.analysis.MEAN)

    cube.data = cube.data - tropics_mean.data

    return cube

def vertical_mean(cube):
    """Calculate the vertical mean"""

    coord_names = [coord.name() for coord in cube.dim_coords]
    depth_axis = cube.coord('depth')

    assert depth_axis.units in ['m', 'dbar'], "Unrecognised depth axis units"
    if depth_axis.units == 'm':
        weights = spatial_weights.calc_vertical_weights_1D(depth_axis, coord_names, cube.shape)
    elif depth_axis.units == 'dbar':
        assert coord_names == ['time', 'depth', 'latitude', 'longitude'], "2D weights will not work for curvilinear grid"
        weights = spatial_weights.calc_vertical_weights_2D(depth_axis, cube.coord('latitude'), coord_names, cube.shape)

    cube = cube.collapsed('depth', iris.analysis.MEAN, weights=weights)
    cube.remove_coord('depth')

    return cube

def main(inargs):
    """Run the program."""

    # Read data
    try:
        time_constraint = gio.get_time_constraint(inargs.time_bounds)
    except AttributeError:
        time_constraint = iris.Constraint()

    depth_constraint = gio.iris_vertical_constraint(inargs.min_depth, inargs.max_depth)

    with iris.FUTURE.context(cell_datetime_objects=True):
        cube = iris.load(inargs.infiles, gio.check_iris_var(inargs.var) & depth_constraint)
        history = cube[0].attributes['history']
        atts = cube[0].attributes
        equalise_attributes(cube)
        iris.util.unify_time_units(cube)
        cube = cube.concatenate_cube()

        cube = gio.check_time_units(cube)
        cube = cube.extract(time_constraint)

        cube = iris.util.squeeze(cube)

        if 'salinity' in inargs.var:
            cube = gio.salinity_unit_check(cube)

        infile_metadata = {inargs.infiles[0]: history}

    if inargs.annual:
        cube = timeseries.convert_to_annual(cube, full_months=True)

    if inargs.min_depth or inargs.max_depth:
        cube = vertical_mean(cube)

    agg_cube = get_agg_cube(cube, inargs.aggregation)

    if inargs.regrid:
        before_sum = agg_cube.data.sum()
        before_mean = agg_cube.data.mean()
        agg_cube, coord_names, regrid_status = grids.curvilinear_to_rectilinear(agg_cube)
        if regrid_status:
            print('Warning: Data has been regridded')
            print('Before sum:', '%.2E' % Decimal(before_sum) )
            print('After sum:', '%.2E' % Decimal(agg_cube.data.sum()) )
            print('Before mean:', '%.2E' % Decimal(before_mean) )
            print('After mean:', '%.2E' % Decimal(agg_cube.data.mean()) )

    if inargs.subtract_tropics:
        agg_cube = subtract_tropics(agg_cube)

    if inargs.land_mask:
        sftlf_cube = iris.load_cube(inargs.land_mask, 'land_area_fraction')
        agg_cube = uconv.apply_land_ocean_mask(agg_cube, sftlf_cube, 'ocean')

    atts['history'] = gio.write_metadata(file_info=infile_metadata)
    agg_cube.attributes = atts

    iris.FUTURE.netcdf_no_unlimited = True
    iris.save(agg_cube, inargs.outfile)

if __name__ == '__main__':

    extra_info ="""
author:
  Damien Irving, irving.damien@gmail.com

"""

    description='Calculate the temporal aggregate (linear trend or climatology) at each grid point'
    parser = argparse.ArgumentParser(description=description,
                                     epilog=extra_info,
                                     argument_default=argparse.SUPPRESS,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument("infiles", type=str, nargs='*', help="Input files")
    parser.add_argument("var", type=str, help="Variable standard_name")
    parser.add_argument("aggregation", type=str, choices=('trend', 'clim'), help="Method for temporal aggregation")
    parser.add_argument("outfile", type=str, help="Output file name")

    parser.add_argument("--time_bounds", type=str, nargs=2, metavar=('START_DATE', 'END_DATE'),
                        help="Time period [default = entire]")

    parser.add_argument("--min_depth", type=float, default=None,
                        help="Only include data below this vertical level")
    parser.add_argument("--max_depth", type=float, default=None,
                        help="Only include data above this vertical level")

    parser.add_argument("--subtract_tropics", action="store_true", default=False,
                        help="Subtract the mean tropics trend from all data points")
    parser.add_argument("--regrid", action="store_true", default=False,
                        help="Regrid to a regular lat/lon grid")

    parser.add_argument("--land_mask", type=str, default=None,
                        help="Name of sftlf file to use for land mask")

    parser.add_argument("--annual", action="store_true", default=False,
                        help="Convert data to annual mean before calculating aggregate [default=False]")

    args = parser.parse_args()
    main(args)


#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.shortcuts import render, render_to_response
from base_api.full_views.helper import get_request_param_as_string
from base_api.models import *
from base_api.form import *
from django.http import *

def full_add_city(request):
    if not request.user.is_active:
        return HttpResponseRedirect('/login/')
    out = {}
    user_role = Roles.objects.get(id=request.user.id).role
    if user_role == 2:
        return HttpResponseRedirect('/oops/')
    else:
        out.update({'user_role': user_role})
    get_params = '?'
    get_params += get_request_param_as_string(request)
    if request.method == 'POST':
        form = CityForm(request.POST)
        if form.is_valid():
            name = request.POST['name']
            if Cities.objects.filter(name=name).count() == 0:
                new_city = Cities.objects.create(name=name)
                return HttpResponseRedirect('/' + get_params)
            else:
                out.update({"error": 1})
                out.update({'page_title': "Добавление города"})
        else:
            out.update({'page_title': "Добавление города"})
    else:
        form = CityForm()
    out.update({'form': form})
    return render(request, 'add_city.html', out)


#! /usr/bin/env python

"""
    How many total Characters are there?
    How many of each specific subclass?
    How many total Items?
    How many of the Items are weapons? How many are not?
    How many Items does each character have? (Return first 20 rows)
    How many Weapons does each character have? (Return first 20 rows)
    On average, how many Items does each Character have?
    On average, how many Weapons does each character have?
"""

import sqlite3

conn = sqlite3.connect('rpg_db.sqlite3')
curs = conn.cursor()

# How many total Characters are there? table: charactercreator_character
count_characters = 'SELECT COUNT(*) FROM charactercreator_character;'
print(curs.execute(count_characters).fetchall())

# How many of each specific subclass?
count_mages = 'SELECT COUNT(*) FROM charactercreator_mage;'
print(curs.execute(count_mages).fetchall())

# How many total Items?
count_items = 'SELECT COUNT(*) FROM armory_item;'
print(curs.execute(count_items).fetchall())

# How many of the Items are weapons? How many are not?
count_weapons = 'SELECT COUNT(*) FROM armory_item WHERE item like("%WEAPON%");'
print(curs.execute(count_weapons).fetchall())

# How many Items does each character have? (Return first 20 rows)
# How many Weapons does each character have? (Return first 20 rows)
# On average, how many Items does each Character have


from scipy import stats
from itertools import chain

df = [[15, 20], [17, 3]]

def check(df):
    if min(list(chain(*df))) < 5:
        fisher(df)
    else:
        pirson(df)

def fisher(df):
    print('fisher')
    odds_ratio, p_value = stats.fisher_exact(df)
    print(f'p-value = {p_value:.3f}')

def pirson(df):
    print('pirson')
    chi2_value, p_value, df_value, expected_array = stats.chi2_contingency(df, correction=True)
    print(f'Хи-квадрат = {chi2_value:.3f}, p-value = {p_value:.3f}, df = {df_value}')
    print(f'Ожидаемые значения:\n{expected_array}')

check(df)

--- Расчет по парсону или фишеру листа 2х2/den.py


import enum
import re
from collections import defaultdict
from typing import (
    Any,
    Callable,
    DefaultDict,
    Dict,
    List,
    Mapping,
    Match,
    NamedTuple,
    Pattern,
    Tuple,
    Union,
)

from django.db.models import Q

from sentry import eventstore
from sentry.api.base import Endpoint
from sentry.incidents.models import Incident
from sentry.models import Group, Project
from sentry.shared_integrations.exceptions import ApiError
from sentry.web.decorators import transaction_start
from sentry.utils import json

from .client import SlackClient
from .requests import SlackEventRequest, SlackRequestError
from .utils import build_group_attachment, build_incident_attachment, parse_link, logger

UnfurledUrl = Any

class LinkType(enum.Enum):
    ISSUES = "issues"
    INCIDENTS = "incidents"
    DISCOVER = "discover"

class UnfurlableUrl(NamedTuple):
    url: str
    args: Mapping[str, Any]

class Handler(NamedTuple):
    matcher: Pattern
    arg_types: Mapping[str, type]
    fn: Callable[[Any, List[UnfurlableUrl]], UnfurledUrl]

def unfurl_issues(integration, links: List[UnfurlableUrl]) -> UnfurledUrl:
    """
    Returns a map of the attachments used in the response we send to Slack
    for a particular issue by the URL of the yet-unfurled links a user included
    in their Slack message.

    url_by_issue_id: a map with URL as the value and the issue ID as the key
    event_id_by_url: a map with the event ID in a URL as the value and the URL as the key
    """
    group_by_id = {
        g.id: g
        for g in Group.objects.filter(
            id__in={link.args["issue_id"] for link in links},
            project__in=Project.objects.filter(organization__in=integration.organizations.all()),
        )
    }
    if not group_by_id:
        return {}

    out = {}
    for link in links:
        issue_id = link.args["issue_id"]

        if issue_id in group_by_id:
            group = group_by_id[issue_id]
            # lookup the event by the id
            event_id = link.args["event_id"]
            event = eventstore.get_event_by_id(group.project_id, event_id) if event_id else None
            out[link.url] = build_group_attachment(
                group_by_id[issue_id], event=event, link_to_event=True
            )
    return out

def unfurl_incidents(integration, links: List[UnfurlableUrl]) -> UnfurledUrl:
    filter_query = Q()
    # Since we don't have real ids here, we use the org slug so that we can
    # make sure the identifiers correspond to the correct organization.
    for link in links:
        identifier = link.args["incident_id"]
        org_slug = link.args["org_slug"]
        filter_query |= Q(identifier=identifier, organization__slug=org_slug)

    results = {
        i.identifier: i
        for i in Incident.objects.filter(
            filter_query,
            # Filter by integration organization here as well to make sure that
            # we have permission to access these incidents.
            organization__in=integration.organizations.all(),
        )
    }
    if not results:
        return {}

    return {
        link.url: build_incident_attachment(
            action=None,
            incident=results[link.args["incident_id"]],
        )
        for link in links
        if link.args["incident_id"] in results
    }

# XXX: The regex matchers could be more tightly bound to our configured domain,
# but slack limits what we can unfurl anyways so its probably safe
_link_handlers = {
    LinkType.ISSUES: Handler(
        fn=unfurl_issues,
        matcher=re.compile(
            r"^https?\://[^/]+/[^/]+/[^/]+/issues/(?P<issue_id>\d+)(?:/events/(?P<event_id>\w+))?"
        ),
        arg_types={
            "issue_id": int,
            "event_id": str,
        },
    ),
    LinkType.INCIDENTS: Handler(
        fn=unfurl_incidents,
        matcher=re.compile(
            r"^https?\://[^/]+/organizations/(?P<org_slug>[^/]+)/incidents/(?P<incident_id>\d+)"
        ),
        arg_types={
            "org_slug": str,
            "incident_id": int,
        },
    ),
}

def match_link(link: str) -> Union[Tuple[LinkType, Match], Tuple[None, None]]:
    for link_type, handler in _link_handlers.items():
        match = handler.matcher.match(link)
        if match:
            return link_type, match
    return None, None

# XXX(dcramer): a lot of this is copied from sentry-plugins right now, and will
# need refactored
class SlackEventEndpoint(Endpoint):
    authentication_classes = ()
    permission_classes = ()

    def _get_access_token(self, integration):
        # the classic bot tokens must use the user auth token for URL unfurling
        # we stored the user_access_token there
        # but for workspace apps and new slack bot tokens, we can just use access_token
        return integration.metadata.get("user_access_token") or integration.metadata["access_token"]

    def on_url_verification(self, request, data):
        return self.respond({"challenge": data["challenge"]})

    def on_message(self, request, integration, token, data):
        channel = data["channel"]
        # if it's a message posted by our bot, we don't want to respond since
        # that will cause an infinite loop of messages
        if data.get("bot_id"):
            return self.respond()

        access_token = self._get_access_token(integration)

        headers = {"Authorization": "Bearer %s" % access_token}
        payload = {
            "channel": channel,
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Want to learn more about configuring alerts in Sentry? Check out our documentation.",
                    },
                },
                {
                    "type": "actions",
                    "elements": [
                        {
                            "type": "button",
                            "text": {"type": "plain_text", "text": "Sentry Docs"},
                            "url": "https://docs.sentry.io/product/alerts-notifications/alerts/",
                            "value": "sentry_docs_link_clicked",
                        }
                    ],
                },
            ],
        }

        client = SlackClient()
        try:
            client.post("/chat.postMessage", headers=headers, data=payload, json=True)
        except ApiError as e:
            logger.error("slack.event.on-message-error", extra={"error": str(e)})

        return self.respond()

    def _parse_url(self, link) -> Union[Tuple[LinkType, Mapping[str, Any]], Tuple[None, None]]:
        """
        Determines if the URL is something we are able to unfurl, if it is we
        will return the named groups from the regex match, along with the link
        type.
        """
        link_type, match = match_link(link)

        if link_type is None or match is None:
            return None, None

        return link_type, match.groupdict()

    def on_link_shared(self, request, integration, token, data):
        matches: DefaultDict[LinkType, List[UnfurlableUrl]] = defaultdict(list)
        links_seen = set()

        # An unfurl may have multiple links to unfurl
        for item in data["links"]:
            try:
                # We would like to track what types of links users are sharing,
                # but it's a little difficult to do in sentry since we filter
                # requests from Slack bots. Instead we just log to Kibana
                logger.info(
                    "slack.link-shared", extra={"slack_shared_link": parse_link(item["url"])}
                )
            except Exception as e:
                logger.error("slack.parse-link-error", extra={"error": str(e)})

            link_type, args = self._parse_url(item["url"])

            if not link_type or args is None:
                continue

            # Coerce link argument types
            arg_types = _link_handlers[link_type].arg_types
            clean_args = {k: arg_types[k](v) if v is not None else None for k, v in args.items()}

            # Don't unfurl the same thing multiple times
            seen_marker = (link_type, frozenset(args.items()))
            if seen_marker not in links_seen:
                links_seen.add(seen_marker)
            else:
                continue

            matches[link_type].append(UnfurlableUrl(url=item["url"], args=clean_args))

        if not matches:
            return

        # Unfurl each link type
        results: Dict[str, Any] = {}
        for link_type, unfurl_data in matches.items():
            results.update(_link_handlers[link_type].fn(integration, unfurl_data))

        if not results:
            return

        access_token = self._get_access_token(integration)

        payload = {
            "token": access_token,
            "channel": data["channel"],
            "ts": data["message_ts"],
            "unfurls": json.dumps(results),
        }

        client = SlackClient()
        try:
            client.post("/chat.unfurl", data=payload)
        except ApiError as e:
            logger.error("slack.event.unfurl-error", extra={"error": str(e)})

        return self.respond()

    # TODO(dcramer): implement app_uninstalled and tokens_revoked
    @transaction_start("SlackEventEndpoint")
    def post(self, request):
        try:
            slack_request = SlackEventRequest(request)
            slack_request.validate()
        except SlackRequestError as e:
            return self.respond(status=e.status)

        if slack_request.is_challenge():
            return self.on_url_verification(request, slack_request.data)

        if slack_request.type == "link_shared":
            resp = self.on_link_shared(
                request,
                slack_request.integration,
                slack_request.data.get("token"),
                slack_request.data.get("event"),
            )

            if resp:
                return resp

        if slack_request.type == "message":
            resp = self.on_message(
                request,
                slack_request.integration,
                slack_request.data.get("token"),
                slack_request.data.get("event"),
            )

            if resp:
                return resp

        return self.respond()


import collections
from corehq import privileges, toggles
from corehq.apps.hqwebapp.templatetags.hq_shared_tags import toggle_enabled
from corehq.apps.userreports.const import REPORT_BUILDER_EVENTS_KEY
from django_prbac.utils import has_privilege

def localize(value, lang):
    """
    Localize the given value.

    This function is intended to be used within UCR to localize user supplied
    translations.

    :param value: A dict-like object or string
    :param lang: A language code.
    """
    if isinstance(value, collections.Mapping) and len(value):
        return (
            value.get(lang, None) or
            value.get(default_language(), None) or
            value[sorted(value.keys())[0]]
        )
    return value

def default_language():
    return "en"

def has_report_builder_add_on_privilege(request):
    return any(
        has_privilege(request, p) for p in privileges.REPORT_BUILDER_ADD_ON_PRIVS
    )

def has_report_builder_access(request):

    builder_enabled = toggle_enabled(request, toggles.REPORT_BUILDER)
    legacy_builder_priv = has_privilege(request, privileges.REPORT_BUILDER)
    beta_group_enabled = toggle_enabled(request, toggles.REPORT_BUILDER_BETA_GROUP)
    has_add_on_priv = has_report_builder_add_on_privilege(request)

    return (builder_enabled and legacy_builder_priv) or beta_group_enabled or has_add_on_priv

def add_event(request, event):
    events = request.session.get(REPORT_BUILDER_EVENTS_KEY, [])
    request.session[REPORT_BUILDER_EVENTS_KEY] = events + [event]

def has_report_builder_trial(request):
    return has_privilege(request, privileges.REPORT_BUILDER_TRIAL)

def can_edit_report(request, report):
    ucr_toggle = toggle_enabled(request, toggles.USER_CONFIGURABLE_REPORTS)
    report_builder_toggle = toggle_enabled(request, toggles.REPORT_BUILDER)
    report_builder_beta_toggle = toggle_enabled(request, toggles.REPORT_BUILDER_BETA_GROUP)
    add_on_priv = has_report_builder_add_on_privilege(request)
    created_by_builder = report.spec.report_meta.created_by_builder

    if created_by_builder:
        return report_builder_toggle or report_builder_beta_toggle or add_on_priv
    else:
        return ucr_toggle

def allowed_report_builder_reports(request):
    """
    Return the number of report builder reports allowed
    """
    builder_enabled = toggle_enabled(request, toggles.REPORT_BUILDER)
    legacy_builder_priv = has_privilege(request, privileges.REPORT_BUILDER)
    beta_group_enabled = toggle_enabled(request, toggles.REPORT_BUILDER_BETA_GROUP)

    if toggle_enabled(request, toggles.UNLIMITED_REPORT_BUILDER_REPORTS):
        return float("inf")
    if has_privilege(request, privileges.REPORT_BUILDER_30):
        return 30
    if has_privilege(request, privileges.REPORT_BUILDER_15):
        return 15
    if (
        has_privilege(request, privileges.REPORT_BUILDER_TRIAL) or
        has_privilege(request, privileges.REPORT_BUILDER_5) or
        beta_group_enabled or
        (builder_enabled and legacy_builder_priv)
    ):
        return 5

def number_of_report_builder_reports(domain):
    from corehq.apps.userreports.models import ReportConfiguration
    existing_reports = ReportConfiguration.by_domain(domain)
    builder_reports = filter(
        lambda report: report.report_meta.created_by_builder, existing_reports
    )
    return len(builder_reports)


import boto3
import json
import sys

"""
Module to help facilitate calls to AWS SNS/SQS
"""
class AWS(object):
    """Container to handle interactions with AWS"""
    def __init__(self):
        self.sns = boto3.client('sns', 'us-east-2')
        self.sqs = boto3.client('sqs', 'us-east-2')
        self.sqs_res = boto3.resource('sqs', 'us-east-2')
        # Placeholder to store player info
        self.players = {}

    def create_topic(self, topic_name):
        print("Creating topic: ", topic_name)
        self.topic = self.sns.create_topic(Name=topic_name)
        self.topic_arn = self.topic['TopicArn']
        print("Topic arn: ", self.topic_arn)
        self.topic_name = topic_name
        return True

    def create_queue(self, queue_name):
        self.queue = self.sqs.create_queue(QueueName=queue_name)
        self.sqs_arn = self.sqs.get_queue_attributes(QueueUrl=self.queue['QueueUrl'], AttributeNames=['QueueArn'])['Attributes']['QueueArn']

        return True

    def add_policy(self):
        """
        Create/Add security policy to queue to allow topics to get tied to them
        """
        self.policy = None
        try:
            self.policy = json.loads(self.get_policy())
            # Do not bother adding a new policy if there is one in place for this topic already
            if self.topic_name in [s['Sid'] for s in self.policy['Statement']]:
                return True
        except KeyError:
            pass

        if not self.policy:
            self.policy = {
                "Version": "2012-10-17",
                "Id": "{}/SQSDefaultPolicy".format(self.sqs_arn),
                "Statement": [],
            }

        #TODO - Add a check to make sure my ARN isn't already in the list
        #if not self.policy_has_permissions_for_topic ():
        #print ("Policy type: ", type(self.policy))
        self.policy['Statement'].append({
          "Sid": self.topic_name,
          "Effect": "Allow",
          "Principal": {
            "AWS": "*"
          },
          "Action": "SQS:SendMessage",
          "Resource": self.sqs_arn,
          "Condition": {
            "ArnEquals": {
              "aws:SourceArn": self.topic_arn
            }
          }
        })
        #print('Policy: ', json.dumps(self.policy))
        self.sqs.set_queue_attributes(QueueUrl=self.queue['QueueUrl'], Attributes={'Policy': json.dumps(self.policy)})
        return True

    def subscribe_to_topic(self):
        """Subscribe to the topic"""
        return self.sns.subscribe(TopicArn=self.topic_arn, Protocol='sqs', Endpoint=self.sqs_arn)

    def get_policy(self):
        return self.sqs.get_queue_attributes(QueueUrl=self.queue['QueueUrl'], AttributeNames=['Policy'])['Attributes']['Policy']

    def delete_policy(self):
        p = json.loads(self.get_policy())
        p['Statement'] = [s for s in p['Statement'] if not s['Condition']['ArnEquals']['aws:SourceArn'] == self.topic_arn]
        self.policy = p

        self.sqs.set_queue_attributes(QueueUrl=self.queue['QueueUrl'], Attributes={'Policy': json.dumps(self.policy)})
        return True

    def receive_message(self, MaxNumberOfMessages=1, WaitTimeSeconds=20, VisibilityTimeout=10):
        return self.sqs.receive_message(QueueUrl=self.queue['QueueUrl'],MaxNumberOfMessages=MaxNumberOfMessages, WaitTimeSeconds=WaitTimeSeconds, VisibilityTimeout=VisibilityTimeout)

    def send_message(self, topic, message):
        try:
            resp = self.sns.publish(TopicArn=self.players[topic], Message=message)
        except KeyError:
            return {'success': False, 'reason': 'Publishing to an unknown topic'}
        else:
            return {'success': True, 'reason': resp}

    def register_player(self, topic):
        # TODO - put Amazon Id and region in a config environment
        # determine arn
        arn = "arn:aws:sns:us-east-2:849664249614:{}".format(topic)
        self.players[topic] = arn

# TODO
# methods to be added:
#   * Delete Message


# destination/main.py
import os
import json
import kafka_interface as kafka
import link_parser
import mongodb_interface as mongo
import xpath_service as xpath

KAFKA_BROKER_URL = os.environ.get('KAFKA_BROKER_URL')
TOPIC_INPUT = os.environ.get('TOPIC_INPUT')
DATABASE = os.environ.get('DATABASE')

if __name__ == '__main__':
    consumer = kafka.connectConsumer(topic = TOPIC_INPUT, server = KAFKA_BROKER_URL)
    working = True
    while working:
        dict = kafka.consume(consumer = consumer)
        if(dict != {}):
            for topic, messages in dict.items():
                for message in messages:
                    domain = str(message.value['domain'])
                    print('Working for domain: '+domain)
                    list_pages = link_parser.check_urls(message.value['filtered_pages'])

                    xpaths = xpath.find_xpath(domain, list_pages)

                    print('Xpaths elaborated')
                    xpath_generalized = xpath.generalize_xpath(xpaths)
                    print('Generated Xpath Generalized')

                    content = {
                        'domain': domain,
                        'Xpaths': xpaths,
                        'Generalized_Xpaths': xpath_generalized
                    }
                    mongo.put(domain,json.dumps(content))


from django import forms
from kartingapp.models import Search

class FormName(forms.ModelForm):
	class Meta:
	    Name = forms.CharField(label='Name',widget=forms.TextInput(attrs={'class':'td1','class' : 'contact-form'}))
	    Email = forms.EmailField(label='Email Address', widget=forms.TextInput(attrs={'class':'td1','class':'contact-form'}))
	    Contact=forms.IntegerField(label='Contact No.', widget=forms.TextInput(attrs={'class':'td1','class':'contact-form'}))
	    Message = forms.CharField(widget=forms.Textarea(attrs={'class':'td1', 'class':'contact-form', 'line-height' : '2', 'margin' : '3%'}))

# class SearchBar(forms.Form):
class SearchBar(forms.Form):
	Search = forms.CharField(label='', max_length=100,
    	widget=forms.TextInput(attrs={'placeholder':'What are you looking for?',
    	                              'class' : "search-input"
                                       }))

	# class Meta:
	# 	model=Search
	# 	fields=['mdl_search']
	# 	widgets ={'mdl_search': forms.TextInput(attrs={'placeholder':'What are you looking for?',
 #    	                              'class' : "search-input"
 #                                       })}


import sqlite3

class Database:
    def __init__(self):
        self.conn = None
        self.cursor = None

    def connect(self):
        self.conn = sqlite3.connect(r"chat.db")
        self.cursor = self.conn.cursor()

    def vote(self, author, voter, chat_id, message_id, vote_type):
        self.create_user(author.username, author.id, chat_id)
        self.create_user(voter.username, voter.id, chat_id)
        if self.check_repeat(voter.id, chat_id, message_id):
            return 'You can\'t vote for this message again'
        if not self.check_positive(voter.id, chat_id):
            user_data = self.get_user_stat(voter.id, chat_id)
            rating = user_data[1]
            return 'You can\'t vote with negative rating @{} ({})'.format(voter.username, rating)
        self.save_message(message_id, voter.id, chat_id)
        self.db_vote(author.id, chat_id, vote_type)
        data = self.get_user_stat(author.id, chat_id)
        return '@{} voted to @{} ({})'.format(voter.username, author.username, data[1])

    def create_user(self, name, user_id, chat_id):
        if self.check_user(user_id, chat_id):
            return True
        self.cursor.execute("INSERT INTO users(name, user_id, chat_id) values (?, ?, ?) ",
                            [name, user_id, chat_id])
        self.conn.commit()
        self.cursor.execute("SELECT name FROM users")
        results = self.cursor.fetchone()
        return bool(results)

    def check_user(self, user_id, chat_id):
        self.cursor.execute("SELECT name FROM users WHERE user_id = ? AND chat_id = ?",
                            [user_id, chat_id])
        results = self.cursor.fetchone()
        return bool(results)

    def check_repeat(self, user_id, chat_id, message_id):
        self.cursor.execute("SELECT message_id FROM messages WHERE message_id = ? AND chat_id = ? AND user_id = ?",
                            [message_id, chat_id, user_id])
        results = self.cursor.fetchone()
        return bool(results)

    def check_positive(self, user_id, chat_id):
        self.cursor.execute("SELECT rating FROM users WHERE user_id = ? AND chat_id = ?",
                            [user_id, chat_id])
        results = self.cursor.fetchone()
        if results is not None:
            return results[0] >= 0
        else:
            return True

    def db_vote(self, user_id, chat_id, vote_type):
        if vote_type:
            self.cursor.execute("UPDATE users SET rating = rating + 1 WHERE user_id = ? AND chat_id = ?",
                                [user_id, chat_id])
        else:
            self.cursor.execute("UPDATE users SET rating = rating - 1 WHERE user_id = ? AND chat_id = ?",
                                [user_id, chat_id])
        self.conn.commit()
        return True

    def save_message(self, message_id, user_id, chat_id):
        self.cursor.execute("INSERT INTO messages(message_id, user_id, chat_id) values (?, ?, ?) ",
                            [message_id, user_id, chat_id])
        self.conn.commit()
        results = self.cursor.fetchone()
        return bool(results)

    def get_user_stat(self, user_id, chat_id):
        self.cursor.execute("SELECT name, rating FROM users WHERE user_id = ? AND chat_id = ?",
                            [user_id, chat_id])
        results = self.cursor.fetchone()
        return results

    def chat_rating(self, chat_id):
        self.cursor.execute("SELECT name, rating, user_id FROM users WHERE chat_id = ? ORDER BY rating DESC",
                            [chat_id])
        results = self.cursor.fetchall()
        return results


import pytest
from cicd import app as flask_app
from cicd import db

@pytest.fixture
def app():
    print('\napp is creating')
    yield flask_app

@pytest.fixture
def client(app):
    client = app.test_client()
    yield client

@pytest.fixture
def empty_db(app):
    print('\ndatabase is creating')
    db.create_all()
    yield db

    # teardown
    db.session.remove()
    db.drop_all()
    print('\ndatabase is destroying')


#!/usr/bin/env python
# --*coding: utf8*--
import tornado.httpserver
import tornado.ioloop
import tornado.options

import os.path
import os
import sys
from handlers import *

from tornado.options import define, options
define("port", default=5000, help="run on the given port", type=int)

class Application(tornado.web.Application):
    def __init__(self):
        self.db = conn["iub_test"]
        handlers = [
            (r"/", MainPageHandler),
            (r"/page/(\w+)", IndexHandler),
            (r"/about", AboutHandler),
            (r"/signup", RegisterHandler),
            (r"/login", LoginHandler),
            (r"/logout", LogoutHandler),
            (r"/sendMail", SendMailHandler),
            (r"/verify", VerifyHandler),
            (r"/newpost", NewPostHandler),
            (r"/post/(\w+)", ShowPostHandler),
            (r"/editPost/(\w+)", EditPostHandler),
            (r"/error", NotFoundHandler),
            (r".*", NotFoundHandler)
        ]
        settings = {
            'template_path':
                os.path.join(os.path.dirname(__file__), "templates"),
            'static_path':
                os.path.join(os.path.dirname(__file__), "static"),
            'cookie_secret':
                'loNdeMclQFe6rcCXOGXGhpCn1NFxXUaTgDQo7gLsyIY=',
            'xsrf_cookies':
                True,
            'debug': True
        }
        tornado.web.Application.__init__(self, handlers, **settings)

def main():
    reload(sys)
    sys.setdefaultencoding("utf-8")
    os.environ['TZ'] = 'US/Eastern'  # set time zone to estern time
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()


'''
@Descripttion:
@version:
@Author: JackZhang
@Date: 2020-03-31 20:48:47
@LastEditors: JackZhang
@LastEditTime: 2020-04-01 13:12:22
'''
# import module.
import pygame, sys, os
from pygame import Color

# set-up path
game_path = os.path.dirname(__file__)
img_path = os.path.join(game_path, "img")
# print(game_path, img_path)

# set up custom game window size.
window_size = width, height = 336, 607

# initialize the pygame and create the main window.
pygame.init()
pygame.display.set_caption("02-Show Your First Sprite")
screen = pygame.display.set_mode(window_size)

# set frame-per-second and the clock.
fps = 30
clock = pygame.time.Clock()

# class Player extends class pygame.sprite.Sprite
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_path, "player.png")).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.center = (width/2, height/2)

# you should add all the sprites you want to show on the screen to the pygame.sprite.Group.
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

# a flag.
running = True

# game's main loop.
while running:
    # keep fps
    clock.tick(fps)

    # process event.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    # update
    all_sprites.update()
    # draw and render
    screen.fill(Color("grey"))
    all_sprites.draw(screen)

    # flush screen.
    pygame.display.flip()
    # or pygame.display.update()

# quit game.
pygame.quit()
# sys.exit()


#!/usr/bin/env python

import argparse
import sys
from collections import Counter

import faiss
import networkx as nx
import numpy as np
from chinese_whispers import chinese_whispers, aggregate_clusters

from utils import triples, grouper, word_vectors, words_vec

parser = argparse.ArgumentParser()
parser.add_argument('--neighbors', '-n', type=int, default=10)
parser.add_argument('--min-weight', type=float, default=0.)
parser.add_argument('--pickle', type=argparse.FileType('wb'))
parser.add_argument('triples', type=argparse.FileType('r', encoding='UTF-8'))
group = parser.add_mutually_exclusive_group()
group.add_argument('--w2v', default=None, type=argparse.FileType('rb'))
group.add_argument('--pyro', default=None, type=str)
args = parser.parse_args()

w2v = word_vectors(args, lambda args: parser.error('Please set the --w2v or --pyro option.'))

spos, _ = triples(args.triples, min_weight=args.min_weight, build_index=False)

vocabulary = {word for triple in spos for word in (triple.subject, triple.predicate, triple.object)}

vectors = {}

for words in grouper(vocabulary, 512):
    vectors.update(words_vec(w2v, words))

spos = [triple for triple in spos if
        triple.subject in vectors and triple.predicate in vectors and triple.object in vectors]

# noinspection PyUnboundLocalVariable
X, index2triple = np.empty((len(spos), w2v.vector_size * 3), 'float32'), {}

for i, triple in enumerate(spos):
    X[i] = np.concatenate((vectors[triple.subject], vectors[triple.predicate], vectors[triple.object]))
    index2triple[i] = (triple.subject, triple.predicate, triple.object)

knn = faiss.IndexFlatIP(X.shape[1])
knn.add(X)

G, maximal_distance = nx.Graph(), -1

for slice in grouper(range(X.shape[0]), 2048):
    slice = [j for j in slice if j is not None]

    D, I = knn.search(X[slice, :], args.neighbors + 1)

    last = min(slice)
    print('%d / %d' % (last, X.shape[0]), file=sys.stderr)

    for i, (_D, _I) in enumerate(zip(D, I)):
        source = index2triple[last + i]
        words = Counter()

        for d, j in zip(_D.ravel(), _I.ravel()):
            if last + i != j:
                words[index2triple[j]] = float(d)

        for target, distance in words.most_common(args.neighbors):
            G.add_edge(source, target, weight=distance)
            maximal_distance = distance if distance > maximal_distance else maximal_distance

for _, _, d in G.edges(data=True):
    d['weight'] = maximal_distance / d['weight']

if args.pickle is not None:
    import pickle

    pickle.dump(list(G.edges(data=True)), args.pickle, protocol=3)
    sys.exit(0)

chinese_whispers(G, weighting='top', iterations=20)
clusters = aggregate_clusters(G)

for label, cluster in sorted(aggregate_clusters(G).items(), key=lambda e: len(e[1]), reverse=True):
    print('# Cluster %d\n' % label)

    predicates = {predicate for predicate, _, _ in cluster}
    subjects = {subject for _, subject, _ in cluster}
    objects = {object for _, _, object in cluster}

    print('Predicates: %s' % ', '.join(predicates))
    print('Subjects: %s' % ', '.join(subjects))
    print('Objects: %s\n' % ', '.join(objects))


from django.urls import path, re_path, include
from rest_framework import routers
from . import views
from .viewsets import PostViewSet, PostImageViewSet, UserViewSet

app_name = 'django_pipes_blog'

# django rest framework url router registration
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'postimages', PostImageViewSet)
router.register(r'posts', PostViewSet)

urlpatterns = [
    # django rest framework urls
    re_path(r'^api/', include(router.urls)),
    re_path(r'^api-auth/', include('rest_framework.urls')),

    re_path(r'^$', views.IndexView.as_view(), name='index'),
    re_path(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[\w\-\%]+)/$',
        views.SinglePostView.as_view(),
        name='post'
    ),
    re_path(r'(?P<year>\d{4})/(?P<month>\d{1,2})/$',
        views.MultiPostView.as_view(),
        name='multi_post'
    ),
    path('post/new/', views.NewPostView.as_view(), name='new_post'),
    path('post/<slug:slug>/', views.SinglePostView.as_view(), name='post_slug'),
    path('post/<int:pk>/preview/', views.SinglePostView.as_view(), name='preview_post'),
    path('post/<int:pk>/edit/', views.EditPostView.as_view(), name='edit_post'),
    path('tags/<str:tags>/', views.SearchTagsView.as_view(), name='tags'),
    path('post/<int:pk>/unpublished/', views.AllPosts.as_view(), name='unpublished'),
    re_path(r'^post/(?P<pk>\d+)/(?P<all>all)/$', views.AllPosts.as_view(), name='all_posts'),
]


# -*- coding: utf-8 -*-

""" Update mongodb database """

import asyncio
import os
import time
import traceback
from datetime import datetime
from datetime import timedelta

import aiohttp
from aiosocks.connector import ProxyConnector, ProxyClientRequest
from hal.profile.mem import get_memory_usage, force_garbage_collect
from hal.time.profile import print_time_eta, get_time_eta
from pymongo import MongoClient

from .parsers import get_url_of_page, get_list_of_stages, \
    get_standings_of_stage, get_stage_details_from_url

LOG_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                        str(os.path.basename(__file__)).split(".")[
                            0] + "-" + str(int(time.time())) + ".log")
MIN_YEAR_PAGE = 1903  # minimum year of tour
MAX_YEAR_PAGE = 2016  # maximum year of tour

DATABASE_NAME = "letour-stages"  # name of mongodb database to use
MONGODB_CLIENT = MongoClient()  # mongodb client
# mongodb_client.drop_database(DATABASE_NAME)  # remove all previous data
DATABASE = MONGODB_CLIENT[
    DATABASE_NAME
]  # database to use
for coll in DATABASE.collection_names():
    DATABASE[coll].create_index("num", unique=True)  # set primary key

async def try_and_fetch(url, max_attempts=8, time_delay_between_attempts=1):
    """
    :param url: str
        Url to fetch
    :param max_attempts: int
        Max number of attempts to get page
    :param time_delay_between_attempts: float
        Number of seconds to wait between 2 consecutive attempts
    :return: str
        Body of page with url or null
    """

    for _ in range(max_attempts):
        try:
            conn = ProxyConnector(remote_resolve=True)
            async with aiohttp.ClientSession(
                    connector=conn,
                    request_class=ProxyClientRequest
            ) as session:
                async with session.get(
                        url,
                        proxy="socks5://127.0.0.1:9150"
                ) as response:  # use tor
                    body = await response.text(encoding='latin-1')
                    RAW_SOURCES.append({
                        "url": str(url),
                        "html": str(body)
                    })  # add url and page source

                    print_time_eta(
                        get_time_eta(
                            len(RAW_SOURCES),
                            TOTAL,
                            START_TIME
                        ),  # get ETA
                        note="Got HTML"
                    )  # debug info
                    return body
        except Exception as e:
            time.sleep(time_delay_between_attempts)
            traceback.print_exc()
            print("Cannot get url " + str(url))
            print(str(e))
    return None

async def bound_fetch(sem, url):
    """
    :param sem: Semaphore
        Asynchronous driver
    :param url: str
        Url of page to fetch
    :return: void
        Asynchronously fetches url
    """

    async with sem:
        await try_and_fetch(url, max_attempts=1, time_delay_between_attempts=0)

async def fetch_urls(list_of_urls, max_concurrent=200):
    """
    :param list_of_urls: [] of str
        List of urls to fetch
    :param max_concurrent: int
        Max number of concurrent connections
    :return: void
        Fetches url
    """

    tasks = []
    sem = asyncio.Semaphore(max_concurrent)
    for url in list_of_urls:
        task = asyncio.ensure_future(bound_fetch(sem, url))
        tasks.append(task)

    responses = asyncio.gather(*tasks)
    await responses

if __name__ == "__main__":
    START_TIME_OVERALL = time.time()

    print("\t0 - Getting URLs list")
    URLS_LIST = [get_url_of_page(year) for year in
                 range(MIN_YEAR_PAGE, MAX_YEAR_PAGE + 1)]  # get list of urls
    TOTAL = len(URLS_LIST)
    RAW_SOURCES = []  # list of raw HTML pages to parse

    print("\t1 - Downloading years pages")
    START_TIME = time.time()
    LOOP = asyncio.get_event_loop()
    FUTURE_LOOPS = asyncio.ensure_future(fetch_urls(URLS_LIST))  # fetch
    LOOP.run_until_complete(FUTURE_LOOPS)

    print("\t2 - Getting list of all stages")
    START_TIME = time.time()
    URLS_LIST = []
    TOTAL = len(RAW_SOURCES)
    for i, value in enumerate(RAW_SOURCES):
        URLS_LIST += get_list_of_stages(
            value["html"]
        )  # get list of stages in page
        print_time_eta(
            get_time_eta(
                i + 1,
                TOTAL,
                START_TIME
            ),  # get ETA
            note="Got stage list"
        )  # debug info

    print("\t3 - Downloading stages pages")
    RAW_SOURCES = []  # list of raw HTML pages to parse
    TOTAL = len(URLS_LIST)

    START_TIME = time.time()
    LOOP = asyncio.get_event_loop()
    FUTURE_LOOPS = asyncio.ensure_future(fetch_urls(URLS_LIST))  # fetch
    LOOP.run_until_complete(FUTURE_LOOPS)
    LOOP.close()

    print("\t4 - Garbage-collecting useless stuff")
    URLS_LIST = None
    force_garbage_collect()

    print("\t5- Parsing stages pages")
    TOTAL = len(RAW_SOURCES)
    START_TIME = time.time()
    for i, value in enumerate(RAW_SOURCES):
        stage_standings = get_standings_of_stage(
            value["html"]
        )
        stage_details = get_stage_details_from_url(value["url"])
        d = {
            "num": stage_details["id"],
            "standings": stage_standings
        }

        try:
            DATABASE[str(stage_details["year"])].insert_one(d)
            RAW_SOURCES[i] = None  # release memory
        except Exception as exception:
            print(str(exception))

        print_time_eta(
            get_time_eta(
                i + 1,
                TOTAL,
                START_TIME
            ),  # get ETA
            note="Saved to database"
        )  # debug info

    MONGODB_CLIENT.close()  # close mongodb connection

    END_TIME_OVERALL = time.time()
    DELTA_TIME_OVERALL = END_TIME_OVERALL - START_TIME_OVERALL
    DELTA_MEM_OVERALL = get_memory_usage()

    print(
        "Done downloading and saving data to mongodb database \"" + str(
            DATABASE_NAME) + "\". Job started at",
        datetime.fromtimestamp(START_TIME_OVERALL).strftime(
            "%Y-%m-%d %H:%M:%S"), "completed at",
        datetime.fromtimestamp(END_TIME_OVERALL).strftime("%Y-%m-%d %H:%M:%S"),
        ", took",
        str(timedelta(seconds=int(DELTA_TIME_OVERALL))), "and ~",
        str(DELTA_MEM_OVERALL), "MB to complete."
    )  # debug info


from tkinter import *
import tkinter.ttk as ttk
import configparser
import json
import os

class ConfigValuesSample(ttk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.label_values="label"
        self.fruit_values=""
        self.load_config()
        self.create_widgets()
        self.pack()

    """
    config.iniから読み取った値を各Widgetのオプションに与える
    """
    def create_widgets(self):
        label = ttk.Label(text=self.label_values)
        label.pack()
        combo = ttk.Combobox(self,values=self.fruit_values)
        combo.pack()

    """
    このファイルのパスからconfig.iniファイルのパスを取得し、UTF-8で読み込む
    json.loadsを使用すること(loadではない)
    """
    def load_config(self):
        path =os.path.join(os.path.dirname(__file__),"config/config.ini")
        config = configparser.ConfigParser()
        config.read(path,"UTF-8")
        self.label_values = config["settings"]["label"]
        self.fruit_values=json.loads(config["settings"]["fruitvalues"])

if __name__ == '__main__':
    master = Tk()
    master.title("ConfigValuesSample")
    master.geometry("300x200")
    ConfigValuesSample(master)
    master.mainloop()


# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
from ..utils.scripts import get_parser

__all__ = ['image_coordinates']

log = logging.getLogger(__name__)

def image_coordinates_main(args=None):
    parser = get_parser(image_coordinates)
    parser.add_argument('infile', type=str,
                        help='Input FITS file name')
    parser.add_argument('outfile', type=str,
                        help='Output FITS file name')
    parser.add_argument('--make_coordinate_maps', action='store_true',
                        help='Create coordinate maps')
    parser.add_argument('--make_distance_map', action='store_true',
                        help='Create distance to mask map')
    parser.add_argument('--overwrite', action='store_true',
                        help='Overwrite existing output file?')
    args = parser.parse_args(args)
    image_coordinates(**vars(args))

def image_coordinates(infile,
                      outfile,
                      make_coordinate_maps,
                      make_distance_map,
                      overwrite):
    """Make maps that can be used to create profiles.

    The following images can be created:
    * LON -- Longitude coordinate
    * LAT -- Latitude coordinate
    * DIST -- Distance to mask
    * SOLID_ANGLE -- Solid angle
    """
    from astropy.io import fits
    from gammapy.utils.fits import get_hdu
    from gammapy.image import SkyImage, SkyMask

    log.info('Reading {0}'.format(infile))
    hdu = get_hdu(infile)

    out_hdus = fits.HDUList()

    if make_coordinate_maps:
        image = SkyImage.empty_like(hdu)
        log.info('Computing LON and LAT maps')
        lon, lat = image.coordinates()
        out_hdus.append(fits.ImageHDU(lon, hdu.header, 'LON'))
        out_hdus.append(fits.ImageHDU(lat, hdu.header, 'LAT'))

    if make_distance_map:
        excl = SkyMask.from_image_hdu(hdu)
        log.info('Computing DIST map')
        dist = excl.exclusion_distance
        out_hdus.append(fits.ImageHDU(dist, hdu.header, 'DIST'))

    log.info('Writing {0}'.format(outfile))
    out_hdus.writeto(outfile, clobber=overwrite)


import turtle as t
from shapely.geometry import mapping
from shapely.wkt import loads
import utilities.read_from_file_or_net as rfn
import utilities.generic_useful_parameters as gp

def calc_ratios(screen_width,screen_height,data):
    """
    dist_x = max easting - min easting
    dist_y = max northing - min northing

    # Scaling ratio each axis
    # to map points from world to screen
    x_ratio = screen width / dist_x
    y_ratio = screen height / dist_y

    :param data: List of rows representing query result set.
    :return: x and y screen ratios, bounding box for collection (result set)
    """
    bbox = [9999999, 9999999, 0, 0]

    for row in data:
        coords = loads(row['geom_str'])
        bbox[0] = min(coords.bounds[0], bbox[0])
        bbox[1] = min(coords.bounds[1], bbox[1])
        bbox[2] = max(coords.bounds[2], bbox[2])
        bbox[3] = max(coords.bounds[3], bbox[3])

    dist_x = bbox[2] - bbox[0]
    dist_y = bbox[3] - bbox[1]

    return screen_width / dist_x, screen_height / dist_y, bbox

def convert_point(screen_width, screen_height,ratios, point):
    """
    Function to convert easting/northing to screen coordinates.
    Remember screen coordinates are necessary for rendering.
    :param point: The point in easting/northing to convert
    :return: x,y pair in screen coordinates
    """

    # ratios[2] is the bounding box. Its elements 2 & 3 are max x and y
    x = screen_width - ((ratios[2][2] - point[0]) * ratios[0])
    y = screen_height - ((ratios[2][3] - point[1]) * ratios[1])

    # Python turtle graphics start in the middle of the screen so we must offset the points so they are centered
    x = x - (screen_width / 2)
    y = y - (screen_height / 2)

    return x, y

def draw_data(screen_Width, screen_height, data):
    """
    Draw map based on coordinates from query result set. 'World' coordinates must be converted to screen (pixel)
    coordinates.

    :param data: List of rows representing query result set.
    :return: None. Function draws map in window.
    """

    ratios = calc_ratios(screen_Width,screen_height,data)

    for row in data:
        t.up()

        # Convert WKT representation to geometry object.
        coords = loads(row['geom_str'])

        centroid = (coords.centroid.bounds[0], coords.centroid.bounds[1])

        # Make 'World' point into screen coordinate pair.
        t.goto(convert_point(screen_Width,screen_height,ratios, centroid))

        # Write row name and population at geometry centroid (approximates to the 'middle' of the row multipolygon..
        legend = "{} (pop: {})".format(row['countyname'], row['total2011'])
        t.write(legend, align="center", font=("Arial", 10))

        # For each polygon in Multpolygon geometry, get each point in polygon, convert it to screen point and draw it.
        for geom in coords.geoms:
            for point in list(geom.exterior.coords):
                t.goto(convert_point(screen_Width,screen_height,ratios, point))
                t.down()
            t.up()

    t.hideturtle()
    t.exitonclick()

def main():
    SCREEN_WIDTH = 800
    SCREEN_HEIGHT = 600
    SQL = "select st_astext(geom) as geom_str, * from \"Counties\" where countyname like '%erry%' or countyname like '%imerick%' or countyname like '%ork%'"
    CONN_STRING = gp.DB_CONN_STRING

    my_data = rfn.get_data_from_postgres(CONN_STRING, SQL)
    if my_data:
        draw_data(SCREEN_WIDTH, SCREEN_HEIGHT, my_data)
    else:
        print("Nothing to draw.")

if __name__ == "__main__":
    main()


from flask import Flask
from flask import request, jsonify
from flask_cors import CORS

app = Flask(__name__)
app.config["DEBUG"] = True

CORS(app)

@app.route('/', methods=['GET'])
def home():
    return "
App en funcionamiento
"

@app.route('/contact-form', methods=['POST'])
def get_form():
    print(request.json)
    return jsonify({'task': 'task'}), 200
app.run()


from typing import Any, Type
import random
import time
from loguru import logger
from operator import itemgetter
from math import log
from node import Node
from referee import Referee
from tic_tac_toe import TicTacToe
import pickle
from tqdm import tqdm

LOGGER = logger

def train(count_limit, mode='first', first_node=None):
    count = 0
    nth_limit = 0
    tictactoe = TicTacToe()

    # LOGGER.info('get referee')
    referee = Referee()
    # LOGGER.success('got referee')

    if mode == 'first':
        # LOGGER.info('mode is first')
        first_node = tictactoe.set_first_node()
    else:
        # LOGGER.info('use saved first node')
        first_node = first_node
    while count < count_limit:
        if count%10000==0:
            print('='*20)
            print(count)
            print('=' * 20)
        target_node = first_node
        cnt = 0
        depth = 0
        while True:
            # LOGGER.info('calculate ucb')
            target = tictactoe.cal_ucb(target_node)

            #
            # check whether now it is end or not for every turn
            #
            if target is None:
                # LOGGER.info('target is None')
                white_list, black_list, prev_list = tictactoe._return_each_list(target_node)
                # LOGGER.info(f'{white_list},{black_list},{prev_list}')
                if referee.check_end(white_list):
                    tictactoe.update_node(target_node, 'white')
                    count += 1
                    break
                elif referee.check_end(black_list):
                    # print('BLACK WIN!!!='*10)
                    tictactoe.update_node(target_node, 'black')
                    count += 1
                    break
                else:
                    if len(prev_list) == 9:
                        tictactoe.update_node(target_node, 'draw')
                        count += 1
                        break
                    else:
                        pass

            #
            # if it is not the end
            # (i) first, expand the tree
            # (ii) or reach for the end
            #

            if target is None:
                # LOGGER.info('next node is None')
                if target_node.total >= nth_limit:
                    # LOGGER.info('start depth process')
                    tictactoe.start_depth(target_node)
            else:
                if target == 'random':
                    # LOGGER.info('ucb result is random, so choice random one')
                    target = random.choice(target_node.next)
                    target_node = target
                else:
                    # LOGGER.info('reach for the last one')
                    target_node = target
    return first_node

def main():
    first_node = train(100000000)
    with open('./model/TTT', 'wb') as f:
        pickle.dump(first_node,f)

if __name__=='__main__':
    main()


# coding=utf-8
#
# This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis)
#
# Most of this work is copyright (C) 2013-2015 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# https://github.com/DRMacIver/hypothesis/blob/master/CONTRIBUTING.rst for a
# full list of people who may hold copyright, and consult the git log if you
# need to determine who owns an individual contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

from hypothesis.control import assume
from hypothesis.searchstrategy.strategies import SearchStrategy

class FixedStrategy(SearchStrategy):

    def __init__(self, block_size):
        self.block_size = block_size

    def do_draw(self, data):
        block = data.draw_bytes(self.block_size, self.distribution)
        assert len(block) == self.block_size
        value = self.from_bytes(block)
        assume(self.is_acceptable(value))
        return value

    def distribution(self, random, n):
        assert n == self.block_size
        for _ in range(100):
            value = self.draw_value(random)
            if self.is_acceptable(value):
                block = self.to_bytes(value)
                assert len(block) == self.block_size
                return block
        raise AssertionError(
            'After 100 tries was unable to draw a valid value. This is a bug '
            'in the implementation of %s.' % (type(self).__name__,))

    def draw_value(self, random):
        raise NotImplementedError('%s.draw' % (
            type(self).__name__,
        ))

    def to_bytes(self, value):
        raise NotImplementedError('%s.to_bytes' % (
            type(self).__name__,
        ))

    def from_bytes(self, value):
        raise NotImplementedError('%s.from_bytes' % (
            type(self).__name__,
        ))

    def is_acceptable(self, value):
        return True


#!/usr/bin/env python

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from collections import OrderedDict

class Wutp():

    def __init__(self, url):

        self.url = url

        uClient = uReq(url)
        html = uClient.read()
        uClient.close()

        self.parsed = soup(html, "html.parser")

    def convert(self, string):
        string = string.replace('\n', '').replace(u'\xa0', ' ')
        string = string.encode('ascii', 'ignore').decode('utf-8')
        return string

    def traverse(self):
        """ traverses a page and returns an ordered dict of headings as keys
        and corresponding points as values """

        content = self.parsed.findAll("table", {"class": 'mcnTextContentContainer'})

        news = OrderedDict()
        heading_list = []   # keeps tracks of added headings
        point_list = []     # keeps track of added points
        points = False      # keeps track of if a point or a heading
        current_heading = ''

        for index,c in enumerate(content):
            if points:
                all_points = c.findAll('li')

                # -- if there are lists use the text of lists
                if len(all_points) > 0:
                    for each_point in all_points:
                        nested_points = each_point.findAll('li')
                        # -- if there are nested lists get only the first span
                        # tag because the loop will grab the texts of the
                        # other lists later on
                        if len(nested_points) > 0:
                            first_tag = each_point.find('span')
                            point_list.append(first_tag.text)
                            news[current_heading].append(self.convert(first_tag.text))
                        # -- if no nested list just grab the text of the list
                        else:
                            point_list.append(each_point.text)
                            news[current_heading].append(self.convert(each_point.text))

                # -- if no lists but still after a heading, then just grab the text
                else:
                    if c.text not in point_list:
                        point_list.append(c.text)
                        news[current_heading].append(self.convert(c.text))

            # -- if it's a heading add it under headings
            if c['cellpadding'] == '18':
                if c.text not in heading_list:
                    heading_list.append(c.text)
                    current_heading = self.convert(c.text)
                    news[current_heading] = []
                # -- next iteration will be points
                points = True

            # -- need try and except for index out of range error
            try:
                if content[index+1]['cellpadding'] == '18':
                    # -- next iteration will be a heading
                    points = False
            except:
                pass

        # -- remove last item which is copyright footer
        news.popitem()

        self.news = news

        return news

    def get_headings(self):
        """ returns an array of the headings in the page """
        return [key for key in self.news]

    def get_points(self):
        ''' returns a two dimensional array with points of a heading at each
        index'''

        return [v for key,v in self.news.items()]

    @staticmethod
    def get_urls():
        """ returns a dictionary of issues with url title as key and url target
        as value """

        urls = {}

        base_url = 'http://us3.campaign-archive2.com/home/?u=4946817b18454973fb1cd7ecc&id=e81d77774f'

        uClient = uReq(base_url)
        html = uClient.read()
        uClient.close()

        archive_parsed = soup(html, "html.parser")

        all_lists = archive_parsed.findAll('li')

        for each_list in all_lists:
            link = each_list.find('a')
            link_target = link['href']
            urls[link.text]=link_target

        return urls

    @staticmethod
    def get_ordered_urls():
        """ returns an array of urls in order of date """
        urls = []

        base_url = 'http://us3.campaign-archive2.com/home/?u=4946817b18454973fb1cd7ecc&id=e81d77774f'

        uClient = uReq(base_url)
        html = uClient.read()
        uClient.close()

        archive_parsed = soup(html, "html.parser")

        all_lists = archive_parsed.findAll('li')

        for each_list in all_lists:
            link = each_list.find('a')
            link_target = link['href']
            urls.append(link_target)

        return urls

    @staticmethod
    def get_dates():
        """ returns an array of dates of published issues from newest to oldest
        """
        dates = []

        base_url = 'http://us3.campaign-archive2.com/home/?u=4946817b18454973fb1cd7ecc&id=e81d77774f'

        uClient = uReq(base_url)
        html = uClient.read()
        uClient.close()

        archive_parsed = soup(html, "html.parser")

        all_lists = archive_parsed.findAll('li')

        for each_list in all_lists:
            unformatted = each_list.text
            date = unformatted.split(' ')[0]
            dates.append(date)

        return dates

if __name__ == '__main__':
    url = 'http://us3.campaign-archive2.com/?u=4946817b18454973fb1cd7ecc&id=091f19cbd4'
    wutp = Wutp(url)
    wutp.traverse()

    print('------------------ HEADINGS ----------------------')
    print(wutp.get_headings())
    print('------------------ POINTS ------------------------')
    print(wutp.get_points())
    # print('------------------- CONTENT ----------------------')
    # print(wutp.get_content())
    # print('------------------- URLS -------------------------')
    # print(Wutp.get_urls())
    # print('-------------------- DATES ------------------------')
    # print(Wutp.get_dates())