import json
import logging
import os
import time
import yaml

from heatclient.common import deployment_utils
from heatclient import exc as heat_exc
from mistral_lib import actions
from swiftclient import exceptions as swiftexceptions

from tripleo_common.actions import base
from tripleo_common.actions import templates
from tripleo_common import constants
from tripleo_common import update
from tripleo_common.utils import overcloudrc
from tripleo_common.utils import plan as plan_utils
from tripleo_common.utils import swift as swiftutils

LOG = logging.getLogger(__name__)

class OrchestrationDeployAction(base.TripleOAction):

    def __init__(self, server_id, config, name, input_values=[],
                 action='CREATE', signal_transport='TEMP_URL_SIGNAL',
                 timeout=300, group='script'):
        super(OrchestrationDeployAction, self).__init__()
        self.server_id = server_id
        self.config = config
        self.input_values = input_values
        self.action = action
        self.name = name
        self.signal_transport = signal_transport
        self.timeout = timeout
        self.group = group

    def _extract_container_object_from_swift_url(self, swift_url):
        container_name = swift_url.split('/')[-2]
        object_name = swift_url.split('/')[-1].split('?')[0]
        return (container_name, object_name)

    def _build_sc_params(self, swift_url):
        source = {
            'config': self.config,
            'group': self.group,
        }
        return deployment_utils.build_derived_config_params(
            action=self.action,
            source=source,
            name=self.name,
            input_values=self.input_values,
            server_id=self.server_id,
            signal_transport=self.signal_transport,
            signal_id=swift_url
        )

    def _wait_for_data(self, container_name, object_name, context):
        body = None
        count_check = 0
        swift_client = self.get_object_client(context)
        while not body:
            body = swiftutils.get_object_string(swift_client, container_name,
                                                object_name)
            count_check += 3
            if body or count_check > self.timeout:
                break
            time.sleep(3)

        return body

    def run(self, context):
        heat = self.get_orchestration_client(context)
        swift_client = self.get_object_client(context)

        swift_url = deployment_utils.create_temp_url(swift_client,
                                                     self.name,
                                                     self.timeout)
        container_name, object_name = \
            self._extract_container_object_from_swift_url(swift_url)

        params = self._build_sc_params(swift_url)
        config = heat.software_configs.create(**params)

        sd = heat.software_deployments.create(
            tenant_id='asdf',  # heatclient requires this
            config_id=config.id,
            server_id=self.server_id,
            action=self.action,
            status='IN_PROGRESS'
        )

        body = self._wait_for_data(container_name, object_name, context)

        # cleanup
        try:
            sd.delete()
            config.delete()
            swift_client.delete_object(container_name, object_name)
            swift_client.delete_container(container_name)
        except Exception as err:
            LOG.error("Error cleaning up heat deployment resources.", err)

        error = None
        if not body:
            body_json = {}
            error = "Timeout for heat deployment '%s'" % self.name
        else:
            body_json = json.loads(body)
            if body_json['deploy_status_code'] != 0:
                error = "Heat deployment failed for '%s'" % self.name

        if error:
            LOG.error(error)

        return actions.Result(data=body_json, error=error)

class DeployStackAction(base.TripleOAction):
    """Deploys a heat stack."""

    def __init__(self, timeout, container=constants.DEFAULT_CONTAINER_NAME,
                 skip_deploy_identifier=False):
        super(DeployStackAction, self).__init__()
        self.container = container
        self.timeout_mins = timeout
        self.skip_deploy_identifier = skip_deploy_identifier

    def run(self, context):
        # check to see if the stack exists
        heat = self.get_orchestration_client(context)
        try:
            stack = heat.stacks.get(self.container, resolve_outputs=False)
        except heat_exc.HTTPNotFound:
            stack = None

        stack_is_new = stack is None

        # update StackAction, DeployIdentifier and UpdateIdentifier
        swift = self.get_object_client(context)

        parameters = dict()
        if not self.skip_deploy_identifier:
            parameters['DeployIdentifier'] = int(time.time())
        parameters['UpdateIdentifier'] = ''
        parameters['StackAction'] = 'CREATE' if stack_is_new else 'UPDATE'

        try:
            env = plan_utils.get_env(swift, self.container)
        except swiftexceptions.ClientException as err:
            err_msg = ("Error retrieving environment for plan %s: %s" % (
                self.container, err))
            LOG.exception(err_msg)
            return actions.Result(error=err_msg)

        self.set_tls_parameters(parameters, env)
        try:
            plan_utils.update_in_env(swift, env, 'parameter_defaults',
                                     parameters)
        except swiftexceptions.ClientException as err:
            err_msg = ("Error updating environment for plan %s: %s" % (
                self.container, err))
            LOG.exception(err_msg)
            return actions.Result(error=err_msg)

        if not stack_is_new:
            try:
                LOG.debug('Checking for compatible neutron mechanism drivers')
                msg = update.check_neutron_mechanism_drivers(env, stack,
                                                             swift,
                                                             self.container)
                if msg:
                    return actions.Result(error=msg)
            except swiftexceptions.ClientException as err:
                err_msg = ("Error getting template %s: %s" % (
                    self.container, err))
                LOG.exception(err_msg)
                return actions.Result(error=err_msg)

        # process all plan files and create or update a stack
        process_templates_action = templates.ProcessTemplatesAction(
            container=self.container
        )
        processed_data = process_templates_action.run(context)

        # If we receive a 'Result' instance it is because the parent action
        # had an error.
        if isinstance(processed_data, actions.Result):
            return processed_data

        stack_args = processed_data.copy()
        stack_args['timeout_mins'] = self.timeout_mins

        if stack_is_new:
            try:
                swift.copy_object(
                    "%s-swift-rings" % self.container, "swift-rings.tar.gz",
                    "%s-swift-rings/%s-%d" % (
                        self.container, "swift-rings.tar.gz", time.time()))
                swift.delete_object(
                    "%s-swift-rings" % self.container, "swift-rings.tar.gz")
            except swiftexceptions.ClientException:
                pass
            LOG.info("Perfoming Heat stack create")
            try:
                return heat.stacks.create(**stack_args)
            except heat_exc.HTTPException as err:
                err_msg = "Error during stack creation: %s" % (err,)
                LOG.exception(err_msg)
                return actions.Result(error=err_msg)

        LOG.info("Performing Heat stack update")
        stack_args['existing'] = 'true'
        try:
            return heat.stacks.update(stack.id, **stack_args)
        except heat_exc.HTTPException as err:
            err_msg = "Error during stack update: %s" % (err,)
            LOG.exception(err_msg)
            return actions.Result(error=err_msg)

    def set_tls_parameters(self, parameters, env,
                           local_ca_path=constants.LOCAL_CACERT_PATH):
        cacert_string = self._get_local_cacert(local_ca_path)
        if cacert_string:
            parameters['CAMap'] = self._get_updated_camap_entry(
                'undercloud-ca', cacert_string, self._get_camap(env))

    def _get_local_cacert(self, local_ca_path):
        # Since the undercloud has TLS by default, we'll add the undercloud's
        # CA to be trusted by the overcloud.
        try:
            with open(local_ca_path, 'rb') as ca_file:
                return ca_file.read().decode('utf-8')
        except IOError:
            # If the file wasn't found it means that the undercloud's TLS
            # was explicitly disabled or another CA is being used. So we'll
            # let the user handle this.
            return None
        except Exception:
            raise

    def _get_camap(self, env):
        return env['parameter_defaults'].get('CAMap', {})

    def _get_updated_camap_entry(self, entry_name, cacert, orig_camap):
        ca_map_entry = {
            entry_name: {
                'content': cacert
            }
        }
        orig_camap.update(ca_map_entry)
        return orig_camap

class OvercloudRcAction(base.TripleOAction):
    """Generate the overcloudrc for a plan

    Given the name of a container, generate the overcloudrc files needed to
    access the overcloud via the CLI.

    no_proxy is optional and is a comma-separated string of hosts that
    shouldn't be proxied
    """

    def __init__(self, container, no_proxy=""):
        super(OvercloudRcAction, self).__init__()
        self.container = container
        self.no_proxy = no_proxy

    def run(self, context):
        orchestration_client = self.get_orchestration_client(context)
        swift = self.get_object_client(context)

        try:
            stack = orchestration_client.stacks.get(self.container)
        except heat_exc.HTTPNotFound:
            error = (
                "The Heat stack {} could not be found. Make sure you have "
                "deployed before calling this action.").format(self.container)
            return actions.Result(error=error)

        # We need to check parameter_defaults first for a user provided
        # password. If that doesn't exist, we then should look in the
        # automatically generated passwords.
        # TODO(d0ugal): Abstract this operation somewhere. We shouldn't need to
        # know about the structure of the environment to get a password.
        try:
            env = plan_utils.get_env(swift, self.container)
        except swiftexceptions.ClientException as err:
            err_msg = ("Error retrieving environment for plan %s: %s" % (
                self.container, err))
            LOG.error(err_msg)
            return actions.Result(error=err_msg)

        try:
            parameter_defaults = env['parameter_defaults']
            passwords = env['passwords']
            admin_pass = parameter_defaults.get('AdminPassword')
            if admin_pass is None:
                admin_pass = passwords['AdminPassword']
        except KeyError:
            error = ("Unable to find the AdminPassword in the plan "
                     "environment.")
            return actions.Result(error=error)

        region_name = parameter_defaults.get('KeystoneRegion')
        return overcloudrc.create_overcloudrc(stack, self.no_proxy, admin_pass,
                                              region_name)

class DeploymentFailuresAction(base.TripleOAction):
    """Return all of the failures (if any) from deploying the plan

    :param plan: name of the Swift container / plan name
    """

    def __init__(self,
                 plan=constants.DEFAULT_CONTAINER_NAME,
                 work_dir=constants.MISTRAL_WORK_DIR,
                 ansible_errors_file=constants.ANSIBLE_ERRORS_FILE):
        super(DeploymentFailuresAction, self).__init__()
        self.plan = plan
        self.work_dir = work_dir
        self.ansible_errors_file = ansible_errors_file

    def _format_return(self, message, failures={}):
        return dict(message=message,
                    failures=failures)

    def run(self, context):
        try:
            failures_file = os.path.join(self.work_dir, self.plan,
                                         self.ansible_errors_file)
            failures = json.loads(open(failures_file).read())
            return self._format_return('', failures)
        except IOError:
            return self._format_return(
                'Ansible errors file not found at %s' % failures_file)

class DeploymentStatusAction(base.TripleOAction):
    """Get the deployment status and update it if necessary

    The status will be set based off of the stack status and any running
    config_download workflow.
    """

    def __init__(self,
                 plan=constants.DEFAULT_CONTAINER_NAME):
        super(DeploymentStatusAction, self).__init__()
        self.plan = plan

    def run(self, context):
        orchestration_client = self.get_orchestration_client(context)
        workflow_client = self.get_workflow_client(context)
        swift_client = self.get_object_client(context)

        try:
            stack = orchestration_client.stacks.get(self.plan)
        except heat_exc.HTTPNotFound:
            return dict(status_update=None,
                        deployment_status=None)

        try:
            body = swiftutils.get_object_string(swift_client,
                                                '%s-messages' % self.plan,
                                                'deployment_status.yaml')

            deployment_status = yaml.safe_load(body)['deployment_status']
        except swiftexceptions.ClientException:
            deployment_status = None

        stack_status = stack.stack_status
        cd_status = None
        ansible_status = None
        # Will get set to new status if an update is required
        status_update = None

        cd_execs = workflow_client.executions.find(
            workflow_name='tripleo.deployment.v1.config_download_deploy')
        cd_execs.sort(key=lambda x: x.updated_at)
        if cd_execs:
            cd_exec = workflow_client.executions.get(cd_execs[-1].id)
            cd_status = cd_exec.state
            ansible_status = json.loads(
                cd_exec.output).get('deployment_status')

        def update_status(status):
            # If we need to update the status return it
            if deployment_status != status:
                return status

        # Update the status if needed. We do this since tripleoclient does not
        # yet use a single API for overcloud deployment. Since there is no long
        # running process to make sure the status is updated, we instead update
        # the status if needed when we get it with this action.
        #
        # The logic here is:
        #
        # If stack or config_download is in progress, then the status is
        # deploying.
        #
        # Else if stack is failed or config_download is failed or ansible is
        # failed, then the status is failed.
        #
        # Else if config_download status is success and ansible is success
        # then status is success.
        #
        # Else, we just return the read deployment_status from earlier.
        if stack_status.endswith('IN_PROGRESS') or cd_status == 'RUNNING':
            status_update = update_status('DEPLOYING')
        elif stack_status.endswith('FAILED') or cd_status == 'FAILED' \
                or ansible_status == 'DEPLOY_FAILED':
            status_update = update_status('DEPLOY_FAILED')
        elif cd_status == 'SUCCESS' and ansible_status == 'DEPLOY_SUCCESS':
            status_update = update_status('DEPLOY_SUCCESS')

        return dict(cd_status=cd_status,
                    stack_status=stack_status,
                    deployment_status=deployment_status,
                    ansible_status=ansible_status,
                    status_update=status_update)

class ConvertStatusAction(base.TripleOAction):
    """Translate a Heat stack status into a config-download deployment status

    """

    def __init__(self, status):
        super(ConvertStatusAction, self).__init__()
        self.status = status

    def run(self, context):
        matrix = {
            'NO_STACK': 'UNDEPLOYED',
            'CREATE_IN_PROGRESS': 'DEPLOYING',
            'CREATE_COMPLETE': 'DEPLOY_SUCCESS',
            'CREATE_FAILED': 'DEPLOY_FAILED',
            'DELETE_COMPLETE': 'UNDEPLOYED',
            'DELETE_IN_PROGRESS': 'UNDEPLOYING',
            'DELETE_FAILED': 'UNDEPLOY_FAILED',
            'UPDATE_IN_PROGRESS': 'DEPLOYING',
            'UPDATE_FAILED': 'DEPLOY_FAILED',
            'UPDATE_COMPLETE': 'DEPLOY_SUCCESS'
        }

        return matrix.get(self.status, 'UNKNOWN')


from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from budget.models import BudgetEntry
from budget.forms import BudgetEntryForm
from budget.models import BudgetCategory
from django.contrib.auth.models import User
from django.shortcuts import render

@login_required(login_url='/login/')
def budget(request):

	if (request.method == "GET" and "delete" in request.GET):
		id = request.GET["delete"]
		BudgetEntry.objects.filter(id=id).delete()
		return redirect("/budget/")
	else:
		table_data = BudgetEntry.objects.filter(user=request.user)

		projectedTotal = 0
		actualTotal = 0
		for budgetE in table_data:
			projectedTotal += budgetE.projected
			actualTotal += budgetE.actual

		total = projectedTotal-actualTotal
		total = round(total, 2)
		context = {
            "table_data": table_data,
			"totalSpendDifference": total
		}
		return render(request, 'budget/budget.html', context)

def load_categories(request):
    categories = BudgetCategory.values('category').order_by('category')
    return render(request, 'tasks/category_dropdown_options.html', {'categories': categories})

@login_required(login_url='/login/')
def add(request):

	if(BudgetCategory.objects.count() == 0):
		BudgetCategory.objects.create(category="Food")
		BudgetCategory.objects.create(category="Clothing")
		BudgetCategory.objects.create(category="Hausing")
		BudgetCategory.objects.create(category="Education")
		BudgetCategory.objects.create(category="Entertainment")
		BudgetCategory.objects.create(category="Others")

	if (request.method == "POST"):
		if ("add" in request.POST):
			add_form = BudgetEntryForm(request.POST)
			if (add_form.is_valid()):
				description = add_form.cleaned_data["description"]
				category = BudgetCategory.objects.get(category=add_form.cleaned_data["category"])
				user = User.objects.get(id=request.user.id)
				projected = add_form.cleaned_data["projected"]
				actual = add_form.cleaned_data["actual"]

				BudgetEntry(user=user, description=description, category=category, projected=projected, actual=actual).save()
				return redirect("/budget/")
			else:
				context = {
                    "form_data": add_form
				}
				return render(request, 'budget/add.html', context)
		else:
			# Cancel
			return redirect("/budget/")
	else:
		context = {
            "form_data": BudgetEntryForm()
		}
		return render(request, 'budget/add.html', context)

@login_required(login_url='/login/')
def edit(request, id):
	if (request.method == "GET"):
		# Load Journal Entry Form with current model data.
		budgetEntry = BudgetEntry.objects.get(id=id)
		form = BudgetEntryForm(instance=budgetEntry)
		context = {"form_data": form}
		return render(request, 'budget/edit.html', context)
	elif (request.method == "POST"):
		# Process form submission
		if ("edit" in request.POST):
			form = BudgetEntryForm(request.POST)
			if (form.is_valid()):
				budgetEntry = form.save(commit=False)
				budgetEntry.user = request.user
				budgetEntry.id = id
				budgetEntry.save()
				return redirect("/budget/")
			else:
				context = {
                    "form_data": form
				}
				return render(request, 'budget/add.html', context)
		else:
			#Cancel
			return redirect("/budget/")


#Figure out import

from Engine.server.move import Move

from game.constants import  Moves, Direction, Units
class MerlinMoveFactory:
    def createMove(self, moveid:int, restInfo):
        '''
        Deserializes move, this is the factory used in client_connection.py
        '''
        valids = []
        for move in Moves:
          valids.append(move)
          if move.value == moveid:
            moveid = move
        if not moveid in valids:
          raise Exception(f"Invalid move id: {moveid}")
        restInfo = restInfo[1:]
        if moveid == Moves.ATTACK:
          if len(restInfo) != 3:
            raise Exception()

          direction = restInfo[1]
          magnitude = restInfo[2]
          if direction not in Direction._value2member_map_ or magnitude <= 0:
            raise Exception

          return AttackMove(*restInfo)
        elif moveid == Moves.BUY:
          if len(restInfo) != 3:
            raise Exception(f"Illegal size of action, requires 2: {restInfo}")

          unitType = restInfo[1]
          direction = restInfo[2]

          if unitType not in Units._value2member_map_ or direction not in Direction._value2member_map_:
            raise Exception

          return BuyMove(*restInfo)
        elif moveid == Moves.CAPTURE:
          if len(restInfo) != 2:
            raise Exception(f"Illegal size of action, requires 2: {restInfo}")

          direction = restInfo[1]

          if direction not in Direction._value2member_map_:
            raise Exception

          return CaptureMove(*restInfo)
        elif moveid == Moves.DIRECTION:
          if len(restInfo) != 3:
            raise Exception(f"Illegal size of action, requires 3: {restInfo}")
          direction = restInfo[1]
          magnitude = restInfo[2]
          if direction not in Direction._value2member_map_ or magnitude <= 0:
            raise Exception
          return DirectionMove(*restInfo)
        elif moveid == Moves.MINE:
          if len(restInfo) != 1:
            raise Exception(f"Illegal size of action, requires 1: {restInfo}")

          return MineMove(*restInfo)
        elif moveid == Moves.UPGRADE:
          if len(restInfo) != 1:
            raise Exception

          return UpgradeMove(*restInfo)
        raise Exception("Invalid move id")

def direction_to_enum(dirStr):
  for dirs in Direction:
    if dirs.value == dirStr:
      return dirs
  raise Exception(f"Invalid direction id: {dirStr}")

def unit_to_enum(unitStr):
  for unit in Units:
    if unit.value == unitStr:
      return unit
  raise Exception(f"Invalid unit id: {unit}")

class GameMove(Move):
  def __init__(self, unit):
    super().__init__(unit)

  def __repr__(self):
      '''
      Returns the serialized form of this move
      The subclass should implement this
      '''
      pass

class AttackMove(GameMove):
  def __init__(self, unit, direction, length):
      super().__init__(unit)
      self.direction = direction_to_enum(direction)
      self.length = length

  def __repr__(self):
      '''
      Returns the serialized form of this move
      '''
      return str({'command': Moves.ATTACK, "unit": self.unit, "target": self.target })

class UpgradeMove(GameMove):
  def __init__(self, unit):
      super().__init__(unit)

  def __repr__(self):
      '''
      Returns the serialized form of this move
      '''
      return str({'command': Moves.UPGRADE, "unit": self.unit})
class DirectionMove(GameMove):
  def __init__(self, unit, direction, magnitude):
      super().__init__(unit)
      self.direction = direction_to_enum(direction)
      self.magnitude = magnitude

  def __repr__(self):
      '''
      Returns the serialized form of this move
      '''
      return str({'command': Moves.DIRECTION, "unit": self.unit, "direction": self.direction})

class MineMove(GameMove):
    def __init__(self, unit):
        super().__init__(unit)

    def __repr__(self):
        '''
        Returns the serialized form of this move
        '''
        return str({'command': Moves.MINE, "unit": self.unit })

class BuyMove(GameMove):
  def __init__(self, unit, unitType, direction):
      super().__init__(unit)
      self.unitType = unit_to_enum(unitType)
      self.direction = direction_to_enum(direction)

  def __repr__(self):
      '''
      Returns the serialized form of this move
      '''
      return str({'command': Moves.BUY, "unitType": self.unitType, 'direction': self.direction})

class CaptureMove(GameMove):
  def __init__(self, unit, direction):
      super().__init__(unit)
      self.direction = direction_to_enum(direction)

  def __repr__(self):
      '''
      Returns the serialized form of this move
      '''
      return str({'command': Moves.CAPTURE, "unit": self.unit, 'direction': self.direction})


# -*- coding: utf-8 -*-
"""
Created on Fri May 24 15:55:35 2019

@author: congwensheng
"""

row=int(input("enter the number of rows:"))
n=row
while n>=0:
    x="*"*n
    print(x)
    n=n-1


import os
from keras.models import load_model
import tensorflow as tf
import keras
import numpy as np

# path to the trained models
model_file_path = os.path.join('model', 'cnn_model.h5')

model = load_model(model_file_path)
model.summary()

# layer name
layer_name = 'conv2d_1'

# Set up a model that returns the activation values for our target layer
layer = model.get_layer(name=layer_name)
feature_extractor = keras.Model(inputs=model.inputs, outputs=layer.output)

img_width = 96
img_height = 96

def deprocess_image(img):

    # Normalize array: center on 0., ensure variance is 0.15
    img -= img.mean()
    img /= img.std() + 1e-5
    img *= 0.15

    # Center crop
    img = img[25:-25, 25:-25, :]

    # Clip to [0, 1]
    img += 0.5
    img = np.clip(img, 0, 1)

    # Convert to RGB array
    img *= 255
    img = np.clip(img, 0, 255).astype("uint8")

    return img

# The "loss" we will maximize is simply the mean of the activation of a specific filter in our target layer.
# To avoid border effects, we exclude border pixels.
def compute_loss(input_image, filter_index):

    activation = feature_extractor(input_image)

    # We avoid border artifacts by only involving non-border pixels in the loss.
    filter_activation = activation[:, 2:-2, 2:-2, filter_index]

    return tf.reduce_mean(filter_activation)

# Our gradient ascent function simply computes the gradients of the loss above with regard to the input image,
# and update the image so as to move it towards a state that will activate the target filter more strongly.
@tf.function
def gradient_ascent_step(img, filter_index, learning_rate):

    with tf.GradientTape() as tape:
        tape.watch(img)
        loss = compute_loss(img, filter_index)

    # Compute gradients.
    grads = tape.gradient(loss, img)

    # Normalize gradients.
    grads = tf.math.l2_normalize(grads)
    img += learning_rate * grads

    return loss, img

def initialize_image():

    # We start from a gray image with some random noise
    # For floats, the default range is [0, 1)
    img = tf.random.uniform(shape=(1, img_width, img_height, 3))

    # ResNet50V2 expects inputs in the range [-1, +1].
    # Here we scale our random inputs to [-0.125, +0.125]
    # return (img - 0.5) * 0.25
    return img * 20 + 128.

# run gradient ascent for 40 steps
def visualize_filter(filter_index):

    # We run gradient ascent for 40 steps
    iterations = 40

    learning_rate = 10.0
    img = initialize_image()

    for iteration in range(iterations):
        loss, img = gradient_ascent_step(img, filter_index, learning_rate)

    # Decode the resulting input image
    img = deprocess_image(img[0].numpy())

    return loss, img

from IPython.display import Image, display

loss, img = visualize_filter(0)
keras.preprocessing.image.save_img("0.png", img)

display(Image("0.png"))


lista = []
for valor in range(0, 5):
    numero = int(input('Digite um número: '))
    if valor == 0 or numero > lista[-1]:
        lista.append(numero)
    else:
        pos = 0 #varredura
        while pos < len(lista):
            if numero <= lista[pos]:
                lista.insert(pos, numero)
                break
            pos += 1

print(f'Os valores em ordem são {lista}')


from django.shortcuts import get_object_or_404, render
from django.views import generic

from .models import Post
from .forms import CommentForm

class PostList(generic.ListView):
    queryset = Post.objects.filter(status=1).order_by('-created_on')
    # template_name = 'index.html'

# class PostDetail(generic.DetailView):
#     model = Post
#     # template = 'post_detail.html'

def post_detail(request, slug):
    post = get_object_or_404(Post, slug=slug)
    comments = post.comments.filter(active=True)
    new_comment = None

    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)

        if comment_form.is_valid():

            # create comment but don,t save to database yet
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            # save comment to the database
            new_comment.save()
    else:
        comment_form = CommentForm()

    context = {
        'post': post,
        'comments': comments,
        'new_comment': new_comment,
        'comment_form': comment_form,
    }

    return render(request, 'post_detail.html', context)


from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from firebase import firebase
from time import time
from datetime import datetime, timedelta

import json
import subprocess
import random

my_firebase = firebase.FirebaseApplication('https://test-cdf02.firebaseio.com', None)

@csrf_exempt
@require_http_methods(["POST"])
def post1(requst):
    return HttpResponse("ok")

@csrf_exempt
@require_http_methods(["POST"])
def rateDay(request):
    username = request.POST["username"]
    mood = int(request.POST["mood"])
    print(username, mood)
    obj = {int(time()*1000): mood}
    result = my_firebase.patch('https://test-cdf02.firebaseio.com/mood_events/'+username, obj)
    print(result)
    return HttpResponse("ok")

def getUsers(request):
    users = my_firebase.get("/users", None)
    return HttpResponse(json.dumps(users))

def getProjects(request):
    projects = my_firebase.get("/projects", None)
    return HttpResponse(json.dumps(projects))

def index(request):
    #f = open("test2.txt","w")
    output = subprocess.check_output("echo 1+2", shell=True)
    #subprocess.call("./test.sh", shell=True)
    #subprocess.call("bash & ls", shell=True)
    #subprocess.call("ls", shell=True)
    #subprocess.call("echo zdava", shell=True)
    return HttpResponse(output)

def gitClone(request):
    subprocess.call("git clone https://github.com/xgrizx1/bugaton_fe/", shell=True)
    subprocess.call("git --git-dir=./bugaton_fe/.git pull --all")
    output = subprocess.check_output("git --git-dir=./bugaton_fe/.git diff --shortstat dbb5281ef6ea26e85c34bcff8271ee5702be03a8 31d133d3dd9a39d42a654958ff77b9c99be81cb6")
    #subprocess.call("cd bugaton_fe & git pull --all", shell=True)
    #output = subprocess.check_output("cd bugaton_fe & ls", shell=True)
    #subprocess.call("cd bugaton_fe", shell=True)
    #output += subprocess.check_output("cd bugaton_fe & git diff --shortstat dbb5281ef6ea26e85c34bcff8271ee5702be03a8 31d133d3dd9a39d42a654958ff77b9c99be81cb6", shell=True)
    return HttpResponse(output)

def ls(request):
    output = subprocess.check_output("ls", shell=True)
    return HttpResponse(output)

@csrf_exempt
@require_http_methods(["POST"])
def hooks(request):

    json_data = json.loads(request.POST["payload"])
    result = my_firebase.patch('https://test-cdf02.firebaseio.com/test_push2', json_data)

    commits = json_data["commits"]

    for commit in commits:
        print("====================")
        added = 0
        modified = 0
        removed = 0
        try:
            added += len(commit["added"])
        except:
            pass
        try:
            modified += len(commit["modified"])
        except:
            pass
        try:
            removed += len(commit["removed"])
        except:
            pass
        github_username = commit["committer"]["username"]
        print("---------------------------")
        #utc_time = datetime.strptime("2017-09-15T17:13:29.380Z", "%Y-%m-%dT%H:%M:%S.%fZ")
        #utc_time = datetime.strptime(commit["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ")
        #print(utc_time)
        #print(datetime.utcfromtimestamp(0)).total_seconds() * 1000.0)
        #milliseconds = (utc_time - datetime(1970, 1, 1)) // timedelta(milliseconds=1)
            #milliseconds = int((utc_time - datetime.utcfromtimestamp(0)).total_seconds() * 1000.0)
        #print milliseconds
        # print(milliseconds)

        # year = commit["timestamp"][0:4]
        # month = commit["timestamp"][5:7]
        # day = commit["timestamp"][8:10]
        # hour = commit["timestamp"][11:13]
        # minute = commit["timestamp"][14:16]
        # second = commit["timestamp"][17:19]
        # print(added,modified,removed)
        # print(year, month, day)
        # print(hour, minute, second)

        milliseconds = int(time() * 1000)
        obj = {
            milliseconds: {
                "code_quality": random.random(),    #TO DO
                "files_added": added,
                "files_changed": modified,
                "files_removed": removed
            }
        }
        print("Before")
        result = my_firebase.patch('https://test-cdf02.firebaseio.com/git_events/' + github_username, obj)
        print("After")
    return HttpResponse(result)

def getAverageEventsWeekly(request):
    today = int(time() * 1000) // (1000 * 60 * 60 * 24)

    #event_types = ["humidity_events", "temperature_events", "light_events", "motion_events", "noise_events"]
    duck_events = my_firebase.get("/duck_events_mock", None)
    out = {}
    for duck_event_type in duck_events:
        out[duck_event_type] = [0, 0, 0, 0, 0, 0, 0]
        sumEvents = [0, 0, 0, 0, 0, 0, 0]
        cntEvents = [0, 0 ,0 ,0 ,0, 0, 0]
        for duck_id in duck_events[duck_event_type]:
            for event_timestamp in duck_events[duck_event_type][duck_id]:
                value = int(duck_events[duck_event_type][duck_id][event_timestamp])
                day = int(event_timestamp) // (1000 * 60 * 60 * 24)
                daysAgo = today - day
                if (daysAgo < 7):
                    sumEvents[daysAgo] += value
                    cntEvents[daysAgo] += 1
        for i in range(7):
            if cntEvents[i] > 0:
                out[duck_event_type][i] = sumEvents[i] // cntEvents[i]
            else:
                out[duck_event_type][i] = -1
        out[duck_event_type].reverse()
    return HttpResponse(json.dumps(out))

def getAverageMoodsWeekly(request):
    today = int(time() * 1000) // (1000 * 60 * 60 * 24)
    averageMoods = [None, None, None, None, None, None, None]
    sumMoods = [0, 0, 0, 0, 0, 0, 0]
    cntMoods = [0, 0, 0, 0, 0, 0, 0]
    mood_events = my_firebase.get("/mood_events_mock", None)
    for user_id in mood_events:
        print(user_id)
        for timestamp in mood_events[user_id]:
            value = int(mood_events[user_id][timestamp])
            print(value)
            day = int(timestamp) // (1000 * 60 * 60 * 24)
            daysAgo = today - day
            print("d",daysAgo)
            if (daysAgo < 7):
                sumMoods[daysAgo] += value
                cntMoods[daysAgo] += 1

    for i in range(7):
        if cntMoods[i] > 0:
            averageMoods[i] = sumMoods[i] // cntMoods[i]
        else:
            averageMoods[i] = 0

    print(averageMoods)
    averageMoods.reverse()
    print(averageMoods)
    return HttpResponse(json.dumps(averageMoods))

def getLists(request):
    ducks = my_firebase.get("/ducks", None)
    return HttpResponse(json.dumps(ducks))

def getTimestamp(request):
    return HttpResponse(int(time()*1000))

def createModelData():

    my_firebase = firebase.FirebaseApplication('https://test-cdf02.firebaseio.com', None)
    users = my_firebase.get("/users", None)
    # print(users)
    # print("*************")
    mood_events = my_firebase.get("/mood_events_mock", None)
    duck_events = my_firebase.get("/duck_events_mock", None)

    model={}
    for user_id in users:
        model[user_id] = {}
        # print("--------")
        # print(user_id)
        duck_id = users[user_id]['duck_id']
        # print("duck_id: ", duck_id)
        user_mood_events = None

        sume = []
        cnts = []
        avgs = []

        try:
            #all user mood events
            user_mood_events = mood_events[user_id]
            print(user_mood_events)
            #all days
            for timestamp in user_mood_events:
                day = int(timestamp) / (1000 * 60 * 24)
                model[user_id][day] = {}
                # print("day ", day)

                event_types = ["noise_events", "temperature_events"]

                for event_type in event_types:
                    suma = 0
                    cnt = 0
                    try:
                        type_events = duck_events[event_type][duck_id]
                        # print("type_events")
                        # print(type_events)
                        for type_event_timestamp in type_events:
                            type_event_day = int(type_event_timestamp) / (1000 * 60 * 24)
                            # print('type_day', type_event_day)
                            if (day == type_event_day):
                                # print("isti:::::::::::::::::::::::")
                                suma += int(type_events[type_event_timestamp])
                                cnt += 1
                    except:
                        pass
                    # print("**1")
                    if (cnt > 1):
                        user_type_avg = float(suma)/cnt
                        model[user_id][day][event_type] = user_type_avg
                    # print("**2")

        except:
            pass
        #print(user_id)
        #print(user_mood_events)
    # print()
    # print()
    # print(model)

if __name__ == "__main__":
    createModelData()


import sys
from collections import deque
input = sys.stdin.readline

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

n = int(input())
graph = [list(map(int, input().rstrip())) for _ in range(n)]

def bfs():
    q = deque()
    visited = [[-1] * n for _ in range(n)] # 문을 부신 갯수 넣어줄 리스트
    q.append((0, 0))
    visited[0][0] = 0
    while q:
        x, y = q.popleft()
        if x == n - 1 and y == n - 1: # 마지막 끝방에 도달하면 종료
            return visited[x][y]

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == -1: # 방문하지 않았으면
                if graph[nx][ny] == 1: # 흰방일때
                    q.appendleft((nx, ny)) # 흰방부터 탐색해주기 위해 흰방을 맨 앞에 넣는다.
                    visited[nx][ny] = visited[x][y]
                else:
                    q.append((nx, ny)) # 검은방이면 부신거를 +1 해준다
                    visited[nx][ny] = visited[x][y] + 1

print(bfs())


#!/usr/bin/env python3
"""Functional Python Programming

Chapter 10, Example Set 4
"""
# pylint: disable=wrong-import-position

from functools import reduce, partial

display = lambda data: reduce(lambda x, y: print(x, y), data)
sum2 = lambda data: reduce(lambda x, y: x+y**2, data, 0)
sum = lambda data: reduce(lambda x, y: x+y, data, 0)
count = lambda data: reduce(lambda x, y: x+1, data, 0)
min = lambda data: reduce(lambda x, y: x if x < y else y, data)
max = lambda data: reduce(lambda x, y: x if x > y else y, data)

test_reductions = """
>>> import math
>>> d = [ 2, 4, 4, 4, 5, 5, 7, 9 ]
>>> sum2(d)
232
>>> sum(d)
40
>>> count(d)
8
>>> sum(d)/count(d)
5.0
>>> math.sqrt((sum2(d)/count(d))-(sum(d)/count(d))**2)
2.0
>>> max(d)
9
>>> min(d)
2
"""

from typing import Callable, Iterable, TypeVar
T_ = TypeVar("T_")
def map_reduce(
        map_fun: Callable[[T_], T_],
        reduce_fun: Callable[[T_, T_], T_],
        source: Iterable[T_]) -> T_:
    """
    This example doesn't *actually* fit the type definitions!

    This involves a mapping transformation from T1_ to T2_,
    Then the reduce works with T2_.

    Either we have to provide super-complex type definitions,
    or resort to using ``Any``.

    >>> from operator import add
    >>> data = ["2", "3", "5", "7"]
    >>> map_reduce(int, add, data)
    17
    """
    return reduce(reduce_fun, map(map_fun, source))

def sum2_mr(source: Iterable[float]) -> float:
    """
    >>> d = [2, 4, 4, 4, 5, 5, 7, 9]
    >>> sum2_mr(d)
    232
    """
    return map_reduce(lambda y: y**2, lambda x, y: x+y, source)

import operator
def sum2_mr2(source: Iterable[float]) -> float:
    """
    >>> d = [2, 4, 4, 4, 5, 5, 7, 9]
    >>> sum2_mr2(d)
    232
    """
    return map_reduce(lambda y: y**2, operator.add, source)

def count_mr(source: Iterable[float]) -> float:
    """
    >>> d = [2, 4, 4, 4, 5, 5, 7, 9]
    >>> count_mr(d)
    8
    """
    return map_reduce(lambda y: 1, lambda x, y: x+y, source)

def comma_fix(data: str) -> float:
    try:
        return float(data)
    except ValueError:
        return float(data.replace(",", ""))

def clean_sum(
        cleaner: Callable[[str], float],
        data: Iterable[str]
    ) -> float:
    """
    >>> d = ('1,196', '1,176', '1,269', '1,240', '1,307',
    ... '1,435', '1,601', '1,654', '1,803', '1,734')
    >>> clean_sum(comma_fix, d)
    14415.0
    """
    return reduce(operator.add, map(cleaner, data))

sum_p = partial(reduce, operator.add)

test_sump = """
>>> iterable = [2, 4, 4, 4, 5, 5, 7, 9]
>>> sum_p(iterable)
40
>>> sum_p(map(lambda x:x**2, iterable))
232
>>> reduce(lambda x, y: x+y**2, iterable, 0)
232
>>> reduce(lambda x, y: x+y**2, iterable)
230
"""

def performance():
    import timeit
    import sys
    for source_len in range(100, 1000, 100):
        data = repr(['x']*source_len)
        op_r = f'reduce( operator.add, {data}, "" )'
        op_j = f'"".join({data})'
        r = timeit.timeit(
            op_r,
            """from functools import reduce; import operator""")
        j = timeit.timeit(
            op_j)
        print('reduce', source_len, r)
        print('join', source_len, j)
        sys.stdout.flush()

__test__ = {
    "test_reductions": test_reductions,
    "test_sump": test_sump,
}

def test():
    import doctest
    doctest.testmod(verbose=1)

if __name__ == "__main__":
    test()
    # performance()


#!/usr/bin/env python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
import kone_api_decompile as res
import time

from time import sleep

class Elevator(Widget):
	r = NumericProperty(1)
	g = NumericProperty(0)
	b = NumericProperty(0)
	color = ReferenceListProperty(r,g,b)
	floor = -2

	def close_doors(self):
		self.color = (1,0,0)

	def open_doors(self):
		self.color = (0,1,0)

	def change_floor(self, next_floor):
		next_floor = int(next_floor)
		if (next_floor == 0):
			print ("Floor 0 doesn't exist")
		else:
			if (next_floor > self.floor):
				print("Subiendo")
				self.close_doors()
				print("self.center_y before: {}".format(self.center_y))
				if (next_floor < 0):
					self.pos[1] = self.parent.height * (next_floor+3) / 22
				else:
					self.pos[1] = self.parent.height * (next_floor+2) / 22
				self.floor = next_floor
				self.open_doors()

			elif (next_floor < self.floor):
				print("Bajando")
				self.close_doors()
				if (next_floor > 0):
					self.pos[1] = self.parent.height * (next_floor+3) / 22
				else:
					self.pos[1] = self.parent.height * (next_floor+2) / 22
				self.floor = next_floor
				self.open_doors()

			else:
				print("Misma planta")
				self.open_doors()

class JunctionKONE(Widget):
	elevator1 = ObjectProperty(None)
	elevator2 = ObjectProperty(None)
	elevator3 = ObjectProperty(None)
	elevators = [elevator1, elevator2, elevator3]

	def update(self, dt):
		floors_array = res.get_building_floors(res.building_id)
		num_elevators = res.get_number_of_elevators(res.building_id)
		floor1 = 0
		floor2 = 0
		floor3 = 0
		for elevator_number, _ in enumerate(range(num_elevators), start=1):
        	# Read elevator deck state
        	# Elevator numbers start from 1, while for loop starts from 0, thus enumerate here
			(carstate_array, links_array) = res.get_elevator_location(res.building_id, elevator_number)
			cardoor_array = res.get_cardoor_state(links_array["carDoors"])

            # Formatting the printout with fixed width fields
			# print("{:1d}: {:8s}, direction={:4s}, floor={:2s}, weight={:3d}%, doors={:1s}".format(
			# 	elevator_number,
			# 	carstate_array["movingState"],
			# 	carstate_array["movingDirection"],
			# 	floors_array[links_array["currentFloor"]]["floorMark"],
			# 	carstate_array["loadPercent"],", ".join(cardoor_array)))
			# if elevator_number == num_elevators:
			# 	print("")
            # e.g. Default Plan: allowed call rate is 5/second: The delay below aims to try to avoid going above that.
			#time.sleep(0.5)
			if((floors_array[links_array["currentFloor"]]["floorMark"] == "K1") and elevator_number == 1):
				floor1 = -1
			elif((floors_array[links_array["currentFloor"]]["floorMark"] == "K2") and elevator_number == 1):
				floor1 = -2
			elif(elevator_number == 1):
				floor1 = int(floors_array[links_array["currentFloor"]]["floorMark"])

			if((floors_array[links_array["currentFloor"]]["floorMark"] == "K1") and elevator_number == 2):
				floor2 = -1
			elif((floors_array[links_array["currentFloor"]]["floorMark"] == "K2") and elevator_number == 2):
				floor2 = -2
			elif(elevator_number == 2):
				floor2 = int(floors_array[links_array["currentFloor"]]["floorMark"])

			if((floors_array[links_array["currentFloor"]]["floorMark"] == "K1") and elevator_number == 3):
				floor3 = -1
			elif((floors_array[links_array["currentFloor"]]["floorMark"] == "K2") and elevator_number == 3):
				floor3 = -2
			elif(elevator_number == 3):
				floor3 = int(floors_array[links_array["currentFloor"]]["floorMark"])

		#f = open('elevators_data.txt', 'r')
		#elevators_pos = f.readline()

		#print("Position: Elevator 1 --> Floor {}, Elevator 2 --> Floor {}, Elevator 3 --> Floor {}".format(floor1, floor2, floor3))

		#self.elevator1.change_floor(int(elevators_pos.split("\n")[0].split(" ")[0]))
		self.elevator1.change_floor(floor1)
		#self.elevator2.change_floor(int(elevators_pos.split("\n")[0].split(" ")[1]))
		self.elevator2.change_floor(floor2)
		#self.elevator3.change_floor(int(elevators_pos.split("\n")[0].split(" ")[2]))
		self.elevator3.change_floor(floor3)

		#f.close()

	def __init__(self, **kwargs):
		super(JunctionKONE, self).__init__(**kwargs)

class JunctionApp(App):
	def build(self):
		kone = JunctionKONE()
		Clock.schedule_interval(kone.update, 0.25)
		return kone
def startUI():
	JunctionApp().run()

if __name__ == '__main__':
	#response = res.get_building(res.building_id)
 	#print("Trying %s responded with code %s" % (res.building_id, response.status_code))
 	#if response.status_code != 200:
  	#	exit(1)
	JunctionApp().run()
	#startUI()


import logging

class LazyLogger(logging.getLoggerClass()):
    def _log(self, level, msg, args, **kwargs):
        def maybe_callable(x):
            return x() if callable(x) else x

        super()._log(
            level,
            maybe_callable(msg),
            tuple(maybe_callable(i) for i in args),
            **kwargs
        )

logging.setLoggerClass(LazyLogger)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("main")


import pandas as pd
import numpy as np
import os
import glob
import utils
import sys

PREDICTIONS_PATH=os.path.join(".", "to_merge")
LOG_PATH=LOG_PATH = os.path.join(".", "log_Merged")
OUT_PATH=os.path.join(LOG_PATH, utils.timestamp())

print("\n\n")
if not os.path.exists(PREDICTIONS_PATH):
        print("3D predictions folder: '{}' does not exists".format(PREDICTIONS_PATH))
        os.makedirs(PREDICTIONS_PATH)
        print("Created predictions folder: '{}'".format(PREDICTIONS_PATH))
        print("Please copy the 3D predictions .csv.gz files to folder '{}' and run again".format(PREDICTIONS_PATH))
        sys.exit(-1)

list_of_files = glob.glob(os.path.join(PREDICTIONS_PATH, "*.csv.gz"))
file_names = [path.split("/")[-1] for path in list_of_files]

print("Merging files: {}".format(file_names))

merged_preds = None
n=0
for f in list_of_files:
    preds = pd.read_csv(f, header=0,index_col=0).values
    if merged_preds is None:
        merged_preds = preds
    else:
        merged_preds = merged_preds + preds
    n += 1

merged_preds = merged_preds/n
print("Done.")

print("Saving merged 3D predictions and code to '{}'".format(OUT_PATH))

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

utils.generate_submission_3d(merged_preds, os.path.join(OUT_PATH, "submission_merged.csv.gz"))

submission_files = [
    "data.py",
    "hourglass2D_model.py",
    "hourglass3D_model.py",
    "linear_model.py",
    "merge_predictions.py",
    "test_hourglass2D.py",
    "test_hourglass3D.py",
    "test_simple_baseline.py",
    "train_hourglass2D.py",
    "train_hourglass3D.py",
    "train_simple_baseline.py",
    "layers.py",
    "setup.py",
    "utils.py",
    "vis.py"
]
utils.create_zip_code_files(os.path.join(OUT_PATH, "code.zip"), submission_files)

print("Done.")


#!/usr/bin/python3

"""
	TheZZAZZGlitch April Fools 2018 server communication lib

	Written by ISSOtm
	Thanks to Parzival, Niedzejkob, Riley/RoL, and TheZZAZZGlitch

	You are allowed to re-use and modify this module if you credit the people mentioned in this header.
"""

from urllib import request
import codecs
import json
from time import sleep

Poke_chars = [
	"<char 0x0>", "<char 0x1>", "<char 0x2>", "<char 0x3>", "<char 0x4>", "<char 0x5>", "<char 0x6>", "<char 0x7>", "<char 0x8>", "<char 0x9>", "<char 0xa>", "<char 0xb>", "<char 0xc>", "<char 0xd>", "<char 0xe>", "<char 0xf>",
	"<char 0x10>", "<char 0x11>", "<char 0x12>", "<char 0x13>", "<char 0x14>", "<char 0x15>", "<char 0x16>", "<char 0x17>", "<char 0x18>", "<char 0x19>", "<char 0x1a>", "<char 0x1b>", "<char 0x1c>", "<char 0x1d>", "<char 0x1e>", "<char 0x1f>",
	"<char 0x20>", "<char 0x21>", "<char 0x22>", "<char 0x23>", "<char 0x24>", "<char 0x25>", "<char 0x26>", "<char 0x27>", "<char 0x28>", "<char 0x29>", "<char 0x2a>", "<char 0x2b>", "<char 0x2c>", "<char 0x2d>", "<char 0x2e>", "<char 0x2f>",
	"<char 0x30>", "<char 0x31>", "<char 0x32>", "<char 0x33>", "<char 0x34>", "<char 0x35>", "<char 0x36>", "<char 0x37>", "<char 0x38>", "<char 0x39>", "<char 0x3a>", "<char 0x3b>", "<char 0x3c>", "<char 0x3d>", "<char 0x3e>", "<char 0x3f>",
	"<char 0x40>", "<char 0x41>", "<char 0x42>", "<char 0x43>", "<char 0x44>", "<char 0x45>", "<char 0x46>", "<char 0x47>", "<char 0x48>", "<char 0x49>", "<char 0x4a>", "<char 0x4b>", "<char 0x4c>", "<char 0x4d>", "<char 0x4e>", "<char 0x4f>",
	"<NUL>", "<char 0x51>", "<char 0x52>", "<char 0x53>", "<char 0x54>", "<char 0x55>", "<char 0x56>", "<char 0x57>", "<char 0x58>", "<char 0x59>", "<char 0x5a>", "<char 0x5b>", "<char 0x5c>", "<char 0x5d>", "<char 0x5e>", "<char 0x5f>",
	"<char 0x60>", "<char 0x61>", "<char 0x62>", "<char 0x63>", "<char 0x64>", "<char 0x65>", "<char 0x66>", "<char 0x67>", "<char 0x68>", "<char 0x69>", "<char 0x6a>", "<char 0x6b>", "<char 0x6c>", "<char 0x6d>", "<char 0x6e>", "<char 0x6f>",
	"<char 0x70>", "<char 0x71>", "<char 0x72>", "<char 0x73>", "<char 0x74>", "<char 0x75>", "<char 0x76>", "<char 0x77>", "<char 0x78>", "<char 0x79>", "<char 0x7a>", "<char 0x7b>", "<char 0x7c>", "<char 0x7d>", "<char 0x7e>", " ",
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
	"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "(", ")", ":", ";", "[", "]",
	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
	"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "é", "<'d>", "<'l>", "<'s>", "<'t>", "<'v>",
	"<char 0xc0>", "<char 0xc1>", "<char 0xc2>", "<char 0xc3>", "<char 0xc4>", "<char 0xc5>", "<char 0xc6>", "<char 0xc7>", "<char 0xc8>", "<char 0xc9>", "<char 0xca>", "<char 0xcb>", "<char 0xcc>", "<char 0xcd>", "<char 0xce>", "<char 0xcf>",
	"<char 0xd0>", "<char 0xd1>", "<char 0xd2>", "<char 0xd3>", "<char 0xd4>", "<char 0xd5>", "<char 0xd6>", "<char 0xd7>", "<char 0xd8>", "<char 0xd9>", "<char 0xda>", "<char 0xdb>", "<char 0xdc>", "<char 0xdd>", "<char 0xde>", "<char 0xdf>",
	"'", "<Pk>", "<Mn>", "-", "<'r>", "<'m>", "?", "!", ".", "<char 0xe9>", "<char 0xea>", "<char 0xeb>", "<empty cursor>", "<full cursor>", "<down arrow>", "<male symbol>",
	"$", "<mult>", "<. (duplicate)>", "/", ",", "<male symbol>", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
]

def Pokeify(str):
	return encode_str(str, Poke_chars)

def DePokeify(bytes):
	return decode_str(bytes, Poke_chars)

ZZAZZ_chars = [
	"<NUL>", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
	"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e",
	"f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
	"v", "w", "x", "y", "z", "!", ",", "?", "\"", "-", ".", ":", "(", ")", " ", "< 0x3F>",
	"$", "'", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "é", "<Deliria A>", "<Deliria B>",
	"<Deliria C>", "<Deliria D>", "<Deliria E>", "<Deliria F>", "<Deliria G>", "<Deliria H>", "<Deliria I>", "<Deliria J>", "<Deliria K>", 	"<Deliria L>", "<Deliria M>", "<Deliria N>", "<Deliria O>", "<Deliria P>", "<Deliria Q>", "<Deliria R>",
	"<Deliria S>", "<Deliria T>", "<Deliria U>", "<Deliria V>", "<Deliria W>", "<Deliria X>", "<Deliria Y>", "<Deliria Z>", "<Deliria a>", 	"<Deliria b>", "<Deliria c>", "<Deliria d>", "<Deliria e>", "<Deliria f>", "<Deliria g>", "<Deliria h>",
	"<Deliria i>", "<Deliria j>", "<Deliria k>", "<Deliria l>", "<Deliria m>", "<Deliria n>", "<Deliria o>", "<Deliria p>", "<Deliria q>", 	"<Deliria r>", "<Deliria s>", "<Deliria t>", "<Deliria u>", "<Deliria v>", "<Deliria w>", "<Deliria x>",
	"<Deliria y>", "<Deliria z>", "<Deliria !>", "<Deliria ?>", "<Deliria *>", "<Deliria .>", "<Deliria :>", "<Deliria (>", "<Deliria )>", "<Deliria shrink.>", "<Deliria shrink!>", "<Deliria shrink?>", "<char 0x8b>", "<char 0x8c>", "<Braille A>", "<Braille B>", "<Braille C>",
	"<Braille D>", "<Braille E>", "<Braille F>", "<Braille G>", "<Braille H>", "<Braille I>", "<Braille J>", "<Braille K>", "<Braille L>", "<Braille M>", "<Braille N>", "<Braille O>", "<Braille P>", "<Braille Q>", "<Braille R>", "<Braille S>",
	"<Braille T>", "<Braille U>", "<Braille V>", "<Braille W>", "<Braille X>", "<Braille Y>", "<Braille Z>", "<char 0xa7>", "<char 0xa8>", "<char 0xa9>", "<char 0xaa>", "<char 0xab>", "<char 0xac>", "<char 0xad>", "<char 0xae>", "<char 0xaf>",
	"<char 0xb0>", "<char 0xb1>", "<char 0xb2>", "<char 0xb3>", "<char 0xb4>", "<char 0xb5>", "<char 0xb6>", "<char 0xb7>", "<char 0xb8>", "<char 0xb9>", "<char 0xba>", "<char 0xbb>", "<char 0xbc>", "<char 0xbd>", "<char 0xbe>", "<char 0xbf>",
	"<char 0xc0>", "<char 0xc1>", "<char 0xc2>", "<char 0xc3>", "<char 0xc4>", "<char 0xc5>", "<char 0xc6>", "<char 0xc7>", "<char 0xc8>", "<char 0xc9>", "<char 0xca>", "<char 0xcb>", "<char 0xcc>", "<char 0xcd>", "<char 0xce>", "<char 0xcf>",
	"<char 0xd0>", "<char 0xd1>", "<char 0xd2>", "<char 0xd3>", "<char 0xd4>", "<char 0xd5>", "<char 0xd6>", "<char 0xd7>", "<char 0xd8>", "<char 0xd9>", "<char 0xda>", "<char 0xdb>", "<char 0xdc>", "<char 0xdd>", "<char 0xde>", "<char 0xdf>",
	"<char 0xe0>", "<char 0xe1>", "<char 0xe2>", "<char 0xe3>", "<char 0xe4>", "<char 0xe5>", "<char 0xe6>", "<char 0xe7>", "<char 0xe8>", "<char 0xe9>", "<char 0xea>", "<char 0xeb>", "<char 0xec>", "<char 0xed>", "<char 0xee>", "<char 0xef>",
	"<char 0xf0>", "<control char 0xf1 NEXTPAGE>", "<control char 0xf2 SECONDLINE>", "<control char 0xf3 WAITBUTTONCLEAR>",
	"<control char 0xf4 WAITBUTTON>", "<control char 0xf5 CALLSUBSTR>", "<control char 0xf6 RETSUBSTR>", "<control char 0xf7 SETFONT>", "<control char 0xf8 PLAYSFX>", "<char 0xf9>", "<char 0xfa>", "<char 0xfb>", "<char 0xfc>", "<char 0xfd>", "<char 0xfe>", "<char 0xff>"
]

def ZZAZZify(str):
	return encode_str(str, ZZAZZ_chars)

def DeZZAZZify(bytes):
	return decode_str(bytes, ZZAZZ_chars)

def encode_str(str, char_list):
	byte_list = []

	for char in str:
		byte_list.append(char_list.index(char))

	return bytes(byte_list)

def decode_str(bytes, char_list):
	str = ""

	for byte in bytes:
		str += char_list[byte]

	return str

class Packet:

	TYPE_PING     = 0x01
	TYPE_MAP_NAME = 0x03
	TYPE_MAP      = 0x04
	TYPE_TREND    = 0x06
	TYPE_LOTTERY  = 0x07
	TYPE_THERAPY  = 0x0A
	TYPE_CAVERN4  = 0x77

	def __init__(self, type = 0, data = []):
		self.set_type(type)
		# Set data and length
		self.set_data(data)
		self.write_checksum()

	def from_bytes(bytes):
		packet = Packet(bytes[4], list(bytes[5:]))
		packet.checksum = (bytes[2], bytes[3])

		return packet

	def map_name_packet(map_id):
		return Packet(Packet.TYPE_MAP_NAME, [0] + [0xFF] * 0x28 + [map_id % 0x100, map_id // 0x100])

	def map_packet(map_id):
		return Packet(Packet.TYPE_MAP, [map_id // 0x100, map_id % 0x100])

	def trend_packet(trend_str):
		return Packet(Packet.TYPE_TREND, ZZAZZify(trend_str) + b"\xf6")

	def lottery_packet():
		return Packet(Packet.TYPE_LOTTERY, [0xFF])

	def therapy_packet():
		return Packet(Packet.TYPE_THERAPY, [0xFF])

	def cavern4_packet():
		return Packet(Packet.TYPE_CAVERN4, [0x13, 0x37, 0xCC])

	def __repr__(self):
		return "Length: {}\nType: {}\nData: {}".format(self.length, self.type, ",".join([hex(byte) for byte in self.data]))

	def __str__(self):
		return ",".join([hex(byte) for byte in self.get_bytes()])

	def __len__(self):
		return self.length

	def get_data(self):
		return self.data

	def get_type(self):
		return self.type

	def set_data(self, data):
		if len(data) > 0xFFFF:
			raise ValueError("Packet too large! (at most 0xFFFF bytes long, header included)")

		self.data = data
		self.length = len(self.data) + 5 # Include header size

	def set_type(self, type):
		assert type >= 0 and type < 0x100, "Type must be an unsigned byte"

		self.type = type

	def calc_checksum(self):
		checksum1 = 0xa5 # e
		checksum2 = 0x5a # d

		for byte in self.data:
			checksum1 += byte
			checksum2 ^= byte

		return (checksum1 & 0xFF),checksum2

	def write_checksum(self):
		self.checksum = self.calc_checksum()

	def verify_checksum(self):
		return self.checksum == self.calc_checksum()

	def get_bytes(self):
		raw_bytes = []

		# Length
		raw_bytes.append(self.length  % 0x100)
		raw_bytes.append(self.length // 0x100)

		# Checksum
		raw_bytes.append(self.checksum[0])
		raw_bytes.append(self.checksum[1])

		# Type
		raw_bytes.append(self.type)

		# Data
		raw_bytes.extend(self.data)

		return bytes(raw_bytes)

class Connection:

	def __init__(self, ip="167.99.192.164", port="12709", token = ""):
		self.ip = ip
		self.port = port
		self.token = token

	def request(self, path, data):
		if (isinstance(data, str)):
			data = bytes(data, "utf8")
		headers = {'Accept-Identity': 'identity',  'Connection': 'close', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': ' Python-urllib/3.5', 'Host': "{}:{}".format(self.ip, self.port)}
		req = request.Request("http://{}:{}".format(self.ip, self.port) + path, data, headers)
		return req

	def register_account(self, username, password, message):
		req = self.request("/api/register", "username={}&password={}&message={}".format(username, password, message))
		return request.urlopen(req).read()

	def get_token(self, username, password):
		req = self.request("/api/relogin", "username={}&password={}".format(username, password))
		response = request.urlopen(req).read().decode("utf8")
		token = json.loads(response)['sessid']
		self.token = token
		return token

	def send_packet(self, packet, retry_if_503 = True):
		packet.write_checksum()

		req = self.request("/req/" + self.token, codecs.encode(packet.get_bytes(), "base64")[:-1]) # strip trailing newline

		try:
			response = request.urlopen(req)
		except request.HTTPError as error:
			code = error.getcode()

			# The server rejects requests sent too quickly ; if it happens, hold on for a bit, then try again
			if code == 503 and retry_if_503:
				print("Might've hit rate limiter. Trying again in 5 seconds.")
				sleep(5)
				self.send_packet(packet, False)

			else:
				raise

		return Packet.from_bytes( codecs.decode(response.read(), "base64") )

class Map:

	def __init__(self, map_id, connection):
		map_name_packet = connection.send_packet( Packet.map_name_packet(map_id) )
		map_packet = connection.send_packet( Packet.map_packet(map_id) )

		raw_name_data = map_name_packet.get_data()
		raw_name = []
		i = 0
		while raw_name_data[i] != 0x50:
			raw_name.append(raw_name_data[i])
			i += 1
		self.name = DePokeify(raw_name)

		self.raw_data = map_packet.get_data()
		ptr = 0xB800
		def get_byte():
			nonlocal ptr
			ptr += 1
			return self.raw_data[ptr - 0xB801]

		def get_ptr():
			return get_byte() + get_byte() * 256

		def get_bytes(size):
			nonlocal ptr
			data = self.get_data_at(ptr, size)
			ptr += size
			return data

		self.tilesetID = get_byte()
		self.height = get_byte()
		self.width = get_byte()
		self.map_data_ptr = get_ptr()
		self.map_text_ptr = get_ptr()
		self.map_script_ptr = get_ptr()
		self.connections = get_byte()
		self.obj_data_ptr = get_ptr()
		self.loading_script_ptr = get_ptr()
		self.setup_ptr = get_ptr()
		ptr += 1
		self.musicID = get_byte()
		self.music_bank = get_byte()
		self.warp_list = []
		for i in range(4):
			self.warp_list.append(get_bytes(5))

	def __repr__(self):
		raw_dump = ""
		cnt = 0
		for byte in self.raw_data:
			raw_dump += hex(byte)[2:].zfill(2)
			cnt += 1
			if cnt == 16:
				cnt = 0
				raw_dump += "\n"
		if cnt != 0:
			raw_dump += "\n"

		warp_dirs = ["West", "East", "North", "South"]
		warp_data = []
		for warp in self.warp_list:
			if warp[0] > 0 and warp[0] < 5:
				warp_dir = warp_dirs[warp[0] - 1]
			else:
				warp_dir = "Unknown ({})".format(hex(warp[0]))

			warp_data.append("{} to map {}, (X,Y) ({}, {})".format( warp_dir, hex(warp[1] * 256 + warp[2]), warp[3], warp[4] ))

		return """
Name: {}
Height: {}
Width: {}
Map data ptr: {}
Map text ptr: {}
Map script ptr: {}
Connections: {}
Obj data ptr: {}
Loading script ptr: {}
Setup ptr: {}
Music ID: {}
Music bank: {}
Warp list: {}

Raw dump:
{}
ZZAZZ text dump:
{}

Poké text dump:
{}
		""".format(
			self.name,
			self.height,
			self.width,
			hex(self.map_data_ptr),
			hex(self.map_text_ptr),
			hex(self.map_script_ptr),
			hex(self.connections),
			hex(self.obj_data_ptr),
			hex(self.loading_script_ptr),
			hex(self.setup_ptr),
			hex(self.musicID),
			self.music_bank,
			"{" + "; ".join(warp_data) + "}",

			raw_dump,
			DeZZAZZify(self.raw_data),
			DePokeify(self.raw_data)
		)

	def get_data_at(self, ptr, size):
		return self.raw_data[ptr - 0xB800 : ptr - 0xB800 + size]

def play_lottery(connection):
	data = connection.send_packet( Packet.lottery_packet() ).get_data()

	nb_matches = data[0]
	letters_raw = []
	index = 1
	while data[index] != 0xF6:
		letters_raw.append(data[index])
		index += 1

	return nb_matches, DeZZAZZify(letters_raw)

def get_fun_value(connection):
	return connection.send_packet(Packet.map_name_packet(0x0110)).get_data()[-1]

def set_fun_value(connection):
	connection.send_packet(Packet.therapy_packet())
	return get_fun_value(connection)

def update_trend(connection):
	trendsetters_map = Map(0x0E3C, connection)
	trendsetter_name = trendsetters_map.get_data_at(0xBAFC, 0x10)
	trend = trendsetters_map.get_data_at(0xBAEC, 0x10)

	trendsetter_name = DeZZAZZify( trendsetter_name[:trendsetter_name.index(0xF6)] )
	trend = DeZZAZZify( trend[:trend.index(0xF6)] )
	return trendsetter_name, trend

def set_trend(trend_str, connection):
	return connection.send_packet(Packet.trend_packet(trend_str))


from typing import (
    List,
    Tuple,
)
from decimal import Decimal
from hummingbot.connector.exchange_base import ExchangeBase
from hummingbot.connector.exchange.paper_trade import create_paper_trade_market
from hummingbot.strategy.market_trading_pair_tuple import MarketTradingPairTuple
from hummingbot.strategy.arbitrage.arbitrage_market_pair import ArbitrageMarketPair
from hummingbot.strategy.order_book_asset_price_delegate import OrderBookAssetPriceDelegate
from hummingbot.strategy.asset_price_delegate import AssetPriceDelegate
from hummingbot.strategy.arbitrage.arbitrage import ArbitrageStrategy
from hummingbot.strategy.arbitrage.arbitrage_config_map import arbitrage_config_map

def start(self):
    primary_market = arbitrage_config_map.get("primary_market").value.lower()
    secondary_market = arbitrage_config_map.get("secondary_market").value.lower()
    raw_primary_trading_pair = arbitrage_config_map.get("primary_market_trading_pair").value
    raw_secondary_trading_pair = arbitrage_config_map.get("secondary_market_trading_pair").value
    min_profitability = arbitrage_config_map.get("min_profitability").value / Decimal("100")
    use_oracle_conversion_rate = arbitrage_config_map.get("use_oracle_conversion_rate").value
    rate_conversion_sources = {
        'base': arbitrage_config_map.get("base_rate_conversion_source").value,
        'quote': arbitrage_config_map.get("quote_rate_conversion_source").value,
    }
    secondary_to_primary_base_conversion_rate = arbitrage_config_map["secondary_to_primary_base_conversion_rate"].value
    secondary_to_primary_quote_conversion_rate = arbitrage_config_map["secondary_to_primary_quote_conversion_rate"].value
    conversion_ext_market_price_types = {
        'base': arbitrage_config_map.get("base_conversion_ext_market_price_type").value,
        'quote': arbitrage_config_map.get("quote_conversion_ext_market_price_type").value,
    }
    conversion_ext_market_markets = {
        'base': arbitrage_config_map.get("base_conversion_ext_market_market").value,
        'quote': arbitrage_config_map.get("quote_conversion_ext_market_market").value,
    }
    conversion_ext_market_exchanges = {
        'base': arbitrage_config_map.get("base_conversion_ext_market_exchange").value,
        'quote': arbitrage_config_map.get("quote_conversion_ext_market_exchange").value,
    }

    try:
        primary_trading_pair: str = raw_primary_trading_pair
        secondary_trading_pair: str = raw_secondary_trading_pair
        primary_assets: Tuple[str, str] = self._initialize_market_assets(primary_market, [primary_trading_pair])[0]
        secondary_assets: Tuple[str, str] = self._initialize_market_assets(secondary_market,
                                                                           [secondary_trading_pair])[0]
    except ValueError as e:
        self._notify(str(e))
        return

    market_names: List[Tuple[str, List[str]]] = [(primary_market, [primary_trading_pair]),
                                                 (secondary_market, [secondary_trading_pair])]
    # Add Asset Price Delegate markets to main markets if already using the exchange.
    for asset_type in ['base', 'quote']:
        if rate_conversion_sources[asset_type] == "external_market":
            ext_exchange: str = conversion_ext_market_exchanges[asset_type]
            if ext_exchange in [primary_market, secondary_market]:
                asset_pair: str = conversion_ext_market_markets[asset_type]
                market_names.append((ext_exchange, [asset_pair]))

    self._initialize_wallet(token_trading_pairs=list(set(primary_assets + secondary_assets)))
    self._initialize_markets(market_names)
    self.assets = set(primary_assets + secondary_assets)

    primary_data = [self.markets[primary_market], primary_trading_pair] + list(primary_assets)
    secondary_data = [self.markets[secondary_market], secondary_trading_pair] + list(secondary_assets)
    self.market_trading_pair_tuples = [MarketTradingPairTuple(*primary_data), MarketTradingPairTuple(*secondary_data)]
    self.market_pair = ArbitrageMarketPair(*self.market_trading_pair_tuples)

    # Asset Price Feed Delegates
    rate_conversion_delegates = {'base': None, 'quote': None}
    shared_ext_mkt = None
    # Initialize price delegates as needed for defined price sources.
    for asset_type in ['base', 'quote']:
        price_source: str = rate_conversion_sources[asset_type]
        if price_source == "external_market":
            # For price feeds using other connectors
            ext_exchange: str = conversion_ext_market_exchanges[asset_type]
            asset_pair: str = conversion_ext_market_markets[asset_type]
            if ext_exchange in list(self.markets.keys()):
                # Use existing market if Exchange is already in markets
                ext_market = self.markets[ext_exchange]
            else:
                # Create markets otherwise
                UseSharedSource = (rate_conversion_sources['quote'] == rate_conversion_sources['base'] and
                                   conversion_ext_market_exchanges['quote'] == conversion_ext_market_exchanges['base'])
                # Use shared paper trade market if both price feeds are on the same exchange.
                if UseSharedSource and shared_ext_mkt is None and asset_type == 'base':
                    # Create Shared paper trade if not existing
                    shared_ext_mkt = create_paper_trade_market(conversion_ext_market_exchanges['base'],
                                                               [conversion_ext_market_markets['base'], conversion_ext_market_markets['quote']])
                ext_market = shared_ext_mkt if UseSharedSource else create_paper_trade_market(ext_exchange, [asset_pair])
                if ext_exchange not in list(self.markets.keys()):
                    self.markets[ext_exchange]: ExchangeBase = ext_market
            rate_conversion_delegates[asset_type]: AssetPriceDelegate = OrderBookAssetPriceDelegate(ext_market, asset_pair)

    self.strategy = ArbitrageStrategy(market_pairs=[self.market_pair],
                                      min_profitability=min_profitability,
                                      logging_options=ArbitrageStrategy.OPTION_LOG_ALL,
                                      use_oracle_conversion_rate=use_oracle_conversion_rate,
                                      secondary_to_primary_base_conversion_rate=secondary_to_primary_base_conversion_rate,
                                      secondary_to_primary_quote_conversion_rate=secondary_to_primary_quote_conversion_rate,
                                      base_rate_conversion_delegate=rate_conversion_delegates['base'],
                                      quote_rate_conversion_delegate=rate_conversion_delegates['quote'],
                                      base_conversion_ext_market_price_type=conversion_ext_market_price_types['base'],
                                      quote_conversion_ext_market_price_type=conversion_ext_market_price_types['quote'],
                                      hb_app_notification=True)


"""
python prep_macro_data.py --vintage='202209'
"""

import os
import sys
import argparse
import yaml
import pickle
import wget

import pandas as pd
import numpy as np
import datetime
from scipy import stats
import collections

parser = argparse.ArgumentParser(description='')
parser.add_argument('--vintage', type=str, help='vintage', default='202209')

def main():

    global args
    args = parser.parse_args()

    setattr(args, 'first_data_date', pd.to_datetime("1973-03-31"))
    args.winsorize = None

    filepath = f'data/macro/{args.vintage}_Qraw.csv'
    if not os.path.exists(filepath):
        url = 'https://files.stlouisfed.org/files/htdocs/fred-md/quarterly/current.csv'
        new_filepath = f'data/macro/{datetime.datetime.now().strftime("%Y%m")}_Qraw.csv'
        filepath_downloaded = wget.download(url,new_filepath)
        print(f' >> {filepath} does not exists. Downloaded the latest from FRED-QD and saved as {new_filepath}')
        setattr(args, 'vintage', datetime.datetime.now().strftime("%Y%m"))
        filepath = new_filepath

    ## read in raw quarterly data
    QD_raw = pd.read_csv(filepath)
    QD_raw = QD_raw.iloc[2:,:].copy()
    QD_raw['sasdate'] = pd.to_datetime(QD_raw['sasdate'],format='%m/%d/%Y')
    QD_raw['sasdate'] = [date - pd.tseries.offsets.DateOffset(days=1) + pd.tseries.offsets.QuarterEnd() for date in QD_raw['sasdate']]
    QD_raw.rename(columns={'sasdate':'date'},inplace=True)
    QD_raw = QD_raw.set_index('date').dropna(how='all')
    print(f"Stage: read in raw data; last data date in QD_raw = {QD_raw.index[-1].strftime('%Y-%m-%d')}")

    ## prepare variable and their respective group information
    datadict = pd.read_excel(f'data/macro/dictionary.xlsx',sheet_name='SVAR')
    datadict = datadict.loc[datadict['SVAR_GROUP'].notna()].sort_values(by=['SVAR_GROUP','MNEMONIC'])

    variables = datadict[['MNEMONIC','DESCRIPTION','TCODE','SVAR_GROUP']].set_index('MNEMONIC').to_dict('index') # dict(zip(datadict['MNEMONIC'], datadict['DESCRIPTION']))
    assert list(variables.keys()) == list(datadict['MNEMONIC'])
    group_info = datadict[['SVAR_GROUP','MNEMONIC']]

    QD = prep_data(QD_raw, variables)
    A_NZ, df_by_group = prep_prior(group_info)

    meta_data = {'df': QD,
                 'A_NZ': A_NZ,
                 'group_info': dict(zip(df_by_group['SVAR_GROUP'],df_by_group['MNEMONIC'])),
                 'description': dict(zip(datadict['MNEMONIC'],datadict['DESCRIPTION']))
                }
    out_filepath = f'data/macro/{args.vintage}.pickle'

    with open(out_filepath, 'wb') as handle:
        pickle.dump(meta_data, handle, protocol = pickle.HIGHEST_PROTOCOL)

    print(f'[FILE SAVED {datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S")}] {out_filepath}; keys={list(meta_data.keys())}')
    return 0

def prep_data(QD_raw, variables):

    print('Stage: checking missing values')
    ## I: filter to required columns, check missing value & prep for imputation
    QD = QD_raw.loc[QD_raw.index >= args.first_data_date - pd.tseries.offsets.DateOffset(days=367) + pd.tseries.offsets.QuarterEnd(), list(variables.keys())].copy()

    variables_needs_imputation = {}
    for col in QD.columns.to_list():
        if any(QD[col].isna()):
            missing_dates = QD.index[QD[col].isna()].to_list()
            missing_dates_filtered = list(filter(lambda x: x >= args.first_data_date, missing_dates))
            if len(missing_dates_filtered):
                variables_needs_imputation[col] = missing_dates_filtered
                print(f' >> {col} ({variables[col]["DESCRIPTION"]}) has NA values on {[x.strftime("%Y-%m-%d") for x in missing_dates_filtered]} and will be imputed')

    ## II: handle missing values
    if len(variables_needs_imputation):
        print('Stage: impute missing values')
        for col, missing_dates in variables_needs_imputation.items():
            if any(QD[col].isna()):
                QD[col].ffill(inplace=True)
                print(f' >> value for {col} has been forward-filled')
    else:
        print(' >> no need for missing values imputation')
    Qlevels = QD.copy()

    ## III: applying transf
    print('Stage: applying transformation and touch up')
    for col in QD.columns:
        QD[col] = apply_tcode(QD[col],variables[col]['TCODE'],freq=4)

    ## IV: filtering, winsorizing, checking
    QD = QD[QD.index >= args.first_data_date]
    if args.winsorize is not None:
        for col in QD.columns:
             QD[col] = stats.mstats.winsorize(QD[col], limits=args.winsorize)
    for col in QD.columns.to_list():
        if any(QD[col].isna()):
            print(f'!! {col} has NA values on {QD.index[QD[col].isna()]}; this should not happen')

    print(f" >> first_data_date = {QD.index[0].strftime('%Y-%m-%d')}; last_data_date = {QD.index[-1].strftime('%Y-%m-%d')}; QD.shape = {QD.shape}")
    return QD

def prep_prior(group_info):

    print('Stage: prep priors; ', end='')

    n = group_info.shape[0]
    counter = list(collections.Counter(group_info['SVAR_GROUP']).values())
    boundaries = np.cumsum(counter)

    A_NZ = np.zeros((n,n))
    for i, group_end in enumerate(boundaries):
        group_start = 0 if i == 0 else boundaries[i-1]
        A_NZ[group_start:group_end, :group_end] = 1
    for i in range(n):
        A_NZ[i,i] = 0
    print(f'A_NZ.shape = {A_NZ.shape}')

    df_by_group = group_info.groupby('SVAR_GROUP',as_index=False).agg({'MNEMONIC':list})
    return A_NZ, df_by_group

def apply_tcode(x,tcode,freq=4):

    ## x: pd.Series -> the raw time series to be processed
    ## tcode: int -> transformation code; 1-7 are what FRED db used; 8- are customized
    if tcode == 1:
        ret = x
    elif tcode == 2: # diff
        ret = x.diff()
    elif tcode == 3: ## twice diff
        dx = x.diff()
        ret = dx.diff()
    elif tcode == 4: ## log
        ret = np.log(x)
    elif tcode == 5: ## 100\delta log (approx for pct change)
        logx = np.log(x)
        ret = 100 * logx.diff()
    elif tcode == 6: ## twice delta log (change in growth rate)
        logx = np.log(x)
        dlogx = logx.diff()
        ret = 100 * dlogx.diff()
    elif tcode == 7: ## diff of pct change (change in growth rate)
        pct_change = x.pct_change()
        ret = 100 * pct_change.diff()
    #### customized
    elif tcode == 8: ## QoQ AR
        pct_change = x.pct_change()
        ret = 100 * ((1+pct_change)**4-1)
    elif tcode == 9: ## YoY
        ret = 100 * (x.pct_change(freq))
    elif tcode == 10: ## divided by 10
        ret = x / 10.0
    elif tcode == 11: ## divided by 100
        ret = x / 100.0
    elif tcode == 12: ## 100*log
        ret = 100 * np.log(x)
    elif tcode == 13: ## 100*x
        ret = 100*x
    else:
        raise ValueError(f'tcode = {tcode}, unrecognized')
    return ret

if __name__ == "__main__":
    main()


import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckanext.datastore.interfaces import IDatastoreBackend
from backend.postgres import VersionedDatastorePostgresqlBackend
from ckanext.datacitation.logic.action import datacitation_querystore_resolve
from ckanext.datacitation.helpers import datacitation_show_citation_info

class DatacitationPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IConfigurer)
    plugins.implements(IDatastoreBackend)
    plugins.implements(plugins.IRoutes,inherit=True)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.ITemplateHelpers)

    # IConfigurer
    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'ckanext-datacitation')

    # IDatastoreBackend
    def register_backends(self):
        return {
            u'postgresql': VersionedDatastorePostgresqlBackend,
            u'postgres': VersionedDatastorePostgresqlBackend,

        }

    # ITemplateHelpers
    def get_helpers(self):

        return {
            'show_citation_info':datacitation_show_citation_info
        }

    # IRoutes
    def before_map(self,map):

        map.connect('querystore.view', '/querystore/view_query',
                  controller='ckanext.datacitation.controller:QueryStoreController',
                  action='view_history_query')

        map.connect('querystore.dump', '/querystore/dump_history_result_set',
                  controller='ckanext.datacitation.controller:QueryStoreController',
                  action='dump_history_result_set')
        return map

    # IActions
    def get_actions(self):
        actions = {
            'querystore_resolve': datacitation_querystore_resolve

        }

        return actions


#import all the necessaries
import os
import random
import time
from multiprocessing.connection import wait

i = int(0)
while i < 3:
    os.system("VerifyIntegrity.py")
    i += 1

#ask the user what number of searches he wants to do nad id not an integer re ask
while True:
    try:
        numberOfSearches = int(input("How many searches do you want to do? "))
        break
    except ValueError:
        print("Please enter a number")
print("everything is ok, starting the program")

import pyautogui

pyautogui.FAILSAFE = False

def FIsMatching():
    matches = False
    while matches == False:
        if pyautogui.pixelMatchesColor(20, 20, (16, 110, 191, 255)):
            pyautogui.moveTo(450, 250)
            matches = True
        else:
            matches = False

def GIsMatching():
    pxielx = 29
    pxiely = 18
    matches = False
    while matches == False:
        if pyautogui.pixelMatchesColor(pxielx, pxiely, (17, 123, 214)):
            pyautogui.moveTo(450, 250)
            matches = True
        else:
            matches = False
            #print the current color of the pixel
            print(pyautogui.pixel(pxielx, pxiely))
            #do a point where the pixel is
            pyautogui.moveTo(pxielx, pxiely)

def EIsMatching():
    matches = False
    while matches == False:
        if pyautogui.pixelMatchesColor(68, 15, (16, 110, 191, 255)):
            pyautogui.moveTo(450, 250)
            matches = True
        else:
            matches = False

with open('preferences', 'r') as file:
        preferences = file.read().splitlines()
        lang = preferences[0].split(':')[1]
        browser = preferences[1].split(':')[1]
        webBrowserIconColomn = int(preferences[2].split(':')[1])
        isPath = preferences[3].split(':')[1]
        browserPath = preferences[4].split(':')[1]

PixelResult = 120 + (webBrowserIconColomn * 50)

pyautogui.click(PixelResult, 745)

time.sleep(1.5)

def RandomWord():
    random_word = ''.join(random.choice(
        'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for _ in range(10))
    pyautogui.typewrite(random_word, interval=0.05)
    pyautogui.press("enter")

def EnterBing():
    pyautogui.typewrite("bing.com")
    pyautogui.press("enter")

def TypeEnterSpam():
    pyautogui.keyDown('ctrl')
    pyautogui.press('backspace')
    pyautogui.keyUp('ctrl')
    random_word = ''.join(random.choice(
            'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for _ in range(10))
    pyautogui.typewrite(random_word, interval=0.01)
    pyautogui.press("enter")

if browser == 'firefox':
    #click on the search bar
    pyautogui.click(300, 65)
    #enter the word "bing" and press enter
    EnterBing()
    FIsMatching()
    RandomWord()
    #do a loop that repeat 30 times so the cursor move first to the pixel 400, 150 then press backspace 10 times then redo the same thing of the random word but wait 1s after each times
    for i in range(numberOfSearches):
        pyautogui.click(400, 150)
        TypeEnterSpam()
        FIsMatching()

elif browser == 'chrome':
    #click on the search bar
    pyautogui.click(330, 50)
    #enter the word "bing" and press enter
    EnterBing()
    #get the 20, 20 pixel color and if its rgba(16,110,191,255) then set the mouse to the pixel: 450, 250 and if not print the color with rgba and ait 0.5s and repeat
    GIsMatching()
    #enter random word with a-z A-Z 0-9 and with 10 characters
    RandomWord()
    #do a loop that repeat 30 times so the cursor move first to the pixel 400, 150 then press backspace 10 times then redo the same thing of the random word but wait 1s after each times
    for i in range(numberOfSearches):
        pyautogui.click(350, 110)
        TypeEnterSpam()

        GIsMatching()


import time

def main():
    infile = open('reads.txt', 'rb')
    CopyFile = open('reads-copy.txt', 'wb')

    for line in infile:
        print(line.strip(), file=CopyFile)
        print('.', end='', flush=True)

    CopyFile.close()
    print('\nDone........')

if __name__ == '__main__':
    main()


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

from django.contrib import admin
from displaySite.models import Donation, Donater, Admin, Group

# Register your models here.

#Вычисляет сколько задонатил каждый пользователь
def count_donations_for_donater(modeladmin, request, queryset):
  for donater in queryset:
    sum = 0
    for donation in Donation.objects.filter(donation_donater=donater.donater_id):
      sum += donation.donation_size
      donater.donater_summOfAllDonations = sum
    donater.save()

#Вычисляет сколько задонатили в группе
def count_donations_for_group(modeladmin, request, queryset):
  for group in queryset:
    sum = 0
    for donation in Donation.objects.filter(donation_group=group.group_id):
      sum += donation.donation_size
      group.group_summOfAllDonations = sum
    group.save()

count_donations_for_donater.short_description = "Count donations!"
count_donations_for_group.short_description = "Count donations!"

class DonaterAdmin(admin.ModelAdmin):
  actions = [count_donations_for_donater]

class GroupAdmin(admin.ModelAdmin):
  actions = [count_donations_for_group]

admin.site.register(Donation)
admin.site.register(Donater, DonaterAdmin)
admin.site.register(Admin)
admin.site.register(Group, GroupAdmin)


import matplotlib.pyplot as plt

#plt.xkcd()

import pygimli as pg
import pygimli.meshtools as mt

# Create geometry definition for the modeling domain
print(pg._("merge", c='g'))
world = mt.createWorld(start=[-10, 0], end=[10, -16],
                       layers=[-8], worldMarker=False)

pg.show(world)
pg.wait()
print(pg._("merge", c='g'))
# Create a heterogeneous block
block = mt.createRectangle(start=[-6, -3.5], end=[6, -6.0],
                           marker=4,  boundaryMarker=10, area=0.1)

print(pg._("merge", c='g'))
circ = mt.createCircle(pos=[0, -11], radius=2, boundaryMarker=11, isHole=True)

# Merge geometrical entities
print(pg._("merge", c='g'))
geom = mt.mergePLC([world, block, circ])
mesh = mt.createMesh(geom)

fig, axs = plt.subplots(3,5)

pg.show(geom, ax=axs[0][0]);
axs[0][0].set_title('plc, (default)')
pg.show(geom, fillRegion=False, ax=axs[0][1]);
axs[0][1].set_title('plc, fillRegion=False')
pg.show(geom, showBoundary=False, ax=axs[0][2]);
axs[0][2].set_title('plc, showBoundary=False')
pg.show(geom, markers=True, ax=axs[0][3]);
axs[0][3].set_title('plc, markers=True')
pg.show(mesh, ax=axs[0][4], showBoundary=False);
axs[0][4].set_title('mesh, showBoundary=False')

pg.show(mesh, ax=axs[1][0]);
axs[1][0].set_title('mesh, (default)')
pg.show(mesh, mesh.cellMarkers(), label='Cell markers', ax=axs[1][1]);
axs[1][1].set_title('mesh, cells, (default)')
pg.show(mesh, markers=True, ax=axs[1][2]);
axs[1][2].set_title('mesh, cells, markers=True')
pg.show(mesh, mesh.cellMarkers(), label='Cell markers', showMesh=True, ax=axs[1][3])
axs[1][3].set_title('mesh, cells, showMesh=True')
pg.show(mesh, mesh.cellMarkers(), label='Cell markers', showBoundary=False, ax=axs[1][4]);
axs[1][4].set_title('mesh, cells, showBoundary=False')

pg.show(mesh, pg.x(mesh), label='Nodes (x)', ax=axs[2][0])
axs[2][0].set_title('mesh, nodes, (default)')
pg.show(mesh, pg.x(mesh), label='Nodes (x)', showMesh=True, ax=axs[2][1])
axs[2][1].set_title('mesh, nodes, showMesh=True')
pg.show(mesh, pg.x(mesh), label='Nodes (x)', showBoundary=True, ax=axs[2][2])
axs[2][2].set_title('mesh, nodes, showBoundary=True')
pg.show(mesh, pg.y(mesh.cellCenters()), label='Cell center (y)', tri=True, shading='flat', ax=axs[2][3]);
axs[2][3].set_title('mesh, cells, tri=True, shading=flat')
pg.show(mesh, pg.y(mesh.cellCenters()), label='Cell center (y)', tri=True, shading='gouraud', ax=axs[2][4]);
axs[2][4].set_title('mesh, cells, tri=True, shading=gouraud')
##pg.show(mesh, mesh.cellMarker(), label(markers), axs[1][1])

#pg.plt.tight_layout()
pg.wait()