import os
import unittest
from decimal import Decimal
from datetime import datetime as dt
from datetime import timedelta
from functools import reduce

from singer import metadata
import tap_tester.connections as connections
import tap_tester.menagerie   as menagerie
import tap_tester.runner      as runner

from base import TestAdrollBase
from test_client import TestClient

class TestAdrollReportsFieldSelection(TestAdrollBase):
    """
    Test that with all fields selected for the 'ad_reports' stream  we replicate data as expected.
    Test that with only automatic fields selected for the 'ad_reports' stream  we replicate data as expected.
    """

    def name(self):
        return "tap_tester_adroll_reports_fields"

    def testable_streams(self):
        return self.expected_incremental_streams()

    @classmethod
    def setUpClass(cls):
        print("\n\nTEST SETUP\n")
        cls.client = TestClient()

    @classmethod
    def tearDownClass(cls):
        print("\n\nTEST TEARDOWN\n\n")

    def format_expected_data(self, records_by_stream):
        """Type the data to match expected tap output"""
        for stream, stream_records in records_by_stream.items():
            print("Altering records for {} ".format(stream))
            for record in stream_records:
                for key, value in record.items():
                    if type(value) == float:
                        record[key] = Decimal(str(value))

    def test_run(self):
        """
        Verify that for each stream you can get data when no fields are selected
        and only the automatic fields are replicated.
        """

        print("\n\nRUNNING {}\n\n".format(self.name()))

        # Initialize start_date state to capture ad_reports records
        self.START_DATE = self.timedelta_formatted(self.REPORTS_START_DATE, -1)
        self.END_DATE = self.REPORTS_END_DATE
        print("INCREMENTAL STREAMS RELY ON A STATIC DATA SET. SO WE TEST WITH:\n" +
              "  START DATE 1 | {}\n".format(self.START_DATE) +
              "  END DATE 2 | {}".format(self.END_DATE))

        # ensure data exists for sync streams and set expectations
        expected_records_all = {x: [] for x in self.expected_streams()} # all fields selected
        expected_records_auto = {x: [] for x in self.expected_streams()} # no fields selected
        for stream in self.testable_streams():
            start_date = self.parse_date(self.START_DATE)
            end_date = self.parse_date(self.END_DATE)
            existing_objects = self.client.get_all(stream, start_date, end_date)

            assert existing_objects, "Test data is not properly set for {}, test will fail.".format(stream)
            print("Data exists for stream: {}".format(stream))
            for obj in existing_objects:
                expected_records_all[stream].append(obj)
                expected_records_auto[stream].append(
                    {field: obj.get(field)
                     for field in self.expected_automatic_fields().get(stream)}
                )

        # format expected data to match expected output of tap
        self.format_expected_data(expected_records_all)

        # Instantiate connection with default start/end dates
        conn_id = connections.ensure_connection(self, original_properties=False)

        # run in check mode
        check_job_name = runner.run_check_mode(self, conn_id)

        # verify check exit codes
        exit_status = menagerie.get_exit_status(conn_id, check_job_name)
        menagerie.verify_check_exit_status(self, exit_status, check_job_name)

        found_catalogs = menagerie.get_catalogs(conn_id)
        self.assertGreater(len(found_catalogs), 0, msg="unable to locate schemas for connection {}".format(conn_id))

        found_catalog_names = set(map(lambda c: c['tap_stream_id'], found_catalogs))
        diff = self.expected_check_streams().symmetric_difference( found_catalog_names )
        self.assertEqual(len(diff), 0, msg="discovered schemas do not match: {}".format(diff))
        print("discovered schemas are OK")

        ##########################################################################
        ### ALL FIELDS SYNC
        ##########################################################################

        # Select all available fields from all streams
        exclude_streams = self.expected_streams().difference(self.testable_streams())
        self.select_all_streams_and_fields(
            conn_id=conn_id, catalogs=found_catalogs, select_all_fields=True, exclude_streams=exclude_streams
        )

        catalogs = menagerie.get_catalogs(conn_id)

        # Ensure our selection worked
        for cat in found_catalogs:
            catalog_entry = menagerie.get_annotated_schema(conn_id, cat['stream_id'])
            # Verify only testable streams are selected
            selected = catalog_entry.get('annotated-schema').get('selected')
            print("Validating selection on {}: {}".format(cat['stream_name'], selected))
            if not cat['stream_name'] in self.testable_streams():
                # None expected for {'inclusion':'available'} happens when menagerie "deselects" stream
                self.assertTrue(selected is None, msg="Stream is selected, but shouldn't be.")
                continue
            self.assertTrue(selected, msg="Stream not selected.")

            # Verify all fields within each selected stream are selected
            for field, field_props in catalog_entry.get('annotated-schema').get('properties').items():
                field_selected = field_props.get('selected')
                print("\tValidating selection on {}.{}: {}".format(cat['stream_name'], field, field_selected))
                self.assertTrue(field_selected, msg="Field not selected.")

        #clear state
        menagerie.set_state(conn_id, {})

        # run sync with all fields selected
        sync_job_name_all = runner.run_sync_mode(self, conn_id)

        # Verify tap exit codes
        exit_status_all = menagerie.get_exit_status(conn_id, sync_job_name_all)
        menagerie.verify_sync_exit_status(self, exit_status_all, sync_job_name_all)

        # read target output
        record_count_by_stream_all = runner.examine_target_output_file(self, conn_id,
                                                                       self.expected_streams(),
                                                                       self.expected_primary_keys())
        replicated_row_count_all =  reduce(lambda accum,c : accum + c, record_count_by_stream_all.values())
        synced_records_all = runner.get_records_from_target_output()

        # Verify target has records for all synced streams
        for stream, count in record_count_by_stream_all.items():
            assert stream in self.expected_streams()
            if stream in self.testable_streams():
                self.assertGreater(count, 0, msg="failed to replicate any data for: {}".format(stream))
        print("total replicated row count: {}".format(replicated_row_count_all))

        ##########################################################################
        ### AUTOMATIC FIELDS SYNC
        ##########################################################################

        # Instantiate connection with default start/end dates
        conn_id = connections.ensure_connection(self, original_properties=False)

        # run in check mode
        check_job_name = runner.run_check_mode(self, conn_id)

        # verify check exit codes
        exit_status = menagerie.get_exit_status(conn_id, check_job_name)
        menagerie.verify_check_exit_status(self, exit_status, check_job_name)

        found_catalogs = menagerie.get_catalogs(conn_id)
        self.assertGreater(len(found_catalogs), 0, msg="unable to locate schemas for connection {}".format(conn_id))

        found_catalog_names = set(map(lambda c: c['tap_stream_id'], found_catalogs))
        diff = self.expected_check_streams().symmetric_difference( found_catalog_names )
        self.assertEqual(len(diff), 0, msg="discovered schemas do not match: {}".format(diff))
        print("discovered schemas are OK")

        # Select no available fields (only automatic) for  all testable streams
        self.select_all_streams_and_fields(
            conn_id=conn_id, catalogs=found_catalogs, select_all_fields=False, exclude_streams=exclude_streams
        )

        catalogs = menagerie.get_catalogs(conn_id)

        # Ensure our selection worked
        for cat in found_catalogs:
            catalog_entry = menagerie.get_annotated_schema(conn_id, cat['stream_id'])
            # Verify all streams are selected
            selected = catalog_entry.get('annotated-schema').get('selected')
            print("Validating selection on {}: {}".format(cat['stream_name'], selected))
            if not cat['stream_name'] in self.testable_streams():
                self.assertTrue(selected is None, msg="Stream is selected, but shouldn't be.")
                continue
            self.assertTrue(selected, msg="Stream not selected.")

            # Verify only automatic fields are selected
            for field, field_props in catalog_entry.get('annotated-schema').get('properties').items():
                field_selected = field_props.get('selected')
                print("\tValidating selection on {}.{}: {}".format(cat['stream_name'], field, field_selected))

                if field in self.expected_automatic_fields().get(cat['stream_name']):
                    # NOTE: AUTOMATIC FIELDS IGNORE THE SELECTED md {'selected': None}
                    print("NOTE: selection for {} is ignored by the Transformer ".format(field) +
                          " so long as 'inlcusion' = 'automatic'")
                else:
                    self.assertFalse(field_selected, msg="Field is selected but not automatic.")

        # run sync with no fields selected (only automatic)
        sync_job_name_auto = runner.run_sync_mode(self, conn_id)

        # Verify tap exit codes
        exit_status_auto = menagerie.get_exit_status(conn_id, sync_job_name_auto)
        menagerie.verify_sync_exit_status(self, exit_status_auto, sync_job_name_auto)

        # read target output
        record_count_by_stream_auto = runner.examine_target_output_file(self, conn_id,
                                                                       self.expected_streams(),
                                                                       self.expected_primary_keys())
        replicated_row_count_auto =  reduce(lambda accum,c : accum + c, record_count_by_stream_auto.values())
        synced_records_auto = runner.get_records_from_target_output()

        # Verify target has records for all synced streams
        for stream, count in record_count_by_stream_auto.items():
            assert stream in self.expected_streams()
            if stream in self.testable_streams():
                self.assertGreater(count, 0, msg="failed to replicate any data for: {}".format(stream))
        print("total replicated row count: {}".format(replicated_row_count_auto))

        # Test by Stream
        for stream in self.testable_streams():
            with self.subTest(stream=stream):
                ##########################################################################
                ### TESTING ALL FIELDS
                ##########################################################################

                data = synced_records_all.get(stream)
                record_messages_keys = [set(row['data'].keys()) for row in data['messages']]
                expected_keys = expected_records_all.get(stream)[0].keys()

                # Verify schema covers all fields
                schema_keys = set(self.expected_schema_keys(stream))
                self.assertEqual(
                    set(), set(expected_keys).difference(schema_keys),
                    msg="\nFields missing from schema: {}\n".format(set(expected_keys).difference(schema_keys))
                )

                # not a test, just logging the fields that are included in the schema but not in the expectations
                if schema_keys.difference(set(expected_keys)):
                    print("WARNING Fields missing from expectations: {}".format(schema_keys.difference(set(expected_keys))))

                # Verify that all fields are sent to the target
                for actual_keys in record_messages_keys:
                    self.assertEqual(
                        actual_keys.symmetric_difference(schema_keys), set(),
                        msg="Expected all fields, as defined by schemas/{}.json".format(stream)
                    )

                actual_records = [row['data'] for row in data['messages']]
                expected_records = expected_records_all.get(stream)

                # Verify the number of records match expectations
                self.assertEqual(len(expected_records),
                                 len(actual_records),
                                 msg="Number of actual records do match expectations. " +\
                                 "Check expectations, check for duplicate records in Target.")

                # verify there are no dup records in the target
                already_tracked = []
                for actual_record in actual_records:
                    if actual_record in already_tracked:
                        continue
                    already_tracked.append(actual_record)
                self.assertEqual(len(already_tracked), len(actual_records), msg="DUPLICATES PRESENT")

                # verify by values, that we replicated the expected records
                for actual_record in actual_records:
                    self.assertTrue(actual_record in expected_records,
                                    msg="Actual record missing from expectations\n" +
                                    "Actual Record: {}".format(actual_record))
                for expected_record in expected_records:
                    self.assertTrue(expected_record in actual_records,
                                    msg="Expected record missing from target." +
                                    "Expected Record: {}".format(expected_record))

                ##########################################################################
                ### TESTING AUTOMATIC FIELDS
                ##########################################################################

                data = synced_records_auto.get(stream)
                record_messages_keys = [set(row['data'].keys()) for row in data['messages']]
                expected_keys = self.expected_automatic_fields().get(stream)

                # Verify that only the automatic fields are sent to the target
                for actual_keys in record_messages_keys:
                    self.assertEqual(
                        actual_keys.symmetric_difference(expected_keys), set(),
                        msg="Expected automatic fields and nothing else.")

                actual_records = [row['data'] for row in data['messages']]
                expected_records = expected_records_auto.get(stream)

                #Verify the number of records match expectations
                self.assertEqual(len(expected_records),
                                 len(actual_records),
                                 msg="Number of actual records do match expectations. " +\
                                 "We probably have duplicate records.")

                # verify there are no dup records in the target
                already_tracked = []
                for actual_record in actual_records:
                    if actual_record in already_tracked:
                        continue
                    already_tracked.append(actual_record)
                self.assertEqual(len(already_tracked), len(actual_records), msg="DUPLICATES PRESENT")

                # verify by values, that we replicated the expected records
                for actual_record in actual_records:
                    self.assertTrue(actual_record in expected_records,
                                    msg="Actual record missing from expectations\n" +
                                    "Actual Record: {}".format(actual_record))
                for expected_record in expected_records:
                    self.assertTrue(expected_record in actual_records,
                                    msg="Expected record missing from target." +
                                    "Expected Record: {}".format(expected_record))

if __name__ == '__main__':
    unittest.main()


import requests
from bs4 import BeautifulSoup
import json

def fetch(url, params):
    headers = params['headers']
    body = params['body']
    if params['method'] == 'GET':
        return requests.get(url, headers=headers)
    if params['method'] == 'POST':
        return requests.post(url, headers=headers, data=body)

card = fetch("https://basket-05.wb.ru/vol735/part73512/73512949/info/ru/card.json", {
    "headers": {},
    "referrerPolicy": "no-referrer-when-downgrade",
    "body": None,
    "method": "GET"
});

print(card.status_code)
print(card.json().keys())

article = card.json()['nm_id']
print(article)

brand = card.json()['selling']['brand_name']
print(brand)

article_name = card.json()['imt_name']
print(article_name)


from app.utils import catch_exceptions,download_from_s3
import os
import re
import nltk
nltk.download('wordnet')
nltk.download('stopwords')
from nltk.stem import WordNetLemmatizer
from constants import STOP_WORDS,punctuations
import json
import logging
logger = logging.getLogger(name=__name__)

class ExtractAttributes(object):
    def __init__(self):
        pass

    def lemmatize_text(self,text):
        try:
            lemmatizer = WordNetLemmatizer()
            lemmatized_list = [lemmatizer.lemmatize(word.lower()) for word in text.split(' ') if word.lower() not in STOP_WORDS]
            lem_string = ' '.join(lemmatized_list).strip().lower()
            return lem_string
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def unpunctuate(self,text):
        try:
            text = str(text)
            text = text.replace("'s","")
            unpunctuated_list = [character if character not in punctuations else ' ' for character in text]
            unpunctuated_text = "".join(unpunctuated_list)
            return unpunctuated_text
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def clean_text(self,text):
        try:
            text = self.lemmatize_text(text) #lemmatize the input text
            text = self.unpunctuate(text)
            return text
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_valid_values(self):
        try:
            valid_values_path = self.check_valid_values_file_exists('valid_values_updated.json')
            with open(valid_values_path,'r') as f:
                valid_values = json.load(f)
            return valid_values
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def search_entity(self, attribute_value, product_data):
        try:
            if re.search(r"\b" + re.escape(attribute_value) + r"\b", product_data) and attribute_value!='':
                return True #ref: "https://stackoverflow.com/questions/4154961/find-substring-in-string-but-only-if-whole-words"
            return False
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def extract_attributes(self, request_data):
        try:
            valid_values = self.get_valid_values()
            attribute_names = list(valid_values.get(str(request_data["category_id"])).get(str(request_data["sub_category_id"])).get("valid_values").keys()) #get the list of all the valid attribute names
            text_attributes = {}
            title = self.clean_text(request_data["product_title"])
            for each_attribute_values in attribute_names:
                    if each_attribute_values:
                        for each_attr_value in each_attribute_values:
                            is_true = self.search_entity(self.clean_text(each_attr_value), title)
                            if is_true:
                                if each_attribute_values in text_attributes.keys():
                                    pass
                                else:
                                    text_attributes[each_attribute_values] = each_attr_value
                            else:
                                pass
            return text_attributes
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_attributes(self, request_data):
        #to manage multiple product_titles
        try:
            product_titles = request_data["product_title"]
            all_titles_attributes = []
            for each_title in product_titles:
                request_data["product_title"] = each_title
                each_title_attributes = self.extract_attributes(request_data)
                all_titles_attributes.append(each_title_attributes)
            return all_titles_attributes
        except Exception as e:
            logger.error(e,exc_info=True)

    @catch_exceptions
    def get_attributes_for_a_string(self,request_data):
        try:
            response_data = {}
            mandatory_fields = ["product_title", "category_id","sub_category_id"]
            for field in mandatory_fields:
                if not field in request_data["data"]:
                    response_data = {
                        "status":False,
                        "message":"Required field is missing",
                        "error_obj":{
                            "description":"{} is missing".format(field),
                            "error_code":"REQUIRED_FIELD_IS_MISSING"
                        }
                    }
            if not response_data:
                response_data = {
                    "status":True,
                    "data":{
                        "product_details":self.get_attributes(request_data["data"])
                    }
                }
            return response_data
        except Exception as e:
            logger.error(e,exc_info=True)

ExtractedAttributes = ExtractAttributes()


import numpy as np
import random as rand
import time as t
class PassThePigsSimulator1P: # for single player

    def readTosses():
        dictTosses = {} # Dictionary: key is toss result, value is array -- first entry probability, second entry points
        file = open('Pass_the_pigs.txt')
        possibleEvents = file.readlines()
        for line in possibleEvents:
            event = line.split()
            dictTosses[event[0]] = [float(event[1]),int(event[2])] # Dictionary: key is toss result, value is array -- first entry probability, second entry points
        return dictTosses

    DICT = readTosses()

    def __init__(self, player, bankPoints, myTurn, turnPoints = 0, numRolls = 0):
        # default values, serves like two constructors in Java
        # turn points are the points that a player has accumulated over the turn before banking
        # numRolls are the number of rolls made in any given turn
        self.bankPoints = bankPoints
        self.turnPoints = turnPoints
        self.numRolls = numRolls
        self.player = player
        self.myTurn = myTurn

    def displayPlayer(self):
        print(self.player)

    def displayPoints(self):
        print(self.bankPoints)

    def displayTurn(self):
        print(self.myTurn)

    def isWinner(self):
        if self.bankPoints >= 100:
            return True
        else:
            return False

    def singularRollResult(self, toss): # used in pointCalc()
        keys = list(self.DICT.keys())
        if toss < self.DICT[keys[0]][0]: # accessing the first element of the array corresponding to the first key -- pink
            # pink
            return keys[0]
        elif toss < self.DICT[keys[1]][0]:
            # dot
            return keys[1]
        elif toss < self.DICT[keys[2]][0]:
            # razorback
            return keys[2]
        elif toss < self.DICT[keys[3]][0]:
            # trotter
            return keys[3]
        elif toss < self.DICT[keys[4]][0]:
            # snouter
            return keys[4]
        else:
            # leaning jowler
            return keys[5]

    def pointCalc(self): # used in updateBankPoints()
        toss1 = rand.randint(0,100)/100
        toss2 = rand.randint(0,100)/100
        result1 = self.singularRollResult(toss1)
        result2 = self.singularRollResult(toss2)
        self.numRolls = self.numRolls + 1

        if (result1 == 'dot' and result2 == 'pink') or (result2 == 'dot' and result1 == 'pink'):
            print('Pig Out! You lost all points in this turn!')
            return 0
        elif result1 == result2: # if same roll result, bonus points awarded
            if result1 == 'pink' or result1 == 'dot': # double pink or double dot = 1
                print('2x sides! +1 point!')
                return 1
            elif result1 == 'razorback':
                print('2x razorback! +20 points!')
                return 20
            elif result1 == 'trotter': # double razorback or double trotter = 20
                print('2x trotter! +20 points!')
                return 20
            elif result1 == 'snouter': # double snouter = 40
                print('2x snouter! +40 points!')
                return 40
            else:
                print('2x leaning jowler!? You deserve a Nobel Prize! +60 points!')
                return 60 # double leaning jowler = 60
        else:
            print('1 ' + result1 + ' and 1 ' + result2 + ': +'
                  + str(self.DICT[result1][1] + self.DICT[result2][1]) + ' points!')
            return self.DICT[result1][1] + self.DICT[result2][1]
        # return the sum of the second element of the value, which are the total points
        #e.g pink + trotter = 5

    def updateBankPoints(self, other):
        # passThePigs = False
        # should update with self.myTurn
        if other.myTurn == False:
            self.myTurn = True
        while(self.myTurn):
            print('Player Rolls!')
            t.sleep(1)
            print('...')
            t.sleep(1)
            currentPoints = self.pointCalc()
            self.turnPoints = self.turnPoints + currentPoints
            if currentPoints == 0: # rolled one dot and one pink
                print('You wasted ' + str(self.turnPoints) +' points!')
                print('You rolled ' + str(self.numRolls) + ' number of times')
                self.myTurn = False
                self.turnPoints = 0
                self.numRolls = 0
                t.sleep(2)
            else:
                print('Continue Rolling? (y/n)')
                print('(You currently have ' + str(self.turnPoints) +' point(s)')
                print('and ' + str(self.bankPoints) + ' in the bank')
                if self.numRolls == 1:
                    print('That was your ' + str(self.numRolls) + 'st roll)')
                elif self.numRolls == 2:
                    print('That was your ' + str(self.numRolls) + 'nd roll)')
                elif self.numRolls == 3:
                    print('That was your ' + str(self.numRolls) + 'rd roll)')
                else:
                    print('That was your ' + str(self.numRolls) + 'th roll)')

                userResponse = input().lower()
                if userResponse[0] == 'y': # yes, continue rolling
                    self.myTurn = True
                else:
                    self.myTurn = False # no, stop rolling
                #self.turnPoints = self.turnPoints + currentPoints
        self.bankPoints = self.bankPoints + self.turnPoints # banking the turnPoints
        self.turnPoints = 0 # resetting turn points
        self.numRolls = 0 # resetting numRolls

    def greaterBankPointsTo(self, other):
        if not isinstance(other, PassThePigsSimulator1P):
            return NotImplemented
        return self.bankPoints > other.bankPoints

    def bankPointsDifference(self, other): # if < 0, self has less points than other
        if not isinstance(other, PassThePigsSimulator1P):
            return NotImplemented
        return self.bankPoints - other.bankPoints

    def greaterTurnPointsTo(self, other):
        if not isinstance(other, PassThePigsSimulator1P):
            return NotImplemented
        return self.turnPoints > other.turnPoints

    def turnPointsDifference(self, other): # if < 0, self has less points than other
        if not isinstance(other, PassThePigsSimulator1P):
            return NotImplemented
        return self.turnPoints - other.turnPoints

class PassThePigsSimulator2P(PassThePigsSimulator1P):

    # attempting to create AI with strategy found online,
    # credits to http://passpigs.tripod.com/strat.html
    def aiRollingStrategy(self, opponentScore, myScore, myTurnScore, round):
        # answers the question of, Continue Rolling? in the
        # updateBankPoints()
        if myTurnScore + myScore >= 100:
            return False
        elif opponentScore >= 85 or myScore >= 90:
            # if opponentScore is greater than or equal to 85 or
            # myScore is greater than or equal to 90, roll
            return True
        elif round == 1 and myTurnScore >= 15:
            # if after one roll myTurnScore is greater than or equal to 90
            # then don't roll
            return False
        #elif myScore > opponentScore - 20:
            # if myScore is within 20 points of opponentScore
            # then roll
        #    return True
        elif myTurnScore > 20:
            # if myTurnScore is greater than 20, then don't roll
            return False
        else:
            # in every other case, roll
            return True

    def aiUpdateBankPoints(self, other):
        if other.myTurn == False:
            self.myTurn = True
        print('AI Rolls!')
        t.sleep(1)
        print('...')
        t.sleep(1)
        while(self.myTurn):
            currentPoints = self.pointCalc()
            self.turnPoints = self.turnPoints + currentPoints
            if currentPoints == 0: # rolled one dot and one pink
                print('The AI wasted ' + str(self.turnPoints) +' points!')
                self.myTurn = False
                self.turnPoints = 0
                self.numRolls = 0
                t.sleep(2)
            else:
                t.sleep(1)
                print('Continue Rolling? (y/n)')
                print('(AI currently has ' + str(self.turnPoints) +' point(s)')
                print('and ' + str(self.bankPoints) + ' in the bank')
                if self.numRolls == 1:
                    print('That was the AI\'s ' + str(self.numRolls) + 'st roll)')
                elif self.numRolls == 2:
                    print('That was the AI\'s ' + str(self.numRolls) + 'nd roll)')
                elif self.numRolls == 3:
                    print('That was the AI\'s ' + str(self.numRolls) + 'rd roll)')
                else:
                    print('That was the AI\'s ' + str(self.numRolls) + 'th roll)')
                t.sleep(2)
                aiResponse = self.aiRollingStrategy(other.bankPoints, self.bankPoints, self.turnPoints, self.numRolls)
                if aiResponse: # yes, continue rolling
                    print('yes.')
                    self.myTurn = True
                else:
                    print('no.')
                    self.myTurn = False # no, stop rolling
                t.sleep(1)
                #self.turnPoints = self.turnPoints + currentPoints
        self.bankPoints = self.bankPoints + self.turnPoints # banking the turnPoints
        self.turnPoints = 0 # resetting turn points
        self.numRolls = 0 # resetting numRolls

def determineOrder(playersArray):# does not work yet
    firstPlayer = playersArray[rand.randint(0, len(playersArray)-1)] # randomly choose who goes first

def main():
    print('Welcome to a game of Toss The Pigs! Current functionalities only allow you to play against an AI...')
    t.sleep(5)
    #print('(Enter a number from 1 - 3): ') # need to add fool-proof method
    #numPlayers = int(input())
    #players = []
    #for i in range(numPlayers):
    #    print('Enter name of player ' + str(i))
    #    players.append()
    #print('Would you like to play against a computer? (y/n)') # need to add fool-proof method
    #print('(Adding a computer player increases the total players by 1)')
    #computerOrNot = input().lower()

    player1 = PassThePigsSimulator1P('Kuan',0 ,True)
    aiPlayer = PassThePigsSimulator2P('AI',0 , False)
    while(player1.isWinner() == False and aiPlayer.isWinner() == False): # no winner

        player1.updateBankPoints(aiPlayer)

        aiPlayer.aiUpdateBankPoints(player1)
    if player1.isWinner():
        print('The Winner is ' + player1.player)
    else:
        print('The AI Won!')

main()

Toss_The_Pigs/Pass the pigs simulator.py


import urllib.request
import os
import zipfile

def download(data_path):
    url = "https://bai-scieval.duiopen.com/bai-scieval.zip"
    save_path = data_path + "/bai-scieval.zip"
    unzip_path = data_path

    if not os.path.isdir(unzip_path):
        os.mkdir(unzip_path)

    try:
        urllib.request.urlretrieve(url, save_path)
    except:
        print("download failed!")
        return False

    #开始解压
    #判断是否是压缩文件
    if zipfile.is_zipfile(save_path):
    #读取压缩文件
        fz = zipfile.ZipFile(save_path, 'r')
    #提取文件到解压目录
        for file in fz.namelist():
            fz.extract(file, unzip_path)
    else:
    #否则返回失败
        return False
    return True


from numpy import float64, array

def remap(values):
    """Remapping function."""

    # S2f.
    s2f = values[0]

    # S2s.
    if values[0] == 0.0:
        s2s = 1e99
    else:
        s2s = values[1]*values[0]

    # ts.
    ts = values[2]

    return array([s2f, s2s, ts], float64)

# The model-free model name.
name = 'm5'

# Create the data pipe.
pipe.create(name, 'mf')

# Set up the 15N spins.
sequence.read(file='noe.500.out', res_num_col=1, res_name_col=2)
spin.name('N')
spin.element('N')
spin.isotope('15N', spin_id='@N')

# Load the relaxation data.
relax_data.read(ri_id='R1_600',  ri_type='R1',  frq=600.0*1e6, file='r1.600.out', res_num_col=1, data_col=3, error_col=4)
relax_data.read(ri_id='R2_600',  ri_type='R2',  frq=600.0*1e6, file='r2.600.out', res_num_col=1, data_col=3, error_col=4)
relax_data.read(ri_id='NOE_600', ri_type='NOE', frq=600.0*1e6, file='noe.600.out', res_num_col=1, data_col=3, error_col=4)
relax_data.read(ri_id='R1_500',  ri_type='R1',  frq=500.0*1e6, file='r1.500.out', res_num_col=1, data_col=3, error_col=4)
relax_data.read(ri_id='R2_500',  ri_type='R2',  frq=500.0*1e6, file='r2.500.out', res_num_col=1, data_col=3, error_col=4)
relax_data.read(ri_id='NOE_500', ri_type='NOE', frq=500.0*1e6, file='noe.500.out', res_num_col=1, data_col=3, error_col=4)

# Initialise the diffusion tensor.
diffusion_tensor.init(1e-8, fixed=True)

# Create all attached protons.
sequence.attach_protons()

# Define the magnetic dipole-dipole relaxation interaction.
interatom.define(spin_id1='@N', spin_id2='@H', direct_bond=True)
interatom.set_dist(spin_id1='@N', spin_id2='@H', ave_dist=1.02 * 1e-10)

# Define the CSA relaxation interaction.
value.set(N15_CSA, 'csa')

# Select the model-free model.
model_free.select_model(model=name)

# Map data.
inc = 100
params = ['s2f', 'ts', 's2s']
lower = [0.5, 0, 0.5]
upper = [1.0, 300e-12, 1.0]
point = [0.952, 32.0e-12, 0.582]
point = [point[0], point[1], point[0]*point[2]]

dx.map(params=params, spin_id=":1", inc=inc, lower=lower, upper=upper, file_prefix='remap', point=point, axis_incs=5, remap=remap)
dx.execute(file='remap')


import operator
import spacy

from flask import Flask
from flask import request, jsonify

app = Flask(__name__)

class TextClassificationModel(object):
    def __init__(self, path_to_model):
        # Load the best trained Text Classification model
        self.nlp_model = spacy.load(path_to_model)

    def predict(self, sentence):
        # Transform the input sentence to SpaCy's Doc with predicted categories
        doc = self.nlp_model(sentence)
        return doc.cats

nlp_model = TextClassificationModel('./training/model-best/')

@app.route('/predict', methods=['GET'])
def predict_category_for_input_sentence():
    sentence = request.args.get('sentence')
    cats = nlp_model.predict(sentence=sentence)

    best_category = max(cats.items(), key=operator.itemgetter(1))[0]
    best_score = [value for key, value in cats.items() if key == best_category][0]

    return jsonify({'code': 200, 'category': best_category, 'score': best_score})

if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port='5000')


import sys

def is_prime(m, n):
    result = [False, False] + ([True] * (n-1))
    sum = 0
    for i in range(2, int(n**1/2) + 1):
        if result[i] == True:
            for j in range(i*2, n+1, i):
                result[j] = False
    try:
        idx = result.index(True, m, n+1)
        for i in range(m, n+1):
            if result[i] == True:
                sum += i
        return sum, idx
    except:
        return -1, -1
m = int(sys.stdin.readline())
n = int(sys.stdin.readline())

sum, idx = is_prime(m, n)
if sum == -1:
    print(-1)
else:
    print(sum)
    print(idx)


import pandas as pd
import time

def smoothing_3(df):
    df_copy = df.copy()
    max_index = df.shape[0] - 1
    for i in range(max_index-1):
        mean = df.iloc[i:i+3]["quantity"].mean()
        df_copy["quantity"].iloc[i+1] = mean
    df_copy["quantity"].iloc[0] = df.iloc[0:2]["quantity"].mean()
    df_copy["quantity"].iloc[-1] = df.iloc[-2:]["quantity"].mean()
    return df_copy

def smoothing_5(df):
    df_copy = df.copy()
    max_index = df.shape[0] - 1
    for i in range(0, max_index-3):
        mean = df.iloc[i:i+5]["quantity"].mean()
        df_copy["quantity"].iloc[i+2] = mean
    df_copy["quantity"].iloc[0] = df.iloc[0:2]["quantity"].mean()
    df_copy["quantity"].iloc[1] = df.iloc[0:3]["quantity"].mean()
    df_copy["quantity"].iloc[-1] = df.iloc[-2:]["quantity"].mean()
    df_copy["quantity"].iloc[-2] = df.iloc[-3:]["quantity"].mean()
    return df_copy

def smoothing_5_new(df):
    df_copy = df.copy()
    max_index = df.shape[0] - 1
    for i in range(0, max_index-3):
        mean = df.iloc[i:i+5]["quantity"].mean()
        df_copy["quantity"].iloc[i+2] = mean
    return df_copy.iloc[2:-2]

def smoothing_7_new(df):
    start = time.time()
    df_copy = df.copy()
    max_index = df.shape[0] - 1
    for i in range(0, max_index-5):
        mean = df.iloc[i:i+7]["quantity"].mean()
        df_copy["quantity"].iloc[i+3] = mean
    end = time.time()
    print("time for smoothing", (end - start), " seconds")
    return df_copy.iloc[3:-3]

if __name__ == "__main__":
    import matplotlib.pyplot as plt
    data = pd.read_csv(
        "~/PycharmProjects/seasonality_hypothesis/data_generated/aggregated_complete_outliers_removed_seas.csv",
        names=["dt_week", "quantity"])

    df_1 = smoothing(data)
    df_2 = smoothing_5(data)
    data["dt_week"] = data["dt_week"].apply(lambda x: pd.to_datetime(x, format="%Y-%m-%d"))
    data = data.set_index("dt_week")
    df_1["dt_week"] = df_1["dt_week"].apply(lambda x: pd.to_datetime(x, format="%Y-%m-%d"))
    df_1 = df_1.set_index("dt_week")
    df_2["dt_week"] = df_2["dt_week"].apply(lambda x: pd.to_datetime(x, format="%Y-%m-%d"))
    df_2 = df_2.set_index("dt_week")
    #plt.plot(df_1["quantity"], marker=".", label=' 3 point average')
    plt.plot(df_2["quantity"], marker=".", label=' 5 point average')
    #plt.plot(data["quantity"], marker=".", label='overall data aggregated')
    plt.legend(loc="upper left")
    plt.xlabel("dt_weeks")
    plt.ylabel("aggregated quantities")
    plt.title("5 point average")
    plt.show()


import csv
import re
import string

headers = []
all_star_data = []

with open("main.csv", "r") as f:
    csv_reader = csv.reader(f)

    for index, row in enumerate(csv_reader):
        if index == 0:
            headers.append(row)
            headers = headers[0]
        else:
            all_star_data.append(row)

# print(headers, all_star_data)

headers.append('Gravity')

gravity_list = []

def filter_data(data):
    non_numeric_chars = list(set(string.printable) - set(string.digits))
    for index, star_data in enumerate(data):
        for index, values in enumerate(star_data):
            if values == '?':
                del data[index]
            if index == 3 or index == 4 or index == 5:
                for char in non_numeric_chars:
                    if char in values:
                        del data [index]
    return data

all_star_data = filter_data(all_star_data)

for index, star_data in enumerate(all_star_data):
    all_star_data[index][3] = re.sub('[^0-9]', '', star_data[3])
    all_star_data[index][4] = re.sub('[^0-9]', '', star_data[4])

for index, star_data in enumerate(all_star_data):
    mass = float(star_data[3]) * 1.989e+30
    radius = float(star_data[4]) * 6.957e+8

    gravity = (6.67e-11) * mass / (radius**2)

    gravity_list.append(gravity)

for index, star_data in enumerate(all_star_data):
    all_star_data[index].append(gravity_list[index])
    all_star_data[index][0] = index + 1

with open("main1.csv", "w") as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(headers)
    csv_writer.writerows(all_star_data)


# -*- coding: utf-8 -*-
"""
Thanks to the author Ruhan Sa, who is the TA of IR project 3 in Fall 2015
"""
print("In Python")
import json
import sys
import urllib.request
from collections import defaultdict

count = 1
langs = {'text_de','text_ru','text_en'}
core = sys.argv[2]
model = {'DFR', 'TFIDF', 'BM25'}
countRows = 0
outf = open(sys.argv[1] + '.txt', 'w+')
count = 1;
sortingList = []
QueryList = []
QueryDict = defaultdict(list)
writingDictionary = {}
with open('queries.txt', encoding="utf-8") as f:
	for line in f:
		query = line.strip('s\n').replace(':', '')[4:]
		query = urllib.parse.quote(query)
		countRows = 1
		for lang in langs:
			inurl = ('http://ec2-35-163-27-180.us-west-2.compute.amazonaws.com:8983/solr/' + core +
					'/select?df=' + lang + '&fl=score,id&indent=on&q=' + query + '&rows=20&wt=json')
			qid = count
			IRModel = core
			data = urllib.request.urlopen(inurl).read()
			docs = json.loads(data.decode('utf-8'))['response']['docs']
			rank = 1
			for doc in docs:
				countRows += 1
				if qid == 10 or qid == 20:
					score = float((str(doc['score']) + str(countRows) + '5'))
					sortingList.append(score)
					writingDictionary.update({str('Q' + str(count) + str(score)): str(doc['id'])})
				else:
					score = float((str(doc['score']) + str(countRows)))
					sortingList.append(score)
					writingDictionary.update({str('Q' + str(count) + str(score)): str(doc['id'])})
		sortingList.sort(reverse=True)
		QueryDict.setdefault('Q' + str(count), sortingList)
		QueryList.append('Q' + str(count))
		sortingList = []
		count += 1
countRows = 1
count = 1
for query in QueryList:
	for key in QueryDict.get(query):
		print('Q' + str(count) + str(key))
		if countRows > 20:
			break
		else:
			if count > 9:
				outf.write('0' + str(count) + ' ' + 'Q' + str(count) + ' ' + writingDictionary.get('Q' + str(count) + str(key)) + ' ' + str(countRows) + ' ' + str(key)[:-1] + ' ' + core + '\n')
			else:
				outf.write('00' + str(count) + ' ' + 'Q' + str(count) + ' ' + writingDictionary.get('Q' + str(count) + str(key)) + ' ' + str(countRows) + ' ' + str(key)[:-1] + ' ' + core + '\n')
			countRows += 1
	count += 1
	countRows = 1
outf.close()


import requests
from telebot import *
from telebot.types import ReplyKeyboardMarkup, KeyboardButton
import webbrowser
import json
import random
TOKEN= '6165585308:AAFqLhFGa10yIc-4S0ktm_E3_oxqO8lBtto'

bot = TeleBot(TOKEN)

keyboard = ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.add(KeyboardButton('👍Годные тайтлы'))
keyboard.add(KeyboardButton('🤷♂Рандомное аниме'))

keyboard.add(KeyboardButton('Об авторе и боте'))

#-=-=-=-=-Получение списка anime-=-=-=-=-=-=-=-=
url = "https://api.jikan.moe/v4/anime"

response = requests.get(url)

data = response.json()
anime_titles = [item['title'] for item in data['data']]

# for i in anime_titles:
#     print(i)

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=

@bot.message_handler(regexp='автор')
def about_author(message):
    bot.send_message(message.chat.id, 'Автор Наум')

@bot.message_handler(regexp='🤷♂Рандомное Аниме')
def godnie_taitls(message):
    bot.send_message(message.chat.id, f'Нашел для тебя: {anime_titles[random.randint(1,20)]}', reply_markup=keyboard)

@bot.message_handler(regexp='👍Годные тайтлы')
def godnie_taitl(message):
    bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEJOoRkfzAXYWnuBMaa7SOhGnKLtchlIQACriIAAvThCUilx87YPT22ey8E')
    a = '\n'.join(str(x) for x in anime_titles)
    bot.send_message(message.chat.id, a)

@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    bot.send_message(message.chat.id, "Я бот, который советует тебе аниме, чтобы тебе не было скучно", reply_markup=keyboard)
    bot.send_sticker(message.chat.id, 'CAACAgUAAxkBAAEJNUlkfJxisSAeZ5X3M1am1HJlQXwdrgACogMAAh4QYVWLQatEtVFxri8E')

@bot.message_handler(regexp='открой сайт')
def opener(message):
    webbrowser.open('https://animego.org/')

bot.infinity_polling()


from pyspark.sql import SparkSession
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql.types import BooleanType, StructField,StructType, IntegerType, DateType, LongType, DoubleType, StringType

spark = SparkSession.builder.appName("Query1_DFAPI").getOrCreate()

fileSchema = spark.read.options(header="true", inferSchema = "true").csv("/home/mikeg/Documents/databases/TPC-H/TPC-H_Tools_v3.0.0/dbgen/tmpqueries/schema/lineitem.csv").schema
lineitem = spark.read.format("csv").options(inferSchema = "true" , header = "false").schema(fileSchema).option("delimiter", "|").csv("hdfs://localhost:9000/user/diplomma/data/data/lineitem.tbl")

partSchema = spark.read.options(header="true", inferSchema = "true").csv("/home/mikeg/Documents/databases/TPC-H/TPC-H_Tools_v3.0.0/dbgen/tmpqueries/schema/part.csv").schema
part = spark.read.format("csv").options(inferSchema = "true" , header = "false").schema(partSchema).option("delimiter", "|").csv("hdfs://localhost:9000/user/diplomma/data/data/part.tbl")

lineitem.registerTempTable("lineitem")
part.registerTempTable("part")

sqlString= "select \
        sum(l_extendedprice) / 7.0 as avg_yearly \
from \
        lineitem, \
        part \
where \
        p_partkey = l_partkey \
        and p_brand = 'Brand#23' \
        and p_container = 'MED BOX' \
        and l_quantity < ( \
                select \
                        0.2 * avg(l_quantity) \
                from \
                        lineitem \
                where \
                        l_partkey = p_partkey \
        );"

res = spark.sql(sqlString)
res.show()


#!/usr/bin/env python3
from typing import Iterable, List
from flask import Flask, jsonify, request, Response
from janome.analyzer import Analyzer
from janome.tokenizer import Token, Tokenizer
from janomeutils import ChunkFilter, token_to_morae

app = Flask(__name__)
tokenizer = Tokenizer(mmap=True)
analyzer = Analyzer(tokenizer=tokenizer, token_filters=[ChunkFilter()])

@app.route('/api/analyze', methods=['POST'])
def analyze() -> Response:
    sentence: str = request.get_data(as_text=True)
    chunks: Iterable[List[Token]] = analyzer.analyze(sentence)
    return jsonify(
        [[{'surface': token.surface, 'morae': list(token_to_morae(token)), 'pos': token.part_of_speech}
          for token in chunk] for chunk in chunks]
    )

@app.route('/', methods=['GET'])
def homepage() -> Response:
    return app.send_static_file('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)


####################################################################################################
####################################     PHI_ACCEPT   ##############################################
####################################################################################################

def loContiene(estadosFinales, celda):
    for q in estadosFinales:
        if(q == celda):
            return True
    return False

def crearLiteralesFinales(estadosFinales, n):
    literalesFinales = []
    literalesFinales_latex=[]
    for i in range(1,n-1,1):
        for j in range(1,n-1,1):
            for q in estadosFinales:
                numero = q[1]
                literal_latex = '$X_'+str(i)+',_'+str(j)+'\\_q_'+numero+'$'
                literal = 'X'+str(i)+'_'+str(j)+'_'+q
                literalesFinales.append(literal)
                literalesFinales_latex.append(literal_latex)
    return literalesFinales, literalesFinales_latex

def generarPhiAccept(tabla, estadosFinales, n):
    #asegura que qaccept aparezca
    #en alguna celda. (QUE HAYA ESTADO FINAL EN ALGUNA CELDA, LA QUE SEA)
    literalesFinales, literalesFinales_latex = crearLiteralesFinales(estadosFinales, n)
    tam = len(literalesFinales)
    loCumple = False
    phi_accept_latex = ''
    phi_accept_valores_latex = ''
    phi_Accept=""
    phi_Accept_valores=""

    for i in range(0,tam,1):
        if(i< tam-1):
            phi_accept_latex += literalesFinales_latex[i]+'\\ $\\vee$\\ '
            phi_Accept += literalesFinales[i] + ' OR '
        else:
            phi_accept_latex += literalesFinales_latex[i]
            phi_Accept += literalesFinales[i]

    for i in range(0,n,1):
        for j in range(0,n,1):
            celda = tabla[i][j]
            if((j == i) and (j == n-1)):
                if(loContiene(estadosFinales, celda)):
                    phi_accept_valores_latex += 'True\\ '
                    phi_Accept_valores += 'True '
                    loCumple = True
                else:
                    phi_accept_valores_latex += 'False\\ '
                    phi_Accept_valores += 'False '
            else:
                if(loContiene(estadosFinales, celda)):
                    phi_accept_valores_latex += 'True\\ $\\vee$\\ '
                    phi_Accept_valores += 'True OR '
                    loCumple = True
                else:
                    phi_accept_valores_latex += 'False\\ $\\vee$\\ '
                    phi_Accept_valores += 'False OR '

    return phi_Accept, phi_Accept_valores, loCumple, phi_accept_latex, phi_accept_valores_latex


import pandas as pd
import json

input_excel_file = "G:\Django\input_excel_file.xlsx"
df = pd.read_excel(input_excel_file)

df = df.fillna("NULL")

grouped_data = df.groupby(['Customer ID', 'age'])

output_data = []

for (customer_id, age), group in grouped_data:
    customer_entry = {
        "Customer ID": str(customer_id),
        "age": str(age),
        "panelList": []
    }

    panels_dict = {}

    for index, row in group.iterrows():
        panel_name = row["panel_name"]
        panel_code = row["panel_code"]
        parameter_entry = {
            "parameterName": row["Parameter Name"],
            "unit": row["Units"],
            "parameterCode": str(row["Parameter Code"]),
            "value": str(row["Result"]),
            "lowerRange": str(row["Low Range"]),
            "upperRange": str(row["High Range"]),
            "displayRange": f"{row['Low Range']}-{row['High Range']}"
        }

        if panel_name not in panels_dict:
            panels_dict[panel_name] = {
                "panel_name": panel_name,
                "panel_code": panel_code,
                "parameters": []
            }

        panels_dict[panel_name]["parameters"].append(parameter_entry)

    customer_entry["panelList"] = list(panels_dict.values())
    output_data.append(customer_entry)

output_json = json.dumps(output_data, indent=4)

with open("final_output.json", "w") as json_file:
    json_file.write(output_json)

print("JSON transformation complete.")


# try:
# 	f=open('decode_in_1001','r')
# 	print(f.read())
# finally:
# 	if f:
# 		f.close()

#with语句自动调用close()方法
with open('decode_in_1001','r') as f:
#	print(f.read())
	for line in f.readlines():
		print(line.strip(),'\n') #把末尾的‘\n’删掉

#还可以读取二进制文件，比如图片
# f=open('psbMQHSGFY3.jpg','rb')
# f.readline()

#写文件
with open('write_file.txt','w') as f:
	f.write('Hello,world!')


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

from aocd.models import Puzzle
import utils

def compare(first, second, debug=False):
    if debug:
        print(first, "vs", second)
    if isinstance(second, int) and isinstance(first, int):
        if first == second:
            return None
        return first < second
    if isinstance(first, int):
        first = [first]
    if isinstance(second, int):
        second = [second]

    for i, j in zip(first, second):
        # now both lists
        is_valid = compare(i, j)
        if is_valid is not None:
            return is_valid

    if len(first) < len(second):
        return True
    if len(first) > len(second):
        return False
    return None

@utils.timeit
def solve(input_f=None):
    if not input_f:
        puzzle = Puzzle(year=2022, day=13)
        data = puzzle.input_data.split("\n")
    else:
        data = open(input_f).readlines()
    data.append("\n")
    processed_data = []
    idx = 1
    res = 0
    for line in data:
        if line.strip():
            item = eval(line.strip())
            processed_data.append(item)
        else:
            is_right = compare(processed_data[0], processed_data[1])
            if is_right:
                res += idx
            idx += 1
            processed_data = []
    return res

if __name__ == "__main__":
    # test_puzzle
    print("--------------TEST--------------")
    res = solve("test.txt")
    print("test answer: ", res)

    # real puzzle
    print("--------------PUZZLE--------------")
    res = solve()
    print("puzzle answer", res)


import click
import matplotlib.pyplot as plt
from matplotlib import rc
import numpy as np
import pandas as pd

rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
# rc('text', usetex=True)

fsl = fs = 9

@click.command()
@click.argument('csv-path', type=click.File())
@click.option('--outputpath', '-o', type=click.Path(), default="diff-selectivity.png")
@click.option('--plotnum', '-n', type=int, default=1)
def d_selectivity(csv_path, outputpath="diff-selectivity.png", plotnum=0):
    fig = plt.figure(figsize=(7., 7.))

    data = pd.read_csv(csv_path)
    data["log_CO2_H2O"] = np.log10(data["CO2 / H2O"])
    data["log_CO2_N2"] = np.log10(data["CO2 / N2"])
    uio66 = data[data.mof.str.startswith("UIO-66")]
    uio67 = data[data.mof.str.startswith("UIO-67")]
    moflabels = list(data.mof.str.replace("UIO-6[67] ", ""))

    ax = fig.subplots(ncols=1)
    sc1 = ax.scatter(uio66.log_CO2_H2O, uio66.log_CO2_N2, zorder=2, s=10, marker=">", label="UIO-66", c="sienna")
    sc2 = ax.scatter(uio67.log_CO2_H2O, uio67.log_CO2_N2, zorder=2, s=20, marker= r'$\bowtie$', label="UIO-67", c="mediumseagreen")
    ax.grid(which='major', axis='both', linestyle='-', color='0.9', zorder=10)
    ax.set_xlabel('log$_{10}$ CO$_2$/H$_2$O diffusion selectivity', fontsize=fsl)
    ax.set_ylabel('log$_{10}$ CO$_2$/N$_2$ diffusion selectivity', fontsize=fsl)

    if plotnum == 0:
        ax.set_xlim(-0.8, 3.3)
        ax.set_ylim(-1.6, .1)
        ax.set_yticks([-1., 0.])
        ax.set_xticks([0., 1., 2., 3.])

        ax.axline([0,0], slope=-1, ls="--", color=".8", zorder=1)
        ax.annotate("0", (-.1,.1), size=5, xytext=(5,-2), textcoords='offset points',
            horizontalalignment="left", verticalalignment='top', fontsize=fsl, c=".5")
        ax.axline([1,0], slope=-1, ls="--", color=".8", zorder=1)
        ax.annotate("+1", (0.9,.1), size=5, xytext=(5,-2), textcoords='offset points',
            horizontalalignment="left", verticalalignment='top', fontsize=fsl, c=".5")
        ax.axline([2,0], slope=-1, ls="--", color=".8", zorder=1)
        ax.annotate("+2", (1.9,.1), size=5, xytext=(5,-2), textcoords='offset points',
            horizontalalignment="left", verticalalignment='top', fontsize=fsl, c=".5")

        for i, mof in enumerate(data.mof):
            x, y = (data.log_CO2_H2O[i], data.log_CO2_N2[i])
            if mof in ["UIO-67"]:
                ax.annotate(moflabels[i], (x, y), size=5, textcoords='offset points', xytext=(x-4.,y - 0), horizontalalignment='right', verticalalignment='center', fontsize=fs)
            elif mof in ["UIO-66", "UIO-66 2x OH", "UIO-66 OH", "UIO-66 4x F", "UIO-67 2x N$_3$", "UIO-67 2x alkane-OC$_3$"]:
                ax.annotate(moflabels[i], (x, y), size=5, textcoords='offset points', xytext=(x+4,y + 0), horizontalalignment='left', verticalalignment='center', fontsize=fsl)

    elif plotnum == 1:
        ax.set_xlim(1.1, 3.3)
        ax.set_ylim(-0.6, .1)
        ax.set_yticks([-0.5, 0.])
        ax.set_xticks([1.5, 2., 2.5, 3.])

        ax.axline([1,0], slope=-1, ls="--", color=".8", zorder=1)
        ax.annotate("+1", (1.1,-0.1), size=5, xytext=(5,-2), textcoords='offset points',
            horizontalalignment="left", verticalalignment='top', fontsize=fsl, c=".5")
        ax.axline([2,0], slope=-1, ls="--", color=".8", zorder=1)
        ax.annotate("+2", (1.9,.1), size=5, xytext=(5,-2), textcoords='offset points',
            horizontalalignment="left", verticalalignment='top', fontsize=fsl, c=".5")

        for i, mof in enumerate(data.mof):
            x, y = (data.log_CO2_H2O[i], data.log_CO2_N2[i])
            if (1.1 < x < 3.3) and (-0.6 < y < 0.1):
                if mof in ["UIO-67", "UIO-67 2x alkane-OC$_4$", "UIO-67 branched-HNC$_5$"]:
                    ax.annotate(moflabels[i] + " - %3.1f" % (float(data.CO2[i]) * 1e4), (x, y), size=5, xytext=(x-0.03,y - 0.), horizontalalignment='right', verticalalignment='center', fontsize=fsl)
                else:
                    ax.annotate(moflabels[i] + " - %3.1f" % (float(data.CO2[i]) * 1e4), (x, y), size=5, xytext=(x+0.03,y - 0.), horizontalalignment='left', verticalalignment='center', fontsize=fsl)

    ax.legend()
    # fig.subplots_adjust(wspace=0.05, hspace=0.05)
    fig.savefig(outputpath, dpi=300, bbox_inches='tight') # , transparent=True
    plt.close(fig)

if __name__ == '__main__':
    d_selectivity()


from typing import Collection

from opentelemetry.instrumentation.aiopg import wrappers
from opentelemetry.instrumentation.aiopg.package import _instruments
from opentelemetry.instrumentation.aiopg.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

class AiopgInstrumentor(BaseInstrumentor):
    _CONNECTION_ATTRIBUTES = {
        "database": "info.dbname",
        "port": "info.port",
        "host": "info.host",
        "user": "info.user",
    }

    _DATABASE_SYSTEM = "postgresql"

    def instrumentation_dependencies(self) -> Collection[str]:
        return _instruments

    def _instrument(self, **kwargs):
        """Integrate with PostgreSQL aiopg library.
        aiopg: https://github.com/aio-libs/aiopg
        """

        tracer_provider = kwargs.get("tracer_provider")

        wrappers.wrap_connect(
            __name__,
            self._DATABASE_SYSTEM,
            self._CONNECTION_ATTRIBUTES,
            version=__version__,
            tracer_provider=tracer_provider,
        )

        wrappers.wrap_create_pool(
            __name__,
            self._DATABASE_SYSTEM,
            self._CONNECTION_ATTRIBUTES,
            version=__version__,
            tracer_provider=tracer_provider,
        )

    # pylint:disable=no-self-use
    def _uninstrument(self, **kwargs):
        """ "Disable aiopg instrumentation"""
        wrappers.unwrap_connect()
        wrappers.unwrap_create_pool()

    # pylint:disable=no-self-use
    def instrument_connection(self, connection, tracer_provider=None):
        """Enable instrumentation in a aiopg connection.

        Args:
            connection: The connection to instrument.
            tracer_provider: The optional tracer provider to use. If omitted
                the current globally configured one is used.

        Returns:
            An instrumented connection.
        """
        return wrappers.instrument_connection(
            __name__,
            connection,
            self._DATABASE_SYSTEM,
            self._CONNECTION_ATTRIBUTES,
            version=__version__,
            tracer_provider=tracer_provider,
        )

    def uninstrument_connection(self, connection):
        """Disable instrumentation in a aiopg connection.

        Args:
            connection: The connection to uninstrument.

        Returns:
            An uninstrumented connection.
        """
        return wrappers.uninstrument_connection(connection)


import keyboard
import mouse
import time
import random
import pyperclip
import tkinter as tk
from tkinter import ttk
import pyautogui
from win32api import GetSystemMetrics
import win32api

r = tk.Tk()
r.title('КлаваБот')
r.resizable(False, False)

rusLet = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮёЁ"
numbers = "0123456789."
specsym = ".,!\"№;%:?*()_+/-=`~[]{}\\|"
conv1 = "aoc,." + chr(171) + chr(187) + chr(8212) + chr(8211)
conv2 = "аос,.\"\"--"
prm = 0
nrm = 0
zrm = 0
mpr = 0
mom = tk.BooleanVar()
mom.set(False)

w = GetSystemMetrics(0)
h = GetSystemMetrics(1)

def show_all():
    global sl
    global sn
    global ss
    global mg
    global po
    global prm
    global nrm
    global zrm
    global mom
    global mpr
    prm = sl.get()
    nrm = sn.get()
    zrm = ss.get()
    mpr = po.get()
    mom = mom.get()
    print(prm, nrm, zrm, mom, mpr)
    return [prm, nrm, zrm, mpr]

def pech(zn):
    if zn in rusLet:
        keyboard.write(zn)
        if not (mom):
            time.sleep(60 / prm / 1.175 + random.randint(-20, 20) / prm)
    elif zn in numbers:
        keyboard.write(zn)
        if not (mom):
            time.sleep(60 / nrm / 1.175 + random.randint(-20, 20) / nrm)
    elif zn in specsym:
        keyboard.write(zn)
        if not (mom):
            time.sleep(60 / zrm / 1.175 + random.randint(-20, 20) / zrm)
    else:
        if zn in conv1:
            pech(conv2[conv1.index(zn)])

def write(text):
    i = 0
    while i < len(text):
        keyboard.release('ctrl')
        if mpr == 0 or random.randint(0, int(len(text) / mpr / 2)) != 0:
            if text[i] != " ":
                pech(text[i])
                i += 1
            else:
                keyboard.press("\u0020")
                time.sleep(60 / prm + random.randint(-20, 20) / prm)
                keyboard.release("\u0020")
                i += 1
        else:
            if text[i] in rusLet:
                a = random.randint(1, 3)
                if i + a < len(text) - 12:
                    keyboard.write(text[i + a])
                    keyboard.press('backspace')
                    keyboard.release('backspace')
                    if not(mom):
                        time.sleep(60 / prm + random.randint(-3, 3) / prm)

def run():
    global prm
    global nrm
    global zrm
    global mpr
    global mom
    r.withdraw()
    for i in show_all():
        for j in range(len(i)):
            if not(i[j] in numbers):
                r.deiconify()
                mom = tk.BooleanVar()
                err = ttk.Label(text="ОШИБКА!", foreground='red', font='Georgia 25')
                err.pack(anchor='nw')
                return 1
    win32api.LoadKeyboardLayout('00000409', 1)
    if prm != '' and nrm != '' and zrm != '' and mpr != '':
        prm = int(prm)
        nrm = int(nrm)
        zrm = int(zrm)
        mpr = float(mpr)
    else:
        r.deiconify()
        err = ttk.Label(text="ОШИБКА!", foreground='red', font='Georgia 25')
        return 1
    #print(type(prm), type(nrm), type(zrm), type(mpr))
    keyboard.wait('alt+k+b')
    keyboard.release('alt+k+b')
    pyperclip.copy("Текст скрыт до начала игры")
    mouse.wheel(10000)
    mouse.move(350 * w / 1920, 90 * h / 1080 + (60 * h / 1080 * int(pyautogui.pixel(mouse.get_position()[0], 100)[0] == pyautogui.pixel(mouse.get_position()[0], 100)[1] == pyautogui.pixel(mouse.get_position()[0], 100)[2])) + 270 * h / 1080)
    i = mouse.get_position()[1]
    while pyautogui.pixel(mouse.get_position()[0], i) != (235, 235, 235):
        i += 1
    mouse.move(mouse.get_position()[0], i + 4 * h / 1080)
    while ("Текст скрыт до начала игры" in pyperclip.paste()):
        mouse.wheel(10000)
        time.sleep(0.01)
        mouse.click("left")
        time.sleep(0.03)
        mouse.click("left")
        time.sleep(0.03)
        mouse.press("left")
        time.sleep(0.03)
        keyboard.press("ctrl+c")
        keyboard.release("ctrl+c")
        mouse.release('left')
    time.sleep(2.1)
    i = mouse.get_position()[1]
    while pyautogui.pixel(mouse.get_position()[0], i) != (255, 255, 255):
        mouse.wheel(10000)
        i += 1
    mouse.move(mouse.get_position()[0], i + 5, True)
    mouse.click('left')
    texta = pyperclip.paste()
    for i in range(len(texta)):
        print(ord(texta[i]), "-", texta[i])
    time.sleep(0.1)
    write(texta)
    r.quit()
    exit(0)

gr = ttk.Label(text="Привет! Я КлаваБот! Мои требования: сделай масштаб страницы 100%! Я пока так не умею  :(   \n")
gr.pack(anchor='nw')

comment = ttk.Label(text="После запуска нужно нажать alt+k+b, чтобы я начал копирование текста,\nэто можно делать еще \
 до начала заезда. А ниже можно настроить меня.  :-)\n")
comment.pack(anchor='nw')

esl = ttk.Label(text="Введи скорость набора букв (в зн./мин)")
esl.pack(anchor='nw')

sl = ttk.Entry()
sl.pack(anchor='nw', padx=3)

esn = ttk.Label(text="Введи скорость набора цифр (также в зн./мин)")
esn.pack(anchor='nw')

sn = ttk.Entry()
sn.pack(anchor='nw', padx=3)

ess = ttk.Label(text="Введи скорость набора знаков перпенания и прочих специальных символов")
ess.pack(anchor='nw')

ss = ttk.Entry()
ss.pack(anchor='nw', padx=3)

epo = ttk.Label(text="Введи процент ошибок")
epo.pack(anchor='nw')

po = ttk.Entry()
po.pack(anchor='nw', padx=3)

pus1 = ttk.Label(text="")
pus1.pack(anchor='nw')

mg = ttk.Checkbutton(text='Набирать текст моментально (в таком случае только у пробелов будет задержка)',
                    variable=mom)
mg.pack(anchor='nw')

pus2 = ttk.Label(text="")
pus2.pack(anchor='nw')

run = ttk.Button(text='Запустить!', command=run)
run.pack(anchor='se')

r.mainloop()


from __future__ import unicode_literals

import codecs
import json
import os.path
import re

FLAGS = re.UNICODE | re.IGNORECASE

# Some examples dates from the data
# =================================
#
# Birth dates
# ===========
# "* 1122"
# "* um 1480"
# "geb. 10.11.1810"
# "* 3o.9.1859"
#
# Death dates
# ===========
# "† 19.4.1967"
# "+ 4.6.1875"
# "gest. 02.05.1899"
# "hingerichtet: 02.02.1945"
# "gestorben 29.02.1980"
# "+ Januar 1944"
# "+ vermutlich am 9.7.1386"
#
# Special cases
# =============
# "+19.3.1884 Eppingen, + 12.4.1971 Eppingen"
# "* 3o.9.1859"
# "1843-1906"

MONTHS = '(?:Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember)'

DATE_REGEXES = [
    # "23.10.1805", "4.9.1965", "24.12. 1930"
    r'(?:am\s+)?\d\d?\.\s*\d\d?\.\s*(\d\d\d\d?)',

    # "10. Dezember 1815"
    r'(?:am\s+)?\d\d?\.\s*' + MONTHS + r'\s*(\d\d\d\d?)',

    # "1122", "um 1480"
    r'(?:um)?\s*(\d\d\d\d?)',

    # "Januar 1944"
    MONTHS + r'\s*(\d\d\d\d?)',
]

# Allow 'o' as a digit
DATE_REGEXES = [r.replace(r'\d', r'(?:\d|o)') for r in DATE_REGEXES]

def extract_year(s, prefixes=None):
    """
    Try to extract a year from a string.

    ``s`` is the string to search in. ``prefixes`` is an optional list
    of patterns that may occur before the actual date.

    Dates must match one of the regexes in ``DATE_REGEXES`` (in
    combination with one of the prefixes if any are given).

    The year of the first date found is returned. If no matching date
    is found then ``None`` is returned.
    """
    prefixes = [unicode(p) for p in (prefixes or [''])]
    for pattern in DATE_REGEXES:
        for prefix in prefixes:
            p = prefix + r'\s*' + pattern
            m = re.search(p, s, FLAGS)
            if not m:
                continue
            return int(m.groups()[0].replace('o', '0'))

def extract_person_data(info):
    """
    Try to extract person data from info text.

    Returns a tuple containing the person's name and the years of birth
    and death. Each of these may be ``None`` if the extraction failed.
    """
    birth = extract_year(info, prefixes=[r'\*', r'geb\.'])
    death = extract_year(info, prefixes=[r'†', r'\+', r'gest\.', r'hingerichtet:',
                         r'gestorben'])
    name = None
    if birth or death:
        m = re.match(r'^([\w\s\-.]+)', info, FLAGS)
        if m:
            name = m.groups()[0].strip()
            if name.endswith(' geb'):
                # Birth name ("geboren")
                name = name[:-4]
            elif name.endswith(' gen'):
                # Nick name ("genannt")
                name = name[:-4]
            elif name.endswith('.'):
                if not name[-2] in 'IVX':
                    # Not a roman literal
                    name = name[:-1]
    return name, birth, death

def parse_entries(streets):
    """
    Parse street name data to extract additional information.
    """
    for street in streets.itervalues():
        person, birth, death = extract_person_data(street['info'])
        if person or birth or death:
            street['person'] = person
            street['birth'] = birth
            street['death'] = death

_GENDER_EXCEPTIONS = {
    'Blaise': 'm',
    'Edith': 'f',
    'Elisabeth': 'f',
    'Hedwig': 'f',
    'Henri': 'm',
    'Rahel': 'f',
    'Willi': 'm',
    'Willy': 'm',
    'Wiltraut': 'f',
}

def guess_gender(name):
    """
    Given a person's name guess their gender.

    Returns either ``'m'`` or ``'f'``.
    """
    names = name.split()
    if names[0] == 'Sankt':
        first_name = names[1]
    else:
        first_name = names[0]
    try:
        return _GENDER_EXCEPTIONS[first_name]
    except KeyError:
        pass
    if first_name[-1] in 'aeiy':
        return 'f'
    return 'm'

def guess_genders(streets):
    """
    Guess genders of persons.
    """
    for street in streets.itervalues():
        try:
            street['gender'] = guess_gender(street['person'])
        except KeyError:
            # Not a person
            pass

if __name__ == '__main__':
    HERE = os.path.dirname(os.path.abspath(__file__))
    SOURCE = os.path.join(HERE, 'raw_data.json')
    TARGET = os.path.join(HERE, 'names.json')

    with codecs.open(SOURCE, 'r', encoding='utf8') as f:
        streets = json.load(f)

    parse_entries(streets)

    def copy_person(src, dest):
        src_dict = streets[src]
        dest_dict = streets[dest]
        for k in ['birth', 'death', 'person']:
            dest_dict[k] = src_dict[k]

    def a_person(name, person, birth, death):
        streets[name]['person'] = person
        streets[name]['birth'] = birth
        streets[name]['death'] = death

    def not_a_person(name):
        del streets[name]['person']
        del streets[name]['birth']
        del streets[name]['death']

    # Manual fixes and additions
    #
    # The following changes are manual fixes for errors in the data, fixes
    # for special cases that are too rare to be worth being implemented,
    # and manual additions for missing data.
    streets['Englerstraße']['person'] = 'Karl Engler'
    a_person('Guntherstraße', 'Gundahar', None, 436)
    streets['Agathenstraße']['person'] = 'Agathe von Baden-Durlach'
    streets['Gerda-Krüger-Nieland-Straße']['person'] = 'Gerda Krüger-Nieland'
    copy_person('Tullaweg', 'Tullastraße')
    copy_person('Tullaweg', 'Tullaplatz')
    a_person('Petrus-Waldus-Straße', 'Petrus Waldus', None, None)
    a_person('Am Thomashäusle', 'Thomas Dorner', None, None)
    streets['Gritznerstraße']['person'] = 'Max Karl Gritzner'
    copy_person('Weinbrennerstraße', 'Weinbrennerplatz')
    streets['Martinstraße']['person'] = 'Sankt Martin'
    streets['Nikolausstraße']['person'] = 'Sankt Nikolaus'
    streets['Kaiserstraße']['person'] = 'Wilhelm I.'
    copy_person('Kaiserstraße', 'Kaiserpassage')
    copy_person('Kaiserstraße', 'Kaiserplatz')
    copy_person('Kaiserstraße', 'Kaiserallee')
    streets['Sepp-Herberger-Weg']['person'] = 'Joseph Herberger'
    streets['Mendelssohnplatz']['person'] = 'Moses Mendelssohn'
    copy_person('Martin-Luther-Straße', 'Martin-Luther-Platz')
    streets['Rolandplatz']['person'] = 'Roland'
    streets['Karlstraße']['person'] = 'Karl Ludwig Friedrich von Baden'
    not_a_person('Curjel-und-Moser-Straße')
    copy_person('Baumeisterstraße', 'Reinhard-Baumeister-Platz')
    streets['Laurentiusstraße']['person'] = 'Sankt Laurentius'
    not_a_person('Winkler-Dentz-Straße')
    not_a_person('Eichrodtweg')
    not_a_person('Bernhardstraße')
    a_person('Bernhardusplatz', 'Bernhard II. von Baden', 1428, 1458)
    not_a_person('Geschwister-Scholl-Straße')
    streets['Jakob-Dörr-Straße']['birth'] = 1884
    streets['Jakob-Dörr-Straße']['death'] = 1971
    a_person('Besoldgasse', 'Christoph Besold', None, None)
    streets['Ostendorfstraße']['death'] = 1915
    streets['Rudolfstraße']['person'] = 'Rudolf I. von Baden'
    not_a_person('Gebrüder-Bachert-Straße')
    copy_person('Friedrichsplatz', 'Alte Friedrichstraße')
    not_a_person('Haid-und-Neu-Straße')
    copy_person('Karl-Wilhelm-Straße', 'Karl-Wilhelm-Platz')
    copy_person('Fritz-Haber-Straße', 'Fritz-Haber-Weg')
    streets['Markusstraße']['person'] = 'Sankt Markus'
    streets['Luisenstraße']['person'] = 'Luise Marie Elisabeth von Preußen'
    copy_person('Hildastraße', 'Nördliche Hildapromenade')
    copy_person('Hildastraße', 'Südliche Hildapromenade')
    streets['Ernst-Friedrich-Straße']['person'] = 'Ernst Friedrich'
    streets['Margarethenstraße']['person'] = 'Margarethe Margräfin von Baden'
    streets['Ludwig-Wilhelm-Straße']['person'] = 'Ludwig Wilhelm Prinz von Baden'
    copy_person('Werderstraße', 'Werderplatz')
    streets['Ada-Lovelace-Straße']['person'] = 'Ada Lovelace'
    not_a_person('Bertholdstraße')
    streets['Karl-Friedrich-Straße']['person'] = 'Karl Friedrich von Baden'
    streets['Stephanstraße']['person'] = 'Heinrich von Stephan'
    copy_person('Stephanstraße', 'Stephanplatz')
    streets['Huttenstraße']['person'] = 'Ulrich Reichsritter von Hutten'
    a_person('Schultheiß-Kiefer-Straße','Erhard Kiefer', None, None)
    copy_person('Lützowstraße', 'Lützowplatz')
    streets['Sankt-Florian-Straße']['person'] = 'Sankt Florian'
    a_person('Blankenhornweg', 'Adolph Blankenhorn', 1843, 1906)
    copy_person('Karlstraße', 'Karlstor')
    copy_person('Brahmsstraße', 'Brahmsplatz')
    copy_person('Hermann-Löns-Weg', 'Lönsstraße')
    not_a_person('Gebrüder-Grimm-Straße')
    streets['Hubertusallee']['person'] = 'Hubrtus von Lüttich'
    streets['Philippstraße']['person'] = 'Philipp I. von Baden'
    streets['Sankt-Valentin-Platz']['person'] = 'Sankt Valentin von Terni'
    streets['Gebhardstraße']['person'] = 'Gebhard III. von Zähringen'
    a_person('Rosalienberg', 'Rosalie Lichtenauer', None, None)
    streets['Charlottenstraße']['person'] = 'Anna Charlotte Amalie von Nassau-Dietz-Oranien'
    streets['Sankt-Barbara-Weg']['person'] = 'Sankt Barbara'
    a_person('Graf-Konrad-Straße', 'Konrad I. von Kärnten', 975, 1011)
    not_a_person('Christofstraße')
    streets['Hennebergstraße']['person'] = 'Berthold von Hohenberg'
    a_person('Winkelriedstraße', 'Arnold von Winkelried', None, 1386)
    streets['Viktoriastraße']['person'] = 'Viktoria von Baden'
    a_person('Schultheißenstraße', 'Bernhard Metz', None, None)
    streets['Sophienstraße']['person'] = 'Sophie Wilhelmine von Schleswig-Holstein-Gottorf'
    streets['Augustastraße']['person'] = 'Augusta von Sachsen-Weimar-Eisenach'
    streets['Karolinenstraße']['person'] = 'Karoline von Baden'
    copy_person('Bismarckstraße', 'Kanzlerstraße')
    not_a_person('Friedenstraße')
    streets['Marie-Alexandra-Straße']['person'] = 'Marie Alexandra von Baden'
    streets['Erbprinzenstraße']['person'] = 'Karl Ludwig von Baden'
    streets['Hauckstraße']['year'] = 1950
    streets['Goethestraße'].update({
        'person': 'Johann Wolfgang von Goethe',
        'birth': 1749,
        'death': 1832,
    })
    streets['Ernststraße']['person'] = 'Ernst I. von Baden-Durlach'
    streets['Kronprinzenstraße']['person'] = 'Friedrich Wilhelm Victor August Ernst von Preußen'

    guess_genders(streets)

    # TODO: The following streets are named after several people. The current
    # data format cannot describe that properly:
    #
    #    Eichrodtweg
    #    Winkler-Dentz-Straße
    #    Curjel-und-Moser-Straße
    #    Bernhardstraße
    #    Geschwister-Scholl-Straße
    #    Gebrüder-Bachert-Straße
    #    Bertholdstraße
    #    Peter-und-Paul-Platz
    #    Engler-Bunte-Ring
    #    Gebrüder-Grimm-Straße
    #    Christofstraße

    with codecs.open(TARGET, 'w', encoding='utf8') as f:
        json.dump(streets, f, sort_keys=True, indent=4, separators=(',', ': '))


# -*- coding: utf-8 -*-
"""
Created on Thu Nov 28 15:11:05 2019

@author: Allen
"""
import os
#import tensorflow as tf
import numpy as np
#import random
import csv
import math
#import matplotlib.pyplot as ma
#import keras
#from keras.utils import np_utils
#from keras.models import Sequential
#from keras.layers import Dense,Dropout,Activation,Flatten
#from keras.layers import Conv1D,MaxPooling1D,ZeroPadding1D,normalization
import pywt
import itertools
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox
import matplotlib.pyplot as plt
import pandas as pd
import time
import TimeDomain as TD

def open_file():
    file_path = filedialog.askdirectory()
    if file_path:
        file_path_text.set(''+file_path)
        list_file(file_path)
    else:
        return None

def list_file(path):
    if path != '':
        listbox.delete(0,tk.END)
        list_files = os.listdir(path)
        for file in list_files:
            listbox.insert(tk.END, file)
    else:
        tk.messagebox.showerror(title = 'Oops', message = 'Please select a file path')

def export_file():
    return 0

def check_parameter(input_path):
    if input_path == '':
        tk.messagebox.showerror(title = 'Oops', message = 'Please select a file path')
    elif output_label_str.get() == '':
        tk.messagebox.showerror(title='Oops', message = 'Please enter label number')

    elif output_label_str.get():
        try:
            int(output_label_str.get())
        except ValueError:
            tk.messagebox.showerror(title='Oops', message = 'Please enter label number')
            return False
        else:
            if sampling_length_str.get() == '':
                tk.messagebox.showerror(title='Oops', message = 'Please enter sampling length')
                return False
            elif sampling_length_str.get():
                try:
                    int(sampling_length_str.get())
                except ValueError:
                    tk.messagebox.showerror(title='Oops', message = 'Please enter sampling length')
                    return False
                else:
                    if int(sampling_length_str.get()) < 2:
                        tk.messagebox.showerror(title='Oops', message = 'Please enter sampling length')
                        return False
                    elif output_decomposition_str.get() == '':
                        tk.messagebox.showerror(title='Oops', message = 'Please enter decomposition level number')
                    elif output_decomposition_str.get():
                        try:
                            int(output_decomposition_str.get())
                        except ValueError:
                            tk.messagebox.showerror(title='Oops', message = 'Please enter decomposition level number')
                            return False
                        else:
                            if int(output_decomposition_str.get()) < 0:
                                tk.messagebox.showerror(title='Oops', message = 'Please enter positive number')
                                return False
                            else:
                                return True
    else:
        return False

def enable_time_domain():
    filemenu.entryconfig("Time domain plot", state = "normal")

def disable_time_domain():
    filemenu.entryconfig("Time domain plot", state = "disabled")

def time_domain_plot():
    try :
        label = output_label_str.get()
        plt.savefig("Time Domain" + label + ".jpg")
        plt.show(block = True)
        tk.messagebox.showinfo(title = "Time domain plot", message = "Image Save!!")
        disable_time_domain()
    except:
        tk.messagebox.showerror(title = "Oops", message = "Save failed")
"""
def time_domain(data):
    time_series = pd.Series(data, index = np.arange(0, len(data)/10240, 1/10240))
    time_series = pd.to_numeric(time_series)
    plt.figure().set_size_inches(14, 10)
    time_series.plot()
    plt.title("Time domain",fontsize=18)
    plt.xticks(fontsize=10)
    plt.yticks(fontsize=10)
    plt.ylabel("Accelaration (g)")
    plt.xlabel('Time (s)')
    #plt.savefig("Time Domain.jpg")
    #plt.show()

    #print(time_series)
"""

def data_sampling(length, data):
    section_data = []
    if length > (len(data) / 2):
        tk.messagebox.showerror(title= 'Oops', message = 'Your sampling length is out of range')
    else:
        if (len(data) / length) > int(len(data) / length):
            section_num = int(len(data) / length) + 1
        else:
            section_num = int(len(data) / length)
        for i in range(section_num):
            section_data.append( data[length*i:length*(i+1)] )

    return section_data

def WPT():
    path = file_path_text.get()
    if check_parameter(path):
        input_data = []
        sampling_length = int(sampling_length_str.get())
        files = os.listdir(path)

        for file in files:
            if os.path.splitext(file)[1] == '.csv':
                tem_input_data = list(csv.reader(open(path + "/" + file,'r')))
                del tem_input_data[0]
                for i in range(len(tem_input_data)):
                    tem_input_data[i].pop(0)
                tem_input_data = list(itertools.chain.from_iterable(tem_input_data))

                ## delete impulse signal
                input_data.extend(TD.DataThreshold(tem_input_data, 2))
                #input_data.extend(tem_input_data)

                    #tk.messagebox.showerror(title = 'Oops', message = path)
                #wp = 0
        TD.TimePlot(input_data, 10240)
        sec_data = data_sampling(sampling_length, input_data)
############################
## Coefficient Initialize ##
        wp_node = []
        coef=[]
        reC = []
        reS = []
        decomposition_level = int(output_decomposition_str.get())
#####################################
## Decompostion and Reconstruction ##
        for i in range(len(sec_data)):
            coef.append(np.zeros((pow(2,decomposition_level),1)))
            start_time = time.time()
            wpt = pywt.WaveletPacket(data = sec_data[i], maxlevel = decomposition_level, wavelet = 'db10', mode = 'symmetric')
            end_time = time.time()
            elapsed_time = end_time - start_time
            print(elapsed_time)
            wp_node = [node.path for node in wpt.get_level(decomposition_level,'freq')]
            index = 0
            for i in range(len(wp_node)):
                reC.append(np.zeros((1,1)))
            new_wp = pywt.WaveletPacket(None,'db10')
            for i in wp_node:
                new_wp[i] = wpt[i]
                reC[index] = new_wp.reconstruct()
                index+=1
                new_wp = pywt.WaveletPacket(None, 'db10')
            reS.append(reC)
            reC=[]
                        #reC.append(np.zeros((len(wp_node), len(new_wp.reconstruct()))))
        temp_energy = []
        energy = []
        energy_ss = []

        for i in range(len(reS)):
            for j in range(len(wp_node)):
                temp_energy.append(np.zeros((1,1)))
                temp_energy[j] = sum(map(lambda x:x*x, reS[i][j]))
            energy.append(temp_energy)
            temp_energy = []
            energy_ss.append(np.zeros((1,1)))
        for i in range(len(energy)):
            energy_ss[i] = math.sqrt(sum(map(lambda x:x*x, energy[i])))
            energy[i] = [x / energy_ss[i] for x in energy[i]]

        label = int(output_label_str.get())
        for i in range(len(energy)):
            energy[i].append(label)

#################
## File output ##
        with open(output_DOE_str.get() + '_Energy_Spectrum_' + '0' + output_decomposition_str.get() + '_' + output_label_str.get() + '.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            for i in range(len(energy)):
                writer.writerow(energy[i])
        tk.messagebox.showinfo(title='WPT', message = 'Decomposition done !!')
        enable_time_domain()

tem_input_data = []
input_data = []
decomposition_level = 0
image_plot = False

window = tk.Tk()
window.title('WPT App')
window.geometry('800x600')

menubar = tk.Menu(window)
filemenu = tk.Menu(menubar, tearoff = 0)
menubar.add_cascade(label = 'File', menu = filemenu)
filemenu.add_command(label = 'Time domain plot', command = time_domain_plot)

input_frame = tk.Frame(window)
input_frame.pack()

title = tk.Label(input_frame, text = 'Wavelet Packet Decomposition')
title.config(font=('Lolita' , 20))
title.pack()

folder_label = tk.Label(input_frame, text = 'Folder Directory')
folder_label.config(font = ('Lolita', 12))
folder_label.pack()

open_dict = tk.Button(input_frame, text= 'Open', command = open_file)
open_dict.pack()

file_path_text = tk.StringVar(window)
tk.Label(input_frame, textvariable = file_path_text).pack()

listbox_frame = tk.Frame(window, bg = "bisque")
listbox_frame.pack()

listbox = tk.Listbox(listbox_frame, width = 80)
listbox.pack(side = tk.LEFT)
scroller = tk.Scrollbar(listbox_frame)
scroller.config(command = listbox.yview)
scroller.pack(side = tk.LEFT, fill = tk.Y)
listbox.config(yscrollcommand = scroller.set)

output_frame = tk.Frame(window, width=50, height=50)
output_frame.pack(pady = 10)

output_frame_left = tk.Frame(output_frame, width=50, height=50)
output_frame_left.pack(side = tk.LEFT, fill = tk.Y)

output_frame_right = tk.Frame(output_frame, width=50, height=50)
output_frame_right.pack(side = tk.RIGHT, fill = tk.Y, padx = 10)

#output_frame_bottom = tk.Frame(output_frame, width=50, height=50, background="#b15165")
#output_frame_bottom.pack(side = tk.BOTTOM, fill = tk.X)

output_DOE_label = tk.Label(output_frame_left, text = 'DOE')
output_DOE_label.config(font = ('Lolita', 12))
output_DOE_label.pack()
output_DOE_str=tk.StringVar()
output_DOE_entry = tk.Entry(output_frame_right, textvariable = output_DOE_str).pack(pady = 3)

output_label_label = tk.Label(output_frame_left, text = 'Label')
output_label_label.config(font = ('Lolita', 12))
output_label_label.pack()
output_label_str=tk.StringVar()
output_label_entry = tk.Entry(output_frame_right, textvariable=output_label_str).pack(pady = 3)

sampling_length_label = tk.Label(output_frame_left, text = 'Sampling Length')
sampling_length_label.config(font = ('Lolita', 12))
sampling_length_label.pack()
sampling_length_str = tk.StringVar()
sampling_length_entry = tk.Entry(output_frame_right, textvariable = sampling_length_str).pack(pady = 3.5)

output_decomposition_label = tk.Label(output_frame_left, text = 'Decomposition Level')
output_decomposition_label.config(font = ('Lolita', 12))
output_decomposition_label.pack()
output_decomposition_str = tk.StringVar()
output_decomposition_entry = tk.Entry(output_frame_right, textvariable = output_decomposition_str).pack(pady = 3.5)

output_button_frame = tk.Frame(window, width=50, height=50)
output_button_frame.pack()

wpd_btn = tk.Button(output_button_frame, text='Start', command = WPT, bg='red').pack(pady = 10)

window.config(menu = menubar)
disable_time_domain()

window.mainloop()

"""
raw_data_direction = "C:/Users/allen/Desktop/Python Code/raw_data/20190927_台灣晶技_機台運轉行為/EXP001_研磨機_位置1/01_靜止"
tem_input_datas = []
input_datas = []
decomposed_level = 6
for file in os.listdir(raw_data_direction):
    tem_input_datas = list(csv.reader(open(raw_data_direction + "/" + file,'r')))
    del tem_input_datas[0]
    for i in range(len(tem_input_datas)):
        tem_input_datas[i].pop(0)
    tem_input_datas = list(itertools.chain.from_iterable(tem_input_datas))
    input_datas.append(tem_input_datas)

wp = 0
wp_node = []
############################
## Coefficient Initialize ##
coef=[]
reC = []
reS = []

for i in range(len(input_datas)):
    coef.append(np.zeros((pow(2,decomposed_level),1)))

    wpt = pywt.WaveletPacket(data = input_datas[i], maxlevel = decomposed_level, wavelet = 'db10', mode = 'symmetric')
    wp_node = [node.path for node in wpt.get_level(6,'freq')]
    index = 0
    for i in range(len(wp_node)):
        reC.append(np.zeros((1,1)))
    index = 0
    new_wp = pywt.WaveletPacket(None,'db10')
    for i in wp_node:
        new_wp[i] = wpt[i]
        reC[index] = new_wp.reconstruct()
        index+=1
        new_wp = pywt.WaveletPacket(None, 'db10')
    reS.append(reC)
    reC=[]
        #reC.append(np.zeros((len(wp_node), len(new_wp.reconstruct()))))

#print(reS[0][0])

#    for j in range(len(wp_node)):
#        coef[i][j] = wpt[wp_node[j]].data

###################
## Reconstuction ##
#for i in range(len(wp_node)):
#    coef[i]= wpt[wp_node[i]].data
#for i in range(len(wp_node)):
#    reC.append(np.zeros((1,1)))
#index = 0
#for i in wp_node:
#    new_wp[i] = wpt[i]
#    reC[index] = new_wp.reconstruct()
#    index+=1
#    new_wp = pywt.WaveletPacket(None, 'db10')

#####################
## Energy Spectrum ##
temp_energy = []
energy = []
energy_ss = []
a=[]
for i in range(64):
    a.append(np.zeros((1,1)))
    a[i]=sum(map(lambda x:x*x, reS[7][i]))
b = sum(a)

for i in range(len(reS)):
    for j in range(len(wp_node)):
        temp_energy.append(np.zeros((1,1)))
        temp_energy[j] = sum(map(lambda x:x*x, reS[i][j]))
    energy.append(temp_energy)
    temp_energy = []
    energy_ss.append(np.zeros((1,1)))
for i in range(len(energy)):
    energy_ss[i] = math.sqrt(sum(map(lambda x:x*x, energy[i])))
    energy[i] = [x / energy_ss[i] for x in energy[i]]

label = 1
for i in range(len(energy)):
    energy[i].append(label)

#A = new_wp.reconstruct(update = False)
#index=0
#for i in wp_node:
#    new_wp[i] = wpt[i]
#    reC[index] = new_wp.reconstruct(update = False)
#    index+=1
#reC = new_wp.reconstruct(update=False)

#arr, coeff_slices = pywt.coeffs_to_array((wpt[wp_node[0]].data).tolist())
#coeffs_from_arr = pywt.array_to_coeffs(arr,coeff_slices,output_format = 'wavedec')
#wpr = pywt.waverec(coeffs_from_arr, wavelet = 'db10')
"""


"""
stanCode SC101 Assignment 5 Problem 5
File: boggle.py
Name: Tiffany Lin
----------------------------------------
This file simulates the word game, boggle.
Users input letters into a 4X4 tray with the format
in which words are separated by space
and only english letters are allowed -- case insensitive.
Once all letters are entered, the game begins
and all words in sequences of adjacent letters
will be found and printed.
The number of words found will be shown in the end.
"""

# Constants
FILE = 'dictionary.txt'		  # This is the file name of the dictionary txt file

# Global Variables
dictionary = []               # A list containing all the vocabs in the dictionary
word = ''  					  # A str saving the letters in the process of forming the word
keys = []    				  # A list saving the index of letters when constructing the word
n = 0						  # An int counting the number of words found
found = []					  # A list of all words found and printed

def main():
	"""
	User input 4X4 letters and all words
	in sequences of adjacent letters
	will be found and printed.
	"""
	boggle_grid = {}
	game = ''

	# Input letters on boggle grid and save them in a list
	for i in range(4):
		r = (str(input(f"{i+1} row of letters: ")))
		r = r.lower()
		if len(r) != 7 or r[1] != ' ' or r[3] != ' ' or r[5] != ' ' or not_all_alpha(r):
			print('Illegal input')
			game = 'ended'
			break
		else:
			r = r.split()
			for j in range(4):
				letter = r.pop(0)
				boggle_grid[(i, j)] = letter

	# Once boggle grid is set, start finding words
	if game != 'ended':
		read_dictionary()
		find_words(boggle_grid)
		print(f"There are {n} words in total.")

def find_words(b):
	"""
	:param b: The dictionary saving all the letters on boggle grid
	:return: (str) Word constructed from the letters of sequentially adjacent ones
	"""
	for x in range(4):
		for y in range(4):
			first_letter = (x, y)
			loop_words(b, first_letter, b[first_letter])

def loop_words(b, current_key, current_value):
	"""
	:param b: A dictionary saving all the letters on boggle grid
	:param current_key: A list saving the index of letters used
	:param current_value: A str saving the letters when constructing words
	:return: Word constructed
	"""
	global word, n, found

	if len(word) >= 4:
		if word in dictionary:
			if word not in found:
				print(f"Found \"{word}\"")
				n += 1
				found.append(word)
				recursive_loop(b, current_key, current_value)
	else:
		recursive_loop(b, current_key, current_value)

def recursive_loop(b, current_key, current_value):
	"""
	:param b: A dictionary saving all the letters on boggle grid
	:param current_key: A list saving the index of letters used
	:param current_value: A str saving the letters when constructing words
	:return: Word constructed
	"""
	global word
	# Choose
	w = word
	word += current_value
	keys.append(current_key)

	# Explore
	if has_prefix(word):
		(x, y) = current_key
		# Upper left
		if 1 <= x <= 3 and 1 <= y <= 3:
			if (x-1, y-1) not in keys:
				loop_words(b, (x-1, y-1), b[(x-1, y-1)])
		# Above
		if 1 <= x <= 3 and 0 <= y <= 3:
			if (x-1, y) not in keys:
				loop_words(b, (x-1, y), b[(x-1, y)])
		# Upper right
		if 1 <= x <= 3 and 0 <= y <= 2:
			if (x-1, y+1) not in keys:
				loop_words(b, (x-1, y+1), b[(x-1, y+1)])
		# Left
		if 0 <= x <= 3 and 1 <= y <= 3:
			if (x, y-1) not in keys:
				loop_words(b, (x, y-1), b[(x, y-1)])
		# Right
		if 0 <= x <= 3 and 0 <= y <= 2:
			if (x, y+1) not in keys:
				loop_words(b, (x, y+1), b[(x, y+1)])
		# Lower left
		if 0 <= x <= 2 and 1 <= y <= 3:
			if (x+1, y-1) not in keys:
				loop_words(b, (x+1, y-1), b[(x+1, y-1)])
		# Below
		if 0 <= x <= 2 and 0 <= y <= 3:
			if (x+1, y) not in keys:
				loop_words(b, (x+1, y), b[(x+1, y)])
		# Lower right
		if 0 <= x <= 2 and 0 <= y <= 2:
			if (x+1, y+1) not in keys:
				loop_words(b, (x+1, y+1), b[(x+1, y+1)])

	# Un-choose
	word = w
	keys.pop()

def read_dictionary():
	"""
	This function reads file "dictionary.txt" stored in FILE
	and appends words in each line into a Python list.
	"""
	global dictionary
	with open(FILE, 'r') as f:
		for w in f:
			w = w.strip()
			dictionary.append(w)
	return dictionary

def has_prefix(sub_s):
	"""
	:param sub_s: (str) A substring that is constructed by neighboring letters on a 4x4 square grid
	:return: (bool) If there is any words with prefix stored in sub_s
	"""
	global dictionary
	t = 0
	for w in dictionary:
		if w.startswith(sub_s) is True:
			t += 1
	if t > 0:
		return True

def not_all_alpha(r):
	"""
	:param r: (str) User's input
	:return: (bool) Whether user's input are all letters and no punctuation marks
	"""
	a = 0
	for ch in [r[0], r[2], r[4], r[6]]:
		if not ch.isalpha():
			a += 1
	if a > 0:
		return True

if __name__ == '__main__':
	main()

stanCode Projects for GitHub/boggle_game_solver/boggle.py