#!/usr/bin/env python3

import json
import locale
import sys
import emails
import os
import reports
from reportlab.platypus import Paragraph, Spacer, Table, Image
from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.units import inch
from email.message import EmailMessage

def load_data(filename):
  """Loads the contents of filename as a JSON file."""
  with open(filename) as json_file:
    data = json.load(json_file)
  return data

def format_car(car):
  """Given a car dictionary, returns a nicely formatted name."""
  return "{} {} ({})".format(
    car["car_make"], car["car_model"], car["car_year"])

def process_data(data):
  """Analyzes the data, looking for maximums.
  Returns a list of lines that summarize the information.
  """
  locale.setlocale(locale.LC_ALL, '')
  max_revenue = {"revenue": 0}
  max_sales = {"total_sales": 0}
  most_popular = {}
  winner_year_total_sales = {}
  most_popular_year_count = {}
  car_sales_for_pdf = {}

  for item in data:
  # Calculate the revenue generated by this model (price * total_sales)
  # We need to convert the price from "$1234.56" to 1234.56
    item_price = locale.atof(item["price"].strip("$"))
    item_revenue = item["total_sales"] * item_price
    if item_revenue > max_revenue["revenue"]:
      item["revenue"] = item_revenue
      max_revenue = item

  # TODO: also handle max sales                                           # max_sales
    if item["total_sales"] > max_sales["total_sales"]:
      max_sales = item

  # Dict for PDF
    if item["car"]["car_year"] not in car_sales_for_pdf:
      car_sales_for_pdf = data.copy()

  # TODO: also handle most popular car_year                               # car year - total_sales
    if item["car"]["car_year"] not in most_popular:                       # car_year - count
      most_popular[item['car']['car_year']] = item["total_sales"]         # Track Year and its total_sales
      most_popular_year_count[item['car']['car_year']] = 1                # Track Frequency of Year
    else:
      most_popular[item['car']['car_year']] += item["total_sales"]        # Aggregate total_sales if exists
      most_popular_year_count[item['car']['car_year']] += 1               # Increment counter Year if exists

  sorted_most_popular_year_count = sorted(most_popular_year_count.items(), key=lambda x:x[1], reverse=True)
  winner_year = sorted_most_popular_year_count[0][0]

  for k, v in most_popular.items():
    if k == winner_year:
      winner_year_total_sales = v

  summary = [
    "The {} generated the most revenue: ${}".format(
      format_car(max_revenue["car"]), max_revenue["revenue"]),
    "The {} had the most Sales: {}".format(
      format_car(max_sales["car"]), max_sales["total_sales"]),
    "The most popular year was {} with: {} sales.".format(winner_year, winner_year_total_sales),
  ]

  return summary

def cars_dict_to_table(car_data):
  """Turns the data in car_data into a list of lists."""
  table_data = [["ID", "Car", "Price", "Total Sales"]]
  for item in car_data:
    table_data.append([item["id"], format_car(item["car"]), item["price"], item["total_sales"]])
  return table_data

def main(argv):
  data = load_data("/Users/chriswong/car_sales.json")
  summary = process_data(data)
  text_summary = "\n".join(summary)
  print(text_summary)

  # Generate PDF using 'reports.generate()'
  table_data = cars_dict_to_table(data)
  reports.generate("/tmp/cars.pdf", "Sales summary for last month", "<br/>".join(summary), table_data)

  # TODO: send the PDF report as an email attachment
  # Send PDF using 'emails.generate' and emails.send()
  sender = "automation@example.com"
  receiver = "chriswong@localhost"       #"{}@example.com".format(os.environ.get('USER'))
  attachment = "/tmp/cars.pdf"
  subject = "Sales summary for last month"
  body = "\n".join(summary)
  message = emails.generate(sender, receiver, subject, body, attachment)
  emails.send(message)                                                   # emails.send()

if __name__ == "__main__":
  main(sys.argv)


from database.models import Team, UserProfile
from _main_.utils.massenergize_errors import MassEnergizeAPIError, InvalidResourceError, ServerError, CustomMassenergizeError
from _main_.utils.massenergize_response import MassenergizeResponse
from _main_.utils.context import Context
from sentry_sdk import capture_message
from typing import Tuple

class TeamStore:
  def __init__(self):
    self.name = "Team Store/DB"

  def get_team_info(self, team_id) -> Tuple[dict, MassEnergizeAPIError]:
    team = Team.objects.filter(id=team_id)
    if not team:
      return None, InvalidResourceError()
    return team, None

  def list_teams(self, community_id) -> Tuple[list, MassEnergizeAPIError]:
    teams = Team.objects.filter(communities__id=community_id)
    if not teams:
      return [], None
    return teams, None

  def create_team(self, args) -> Tuple[dict, MassEnergizeAPIError]:
    try:
      new_team = Team.create(**args)
      new_team.save()
      return new_team, None
    except Exception:
      return None, ServerError()

  def update_team(self, team_id, args) -> Tuple[dict, MassEnergizeAPIError]:
    team = Team.objects.filter(id=team_id)
    if not team:
      return None, InvalidResourceError()
    team.update(**args)
    return team, None

  def delete_team(self, team_id) -> Tuple[dict, MassEnergizeAPIError]:
    teams = Team.objects.filter(id=team_id)
    if not teams:
      return None, InvalidResourceError()

  def list_teams_for_community_admin(self, community_id) -> Tuple[list, MassEnergizeAPIError]:
    teams = Team.objects.filter(communities__id = community_id)
    return teams, None

  def list_teams_for_super_admin(self):
    try:
      teams = Team.objects.all()
      return teams, None
    except Exception as e:
      capture_message(str(e), level="error")
      return None, CustomMassenergizeError(e)


config = {
    'device': 'cuda',
    'root_dir': './datasets.nosync/coco',
    'train_img_dir': './datasets.nosync/coco/images/train2017',
    'train_annotation_dir': './datasets.nosync/coco/annotations/instances_train2017.json',
    'val_img_dir': './datasets.nosync/coco/images/val',
    'val_annotation_dir': './datasets.nosync/coco/annotations/instances_val2017.json',
    'train_batch_size': 4,
    'val_batch_size': 4,
    'num_workers': 1,
    'num_epochs': 50,
    'pretrained': True,
    'img_size' : 480,
    'batch_size': 4,
    'learning_rate': 1e-4,
    'class_labels': ['person', 'bicycle', 'car', 'motorcycle', 'airplane',
                     'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
                     'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
                     'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',
                     'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
                     'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
                     'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
                     'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
                     'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
                     'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
                     'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
                     'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
                     'hair drier', 'toothbrush'],
    'rl_agent': {
        'alpha': 0.99,
        'gamma': 0.95,
        'eps_start': 1.0,
        'eps_end': 0.01,
        'eps_decay': 0.995,
        'num_timesteps': 100
    },
    'loss_settings': {
        'classification_weight': 1.0,
        'bbox_regression_weight': 1.0
    }
}


"""
|-------------------------------------------------------------------------------
| manhattan_distance.py
|-------------------------------------------------------------------------------
|
| Author:   Alwin Tareen
| Created:  Sep 29, 2021
|
| Run:      python3 manhattan_distance.py
|
| Description:
| This program creates a distance field using the Manhattan distance equation.
|
"""

GRID_HEIGHT = 6
GRID_WIDTH = 8

def manhattan_distance(row0, col0, row1, col1):
    """
    Compute the Manhattan distance between the cells
    (row0, col0) and (row1, col1)
    """
    return abs(row0 - row1) + abs(col0 - col1)

def create_distance_field(entity_list):
        """
        Create a Manhattan distance field that contains the minimum distance to
        each entity (zombies or humans) in entity_list
        Each entity is represented as a grid position of the form (row, col)
        """
        field = [[0 for col in range(GRID_WIDTH)] for row in range(GRID_HEIGHT)]
        for row0 in range(GRID_HEIGHT):
            for col0 in range(GRID_WIDTH):
                lowest = GRID_HEIGHT * GRID_WIDTH
                for entity in entity_list:
                    distance = manhattan_distance(row0, col0, entity[0], entity[1])
                    if distance < lowest:
                        lowest = distance
                        field[row0][col0] = distance
        return field

def print_field(field):
    """
    Print a distance field in a human readable manner with
    one row per line
    """
    for row in field:
        print(row)

def run_example():
    """
    Create and print a small distance field
    """
    field = create_distance_field([[4, 0],[2, 5]])
    print_field(field)

run_example()

# Sample output for the default example
#[4, 5, 5, 4, 3, 2, 3, 4]
#[3, 4, 4, 3, 2, 1, 2, 3]
#[2, 3, 3, 2, 1, 0, 1, 2]
#[1, 2, 3, 3, 2, 1, 2, 3]
#[0, 1, 2, 3, 3, 2, 3, 4]
#[1, 2, 3, 4, 4, 3, 4, 5]


import torch
from torch.utils.data import DataLoader,dataset
from torch.utils.data import sampler,TensorDataset
import torchvision.datasets as dset
from torchvision import transforms
import torchvision
from pprint import pprint
import os
import cv2
import numpy as np
from PIL import Image
import random
import pandas as pd

class ChunkSampler(sampler.Sampler):
    """Samples elements sequentially from some offset.
    Arguments:
        num_samples: # of desired datapoints
        start: offset where we should start selecting from
    """
    def __init__(self, num_samples, start=0):
        self.num_samples = num_samples
        self.start = start

    def __iter__(self):
        return iter(range(self.start, self.start + self.num_samples))

    def __len__(self):
        return self.num_samples

class Date(dataset.Dataset):
    def __init__(self,label_dic,train=True,transform_train = None , transform_val = None , root = None):
        self.root = root
        super(Date,self).__init__()
        self.dic = label_dic
        self.train = train
        self.data = os.listdir(self.root)
        random.shuffle(self.data)
        self.len = len(self.data)
        self.transform_train = transform_train
        self.transform_val = transform_val

    def __getitem__(self, index):
        name = self.data[index]
        data = cv2.imread(self.root+"/"+name)
        transform = self.transform_train if self.train else self.transform_val
        data = transform(data)
        label = self.dic[name[:12]]
        return data,label

    def __len__(self):
        return self.len

transform_train = transforms.Compose([
    transforms.ToTensor(),
    # transforms.RandomResizedCrop(224,scale=(0.08,1.0),ratio=(3.0/4.0,4.0/3.0)),
    # transforms.RandomHorizontalFlip(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
)

transform_val = transforms.Compose([
    transforms.ToTensor(),
    # transforms.Resize((224,224)),
    # transforms.CenterCrop(224),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

def get_loader(batch_size=16,root=""):
    """
    :return: loader_train , loader_val
    """
    data = pd.read_excel("tem_10.xlsx")
    dic = dict(zip(data['cases'].values, data['tag'].values))
    data_train = Date(dic,True,transform_train,transform_val,root=root)
    data_val = Date(dic,False,transform_train,transform_val,root=root)
    total_num = len(os.listdir(root))
    train_num = int(total_num * 0.9)
    val_num = int(total_num * 0.1)
    loader_train = DataLoader(data_train,batch_size=batch_size,sampler=ChunkSampler(train_num,0),drop_last=True)
    loader_val = DataLoader(data_val,batch_size=batch_size,sampler=ChunkSampler(val_num,train_num),drop_last=True)
    return loader_train ,loader_val

def get_random_loader(batch_size=16):
    data = pd.read_excel("tem_10.xlsx")
    dic = dict(zip(data['cases'].values, data['tag'].values))
    Full_data = Date(dic,True,transform_train,transform_val)
    train_size = int(len(Full_data) * 0.8)
    test_size = len(Full_data) - train_size
    data_train , data_test = torch.utils.data.random_split(Full_data,[train_size,test_size])
    data_train.train = True
    data_test.train = False
    loader_train = DataLoader(data_train, batch_size=batch_size)
    loader_val = DataLoader(data_test, batch_size=batch_size)
    return loader_train, loader_val

def show_pic(img):
    cv2.imshow("test", img)
    cv2.waitKey()
    cv2.destroyAllWindows()  # important part!

def to_pic(pic):
    beard = transforms.Compose([torchvision.transforms.ToPILImage(mode="RGB")])
    way = Image.fromarray(pic)
    return way

if __name__ == '__main__':
    #dic = {'TCGA-EJ-5525': 1, 'TCGA-2A-A8VX': 1, 'TCGA-EJ-5517': 0, 'TCGA-H9-A6BX': 0, 'TCGA-VN-A88R': 1, 'TCGA-CH-5752': 1, 'TCGA-YL-A8SF': 1, 'TCGA-2A-A8VL': 0, 'TCGA-G9-A9S0': 1,'TCGA-EJ-AB20': 0}
    loader_train , loader_val = get_loader(batch_size=50)
    data = (loader_train.__iter__().next()[0][0])
    print(data)


import pygame
from random import randint

BLOCK_SIZE = 10

class Color:
    def __init__(self):
        self.black = [0,0,0]
        self.red = [255,0,0]
        self.green = [0,255,0]
        self.blue = [0,0,255]

class Food:
    def __init__(self):
        self.height = BLOCK_SIZE
        self.width = BLOCK_SIZE
        self.food = pygame.Rect(randint(1, 599), randint(1, 599), self.height, self.width)

class Player:
    def __init__(self):
        self.x, self.y = randint(20, 580), randint(20, 580)
        self.speed = 1
        self.blocks = 1
        self.height = BLOCK_SIZE
        self.width = BLOCK_SIZE
        self.snake_positions = [pygame.Rect(self.x, self.y, self.height, self.width),
                                pygame.Rect(self.x - 10, self.y, self.height, self.width),
                                pygame.Rect(self.x - 20, self.y, self.height, self.width),
                                pygame.Rect(self.x - 30, self.y, self.height, self.width)]
        self.snake_head = self.snake_positions[0]
        #self.snake = pygame.Rect(self.x, self.y, self.height, self.width)
        self.score = 0
        self.direction = R

    def __len__(self):
        return self.blocks

    def check_position(self):
        if self.snake_head.bottom > 600:
            self.snake_head.top = 0
        elif self.snake_head.top < 0:
            self.snake_head.bottom = 600
        elif self.snake_head.left < 0:
            self.snake_head.right = 600
        elif self.snake_head.right > 600:
            self.snake_head.left = 0

    def set_direction(self, new_direction):
        direction = (0,0)
        if new_direction == R:
            direction = (self.speed,0)

        elif new_direction == L:
            direction = (-self.speed, 0)

        elif new_direction == U:
            direction = (0,-self.speed)

        elif new_direction == D:
            direction = (0, self.speed)

        center = 0,0
        for index, rect in enumerate(self.snake_positions):
            rect.move_ip(direction)

        #self.snake.move_ip(direction)

    def increment_score(self):
        self.score += 1

    def get_score(self):
        return self.score

    def increase_speed(self):
        self.speed += .25

class Game:
    def __init__(self):
        pygame.display.set_caption('Dennis Snake Game')
        self.window_resolution = [600, 600]
        self.game_window = pygame.display.set_mode(self.window_resolution)
        self.start_time = pygame.time.Clock()
        self.colors = Color()
        self.snake = None
        self.food = None
        self.current_move = R
        self.init_new_game_window()

    def init_new_game_window(self):
        self.game_window.fill(self.colors.black)
        self.snake = Player()
        self.food = Food()
        pygame.display.update()

    def update_window(self):
        self.game_window.fill(self.colors.black)
        pygame.draw.rect(self.game_window, self.colors.red, self.food.food)
        # pygame.draw.rect(self.game_window, self.colors.green, self.snake.snake, 2)
        for rect in self.snake.snake_positions:
            pygame.draw.rect(self.game_window, self.colors.green, rect, 2)
        pygame.display.update()

    def check_for_eat_event(self):
        if self.snake.snake_head.colliderect(self.food.food):
            self.food = Food()
            self.snake.increment_score()
            print("Current Score: %d" % self.snake.score)
            if self.snake.get_score() % 10 == 0:
                self.snake.increase_speed()
    def start(self):
        while True:
            # clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    raise SystemExit

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_DOWN and self.current_move != U:
                        self.current_move = D
                    if event.key == pygame.K_UP and self.current_move != D:
                        self.current_move = U
                    if event.key == pygame.K_LEFT and self.current_move != R:
                        self.current_move = L
                    if event.key == pygame.K_RIGHT and self.current_move != L:
                        self.current_move = R
            print(self.snake.speed)
            self.check_for_eat_event()
            self.snake.check_position()
            self.snake.set_direction(self.current_move)
            self.update_window()

if __name__ == '__main__':

    pygame.init()
    game = Game()
    game.start()


player_one_score=0
player_two_score=0

for i in range(10):
    player_one=input("Enter the correct foot either leftfoot or rightfoot: ")
    player_two=input("Enter the correct foot either leftfoot or rightfoot: ")

    if(player_one=="rightfoot" and player_two=="leftfoot"):
     print("player one won")
    player_one_score +=1

    if(player_two=="rightfoot" and player_one == "leftfoot"):
        print("player two won")
        player_two_score +=1

    elif(player_one==player_two):
     print("it a draw")

print("The score of player_one is  ",player_one_score)
print("The score of player_two is  ",player_two_score)


# coding=utf-8
"""
    Created by Abadie Moran at 02/07/2019

"""
from source.parser.link import link_parser

def inside_parse(split_code, folder, pattern):
    """
        Manage whats at the right of the <script/<link/<img

    :param pattern: the pattern code for the css or the js
    :type pattern: CodePattern
    :param split_code: the split code at the right of each <script
    :type split_code: str
    :param folder: the folder of the html file
    :type folder: str
    :return: the updated code
    :rtype: str

    Examples:

    # When it is not well constructed
    code = ' hre="css/test.css"'
    pattern = CodePattern(CodePattern.CSS)
    inside_parse(code, "/home/path/, pattern)
    >> <link href="css/test.css"

    # When it is well constructed
    code = ' href="css/test.css">'
    pattern = CodePattern(CodePattern.CSS)
    inside_parse(code, "/home/path/, pattern)
    >> <style>\nh1 {\nwidth: 50px;\n}\n</style>

    """
    # 1. Split with the </script>
    split_script = split_code.split(pattern.original_end)
    # 2. If </script> is not found, return the original script code
    if len(split_script) < 2:
        return pattern.original + split_code
    # 3. Get the code inside <script and </script>
    inside = split_script[0]
    # 4. For each src possibilities
    for possibility in pattern.possibilities:
        (left, _) = possibility
        # 4.1 if the src is in the code, its the right one
        if left in inside:
            # 4.2 Try to replace the code
            right_of_end_script = ""
            first = True
            for extra_code in split_script[1:]:
                plus_str = ""
                if not first:
                    plus_str = pattern.original_end
                first = False
                right_of_end_script += plus_str + extra_code
            new_code = link_parser(split_code, inside, possibility, folder,
                                   pattern, right_of_end_script)
            return new_code
    # 5. if nothing is found, return the original script
    return pattern.original + split_code


"""
Use this plugin to activate coverage report.

To install this plugin, you need to activate ``coverage-plugin``
with extra requirements :

::

    $ pip install nose2[coverage-plugin]

Next, you can enable coverage reporting with :

::

    $ nose2 --with-coverage

Or with this lines in ``unittest.cfg`` :

::

    [coverage]
    always-on = True

"""
from nose2.events import Plugin

# pylint: disable=no-member
# config is aquired from outside file
# pylint: disable=bare-except
# while no exception is raised a message is output

class Coverage(Plugin):
    """ coverage plugin class """
    configSection = 'coverage'
    commandLineSwitch = ('C', 'with-coverage', 'Turn on coverage reporting')

    def __init__(self):
        """Get our config and add our command line arguments."""
        self.con_source = self.config.as_list('coverage', [])
        self.con_report = self.config.as_list('coverage-report', [])
        self.con_config = self.config.as_str('coverage-config', '').strip()
        self.cov_source = self.config.as_str('coverage-source', '').strip()
        self.cov_report = None
        self.cov_config = None
        self.cov_controller = None

        group = self.session.pluginargs
        group.add_argument(
            '--coverage', action='append', default=[], metavar='PATH',
            dest='coverage_source',
            help='Measure coverage for filesystem path (multi-allowed)'
        )
        group.add_argument(
            '--coverage-report', action='append', default=[], metavar='TYPE',
            choices=['term', 'term-missing', 'annotate', 'html', 'xml'],
            dest='coverage_report',
            help='Generate selected reports, available types:'
                 ' term, term-missing, annotate, html, xml (multi-allowed)'
        )
        group.add_argument(
            '--coverage-config', action='store', default='', metavar='FILE',
            dest='coverage_config',
            help='Config file for coverage, default: .coveragerc'
        )

    def handle_args(self, event):
        """Get our options in order command line, config file, hard coded."""

        self.cov_source = (event.args.coverage_source or
                           self.con_source or ['.'])
        self.cov_report = (event.args.coverage_report or
                           self.con_report or ['term'])
        self.cov_config = (event.args.coverage_config or
                           self.con_config or '.coveragerc')

        try:
            import cov_core
        except:
            print('Warning: you need to install [coverage-plugin] '
                  'extra requirements to use this plugin')
            return

        self.cov_controller = cov_core.Central(self.cov_source,
                                               self.cov_report,
                                               self.cov_config)
        self.cov_controller.start()

    def after_summary_report(self, event):
        """Only called if active so stop coverage and produce reports."""

        if self.cov_controller:
            self.cov_controller.finish()
            self.cov_controller.summary(event.stream)


import pandas as pd

df = pd.read_csv("Book\Sukkiri_ML\ProjectA\datafiles\cinema.csv")
df2 = df.fillna(df.mean())

no = df2[(df2['SNS2']>1000) & (df2["sales"]<8500)].index
df3 = df2.drop(no,axis=0)

x = df3[['SNS1','SNS2','actor','original']]
t = df3["sales"]

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,t,test_size=0.2,random_state=0)

from sklearn.linear_model import LinearRegression
model = LinearRegression()

print(model.fit(x_train,y_train))
print(model.score(x_test,y_test))

new = [[150,700,300,0]]
print(model.predict(new))

from sklearn.metrics import median_absolute_error
pred = model.predict(x_test)

print(median_absolute_error(y_pred=pred,y_true=y_test))

import pickle

with open("Book\Sukkiri_ML\ProjectA\cinema.pkl","wb") as f:
    pickle.dump(model,f)


def sprawdz_ciag(t, x : int, y: int, N):
    maks_dlugosc = 2
    obecna_dlugosc = 2
    obecne_q = t[x+1][y+1] / t[x][y]
    x += 2
    y += 2

    while x < N and y < N:
        if t[x][y] / t[x-1][y-1] == obecne_q:
            obecna_dlugosc += 1
        else:
            if obecna_dlugosc > maks_dlugosc:
                maks_dlugosc = obecna_dlugosc

            obecna_dlugosc = 2
            obecne_q = t[x][y] / t[x-1][y-1]

        x += 1
        y += 1

    if obecna_dlugosc > maks_dlugosc:
        maks_dlugosc = obecna_dlugosc

    return maks_dlugosc

def ciag_geometryczny(T1, N : int):
    if N < 3:
        print("Nie znaleziono")
        return 0

    x = N - 3
    y = 0

    maks = 2

    while x >= 0 and y <= N-3:
        dlu = sprawdz_ciag(T1, x, y, N)
        if dlu > maks:
            maks = dlu

        if x > 0:
            x -= 1
        else:
            y += 1

    return maks

n = int(input("Rozmiar: "))
t1 = [[int(input()) for _ in range(n)] for _ in range(n)]
print(ciag_geometryczny(t1, n))


# learning class

class Test:

    name = "lol"

    def __init__(self,name): # concreate method
        self.name = name

    def YouNameIs(self,name): # use self to set the variable in this class
        self.name = name

    def Kick(self):
        print("Kick " + self.name)

test = Test("xD")
test.Kick()
test.YouNameIs("Right")
test.Kick()

# private and public variable
class Animal:
    name = "Cow"
    __age = 13 # use "__" to make variable a private one

    def PrintAge(self):
        print(self.__age)

a = Animal()
print(a.name)
a.PrintAge()

print(a._Animal__age) # using format _ClassName__VariableName

Lesson 38 private and public variable.py


"""
Estimates the number of days in the future which could have drifting snow
and blizzards, from the UKCP18 Local projections.
"""

import os
import getpass
import numpy as np
import iris
from iris.time import PartialDateTime
from iris.analysis.cartography import rotate_pole
import matplotlib.pyplot as plt
from whichbox import whichbox

def get_ws_in_ms(ws, units='kn'):
    """
    Converts wind speed in knots or mph to metres per second.
    1 knot (kn, kt or kts) = 1.15077945 miles per hour (mph) = 1.852 kilometer per hour (kph)
      = 1.68780986 foot / second (ft/s) = 0.514444444 meters per second (m/s).
    1 nautical mile is now defined as exactly 1.852 km.
    """

    if units in ['kn', 'kt', 'kts', 'knot', 'knots']:
    # Convert wind speed in knots to metres per second
        wspeed = ws * 0.514444444
    elif units == 'mph':
    # Convert wind speed in miles per hour to metres per second
        wspeed = ws * (1760 * 3 * 12 * 2.54) / (3600 * 100)

    return wspeed

def make_ukcp_local_datadir(ensemble_member, var_name):
    """
    Constructs the name of the directory containing the UKCP Local data

    ensemble_member -- The UKCP ensemble member, an integer between 1 and 15.
    var_name -- The CF-name of the UKCP variable
    """

    if var_name == 'wsgmax10m':
        datadir = '/scratch/hadmi/UKCP18/ukcp18_data/'
    else:
        datadir = f"/project/ukcp/land-cpm/uk/2.2km/rcp85/{ensemble_member:02d}/{var_name}/day/v20210615/"

    return datadir

def make_ukcp_filename(ensemble_member, var_name, the_year):
    """
    Constructs the UKCP Local filename for the given ensemble member and variable,
    including the directory. The daily data are stored in 1 year chunks.

    ensemble_member -- The UKCP ensemble member, an integer between 1 and 15.
    var_name -- The CF-name of the UKCP variable
    the_year -- The year required.
    """

    datadir = make_ukcp_local_datadir(ensemble_member, var_name)
    fhead = f"{var_name}_rcp85_land-cpm_uk_2.2km_{ensemble_member:02d}_day"
    fend = "{:4d}1201-{:4d}1130.nc".format(the_year, the_year+1)

    return os.path.join(datadir, '_'.join([fhead, fend]))

def ukcp_callback(cube, field, filename):

# Remove 2D coordinates and other coords not needed.
    cube.attributes = {}
    cube.remove_coord('latitude')
    cube.remove_coord('longitude')
    cube.remove_coord('ensemble_member_id')
    cube.remove_coord('yyyymmdd')

def read_ukcp18_local(ensemble_member, var_name, the_year, lon_limits, lat_limits):
    """
    Reads in UKCP18 Local data for the extended winter season (October to May)

    ensemble_member -- The UKCP ensemble member, an integer between 1 and 15.
    var_name -- The CF-name of the UKCP variable
    the_year -- The year required. Data for October [year-1] to May [year] are loaded.
    lon_limits -- Only load data for grid boxes within this limit
    lat_limits -- Only load data for grid boxes within this limit
    """

    filename_y1 = make_ukcp_filename(ensemble_member, var_name, the_year-1)
    filename_y2 = make_ukcp_filename(ensemble_member, var_name, the_year)
    print(the_year)
    print(lon_limits)
    print(lat_limits)

# Constraints for the given lon/lat limits
    lon_con = iris.Constraint(grid_longitude = lambda l: lon_limits[0] < l.point < lon_limits[1])
    lat_con = iris.Constraint(grid_latitude = lambda l: lat_limits[0] < l.point < lat_limits[1])
    e_con = iris.Constraint(ensemble_member = ensemble_member)
    cube_1 = iris.load_cube(filename_y1, constraint=e_con & lat_con & lon_con, callback=ukcp_callback)
    cube_2 = iris.load_cube(filename_y2, constraint=e_con & lat_con & lon_con, callback=ukcp_callback)
    cube = iris.cube.CubeList([cube_1, cube_2]).concatenate_cube()

# Now constrain on time
    pdt1 = PartialDateTime(year=the_year, month=10, day=1)
    pdt2 = PartialDateTime(year=the_year+1, month=5, day=30)

    return cube.extract(iris.Constraint(time = lambda t: pdt1 <= t.point <= pdt2))

def make_empty_cube(nlats, nlons, var_name, the_year, lat_coord, lon_coord):
    """
    Makes an empty cube (i.e. a cube whose data are all zeros)
    """

    tcoord = iris.coords.AuxCoord(the_year, long_name='year', var_name='year', units='1')
    data = np.zeros((nlats, nlons), dtype=np.int)

    cube = iris.cube.Cube(data, long_name = f'days with {var_name} above/below threshold',
        var_name=var_name, dim_coords_and_dims = [(lat_coord, 0), (lon_coord, 1)])
    cube.add_aux_coord(tcoord)

    return cube

def find_snow_drift_days(the_year, tasmin, gust, snw):
    """
    Identifies days on which drifting snow or blizzards could occur, using thresholds
    from the forecasters' handbook (3rd Edn).
    https://digital.nmla.metoffice.gov.uk/IO_74afe7b9-24f7-4de9-96dd-20f4d5ac4094/
    """

# Thresholds for drifting snow, from the 3rd Edn of the Forecaster's Handbook.
# SWE of 0.02 mm used in the revised UKCP Local report, by Kendon et al. (2021), but is too low.
# 10 mm from critical value of Roesch et al. 2001, Clim Dyn.
    tlo = 0.0        #  Temperature must be below this threshold for drifting snow
    gmin_kn = 12.0   #  Wind speed in knots above which drifting snow occurs.
    swe_min = 10.0   #  Snow water equivalent (in mm) must be greater than this limit for there to be sufficient lying snow

# Constants for estimating snow cover fraction from snow water equivalent. SWE must be in metres.
# See Roesch et al. 2001, Clim Dyn. and Yang et al. (1997), J Climate, pp.353-373
    a = 100.0
    b = 0.95

# Convert speed in knots to m/s.
    gmin = get_ws_in_ms(gmin_kn, units='kn')

    ntimes, nlats, nlons = tasmin.shape

# Get coordinate for output cubes
    lon_coord = tasmin.coord('grid_longitude')
    lat_coord = tasmin.coord('grid_latitude')

# Set up output cubes for number of days with tasmin < tlo, wind gusts greater than gmin, number of days with lying
# snow, number of days with a snow fraction > 0.5, and number of days with drifting snow.
    d_below = make_empty_cube(nlats, nlons, tasmin.var_name, the_year, lat_coord, lon_coord)
    d_windy = make_empty_cube(nlats, nlons, gust.var_name, the_year, lat_coord, lon_coord)
    d_lying_snow = make_empty_cube(nlats, nlons, snw.var_name, the_year, lat_coord, lon_coord)
    d_snow_frac = make_empty_cube(nlats, nlons, 'snow_frac', the_year, lat_coord, lon_coord)
    d_drift = make_empty_cube(nlats, nlons, 'drift', the_year, lat_coord, lon_coord)

    for j in list(range(nlats)):
        for i in list(range(nlons)):
            # Find all days on which each criteria for drifting snow is met, and days when all three are met simultaneously
            wt = (tasmin.data[:,j,i] < tlo)
            wg = (gust.data[:,j,i] > gmin)
            ws = (snw.data[:,j,i] > swe_min)
            wd = (wt & wg & ws)
            snow_fracs = b * np.tanh(a * snw.data[:,j,i]/1000)
            wf = (snow_fracs >= 0.5)  #  Obs of snow cover require at least 50% coverage.

            d_below.data[j,i] = sum(wt)
            d_windy.data[j,i] = sum(wg)
            d_lying_snow.data[j,i] = sum(ws)
            d_snow_frac.data[j,i] = sum(wf)
            d_drift.data[j,i] = sum(wd)

    return iris.cube.CubeList([d_below, d_windy, d_lying_snow, d_snow_frac, d_drift])

def count_snow_drift_days(ensemble_member, year_range, lon_limits, lat_limits):

    dout = '/scratch/hadmi/UKCP18/processed/'

    # UKCP18 variable names
    var_name_t = 'tasmin'
    var_name_g = 'wsgmax10m'
    var_name_s = 'snw'

# Analyse data for October to May, so start at year+1
    for the_year in list(range(year_range[0]+1, year_range[1])):
        print('Loading data for year', the_year)

        cube_t = read_ukcp18_local(ensemble_member, var_name_t, the_year, lon_limits, lat_limits)
        cube_s = read_ukcp18_local(ensemble_member, var_name_s, the_year, lon_limits, lat_limits)
        print(np.min(cube_s.data), np.max(cube_s.data))

        cube_g10m = read_ukcp18_local(ensemble_member, var_name_g, the_year, lon_limits, lat_limits)
        # Estimate 2m wind gusts from 10m gusts
        cube_g = calculate_2m_gusts(cube_g10m)

        clist = find_snow_drift_days(the_year, cube_t, cube_g, cube_s)

        fout = os.path.join(dout, f'drift_rcp85_land-cpm_uk_2.2km_{ensemble_member:02d}_{the_year:4d}.nc')
        iris.save(clist, fout)

def calculate_2m_gusts(cube):
    """
    Calculates gust speeds at 2m above the surface, assuming log wind profile is valid.

    cube -- Iris cube containing 10m wind gusts
    """

    z0 = 0.03  # Roughness length (m) for open moorland, from Hammond et al. (2011), https://doi.org/10.1002/met.273
    heather_height = 0.30   # Heather on moorland assumed to be about 30 cm high
    z2 = 2     # Height of weather station
    z1 = 10    # Height of modelled wind gust speed

    # Zero plane displacement, m.
    d = heather_height * 0.67

# Calculate wind speed at 2m from speed at 10 m using:
# u2 = u10 * ln((z2 - d) / z0) / ln((z1 - d) / z0)
# where z2 = 2 m, z1 = 10 m

    fac = np.log((z2 - d) / z0) / np.log((z1 - d) / z0)

    cube_out = cube.copy()
    cube_out.data *= fac
    cube_out.var_name = 'wsgmax2m'

    return cube_out

def get_area_limits(ensemble_member, the_lat, the_lon, the_year, radius):

# Read in sample data on the rotated grid
    var_name = 'tasmin'
    filename = make_ukcp_filename(ensemble_member, var_name, the_year)
    e_con = iris.Constraint(ensemble_member = ensemble_member)
    cube = iris.load_cube(filename, constraint=e_con, callback=ukcp_callback)

# Get the data size, then the grid spacing
    ntimes, nlats, nlons = cube.shape
    nx = nlons // 2
    ny = nlats // 2
    grid_latitudes = cube.coord('grid_latitude').points
    grid_longitudes = cube.coord('grid_longitude').points
    dx, = np.diff(grid_longitudes[nx:nx+2])
    dy, = np.diff(grid_latitudes[ny:ny+2])

# Convert the coordinates of the snow shed to rotated coordinates
    pole_lon = cube.coord('grid_longitude').coord_system.grid_north_pole_longitude
    pole_lat = cube.coord('grid_latitude').coord_system.grid_north_pole_latitude
    lons = np.array([the_lon])
    lats = np.array([the_lat])
    rotated_lons, rotated_lats = rotate_pole(lons, lats, pole_lon, pole_lat)
    rotated_lons += 360

# Find the indices of grid box containing the point of interest
    i_pts = whichbox(cube, [rotated_lons[0], rotated_lats[0]],
        xcoord_name='grid_longitude', ycoord_name='grid_latitude')

# Coordinates of centre of grid box containing the point of interest
    clon = grid_longitudes[i_pts[0]]
    clat = grid_latitudes[i_pts[1]]

# Get edges of an area of n x n boxes around this point, where n = 2*radius + 1
    lon_l = clon - dx * (radius + 0.1)
    lon_r = clon + dx * (radius + 0.1)
    lat_b = clat - dy * (radius + 0.1)
    lat_t = clat + dy * (radius + 0.1)

    return (lon_l, lon_r), (lat_b, lat_t)

if __name__ == '__main__':

# Coordinates of the Cruach Snow Shed (from the customer).
    clat = 56.70308
    clon = -4.58581

# Radius in grid boxes of an area around the snow shed to study
    radius = 2

# Get the limits of a box around the shed. radius = 2 means a 5 x 5 grid of boxes
    ensemble_member = 1
    year = 1981
    lon_limits, lat_limits = get_area_limits(ensemble_member, clat, clon, year, radius)

#   year_ranges = [[1980, 2000], [2020, 2040], [2060, 2080]]  #  Models run from December to November
    year_ranges = [[2020, 2040], [2060, 2080]]  #  Models run from December to November

    for year_range in year_ranges:
        count_snow_drift_days(ensemble_member, year_range, lon_limits, lat_limits)


import os, sys
import pickle

from typing import Dict

from datetime import datetime , date

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import statsmodels.formula.api as smf
from sklearn.preprocessing import MinMaxScaler

import torch
from torch.utils.data import TensorDataset, RandomSampler, SequentialSampler, DataLoader

class Preprocessor ():

    def __init__(self,
                batch_size,
                dataset_dir: str = "datasets/grocery_data.csv",
                preprocessed_dir: str = "datasets/grocery_preprocessed.pkl",
                city_dir: str = "datasets/city.pkl",
                product_dir: str = "datasets/product.pkl") -> None:

        if os.path.exists(preprocessed_dir):
            print("Load Data")
            self.dataset = pd.read_pickle(preprocessed_dir)
        else:
            print("Creating Preprocessed Data")
            self.dataset = self.load_data(dir = dataset_dir, save_dir = preprocessed_dir)

        if os.path.exists(city_dir):
            print("Load Data")
            with open(city_dir, "rb") as rc_handler:
                self.city_id: Dict = pickle.load(rc_handler)
        else:
            print("Creating Preprocessed Data")
            self.city_id = self.extract_index(self.dataset, index_name = "City", index_key="Sales")

        if os.path.exists(product_dir):
            print("Load Data")
            with open(product_dir, "rb") as rc_handler:
                self.product_id: Dict = pickle.load(rc_handler)
        else:
            print("Creating Preprocessed Data")
            self.product_id = self.extract_index(self.dataset, index_name = "Product", index_key="Quantity Ordered")

    def extract_index(self, dataset: pd.DataFrame, index_name: str, index_key: str) -> Dict:
        sum_for_data = dataset.groupby([index_name]).sum()
        sum_for_data = sum_for_data[["Quantity Ordered", "Sales"]].reset_index()

        sorted_data = sum_for_data.sort_values(by=index_key, ascending=False)

        data_dict = {c : i for i, c in enumerate(sorted_data.reset_index()[index_name].tolist())}

        with open("datasets/" + index_name.lower() + ".pkl", "wb") as w_handler:
            pickle.dump(data_dict, w_handler, protocol=pickle.HIGHEST_PROTOCOL)

        return data_dict

    def get_city(self, address):
        return address.split(",")[1].strip(" ")

    def get_state(self, address):
        return address.split(",")[2].split(" ")[1]

    def load_data(self, dir: str, save_dir: str):
        # Load CSV File
        d_grocery = pd.read_csv(dir)
        # Grab only NAN row
        d_nan = d_grocery[d_grocery.isna().any(axis=1)]

        # Remove NAN from datasets
        d_grocery = d_grocery.dropna(how="all")

        # In column Order Date remove Cell that have string instead of datetime
        d_grocery = d_grocery[d_grocery['Order Date'].str[0:2] != 'Or']

        # Convert and Corecting All Item in Column Quantity Ordered and Price Each to Integer
        d_grocery["Quantity Ordered"] = pd.to_numeric(d_grocery["Quantity Ordered"])
        d_grocery["Price Each"] = pd.to_numeric(d_grocery["Price Each"])

        # Get Date Item (Month Only) In cell : 2 Ways

        # 1 Way, Slice String in Cell Order Date in order to get only month
        d_grocery["Month"] = d_grocery["Order Date"].str[0:2]
        # Change Data Type from String to Integer
        d_grocery["Month"] = d_grocery["Month"].astype('int32')

        # 2 Way, With DateTime Function
        d_grocery["Month"] = pd.to_datetime(d_grocery["Order Date"]).dt.month

        # Apply Function to Column in Dataframe and Add to New Column
        d_grocery["City"] = d_grocery["Purchase Address"].apply(lambda x: f"{self.get_city(x)} {self.get_state(x)}")

        # Calculate Sales which quantity * price each
        d_grocery["Sales"] = d_grocery["Quantity Ordered"].astype("int") * d_grocery["Price Each"].astype("float")

        d_grocery.to_pickle(save_dir)

        return d_grocery

    def preprocessor(self):
        data = self.dataset[["Order Date", "Product", "City", "Quantity Ordered", "Sales"]]

        data["Date"] = pd.to_datetime(data["Order Date"].apply(lambda x: x.split(" ")[0]))
        data = data[["Date", "Product", "City", "Quantity Ordered", "Sales"]]

        data_sales = data.groupby("Date")["Sales"].sum().reset_index()
        data_sales = data_sales.rename(columns = {"Date": "date", "Sales": "sales"})

        data_diff = data_sales.copy()
        data_diff["prev_sales"] = data_diff["sales"].shift(1)
        data_diff = data_diff.dropna()
        data_diff["diff"] = (data_diff["sales"] - data_diff["prev_sales"])

        data_supervised = data_diff.drop(["prev_sales"], axis = 1)

        for inc in range(1, 13):
            field_name = "lag_" + str(inc)
            data_supervised[field_name] = data_supervised["diff"].shift(inc)

        data_supervised = data_supervised.dropna().reset_index(drop = True)

        model = smf.ols(formula = 'diff ~ lag_1 + lag_2 + lag_3 + lag_4 + lag_5 + lag_6 + lag_7 + lag_8 + lag_9 + lag_10 + lag_11 + lag_12', data = data_supervised)
        model_fit = model.fit()

        regression_adj_rsq = model_fit.rsquared_adj

        data_model = data_supervised.drop(["sales", "date"], axis = 1)

        train_set = data_model[0: -6].values
        test_set = data_model[-6:].values

        scaler = MinMaxScaler(feature_range = (-1, 1))
        scaler = scaler.fit(train_set)

        train_set = train_set.reshape(train_set.shape[0], train_set.shape[1])
        train_set_scaled = scaler.transform(train_set)

        test_set = test_set.reshape(test_set.shape[0], test_set.shape[1])
        test_set_scaled = scaler.transform(test_set)

        x_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0:1]
        x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1])

        x_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0:1]
        x_test = x_test.reshape(x_test.shape[0], 1, x_test.shape[1])

        x_train, y_train = torch.from_numpy(x_train).float(), torch.from_numpy(y_train).float()
        x_test, y_test = torch.from_numpy(x_test).float(), torch.from_numpy(y_test).float()

        return x_train, y_train, x_test, y_test, scaler, train_set_scaled, test_set_scaled, data_sales


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

## library Imports
import pandas as pd
import numpy as np
import json, os, time
try:
        import urllib.request as url   # python 3
except:
        import urllib2 as url  # python 2

import sys, getopt
import shapely

#### SET DEFAULT VARIABLES ####
# these can be overidden by using the tag commands brought up if you use -h
# when running the script
infile = r'merged_df.csv'
infile_2 = r'not_found!'
lat_name = 'Lat'
lon_name = 'Long'
limit = 0
call_type = 'OSRM'
UID = 'Unique_ID'
Pop = 'vNTL_PopD_GPW_2015'
ffpath = r'C:\Users\charl\Documents\Market Access\curl'
rescue = 0
rescue_num = 0
MB_Toke = r'[insert your mapbox token here]'

helpstr = """\nCommands recognised for this script:\n
        \n\t-p   Path - string for input and output folder path
        \n\t-f   File name of .csv containing input data
        \n\t-m   Latitude column name.
        \n\t-n   Longitude column name
        \n\t-o   Origin Unique Identifier column name (e.g. District, Name, Object ID...).
             This is mainly helpful for joining the output back to the input data / a shapefile,
             and is non-essential in terms of the calculation. It can be text or a number.
        \n\t-q   Population / weighting column name
        \n\t-c   Server call type - "OSRM" for OSRM, "MB" for Mapbox, "MBT" for Mapbox with traffic, or "Euclid" for Euclidean distances (as the crow flies)

        \n\t*** Optional - if sources =/= destinations. Note - Unique identifier and Pop column names must remain the same ***
        \n\t-l   Limit - use this to limit the coordinate input list (int). Optional.
        \n\t-D   Filename of destinations csv
        \n\t-R   Save - input latest save number to pick up from there
        \n\t-Z   Rescue number parameter - if you have already re-started the process, denote how many times. First run = 0, restarted once = 1...
        \n\nDo NOT put column names or indeed any input inside quotation marks.
        \nThe only exceptions is if the file paths have spaces in them.
        \n"""

# Import variables
try:
        opts, args = getopt.getopt(sys.argv[1:],"hp:f:m:n:o:q:c:l:D:R:Z:",['ffpath','infile','latitude','longitude','UID','Pop','call_type','limit','infile_dest', 'rescue','resnum'])
except getopt.GetoptError:
        print('**Check inputs by typing -h. This program will now exit**')
        sys.exit(2)
limit, rescue, rescue_num = 0, 0, 0 # assign dummy vars; limit + resuce does not apply unless specified in call
for opt, arg in opts:
        if opt == '-h':
                print(helpstr)
                sys.exit()
        elif opt in ("-p", "--ffpath"):
                ffpath = arg
        elif opt in ("-f", "--infile"):
                infile = arg
        elif opt in ("-m", "--latitude"):
                lat_name = arg
        elif opt in ("-n", "--longitude"):
                lon_name = arg
        elif opt in ("-o", "--UID"):
                UID = arg
        elif opt in ("-q", "--Pop"):
                Pop = arg
        elif opt in ("-c", "--call_type"):
                call_type = arg
        elif opt in ("-l", "--limit"):
                limit = arg
                limit = int(limit)
        elif opt in ("-D", "--infile_dest"):
                infile_2 = arg
        elif opt in ("-R", "--rescue"):
                rescue = arg
                rescue = int(rescue)
        elif opt in ("-Z", "--resnum"):
                rescue_num = arg
                rescue_num = int(rescue_num)
#print(opts)
start = time.time()
print('\nChosen server: %s\n\nStart time: %s' % (call_type, time.ctime(start)))
print('Origins: %s' % infile)
print('Destinations: %s\n' % infile_2)

# Save settings
save_rate = 5
def save(returns, j, i, numcalls, rescue_num):
        elapsed_mins = (time.time() - start)/60
        elapsed_secs = (time.time() - start)%60
        total = ((numcalls / float(i)) * (time.time() - start)/60.0)
        remaining = total - elapsed_mins
        print ('\n______________________________________\n')
        print ('\nSave point %s. Running for: %d minutes %d seconds' % (j, elapsed_mins, elapsed_secs))
        print ('\ncalls completed: %d of %d. Est. run time: %d minutes. Time remaining: %d' % (i-1, numcalls, total, remaining))
        print ('\npercentage complete: %d percent' % (((i-1) / float(numcalls)*100)))
        print ('\n______________________________________\n')
        try:
                df = pd.concat(returns)
        except:
                df = returns
        df.to_csv(os.path.join(ffpath,'temp_file_%d.csv' % rescue_num))

def save_current(O_list, D_list):
        O_df = pd.DataFrame({'Origins': O_list})
        S_df = pd.DataFrame({'Destinations': D_list})
        curr = pd.concat([O_df,S_df], ignore_index=True, axis=1)
        curr.columns = ['Origin', 'Desintation']
        curr.to_csv(os.path.join(ffpath, 'Current_O_D_combo.csv'))

# File Import for sources file
input_df = pd.read_csv(os.path.join(ffpath, infile))
input_df['source_list'] = input_df[lon_name].map(str).str.cat(input_df[lat_name].map(str), sep = ',')
input_df['source_list'] = input_df['source_list']+';'
source_list = input_df['source_list'].values.tolist()
source_UIDs = input_df[UID].values.tolist()
#input_df['source_point'] = input_df.apply(lambda x: Point(x[lon_name],x[lat_name]), axis = 1)
#source_points = input_df['source_point'].tolist()

# Look to import separate file for destinations; if not, set destinations = sources
try:
        input_df2 = pd.read_csv(os.path.join(ffpath, infile_2))
        input_df2['dest_list'] =  input_df2[lon_name].map(str).str.cat(input_df2[lat_name].map(str), sep = ',')
        input_df2['dest_list'] = input_df2['dest_list']+';'
        dest_list = input_df2['dest_list'].values.tolist()
        dest_UIDs = input_df2[UID].values.tolist()
        #input_df2['dest_points'] = input_df2.apply(lambda x: Point(x[lon_name],x[lat_name]), axis = 1)
        #dest_points = input_df2['dest_points'].tolist()
except:
        dest_list = source_list
        dest_UIDs = source_UIDs
        #dest_points = source_points
        pass

# apply limit if in test mode
if limit > 0:
        source_list = source_list[:limit]
        dest_list = dest_list[:limit]
        source_UIDs = source_UIDs[:limit]
        dest_UIDs = dest_UIDs[:limit]
        #dest_points = dest_points[:limit]
        #source_points = source_points[:limit]

# Function for calling Mapbox server.
def MapboxCall(O_list, D_list, i, O_IDs, D_IDs):

        # prevent server annoyance
        print('Call to Mapbox server number: %d' % i)

        # Mapbox - construct request
        header = 'https://api.mapbox.com/directions-matrix/v1/mapbox/driving/'

        # Convert origins to HTTP request string
        Os = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in O_list)

        # Destinations to HTTP request string
        Ds = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in D_list)

        # Join them together
        data = Os+';'+Ds

        # Add mapbox token key here
        token = MB_Toke

        # Define which coords in data string are origins, and which are destinations
        sources = ['%d' % x for x in range(0,len(O_list))]
        sources = ';'.join(str(x).replace("'", "") for x in sources)
        lenth = len(O_list)+len(D_list)
        destinations = ['%d' % x for x in range(len(O_list),lenth)]
        destinations = ';'.join(str(x).replace("'", "") for x in destinations)

        # Build request string
        request = header+data+'?sources='+sources+'&destinations='+destinations+'&access_token='+token

        # Pass request to interweb
        r = url.urlopen(request)

        # Error handle
        try:
                # Convert Bytes response to readable Json
                MB_TelTest_json = json.loads(r.read().decode('utf-8'))
                data_block = MB_TelTest_json['durations']
        except:
                data_block = 'null'
        # Build df from JSON
        #sources_label = [str(i['location']) for i in MB_TelTest_json['sources']]
        #dest_label = [str(i['location']) for i in MB_TelTest_json['destinations']]
        sources_label = O_IDs
        dest_label = D_IDs
        chunk = pd.DataFrame(data = data_block,
                                  columns = dest_label,
                                  index = sources_label)
        # Convert to minutes, stack 2D array to 1D array
        chunk = chunk.stack(level =-1)
        chunk.columns = ['O','D','DIST']
        return chunk

# Function for calling Mapbox Traffic server.
def MapboxCallTraffic(O_list, D_list, i, O_IDs, D_IDs):

        # prevent server annoyance
        print('Call to Mapbox Traffic server number: %d' % i)

        # Mapbox - construct request
        header = 'https://api.mapbox.com/directions-matrix/v1/mapbox/driving-traffic/'

        # Convert origins to HTTP request string
        Os = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in O_list)

        # Destinations to HTTP request string
        Ds = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in D_list)

        # Join them together
        data = Os+';'+Ds

        # Add mapbox token key here
        token = MB_Toke

        # Define which coords in data string are origins, and which are destinations
        sources = ['%d' % x for x in range(0,len(O_list))]
        sources = ';'.join(str(x).replace("'", "") for x in sources)
        lenth = len(O_list)+len(D_list)
        destinations = ['%d' % x for x in range(len(O_list),lenth)]
        destinations = ';'.join(str(x).replace("'", "") for x in destinations)

        # Build request string
        request = header+data+'?sources='+sources+'&destinations='+destinations+'&access_token='+token

        # Pass request to interweb
        r = url.urlopen(request)

        # Error handle
        try:
                # Convert Bytes response to readable Json
                MB_TelTest_json = json.loads(r.read().decode('utf-8'))
                data_block = MB_TelTest_json['durations']
        except:
                data_block = 'null'
        # Build df from JSON
        #sources_label = [str(i['location']) for i in MB_TelTest_json['sources']]
        #dest_label = [str(i['location']) for i in MB_TelTest_json['destinations']]
        sources_label = O_IDs
        dest_label = D_IDs
        chunk = pd.DataFrame(data = data_block,
                                  columns = dest_label,
                                  index = sources_label)
        # Convert to minutes, stack 2D array to 1D array
        chunk = chunk.stack(level =-1)
        chunk.columns = ['O','D','DIST']
        return chunk

# Function for calling OSRM server.
def OSRMCall(O_list, D_list, i, O_IDs, D_IDs):

        # prevent server annoyance
        print('Call to OSRM server number: %d' % i)

        # Mapbox - construct request
        header = 'http://router.project-osrm.org/table/v1/driving/'

        # Convert origins to HTTP request string
        Os = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in O_list)

        # Destinations to HTTP request string
        Ds = ';'.join(str(coord).replace("'", "").replace(";", "") for coord in D_list)

        # Join them together
        data = Os+';'+Ds

        # Define which coords in data string are origins, and which are destinations
        sources = ['%d' % x for x in range(0,len(O_list))]
        sources = ';'.join(str(x).replace("'", "") for x in sources)
        lenth = len(O_list)+len(D_list)
        destinations = ['%d' % x for x in range(len(O_list),lenth)]
        destinations = ';'.join(str(x).replace("'", "") for x in destinations)

        # Build request string
        request = header+data+'?sources='+sources+'&destinations='+destinations

        # Pass request to interweb
        r = url.urlopen(request)

        # Error handle
        try:
                # Convert Bytes response to readable Json
                MB_TelTest_json = json.loads(r.read().decode('utf-8'))
                data_block = MB_TelTest_json['durations']
        except:
                data_block = 'null'

        # Build df from JSON
        #sources_label = [str(i['location']) for i in MB_TelTest_json['sources']]
        #dest_label = [str(i['location']) for i in MB_TelTest_json['destinations']]
        sources_label = O_IDs
        dest_label = D_IDs
        chunk = pd.DataFrame(data = data_block,
                                  columns = dest_label,
                                  index = sources_label)
        # Convert to minutes, stack 2D array to 1D array
        chunk = chunk.stack(level =-1)
        chunk.columns = ['O','D','DIST']
        return chunk
"""
# Function for performing Euclidian distances.
def EuclidCall(source_list,dest_list,source_points,dest_points):
        distmatrix = np.zeros((len(source_points),len(dest_points)))
        for s in range(0,len(source_points)):
                for d in range(0,len(dest_points)):
                        # 100 included as normalisation factor to MapBox / OSRM results
                        distmatrix[s,d] = (source_points[s].distance(dest_points[d])*100)
        df = pd.DataFrame(data = distmatrix,
                                  columns = dest_list,
                                  index = source_list)
        df = df.stack(level =-1)
        df.columns = ['O','D','DIST']
        return df
"""
# Generate appropriately split source and destination lists
def split_and_bundle(in_list,break_size):
        new_list = []
        for i in range (0,(int(max(len(in_list)/break_size,1)))):
                upper = (i+1) * break_size
                lower = (upper - break_size)
                objs = in_list[lower:upper]
                new_list.append(objs)
        if len(in_list) > break_size:
                rem = len(in_list) % break_size
                if rem > 0:
                        final = upper+rem
                        new_list.append(in_list[upper:final])
        return new_list

if call_type == 'MBT' :
        sources_list = split_and_bundle(source_list, 5)
        dests_list = split_and_bundle(dest_list, 5)
        sources_UIDs = split_and_bundle(source_UIDs, 5)
        dests_UIDs = split_and_bundle(dest_UIDs, 5)
elif call_type == 'MB'or call_type == 'OSRM':
        sources_list = split_and_bundle(source_list, 12)
        dests_list = split_and_bundle(dest_list, 13)
        sources_UIDs = split_and_bundle(source_UIDs, 12)
        dests_UIDs = split_and_bundle(dest_UIDs, 13)
else:
        pass

# Run function call across the O-D matrix; output is 'df'
returns = []
numcalls = (len(sources_list) * len(dests_list))
s , d = sources_list, dests_list
i, j = 1 + (rescue * len(sources_list)), 1 + rescue

if call_type == 'Euclid':
        df = EuclidCall(source_list,dest_list,source_points,dest_points)
else:
        if rescue > 0:
                s = s[rescue:] # possibly rescue -1
                sources_UIDs = sources_UIDs[rescue:]
        print('source list: %s' % len(source_list))
        print('sources list: %s' % len(sources_list))
        print('dest list: %s' % len(dest_list))
        print('dests list: %s' % len(dests_list))
        numcalls_rem = (len(s) * len(d))
        print('\nEstimated remaining calls to chosen server: %d\n' % numcalls_rem)
        print('save points will occur every %d calls\n' % (len(dests_list)))
        time.sleep(5)
        for O_list in s:
                O_IDs = sources_UIDs[s.index(O_list)]
                for D_list in d:
                        #try:
                        if call_type == 'MBT' :
                                time.sleep(2)
                        else:
                                time.sleep(1)
                        D_IDs = dests_UIDs[d.index(D_list)]
                        if call_type == 'MB':
                                returns.append(MapboxCall(O_list,D_list,i,O_IDs,D_IDs))
                        elif call_type == 'MBT':
                                returns.append(MapboxCallTraffic(O_list,D_list,i,O_IDs,D_IDs))
                        elif call_type == 'OSRM':
                                returns.append(OSRMCall(O_list,D_list,i,O_IDs,D_IDs))
                        i += 1

                        #except:
                                #save_current(O_list, D_list)
                save(returns, j, i, numcalls, rescue_num)
                j += 1
        try:
                df = pd.concat(returns)
        except:
                df = returns

# re-attach the population of origins and destinations, prep dataframe
all_matrices = []
if rescue_num > 0:
        for r in range(0,rescue_num):
                rescued_matrix = pd.read_csv(os.path.join(ffpath,'temp_file_%d.csv' % (r)),header=None)
                rescued_matrix.columns = ['O_UID','D_UID','DIST']
                all_matrices.append(rescued_matrix)
df = df.reset_index()
df.columns = ['O_UID','D_UID','DIST']
all_matrices.append(df)
new = pd.concat(all_matrices)
new = new.set_index('O_UID')
new['DIST'] = new['DIST'].apply(pd.to_numeric)
popdf = input_df[[UID,Pop]].set_index(UID)
new['O_POP'] = popdf[Pop]
new = new.reset_index()
new = new.set_index('D_UID')
if dest_list == source_list:
        new['D_POP'] = popdf[Pop]
        new = new.reset_index()
else:
        popdf_dest = input_df2[[UID,Pop]].set_index(UID)
        new['D_POP'] = popdf_dest[Pop]
        new = new.reset_index()
new['O_UID'] = new['O_UID'].astype(str)
new['D_UID'] = new['D_UID'].astype(str)
new['combo'] = new['O_UID']+'_X_'+new['D_UID']
new = new.drop_duplicates('combo')
new = new.drop(['combo'], axis = 1)
outpath = os.path.join(ffpath, 'Output')
if not os.path.exists(outpath):
        os.mkdir(os.path.join(ffpath, 'Output'))
new.to_csv(os.path.join(outpath, 'Pairs.csv'))

###### Market Access ######
# Define a range of lambas - the distance sensitivity factor for market access
lambder_list = [0.01,
                0.005,
                0.001,
                0.0007701635,   # Market access halves every 15 mins
                0.0003850818,   # Market access halves every 30 mins
                0.0001925409,   # Market access halves every 60 mins
                0.0000962704,   # Market access halves every 120 mins
                0.0000385082,   # Market access halves every 300 mins
                0.00001]

# Run market access for all lambda across 'new' dataframe
output = pd.DataFrame()
new = new.loc[new['DIST'] > -1]
def market_access(x,lambdar):
    return sum(x.D_POP*np.exp(-lambdar*x.DIST))
for lamdar in lambder_list:
    output[lamdar] = new.loc[new['DIST'] > 0].groupby('O_UID').apply(lambda x:market_access(x,lamdar))

#File output, print completion time
output.to_csv(os.path.join(outpath, 'Output.csv'))
readmetext = ("""
        GOST: Market Access: Product Assumptions

        Last Updated: 9 Feb 2018
        Programmer: C. Fox
        Theory: K. Garrett, T. Norman

        This GOST Market Access product is based off of:
                - Mapbox's Matrix API for travel times;
                - OSRM's API for travel times

        Travel Time Calculation

        The Mapbox Matrix API provides estimated trip durations in seconds.
        The time it takes to travel from one point to another is determined by a
        number of factors, including:
        - The profile used (walking, cycling, or driving); (GOST: set to driving)
        - The speed stored in the maxspeed tag in OpenStreetMap
          (https://wiki.openstreetmap.org/wiki/Key:maxspeed)
        - Traffic derived from real-time telemetry data, provided by Mapbox

        Traffic data

        In addition to the contributions of OpenStreetMap, Mapbox SDKs collect
        anonymous data, or telemetry, about devices using their services to continuously
        update their routing network. Attributes such as speed, turn restrictions, and
        travel mode can be collected to improve OpenStreetMap.

        Advanced - Speed Assumptions

        See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/profiles.md
        For a full explanation of profiles, and how speeds are calculated across segments

        Note on API request timings

        Requests using mapbox/driving, mapbox/walking, and mapbox/cycling profiles
        can specify up to 25 input coordinates per request. Requests using the
        mapbox/driving-traffic profiles can specify up to 10 input coordinates per request.

        Requests using mapbox/driving, mapbox/walking, and mapbox/cycling profiles
        have a maximum limit of 60 requests per minute. Requests using the
        mapbox/driving-traffic profiles have a maximum of 30 requests per minute.

        Algorithm flags

        Commands recognised for this script:
        -p   Path - string for input and output folder path
        -f   File name of .csv containing input data
        -m   Latitude column name.
        -n   Longitude column name
        -o   Origin Unique Identifier column name (e.g. District, Name, Object ID...).
             This is mainly helpful for joining the output back to the input data / a shapefile,
             and is non-essential in terms of the calculation. It can be text or a number.
        -q   Population / weighting column name
        -c   Server call type - "OSRM" for OSRM, "MB" for Mapbox, "MBT" for Mapbox traffic, or "Euclid" for Euclidean distances (as the crow flies)
        -l   Limit - use this to limit the coordinate input list (int). Optional.

        *** Optional - if sources =/= destinations. Note - Unique identifier and Pop column names must remain the same ***
        -W   Filename of destinations csv
        *** Optional - if encountering server errors / internet connectivity instability ***
        -R   Save - input latest save number to pick up matrix construciton process from there.
        -Z   Rescue number parameter - If you have already re-started the download process, denote how many times. First run = 0, restarted once = 1...
        Do NOT put column names or indeed any input inside quotation marks.
        The only exceptions is if the file paths have spaces in them.
        """)

text_file = open(os.path.join(ffpath,"GOST_ReadMe_MarketAccess.txt"), "w")
text_file.write(readmetext)
text_file.close()

print('\nAll processes complete. Check your path for outputs.')
print('Script will now exit.\n')


import requests
import json

def github_search(a,b):
    links = []
    params = {'q': a +'language:'+ b}
    response = requests.get('https://api.github.com/search/repositories', params=params)
    json_file = response.json()
    for item in json_file['items']:
        links.append(item['url'])
    return links

query = "arduino"
language = "python"
repos = github_search(query, language)


import unittest
from typing import List, Tuple
import numpy as np

from hw_asr.metric.utils import calc_wer, calc_cer

class TestMetric(unittest.TestCase):
    samples: List[Tuple[str, str, float, float]] = [
            ("if you can not measure it you can not improve it",
             "if you can nt measure t yo can not i",
             0.454, 0.25),
            ("if you cant describe what you are doing as a process you dont know what youre doing",
             "if you cant describe what you are doing as a process you dont know what youre doing",
             0.0, 0.0),
            ("one measurement is worth a thousand expert opinions",
             "one  is worth thousand opinions",
             0.375, 0.392),
            ("", "sm", 1, 1),
            ("", "", 0, 0)
        ]

    def test_wer(self):
        for target, pred, expected_wer, _ in self.samples:
            wer = calc_wer(target, pred)
            self.assertTrue(np.isclose(wer, expected_wer, atol=1e-3))

    def test_cer(self):
        for target, pred, _, expected_cer in self.samples:
            cer = calc_cer(target, pred)
            self.assertTrue(np.isclose(cer, expected_cer, atol=1e-3))


import os
from urllib.parse import urlencode
import requests
from datetime import date, timedelta

class NewsRequest:
    def __init__(self) -> None:
        # Get the directory where the script is located
        script_dir = os.path.dirname(os.path.abspath(__file__))

        # Combine the script directory with the 'key' file name
        # key_path = os.path.join(script_dir, 'key')

        # with open(key_path) as f:
        #     self.key = f.read()
        self.key = os.environ['NEWS_API_KEY']

        self.base_url = 'https://newsapi.org/v2/'

    def get_article(self, company, num_articles=3) -> list:
        data = {
            'q': company,
            'apiKey': self.key,
            'pageSize': num_articles,
            'language': 'en',
            'sortBy': 'popularity',
            'to': date.today(),
            'from': date.today() - timedelta(days=3)
        }

        params = urlencode(data)
        r = requests.get(url=f'{self.base_url}everything?', params=data)
        articles = r.json()['articles']

        response = []

        for article in articles:
            article_info = dict({})
            article_info['title'] = article['title']
            article_info['description'] = article['description']
            article_info['url'] = article['url']

            response.append(article_info)

        return response

if __name__ == '__main__':
    n = NewsRequest()
    print(n.get_article('Tesla'))


"""
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7.

The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.

Example 1:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
Output: 60
Explanation:
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
Example 2:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
Output: 68
Explanation:
This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
Example 3:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
Output: 72

Constraints:

1 <= n <= 10^5
speed.length == n
efficiency.length == n
1 <= speed[i] <= 10^5
1 <= efficiency[i] <= 10^8
1 <= k <= n
"""
import heapq
from typing import List

class MaximumPerformanceofaTeam:
    def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
        #effspeed = sorted(zip(efficiency, speed), reverse=True)
        effspeed = sorted(zip(efficiency, speed), key = lambda x : -x[0])

        ans, sum = 0, 0
        pqspeed: heapq = []
        for efc, spd in effspeed:
            if len(pqspeed) == k:
                #evict the slowest engineer from queue
                sum -= heapq.heappop(pqspeed);

            heapq.heappush(pqspeed, spd)
            sum += spd
            #effspeed is sort by efficiency desc, so efc is the minimum value
            ans = max(ans, sum * efc)

        return ans % (10**9 + 7)  #int(1e9+7)


"""empty message

Revision ID: 6eaa252e5764
Revises: 2e51d44d4a90
Create Date: 2021-12-12 17:47:00.868038

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '6eaa252e5764'
down_revision = '2e51d44d4a90'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('spends', sa.Column('owner_id', sa.Integer(), nullable=False))
    op.create_foreign_key(None, 'spends', 'users', ['owner_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'spends', type_='foreignkey')
    op.drop_column('spends', 'owner_id')
    # ### end Alembic commands ###


import asyncio
import csv
import logging
import os

headers = {
            'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                          '(KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36'
        }

DEBUG = True

async def send_request(session, url, response_type='text', method='get', post_payload=None):
    if post_payload is None:
        post_payload = dict()
    method = method.lower()
    if response_type not in ('text', 'json'):
        return False
    if method not in ('get', 'post'):
        return False

    logging.info(f'sending {method}: {url.replace(" ", "%20")}')
    async with session.get(url) if method.lower() == 'get' else session.post(url, json=post_payload) as response:
        if response.status == 200:
            if response_type == 'text':
                return await response.text()
            else:
                return await response.json()
        else:
            return False

async def send_request_multiple(session, url, tries=5, sleep=120, response_type='text',
                                method='get', post_payload=None):
    __count = 0

    result = await send_request(session, url, response_type=response_type, method=method, post_payload=post_payload)

    while result is False and __count < tries:
        await asyncio.sleep(sleep)
        result = await send_request(session, url, response_type=response_type, method=method, post_payload=post_payload)
        __count += 1
        logging.warning(f'FETCH 1: request {url.split("=")[-1]} fail: {__count} time, sleeping {sleep} sec')
    if __count >= 5:
        result = ''
    return result

async def send_data(output, running, check_id, bot, fetch_name,
                    ru_key, en_key, orig_key, dir):
    first_row = ['Русс название', 'Англ название', 'Ориг название']
    rows = [first_row, ]
    for item in output:
        ru_name = item[ru_key].replace(',', '').replace(';', '')
        en_name = item[en_key].replace(',', '').replace(';', '')
        orig_name = item[orig_key].replace(',', '').replace(';', '')
        dir_href = item[dir].replace(',', '').replace(';', '')
        rows.append([ru_name, en_name, orig_name, dir_href])
    logging.info(f'Saving data to output_{fetch_name}.csv...')
    with open(f'output_{fetch_name}.csv', 'w', encoding="utf-8-sig", newline="") as mint_out:
        writer = csv.writer(mint_out, delimiter=';')
        writer.writerows(rows)
    logging.info('Data saved, sending document to users...')
    print(check_id, running, running[check_id])
    for user_id in running[check_id]['users']:
        await bot.send_message(user_id, f'Проверка {fetch_name} завершена, отправка файла...')
        await bot.send_document(user_id, open(f'output_{fetch_name}.csv', 'rb'))
    logging.info('Send complete, deleting file...')
    os.remove(f'output_{fetch_name}.csv')
    logging.info('Done')