import os
from typing import Optional

import tensorflow as tf
from tfx.dsl.compiler import constants
from tfx.orchestration import metadata
from tfx.orchestration.portable import beam_dag_runner
from tfx.orchestration.portable import test_utils
from tfx.proto.orchestration import executable_spec_pb2
from tfx.proto.orchestration import local_deployment_config_pb2
from tfx.proto.orchestration import pipeline_pb2

from google.protobuf import message
from google.protobuf import text_format

_PythonClassExecutableSpec = executable_spec_pb2.PythonClassExecutableSpec
_LOCAL_DEPLOYMENT_CONFIG = text_format.Parse("""
    executor_specs {
      key: "my_example_gen"
      value {
        python_class_executable_spec {
          class_path: "tfx.components.example_gen_executor"
        }
      }
    }
    executor_specs {
      key: "my_transform"
      value {
        python_class_executable_spec {
          class_path: "tfx.components.transform_executor"
        }
      }
    }
    executor_specs {
      key: "my_trainer"
      value {
        python_class_executable_spec {
          class_path: "tfx.components.trainer_executor"
        }
      }
    }
    custom_driver_specs {
      key: "my_example_gen"
      value {
        python_class_executable_spec {
          class_path: "tfx.components.example_gen_driver"
        }
      }
    }
    metadata_connection_config {
      fake_database {}
    }
""", local_deployment_config_pb2.LocalDeploymentConfig())

_INTERMEDIATE_DEPLOYMENT_CONFIG = text_format.Parse("""
    executor_specs {
      key: "my_example_gen"
      value {
        [type.googleapis.com/tfx.orchestration.executable_spec.PythonClassExecutableSpec] {
          class_path: "tfx.components.example_gen_executor"
        }
      }
    }
    executor_specs {
      key: "my_transform"
      value {
        [type.googleapis.com/tfx.orchestration.executable_spec.PythonClassExecutableSpec] {
          class_path: "tfx.components.transform_executor"
        }
      }
    }
    executor_specs {
      key: "my_trainer"
      value {
        [type.googleapis.com/tfx.orchestration.executable_spec.PythonClassExecutableSpec] {
          class_path: "tfx.components.trainer_executor"
        }
      }
    }
    custom_driver_specs {
      key: "my_example_gen"
      value {
        [type.googleapis.com/tfx.orchestration.executable_spec.PythonClassExecutableSpec] {
          class_path: "tfx.components.example_gen_driver"
        }
      }
    }
    metadata_connection_config {
      [type.googleapis.com/ml_metadata.ConnectionConfig] {
        fake_database {}
      }
    }
""", pipeline_pb2.IntermediateDeploymentConfig())

_executed_components = []
_component_executors = {}
_component_drivers = {}
_conponent_to_pipeline_run = {}

# TODO(b/162980675): When PythonExecutorOperator is implemented. We don't
# Need to Fake the whole FakeComponentAsDoFn. Instead, just fake or mock
# executors.
class _FakeComponentAsDoFn(beam_dag_runner.PipelineNodeAsDoFn):

  def __init__(self, pipeline_node: pipeline_pb2.PipelineNode,
               mlmd_connection: metadata.Metadata,
               pipeline_info: pipeline_pb2.PipelineInfo,
               pipeline_runtime_spec: pipeline_pb2.PipelineRuntimeSpec,
               executor_spec: Optional[message.Message],
               custom_driver_spec: Optional[message.Message],
               deployment_config: Optional[message.Message]):
    super().__init__(pipeline_node, mlmd_connection, pipeline_info,
                     pipeline_runtime_spec, executor_spec, custom_driver_spec,
                     deployment_config)
    _component_executors[self._node_id] = executor_spec
    _component_drivers[self._node_id] = custom_driver_spec
    pipeline_run = None
    for context in pipeline_node.contexts.contexts:
      if context.type.name == constants.PIPELINE_RUN_ID_PARAMETER_NAME:
        pipeline_run = context.name.field_value.string_value
    _conponent_to_pipeline_run[self._node_id] = pipeline_run

  def _run_component(self):
    _executed_components.append(self._node_id)

class BeamDagRunnerTest(test_utils.TfxTest):

  def setUp(self):
    super(BeamDagRunnerTest, self).setUp()
    # Setup pipelines
    self._pipeline = pipeline_pb2.Pipeline()
    self.load_proto_from_text(
        os.path.join(
            os.path.dirname(__file__), 'testdata',
            'pipeline_for_beam_dag_runner_test.pbtxt'), self._pipeline)
    _executed_components.clear()
    _component_executors.clear()
    _component_drivers.clear()
    _conponent_to_pipeline_run.clear()

  def testRunWithLocalDeploymentConfig(self):
    self._pipeline.deployment_config.Pack(_INTERMEDIATE_DEPLOYMENT_CONFIG)
    beam_dag_runner.BeamDagRunner._PIPELINE_NODE_DO_FN_CLS = _FakeComponentAsDoFn
    beam_dag_runner.BeamDagRunner().run(self._pipeline)
    self.assertEqual(
        _component_executors, {
            'my_example_gen':
                text_format.Parse(
                    'class_path: "tfx.components.example_gen_executor"',
                    _PythonClassExecutableSpec()),
            'my_transform':
                text_format.Parse(
                    'class_path: "tfx.components.transform_executor"',
                    _PythonClassExecutableSpec()),
            'my_trainer':
                text_format.Parse(
                    'class_path: "tfx.components.trainer_executor"',
                    _PythonClassExecutableSpec()),
            'my_importer': None,
        })
    self.assertEqual(
        _component_drivers, {
            'my_example_gen':
                text_format.Parse(
                    'class_path: "tfx.components.example_gen_driver"',
                    _PythonClassExecutableSpec()),
            'my_transform': None,
            'my_trainer': None,
            'my_importer': None,
        })
    # 'my_importer' has no upstream and can be executed in any order.
    self.assertIn('my_importer', _executed_components)
    _executed_components.remove('my_importer')
    self.assertEqual(_executed_components,
                     ['my_example_gen', 'my_transform', 'my_trainer'])
    # Verifies that every component gets a not-None pipeline_run.
    self.assertTrue(all(_conponent_to_pipeline_run.values()))

  def testRunWithIntermediateDeploymentConfig(self):
    self._pipeline.deployment_config.Pack(_LOCAL_DEPLOYMENT_CONFIG)
    beam_dag_runner.BeamDagRunner._PIPELINE_NODE_DO_FN_CLS = _FakeComponentAsDoFn
    beam_dag_runner.BeamDagRunner().run(self._pipeline)
    self.assertEqual(
        _component_executors, {
            'my_example_gen':
                text_format.Parse(
                    'class_path: "tfx.components.example_gen_executor"',
                    _PythonClassExecutableSpec()),
            'my_transform':
                text_format.Parse(
                    'class_path: "tfx.components.transform_executor"',
                    _PythonClassExecutableSpec()),
            'my_trainer':
                text_format.Parse(
                    'class_path: "tfx.components.trainer_executor"',
                    _PythonClassExecutableSpec()),
            'my_importer': None,
        })
    self.assertEqual(
        _component_drivers, {
            'my_example_gen':
                text_format.Parse(
                    'class_path: "tfx.components.example_gen_driver"',
                    _PythonClassExecutableSpec()),
            'my_transform': None,
            'my_trainer': None,
            'my_importer': None,
        })
    # 'my_importer' has no upstream and can be executed in any order.
    self.assertIn('my_importer', _executed_components)
    _executed_components.remove('my_importer')
    self.assertEqual(_executed_components,
                     ['my_example_gen', 'my_transform', 'my_trainer'])
    # Verifies that every component gets a not-None pipeline_run.
    self.assertTrue(all(_conponent_to_pipeline_run.values()))

if __name__ == '__main__':
  tf.test.main()


"""initial db

Revision ID: 4611441634ad
Revises:
Create Date: 2020-11-15 13:51:13.480401

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "4611441634ad"
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "users",
        sa.Column("id", sa.String(length=36), nullable=False),
        sa.Column("email", sa.String(length=128), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
    )
    op.create_table(
        "presence",
        sa.Column("user_id", sa.String(length=36), nullable=False),
        sa.Column("event_id", sa.String(length=128), nullable=False),
        sa.Column("presence", sa.String(length=7), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.id"], onupdate="CASCADE", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("user_id", "event_id"),
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("presence")
    op.drop_table("users")
    # ### end Alembic commands ###


import torch.nn as nn

class BatchNormLayer(nn.Module):
    """
    Batch Norm layer abstraction. Input is assumed to be in canonical form: [Batch_size, channels, frames, features] for the 2d case.
    """

    def __init__(self, bn_channels, bn_init='default'):
        super(BatchNormLayer, self).__init__()

        self.batchnorm = nn.BatchNorm2d(bn_channels)
        if bn_init == 'ones':
            nn.init.constant_(self.batchnorm.weight, 1)
            nn.init.constant_(self.batchnorm.bias, 0)
        elif bn_init == 'zeros':
            # Zero-initialize the last BN in each residual branch,
            # so that the residual branch starts with zeros, and each residual block behaves like an identity.
            # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
            nn.init.constant_(self.batchnorm.weight, 0)
            nn.init.constant_(self.batchnorm.bias, 0)
        elif bn_init != 'default':
            raise ValueError(f"Invalid bn_init {bn_init}, should be one of 'ones', 'zeros'")

    def forward(self, input):
        output = self.batchnorm(input)
        return output


from typing import Union
import re

from pydantic import BaseModel, validator

class Unit(BaseModel):
    ai_value: int
    attack: int
    base_lvl: int
    cost: int
    defence: int
    growth: int
    health: int
    max_damage: int
    min_damage: int
    name: str
    resources_cost: Union[dict | None]
    special: Union[str | None]
    speed: int
    town: str
    upgrade_lvl: int

    @validator('ai_value', 'attack', 'base_lvl', 'cost',
               'defence', 'growth', 'health', 'max_damage', 'min_damage', 'speed')
    def pos_int_validator(cls, v):
        if v < 1:
            raise ValueError(f'{v} must be equal to or greater than 1')
        return v

    @validator('min_damage')
    def min_dmg_smaller_validator(cls, v, values):
        if 'max_damage' in values and v > values['max_damage']:
            raise ValueError(f'min_damage {v} must be less than or equal to max_damage')
        return v

    @validator('name', 'town')
    def name_town_validator(cls, v):
        if re.search(r"[^A-Za-z ]+", v):
            raise ValueError(
                'String must not contain Any Non-alphanumeric characters, except spaces and/or Any Numeric characters')
        return v


import socketserver
import json
from abc import ABCMeta, abstractmethod
import struct
#import numpy as np

CARLIST=[]
PHONELIST=[]

class MyTCPHandler(socketserver.BaseRequestHandler):

    def handle(self):
        mtype = ''
        name = ''
        try:
            #data = self.request.recv(4)
            data = self.request.recv(1024).decode('utf8')
            print(data)
            json_info = json.loads(data)
            if json_info['type'] =='init':
                mtype =json_info['which']
                name = json_info['name']
        except:
            print ('hack attack me!')

        if mtype =='car':
            car = Car(self.request,self.client_address[0])
        elif mtype =='phone':
            phone = Phone(self.request,self.client_address)

    def setup(self):
        print("before handle,连接建立：",self.client_address)

    def finish(self):
        print("finish run  after handle")
        for car in CARLIST:
            if car.add == self.client_address:
                CARLIST.remove(car)

class Manager():
    __metaclass__ = ABCMeta
    def __init__(self,resquest,address):
        self.request =resquest
        self.add = address
        self.link = None
        self.buff_len=0
        self.loop()

    def loop(self):
        while True:
            try:
                data=self.request.recv(1024)
                #print(data)
            except Exception as e:
                print(self.add,"disconnect",e)
                self.request.close()

            if not data:
                print("connection lost")
                self.disconect()
                break
            #print(self.add,data)
            self.rev(data)

    @abstractmethod
    def rev(self,data):
        pass

    @abstractmethod
    def disconect(self):
        pass

    def send_append_data(self,data):
        # self.request.send(data)

        try:
            self.request.send(b'\xaa\xff')
            self.request.send(data)
            self.request.send(b'\xff\xaa')
        except:
            #self.disconnect()
            print ('phone send fald')

    def sendcarlist(self):
        print(len(CARLIST))
        dic_list =[]
        for cars in CARLIST:
            dic_name ={'name':cars.add}
            dic_list.append(dic_name)
        dic = {'type':'carlist'}
        dic.update({'carlist':dic_list})
        js = json.dumps(dic)
        print (js)
        self.send_append_data(js.encode())

class Car(Manager):
    def __init__(self,resquest,address):
        print('join a car')
        CARLIST.append(self)
        super(Car, self).__init__(resquest,address)

    def rev(self,data):
        if self.link:
            self.link.request.send(data)

    def disconect(self):
        CARLIST.remove(self)
        if self.link:
            dic = {'type':'disconnect'}
            js = json.dumps(dic)
            self.link.send_append_data(js.encode())
            self.link.link =None

class Phone(Manager):
    def __init__(self,resquest,address):
        print('join a phone')
        PHONELIST.append(self)
        super(Phone, self).__init__(resquest,address)

    def rev(self,data):
        try:
            data_json= data.decode('utf8')
            json_info = json.loads(data_json)
            if json_info['type']=='getcarlist':
                self.sendcarlist()
            if json_info['type']=='linkcar':
                self.linkcar(json_info['ip'])
        except Exception as e:
            print ('phonerev;',e)
        if self.link:
            self.link.request.send(data)

    def linkcar(self,ip):
        for car in CARLIST:
            print (ip ,car.add)
            if ip == car.add:
                self.link = car
                car.link = self
                print ('link a car~')

    def disconect(self):
        PHONELIST.remove(self)
        if self.link:
            dic = {'type':'disconnect'}
            js = json.dumps(dic)
            self.link.request.send(js.encode())
            self.link.link =None

#HOST,PORT = "172.16.31.10",8080
HOST,PORT = "211.159.168.54",8080

server=socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler)#mutilt
server.serve_forever()


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

from django.shortcuts import render

# Create your views here.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test
from django.http import JsonResponse, HttpResponse

from models import *
from django.db.models import Q
from collections import defaultdict

from django.shortcuts import render
from pprint import pprint
import json, datetime

@login_required
def session_handler(request):
    if request.method == 'POST':  # handler for when request is first made
        if request.POST.get('CreateSession', 0):
            Session.objects.create(user=request.user, sessionName=request.POST.get('sessionName', ''))

        else:
            session = Session.objects.get(user=request.user, sessionName=request.POST.get('sessionName', ''))
            if request.POST.get('CreateSessionExercise', 0):
                exercise = UserExercises.objects.get(user=request.user,
                                                     exercise__exerciseName=request.POST.get('exerciseName', ''))
                ExercisesInSession.objects.create(session=session, exercise=exercise)
            if request.POST.get('CreateSessionSchedule', 0):
                date_list = json.loads(request.POST.get('sessionDates', 0))
                for date in date_list:
                    date_obj = datetime.datetime.strptime(date, "%d-%m-%Y").date()
                    ExerciseSessionSchedule.objects.create(session=session, chosenDays=date_obj)

@login_required
def user_exercise_handler(request):
    if request.method == 'POST':
        exercise = ExerciseName.objects.get(exerciseName=request.POST.get('exerciseName', ''))
        if request.POST.get('CreateUserExercise', 0):
            UserExercises.objects.create(user=request.user, exercise=exercise)
        else:
            exercise = UserExercises.objects.get(user=request.uesr, exercise=exercise)
            if request.POST.get('CreateExerciseDetails', 0):
                reps = json.loads(request.POST.get('exerciseReps', 0))
                for i in enumerate(reps):
                    UserExercisesSetReps.objects.create(userExercise=exercise, setNumber=i, reps=reps[i])
                pass


from django.contrib import admin
from django.urls import path, include
# from albums.views import AlbumList
# from .views import *
from . import views

urlpatterns = [
  # route for home
  path('', views.home, name='home'),
  # route for about
  path('about/', views.about, name='about'),
  # route for album index
  path('albums/', views.album_index, name='index'),
  # route for album detail
  path('albums/<int:album_id>/', views.albums_detail, name='detail'),
  # route used to show a form and create an album
  path('albums/create/', views.AlbumCreate.as_view(), name='albums_create'),
  path('albums/<int:pk>/update/', views.AlbumUpdate.as_view(), name='albums_update'),
  path('albums/<int:pk>/delete/', views.AlbumDelete.as_view(), name='albums_delete'),
  path('albums/<int:album_id>/add_song/', views.add_song, name='add_song'),
  path('albums/<int:album_id>/add_photo/', views.add_photo, name='add_photo'),
  path('admin/', admin.site.urls),
  path('accounts/signup/', views.signup, name='signup'),
  # include the built-in auth urls for the built-in views
  # path('accounts/', include('django.contrib.auth.urls')),
]


#!/usr/bin/python3
"""queries the Reddit API and prints the titles of the first 10 hot posts
listed for a given subreddit.
"""
import requests

def top_ten(subreddit):
    """Return the number of subscribers """
    URL = 'https://www.reddit.com/r/{}/hot.json?limit=10'.format(subreddit)
    headers = {
        'User-Agent':
        'AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75'}
    r = requests.get(URL, headers=headers, allow_redirects=False)
    if r.status_code == 200:
        children = r.json().get('data').get('children')
        for child in children:
            print('{}'.format(child.get('data').get('title')))
    else:
        print(None)


"""Main router for the server.

Author: jon-chow
Created: 2023-06-13
Last Modified: 2023-07-15
"""

import json
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from pydantic import BaseModel

from main import genshin_scraper
from utils.scrapers import get_all_names

# ---------------------------------------------------------------------------- #
#                                     SETUP                                    #
# ---------------------------------------------------------------------------- #

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.title = "Genshin JSON Data Generator API"
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

origins = [
  "http://localhost:3000",
]

app.add_middleware(
  CORSMiddleware,
  allow_origins=origins,
  allow_credentials=True,
  allow_methods=["*"],
  allow_headers=["*"],
)

class GetParams(BaseModel):
  category: str | None = ""

class RunParams(BaseModel):
  function: str = ""
  category: str | None = ""
  folders: list | None = []

# ---------------------------------------------------------------------------- #
#                                    ROUTES                                    #
# ---------------------------------------------------------------------------- #

@app.post('/get')
@limiter.limit("1/second")
async def get(request: Request, body: GetParams):
  """
  Gets the data from the specified category.
  POST /get
  """
  data = []
  try:
    # Get parameters.
    category = "" if body.category is None else body.category

    data = get_all_names(category=category) if category != "" else []
  except Exception as e:
    return {
      "status": "error",
      "message": str(e),
    }

  return {
    "status": "success",
    "message": data,
  }

@app.post('/run')
@limiter.limit("1/second")
async def run(request: Request, body: RunParams):
  """
  Runs the scraper with the specified parameters.
  POST /run
  """
  try:
    # Get parameters.
    function = "" if body.function is None else body.function
    category = "" if body.category is None else body.category
    folders = [] if body.folders is None else body.folders

    genshin_scraper(function=function, category=category, folders=folders)
  except Exception as e:
    return {
      "status": "error",
      "message": str(e),
    }

  return {
    "status": "success",
    "message": "Successfully ran scraper.",
  }

if __name__ == '__main__':
  uvicorn.run(
    "server:app",
    host="localhost",
    port=3001,
    reload=True,
  )


import math
import matplotlib.pyplot as plt

z_1_0, z_1_1 = 5, 7
z_2_0 = 6
z_3_0, z_3_1 = 2, 7

trials = [100, 500, 1000]

for n in trials:
    z1_list = list()
    z1_list.append(z_1_0)
    z1_list.append(z_1_1)
    z2_list = list()
    z2_list.append(z_2_0)
    z3_list = list()
    z3_list.append(z_3_0)
    z3_list.append(z_3_1)
    z_list = list()
    u_list = list()

    for i in range(1, n):
        z1 = (z1_list[i - 1] ** 2 + z1_list[i] ** 3 + 5) % 15
        z1_list.append(z1)
    # print(len(z1_list))
    for i in range(n):
        z2 = (z2_list[i] + 4 * z2_list[i]) % 16
        z2_list.append(z2)
    # print(len(z2_list))

    for i in range(1, n):
        z3 = (3 * z3_list[i] + z3_list[i - 1] ** 3) % 15
        z3_list.append(z3)
    # print(len(z1_list))

    for i in range(1, n + 1):
        z = (z1_list[i] + 5 * (z2_list[i] ** 3) + 3 * z3_list[i]) % 16  # change
        z_list.append(z)
        u_list.append(z / 16)
    # print(len(z_list))
    print(f'For n = {n}, the list of U is ')
    print(u_list)
    n = [str(i) for i in range(n)]
    plt.bar(n, u_list, label='Random Number')
    plt.legend(loc='upper right')
    plt.show()


import sys
import pika
import base64
import time

import os
dir_path = os.path.dirname(os.path.realpath(__file__))

def size(b64string):
    return (len(b64string) * 3) / 4

def send(msg):
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='hello')
    channel.basic_publish(exchange='', routing_key='hello', body=encoded_string)
    connection.close()
    return time.time()

def receive():
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='hello')
    method_frame, header_frame, body = channel.basic_get(queue = 'hello')
    if method_frame.NAME == 'Basic.GetEmpty':
        connection.close()
        return ''
    else:
        channel.basic_ack(delivery_tag=method_frame.delivery_tag)
        connection.close()
        return time.time()

def calculate(i, sTime, rTime):
    val = rTime-sTime
    valms = int(round(val*1000))
    with open("test.txt", "a") as myFile:
        s = str(i) + ". " + str(valms) + "ms, " + str(val) + " unix\n"
        myFile.write(s)

for filename in os.listdir(dir_path):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        with open(filename, "rb") as imageFile:
            encoded_string = base64.b64encode(imageFile.read())
            with open("test.txt", "a") as myFile:
                s = "SENT " + str(size(encoded_string)) + " B\n"
                myFile.write(s)
            for i in range(1,11):
                sTime = send(encoded_string)
                rTime = receive()
                calculate(i, sTime, rTime)


import streamlit as st
import requests
from PIL import Image
from streamlit_lottie import st_lottie

st.set_page_config(page_title = 'My Portfolio', page_icon = ":telescope:", layout = "wide")

def load_lottieurl(url):
    r = requests.get(url)
    if r.status_code !=200:
        return None
    return r.json()

# USE LOCAL CSS
def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html = True)

local_css("style/style.css")
# ---- LOAD ASSETS ----
lottie_coding = load_lottieurl("https://assets10.lottiefiles.com/packages/lf20_3rwasyjy.json")
img_recommendation = Image.open("images/movie.jpg")
img_olympics = Image.open("images/olympics.png")

#------Header Section------#

with st.container():

    st.subheader("Hi, I am Nitin :wave:")
    st.title("A Data Scientist from India")
    st.write("I am passionate about Data Science & Machine Learning & its applications in "
             "Businesses & Humans. I would really like to work on Cognitive Artificial Intelligence")
    st.write("[Learn More >](https://Google.com)")

with st.container():
    st.write("--------")
    left_column, right_column = st.columns(2)
    with left_column:
        st.header("What I do")
        st.write("##")
        st.write("""
        BlaBla Bla blabalbal blabal bla & more blaBlaBla Bla blabalbal blabal bla & more bla
        - BlaBla Bla blabalbal blabal bla & more bla
        - BlaBla Bla blabalbal blabal bla & more bla
        - BlaBla Bla blabalbal blabal bla & more bla
        - BlaBla Bla blabalbal blabal bla & more bla

        if this sounds interesting to you, please BlaBla Bla blabalbal blabal bla & more bla some more

        """)

    with right_column:
        st_lottie(lottie_coding, height = 300, key = "coding")

        #---- PROJECTS ----#
with st.container():

    st.write("---")
    st.header("My Data Science Projects")
    st.write("##")
    image_column, text_column = st.columns((1,2))
    with image_column:
        st.image(img_recommendation)
    with text_column:
        st.subheader(" Movie Recommender System")
        st.write(
            """
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
            """
        )
        st.markdown("[Watch GitHub Repo](https://github.com/nitinportfolio/gitportfolio.git)")

    st.write("#")
    st.write("#")
    st.write("##")
with st.container():

    image_column, text_column = st.columns((1,2))
    with image_column:
        st.image(img_olympics)
    with text_column:
        st.subheader(" Movie Recommender System")
        st.write(
            """
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
            """
        )
        st.markdown("[Watch GitHub Repo](https://github.com/nitinportfolio/gitportfolio.git)")

# ---- CONTACT ----
with st.container():
    st.write("---")
    st.header("Get In Touch With Me!")
    st.write("##")

    # Documention: https??formsubmit.com/ !!!
    contact_form = """
    <form action="https://formsubmit.co/nitinportfolio.ai@gmail.com" method="POST">
    <input type = "hidden" name="_captcha" value="false">
     <input type="text" name="name" placeholder = "Your Name" required>
     <input type="email" name="email" placeholder = "Your Email" required>
     <textarea name = "message" placeholder = Your message here" required></textarea>
     <button type="submit">Send</button>
</form>
    """
    left_column, right_column = st.columns(2)
    with left_column:
        st.markdown(contact_form, unsafe_allow_html = True)
    with right_column:
        st.empty()


from unittest.mock import sentinel

import pytest
from h_matchers import Any
from pyramid.httpexceptions import HTTPClientError, HTTPUnsupportedMediaType
from pyramid.testing import DummyRequest
from pytest import param

from via.exceptions import BadURL, UnhandledException
from via.views.exceptions import EXCEPTION_MAP, all_exceptions

class TestErrorView:
    @pytest.mark.parametrize(
        "exception_class,status_code",
        (
            param(
                HTTPUnsupportedMediaType,
                HTTPUnsupportedMediaType.code,
                id="Pyramid error",
            ),
            param(BlockingIOError, 417, id="Unknown python error"),
        ),
    )
    def test_values_are_copied_from_the_exception(
        self, exception_class, status_code, pyramid_request
    ):
        exception = exception_class("details string")

        values = all_exceptions(exception, pyramid_request)

        assert values == Any.dict.containing(
            {
                "exception": Any.dict.containing(
                    {"class": exception.__class__.__name__, "details": "details string"}
                ),
                "status_code": status_code,
            }
        )

        assert pyramid_request.response.status_int == status_code

    @pytest.mark.parametrize(
        "exception_class,mapped_exception",
        (
            param(BadURL, BadURL, id="Mapped directly"),
            param(HTTPUnsupportedMediaType, HTTPClientError, id="Inherited"),
            param(BlockingIOError, UnhandledException, id="Unmapped"),
        ),
    )
    def test_we_fill_in_other_values_based_on_exception_lookup(
        self, exception_class, mapped_exception, pyramid_request
    ):
        exception = exception_class("details string")

        values = all_exceptions(exception, pyramid_request)

        assert values["exception"] == Any.dict.containing(
            EXCEPTION_MAP[mapped_exception]
        )

    @pytest.mark.parametrize("doc_url", (sentinel.doc_url, None))
    def test_it_reads_the_urls_from_the_request(self, pyramid_request, doc_url):
        pyramid_request.GET["url"] = doc_url
        pyramid_request.url = sentinel.request_url

        values = all_exceptions(ValueError(), pyramid_request)

        assert values["url"] == {"original": doc_url, "retry": sentinel.request_url}

    @pytest.fixture
    def pyramid_request(self):
        return DummyRequest()


#Abhijeet Solanki T00221273
#Project: 1 Dataset:4
#CSC-6903-001
import numpy as np
import tensorflow as tf
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout, BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
import matplotlib.pyplot as plt
import pandas as pd

#1. Load the MNIST dataset and modify it:
# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Modify the dataset

def concatenate_images(images, labels, is_train=True):
    if is_train:
        first = images[:40000]
        second = images[10000:50000]
        third = images[20000:60000]
    else:
        first = images[:8000]
        second = images[1000:9000]
        third = images[2000:10000]

    concatenated_images = np.hstack((first, second, third)).reshape(-1, 28, 84)

    if is_train:
        concatenated_labels = labels[:40000] * 100 + labels[10000:50000] * 10 + labels[20000:60000]
    else:
        concatenated_labels = labels[:8000] * 100 + labels[1000:9000] * 10 + labels[2000:10000]

    return concatenated_images, concatenated_labels

train_images_modified, train_labels_modified = concatenate_images(train_images, train_labels)
test_images_modified, test_labels_modified = concatenate_images(test_images, test_labels, is_train=False)

print("Shapes:", train_images_modified.shape, train_labels_modified.shape, test_images_modified.shape, test_labels_modified.shape)

# Normalize the data
train_images_modified = train_images_modified.astype('float32') / 255.0
test_images_modified = test_images_modified.astype('float32') / 255.0

#2. Define the fully connected neural network:
model = Sequential([
    Flatten(input_shape=(28, 84)),
    Dense(512, activation='relu', kernel_initializer='he_normal'),
    Dropout(0.2),
    Dense(256, activation='relu', kernel_initializer='he_normal'),
    Dropout(0.2),
    Dense(128, activation='relu', kernel_initializer='he_normal'),
    Dropout(0.2),
    Dense(64, activation='relu', kernel_initializer='he_normal'),
    Dropout(0.2),
    Dense(1000, activation='softmax')
])

#3. Train the neural network:
# Hyperparameters
batch_size = 128
epochs = 100
learning_rate = 0.001

# Compile the model
model.compile(optimizer=Adam(learning_rate=learning_rate),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Early stopping and learning rate reduction on plateau
early_stopping = EarlyStopping(monitor='val_loss', patience=5)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=0.0001)

# Train the model
history = model.fit(train_images_modified, train_labels_modified,
                    batch_size=batch_size,
                    epochs=epochs,
                    validation_data=(test_images_modified, test_labels_modified),
                    callbacks=[early_stopping, reduce_lr])

#4. Evaluate the model:
train_accuracy = history.history['accuracy'][-1]
test_accuracy = history.history['val_accuracy'][-1]
total_parameters = np.sum([np.prod(v.get_shape().as_list()) for v in model.trainable_variables])
ratio = test_accuracy / (total_parameters ** (1/10))

# Create a dictionary with the results and hyperparameters
results = {
    'Mini-batch size': [batch_size],
    'Number of hidden layers': [len(model.layers) - 2],
    'Number of hidden units': [[256, 128, 64]],
    'Total number of parameters': [total_parameters],
    'Optimization method used': ['Adam'],
    'Learning rate': [learning_rate],
    'Initializer': ['Glorot Uniform'],
    'Regularization': ['Dropout (0.5)'],
    'Number of training epochs': [len(history.history['loss'])],
    'Final training accuracy': [train_accuracy],
    'Final testing accuracy': [test_accuracy],
    'Ratio': [ratio]
}
# Set pandas display options to show all columns
pd.set_option('display.max_columns', None)

# Convert the dictionary to a pandas DataFrame
df = pd.DataFrame(results)

# Display the DataFrame
print(df)

#5. Plot the convergence curve:
plt.figure(figsize=(12, 4))

# Plotting loss
plt.subplot(1, 3, 1)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.legend()
plt.title('Loss Convergence')

#5. Plot the convergence curve:
plt.figure(figsize=(10, 4))

# Plotting loss
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('Loss Convergence')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

# Plotting accuracy convergence
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Test Accuracy')
plt.title('Accuracy Convergence')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.tight_layout()
plt.show()


import os
import sys
from setuptools import setup

version = '0.1.6'

readme = open('README.md', 'r')

if sys.argv[-1] == 'publish':
    if os.system("pip freeze | grep wheel"):
        print("wheel not installed.\nUse `pip install wheel`.\nExiting.")
        sys.exit()
    if os.system("pip freeze | grep twine"):
        print("twine not installed.\nUse `pip install twine`.\nExiting.")
        sys.exit()
    os.system("python setup.py sdist bdist_wheel")
    os.system("twine upload dist/*")
    print("You probably want to also tag the version now:")
    print("  git tag -a %s -m 'version %s'" % (version, version))
    print("  git push --tags")
    sys.exit()

setup(
    name='atc_thrift',
    version=version,
    description='ATC Thrift Library',
    author='Emmanuel Bretelle',
    author_email='chantra@fb.com',
    url='https://github.com/facebook/augmented-traffic-control',
    packages=['atc_thrift'],
    classifiers=['Programming Language :: Python', ],
    long_description=readme.read(),
    install_requires=['thrift']
)


import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(0)

mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
prev_time = 0
curr_time = 0
while True:
    success, img = cap.read()
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(img_rgb)
    # print(results.multi_hand_landmarks)
    #If there are hands detected in an image
    if results.multi_hand_landmarks:
        #Loop through the landmarks
        for handlms in results.multi_hand_landmarks:
            #Loop through each hands' every landmark and get their coordinates
            for id, landmark in enumerate(handlms.landmark):
                height, width, channel = img.shape

                #Position of center (x and y coordinates)
                #The coordinates are in decimals and we need pixel values, so to get them we multiply x by width and y by height of image.
                cx, cy = int(landmark.x * width), int(landmark.y * height)
                # print(id, cx, cy)

                #Let's try and locate a specific landmark on img
                cv2.circle(img, (cx, cy), 10, (255,0,255))
                cv2.putText(img, str(id), (cx+10, cy+20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 1)

            #Draw hand landmarks on image and connect the landmarks
            mpDraw.draw_landmarks(img, handlms, mpHands.HAND_CONNECTIONS)

    #Calculating FPS
    curr_time = time.time()
    fps = 1/(curr_time - prev_time)
    prev_time = curr_time

    #Showing FPS on screen
    cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 255), 3)

    #Display Image
    cv2.imshow("Image", img)
    cv2.waitKey(1)


import sys
from enum import Enum
from typing import Union, Callable, Any

import keyboard
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QThread, QObject, QEvent
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QDesktopWidget, QMainWindow

from src.LinkableValue import editLinkableValue
from src.utils import distance
from src.UIPrimitives import Point, Line, Rect, Text, Image

UIPrimitive = Union[Point, Line, Rect, Text, Image]

FPS = 60
FRAME_TIME = int(1000 / FPS)

class KeyboardKeys(Enum):
    esc = 1
    ctrl = 29
    shift = 42
    alt = 56
    tab = 15
    enter = 28
    space = 57

class _Window(QMainWindow):
    objects: list[UIPrimitive] = []
    keysCallbacks: dict[KeyboardKeys, (Callable, list[Any])] = {}
    mousePressCallbacks: list[(Callable, list[Any])] = []
    mouseReleaseCallbacks: list[(Callable, list[Any])] = []
    mouseMoveCallbacks: list[(Callable, list[Any])] = []
    currentMovingObject = None

    def __init__(self, opacity=1.0, w=None, h=None):
        QMainWindow.__init__(self, None,
                             Qt.FramelessWindowHint | Qt.MSWindowsFixedSizeDialogHint | Qt.WindowStaysOnTopHint
                             # | Qt.Popup | Qt.WindowDoesNotAcceptFocus | Qt.WindowTransparentForInput
                             )
        fillScreenGeometry = QDesktopWidget().availableGeometry()
        self.w = w or fillScreenGeometry.width()
        self.h = h or fillScreenGeometry.height()

        self.resize(self.w, self.h)
        self.setFixedSize(self.w, self.h)

        self.setAttribute(Qt.WA_TransparentForMouseEvents)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowOpacity(opacity)

        def onKeyboardEvent(event: keyboard.KeyboardEvent):
            if event.event_type != keyboard.KEY_DOWN:
                return

            for key in self.keysCallbacks.keys():
                if event.scan_code == key:
                    self.keysCallbacks[key][0](*self.keysCallbacks[key][1])
        keyboard._listener.add_handler(onKeyboardEvent)

        _ = self.startTimer(FRAME_TIME)

    def getCenter(self):
        return self.w / 2, self.h / 2

    def timerEvent(self, t):
        self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        for obj in self.objects:
            obj.render(painter)

    def mousePressEvent(self, event: QtGui.QMouseEvent) -> None:
        for obj in self.objects:
            if isinstance(obj, Point) and obj.movable and (
                    distance(obj.x, obj.y, event.x(), event.y()) <= (obj.size / 2)):
                self.currentMovingObject = obj
                break

        for callback in self.mousePressCallbacks:
            callback[0](event.x(), event.y(), *callback[1])

    def mouseMoveEvent(self, event: QtGui.QMouseEvent) -> None:
        if self.currentMovingObject is None:
            return

        self.currentMovingObject.x = editLinkableValue(self.currentMovingObject.x, event.x())
        self.currentMovingObject.y = editLinkableValue(self.currentMovingObject.y, event.y())

        onMoveCallback = getattr(self.currentMovingObject, 'onMoveCallback')
        if onMoveCallback is not None:
            onMoveCallback()

        for callback in self.mouseMoveCallbacks:
            callback[0](event.x(), event.y(), *callback[1])

    def mouseReleaseEvent(self, event: QtGui.QMouseEvent) -> None:
        self.currentMovingObject = None
        for callback in self.mouseReleaseCallbacks:
            callback[0](event.x(), event.y(), *callback[1])

    def addObject(self, obj: UIPrimitive):
        self.objects.append(obj)
        return obj

    def clear(self):
        self.objects = []

    def getObjectsByFilter(self, objectType: UIPrimitive):
        res = []
        for obj in self.objects:
            if isinstance(obj, objectType):
                res.append(obj)
        return res

    def setKeyCallback(self, key: KeyboardKeys, callback: Callable, *args: list[Any]):
        self.keysCallbacks[key.value] = (callback, args)

    def setMouseCallback(self, eventType: QEvent.Type, callback: Callable, *args: list[Any]):
        if eventType == QEvent.MouseButtonPress:
            self.mousePressCallbacks.append((callback, args))
        elif eventType == QEvent.MouseButtonRelease:
            self.mouseReleaseCallbacks.append((callback, args))
        elif eventType == QEvent.MouseMove:
            self.mouseMoveCallbacks.append((callback, args))

    def clearKeyCallbacks(self):
        self.keysCallbacks = {}

    def clearMouseCallbacks(self):
        self.mousePressCallbacks = []
        self.mouseReleaseCallbacks = []
        self.mouseMoveCallbacks = []

    def clearAll(self):
        self.clearKeyCallbacks()
        self.clearMouseCallbacks()
        self.clear()

class _Worker(QObject):
    def __init__(self, foo):
        super().__init__()
        self.foo = foo

    def work(self):
        self.foo()

class OverlayUI(_Window):
    def __init__(self, opacity=1.0):
        print("UI INIT!")
        self.app = QApplication(sys.argv)
        _Window.__init__(self, opacity=opacity)

    def start(self, otherProcessFoo):
        self.otherProcessThread = QThread()
        otherProcessWorker = _Worker(otherProcessFoo)
        otherProcessWorker.moveToThread(self.otherProcessThread)

        self.otherProcessThread.started.connect(otherProcessWorker.work)
        self.otherProcessThread.start()

        self.show()

        sys.exit(self.app.exec_())


#!/usr/bin/env python
# coding: utf-8

# In[21]:

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import sweep_poly, chirp, spectrogram

from sklearn.decomposition import FastICA, PCA

# Generate the data
np.random.seed(1)
n = 1000
t = np.linspace(0, 10, n)
p = np.poly1d([0.025, -0.36, 1.25, 2.0])

s1 = np.cos(2 * t)  # Signal 1
s2 = sweep_poly(t, p) # Signal 2
s3 = chirp(t, f0=6, f1=1, t1=10, method='linear') # Signal 3

S = np.c_[s1, s2, s3]
S += 0.2 * np.random.normal(size=S.shape)  # Add noise

S /= S.std(axis=0)  # Standardized data

# Mix data
A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]])  # Mixing matrix
X = np.dot(S, A.T)  # Observations

# ICA
ica = FastICA(n_components=3)
S_ = ica.fit_transform(X)
A_ = ica.mixing_  # estimated mixing matrix

# PCA
pca = PCA(n_components=3)
H = pca.fit_transform(X)

#Plot
plt.figure()

models = [S, X, S_, H]
names = ['True signals',
         'Observations',
         'ICA signals',
         'PCA signals']
colors = ['red', 'orange', 'steelblue']

for ii, (model, name) in enumerate(zip(models, names), 1):
    plt.subplot(4, 1, ii)
    plt.title(name)
    for sig, color in zip(model.T, colors):
        plt.plot(sig, color=color)

plt.tight_layout()
plt.show()

# In[ ]:


from pathlib import Path
from pkg_resources import resource_filename

"""
Paths to data files in this Python module.
"""

# The path to the data files.
DATA_DIRECTORY = Path(resource_filename(__name__, "data"))
# The path to object data.
OBJECT_DATA_DIRECTORY = DATA_DIRECTORY.joinpath("objects")
# The path to object categories dictionary.
OBJECT_CATEGORIES_PATH = OBJECT_DATA_DIRECTORY.joinpath("categories.json")
# The path to the dictionary of which sides per object are convex.
CONVEX_SIDES_PATH = OBJECT_DATA_DIRECTORY.joinpath("convex.json")
# The path to the scene data.
SCENE_DATA_DIRECTORY = DATA_DIRECTORY.joinpath("scenes")
# The path to the dictionary of where the robot can spawn.
SPAWN_POSITIONS_PATH = SCENE_DATA_DIRECTORY.joinpath("spawn_positions.json")
# The directory for occupancy maps.
OCCUPANCY_MAPS_DIRECTORY = SCENE_DATA_DIRECTORY.joinpath("occupancy_maps")
# The directory for room maps.
ROOM_MAPS_DIRECTORY = SCENE_DATA_DIRECTORY.joinpath("room_maps")
# The directory of Magnebot data.
MAGNEBOT_DIRECTORY = DATA_DIRECTORY.joinpath("magnebot")
# The path to the turn constants data.
TURN_CONSTANTS_PATH = MAGNEBOT_DIRECTORY.joinpath("turn_constants.csv")
# The directory for IK orientation data.
IK_DIRECTORY = MAGNEBOT_DIRECTORY.joinpath("ik")
# The path to the IK target positions.
IK_POSITIONS_PATH = IK_DIRECTORY.joinpath("positions.npy")
# The path to the IK orientations data for the left arm.
IK_ORIENTATIONS_LEFT_PATH = IK_DIRECTORY.joinpath("orientations_left.npy")
# The path to the IK orientations data for the right arm.
IK_ORIENTATIONS_RIGHT_PATH = IK_DIRECTORY.joinpath("orientations_right.npy")


import time

from selenium.webdriver import ActionChains

from pages.BaseApp import BasePage
from src_pages.MainPageElements import *
from src_pages.Text.MainPageText import key_search, correct_text, not_correct_text, correct_basket_text

class MainPage(BasePage):
    def close_shipping_window(self, close_lct):
        time.sleep(7)
        self.click_btn(close_lct)
        time.sleep(7)

    def open_header_links(self, browser, locator):
        for i in range(0, self.find_len(locator)):
            print(i)
            books_btn = self.find_element(LCT_BOOKS_BTN)
            self.hover_over(browser, books_btn)
            time.sleep(1.5)
            elements = self.find_elements(locator)[i]
            self.hover_over(browser, elements)
            self.click_js(browser, elements)
            time.sleep(1.5)
            self.go_to_back()
            print(i)

    def open_more_header_links(self, browser):
        for i in range(3):
            more_btn = self.find_element(LCT_MORE_BTN)
            self.hover_over(browser, more_btn)
            time.sleep(1.5)
            self.click_btn(get_more_header_btn_lct(i))
            time.sleep(1.5)
            self.go_to_back()

    def open_links_club(self, browser, locator):
        for i in range(0, self.find_len(locator)):
            time.sleep(1.5)
            elements = self.find_elements(locator)
            self.click_js(browser, elements[i])
            time.sleep(1.5)
            self.go_to_site(self.base_url+'club/')

    def search_block(self, browser):
        for i in range(len(key_search)):
            action = ActionChains(browser)
            action.move_to_element(self.find_element(LCT_INPUT_SEARCH)).perform()
            self.clear_input(LCT_INPUT_SEARCH)
            self.send_answer(LCT_INPUT_SEARCH, key=key_search[i])
            self.click_btn(LCT_SUBMIT_SEARCH_BTN)
            time.sleep(1)
            if i == 0:
                answer = self.get_text(LCT_CORRECT_ANSWER_SEARCH_TEXT)
                assert answer == correct_text, f"Текст НЕ соответствует: факт.текст. {answer}, ожид.текст. {correct_text}"
            else:
                answer = self.get_text(LCT_NOT_CORRECT_ANSWER_TEXT)
                assert answer == not_correct_text, f"Текст НЕ соответствует: факт.текст. {answer}, ожид.текст. {correct_text}"

    def check_text_basket(self, browser):
        answer = self.get_text(LCT_NTFCN_BASKET)
        assert correct_basket_text in answer, f"Текст НЕ соответствует: факт.текст. {answer}, ожид.текст. {correct_basket_text}"


import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets, Qt

class Ui_Dialog(object):
    def __init__(self, mode, fontsize):
        self.mainDialog = QtWidgets.QDialog()
        self.mainDialog.resize(1024, 600)
        self.mainDialog.setWindowTitle("음성인식 점자프린터")

        # Layout
        self.mainLayout = QtWidgets.QGridLayout() # Buttons
        self.mainLayout_2 = QtWidgets.QGridLayout() # mainInfo and spacer
        self.mainLayout_3 = QtWidgets.QGridLayout() # workTable and listView
        self.mainLayout_4 = QtWidgets.QGridLayout(self.mainDialog) # mainLayout and mainLayout_2
        self.mainLayout.setContentsMargins(75, 0, 75, 50)
        self.mainLayout_3.setContentsMargins(75, 0, 75, 0)
        self.mainLayout_4.addLayout(self.mainLayout_2, 0, 0, 1, 1)
        self.mainLayout_2.setContentsMargins(20, 30, 20, 30)
        self.mainLayout_4.addLayout(self.mainLayout_3, 1, 0, 1, 1)
        self.mainLayout_4.addLayout(self.mainLayout, 2, 0, 1, 1)

        # Button
        self.mainBtnlist = [QtWidgets.QPushButton()] * 4
        for i in range(4):
            self.mainBtnlist[i] = QtWidgets.QPushButton(self.mainDialog)
            self.mainBtnlist[i].setMinimumSize(QtCore.QSize(300, 75))
            self.mainBtnlist[i].setStyleSheet('font-size:'+str(fontsize)+'px;')
            self.mainLayout.addWidget(self.mainBtnlist[i], int(i//2), int(i%2), 1, 1)

        # mainInfo
        self.mainInfo = QtWidgets.QTextBrowser(self.mainDialog)
        self.mainInfo.setMinimumSize(QtCore.QSize(0, 45))
        self.mainInfo.setMaximumSize(QtCore.QSize(500, 45))
        self.mainInfo.setStyleSheet('font-size:'+str(fontsize)+'px;')
        self.mainLayout_2.addWidget(self.mainInfo, 0, 0, 1, 1)

        #listView
        self.itemList = os.listdir('C://')
        self.listView = QtWidgets.QListView()
        self.model = QtGui.QStandardItemModel()
        for item in self.itemList:
            self.model.appendRow(QtGui.QStandardItem(item))
        self.listView.setModel(self.model)
        self.listView.setStyleSheet('font-size:'+str(fontsize)+'px;')
        self.mainLayout_3.addWidget(self.listView, 0, 0, 1, 1)

        QtCore.QMetaObject.connectSlotsByName(self.mainDialog)
        self.mainDialog.show()

    def refresh_ui(self, text_list, ints):
        for i in range(5):
            if i == 0 : self.mainInfo.setText(text_list[i])
            else : self.mainBtnlist[i-1].setText(text_list[i])
        print(ints, text_list[0])

def main():
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_Dialog("extend",30)
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()


"""
=================================================================
3.1. Cross-validation: evaluating estimator performance（交叉验证）
=================================================================

('BaseCrossValidator',
'GridSearchCV',
'TimeSeriesSplit',
'KFold',
'GroupKFold',
'GroupShuffleSplit',
'LeaveOneGroupOut',
'LeaveOneOut',
'LeavePGroupsOut',
'LeavePOut',
'RepeatedKFold',
'RepeatedStratifiedKFold',
'ParameterGrid',
'ParameterSampler',
'PredefinedSplit',
'RandomizedSearchCV',
'ShuffleSplit',
'StratifiedKFold',
'StratifiedShuffleSplit',
'check_cv',
'cross_val_predict',
'cross_val_score',
'cross_validate',
'fit_grid_point',
'learning_curve',
'permutation_test_score',
'train_test_split',
'validation_curve')

多评价函数： GridSearchCV, RandomizedSearchCV and cross_validate.
"""

def print_train_test_split():
    """测试 数据集切割"""
    from sklearn.model_selection import train_test_split
    from sklearn import datasets
    from sklearn import svm

    iris = datasets.load_iris()
    print(iris.data.shape, iris.target.shape)

    X_train, X_test, y_train, y_test = train_test_split(
        iris.data, iris.target, test_size=0.4, random_state=0)

    print(X_train.shape, y_train.shape)
    print(X_test.shape, y_test.shape)

    clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
    print(clf.score(X_test, y_test))

    ################################
    from sklearn.model_selection import cross_val_score

    clf = svm.SVC(kernel='linear', C=1)
    scores = cross_val_score(clf, iris.data, iris.target, cv=5, scoring='f1_macro')
    print(scores)
    print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

    ################################
    from sklearn.model_selection import ShuffleSplit
    cv = ShuffleSplit(n_splits=5, test_size=0.3, random_state=0)
    print(cross_val_score(clf, iris.data, iris.target, cv=cv))

    ################################
    from sklearn import preprocessing
    X_train, X_test, y_train, y_test = train_test_split(
        iris.data, iris.target, test_size=0.4, random_state=0)
    scaler = preprocessing.StandardScaler().fit(X_train)
    print(scaler.mean_)
    print(scaler.var_)
    X_train_transformed = scaler.transform(X_train)
    clf = svm.SVC(C=1).fit(X_train_transformed, y_train)
    X_test_transformed = scaler.transform(X_test)
    print(clf.score(X_test_transformed, y_test))

    ##################################
    from sklearn.pipeline import make_pipeline
    clf = make_pipeline(preprocessing.StandardScaler(), svm.SVC(C=1))
    print(cross_val_score(clf, iris.data, iris.target, cv=cv))

    ##################################
    # 多个评分函数，字典形式返回
    # cross_validate()
    from sklearn.model_selection import cross_validate
    scoring = ['precision_macro', 'recall_macro', 'f1_macro']
    clf = svm.SVC(kernel='linear', C=1, random_state=0)
    scores = cross_validate(clf, iris.data, iris.target, scoring=scoring,
                            cv=5, return_train_score=False)
    print(scores.keys())

    print(scores['test_recall_macro'])

def plot_roc_crossval():
    """
    =============================================================
    Receiver Operating Characteristic (ROC) with cross validation
    =============================================================

    Example of Receiver Operating Characteristic (ROC) metric to evaluate
    classifier output quality using cross-validation.

    ROC curves typically feature true positive rate on the Y axis, and false
    positive rate on the X axis. This means that the top left corner of the plot is
    the "ideal" point - a false positive rate of zero, and a true positive rate of
    one. This is not very realistic, but it does mean that a larger area under the
    curve (AUC) is usually better.

    The "steepness" of ROC curves is also important, since it is ideal to maximize
    the true positive rate while minimizing the false positive rate.

    This example shows the ROC response of different datasets, created from K-fold
    cross-validation. Taking all of these curves, it is possible to calculate the
    mean area under curve, and see the variance of the curve when the
    training set is split into different subsets. This roughly shows how the
    classifier output is affected by changes in the training data, and how
    different the splits generated by K-fold cross-validation are from one another.

    .. note::

        See also :func:`sklearn.metrics.roc_auc_score`,
                 :func:`sklearn.model_selection.cross_val_score`,
                 :ref:`sphx_glr_auto_examples_model_selection_plot_roc.py`,

    """
    print(__doc__)

    import numpy as np
    from scipy import interp
    import matplotlib.pyplot as plt

    from sklearn import svm, datasets
    from sklearn.metrics import roc_curve, auc
    from sklearn.model_selection import StratifiedKFold

    # #############################################################################
    # Data IO and generation

    # Import some data to play with
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target
    X, y = X[y != 2], y[y != 2]
    n_samples, n_features = X.shape

    # Add noisy features
    random_state = np.random.RandomState(0)
    X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

    # #############################################################################
    # Classification and ROC analysis

    # Run classifier with cross-validation and plot ROC curves
    cv = StratifiedKFold(n_splits=6)
    classifier = svm.SVC(kernel='linear', probability=True,
                         random_state=random_state)

    tprs = []
    aucs = []
    mean_fpr = np.linspace(0, 1, 100)

    i = 0
    for train, test in cv.split(X, y):
        probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test])
        # Compute ROC curve and area the curve
        fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
        tprs.append(interp(mean_fpr, fpr, tpr))
        tprs[-1][0] = 0.0
        roc_auc = auc(fpr, tpr)
        aucs.append(roc_auc)
        plt.plot(fpr, tpr, lw=1, alpha=0.3,
                 label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))

        i += 1
    plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
             label='Chance', alpha=.8)

    mean_tpr = np.mean(tprs, axis=0)
    mean_tpr[-1] = 1.0
    mean_auc = auc(mean_fpr, mean_tpr)
    std_auc = np.std(aucs)
    plt.plot(mean_fpr, mean_tpr, color='b',
             label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
             lw=2, alpha=.8)

    std_tpr = np.std(tprs, axis=0)
    tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
    tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
    plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
                     label=r'$\pm$ 1 std. dev.')

    plt.xlim([-0.05, 1.05])
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()

def plot_grid_search_digits():
    """
    ============================================================
    Parameter estimation using grid search with cross-validation
    ============================================================

    This examples shows how a classifier is optimized by cross-validation,
    which is done using the :class:`sklearn.model_selection.GridSearchCV` object
    on a development set that comprises only half of the available labeled data.

    The performance of the selected hyper-parameters and trained model is
    then measured on a dedicated evaluation set that was not used during
    the model selection step.

    More details on tools available for model selection can be found in the
    sections on :ref:`cross_validation` and :ref:`grid_search`.

    """

    from __future__ import print_function

    from sklearn import datasets
    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import GridSearchCV
    from sklearn.metrics import classification_report
    from sklearn.svm import SVC

    print(__doc__)

    # Loading the Digits dataset
    digits = datasets.load_digits()

    # To apply an classifier on this data, we need to flatten the image, to
    # turn the data in a (samples, feature) matrix:
    n_samples = len(digits.images)
    X = digits.images.reshape((n_samples, -1))
    y = digits.target

    # Split the dataset in two equal parts
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.5, random_state=0)

    # Set the parameters by cross-validation
    tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                         'C': [1, 10, 100, 1000]},
                        {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

    scores = ['precision', 'recall']

    for score in scores:
        print("# Tuning hyper-parameters for %s" % score)
        print()

        clf = GridSearchCV(SVC(), tuned_parameters, cv=5,
                           scoring='%s_macro' % score)
        clf.fit(X_train, y_train)

        print("Best parameters set found on development set:")
        print()
        print(clf.best_params_)
        print()
        print("Grid scores on development set:")
        print()
        means = clf.cv_results_['mean_test_score']
        stds = clf.cv_results_['std_test_score']
        for mean, std, params in zip(means, stds, clf.cv_results_['params']):
            print("%0.3f (+/-%0.03f) for %r"
                  % (mean, std * 2, params))
        print()

        print("Detailed classification report:")
        print()
        print("The model is trained on the full development set.")
        print("The scores are computed on the full evaluation set.")
        print()
        y_true, y_pred = y_test, clf.predict(X_test)
        print(classification_report(y_true, y_pred))
        print()

    # Note the problem is too easy: the hyperparameter plateau is too flat and the
    # output model is the same for precision and recall with ties in quality.

def grid_search_text_feature_extraction():
    """
    ==========================================================
    Sample pipeline for text feature extraction and evaluation
    ==========================================================

    The dataset used in this example is the 20 newsgroups dataset which will be
    automatically downloaded and then cached and reused for the document
    classification example.

    You can adjust the number of categories by giving their names to the dataset
    loader or setting them to None to get the 20 of them.

    Here is a sample output of a run on a quad-core machine::

      Loading 20 newsgroups dataset for categories:
      ['alt.atheism', 'talk.religion.misc']
      1427 documents
      2 categories

      Performing grid search...
      pipeline: ['vect', 'tfidf', 'clf']
      parameters:
      {'clf__alpha': (1.0000000000000001e-05, 9.9999999999999995e-07),
       'clf__max_iter': (10, 50, 80),
       'clf__penalty': ('l2', 'elasticnet'),
       'tfidf__use_idf': (True, False),
       'vect__max_n': (1, 2),
       'vect__max_df': (0.5, 0.75, 1.0),
       'vect__max_features': (None, 5000, 10000, 50000)}
      done in 1737.030s

      Best score: 0.940
      Best parameters set:
          clf__alpha: 9.9999999999999995e-07
          clf__max_iter: 50
          clf__penalty: 'elasticnet'
          tfidf__use_idf: True
          vect__max_n: 2
          vect__max_df: 0.75
          vect__max_features: 50000

    """

    # Author: Olivier Grisel <olivier.grisel@ensta.org>
    #         Peter Prettenhofer <peter.prettenhofer@gmail.com>
    #         Mathieu Blondel <mathieu@mblondel.org>
    # License: BSD 3 clause

    from __future__ import print_function

    from pprint import pprint
    from time import time
    import logging

    from sklearn.datasets import fetch_20newsgroups
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.linear_model import SGDClassifier
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import Pipeline

    print(__doc__)

    # Display progress logs on stdout
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(levelname)s %(message)s')

    # #############################################################################
    # Load some categories from the training set
    categories = [
        'alt.atheism',
        'talk.religion.misc',
    ]
    # Uncomment the following to do the analysis on all the categories
    # categories = None

    print("Loading 20 newsgroups dataset for categories:")
    print(categories)

    data = fetch_20newsgroups(subset='train', categories=categories)
    print("%d documents" % len(data.filenames))
    print("%d categories" % len(data.target_names))
    print()

    # #############################################################################
    # Define a pipeline combining a text feature extractor with a simple
    # classifier
    pipeline = Pipeline([
        ('vect', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', SGDClassifier()),
    ])

    # uncommenting more parameters will give better exploring power but will
    # increase processing time in a combinatorial way
    parameters = {
        'vect__max_df': (0.5, 0.75, 1.0),
        # 'vect__max_features': (None, 5000, 10000, 50000),
        'vect__ngram_range': ((1, 1), (1, 2)),  # unigrams or bigrams
        # 'tfidf__use_idf': (True, False),
        # 'tfidf__norm': ('l1', 'l2'),
        'clf__max_iter': (5,),
        'clf__alpha': (0.00001, 0.000001),
        'clf__penalty': ('l2', 'elasticnet'),
        # 'clf__max_iter': (10, 50, 80),
    }

    # multiprocessing requires the fork to happen in a __main__ protected
    # block

    # find the best parameters for both the feature extraction and the
    # classifier
    grid_search = GridSearchCV(pipeline, parameters, cv=5,
                               n_jobs=-1, verbose=1)

    print("Performing grid search...")
    print("pipeline:", [name for name, _ in pipeline.steps])
    print("parameters:")
    pprint(parameters)
    t0 = time()
    grid_search.fit(data.data, data.target)
    print("done in %0.3fs" % (time() - t0))
    print()

    print("Best score: %0.3f" % grid_search.best_score_)
    print("Best parameters set:")
    best_parameters = grid_search.best_estimator_.get_params()
    for param_name in sorted(parameters.keys()):
        print("\t%s: %r" % (param_name, best_parameters[param_name]))

if __name__ == '__main__':
    pass

Model selection and evaluation/3.1. Cross-validation  evaluating estimator performance.py


# Seaborn

import seaborn as sns
import matplotlib.pyplot as plt

# scatterplot
sns.scatterplot(x=height,y=weight)
plt.show()

# Count Plot
# Take in lists of data nad plot bars for each category of data
sns.countplot(y=gender)
plt.show()

import pandas as pd
df = pd.read_csv('masculinity.csv')
df.head()

# countplot with a dataframe
sns.countplot(x='how_masculine', data=df)
plt.show()

# adding color to our graphs
tips = sns.load_dataset('tips')
tips.head()

hue_colors = {"yes":"black", "no":"red"}

sns.scatterplot(x='total_bill', y='tip',data=tips, hue='smoker', hue_order=['yes', 'no'], palette=hue_colors)
plt.show()

# You can use hexcodes to make your own colors
hue_colors = {"yes": '#808080',
				"no": "#00FF00"
}

# Relational plots
# Plots and Sub plots
relplot()

sns.scatterplot(x='total_bill', y='tip',data=tips)
# These are the same
sns.relplot(x='total_bill', y='tip',data=tips, kind="scatter")

# To arange subplots use col=
sns.relplot(x='total_bill', y='tip',data=tips, kind="scatter", col='smoker')
# to range them vertically use row=
sns.relplot(x='total_bill', y='tip',data=tips, kind="scatter", row='smoker')
# to use both...
sns.relplot(x='total_bill', y='tip',data=tips, kind="scatter", col='smoker', row='time')

col_wrap=2 #is an optional argument saying how many displays you want per column
sns.relplot(x='total_bill', y='tip',data=tips, kind="scatter", col='day', col_wrap=2, col_order=['Thur', 'Fri', 'Sat', 'Sun'])

# Customizing
# we have seen col, row, hue
# point size, style, and transparency

# size
size = 'size'
hue = 'size'

# point style
hue = 'smoker'
style = 'smoker'

# transparency
# alpha is a value that is set between 0 and 1
alpha = 0.4

# line plots
sns.relplot(x='hour',y='NO_2_mean', data=air_df_loc_mean,
	kind='line', style='location', hue='location', markers=True)

# With markers set, the lines will be made of markers too
# set dashes=False to make the lines solid again
dashes=False

# built in 95% confidence interval
sns.relplot(x='hour', y='NO_2', data=air_df, kind='line')

# To use standard deviation, set ci='sd'
sns.relplot(x='hour', y='NO_2', data=air_df, kind='line', ci='sd')

# Turn off the confidence interval by ci=None

# Categorical plots catplot
# Count plots and bar plots

catplot()
sns.catplot(x='how_masculine', data=masculinity_data, kind='count')
plt.show()

# to change the order of categories, make a list
category_order = ['No answer', 'not at all', 'not very', 'somewhat', 'very']
sns.catplot(x='how masculine', data=masculinity_data, kind='count', order=category_order)

# bar plots will show the mean of observations in each category
sns.catplot(x='day', y='total_bill', data=tips, kind='bar')
ci=None # will get rid of the confidence interval

# Box Plots
g = sns.catplot(x='time', y='total_bill', data=tips, kind='box')
# order = []
sym='' #omit outliers
#whiskers default to 1.5 * IQR
whis = 2.0
whis = [5,95] #5th percentile, 95th percentile
whis = [0,100] #set these to the min and max values

# Point Plots
sns.catplot(x='age',y='masculinity_important', hue='feel_masculine', kind='point')

# to remove the lines connecting the points
join = False

from numpy import median
# for median
esimator = median

# to show caps at the end of the intervals
capsize = 0.2

# turn off the confidence interval
ci = None


import boto3
import logging
import os
import re
from typing import Any

from botocore.exceptions import ClientError
from box import Box
from dotenv import find_dotenv, load_dotenv
from tomlkit import parse, items

logger = logging.getLogger(__name__)

class Settings:
    """Settings class that can be accessed using either dict notation (settings.get('abc')) or
    dot notation (settings.snowflake.password). Reads from toml file and requires a table/section
    called 'default', along with a table/section for each environment, e.g. [local], [dev], [prd].
    Supports nested tables with dot notation, e.g. [local.snowflake].
    Inspired by ConfigParser and dot notation implementation borrowed from DynaConf
    """

    TOML_TO_BUILTIN_MAP = {
        items.String: str,
        items.Bool: bool,
        items.Integer: int,
        items.Float: float,
    }

    INTERNAL_ATTRS = ["_store", "_ssm", "env"]

    def __init__(self, config_filepath: str = None, env: str = os.getenv("PROJECT_ENV", "local")):
        self._store = Box()
        self.env = env
        self._ssm = boto3.client("ssm", region_name="us-east-1")

        # Load from .env file if exists. Will set env variables for use in .ini files.
        load_dotenv(find_dotenv(usecwd=True), verbose=True)

        if config_filepath:
            self.load(config_filepath)

    def load(self, config_filepath: str):
        if not os.path.exists(config_filepath):
            raise OSError("Could not load the default or provided settings file.")

        self.clear()

        with open(config_filepath, "r") as f:
            settings_data = parse(f.read())

        if "default" not in settings_data:
            raise Exception("Settings file missing required section 'default'")

        for table, items in settings_data.items():
            if table.startswith(self.env) or table == "default":
                for k, v in items.items():
                    self._set_value_from_config(k, v)

    def clear(self):
        self._store = Box()

    def _set_value_from_config(self, name: str, value: Any, parent: str = None):
        if name.upper() in os.environ and not parent:
            self.set_attr(name, os.getenv(name.upper()), parent)
        elif parent and f"{parent.upper()}_{name.upper()}" in os.environ:
            attr = os.getenv(f"{parent.upper()}_{name.upper()}")
            try:
                if type(value) in self.TOML_TO_BUILTIN_MAP:
                    attr = self.TOML_TO_BUILTIN_MAP[type(value)](attr)
                elif type(value) == bool:
                    attr = attr.lower() == "true"
                else:
                    attr = type(value)(attr)
            except TypeError:
                logger.info(f"Could not cast setting {parent}.{name} with value {attr} to type {type(value)}")
                pass
            self.set_attr(name, attr, parent)
        elif isinstance(value, dict):
            for k, v in value.items():
                self._set_value_from_config(k, v, name)
        elif isinstance(value, int) or isinstance(value, float):
            self.set_attr(name, value, parent)
        elif isinstance(value, str) and value.startswith("${") and value.endswith("}"):
            # Expecting environment variable with the value between ${}
            var_name = re.findall(r'\${(.*?)}', value)[0]

            if var_name in os.environ:
                self.set_attr(name, os.getenv(var_name), parent)
        elif isinstance(value, str) and value.startswith("ssm:"):
            try:
                param = self._ssm.get_parameter(
                    Name=value.replace("ssm:", ""),
                    WithDecryption=True
                )

                if param.get("Parameter"):
                    self.set_attr(name, param["Parameter"]["Value"], parent)
            except ClientError as e:
                # Best effort to load parameter
                logger.error(e)
        else:
            self.set_attr(name, value, parent)

    def set_attr(self, name: str, value: Any, parent: str = None):
        if type(value) in self.TOML_TO_BUILTIN_MAP:
            value = self.TOML_TO_BUILTIN_MAP[type(value)](value)

        if isinstance(value, dict):
            value = Box(value)

        if parent:
            if parent not in self._store:
                self._store[parent] = Box()

            self._store[parent][name] = value
        else:
            self._store[name] = value

    def __dir__(self):
        """Enable auto-complete for code editors"""
        return (
            self.INTERNAL_ATTRS
            + [k.lower() for k in self._store.keys()]
        )

    def __getattr__(self, name):
        """Allow getting keys from self._store using dot notation"""
        if name in self.INTERNAL_ATTRS:
            return super(Settings, self).__getattribute__(name)
        value = getattr(self._store, name)
        return value

    def __setattr__(self, name, value):
        """Allow `settings.FOO = 'value'` while keeping internal attrs."""
        if name in self.INTERNAL_ATTRS:
            super(Settings, self).__setattr__(name, value)
        else:
            self.set_attr(name, value)

    def __contains__(self, item):
        """Respond to `item in settings`"""
        return item.upper() in self._store or item.lower() in self._store

    def __getitem__(self, item):
        """Allow getting variables as dict keys `settings['KEY']`"""
        value = self._store.get(item)
        if value is None:
            raise KeyError(f"{item} does not exist")
        return value

    def __setitem__(self, key, value):
        """Allow `settings['KEY'] = 'value'`"""
        self.set_attr(key, value)

    def __iter__(self):
        """Redirects to store object"""
        yield from self._store

    def items(self):
        """Redirects to store object"""
        return self._store.items()

    def keys(self):
        """Redirects to store object"""
        return self._store.keys()

    def values(self):
        """Redirects to store object"""
        return self._store.values()

# Global singleton object that can be imported anywhere
settings = Settings(config_filepath="settings.toml")


import base64
import io

from PIL import Image, ImageOps

def load_img64(image64):
    buffer = io.BytesIO(base64.b64decode(image64))
    return Image.open(buffer)

def save_img64(im, **kwargs):
    buffer = io.BytesIO()
    im.save(buffer, **kwargs)
    return base64.b64encode(buffer.getvalue())

def resize_and_colorize_gif(image64, height, color):
    im = load_img64(image64)

    resize_to = (im.size[0] * height / im.size[1], height)
    frames = []

    try:
        while True:
            frame = ImageOps.colorize(
                ImageOps.grayscale(im), white="white", black=color
            )
            frame = frame.convert("RGBA")
            frame.thumbnail(resize_to)
            frames.append(frame)
            im.seek(im.tell() + 1)

    except EOFError:
        pass

    return save_img64(
        frames[0],
        optimize=False,
        save_all=True,
        append_images=frames[1:],
        loop=0,
        format="GIF",
        transparency=0,
    )

def get_gif_durations(image64):
    im = load_img64(image64)

    durations = []
    first_frame_duration = im.info["duration"]

    try:
        while True:
            durations.append(im.info.get("duration", first_frame_duration))
            im.seek(im.tell() + 1)

    except EOFError:
        pass

    return durations

def resize_and_colorize_img(image, height, color, canvas_size=None, margin=None):
    im = Image.open(image)

    alpha = im.split()[3]
    im = ImageOps.colorize(ImageOps.grayscale(im), white="white", black=color)
    im.putalpha(alpha)
    width = round(im.size[0] * height / im.size[1])
    im.thumbnail((width, height))

    w, h = im.size
    if margin:
        padw, padh = margin
        canvas_size = w + 2 * padw, h + 2 * padh

    elif canvas_size:
        W, H = canvas_size
        padw, padh = round((W - w) * 0.5), round((H - h) * 0.5)

    if canvas_size:
        new = Image.new("RGBA", canvas_size, 0)
        new.paste(im, (padw, padh))
        im = new

    return save_img64(im, format="PNG")

def get_img64_size(image64):
    im = load_img64(image64)
    return im.size


import telebot
from telebot import types

import sys
import requests
from requests import RequestException, HTTPError

telegramToken = sys.argv[1]
serverHost = sys.argv[2]
serverPort = sys.argv[3]

bot = telebot.TeleBot(telegramToken)
chatState = {}
serverUrl = 'http://' + serverHost + ':' + serverPort + '/api/bets/{}'

def make_request_to_server(id='', method='GET', data={}):
    url = serverUrl.format(id)
    resp = requests.request(method, url, data=data)
    resp.raise_for_status()
    try:
        return resp.json()
    except ValueError:
        return None

####################### START #######################
@bot.message_handler(commands=['start'])
def start(message):
    bot.send_message(message.chat.id, "Good morning friend! What to predict something?")
    send_help(message)
    chatState[message.chat.id] = ''
    # todo add authentification

####################### HELP #######################
@bot.message_handler(commands=['help'])
def send_help(message):
    bot.send_message(message.chat.id, "You can do several things. Type: \n"
                                      "/predict - to predict something\n"
                                      "/list - to list your predictions \n"
                                      "/resolve - to resolve one of them\n"
                                      "/cancel - cancel current operation\n"
                                      "Good luck!")

####################### cancel #######################
@bot.message_handler(commands=['cancel'])
def cancel(message):
    chatState[message.chat.id] = ''
    bot.send_message(message.chat.id, "Canceled", reply_markup=types.ReplyKeyboardHide())
    send_help(message)

def save_prediction(message):
    chat_id = message.chat.id
    try:
        make_request_to_server(method="POST",
                               data={'title': chatState[chat_id]['prediction'],
                                     'confidence': chatState[chat_id]['chance']})
        text = 'Done!'
    except RequestException:
        text = 'Something went wrong.'
    bot.send_message(chat_id, text, reply_markup=types.ReplyKeyboardHide())
    chatState[message.chat.id] = {}

####################### PREDICT #######################
## step 1
@bot.message_handler(commands=['predict'])
def predict_start(message):
    chatState[message.chat.id] = {'state': 'predict_start'}
    bot.send_message(message.chat.id, "Type something you want to predict")

## step 2
def predict_answer(message):
    chatState[message.chat.id]['state'] = 'predict_answer'
    chatState[message.chat.id]['prediction'] = message.text
    markup = types.ReplyKeyboardMarkup()
    markup.row('YES', 'NO', )
    bot.send_message(message.chat.id, "What is your answer? Choose one of the options or type in your answer ",
                     reply_markup=markup)

## step 3
def predict_chance(message):
    chatState[message.chat.id]['state'] = 'predict_chance'
    chatState[message.chat.id]['answer'] = message.text
    markup = types.ReplyKeyboardMarkup()
    markup.row('50%', '60%', '70%')
    markup.row('80%', '90%', '99%')
    bot.send_message(message.chat.id, "How sure you about it? Choose one of the options or type in your number",
                     reply_markup=markup)

## step 3.5
def check_chance(message):
    chance_str = message.text
    is_percentage = False
    if chance_str.endswith('%'):
        chance_str = chance_str[:-1]
        is_percentage = True

    try:
        chance = float(chance_str)

        if is_percentage:
            chance /= 100

        if chance < 0 or chance > 1:
            bot.send_message(message.chat.id,
                             "Probability should be in interval from 0 to 1. Try again.")
        else:
            chatState[message.chat.id]['chance'] = chance
            predict_reminder(message)

    except ValueError:
        bot.send_message(message.chat.id, "You should type proper number. Try again.")

## step 4
def predict_reminder(message):
    chatState[message.chat.id]['state'] = 'predict_reminder'
    markup = types.ReplyKeyboardMarkup()
    markup.row('1 Hour', '6 Hours', '12 Hours')
    markup.row('1 Day', '1 Week', '1 Month')
    markup.row('No reminder')
    markup.one_time_keyboard = True
    bot.send_message(message.chat.id, "Do you want me to remind you about it? You can type in date if you want",
                     reply_markup=markup)

####################### LIST #######################
@bot.message_handler(commands=['list'])
def prediction_list(message):
    try:
        response = make_request_to_server()
        text = ''
        for element in response:
            text += '{}. {}\n'.format(element['id'], element['title'])
        print(response)
    except RequestException:
        text = "Something went wrong."
    bot.send_message(message.chat.id, text)

####################### RESOLVE #######################
@bot.message_handler(commands=['resolve'])
## step 1
def resolve_start(message):
    chatState[message.chat.id] = {'state': 'resolve_start'}
    bot.send_message(message.chat.id, "Which prediction do you want to resolve? Type in the number:")
    prediction_list(message)

## step 2
def resolve_choose(message):
    chatState[message.chat.id]['state'] = 'resolve_choose'
    chatState[message.chat.id]['prediction_id'] = message.text
    try:
        prediction = make_request_to_server(message.text)
        bot.send_message(message.chat.id,
                         "So your prediction was:\n{}".format(
                             prediction['title']))
        markup = types.ReplyKeyboardMarkup()
        markup.row('Correct', 'Incorrect')
        markup.row('Cancel')
        bot.send_message(message.chat.id, "Did you guess it right?", reply_markup=markup)
    except RequestException as e:
        if type(e) == HTTPError and e.response.status_code == 404:
            bot.send_message(message.chat.id, "There is no prediction with such number.")
            cancel(message)
        else:
            bot.send_message(message.chat.id, "Something went wrong.")

## step 3
def resolve_resolution(message):
    chatState[message.chat.id]['state'] = 'resolve_resolution'

    if message.text.lower() in ['y', 'yes', 'correct', 'right']:
        outcome = True
    elif message.text.lower() in ['n', 'no', 'incorrect']:
        outcome = False
    else:
        if message.text.lower() != 'cancel':
            bot.send_message(message.chat.id, "Cannot interpret your answer",
                             reply_markup=types.ReplyKeyboardHide())
        cancel(message)
        return

    chatState[message.chat.id]['prediction_outcome'] = outcome
    try:
        make_request_to_server(
            chatState[message.chat.id]['prediction_id'],
            method="PATCH",
            data={'outcome': outcome})
    except RequestException:
        bot.send_message(message.chat.id, "Something went wrong.",
                         reply_markup=types.ReplyKeyboardHide())
        cancel(message)
        return

    bot.send_message(message.chat.id, "Ok, we saved that. Good luck next time",
                     reply_markup=types.ReplyKeyboardHide())

####################### ALL OTHER MESSAGES #######################
state2handler = {
    'predict_start': predict_answer,
    'predict_answer': predict_chance,
    'predict_chance': check_chance,
    'check_chance': check_chance,
    'predict_reminder': save_prediction,
    'resolve_start': resolve_choose,
    'resolve_choose': resolve_resolution,
}

@bot.message_handler(func=lambda message: True)
def unrecognized(message):
    chat_id = message.chat.id
    if chat_id in chatState and 'state' in chatState[chat_id] and chatState[chat_id]['state'] in state2handler:
        state2handler[chatState[chat_id]['state']](message)
    else:
        start(message)
        print("Don't know how to handle message ")
        print(message)

print("Start polling...")
bot.polling()


__author__ = 'Shade390'
import traceback

from flask import Flask, redirect, render_template, session, g, current_app as app, url_for

import sqlite3
from tools.pictures import get_user_profile_pic, get_profile_pic_path

def home_page(username, db):
    posts = []
    try:
        c = db.cursor()
        d= db.cursor()
        t = (username, username, username,)
        c.execute('SELECT DISTINCT postTitle, user, postText, p.postid  \
                    FROM Posts p \
                    LEFT JOIN postTarget pt on p.postid = pt.postid \
                    OR p.user = ? \
                    WHERE (cid IN(SELECT cid From circle_members WHERE user=?) OR p.user = ?)', t)
        for row in c.fetchall():
            pictures = []
            #search for pictures based on postid
            t= (row[3],)
            d.execute('SELECT owner, path From pictures pic INNER JOIN postpictures post on pic.picid=post.picid \
            WHERE post.postid=?',t)
            for stuff in d.fetchall():
                pic= "/images/" + stuff[0] +"/"+ stuff[1]
                pictures.append(pic)
            profileId = get_user_profile_pic(row[1])
            profile_path = get_profile_pic_path(profileId)
            post = (row[0], row[1], row[2], pictures, profile_path)
            posts.append(post)
        posts.reverse()
        return render_template('posts_home.html', posts=posts)
    except sqlite3.OperationalError:
        traceback.print_exc()
    return render_template('posts_home.html', posts=posts)


from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd

###
#Scrape through NBA draft data until the most recent season

#Columns for final dataset
draft_columns=['Pk', 'Tm', 'Player', 'College', 'Yrs', 'G', 'MP', 'PTS', 'TRB', 'AST', 'FG%', '3P%', 'FT%', 'MP', 'PTS', 'TRB', 'AST', 'WS', 'WS/48', 'BPM', 'VORP']

def scrape_draft_data(years=[2017,2018]):
    final_df=pd.DataFrame(columns=draft_columns)

    for y in years:
        #Draft year to scrape
        year = y
       #Placing string url string with year being iterable
        url = f"https://www.basketball-reference.com/draft/NBA_{year}.html"
        html = urlopen(url)
        soup= BeautifulSoup(html, features = 'lxml')
        # get rows from table (tr denotes table rows, td is table data)
        rows = soup.findAll('tr')[0:] # Removes Rk
        rows_data = [[td.getText() for td in rows[i].findAll('td')]
                     for i in range(len(rows))]
        #placing data in a DataFrame
        df_1 = pd.DataFrame(rows_data,columns=draft_columns)
        print(df_1)
        # merge data frames
        final_df=pd.concat([final_df,df_1])
        print(final_df)
#Export data to csv file
    final_df.to_csv("nba_draft_data.csv", index=False)

###Calling function with the draft years I want data from
scrape_draft_data(years=[2000,2001,2002,2003,2004,2005,2006,2007,
                         2008,2009,2010,2011,2012,2013,2014,2015,
                         2016,2017,2018,2019, 2020, 2021])


#!/usr/bin/env python3

import pygraphviz  as pgv
import collections as coll
import os

Node = coll.namedtuple('Node', ['id','datum'])
Arc  = coll.namedtuple('Arc', ['in_node','out_node','datum'])

def arc_to_id(arc):
    #if(arc.datum):
    #    label =  '['+str(arc.in_node.id)+']'+'['+str(arc.out_node.id)+']'+'{'+str(arc.datum)+'}'
    #else:
    #    label =  '['+str(arc.in_node.id)+']'+'['+str(arc.out_node.id)+']'+'{}'
    label =  '['+str(arc.in_node.id)+']'+'['+str(arc.out_node.id)+']'
    return label

def node_to_label(node):
    label = str(node.id)+'{'+str(node.datum)+'}' if node.datum else str(node.id)
    return label

def arc_to_label(arc):
    label = str(arc.datum) if arc.datum else ''
    return label

def color_path(path,dot_G,color='red'):
    dot_G = dot_G.copy()
    color = str(color)
    miter = iter(path)
    done  = False
    try:
        n = next(miter)
        while(not done):
            node = dot_G.get_node(n.id)
            node.attr['color']     = color
            node.attr['fontcolor'] = color
            p = n
            n = next(miter)
            arc_name = arc_to_id(Arc(p,n,None))
            for a in dot_G.edges(nbunch=node):
                in_id, out_id = a
                if( out_id == arc_name ): #(in_id == node.name) and (out_id == n.id) ):
                    dot_G.get_edge(p.id,arc_name).attr['color'] = color
                    dot_G.get_node(arc_name).attr['fontcolor']  = color
                    dot_G.get_edge(arc_name,n.id).attr['color'] = color
    except StopIteration as e:
        done = True
    return dot_G

def color_nodes(nodes,dot_G,color='red'):
    dot_G = dot_G.copy()
    nodes = set(nodes)
    idz = [n.id for n in nodes]
    for n in idz:
        node = dot_G.get_node(n)
        node.attr['color']     = color
        node.attr['fontcolor'] = color
    return dot_G

def color_arcs(arcs,dot_G,color='red'):
    dot_G = dot_G.copy()
    arcs = set(arcs)
    for a in arcs:
        for e in dot_G.edges(nbunch=a.in_node.id):
            in_id, out_id = e
            if(a.in_node.id == in_id):
                arc_name = arc_to_id(a)
                dot_G.get_edge(in_id,arc_name).attr['color']  = color
                dot_G.get_node(arc_name).attr['fontcolor']    = color
                dot_G.get_edge(arc_name,a.out_node.id).attr['color'] = color
    return dot_G

def example_gdict():
    A = Node('A',None)
    B = Node('B',None)
    C = Node('C',None)

    AB = Arc(A,B,'A,B,C, \nit\'s as easy as')
    BC = Arc(B,C,'1,2,3!')

    g = {}
    g[A] = frozenset([AB])
    g[B] = frozenset([BC])
    g[C] = frozenset([])

    return g

def example_gdict_1():
    s = Node('s',None)
    a = Node('a',None)
    b = Node('b',None)
    c = Node('c',None)
    d = Node('d',None)
    t = Node('t',None)

    sa = Arc(s,a,'0:3')
    sd = Arc(s,d,'0:3')
    ab = Arc(a,b,'0:2')
    ad = Arc(a,d,'0:2')
    bc = Arc(b,c,'0:4')
    bt = Arc(b,t,'0:2')
    ct = Arc(c,t,'0:2')
    dc = Arc(d,c,'0:3')

    g = {}
    g[s] = frozenset([sa,sd])
    g[a] = frozenset([ab,ad])
    g[b] = frozenset([bc,bt])
    g[c] = frozenset([ct])
    g[d] = frozenset([dc])
    g[t] = frozenset([])

    return g,[s,a,b,c,t],[a,b,c],[sa,ad,bt]

'''
https://www.python.org/doc/essays/graphs/
http://www.graphviz.org/doc/info/lang.html
http://pygraphviz.github.io/documentation/latest/pygraphviz.pdf
'''
def dict_to_dot(dict_G, name=''):
    dict_G    = dict(dict_G)
    name      = str(name)

    nodes = dict_G.keys()
    G = pgv.AGraph(name=name, label=name, strict=False, directed=True, rankdir="LR")

    x_dim, y_dim         = 0.1, 0.1
    tail,adir,ahlen      = 'dot','both',0.62
    arc_descriptor_shape = 'plain'
    node_shape           = 'egg'
    arc_shape            = 'plain'
    arc_arrow_head       = 'normal'
    arc_arrow_tail       = 'dot'
    arc_mid              = 'none'
    arc_dir              = 'both'

    for node in nodes:
        G.add_node(node.id,label=node_to_label(node))
        arcs = dict_G[node]
        for arc in arcs:
            arc_id = arc_to_id(arc)
            G.add_node(arc_id,shape=arc_descriptor_shape,label=arc_to_label(arc),width=x_dim,height=y_dim)
            G.add_edge(node.id,arc_id,len=ahlen,dir=arc_dir,arrowtail=arc_arrow_tail,arrowhead=arc_mid)
            G.add_edge(arc_id,arc.out_node.id,shape=arc_shape,arrowtail=arc_mid,arrowhead=arc_arrow_head)

    return G

def dot_to_file(dot_G, name='out', env='./env'):
    G           = dot_G.copy()
    env         = os.path.abspath(env)
    layout_prog = 'dot'
    dot_suffix  = 'dot'
    img_suffix  = 'png'

    dot_G.graph_attr.update(name = name)
    dot_G.graph_attr.update(label = name)

    # print to screen
    print(G.string())

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

    G.layout(prog=layout_prog)
    dot_fname = os.path.join(env,name +'.' + str(dot_suffix))
    img_fname = os.path.join(env,name + '.' + str(img_suffix))
    G.write(dot_fname)
    print("Wrote " + str(dot_fname))

    # create a new graph from file
    G = pgv.AGraph(dot_fname)
    G.layout(prog=layout_prog)
    G.draw(img_fname)
    print("Wrote " + str(img_fname))

def test_1():
    g_dict, g_path, nodes, arcs = example_gdict_1()
    g_dot       = dict_to_dot(g_dict)
    color_pathz = color_path(g_path, g_dot)
    color_nodez = color_nodes(nodes, g_dot)
    color_arcz  = color_arcs(arcs, g_dot)

    dot_to_file(g_dot,name='1')
    dot_to_file(color_pathz,name='2')
    dot_to_file(color_nodez,name='3')
    dot_to_file(color_arcz,name='4')

    g_dict = example_gdict()
    g_dot  = dict_to_dot(g_dict)
    dot_to_file(g_dot,name='ABC')

if __name__ == '__main__':
    progz = ['neato','dot','twopi','circo','fdp','nop']
    #Seems that dot and neato work, the others don't look good, or break.
    test_1()


## Matplotlib Part 2

import matplotlib.pyplot as plt

x=[1,2,3,4,5,6,7] # week days
y=[964,287,42,390,64,800,523] # views

plt.plot(x,y,label='Youtube views',
         color='r',
         marker='o',
         markerfacecolor='g',
         linestyle='--',
         linewidth=3)
plt.xlabel('Week days')
plt.ylabel('Views')
plt.title('Youtube views on daily basis')
plt.legend(loc='upper left')

plt.show()

Udemy/data science with python/Data Visualization with Matplotlib 2.py