import ast
import jinja2

from collections.abc import Mapping
from datetime import date, datetime, timedelta
from enum import Enum
from typing import Dict
from uuid import UUID

from clipped.utils.serialization import (
    date_serialize,
    datetime_serialize,
    timedelta_serialize,
    uuid_serialize,
)

from polyaxon._flow import ParamSpec
from polyaxon._polyaxonfile.specs.libs.engine import get_engine
from polyaxon._polyaxonfile.specs.sections import Sections
from polyaxon.exceptions import PolyaxonSchemaError

try:
    import numpy as np
except (ImportError, ModuleNotFoundError):
    np = None

class PolyaxonfileParser:
    """Parses the Polyaxonfile."""

    engine = get_engine()

    @staticmethod
    def _get_section_data(section_data):
        if hasattr(section_data, "to_dict"):
            return section_data.to_dict()
        return section_data

    @classmethod
    def _get_section(cls, config, section):
        if not hasattr(config, section):
            return None
        section_data = getattr(config, section)
        if isinstance(section_data, list):
            return [cls._get_section_data(d) for d in section_data]

        return cls._get_section_data(section_data)

    @staticmethod
    def get_parsed_params(param_spec: Dict[str, ParamSpec]) -> Dict:
        parsed_params = {"params": {}}
        as_arg = []
        for param in param_spec or {}:
            parsed_params[param] = param_spec[param].get_display_value()
            # Add params level
            if param_spec[param].is_requested:
                as_arg.append(param_spec[param].as_arg())
                parsed_params["params"][param] = param_spec[param].to_parsed_param()

        parsed_params["params"]["as_args"] = [a for a in as_arg if a]
        return parsed_params

    @classmethod
    def parse_operation(
        cls, config, param_spec: Dict[str, ParamSpec]
    ):  # pylint:disable=too-many-branches
        parsed_params = cls.get_parsed_params(param_spec)

        parsed_data = {Sections.VERSION: config.version, Sections.KIND: config.kind}

        if config.name:
            parsed_data[Sections.NAME] = config.name
        if config.description:
            parsed_data[Sections.DESCRIPTION] = config.description
        if config.tags:
            parsed_data[Sections.TAGS] = config.tags
        contexts = getattr(config, Sections.CONTEXTS)
        if contexts:
            parsed_data[Sections.CONTEXTS] = [
                cls.parse_io(io.to_dict(), parsed_params) for io in contexts
            ]
        inputs = getattr(config, Sections.INPUTS)
        if inputs:
            parsed_data[Sections.INPUTS] = [
                cls.parse_io(io.to_dict(), parsed_params) for io in inputs
            ]
        outputs = getattr(config, Sections.OUTPUTS)
        if outputs:
            parsed_data[Sections.OUTPUTS] = [
                cls.parse_io(io.to_dict(), parsed_params) for io in outputs
            ]

        # Check workflow
        for section in Sections.PARSING_SECTIONS:
            config_section = cls._get_section(config, section)
            if config_section:
                parsed_data[section] = cls.parse_expression(
                    config_section, parsed_params
                )

        parsed_data = cls._parse_conditions(parsed_data)

        config_section = cls._get_section(config, Sections.RUN)
        if config_section:
            parsed_data[Sections.RUN] = config_section
        config_section = cls._get_section(config, Sections.BUILD)
        if config_section:
            parsed_data[Sections.BUILD] = config_section
        config_section = cls._get_section(config, Sections.HOOKS)
        if config_section:
            parsed_data[Sections.HOOKS] = config_section

        return parsed_data

    @classmethod
    def _parse_conditions(cls, parsed_data: Dict) -> Dict:
        if Sections.CONDITIONS in parsed_data and not isinstance(
            parsed_data[Sections.CONDITIONS], str
        ):
            parsed_data[Sections.CONDITIONS] = str(parsed_data[Sections.CONDITIONS])
        if Sections.SKIP_ON_UPSTREAM_SKIP in parsed_data and not isinstance(
            parsed_data[Sections.SKIP_ON_UPSTREAM_SKIP], str
        ):
            parsed_data[Sections.SKIP_ON_UPSTREAM_SKIP] = str(
                parsed_data[Sections.SKIP_ON_UPSTREAM_SKIP]
            )
        if Sections.IS_APPROVED in parsed_data and not isinstance(
            parsed_data[Sections.IS_APPROVED], str
        ):
            parsed_data[Sections.IS_APPROVED] = str(parsed_data[Sections.IS_APPROVED])
        return parsed_data

    @classmethod
    def _parse_hook(cls, config, param_spec: Dict[str, ParamSpec]):
        hook = cls.parse_section(config, param_spec=param_spec, parse_params=True)
        return cls._parse_conditions(hook)

    @classmethod
    def parse_hooks(cls, config, param_spec: Dict[str, ParamSpec]):
        hooks = config.hooks or []
        return [
            cls._parse_hook(hook.to_dict(), param_spec=param_spec) for hook in hooks
        ]

    @classmethod
    def parse_runtime(cls, parsed_data, param_spec: Dict[str, ParamSpec]):
        config_section = cls.parse_section(
            parsed_data.get(Sections.RUN), param_spec=param_spec, parse_params=True
        )
        if config_section:
            parsed_data[Sections.RUN] = config_section
        return parsed_data

    @classmethod
    def parse_distributed_runtime(
        cls, parsed_data, param_spec: Dict[str, Dict[str, ParamSpec]]
    ):
        run_section = parsed_data.get(Sections.RUN)
        config_section = {}
        for k in run_section:
            if k in param_spec:
                config_section[k] = cls.parse_section(
                    run_section[k], param_spec=param_spec[k], parse_params=True
                )
            else:
                config_section[k] = run_section[k]
        if config_section:
            parsed_data[Sections.RUN] = config_section
        return parsed_data

    @classmethod
    def parse_section(
        cls, config_section, param_spec: Dict[str, ParamSpec], parse_params: bool = True
    ):
        if parse_params:
            param_spec = cls.get_parsed_params(param_spec)
        if config_section:
            return cls.parse_expression(config_section, param_spec)
        return config_section

    @classmethod
    def parse_io(
        cls,
        expression: Dict,
        params: Dict[str, ParamSpec],
        check_operators: bool = False,
    ):
        # Arg format should not be converted
        arg_format = expression.pop("argFormat", None)
        value = cls.parse_expression(expression, params, check_operators)
        if arg_format:
            value["argFormat"] = arg_format
        return value

    @classmethod
    def parse_expression(  # pylint:disable=too-many-branches
        cls, expression, params: Dict, check_operators: bool = False
    ):
        try:
            return cls._parse_expression(expression, params, check_operators)
        except jinja2.exceptions.TemplateError as e:
            raise PolyaxonSchemaError(
                "Encountered a problem parsing the template, "
                "please make sure your variables are resolvable. "
                "Error: {}".format(repr(e))
            )

    @classmethod
    def _parse_expression(  # pylint:disable=too-many-branches
        cls, expression, params: Dict, check_operators: bool = False
    ):
        if isinstance(expression, (int, float, complex, type(None))):
            return expression
        if isinstance(expression, Enum):
            return expression.value
        if isinstance(expression, datetime):
            return datetime_serialize(expression)
        if isinstance(expression, date):
            return date_serialize(expression)
        if isinstance(expression, timedelta):
            return timedelta_serialize(expression)
        if isinstance(expression, UUID):
            return uuid_serialize(expression)
        if np and isinstance(expression, np.integer):
            return int(expression)
        if np and isinstance(expression, np.floating):
            return float(expression)
        if isinstance(expression, Mapping):
            if len(expression) == 1:
                old_key, value = list(expression.items())[0]
                # always parse the keys, they must be base object or evaluate to base objects
                key = cls._parse_expression(old_key, params)
                if check_operators and cls.is_operator(key):
                    return cls._parse_operator({key: value}, params)
                else:
                    return {key: cls.parse_expression(value, params, check_operators)}

            new_expression = {}
            for k, v in expression.items():
                new_expression.update(
                    cls._parse_expression({k: v}, params, check_operators)
                )
            return new_expression

        if isinstance(expression, list):
            return list(
                cls.parse_expression(v, params, check_operators) for v in expression
            )
        if isinstance(expression, tuple):
            return tuple(
                cls.parse_expression(v, params, check_operators) for v in expression
            )
        if isinstance(expression, str):
            return cls._evaluate_expression(expression, params, check_operators)

    @classmethod
    def _evaluate_expression(cls, expression, params, check_operators):
        try:
            result = cls.engine.from_string(expression).render(**params)
        except (ValueError, TypeError) as e:
            raise PolyaxonSchemaError(
                "Encountered a problem parsing the template, "
                "please make sure your variables are resolvable. "
                "Error: {}".format(repr(e))
            )
        if result == expression:
            try:
                return ast.literal_eval(result)
            except (ValueError, SyntaxError):
                pass
            return result
        return cls.parse_expression(result, params, check_operators)

    @classmethod
    def _parse_operator(cls, expression, params):
        k, v = list(expression.items())[0]
        op = Sections.OPERATORS[k].from_dict(v)
        return op.parse(parser=cls, params=params)

    @staticmethod
    def is_operator(key):
        return key in Sections.OPERATORS


# -*- coding: utf-8 -*-
from django.conf import settings
from toolkit.celery import app

from .services import PusherPublisherService, RealTimeMatterEvent

import logging
logger = logging.getLogger('django.request')

@app.task
def youve_got_notifications(username, event, *args, **kwargs):
    if settings.PROJECT_ENVIRONMENT not in ['test']:
        logger.info('Sending youve_got_notifications: %s' % username)
        pusher = PusherPublisherService(channel=username,
                                        event=event)
        pusher.process(**kwargs)

@app.task
def realtime_matter_event(matter, event, obj, ident, from_ident=None, **kwargs):
    if 'countdown' in kwargs:
        kwargs.pop('countdown')  # remove celery kwarg
    if settings.PROJECT_ENVIRONMENT not in ['test']:
        realtime = RealTimeMatterEvent(matter=matter)
        realtime.process(event=event, obj=obj, ident=ident, from_ident=from_ident, **kwargs)


import time
import random
# Уровней здания
urovni = list(range(9))

# Вызовы с уровней
vyzovy_vverh = [8, 5]
vyzovy_vverh.sort()
vyzovy_vniz = [1, 4]
vyzovy_vniz.sort(reverse = True)

# Вызовы из подъемника
vyzov_iz_podyemnika = []

# Производство случайных вызовов
def sozdat_vyzov():
    sluchaynoe_chislo = random.choice(urovni)
    sluchayny_vyzov = sluchaynoe_chislo
    return sluchayny_vyzov

# Нахождение подъёмника
tek_uroven = 10

# Направление
napravlenie = True

# Сообщения
opisanie = ["↑", "↓", "Едем на:", "Текущий:", "Очередь:", "Остановка на:", "Выбран уровень:", "Двери закрываются", "--------------------------------", "Загрузка/Выгрузка пассажиров", "Очередь:"]

# Задержка.
def zaderzhka():
    time.sleep(1)

# Остановка
def ostanovka():

    # Вызов из подъёмника
    vyzov_iz_podyemnika.append(6)
    print(random.seed())
    # Описание остановки
    print(opisanie[8]) # Черта
    print(opisanie[5], tek_uroven) # Текущий уровень

    # Перенос вызовов с временного списка в постоянные
    for vyzov in vyzov_iz_podyemnika:
        if tek_uroven < vyzov:
            vyzovy_vverh.append(vyzov)

        elif tek_uroven > vyzov:
            vyzovy_vniz.append(vyzov)

        else:
            break

    # Удаление повторов
    list(set(vyzovy_vverh))
    list(set(vyzovy_vniz))

    # Сортировка
    vyzovy_vverh.sort()
    vyzovy_vniz.sort(reverse=True)

    if napravlenie == True:
        vyzovy_vverh.remove(vyzovy_vverh[0]) # Удаление пройденного вызова в списке вверх
        print(opisanie[10], vyzovy_vverh) # Очередь вверх
    elif napravlenie == False:
        vyzovy_vniz.remove(vyzovy_vniz[0]) # Удаление пройденного вызова в списке вниз
        print(opisanie[10], vyzovy_vniz) # Очередь вниз

    print(opisanie[6], vyzov_iz_podyemnika)  # Вызов из подъёмника

    # Очистка временного списка
    vyzov_iz_podyemnika.clear()

    print(opisanie[8]) # Черта
    time.sleep(3)

# Описание движения
def dvizhenie():
    if napravlenie == True:
        print(opisanie[0], opisanie[2], vyzovy_vverh[0], opisanie[3], tek_uroven, opisanie[10], vyzovy_vverh)
    elif napravlenie == False:
        print(opisanie[1], opisanie[2], vyzovy_vniz[0], opisanie[3], tek_uroven, opisanie[10], vyzovy_vniz)

# =====================================================================

# Работа подъемника
petlya = True
while petlya:
    zaderzhka()
    if vyzovy_vverh != [] and tek_uroven == vyzovy_vverh[0]:
        ostanovka()

    elif vyzovy_vverh != [] and  tek_uroven < vyzovy_vverh[0]:
        tek_uroven += 1
        dvizhenie()
        if tek_uroven in vyzovy_vverh:
            for vyzov in vyzovy_vverh:
                if vyzov > tek_uroven:
                    ostanovka()

    elif vyzovy_vniz != [] and tek_uroven == vyzovy_vniz[0]:
        ostanovka()

    elif vyzovy_vniz != [] and tek_uroven > vyzovy_vniz[0]:
        napravlenie = False
        tek_uroven -= 1
        dvizhenie()
        if tek_uroven in vyzovy_vniz:
            for vyzov in vyzovy_vniz:
                if vyzov < tek_uroven:
                    ostanovka()

    else:
        print("Стоим")
        break


# Don't change the code below
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))
# Don't change the code above

#Write your code below this line

#Under 18.5 they are underweight
#Over 18.5 but below 25 they have a normal weight
#Over 25 but below 30 they are slightly overweight
#Over 30 but below 35 they are obese
#Above 35 they are clinically obese.
result = int(weight) / (float(height) ** 2)
message = ""
if result < 18.5:
    message = "you are underweight"
elif result < 25:
    message = "you have a normal weight"
elif result < 30:
    message = "you are slightly overweight"
elif result < 35:
    message = "you are obese"
else:
    message = "you are clinically obese"

print(f"Your BMI is {int(result)}, {message}.")

1 - Beginner/Day 3 - Logical Operators/Exercises/2 - BMI 2.0/main.py


import tweepy
from src.Obj.tweet import Tweet
from src.Obj.tweetlist import TweetList
from datetime import datetime
from datetime import timedelta

# Tweet Scrape Class: This class uses tweepy to handle twitter api's and builds tweets
class TweetScraper:
    # connect to twitter api with account
    def __init__(self, consumer_key, consumer_secret, access_token=None, access_token_secret=None):
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        # does not use access token
        if access_token is None:
            self.api = tweepy.API(auth, wait_on_rate_limit=True)
        # uses required auth access tokens
        else:
            auth.set_access_token(access_token, access_token_secret)
            self.api = tweepy.API(auth, wait_on_rate_limit=True)

    # search with keyword, user, and date objects
    # Rate: 180 calls per window
    # Returns a tweet list
    def search(self, keyword, user=None, start_date=None, end_date=None):
        # search with just a keyword
        if user is None and start_date is None and end_date is None:
            query = keyword + " -RT"
            return self.parse_tweets(self.api.search(query, tweet_mode='extended', count=100))

        # search with just keyword and user
        elif start_date is None and end_date is None:
            query = keyword + "(from:" + user + ")(to:" + user + ")" + " -RT"
            return self.parse_tweets(self.api.search(query, tweet_mode='extended'))

        # search with user, keyword, and dates
        elif end_date is None:
            query = keyword + "(from:" + user + ")(to:" + user + ")since:" + str(start_date.year) + "-" + str(
                start_date.month) + "-" + str(start_date.day) + "until:" + str(end_date.year) + "-" + str(
                end_date.month) + "-" + str(end_date.day) + " -RT"
            return self.parse_tweets(self.api.search(query, tweet_mode='extended'))
        # search with keyword and end date (used in get_weekly_tweets)
        elif user is None and start_date is None:
            query = keyword + "()until:" + str(end_date.year) + "-" + str(end_date.month) + "-" + str(
                end_date.day) + " -RT"
            return self.parse_tweets(self.api.search(query, tweet_mode='extended', count=100))

    # gets weekly tweets
    def get_weekly_tweets(self, keyword):
        today = datetime.today()
        cur_date = today
        sub_week = timedelta(days=6, hours=20)
        last_week = today - sub_week
        tweet_list = TweetList()
        query_count = 0
        while cur_date > last_week:
            tweet_list.insert_list(self.search(keyword=keyword, end_date=cur_date))
            query_count += 1
            cur_date = tweet_list.get_last().date
        print("Over the Past Week: \nThere have been {0} tweets about {1} which were collected over {2} queries".format(
            tweet_list.get_size(), keyword, query_count))
        return tweet_list

    # gather a users lists members (helper method to get valuable users)
    # Rate: 75
    # Returns a python list
    def list_members(self, user, slug):
        members = []
        for page in tweepy.Cursor(self.api.list_members, user, slug).items():
            members.append(page)
        return [m.screen_name for m in members]

    # Method takes a user - gathers their lists and then compiles all members in those lists
    # Rate: 15 lists, 75 members
    # Returns a python list
    def get_valuable_users(self, base_user):
        # gather users lists
        lists = self.users_lists(base_user)
        valuable_users = []
        count = 1
        seen = set(valuable_users)
        # adds members of lists into a set. Does not repeat users
        for item in lists:
            print(str(count) + "/" + str(len(lists)))
            slug = self.get_list_slug(item)
            print(slug)
            users = self.list_members(base_user, slug)
            count += 1
            for user in users:
                print(user)
                if user not in seen:
                    seen.add(user)
                    valuable_users.append(user)
        return valuable_users

    # gather a users lists
    # Rate: 15
    # Returns a list status object (tweepy defined)
    def users_lists(self, user):
        lists = []
        for tweet_list in self.api.lists_all(user):
            if tweet_list.user.screen_name == user:
                lists.append(tweet_list)
        return lists

    # Returns the text of a tweet id
    def get_status(self, tweet):
        return self.api.get_status(tweet)

    @staticmethod
    # helper method to return a lists slug
    def get_list_slug(list_item):
        return list_item.slug

    # helper method to transform tweet into usable tweet object
    # returns a tweet list object (Data)
    @staticmethod
    def parse_tweets(data):
        tweet_list = TweetList()
        for tweet in data:
            tweet_obj = Tweet()
            tweet_obj.add_tweet_json(tweet)
            tweet_list.insert_data(tweet_obj)
        return tweet_list


#!/usr/bin/python3
'''
Gatherin data form REST API dummy JSON file
'''
import requests
import sys

if __name__ == "__main__":

    employee_id = sys.argv[1]
    employee = requests.get('https://jsonplaceholder.typicode.com/users/{}'
                            .format(employee_id)).json()
    num_task = requests.get('https://jsonplaceholder.typicode.com/todos',
                            params={'userId': employee_id}).json()

    employee_name = employee.get('name')
    tasks = []
    total_number_tasks = 0
    number_done_task = 0
    for ind in num_task:
        total_number_tasks += 1
        if ind.get('completed'):
            tasks.append(ind.get('title'))
            number_done_task += 1

    print('Employee {} is done with tasks({}/{}):'.format(
        employee_name, number_done_task, total_number_tasks))

    for phrase in tasks:
        print('\t {}'.format(phrase))


from math import atan, sqrt, sin, cos

def dot(matrix, vector):
    sol = []
    for row in matrix:
        total = 0
        for left, right in zip(row, vector):
            total += left * right
        sol.append(total)
    return sol

def solve(A):
    # vectors to the faces of the cube
    p1 = (0.5, 0, 0)
    p2 = (0, 0.5, 0)
    p3 = (0, 0, 0.5)
    sqrt2 = sqrt(2)
    # u is the unitary along rotation axis (-0.5, 0, 0.5)
    ux = -1 * 0.5 * sqrt2
    uz = 0.5 * sqrt2
    # inverse of the relation between area and angle
    theta = 2 * atan((sqrt2 - sqrt(3 - A ** 2)) / (A + 1))
    c, s = cos(theta), sin(theta)
    # R is the rotation matrix
    R = [
        [c + ux ** 2 * (1 - c), -uz * s, ux * uz * (1 - c)],
        [uz * s, c, -ux * s],
        [ux * uz * (1 - c), ux * s, c + uz ** 2 * (1 - c)]
    ]
    p1r = dot(R, p1)
    p2r = dot(R, p2)
    p3r = dot(R, p3)
    return [p1r, p2r, p3r]

def read_input():
    ncases = int(input())
    for case in range(1, ncases + 1):
        area = float(input())
        sol = solve(area)
        print("Case #{}:".format(case))
        for row in sol:
            print(" ".join(map(str, row)))

read_input()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 28 16:29:52 2022

@author: ghiggi
"""
import re
import pathlib
import pandas as pd
import xarray as xr
import numpy as np
from warnings import warn
from zipfile import ZipFile
from xarray.core import dtypes

def xr_regularize_time_dimension(ds: xr.Dataset,
                                 t_res: str = "2min30s",
                                 fill_value = dtypes.NA):
    """Fill missing timesteps with default values in a xarray Dataset.

    Parameters
    ----------
    ds : xr.Dataset
        Input xarray Dataset
    range_freq : str, optional
        Interval between each timestep, by default "2min30s"
    fill_value : optional
        Fill value for missing timesteps

    Returns
    -------
    xr.Dataset
        Dataset with a contiguous time range and no missing timesteps
    """
    start = ds.time.to_numpy()[0]
    end = ds.time.to_numpy()[-1]
    start_date = pd.to_datetime(start).date()
    end_date = pd.to_datetime(pd.to_datetime(end).date()) + pd.Timedelta(hours=23, minutes=57, seconds=30)
    full_range = pd.date_range(start=start_date,
                               end=end_date,
                               freq=t_res).to_numpy()
    ds_reindexed = ds.reindex({"time": full_range}, fill_value=fill_value)
    return ds_reindexed

def xr_drop_duplicated_timesteps(ds: xr.Dataset):
    """Drop duplicate timesteps in the input Dataset by only keeping the
    data measurements taken with the highest number of available radars.

    Parameters
    ----------
    ds : xr.Dataset
        Input xarray Dataset

    Returns
    -------
    xr.Dataset
        Processed dataset with no duplicate timesteps
    """
    idx_keep = np.arange(len(ds.time))
    to_remove = []
    _, idx, count = np.unique(ds.time, return_counts=True, return_index=True)
    index_duplicates = [list(range(idx[i], idx[i]+count[i])) for i, _ in enumerate(idx) if count[i] > 1]

    for dup in index_duplicates:
        radar_names = [len(re.sub(r'[^a-zA-Z]', '', str(ds.radar_names[i].values))) for i in dup]
        to_remove.extend([dup[i] for i in range(len(radar_names)) if i != radar_names.index(max(radar_names))])

    idx_keep = [i for i in idx_keep if i not in to_remove]
    ds = ds.isel(time=idx_keep)
    return ds


from django.db import models
from django.contrib.auth.models import User
from .department import Department

class Feature(models.Model):
    title = models.CharField(max_length=2000)
    description = models.TextField(blank=True,
                                   null=True)
    final_price = models.DecimalField(max_digits=15,
                                      decimal_places=2,
                                      blank=True,
                                      null=True)
    price = models.DecimalField(max_digits=15,
                                decimal_places=2,
                                blank=True,
                                null=True)
    discount = models.DecimalField(max_digits=15,
                                   decimal_places=2,
                                   blank=True,
                                   null=True)
    rate = models.FloatField(blank=True,
                             null=True)
    insert_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True,
                                       null=True)
    user = models.ForeignKey(User,
                             on_delete=models.CASCADE)
    department = models.ForeignKey(Department,
                                   on_delete=models.CASCADE,
                                   related_name='department_id',
                                   db_column='department')

    def __str__(self):
        return self.title


def hello():
    print("Hello :)")

hello()

def pack(el1, el2, el3):
    return {el1, el2, el3}

hey = "Hey"
there = "there"
buddy = "buddy"

print(pack(hey, there, buddy))

def eat_lunch(lst):
    for i, item in enumerate(lst):
        if i == 0:
            print(f"First I eat {item}")
        else:
            print(f"Next I eat {item}")
    print("My lunchbox is empty!!")

lst = ["Banana", "Burrito", "Sandwich"]
empty_lst = []
eat_lunch(lst)


import math

import torch
import torch.nn as nn
import torch.nn.functional as F

from dril.a2c_ppo_acktr.utils import AddBias, init
import matplotlib.pyplot as plt
"""
Modify standard PyTorch distributions so they are compatible with this code.
"""

#
# Standardize distribution interfaces
#

# Categorical
FixedCategorical = torch.distributions.Categorical

old_sample = FixedCategorical.sample
FixedCategorical.sample = lambda self: old_sample(self).unsqueeze(-1)

log_prob_cat = FixedCategorical.log_prob
FixedCategorical.log_probs = lambda self, actions: log_prob_cat(
    self, actions.squeeze(-1)).view(actions.size(0), -1).sum(-1).unsqueeze(-1)

FixedCategorical.mode = lambda self: self.probs.argmax(dim=-1, keepdim=True)

# Normal
FixedNormal = torch.distributions.Normal

log_prob_normal = FixedNormal.log_prob
FixedNormal.log_probs = lambda self, actions: log_prob_normal(
    self, actions).sum(
        -1, keepdim=True)

normal_entropy = FixedNormal.entropy
FixedNormal.entropy = lambda self: normal_entropy(self).sum(-1)

FixedNormal.mode = lambda self: self.mean

###################################################################
# Beta
FixedBeta = torch.distributions.Beta

log_prob_beta = FixedBeta.log_prob
FixedBeta.log_probs = lambda self, actions: log_prob_beta(
    self, actions).sum(
        -1, keepdim=True)

beta_entropy = FixedBeta.entropy
FixedBeta.entropy = lambda self: beta_entropy(self).sum(-1)

FixedBeta.mode = lambda self: self.mean
###################################################################

# Bernoulli
FixedBernoulli = torch.distributions.Bernoulli

log_prob_bernoulli = FixedBernoulli.log_prob
FixedBernoulli.log_probs = lambda self, actions: log_prob_bernoulli(
    self, actions).view(actions.size(0), -1).sum(-1).unsqueeze(-1)

bernoulli_entropy = FixedBernoulli.entropy
FixedBernoulli.entropy = lambda self: bernoulli_entropy(self).sum(-1)
FixedBernoulli.mode = lambda self: torch.gt(self.probs, 0.5).float()

class Categorical(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(Categorical, self).__init__()

        init_ = lambda m: init(
            m,
            nn.init.orthogonal_,
            lambda x: nn.init.constant_(x, 0),
            gain=0.01)

        self.linear = init_(nn.Linear(num_inputs, num_outputs))

    def forward(self, x):
        x = self.linear(x)
        return FixedCategorical(logits=x)

    def get_logits(self, x):
        x = self.linear(x)
        return FixedCategorical(logits=x).logits

class DiagGaussian(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(DiagGaussian, self).__init__()

        init_ = lambda m: init(m, nn.init.orthogonal_, lambda x: nn.init.
                               constant_(x, 0))

        self.fc_mean = init_(nn.Linear(num_inputs, num_outputs))
        self.logstd = AddBias(torch.zeros(num_outputs))

    def forward(self, x):
        action_mean = self.fc_mean(x)

        #  An ugly hack for my KFAC implementation.
        zeros = torch.zeros(action_mean.size())
        if x.is_cuda:
            zeros = zeros.cuda()

        action_logstd = self.logstd(zeros)
        return FixedNormal(action_mean, action_logstd.exp())

    def get_mean(self, x):
        return self.fc_mean(x)

###########################################################################
class DiagBeta(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(DiagBeta, self).__init__()

        init_ = lambda m: init(m, nn.init.orthogonal_, lambda x: nn.init.
                               constant_(x, 0))

        #self.fc_mean = init_(nn.Linear(num_inputs, num_outputs))
        #self.logstd = AddBias(torch.zeros(num_outputs))

        self.alpha_head = init_(nn.Linear(num_inputs, num_outputs))
        self.beta_head  = init_(nn.Linear(num_inputs, num_outputs))
        self.sp = nn.Softplus()

    def forward(self, x):
        alpha = self.sp(self.alpha_head(x)) + 1
        beta = self.sp(self.beta_head(x)) + 1

        return FixedBeta(alpha, beta) #, alpha, beta

    def get_mean(self, x):
        alpha = self.sp(self.alpha_head(x)) + 1
        beta = self.sp(self.beta_head(x)) + 1

        return alpha / (alpha + beta)

###########################################################################

class Bernoulli(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(Bernoulli, self).__init__()

        init_ = lambda m: init(m, nn.init.orthogonal_, lambda x: nn.init.
                               constant_(x, 0))

        self.linear = init_(nn.Linear(num_inputs, num_outputs))

    def forward(self, x):
        x = self.linear(x)
        return FixedBernoulli(logits=x)

    def get_logits(self, x):
        return self.linear(x)

# CHECKS DRIL BETA DISTRIBUTION

def test_1():
    input_th = torch.ones([8])
    input_th = input_th.unsqueeze(0)
    beta_dist = DiagBeta(8,1)
    dist_curve, alpha, beta = beta_dist(input_th)

    n = 1000
    sp_hist = []
    for i in range(n):
        s = dist_curve.sample()
        sp_hist.append(s.item())

    print(f'mean = {dist_curve.mean}')

    plt.hist(sp_hist, bins=100)
    plt.show()

def test_2():
    input_th = torch.ones([8])
    input_th = input_th.unsqueeze(0)
    beta_dist = DiagBeta(8, 2)
    dist_curve, alpha, beta = beta_dist(input_th)

    output = beta_dist.get_mean(input_th)
    print(alpha)
    print(beta)
    print(output)

if __name__ == '__main__':
    #test_1() #deprecated
    test_2()


# coding=utf-8
from bs4 import BeautifulSoup as bs
from urllib.error import HTTPError
from urllib.request import urlopen
from sqlalchemy import create_engine
import json
import pandas as pd
from server_config import *

print('Starting to crawl......')

# 打开网页，获取源码
def open_page(url):
    try:
        netword=urlopen(url)
    except HTTPError as hp:
        print(hp)
    else:
    # 采用BeautifulSoup来解析，且指定解析器
        html=bs(netword,'lxml')
        return html

# 获取网页数据
def get_page(url):
    # 电影名称，上映天数，电影总票房，票房占比，排片场次，排片占比，场均人次，上座率
    movieName, boxInfo = [], []
    html=open_page(url)
    # print(html)
    p=html.find('p')
    text=p.get_text()
    # 将数据转换为python能够处理的格式
    jsonObj=json.loads(text)
    # 获取字典里面特定的键对应的键值
    data=jsonObj.get('data')
    # 想要的数据就在字典的键"list"对应的值
    lists=data.get('list')
    for list in lists:
        # 获取字典里面特定的键对应的键值,并存储到列表中去
        movieName.append(list.get('movieName'))
        boxInfo.append(list.get('boxInfo'))
    return movieName, boxInfo

a = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
movieName, boxInfos, year, month, day = [], [], [], [], []

for i in range(2015, 2019):
    for j in range(1, 13):
        if i == 2016 & j == 2:
            n = 29
        else:
            n = a[j]
        for k in range(1, n):
            tmpMonth = str(j)
            tmpDay = str(k)
            tmpYear = str(i)
            if j < 10:
                tmpMonth = '0' + tmpMonth
            if k < 10:
                tmpDay = '0' + tmpDay
            url = 'https://box.maoyan.com/promovie/api/box/second.json?beginDate=' + tmpYear + tmpMonth + tmpDay
            # print(url)
            tmpMovieName, tmpboxInfos = get_page(url)
            # print(tmpMovieName)
            for x in tmpMovieName:
                movieName.append(x)
                year.append(tmpYear)
                month.append(tmpMonth)
                day.append(tmpDay)
            for x in tmpboxInfos:
                boxInfos.append(float(x))
print('Summing up result......')
d=pd.DataFrame({'name': movieName, 'boxing': boxInfos, 'year': year, 'month': month, 'day': day})#数据有三列，列名分别为one,two,three
# d.to_csv("test.csv", index = False, sep=',')
print('Started to update database......')
engine = create_engine('mysql+pymysql://'+SQL_USERNAME+':'+SQL_PASSWORD+'@'+SQL_ADDRESS+':'+SQL_PORT+'/'+SQL_SCHEMA)
d.to_sql("boxing_day",engine,if_exists='replace', index=False)
print('Daily boxing data crawled Okay!')


import random
import subprocess

def main():
    expr_count = 25

    recursive = "./recursive"
    brute_force = "./brute_force"

    for expr_step in range(1, expr_count):
        numbers = []
        for n in range(1, expr_step + 1):
            numbers.append(random.randint(-100, 100))

        infile = open("input_numbers.txt", 'w')
        infile.write('\n'.join(map(str, numbers)))
        infile.close()

        print("Iteration: " + str(expr_step))

        popen = subprocess.Popen(recursive, stdout=subprocess.PIPE);
        popen.wait()
        output = popen.stdout.read()
        print("Recursive: ", end='')
        print(output)

        popen = subprocess.Popen(brute_force, stdout=subprocess.PIPE);
        popen.wait()
        output = popen.stdout.read()
        print("Brute force: ", end='')
        print(output)
    pass

main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Dec 31, 2013

@author: zhong
'''
import random
import pickle
import os
import json

""" Basic blog using webpy 0.3 """
import web
import models

### Url mappings

urls = (
    '/favicon.ico','favicon',
    '/', 'Login',
    '/admin', 'Admin',
    '/restore', 'Restore',
    '/game/(\w+)', 'Game',
    '/process/(\w+)', 'Process',
    '/message/(\w+)', 'Message',
    '/update', 'Update',
)

class favicon:
    def GET(self):
        f = open("static/favicon.ico", 'rb')
        return f.read()

### Templates
render = web.template.render('templates', base='base', globals = {'models':models})

class Login:
    def GET(self):
        form = web.form.Form(
            web.form.Textbox('username', description = u"用户名"),
            web.form.Textbox('nickname', description = u"昵称"),
            web.form.Button('Login', description = u"登录"),
        )
        return render.login(form)

    def POST(self):
        global s
        if s is None:
            return "Game has not started yet."
        else:
            for p in s.context.players:
                if p.username == web.input()['username'].strip():
                    nickname = web.input()['nickname'].strip()
                    if nickname != '':
                        p.nickname = nickname
                    raise web.seeother('/game/%s' % web.input()['username'])
                    break
            else:
                return "User not found."

class Admin:
    def GET(self):
        form = web.form.Form(
            web.form.Textbox('players', description = u"全部玩家用户名，逗号分隔"),
            web.form.Textbox('killer_num', description = u"杀手人数"),
            web.form.Textbox('police_num', description = u"警察人数"),
            web.form.Textbox('civilian_num', description = u"平民人数"),
            web.form.Radio('civilian_all_die_winner', ['killer', 'police'], description = u"平民全部牺牲情况下的获胜方"),
            web.form.Button('submit', description = u"创建游戏"),
        )
        return render.admin(form)

    def POST(self):
        global s
        usernames = [n.strip() for n in web.input()['players'].split(',')]
        killer_num = int(web.input()['killer_num'])
        police_num = int(web.input()['police_num'])
        civilian_num = int(web.input()['civilian_num'])
        assert len(usernames) == killer_num + police_num + civilian_num
        identities = [models.KILLER] * killer_num + [models.POLICE] * police_num + [models.CIVILIAN] * civilian_num
        random.shuffle(identities)

        context = models.Context([models.Player(username, identity) for username, identity in zip(usernames, identities)])
        context.civilian_all_die_winner = models.POLICE if web.input()['civilian_all_die_winner'] == 'police' else models.KILLER

        s = models.GameStart(context).pre()

        raise web.seeother('/')

class Restore:
    def GET(self):
        form = web.form.Form(
            web.form.Dropdown('status', args = [filename for filename in os.listdir('status') if 'pickle' in filename]),
            web.form.Button('Restore', description = u"恢复状态"),
        )
        return render.restore(form)

    def POST(self):
        global s
        filename = web.input()['status']
        s = pickle.load(open('status/%s'%filename)).pre()
        raise web.seeother('/')

class Update:
    def POST(self):
        global s
        web.header('Content-Type', 'application/json')

        if s is None:
            messages = None
            new_message_count = 0
            players = None
            form = get_form(None, None)
            new_sid = 0
        else:
            username = web.input()['username']
            message_count = int(web.input()['message_count'])
            sid = int(web.input()['sid'])

            player = s.context.get_player(username)
            if player is None:
                messages = None
                new_message_count = 0
                players = None
                form = get_form(s, None)
                new_sid = 0
            else:
                messages = get_messages(s, player, message_count)
                new_message_count = len(player.message.data)

                new_sid = id(s)
                if new_sid == sid:
                    players = form = None
                else:
                    players = get_players(s, player)
                    form = get_form(s, player)

        return json.dumps(
            ((messages, new_message_count),
             players, form, new_sid)
        )

formrender = web.template.render('templates')

def get_messages(s, player, message_count):
    if message_count < len(player.message.data):
        return str(formrender.messages(s, player, message_count))
    else:
        return None

def get_players(s, player):
    return str(formrender.players(s, player))

def get_form(s, player):

    if s is None:
        return '
游戏尚未开始
 Admin   Restore'
    elif player is None:
        return '
该用户不在游戏中
 Admin   Restore'
    elif isinstance(s, models.KillStatus):
        if player in s.actors:
            return str(formrender.kill(s))
        else:
            return str(formrender.wait(s, secret = True))
    elif isinstance(s, models.InvestigateStatus):
        if player in s.actors:
            return str(formrender.investigate(s))
        else:
            return str(formrender.wait(s, secret = True))
    elif isinstance(s, models.TalkStatus):
        if player in s.actors:
            return str(formrender.talk(s))
        else:
            return str(formrender.wait(s, secret = False))
    elif isinstance(s, models.VoteStatus):
        if player in s.actors:
            return str(formrender.vote(s))
        else:
            return str(formrender.wait(s, secret = False))
    elif isinstance(s, models.LastWordsStatus):
        if player in s.actors:
            return str(formrender.lastwords(s))
        else:
            return str(formrender.wait(s, secret = False))
    elif isinstance(s, models.GameOver):
        return '
游戏结束
 Admin   Restore'
    else:
        return None

class Process:
    def POST(self, username):
        global s
        actor = s.context.get_player(username)
        if str(id(s)) == web.input()['sid']:
            kwargs = {'actor':actor}
            if 'target' in web.input():
                target_text = web.input()['target']
                if target_text == 'None':
                    kwargs['target'] = None
                else:
                    kwargs['target'] = s.context.get_player(target_text[5:]) # perfix is user:

            if 'words' in web.input():
                kwargs['words'] = web.input()['words']

            if web.input()['type'] == 'submit':
                s = s.act(**kwargs)
            elif web.input()['type'] == 'cancel':
                s = s.cancel(actor = actor)
        else:
            actor.message.add("warn", u"您刚刚的操作已过期，请重试")
        return json.dumps(""); # Create a success result

class Game:
    def GET(self, username):
        global s
        return render.game(username, s)

s = None

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()


# https://projecteuler.net/problem=7

N = 10001
MAX_SIZE = int(1E7)

count = 0
prime_list = [False, False]
prime_list += [True for i in range(MAX_SIZE - 2)]

for i in range(2, MAX_SIZE):
    if prime_list[i]:
        count += 1
        if count == N:
            print(i)
            break
        for j in range(i*2, MAX_SIZE, i):
            prime_list[j] = False


import logging
from typing import Any, Iterable

from scrapy import Request, Spider
from scrapy.http import Response, JsonRequest

from util import get_stripped, get_all_stripped, get_list

logger = logging.getLogger('aa')

PAGE_SIZE = 10
BASE_URL = 'https://www.auswaertiges-amt.de'
LIST_URL = (f'{BASE_URL}/ajax/json-filterlist/de/search/-/1798?search=%22%22'
            f'&documenttype=1796%23AAArticle%20OR%20AAPress%20OR%20AAInterview%20OR%20AAFaq'
            f'&startfield=date&endfield=date&limit={PAGE_SIZE}')

class AASpider(Spider):
    name = 'aa'
    start_urls = [LIST_URL]

    def start_requests(self) -> Iterable[Request]:
        for url in self.start_urls:
            yield JsonRequest(url=url, callback=self.parse_list, dont_filter=True, meta={'offset': 0})

    def parse_list(self, response: Response):
        body = response.json()
        num_total = body.get('itemsTotal')
        next_offset = response.meta.get('offset', 0) + PAGE_SIZE

        for article in body.get('items', []):
            logger.debug(f'Found article link: {article["link"]}')
            yield response.follow(BASE_URL + article['link'], self.parse_article)

        if next_offset < num_total:
            next_link = f'{LIST_URL}&offset={next_offset}'
            logger.info(f'Found next page: {next_link}')
            yield JsonRequest(url=next_link, callback=self.parse_list, meta={'offset': next_offset})

    def parse_article(self, response: Response, **kwargs: Any):
        logger.debug(f'Parsing article page: {response.request.url}')
        content = response.css('#main')
        head = response.css('#main header')

        date = None
        desc = None
        date_type = get_stripped(head, 'span.heading__meta::text')
        if date_type is not None:
            parts = date_type.split(' - ')
            if len(parts) >= 2:
                date = parts[0]
                desc = parts[1]

        yield {
            'date': date,
            'descriptor': desc,
            'tags': get_list(content, 'section.u-section ul.link-list__list a.link-list__link::attr("title")'),
            'title': get_all_stripped(head, 'h1.heading__title *::text'),
            'teaser': get_stripped(head, 'p.heading__intro *::text'),
            'text': get_all_stripped(content, 'div.c-rte--default *::text'),
            'link': response.request.url
        }


import numpy as np
from PIL import Image
import os

def bfs(mask, i, j, vis, val):
    q = [(i, j)]
    vis[i][j] = False
    for i, j in q:
        for x, y in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)):
            if 0 <= x < mask.shape[0] and 0 <= y < mask.shape[1] and mask[x][y] != [0, 0, 0] and vis[x][y] == True:
                if mask[x][y] != mask[i][j]:
                    mask[i][j] = 0
                else:
                    vis[x][y] = False
                    q.append((x, y))

def reduction(mask):
    print(mask.shape)
    vis = np.ones((mask.shape[0], mask.shape[1]), dtype=bool)
    for i in range(mask.shape[0]):
        for j in range(mask.shape[1]):
            if mask[i][j] != [0, 0, 0] and vis[i][j] == True:
                bfs(mask, i, j, vis, mask[i][j])

def process(input, output):
    print(len(os.listdir(input)))
    for f in os.listdir(input):
        print(os.path.join(input, f))
        mask = np.array(Image.open(os.path.join(input, f)))
        reduction(mask)

        output_img = np.ones((mask.shape[0], mask.shape[1]), dtype=np.uint8)
        for i in range(mask.shape[0]):
            for j in range(mask.shape[1]):
                output_img[i][j] = 0 if mask[i][j] == [0, 0, 0] else 255

        print(os.path.join(output, f))
        Image.fromarray(output_img).save(os.path.join(output, f))

if __name__ == '__main__':
    input = 'D:\\zl\\GraduationThesis\\data\\new_data\\label1'
    output = 'D:\\zl\\GraduationThesis\\data\\new_data\\label2'
    process(input, output)


import pygame
from MazeGame.GUI.GuiState.State import State
from MazeGame.GUI.GuiDesign.RankingScreen import RankingScreen
from MazeGame.Ranking.RankingManager import RankingManager
from Engine.Structs.GameSettings import GameMode

class RankingState(State):
    def __init__(self,setstatus,view):
       super().__init__(setstatus,view,)
       self.ranking = RankingManager()

       self.__game_mode = GameMode.EASY
       self.view.results = self.ranking.get_results_by_mode(GameMode.EASY)

    def set_game_mode(self, mode:GameMode):
      self.__game_mode = mode

    def render(self):
      #  self.view = RankingScreen()
      self.view.screen_design()
      return self.view.render()

    def update(self, event):
      if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        mouse_pos = pygame.mouse.get_pos()
        if self.view.buttons['Menu inicial'].clicked(mouse_pos):
            self.setstatus.state('home')

        if self.view.buttons['Fácil'].clicked(mouse_pos):
          self.set_game_mode(GameMode.EASY)
        if self.view.buttons['Intermediário'].clicked(mouse_pos):
          self.set_game_mode(GameMode.MEDIUM)
        if self.view.buttons['Difícil'].clicked(mouse_pos):
          self.set_game_mode(GameMode.HARD)
        self.view.results = self.ranking.get_results_by_mode(self.__game_mode)

Programação Orientada a Objetos 2/versao_final/MazeGame/GUI/GuiState/RankingState.py


a = int(input())
b = int(input())
c = int(input())
abc = a * b * c
count = [0] * 10
while abc != 0:
    n = abc % 10
    abc = int(abc / 10)
    count[int(n)] += 1
for i in range(0, 10):
    print(count[i])

# https://pacific-ocean.tistory.com/34
"""
a = int(input())
b = int(input())
c = int(input())
result = list(str(a * b * c))
for i in range(10):
    print(result.count(str(i)))
"""

# str.count(chr) : count the number of 'chr' in list 'str'

# https://sssunho.tistory.com/29
"""
i=input;a=i()*i()*i()
for i in range(10):print`a`.count(`i`)
"""


# coding=utf-8
"""Tests that publish rpm plugin repositories."""
import unittest
from random import choice
from urllib.parse import urljoin

from requests.exceptions import HTTPError

from pulp_smash import api, config
from pulp_smash.pulp3.constants import REPO_PATH
from pulp_smash.pulp3.utils import (
    gen_repo,
    get_content,
    get_content_summary,
    get_versions,
    publish,
    sync,
)

from pulp_rpm.tests.functional.utils import (
    gen_rpm_publisher,
    gen_rpm_remote,
)
from pulp_rpm.tests.functional.constants import (
    RPM_ALT_LAYOUT_FIXTURE_URL,
    RPM_FIXTURE_SUMMARY,
    RPM_LONG_UPDATEINFO_FIXTURE_URL,
    RPM_PACKAGE_CONTENT_NAME,
    RPM_PUBLISHER_PATH,
    RPM_REFERENCES_UPDATEINFO_URL,
    RPM_REMOTE_PATH,
    RPM_RICH_WEAK_FIXTURE_URL,
    RPM_SHA512_FIXTURE_URL,
    SRPM_UNSIGNED_FIXTURE_URL,
)
from pulp_rpm.tests.functional.utils import set_up_module as setUpModule  # noqa:F401

class PublishAnyRepoVersionTestCase(unittest.TestCase):
    """Test whether a particular repository version can be published.

    This test targets the following issues:

    * `Pulp #3324 <https://pulp.plan.io/issues/3324>`_
    * `Pulp Smash #897 <https://github.com/PulpQE/pulp-smash/issues/897>`_
    """

    @classmethod
    def setUpClass(cls):
        """Create class-wide variables."""
        cls.cfg = config.get_config()
        cls.client = api.Client(cls.cfg, api.json_handler)

    def test_all(self):
        """Test whether a particular repository version can be published.

        1. Create a repository with at least 2 repository versions.
        2. Create a publication by supplying the latest ``repository_version``.
        3. Assert that the publication ``repository_version`` attribute points
           to the latest repository version.
        4. Create a publication by supplying the non-latest
           ``repository_version``.
        5. Assert that the publication ``repository_version`` attribute points
           to the supplied repository version.
        6. Assert that an exception is raised when providing two different
           repository versions to be published at same time.
        """
        body = gen_rpm_remote()
        remote = self.client.post(RPM_REMOTE_PATH, body)
        self.addCleanup(self.client.delete, remote['_href'])

        repo = self.client.post(REPO_PATH, gen_repo())
        self.addCleanup(self.client.delete, repo['_href'])

        sync(self.cfg, remote, repo)

        publisher = self.client.post(RPM_PUBLISHER_PATH, gen_rpm_publisher())
        self.addCleanup(self.client.delete, publisher['_href'])

        # Step 1
        repo = self.client.get(repo['_href'])
        for rpm_content in get_content(repo)[RPM_PACKAGE_CONTENT_NAME]:
            self.client.post(
                repo['_versions_href'],
                {'add_content_units': [rpm_content['_href']]}
            )
        version_hrefs = tuple(ver['_href'] for ver in get_versions(repo))
        non_latest = choice(version_hrefs[:-1])

        # Step 2
        publication = publish(self.cfg, publisher, repo)

        # Step 3
        self.assertEqual(publication['repository_version'], version_hrefs[-1])

        # Step 4
        publication = publish(self.cfg, publisher, repo, non_latest)

        # Step 5
        self.assertEqual(publication['repository_version'], non_latest)

        # Step 6
        with self.assertRaises(HTTPError):
            body = {
                'repository': repo['_href'],
                'repository_version': non_latest
            }
            self.client.post(urljoin(publisher['_href'], 'publish/'), body)

class SyncPublishReferencesUpdateTestCase(unittest.TestCase):
    """Sync/publish a repo that ``updateinfo.xml`` contains references."""

    @classmethod
    def setUpClass(cls):
        """Create class-wide variables."""
        cls.cfg = config.get_config()
        cls.client = api.Client(cls.cfg, api.json_handler)

    def test_all(self):
        """Sync/publish a repo that ``updateinfo.xml`` contains references.

        This test targets the following issue:

        `Pulp #3998 <https://pulp.plan.io/issues/3998>`_.
        """
        repo = self.client.post(REPO_PATH, gen_repo())
        self.addCleanup(self.client.delete, repo['_href'])

        body = gen_rpm_remote(url=RPM_REFERENCES_UPDATEINFO_URL)
        remote = self.client.post(RPM_REMOTE_PATH, body)
        self.addCleanup(self.client.delete, remote['_href'])

        sync(self.cfg, remote, repo)
        repo = self.client.get(repo['_href'])

        self.assertIsNotNone(repo['_latest_version_href'])

        content_summary = get_content_summary(repo)
        self.assertDictEqual(
            content_summary,
            RPM_FIXTURE_SUMMARY,
            content_summary
        )

        publisher = self.client.post(RPM_PUBLISHER_PATH, gen_rpm_publisher())
        self.addCleanup(self.client.delete, publisher['_href'])

        publication = publish(self.cfg, publisher, repo)
        self.addCleanup(self.client.delete, publication['_href'])

class SyncPublishTestCase(unittest.TestCase):
    """Test sync and publish for different RPM repositories.

    This test targets the following issue:

    `Pulp #4108 <https://pulp.plan.io/issues/4108>`_.
    `Pulp #4134 <https://pulp.plan.io/issues/4134>`_.
    """

    @classmethod
    def setUpClass(cls):
        """Create class-wide variables."""
        cls.cfg = config.get_config()
        cls.client = api.Client(cls.cfg, api.page_handler)

    def test_rpm_rich_weak(self):
        """Sync and publish an RPM repository. See :meth: `do_test`."""
        self.do_test(RPM_RICH_WEAK_FIXTURE_URL)

    def test_rpm_long_updateinfo(self):
        """Sync and publish an RPM repository. See :meth: `do_test`."""
        self.do_test(RPM_LONG_UPDATEINFO_FIXTURE_URL)

    def test_rpm_alt_layout(self):
        """Sync and publish an RPM repository. See :meth: `do_test`."""
        self.do_test(RPM_ALT_LAYOUT_FIXTURE_URL)

    def test_rpm_sha512(self):
        """Sync and publish an RPM repository. See :meth: `do_test`."""
        self.do_test(RPM_SHA512_FIXTURE_URL)

    def test_srpm(self):
        """Sync and publish a SRPM repository. See :meth: `do_test`."""
        self.do_test(SRPM_UNSIGNED_FIXTURE_URL)

    def do_test(self, url):
        """Sync and publish an RPM repository given a feed URL."""
        repo = self.client.post(REPO_PATH, gen_repo())
        self.addCleanup(self.client.delete, repo['_href'])

        remote = self.client.post(RPM_REMOTE_PATH, gen_rpm_remote(url=url))
        self.addCleanup(self.client.delete, remote['_href'])

        sync(self.cfg, remote, repo)
        repo = self.client.get(repo['_href'])

        self.assertIsNotNone(repo['_latest_version_href'])

        publisher = self.client.post(RPM_PUBLISHER_PATH, gen_rpm_publisher())
        self.addCleanup(self.client.delete, publisher['_href'])

        publication = publish(self.cfg, publisher, repo)
        self.addCleanup(self.client.delete, publication['_href'])


import collections
import re
from src.Validator.EmailValidator import EmailValidator

class GroupByDomainEmailProcessor:
    DOMAIN_REGEX = "^[^@]+@([^@]+\\.[a-zA-Z0-9]{1,4})$"
    __grouped_by_domains = {}

    def __init__(self, email_validator: EmailValidator):
        self.__email_validator = email_validator

    def process(self, email):
        if self.__email_validator.is_valid(email):
            domain = re.search(self.DOMAIN_REGEX, email).group(1)
            if domain not in self.__grouped_by_domains:
                self.__grouped_by_domains[domain] = [email]
            else:
                self.__grouped_by_domains[domain].append(email)

    def get_result(self):
        result = collections.OrderedDict(sorted(self.__grouped_by_domains.items()))
        for i in result:
            result[i] = list(dict.fromkeys(result[i]))
            result[i].sort()
        return result


'''
COMPOSITE
    - component
    - component
    - COMPOSITE
        - component
        - component
        - COMPOSITE
            - component
'''

class Component:
    def __init__(self, name):
        self.name = name

    def operation(self):
        print(f'{self.name}: Performing operation')

class Composite:
    def __init__(self, name):
        self.name = name
        self.children = []

    def add(self, component):
        self.children.append(component)

    def remove(self,component):
        self.children.remove(component)

    def operation(self):
        print(f'{self.name}: Performing operation')
        for child in self.children:
            child.operation()

leaf1 = Component('Leaf 1')
leaf2 = Component('Leaf 2')

composite1 = Composite('Composite 1')
composite1.add(leaf1)
composite1.add(leaf2)

composite2 = Composite('Composite 2')
leaf3 = Component('Leaf 3')
leaf4 = Component('Leaf 4')
leaf5 = Component('Leaf 5')

composite2.add(leaf3)
composite2.add(leaf4)
composite2.add(leaf5)

composite1.add(composite2)

composite1.operation()

02- Structural DP/03- Composite/01-exercise01.py


import logging
import sys

class Formatter(logging.Formatter):

    grey = "\x1b[38;20m"
    yellow = "\x1b[33;20m"
    red = "\x1b[31;20m"
    bold_red = "\x1b[31;1m"
    reset = "\x1b[0m"
    msg = "%(message)s"

    FORMATS = {
        logging.DEBUG: grey + msg + reset,
        logging.INFO: grey + msg + reset,
        logging.WARNING: yellow + msg + reset,
        logging.ERROR: red + msg + reset,
        logging.CRITICAL: bold_red + msg + reset
    }

    def format(self, record):
        log_fmt = self.FORMATS.get(record.levelno)
        formatter = logging.Formatter(log_fmt)
        return formatter.format(record)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
handler.setFormatter(Formatter())
# Add handler to stdout. Strangely just using basicConfig doesn't work
# May want to change the level to something else
# using an .ini config, or to put FileHandlers.
logger.addHandler(handler)


class Solution:
       def replaceElements(self, arr: List[int]) -> List[int]:
        l = len(arr)-1
        g, ug = arr[-1], arr[-1]
        while l>=0:
            if arr[l]>g:
                g = arr[l]
            arr[l] = ug
            l-=1
            ug = g
        arr[-1] = -1
        return arr

Arrays/1299. Replace Elements with Greatest Element on Right Side.py


import numpy as np
from scipy.linalg  import lu, lu_factor, lu_solve   # Biblioteca com funções para resolução LU
from time import process_time

# Imprime as saídas
PRINT = False

# Imprime a Matriz formatada
def printMatrix(A, b):
    y = 0
    for i in A:
        print("|", end='')
        for j in i:
            # Mostra os valores apena com 2 casas decimais
            v = "{:.2f}".format(j)
            print(f'{v:^8}', end="")
        # Mostra os valores apena com 2 casas decimais
        v = "{:.2f}".format(b[y])
        print("|", f'{v:^7}', end="|\n")
        y += 1

# Registra o tempo de inicio da execução
t1_start = process_time()

# Array usado na validação
A = np.array([ [11, -6,  -2,  0,  0,  0,  0],
            [-1,  3,   0, -2,  0,  0,  0],
            [-4,  0,   9, -2, -3,  0,  0],
            [ 0, -6,  -1, 10,  0, -3,  0],
            [ 0,  0,  -5,  0, 19,-10,  0],
            [ 0,  0,   0, -6, -3, 13, -4],
            [ 0,  0,   0,  0,  0, -5,  8]], float)

b = np.array([60, 0, 0, 0, 0, 0, 0], float )

# Imprime as matrizes
if PRINT:
    printMatrix(A,b)

# Função que decompõem a Matriz; P Matriz permutada, L triangular Inferior e U triangular superior
# P, L, U = lu(A)

# LU contém as matrizes Triangular U e L; piv contém a matriz de permutação
LU, piv = lu_factor(A)

# Resolve a matriz usando a fatoração LU
R = lu_solve((LU, piv), b)

# Imprime a resposta
print(R)

# Testa a solução com base na resposta encontrada em R
#print(np.allclose(np.dot(A, R), b))
#print(np.allclose(A @ R - b, np.zeros((len(A),))))

# Registra o tempo de final da execução
t1_stop = process_time()
print("Tempo decorrido do processamento [em segundos]:",
                                         t1_stop-t1_start)


import pytest

from vyper import compiler
from vyper.exceptions import StructureException

fail_list = [
    """
@external
def foo() -> int128:
    x: int128 = 45
    return x.codesize
    """
]

@pytest.mark.parametrize("bad_code", fail_list)
def test_block_fail(bad_code):
    with pytest.raises(StructureException):
        compiler.compile_code(bad_code)

valid_list = [
    """
@external
def foo() -> uint256:
    x: address = 0x1234567890123456789012345678901234567890
    return x.codesize
    """,
    """
@external
def foo() -> uint256:
    return self.codesize
    """,
    """
struct Foo:
    t: address
foo: Foo

@external
def bar() -> uint256:
    return self.foo.t.codesize
    """,
]

@pytest.mark.parametrize("good_code", valid_list)
def test_block_success(good_code):
    assert compiler.compile_code(good_code) is not None


from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

# python manage.py migrate --> allows tables to be created
#tasks = []

class NewTaskForm(forms.Form):
    task = forms.CharField(label="New Task")
    #priority = forms.IntegerField(label="Priority", min_value=1, max_value=10)

# Create your views here.
def index(request):
    if "tasks" not in request.session:  # is there any "tasks" inside of the session
        request.session["tasks"] = []   # if not, give a tasks

    return render(request, "tasks/index.html", {
        "tasks": request.session["tasks"]
    })

def add(request):
    if request.method == "POST":
        form = NewTaskForm(request.POST) # user's submitted data
        if form.is_valid():
            task = form.cleaned_data["task"] #all data user submitted
            request.session["tasks"] += [task] #adds the task comes from user post to the session's tasks
            return HttpResponseRedirect(reverse("tasks:index")) # reverse -> finds url of tasks:index
        else:
            return render(request, "tasks/add.html", {
                "form": form
            })
    return render(request, "tasks/add.html", {
        "form": NewTaskForm()
    })


from util.server_util import install_heroku_cli
import subprocess
import json

import  sqlalchemy as db

def get_database_url(task_name):
    try:
        with open("../appname.json", "r") as f:
            loaded_dict = json.loads(f.read())
            if task_name not in loaded_dict:
                raise RuntimeError("Uh oh. This task name does not exist!")
            app_name = loaded_dict[task_name]
            print(f"Reading database for {app_name}")
        heroku_executable_path = install_heroku_cli()
        result = subprocess.check_output([f'{heroku_executable_path}', 'config:get', 'DATABASE_URL',  '-a',  f'{app_name}'])
        return result.decode('ascii').strip()
    except:
        raise RuntimeError('"appname.json" in ../app does not exist! Please run the task first via launch_hits.py!')

def query_raw_data(task_name):
    engine = db.create_engine(get_database_url(task_name), echo=False)
    connection = engine.connect()
    metadata = db.MetaData()
    tasks = db.Table('task', metadata, autoload=True, autoload_with=engine)
    query = db.select([tasks])
    ResultProxy = connection.execute(query)
    ResultSet = ResultProxy.fetchall()
    return ResultSet

def load_data(task_name):
    results = []
    raw_data = query_raw_data(task_name)
    for i in raw_data:
        d = json.loads(i[2])

        for key in ['messages', 'text', 'compare_bot_a', 'compare_bot_b', 'compare_bot_preference', 'pipeline']:
            try:
                del d[key]
            except:
                pass

        results.append(d)
    return results


# coding: utf-8

# In[1]:

from keras.utils.vis_utils import plot_model
import numpy as np
from keras.models import Sequential
from keras.layers import Activation, TimeDistributed, Dense, RepeatVector, CuDNNLSTM, GRU, Bidirectional, Input, CuDNNGRU
from keras.utils import np_utils
from keras.callbacks import TensorBoard
import tensorflow as tf
import os
from attention_decoder import AttentionDecoder
from keras import backend as K
from keras.models import Model
from keras.layers.core import Dense, Reshape
from keras.layers.wrappers import TimeDistributed
import difflib
from keras.models import load_model
import keras

K.clear_session()
keras.backend.clear_session()

X_train=np.load('prepData/X_train_camFam3_1mutOnly_v3_chr2.npy')
X_val=np.load('prepData/X_val_camFam3_1mutOnly_v3_chr2.npy')
X_test=np.load('prepData/X_test_camFam3_1mutOnly_v3_chr2.npy')
y_train=np.load('prepData/y_train_camFam3_1mutOnly_v3_chr2.npy')
y_val=np.load('prepData/y_val_camFam3_1mutOnly_v3_chr2.npy')
y_test=np.load('prepData/y_test_camFam3_1mutOnly_v3_chr2.npy')

y_train1 = np.load('prepData/y_train1_camFam3_1mutOnly_v3_chr2.npy')
y_val1 = np.load('prepData/y_val1_camFam3_1mutOnly_v3_chr2.npy')
y_test1 = np.load('prepData/y_test1_camFam3_1mutOnly_v3_chr2.npy')

# X_train=np.load('prepData/X_train_camFam3_1mut.npy')
# X_val=np.load('prepData/X_val_camFam3_1mut.npy')
# X_test=np.load('prepData/X_test_camFam3_1mut.npy')
# y_train=np.load('prepData/y_train_camFam3_1mut.npy')
# y_val=np.load('prepData/y_val_camFam3_1mut.npy')
# y_test=np.load('prepData/y_test_camFam3_1mut.npy')

# X_train=np.load('prepData20/X_train_camFam3_1mutOnly.npy')
# X_val=np.load('prepData20/X_val_camFam3_1mutOnly.npy')
# X_test=np.load('prepData20/X_test_camFam3_1mutOnly.npy')
# y_train=np.load('prepData20/y_train_camFam3_1mutOnly.npy')
# y_val=np.load('prepData20/y_val_camFam3_1mutOnly.npy')
# y_test=np.load('prepData20/y_test_camFam3_1mutOnly.npy')
nucleotide = ['0', 'A', 'C', 'G', 'T', '-']
#model5 = load_model('model/seq2seq_nogap_camFam3_1mutOnly.h5')
def decoder(array):
    result = ""
    size = len(array)
    for i in range(size):
        if array[i].tolist() == [1, 0, 0, 0, 0, 0]:
            result=result+"0"
        elif array[i].tolist() == [0, 1, 0, 0, 0, 0]:
            result=result+"A"
        elif array[i].tolist() == [0, 0, 1, 0, 0, 0]:
            result=result+"C"
        elif array[i].tolist() == [0, 0, 0, 1, 0, 0]:
            result=result+"G"
        elif array[i].tolist() == [0, 0, 0, 0, 1, 0]:
            result=result+"T"
        elif array[i].tolist() == [0, 0, 0, 0, 0, 1]:
            result=result+"-"
    return result

#model5 = load_model('model/seq2seq_nogap_camFam3_1mutOnly.h5')
def decoderY(array):
    result = ""
    size = len(array)

    if array.tolist() == [1, 0, 0, 0, 0, 0]:
        result=result+"0"
    elif array.tolist() == [0, 1, 0, 0, 0, 0]:
        result=result+"A"
    elif array.tolist() == [0, 0, 1, 0, 0, 0]:
        result=result+"C"
    elif array.tolist() == [0, 0, 0, 1, 0, 0]:
        result=result+"G"
    elif array.tolist() == [0, 0, 0, 0, 1, 0]:
        result=result+"T"
    elif array.tolist() == [0, 0, 0, 0, 0, 1]:
        result=result+"-"
    return result

def printHitMiss(a,b):
    if a==b:
        return 'Hit'
    else:
        return 'Miss'

print(X_test.shape)
def accuracy(a, b):
    count = 0
    for i in range(len(a)):
        if a[i] == b[i]:
            count = count+1
    return count/len(a)

def accuracy2(a, b, c):
    count = 0
    count2 =0
    for i in range(len(a)):
        if a[i] != c[i]:
            count2 = count2 +1
        if a[i] != c[i] and b[i]==c[i]:
            count = count+1
    return count/count2

def isMutation(a, b):
    if a!= b:
        print("mutation")

def predict(model):
    x_true=[]
    y_hat_list = []
    y_true = []
    predictions = model.predict(X_test, batch_size=250, verbose=0)

    for i, prediction in enumerate(predictions):
        #print(prediction)
        # print(prediction)
        #x_index = np.argmax(testX[i], axis=1)
        #print(prediction[i])
        x_str = decoder(X_test[i])

        index = np.argmax(prediction)
        result = [nucleotide[index]]

        print(''.join(x_str), ' -> ', ''.join(result),
              " true: ", ''.join(decoderY(y_test[i])), printHitMiss(''.join(result), ''.join(decoderY(y_test[i]))))
        x_true.append(''.join(x_str[5]))
        y_hat_list.append(''.join(result))
        y_true.append(''.join(decoderY(y_test[i])))
    sm=difflib.SequenceMatcher(None,y_hat_list,y_true)
    sm2=difflib.SequenceMatcher(None,y_hat_list,x_true)
    print()
    print("Percentage of target and prediction being identical: {}".format(accuracy(y_hat_list, y_true)))
    print("Percentage of training and prediction being identical: {}".format(accuracy(y_hat_list, x_true)))
    print("Accuracy given mutation happened : {}".format(accuracy2(x_true, y_hat_list, y_true)))
    return x_true, y_hat_list, y_true

# In[2]:

batch_size = 1000  # Batch size for training.
epochs = 45  # Number of epochs to train for.
latent_dim = 256  # Latent dimensionality of the encoding space.
num_samples = 10000  # Number of samples to train on.

def lstm_model():
    encoder_inputs = Input(shape=(None, 6))
    encoder = CuDNNLSTM(latent_dim, return_state=True)
    encoder_outputs, state_h, state_c = encoder(encoder_inputs)
    # We discard `encoder_outputs` and only keep the states.
    encoder_states = [state_h, state_c]

    # Set up the decoder, using `encoder_states` as initial state.
    decoder_inputs = Input(shape=(None, 6))
    # We set up our decoder to return full output sequences,
    # and to return internal states as well. We don't use the
    # return states in the training model, but we will use them in inference.
    decoder_lstm = CuDNNLSTM(latent_dim, return_sequences=True, return_state=True)
    decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                         initial_state=encoder_states)
    decoder_dense = Dense(6, activation='softmax')
    decoder_outputs = decoder_dense(decoder_outputs)

    # Define the model that will turn
    # `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
    model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

    # inference
    encoder_model = Model(encoder_inputs, encoder_states)

    decoder_state_input_h = Input(shape=(latent_dim,))
    decoder_state_input_c = Input(shape=(latent_dim,))
    decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
    decoder_outputs, state_h, state_c = decoder_lstm(
        decoder_inputs, initial_state=decoder_states_inputs)
    decoder_states = [state_h, state_c]
    decoder_outputs = decoder_dense(decoder_outputs)
    decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)

    # Run training
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy']
                  )
    return model, encoder_model, decoder_model

def gru_model():
    encoder_inputs = Input(shape=(None, 6))
    encoder = CuDNNGRU(latent_dim, return_state=True)
    encoder_outputs, state_h = encoder(encoder_inputs)

    decoder_inputs = Input(shape=(None, 6))
    decoder_gru = CuDNNGRU(latent_dim, return_sequences=True)
    decoder_outputs = decoder_gru(decoder_inputs, initial_state=state_h)
    decoder_dense = Dense(6, activation='softmax')
    decoder_outputs = decoder_dense(decoder_outputs)
    model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy']
                  )
    # inference
    encoder_model = Model(encoder_inputs, state_h)

    decoder_state_input_h = Input(shape=(latent_dim,))
    #decoder_state_input_c = Input(shape=(latent_dim,))
    decoder_states_inputs = decoder_state_input_h
    decoder_outputs, state_h = decoder_gru(
        decoder_inputs, initial_state=decoder_states_inputs)
    decoder_states = state_h
    decoder_outputs = decoder_dense(decoder_outputs)
    decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)

    return model, encoder_model, decoder_model

model1, encoder_model1, decoder_model1 = lstm_model()
model1.fit([X_train, y_train1], y_train,
          batch_size=1000,
          epochs=epochs,
          validation_data=([X_val,y_val1], y_val),
          verbose = 1
         )

model2, encoder_model2, decoder_model2 = lstm_model()
model2.fit([X_train, y_train1], y_train,
          batch_size=1000,
          epochs=150,
          validation_data=([X_val,y_val1], y_val),
          verbose = 1
         )
'''
model2, inference_model2 = gru_model()
model2.fit([X_train, y_train1], y_train,
          batch_size=1000,
          epochs=45,
          validation_data=([X_val,y_val1], y_val),
          verbose = 1
         )
'''

# In[27]:

# Save model
#model.save('s2s.h5')

def decode_sequence(input_seq, model, encoder_model, decoder_model):
    # Encode the input as state vectors.
    states_value = encoder_model.predict(input_seq)
    #print(states_value)
    # Generate empty target sequence of length 1.
    target_seq = np.zeros((1, 1, 6))
    target_seq[0][0]= np.array([1,0,0,0,0,0])
    #print(target_seq)
    # Populate the first character of target sequence with the start character.

    # Sampling loop for a batch of sequences
    # (to simplify, here we assume a batch of size 1).
    stop_condition = False
    decoded_sentence = ''
    while not stop_condition:
        output_tokens, h, c = decoder_model.predict(
            [target_seq] + states_value)

        # Sample a token
        sampled_token_index = np.argmax(output_tokens[0, -1, :])
        sampled_char = nucleotide[sampled_token_index]
        decoded_sentence += sampled_char

        # Exit condition: either hit max length
        # or find stop character.
        if (len(decoded_sentence) == 10):
            stop_condition = True

        # Update the target sequence (of length 1).
        target_seq = np.zeros((1, 1, 6))
        target_seq[0, 0, sampled_token_index] = 1.

        # Update states
        states_value = [h, c]

    return decoded_sentence

#for seq_index in range(1):
def predict2(X_test, y_test, model, encoder_model, decoder_model):
    x_true =[]
    y_hat =[]
    y_true =[]
    for seq_index in range(len(X_test)):
        # Take one sequence (part of the training set)
        # for trying out decoding.
        input_seq = X_test[seq_index: seq_index + 1]
        decoded_sentence = decode_sequence(input_seq, model, encoder_model, decoder_model)
        input_sen = decoder(input_seq[0])
        #print('-')
        print(input_sen, ' -> ', decoded_sentence, 'True:', decoder(y_test[seq_index]), printHitMiss(decoded_sentence, decoder(y_test[seq_index])))
        x_true.append(input_sen)
        y_hat.append(decoded_sentence)
        y_true.append(decoder(y_test[seq_index]))
    print("Percentage of target and prediction being identical: {}".format(accuracy(y_hat, y_true)))
    print("Percentage of training and prediction being identical: {}".format(accuracy(y_hat, x_true)))
    print("Accuracy given mutation happened : {}".format(accuracy2(x_true, y_hat, y_true)))
    return x_true, y_hat, y_true

# In[29]:

#model1.summary()
#model2.summary()

#encoder_model1.summary()
#decoder_model1.summary()

# In[28]:

x_true, y_hat, y_true = predict2(X_test, y_test, model1, encoder_model1, decoder_model1)

# In[22]:

x_true2, y_hat2, y_true2 = predict2(X_test, y_test, model2, encoder_model2, decoder_model2)

# ## Finding ratio of mutation on predicted sequences

# In[7]:

def countMutation(ancestor, target, realDes):
    AtoG = 0
    AtoC = 0
    AtoT= 0
    GtoA= 0
    GtoC= 0
    GtoT= 0
    CtoA= 0
    CtoG= 0
    CtoT= 0
    TtoA= 0
    TtoG= 0
    TtoC= 0
    transitionCorrect = 0
    transversionCorrect = 0
    for j in range(len(ancestor)):
        for i in range(10):

                if ancestor[j][i] == 'A' and target[j][i] == 'G':
                    AtoG = AtoG+1
                    if target[j][i] == realDes[j][i]:
                        transitionCorrect+=1
                elif ancestor[j][i] == 'A' and target[j][i] == 'C':
                    AtoC = AtoC+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'A' and target[j][i] == 'T':
                    AtoT = AtoT+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'G' and target[j][i] == 'A':
                    GtoA = GtoA+1
                    if target[j][i] == realDes[j][i]:
                        transitionCorrect +=1
                elif ancestor[j][i] == 'G' and target[j][i] == 'C':
                    GtoC = GtoC+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'G' and target[j][i] == 'T':
                    GtoT = GtoT+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'C' and target[j][i] == 'A':
                    CtoA = CtoA+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'C' and target[j][i] == 'G':
                    CtoG = CtoG+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'C' and target[j][i] == 'T':
                    CtoT = CtoT+1
                    if target[j][i] == realDes[j][i]:
                        transitionCorrect +=1
                elif ancestor[j][i] == 'T' and target[j][i] == 'A':
                    TtoA = TtoA+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'T' and target[j][i] == 'G':
                    TtoG = TtoG+1
                    if target[j][i] == realDes[j][i]:
                        transversionCorrect +=1
                elif ancestor[j][i] == 'T' and target[j][i] == 'C':
                    TtoC = TtoC+1
                    if target[j][i] == realDes[j][i]:
                        transitionCorrect +=1
    print('This is A-->G :{}'.format(AtoG))
    print('This is A-->C :{}'.format(AtoC))
    print('This is A-->T :{}'.format(AtoT))
    print('This is G-->A :{}'.format(GtoA))
    print('This is G-->C :{}'.format(GtoC))
    print('This is G-->T :{}'.format(GtoT))
    print('This is C-->A :{}'.format(CtoA))
    print('This is C-->G :{}'.format(CtoG))
    print('This is C-->T :{}'.format(CtoT))
    print('This is T-->A :{}'.format(TtoA))
    print('This is T-->G :{}'.format(TtoG))
    print('This is T-->C :{}'.format(TtoC))
    numMutation = AtoG+AtoC+AtoT+GtoA+GtoC+GtoT+CtoA+CtoG+CtoT+TtoA+TtoG+TtoC
    transition = AtoG+GtoA+CtoT+TtoC
    transversion = numMutation-transition
    print('Total number of mutations: {}'.format(numMutation))
    print('Number of transitions: {}'.format( transition))
    print('Number of transversions: {}'.format(transversion))
    print('Ratio of transition/transversion : {}'.format(transition/transversion))
    print('Percentage of mutation : {}'.format(numMutation/(10*(len(X_test)))))
    print('Percentage of transition predicted correct : {}'.format(transitionCorrect/transition))
    print('Percentage of transversion predicted correct : {}'.format(transversionCorrect/transversion))

#countMutation(x_true6, y_hat6, y_true6)
#countMutation(x_true8, y_hat8, y_true8)

# ## Mutation Ratio

# In[8]:

countMutation(x_true, y_hat, y_true)

# In[9]:

countMutation(x_true2, y_hat2, y_true2)

# In[10]:

countMutation(x_true, y_true, y_true)

# In[30]:

X_test5=np.load('prepData/X_test_camFam3_1mutOnly_v3.npy')
y_test5=np.load('prepData/y_test_camFam3_1mutOnly_v3.npy')
x_true5, y_hat5, y_true5 = predict2(X_test5, y_test5, model1, encoder_model1, decoder_model1)

# In[31]:

x_true6, y_hat6, y_true6 = predict2(X_test5, y_test5, model2, encoder_model2, decoder_model2)

# In[13]:

countMutation(x_true5, y_hat5, y_true5)
countMutation(x_true6, y_hat6, y_true6)
countMutation(x_true5, y_true5, y_true5)

# In[14]:

#K.clear_session()


import json

def show_menu():
    print('\n---')
    print('Example code snippets menu')
    print('1 - Load data from a JSON file')
    print('2 - Load data from a JSON string')
    print('3 - Write data to a JSON file')
    print('4 - Write data to a JSON string')
    print('5 - Write data to a JSON file with indentation')
    print('S - Stop the program')
    print('---')
    menu_item = input('Select menu item: ')
    print()
    if menu_item not in ['S', 's']:
        if menu_item == '1':
            load_data_from_json_file()
        elif menu_item == '2':
            load_data_from_json_string()
        elif menu_item == '3':
            write_data_to_a_json_file()
        elif menu_item == '4':
            write_data_to_json_string()
        elif menu_item == '5':
            write_data_to_a_json_file_with_indentation()
        else:
            print('Invalid menu selection. Please try again.')
        show_menu()

def load_data_from_json_file():
    # Declare the data dictionary before the with statement,
    # so that we can access it after the file has closed.
    recipes_data = {}
    with open('recipes-original.json') as file:
        recipes_data = json.load(file)

    # Iterate through each of the recipes in the "recipes" list in the data
    # and print details of each recipe
    recipes = recipes_data['recipes']
    for recipe in recipes:
        # Get the values of name and serves attributes, assign to variables for printing
        name = recipe['name']
        serves = recipe['serves']
        print(f'---\n{name}, serves: {serves}\n')

        # Prints all of the ingredient children data, from ingredients list
        ingredients = recipe['ingredients']
        for ingredient in ingredients:
            print(f' * {ingredient}')
        print(f'---\n')

def load_data_from_json_string():
    # Create a string that has data about countries in JSON format
    data = '{"countries": [' + \
        '{"name": "Estonia", "population_millions": 1.3, "neighbours": ["Latvia", "Russia"]},' + \
        '{"name": "Belarus", "population_millions": 9.5, "neighbours": ["Lithuania", "Latvia", "Poland", "Russia", "Ukraine"]}' + \
        ']}'
    print(f'String to parse as JSON data: {data}')

    # Load the JSON string into a dictionary
    data_dict = json.loads(data)
    print(f"Loaded the JSON data into a dictionary")

    countries = data_dict['countries']
    for country in countries:
        # Assign name and population to variables for printing
        name = country['name']
        population = country['population_millions']
        print(f'\nCountry: {name}')
        print(f'Population (in millions): {population}\n')

        # Iterate through the neighbouring countries for the country
        print('Neighbours:')
        neighbours = country['neighbours']
        for neighbour in neighbours:
            print(f'* {neighbour}')
        print('---')

def write_data_to_a_json_file():
    # Create a dictionary of lists for writing to json file
    favourite_recipes = {
        'recipes': [
            {
                'name': 'Nachos',
                'serves': 4,
                'ingredients': [
                    '8 cherry tomatoes', '100g natural corn chips',
                    '2/3 cup Hommus', '1/4 cup fresh parsley'
                ]
            },
            {
                'name': 'Mushroom Risotto',
                'serves': 4,
                'ingredients': [
                    '2 cups mushrooms, sliced', '1 cup Arborio rice',
                    '4 cups vegetable stock', '1 cup grated Parmesan cheese'
                ]
            }
        ]
    }
    number_of_recipes = len(favourite_recipes['recipes'])

    # Open a file and write the dictionary of lists to file in json format
    with open('favourite-recipes.json', 'w') as file:
        json.dump(favourite_recipes, file)
    print(f'Wrote {number_of_recipes} recipes to a json file')

def write_data_to_json_string():
    # Create a dictionary with data for two countries
    countries_data = {
        'countries': [
            {
                'name': 'Latvia',
                'population_in_millions': 1.9,
                'countries': ['Belarus', 'Estonia', 'Lithuania', 'Russia']
            },
            {
                'name': 'Finland',
                'population_in_millions': 5.5,
                'countries': ['Norway', 'Russia', 'Sweden']
            }
        ]
    }
    # Write the countries data to a string in JSON format
    countries_string = json.dumps(countries_data)
    print('String created with json.dumps printed below:\n')
    print(countries_string)

def write_data_to_a_json_file_with_indentation():
    # Open a json file with recipes that has no indentation
    recipe_data = {}
    with open('recipes-no-format.json') as file:
        recipe_data = json.load(file)

    # Create a json file and write the recipes data with indentation (note the indent argument)
    with open('recipes-indentation.json', 'w') as file:
        json.dump(recipe_data, file, indent=4)
    print("\nWrote the data to the 'recipes-indentation.json' file, with indent of 4 spaces.\n")

# Mainline of the program
show_menu()


from hklearn_genetic.genetic_algorithm import GeneticAlgorithm
from hklearn_genetic.problem import IntegerNQueen, RealNQueen, BinaryNQueen
from scipy import signal
from utils import average_list_of_lists
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import logging
import logging.handlers
import os

# labels = ['G1', 'G2', 'G3', 'G4', 'G5']
# men_means = [20, 34, 30, 35, 27]
# women_means = [25, 32, 34, 20, 25]
# child_means = [25, 32, 34, 20, 25]

# x = np.arange(len(labels))  # the label locations
# width = 0.2  # the width of the bars

# fig, ax = plt.subplots()
# rects1 = ax.bar(x - width, men_means, width, label='Men')
# rects2 = ax.bar(x, women_means, width, label='Women')
# rects3 = ax.bar(x + width, child_means, width, label='Children')

# # Add some text for labels, title and custom x-axis tick labels, etc.
# ax.set_ylabel('Scores')
# ax.set_title('Scores by group and gender')
# ax.set_xticks(x)
# ax.set_xticklabels(labels)
# ax.legend()

# ax.bar_label(rects1, padding=3)
# ax.bar_label(rects2, padding=3)
# ax.bar_label(rects3, padding=3)

# fig.tight_layout()

# plt.show()

handler = logging.handlers.WatchedFileHandler(os.environ.get("LOGFILE", "nqueen_tests.log"))
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "INFO"))
root.addHandler(handler)

colors = ['b', 'g', 'y', 'c', 'k', 'm']
params = {"Integer coding":500, "Real coding":500, "Binary coding":1000}
integer_means = []
integer_best = []
integer_global_count = []
real_means = []
real_best = []
real_global_count = []
binary_means = []
binary_best = []
binary_global_count = []
n_groups = list(range(12, 12 + 6*4 - 1, 4))
for dim in n_groups:
    gens_li_dict = {"Integer coding":[], "Real coding":[], "Binary coding":[]}
    ga_best_li_dict = {"Integer coding":[], "Real coding":[], "Binary coding":[]}
    ga_avg_li_dict = {"Integer coding":[], "Real coding":[], "Binary coding":[]}
    integer_best += [float('inf')]
    real_best += [float('inf')]
    binary_best += [float('inf')]
    integer_global_count += [0]
    real_global_count += [0]
    binary_global_count += [0]
    logging.info(f"Solutions for N = "f"{dim}")
    print(f"Solutions for N = "f"{dim}")
    for run in range(5):
        i_nqueen = IntegerNQueen(n_dim = dim)
        r_nqueen = RealNQueen(n_dim=dim)
        b_nqueen = BinaryNQueen(n_dim=dim)
        logging.info(f"Solutions for run = "f"{run}")
        print(f"Solutions for run = "f"{run}")

        ga = GeneticAlgorithm(pc = 0.9, pm = 1./dim, max_iter= params["Integer coding"], selection="tournament", elitism=0.1)
        sol, gens = ga.evolve(i_nqueen, 500)
        if gens < params["Integer coding"]:
            integer_best[-1] = 0
            integer_global_count[-1]+=1
            logging.info("Integer coding global solution: ")
            print("Integer coding global solution: ")
        else:
            integer_best[-1] = -ga.best[-1] if integer_best[-1] > -ga.best[-1] else integer_best[-1]
            logging.info("Integer coding local solution: ")
            print("Integer coding local solution: ")
        logging.info(sol)
        print(sol)
        logging.info(f"After "f"{gens}"f" generations")

        best = [-b for b in ga.best]
        avgs = [-avg for avg in ga.averages]
        gens_li_dict["Integer coding"]+=[gens]
        ga_best_li_dict["Integer coding"]+=[best]
        ga_avg_li_dict["Integer coding"]+=[avgs]
        # fig, axs = plt.subplots(2, 1, constrained_layout=True)
        # axs[0].plot(np.arange(gens), best)
        # axs[0].set_title(f"Integer coding, N = "f"{dim}")
        # axs[0].set_xlabel('Generations')
        # axs[0].set_ylabel('Best fitness')
        # fig.suptitle("N Queen Problem", fontsize=16)
        # axs[1].plot(np.arange(gens), avgs)
        # axs[1].set_title(f"Integer coding, N = "f"{dim}")
        # axs[1].set_xlabel('Generations')
        # axs[1].set_ylabel('Average fitness')
        # plt.show()

        ga = GeneticAlgorithm(pc = 0.9, pm = 1./dim, max_iter= params["Real coding"], selection="tournament", elitism=0.1)
        sol, gens = ga.evolve(r_nqueen, 500)
        if gens < params["Real coding"]:
            real_global_count[-1]+=1
            real_best[-1] = 0
            logging.info("Real coding global solution: ")
            print("Real coding global solution: ")
        else:
            real_best[-1] = -ga.best[-1] if real_best[-1] > -ga.best[-1] else real_best[-1]
            logging.info("Real coding local solution: ")
            print("Real coding local solution: ")
        logging.info(sol)
        print(sol)
        logging.info(f"After "f"{gens}"f" generations")
        best = [-b for b in ga.best]
        avgs = [-avg for avg in ga.averages]
        gens_li_dict["Real coding"]+=[gens]
        ga_best_li_dict["Real coding"]+=[best]
        ga_avg_li_dict["Real coding"]+=[avgs]
        # fig, axs = plt.subplots(2, 1, constrained_layout=True)
        # axs[0].plot(np.arange(gens), best)
        # axs[0].set_title(f"Real coding, N = "f"{dim}")
        # axs[0].set_xlabel('Generations')
        # axs[0].set_ylabel('Best fitness')
        # fig.suptitle("N Queen Problem", fontsize=16)
        # axs[1].plot(np.arange(gens), avgs)
        # axs[1].set_title(f"Real coding, N = "f"{dim}")
        # axs[1].set_xlabel('Generations')
        # axs[1].set_ylabel('Average fitness')
        # plt.show()

        ga = GeneticAlgorithm(pc = 0.9, pm = 1./(b_nqueen.gene_length*b_nqueen.n_dim), max_iter= params["Binary coding"], selection="tournament", elitism=0.1)
        sol, gens = ga.evolve(b_nqueen, 500)
        if gens < params["Binary coding"]:
            binary_global_count[-1]+=1
            binary_best[-1] = 0
            logging.info("Binary coding global solution: ")
            print("Binary coding global solution: ")
        else:
            binary_best[-1] = -ga.best[-1] if binary_best[-1] > -ga.best[-1] else binary_best[-1]
            logging.info("Binary coding local solution: ")
            print("Binary coding local solution: ")
        logging.info(sol)
        print(sol)
        logging.info(f"After "f"{gens}"f" generations")
        best = [-b for b in ga.best]
        avgs = [-avg for avg in ga.averages]
        gens_li_dict["Binary coding"]+=[gens]
        ga_best_li_dict["Binary coding"]+=[best]
        ga_avg_li_dict["Binary coding"]+=[avgs]
        # fig, axs = plt.subplots(2, 1, constrained_layout=True)
        # axs[0].plot(np.arange(gens), best)
        # axs[0].set_title(f"Binary coding, N = "f"{dim}")
        # axs[0].set_xlabel('Generations')
        # axs[0].set_ylabel('Best fitness')
        # fig.suptitle("N Queen Problem", fontsize=16)
        # axs[1].plot(np.arange(gens), avgs)
        # axs[1].set_title(f"Binary coding, N = "f"{dim}")
        # axs[1].set_xlabel('Generations')
        # axs[1].set_ylabel('Average fitness')
        # plt.show()

    for key in ["Integer coding", "Real coding", "Binary coding"]:
        gens_avg = np.average(gens_li_dict[key])
        if key == "Integer coding":
            integer_means += [gens_avg]
        elif key == "Real coding":
            real_means += [gens_avg]
        else:
            binary_means += [gens_avg]
        # for i in range(len(ga_best_li_dict[key])):
        #     if len(ga_best_li_dict[key][i]) < params[key]:
        #         ga_best_li_dict[key][i] += list(np.zeros(params[key] - len(ga_best_li_dict[key][i])))
        # for i in range(len(ga_avg_li_dict[key])):
        #     if len(ga_avg_li_dict[key][i]) < params[key]:
        #         ga_avg_li_dict[key][i] += list(np.zeros(params[key] - len(ga_avg_li_dict[key][i])))
        # ga_best_avg = np.average(np.array(ga_best_li_dict[key]), axis=0)
        # ga_avg_avg = np.average(np.array(ga_avg_li_dict[key]), axis=0)
        fig, axs = plt.subplots(2, 1, constrained_layout=True)
        for i, data in enumerate(ga_best_li_dict[key]):
            if len(data) <= int(gens_avg):
                axs[0].plot(np.arange(len(data)), data, color = colors[i] ,label = f'Run: {i}')
            else:
                axs[0].plot(np.arange(int(gens_avg)), data[0 : int(gens_avg)], color = colors[i], label = f'Run: {i}')
        for i, data in enumerate(ga_avg_li_dict[key]):
            if len(data) <= int(gens_avg):
                axs[1].plot(np.arange(len(data)), data, color = colors[i], label = f'Run: {i}')
            else:
                axs[1].plot(np.arange(int(gens_avg)), data[0 : int(gens_avg)], color = colors[i], label = f'Run: {i}')
        ga_best_avg = average_list_of_lists(ga_best_li_dict[key])
        ga_avg_avg = average_list_of_lists(ga_avg_li_dict[key])

        axs[0].plot(np.arange(int(gens_avg)), ga_best_avg[0 : int(gens_avg)], color = 'r', label = 'Average')
        axs[0].set_title(f"{key} "f", N = "f"{dim}")
        axs[0].set_xlabel('Generations')
        axs[0].set_ylabel('Best fitness')
        fig.suptitle("N Queen Problem for 5 runs", fontsize=16)
        axs[1].plot(np.arange(int(gens_avg)), ga_avg_avg[0 : int(gens_avg)], color = 'r', label = 'Average')
        axs[1].set_title(f"{key} "f", N = "f"{dim}")
        axs[1].set_xlabel('Generations')
        axs[1].set_ylabel('Average fitness')
        plt.legend()
        plt.savefig(f"{key}"f"_{dim}", bbox_inches='tight')
        #plt.show()

x = np.arange(len(n_groups))  # the label locations
width = 0.2  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width, integer_best, width, label='Integer')
rects2 = ax.bar(x, real_best, width, label='Real')
rects3 = ax.bar(x + width, binary_best, width, label='Binary')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Best fitness')
ax.set_title('Best fitness by Nqueens and coding')
ax.set_xticks(x)
ax.set_xticklabels(n_groups)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
ax.bar_label(rects3, padding=3)

fig.tight_layout()

plt.savefig("Best_Fitness", bbox_inches='tight')
#plt.show()

fig, ax = plt.subplots()
rects1 = ax.bar(x - width, integer_global_count, width, label='Integer')
rects2 = ax.bar(x, real_global_count, width, label='Real')
rects3 = ax.bar(x + width, binary_global_count, width, label='Binary')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Global solutions')
ax.set_title('Global solutions found by Nqueens and coding')
ax.set_xticks(x)
ax.set_xticklabels(n_groups)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
ax.bar_label(rects3, padding=3)

fig.tight_layout()

plt.savefig("Global_Solutions", bbox_inches='tight')
#plt.show()


from django.conf.urls import patterns, include, url
from django.contrib import admin
from records.views import index, set_language, CreateRecordView, UpdateRecordView,\
         DeleteRecordView, UpdateInitialBalanceView, CreateInitialBalanceView,\
         CreateUnscheduledDebtView, CreateUnscheduledCreditView, UpdateUnscheduledDebtView,\
         UpdateUnscheduledCreditView, CreateRecurrentMonthView, EditRecurrentMonthView
from profiles.views import set_timezone, UpdateProfileView
from django.core.urlresolvers import reverse_lazy

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    # User general
    url(r'^logout/$', 'django.contrib.auth.views.logout',
         name="logout"),
    url(r'^i18n/$', set_language, name='set_language'),
    url(r'^tz/$', set_language, name='set_language'),
    url(r'^profile/update/(?P<pk>\d+)/$', UpdateProfileView.as_view(), name='update_profile'),

    # Records
    url(r'^record/create/(?P<type>\d+)/$', CreateRecordView.as_view(), name='create_record'),
    url(r'^record/update/(?P<pk>\d+)/$', UpdateRecordView.as_view(), name='update_record'),
    url(r'^record/delete/(?P<pk>\d+)/$', DeleteRecordView.as_view(), name='delete_record'),
    url(r'^record/recurrent/create/(?P<parent_pk>\d+)/(?P<month>\d+)/(?P<year>\d+)/$', CreateRecurrentMonthView.as_view(), name='create_recurrent_month'),
    url(r'^record/recurrent/edit/(?P<pk>\d+)/$', EditRecurrentMonthView.as_view(), name='edit_recurrent_month'),

    # System accounts
    url(r'^balance/create/$', CreateInitialBalanceView.as_view(), name='create_initial_balance'),
    url(r'^balance/update/(?P<pk>\d+)/$', UpdateInitialBalanceView.as_view(), name='update_initial_balance'),
    url(r'^u_debt/create/$', CreateUnscheduledDebtView.as_view(), name='create_unscheduled_debt'),
    url(r'^u_debt/update/(?P<pk>\d+)/$', UpdateUnscheduledDebtView.as_view(), name='update_unscheduled_debt'),
    url(r'^u_credit/create/$', CreateUnscheduledCreditView.as_view(), name='create_unscheduled_credit'),
    url(r'^u_credit/update/(?P<pk>\d+)/$', UpdateUnscheduledCreditView.as_view(), name='update_unscheduled_credit'),

    #Dashboard
    url(r'^$', index, name='dashboard'),
)