"""soft delete

Revision ID: a67ffc6928ab
Revises: e652369d0a02
Create Date: 2023-04-12 09:31:48.602594

"""
from alembic import op
import sqlalchemy as sa

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "a67ffc6928ab"
down_revision = "e652369d0a02"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "accounts", sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True)
    )
    op.add_column(
        "issue_dependencies",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "issue_references",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "issues", sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True)
    )
    op.add_column(
        "notifications",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "oauth_accounts",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "organizations",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "pledges", sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True)
    )
    op.add_column(
        "pull_requests",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "repositories",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "user_organization_settings",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.add_column(
        "user_organizations",
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
    )
    op.drop_column("user_organizations", "status")
    op.add_column(
        "users", sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True)
    )
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("users", "deleted_at")
    op.add_column(
        "user_organizations",
        sa.Column("status", sa.INTEGER(), autoincrement=False, nullable=False),
    )
    op.drop_column("user_organizations", "deleted_at")
    op.drop_column("user_organization_settings", "deleted_at")
    op.drop_column("repositories", "deleted_at")
    op.drop_column("pull_requests", "deleted_at")
    op.drop_column("pledges", "deleted_at")
    op.drop_column("organizations", "deleted_at")
    op.drop_column("oauth_accounts", "deleted_at")
    op.drop_column("notifications", "deleted_at")
    op.drop_column("issues", "deleted_at")
    op.drop_column("issue_references", "deleted_at")
    op.drop_column("issue_dependencies", "deleted_at")
    op.drop_column("accounts", "deleted_at")
    # ### end Alembic commands ###


# Problem Link: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/

def number_steps(n):
    steps = 0

    while n > 0:

        if n % 2 == 0:
            n = n // 2
        else:
            n -= 1
        steps += 1

    return steps

print(number_steps(123))

1342. Number of Steps to Reduce a Number to Zero.py


"""
	生成带图片的二维码
"""

import qrcode
from PIL import Image
import os, sys

def get_qrcode(string, path, logo=''):
	qr = qrcode.QRCode(
		version = 2,
		error_correction = qrcode.constants.ERROR_CORRECT_H,
		box_size = 8,
		border = 1
		)

	qr.add_data(string)
	qr.make(fit=True)

	img = qr.make_image()
	img = img.convert("RGBA")

	if logo and os.path.exists(logo):
		try:
			icon = Image.open(logo)
			img_w, img_h = img.size
		except Exception as e:
			print(e)
			sys.exit(1)

		factor = 4
		# 计算 logo 的尺寸
		size_w = int(img_w/factor)
		size_h = int(img_h/factor)

		# 比较并重新设置 logo 文件的尺寸
		icon = icon.resize((size_w, size_h), Image.ANTIALIAS)

		# 计算 logo 的位置
		x = int((img_w - size_w)/2)
		y = int((img_h - size_h)/2)

		icon = icon.convert('RGBA')
		img.paste(icon, (x, y), icon)

		img.save(path)

if __name__ == '__main__':
	info = 'http://www.zhuhai.pro'
	pic_path = 'logo.png'
	logo_path = 'yn.jpg'
	get_qrcode(info, pic_path, logo_path)


# 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.

# As we unfortunately support Python 2.7, it lacks TestCase.subTest which
# is in 3.4+ or in unittest2
import unittest2
import warnings

import deprecation
from datetime import date

class Test_deprecated(unittest2.TestCase):

    def test_args_set_on_base_class(self):
        args = (1, 2, 3, 4)
        dw = deprecation.DeprecatedWarning(*args)
        self.assertEqual(dw.args, args)

    def test_removing_without_deprecating(self):
        self.assertRaises(TypeError, deprecation.deprecated,
                          deprecated_in=None, removed_in="1.0")

    def test_docstring(self):
        for test in [{"args": {},
                      "__doc__": "docstring\n\n.. deprecated::"},
                     {"args": {"deprecated_in": "1.0"},
                      "__doc__": "docstring\n\n.. deprecated:: 1.0"},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0"},
                      "__doc__": "docstring\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0."},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0",
                               "details": "some details"},
                      "__doc__": "docstring\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0. "
                                 "some details"},
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2200, 5, 20)},
                      "__doc__": "docstring\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2200-05-20."},
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2100, 3, 15),
                               "details": "some details"},
                      "__doc__": "docstring\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2100-03-15. "
                                 "some details"}]:
            with self.subTest(**test):
                @deprecation.deprecated(**test["args"])
                def fn():
                    """docstring"""

                self.assertEqual(fn.__doc__, test["__doc__"])

    def test_multiline_docstring(self):
        docstring = "summary line\n\ndetails\nand more details\n"
        for test in [{"args": {},
                      "__doc__": "%s\n\n.. deprecated::"},
                     {"args": {"deprecated_in": "1.0"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0."},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0",
                               "details": "some details"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0. "
                                 "some details"},
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2200, 11, 20)},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2200-11-20."},
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2100, 3, 15),
                               "details": "some details"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2100-03-15. "
                                 "some details"}]:
            with self.subTest(**test):
                @deprecation.deprecated(**test["args"])
                def fn():
                    """summary line

                    details
                    and more details
                    """

                self.assertEqual(fn.__doc__, test["__doc__"] % (docstring))

    def test_multiline_docstring_top(self):
        summary = "summary line"
        content = "\n\ndetails\nand more details\n"
        for test in [{"args": {},
                      "__doc__": "%s\n\n.. deprecated::%s"},
                     {"args": {"deprecated_in": "1.0"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0%s"},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0.%s"},
                     {"args": {"deprecated_in": "1.0", "removed_in": "2.0",
                               "details": "some details"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed in 2.0. "
                                 "some details%s"},#####
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2200, 11, 20)},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2200-11-20.%s"},
                     {"args": {"deprecated_in": "1.0", "removed_in": date(2100, 3, 15),
                               "details": "some details"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"
                                 "\n   This will be removed on 2100-03-15. "
                                 "some details%s"}]:
            with self.subTest(**test):
                deprecation.message_location = "top"

                @deprecation.deprecated(**test["args"])
                def fn():
                    """summary line

                    details
                    and more details
                    """

                self.assertEqual(fn.__doc__, test["__doc__"] % (summary,
                                                                content))

    def test_multiline_fallback(self):
        docstring = "summary line\n\ndetails\nand more details\n"
        for test in [{"args": {"deprecated_in": "1.0"},
                      "__doc__": "%s\n\n.. deprecated:: 1.0"}]:
            with self.subTest(**test):
                deprecation.message_location = "pot"

                @deprecation.deprecated(**test["args"])
                def fn():
                    """summary line

                    details
                    and more details
                    """

                self.assertEqual(fn.__doc__, test["__doc__"] % (docstring))

    def test_warning_raised(self):
        ret_val = "lololol"

        for test in [{"args": {},  # No args just means deprecated
                      "warning": deprecation.DeprecatedWarning,
                      "message": "method is deprecated"},
                     {"args": {"details": "do something else."},
                      "warning": deprecation.DeprecatedWarning,
                      "message": "method is deprecated. do something else."},
                     {"args": {"deprecated_in": "1.0",
                               "current_version": "2.0"},
                      "warning": deprecation.DeprecatedWarning,
                      "message": "method is deprecated as of 1.0."},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": "3.0",
                               "current_version": "2.0"},
                      "warning": deprecation.DeprecatedWarning,
                      "message": ("method is deprecated as of 1.0 "
                                  "and will be removed in 3.0.")},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": "2.0",
                               "current_version": "2.0"},
                      "warning": deprecation.UnsupportedWarning,
                      "message": "method is unsupported as of 2.0."},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": "2.0",
                               "current_version": "2.0",
                               "details": "do something else."},
                      "warning": deprecation.UnsupportedWarning,
                      "message": ("method is unsupported as of 2.0. "
                                  "do something else.")},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": date(2100, 4, 19),
                               "current_version": "2.0"},
                      "warning": deprecation.DeprecatedWarning,
                      "message": ("method is deprecated as of 1.0 "
                                  "and will be removed on 2100-04-19.")},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": date.today(),
                               "current_version": "2.0"},
                      "warning": deprecation.UnsupportedWarning,
                      "message": "method is unsupported as of %s." % date.today()},
                     {"args": {"deprecated_in": "1.0",
                               "removed_in": date(2020, 1, 30),
                               "current_version": "2.0",
                               "details": "do something else."},
                      "warning": deprecation.UnsupportedWarning,
                      "message": ("method is unsupported as of 2020-01-30. "
                                  "do something else.")}]:
            with self.subTest(**test):
                class Test(object):
                    @deprecation.deprecated(**test["args"])
                    def method(self):
                        return ret_val

                with warnings.catch_warnings(record=True) as caught_warnings:
                    warnings.simplefilter("always")

                    sot = Test()
                    self.assertEqual(ret_val, sot.method())

                self.assertEqual(len(caught_warnings), 1)
                self.assertEqual(caught_warnings[0].category, test["warning"])
                self.assertEqual(str(caught_warnings[0].message),
                                 test["message"])

    def test_DeprecatedWarning_not_raised(self):
        ret_val = "lololol"

        class Test(object):
            @deprecation.deprecated(deprecated_in="2.0",
                                    removed_in="3.0",
                                    current_version="1.0")
            def method(self):
                """method docstring"""
                return ret_val

        with warnings.catch_warnings(record=True):
            # If a warning is raised it'll be an exception, so we'll fail.
            warnings.simplefilter("error")

            sot = Test()
            self.assertEqual(sot.method(), ret_val)

class Test_fail_if_not_removed(unittest2.TestCase):

    @deprecation.deprecated(deprecated_in="1.0", current_version="2.0")
    def _deprecated_method(self):
        pass

    @deprecation.deprecated(deprecated_in="1.0", removed_in="2.0",
                            current_version="2.0")
    def _unsupported_method(self):
        pass

    def test_UnsupportedWarning_causes_failure(self):
        with self.assertRaises(AssertionError):
            @deprecation.fail_if_not_removed
            def fn():
                self._unsupported_method()

            fn()

    def test_DeprecatedWarning_doesnt_fail(self):
        @deprecation.fail_if_not_removed
        def fn():
            self._deprecated_method()

        try:
            fn()
        except AssertionError:
            self.fail("A DeprecatedWarning shouldn't cause a failure")

    @unittest2.expectedFailure
    @deprecation.fail_if_not_removed
    def test_literal_UnsupportedWarning(self):
        self._unsupported_method()

    @deprecation.fail_if_not_removed
    def test_literal_DeprecatedWarning(self):
        self._deprecated_method()


import math

def divisor_sum(num):
    if num == 1:
        return 1
    n = math.ceil(math.sqrt(num))
    total = 1
    divisor = 2
    while divisor < n:
        if num % divisor == 0:
            total += divisor
            total += num // divisor
        divisor += 1
    if n ** 2 == num:
        total += n
    return total

def is_abundant(num):
    if divisor_sum(num) > num:
        return True
    else:
        return False

abundant_nums = []
for x in range(0, 28124):
    if is_abundant(x):
        abundant_nums.append(x)
del abundant_nums[0]

sums = [0] * 28124
for x in range(0, len(abundant_nums)):
    for y in range(x, len(abundant_nums)):
        current_sum = abundant_nums[x] + abundant_nums[y]
        if current_sum <= 28123:
            if sums[current_sum] == 0:
                sums[current_sum] = current_sum

t = 0
for x in range(1, len(sums)):
    if sums[x] == 0:
        t += x

print(t)


from adapters.dbconnection import MyMongoDB
from adapters.lib import Lib
from flask import current_app
import requests
import json

class SalesServices:

    def __init__(self):
        self.lib = Lib()
        self.db = MyMongoDB()
        self.resp = {}

    def get_sales(self) -> dict:
        """
        :return: mensagem json
        """
        sale_list = []
        for sale in self.db.find_all({}, 'sale'):
            sale['_id'] = str(sale['_id'])
            porcentagem, indice = self.lib.calcular_cashback(float(sale['valor']))
            sale['cashback_porcentagem'] = porcentagem
            sale['cashback_valor'] = round((float(sale['valor']) * float(indice)), 2)
            sale_list.append(sale)

        return self.lib.render_http_json(200, 'Solicitação bem-sucedida', sale_list)

    def create_sale(self, params: dict) -> dict:
        """
        :param params: request requisicao
        :return: mensagem json
        """
        if self.db.find_one({'codigo': params['codigo']}, 'sale'):
            self.resp['http_code'] = 409
            self.resp['message'] = 'nao foi possivel cadastrar a compra, codigo ja existente'
        else:
            params['status'] = 'Em validação' if params['venda_cpf'] != '15350946056' else 'Aprovado'
            params['valor'] = float(params['valor'])
            self.db.insert(params, 'sale')
            self.resp['http_code'] = 200
            self.resp['message'] = 'Venda cadastrada com sucesso!'

        return self.lib.render_http_json(self.resp['http_code'], self.resp['message'], {})

    def update_sale(self, params: dict) -> dict:
        """
        :param params: request requisicao
        :return: mensagem json
        """
        query = {'venda_cpf': params['venda_cpf'], 'codigo': params['codigo']}
        status_check = self.db.find_one(query, 'sale')
        if status_check:
            if status_check['status'] != 'Aprovado':
                put_list = {}
                for key, value in params['campos_put'].items():
                    if value:
                        put_list[key] = value
                campos_put = put_list
                self.db.update_one(query, campos_put, 'sale')
                self.resp['http_code'] = 200
                self.resp['message'] = 'Venda atualizada com sucesso'
            else:
                self.resp['http_code'] = 400
                self.resp['message'] = 'Status da venda ja esta aprovado'
        else:
            self.resp['http_code'] = 400
            self.resp['message'] = 'Item nao encontrado'

        return self.lib.render_http_json(self.resp['http_code'], self.resp['message'], {})

    def delete_sale(self, params: dict) -> dict:
        """
        :param params: request requisicao
        :return: mensagem json
        """

        status_check = self.db.find_one(params, 'sale')
        if status_check:
            if status_check['status'] != 'Aprovado':
                self.db.find_one_and_delete(params, 'sale')
                self.resp['http_code'] = 200
                self.resp['message'] = 'Venda deletada com sucesso'
            else:
                self.resp['http_code'] = 400
                self.resp['message'] = 'Impossivel excluir item, status ja aprovado'
        else:
            self.resp['http_code'] = 400
            self.resp['message'] = 'Item nao encontrado'

        return self.lib.render_http_json(self.resp['http_code'], self.resp['message'], {})

    def get_cashback(self) -> dict:
        """
        :return: mensagem json
        """
        try:
            req = requests.get(current_app.config['URL_API'], headers={'token': current_app.config['SECRET_KEY']})
        except ConnectionError:
            self.resp['http_code'] = 500
            self.resp['message'] = 'Problema ao conectar com a API'
            self.resp['payload'] = {}
            return self.lib.render_http_json(self.resp['http_code'], self.resp['message'], self.resp['payload'])

        http_code = req.status_code
        if http_code != 200:
            self.resp['http_code'] = http_code
            self.resp['message'] = 'Solicitação mal sucedida'
            self.resp['payload'] = {}
        else:
            ret = json.loads(req.text)
            self.resp['http_code'] = 200
            self.resp['message'] = 'Solicitação bem sucedida!'
            self.resp['payload'] = ret['body']

        return self.lib.render_http_json(self.resp['http_code'], self.resp['message'], self.resp['payload'])


#coding:utf-8
from model.base import BaseModel
import tensorflow as tf
from nn.layer import Embedding,Dropout
from nn.recurrent import BiLSTM
from collections import OrderedDict
from nn.layer import MultiHeadAttention,create_padding_mask
class CharCNN(BaseModel):
    def __init__(self, vocab, pretrained_word_embedding=None,
             word_embedding_size=100,
             rnn_hidden_size = 64,
             dropout_keep_prob=0.9, num_class=3):
        super(CharCNN, self).__init__(vocab)
        self.keep_prob = dropout_keep_prob
        self.num_class = num_class
        self.word_embedding_size = word_embedding_size
        self.pretrained_word_embedding = pretrained_word_embedding
        self.word_embedding_trainable = True
        self.rnn_hidden_size =rnn_hidden_size
        self.kernel_size = [3,3]
        self._build_graph()

    def _build_graph(self):
        self.x = tf.placeholder(tf.int32, [None, None])
        self.x_len = tf.placeholder(tf.int32,[None])
        self.y = tf.placeholder(tf.int32, [None])
        self.training = tf.placeholder_with_default(False, shape=(), name='is_training')
        self.kernel_initializer = tf.truncated_normal_initializer(stddev=0.05)

        self.filter_sizes = [5,5,3,3,3,3]
        self.num_filters = 256

        word_embedding = Embedding(pretrained_embedding=self.pretrained_word_embedding,
                                   embedding_shape=(self.vocab.get_char_vocab_size() + 1, self.word_embedding_size),
                                   trainable=self.word_embedding_trainable)

        input_x = word_embedding(self.x)

        # pos_embedding = Embedding(pretrained_embedding=None,
        #                           embedding_shape=(self.pos_vocab_size, self.pos_embedding_size))
        #
        # input_x_pos = pos_embedding(self.pos_feature)
        #
        # feature_x = tf.one_hot(self.in_name_feature, depth=2)
        # input_x = tf.concat([input_x, feature_x], axis=-1)
        # input_x = tf.concat([input_x, input_x_pos], axis=-1)
        dropout = Dropout(self.keep_prob)
        input_x = dropout(input_x, self.training)

        input_x = tf.expand_dims(input_x,axis=-1)
        print(input_x.shape)

        # ============= Convolutional Layers =============
        with tf.name_scope("conv-maxpool-1"):
            conv1 = tf.layers.conv2d(
                input_x,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[0], self.word_embedding_size],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            pool1 = tf.layers.max_pooling2d(
                conv1,
                pool_size=(3, 1),
                strides=(3, 1))
            pool1 = tf.transpose(pool1, [0, 1, 3, 2])

        with tf.name_scope("conv-maxpool-2"):
            conv2 = tf.layers.conv2d(
                pool1,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[1], self.num_filters],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            pool2 = tf.layers.max_pooling2d(
                conv2,
                pool_size=(3, 1),
                strides=(3, 1))
            pool2 = tf.transpose(pool2, [0, 1, 3, 2])

        with tf.name_scope("conv-3"):
            conv3 = tf.layers.conv2d(
                pool2,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[2], self.num_filters],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            conv3 = tf.transpose(conv3, [0, 1, 3, 2])

        with tf.name_scope("conv-4"):
            conv4 = tf.layers.conv2d(
                conv3,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[3], self.num_filters],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            conv4 = tf.transpose(conv4, [0, 1, 3, 2])

        with tf.name_scope("conv-5"):
            conv5 = tf.layers.conv2d(
                conv4,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[4], self.num_filters],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            conv5 = tf.transpose(conv5, [0, 1, 3, 2])

        with tf.name_scope("conv-maxpool-6"):
            conv6 = tf.layers.conv2d(
                conv5,
                filters=self.num_filters,
                kernel_size=[self.filter_sizes[5], self.num_filters],
                kernel_initializer=self.kernel_initializer,
                activation=tf.nn.relu)
            pool6 = tf.layers.max_pooling2d(
                conv6,
                pool_size=(3, 1),
                strides=(3, 1))
            pool6 = tf.transpose(pool6, [0, 2, 1, 3])
            print(pool6.get_shape().as_list())
            h_pool = tf.reshape(pool6, [-1, self.num_filters])
        print(h_pool.shape)

        print(input_x)
        fc1_layer = tf.keras.layers.Dense(128,activation=tf.nn.relu)
        fc1_out = fc1_layer(h_pool)
        # fc1_out = dropout(fc1_out,self.training)
        # fc2_layer = tf.keras.layers.Dense(1024,activation=tf.nn.relu)
        # fc2_out = fc2_layer(fc1_out)

        # encoder1 = BiLSTM(self.rnn_hidden_size,name='layer_1')
        # input_x,_ = encoder1(input_x,self.x_len)
        #
        # encoder2 = BiLSTM(self.rnn_hidden_size,name='layer_2')
        # input_x,_ = encoder2(input_x,self.x_len)
        # print(input_x.shape)
        # merge = tf.reshape(input_x,[tf.shape(input_x)[0],-1])

        # avg_pool = tf.reduce_mean(input_x,axis=1)
        # avg_max =  tf.reduce_max(input_x,axis=1)
        #
        # merge = tf.concat([avg_pool,avg_max],axis=1)
        # print(merge.shape)
        # h_conc_linear1 = tf.keras.layers.Dense(200,use_bias=False,activation=tf.nn.relu)(merge)
        # h_conc_linear2 = tf.keras.layers.Dense(200,use_bias=False,activation=tf.nn.relu)(merge)
        # merge = merge+h_conc_linear1+h_conc_linear2

        #
        # dense = tf.keras.layers.Dense(16,activation=tf.nn.relu)
        # merge = dense(merge)
        # merge = dropout(merge, self.training)
        self.logits = tf.keras.layers.Dense(self.num_class,activation=None)(fc1_out)
        self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits,labels=self.y))
        global_step = tf.train.get_or_create_global_step()

        self.input_placeholder_dict = OrderedDict({
            "char_ids": self.x,
            "labels": self.y,
            "text_len":self.x_len,
            "training": self.training
        })

        self.output_variable_dict = OrderedDict({
            "predict": tf.argmax(self.logits, axis=1)
        })

        # 8. Metrics and summary
        with tf.variable_scope("train_metrics"):
            self.train_metrics = {
                'loss': tf.metrics.mean(self.loss)
            }

        self.train_update_metrics = tf.group(*[op for _, op in self.train_metrics.values()])
        metric_variables = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="train_metrics")
        self.train_metric_init_op = tf.variables_initializer(metric_variables)

        with tf.variable_scope("eval_metrics"):
            self.eval_metrics = {
                'loss': tf.metrics.mean(self.loss)
            }

        self.eval_update_metrics = tf.group(*[op for _, op in self.eval_metrics.values()])
        metric_variables = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="eval_metrics")
        self.eval_metric_init_op = tf.variables_initializer(metric_variables)

        tf.summary.scalar('loss', self.loss)
        self.summary_op = tf.summary.merge_all()

    def compile(self, optimizer, initial_lr, clip_norm=5.0):
        self.optimizer = optimizer(initial_lr)
        grads, vars = zip(*self.optimizer.compute_gradients(self.loss))
        gradients, _ = tf.clip_by_global_norm(grads, clip_norm=clip_norm)
        self.train_op = self.optimizer.apply_gradients(zip(gradients, vars))


"""	Questão: Lista E 02
	Descrição: Leia dois valores e uma das seguintes operações a serem executadas
	(codificadas da seguinte forma: 1 – Adição, 2 – Subtração, 3 – Multiplicação e 4 – Divisão).
	Calcule e escreva o resultado dessa operação sobre os dois valores lidos.
"""

def main():
	# entrada
	valor1 = float(input("Digite um valor: "))
	valor2 = float(input("Digite outro valor: "))
	operacao = int(input("Escolha uma das opções abaixo\n1 - Adição\n2 - Subtração\n3 - Multiplicação\n4 - Divisão\n\nDigite a operação desejada: "))

	# calculos, operacoes, processamento
	resultado = operacacao(valor1, valor2, operacao)

	# saida
	print(resultado)

def operacacao(valor1, valor2, operacao):
	if operacao == 1:
		return valor1 + valor2
	elif operacao == 2:
		return valor1 - valor2
	elif operacao == 3:
		return valor1 * valor2
	elif operacao == 4:
		return valor1 / valor2
	else:
		return "Operação inválida"

if __name__ == '__main__':
	main()


import sys
import matplotlib.colors

target_color_hex = str(sys.argv[1])  # example input: "#44ca23"
main_color_hex = "#4169e1"

target_color_hsv = matplotlib.colors.rgb_to_hsv(matplotlib.colors.to_rgb(target_color_hex))
main_color_hsv = matplotlib.colors.rgb_to_hsv(matplotlib.colors.to_rgb(main_color_hex))

colors = {
    "$c1": "#06102b",
    "$c2": main_color_hex,
    "$c3": "#2f4893",
    "$c4": "#23366d",
    "$c5": "#2f4893",
    "$c6": "#3759bf",
}

new_colors = {}
for color in colors:
    selected_color_hsv = matplotlib.colors.rgb_to_hsv(matplotlib.colors.to_rgb(colors[color]))
    diff = selected_color_hsv - main_color_hsv
    new_color_hsv = target_color_hsv + diff
    if new_color_hsv[2] < 0.05:
        print(f"Warning: The light value ({round(new_color_hsv[2] * 100) / 100}) was too low. Now setting it to 0.05. The color relations are not kept!")
        new_color_hsv[2] = 0.05
    if new_color_hsv[1] > 1:
        print(f"Warning: The saturation value ({round(new_color_hsv[1] * 100) / 100}) was too high. Now setting it to 1. The color relations are not kept!")
        new_color_hsv[1] = 1
    new_colors[color] = matplotlib.colors.to_hex(matplotlib.colors.hsv_to_rgb(new_color_hsv))

dark_filter_name = "$f1"
dark_filter_hue = 191

def get_dark_filter(hue):
    return f"invert(90%) sepia(50%) saturate(1000%) hue-rotate({hue}deg) brightness(98%) contrast(92%)"

diff = target_color_hsv - main_color_hsv
new_dark_filter_hue = dark_filter_hue + diff[0] * 360

with open('colors.scss', 'w') as f:
    for color in new_colors:
        f.write(f"{color}: {new_colors[color]};\n")
    f.write(f"{dark_filter_name}: {get_dark_filter(new_dark_filter_hue)};")


"""DiskImage class for handling Amstrad CPC and Spectrum +3 disk images

The specification of the Amstrad CPC disk image format is available at the
following URL:
<https://www.cpcwiki.eu/index.php/Format:DSK_disk_image_file_format>

Author: Nicholas Campbell
Last updated: 2020-08-06
"""

import os

class DiskImage():
	# Exceptions for this class
	class FileFormatError(Exception):
		def __init__(self, message):
			self.message = message
		def __str__(self):
			return(self.message)

	class InvalidTrackError(Exception):
		def __init__(self, track):
			self.track = track
		def __str__(self):
			return('Track {0} does not exist on this disk image'.format(
				self.track))

	class TrackDataError(Exception):
		def __init__(self, track):
			self.track = track
		def __str__(self):
			return('Track {0} has no data'.format(self.track))

	class InvalidSectorError(Exception):
		def __init__(self, track, sector):
			self.track = track
			self.sector = sector
		def __str__(self):
			return('Sector 0x{0:02X} not found in track {1}'.format(
				self.sector, self.track))

	class SectorWriteError(Exception):
		def __init__(self, track, sector, message):
			self.track = track
			self.sector = sector
			self.message = message
		def __str__(self):
			error_message = ('Unable to write data to '
				+ 'track {0}, sector 0x{1:0X}').format(self.track, self.sector)
			if (self.message != ''):
				error_message += '; ' + self.message
			return(error_message)

	@property
	def number_of_tracks(self):
		return self.header[0x30]

	# Methods

	def __init__(self, filepath):
		"""Create a new disk image object using a disk image file.

Parameters:
    filepath (str): The disk image file to read.
        """
		# Read the disk information black at the beginning of the disk image
		# file, which is 256 bytes long
		with open(filepath, 'rb') as dsk_file:
			self.header = bytearray(dsk_file.read(256))

			# Amstrad CPC extended disk image files are identified by the ASCII
			# string 'EXTENDED CPC DSK File\r\n' in the first 23 bytes of the
			# file
			if self.header[0:23].decode('latin_1') != \
				'EXTENDED CPC DSK File\r\n':
				raise DiskImage.FileFormatError(('{0} is not an Amstrad CPC '
					+ 'disk image file').format(repr(filepath)))

			# The disk image file is valid, so read the rest of the file
			else:
				self.track_info_block = bytearray(dsk_file.read(
					(os.stat(filepath).st_size) - 256))

	def _validate_track_number(self, track):
		"""Validate a track number that is passed to various methods in this class.

Parameters:
    track (int): The track number.

Raises:
    TypeError: The track number is not specified as an integer.
    DiskImage.InvalidTrackError: There is no track with this number in this disk
        image.
		"""
		if type(track) is not int:
			raise TypeError('Track number must be an integer')
		elif track not in range(0, self.number_of_tracks):
			raise DiskImage.InvalidTrackError(track)

	def get_track_info_block_size(self, track):
		"""Return the size in bytes of the track information block in the disk image file
that corresponds to the specified track, including the header (which is 0x100
bytes in length).

Parameters:
    track (int): The track number.

Returns:
    int: The size of the track information block in bytes.

Raises:
    DiskImage.TrackDataError: There is no data for this track, or the track
        information block contains corrupted data.
		"""
		self._validate_track_number(track)
		return (self.header[0x34 + track] * 0x100)

	def get_track_info_block_offset(self, track):
		"""Return the offset of the track information block in the disk image file that
corresponds to the specified track.

Parameters:
    track (int): The track number.

Returns:
    int: The offset of the track information block.

Raises:
    DiskImage.TrackDataError: There is no data for this track.
		"""
		self._validate_track_number(track)
		if self.get_track_info_block_size(track) == 0:
			raise DiskImage.TrackDataError(track)

		track_info_block_offset = 0
		for i in range(0, track):
			track_info_block_offset += \
				self.get_track_info_block_size(i)
		return track_info_block_offset

	def get_sector_info(self, track):
		"""Return information about the sectors of the specified track.

Parameters:
    track (int): The track number.

Returns:
    list: A list of tuples containing information about each sector of the
        track (track/cylinder number (C), side/head number (H), sector ID (R),
		sector size (N), FDC status register 1 (ST1), FDC status register 2
		(ST2), actual size of sector data in disk image file).
		"""
		self._validate_track_number(track)

		# If there is no data for the specified track, then return an empty
		# list
		if self.get_track_info_block_size(track) == 0:
			return []

		# Get the number of sectors in this track
		number_of_sectors = self.track_info_block[
			self.get_track_info_block_offset(track) + 0x15]

		# Read information about each sector from the track information block
		# and store it in the sector information list
		sector_info = [None] * number_of_sectors
		track_info_block_ptr = self.get_track_info_block_offset(track) + 0x18

		for sector in range(0, number_of_sectors):
			sector_info[sector] = \
				(self.track_info_block[track_info_block_ptr],
				self.track_info_block[track_info_block_ptr + 1],
				self.track_info_block[track_info_block_ptr + 2],
				self.track_info_block[track_info_block_ptr + 3],
				self.track_info_block[track_info_block_ptr + 4],
				self.track_info_block[track_info_block_ptr + 5],
				(self.track_info_block[track_info_block_ptr + 6]) +
					(self.track_info_block[track_info_block_ptr + 7] * 0x100)
				)
			track_info_block_ptr += 8
		return sector_info

	def read_sector(self, track, sector):
		"""Read a sector from the disk image.

Parameters:
    track (int): The track number where the sector is located.
    sector (int): The sector ID number to read.

Returns:
    bytearray: The data that was read from the specified sector.

Raises:
	DiskImage.TrackDataError: The specified track does not contain any data.
    DiskImage.InvalidSectorError: The sector ID number does not exist on the
        specified track.
        """
		self._validate_track_number(track)
		if self.get_track_info_block_size(track) == 0:
			raise DiskImage.TrackDataError(track)

		sector_info = self.get_sector_info(track)
		sector_block_ptr = self.get_track_info_block_offset(track) + 0x100

		# Read the sector information list
		for i in range(0,len(sector_info)):
			# If the sector number is found, then read and return the data
			# block for that sector
			if sector_info[i][2] == sector:
				return self.track_info_block[sector_block_ptr:
					(sector_block_ptr + 128 * 2**(sector_info[i])[3])]

			# If the sector number is not found, move the sector block pointer
			# to the data block for the next sector
			sector_block_ptr += (sector_info[i])[6]

		# If the sector number does not exist in this track, then raise an
		# exception
		raise DiskImage.InvalidSectorError(track, sector)

	def write_sector(self, track, sector, data):
		"""Write a sector to the disk image.

Parameters:
    track (int): The track number where the sector is located.
    sector (int): The sector ID number to read.
    data (bytearray): The data to write to the sector.

Raises:
    DiskImage.TrackDataError: The specified track does not contain any data.
    DiskImage.SectorWriteError: The data could not be written to the specified
        sector.
    DiskImage.InvalidSectorError: The sector ID number does not exist on the
        specified track.
        """
		self._validate_track_number(track)
		if self.get_track_info_block_size(track) == 0:
			raise DiskImage.TrackDataError(track)

		sector_info = self.get_sector_info(track)
		sector_block_ptr = self.get_track_info_block_offset(track) + 0x100

		# Read the sector information list
		for i in range(0,len(sector_info)):
			# If the sector number is found, then try to write the block of
			# data to the sector
			if sector_info[i][2] == sector:
				# Check that the size of the block of data to write to the
				# sector matches its size on the disk image; if it doesn't,
				# then raise an exception
				if len(data) != sector_info[i][6]:
					raise DiskImage.SectorWriteError(track, sector,
						('length of data to write ({0:,} bytes)'.format(len(data))
						+ ' does not match actual size of sector data in disk '
						+ 'image ({0:,} bytes)'.format(sector_info[i][6])))
				else:
					self.track_info_block[sector_block_ptr:
						sector_block_ptr+len(data)] = data
					return

			# If the sector number is not found, move the sector block pointer
			# to the data block for the next sector
			sector_block_ptr += (sector_info[i])[6]

		# If the sector number does not exist in this track, then raise an
		# exception
		raise DiskImage.InvalidSectorError(track, sector)


# Tools for dealing with the ORFEO files in the CEFC data set.

import os.path
from os import walk
from sentence import Sentence
from .text import read_text

def find_orfeo_files(directory):
    """From a path generates a list of all files matching ORFEO file extension."""
    result = []

    for (dirpath, dirnames, filenames) in walk(directory):
        for file in filenames:
            if file[-5:].lower() == "orfeo":
                result.append(os.path.join(dirpath, file))

    return result

def read_sentences(file, speakers):
    """Reads sentence data from file and parses it with the speaker data."""
    bulk_data = read_text(file)

    sentence_indecies = []
    sentences = []

    # Use these for index bounds of each block of sentence data.
    x = -1
    y = -1

    # Find the beginning and end indecies for each sentence block.
    for idx in range(0, len(bulk_data)):
        if len(bulk_data[idx]) > 0:
            if bulk_data[idx][0] == '1':
                x = idx
        else:
            y = idx

        if x > -1 and y > -1:
            sentence_indecies.append((x, y))
            x = -1
            y = -1

    # Use the bounds of each sentence block to extract each sentence.
    # Making the assumption that while each word has a speaker ID, all sentence
    # blocks are uttered by the same person.  This may not be true, but we
    # don't really care that much about the speaker data.
    for block in sentence_indecies:
        data_block = bulk_data[block[0]:block[1]]

        # Lets extract the speaker information.
        speaker_ID = data_block[0][-1]
        this_speaker = None
        for speaker in speakers:
            if speaker_ID == speaker.sid:
                this_speaker = speaker
                break

        this_word_list = []
        this_sentence_list = []
        for data in data_block:
            word_data = data.split('\t')
            this_word_list.append((word_data[1], word_data[3]))
            this_sentence_list.append(word_data[1])

        sentence_text = " ".join(this_sentence_list)

        this_sentence = Sentence(this_speaker, sentence_text, this_word_list)
        this_sentence.lem()

        sentences.append(this_sentence)

    return sentences


"""
ラムダ出力
ここまでは、ラムダ関数が入力として渡されたときの動作を見てきました。
ラムダ関数が出力として返される場合を見てみましょう。

関数を作って、それを高階関数の中で返すことができます。
もし、入力が何であっても常に返される関数が同じ場合、通常の関数を定義すればよいのですから、
この高階関数の処理は全く意味をなさないことになってしまいます。
"""

# この関数は通常の関数として定義すればよいので、これは無意味です。
# どのような入力であっても、常に同じ関数が返されます。

def lambdaHelloWorld(randomInput):
    print(randomInput + " was passed in but this function always \
     return the same lambda function")
    return lambda: 'Hello World'

def helloWorld():
    return 'Hello World'

# print(helloWorld())
# print((lambdaHelloWorld("laliluleko")()))

# Lambda Machine

import math
import random

class LambdaMachine:
    def __init__(self):
        self.lambdaStorage = []
        # バンドらはキーと値のペアを含み、キーは関数名、値は格納された
        # 関数のインデックスになります。ラムダ関数はこのlamdbaStorageに
        # 格納されます。
        self.handlers = {}
        self.counter = 0

    # キーに基づいて、ラムダ関数をデータ構造に挿入します。
    def insert(self, key, fLambda):
        if key in self.handlers:
            self.lambdaStorage[self.handlers[key]] = fLambda
            return
        else:
            self.lambdaStorage.append(fLambda)
            self.handlers[key] = len(self.lambdaStorage) - 1
    # 取得
    def retrive(self, key):
        if len(self.lambdaStorage) > 0 and key in self.handlers:
            return self.lambdaStorage[self.handlers[key]]
        else:
            None

    def roundRobinRetrieve(self):
        l = len(self.lambdaStorage)
        if l == 0:
            return None
        index = self.counter % l
        print("Round-Robin retrieval at index: " + str(index))
        self.counter += 1
        return self.lambdaStorage[index]

    def randomRetrieve(self):
        l = len(self.lambdaStorage)
        if l == 0:
            return None
        ran = math.floor(random.random() * l)
        print("Random retrieval at index: " + str(ran))
        return self.lambdaStorage[ran]

lambdaMachine = LambdaMachine()

# 2つの入力と共に、構造体ラムダに挿入します
lambdaMachine.insert("pythagora", lambda a, b: math.sqrt(a * a + b * b))
lambdaMachine.insert("addition", lambda x, y: x + y)
lambdaMachine.insert("subtraction", lambda x, y: x - y)
lambdaMachine.insert("multiplication", lambda x, y: x * y)
lambdaMachine.insert("division", lambda x, y: x / y)
lambdaMachine.insert("noises", lambda x, y: str(x) + "-DUM-DUM-DUM-DUM-" + str(y))

print(lambdaMachine.retrive("pythagora"))
print(lambdaMachine.retrive("pythagora")(3, 4))
print(lambdaMachine.retrive("multiplication"))
print(lambdaMachine.retrive("multiplication")(4, 10))
print(lambdaMachine.retrive("noises"))
print(lambdaMachine.retrive("noises")(10, 15))

x = 1
y = 10

print(lambdaMachine.randomRetrieve()(x, y))
print(lambdaMachine.randomRetrieve()(x, y))
print(lambdaMachine.randomRetrieve()(x, y))
print(lambdaMachine.randomRetrieve()(x, y))
print(lambdaMachine.randomRetrieve()(x, y))
print(lambdaMachine.randomRetrieve()(x, y))

# ラウンドロビンによる取得
print(lambdaMachine.roundRobinRetrieve()(x, y))
print(lambdaMachine.roundRobinRetrieve()(x, y))
print(lambdaMachine.roundRobinRetrieve()(x, y))
print(lambdaMachine.roundRobinRetrieve()(x, y))
print(lambdaMachine.roundRobinRetrieve()(x, y))
print(lambdaMachine.roundRobinRetrieve()(x, y))

上級_ラムダ関数_ラムダ出力 .py


import sys
sys.stdin = open('input.txt', 'r')

dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

T = int(input())

for tc in range(1, T+1):
    N = int(input())

    maze = [input() for _ in range(N)]
    sx = sy = ex = ey = 0
    for i in range(N):
        for j in range(N):
            if maze[i][j] == '2':
                sx, sy = i, j
            elif maze[i][j] == '3':
                ex, ey = i, j

    visited = [[0]*N for _ in range(N)]
    Q = [[sx, sy]]
    visited[sx][sy] = 1

    while Q:
        x, y = Q.pop(0)
        if x == ex and y == ey:
            break
        for i in range(4):
            tx, ty = x + dx[i], y + dy[i]

            if tx < 0 or tx == N or ty < 0 or ty == N: continue
            if maze[tx][ty] == '1' or visited[tx][ty]: continue
            visited[tx][ty] = visited[x][y] + 1
            Q.append([tx, ty])

    if visited[ex][ey]: visited[ex][ey] -= 2
    print(visited[ex][ey])

알고리즘/알고리즘/강의내용/0903/문제/미로의 거리(5105)/first_try.py


import nltk
from utils.file_io_helper import read_books_json
from utils.file_io_helper import read_book_nlp_html
import xml.etree.ElementTree as ET
import spacy
from collections import defaultdict
import re

def find_matched_block(character_pairs,sentences):
    matched = defaultdict(list)
    for sent in sentences:
        sent = str(sent)

        for pair in character_pairs:
            search_go_to = pair
            m = ' .*? '.join(x for x in search_go_to)
            if re.search(m, str(sent)):
                matched[pair].append(sent)
    return matched

# def co_occurence(sentences):
#     pair_sent = defaultdict(list)
#     for sent in sentences:
#         curr = nlp(sent.text)
#         curr_name = []
#         for ent in curr.ents:
#             if ent.label_ == 'PERSON':
#                 name = ent.text
#                 size = len(name)
#                 if name[size/2]==' ' and name[:size/2] ==name[size/2+1:]:
#                     name = name[:size/2]
#                 curr_name.append(name)

#         if curr_name:
#             pair = []
#             prev = None
#             for name_ in curr_name:
#                 if (not prev or prev!=name_) and len(pair)<2:
#                     pair.append(name_)
#                     prev = name_
#             pair = tuple(sorted(pair))
#         if len(pair)==2:
#             pair_sent[pair].append(sent.text)
#     return pair_sent

def sentence_out(sent):
    return ' '.join([w['word'] for w in sent['tokens']])

def co_occurence(doc):
    pair_sent = defaultdict(list)
    for sentence in doc.sentences:

        curr_name = []
        prev_ner = None
        for ent in sentence['tokens']:
            if ent['ner'] == 'PERSON':
                name = ent['word']
                size = len(name)
                if not prev_ner or prev_ner == name:
                    prev_ner = name
                else:
                    prev_ner += ' ' + name
            else:
                if prev_ner:
                    curr_name.append(prev_ner)
                prev_ner = None
        if prev_ner:
            curr_name.append(prev_ner)

        if curr_name:
            pair = []
            prev = None
            for name_ in curr_name:
                if (not prev or prev!=name_) and len(pair)<2:
                    pair.append(name_)
                    prev = name_
            pair = tuple(sorted(pair))

        if len(pair)==2:
            pair_sent[pair].append((sentence_out(sentence),sentiment[sentence['id']]))
    return pair_sent

def xml_analysis(xml_file,limit = 1):
    translate = {'Negative':-1,'Positive':1,'Neutral':0}
    xml = open(xml_file).read()
    annotated_text = A(xml)

    tree = html.fromstring(xml)
    sentiment = tree.xpath('//sentence/@sentiment')
    sentiment = [translate[key] for key in sentiment]

    pair_dict =co_occurence(annotated_text)

    avg_sent = lambda x:np.mean([y[1] for y in x])

    G = nx.Graph()
    for key,value in pair_dict.iteritems():
        appear = len(value)
        if appear>limit:
            G.add_edge(key[0],key[1], label='Connect:{},Sentiment:{}'.format(appear,avg_sent(value) ))

#     nxpd.draw(G, show='ipynb')

    return G

if __name__ == '__main__':
    # clean = read_book_nlp_html()
    # with open('../data/summary-tag/1.1984_tag.txt','w') as f:
    #     f.write(clean)

    clean_full = read_book_nlp_html('../tools/book-nlp/data/output/1.1984_full.txt/1.1984_full.txt.html')
    with open('../data/summary-tag/1.1984_full_tag.txt','w') as f:
        f.write(clean_full)

    # books = read_books_json('../data/SparkNotes_book_summary.json')
    # book0 = books[0]
    # nlp = spacy.load('en')

    # doc = nlp(clean)
    # sentences = list(doc.sents)
    # character_pairs = [('Winston','Julia'),('Winston',u'O\u2019Brien'.encode('utf-8'))]
    # block_1 = find_matched_block(character_pairs,sentences)
    # tree = ET.parse('../data/summary-parse/1.1984_summary.txt.xml')
    # root = tree.getroot()
    # pair_test = co_occurence(doc.sents)


import os
import time
import subprocess
from slackclient import SlackClient

# starterbot's ID as an environment variable
BOT_ID = os.environ.get("BOT_ID")

# constants
AT_BOT = "<@" + BOT_ID + ">"
BOT_CHANNEL = os.environ.get("BOT_CHANNEL")
UAT_API_TEST = "uat_api_test"

slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))

def uat_api_test(channel):
    """
        Proceed uat_api_test and send report to BOT_CHANNEL channel in Slack.
        BOT_CHANNEL is config at startbot.sh in local server.
    """

    slack_client.api_call("chat.postMessage", channel=channel, text="Running test...", as_user=True)
    bashCommand = "./uatapitest.sh || true"
    subprocess.check_output(['bash','-c', bashCommand])
    f = open('log.txt', 'r')
    newf = open('Slack.msg', 'w')
    log = f.readlines()
    f.close()
    for line in log:
        if 'testsuite SUCCEEDED' in line or 'testsuite FAILED' in line:
            line = line.replace("[91m", ":x:")
            line = line.replace("[92m", ":white_check_mark:")
            line = line.replace("[0m", "")
            newf.write(line)

    f = open('log.txt', 'r')
    log = f.read()
    f.close()
    response = "```" + log + "```"
    slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True)

    newf = open('Slack.msg', 'r')
    response = newf.read()
    slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True)

uat_api_test(BOT_CHANNEL)


from .models import Permission, Permission_types
from identifiers.models import Identifier
from identifiers.serializers import IdentifierSerializer, PermIdentSerializer
from doors.serializers import DoorSerializer
from rest_framework import serializers

class PermissionTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission_types
        fields = (
            'id', 'name', 'created_date', 'updated_date'
        )

class PermissionSerializer(serializers.ModelSerializer):
#    permission = PermissionTypeSerializer()
#    door = DoorSerializer()
#    identifier = IdentifierSerializer()
    class Meta:
        model = Permission
        fields = (
            'id', 'identifier', 'door', 'permission', 'created_date', 'updated_date'
        )

class IdentSerializer(serializers.ModelSerializer):

    identifier = PermIdentSerializer()
    class Meta:
        model = Permission
        fields = (
            'id','identifier'
        )


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime

Base = declarative_base()

class Sample(Base):
    __tablename__ = 'samples'

    id              = Column(Integer, primary_key=True)
    lab_tech        = Column(String)
    receive_time    = Column(DateTime)

    def __repr__(self):
        return "<Sample(%r, %r)>" % (self.lab_tech, self.receive_time)

class Cell(Base):
    __tablename__ = 'cells'

    cell_id         = Column(Integer)


"""
A trie is a special data structure used to store string that can be
visualized like a graph. Tries are generally used on group of string
rather that a single string.

Use: If you have long string of word tries can help in storing string
in a single tree represent all of the words.

Modified by: Rahul Prajapati
Source: https://www.youtube.com/results?search_query=trie+in+python
"""

class TrieDict:

    def __init__(self):
        self.root = {"*": "*"}

    def insert_word(self, word):

        c_node = self.root

        for letter in word:
            if letter not in c_node:
                c_node[letter] = {}
            c_node = c_node[letter]

        c_node["*"] = "*"

    def has_word(self, word):

        c_node = self.root

        for letter in word:
            if letter not in c_node:
                return False
            c_node = c_node[letter]

        return "*" in c_node

    def has_prefix(self, word):

        c_node = self.root

        for letter in word:
            if letter not in c_node:
                return False
            c_node = c_node[letter]

        return True

class TrieNode:

    def __init__(self, letter):
        self.letter = letter
        self.is_end_of_word = False
        self.children = {}

class Trie:

    """
    Time Complexity:

    Insert: O(M)
    Search: O(M)

    M is the length of word
    """
    def __init__(self):
        self.root = TrieNode('*')

    def insert_word(self, word):
        c_node = self.root
        for letter in word:
            if letter not in c_node.children:
                c_node.children[letter] = TrieNode(letter)
            c_node = c_node.children[letter]
        c_node.is_end_of_word = True

    def has_word(self, word):
        if word == "":
            return True

        c_node = self.root
        for letter in word:
            if letter not in c_node.children:
                return False
            c_node = c_node.children[letter]

        return c_node.is_end_of_word == True

    def has_prefix(self, word):

        if word == "":
            return True

        c_node = self.root
        for letter in word:
            if letter not in c_node.children:
                return False
            c_node = c_node.children[letter]
        return True

words = ["wait", "waiter", "shop", "shopper"]

trie = TrieDict()

trie.insert_word(words[0])
trie.insert_word(words[1])
trie.insert_word(words[2])
trie.insert_word(words[3])

print(trie.root)

print(trie.has_word("wait"))
print(trie.has_word(""))
print(trie.has_prefix("wa"))
print(trie.has_prefix("waitert"))
print(trie.has_prefix("we"))


#encoding=utf-8
import sys
from functools import partial
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView, QWebSettings
from Analysis.DomAnalysis import DomAnalysis

class Crawler(object):

    def __init__(self, app, check_urls=[]):
        self.app = app
        self.page_url = ''
        self.result = check_urls
        self.browsers = dict()

    def _path(self, url):
        return url[0:url.rfind("/")]

    def _load_finished(self, browser_id, ok):
        #print ok, browser_id
        web_view, _flag = self.browsers[browser_id]
        self.browsers[browser_id] = (web_view, True)

        frame = web_view.page().mainFrame()
        dom = unicode(frame.toHtml()).encode('utf-8','ignore')
        self.result += parse(dom, self.page_url)

        web_view.loadFinished.disconnect()
        web_view.stop()

        if all([closed for bid, closed in self.browsers.values()]):
            #print 'all finished'
            #for u in self.result:
                #print u
            #print "exequit"
            self.app.quit()

    def start(self, urls):
        for browser_id, url in enumerate(urls):
            self.page_url = self._path(url)
            web_view = QWebView()
            web_view.settings().setAttribute(QWebSettings.AutoLoadImages,
                                             False)
            loaded = partial(self._load_finished, browser_id)
            web_view.loadFinished.connect(loaded)
            web_view.load(QUrl(url))
            self.browsers[browser_id] = (web_view, False)
        return self.result

def parse(dom, url):
    DomHandle = DomAnalysis(dom, url)
    return DomHandle.GetURL()

if __name__ == '__main__':
    url = ["http://demo.aisec.cn/demo/aisec/"]
    app = QApplication(sys.argv)
    crawler = Crawler(app)
    crawler.start(url)
    app.exec_()


from gpiozero import LED
from time import sleep
#from picamera import PiCamera
from picamerax import PiCamera
import subprocess
import numpy as np
import warnings #picamera uses this

#Turn off any warnings produced by picamera
warnings.simplefilter("ignore")

#Camera Capture Resolution
#  camera.resolution = (CAMERA_REZ_HORZ,CAMERA_REZ_HORZ)
#  Examples:
#    camera.resolution = (2592, 1944)
#    camera.resolution = (1920, 1080)
#    camera.resolution = (1296, 972)
#    camera.resolution = (640, 480)
CAMERA_REZ_HORZ = 2592
CAMERA_REZ_VERT = 1944
#CAMERA_REZ_HORZ = 640
#CAMERA_REZ_VERT = 480

class active_led:
    """ Class that manages Pi' onboard ACTivity LED """
    def led_on_off(self,value):
        """
          run the shell command:
          echo $value | sudo tee /sys/class/leds/led0/brightness
        """
        filename = "/sys/class/leds/led0/brightness"
        p1 = subprocess.Popen(["echo",value], stdout=subprocess.PIPE)
        p2 = subprocess.Popen(["sudo","tee",filename], stdin=p1.stdout,
                        stdout=subprocess.PIPE)
        p2.communicate()
    def on(self):
        self.led_on_off("1")
    def off(self):
        self.led_on_off("0")

def main():
    led= LED(17)   # LED1 is connected to GPIO 17 (3.3 V on a Pi Zero)
    led2 = LED(27) # LED2 is connected to GPIO 27 (3.3 V on a Pi Zero)
    led_act = active_led() # Pi Zero ACTivity LED

    # Turn on Illuminating LEDs. Turn Off Pi's ACTivity LED
    led.on()
    led2.on()
    led_act.off()

    camera = PiCamera()

    #Future to do someday: turn off the camera's LED.
    #camera.led = False #requires RPi.GPIO package installed and in BCM mode.

    camera.resolution = (CAMERA_REZ_HORZ, CAMERA_REZ_VERT)
   #camera.sensor_mode=3 #This causes "Timed out waiting for capture to end" error.

    #Setup a Numpy Array for capturing images.
    #  For Numpy Array, according to PiCamera Docs
    #  horizontal: round up nearest x32, vertical: round up nearest x16
    np_vert = int( np.ceil(CAMERA_REZ_VERT/16)*16 )
    np_horz = int( np.ceil(CAMERA_REZ_HORZ/32)*32 )
   #print("np_vert: %i, np_horz: %i" % (np_vert, np_horz) )
   #output = np.empty((1952, 2592, 3), dtype=np.uint8)
    output = np.empty((np_vert, np_horz, 3), dtype=np.uint8)

    # If you want to preview it on the display, uncoment the next line.
    #camera.start_preview()

    # Meter Mode
    #   All modes set up two regions:
    #   - centeral region,
    #   - outer region
    #camera.meter_mode    = 'average' #default
    camera.meter_mode    = 'spot'    #smallest central region
    #camera.meter_mode    = 'backlit' #largest central region
    #camera.meter_mode    = 'matrix'  #

    # Exposure Mode
    #camera.exposure_mode = 'off'
    #camera.exposure_mode = 'auto'
    camera.exposure_mode = 'nightpreview'
    #camera.exposure_mode = 'night'
    #camera.exposure_mode = 'fireworks' #don't use this. Too dark
    #camera.exposure_mode = 'verylong'  #don't use this. Too dark
    #camera.exposure_mode = 'snow'      #don't use this. Too dark
    #camera.exposure_mode = 'backlight' #About the same as auto
    #camera.exposure_mode = 'spotlight' #don't use this. Its black.

    # Flash Mode
    #camera.flash_mode = 'redeye'

    # Average White Balance Mode
    #camera.awb_mode = 'off'         #requires adjusting gains manually.
    #camera.awb_mode = 'auto'
    #camera.awb_mode = 'incandescent'#warm still shows some yellow-ish white
    #camera.awb_mode = 'tungsten'    #2500K to 3500K
    #camera.awb_mode = 'fluorescent' #2500K to 4500K
    #camera.awb_mode = 'sun'         #5000K to 6500K
    #camera.awb_mode = 'cloud'       #6500K to 12000K
    #camera.awb_mode = 'flash'  	 #should make less blue
    camera.awb_mode = 'greyworld' #Mimic IRFilter.Not valid w. regular PiCamera

    #Other settings you may want to play with
    #camera.iso = 800 #makes a little darker. Non-0 nullifies Exposure mode
    #camera.brightness = 80
    #camera.contrast = 80
    #camera.sharpness = 100 #default=0 range -100 to +100
    #camera.color_effects = (128,128)

    sleep(2)
    #Capture an image for both JPEG and Numpy
    #  Put it in RAM file system /run/shm/ to minimize wear on SD card
    camera.capture('/run/shm/gasmeter_last.jpg')
    camera.capture(output,format='rgb') #Note:set to 'bgr' did not work
    np.save('/run/shm/gasmeter_last', output) #extension is .npy

    #If you are running preview, then uncomment next line
    #camera.stop_preview()

    sleep(1)  #Makes a very very slight difference.

    #Future to do someday: turn back on the camera's LED.
    #camera.led = True

    camera.close()

    led.off()
    led2.off()
    led_act.on()

if __name__ == "__main__":
    main()


import numpy as np
from scipy.io import FortranFile as FF

import pynbody

def test_load_adaptahop_catalogue():
    f = pynbody.load("testdata/output_00080")
    h = f.halos()
    assert len(h) == h._headers["nhalos"] + h._headers["nsubs"]

def test_load_one_halo():
    def helper(path, nhalos):
        f = pynbody.load(path)
        h = f.halos()
        np.testing.assert_allclose(h[1].properties["members"], h[1]["iord"])
        assert len(h) == nhalos

    for path, nhalos in (
        ("testdata/output_00080", 170),
        ("testdata/new_adaptahop_output_00080", 2),
    ):
        yield helper, path, nhalos

def test_get_group():
    f = pynbody.load("testdata/output_00080")
    h = f.halos()

    group_array = h.get_group_array()
    iord = f.dm["iord"]

    for halo_id in range(1, len(h) + 1):
        mask = group_array == halo_id

        # Check that the indices
        # - read from halo (h[halo_id]['iord'])
        # - obtained from get_group_array masking
        # are the same (in term of sets)
        iord_1 = np.sort(iord[mask])
        iord_2 = np.sort(h[halo_id]["iord"])

        np.testing.assert_equal(iord_1, iord_2)

def test_halo_particle_ids():
    f = pynbody.load("testdata/output_00080")
    h = f.halos()

    with FF(h._fname, mode="r") as f:
        for halo_id in range(1, len(h) + 1):
            # Manually read the particle ids and make sure pynbody is reading them as it should
            f._fp.seek(h[halo_id].properties["file_offset"])

            f.read_ints()  # number of particles
            expected_members = f.read_ints("i")  # halo members

            np.testing.assert_equal(expected_members, h[halo_id].properties["members"])


import mxnet as mx
import random
import numpy as np
import re
import copy
import json

def _str2tuple(string):
    """Convert shape string to list, internal use only.
    Parameters
    ----------
    string: str
        Shape string.
    Returns
    -------
    list of str
        Represents shape.
    """
    return re.findall(r"\d+", string)

def plot_network(symbol, title="plot", save_format='pdf', shape=None, node_attrs={},
                 hide_weights=True):
    """Creates a visualization (Graphviz digraph object) of the given computation graph.
    Graphviz must be installed for this function to work.
    Parameters
    ----------
    title: str, optional
        Title of the generated visualization.
    symbol: Symbol
        A symbol from the computation graph. The generated digraph will visualize the part
        of the computation graph required to compute `symbol`.
    shape: dict, optional
        Specifies the shape of the input tensors. If specified, the visualization will include
        the shape of the tensors between the nodes. `shape` is a dictionary mapping
        input symbol names (str) to the corresponding tensor shape (tuple).
    node_attrs: dict, optional
        Specifies the attributes for nodes in the generated visualization. `node_attrs` is
        a dictionary of Graphviz attribute names and values. For example,
            ``node_attrs={"shape":"oval","fixedsize":"false"}``
            will use oval shape for nodes and allow variable sized nodes in the visualization.
    hide_weights: bool, optional
        If True (default), then inputs with names of form *_weight (corresponding to weight
        tensors) or *_bias (corresponding to bias vectors) will be hidden for a cleaner
        visualization.
    Returns
    -------
    dot: Digraph
        A Graphviz digraph object visualizing the computation graph to compute `symbol`.
    Example
    -------
    >>> net = mx.sym.Variable('data')
    >>> net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=128)
    >>> net = mx.sym.Activation(data=net, name='relu1', act_type="relu")
    >>> net = mx.sym.FullyConnected(data=net, name='fc2', num_hidden=10)
    >>> net = mx.sym.SoftmaxOutput(data=net, name='out')
    >>> digraph = mx.viz.plot_network(net, shape={'data':(100,200)},
    ... node_attrs={"fixedsize":"false"})
    >>> digraph.view()
    """
    # todo add shape support
    try:
        from graphviz import Digraph
    except:
        raise ImportError("Draw network requires graphviz library")
    if not isinstance(symbol, Symbol):
        raise TypeError("symbol must be a Symbol")
    draw_shape = False
    if shape is not None:
        draw_shape = True
        interals = symbol.get_internals()
        _, out_shapes, _ = interals.infer_shape(**shape)
        if out_shapes is None:
            raise ValueError("Input shape is incomplete")
        shape_dict = dict(zip(interals.list_outputs(), out_shapes))
    conf = json.loads(symbol.tojson())
    nodes = conf["nodes"]
    # default attributes of node
    node_attr = {"shape": "box", "fixedsize": "true",
                 "width": "1.3", "height": "0.8034", "style": "filled"}
    # merge the dict provided by user and the default one
    node_attr.update(node_attrs)
    dot = Digraph(name=title, format=save_format)
    # color map
    cm = ("#8dd3c7", "#fb8072", "#ffffb3", "#bebada", "#80b1d3",
          "#fdb462", "#b3de69", "#fccde5")

    def looks_like_weight(name):
        """Internal helper to figure out if node should be hidden with `hide_weights`.
        """
        if name.endswith("_weight"):
            return True
        if name.endswith("_bias"):
            return True
        return False

    # make nodes
    hidden_nodes = set()
    for node in nodes:
        op = node["op"]
        name = node["name"]
        # input data
        attr = copy.deepcopy(node_attr)
        label = name

        if op == "null":
            if looks_like_weight(node["name"]):
                if hide_weights:
                    hidden_nodes.add(node["name"])
                # else we don't render a node, but
                # don't add it to the hidden_nodes set
                # so it gets rendered as an empty oval
                continue
            attr["shape"] = "oval" # inputs get their own shape
            label = node["name"]
            attr["fillcolor"] = cm[0]
        elif op == "Convolution":
            label = r"Convolution\n%s/%s, %s" % ("x".join(_str2tuple(node["attr"]["kernel"])),
                                                 "x".join(_str2tuple(node["attr"]["stride"]))
                                                 if "stride" in node["attr"] else "1",
                                                 node["attr"]["num_filter"])
            attr["fillcolor"] = cm[1]
        elif op == "FullyConnected":
            label = r"FullyConnected\n%s" % node["attr"]["num_hidden"]
            attr["fillcolor"] = cm[1]
        elif op == "BatchNorm":
            attr["fillcolor"] = cm[3]
        elif op == "Activation" or op == "LeakyReLU":
            label = r"%s\n%s" % (op, node["attr"]["act_type"])
            attr["fillcolor"] = cm[2]
        elif op == "Pooling":
            label = r"Pooling\n%s, %s/%s" % (node["attr"]["pool_type"],
                                             "x".join(_str2tuple(node["attr"]["kernel"])),
                                             "x".join(_str2tuple(node["attr"]["stride"]))
                                             if "stride" in node["attr"] else "1")
            attr["fillcolor"] = cm[4]
        elif op == "Concat" or op == "Flatten" or op == "Reshape":
            attr["fillcolor"] = cm[5]
        elif op == "Softmax":
            attr["fillcolor"] = cm[6]
        else:
            attr["fillcolor"] = cm[7]
            if op == "Custom":
                label = node["attr"]["op_type"]

        dot.node(name=name, label=label, **attr)

    # add edges
    for node in nodes:          # pylint: disable=too-many-nested-blocks
        op = node["op"]
        name = node["name"]
        if op == "null":
            continue
        else:
            inputs = node["inputs"]
            for item in inputs:
                input_node = nodes[item[0]]
                input_name = input_node["name"]
                if input_name not in hidden_nodes:
                    attr = {"dir": "back", 'arrowtail':'open'}
                    # add shapes
                    if draw_shape:
                        if input_node["op"] != "null":
                            key = input_name + "_output"
                            if "attr" in input_node:
                                params = input_node["attr"]
                                if "num_outputs" in params:
                                    key += str(np.maximum(int(params["num_outputs"]) - 1, 0))
                                    params["num_outputs"] = int(params["num_outputs"]) - 1
                            shape = shape_dict[key][1:]
                            label = "x".join([str(x) for x in shape])
                            attr["label"] = label
                        else:
                            key = input_name
                            shape = shape_dict[key][1:]
                            label = "x".join([str(x) for x in shape])
                            attr["label"] = label
                    dot.edge(tail_name=name, head_name=input_name, **attr)

    return dot


"""add fides cloud table

Revision ID: 192f23f4c968
Revises: 093bb28a8270
Create Date: 2023-09-01 17:28:22.099543

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "192f23f4c968"
down_revision = "093bb28a8270"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "fidescloud",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=True,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=True,
        ),
        sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_fidescloud_id"), "fidescloud", ["id"], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_fidescloud_id"), table_name="fidescloud")
    op.drop_table("fidescloud")
    # ### end Alembic commands ###


#!/usr/bin/env python

from Bio import SeqIO
import sys

outfile = open(sys.argv[2], 'w')
addlyr = open("additional_layers.txt", 'w')
addlyr.write("split"+'\t'+"seq_length"+'\t'+"count"+'\n')
for rec in SeqIO.parse(open(sys.argv[1], 'rU'), "fasta"):
    id =  rec.id.split("|")[0]
    start = rec.id.split("|")[1].split(":")[1]
    stop = rec.id.split("|")[2].split(";")[0].split(":")[1]
    cnt =  rec.id.split(";")[1].split("=")[1]
    outfile.write(">"+id+'_'+start+'_'+stop+'\n'+str(rec.seq)+'\n')
    addlyr.write(id+'_'+start+'_'+stop+'\t'+str(len(rec.seq))+'\t'+cnt+'\n')

outfile.close()
addlyr.close()


import os
import re
import json
import hashlib
import logging
from time import time, sleep
from threading import Lock
from collections import defaultdict
from urllib.parse import urlsplit, urlencode

import urllib3
from urllib3.contrib.socks import SOCKSProxyManager

from .error import SearchError

def get_proxy_manager(proxy):
    if not proxy:
        return urllib3.PoolManager()
    if proxy.startswith('socks'):
        return SOCKSProxyManager(proxy)
    return urllib3.ProxyManager(proxy)

def get_referer(url):
    split_url = urlsplit(url)
    return '%s://%s/' % (split_url.scheme, split_url.netloc)

def request(http, method, url, **kwargs):
    ret = http.request(method, url, **kwargs)
    if ret.status != 200:
        raise urllib3.exceptions.ResponseError(
            '%r: response status %d' % (url, ret.status)
        )
    return ret.data

class Search:
    DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0'

    def __init__(self, throttle=1, proxy=None, headers=None):
        if headers is None:
            headers = {}
        if 'User-Agent' not in headers:
            headers['User-Agent'] = self.DEFAULT_USER_AGENT
        self.lock = Lock()
        self.logger = logging.getLogger(__name__)
        self.last_used = time()
        self.http = get_proxy_manager(proxy)
        self.headers = headers
        self.throttle = throttle
        self.cache = defaultdict(SearchResults)

    def _request_json(self, results, url, fields=None):
        self.logger.info('request search json %s %r', url, fields)
        data = request(self.http, 'GET', url,
                       headers=self.headers, fields=fields)
        data = json.loads(data.decode('utf-8'))

        try:
            results.next_url = 'https://duckduckgo.com/%s&%s' % (
                data['next'],
                urlencode({'vqd': next(iter(data['vqd'].values()))})
            )
        except (KeyError, StopIteration):
            results.next_url = None
            results.full = True

        results.full = True #

        for i, res in enumerate(data['results'], len(results.items)):
            try:
                res = SearchResult(i, res['thumbnail'], res['image'],
                                   res['url'], res['title'])
                results.items.append(res)
            except KeyError as ex:
                self.logger.warning('results: %r: %r', ex, res)

    def _request(self, query):
        results = self.cache[query]
        self.headers['Referer'] = 'https://duckduckgo.com/'
        if results.next_url is None:
            data = request(
                self.http,
                'GET',
                'https://duckduckgo.com',
                headers=self.headers,
                fields={
                    'q': query,
                    'ia': 'images',
                    'iar': 'images',
                    'iax': 'images'
                }
            ).decode('utf-8')
            vqd = re.search(r'vqd=[\'"]([^\'"]+)[\'"]', data)
            if vqd is None:
                raise urllib3.exceptions.ResponseError('no vqd in response')
            vqd = vqd.group(1)
            url = 'https://duckduckgo.com/i.js'
            fields = {
                'q': query,
                'vqd': vqd,
                'l': 'us-en',
                'o': 'json',
                'f': ',,,',
                'p': '-1'
            }
        else:
            url = results.next_url
            fields = None
        self._request_json(results, url, fields)

    def _throttle(self):
        dt = time() - self.last_used
        if dt < self.throttle:
            sleep(self.throttle - dt)
        self.last_used = time()

    def __getitem__(self, query):
        query = query.strip().lower()
        return self.cache[query]

    def __call__(self, query, offset):
        with self.lock:
            self._throttle()
            query = query.strip().lower()
            results = self.cache[query]
            while True:
                try:
                    self.logger.info('get next result %r', query)
                    ret = results[offset]
                    break
                except IndexError:
                    if results.full:
                        if not results.items:
                            self.logger.info('no results')
                            raise SearchError('no search results')
                        self.logger.info('no more results')
                        ret = results[-1]
                        break
                    else:
                        self.logger.info('request more results')
                        self._request(query)
            is_last = results.full and offset >= len(results) - 1
            return ret, is_last

class SearchResults:
    def __init__(self, results=None, full=False, next_url=None):
        self.items = results if results is not None else []
        self.full = full
        self.next_url = next_url

    def __len__(self):
        return len(self.items)

    def __getitem__(self, i):
        return self.items[i]

class SearchResult:
    def __init__(self, offset, thumbnail, image, url, title):
        self.offset = offset
        self.thumbnail = thumbnail
        self.image = image
        self.title = title
        self.url = url
        self.filename = hashlib.md5(self.thumbnail.encode('utf-8')).hexdigest()

    def __repr__(self):
        return 'SearchResult(%r, %r, %r, %r)' % (
            self.thumbnail,
            self.image,
            self.url,
            self.title
        )

    __str__ = __repr__

    def download(self, http, dir_='/tmp', headers=None):
        fname = os.path.join(dir_, self.filename)
        if os.path.exists(fname):
            return
        if headers is None:
            headers = {}
        url = self.image
        headers['Referer'] = get_referer(url)
        data = request(http, 'GET', url, headers=headers)
        with open(fname, 'wb') as fp:
            fp.write(data)


# -*- coding: utf-8 -*-
from pynes.nes_types import NesRs, NesArray, NesSprite, NesString, NesChrFile
from pynes.game import PPUSprite

class BitPak:

    def __init__(self, game):
        self.game = game
        self.assigned = None

    def __call__(self):
        return None

    def asm(self):
        return ''

    def procedure(self):
        return None

    def attribute(self):
        return ''

    def assigned_to(self, assigned):
        self.assigned = assigned

class rs(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self, size):
        return NesRs(size)

class get_sprite(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self, sprite):
        return PPUSprite(sprite, self.game)

class wait_vblank(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self):
        return None

    def asm(self):
        return '  JSR WAITVBLANK\n'

    def procedure(self):
        return ('WAITVBLANK:\n'
                '  BIT $2002\n'
                '  BPL WAITVBLANK\n'
                '  RTS\n')

class clearmem(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def asm(self):
        return ('CLEARMEM:\n'
                '  LDA #$00\n'
                '  STA $0000, x\n'
                '  STA $0100, x\n'
                '  STA $0200, x\n'
                '  STA $0400, x\n'
                '  STA $0500, x\n'
                '  STA $0600, x\n'
                '  STA $0700, x\n'
                '  LDA #$FE\n'
                '  STA $0300, x\n'
                '  INX\n'
                '  BNE CLEARMEM\n')

class import_chr(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self, string):
        assert isinstance(string, NesString)
        return NesChrFile(string)

class define_sprite(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self, x, y, tile, attrib=0x80):
        assert isinstance(x, int)
        assert isinstance(y, int)
        assert isinstance(tile, int) or isinstance(tile, NesArray)
        return NesSprite(x, y, tile, attrib)

class cls(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self):
        self.line = self.game.get_param('line', 1)

    def asm(self):
        return '  JSR CLS\n'

    def procedure(self):
        return ('CLS:\n'
                '  LDA $2002\n'
                '  LDA $20\n'
                '  LDA $2006\n'
                '  LDA $00\n'
                '  LDA $2006\n'
                '  LDA #00\n'
                'LineLoop:'
                '  STA line\n'
                '  LDY #00\n'
                '  LDA #$25\n'  # blank == space
                'ColumnLoop:'
                '  STA $2007\n'
                '  INY'
                '  CPY #16\n'
                '  BNE ColumnLoop\n'
                '  LDA line\n'
                '  CLC\n'
                '  ADC #01\n'
                # '  STA line\n'
                '  CMP #16\n'
                '  BNE LineLoop\n'

                "  LDA #00\n"
                "  STA $2005\n"
                "  STA $2005\n")

class show(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)
        self.game.ppu.nmi_enable = True
        self.game.ppu.background_enable = True
        self.game.ppu.background_pattern_table = 1
        self.game.has_nmi = True

        self.addressLow = game.get_param('addressLow', 1)
        self.addressHigh = game.get_param('addressHigh', 1)
        self.posLow = game.get_param('posLow', 1)
        self.posHigh = game.get_param('posHigh', 1)

    def __call__(self, string, y=None, x=None, nametable=0):
        assert isinstance(string, NesString)
        string.is_used = True
        self.string = string
        base_adress = 0x2000

        if y is None:
            y = 15
        if x is None:
            x = 16 - len(string) / 2
        pos = base_adress + y * 32 + x
        self.posHigh = (pos & 0xff00) >> 8
        self.posLow = (pos & 0x00ff)

    def asm(self):
        asmcode = ("  LDA #LOW(%s)\n"
                   "  STA addressLow\n"
                   "  LDA #HIGH(%s)\n"
                   "  STA addressHigh\n"
                   "  LDA #$%02X\n"
                   "  STA posHigh\n"
                   "  LDA #$%02X\n"
                   "  STA posLow\n"
                   "  JSR Show\n") % (self.string.instance_name,
                                      self.string.instance_name,
                                      self.posHigh,
                                      self.posLow)
        return asmcode

    def procedure(self):
        asmcode = ("Show:\n"
                   "  LDA $2002\n"
                   "  LDA posHigh\n"
                   "  STA $2006\n"
                   "  LDA posLow\n"
                   "  STA $2006\n"
                   "  LDY #$00\n"
                   "PrintLoop:\n"
                   "  LDA (addressLow), y\n"
                   "  CMP #$25\n"
                   "  BEQ PrintEnd\n"
                   "  STA $2007\n"
                   "  INY\n"
                   "  JMP PrintLoop\n"
                   "PrintEnd:\n"
                   "  LDA #00\n"
                   "  STA $2005\n"
                   "  STA $2005\n"
                   "  RTS\n")
        return asmcode

class load_palette(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)

    def __call__(self, palette):
        assert isinstance(palette, NesArray)
        assert palette.instance_name is not None
        self.palette = palette
        return palette

    def asm(self):
        asmcode = (
            'LoadPalettes:\n'
            '  LDA $2002             ; Reset PPU, start writing\n'
            '  LDA #$3F\n'
            '  STA $2006             ; High byte = $3F00\n'
            '  LDA #$00\n'
            '  STA $2006             ; Low byte = $3F00\n'
            '  LDX #$00\n'
            'LoadPalettesIntoPPU:\n'
            '  LDA %s, x\n'
            '  STA $2007\n'
            '  INX\n') % self.palette.instance_name
        asmcode += '  CPX #$%02x\n' % len(self.palette)
        asmcode += '  BNE LoadPalettesIntoPPU\n'
        return asmcode

class load_sprite(BitPak):

    def __init__(self, game):
        BitPak.__init__(self, game)
        self.game.has_nmi = True  # TODO remove this
        self.game.ppu.sprite_enable = True
        self.game.ppu.nmi_enable = True

    def __call__(self, sprite, ppu_pos):
        assert isinstance(sprite, NesSprite)
        assert ppu_pos < 64
        self.sprite = sprite
        self.start_address = 0x0200 + (ppu_pos * 4)
        self.sprite.ppu_address = ppu_pos
        return None

    def asm(self):
        size = len(self.sprite)
        load_sprites = self.game.get_label_for('LoadSprites')
        load_sprites_into_PPU = self.game.get_label_for('LoadSpritesIntoPPU')
        '''
        Proposal
        with asm(self.game) as a:
            a.label('LoadSprites')
            a.ldx = 0
            a.lda = ('LoadSpritesIntoPPU', a.x)
            a.sta = (self.start_address, a.x)
            a.inx()
            a.cpx(size * 4)
            bne('LoadSpritesIntoPPU')
        '''
        asmcode = (
            '%s:\n'
            '  LDX #$00\n'
            '%s:\n'
            '  LDA %s, x\n'
            '  STA $%04X, x\n'
            '  INX\n'
            '  CPX #%d\n'
            '  BNE %s\n'
        ) % (load_sprites,
             load_sprites_into_PPU,
             self.sprite.instance_name,
             self.start_address,
             size * 4,
             load_sprites_into_PPU)
        return asmcode


import requests
import json
import dbConfig

def current_position():
    session = requests.Session()
    a = session.post('https://www.googleapis.com/geolocation/v1/geolocate?key='+dbConfig.googleCloudConfig['GOOGLE_GEOLOCATION'] , headers = {'Content-Type': 'application/json'})
    print(a.text)
    geo_json = json.loads(a.text)
    lat = geo_json['location']['lat']
    lng = geo_json['location']['lng']
    return [lat, lng]