import pygame

class Button:
    def __init__(self, image, center, button_press_command, screen_size, scale_factor, call, scene=None, list_to_append=None):
        self.screen_size = screen_size
        self.image = pygame.image.load(image)
        self.image = pygame.transform.scale(self.image, (round(screen_size[0] / scale_factor[0]), round(screen_size[1] / scale_factor[1])))
        self.center = center
        if scene is not None:
            self.scene = scene
            scene.append(self)
        self.rect = self.image.get_rect()
        self.rect = pygame.Rect(self.center[0] - (self.rect[2] // 2), self.center[1] - (self.rect[3] // 2), self.rect[2], self.rect[3])

        # Making a hover over version of itself (lighter and slightly bigger)
        self.image_lighter = self.image.copy()
        self.image_lighter = brighten_image(self.image_lighter, 20)
        self.image_lighter = pygame.transform.scale(self.image_lighter, (int(self.rect[2] * 1.2), int(self.rect[3] * 1.2)))

        # Making a press down version of itself (darker)
        self.image_darker = self.image.copy()
        self.image_darker = darken_image(self.image_darker, 50)

        # Start by displaying default version of itself
        self.image_to_display = self.image
        self.is_hovering = False
        self.is_pressed_down = False
        self.is_pressed_up = False
        self.button_press_command = button_press_command
        self.call_events = call
        self.is_active = False
        if list_to_append is not None:
            list_to_append.append(self)

    def __repr__(self):
        return "Button: " + str(self.button_press_command)

    def state_check(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.is_hovering:
            self.is_hovering = False

        if self.rect[0] + self.rect[2] > mouse_pos[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse_pos[
            1] > self.rect[1] and not self.is_hovering and self.is_active:
            self.is_hovering = True

        if self.is_pressed_up:
            self.button_press_command()
            self.is_pressed_up = False

        if self.is_hovering and not self.is_pressed_down:
            self.image_to_display = self.image_lighter
        elif self.is_pressed_down and self.is_hovering:
            self.image_to_display = self.image_darker
        else:
            self.image_to_display = self.image

        for event in self.call_events:
            if not event:
                self.is_active = False
        for event in self.call_events:
            if event:
                self.is_active = True

    def get_center_cor(self):
        if self.is_hovering and not self.is_pressed_down:
            return self.center[0] - ((self.rect[2] * 1.2) // 2), self.center[1] - ((self.rect[3] * 1.2) // 2)
        elif not self.is_hovering or self.is_pressed_down:
            return self.center[0] - (self.rect[2] // 2), self.center[1] - (self.rect[3] // 2)

# Function that brightens image by certain amount
def brighten_image(image, brighten):
    image.fill((brighten, brighten, brighten), special_flags=pygame.BLEND_RGB_ADD)
    return image

# Function that darkens image by certain amount
def darken_image(image, darken):
    image.fill((darken, darken, darken), special_flags=pygame.BLEND_RGB_SUB)
    return image


import logging
import json
import os

from django.http import HttpResponse
from pysearpc import SearpcError
from rest_framework import status
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
import seaserv
from seaserv import seafile_api

from seahub.api2.authentication import TokenAuthentication
from seahub.api2.permissions import IsRepoAccessible
from seahub.api2.throttling import UserRateThrottle
from seahub.api2.utils import api_error
from seahub.base.templatetags.seahub_tags import email2nickname
from seahub.base.accounts import User
from seahub.share.signals import share_repo_to_user_successful, \
    share_repo_to_group_successful
from seahub.utils import (is_org_context, is_valid_username,
                          send_perm_audit_msg)

logger = logging.getLogger(__name__)
json_content_type = 'application/json; charset=utf-8'

class DirSharedItemsEndpoint(APIView):
    """Support uniform interface(list, share, unshare, modify) for sharing
    library/folder to users/groups.
    """
    authentication_classes = (TokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated, IsRepoAccessible)
    throttle_classes = (UserRateThrottle, )

    def list_user_shared_items(self, request, repo_id, path):
        username = request.user.username
        if path == '/':
            share_items = seafile_api.list_repo_shared_to(username, repo_id)
        else:
            share_items = seafile_api.get_shared_users_for_subdir(repo_id,
                                                                  path, username)
        ret = []
        for item in share_items:
            ret.append({
                "share_type": "user",
                "user_info": {
                    "name": item.user,
                    "nickname": email2nickname(item.user),
                },
                "permission": item.perm,
            })
        return ret

    def list_group_shared_items(self, request, repo_id, path):
        username = request.user.username
        if path == '/':
            share_items = seafile_api.list_repo_shared_group_by_user(username, repo_id)
        else:
            share_items = seafile_api.get_shared_groups_for_subdir(repo_id,
                                                                   path, username)
        ret = []
        for item in share_items:
            ret.append({
                "share_type": "group",
                "group_info": {
                    "id": item.group_id,
                    "name": seaserv.get_group(item.group_id).group_name,
                },
                "permission": item.perm,
            })
        return ret

    def handle_shared_to_args(self, request):
        share_type = request.GET.get('share_type', None)
        shared_to_user = False
        shared_to_group = False
        if share_type:
            for e in share_type.split(','):
                e = e.strip()
                if e not in ['user', 'group']:
                    continue
                if e == 'user':
                    shared_to_user = True
                if e == 'group':
                    shared_to_group = True
        else:
            shared_to_user = True
            shared_to_group = True

        return (shared_to_user, shared_to_group)

    def get_sub_repo_by_path(self, request, repo, path):
        if path == '/':
            raise Exception("Invalid path")

        # get or create sub repo
        username = request.user.username
        if is_org_context(request):
            org_id = request.user.org.org_id
            sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo(
                org_id, repo.id, path, username)
        else:
            sub_repo = seafile_api.get_virtual_repo(repo.id, path, username)

        return sub_repo

    def get_or_create_sub_repo_by_path(self, request, repo, path):
        username = request.user.username
        sub_repo = self.get_sub_repo_by_path(request, repo, path)
        if not sub_repo:
            name = os.path.basename(path)
            # create a sub-lib,
            # use name as 'repo_name' & 'repo_desc' for sub_repo
            if is_org_context(request):
                org_id = request.user.org.org_id
                sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo(
                    org_id, repo.id, path, name, name, username)
            else:
                sub_repo_id = seafile_api.create_virtual_repo(repo.id, path,
                                                              name, name, username)
            sub_repo = seafile_api.get_repo(sub_repo_id)

        return sub_repo

    def get_repo_owner(self, request, repo_id):
        if is_org_context(request):
            return seafile_api.get_org_repo_owner(repo_id)
        else:
            return seafile_api.get_repo_owner(repo_id)

    def get(self, request, repo_id, format=None):
        """List shared items(shared to users/groups) for a folder/library.
        """
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            return api_error(status.HTTP_404_NOT_FOUND, 'Library %s not found.' % repo_id)

        shared_to_user, shared_to_group = self.handle_shared_to_args(request)

        path = request.GET.get('p', '/')
        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_404_NOT_FOUND, 'Folder %s not found.' % path)

        ret = []
        if shared_to_user:
            ret += self.list_user_shared_items(request, repo_id, path)

        if shared_to_group:
            ret += self.list_group_shared_items(request, repo_id, path)

        return HttpResponse(json.dumps(ret), status=200,
                            content_type=json_content_type)

    def post(self, request, repo_id, format=None):
        """Update shared item permission.
        """
        username = request.user.username
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            return api_error(status.HTTP_404_NOT_FOUND, 'Library %s not found.' % repo_id)

        path = request.GET.get('p', '/')
        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_400_BAD_REQUEST, 'Directory not found.')

        if username != self.get_repo_owner(request, repo_id):
            return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')

        shared_to_user, shared_to_group = self.handle_shared_to_args(request)

        permission = request.data.get('permission', 'r')
        if permission not in ['r', 'rw']:
            return api_error(status.HTTP_400_BAD_REQUEST, 'permission invalid.')

        path = request.GET.get('p', '/')
        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_404_NOT_FOUND, 'Folder %s not found.' % path)

        if path == '/':
            shared_repo = repo
        else:
            try:
                sub_repo = self.get_sub_repo_by_path(request, repo, path)
                if sub_repo:
                    shared_repo = sub_repo
                else:
                    # unlikely to happen
                    return api_error(status.HTTP_404_NOT_FOUND, 'Failed to get sub repo')
            except SearpcError as e:
                logger.error(e)
                return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')

        if shared_to_user:
            shared_to = request.GET.get('username')
            if shared_to is None or not is_valid_username(shared_to):
                return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % shared_to)

            try:
                User.objects.get(email=shared_to)
            except User.DoesNotExist:
                return api_error(status.HTTP_400_BAD_REQUEST, 'Invalid user, should be registered')

            if is_org_context(request):
                org_id = request.user.org.org_id
                seaserv.seafserv_threaded_rpc.org_set_share_permission(
                    org_id, shared_repo.id, username, shared_to, permission)
            else:
                seafile_api.set_share_permission(shared_repo.id, username,
                                                 shared_to, permission)

            send_perm_audit_msg('modify-repo-perm', username, shared_to,
                                repo_id, path, permission)

        if shared_to_group:
            gid = request.GET.get('group_id')
            try:
                gid = int(gid)
            except ValueError:
                return api_error(status.HTTP_400_BAD_REQUEST, 'group_id %s invalid.' % gid)
            group = seaserv.get_group(gid)
            if not group:
                return api_error(status.HTTP_404_NOT_FOUND, 'Group %s not found.' % gid)

            if is_org_context(request):
                org_id = request.user.org.org_id
                seaserv.seafserv_threaded_rpc.set_org_group_repo_permission(
                    org_id, gid, shared_repo.id, permission)
            else:
                seafile_api.set_group_repo_permission(gid, shared_repo.id,
                                                      permission)

            send_perm_audit_msg('modify-repo-perm', username, gid,
                                repo_id, path, permission)

        return HttpResponse(json.dumps({'success': True}), status=200,
                            content_type=json_content_type)

    def put(self, request, repo_id, format=None):
        username = request.user.username
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            return api_error(status.HTTP_404_NOT_FOUND, 'Library %s not found.' % repo_id)

        path = request.GET.get('p', '/')
        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_404_NOT_FOUND, 'Folder %s not found.' % path)

        if username != self.get_repo_owner(request, repo_id):
            return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')

        if path != '/':
            try:
                sub_repo = self.get_or_create_sub_repo_by_path(request, repo, path)
            except SearpcError as e:
                logger.error(e)
                return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub repo.')
        else:
            sub_repo = None

        share_type = request.data.get('share_type')
        if share_type != 'user' and share_type != 'group':
            return api_error(status.HTTP_400_BAD_REQUEST, 'share_type invalid.')

        permission = request.data.get('permission', 'r')
        if permission not in ['r', 'rw']:
            return api_error(status.HTTP_400_BAD_REQUEST, 'permission invalid.')

        shared_repo = repo if path == '/' else sub_repo
        result = {}
        result['failed'] = []
        result['success'] = []

        if share_type == 'user':
            share_to_users = request.data.getlist('username')
            for to_user in share_to_users:
                if not is_valid_username(to_user):
                    result['failed'].append({
                        'email': to_user,
                        'error_msg': 'Nutzername oder E-Mail-Adresse ungültig.'
                        })
                    continue

                try:
                    User.objects.get(email=to_user)
                except User.DoesNotExist:
                    result['failed'].append({
                        'email': to_user,
                        'error_msg': 'Es wurde kein Nutzer mit der E-Mail-Adresse %s gefunden. Benutzen Sie die Option „Download-Link erstellen” bzw. „Upload-Link erstellen”, um Elemente für nicht-registrierte Nutzer freizugeben.' % to_user
                        })
                    continue

                try:
                    if is_org_context(request):
                        org_id = request.user.org.org_id
                        seaserv.seafserv_threaded_rpc.org_add_share(
                            org_id, shared_repo.id, username, to_user,
                            permission)
                    else:
                        seafile_api.share_repo(shared_repo.id, username,
                                               to_user, permission)

                    # send a signal when sharing repo successful
                    share_repo_to_user_successful.send(sender=None,
                                                       from_user=username,
                                                       to_user=to_user,
                                                       repo=shared_repo)
                    result['success'].append({
                        "share_type": "user",
                        "user_info": {
                            "name": to_user,
                            "nickname": email2nickname(to_user),
                        },
                        "permission": permission
                    })

                    send_perm_audit_msg('add-repo-perm', username, to_user,
                                        repo_id, path, permission)
                except SearpcError as e:
                    logger.error(e)
                    result['failed'].append({
                        'email': to_user,
                        'error_msg': 'Internal Server Error'
                        })
                    continue

        if share_type == 'group':
            group_ids = request.data.getlist('group_id')
            for gid in group_ids:
                try:
                    gid = int(gid)
                except ValueError:
                    return api_error(status.HTTP_400_BAD_REQUEST, 'group_id %s invalid.' % gid)
                group = seaserv.get_group(gid)
                if not group:
                    return api_error(status.HTTP_404_NOT_FOUND, 'Group %s not found' % gid)

                try:
                    if is_org_context(request):
                        org_id = request.user.org.org_id
                        seafile_api.add_org_group_repo(shared_repo.repo_id,
                                                       org_id, gid, username,
                                                       permission)
                    else:
                        seafile_api.set_group_repo(shared_repo.repo_id, gid,
                                                   username, permission)

                    share_repo_to_group_successful.send(sender=None,
                        from_user=username, group_id=gid, repo=shared_repo)

                    result['success'].append({
                        "share_type": "group",
                        "group_info": {
                            "id": gid,
                            "name": group.group_name,
                        },
                        "permission": permission
                    })

                    send_perm_audit_msg('add-repo-perm', username, gid,
                                        repo_id, path, permission)
                except SearpcError as e:
                    logger.error(e)
                    result['failed'].append({
                        'group_name': group.group_name,
                        'error_msg': 'Internal Server Error'
                        })
                    continue

        return HttpResponse(json.dumps(result),
            status=200, content_type=json_content_type)

    def delete(self, request, repo_id, format=None):
        username = request.user.username
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            return api_error(status.HTTP_404_NOT_FOUND, 'Library %s not found.' % repo_id)

        path = request.GET.get('p', '/')
        if seafile_api.get_dir_id_by_path(repo.id, path) is None:
            return api_error(status.HTTP_404_NOT_FOUND, 'Folder %s not found.' % path)

        if username != self.get_repo_owner(request, repo_id):
            return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.')

        shared_to_user, shared_to_group = self.handle_shared_to_args(request)

        if path == '/':
            shared_repo = repo
        else:
            try:
                sub_repo = self.get_sub_repo_by_path(request, repo, path)
                if sub_repo:
                    shared_repo = sub_repo
                else:
                    return api_error(status.HTTP_404_NOT_FOUND, 'Sub-library not found.')
            except SearpcError as e:
                logger.error(e)
                return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Failed to get sub-library.')

        if shared_to_user:
            shared_to = request.GET.get('username')
            if shared_to is None or not is_valid_username(shared_to):
                return api_error(status.HTTP_400_BAD_REQUEST, 'Email %s invalid.' % shared_to)

            try:
                User.objects.get(email=shared_to)
            except User.DoesNotExist:
                return api_error(status.HTTP_400_BAD_REQUEST, 'Invalid user, should be registered')

            if is_org_context(request):
                org_id = request.user.org.org_id
                seaserv.seafserv_threaded_rpc.org_remove_share(
                    org_id, shared_repo.id, username, shared_to)
            else:
                seaserv.remove_share(shared_repo.id, username, shared_to)

            permission = seafile_api.check_permission_by_path(repo.id, path,
                                                              shared_to)
            send_perm_audit_msg('delete-repo-perm', username, shared_to,
                                repo_id, path, permission)

        if shared_to_group:
            group_id = request.GET.get('group_id')
            try:
                group_id = int(group_id)
            except ValueError:
                return api_error(status.HTTP_400_BAD_REQUEST, 'group_id %s invalid' % group_id)

            # hacky way to get group repo permission
            permission = ''
            for e in seafile_api.list_repo_shared_group_by_user(username, shared_repo.id):
                if e.group_id == group_id:
                    permission = e.perm
                    break

            if is_org_context(request):
                org_id = request.user.org.org_id
                seaserv.del_org_group_repo(shared_repo.id, org_id, group_id)
            else:
                seafile_api.unset_group_repo(shared_repo.id, group_id, username)

            send_perm_audit_msg('delete-repo-perm', username, group_id,
                                repo_id, path, permission)

        return HttpResponse(json.dumps({'success': True}), status=200,
                            content_type=json_content_type)


import sys
from clean_folder.functions import get_file_list

def do_cleaning():
    if len(sys.argv) == 1:
        print("Можна передати шлях до каталогу у якості праметра командного рядка наступним чином:")
        print(f"\n\tpython {sys.argv[0]} <шлях_до_каталогу>\n")
        print("або ввести з клавіатури нижче.\n")
        try:
            scan_dir = input("Введіть шлях до каталогу: ")
        except KeyboardInterrupt:
            print("")
            exit()
    else:
        scan_dir = sys.argv[1]

    try:
        file_list = get_file_list(scan_dir)

        for key, val in file_list.items():
            print(f"{key} ->")
            for each in val:
                print(each)
    except Exception as ex:
        print(ex)

if __name__ == "__main__":
    do_cleaning()


import tensorflow as tf
import json
import os
import custom_function as cf
import wav
import numpy as np
import denoise_wavenet_condition_test as DWC
import time
import datetime
import math
import make_dataset_test as md

# tf version check
tf_version = cf.get_tf_version()

# prevent GPU overflow
cf.tf_gpu_active_alloc()

# read config file
with open("config.json", "r") as f_json:
    config = json.load(f_json)

previous_size = config['previous_size']
frame_size = config["frame_size"]
future_size = config["future_size"]
shift_size = config["shift_size"]
window_type = config["window_type"]
dilation = config["dilation"]
num_of_speaker = config["num_of_speaker"]
num_of_noise = config["num_of_noise"]
if num_of_speaker < 2:
    num_of_speaker = 0
if num_of_noise < 2:
    num_of_noise = 0
max_condition = num_of_speaker + num_of_noise
if max_condition == 0:
    raise Exception("E: num_of_speaker and num_of_noise can't 0 same time in train_cond.py")

batch_size = 1
epochs = 1
learning_rate = config["learning_rate"]
default_float = config["default_float"]

train_source_path = config["train_source_path"]
train_target_path = config["train_target_path"]

load_check_point_name = config["load_check_point_name"]
save_check_point_name = config["save_check_point_name"]
save_check_point_period = config["save_check_point_period"]

# multi gpu init
# strategy = tf.distribute.experimental.CentralStorageStrategy()
strategy = tf.distribute.MirroredStrategy()

with strategy.scope():
    # make dataset
    train_source_cut_list, train_source_condition_list, train_target_cut_list, number_of_total_frame = md.make_dataset(train_source_path, train_target_path, batch_size,
                                                                 previous_size, frame_size, future_size, num_of_speaker,
                                                                 num_of_noise, shift_size, window_type, shuffle=True)
    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_source_cut_list, train_source_condition_list, train_target_cut_list)).shuffle(number_of_total_frame).batch(batch_size)
    dist_dataset_train = strategy.experimental_distribute_dataset(dataset=train_dataset)

    # make model
    model = DWC.DenoiseWavenetCondition(dilation, previous_size+frame_size+future_size, max_condition)
    loss_object = tf.keras.losses.MeanAbsoluteError(reduction=tf.keras.losses.Reduction.NONE)
    optimizer = tf.keras.optimizers.Adam(learning_rate=config['learning_rate'])
    train_loss = tf.keras.metrics.Mean(name='train_loss')

# train function
@tf.function
def train_step(dist_inputs):
    def step_fn(inputs):
        x, cond, y = inputs
        cond = tf.reshape(cond, [-1, max_condition, 1])

        with tf.GradientTape() as tape:
            condition_list = model(cond)
        cond = tf.squeeze(cond)
        return cond, condition_list

    if tf_version[1] > 2:
        cond, condition_list = strategy.run(step_fn, args=(dist_inputs,))
    else:
        cond, condition_list = strategy.experimental_run_v2(step_fn, args=(dist_inputs,))
    return cond, condition_list
# train run
with strategy.scope():
    # load model
    if load_check_point_name != "":
        saved_epoch = int(load_check_point_name.split('_')[-1])
        model.load_weights('{}/checkpoint/{}/data.ckpt'.format(cf.load_directory(), load_check_point_name))

    for epoch in range(saved_epoch, saved_epoch+epochs):
        i = 0
        for inputs in dist_dataset_train:
            cond, condition_list = train_step(inputs)
            print(cond.numpy(), np.array(condition_list))
            i += 1


while True:
    a = int(input('a = '))
    if a == 0:
        print('a != 0')
        continue
    break
b = int(input('b = '))
c = int(input('c = '))
delta = b**2 - (4*a*c)
if delta < 0:
    print('no real roots')
elif delta == 0:
    print('1 real root')
    x = -b/(2*a)
    print('x =', x)
elif delta > 0:
    print('2 real roots')
    print('x1 = the equation with the addition sign')
    print('x2 = the equation with the subtraction sign')
    import math
    d = math.sqrt(delta)
    x1 = (-b + d)/(2*a)
    x2 = (-b - d)/(2*a)
    print('x1 =', x1)
    print('x2 =', x2)


def read_matrix(is_test=False):
    if is_test:
        return [
            [7, 1, 3, 3, 2, 1],
            [1, 3, 9, 8, 5, 6],
            [4, 6, 7, 9, 1, 0],
        ]
    (rows_count, column_count) = map(int, input().split(', '))
    matirx = []
    for r in range(rows_count):
        row = list(map(int, input().split(', ')))
        matirx.append(row)
    return matirx

def get_sum_of_submatrix(matrix, row_index, column_index, size):
    the_sum = 0
    for r in range(row_index, row_index + size):
        for c in range(column_index, column_index + size):
            the_sum += matrix[r][c]

    return the_sum

def get_best_submatrix(matrix, submatrix_size):
    best_row = 0
    best_col = 0
    best_sum = get_sum_of_submatrix(matrix, best_row, best_row, submatrix_size)

    for row in range(len(matrix) - submatrix_size + 1):
        for c in range(len(matrix[row]) - submatrix_size + 1):
            current_sum = get_sum_of_submatrix(matrix, row, c, submatrix_size)
            if best_sum < current_sum:
                best_sum = current_sum
                best_row = row
                best_col = c

    return (best_row, best_col)

def print_result(coordinates, size):
    (rows, cols) = coordinates
    for r in range(rows, rows + size):
        line = []
        for c in range(cols, cols + size):
            line.append(matrix[r][c])
        print(' '.join(str(x) for x in line))
    print(get_sum_of_submatrix(matrix, rows, cols, size))

SUBMATRIX_TEST = 2

matrix = read_matrix()
coordinates = get_best_submatrix(matrix, SUBMATRIX_TEST)
print_result(coordinates, SUBMATRIX_TEST)


"""Entry points for dwolla."""
import logging

import click

from .weather import weather

@click.group(invoke_without_command=True)
@click.version_option(prog_name='Dwolla', message='%(prog)s %(version)s')
@click.pass_context
def cli(ctx) -> None:
    """CLI for dwolla."""
    configure_logging()

    if ctx.invoked_subcommand is None:
        click.echo(ctx.get_help())
        ctx.exit(1)

cli.add_command(weather)

def configure_logging() -> None:
    """Configure logging to write to logs.txt."""
    log_format = '%(asctime)s | %(levelname)s | %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_format)
    root_logger = logging.getLogger()
    try:
        log_handler = logging.FileHandler('logs.txt', mode='w')
        root_logger.addHandler(log_handler)
    except Exception as e:
        print('Error configuring logs: {}'.format(e.message))

if __name__ == '__main__':
    cli()


import os
import os.path
import stat
import sys
import time
import unittest

from nc2zarr.batch import DryRunJob
from nc2zarr.batch import JobStatus
from nc2zarr.batch import LocalJob
from nc2zarr.batch import SlurmJob
from nc2zarr.batch import TemplateBatch
from tests.helpers import IOCollector

TEST_DIR = os.path.dirname(__file__)

class TemplateBatchTest(unittest.TestCase):
    CONFIG_TEMPLATE_PATH = os.path.join(TEST_DIR, 'ghg-template.yml')

    CONFIG_DIR = os.path.join(TEST_DIR, '_ghg')
    CONFIG_PATH_TEMPLATE = os.path.join(CONFIG_DIR, 'ghg-${year}.yml')

    io_collector = IOCollector()

    CONFIG_TEMPLATE_TEXT = (
        'input:\n'
        '  paths:\n'
        '    - "${input_dir}/${year}/ESACCI-GHG-L2-CO2-SCIAMACHY-WFMD-*-fv1.nc"\n'
        '  sort_by: "name"\n'
        'output:\n'
        '  path: "${output_dir}/${year}-ESACCI-GHG-L2-CO2-SCIAMACHY-WFMD-fv1.zarr/"\n'
    )

    CONFIG_VARIABLES = [
        dict(year=year,
             input_dir='/neodc/esacci/ghg/data/crdp_4/SCIAMACHY/CO2_SCI_WFMD/v4.0',
             output_dir='./data')
        for year in range(2008, 2012 + 1)
    ]

    def setUp(self) -> None:
        self.io_collector.reset_paths()
        self.io_collector.add_path(self.CONFIG_TEMPLATE_PATH)
        self.io_collector.add_path(self.CONFIG_DIR)

        with open(self.CONFIG_TEMPLATE_PATH, 'w') as fp:
            fp.write(self.CONFIG_TEMPLATE_TEXT)

    def tearDown(self) -> None:
        self.io_collector.delete_paths()

    def test_execute_dry_run(self):
        batch = TemplateBatch(self.CONFIG_VARIABLES,
                              self.CONFIG_TEMPLATE_PATH,
                              self.CONFIG_PATH_TEMPLATE,
                              dry_run=True)
        jobs = batch.execute()
        self.assertIsInstance(jobs, list)
        self.assertEqual(5, len(jobs))
        for job in jobs:
            self.assertIsInstance(job, DryRunJob)
            self.assertIs(job.status, JobStatus.COMPLETED)

    def test_execute_dry_run_illegal_job_type(self):
        batch = TemplateBatch(self.CONFIG_VARIABLES,
                              self.CONFIG_TEMPLATE_PATH,
                              self.CONFIG_PATH_TEMPLATE, dry_run=True)
        with self.assertRaises(ValueError) as cm:
            batch.execute(job_type='pippo')
        self.assertEqual('illegal job_type "pippo"', f'{cm.exception}')

    def test_write_config_files(self):
        batch = TemplateBatch(self.CONFIG_VARIABLES,
                              self.CONFIG_TEMPLATE_PATH,
                              self.CONFIG_PATH_TEMPLATE)
        paths = batch.write_config_files()

        expected_files = [('ghg-2008.yml', 'ghg-2008.out', 'ghg-2008.err'),
                          ('ghg-2009.yml', 'ghg-2009.out', 'ghg-2009.err'),
                          ('ghg-2010.yml', 'ghg-2010.out', 'ghg-2010.err'),
                          ('ghg-2011.yml', 'ghg-2011.out', 'ghg-2011.err'),
                          ('ghg-2012.yml', 'ghg-2012.out', 'ghg-2012.err')]
        self.assertEqual([tuple(map(lambda f: os.path.join(self.CONFIG_DIR, f), t))
                          for t in expected_files],
                         paths)

        self.assertTrue(os.path.isdir(self.CONFIG_DIR))
        self.assertEqual([t[0] for t in expected_files],
                         sorted(os.listdir(self.CONFIG_DIR)))

        with open(os.path.join(self.CONFIG_DIR, 'ghg-2012.yml'), 'r') as fp:
            ghg_config_2012 = fp.read()

        self.assertEqual((
            'input:\n'
            '  paths:\n'
            '    - "/neodc/esacci/ghg/data/crdp_4/SCIAMACHY/CO2_SCI_WFMD/v4.0/'
            '2012/ESACCI-GHG-L2-CO2-SCIAMACHY-WFMD-*-fv1.nc"\n'
            '  sort_by: "name"\n'
            'output:\n'
            '  path: "./data/2012-ESACCI-GHG-L2-CO2-SCIAMACHY-WFMD-fv1.zarr/"\n'
        ), ghg_config_2012)

JOB_OUT_PATH = "_job_test.out"
JOB_ERR_PATH = "_job_test.err"

# noinspection PyPep8Naming
class BatchJobTest(unittest.TestCase):
    io_collector = IOCollector()

    def setUp(self) -> None:
        self.io_collector.reset_paths()
        self.io_collector.add_path(JOB_OUT_PATH)
        self.io_collector.add_path(JOB_ERR_PATH)

    def tearDown(self) -> None:
        self.io_collector.delete_paths()

class DryRunJobTest(BatchJobTest):

    def test_job_ok(self):
        job = DryRunJob.submit_job(['nc2zarr', '--help'], JOB_OUT_PATH, JOB_ERR_PATH)
        self.assertIsInstance(job, DryRunJob)
        self.assertIs(job.status, JobStatus.COMPLETED)
        self.assertTrue(job.done)

class LocalJobTest(BatchJobTest):

    def test_job_ok(self):
        job = LocalJob.submit_job(['nc2zarr', '--help'],
                                  JOB_OUT_PATH,
                                  JOB_ERR_PATH,
                                  env_vars=dict(TEST='42'),
                                  cwd_path='.',
                                  poll_period=0.1)
        self.assertIsInstance(job, LocalJob)
        self.assertFalse(job.done)
        while job.status is JobStatus.RUNNING:
            time.sleep(0.1)
        self.assertIs(job.status, JobStatus.COMPLETED)
        self.assertTrue(job.done)
        state = job.state
        self.assertIsInstance(state, dict)
        self.assertIn('exit_code', state)
        self.assertIn('pid', state)

SBATCH_MOCK_WIN32 = 'sbatch-mock.bat'
SBATCH_MOCK_UNIX = './sbatch-mock.sh'

SQUEUE_MOCK_WIN32 = 'squeue-mock.bat'
SQUEUE_MOCK_UNIX = './squeue-mock.sh'

class SlurmJobTest(BatchJobTest):

    def setUp(self) -> None:
        super().setUp()
        if sys.platform == 'win32':
            self.sbatch_program = SBATCH_MOCK_WIN32
            self.squeue_program = SQUEUE_MOCK_WIN32
        else:
            self.sbatch_program = SBATCH_MOCK_UNIX
            self.squeue_program = SQUEUE_MOCK_UNIX
        self.io_collector.add_path(self.sbatch_program)
        self.io_collector.add_path(self.squeue_program)

    def _write_sbatch_exe(self, unix_content: str, win32_content: str):
        self._write_exe(self.sbatch_program, unix_content, win32_content)

    def _write_squeue_exe(self, unix_content: str, win32_content: str):
        self._write_exe(self.squeue_program, unix_content, win32_content)

    @classmethod
    def _write_exe(cls, path: str, unix_content: str, win32_content: str):
        if sys.platform == 'win32':
            with open(path, 'w') as fp:
                fp.write(win32_content)
        else:
            with open(path, 'w') as fp:
                fp.write(unix_content)
            st = os.stat(path)
            os.chmod(path, st.st_mode | stat.S_IEXEC)

SBATCH_OUT = 'Submitted batch job 43862490'

SQUEUE_OUT_OK_1 = r'             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)'
SQUEUE_OUT_OK_2 = r'          43862490 short-ser ghg/ch4/   forman  R       0:03      1 host495'

class SlurmJobSuccessTest(SlurmJobTest):

    def setUp(self) -> None:
        super().setUp()
        self._write_sbatch_exe(f'#!/bin/sh\n'
                               f'echo {SBATCH_OUT}\n',
                               f'@echo {SBATCH_OUT}\n')
        self._write_squeue_exe(
            f'#!/bin/sh\n'
            f"echo '{SQUEUE_OUT_OK_1}'\n"
            f"echo '{SQUEUE_OUT_OK_2}'\n",
            f'@echo {SQUEUE_OUT_OK_1}\n'
            f'@echo {SQUEUE_OUT_OK_2}\n'
        )

    def test_job_ok(self):
        job = SlurmJob.submit_job(['nc2zarr', '--help'],
                                  JOB_OUT_PATH,
                                  JOB_ERR_PATH,
                                  cwd_path='.',
                                  env_vars=dict(TEST=123),
                                  partition='short-serial',
                                  duration='02:00:00',
                                  poll_period=0.1,
                                  sbatch_program=self.sbatch_program,
                                  squeue_program=self.squeue_program)
        self.assertEqual('43862490', job.job_id)
        self.assertIsInstance(job, SlurmJob)
        time.sleep(0.2)
        self.assertEqual(
            {
                'JOBID': '43862490',
                'NAME': 'ghg/ch4/',
                'NODELIST(REASON)': 'host495',
                'NODES': '1',
                'PARTITION': 'short-ser',
                'ST': 'R',
                'TIME': '0:03',
                'USER': 'forman'
            },
            job.state)
        self.assertTrue(job.observing)
        self.assertFalse(job.done)
        job.end_observation()
        self.assertFalse(job.observing)
        self.assertFalse(job.done)

SQUEUE_OUT_ERR = 'slurm_load_jobs error: Invalid job id specified'

class SlurmJobSuccessButPollFailsTest(SlurmJobTest):

    def setUp(self) -> None:
        super().setUp()
        self._write_sbatch_exe(f'#!/bin/sh\n'
                               f'echo {SBATCH_OUT}\n',
                               f'@echo {SBATCH_OUT}\n')
        self._write_squeue_exe(
            f'#!/bin/sh\n'
            f'echo {SQUEUE_OUT_ERR}\n'
            f'exit 1',
            f'@echo {SQUEUE_OUT_ERR}\n'
            f'@exit /B 2\n'
        )

    def test_job_ok_but_poll_fails(self):
        job = SlurmJob.submit_job(['nc2zarr', '--help'], JOB_OUT_PATH, JOB_ERR_PATH, cwd_path='.',
                                  env_vars=dict(TEST=123), partition='short-serial', duration='02:00:00',
                                  poll_period=0.1,
                                  sbatch_program=self.sbatch_program,
                                  squeue_program=self.squeue_program)
        self.assertEqual('43862490', job.job_id)
        self.assertIsInstance(job, SlurmJob)
        self.assertIsNone(job.done)
        time.sleep(0.2)
        self.assertIsNone(job.state)
        self.assertIs(job.status, JobStatus.UNKNOWN)
        self.assertTrue(job.observing)
        self.assertIsNone(job.done)
        time.sleep(0.4)
        self.assertFalse(job.observing)
        self.assertIsNone(job.done)

class SlurmJobFailureTest(SlurmJobTest):

    def setUp(self) -> None:
        super().setUp()
        self._write_sbatch_exe('#!/bin/sh\n'
                               'exit 2\n',
                               '@exit /B 2\n')

    def test_job_fails(self):
        with self.assertRaises(EnvironmentError) as cm:
            SlurmJob.submit_job(['nc2zarr', '--help'],
                                JOB_OUT_PATH,
                                JOB_ERR_PATH,
                                cwd_path='.',
                                env_vars=dict(TEST1=123, TEST2='ABC'),
                                partition='short-serial',
                                duration='02:00:00',
                                sbatch_program=self.sbatch_program)
        print(f'{cm.exception}')
        self.assertEqual(f"Slurm job submission failed for command line:"
                         f" {self.sbatch_program}"
                         f" -o _job_test.out"
                         f" -e _job_test.err"
                         f" --partition=short-serial"
                         f" --time=02:00:00"
                         f" --chdir=."
                         f" --export=ALL,TEST1=123,TEST2=ABC"
                         f" nc2zarr --help",
                         f'{cm.exception}')

class JobStatusTest(unittest.TestCase):
    def test_init(self):
        with self.assertRaises(ValueError) as cm:
            JobStatus('Pippo')
        self.assertEqual("invalid status_id: 'Pippo'", f'{cm.exception}')
        with self.assertRaises(TypeError) as cm:
            # noinspection PyTypeChecker
            JobStatus(137)
        self.assertEqual("invalid status_id: 137", f'{cm.exception}')

    def test_is(self):
        self.assertIs(JobStatus('Running'), JobStatus.RUNNING)
        self.assertIs(JobStatus('Running'), JobStatus('Running'))
        self.assertIsNot(JobStatus.RUNNING, JobStatus.PENDING)

    def test_eq(self):
        self.assertEqual(JobStatus('Running'), JobStatus.RUNNING)
        self.assertEqual(JobStatus('Running'), JobStatus('Running'))
        self.assertEqual(JobStatus('Running'), 'Running')
        self.assertEqual('Running', JobStatus.RUNNING)
        self.assertNotEqual(JobStatus.RUNNING, JobStatus.PENDING)

    def test_hash(self):
        self.assertEqual(hash(JobStatus('Running')), hash(JobStatus.RUNNING))
        self.assertEqual(hash(JobStatus('Running')), hash('Running'))

    def test_str(self):
        self.assertEqual('Running', f'{JobStatus.RUNNING}')

    def test_repr(self):
        self.assertEqual("JobStatus('Running')", f'{JobStatus.RUNNING!r}')


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 24 14:21:47 2021

@author: Manjuanel
"""

from pprint import pprint
import csv
def leer_camion(nombre_archivo):
    with open(nombre_archivo, 'rt') as file:  #abre el archivo
            rows = csv.reader(file) #ahora lee el archivo con modulo CSV
            header = next(rows) #esto para no leer la primera linea
            camion = []
            precio_total = 0

            for n_row, row in enumerate(rows, start=1): #para cada row (con indice n_row, empezando de 1) en las rows
                record = dict(zip(header, row)) #crea un objeto que tenga la lista de cada header con cada elemento de row
                #pprint(record)
                cajones = int(record['cajones']) #transforma la cadena a número entero
                precio = float(record['precio']) #transforma la cadena a número con coma
                inter = {'nombre': row[0], 'cajones': cajones , 'precio': precio} #pone los valores en diccionario
                camion.append(inter) #metelo en camion
                precio_total = precio_total + inter['precio'] * inter['cajones'] #va sumando precios
                inter = {} #vacia el intermediario

            return camion

            print(precio_total)
nombre_archivo = 'F:/Desktop/Ejercicios/ejercicios_python/Data/fecha_camion.csv'
camionsito = leer_camion(nombre_archivo)

def leer_precios(nombre_archivo):
    with open(nombre_archivo, 'rt') as file:
        precios = {}
        rows = csv.reader(file)
        for row in rows:
            if row == []:
                del row
                break
            precios[row[0]] = row[1]
        return precios

nombre_archivo = 'F:/Desktop/Ejercicios/ejercicios_python/Data/precios.csv'
precios = leer_precios(nombre_archivo)
pprint('Detalles de la compra al productor: ' + str(camionsito))
pprint('Detalles de la venta: ' + str(precios))

total_verdu = 0
precio_total = 0
a = 0

while a < len(camionsito):
    total_verdu += float(precios[camionsito[a]['nombre']]) * int(camionsito[a]['cajones'])
    precio_total += int(camionsito[a]['cajones']) * float(camionsito[a]['precio'])

    a += 1

balance = total_verdu - precio_total
print('El costo de compra al productor fue de ' + str(precio_total) + ' y el monto de venta de esos productos fue de ' + str(total_verdu))
print('La ganancia fue de ' + str(balance))

def hacer_informes(camionsito, precios):
    camioneta = camionsito.copy()
    listón = []
    for i in range(len(camioneta)):
        camioneta[i]['cambio'] = float(precios[camioneta[i]['nombre']]) - camioneta[i]['precio']
        lista = []
        for keys in camioneta[i]:
            lista.append(camioneta[i][keys])

        listón.append(tuple(lista))
    #print(listón)
    return listón

print(hacer_informes(camionsito, precios))

headers = ('Nombre', 'Cajones', 'Precio', 'Cambio')
lineas = '---------- ---------- ---------- ----------'
print(f'{headers[0]:>10s} {headers[1]:>10s} {headers[2]:>10s} {headers[3]:>10s}')
print(lineas)
informe = hacer_informes(camionsito, precios)
for nombre, cajones, precio, cambio in informe:
    print(f'{nombre:>10s} {cajones:>10d} {"$":>5s}{precio:<10.2f} {cambio:<10.2f}')

03 - Trabajando con datos/tabla_informe.py


import numpy as np
import pandas as pd
import time
from sklearn import preprocessing
from models.Label_Encoder import Label_Encoder
from sklearn import metrics

class KNN:
    def __init__(self, k):
        self.k = k

    def find_euclidean_distance(self,X_train, X_test):
        # the length of the datasets
        test_length = X_test.shape[0]
        train_length = X_train.shape[0]

        # Initilizing distance vector
        distance = np.zeros((test_length, train_length))

        # Computing distance between two points
        for i in range(test_length):
            for j in range(train_length):
                distance[i, j] = sum(abs(X_test[i, :] - X_train[j, :]) ** 2)
                distance[i, j] = (distance[i, j]) ** (1 / 2)

        return distance

    def fit(self, X_train, X_test):
        distances = self.find_euclidean_distance(X_train, X_test)
        return distances

    def predict(self, distance):
        test_length = distance.shape[0]  # distance length
        y_predicted = np.zeros(test_length)
        freq = []
        for i in range(test_length):
            y_indices= distance[i, :]
            y_indices = np.argsort(y_indices)
            k_closest_classes = y_train[y_indices[:self.k]]
            k_closest_classes =k_closest_classes.astype(int)#coverting it to int type
            frequencies = np.bincount(k_closest_classes.flatten())  # counting the frequency of the classes
            y_predicted[i] = np.argmax(frequencies)  # the most frequent class
            freq.append(frequencies/(self.k +1))
        return y_predicted, freq

    def confusion_matrix(self,y_pred, y_test):
     #contructing empty confusion matrix with shape(7,7)
        conf_matrix = np.zeros((6,6))
        for i, j in zip(y_test, y_pred):
            conf_matrix[i-1][j-1] = conf_matrix[i-1][j-1] + 1

        return conf_matrix

if __name__ == '__main__':
    df = pd.read_csv(r'C:\Users\sevki\PycharmProjects\485data\Data\weather_dataset1')
    df =  df.sample(frac=1)
    # print(df['Severity'].unique())
    df = df.drop('Unnamed: 0', 1)

    le = preprocessing.LabelEncoder()

    df['Severity'] = df['Severity'].astype(str)
    le.fit(df['Severity'])
    df['Severity'] = le.transform(df['Severity'])

    x_train = df.iloc[:6000, :]
    x_test = df.iloc[6000:8000, :]
    x_validation = df.iloc[8000:, :]

    y_train = x_train['Labels']
    y_test = x_test['Labels']
    y_validation = x_validation['Labels']
    X_train = x_train.drop('Labels', 1)
    X_validation = x_validation.drop('Labels', 1)
    X_test = x_test.drop('Labels', 1)

    X_train = preprocessing.StandardScaler().fit_transform(X_train)
    X_test = preprocessing.StandardScaler().fit_transform(X_test)
    X_validation = preprocessing.StandardScaler().fit_transform(X_validation)

    # X_train = X_train.to_numpy()
    # X_test = X_test.to_numpy()
    # X_validation = X_validation.to_numpy()

    y_train = Label_Encoder(y_train)
    y_test = Label_Encoder(y_test)
    y_validation = Label_Encoder(y_validation)

    #initilizing model
    knn = KNN(k=7)
    #training
    Start = time.time()
    distance = knn.fit(X_train, X_validation)
    print('Training Time: ',time.time() - Start)

    #predicting
    Start = time.time()
    y_pred,freq = knn.predict(distance)
    # print(freq)
    import pickle
    with open("../scractess/test1.txt", "wb") as fp:  # Pickling
        pickle.dump(freq, fp)

    print('Prediction Time: ', time.time() - Start)
    y_pred=y_pred.reshape(y_validation.shape[0],1)

    y_pred=y_pred.astype(int)
    y_pred = np.concatenate(y_pred)
    y_validation = y_validation.astype(int)
    #accuracy
    print(np.sum(y_pred == y_validation.flatten()) /y_validation.shape[0])
    print(knn.confusion_matrix(y_pred, y_validation))

    # cnf_matrix = metrics.confusion_matrix(y_pred, y_test)

    df = pd.DataFrame(y_pred)

    # saving the dataframe
    df.to_csv('knn_predict.csv')


from datetime import datetime
from calendar import monthrange
import json

class planer():
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    months = ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"]
    cleaners = ["Abdurrahman", "Adnan"]
    def create_plan():
        plan = []
        p = 0
        id = 0
        for m in range(datetime.utcnow().month-1, 12):
            mi = monthrange(datetime.utcnow().year, m+1)
            for i in range(1, mi[1]+1):
                id += 1
                data = {
                    "id": id,
                    "cleaner": planer.cleaners[p],
                    "date": f"{i}-{m+1}-{datetime.utcnow().year}",
                    "day": planer.days[datetime(datetime.utcnow().year, m+1, i).weekday()],
                    "cleaned": False,
                    "person_id": p
                }
                plan.append(data)
                if p == 1:
                    p = 0
                    continue
                p += 1
        with open("plan.json", "w", encoding="utf-8") as f:
            json.dump(plan, f, indent=4, ensure_ascii=False)

    def update_plan():
        with open("plan.json", "r",encoding="utf-8") as f:
            plan = json.loads(f.read())
            x = ""
            p = None
            for i in plan:
                if p == None:
                    p = (True, i["person_id"]) if i["cleaned"] == True else (False, i["person_id"])
                    continue
                if p[0] == False:
                    i["cleaner"] = planer.cleaners[p[1]]
                    for t in plan:
                        if t["date"] == f"{datetime.utcnow().day}-{datetime.utcnow().month}-{datetime.utcnow().year}":
                            today = plan.index(t)
                    if plan.index(i) > today:
                        i["cleaner"] = f"({planer.cleaners[p[1]]})"
                    i["person_id"] = p[1]
                elif p[0] == True:
                    if p[1] == len(planer.cleaners)-1:
                        x = planer.cleaners[0]
                    else:
                        x = planer.cleaners[p[1]+1]
                    i["cleaner"] = x
                    i["person_id"] = planer.cleaners.index(x)
                    p = (True, i["person_id"]) if i["cleaned"] == True else (False, i["person_id"])
                if i["cleaned"] == True:
                    p = (True, i["person_id"]) if i["cleaned"] == True else (False, i["person_id"])
        with open("plan.json", "w", encoding="utf-8") as f:
            json.dump(plan,f, indent=4, ensure_ascii=False)

    def get_plan():
        with open("plan.json", "r", encoding="utf-8") as f:
            plan = json.loads(f.read())
            max = len(plan)
            span = (6,5)
            for i in plan:
                if i["date"] == f"{datetime.utcnow().day}-{datetime.utcnow().month}-{datetime.utcnow().year}":
                    w = plan.index(i)
                    id = i["id"]
            print(w, max)
            if w-span[0] >= 0 and w+span[1] < max:
                return plan[w-span[0]:w+span[1]], id
            elif w-span[0] <= 0:
                return plan[0:w+span[1]], id
            elif w+span[1] > max:
                return plan[w-span[0]:max-1],id

    def make_stats():
        with open("plan.json", "r", encoding="utf-8") as f:
            plans = json.loads(f.read())
            c = ["Abdurrahman", "Adnan"]
            p = [0, 0]
            u = [0, 0]
            t = [0, 0]
            r = [0, 0]
            for i in c:
                for plan in plans:
                    if plan["cleaner"] == i:
                        if plan["cleaned"]:
                            p[c.index(i)] += 1
                        else:
                            u[c.index(i)] -= 1
                        t[c.index(i)] += 1
                        r[c.index(i)] = (p[c.index(i)]+u[c.index(i)])/t[c.index(i)]
            ab = planer.cleaner(c[0], p[0], u[0], t[0], r[0])
            ad = planer.cleaner(c[1], p[1], u[1], t[1], r[1])
            return ad, ab

    class cleaner():
        def __init__(self, name, clean, unclean, total, ratio):
            self.name = name
            self.clean = clean
            self.unclean = unclean
            self.total = total
            self.ratio = ratio


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

import os
from setuptools import setup, find_packages

version = '3.0.2.dev0'

tests_require = ['zope.testing', 'Products.PloneTestCase']

setup(name='cciaa.modulistica',
      version=version,
      description="An additional view for Plone folders, to improve"
                  " management of repositories of files",
      long_description=open("README.rst").read() + "\n" + open(
          os.path.join("docs", "HISTORY.txt")).read(),
      classifiers=[
        'Framework :: Plone',
        'Framework :: Plone :: 4.0',
        'Framework :: Plone :: 4.1',
        'Framework :: Plone :: 4.2',
        'Framework :: Plone :: 4.3',
        'Framework :: Plone :: 5.0',
        'Framework :: Plone :: 5.1',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'License :: OSI Approved :: GNU General Public License (GPL)',
        ],
      keywords='plone plonegov form file',
      author='RedTurtle Technology',
      author_email='sviluppoplone@redturtle.it',
      url='http://plone.org/products/cciaa.modulistica',
      license='GPL',
      packages=find_packages(exclude=['ez_setup']),
      namespace_packages=['cciaa', ],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          'Products.CMFPlone',
      ],
      tests_require=tests_require,
      extras_require=dict(test=tests_require),
      entry_points="""
      [z3c.autoinclude.plugin]
      target = plone
      """,
      )


import numpy as np
import os
import cv2

filename = 'video.avi'
frames_per_second = 24.0
my_res = '700p'

# Set resolution for the video capture
# Function adapted from https://kirr.co/0l6qmh
def change_res(cap, width, height):
    cap.set(3, width)
    cap.set(4, height)

# Standard Video Dimensions Sizes
STD_DIMENSIONS =  {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}

# grab resolution dimensions and set video capture to it.
def get_dims(cap, res='1080p'):
    width, height = STD_DIMENSIONS["480p"]#if params of resolution is error then it default set 480p res

    if res in STD_DIMENSIONS:
        width,height = STD_DIMENSIONS[res]
    ## change the current caputre device
    ## to the resulting resolution
    change_res(cap, width, height)
    return width, height

# Video Encoding, might require additional installs
# Type of Codes: http://www.fourcc.org/codecs.php
VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'XVID'),
    #'mp4': cv2.VideoWriter_fourcc(*'H264'),
    'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}

def get_video_type(filename):
    filename, ext = os.path.splitext(filename)#it splits video.avi to video and avi and assign to var
    print(filename)

    if ext in VIDEO_TYPE:
      return  VIDEO_TYPE[ext]

    return VIDEO_TYPE['avi']

cap = cv2.VideoCapture(0)
dims = get_dims(cap, res=my_res)
video_type_cv2 = get_video_type(filename)

out = cv2.VideoWriter(filename,video_type_cv2,frames_per_second,dims)#instead of dims i can also write width height
# dims = get_dims(cap,res=my_res)

while True:
    ret, frame = cap.read()
    out.write(frame)#save video to video.avi file in project folder
    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 12 09:50:59 2020
@author: EmrahSariboz

"""

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import argparse
import pandas as pd
import AdaBoostC
import os
import sys
import timeit
import RandomFC
import BaggingC
from sklearn.svm import SVC

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("classifier", help = "Name of the Classifier")
    parser.add_argument("datasetPassed", help = "Dataset Location")
    args = parser.parse_args()

    classifierName = str(args.classifier)
    dataset = str(args.datasetPassed)

    #pass the dataset varible to if statement
    #example: if (dataset == dataset)
    isfile = os.path.isfile(dataset)

    #Check the existence of dataset and split it
    if isfile:
        df = pd.read_csv(dataset)
        X = df.iloc[:, 0:(df.shape[1]-1)]
        y = df.iloc[:, -1]
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
    else:
        if dataset == "digit":
            #Loading the digits dataset
            digist_dataset = datasets.load_digits()
            X = digist_dataset.data
            y = digist_dataset.target
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
        else:
            print("\nThe provided dataset does not exist!")
            sys.exit()

    #Comparison
    #comparison = SVC(kernel = 'rbf')
    #comparison.fit(X_train, y_train)
    #print(accuracy_score(y_test, comparison.predict(X_test)))

    #If else statemets for each model
    if classifierName == "randomForest":
        start = timeit.default_timer()
        classifier =  RandomFC.RandomForest(X_train, y_train)
        classifier.fit(X_train, y_train)
        prediction = classifier.predict(X_test)
        prediction_training = classifier.predict(X_train);
        stop = timeit.default_timer()
        print('The total runtime of this classifier on ', dataset, ' dataset is: ', stop-start , ' seconds')
        print('\nThe accuracy of random forest classifier on test dataset is: ', accuracy_score(y_test, prediction))
        print('\nThe accuracy of random forest classifier on training dataset is: ', accuracy_score(y_train, prediction_training))
    elif classifierName == "bagging":
        start = timeit.default_timer()
        classifier =  BaggingC.BaggingEns(X_train, y_train)
        classifier.fit(X_train, y_train)
        prediction = classifier.predict(X_test)
        prediction_training = classifier.predict(X_train);
        stop = timeit.default_timer()
        print('The total runtime of this classifier is: ', stop-start , ' seconds')
        print('\nThe accuracy of bagging classifier on test dataset is: ', accuracy_score(y_test, prediction))
        print('\nThe accuracy of bagging classifier on training dataset is: ', accuracy_score(y_train, prediction_training))
    elif classifierName == "adaboost":
        start = timeit.default_timer()
        classifier =  AdaBoostC.AdaBoostEns(X_train, y_train)
        classifier.fit(X_train, y_train)
        prediction = classifier.predict(X_test)
        prediction_training = classifier.predict(X_train);
        stop = timeit.default_timer()
        print('The total runtime of this classifier is: ', stop-start , ' seconds')
        print('\nThe accuracy of adaboost classifier on test dataset is: ', accuracy_score(y_test, prediction))
        print('\nThe accuracy of adaboost classifier on training dataset is: ', accuracy_score(y_train, prediction_training))
    else:
        print('Wrong parameter(s)')

Selected Homework/Ensemble Method Analysis/main.py


from __future__ import division

from util.tf_util import batch_normalization_wrapper
from tensorflow.contrib.keras import backend as K
import tensorflow as tf

kernel_initializer = tf.contrib.keras.initializers.he_normal()
kernel_regularizer = tf.contrib.layers.l2_regularizer(1e-4)

ROW_AXIS = 1
COL_AXIS = 2
CHANNEL_AXIS = 3

def resnet_18(input_tensor, repetitions=(2, 2, 2, 2)):
  """Builds a custom ResNet like architecture.

  Args:
  input_shape: The input tensor with shape (nb_rows, nb_cols, nb_channels)
  num_outputs: The number of outputs at final softmax layer
  block_fn: The block function to use. This is either `basic_block` or `bottleneck`.
  The original paper used basic_block for layers < 50
  repetitions: Number of repetitions of various block units.
  At each block unit, the number of filters are doubled and the input size is halved

  Returns:
  The keras `lip_model.Model`.
  """

  block = input_tensor
  filters = 64
  for i, r in enumerate(repetitions):
    block = _residual_block(filters=filters, repetitions=r,
                            is_first_layer=(i == 0))(block)
    filters *= 2

  # Last activation
  block = _bn_relu(block)

  # Classifier block
  block_shape = K.int_shape(block)
  pool2 = tf.layers.AveragePooling2D(pool_size=(block_shape[ROW_AXIS], block_shape[COL_AXIS]),
                           strides=(1, 1))(block)

  flatten1 = tf.layers.Flatten()(pool2)

  return flatten1

def _residual_block(filters, repetitions, is_first_layer=False):
  """Builds a residual block with repeating bottleneck blocks.
  """
  def f(input):

    init_strides = (1, 1)
    if not is_first_layer:
      init_strides = (2, 2)
    input = _basic_block(filters=filters, init_strides=init_strides, is_first_block=True,
                         is_first_block_of_first_layer=(is_first_layer))(input)

    init_strides = (1, 1)
    for i in range(repetitions-1):
      input = _basic_block(filters=filters, init_strides=init_strides, is_first_block=False,
                           is_first_block_of_first_layer=False)(input)
    return input

  return f

def _basic_block(filters, init_strides=(1, 1), is_first_block_of_first_layer=False, is_first_block=False):
  """Basic 3 X 3 convolution blocks for use on resnets with layers <= 34.
  Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf
  """
  def f(input):

    if is_first_block and not is_first_block_of_first_layer:
      activation = _bn_relu(input)
      conv1 = _only_conv(filters=filters, kernel_size=(3, 3),
                            strides=init_strides)(activation)
      residual = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv1)
      return _shortcut(activation, residual)

    if is_first_block_of_first_layer:
      # don't repeat bn->relu since we just did bn->relu->maxpool
      conv1 = tf.layers.conv2d(input, filters=filters, kernel_size=(3, 3), use_bias=False,
                     strides=init_strides,
                     padding="same",
                     kernel_initializer=kernel_initializer,
                     kernel_regularizer=kernel_regularizer )
    else:
      conv1 = _bn_relu_conv(filters=filters, kernel_size=(3, 3),
                            strides=init_strides)(input)

    residual = _bn_relu_conv(filters=filters, kernel_size=(3, 3))(conv1)
    return _shortcut(input, residual)

  return f

def _shortcut(input, residual):
  """Adds a shortcut between input and residual block and merges them with "sum"
  """
  # Expand channels of shortcut to match residual.
  # Stride appropriately to match residual (width, height)
  # Should be int if network architecture is correctly configured.
  input_shape = K.int_shape(input)
  residual_shape = K.int_shape(residual)
  stride_width = int(round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS]))
  stride_height = int(round(input_shape[COL_AXIS] / residual_shape[COL_AXIS]))
  equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[CHANNEL_AXIS]

  shortcut = input
  # 1 X 1 conv if shape is different. Else identity.
  if stride_width > 1 or stride_height > 1 or not equal_channels:
    shortcut = tf.layers.conv2d(input, filters=residual_shape[CHANNEL_AXIS], use_bias=False,
                      kernel_size=(1, 1),
                      strides=(stride_width, stride_height),
                      padding="valid",
                      kernel_initializer=kernel_initializer,
                      kernel_regularizer=kernel_regularizer)

  return tf.add(shortcut, residual)

def _conv_bn_relu(filters, kernel_size, strides=(1,1), padding='same', kernel_regularizer=kernel_regularizer):
  """Helper to build a conv -> BN -> relu block
  """
  def f(input):
    conv = tf.layers.conv2d(input, filters=filters, kernel_size=kernel_size, use_bias=False,
                  strides=strides, padding=padding,
                  kernel_initializer=kernel_initializer,
                  kernel_regularizer=kernel_regularizer)
    return _bn_relu(conv)

  return f

def _bn_relu_conv(filters, kernel_size, strides=(1,1), padding='same', kernel_regularizer=kernel_regularizer):
  """Helper to build a BN -> relu -> conv block.
  This is an improved scheme proposed in http://arxiv.org/pdf/1603.05027v2.pdf
  """

  def f(input):
    activation = _bn_relu(input)
    return tf.layers.conv2d(activation, filters=filters, kernel_size=kernel_size, use_bias=False,
                  strides=strides, padding=padding,
                  kernel_initializer=kernel_initializer,
                  kernel_regularizer=kernel_regularizer)

  return f

def _bn_relu(input):
  """Helper to build a BN -> relu block
  """
  norm = batch_normalization_wrapper(input, axis=CHANNEL_AXIS, scale=True)
  return tf.nn.relu(norm)

def _only_conv(filters, kernel_size, strides=(1,1), padding='same', kernel_regularizer=kernel_regularizer):
  """Helper to build a BN -> relu -> conv block.
  This is an improved scheme proposed in http://arxiv.org/pdf/1603.05027v2.pdf
  """

  def f(input):

    assert strides[0] == 2
    padded = tf.contrib.keras.layers.ZeroPadding2D(padding=(1, 1))(input)
    return tf.layers.conv2d(padded, filters=filters, kernel_size=kernel_size, use_bias=False,
                  strides=strides, padding='valid',
                  kernel_initializer=kernel_initializer,
                  kernel_regularizer=kernel_regularizer )

  return f


import json
from coincurve import PrivateKey as ECPrivateKey
from core.crypto import Point, ripemd160_sha256
from core.format import (
    bytes_to_wif, public_key_to_address, public_key_to_coords, wif_to_bytes,
    public_key_to_segwit_address
)
from core.api import NetworkAPI
from core.meta import Unspent
from core.transaction import (
    calc_txid, create_new_transaction, sanitize_tx_data, sign_tx,
    deserialize, address_to_scriptpubkey
)
from core.constants import OP_0, OP_PUSH_20

from core.utils import bytes_to_hex

def wif_to_key(wif):
    private_key_bytes, compressed, version = wif_to_bytes(wif)

    if compressed:
        return PrivateKey.from_bytes(private_key_bytes)
    else:
        return PrivateKey(wif)

def get_fee_cached():
    return 10

class BaseKey:
    """This class represents a point on the elliptic curve secp256k1 and
    provides all necessary cryptographic functionality. You shouldn't use
    this class directly.

    :param wif: A private key serialized to the Wallet Import Format. If the
                argument is not supplied, a new private key will be created.
                The WIF compression flag will be adhered to, but the version
                byte is disregarded. Compression will be used by all new keys.
    :type wif: ``str``
    :raises TypeError: If ``wif`` is not a ``str``.
    """

    def __init__(self, wif=None):
        if wif:
            if isinstance(wif, str):
                private_key_bytes, compressed, version = wif_to_bytes(wif)
                self._pk = ECPrivateKey(private_key_bytes)
            elif isinstance(wif, ECPrivateKey):
                self._pk = wif
                compressed = True
            else:
                raise TypeError('Wallet Import Format must be a string.')
        else:
            self._pk = ECPrivateKey()
            compressed = True

        self._public_point = None
        self._public_key = self._pk.public_key.format(compressed=compressed)

    @property
    def public_key(self):
        """The public point serialized to bytes."""
        return self._public_key

    @property
    def public_point(self):
        """The public point (x, y)."""
        if self._public_point is None:
            self._public_point = Point(*public_key_to_coords(self._public_key))
        return self._public_point

    def sign(self, data):
        """Signs some data which can be verified later by others using
        the public key.

        :param data: The message to sign.
        :type data: ``bytes``
        :returns: A signature compliant with BIP-62.
        :rtype: ``bytes``
        """
        return self._pk.sign(data)

    def verify(self, signature, data):
        """Verifies some data was signed by this private key.

        :param signature: The signature to verify.
        :type signature: ``bytes``
        :param data: The data that was supposedly signed.
        :type data: ``bytes``
        :rtype: ``bool``
        """
        return self._pk.public_key.verify(signature, data)

    def pub_to_hex(self):
        """:rtype: ``str`` """
        return bytes_to_hex(self.public_key)

    def to_hex(self):
        """:rtype: ``str``"""
        return self._pk.to_hex()

    def to_bytes(self):
        """:rtype: ``bytes``"""
        return self._pk.secret

    def to_der(self):
        """:rtype: ``bytes``"""
        return self._pk.to_der()

    def to_pem(self):
        """:rtype: ``bytes``"""
        return self._pk.to_pem()

    def to_int(self):
        """:rtype: ``int``"""
        return self._pk.to_int()

    def is_compressed(self):
        """Returns whether or not this private key corresponds to a compressed
        public key.

        :rtype: ``bool``
        """
        return True if len(self.public_key) == 33 else False

    def __eq__(self, other):
        return self.to_int() == other.to_int()

class PrivateKey(BaseKey):
    """This class represents a Bitcoin private key. ``Key`` is an alias.

    :param wif: A private key serialized to the Wallet Import Format. If the
                argument is not supplied, a new private key will be created.
                The WIF compression flag will be adhered to, but the version
                byte is disregarded. Compression will be used by all new keys.
    :type wif: ``str``
    :raises TypeError: If ``wif`` is not a ``str``.
    """

    def __init__(self, wif=None):
        super().__init__(wif=wif)

        self.version = 'main'
        self.instance = 'PrivateKey'

        self._address = None
        self._segwit_address = None
        self._scriptcode = None
        self._segwit_scriptcode = None

        self.balance = 0
        self.unspents = []
        self.transactions = []

    @property
    def address(self):
        """The public address you share with others to receive funds."""
        if self._address is None:
            self._address = public_key_to_address(self._public_key,
                                                  version=self.version)
        return self._address

    @property
    def segwit_address(self):
        """The public segwit nested in P2SH address you share with others to
        receive funds."""
        # Only make segwit address if public key is compressed
        if self._segwit_address is None and self.is_compressed():
            self._segwit_address = public_key_to_segwit_address(
                self._public_key, version=self.version)
        return self._segwit_address

    @property
    def scriptcode(self):
        self._scriptcode = address_to_scriptpubkey(self.address)
        return self._scriptcode

    @property
    def segwit_scriptcode(self):
        self._segwit_scriptcode = (OP_0 + OP_PUSH_20
                                   + ripemd160_sha256(self.public_key))
        return self._segwit_scriptcode

    def can_sign_unspent(self, unspent):
        script = bytes_to_hex(address_to_scriptpubkey(self.address))
        if self.segwit_address:
            segwit_script = bytes_to_hex(address_to_scriptpubkey(
                self.segwit_address))
            return unspent.script == script or unspent.script == segwit_script
        else:
            return unspent.script == script

    def to_wif(self):
        return bytes_to_wif(
            self._pk.secret,
            version=self.version,
            compressed=self.is_compressed()
        )

    def get_balance(self):
        """Fetches the current balance by calling
        :func:`~core.PrivateKey.get_unspents` and returns it using
        :func:`~core.PrivateKey.balance_as`.

        :param currency: One of the :ref:`supported currencies`.
        :type currency: ``str``
        :rtype: ``str``
        """
        print(self.get_unspents())
        return self.balance

    def get_unspents(self):
        """Fetches all available unspent transaction outputs.

        :rtype: ``list`` of :class:`~core.meta.Unspent`
        """
        # ToDo: make this optional?
        self.unspents[:] = list(map(
            lambda u: u.set_type('p2pkh' if self.is_compressed() else
                                 'p2pkh-uncompressed'),
            NetworkAPI.get_unspent(self.address)
        ))
        self.unspents += list(map(
            lambda u: u.set_type('np2wkh'),
            NetworkAPI.get_unspent(self.segwit_address)
        ))
        self.balance = sum(unspent.amount for unspent in self.unspents)
        return self.unspents

    def create_transaction(self, outputs, fee=None, absolute_fee=False,
                           leftover=None, combine=True, message=None,
                           unspents=None):  # pragma: no cover
        """Creates a signed P2PKH transaction.

        :param outputs: A sequence of outputs you wish to send in the form
                        ``(destination, amount, currency)``. The amount can
                        be either an int, float, or string as long as it is
                        a valid input to ``decimal.Decimal``. The currency
                        must be :ref:`supported <supported currencies>`.
        :type outputs: ``list`` of ``tuple``
        :param fee: The number of satoshi per byte to pay to miners. By default
                    Bit will poll `<https://bitcoinfees.earn.com>`_ and use a fee
                    that will allow your transaction to be confirmed as soon as
                    possible.
        :type fee: ``int``
        :param leftover: The destination that will receive any change from the
                         transaction. By default Bit will send any change to
                         the same address you sent from.
        :type leftover: ``str``
        :param combine: Whether or not Bit should use all available UTXOs to
                        make future transactions smaller and therefore reduce
                        fees. By default Bit will consolidate UTXOs.
        :type combine: ``bool``
        :param message: A message to include in the transaction. This will be
                        stored in the blockchain forever. Due to size limits,
                        each message will be stored in chunks of 40 bytes.
        :type message: ``str``
        :param unspents: The UTXOs to use as the inputs. By default Bit will
                         communicate with the blockchain itself.
        :type unspents: ``list`` of :class:`~core.meta.Unspent`
        :returns: The signed transaction as hex.
        :rtype: ``str``
        """
        try:
            unspents = unspents or self.get_unspents()
        except ConnectionError:
            raise ConnectionError('All APIs are unreachable. Please provide '
                                  'the unspents to spend from directly.')

        # If at least one input is from segwit the return address is for segwit
        return_address = self.segwit_address if any(
            [u.segwit for u in unspents]) else self.address

        unspents, outputs = sanitize_tx_data(
            unspents,
            outputs,
            fee or get_fee_cached(),
            leftover or return_address,
            combine=combine,
            message=message,
            absolute_fee=absolute_fee,
            version=self.version
        )

        return create_new_transaction(self, unspents, outputs)

    def new_tx(self, outputs, fee=None, absolute_fee=False, leftover=None,
             combine=True, message=None, unspents=None):  # pragma: no cover
        """Creates a signed P2PKH transaction and attempts to broadcast it on
        the blockchain. This accepts the same arguments as
        :func:`~core.PrivateKey.create_transaction`.

        :param outputs: A sequence of outputs you wish to send in the form
                        ``(destination, amount, currency)``. The amount can
                        be either an int, float, or string as long as it is
                        a valid input to ``decimal.Decimal``. The currency
                        must be :ref:`supported <supported currencies>`.
        :type outputs: ``list`` of ``tuple``
        :param fee: The number of satoshi per byte to pay to miners. By default
                    Bit will poll `<https://bitcoinfees.earn.com>`_ and use a fee
                    that will allow your transaction to be confirmed as soon as
                    possible.
        :type fee: ``int``
        :param leftover: The destination that will receive any change from the
                         transaction. By default Bit will send any change to
                         the same address you sent from.
        :type leftover: ``str``
        :param combine: Whether or not Bit should use all available UTXOs to
                        make future transactions smaller and therefore reduce
                        fees. By default Bit will consolidate UTXOs.
        :type combine: ``bool``
        :param message: A message to include in the transaction. This will be
                        stored in the blockchain forever. Due to size limits,
                        each message will be stored in chunks of 40 bytes.
        :type message: ``str``
        :param unspents: The UTXOs to use as the inputs. By default Bit will
                         communicate with the blockchain itself.
        :type unspents: ``list`` of :class:`~core.meta.Unspent`
        :returns: The transaction ID.
        :rtype: ``str``
        """

        tx_hex = self.create_transaction(
            outputs,
            fee=fee,
            absolute_fee=absolute_fee,
            leftover=leftover,
            combine=combine,
            message=message,
            unspents=unspents
        )

        return {"raw_tx": tx_hex, "txid": calc_txid(tx_hex)}

    @classmethod
    def prepare_transaction(cls, address, outputs, compressed=True, fee=None,
                            absolute_fee=False, leftover=None, combine=True,
                            message=None, unspents=None):  # pragma: no cover
        """Prepares a P2PKH transaction for offline signing.

        :param address: The address the funds will be sent from.
        :type address: ``str``
        :param outputs: A sequence of outputs you wish to send in the form
                        ``(destination, amount, currency)``. The amount can
                        be either an int, float, or string as long as it is
                        a valid input to ``decimal.Decimal``. The currency
                        must be :ref:`supported <supported currencies>`.
        :type outputs: ``list`` of ``tuple``
        :param compressed: Whether or not the ``address`` corresponds to a
                           compressed public key. This influences the fee.
        :type compressed: ``bool``
        :param fee: The number of satoshi per byte to pay to miners. By default
                    Bit will poll `<https://bitcoinfees.earn.com>`_ and use a fee
                    that will allow your transaction to be confirmed as soon as
                    possible.
        :type fee: ``int``
        :param leftover: The destination that will receive any change from the
                         transaction. By default Bit will send any change to
                         the same address you sent from.
        :type leftover: ``str``
        :param combine: Whether or not Bit should use all available UTXOs to
                        make future transactions smaller and therefore reduce
                        fees. By default Bit will consolidate UTXOs.
        :type combine: ``bool``
        :param message: A message to include in the transaction. This will be
                        stored in the blockchain forever. Due to size limits,
                        each message will be stored in chunks of 40 bytes.
        :type message: ``str``
        :param unspents: The UTXOs to use as the inputs. By default Bit will
                         communicate with the blockchain itself.
        :type unspents: ``list`` of :class:`~core.meta.Unspent`
        :returns: JSON storing data required to create an offline transaction.
        :rtype: ``str``
        """
        unspents, outputs = sanitize_tx_data(
            unspents or NetworkAPI.get_unspent(address),
            outputs,
            fee or get_fee_cached(),
            leftover or address,
            combine=combine,
            message=message,
            absolute_fee=absolute_fee,
            version='main'
        )

        data = {
            'unspents': [unspent.to_dict() for unspent in unspents],
            'outputs': outputs
        }

        return json.dumps(data, separators=(',', ':'))

    def sign_transaction(self, tx_data, unspents=None):  # pragma: no cover
        """Creates a signed P2PKH transaction using previously prepared
        transaction data.

        :param tx_data: Hex-encoded transaction or output of :func:`~core.Key.prepare_transaction`.
        :type tx_data: ``str``
        :param unspents: The UTXOs to use as the inputs. By default Bit will
                         communicate with the blockchain itself.
        :type unspents: ``list`` of :class:`~core.meta.Unspent`
        :returns: The signed transaction as hex.
        :rtype: ``str``
        """
        try:  # Json-tx-data from :func:`~core.Key.prepare_transaction`
            data = json.loads(tx_data)
            assert(unspents is None)

            unspents = [Unspent.from_dict(unspent) for unspent in data['unspents']]
            outputs = data['outputs']

            return create_new_transaction(self, unspents, outputs)
        except:  # May be hex-encoded transaction using batching:
            try:
                unspents = unspents or self.get_unspents()
            except ConnectionError:
                raise ConnectionError(
                    'All APIs are unreachable. Please provide the unspent '
                    'inputs as unspents directly to sign this transaction.')

            tx_data = deserialize(tx_data)
            return sign_tx(self, tx_data, unspents=unspents)

    @classmethod
    def from_hex(cls, hexed):
        """
        :param hexed: A private key previously encoded as hex.
        :type hexed: ``str``
        :rtype: :class:`~core.PrivateKey`
        """
        return PrivateKey(ECPrivateKey.from_hex(hexed))

    @classmethod
    def from_bytes(cls, bytestr):
        """
        :param bytestr: A private key previously encoded as hex.
        :type bytestr: ``bytes``
        :rtype: :class:`~core.PrivateKey`
        """
        return PrivateKey(ECPrivateKey(bytestr))

    @classmethod
    def from_der(cls, der):
        """
        :param der: A private key previously encoded as DER.
        :type der: ``bytes``
        :rtype: :class:`~core.PrivateKey`
        """
        return PrivateKey(ECPrivateKey.from_der(der))

    @classmethod
    def from_pem(cls, pem):
        """
        :param pem: A private key previously encoded as PEM.
        :type pem: ``bytes``
        :rtype: :class:`~core.PrivateKey`
        """
        return PrivateKey(ECPrivateKey.from_pem(pem))

    @classmethod
    def from_int(cls, num):
        """
        :param num: A private key in raw integer form.
        :type num: ``int``
        :rtype: :class:`~core.PrivateKey`
        """
        return PrivateKey(ECPrivateKey.from_int(num))

    def __repr__(self):
        return '<PrivateKey: {}>'.format(self.address)

Key = PrivateKey