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

"""
For Managing NiFi Templates
"""

from __future__ import absolute_import

import json
import io
from os import access, R_OK, W_OK
from os.path import isfile, dirname
import logging
import six
import xmltodict
from lxml import etree
import nipyapi

log = logging.getLogger(__name__)

__all__ = [
    "list_all_templates", "get_template_by_name", "deploy_template",
    "upload_template", "create_pg_snippet", "create_template",
    "delete_template", "export_template", 'get_template',
    "load_template_from_xml_file_path", "load_template_from_xml_file_stream",
    "load_template_from_xml_string"
]

def get_template_by_name(name):
    """
    DEPRECATED
    Returns a specific template by name, if it exists.

    Note: This function is replaced by get_template

    Args:
        name (str): The Name of the template, exact match required

    Returns:
        (TemplateEntity)

    """
    out = [
        i for i in
        list_all_templates(native=False)
        if
        name == i.template.name
    ]
    if len(out) == 1:
        return out[0]
    return None

def get_template(identifier, identifier_type='name', greedy=False):
    """
    Filters the list of all Templates for a given string in a given field.
    Note that filters are configured in config.py

    Args:
        identifier (str): The string to filter on
        identifier_type (str): The identifier of the field to filter on
        greedy (bool): True for greedy match, False for exact match

    Returns:
        None for no matches, Single Object for unique match,
        list(Objects) for multiple matches

    """
    assert isinstance(identifier, six.string_types)
    assert identifier_type in ['name', 'id']
    with nipyapi.utils.rest_exceptions():
        obj = nipyapi.templates.list_all_templates(native=False)
    if obj:
        return nipyapi.utils.filter_obj(obj, identifier, identifier_type,
                                        greedy)
    return None

def deploy_template(pg_id, template_id, loc_x=0.0, loc_y=0.0):
    """
    Instantiates a given template request in a given process group

    Args:
        pg_id (str): The UUID of the Process Group to deploy into
        template_id (str): The UUID of the Template to deploy. Note that the
            Template must already be uploaded and available to the target
            Process Group
        loc_x (float): The X coordinate to deploy the Template at. Default(0.0)
        loc_y (float): The X coordinate to deploy the Template at. Default(0.0)

    Returns:
        (FlowEntity): The FlowEntity of the Process Group with the deployed
            template

    """
    with nipyapi.utils.rest_exceptions():
        return nipyapi.nifi.ProcessGroupsApi().instantiate_template(
            id=pg_id,
            body=nipyapi.nifi.InstantiateTemplateRequestEntity(
                origin_x=loc_x,
                origin_y=loc_y,
                template_id=template_id
            )
        )

def create_pg_snippet(pg_id):
    """
    Creates a snippet of the targeted process group, and returns the object
    ready to be turned into a Template

    Args:
        pg_id: UUID of the process Group to snippet

    Returns:
        (SnippetEntity): The Snippet Object
    """
    target_pg = nipyapi.canvas.get_process_group(pg_id, 'id')
    new_snippet_req = nipyapi.nifi.SnippetEntity(
        snippet={
            'processGroups': {
                target_pg.id: target_pg.revision
            },
            'parentGroupId':
                target_pg.component.parent_group_id
        }
    )
    with nipyapi.utils.rest_exceptions():
        snippet_resp = nipyapi.nifi.SnippetsApi().create_snippet(
            new_snippet_req
        )
    return snippet_resp

def create_template(pg_id, name, desc=''):
    """
    Creates a Template from a Process Group

    Args:
        pg_id (str): The UUID of the target Process Group
        name (str): The name for the new Template. Must be unique
        desc (optional[str]): The description for the new Template

    Returns:
        (TemplateEntity): The newly created Template

    """
    snippet = create_pg_snippet(pg_id)
    with nipyapi.utils.rest_exceptions():
        new_template = nipyapi.nifi.CreateTemplateRequestEntity(
            name=str(name),
            description=str(desc),
            snippet_id=snippet.snippet.id
        )
        return nipyapi.nifi.ProcessGroupsApi().create_template(
            id=snippet.snippet.parent_group_id,
            body=new_template
        )

def delete_template(t_id):
    """
    Deletes a Template

    Args:
        t_id (str): UUID of the Template to be deleted

    Returns:
        The updated Template object
    """
    with nipyapi.utils.rest_exceptions():
        return nipyapi.nifi.TemplatesApi().remove_template(id=t_id)

def upload_template(pg_id, template_file):
    """
    Uploads a given template xml from from the file system to the given
    Process Group

    Args:
        pg_id (str): The UUID of the Process Group to upload to
        template_file (str): The path including filename to the template file

    Returns:
        (TemplateEntity): The new Template object

    """
    with nipyapi.utils.rest_exceptions():
        this_pg = nipyapi.canvas.get_process_group(pg_id, 'id')
        assert isinstance(this_pg, nipyapi.nifi.ProcessGroupEntity)
    log.info("Called upload_template against endpoint %s with args %s",
             nipyapi.config.nifi_config.api_client.host, locals())
    # Ensure we are receiving a valid file
    assert isfile(template_file) and access(template_file, R_OK), \
        SystemError("File {0} invalid or unreadable".format(template_file))
    # Test for expected Template XML elements
    tree = etree.parse(template_file)
    root_tag = tree.getroot().tag
    if root_tag != 'template':
        raise TypeError(
            "Expected 'template' as xml root element, got {0} instead."
            "Are you sure this is a Template?"
            .format(root_tag)
        )
    t_name = tree.find('name').text
    with nipyapi.utils.rest_exceptions():
        # For some reason identical code that produces the duplicate error
        # in later versions is going through OK for NiFi-1.1.2
        # The error occurs as normal in Postman, so not sure what's going on
        # Will force this error for consistency until it can be investigated
        if nipyapi.templates.get_template(t_name):
            raise ValueError('A template named {} already exists.'
                             .format(t_name))
        nipyapi.nifi.ProcessGroupsApi().upload_template(
            id=this_pg.id,
            template=template_file
        )
        return nipyapi.templates.get_template(
            tree.find('name').text
        )

def export_template(t_id, output='string', file_path=None):
    """
    Exports a given Template as either a string or a file.

    Note that to reimport the Template it must be a file

    Args:
        t_id (str): The UUID of the Template to export
        output (str): 'string' or 'file' to set the export action
        file_path (Optional [str]): The full path including filename to write
            the Template export to

    Returns:
        (str): A String representation of the exported Template XML. Note
            that this may not be utf-8 encoded.

    """
    assert output in ['string', 'file']
    assert file_path is None or isinstance(file_path, six.string_types)
    template = nipyapi.templates.get_template(t_id, 'id')
    assert isinstance(template, nipyapi.nifi.TemplateEntity)
    obj = nipyapi.nifi.TemplatesApi().export_template(t_id)
    assert isinstance(obj, six.string_types)
    assert obj[0] == '<'
    if output == 'string':
        return obj
    if output == 'file':
        assert access(dirname(file_path), W_OK), \
            "File_path {0} is inaccessible or not writable".format(file_path)
        nipyapi.utils.fs_write(obj, file_path)
        return obj

def list_all_templates(native=True):
    """
    Gets a list of all templates on the canvas

    Returns:
        (list[TemplateEntity]): A list of TemplateEntity's
    """
    with nipyapi.utils.rest_exceptions():
        templates = nipyapi.nifi.FlowApi().get_templates()
    if not native:
        if templates:
            return templates.templates
        return None
    return templates

def load_template_from_xml_file_path(file_path):
    """
    Loads a TemplateEntity from an xml file for a
    given path

    Args:
        file_path (str): path to the xml file

    Returns:
        TemplateEntity
    """
    assert isfile(file_path) and access(file_path, R_OK), \
        SystemError("File {0} invalid or unreadable".format(file_path))
    with io.open(file_path, "r", encoding='utf8') as template_file:
        return load_template_from_xml_file_stream(template_file)

def load_template_from_xml_file_stream(file_stream):
    """
    Loads a TemplateEntity from a template xml file

    Args:
        file_stream (io stream): the xml file stream as returned by open

    Returns:
        TemplateEntity
    """
    return load_template_from_xml_string(file_stream.read())

def load_template_from_xml_string(xml_string):
    """
    Loads a TemplateEntity from xml string, as if
    you had read in the xml file to string

    Args:
        xml_string (str): string of xml

    Returns:
        TemplateEntity
    """
    assert isinstance(xml_string, six.string_types)

    json_string = json.dumps(xmltodict.parse(xml_string))
    unset = False
    if nipyapi.config.nifi_config.api_client is None:
        unset = True
        nipyapi.config.nifi_config.api_client = nipyapi.nifi.ApiClient()

    template_entity = nipyapi.utils.load(json_string,
                                         ('nifi', 'TemplateEntity'))
    if unset:
        nipyapi.config.nifi_config.api_client = None
    return template_entity


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('profiles', '0016_delete_defaultavatar'),
    ]

    operations = [
        migrations.AddField(
            model_name='profilesettings',
            name='is_provider',
            field=models.BooleanField(help_text='provider functionality', default=True, verbose_name='I am provider'),
        ),
    ]


import wx
import conf.config as config
import utils.wordActivitiesUtils as nGUtils
import utils.bottomMenu as bottomMenu
from os.path import join
import matchImageCharAnswerList
import Image

class matchPairs():
    def __init__(self, parent, id, mainWin, mainPanel, currentLesson):
        self.mainWin = mainWin
        self.mainPanel = mainPanel
        self.currentLesson = currentLesson

        self.questionList, self.questionImgList, self.answerList, self.answerNepaliList = nGUtils.nextMatchImageCharQuestion(self.currentLesson)
        self.questionImgList = resizeImage(self.questionImgList, 50)

        self.displayPanel = wx.Panel(parent, -1, (0, 0), (800, 600))
        self.displayPanel.SetBackgroundColour(config.backgroundColour)

        self.matchImageCharAnswerList = matchImageCharAnswerList.matchImageCharAnswerList(self.displayPanel, self.questionList, self.questionImgList, self.answerList, self.answerNepaliList, self.currentLesson)

        nextQuestionImg = wx.Bitmap(join(config.buttonsPath,'nextQuestion.png'), wx.BITMAP_TYPE_PNG)
        self.nextQuestionButton = wx.BitmapButton(self.displayPanel, 4, nextQuestionImg, (460,400), style = wx.NO_BORDER)
        self.nextQuestionButton.SetBackgroundColour(config.backgroundColour)
        self.nextQuestionButton.Bind(wx.EVT_BUTTON, self.OnNextQuestion, id=4)

        bottomMenu.bottomMenu([self.displayPanel], parent, self.mainWin, 'threeButton', self.mainPanel)

    def OnNextQuestion(self, event=None):
        self.questionList, self.questionImgList, self.answerList, self.answerNepaliList = nGUtils.nextMatchImageCharQuestion(self.currentLesson)
        self.questionImgList = resizeImage(self.questionImgList, 50)
        self.matchImageCharAnswerList.nextQuestion(self.questionList, self.questionImgList, self.answerList, self.answerNepaliList)

def resizeImage(imageList, resizePercent):
    newImageList = []
    for i in imageList:
        temp = Image.open(i)
        temp = temp.resize(((resizePercent*temp.size[0])/100,(resizePercent*temp.size[1])/100), Image.ANTIALIAS)
        returnNumberImg = wx.EmptyImage(temp.size[0], temp.size[1])
        returnNumberImg.SetData(temp.convert('RGB').tostring())
        returnNumberImg.SetAlphaData(temp.convert("RGBA").tostring()[3::4])
        newImageList.append(returnNumberImg)
    return newImageList


import datetime

from django.test import TestCase
from django.utils import timezone
from django.urls import reverse

from .models import Question

# Create your tests here.
class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() はpub_dateが将来の質問に対してFalseを返却する
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() はpub_dateが24時間以内の質問に対してTrueを返す
        """
        time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
        recent_question = Question(pub_date=time)

def create_question(question_text, days):
    """
    Questionモデルインタスタンスを作成し保存します。
    質問内容は引数question_textを、pub_dateはシステム日付に引数daysを加減算し設定します。
    """
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)

class QuestionIndexViewTests(TestCase):
    def test_no_question(self): # テストメソッド実行時、リセットされた
        """
        質問が存在しない場合、適切なメッセージが表示されること
        """
        response = self.client.get(reverse('polls:index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No polls are available.") # responseに存在しない旨のメッセージが含まれることを確認
        self.assertQuerysetEqual(response.context['last_question_list'], []) # responseのコンテキスト辞書に設定されたQuerySetが空であることを確認

    def test_past_question(self):
        """
        pub_dateが過去の質問は、indexページに表示されること
        """
        create_question(question_text="Past question.", days=-30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question.>']
        ) # responseのコンテキスト辞書に設定されたQuerySetに、モデル登録したインスタンスが設定されていることを確認

    def test_future_question(self):
        """
        pub_dateが将来の質問は、indexページに表示されないこと
        :return:
        """
        create_question(question_text="Future question.", days=30) # pub_dateが30日後のQuestionモデルを登録
        response = self.client.get(reverse('polls:index'))
        self.assertContains(response, "No polls are available.") # responseに文字列"No polls are available"が含まれていることを確認
        self.assertQuerysetEqual(response.context['latest_question_list'], []) # responseのコンテキスト辞書に設定されたQuerySetが空であることを確認

    def test_future_question_and_past_question(self):
        """
        pub_dateが過去と将来の質問が存在する場合、過去の質問のみがindexページに表示されること
        :return:
        """
        create_question(question_text="Past question.", days=30)
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question. >']
        )

    def test_two_past_questions(self):
        """
        pub_dateが過去の質問が複数存在する場合、該当する質問が全てindexページに表示されること
        :return:
        """
        create_question(question_text="Page question 1.", days=-30) # pub_dateが30日前のQuestionモデルを登録
        create_question(question_text="Past question 2.", days=-5) # pub_dateが5日前のQuestionモデルを登録
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question 2.>', '<Question: Past question 1.>']
        ) # responseのコンテキスト辞書に設定されたQuerySetに、モデル登録したインスタンスが全て設定されていることを確認

class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
        """
        pub_dateが将来の質問の場合は、404 Not Foundエラーが表示されること
        :return:
        """
        future_question = create_question(question_text='Future question.', days=5)
        url = reverse('polls:detail', args=(future_question.id,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_past_question(self):
        """
        pub_dateが過去の質問は、質問のテキストが表示されること
        :return:
        """
        past_question = create_question(question_text='Past Question.', days=-5)
        url = reverse('polls:detail', args=(past_question))
        response = self.client.get(url)
        self.assertContains(response, past_question.question_text) # モデルに登録した質問のテキストと同じ文字列がresopnseに含まれていることを確認


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

from setuptools import setup, find_packages

with open('README.md') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='preseries_api',
    version='0.1.0',
    description='Preseries API for Python & Examples',
    long_description=readme,
    author='Javier Alperte',
    author_email='alperte@preseries.com',
    url='https://github.com/preseries/api_examples',
    license=license,
    setup_requires=[],
    namespace_packages=['preseries'],
    packages=find_packages('src', exclude=['tests', 'docs']),
    package_dir={'': 'src'},
    include_package_data=True,
    install_requires=[
        'openpyxl==2.4.8',
        'python-Levenshtein==0.12.0',
        'requests==2.18.4',
        'httplib2==0.10.3',
        'xlrd==1.1.0',
        'xlwt==1.3.0'
    ]
)


## Exercise 12 :
# Implement Batch Gradient Descent with early stopping
# for Softmax Regression (without using Scitkit-Learn).

from sklearn import datasets
import soft_max_scratch
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

## Converts class labels to one hot encodings
def convert_to_one_hot(labels):

    class_count = len(set(labels))
    one_hot = np.zeros((len(labels),class_count))

    for i in range(len(labels)):
        one_hot[i][labels[i]] = 1

    return one_hot

def main():
    iris = datasets.load_iris()
    data = iris["data"]
    labels_one_hot = convert_to_one_hot(iris["target"])

    ## shuffle training data and labels (Gradient Descent performs better this way)
    rand = np.random.permutation(len(data))
    X_train, X_test, y_train, y_test = train_test_split(data[rand],
                                                        labels_one_hot[rand],
                                                        test_size = .33)

    soft_clf = soft_max_scratch.SoftMaxClassifier(learning_rate = 0.05,
                                                max_iter = 40000,
                                                minimum_step_size = 0.0001,
                                                early_stopping = True,
                                                n = 2000)
    soft_clf.train(X_train,y_train)

    y_pred = soft_clf.predict(X_test)

    print(accuracy_score(y_test,y_pred))

if __name__ == "__main__":
    main()

Chapter 4 - Training/chapter_4_exercise_12.py


import struct
import matplotlib.pyplot as plt
import numpy as np

def read(file):
    with open(file, "rb") as f:
        buff = f.read()
        print(len(buff))
        data = struct.unpack("%dB" % (108000 * 10), buff)
        data = np.array(data).reshape(10, 600, 180)
        return data

if __name__ == "__main__":
    file = "c_0.35_0.02_180_600_108000_1_100.bin"
    data = read(file)
    for frame in data:
        plt.imshow(frame.T, origin="lower")
        plt.show()
        plt.close()


import io
import os
import textwrap

from PIL import Image, ImageDraw, ImageFont

from userge import Message, userge

@userge.on_cmd(
    "slet",
    about={
        "header": "Get a Sticker",
        "description": "Generates Sticker with provided text",
        "usage": "{tr}slet [text | reply]",
        "examples": "{tr}slet DeadSun",
    },
    allow_via_bot=False,
)
async def sticklet(message: Message):
    sticktext = message.input_or_reply_str
    if not sticktext:
        await message.edit("**Bruh** ~`I need some text to make sticklet`")
        return
    await message.delete()
    if message.reply_to_message:
        reply_to = message.reply_to_message.message_id
    else:
        reply_to = message.message_id
    # https://docs.python.org/3/library/textwrap.html#textwrap.wrap

    sticktext = find_optimal_wrap(sticktext)
    sticktext = "\n".join(sticktext)

    image = Image.new("RGBA", (512, 512), (255, 255, 255, 0))
    draw = ImageDraw.Draw(image)
    fontsize = 230
    font = ImageFont.truetype("resources/Roboto-Regular.ttf", size=fontsize)

    while True:
        current_size = draw.multiline_textsize(
            sticktext, font=font, stroke_width=6, spacing=-10
        )

        if current_size[0] <= 512 and current_size[1] <= 512 - 64:
            break

        fontsize -= 3
        font = ImageFont.truetype("resources/Roboto-Regular.ttf", size=fontsize)
    width, height = draw.multiline_textsize(
        sticktext, font=font, stroke_width=6, spacing=-10
    )
    image = Image.new("RGBA", (512, height + 64), (255, 255, 255, 0))
    draw = ImageDraw.Draw(image)
    draw.multiline_text(
        (int((512 - width) / 2), 0),
        sticktext,
        font=font,
        fill="white",
        stroke_width=6,
        stroke_fill="black",
        spacing=-10,
    )
    bbox = image.getbbox()
    image = image.crop((0, bbox[1], 512, bbox[3]))

    image_name = io.BytesIO()
    image_name.name = "sticker.webp"
    image.save(image_name, "WebP")
    image_name.seek(0)

    await userge.send_sticker(
        chat_id=message.chat.id, sticker=image_name, reply_to_message_id=reply_to
    )

    # cleanup
    try:
        os.remove(image_name)
    except Exception:
        pass

def find_optimal_wrap(text):
    chicken_wrap = int(len(text) / 18) or 20
    wrapped_text = textwrap.wrap(text, width=chicken_wrap)

    while len(wrapped_text) * 3 > chicken_wrap:
        chicken_wrap += 1
        wrapped_text = textwrap.wrap(text, width=chicken_wrap)

    return wrapped_text


import pygame

from Util.window import Window

def text_ob(text, font, color):
    textforScreen = font.render(text, True, color)
    return textforScreen, textforScreen.get_rect()

def game_over():
    """"Create Game Over Screen"""
    WIN = Window.WIN
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    end = True

    while end:
        for event in pygame.event.get():
            print(event)

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        WIN.fill(BLACK)
        text = pygame.font.SysFont('Times New Roman', 100)
        Text, TextRect = text_ob("Game Over", text, WHITE)
        TextRect.center = ((Window.WIDTH / 2), (Window.HEIGHT / 2))
        WIN.blit(Text, TextRect)
        pygame.display.update()


"""Clear Statement --> For each user we need to get all possible combinations of 3 sequences
Then from those possible 3 sequences we need to return the 3 sequence which has been visited by maximum
number of users.
1) Create a dictionary
{
    Joe:[home,about,career],
    Mary:[home,about,career],
    James:[home,cart,maps,home]
}
2) For each value in the above dictionary, create all possible combinations and append each combination
into a list and then apply counter on that list which will give count of each element in that list
i.e count of each 3 sequence. Return the 3 sequence having maximum count by using the most_common method
from Counter class
"""

from collections import defaultdict,Counter
from itertools import combinations
class Solution(object):
    def mostVisitedPattern(self, username, timestamp, website):
        """
        :type username: List[str]
        :type timestamp: List[int]
        :type website: List[str]
        :rtype: List[str]
        """
        data_dict = defaultdict(list)
        for i in range(len(username)):
            data_dict[username[i]].append(website[i])
        val_lst = list(data_dict.values())
        temp_lst = []
        for i in range(len(val_lst)):
            comb = list(combinations(val_lst[i],3))
            for val in comb:
                temp_lst.append(val)
        c = Counter(temp_lst)
        return c.most_common(1)[0][0]

username = ["joe","joe","joe","james","james","james","james","mary","mary","mary"]
timestamp = [1,2,3,4,5,6,7,8,9,10]
website = ["home","about","career","home","cart","maps","home","home","about","career"]

s = Solution()
print(s.mostVisitedPattern(username,timestamp,website))


# url_data
# https://docs.python.org/3/library/urllib.parse.html

import requests
from datetime import datetime
from urllib.parse import urlparse

class urlObject:
    def __init__(self, url,AR=False):
        super().__init__()
        self.url = url

        '''
        Parse url, decomposing in elements, preparating for test
        '''
        results = urlparse(self.url)

        self.scheme = results.scheme
        self.netloc = results.netloc
        self.path = results.path
        self.params = results.params
        self.fragment = results.fragment

        '''
        Test URL, check if url is alive (200) or not, or redirect (301). Decomposing the url's information in
        fields returned
        '''
        if self.scheme == '':
            url2Test = 'http://'+self.path
        else:
            url2Test = self.scheme+'://'+self.netloc

        d = datetime.now()
        self.datetime = str(d)
        self.urlTested = ''
        self.headers = ''
        try:
            r = requests.get(url2Test, allow_redirects=AR,timeout=1)
            #self.isAlive = True
            #self.is_permanent_redirect = r.is_permanent_redirect
            self.status_code = r.status_code
            self.urlTested = r.url
            self.headers = r.headers
        except TimeoutError:
            #self.isAlive = False
            print('Error Timeout:  '+url2Test)
            self.status_code = 0
            self.urlTested = url2Test
        except ConnectionError:
            #self.isAlive = False
            print('Error ConnectionError:  '+url2Test)
            self.status_code = -1
            self.urlTested = url2Test
        #except TooManyRedirects:
            #self.isAlive = False
        #    print('Error TooManyRedirects:  '+url2Test)
        #    self.status_code = -2
        except:
            print('Error:  '+url2Test)
            self.status_code = -2
            self.urlTested = url2Test

        print (self.url+' status code --'+str(self.status_code))


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

"""
Skeleton for first part of the blob-detection coursework as part of INF250
at NMBU (Autumn 2017).
"""

__author__ = "Yngve Mardal Moe"
__email__ = "yngve.m.moe@gmail.com"

import numpy as np
import matplotlib.pyplot as plt

def threshold(image, th=None):
    """Returns a binarised version of given image, thresholded at given value.

    Binarises the image using a global threshold `th`. Uses Otsu's method
    to find optimal thrshold value if the threshold variable is None. The
    returned image will be in the form of an 8-bit unsigned integer array
    with 255 as white and 0 as black.

    Parameters:
    -----------
    image : np.ndarray
        Image to binarise. If this image is a colour image then the last
        dimension will be the colour value (as RGB values).
    th : numeric
        Threshold value. Uses Otsu's method if this variable is None.

    Returns:
    --------
    binarised : np.ndarray(dtype=np.uint8)
        Image where all pixel values are either 0 or 255.
    """
    # Setup
    shape = np.shape(image)
    binarised = np.zeros([shape[0], shape[1]], dtype=np.uint8)

    if len(shape) == 3:
        image = image.mean(axis=2)
    elif len(shape) > 3:
        raise ValueError('Must be at 2D image')

    if th is None:
        th = otsu(image)
    # Start thresholding

    #for row in range(len(image)):
    #    for element in range(len(image[row])):
    #        binarised[row][element] = 255*(image[row][element]>=th)

    binarised = [[255*(image[row][element]>=th) for element in range(len(image[row]))]for row in range(len(image))]

    return binarised

def histogram(image):
    """Returns the image histogram with 256 bins.
    """
    # Setup
    shape = np.shape(image)
    histogram = np.zeros(256)

    if len(shape) == 3:
        image = image.mean(axis=2)
    elif len(shape) > 3:
        raise ValueError('Must be at 2D image')

    # Start to make the histogram

    for row in range(len(image)):
        for element in range(len(image[row])):
            wot = int(image[row][element])
            histogram[wot] += 1

    return histogram

def otsu(image):
    """Finds the optimal thresholdvalue of given image using Otsu's method.
    """
    hist = histogram(image)
    th = 0
    cum_hist = [0 for i in range(len(hist))]
    cum_hist[0] = hist[0]

    for i in range(1,len(hist)):
        cum_hist[i] = hist[i] + cum_hist[i-1]

    ## WRITE YOUR CODE HERE
    levels = range(256)

    sum_values = cum_hist[-1]

    t_vals = dict()

    for t in range(len(levels)):

        lower_sum = cum_hist[t]
        higher_sum = cum_hist[-1] - lower_sum

        if lower_sum == 0 or higher_sum == 0: # sanity check
            continue

        Wb=lower_sum/sum_values
        Wf=higher_sum/sum_values

        mean_b=sum([lev*hist[lev]  for lev in levels[:t+1]])/lower_sum
        mean_f=sum([lev*hist[lev]  for lev in levels[t+1:]])/higher_sum

        t_vals[t] = Wb*Wf*(mean_f-mean_b)**2

    th = max(t_vals, key=t_vals.get)
    return th

if __name__ == "__main__":
    img = plt.imread("bie_threshold.jpeg",format="jpeg")
    new_img = threshold(img)
    plt.imsave("biw_theshold_bin.jpeg",new_img,cmap="gray")
    plt.imshow(new_img,cmap="gray")
    plt.show()


import motor.motor_asyncio
import datetime
from TG_ROBOT import MONGO_DB_URI

class Database:
    def init(self, uri, database_name):
        self._client = motor.motor_asyncio.AsyncIOMotorClient(uri)
        self.db = self._client[database_name]
        self.col = self.db.users

    async def add_chat_list(self, chat_id, ch_id=None):
        get_chat = await self.is_chat_exist(chat_id)
        if get_chat:
            chat_list = list(get_chat.get("chats"))
            if ch_id != None and int(ch_id) in chat_list:
                return True, f"{ch_id} already in white list."
            elif ch_id is not None:
                chat_list.append(int(ch_id))
                await self.col.update_one(
                    {"id": chat_id}, {"$set": {"chats": chat_list}}
                )
                return True, f"{ch_id}, added into white list"
        a_chat = {"id": int(chat_id), "chats": [ch_id]}
        await self.col.insert_one(a_chat)
        return False, " "

    def new_user(self, id):
        return dict(
            id=id,
            join_date=datetime.date.today().isoformat(),
            notif=True,
        )

    async def is_user_exist(self, id):
        user = await self.col.find_one({"id": int(id)})
        return True if user else False

    async def total_users_count(self):
        count = await self.col.count_documents({})
        return count

    async def get_all_users(self):
        all_users = self.col.find({})
        return all_users

    async def set_notif(self, id, notif):
        await self.col.update_one({"id": id}, {"$set": {"notif": notif}})

    async def get_notif(self, id):
        user = await self.col.find_one({"id": int(id)})
        return user.get("notif", False)

    async def get_all_notif_user(self):
        notif_users = self.col.find({"notif": True})
        return notif_users

    async def total_notif_users_count(self):
        count = await self.col.count_documents({"notif": True})
        return count

    async def delete_user(self, user_id):
        await self.col.delete_many({"id": int(user_id)})

    async def is_chat_exist(self, id):
        user = await self.col.find_one({"id": int(id)})
        return user if user else False

    async def get_chat_list(self, chat_id):
        get_chat = await self.is_chat_exist(chat_id)
        if get_chat:
            return get_chat.get("chats", [])
        else:
            return False

    async def del_chat_list(self, chat_id, ch_id=None):
        get_chat = await self.is_chat_exist(chat_id)
        if get_chat:
            chat_list = list(get_chat.get("chats"))
            if ch_id != None and ch_id in chat_list:
                chat_list.remove(int(ch_id))
                await self.col.update_one(
                    {"id": chat_id}, {"$set": {"chats": chat_list}}
                )
                return True, f"{ch_id}, removed from white list"
            elif int(ch_id) not in chat_list:
                return True, f"{ch_id}, not found in white list."

remdb = Database(MONGO_DB_URI, "whitelist_chats")


# 1) smoothing to remove noise from the image
# 2) homogeneous filter is the most simple filter each output pixel is the mean of its kernel neighbours
# 3)  in image processing a kernel, convolution matrix or mask is a small matrix , it is used to blurring,sharpening
# ,embossing,edge detection and more.

# 4) in 1 D signals images can be filtered with various low pass filters (LPF) high pass filters (HPF)
# 5) LPF- helps in removing noise from the imaage
# 6) HPF - helps in finding edges in the images
# 7) guassian filter is nothing but using different-weight-kernel, in both x and y direction
# 8) median filter is something that replaces each pixel value with the median of its neighbouring pixel
# this method is great when dealing with salt and pepper noise
# 9) bilateral filter-A bilateral filter is a non-linear, edge-preserving, and noise-reducing smoothing
# filter for images. It replaces the intensity of each pixel with a weighted average of intensity values
# from nearby pixels. (used when u have to preserve edges)
import cv2        # bgr format
import matplotlib.pyplot as plt   # rgb format
import numpy as np

#img=cv2.imread("opencv-logo.png")
img=cv2.imread("class.png")
#img=cv2.imread("g_blur.jpg")
#cv2.imshow("image",img)
#img=cv2.imread("saltnpepper.jpg")
#img=cv2.imread("lena.jpg")
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
kernel=np.ones((5,5),np.float32)/25
dst=cv2.filter2D(img,-1,kernel)
blur=cv2.blur(img,(5,5))  #averaging
gblur=cv2.GaussianBlur(img,(5,5),0) # inner values have more weight and outer values will be lesser in matrix
median=cv2.medianBlur(img,3) # 3/ kernel size must always be odd and 1 will give original image
bilat=cv2.bilateralFilter(img,9,75,75) #remove noise and keep edges sharp
title=["OG","2D","blur","gblur","median","bilateralfilter"]
images=[img,dst,blur,gblur,median,bilat]
for i in range(len(images)):
    plt.subplot(2,3,i+1)
    plt.imshow(images[i],"gray")
    plt.title(title[i])
    plt.xticks([])
    plt.yticks([])

plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()


from pathlib import Path
import yaml
#import ruamel.yaml
import src.common.processing_vars as var
import os.path
#import ruamel.yaml as yaml
def write_database_file(self,d:dict, ignore_existing:bool=False):
    yml_path = 'snowflake/data/' + d['DATABASE_NAME_SANS_ENV'] + '/database.yml'

    data = self.create_parent_load_data(yml_path)

    #data['version']="2.1"
    data['DATA_RETENTION_TIME_IN_DAYS']=self.choose_value_string(data, 'DATA_RETENTION_TIME_IN_DAYS', d,'DATA_RETENTION_TIME_IN_DAYS' , var.EMPTY_STRING)
    #data['COMMENT']=db['COMMENT'] if (db['COMMENT'] is not None and db['COMMENT'] != '') else var.EMPTY_STRING
    #print('ddidididididididididididid')
    #print(data['COMMENT'])
    #print('ddidididididididididididid')
    data['COMMENT'] = self.choose_value_string(data, 'COMMENT', d,'COMMENT', var.EMPTY_STRING)
    data['OWNER']=self.choose_value_string(data, 'OWNER', d,'OWNER' , var.EMPTY_STRING)

    if 'TAGS' in d and d['TAGS'] != []:
        if 'TAGS' in data:
            data['TAGS'] = self.choose_list_objects(d['TAGS'], data['TAGS'])
        else:
            data['TAGS'] = d['TAGS']
    else:
        data['TAGS']=var.EMPTY_STRING

    if 'GRANTS' in d and d['GRANTS'] != []:
        if 'GRANTS' in data:
            data['GRANTS'] = self.choose_list_objects(d['GRANTS'], data['GRANTS'])
        else:
            data['GRANTS'] = d['GRANTS']
    else:
        data['GRANTS']=var.EMPTY_STRING

    #ryaml = ruamel.yaml.YAML(typ=['rt', 'string'])
    #yaml_string = ryaml.dump_to_string(data)
    yaml_string = yaml.safe_dump(data, sort_keys=False)
    #yaml_string_converted = self.replace_jinja_ref_string(yaml_string)
    #yaml_string_converted = yaml_string_converted.replace("'"+var.EMPTY_STRING+"'",'')
    #yaml_str = yaml.dump(data, default_flow_style=False,indent=4).replace(r"''", '') #ruamel version
    yaml_string_converted = self.convert_special_characters_back_in_file(yaml_string)
    db_file_exists = os.path.isfile(yml_path)
    if not(db_file_exists and ignore_existing):
        with open(yml_path, "w+") as f:
            f.write(yaml_string_converted)

    #with Path(yml_path).open("w", encoding="utf-8") as f:
    #    yaml.dump(data,f,default_style=None,default_flow_style=False, sort_keys=False)
    #    #f.write('#RENAME: #uncomment with new name to rename')


import numpy as np
import copy
from dataInfo import dataInfo
from createNode import createNode
import xml.etree.cElementTree as ET
from readData import readData
import sys

def getDistOfPnts(subMat):
    dict = {}
    for key in subMat[:,-1]:
        if key in dict:
            dict[key] += 1
        else:
            dict[key] = 1
    return dict

def calGain(dict1, dict2, mdlName):
    sumFor1 = 0.0
    sumFor2 = 0.0
    n1 = 0.0
    n2 = 0.0
    for key in dict1.keys():
        n1 += dict1[key]
        n2 += dict2[key]
        if mdlName == 'gini':
            sumFor1 += (dict1[key]) ** 2
            sumFor2 += (dict2[key]) ** 2
        elif mdlName == 'information gain':
            if dict1[key] != 0.0: sumFor1 += (dict1[key]) * np.log2(dict1[key])
            if dict2[key] != 0.0: sumFor2 += (dict2[key]) * np.log2(dict2[key])
    if mdlName == 'gini':
        if n1 != 0.0:sumFor1 = 1 - sumFor1 / (n1 ** 2)
        if n2 != 0.0: sumFor2 = 1 - sumFor2 / (n2 ** 2)
    elif mdlName == 'information gain':
        if n1 != 0.0:sumFor1 = np.log2(n1) - sumFor1 / n1
        if n2 != 0.0: sumFor2 = np.log2(n2) - sumFor2 / n2
    try:
        r = n1/(n1+n2)* sumFor1 + n2 / (n1 + n2)* sumFor2
    except Exception as error:
        print('Caught this error: ' + repr(error))
    return n1, n2, r

def findTresh(firstPnt, secondPnt, prntGain, subMat, n1, n2, mdlName):
    chldGain = prntGain
    bestSndPnt = {}
    bestFrstPnt = {}
    bestInd = 0

    for c in subMat[:, -1]:
        firstPnt[c] +=1
        secondPnt[c] -= 1
        n1 += 1
        n2 -= 1
        _, _, gain = calGain(firstPnt, secondPnt, mdlName)
        if gain < chldGain :
            chldGain = gain
            bestFrstPnt = copy.copy(firstPnt)
            bestSndPnt = copy.copy(secondPnt)
            bestInd = int(n1)
        #gain = np.append(gain, [calGain(firstPnt, secondPnt, n1, n2)], axis=0)

    splitGn = prntGain - chldGain
    return splitGn, bestFrstPnt, bestSndPnt, bestInd

def find_best_split(inst, mdlName):
    secondPnt = inst.clsDist
    firstPnt = {}
    for key in secondPnt.keys():
        firstPnt[key] = 0
    [n1, n2, prntGain] = calGain(firstPnt, secondPnt, mdlName)
    maxSpltGn = -1
    maxGnInd = -1

    for i in range(0,len(inst.data[0,:])-1):
        numbAttr = inst.data[:,i].astype(float)
        #if np.isnan(inst.indices[0,i]):
        ind = (numbAttr).argsort()
        inst.indices[:,i] = ind
        #else:
        #    ind = range(0,inst.data[:,0].size)
        subMat = np.array([numbAttr[ind], inst.data[ind, - 1]]).transpose()
        #[firstPnt, secondPnt, n1, n2] = getDistOfPnts(subMat)
        [splitGn, _, _, _] = findTresh(copy.copy(firstPnt), copy.copy(secondPnt), prntGain, subMat, n1, n2, mdlName)
        if splitGn > maxSpltGn:
            maxSpltGn = splitGn
            maxGnInd = i
    if maxSpltGn == 0:
        print("No progress error")
    numbAttr = inst.data[:, maxGnInd].astype(float).transpose()
    ind = inst.indices[:, maxGnInd].astype(int)
    subMat = np.array([numbAttr[ind], inst.data[ind, - 1]]).transpose()
    [splitGn, bestFrstPnt, bestSndPnt, bestInd] = findTresh(copy.copy(firstPnt), copy.copy(secondPnt), prntGain, subMat, n1, n2, mdlName)
    tresh = (numbAttr[ind[bestInd-1]]+ numbAttr[ind[bestInd]]) / 2.0
    sub1 = inst.data[ind[0:bestInd], :]
    sub2 = inst.data[ind[bestInd:], :]
    #indicesMat = (inst.indices[:,:-1])
    #indicesMat = indicesMat.flatten('F')
    #indicesMat = np.reshape(indicesMat,)
    #maskMat = inst.indices[:,maxGnInd] < bestInd
    #vecInd1 = indicesMat[maskMat]
    #vecInd2 = indicesMat[np.invert(maskMat)]
    #indices1 = np.reshape(vecInd1, (bestInd, 3), order='F')
    #indices2 = np.reshape(vecInd2, (10 - bestInd, 3), order='F')
    #indices1 = inst.indices[maskMat,:]
    #indices2 = inst.indices[np.invert(maskMat),:]
    #print(inst.indices, '\n', indices1, '\n', indices2)
    node1 = dataInfo(sub1, bestFrstPnt, np.zeros(shape=(sub1).shape))
    node2 = dataInfo(sub2, bestSndPnt, np.zeros(shape=(sub2).shape))
    return node1, node2, tresh, maxGnInd

#def find_best_split(inst, attr):
#    dataSize = inst.shape
#    numOfClm = dataSize[1]
#    numOfRow = dataSize[0]
#    [clsDist1, clsDist2, splitGn, sub1, sub2, thresh] = calImpurity(inst, attr)
#    while (splitGn < 0 and attr[0] < numOfClm-1):
#        attr[0] += 1
#        attr[1] = 0
#        [clsDist1, clsDist2, splitGn, sub1, sub2, thresh] = calImpurity(inst, attr)
#    if splitGn <0: print("ERROR!")
#    return clsDist1, clsDist2, thresh, sub1, sub2,

def stopping_cond(inst, mdlName, epsl):
    try:
        _, _, gain = calGain(inst.clsDist, inst.clsDist, mdlName)
    except Exception as error:
        _, _, gain = calGain(inst.clsDist, inst.clsDist, mdlName)
    try:
        a1 = (inst.data[:,0].size) * inst.data[0,:-1]
    except Exception as error:
        print('Caught this error: ' + repr(error))
    a2 = np.sum(inst.data[:, :-1], axis=0)
    if gain < epsl or  np.array_equal(a1, a2) :
        return True
    return False

def Classify(clsDist):
    mainClass = 0
    mainKey = 0
    for key in clsDist:
        if clsDist[key] > mainClass: mainKey = key
    return mainKey

def TreeGrowth (inst, level, mdlName, root = None):
    level += 1
    if root == None:
        inst.indices = np.zeros(shape=(inst.data).shape)
        #inst.sorted = np.zeros(shape=(inst.data[0,:]).shape)
        inst.indices[:] = np.nan
        inst.clsDist = getDistOfPnts(inst.data)
        root = ET.Element("Node", name="root")
    if stopping_cond(inst, mdlName, 0.001) == True:
        #leaf = createNode(clsDist)
        #leaf.setLabel()
        label = Classify(inst.clsDist)
        data = ET.SubElement(root, "data")
        ET.SubElement(data, "test_cond").text = ""
        ET.SubElement(data, "label").text = repr(label)
        ET.SubElement(data, "attrNum").text = ""
        #print(inst.data,'\n\n')
        return 0#leaf
    else:
        #key = iter(clsDist)
        #root = createNode(level, clsDist[next(key)], clsDist[next(key)], inst.shape)
        [node1, node2, v, maxGnInd]= find_best_split(inst, mdlName)
        data = ET.SubElement(root, "data")
        ET.SubElement(data, "test_cond").text = repr(v)
        ET.SubElement(data, "label").text = ""
        ET.SubElement(data, "attrNum").text = repr(maxGnInd)
        lftChld = ET.SubElement(root, "Node", name="leftChild")
        child = TreeGrowth(node1, level, mdlName, lftChld)
        #root.setLftChld(child.getID)
        #root.addTestCond(v)
        rghtChld = ET.SubElement(root, "Node", name="rightChild")
        child = TreeGrowth(node2, level, mdlName, rghtChld)
        #root.setRghtChld(child.getID)
        #for v in V:
        #    subInst = distr(inst, v) #(e I root.test..cond(e) = v and e EE}.
        #    child= TreeGrowth(subInst, attr)
        #    root.addEdge(v, child)
    return root

#main body of the program for loading data and
# running a recursive tree function to learn from data
args = iter(sys.argv)
next(args)
mdlName = ""
inputFileTr = ""
inputClassesTr = ""
inputFileTs = ""
inputClassesTs = ""
for item in args:
    if item == "-m":
        mdlName = next(args)
    elif item == "-itr1":
        inputFileTr = next(args)
    elif item == "-itr2":
        inputClassesTr = next(args)
    elif item == "-its1":
        inputFileTs = next(args)
    elif item == "-its2":
        inputClassesTs = next(args)

if inputFileTr.lower() == "" or inputClassesTr == "" or (mdlName.lower() != "gini"  and mdlName.lower() != "information gain"):#or inputFileTs.lower() == "" or inputClassesTs.lower() == "" or
    print("You have NOT entered one of the required inputs!")
    sys.exit()

#mdlName = "information gain"
#inputFileTr = "X_iris.txt"
#inputClassesTr = "y_iris.txt"
#inputFileTs = "X_test.txt"
#inputClassesTs = "y_test.txt"

print("\nReading 1 set of data ...")
clases = readData(inputClassesTr)
data1 = readData(inputFileTr)
data1 = np.append(data1, clases, axis=1)

if inputClassesTs != "":
    print("\nReading 2 set of data ...")
    clases = readData(inputClassesTs)
    data2 = readData(inputFileTs)
    data2 = np.append(data2, clases, axis=1)

data = np.append(data1, data2, axis=0) if (inputClassesTs != "") else data1

#sh = instances.shape
#data = np.empty((sh[0], sh[1]+1), float)
#print(data.shape)
#data[:,:-1] = instances
#data[:,-1] = clases
#data = np.array([[1, 1, 125, "No"],
#                [0, 2, 100, "Yes"],
#                [0, 1, 70, "No"],
#                [1, 2, 120, "No"],
#                [0, 3, 95, "Yes"],
#                [0, 2, 60, "No"],
#                [1, 3, 220, "No"],
#                [0, 1, 85, "Yes"],
#                [0, 2, 75, "No"],
#                [0, 1, 90, "Yes"]])

print("\nTraining a tree model ...")
arraySize = int(data[:,0].size )
randIndx = np.random.choice([True, False], arraySize)#[bool(random.getrandbits(1)) for i in range(arraySize)]
invIndx = [not i for i in randIndx]
np.save( 'randData' , randIndx)
np.save( 'invtRandData' , invIndx)
inst = dataInfo(data[randIndx, :])
root = TreeGrowth (inst, 0, mdlName.lower())

tree = ET.ElementTree(root)
tree.write("trained_Tree.xml")
print("\nThe tree model has been trained,\nYou can look at it in a file named \"trained_Tree.xml\"\nwhich is created inside your project folder")
#saveTree(root)


from colorama import Style, Fore

from common.controllers import Controller
from common.notifications import show_push_notification
from mediagrabber.universal_downloader import UniversalMediaDownloader

class MediaGrabberController(Controller):

    @property
    def header(self):
        return f'{Fore.LIGHTGREEN_EX}MEDIAGRABBER ❯❯{Style.RESET_ALL} {super(MediaGrabberController, self).header}'

    @property
    def title(self) -> str:
        return 'Скачивание аудио и видео с YouTube, Twitch, Vimeo и т.п.'

    def main(self) -> bool:
        downloader = UniversalMediaDownloader()
        self.show_app_header()
        print('\nПолучаем метаданные...')
        info = downloader.info
        self.show_app_header()
        print(f'\n{info}\n')
        media_file = downloader.download()
        if media_file:
            media_file.move_to_downloads()
            self.show_app_header()
            print(f'\n{info}')
            print(f'\n{Style.DIM}{Fore.LIGHTGREEN_EX}ВСЕ ОПЕРАЦИИ УСПЕШНО ЗАВЕРШЕНЫ{Style.RESET_ALL}')
            print(f'Скачан медиафайл: {media_file.info}')
            show_push_notification('Скачан медиафайл',
                                   f'{media_file.name_with_extension} [{media_file.formatted_size}]',
                                   'mediagrabber_icon.ico', 'SMG: MEDIAGRABBER')
            return True
        else:
            show_push_notification('Не удалось скачать медиафайл',
                                   f'{downloader.metadata.title}', 'mediagrabber_icon.ico', 'SMG: MEDIAGRABBER')
            return False


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

__license__   = 'GPL v3'

import os, re

try:
    from qt.core import (Qt, QVBoxLayout, QLabel, QComboBox, QApplication, QSizePolicy,
                  QGroupBox, QButtonGroup, QRadioButton, QDialogButtonBox, QHBoxLayout,
                  QProgressDialog, QSize, QDialog, QCheckBox, QSpinBox, QScrollArea, QWidget,
                  QPushButton)
except ImportError:
    from PyQt5.Qt import (Qt, QVBoxLayout, QLabel, QComboBox, QApplication, QSizePolicy,
                          QGroupBox, QButtonGroup, QRadioButton, QDialogButtonBox, QHBoxLayout,
                          QProgressDialog, QSize, QDialog, QCheckBox, QSpinBox, QScrollArea, QWidget,
                          QPushButton)

from calibre.utils.config import config_dir

from calibre.gui2.tweak_book.widgets import Dialog

'''
ConversionDialog
The conversion dialog asks/displays the following:
    -Which direction of conversion is desired (i.e. Traditional->Simplified, Simplified->Traditional, or Traditional->Traditional)
    -If converting from Traditional, what country style is the source of the text (Hong Kong, Mainland, or Taiwan)
    -If converting to Traditional, what country style is desired (Hong Kong, Mainland, or Taiwan)
    -What text should be converted (the currently entire book, current file or selected text)

The chosen settings are saved between program starts.

Note: This code is based on the Calibre plugin Diap's Editing Toolbag
'''

class ConversionDialog(Dialog):
    def __init__(self, parent, prefs, punc_dict, default_omitted_puncuation, force_entire_book=False):
        self.prefs = prefs
        self.parent = parent
        self.force_entire_book = force_entire_book
        Dialog.__init__(self, _('Chinese Conversion'), 'chinese_conversion_dialog', parent)
        self.punctuation_dialog = PuncuationDialog(self.parent, self.prefs, punc_dict, default_omitted_puncuation)

    def setup_ui(self):

##        print('Dialog preferences')
##        print(self.prefs['input_source'])           # 0=whole book, 1=current file, 2=selected text
##
##        print(self.prefs['conversion_type'])        # 0=No change, 1=trad->simp, 2=simp->trad, 3=trad->trad
##        print(self.prefs['input_locale'])           # 0=Mainland, 1=Hong Kong, 2=Taiwan 3=Japan
##        print(self.prefs['output_locale'])          # 0=Mainland, 1=Hong Kong, 2=Taiwan 3=Japan
##        print(self.prefs['use_target_phrases'])     # True/False
##
##        print(self.prefs['quotation_type'])         # 0=No change, 1=Western, 2=East Asian
##
##        print(self.prefs['output_orientation'])     # 0=No change, 1=Horizontal, 2=Vertical
##
##        print(self.prefs['punc_omits'])             # Horizontal mark string in horizontal/vertical
##                                                    # dictionary pairs that is NOT to be used. No
##                                                    # space between marks in string.

        self.quote_for_trad_target = _("Update quotes: “ ”,‘ ’ -> 「 」,『 』")
        self.quote_for_simp_target = _("Update quotes: 「 」,『 』 -> “ ”,‘ ’")

        # Create layout for entire dialog
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        #Create a scroll area for the top part of the dialog
        self.scrollArea = QScrollArea(self)
        self.scrollArea.setWidgetResizable(True)

        # Create widget for all the contents of the dialog except the OK and Cancel buttons
        self.scrollContentWidget = QWidget(self.scrollArea)
        self.scrollArea.setWidget(self.scrollContentWidget)
        widgetLayout = QVBoxLayout(self.scrollContentWidget)

        # Add scrollArea to dialog
        layout.addWidget(self.scrollArea)

        self.operation_group_box = QGroupBox(_('Conversion Direction'))
        widgetLayout.addWidget(self.operation_group_box)
        operation_group_box_layout = QVBoxLayout()
        self.operation_group_box.setLayout(operation_group_box_layout)

        self.operation_group=QButtonGroup(self)
        self.no_conversion_button = QRadioButton(_('No Conversion'))
        self.operation_group.addButton(self.no_conversion_button)
        self.trad_to_simp_button = QRadioButton(_('Traditional to Simplified'))
        self.operation_group.addButton(self.trad_to_simp_button)
        self.simp_to_trad_button = QRadioButton(_('Simplified to Traditional'))
        self.operation_group.addButton(self.simp_to_trad_button)
        self.trad_to_trad_button = QRadioButton(_('Traditional to Traditional'))
        self.operation_group.addButton(self.trad_to_trad_button)
        operation_group_box_layout.addWidget(self.no_conversion_button)
        operation_group_box_layout.addWidget(self.trad_to_simp_button)
        operation_group_box_layout.addWidget(self.simp_to_trad_button)
        operation_group_box_layout.addWidget(self.trad_to_trad_button)
        self.operation_group.buttonClicked.connect(self.on_op_button_clicked)

        self.style_group_box = QGroupBox(_('Language Styles'))
        widgetLayout.addWidget(self.style_group_box)
        style_group_box_layout = QVBoxLayout()
        self.style_group_box.setLayout(style_group_box_layout)

        input_layout = QHBoxLayout()
        style_group_box_layout.addLayout(input_layout)
        self.input_region_label = QLabel(_('Input:'))
        input_layout.addWidget(self.input_region_label)
        self.input_combo = QComboBox()
        input_layout.addWidget(self.input_combo)
        self.input_combo.addItems([_('Mainland'), _('Hong Kong'), _('Taiwan'), _('Japan')])
        self.input_combo.setToolTip(_('Select the origin region of the input'))
        self.input_combo.currentIndexChanged.connect(self.update_gui)

        output_layout = QHBoxLayout()
        style_group_box_layout.addLayout(output_layout)
        self.output_region_label = QLabel(_('Output:'))
        output_layout.addWidget(self.output_region_label)
        self.output_combo = QComboBox()
        output_layout.addWidget(self.output_combo)
        self.output_combo.addItems([_('Mainland'), _('Hong Kong'), _('Taiwan'), _('Japan')])
        self.output_combo.setToolTip(_('Select the desired region of the output'))
        self.output_combo.currentIndexChanged.connect(self.update_gui)

        self.use_target_phrases = QCheckBox(_('Use output target phrases if possible'))
        self.use_target_phrases.setToolTip(_('Check to allow region specific word replacements if available'))
        style_group_box_layout.addWidget(self.use_target_phrases)
        self.use_target_phrases.stateChanged.connect(self.update_gui)

        self.quotation_group_box = QGroupBox(_('Quotation Marks'))
        widgetLayout.addWidget(self.quotation_group_box)
        quotation_group_box_layout = QVBoxLayout()
        self.quotation_group_box.setLayout(quotation_group_box_layout)

        quotation_group=QButtonGroup(self)
        self.quotation_no_conversion_button = QRadioButton(_('No Conversion'))
        quotation_group.addButton(self.quotation_no_conversion_button)
        self.quotation_trad_to_simp_button = QRadioButton(self.quote_for_simp_target)
        quotation_group.addButton(self.quotation_trad_to_simp_button)
        self.quotation_simp_to_trad_button = QRadioButton(self.quote_for_trad_target)
        quotation_group.addButton(self.quotation_simp_to_trad_button)
        quotation_group_box_layout.addWidget(self.quotation_no_conversion_button)
        quotation_group_box_layout.addWidget(self.quotation_simp_to_trad_button)
        quotation_group_box_layout.addWidget(self.quotation_trad_to_simp_button)
        self.quotation_no_conversion_button.toggled.connect(self.update_gui)
        self.quotation_trad_to_simp_button.toggled.connect(self.update_gui)
        self.quotation_simp_to_trad_button.toggled.connect(self.update_gui)

        self.other_group_box = QGroupBox(_('Other Changes'))
        widgetLayout.addWidget(self.other_group_box)
        other_group_box_layout = QVBoxLayout()
        self.other_group_box.setLayout(other_group_box_layout)

        text_dir_layout = QHBoxLayout()
        other_group_box_layout.addLayout(text_dir_layout)
        direction_label = QLabel(_('Text Direction:'))
        text_dir_layout.addWidget(direction_label)
        self.text_dir_combo = QComboBox()
        text_dir_layout.addWidget(self.text_dir_combo)
        self.text_dir_combo.addItems([_('No Conversion'), _('Horizontal'), _('Vertical')])
        self.text_dir_combo.setToolTip(_('Select the desired text orientation'))
        self.text_dir_combo.currentIndexChanged.connect(self.direction_changed)

        punctuation_layout = QHBoxLayout()
        other_group_box_layout.addLayout(punctuation_layout)
        self.update_punctuation = QCheckBox(_('Update punctuation'))
        punctuation_layout.addWidget(self.update_punctuation)
        self.update_punctuation.stateChanged.connect(self.update_gui)
        self.punc_settings_btn = QPushButton()
        self.punc_settings_btn.setText("Settings...")

        punctuation_layout.addWidget(self.punc_settings_btn)
        self.punc_settings_btn.clicked.connect(self.punc_settings_btn_clicked)
        self.punctuation_dialog = None

        source_group=QButtonGroup(self)
        self.book_source_button = QRadioButton(_('Entire eBook'))
        self.file_source_button = QRadioButton(_('Current File'))
        self.seltext_source_button = QRadioButton(_('Selected Text in Current File'))
        self.seltext_source_button.setToolTip(_('“Selected Text” is bracketed by <!--PI_SELTEXT_START--> and <!--PI_SELTEXT_END-->'))
        source_group.addButton(self.book_source_button)
        source_group.addButton(self.file_source_button)
        source_group.addButton(self.seltext_source_button)
        self.source_group_box = QGroupBox(_('Source'))
        if not self.force_entire_book:
            widgetLayout.addWidget(self.source_group_box)
            source_group_box_layout = QVBoxLayout()
            self.source_group_box.setLayout(source_group_box_layout)
            source_group_box_layout.addWidget(self.book_source_button)
            source_group_box_layout.addWidget(self.file_source_button)
            source_group_box_layout.addWidget(self.seltext_source_button)

        layout.addSpacing(10)
        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        self.button_box.accepted.connect(self._ok_clicked)
        self.button_box.rejected.connect(self._reject_clicked)
        layout.addWidget(self.button_box)

        self.set_to_preferences()
        self.update_gui()

    def on_op_button_clicked(self, btn):
        self.block_signals(True)
        if btn == self.no_conversion_button:
            self.input_combo.setCurrentIndex(-1)  # blank out the entry
            self.output_combo.setCurrentIndex(-1) # blank out the entry
        else:
            self.input_combo.setCurrentIndex(0)   # mainland
            self.output_combo.setCurrentIndex(0)  # mainland
        self.block_signals(False)
        self.update_gui()

    def block_signals(self, state):
        # block or unblock the signals generated by these objects to avoid recursive calls
        self.input_combo.blockSignals(state)
        self.output_combo.blockSignals(state)
        self.no_conversion_button.blockSignals(state)
        self.trad_to_simp_button.blockSignals(state)
        self.simp_to_trad_button.blockSignals(state)
        self.trad_to_trad_button.blockSignals(state)
        self.file_source_button.blockSignals(state)
        self.seltext_source_button.blockSignals(state)
        self.book_source_button.blockSignals(state)
        self.quotation_trad_to_simp_button.blockSignals(state)
        self.quotation_simp_to_trad_button.blockSignals(state)
        self.quotation_no_conversion_button.blockSignals(state)
        self.text_dir_combo.blockSignals(state)
        self.update_punctuation.blockSignals(state)

    def set_to_preferences(self):
        # set the gui values to match those in the preferences
        self.block_signals(True)

        self.input_combo.setCurrentIndex(self.prefs['input_locale'])
        self.output_combo.setCurrentIndex(self.prefs['output_locale'])

        if self.prefs['conversion_type'] == 0:
            self.no_conversion_button.setChecked(True)
        elif self.prefs['conversion_type'] == 1:
            self.trad_to_simp_button.setChecked(True)
        elif self.prefs['conversion_type'] == 2:
            self.simp_to_trad_button.setChecked(True)
        else:
            self.trad_to_trad_button.setChecked(True)

        if not self.force_entire_book:
            if self.prefs['input_source'] == 1:
                self.file_source_button.setChecked(True)
            elif self.prefs['input_source'] == 2:
                self.seltext_source_button.setChecked(True)
            else:
                self.book_source_button.setChecked(True)
        else:
            self.book_source_button.setChecked(True)
            self.file_source_button.setChecked(False)
            self.seltext_source_button.setChecked(False)

        if self.prefs['quotation_type'] == 1:
            self.quotation_trad_to_simp_button.setChecked(True)
        elif self.prefs['quotation_type'] == 2:
            self.quotation_simp_to_trad_button.setChecked(True)
        else:
            self.quotation_no_conversion_button.setChecked(True)

        self.text_dir_combo.setCurrentIndex(self.prefs['output_orientation'])
        if self.text_dir_combo.currentIndex() == 0:
            self.update_punctuation.setChecked(False)
        else:
            self.update_punctuation.setChecked(self.prefs['update_punctuation'])

        self.block_signals(False)

    def direction_changed(self):
        # callback when text direction changes
        self.update_punctuation.blockSignals(True)
        self.punc_settings_btn.blockSignals(True)

        if self.text_dir_combo.currentIndex() == 0:    # no direction change
            self.update_punctuation.setChecked(False)
            self.update_punctuation.setEnabled(False)
            self.punc_settings_btn.setEnabled(False)

        else:
            self.update_punctuation.setChecked(True)
            self.update_punctuation.setEnabled(True)
            self.punc_settings_btn.setEnabled(True)

        self.punc_settings_btn.blockSignals(False)
        self.update_punctuation.blockSignals(False)

    def update_gui(self):
        # callback to update other gui items when one changes
        if self.no_conversion_button.isChecked():
            self.input_combo.setEnabled(False)
            self.output_combo.setEnabled(False)
            self.input_combo.setToolTip(_('Valid input/output combinations:\nNot Applicable'))
            self.output_combo.setToolTip(_('Valid input/output combinations:\nNot Applicable'))
            self.use_target_phrases.setEnabled(False)
            self.output_region_label.setEnabled(False)
            self.input_region_label.setEnabled(False)
            self.style_group_box.setEnabled(False)

        elif self.trad_to_simp_button.isChecked():
            self.input_combo.setEnabled(True)
            self.output_combo.setEnabled(True)
            self.use_target_phrases.setEnabled(True)
            self.output_region_label.setEnabled(True)
            self.input_region_label.setEnabled(True)
            self.input_combo.setToolTip(_('Valid input/output combinations:\nHong Kong/Mainland\nMainland/Mainland\nTaiwan/Mainland\nMainland/Japan'))
            self.output_combo.setToolTip(_('Valid input/output combinations:\nHong Kong/Mainland\nMainland/Mainland\nTaiwan/Mainland\nMainland/Japan'))
            self.style_group_box.setEnabled(True)

        elif self.simp_to_trad_button.isChecked():
            self.input_combo.setEnabled(True)
            self.output_combo.setEnabled(True)
            self.input_combo.setToolTip(_('Valid input/output combinations:\nMainland/Hong Kong\nMainland/Mainland\nMainland/Taiwan\nJapan/Mainland'))
            self.output_combo.setToolTip(_('Valid input/output combinations:\nMainland/Hong Kong\nMainland/Mainland\nMainland/Taiwan\nJapan/Mainland'))
            self.use_target_phrases.setEnabled(True)
            self.output_region_label.setEnabled(True)
            self.input_region_label.setEnabled(True)
            self.style_group_box.setEnabled(True)

        elif self.trad_to_trad_button.isChecked():
            self.input_combo.setEnabled(True)
            self.output_combo.setEnabled(True)
            self.input_combo.setToolTip(_('Valid input/output combinations:\nHong Kong/Mainland\nMainland/Hong Kong\nTaiwan/Mainland\nMainland/Taiwan\nMainland/Mainland\nHong Kong/Hong Kong\nTaiwan/Taiwan'))
            self.output_combo.setToolTip(_('Valid input/output combinations:\nHong Kong/Mainland\nMainland/Hong Kong\nTaiwan/Mainland\nMainland/Taiwan\nMainland/Mainland\nHong Kong/Hong Kong\nTaiwan/Taiwan'))
            self.use_target_phrases.setEnabled(True)
            self.output_region_label.setEnabled(True)
            self.input_region_label.setEnabled(True)
            self.style_group_box.setEnabled(True)

        if self.text_dir_combo.currentIndex() == 0:
            self.update_punctuation.blockSignals(True)
            self.update_punctuation.setChecked(False)
            self.update_punctuation.setEnabled(False)
            self.update_punctuation.blockSignals(False)
        else:
            self.update_punctuation.blockSignals(True)
            self.update_punctuation.setEnabled(True)
            self.update_punctuation.blockSignals(False)

        if self.update_punctuation.isChecked():
            self.punc_settings_btn.setEnabled(True)
        else:
            self.punc_settings_btn.setEnabled(False)

    def _ok_clicked(self):
        # save current settings into preferences and close dialog
        self.savePrefs()
        self.accept()

    def _reject_clicked(self):
        # restore initial settings and close dialog
        self.set_to_preferences()
        self.update_gui()
        self.reject()

    def punc_settings_btn_clicked(self):
        # open the punctuation dialog
        self.punctuation_dialog.exec_()

    def savePrefs(self):
        # save the current settings into the preferences
        self.prefs['input_locale'] = self.input_combo.currentIndex()
        self.prefs['output_locale'] = self.output_combo.currentIndex()

        if self.trad_to_simp_button.isChecked():
            self.prefs['conversion_type'] = 1
        elif self.simp_to_trad_button.isChecked():
            self.prefs['conversion_type'] = 2
        elif self.trad_to_trad_button.isChecked():
            self.prefs['conversion_type'] = 3
        else:
            self.prefs['conversion_type'] = 0

        if self.file_source_button.isChecked():
            self.prefs['input_source'] = 1
        elif self.seltext_source_button.isChecked():
            self.prefs['input_source'] = 2
        else:
            self.prefs['input_source'] = 0

        self.prefs['use_target_phrases'] = self.use_target_phrases.isChecked()

        if self.quotation_trad_to_simp_button.isChecked():
            self.prefs['quotation_type'] = 1
        elif self.quotation_simp_to_trad_button.isChecked():
            self.prefs['quotation_type'] = 2
        else:
            self.prefs['quotation_type'] = 0

        self.prefs['output_orientation'] = self.text_dir_combo.currentIndex()
        self.prefs['update_punctuation'] = self.update_punctuation.isChecked()

    def getRegex(self):
        # getter for the punctuation conversion regular expression object
        return self.punctuation_dialog.getRegex()

class PuncuationDialog(Dialog):

    def __init__(self, parent, prefs, punc_dict, default_omitted_puncuation):
        self.prefs = prefs
        self.punc_dict = punc_dict
        self.default_omitted_puncuation = default_omitted_puncuation
        self.parent = parent
        self.puncSettings = set()
        Dialog.__init__(self, _('Chinese Punctuation'), 'chinese_conversion_punctuation_dialog', parent)

    def setup_ui(self):
        self.punc_setting = {}
        self.checkbox_dict = {}

        # Create layout for entire dialog
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        #Create a scroll area for the top part of the dialog
        self.scrollArea = QScrollArea(self)
        self.scrollArea.setWidgetResizable(True)

        # Create widget for all the contents of the dialog except the buttons
        self.scrollContentWidget = QWidget(self.scrollArea)
        self.scrollArea.setWidget(self.scrollContentWidget)
        widgetLayout = QVBoxLayout(self.scrollContentWidget)

        # Add scrollArea to dialog
        layout.addWidget(self.scrollArea)

        self.punctuation_group_box = QGroupBox(_('Punctuation'))
        widgetLayout.addWidget(self.punctuation_group_box)

        self.punctuation_group_box_layout = QVBoxLayout()
        self.punctuation_group_box.setLayout(self.punctuation_group_box_layout)

        for x in self.punc_dict:
            str = x + " <-> " + self.punc_dict[x]
            widget = QCheckBox(str)
            self.checkbox_dict[x] = widget
            self.punctuation_group_box_layout.addWidget(widget)
            if x in self.prefs['punc_omits']:
                widget.setChecked(False)
            else:
                widget.setChecked(True)

        self.button_box_settings = QDialogButtonBox()
        self.clearall_button = self.button_box_settings.addButton("Clear All", QDialogButtonBox.ActionRole)
        self.setall_button = self.button_box_settings.addButton("Set All", QDialogButtonBox.ActionRole)
        self.default_button = self.button_box_settings.addButton("Default", QDialogButtonBox.ActionRole)
        self.button_box_settings.clicked.connect(self._action_clicked)
        layout.addWidget(self.button_box_settings)

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.button_box.accepted.connect(self._ok_clicked)
        self.button_box.rejected.connect(self._reject_clicked)
        layout.addWidget(self.button_box)

    def savePrefs(self):
        setting = ""
        for x in self.puncSettings:
            setting = setting + x
        self.prefs['punc_omits'] = setting

    def _ok_clicked(self):
        self.puncSettings.clear()
        # Loop through and update set of unchecked items
        for x in self.checkbox_dict.keys():
            if not self.checkbox_dict[x].isChecked():
                self.puncSettings.add(x)
        self.savePrefs()
        self.accept()

    def _reject_clicked(self):
        # Restore back to values when first opened
        # This will be the same as the preferences
        ## loop through all checkboxes
        for x in self.checkbox_dict.keys():
            self.checkbox_dict[x].blockSignals(True)
            if x in self.prefs['punc_omits']:
                self.checkbox_dict[x].setChecked(False)
            else:
                self.checkbox_dict[x].setChecked(True)
            self.checkbox_dict[x].blockSignals(False)
        self.reject()

    def _action_clicked(self, button):
        ## Find out which button is pressed
        if button is self.clearall_button:
            ## loop through all checkboxes and unset
            for x in self.checkbox_dict.values():
                x.blockSignals(True)
                x.setChecked(False)
                x.blockSignals(False)

        elif button is self.setall_button:
            ## loop through all checkboxes and set
            for x in self.checkbox_dict.values():
                x.blockSignals(True)
                x.setChecked(True)
                x.blockSignals(False)

        elif button is self.default_button:
            ## loop through all checkboxes
            for x in self.checkbox_dict.keys():
                self.checkbox_dict[x].blockSignals(True)
                if x in self.default_omitted_puncuation:
                    self.checkbox_dict[x].setChecked(False)
                else:
                    self.checkbox_dict[x].setChecked(True)
                self.checkbox_dict[x].blockSignals(False)


import pandas as pd
import sqlalchemy
import requests
import sqlite3

DATABASE_LOCATION = "sqlite:///users.sqlite"

def get_user(url="https://randomuser.me/api/"):
    response = requests.get(url)
    if response.status_code == 200:
        payload = response.json()
        result = payload.get("results", [])
        for user in result:
            return {
                "firstname": user["name"]["first"],
                "lastname": user["name"]["last"],
                "gender": user["gender"],
                "age": user["dob"]["age"],
                "country": user["location"]["country"],
                "city": user["location"]["city"],
                "email": user["email"],
            }
    else:
        print("connection fail.")

def load_data():
    user_info = get_user()
    users = pd.DataFrame(
        user_info,
        columns=["firstname", "lastname", "gender", "age", "country", "city", "email"],
        index=[0],
    )

    engine = sqlalchemy.create_engine(DATABASE_LOCATION)
    conn = sqlite3.connect("users.sqlite")
    cursor = conn.cursor()

    sql_query = """
    CREATE TABLE IF NOT EXISTS users(
        id INTEGER,
        firstname VARCHAR(100),
        lastname VARCHAR(100),
        gender VARCHAR(50),
        age INTEGER,
        country VARCHAR(100),
        city VARCHAR(100),
        email VARCHAR(200),
        CONSTRAINT primary_key_constraint PRIMARY KEY (id)
    )
    """

    cursor.execute(sql_query)
    try:
        users.to_sql("users", engine, index=False, if_exists="append")
    except:
        print("Data already exists in the database")

    conn.close()
    print("Close database successfully")

if __name__ == "__main__":
    load_data()


import numpy as np
import scipy

np.set_printoptions(threshold=np.inf)

def main():
    lines = []
    max_x, max_y, max_z = 0, 0, 0
    with open("input.txt", "r") as f:
        for line in f:
            lines.append(line.strip("\n"))
            x, y, z = (int(x) for x in line.strip("\n").split(","))
            max_x, max_y, max_z = max(max_x, x), max(max_y, y), max(max_z, z)

    result = []

    # Part 1
    grid = np.array(
        [
            [[0 for _ in range(max_z + 1)] for _ in range(max_y + 1)]
            for _ in range(max_x + 1)
        ]
    )
    for line in lines:
        x, y, z = (int(a) for a in line.strip("\n").split(","))
        grid[x, y, z] = 1

    bin = scipy.ndimage.generate_binary_structure(3, 1)
    conn_grid = grid * scipy.ndimage.convolve(grid, bin, mode="constant")

    number_of_blocks = np.count_nonzero(conn_grid > 0)
    conn_grid = conn_grid[conn_grid > 0] - 1
    result.append(number_of_blocks * 6 - conn_grid.sum())

    # Part 2

    with open("output.txt", "w") as f:
        for i in result:
            f.write(f"{i}\n")

if __name__ == "__main__":
    main()


# Raspberry_Pi
import wave     #wavファイルを扱うためのライブラリ
import pyaudio
# import webapi

WAVE_OUTPUT_FILENAME = "sample.wav" #音声を保存するファイル名
iDeviceIndex = 0 #録音デバイスのインデックス番号

def MakeWavFile(FileName = "sample.wav", Record_Seconds = 60):

	#生音声取得時の設定
	CHUNK = 1024  #1024個のデータをfsというサンプル周波数で取ってきた。
	FORMAT = pyaudio.paInt16
	CHANNELS = 1 #モノラル
	RATE = 44100 #サンプルレート（録音の音質）

	p = pyaudio.PyAudio()
	print(p)
	stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, input_device_index = iDeviceIndex, frames_per_buffer = CHUNK)

	#レコード開始
	print('Now Recording...')
	all = []
	for i in range(0, int(RATE / CHUNK * Record_Seconds)):
	    data = stream.read(CHUNK) #音声を読み取って、
	    all.append(data) #データを追加

	#レコード終了
	print('Now Recording...')

	stream.close()
	p.terminate()

	#保存
	wavFile = wave.open(FileName, 'wb')
	wavFile.setnchannels(CHANNELS)
	wavFile.setsampwidth(p.get_sample_size(FORMAT))
	wavFile.setframerate(RATE)
	wavFile.writeframes(b"".join(all)) #Python3用
	wavFile.close()

if __name__ == "__main__":
#WAVファイル作成, 引数は（ファイル名, 録音する秒数）
	MakeWavFile("sample.wav", Record_Seconds = 60)
#webapi.changedata(wavFile)


import multiprocessing
import sys
import cv2
import random
import numpy as np
import os
import mmap
import torchvision.transforms as transforms
from PIL import Image
import torch
import time
def random_crop(image, crop_height, crop_width):
    max_x = image.shape[1] - crop_width + 1
    max_y = image.shape[0] - crop_height + 1
    x = np.random.randint(0, max_x)
    y = np.random.randint(0, max_y)
    crop = image[y: y + crop_height, x: x + crop_width]
    return crop

# def pil_loader(path):
#     # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
#     with open(path, 'rb') as f:
#         img = Image.open(f)
#         return img.convert('RGB')

def readImageWithMmap(path):
    fd =  os.open(path, os.O_RDONLY | os.O_DIRECT)
    mm = mmap.mmap(fd, 0, access=mmap.ACCESS_READ)
    os.close(fd)
    img = np.fromstring(mm.read(), dtype="uint8")

    return img

def transform(image):
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    image = cv2.resize(image, (224, 224))
    image = random_crop(image,224,224)

    if random.random()<0.5:
        image = cv2.flip(image, 1)

    image = image / 255.0
    image = image - (0.485, 0.456, 0.406)
    image = image / (0.229, 0.224, 0.225)
    image = image.transpose((2, 0, 1))
    return image

NUM_WORKER = 3
print(NUM_WORKER)

# normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
#                                 std=[0.229, 0.224, 0.225])
# transform = transforms.Compose([
#         transforms.RandomResizedCrop(224),
#         transforms.RandomHorizontalFlip(),
#         transforms.ToTensor(),
#         normalize,
#     ])

def get_metadata(metadata_path='/home/Adama/metadata'):
    with open("metadata", "r") as f:
        lines = f.readlines()
    records = []
    for line in lines:
        line = line.split()
        records.append((line[0] , int(line[1])))
    return records

def work_process(q_in):
    t=0
    # mytransform = transform
    start = time.time()
    while True:
        deq = q_in.get()
        if deq is None:
            print(time.time()-start)
            break
        path, label = deq
        if t==0:
            image = readImageWithMmap(path)
            t = 1
        # image = readImageWithMmap(path)
        image2 = transform(image)
        #process.process(path)
        #insert your process function

def main(records):
    if NUM_WORKER > 0:
        q_in = [ multiprocessing.Queue(1024) for i in range(NUM_WORKER) ]
        multiprocess = [ multiprocessing.Process(target=work_process, args=(q_in[i],)) for i in range(NUM_WORKER) ]

        for p in multiprocess:
            p.start()

        for i, record in enumerate(records):
            q_in[i % NUM_WORKER].put(record)

        for q in q_in:
            q.put(None)

        for p in multiprocess:
            p.join()

if __name__ == "__main__":

    #you should prepare your metadata
    #and give your metadata path
    records = get_metadata('/home/Adama/dataloading/metadata')
    records = records[0:10000]
    print(len(records))
    begin = time.time()
    main(records)
    end = time.time()
    print("speed:", len(records) / (end - begin))


from django.db.models import fields
from rest_framework import serializers
from products.models import Products
from rest_framework_simplejwt.tokens import AccessToken, RefreshToken, TokenError

class ProductsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = [
            "id",
            "name",
            "price",
            "description",
            "image",
        ]

# class RefreshTokenSerializer(serializers.Serializer):
#     refresh = serializers.CharField()
#     # access = serializers.CharField()

#     default_error_messages = {
#         'bad_token': _('Token is invalid or expired')
#     }

#     def validate(self, attrs):
#         print(attrs)
#         self.refresh_token = attrs['refresh']
#         # self.access_token = attrs['access']
#         return attrs

#     def save(self, **kwargs):
#         try:
#             # AccessToken(self.access_token).blacklist()
#             RefreshToken(self.refresh_token).blacklist()
#         except TokenError:
#             self.fail('bad_token')


from matplotlib import pyplot as plt
from Point import Point

# Constructing cutoff of a polygon by segments using the Cirus-Beck algorithm

def init_points_for_polygon():
    points = []
    xs = [2, 4, 9, 12, 14, 15, 12, 6, 1]
    ys = [3, 1, 1, 3, 7, 12, 14, 13, 7]
    for i in range(len(xs)):
        x = Point(xs[i], ys[i])
        points.append(x)
    return points

def init_segments():
    return [(Point(4, 0), Point(14, 6)), (Point(12, 0), Point(16, 2)), (Point(4, 12), Point(16, 13)),
            (Point(14, 6), Point(14, 14)), (Point(1, 7), Point(4, 1)), (Point(6, -2), Point(0, 12)),
            (Point(11, 16), Point(0, 8)), (Point(12, 15), Point(3, -2)), (Point(10, 0), Point(0, 12.5))]

def print_points(points: list):
    for i in range(len(points)):
        print("i: ", i, ": ", points[i].x, ":", points[i].y)

def get_cross_point(p1: Point, p2: Point, q1: Point, q2: Point):
    if (p1.x == p2.x and p2.y == p1.y) or (q1.x == q2.x and q1.y == q2.y):
        print("Not lines")
        return

    if p1.x > p2.x:
        p1.x, p2.x = p2.x, p1.x
        p1.y, p2.y = p2.y, p1.y
    if q1.x > q2.x:
        q1.x, q2.x = q2.x, p1.x
        q1.y, q2.y = q2.y, p1.y

    k1 = 0
    k2 = 0

    if p1.x != p2.x and p2.y != p1.y:
        k1 = (p2.y - p1.y) / (p2.x - p1.x)
    if q1.x != q2.x and q2.y != q1.y:
        k2 = (q2.y - q1.y) / (q2.x - q1.x)

    if k2 == k1:
        print("k1 == k2: Lines are parallel or collinear")
        return

    b1 = p1.y - k1 * p1.x
    b2 = q1.y - k2 * q1.x

    if p1.x == p2.x:
        x = p1.x
    elif q1.x == q2.x:
        x = q1.x
    else:
        x = (b2 - b1) / (k1 - k2)

    y = k1 * x + b1

    return Point(x, y)

def get_current_parameter(segment: tuple, vertex1: Point, vertex2: Point):
    normal_vector = Point(vertex2.y - vertex1.y, -(vertex2.x - vertex1.x))
    segment_vertex_vector = Point(vertex1.x - segment[0].x, vertex1.y - segment[0].y)
    segment_vector = Point(segment[1].x - segment[0].x, segment[1].y - segment[0].y)
    return (segment_vertex_vector * normal_vector) / (segment_vector * normal_vector)

def determine_intersection_type(p1: Point, p2: Point, segment: tuple):
    normal_vector = Point(p2.y - p1.y, -(p2.x - p1.x))
    segment_vector = Point(segment[1].x - segment[0].x, segment[1].y - segment[0].y)
    if segment_vector * normal_vector > 0:
        return 1
    elif segment_vector * normal_vector < 0:
        return -1
    else:
        return 0  # no intersection

def cirus_beck(polygon_points: list, segment: tuple):
    t0_values = [0]
    t1_values = [1]
    n = len(polygon_points)

    for i in range(len(polygon_points)):
        intersection_type = determine_intersection_type(polygon_points[i], polygon_points[(i + 1) % n], segment)
        t = get_current_parameter(segment, polygon_points[i], polygon_points[(i + 1) % n])
        if intersection_type == -1:
            t0_values.append(t)
        elif intersection_type == 1:
            t1_values.append(t)
        else:
            continue

    t0 = max(t0_values)
    t1 = min(t1_values)

    print(t0, t1)

    if t0 <= t1:
        x1 = segment[0].x + (segment[1].x - segment[0].x) * t0
        x2 = segment[0].x + (segment[1].x - segment[0].x) * t1
        y1 = segment[0].y + (segment[1].y - segment[0].y) * t0
        y2 = segment[0].y + (segment[1].y - segment[0].y) * t1
        plt.plot([x1, x2], [y1, y2], color="red")

def draw_polygon(points: list, color):
    for i in range(len(points)):
        plt.plot([points[i].x, points[(i + 1) % len(points)].x], [points[i].y, points[(i + 1) % len(points)].y], color=color)

def draw_segments(segments: list, color):
    for i in range(len(segments)):
        plt.plot([segments[i][0].x, segments[i][1].x], [segments[i][0].y, segments[i][1].y], color=color)

def draw_point(point: Point):
    plt.scatter(point.x, point.y)

def init():
    polygon_points = init_points_for_polygon()
    draw_polygon(polygon_points, "blue")
    segments = init_segments()
    draw_segments(segments, "green")

    for i in range(len(segments)):
        cirus_beck(polygon_points, segments[i])

    plt.grid(True)
    plt.show()

init()


import os
from werkzeug.contrib.fixers import ProxyFix
from flask import Flask, redirect, url_for, render_template
from flask_dance.contrib.google import make_google_blueprint, google
from raven.contrib.flask import Sentry
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'
blueprint = make_google_blueprint(client_id='236746006863-19jtqj68p0hkk9lf60eqkmt0i5s9fo09.apps.googleusercontent.com'
                                  , client_secret='gInPVAmZFZKpCDMOosNjAEZd', offline=True,scope=['profile', 'email'])
app.register_blueprint(blueprint,url_prefix='/login')

@app.route('/')
def index():
    return render_template('home.html')

@app.route('/welcome')
def welcome():
    resp = google.get("/oauth2/v2/userinfo")
    assert resp.ok, resp.text
    email = resp.json()['email']
    return render_template('welcome.html', email=email)

@app.route('/login/google')
def login():
    if not google.authorized:
        return render_template(url_for('google.login'))
    resp = google.get("/oauth2/v2/userinfo")
    assert resp.ok, resp.text
    email = resp.json()['email']
    return render_template('welcome.html', email=email)

if __name__ == "__main__":
    app.run(debug=True)


import requests
import json, sys
from dewiki import parser

def wiki():
    if len(sys.argv) != 2:
        print("1 argument required")
    else:
        url = 'https://en.wikipedia.org/w/api.php'
        page = sys.argv[1].replace(" ", "_")
        PARAMS = {
            "action": "query",
            "titles": page,
            "format": "json",
            "prop" : "revisions",
            "rvprop" : "content",
            "redirects" : ""
                }
        req = requests.get(url=url, params= PARAMS)
        if req.ok != True:
            exit("Error" + req.status_code)
        revisions = json.loads(req.text)['query']["pages"]
        for a, val in revisions.items():
            rev = val.get('revisions')
            if rev is None:
                print("Information not found")
                exit(1)
        page_name = rev[0]['*']
        #res = dewiki.from_string(rev[0]['*'])
        res = parser.Parser().parse_string(page_name)
        with open(page+".wiki ", "w") as file:
            file.write(str(res))

if __name__ == '__main__':
    wiki()


import logging
import sys

# BUG FIX: https://github.com/tensorflow/tensorflow/issues/26691
try:
    # Capirca uses Google's abseil-py library, which uses a Google-specific
    # wrapper for logging. That wrapper will write a warning to sys.stderr if
    # the Google command-line flags library has not been initialized.
    # https://github.com/abseil/abseil-py/blob/pypi-v0.7.1/absl/logging/__init__.py#L819-L825
    # This is not right behavior for Python code that is invoked outside of a
    # Google-authored main program. Use knowledge of abseil-py to disable that
    # warning; ignore and continue if something goes wrong.
    import absl.logging

    # https://github.com/abseil/abseil-py/issues/99
    logging.root.removeHandler(absl.logging._absl_handler)
    # https://github.com/abseil/abseil-py/issues/102
    absl.logging._warn_preinit_stderr = False
except Exception:
    pass

# handler for logging to StdOut
STDOUT_HANDLER = \
    logging.StreamHandler(sys.stdout)

STDOUT_HANDLER.setFormatter(
    logging.Formatter(
        fmt='%(asctime)s   %(levelname)s   %(name)s:   %(message)s\n',
        datefmt='%Y-%m-%d %H:%M'))

# utility class to flush logging stream upon each write
# http://stackoverflow.com/questions/29772158/make-ipython-notebook-print-in-real-time
class _FlushFile:
    def __init__(self, f):
        self.f = f

    def __getattr__(self, item):
        return self.f.__getattribute__(item)

    def flush(self):
        self.f.flush()

    def write(self, x):
        self.f.write(x)
        self.flush()

# enable live printing
def enable_live_print():
    sys.stdout = _FlushFile(sys.stdout)
    print('Live Printing Enabled')