'''
Utility functions
'''
import re
from sqlalchemy.orm import sessionmaker
from progress.bar import FillingCirclesBar
from sqlalchemy.types import DateTime, Text
from geoalchemy2.types import Geometry
import urllib
import json
import string
import warnings

from sql4housing.parsers import parse_datetime, parse_geom, parse_str
from sql4housing import ui

def get_table_name(raw_str):
    '''
    Transform a string into a suitable table name

    Swaps spaces for _s, lowercaes and strips special characters. Ex:
    'Calls to 9-1-1' becomes 'calls_to_911'
    '''
    no_spaces = raw_str.replace(' ', '_')
    return re.sub(r'\W', '', no_spaces).lower()

def insert_data(page, session, circle_bar, Binding, srid=4326, socrata=False):
    '''
    Inserts data into binding after parsing values in each row. Shows progress
    on circle bar.
    '''
    to_insert = []

    for row in page:
        to_insert.append(Binding(**parse_row(row, Binding, srid)))
        if not socrata:
            circle_bar.next()
    session.add_all(to_insert)
    if socrata:
        circle_bar.next(n=len(to_insert))

    return

def clean_string(sub_str):
    return re.compile('[%s]' % re.escape(string.punctuation)).sub(
                "_", sub_str.lower())

def geojson_data(geojson):
    '''
    Parses ['features'] within geojson data and reformats all variable names.
    '''
    ui.item(
        "Gathering data (this can take a bit for large datasets).")
    new_data = []
    data = geojson['features']
    for row in data:
        output = \
            {k.lower().replace(" ", "_"): v \
            for k, v in row['properties'].items()}
        output['geometry'] = row['geometry']
        new_data.append(output)
    return new_data

def edit_columns(df):
    '''
    Reformats columns of a dataframe.
    '''
    df.columns = \
        [col_name.lower().replace(" ", "_") for col_name in df.columns]
    return df

def parse_row(row, binding, srid):
    """Parse API data into the Python types our binding expects"""
    parsers = {
        # TO DO: move to classes
        # This maps SQLAlchemy types (key) to functions that return their
        # expected Python type from the raw Socrata data.
        DateTime: parse_datetime,
        Geometry: parse_geom,
        Text: parse_str,
    }

    parsed = {}
    binding_columns = binding.__mapper__.columns
    for col_name, col_val in row.items():
        col_name = col_name.lower()

        if col_name not in binding_columns:
            # We skipped this column when creating the binding; skip it now too
            continue

        mapper_col_type = type(binding_columns[col_name].type)
        if mapper_col_type in parsers:
            parsed[col_name] = parsers[mapper_col_type](col_val, srid)
        else:
            parsed[col_name] = col_val

    return parsed

def create_metadata(data, mappings):
    '''
    Given a dictionary of data, maps python types of each value to
    SQLAlchemy types.
    '''
    ui.item("Gathering metadata")
    print()
    metadata = []
    for col_name in data[0].keys():

        for record in data:
            if col_name == 'geometry':
                metadata.append(
                    (col_name, Geometry(geometry_type='GEOMETRY', srid=4326)))
                break
            elif record[col_name]:
                try:
                    py_type = type(record[col_name])
                    print(col_name, ":", py_type)
                    metadata.append((col_name, mappings[py_type]))
                    break
                except KeyError:
                    warnings.warn(
                        'Unable to map "%s" to a SQL type.' % col_name)
                    break
    return metadata

def spreadsheet_metadata(spreadsheet):
    '''
    Given a spreadsheet object, maps column types as interpreted by pandas into
    SQLAlchemy types.
    '''
    ui.item("Gathering metadata")
    print()
    metadata = []
    for col_name, col_type in dict(spreadsheet.df.dtypes).items():
        print(col_name, ":", col_type)
        try:
            metadata.append((col_name, spreadsheet.col_mappings[col_type]))
        except KeyError:
            warnings.warn('Unable to map "%s" to a SQL type.' % col_name)
            continue
    return metadata


"""
Eliezer Silva
Exercício 01
------------
Faça um Programa que leia um vetor de 5 números inteiros e mostre-os.

obs: Python é uma linguagem de programação de alto nível e tipagem dinâmica, sendo assim não há
necessidade de pré definir o tamanho do seu vetor, como é feito em C por exemplo (int vetor[5]={1, 2, 3, 4, 5}).
"""
import random
vetor = []
cont = 1
while cont <= 5:
    vetor.append(random.randrange(1, 99, 1))
    cont += 1
print(f'Vetor: {vetor}')

4 - Exercícios com Listas 24-24/Exercicio_001.py


from django.conf.urls import url, include
from rest_framework.urlpatterns import format_suffix_patterns

from .views import NotificationList, DayWeekAPI, UserRegisterViewSet
from accounts.views import UserView
from athletes.views import AthleteAPI, ZonePriorityAPI, TargetPriorityAPI, AthleteAllDataAPI
from trainings.views import TrainingsViewSet
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'training', TrainingsViewSet, base_name='training')
router.register(r'register-user', UserRegisterViewSet, base_name='register-user')

urlpatterns = [
    url(r'^extend_notifications/$', NotificationList.as_view(), name='notification-list'),
]

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'api'])

rest_v1_patterns = [
    url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    url(r'^user/$', UserView.as_view()),
    url(r'^user/day_week/$', DayWeekAPI.as_view()),
    url(r'^user/info/$', AthleteAPI.as_view()),
    url(r'^user/all_info/$', AthleteAllDataAPI.as_view()),
    url(r'^user/target/$', TargetPriorityAPI.as_view()),
    url(r'^user/zone/$', ZonePriorityAPI.as_view()),
    url(r'^routed/', include(router.urls)),
]

api_patterns = [
    url(r'^v1/', include(rest_v1_patterns)),
]


from dataclasses import dataclass, field
from itertools import groupby

import numpy as np
import matplotlib.patheffects as pe
import geopandas as gpd
from adjustText import adjust_text
import networkx as nx

from .grid import FvcomGrid
from .transect import Transect

text_outline = pe.withStroke(linewidth=3, foreground='white', alpha=0.6)

@dataclass(frozen=True, kw_only=True)
class ControlVolume:
    grid: FvcomGrid
    nodes: set
    calc: bool = False

    def __post_init__(self):
        if self.calc:
            object.__setattr__(self, "_elements", self._calc_elements())
            object.__setattr__(self, "_tces", self._calc_tces())

    @staticmethod
    def from_transects(transects, calc=False):
        # Check that all transects were made from the same Grid
        grids = [t.grid for t in transects]
        g = groupby(grids)
        assert next(g, True) and not next(g, False), "Grids don't match"

        grid = grids[0]

        border_nodes = [t.get_nodes() for t in transects]
        # Get the grid's node adjacency, then remove all connections
        # between upstream and downstream nodes on all transects. If
        # caclulations were done correctly, this will break up the graph
        # into separate components, one of which will be our control
        # volume
        adj_dict = grid.node_neis()
        for (upstream_nodes, downstream_nodes) in border_nodes:
            for un in upstream_nodes:
                adj_dict[un] -= downstream_nodes
            for dn in downstream_nodes:
                adj_dict[dn] -= upstream_nodes

        g = nx.Graph(adj_dict)

        # Use the Graph to find which component is our control volume,
        # based on all sections having nodes within it
        if len(transects) == 1:
            # Just pick an upstream node
            node = list(upstream_nodes)[0]
        else:
            # Start with the first transect, index 0
            # Pick a candidate node from each side of the transect
            candidates = [next(iter(bns)) for bns in border_nodes[0]]
            node = None
            # We need to test every other section to handle sections that
            # end at islands. It's then possible for mirror sides of two
            # sections to connect to each other.
            for nodes in border_nodes[1:]:
                tests = [next(iter(bns)) for bns in nodes]
                paths = np.array(
                        [[nx.has_path(g, c, t) for t in tests[:2]] for c in candidates[:2]])
                assert (not (paths[0,0] and paths[0,1])
                    and not (paths[1,0] and paths[1,1])), 'Loop detected, invalid CV'
                if not paths[0,0] and not paths[0,1]:
                    node = candidates[1]
                    break
            else:
                node = candidates[0]
        nodes = nx.node_connected_component(g, node)

        return TransectControlVolume(grid=grid, nodes=nodes,
                transects=transects, calc=calc)

    @property
    def nodes_list(self):
        """The nodes as a list"""
        nlist = list(self.nodes)
        nlist.sort()
        return nlist

    @property
    def tces(self):
        if self.calc:
            return self._tces.copy()
        else:
            return self._calc_tces()

    def _calc_tces(self):
        return self.grid.tces_gdf().loc[self.nodes_list]

    @property
    def area(self):
        """The total CV area."""
        return self.tces['geometry'].area.sum()

    @property
    def elements(self):
        """The elements property."""
        if self.calc:
            return self._elements
        else:
            return self._calc_elements()

    def _calc_elements(self):
        elements = set()
        node_list = list(self.nodes)
        for ele,nv in enumerate(self.grid.nv.T):
            if (np.isin(nv, node_list)).all():
                elements.add(ele+1)
        return elements

    @property
    def elements_list(self):
        """The elements as a list"""
        elist = list(self.elements)
        elist.sort()
        return elist

    def plot(self, data=None, label=None, base='union',
            helpers=[], **kwargs):
        cv_tces = self.tces
        if data is not None:
            cv_tces['data'] = data
            col = 'data'
            if 'legend' not in kwargs:
                kwargs['legend'] = True
        else:
            col = None
        ax = cv_tces.plot(col, zorder=2, **kwargs)
        if ax is None:
            ax = kwargs['ax']
        xmin, xmax, ymin, ymax = ax.axis()
        if base == 'elements':
            grid_base = self.grid.elements_gdf()
            grid_base.plot(ax=ax, facecolor='#ccc', edgecolors='#aaa', zorder=1)
        elif base == 'union':
            grid_els = self.grid.elements_gdf()
            grid_base = gpd.GeoDataFrame({'geometry': [grid_els['geometry'].unary_union]}, crs=grid_els.crs)
            grid_base.plot(ax=ax, facecolor='#ccc', edgecolors='k',
                    zorder=1)

        for h in helpers:
            h.reset()
            h.set_cv(self)
            h(ax)

        texts = []
        avoid_x = []
        avoid_y = []
        if label is not None:
            pt = cv_tces['geometry'].unary_union.representative_point()
            ax.annotate(label, (pt.x, pt.y), ha='center', va='center',
                    path_effects=[text_outline]
            )
            avoid_x.append(pt.x)
            avoid_y.append(pt.y)
        ax.set(ybound=(ymin, ymax), xbound=(xmin, xmax))
        for h in helpers:
            texts.extend(h.texts)
            avoid_x.extend(h.avoid_x)
            avoid_y.extend(h.avoid_y)
        if len(texts):
            adjust_text(texts, avoid_x, avoid_y)
        return ax

    def __sub__(self, ns: set):
        return ControlVolume(grid=self.grid, nodes=self.nodes - ns,
                calc=self.calc)

    def __add__(self, ns: set):
        return ControlVolume(grid=self.grid, nodes=self.nodes + ns,
                calc=self.calc)

@dataclass(frozen=True, kw_only=True)
class TransectControlVolume(ControlVolume):
    transects: list[Transect]

    def transect_directions(self):
        """List of bools; True if upstream from transect is in CV"""
        cv_node = next(iter(self.nodes))
        directions = []
        for t in self.transects:
            up, down = t.get_nodes()
            directions.append(next(iter(up)) in self.nodes)
        return directions

    def __sub__(self, ns: set):
        return TransectControlVolume(grid=self.grid,
                nodes=self.nodes - ns, transects=self.transects,
                calc=self.calc)

    def __add__(self, ns: set):
        return TransectControlVolume(grid=self.grid,
                nodes=self.nodes + ns, transects=self.transects,
                calc=self.calc)

    def plot(self, transect_labels=None, helpers=[], **kwargs):
        if transect_labels is not None:
            helpers.append(TransectHelper(self.cv.transects, transect_labels))
        return super().plot(helpers=helpers, **kwargs)

class PlotHelper:
    cv = None
    avoid_x = []
    avoid_y = []
    texts = []

    def set_cv(self, cv):
        self.cv = cv

    def reset(self):
        self.avoid_x = []
        self.avoid_y = []
        self.texts = []

    def __call__(self, ax):
        """Perform any additional plotting before axis bounds are adjusted"""
        raise NotImplementedError("Subclass must override")

class TransectHelper(PlotHelper):
    def __init__(self, transects, transect_labels):
        self.transects = transects
        self.transect_labels = transect_labels

    def __call__(self, ax):
        for t,label in zip(self.transects, self.transect_labels):
            ls = t.to_geom()
            gs = gpd.GeoSeries(ls)
            gs.plot(ax=ax, color='k', zorder=3)
            self.texts.append(ax.annotate(label,
                xy=(ls.centroid.x, ls.centroid.y), ha='center',
                va='center', zorder=4,
                path_effects=[text_outline]
            ))
            self.avoid_x.extend(ls.coords.xy[0])
            self.avoid_y.extend(ls.coords.xy[1])

class StationHelper(PlotHelper):
    sites_df = None

    def __init__(self, sites: dict):
        self.sites = sites

    def __call__(self, ax):
        if self.sites_df is None:
            self.sites_df = self.cv.grid.nodes_gdf().loc[
                    self.sites.keys()].copy()
            self.sites_df['name'] = ''
            for n,s in self.sites.items():
                self.sites_df.loc[n, 'name'] = s
        self.sites_df.plot(ax=ax, color='k', marker='^', alpha=0.6, zorder=3)
        for n,row in self.sites_df.iterrows():
            self.texts.append(ax.annotate(row['name'],
                xy=(row['geometry'].x, row['geometry'].y), ha='center',
                va='center', zorder=4, fontstyle='italic',
                path_effects=[text_outline]
            ))
            self.avoid_x.append(row['geometry'].x)
            self.avoid_y.append(row['geometry'].y)


from rest_framework import serializers
from . import models

class RestaurantSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Restaurant
        fields = ('pk', 'name', 'link')

class ResistanceSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Resistance
        fields = ('pk', 'resistance', 'restaurant', 'created')

class TotalResistanceSerializer(serializers.ModelSerializer):
    total_resistance = serializers.SerializerMethodField()

    def get_total_resistance(self, obj):
        return obj.get_total_resistance_today()

    class Meta:
        model = models.Restaurant
        fields = ('pk', 'name', 'total_resistance')


from facebook_scraper.facebook_scraper import FacebookScraper
import re
import requests
from bs4 import BeautifulSoup
import datetime
from datetime import date
from datetime import timedelta
from time import sleep
import pymongo
import json
import os
from flask import Flask
from flask import Flask, request
from flask.json import jsonify
from threading import Thread

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

with open(ROOT_DIR+'/config_db.json') as json_file:
    config = json.loads(json_file.read())
# config = json.load(config)

mongoclient = pymongo.MongoClient("mongodb://{}:{}/".format(config["host"], config["port"]))
db = mongoclient[config["db"]]

class FbCrawl(Thread):
    def run(self):
        run_crawler()

def get_list_group():
    mycol = db["facebook_groups"]
    groups = []
    for x in mycol.find({}, {"plafform": "facebook"}):
        groups.append(x["_id"])
    print(groups)
    return groups
#
def insert_post_one(post):
    post_dict = post
    # print(post_dict["post_id"])
    post_dict.update({"_id":post_dict["post_id"]})
    mycol = db["facebook_posts"]
    try:
        x = mycol.insert_one(post)
        print(x.inserted_id)
    except Exception as e:
        # print('Mongo Err',e)
        pass

def parse(g_id, p_id):
    # url = 'http://m.facebook.com/groups/1000043713742145/permalink/1030776880668828'
    url = 'http://m.facebook.com/groups/{}/permalink/{}'.format(g_id, p_id)
    web_url = 'https://facebook.com/groups/{}/permalink/{}'.format(g_id, p_id)
    data = {}
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    raw = soup.find(id='u_0_4')
    # print(raw)
    story_raw = soup.find(id='m_story_permalink_view')

    # print(story_raw)
    if 'original_content_id' in str(raw):
        try:

            # print('share')
            time = re.findall("publish_time\"\:(.*?)\,\"story_name",str(raw))
            # print(time)
            if len(time) > 1:
                timestamp = time[1]
            else:
                timestamp = time[0]
            # print(timestamp)
            dt = datetime.datetime.fromtimestamp(int(timestamp))
            pubdate = dt.strftime("%Y-%m-%d %H:%M:%S")
            post_date = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
            # print(post_date)
            datetime_object = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')

            date_str = dt.strftime("%Y-%m-%d")
            # print(type(date_str))
            # until_obj = datetime.datetime.strptime(until, "%Y-%m-%d")
            # date_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d")
            # # print(date_obj)
            #
            # d0 = date(date_obj.year, date_obj.month, date_obj.day)
            # d1 = date(until_obj.year, until_obj.month, until_obj.day)
            #
            # #
            # delta = d0 - d1
            # print(delta)
            # #
            # if delta.days >=0:
            #
            #
            ids = re.findall('actor_id\"\:\"(.*?)\"\,\"', str(raw))
            # print(ids)
            if ids is None or ids==[]:
                ids = re.findall('actor_id\"\:(.*?)\,\"dm', str(raw))

            if len(ids) > 1:
                user_id = ids[0]
            else:
                user_id = ids[0]
            usr_name = re.findall('bt"><a hre(.*?)<\/strong>',str(raw))
            fname = re.findall('tn__=C-R">(.*?)<\/a>',''.join(user_raw))
            # # # print(fname)
            fullname = ''.join(fname)
            username = usr_name[0]
            # # # print(user_id)

            data["post_date"] = post_date
            data["pubdate"] = datetime_object
            data["user_id"] = user_id
            data["username"] = username
            data["fullname"] = fullname
            data["post_url"] = web_url
            # if (bool(BeautifulSoup(data["pubdate"] , "html.parser").find())==True) or(bool(BeautifulSoup(data["user_id"], "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["username"] , "html.parser").find())==True) or (bool(BeautifulSoup(data["fullname"] , "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["post_url"] , "html.parser").find())==True):
            #     return None
            # print(data)
            return data
            # if post_date < last_hour:
            #     return data
            # else:
            #     print(post_date)
        except Exception as e:
            print('Err 1', e)
            # print(str(raw))
            return None

    elif 'originalPostOwnerID' in str(raw):
        try:
            # print('ori')
            # print(raw)
            time = re.findall("publish_time\"\:(.*?)\,\"story_name", str(raw))
            # print(time)
            if len(time) > 1:
                timestamp = time[1]
            else:
                timestamp = time[0]
            # print(timestamp)
            dt = datetime.datetime.fromtimestamp(int(timestamp))
            pubdate = dt.strftime("%Y-%m-%d %H:%M:%S")
            # post_date = datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
            post_date = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
            datetime_object = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')

            date_str = dt.strftime("%Y-%m-%d")
            # print(type(date_str))
            # until_obj = datetime.datetime.strptime(until, "%Y-%m-%d")
            # date_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d")
            # # print(until_obj)
            # # print(date_obj)
            #
            # d0 = date(date_obj.year, date_obj.month, date_obj.day)
            # d1 = date(until_obj.year, until_obj.month, until_obj.day)
            # print(d0)
            # print(d1)
            # #
            # delta = d0 - d1
            # # print(delta)
            # #
            # if delta.days >= 0:
            # print('lanjut')
            # print(raw)
            # print(url)
            ids = re.findall('actor_id\"\:\"(.*?)\"\,\"', str(raw))
            if ids is None or ids==[]:
                ids = re.findall('actor_id\"\:(.*?)\,\"dm', str(raw))
            if len(ids) >0:
                user_id = ids[0]
            else:
                user_id = ids[0]
            usr_name = re.findall('bt"> 0:
                username = usr_name[0]
            else:
                username = None
            # user_raw = re.findall('bt"><a hre(.*?)<\/strong>', str(raw))
            fname = re.findall('(.*?)<\/a><\/strong><span class="bv"',str(raw))
            # # # print(fname)
            fullname = ''.join(fname)
            # # # print(user_id)
            data["post_date"] = post_date
            data["pubdate"] = datetime_object
            data["user_id"] = user_id
            data["username"] = username
            data["fullname"] = fullname
            data["post_url"] = web_url
            # print(raw)
            # if (bool(BeautifulSoup(data["pubdate"] , "html.parser").find())==True) or(bool(BeautifulSoup(data["user_id"], "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["username"] , "html.parser").find())==True) or (bool(BeautifulSoup(data["fullname"] , "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["post_url"] , "html.parser").find())==True):
            #     return None
            return data
            # if post_date < last_hour:
            #     return data
            # else:
            #     print(post_date)
        except Exception as e:
            print('Err 2', e)
            # print(str(raw))
            return None
    else:
        # print(story_raw)
        # print(url)
        # print("else")
        try:
            time = re.findall("publish_time\"\:(.*?)\,\"story_name", str(story_raw))
            # print(time)
            if len(time) > 1:
                timestamp = time[1]
            else:
                timestamp = time[0]
            # print(timestamp)
            dt = datetime.datetime.fromtimestamp(int(timestamp))
            pubdate = dt.strftime("%Y-%m-%d %H:%M:%S")
            # post_date = datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
            post_date = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
            datetime_object = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')

            ids = re.findall('actor_id\"\:\"(.*?)\"\,\"', str(story_raw))
            if ids is None or ids==[]:
                ids = re.findall('actor_id\"\:(.*?)\,\"dm', str(story_raw))

            if len(ids) > 0:
                user_id = ids[0]
            else:
                user_id = ids[0]

            usr_name = re.findall('bt"> 0:
                username = usr_name[0]
            else:
                username = None

            # user_raw = re.findall('bt"><a hre(.*?)<\/strong>', str(raw))
            fname = re.findall('(.*?)<\/a><\/strong><span class="bv"', str(story_raw))
            fullname = ''.join(fname)
            data["post_date"] = post_date
            data["pubdate"] = datetime_object
            data["user_id"] = user_id
            data["username"] = username
            data["fullname"] = fullname
            data["post_url"] = web_url
            # if (bool(BeautifulSoup(data["pubdate"] , "html.parser").find())==True) or(bool(BeautifulSoup(data["user_id"], "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["username"] , "html.parser").find())==True) or (bool(BeautifulSoup(data["fullname"] , "html.parser").find())==True)\
            #    (bool(BeautifulSoup(data["post_url"] , "html.parser").find())==True):
            #     return None
            return data
            # if post_date < last_hour:
            #     return data
            # else:
            #     print(post_date)

        except Exception as e:
            print('Err 3', e)
            # print(story_raw)
            # print(e)
            return None

def run_crawler():
    fb = FacebookScraper()
    list_group = get_list_group()
    last_hour_datetime = datetime.datetime.now() - timedelta(hours=6)
    last_hour_datetime = last_hour_datetime.strftime('%Y-%m-%d %H:%M:%S')
    last_hour = datetime.datetime.strptime(last_hour_datetime, '%Y-%m-%d %H:%M:%S')
    print(last_hour)
    n = len(list_group)
    i = 0
    while i <= n:
        endDate = 0
        if i == n:
            print('close bos')
            break
        for post in fb.get_group_posts(group=list_group[i] ,page_limit=100):
            # print(post)
            if (post["post_id"] is not None) and (post["text"] is not None) and (post["text"] !=''):
                # print(post["post_id"])
                detail = parse(list_group[i], post["post_id"])
                if detail is not None and detail["post_date"] >= last_hour:
                    del detail["post_date"]
                    del post['time']
                    del post['shared_text']
                    del post['post_text']
                    # print(post["text"])
                    # post.update({"text":str(post["text"].encode())})
                    post.update({"group_id":list_group[i]})
                    post.update({"type": "post"})
                    post.update({"platform": "facebook"})
                    post.update(detail)
                    # print(str(post["text"]))
                    # print(type(post["text"]))
                    # print(post["text"].decode('ascii'))
                    # a = post["text"].encode('utf-8')
                    # print(a)

                    if (post["username"] is not None and '</' not in post["username"]) or (post["username"] is not None and '<a href=' in post["username"])\
                        or (post["fullname"] is not None and '</' not in post["fullname"]) or (post["fullname"] is not None and'<a href=' in post["fullname"])\
                        or (post["user_id"] is not None and '</' not in post["user_id"]) or (post["user_id"] is not None and '= 10:
                        i += 1
                        break
                    print(detail)

@app.route("/fb/group", methods=['GET'])
def fb_group():
    thread_fb = FbCrawl()
    thread_fb.start()
    resp = {'running': True}
    return jsonify(resp)
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8082, debug=False, threaded=True)


import matplotlib.pyplot as plt
import numpy as np
import csv
from loaddata import *
import matplotlib.cm as cm
from matplotlib import colors

WIDTH_NUM = 20
HEIGHT_NUM = 20

TEST_NUM = 1000
TEST_TARGET = list(range(5010, 5030))
NUM_SIDES = list(range(3, 6))
BUFFER_OPT = "buffer_001"
RESULT_FILE = "problem2/model3/01_" + BUFFER_OPT + ".txt"
RESULT_FILE_PATH = "../result/" + RESULT_FILE

WHOLE_RANGE = [-5.0, 5., -5., 5.]

test_x_data = []
test_y_data = []

tf.reset_default_graph()

for numSides in NUM_SIDES:
    print(str(numSides) + " =========== LOAD DATA ===========")
    _test_x_data, _test_y_data = load_raster_data("../data/problem2/raster_pc/" + BUFFER_OPT + "/test_" + str(numSides) + ".csv")
    test_x_data.extend(_test_x_data)
    test_y_data.extend(_test_y_data)

# result file
print("Accuraccy : " + open(RESULT_FILE_PATH, 'r').readline())
result_data = np.loadtxt(RESULT_FILE_PATH, dtype=np.float32, skiprows=1)
# result_data = result_data.reshape(result_data.shape[0], 1)

# polygon
for test_i in TEST_TARGET:
    coords_x = []
    coords_y = []

    # create discrete colormap
    cmap = colors.ListedColormap(['red', 'blue'])
    bounds = [0, 10, 20]
    norm = colors.BoundaryNorm(bounds, cmap.N)

    fig, ax = plt.subplots()
    ax.imshow(data, cmap=cmap, norm=norm)

    # draw gridlines
    ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
    ax.set_xticks(np.arange(WHOLE_RANGE[0], WHOLE_RANGE[1], WIDTH_NUM));
    ax.set_yticks(np.arange(WHOLE_RANGE[2], WHOLE_RANGE[3], HEIGHT_NUM));

    plt.show()


import docker
import pytest

from .. import helpers
from ..base import requires_api_version

@requires_api_version('1.21')
class TestVolumes(helpers.BaseTestCase):
    def test_create_volume(self):
        name = 'perfectcherryblossom'
        self.tmp_volumes.append(name)
        result = self.client.create_volume(name)
        self.assertIn('Name', result)
        self.assertEqual(result['Name'], name)
        self.assertIn('Driver', result)
        self.assertEqual(result['Driver'], 'local')

    def test_create_volume_invalid_driver(self):
        driver_name = 'invalid.driver'

        with pytest.raises(docker.errors.NotFound):
            self.client.create_volume('perfectcherryblossom', driver_name)

    def test_list_volumes(self):
        name = 'imperishablenight'
        self.tmp_volumes.append(name)
        volume_info = self.client.create_volume(name)
        result = self.client.volumes()
        self.assertIn('Volumes', result)
        volumes = result['Volumes']
        self.assertIn(volume_info, volumes)

    def test_inspect_volume(self):
        name = 'embodimentofscarletdevil'
        self.tmp_volumes.append(name)
        volume_info = self.client.create_volume(name)
        result = self.client.inspect_volume(name)
        self.assertEqual(volume_info, result)

    def test_inspect_nonexistent_volume(self):
        name = 'embodimentofscarletdevil'
        with pytest.raises(docker.errors.NotFound):
            self.client.inspect_volume(name)

    def test_remove_volume(self):
        name = 'shootthebullet'
        self.tmp_volumes.append(name)
        self.client.create_volume(name)
        result = self.client.remove_volume(name)
        self.assertTrue(result)

    def test_remove_nonexistent_volume(self):
        name = 'shootthebullet'
        with pytest.raises(docker.errors.NotFound):
            self.client.remove_volume(name)


import pandas as pd
import math
import numpy as np

def dtw_distance(ts_a, ts_b, d=lambda x, y: abs(x - y), mww=10000):
    """Computes dtw distance between two time series
    Args:
        ts_a: time series a
        ts_b: time series b
        d: distance function
        mww: max warping window, int, optional (default = infinity)

    Returns:
        dtw distance
    """
    # Create cost matrix via broadcasting with large int
    ts_a, ts_b = np.array(ts_a), np.array(ts_b)
    M, N = len(ts_a), len(ts_b)
    cost = np.ones((M, N))

    # Initialize the first row and column
    cost[0, 0] = d(ts_a[0], ts_b[0])
    for i in range(1, M):
        cost[i, 0] = cost[i - 1, 0] + d(ts_a[i], ts_b[0])

    for j in range(1, N):
        cost[0, j] = cost[0, j - 1] + d(ts_a[0], ts_b[j])

    # Populate rest of cost matrix within window
    for i in range(1, M):
        for j in range(max(1, i - mww), min(N, i + mww)):
            choices = cost[i - 1, j - 1], cost[i, j - 1], cost[i - 1, j]
            cost[i, j] = min(choices) + d(ts_a[i], ts_b[j])

    # Return DTW distance given window
    return cost[-1, -1]

x1 = range(1000)
yx1 = [math.sin(x) for x in x1]
yx2 = [math.cos(x) for x in x1]

t1 = range(800)
yt1 = [math.sin(t) for t in t1]
yt2 = [math.cos(t) for t in t1]
print(yx1)
print('绝对距离的输出：{},{}'.format(dtw_distance(yx1,yx2), dtw_distance(yt1,yt2)))
print('相对距离的输出：{},{}'.format(dtw_distance(yx1,yx2)/(1000), dtw_distance(yt1,yt2)/800))


# coding=utf-8

import web

from apps.api.console import app as console_app

import settings

urls = (
    # ==== 管理接口 ====
    '/console', console_app,

)

app = web.application(urls, locals())

def loadhook():
    # 存在于允许列表的才可以跨域,否则禁止跨域
    request_domain = web.ctx.env.get('HTTP_ORIGIN')
    if request_domain in settings.CORS_DOMAINS:
        web.header('Access-Control-Allow-Origin', request_domain)
        # 客户端也需要配置 withCredentials 需用到cookie 且增强安全性
        # 注意, 使用认证方式 Access-Control-Allow-Origin 不能为*
        web.header("Access-Control-Allow-Credentials", "true")
        # authorized_key 为验证header必须加入才能跨域,如有其它自定义header也需要加入
        web.header("Access-Control-Allow-Headers",
                   "x-requested-with, content-type, accept, authorized-key, content-type, origin, client")
        # 告诉请求对象验证有效时长，在接下来的1728000秒（20天）不用再发送OPTIONS请求验证合法性。
        web.header('Access-Control-Max-Age', '1728000')

app.add_processor(web.loadhook(loadhook))

class ApiViewBase(object):
    "API视图基类"
    def OPTIONS(self, *kargs, **kwargs):
        return ''


import numpy as np
import cPickle

def train_test_split(posts, train_ratio, valid_ratio):
    """
    split reaction data into train, valid and test by ratio
    :param posts: {doc_id: [uids, eids]}
    :param train_ratio: percentage of training
    :param valid_ratio: percentage of validation
    :return: posts_train, posts_valid, posts_test
    """
    test_ratio = 1.0 - train_ratio - valid_ratio
    assert test_ratio > 0
    posts_train, posts_valid, posts_test = {}, {}, {}
    for doc_id in posts:
        uids, eids = posts[doc_id]
        uids = np.array(uids, dtype=np.int64)
        eids = np.array(eids, dtype=np.int64)

        Md = uids.shape[0]
        Md_train = max(1, int(Md * train_ratio))
        Md_valid = (Md - Md_train) * valid_ratio / (valid_ratio + test_ratio)
        Md_test = Md - Md_train - Md_valid

        inds = np.arange(Md)
        np.random.shuffle(inds)
        inds_train = inds[:Md_train]
        inds_valid = inds[Md_train: Md_train + Md_valid]
        inds_test = inds[Md_train + Md_valid: Md]

        uids_train, eids_train = uids[inds_train], eids[inds_train]
        uids_valid, eids_valid = uids[inds_valid], eids[inds_valid]
        uids_test, eids_test = uids[inds_test], eids[inds_test]

        ### test ###
        print("doc_id: %d, # train: %d, # valid: %d, # test: %d" % (doc_id, uids_train.shape[0], uids_valid.shape[0], uids_test.shape[0]))

        posts_train[doc_id] = [uids_train, eids_train]
        posts_valid[doc_id] = [uids_valid, eids_valid]
        posts_test[doc_id] = [uids_test, eids_test]
    return posts_train, posts_valid, posts_test

if __name__ == "__main__":
    posts = cPickle.load(open("data/test/reactions_full", "rb"))
    print(train_test_split(posts, 0.7, 0.1))


from django.conf import settings
from django.db import models
from django.db.models import Q, Subquery, OuterRef
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from lib.convert import *
from lib.functions import *
from lib.data_helper import DataHelper
from rent_db.models import *
from .search_garage_conditions import SearchGarageConditions
from .searched_garage_detail import SearchedGarageDetail
from .searched_garage_picture import SearchedGaragePicture

class SearchedGarage(models.Model):
    """駐車場リスト（月極駐車場の建物リスト）"""
    id = models.AutoField(_('id'), db_column='id', primary_key=True)
    oid = models.CharField(_('oid'), db_column='oid', db_index=True, unique=True, max_length=50)
    building_code = models.CharField(_('building_code'), db_column='building_code', max_length=20, db_index=True, null=True, blank=True)
    building_name = models.CharField(_('building_name'), db_column='building_name', max_length=100, db_index=True, null=True, blank=True)
    building_kana = models.CharField(_('building_kana'), db_column='building_kana', max_length=100, db_index=True, null=True, blank=True)
    building_old_name = models.CharField(_('building_old_name'), db_column='building_old_name', max_length=100, null=True, blank=True)

    postal_code = models.CharField(_('postal_code'), db_column='postal_code', max_length=10, null=True, blank=True)
    pref = models.ForeignKey(
        Pref,
        db_column='pref_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_trading_area=True) | Q(pk=0),
    )
    city = models.ForeignKey(
        City,
        db_column='city_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_trading_area=True, is_stopped=False) | Q(pk=0),
    )
    town_address = models.CharField(_('town_address'), db_column='town_address', max_length=255, null=True, blank=True)
    town_name = models.CharField(_('town_name'), db_column='town_name', max_length=100, null=True, blank=True)
    house_no = models.CharField(_('house_no'), db_column='house_no', max_length=100, null=True, blank=True)
    building_no = models.CharField(_('building_no'), db_column='building_no', max_length=100, null=True, blank=True)
    lat = models.FloatField(_('lat'), db_column='lat', db_index=True, default=0)
    lng = models.FloatField(_('lng'), db_column='lng', db_index=True, default=0)
    area = models.ForeignKey(
        Area,
        db_column='area_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_stopped=False) | Q(pk=0),
    )
    department = models.ForeignKey(
        Department,
        db_column='department_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_stopped=False, is_deleted=False) | Q(pk=0),
    )
    priority_level = models.IntegerField(_('priority_level'), db_column='priority_level', db_index=True, default=50)
    agency_department = models.ForeignKey(
        Department,
        db_column='agency_department_id',
        related_name='agency_searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_stopped=False, is_deleted=False) | Q(pk=0),
    )
    arrival_type1 = models.ForeignKey(
        ArrivalType,
        db_column='arrival_type_id1',
        related_name='searched_garages1',
        on_delete=models.PROTECT,
        default=0,
    )
    station1 = models.ForeignKey(
        Station,
        db_column='station_id1',
        related_name='searched_garages1',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_trading=True, is_stopped=False) | Q(pk=0),
    )
    station_time1 = models.IntegerField(_('station_time1'), db_column='station_time1', default=0)
    bus_stop1 = models.CharField(_('bus_stop1'), db_column='bus_stop1', max_length=50, null=True, blank=True)
    bus_stop_time1 = models.IntegerField(_('bus_stop_time1'), db_column='bus_stop_time1', default=0)

    arrival_type2 = models.ForeignKey(
        ArrivalType,
        db_column='arrival_type_id2',
        related_name='searched_garages2',
        on_delete=models.PROTECT,
        default=0,
    )
    station2 = models.ForeignKey(
        Station,
        db_column='station_id2',
        related_name='searched_garages2',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_trading=True, is_stopped=False) | Q(pk=0),
    )
    station_time2 = models.IntegerField(_('station_time2'), db_column='station_time2', default=0)
    bus_stop2 = models.CharField(_('bus_stop2'), db_column='bus_stop2', max_length=50, null=True, blank=True)
    bus_stop_time2 = models.IntegerField(_('bus_stop_time2'), db_column='bus_stop_time2', default=0)

    arrival_type3 = models.ForeignKey(
        ArrivalType,
        db_column='arrival_type_id3',
        related_name='searched_garages3',
        on_delete=models.PROTECT,
        default=0,
    )
    station3 = models.ForeignKey(
        Station,
        db_column='station_id3',
        related_name='searched_garages3',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
        limit_choices_to=Q(is_trading=True, is_stopped=False) | Q(pk=0),
    )
    station_time3 = models.IntegerField(_('station_time3'), db_column='station_time3', default=0)
    bus_stop3 = models.CharField(_('bus_stop3'), db_column='bus_stop3', max_length=50, null=True, blank=True)
    bus_stop_time3 = models.IntegerField(_('bus_stop_time3'), db_column='bus_stop_time3', default=0)

    building_type = models.ForeignKey(
        BuildingType,
        db_column='building_type_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
    )
    garage_status = models.ForeignKey(
        GarageStatus,
        db_column='garage_status_id',
        related_name='searched_garages',
        db_index=True,
        on_delete=models.PROTECT,
        default=0,
    )
    web_catch_copy = models.CharField(_('web_catch_copy'), db_column='web_catch_copy', max_length=100, null=True, blank=True)
    web_appeal = models.CharField(_('web_appeal'), db_column='web_appeal', max_length=255, null=True, blank=True)

    class Meta:
        managed = False

    """
    検索関連
    """
    @classmethod
    def get_garages(cls, conditions: SearchGarageConditions):
        """クエリセットの取得"""
        params = {}

        sql = 'SELECT {0} FROM {1} WHERE {2} ORDER BY {3};'.format(
            cls.__get_sql_columns(),
            cls.__get_sql_tables(conditions=conditions, params=params),
            cls.__get_sql_conditions(conditions=conditions, params=params),
            cls.__get_sql_orders(),
        )

        ans = cls.objects.raw(raw_query=sql, params=params)

        return ans

    """
    SQL関連内部メソッド
    """
    @classmethod
    def __get_sql_columns(cls):
        """SQLのカラム一覧"""
        ans = 'building.id'
        ans += ', building.oid'
        ans += ', building.building_code'
        ans += ', building.building_name'
        ans += ', building.building_kana'
        ans += ', building.building_old_name'
        ans += ', building.postal_code'
        ans += ', building.pref_id'
        ans += ', building.city_id'
        ans += ', building.town_address'
        ans += ', building.town_name'
        ans += ', building.house_no'
        ans += ', building.building_no'
        ans += ', building.lat'
        ans += ', building.lng'
        ans += ', building.area_id'
        ans += ', building.department_id'
        ans += ', building.agency_department_id'
        ans += ', building.arrival_type_id1'
        ans += ', building.station_id1'
        ans += ', building.station_time1'
        ans += ', building.bus_stop1'
        ans += ', building.bus_stop_time1'
        ans += ', building.arrival_type_id2'
        ans += ', building.station_id2'
        ans += ', building.station_time2'
        ans += ', building.bus_stop2'
        ans += ', building.bus_stop_time2'
        ans += ', building.arrival_type_id3'
        ans += ', building.station_id3'
        ans += ', building.station_time3'
        ans += ', building.bus_stop3'
        ans += ', building.bus_stop_time3'
        ans += ', building.building_type_id'
        ans += ', building.garage_status_id'
        ans += ', building.web_catch_copy'
        ans += ', building.web_appeal'

        return ans

    @classmethod
    def __get_sql_tables(cls, conditions: SearchGarageConditions, params):
        """SQLのテーブル一覧"""
        ans = 'building'
        ans += ' INNER JOIN pref ON building.pref_id = pref.id'
        ans += ' INNER JOIN city ON building.city_id = city.id'

        if conditions:
            # 管理種別の限定有無
            ans += xstr(conditions.get_no_limit_sql())

            # ランドマーク
            ans += xstr(conditions.get_landmarks_sql(params=params))

        return ans

    @classmethod
    def __get_sql_conditions(cls, conditions: SearchGarageConditions, params):
        """SQLの建物用条件一覧"""
        ans = 'building.is_deleted = FALSE'
        ans += ' AND building.is_hidden_web = FALSE'
        ans += ' AND building.garage_status_id IN (1, 3, 4)'        # 空き有・要確認・別参照
        ans += ' AND building.building_type_id IN ('
        building_types = (310, )        # 駐車場に該当する建物種別IDのリスト
        counter = 1
        for item in building_types:
            param_name = '{0}_{1}'.format('building_type', counter)
            if counter > 1:
                ans += ','
            ans += '%({0})s'.format(param_name)
            params[param_name] = item
            counter += 1
        ans += ')'

        if conditions:
            ans += cls.__and_condition_sql(conditions.get_stations_sql(params=params), ans)
            ans += cls.__and_condition_sql(conditions.get_cities_sql(params=params), ans)
            ans += cls.__and_condition_sql(conditions.get_areas_sql(params=params), ans)
            ans += cls.__and_condition_sql(conditions.get_latlng_sql(params=params), ans)

        return ans

    @classmethod
    def __and_condition_sql(cls, condition_sql, sql):
        """SQL文字列に条件SQLをAND条件で追加"""
        ans = ''
        if xstr(condition_sql):
            if xstr(sql) != '':
                ans += " AND "
            ans += xstr(condition_sql)
        return ans

    @classmethod
    def __get_sql_orders(cls):
        """SQLの並び順一覧"""
        ans = 'building.priority_level DESC'
        ans += ', pref.priority, city.priority, building.building_kana'
        return ans

    """
    プロパティ
    """
    @property
    def details(self):
        return SearchedGarageDetail.get_garage_details(building_id=self.id)

    @property
    def garage_picture(self):
        return SearchedGaragePicture.get_picture(self.id)

    """
    表示用プロパティ
    """
    @property
    def address(self):
        return DataHelper.get_address_text(
            self.pref,
            self.city,
            self.town_address,
            self.house_no,
            self.building_no)

    @property
    def area_text(self):
        return DataHelper.get_area_text(self.area)

    @property
    def nearest_station1(self):
        return DataHelper.get_nearest_station_text(
            self.arrival_type1,
            self.station1,
            self.station_time1,
            self.bus_stop1,
            self.bus_stop_time1)

    @property
    def nearest_station2(self):
        return DataHelper.get_nearest_station_text(
            self.arrival_type2,
            self.station2,
            self.station_time2,
            self.bus_stop2,
            self.bus_stop_time2)

    @property
    def nearest_station3(self):
        return DataHelper.get_nearest_station_text(
            self.arrival_type3,
            self.station3,
            self.station_time3,
            self.bus_stop3,
            self.bus_stop_time3)

    @property
    def building_type_text(self):
        return DataHelper.get_building_type_text(
            self.building_type)

    @property
    def garage_status_text(self):
        return DataHelper.get_garage_status_text(self.garage_status)


from tkinter import *
import cv2
import os
from PIL import Image, ImageTk
from tkinter import ttk

class APP:
    def __init__(self):
        self.camera = None  # 摄像头
        self.root = Tk()
        self.root.title('FACE')
        self.root.geometry('%dx%d' % (1600, 600))
        self.createFirstPage()
        mainloop()

    def createFirstPage(self):
        self.page1 = Frame(self.root)
        self.page1.pack()
        Label(self.page1, text='欢迎使用人脸识别系统', font=('粗体', 20)).pack()
        image = Image.open("/home/shike/A_graduationProject/introralArea_seg_project2/IntraoralArea_seg_project/IntraoralArea_project/mouth.png")  # 随便使用一张图片 不要太大
        photo = ImageTk.PhotoImage(image=image)
        self.data1 = Label(self.page1, width=780, image=photo)
        self.data1.image = photo
        self.data1.pack(padx=5, pady=5)

        self.button12 = Button(self.page1, width=18, height=2, text="录入新的人脸", bg='green', font=("宋", 12),
                               relief='raise', command=self.createSecondPage)
        self.button12.pack(side=LEFT, padx=25, pady=10)

    def createSecondPage(self):
        self.camera = cv2.VideoCapture(0)
        self.page1.pack_forget()
        self.page2 = Frame(self.root)
        self.page2.pack()
        Label(self.page2, text='欢迎使用人脸识别系统', font=('粗体', 20)).pack()
        self.data2 = Label(self.page2)
        self.data2.pack(padx=5, pady=5)

        self.button21 = Button(self.page2, width=18, height=2, text="返回", bg='gray', font=("宋", 12),
                               relief='raise', command=self.backFirst)
        self.button21.pack(padx=25, pady=10)

        self.video_loop(self.data2)

    def video_loop(self, panela):
        success, img = self.camera.read()  # 从摄像头读取照片
        if success:
            cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)  # 转换颜色从BGR到RGBA
            current_image = Image.fromarray(cv2image)  # 将图像转换成Image对象
            imgtk = ImageTk.PhotoImage(image=current_image)
            panela.imgtk = imgtk
            panela.config(image=imgtk)
            self.root.after(1, lambda: self.video_loop(panela))

    def backFirst(self):
        self.page2.pack_forget()
        self.page1.pack()
        # 释放摄像头资源
        self.camera.release()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    demo = APP()


# Load Our Packages
import spacy
import openpyxl
from termcolor import colored
from collections import Counter
# Load the Excel sheet
wb = openpyxl.load_workbook("b2bPlatform_Final.xlsx")
# Select the sheet you want to work with
sheet = wb['Companies List']
nlp = spacy.load("en_core_web_sm")

def mostCommonWords(cell):
    col = 10
    cell_value = sheet.cell(cell,col).value
    text = cell_value
    doc = nlp(text)
# Get the words in the text
    words = [token.text for token in doc]
# Remove stop words (e.g. "is", "the", "a")
    stop_words = spacy.lang.en.stop_words.STOP_WORDS
    stop_words |= {".",",",'"',"-","`",'™','&','_', '\n','(', ')',';','%','\n\n','/','x000D',', ',"2014",'x000D',' ' }
    filtered_words = [word for word in words if word.lower() not in stop_words]
# Count the occurrences of each word
    word_counts = Counter(filtered_words)
# Sort the word_counts dictionary by count
    sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
# Get the top N most common words
    N = 5
    top_words = [x[0] for x in sorted_word_counts[:N]]

    print(colored(top_words,"green"))

for i in range(2,166):
     print(colored("\033[1mCompany Name: \033[0m","light_cyan"),end='')
     print(colored(sheet.cell(i,3).value,"magenta"),end='')
     print(colored("\033[1m    Top N Words:  \033[0m","light_cyan"),end='')
     mostCommonWords(i)


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

# tkinter example for VLC Python bindings
# This is an extension of an app created by Patrick Fay in 23-09-2015

import os
import time
import vlc
import pathlib
import platform
import customtkinter as Ctk
from customtkinter import filedialog
from tkinter import messagebox
from threading import Thread, Event

Ctk.set_appearance_mode("System")

class ttkTimer(Thread):
    def __init__(self, callback, tick) -> None:
        super().__init__()
        self.callback = callback
        self.stopFlag = Event()
        self.tick = tick
        self.iters = 0

    def run(self):
        while not self.stopFlag.wait(self.tick):
            self.iters += 1
            self.callback()

    def stop(self):
        self.stopFlag.set()

    def get(self):
        return self.iters

class Player(Ctk.CTkFrame):

    def __init__(self, master, title=None, **kwargs):
        super().__init__(master, **kwargs)

        self.parent = master

        if title == None:
            title = "Video Player"
        self.parent.title(title)

        # Bar
        Frame1 = Ctk.CTkFrame(self.parent)
        Frame1.pack(fill=Ctk.BOTH)

        menubar = Ctk.CTkOptionMenu(
            Frame1, values=["Open File", "Exit"], command=self.select_media_file)
        menubar.pack(side=Ctk.LEFT)
        appearance_mode_optionemenu = Ctk.CTkOptionMenu(
            Frame1, values=["Light", "Dark", "System"], command=self.change_appearance_mode)
        appearance_mode_optionemenu.pack(side=Ctk.RIGHT)
        appearance_mode_optionemenu.set("System")

        # Player
        self.videopanel = Ctk.CTkFrame(self.parent)
        self.canvas = Ctk.CTkCanvas(
            self.videopanel, bg="black")
        self.canvas.pack(fill=Ctk.BOTH, expand=1)
        self.videopanel.pack(fill=Ctk.BOTH, expand=1)

        # contols
        ctrlpanel = Ctk.CTkFrame(self.parent)
        pause = Ctk.CTkButton(ctrlpanel, text="Pause", command=self.OnPause)
        play = Ctk.CTkButton(ctrlpanel, text="Play", command=self.OnPlay)
        stop = Ctk.CTkButton(ctrlpanel, text="Stop", command=self.OnStop)
        volume = Ctk.CTkButton(ctrlpanel, text="Volume",
                               command=self.OnSetVolume)
        pause.pack(side=Ctk.LEFT)
        pause.pack(side=Ctk.LEFT)
        play.pack(side=Ctk.LEFT)
        stop.pack(side=Ctk.LEFT)
        volume.pack(side=Ctk.LEFT)
        self.volume_var = Ctk.IntVar()
        self.volslider = Ctk.CTkSlider(
            ctrlpanel, variable=self.volume_var, command=self.volume_sel, from_=0, to=100)
        self.volslider.pack(side=Ctk.LEFT)
        ctrlpanel.pack(side=Ctk.BOTTOM)

        ctrlpanel2 = Ctk.CTkFrame(self.parent)
        self.scale_var = Ctk.DoubleVar()
        self.timeslider_last_val = ""
        self.timeslider = Ctk.CTkSlider(ctrlpanel2, variable=self.scale_var, command=self.scale_sel,
                                        from_=0, to=1000, height=20)
        self.timeslider.pack(side=Ctk.BOTTOM, fill=Ctk.X, expand=1)
        self.timeslider_last_update = time.time()
        ctrlpanel2.pack(side=Ctk.BOTTOM, fill=Ctk.X)

        # Vlc player controls
        self.Instance = vlc.Instance("--no-xlib", "--vout", "x11")
        self.player = self.Instance.media_player_new()

        self.player.video_set_deinterlace(self.str_to_bytes('yadif'))
        self.player.set_fullscreen(True)

        self.timer = ttkTimer(self.OnTimer, 1.0)
        self.timer.start()
        self.parent.update()

    def str_to_bytes(self, input_string):
        if isinstance(input_string, str):
            return input_string.encode('utf-8')
        else:
            raise TypeError("Input must be a string")

    def change_appearance_mode(self, new_appearance):
        Ctk.set_appearance_mode(new_appearance)

    def select_media_file(self, choice):
        if choice == "Exit":
            _quit()

        elif choice == "Open File":
            self.OnOpen()

    def OnExit(self, evt):
        self.Close()

    def SetTitle(self, title: str):
        self.parent.title = title

    def OnOpen(self):
        self.OnStop()

        p = pathlib.Path(os.path.expanduser("~"))
        filetypes = (
            ("video files", "*.mp4 *.mkv *.avi *.mp3"),
            ("all files", "*.*")
        )
        fullname = filedialog.askopenfilename(
            initialdir=p, title="choose your file", filetypes=filetypes)
        if os.path.isfile(fullname):
            dirname = os.path.dirname(fullname)
            filename = os.path.basename(fullname)
            self.Media = self.Instance.media_new(
                str(os.path.join(dirname, filename))
            )
            self.player.set_media(self.Media)

            title = self.player.get_title()
            if title == -1:
                title = filename
            self.SetTitle(title)

            if platform.system() == "Windows":
                self.player.set_hwnd(self.GetHandle())
            else:
                self.player.set_xwindow(self.GetHandle())
            # FIXME:
            self.OnPlay()
            self.volslider.set(self.player.audio_get_volume())

    def OnPlay(self):
        if not self.player.get_media():
            self.OnOpen()
        else:
            if self.player.play() == -1:
                self.errorDialog("Unable to play.")

    def GetHandle(self):
        return self.videopanel.winfo_id()

    def OnPause(self):
        self.player.pause()

    def OnStop(self):
        self.player.stop()
        # reset the time slider
        self.timeslider.set(0)

    def OnTimer(self):
        if self.player == None:
            return

        length = self.player.get_length()
        dbl = length * 0.001
        self.timeslider.configure(to=dbl)

        tyme = self.player.get_time()
        if tyme == -1:
            tyme = 0
        dbl = tyme * 0.001
        self.timeslider_last_val = ("%.0f" % dbl) + ".0"
        if time.time() > (self.timeslider_last_update + 2.0):
            self.timeslider.set(dbl)

    def scale_sel(self, evt):
        if self.player == None:
            return
        nval = self.scale_var.get()
        sval = str(nval)
        if self.timeslider_last_val != sval:
            self.timeslider_last_update = time.time()
            mval = "%.0f" % (nval * 1000)
            self.player.set_time(int(mval))  # expects milliseconds

    def volume_sel(self, evt):
        if self.player == None:
            return
        volume = self.volume_var.get()
        if volume > 100:
            volume = 100
        if self.player.audio_set_volume(volume) == -1:
            self.errorDialog("Failed to set volume")

    def OnToggleVolume(self, evt):
        is_mute = self.player.audio_get_mute()

        self.player.audio_set_mute(not is_mute)
        self.volume_var.set(self.player.audio_get_volume())

    def OnSetVolume(self):
        volume = self.volume_var.get()
        if volume > 100:
            volume = 100
        if self.player.audio_set_volume(volume) == -1:
            try:
                self.errorDialog("Failed to set volume")
            except TypeError:
                self.errorDialog("Wrong Type")

    def errorDialog(self, errormessage):
        edialog = messagebox.showerror(title="Error", message=errormessage)
        return edialog

    def OnExit(self):
        self.Close()

def Tk_get_root():
    if not hasattr(Tk_get_root, "root"):
        Tk_get_root.root = Ctk.CTk()
    return Tk_get_root.root

def _quit():
    print("_quite: see you soon guffer!")
    root = Tk_get_root()
    root.quit()
    root.destroy()
    os._exit(1)

if __name__ == '__main__':
    root = Tk_get_root()
    root.protocol("WM_DELETE_WINDOW", _quit)
    root.geometry("700x500")
    player = Player(root, title="Video Player")
    root.mainloop()


from typing import Literal
from pydantic import BaseModel, ConfigDict

class TrainPlace(BaseModel):
    model_config = ConfigDict(frozen=True)

    trainNumber: str
    carNumber: str
    compartmentNumber: str
    placeNumber: str
    carType: Literal['КУПЕ', 'ПЛАЦ']
    price: float
    isLower: bool
    isSide: bool
    isNearToilet: bool


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

# Example of performing linear least squares fitting

# In[ ]:

get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
import numpy as np

# Generating random data

# In[12]:

#set random number seed, seeding random number generator
np.random.seed(119)
npoints = 50
x=np.linspace(0,10.,npoints)
#set slope, intercept, and scatter rms, these are underlying trends
m = 2.0
b = 1.0
sigma = 2.0

#generate y points
y = m*x + b + np.random.normal(scale=sigma, size=npoints)
y_err = np.full(npoints, sigma)

# To make sure line is accurate, can simulate data, make fake data

# Let's plot data

# In[4]:

f = plt.figure(figsize=(7,7))
plt.errorbar(x,y, sigma, fmt='o')
plt.xlabel('x')
plt.ylabel('y')

# Method #1, polyfit

# In[15]:

m_fit, b_fit = np.poly1d(np.polyfit(x, y, 1, w=1./y_err)) #weight with uncertainties, y_err not yet defined
print(m_fit, b_fit)

y_fit = m_fit*x + b_fit

f = plt.figure(figsize=(7,7))
plt.errorbar(x,y, yerr=y_err, fmt='o', label ='data')
plt.plot(x,y_fit,label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=2,frameon=False)

# In[ ]:


from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name='koi',
    packages=find_packages(exclude=['build', 'doc', 'templates']),
    version='0.0.0',
    install_requires=[
        # "numpy",
        # "scipy",
        # "torch"
    ],
    python_requires='>=3.6',
    license="MIT",
    description='koi: wip package',
    long_description=long_description,
    long_description_content_type="text/markdown",
    author='Luca Bergamin',
    author_email='luca.bergamin95+github@gmail.com',
    url='https://github.com/bouncybutton/koi',
    download_url='https://github.com/bouncybutton/koi',
    keywords=['pytorch', 'machine-learning', 'variational-autoencoder', 'anomaly-detection'],
    classifiers=[
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'License :: OSI Approved :: MIT License',
    ]
)


import os
import numpy as np
import warnings
from . import io

class ConfigParser( object ):
    """ Handle configuration files and their parameters. """

    _TYPE = "_configholder_"
    # ================ #
    #    Methods       #
    # ================ #
    def __init__(self, filename=None, fileconfout=None):
        """
        Class builder.

        Parameters
        ----------
        filename : [string or None]
            Configuration file (input) directory.
            If None, the configuration file will be the default one ($LEPHAREWORK/pylephare/config/default.config).
            Default is None.

        fileconfout : [string or None]
            Output configuration file.
            If None, the output configuration file will be the default one ($LEPHAREWORK/pylephare/config/default_output.config).
            Default is None.

        Returns
        -------
        Void
        """
        self._init_config_()
        if filename is None:
            filename = io.DEFAULTCONFIG
            warnings.warn("Using default configuration file ($LEPHAREWORK/pylephare/config/default.config).")
        if fileconfout is None:
            fileconfout = io.DEFAULTCONFIG_OUT
            warnings.warn("Using default configuration file ($LEPHAREWORK/pylephare/config/default_output.config).")

        self.load_config(filename, fileconfout=fileconfout)

    def _init_config_(self):
        """
        Reset the configuration parameters attribute.

        Returns
        -------
        Void
        """
        self._config= {}

    @classmethod
    def read(cls, filename):
        """
        Read a given configuration file and build a ConfigParser object.

        Parameters
        ----------
        filename : [string or None]
            Configuration file (input) directory.

        Returns
        -------
        ConfigParser
        """
        return cls(filename)

    def writeto(self, filename=None):
        """
        Write the configuration parameters into the given configuration file.

        Parameters
        ----------
        filename : [string or None]
            Configuration file (input) directory.
            If None, the directory will be the already set 'fileout'.
            Default is None

        Returns
        -------

        """
        if filename is None:
            if self.fileout is not None:
                filename = self.fileout
            else:
                raise IOError("filename is None and no self.fileout set.")

        with open(filename, "w") as f:
            f.writelines("\n".join(self.get_config("configfile")))

    # -------- #
    #  LOADDER #
    # -------- #
    def load_config(self, filename, fileconfout=None):
        """
        Handle the original configuration file directories (input and output).
        Load the input configuration parameters from the file into attribute.

        Parameters
        ----------
        filename : [string or None]
            Configuration file (input) directory.

        fileconfout : [string or None]
            // ignored if None //
            Output configuration file directory.

        Returns
        -------
        Void
        """
        self._filename = filename
        self._init_config = open(filename).read().splitlines()
        metaconfig = [l for l in self._init_config
                      if not (l.startswith("#") or l.startswith("\t") or l.startswith(" ")) and len(l)>0]

        for k in metaconfig:
            key, *value = k.split()
            if len(value)==1:
                self.set_value(key, value[0], None)

            elif len(value)>1:
                if value[1]=="#":
                    self.set_value(key, value[0], " ".join(value[2:]))
                else:
                    raise IOError("Cannot parse the line %s"%k)
            else:
                raise IOError("cannot parse the line %s"%k)
        if fileconfout is not None:
            self.set_value("PARA_OUT", fileconfout)

    def _build_new_config_(self):
        """
        Build and return the configuration parameters, starting from the original one (if any) and accounting for any modification yet.

        Returns
        -------
        np.array
        """
        if hasattr(self,"_init_config"):
            # Has been loaded
            newconfig = []
            set_key = []
            for l in self._init_config:
                if ((l.startswith("#") or l.startswith("\t") or l.startswith(" ")) or len(l)==0):
                    newconfig.append(l)
                else:
                    key = l.split()[0]
                    if key in self.switched_off_keys:
                        key = "# "+key
                    newconfig.append(self.get_config_lines(key))
                    set_key.append(key)

            keys = np.asarray(list(self.config.keys()))
            not_set_key = keys[~np.in1d(keys, set_key)]

            if len(not_set_key)>0:
                newconfig.append("# == NEW KEYS == #")
                for key in not_set_key:
                    newconfig.append(self.get_config_lines(key))
            return np.array(newconfig)
        else:
            # new
            return self.get_config("array")

    # -------- #
    #  SETTER  #
    # -------- #
    def set_fileout(self, fileout, builddir=True):
        """
        Set as attribute the configuration file directory, which will be used during the SED fitting.

        Parameters
        ----------
        fileout : [string]
            Configuration file directory.

        Options
        -------
        builddir : [bool]
            If True, make the folder to save in the configuration file (if it doesn't exist yet).
            Default is True.

        Returns
        -------
        Void
        """
        if builddir:
            dir_ = os.path.dirname(fileout)
            if not os.path.isdir(dir_):
                os.makedirs(dir_)

        self._fileout = fileout

    def set_value(self, key, value, comments=None):
        """
        Mother setter methods. All data set this way will be recorded with using self.writeto().

        Parameters
        ----------
        key : [string]
            Parameter name on which to change the value.

        value : [string, int, float, list]
            New value to apply on the given parameter name.

        Options
        -------
        comments : [string or None]
            Comment(s) to add to the parameter.
            Default is None.

        Returns
        -------
        Void
        """
        self._config[key.upper()] = {"value":value,
                                     "comments":comments
                                     }

    def switch_off_key(self, key):
        """
        Make the given parameter to be ignored by LePhare.

        Parameters
        ----------
        key : [string]
            Parameter name to switch off.

        Returns
        -------
        Void
        """
        if key not in self.switched_off_keys:
            self._switched_off_keys.append(key)
            self._config["# "+key] = self._config.pop(key)

    def switch_on_key(self, key):
        """
        Make the given parameter to be taken into account by LePhare.

        Parameters
        ----------
        key : [string]
            Parameter name to switch on.

        Returns
        -------
        Void
        """
        if key in self.switched_off_keys:
            self._switched_off_keys.pop(self._switched_off_keys.index(key))
            self._config[key] = self._config.pop("# "+key)

    def set_filter_suffix(self, suffix, verbose=False):
        """
        Add the suffix to the {}_LIB, {}_LIB_IN {}_LIB_OUT values ({} = {STAR, QSO or GAL}).
        It also sets FILTER_FILE = suffix.filt

        Parameters
        ----------
        suffix : [string]
            Suffix to add to the concerned parameter values.

        Options
        -------
        verbose : [bool]
            Print informations.
            Default is True.

        Returns
        -------
        Void
        """
        if verbose:
            print("Running set_filter_suffix.")
        for key in ["STAR","QSO","GAL"]:
            self.set_value("%s_LIB"%key, "LIB_%s_%s"%(key,suffix))
            self.set_value("%s_LIB_IN"%key, "LIB_%s_%s"%(key,suffix))
            self.set_value("%s_LIB_OUT"%key, "%s_%s"%(key,suffix))

        self.set_value("FILTER_FILE", suffix+".filt")
        self.set_zphotlib(verbose=verbose)

    def set_zphotlib(self, gal=True, star=False, qso=False, gallib="BC03", verbose=True):
        """
        Select the template libraries on which to run the SED fitting.
        Return the value for "ZPHOTLIB" parameter.

        Parameters
        ----------
        gal : [bool]
            If True, run LePhare on galaxy SED templates.
            Default is True.

        star : [bool]
            If True, run LePhare on star SED templates.
            Default is False.

        qso : [bool]
            If True, run LePhare on QSO SED templates.
            Default is False.

        gallib : [bool]
            If 'gal' is True, define the used galaxy library among the available ones ($LEPHAREDIR/sed/GAL/).
            Default is "BC03".

        Options
        -------
        verbose : [bool]
            Print informations.
            Default is True.

        Returns
        -------
        string
        """
        def switch_lib(self, lib, button):
            _button = "on" if button else "off"
            for ii in ["_LIB", "_LIB_IN", "_LIB_OUT"]:
                eval("""self.switch_{}_key("{}")""".format(_button, lib+ii))
            return eval("""self.get_value("{}_LIB_OUT")""".format(lib)) if button else None

        if verbose:
            print("calling set_zphotlib")

        if gal:
            for key in ["GAL_LIB","GAL_LIB_IN","GAL_LIB_OUT"]:
                self.set_value(key, self.get_value(key).replace("GAL",gallib) )

        _zphotlib = []
        for _lib in ["STAR", "QSO", "GAL"]:
            _lib_out = switch_lib(self, lib=_lib, button=eval(_lib.lower()))
            if _lib_out is not None:
                _zphotlib.append(_lib_out)

        self.set_value("ZPHOTLIB", ",".join(_zphotlib))
        return self.get_value("ZPHOTLIB")

    def set_intrinsic_error(self, err_scale):
        """
        Set the value for the "ERR_SCALE" parameter.

        Parameters
        ----------
        err_scale : [list(float) or None]
            List of intrinsic errors to apply on each used filter.
            Must be the same size as for the list of filters (check with get_filters() method).
            If None, automatically switch off the "ERR_SCALE" parameter.

        Returns
        -------
        Void
        """
        if err_scale is None:
            self.switch_off_key("ERR_SCALE")
            return
        elif len(err_scale) != len(self.get_filters()):
            raise ValueError("You must provide the number of intrinsic magnitudes as you have filters.\n"+
                             "(err_scale: {} vs. filters: {})".format(err_scale, self.get_filters()))

        self.switch_on_key("ERR_SCALE") # ignored if already on
        self.set_value("ERR_SCALE",",".join(["{}".format(l) for l in err_scale]))

    # -------- #
    #  GETTER  #
    # -------- #
    def get_value(self, key):
        """
        Return the value for the given parameter name.

        Parameters
        ----------
        key : [string]
            Parameter name.

        Returns
        -------
        string or int or float or list().
        """
        if key not in self._config:
            raise ValueError("%s not in self.config"%key)
        return self._config[key]["value"]

    def get_comments(self, key):
        """
        Return the comment(s) associated to the given parameter name.

        Parameters
        ----------
        key : [string]
            Parameter name.

        Returns
        -------
        string
        """
        if key not in self._config:
            raise ValueError("%s not in self.config"%key)
        return self._config[key]["comments"]

    def get_config_lines(self, key):
        """
        Return a LePhare configuration file formatted string line given by the given parameter name.

        Parameters
        ----------
        key : [string]
            Parameter name.

        Returns
        -------
        string
        """
        return " ".join([key, self._config[key]["value"], "#", self._config[key]["comments"]] if self._config[key]["comments"] is not None
                        else [key, self._config[key]["value"]])

    def get_config(self, astype="dict"):
        """
        Return the configuration parameters in the given format.

        Parameters
        ----------
        astype : [string]
            Choice of the returning configuration parameters format:
            - "dict": dictionary
            - "dataframe": pandas.DataFrame
            - "array": np.array
            - "file" or "configfile": list (configuration file visual)
            - "original" or "input" or "inputfile": list (same format as for "file" or "configfile", \n
                                                          but with the parameters coming from the original configuration file).

        Returns
        -------
        dict or pandas.DataFrame or array
        """
        if astype == "dict":
            return self.config
        elif astype.lower() == "dataframe":
            import pandas
            return pandas.DataFrame(self.config).T

        elif astype.lower() == "array":
            return np.array([self.get_config_lines(k_) for k_ in self.config.keys()])

        elif astype.lower() in ["file", "configfile"]:
            return self._build_new_config_()

        elif astype.lower() in ["original","input","inputfile"]:
            if hasattr(self,"_init_config"):
                return self._init_config
            else:
                raise ValueError("No original configuration file")

        raise ValueError("astype '%s' not supported. Could be dict, dataframe, array, configfile, original"%astype)

    def get_fileout(self, buildit=True, update=False):
        """
        Return the configuration file directory which will be used for the SED fitting.

        Options
        -------
        buildit : [bool]
            If True, make the configuration file, if it doesn't exist yet.
            Default is True.

        update : [bool]
            If True, update the configuration file saved in self.fileout.
            Default is False.

        Returns
        -------
        string
        """
        if self.has_fileout():
            if update or (not os.path.isfile(self.fileout) and buildit):
                self.writeto()

        return self.fileout

    def get_filters(self, name=False):
        """
        Return the filters implemented in the configuration parameters.

        Options
        -------
        name : [bool]
            If True, return a list of the used filters (with the format instrument.band).
            If False, directly return the value for the parameter "FILTER_LIST".
            Default is False.

        Returns
        -------
        string or list(string)
        """
        filtfile_list = self.get_value("FILTER_LIST").split(",")
        if not name:
            return filtfile_list
        return [io.filterfile_to_filtername(filt) for filt in filtfile_list]

    def get_catin_columns(self):
        """
        Return a list of the columns in the input catalog which will be used for the SED fitting.

        Returns
        -------
        list(string)
        """
        catformat = self.get_value("CAT_FMT")
        cattype   = self.get_value("CAT_TYPE")
        filters   = self.get_filters(name=True)

        if catformat == "MMEE":
            names = list(filters)+[f+".err" for f in filters]
        elif catformat == "MEME":
            names = list(np.concatenate([[f,f+".err"] for f in filters]))
        else:
            raise ValueError("Cannot parse the CAT_FMT (%s) from the configuration file."%catformat)

        if cattype == "LONG":
            names += ["context","z-spec", "string"]
        elif cattype == "SHORT":
            names += ["context"]
        else:
            raise ValueError("Cannot parse the CAT_TYPE (%s) from the configuration file."%cattype)

        return names

    # ================ #
    #  Properties      #
    # ================ #
    @property
    def switched_off_keys(self):
        """ List of switched off parameter keys """
        if not hasattr(self, "_switched_off_keys"):
            self._switched_off_keys = []

        return self._switched_off_keys
    @property
    def config(self):
        """ Dictionary containing every LePhare configuration parameters """
        if not hasattr(self,"_config") or self._config is None:
            self._config = {}

        return self._config

    @property
    def fileout(self):
        """ Configuration file directory """
        if not hasattr(self, "_fileout"):
            self._fileout = None
        return self._fileout

    def has_fileout(self):
        """ Test that a configuration file directory is set yet """
        return self.fileout is not None


import cv2
import numpy as np

cap = cv2.VideoCapture(0)
img = cv2.imread("picturs/window.jpg")
img = cv2.resize(img,(600,600))
pts = []

while True :
    ret , frame = cap.read()

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    low_red = np.array([0,0,255])
    upp_red = np.array([255,255,255])
    mask = cv2.inRange(hsv , low_red , upp_red)

    (minVal , maxVal , minLoc , maxLoc) = cv2.minMaxLoc(mask)
    if maxLoc[0] != 0 and maxLoc[1] != 0 :
        font = cv2.FONT_HERSHEY_COMPLEX
        color = (255,0,0)
        fontScale = 1
        thickness = 2
        mask = cv2.putText(mask , str(maxLoc[0]) + "," + str(maxLoc[1]), maxLoc,font,fontScale,color,thickness,cv2.LINE_AA )

        print(maxLoc)
        cv2.circle(mask , maxLoc , 20 , (255,255,255), 2 , cv2.LINE_AA)

        #conect with image
        font = cv2.FONT_HERSHEY_COMPLEX
        cv2.putText(img , "*", maxLoc , font , 0.5, (55,144,144), 1)
        #img = cv2.putText(img , str(maxLoc[0]) + "," + str(maxLoc[1]), maxLoc,font,fontScale,color,thickness,cv2.LINE_AA )
        print(maxLoc)
        cv2.circle(mask , maxLoc , 20 , (255,255,255), 2 , cv2.LINE_AA)

    cv2.imshow('fram', mask)
    cv2.imshow('images', img)

    k=cv2.waitKey(1)
    if k == ord ('q'):
        break

cap.release()
cv2.destroyAllWindows()


# GOAL: Create a simple groovebox using pydub to render JSON sequences created via web
import copy
import pyaudio
import wave
import sys
import json
import simplejson
from pydub import AudioSegment
import pydub.scipy_effects
import cgitb
from os import curdir, rename
from os.path import join as pjoin

cgitb.enable()

channels = {}

# Beats per minute
bpm = 102

# Beats per bar
beatDiv = 4
# Ticks per beat
tickDiv = 32
# Pattern length in bars
patternLength = 1

# Amount of swing to apply to patterns
# Starts getting crazy above 1, reasonably groovy at like .25.  Default to none.
swingAmount = 0;
# swingUp is an iterator that gets toggled with each note, to alternate the swing direction
# i.e., it swings up and then down, or back and then forth.  You get it.
swingUp = False

# Default channel volume and pan
channelDefaults = {'volume': -12.0, 'pan': 0}

# Start with an empty dictionary of tracks
tracks = {}

# Pitch indexes
pitchIndexes = {'C': 0, 'C#': 1, 'D': 2, 'D#': 3, 'E': 4, 'F': 5, 'F#': 6, 'G': 7, 'G#': 8, 'A': 9, 'A#': 10, 'B': 11}

def pitch_diff(fromPitch, toPitch):
    for octave in range(9):
      if str(octave) in fromPitch:
          fromOctave = octave
          fromNote = fromPitch.replace(str(fromOctave),"")
      if str(octave) in toPitch:
          toOctave = octave
          toNote = toPitch.replace(str(toOctave),"")
    fromDiff = (toOctave - fromOctave) * 12
    fromNote = fromNote.upper()
    toNote = toNote.upper()
    fromDiff = fromDiff + (pitchIndexes[toNote] - pitchIndexes[fromNote])
    return fromDiff

def detect_leading_silence(sound, silence_threshold=-50.0, chunk_size=10):
    trim_ms = 0 # ms

    assert chunk_size > 0 # to avoid infinite loop
    while sound[trim_ms:trim_ms+chunk_size].dBFS < silence_threshold and trim_ms < len(sound):
        trim_ms += chunk_size

    return trim_ms

def trim(sound, silence_threshold=-50.0, chunk_size=10):
    start_trim = detect_leading_silence(sound, silence_threshold, chunk_size)
    end_trim = detect_leading_silence(sound.reverse(), silence_threshold, chunk_size)
    duration = len(sound)
    trimmed_sound = sound[start_trim:duration-end_trim]
    return trimmed_sound

def newTrack(name = ''):
    trackLength = 0
    # Length of the track is equal to number of bars times beats-per-bar times beat length
    trackLength += (patternLength * beatDiv) * beatLen()
    track = AudioSegment.silent(trackLength)
    if name:
        global tracks
        tracks[name] = track
    else:
        return track
    return

def addChannel():
    global channels
    newName = input()
    if len(newName):
        newChannel = channelDefaults
        newChannel['name'] = newName
        channels[len(channels)+1] = copy.copy(newChannel)
        listChannels()
    else:
        # do nothing, just end the process
        return()

def reindexChannels():
    global channels
    newChannels = {}
    for key, channel in channels.items():
        newChannels[len(newChannels)+1] = copy.copy(channel)
    channels = newChannels

def deleteChannel():
    global channels
    listChannels('Select a channel to delete:')
    selection = input()
    try:
        selection = int(selection)
        if selection not in channels:
            print("Invalid selection.")
        else:
            del channels[selection]
            reindexChannels()
    except ValueError:
        print("Invalid selection.")

def menuPrompt(menuName):
    menu = menuItems[menuName];
    for key, item in menu.items():
        skip = False
        if ('condition' in item and item['condition'] == 'hasChannels' and len(channels) < 1):
            skip = True
        if not skip:
            print("{key}. {label}".format(key=key, label=item['label']))
    selection = input()
    try:
        selection = int(selection)
        if selection not in menu:
            print("Invalid selection.")
            menuPrompt(menuName)
        else:
            print("OK then we should do {operation}".format(operation=menu[selection]['operation']))
            exec(menu[selection]['operation']+'()')
    except ValueError:
        print("Invalid selection.")
        menuPrompt(menuName)

def dashLine(length):
    lineStr = ''
    count = 1
    while count <= length:
        lineStr += '-'
        count += 1
    return lineStr

def panDisplay(pan):
    if pan == 0:
        return 'C'
    if pan > 0:
        return "{}R".format(pan)
    if pan < 0:
        return "{}L".format(abs(pan))

def listChannels(prompt = 'Available channels:'):
    if (len(channels)):
        print(prompt)
    else:
        print("No channels have been added.")
    for index, channel in channels.items():
        print("{index}. {name} (Vol: {vol}, Pan: {pan})".format(index=index,name=channel['name'], vol=channel['volume'], pan=channel['pan']))

def channelSelect(prompt):
    print(prompt)
    selection = input()
    try:
       return int(selection)
    except ValueError:
       return None;

# Length of a bar in milliseconds
def barLen():
    return (60/bpm) * 1000 * beatDiv

# Length of a beat in milliseconds
def beatLen():
    return (60/bpm) * 1000

# Length of a tick in milliseconds
def tickLen():
    return (60/bpm) * 1000 / tickDiv

# Add an instance of a sound at a location determined by bpm-based placement
def addNote(trackName,sound,bar,beat,tick,options = {}):
    global swingUp
    global tracks
    if tracks[trackName]:
        track = tracks[trackName]
    else:
        return
    # If swing is active and we are not on the very first tick of the pattern, apply swing
    if (swingAmount and (tick > 1 or beat > 1 or bar > 1)):
        if (swingUp):
            tick += swingAmount
        else:
            tick -= swingAmount
        swingUp = not swingUp
    loc = 0
    loc += tickLen() * (tick - 1)
    loc += beatLen() * (beat - 1)
    loc += barLen() * (bar - 1)
    loc = int(loc)
    track = track.overlay(sound,int(loc),0)
    tracks[trackName] = track
    # print("Added sound at {loc}".format(loc=loc))

def split(wavFile,pieces):
    soundLoc = './audio/uploaded/'+wavFile
    sound = AudioSegment.from_wav(soundLoc)
    slices = []
    sliceLen = int(len(sound) / pieces)
    for i, chunk in enumerate(sound[::sliceLen]):
        if (i >= pieces):
            break;
        sliceName = "./audio/uploaded/split/{fileName}-{num}.wav".format(fileName=wavFile.replace('.wav',''),num=i+1)
        sliceInfo = {'filename': sliceName, 'parent': soundLoc, 'start': i*sliceLen, 'end': (i+1)*sliceLen, 'len': len(chunk)}
        with open(sliceName, "wb") as f:
            chunk.export(f, format="wav")
            slices.append(sliceInfo)
    return slices

def transpose(sound,st):
    octaves = float(st) / 12
    new_sample_rate = int(sound.frame_rate * (2.0 ** octaves))
    return sound._spawn(sound.raw_data, overrides={'frame_rate': new_sample_rate})

def renderJSON(json):
    global channels, bpm, beatDiv, tickDiv, patternLength, swingAmount, swingUp, channelDefaults, tracks
    data = simplejson.loads(json)

    bpm = int(data['bpm'])
    beatDiv = int(data['beatDiv'])
    tickDiv = int(data['tickDiv'])
    patternLength = int(data['bars'])
    swingAmount = float(data['swing'])
    swingUp = False

    tracks = {}

    title = data['title']
    channels = data['tracks']
    if ('repeat' in data):
        repeat = data['repeat']

    # Create the master track
    master = newTrack()
    print("Rendering")
    print(dashLine(9))
    print("Title:  {}".format(title))
    print("Tempo:  {} BPM".format(bpm))
    print("Length: {} bars".format(patternLength))
    print(" ")
    print("Tracks")
    print(dashLine(6))
    # For each listed channel, add a track, add the notes, apply any filters, set the volume, set the pan and overlay onto master
    # TODO: Split all these steps into functions for neatness' sake
    for trackName, channel in channels.items():
        # Add a track
        newTrack(trackName)
        # Add the notes
        trackNotes = channel['notes']
        trackWav = './audio/' + channel['sample']['wav']
        trackSound = AudioSegment.from_wav(trackWav)
        trackTranspose = 0
        if 'trim' in channel:
            if (channel['trim'] == True):
                trackSound = trim(trackSound)
        if 'reverse' in channel:
            if (channel['reverse'] == True):
                trackSound = trackSound.reverse()
        # Handle transposition.  trackTranspose is the transposition value in semitones
        if 'transpose' in channel:
            trackTranspose = channel['transpose']
            trackSound = transpose(trackSound,trackTranspose)
        amp = False
        if 'amp' in channel:
            amp = channel['amp']
            if 'attack' in amp:
                peak = 0.0
                if 'peak' in amp:
                    peak += float(amp['peak'])
                trackSound = trackSound.fade(from_gain=-120.0, to_gain = peak, start=0, duration=200)

        for note in trackNotes:
            # Parse note location, volume and any other settings
            noteData = note['loc'].split('.')
            noteBar = int(noteData[0])
            noteBeat = int(noteData[1])
            noteTick = int(noteData[2])
            addSound = copy.copy(trackSound)
            # Transpose
            if ('transpose' in note):
                addSound = transpose(addSound,int(note['transpose']))
            # Pitch
            if ('pitch' in note):
                addSound = transpose(addSound,pitch_diff(channel['rootPitch'],note['pitch']))
            # Apply the volume
            if ('volume' in note):
                addSound = addSound + int(note['volume'])
            # Apply the pan
            if ('pan' in note):
                addSound = addSound.pan(int(note['pan'])/100)
            # Add note
            out_f = open(pjoin("rendered/tmp","processed-{}.wav".format(trackName)), 'wb')
            addSound.export(out_f, format='wav')
            addNote(trackName,addSound,noteBar,noteBeat,noteTick)
            out_f = open(pjoin("rendered/tmp","layer-{}.wav".format(trackName)), 'wb')
            tracks[trackName].export(out_f, format='wav')
        # Apply filter1
        if ('filterOn' in channel and channel['filterOn'] == True):
            if (channel['filter']['type'] == 'hp' and channel['filter']['frequency']):
                tracks[trackName] = tracks[trackName].high_pass_filter(cutoff_freq=int(channel['filter']['frequency']), order=3)
            if (channel['filter']['type'] == 'lp' and channel['filter']['frequency']):
                tracks[trackName] = tracks[trackName].low_pass_filter(cutoff_freq=int(channel['filter']['frequency']), order=3)
            if (channel['filter']['type'] == 'bp' and channel['filter']['frequency'] and channel['filter']['frequency2']):
                tracks[trackName] = tracks[trackName].band_pass_filter(low_cutoff_freq=int(channel['filter']['frequency']), high_cutoff_freq=int(['filter']['frequency2']), order=3)
        # Apply the volume
        if amp:
            trackVol = float(amp['volume'])
            tracks[trackName] += trackVol
        # Apply the pan
        if ('pan' in channel):
            trackPan = int(channel['pan'])
            tracks[trackName] = tracks[trackName].pan(trackPan/100)
        # Overlay onto master
        master = master.overlay(tracks[trackName],0,0)
        print("{name}: {vol}db, Pan: {pan} ".format(name=trackName,vol=trackVol,pan=panDisplay(trackPan)))

    renderFilename = pjoin("rendered","{}.mp3".format(title))
    print(" ")
    print("Rendered to {}".format(renderFilename))
    print(" ")
    out_f = open(renderFilename, 'wb')
    master *= repeat
    master.normalize().export(out_f, format='mp3')
    return renderFilename


from .abstract import Addon
import logging
import tornado.ioloop
import sys
from datetime import datetime, timedelta

try:
    from scapy.all import IP, UDP, DNS
except ImportError:
    logging.error("Cannot import scapy. It's required for NameserverRewriter.")
    sys.exit(1)

DNS_TIMEOUT = 60
DNS_TIMEOUT_CLEARANCE = 10

class NameserverRewriter(Addon):
    def __init__(self, config, session):
        super(NameserverRewriter, self).__init__(config, session)
        self.logger = logging.getLogger(str(self))
        self.logger.debug("created.")
        self.records = {}
        self.periodic_callback = tornado.ioloop.PeriodicCallback(
            self.clear_timeout, DNS_TIMEOUT_CLEARANCE * 1000)
        if "force_nameserver" not in self.config:
            self.config['force_nameserver'] = '8.8.8.8'
            self.logger.warning("Using default nameserver " +
                self.config['force_nameserver'])

    def __str__(self):
        return "nameserver-rewriter(%s)" % self.config.get(
            "force_nameserver", 'Unknown')

    def on_session_established(self):
        self.session.add_rewriter_callback(self.rewrite)

    def rewrite(self, pkt):
        ip = IP(pkt)
        if ip.haslayer(DNS):
            iph = ip.getlayer(IP)
            udph = ip.getlayer(UDP)
            dns = ip.getlayer(DNS)
            if dns.qr == 0:  # query
                record = {
                    'dst_ip':  iph.dst,
                    'time': datetime.utcnow()
                }
                self.records[dns.id] = record
                self.logger.debug("rewriting DNS query: %s to %s" % (iph.dst,
                    self.config['force_nameserver']))
                iph.dst = self.config['force_nameserver']
            elif dns.qr == 1:  # answer
                self.logger.debug("found DNS answer: " + dns.summary())
                record = self.records.get(dns.id, None)
                if record:
                    self.logger.debug("rewriting DNS answer: %s to %s" % (
                        iph.src, record['dst_ip']))
                    iph.src = record['dst_ip']
                    del self.records[dns.id]
            del iph.chksum
            del udph.chksum
            del iph.len
            del udph.len
            return str(iph / udph / dns)

    def clear_timeout(self):
        for dns_id in self.records:
            record = self.records[dns_id]
            if (record['time'] - datetime.utcnow()) > timedelta(
                seconds=DNS_TIMEOUT):
                del self.records[dns_id]

    def cleanup(self):
        self.periodic_callback.stop()

    def setup(self):
        self.periodic_callback.start()


#membalik kalimat

def getReversed (x):
    kalimatRev = ''
    listKata = x.split(' ')
    for kata in listKata:
        kalimatRev += kata[: : -1] + ' '
    return kalimatRev

x = input('masukan kalimat : ')
hasilRev = getReversed(x)
print(f'{x} kebalikannya adalah {hasilRev}')


import torch
import torchvision
import numpy as np
def miouf(_pred,_t_idx):
    pred=_pred.clone()
    t_idx=_t_idx.clone()
    B,numcls,H,W=pred.shape
    pred=pred.argmax(1)
    assert pred.shape == t_idx.shape

    with torch.no_grad():
        miou=[]
        for clsidx in range(1,numcls):
            if not (t_idx==clsidx).any():
                continue
            iou=(((pred==clsidx) & (t_idx==clsidx)).sum())/(((pred==clsidx) | (t_idx==clsidx)).sum().float())
            miou+=[iou.item()]
        miou=np.mean(miou)
        return miou

def prmaper(_pred,_t_idx,numcls):
    pred=_pred.clone()
    t_idx=_t_idx.clone()
    with torch.no_grad():
        pred=pred.argmax(1)
        prmap=torch.zeros(numcls,numcls)
        for pred_i in range(numcls):
            for t_i in range(numcls):
                prmap[pred_i,t_i]=((pred==pred_i) & (t_i==t_idx)).sum()
        return prmap
def cal_grad_ratio(pred,y_true,num_cls=3):
    grad=pred.grad.detach()
    with torch.no_grad():
        loss=torch.zeros(num_cls)
        for cls in range(num_cls):
            for i in range(3):
                loss[cls]+=(grad[:,i][(y_true==cls)]**2).sum()
        return loss

def setcolor(idxtendor, colors):
    assert idxtendor.max() + 1 <= len(colors)
    B, H, W = idxtendor.shape
    colimg = torch.zeros(B, 3, H, W).to(idxtendor.device).to(idxtendor.device)
    colors = colors[1:]
    for b in range(B):
        for idx, color in enumerate(colors, 1):
            colimg[b, :, idxtendor[b] == idx] = (color.reshape(3, 1)).to(idxtendor.device).float()
    return colimg

def mAP(_pred,_t_idx):
    import numpy as np
    from sklearn.metrics import average_precision_score
    pred=_pred.clone()
    t_idx=_t_idx.clone()
    B,C,H,W=pred.shape
    with torch.no_grad():
        map=[]
        for clsidx in range(1,C):
            map.append(average_precision_score((t_idx==clsidx).cpu().numpy().reshape(-1),pred[:,clsidx].cpu().numpy().reshape(-1)))

    return np.mean(map)


#Contest URL - http://datahack.analyticsvidhya.com/contest/last-man-standing
import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn.grid_search import GridSearchCV
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import pandas_profiling

train = pd.read_csv('Train_Fyxd0t8.csv')
test = pd.read_csv('Test_C1XBIYq.csv')

#feature Engineering
def diff_week(df):
    df['diff_weeks'] = df['Number_Doses_Week'] - df['Number_Weeks_Used']
    df = df.drop(['Number_Doses_Week'], axis =1 )
    df = df.drop(['Number_Weeks_Used'], axis =1)
    return df

def crop_type_sq(df):
    df['Crop_Type'] = df['Crop_Type'] * df['Crop_Type']
    return df

def season_sq(df):
    df['Season'] = df['Season'] * df['Season'] * df['Season']
    return df

def rescale_crop(df):
    df['Crop_Type'] = df['Crop_Type'] + 1
    return df

def rescale_soil(df):
    df['Soil_Type'] = df['Soil_Type'] + 1
    return df

def soil_crop_pesticide_combo(df):
    df['Crop_Type'] = df['Crop_Type'] + 0
    df['Soil_Type'] = df['Soil_Type'] + 2
    df['Pesticide_Use_Category'] = df['Pesticide_Use_Category'] + 4
    #for generating unique number for each combination.
    df['scp_combo'] = df['Soil_Type'] * df['Season'] * df['Pesticide_Use_Category']
    df = df.drop( 'Crop_Type' , axis = 1)
    df = df.drop( 'Soil_Type', axis = 1)
    df = df.drop( 'Pesticide_Use_Category', axis = 1)
    return df

#train = diff_week(train)
#train = crop_type_sq(train)
#train = season_sq(train)
train = rescale_crop(train)
train = rescale_soil(train)
train = soil_crop_pesticide_combo(train)

#test = diff_week(test)
#test = crop_type_sq(test)
#test = season_sq(test)
test = rescale_crop(test)
test = rescale_soil(test)
test = soil_crop_pesticide_combo(test)

Y = train['Crop_Damage']
train = train.drop('Crop_Damage', axis = 1)
train = train.drop('ID', axis = 1)
Ids = test['ID']
test = test.drop('ID', axis = 1)

clf = xgb.XGBClassifier(objective='reg:logistic', nthread=4, seed=0, max_depth=7, learning_rate=0.005, n_estimators=1000)
#, max_depth=7, learning_rate=0.005, n_estimators=1000, gamma = 2
clf.fit(train, Y)

clf.score(train, Y)

test_y = clf.predict(test)
test_ans = pd.DataFrame({'ID':Ids, 'Crop_Damage': test_y})
order = ['ID', 'Crop_Damage']
test_ans[order].to_csv('out.csv',index= False)