import scrapy
from scrapy_splash import SplashRequest
from datetime import datetime

class SVNIndividualPageSpider(scrapy.Spider):
    name = 'svn'
    start_urls = ['https://svn.com/find-properties/?propertyId=wagenburgwest']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url, self.parse, args={'wait': 0.5})

    def parse(self, response):
        iframe_url = response.css('#buildout iframe::attr(src)').extract_first()
        orig_url = response.url
        yield SplashRequest(url=iframe_url, callback=self.parse_buildout_iframe, meta={'orig_url': orig_url})

    def parse_buildout_iframe(self, response):
        contacts_data = []
        items = ['1', '2', '3']
        for i in items:
            scope = f'div:nth-child({i}) > table > tbody > tr > td.js-broker-details'
            contacts_data.append(
                {
                    'name': response.css(f'{scope} > strong > a::text').extract_first(),
                    'job_title': response.css(f'{scope} > div:nth-child(2) > small::text').extract_first(),
                    'email': response.css(f'{scope} > div.js-broker-contact-info > a::text').extract_first(),
                    'phone_main': response.css(f'{scope} > div.js-broker-contact-info > div:nth-child(2) > a::text').extract_first(),
                    'phone_office': response.css(f'{scope} > div.js-broker-contact-info > div:nth-child(3) > a::text').extract_first(),
                    'fax': response.css(f'{scope} > div.js-broker-contact-info > div:nth-child(4)::text').extract_first(),
                },
            )

        yield {
            'url': response.meta['orig_url'],
            'scraped_at': datetime.now(),
            'property_name': response.css('.header-text h1::text').extract_first(),
            'address': response.css('.text-muted::text').extract(),
            'price': response.css('tr:nth-child(1) > td:nth-child(2)::text').extract_first(),
            'property-type': response.css('tr:nth-child(2) > td:nth-child(2)::text').extract_first(),
            'size': response.css('tr:nth-child(3) > td:nth-child(2)::text').extract_first(),
            'description': response.css('.js-description-content::text').extract_first(),
            'highlghts': [
                response.css('ul > div > div:nth-child(1) > li:nth-child(1)::text').extract_first(),
                response.css('ul > div > div:nth-child(1) > li:nth-child(2)::text').extract_first(),
                response.css('ul > div > div:nth-child(2) > li:nth-child(1)::text').extract_first(),
                ],
            'location': response.css('.js-location-content::text').extract_first(),
            'contacts': contacts_data
        }

class AmRealIndividualPageSpider(scrapy.Spider):
    name = 'amreal'
    start_urls = ['http://www.amreal.com/property.htm?propertyId=419174-sale']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url, self.parse, args={'wait': 0.5})

    def parse(self, response):
        iframe_url = response.css('#buildout iframe::attr(src)').extract_first()
        orig_url = response.url
        yield SplashRequest(url=iframe_url, callback=self.parse_buildout_iframe, meta={'orig_url': orig_url})

    def parse_buildout_iframe(self, response):
        description_text = []
        items = ['2', '3', '4', '5', '6', '7', '9']
        for i in items:
            scope = f'.col-md-9.pr-3-lg > div:nth-child(3) > p:nth-child({i})'
            description_text.append(response.css(f'{scope}::text').extract_first())

        yield {
            'url': response.meta['orig_url'],
            'scraped_at': datetime.now(),
            'property_name': response.css('.plugin-header-title::text').extract_first(),
            'address': response.css('.plugin-header-address::text').extract_first(),
            'price': response.css('tr:nth-child(1) > td::text').extract_first(),
            'property-type': response.css('tr:nth-child(2) > td::text').extract_first(),
            'size': response.css('div:nth-child(2) > table > tbody > tr > td::text').extract_first(),
            'description': description_text,
            'latitude': response.css('.map-container.w-100::attr(data-latitude)').extract_first(),
            'longitude': response.css('.map-container.w-100::attr(data-longitude)').extract_first(),
            'contacts': [
               {
                 'name': response.css('div:nth-child(1) > strong::text').extract_first(),
                 'telephone': response.css('.broker-phone > a.text-dark::text').extract_first(),
                 'email': response.css('.text-dark.text-truncate.d-block::text').extract_first(),
               }
             ],
        }


import numpy as np
from matplotlib.patches import ConnectionPatch
from matplotlib.path import Path
import matplotlib.patches as patches
import matplotlib.image as mpimg
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
gs = gridspec.GridSpec(2, 3)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[1, 0])
ax = plt.subplot(gs[:, 1:3])

xy1 = (1390, 1186)
xy2 = (473, 1408)
image = np.load('image.npy')
ax.imshow(np.log(image), interpolation="none", aspect="auto", cmap=cm.Greys_r)
subimage1 = image[xy1[1]-10:xy1[1]+10,xy1[0]-10:xy1[0]+10]
subimage2 = image[xy2[1]-10:xy2[1]+10,xy2[0]-10:xy2[0]+10]
ax1.imshow(np.log(subimage1), interpolation="none", aspect="auto", cmap=cm.Greys_r)
ax2.imshow(np.log(subimage2), interpolation="none", aspect="auto", cmap=cm.Greys_r)

xy = (11, 11)

con1 = ConnectionPatch(xyA=xy, xyB=xy1, coordsA="data", coordsB="data",
                       axesA=ax1, axesB=ax,
                       arrowstyle="->", shrinkB=5, color='r')
con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA="data", coordsB="data",
                       axesA=ax2, axesB=ax,
                       arrowstyle="->", shrinkB=5, color='r')
ax.add_artist(con1)
ax1.add_artist(con1)
ax.add_artist(con2)
ax2.add_artist(con2)

#rect = patches.Rectangle((5, 5), 11, 11, fill=False, ls='solid')
#ax1.add_patch(rect)
ax1.plot([5,5,16,16,5], [5,16,16,5,5])
#ax1.axis([0,20,0,20])
#ax1.axes.get_xaxis().set_visible(False)
#ax1.axes.get_yaxis().set_visible(False)
ax1.axis('off')
ax2.plot([5,5,16,16,5], [5,16,16,5,5])
#ax2.axis([0,20,0,20])
#ax2.axes.get_xaxis().set_visible(False)
#ax2.axes.get_yaxis().set_visible(False)
ax2.axis('off')

ax.axis('off')
#plt.show()
plt.savefig('spot.pdf',format='pdf', bbox_inches='tight', pad_inches=0)


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 17:24:02 2015

@author: magistr
"""

# -*- coding: utf-8 -*-
import sys
import os.path
import matplotlib.pyplot as plt

from sklearn.svm import SVR
from EnsembleClassifier import EnsembleClassifier,OMEnsembleClassifier
from EnsembleClassifier import ANNEnsembleClassifier
from regres import read_data, read_ens_coeffs
from sklearn.cross_validation import ShuffleSplit
from sklearn.utils import shuffle

from statistics import mean, median
from SelectionStrategy import NoneStrategy

from numpy import percentile, std
from collections import defaultdict

from enscalibration_3 import calibrate_ensemble
from sklearn.metrics import mean_squared_error

from math import sqrt

def rmse(actual, predicted):
    ln = min(len(predicted), len(actual))
    rmse = sqrt(mean_squared_error(actual[:ln], predicted[:ln]))
    return rmse

if __name__ == "__main__":
    path = sys.argv[1]
    path2 = sys.argv[2]

    MODEL = "S1"

    measurementsFile = os.path.join(path, "2011080100_measurements_{}_2623.txt".format(MODEL))
    noswanFile = os.path.join(path, "2011080100_noswan_{}_48x434.txt".format(MODEL))
    swanFile = os.path.join(path, "2011080100_swan_{}_48x434.txt".format(MODEL))
    hirombFile = os.path.join(path, "2011080100_hiromb_{}_60x434.txt".format(MODEL))
    coeffsFile = os.path.join(path2, "ens_coefs.txt")
#    coeffsFile = "../Data/NewEns/ens_coefs.txt"

    measurements = read_data(measurementsFile)
    noswan = read_data(noswanFile)
    swan = read_data(swanFile)
    hiromb = read_data(hirombFile)

    coefs = list(read_ens_coeffs(coeffsFile))
    classifier = OMEnsembleClassifier([hiromb, swan, noswan],
                                              coefs, measurements, error_measurement=rmse)
    classifier.prepare(1)
    #classifier = EnsembleClassifier([hiromb, swan, noswan], coefs, measurements)

   # classifier2 = classifier.copy()

    total = len(hiromb)

    #svm_model = lambda : SVR(kernel='rbf', C=1e6, gamma=0.3)

#        learn_count = 30
    lin = []
    lin_s = []
    bst = []
    ns = []
    ptas = []
    ptas_by_ens = []
    ens_by_place = []
    best2selected_pred_dist = []
    best2selected_act_dist = []

    validate_count = total - 250
    variants = 40
    cross_set = list(ShuffleSplit(total, variants, validate_count, 250))

    max_size = 100
    ts_sizes = range(1, max_size) #250) #[30, 70, 120, 145]
    for learn_count in ts_sizes:
#        coefs = list(calibrate_ensemble([hiromb, swan, noswan],
#                                            measurements, learn_count))
#        classifier = OMEnsembleClassifier([hiromb, swan, noswan],
#                                              coefs, measurements)
#        classifier.prepare(1)

        predicted = []
        spred = []
        best = []
        ens = []
        pta = []

        ens_places = [0] * len(coefs)

        best2selected_pred = []
        best2selected_act = []

#        for (training_set, validate_set) in cross_set:
        for t in range(max_size, total):
           #ts = training_set[:learn_count]
#            ts = list(range(t-learn_count, t-8))
            classifier.train(t, learn_count)
            validate_set = [t]

            pred_ens, pred_err = classifier.predict_best_ensemble(t)
            predicted.append(pred_err)

            best_ens, best_err = classifier.get_best_ensemble(t)
            best.append(best_err)

            ens.append(classifier.get_biggest_ensemble(t)[1])
            pta.append(list(map(lambda x: abs(x[0]-x[1]),
                                classifier.get_predict_to_actual_error(t))))

            ensemble_prediction, actual_errors = classifier.get_ens_ranged_by_prediction(t)
            ens_places[ensemble_prediction.index(best_ens)] += 1
            spred.append(actual_errors[1:])

            pred_pred_err, best_pred_err = classifier.get_predicted_selected_to_best_error(t)
            best2selected_pred.append(pred_pred_err - best_pred_err)
            best2selected_act.append(pred_err - best_err)

#            full2selected.append(pred_err-)

        lmn = mean(predicted)
        lin.append((lmn, min(predicted), max(predicted)))
        lin_s.append([mean(p) for p in zip(*spred)])
        bst.append((mean(best), min(best), max(best)))
        ns.append((mean(ens), min(ens), max(ens)))
        ptas.append((mean(map(mean, pta)), std(list(map(mean, pta)))))
        pta_by_ens = list(zip(*pta))
        ptas_by_ens.append((list(map(mean, pta_by_ens)),
                            list(map(std, pta_by_ens)),
                            list(map(max, pta_by_ens)),
                            list(map(min, pta_by_ens))))

        ens_by_place.append(ens_places)

        best2selected_pred_dist.append(mean(best2selected_pred))
        best2selected_act_dist.append(mean(best2selected_act))
#        lin.append((lmn, lmn-std(predicted)/2, lmn+std(predicted)/2))
#        bst.append((mean(best), mean(best)-std(best)/2, mean(best)+std(best)/2))
#        ns.append((mean(ens), mean(ens)-std(ens)/2, mean(ens)+std(ens)/2))

#        bst.append((mean(best), percentile(best, 25), percentile(best, 75)))

    [lmean, l25, l75] = list(zip(*lin))
    [bmean, b25, b75] = list(zip(*bst))
    [emean, e25, e75] = list(zip(*ns))

    mean_err_by_place = list(zip(*lin_s))

    ###Mean error by training size
    plt.figure(figsize=[10,10])
    plt.title("Mean error by training size", fontsize=15)
    pline, = plt.plot(ts_sizes, lmean, "r-", label="Predicted ensemble", linewidth=2.0)
#    plt.fill_between(ts_sizes, l25, l75, facecolor="red", alpha=0.2)
    fline, = plt.plot(ts_sizes, emean, "b-", label="Full ensemble", linewidth=2.0)
#    plt.fill_between(ts_sizes, e25, e75, facecolor="blue", alpha=0.5)
    bline, = plt.plot(ts_sizes, bmean, "g-", label="Best ensemble", linewidth=2.0)
#    plt.fill_between(ts_sizes, b25, b75, facecolor="green", alpha=0.5)

    lines = []
#    for place, i in zip(mean_err_by_place, range(2, len(mean_err_by_place))):
#        line, = plt.plot(ts_sizes, place, "-", label="Predicted ensemble on {}".format(i))
#        lines.append(line)

    plt.ylabel("Mean RMS error, cm", fontsize=15)
    plt.xlabel("Training set size, forecast", fontsize=15)
    plt.legend(handles = [pline, fline, bline] + lines, fontsize=15)
    plt.show()
    plt.close()

    ###Mean error by training size
    [pta_mean, pta_std] = list(zip(*ptas))
    plt.figure(figsize=[10,10])

    plt.title("Mean error prediction error by training size")
    mean_line, = plt.plot(ts_sizes, pta_mean, "b-", label="Mean")
    pta_lower = [m-s for m,s in zip(pta_mean, pta_std)]
    pta_upper = [m+s for m,s in zip(pta_mean, pta_std)]
    plt.fill_between(ts_sizes[2:], pta_lower[2:], pta_upper[2:],
                         facecolor="blue", alpha=0.5)

    plt.ylabel("Mean error, sm")
    plt.xlabel("Training set size, forecast")
    plt.legend(handles=[mean_line])
    plt.show()
    plt.close()

    plt.figure(figsize=[14,12])
    plt.suptitle("Error prediction error by training size for each ensemble ")
    [pta_mean, pta_std, pta_max, pta_min] = zip(*ptas_by_ens)
    for mn, st, mi, mx, i in zip(zip(*pta_mean), zip(*pta_std),
                         zip(*pta_min), zip(*pta_max), range(8)):

        pta_lower =[m-s for m,s in zip(mn, st)]
        pta_upper =[m+s for m,s in zip(mn, st)]
        plt.subplot(3,3,i+1)

        plt.title("Ensemble {}".format(i+1))
        plt.plot(ts_sizes[2:], mn[2:])
        plt.fill_between(ts_sizes[2:], pta_lower[2:], pta_upper[2:],
                         facecolor="blue", alpha=0.5)
        plt.fill_between(ts_sizes[2:], mi[2:], mx[2:], facecolor="red", alpha=0.5)

        plt.ylim(0, 6)

    plt.show()
    plt.close()

    ###Fraction of predictions
    plt.figure(figsize=[10,10])
    plt.title("Fraction of predictions")
    plt.stackplot(ts_sizes, *(list(zip(*ens_by_place))), alpha=0.5)
    plt.ylabel("Count of predictions")
    plt.xlabel("Training set size, forecast")
    plt.show()
    plt.close()

    ###Mean error by training size
#    plt.figure(figsize=[10,10])
#    plt.title("Best to selected ensemble error distance")
#    pline, = plt.plot(ts_sizes, best2selected_pred_dist, label="Predicted")
#    aline, = plt.plot(ts_sizes, best2selected_act_dist, label="Actual")
#    plt.legend(handles=[pline, aline])
#    plt.ylabel("Mean error")
#    plt.xlabel("Training set size, forecast")
#    plt.show()
#    plt.close()

#    plt.suptitle("Mean error by history length\nvalidation set size={0}".format(validate_count),
#                        fontsize = 15)
#    for (errors_by_points_linear, errors_by_points_svr), i in zip(errors_by_ts, ts_sizes):
#        #plt.subplot(2,2, ts_sizes.index(i))
#        plt.title("Training set size={0}".format(i),
#                      fontsize=15)
#
#        l_line, = plt.plot(range(1, max_points), errors_by_points_linear, "-o", label="Linear regression")
#        #svr_line, = plt.plot(range(1, max_points), errors_by_points_svr, "-*", label="SVR")
#
#        #plt.legend(handles=[l_line, svr_line])
#
#        plt.ylim(3.25, 3.75)
#
#        plt.xlabel("Points", fontsize=12)
#        plt.ylabel("Mean error", fontsize=12)
#    plt.show()
#        ens_errors.append(mean(ens_list))
#        best_errors.append(mean(best_list))

#    fig = plt.figure(figsize=[7, 7])
#    plt.title("Error distribution for operative mode")
#
#    diffun = mean
#
#    plt.axhline(y=diffun(ml_list), linewidth = 1, color = '0.25',
#             linestyle = "--", label=median(ml_list))
#
#    plt.annotate("{0:.2f}".format(diffun(ml_list)),
#                 xy=(3, diffun(ml_list)), xytext=(3.33, diffun(ml_list)+0.1))
#
#    plt.axhline(y=diffun(ens_list), linewidth = 1, color = '0.25',
#             linestyle = "--", label=diffun(ml_list))
#
#    plt.annotate("{0:.2f}".format(diffun(ens_list)),
#                 xy=(3, diffun(ens_list)), xytext=(3-0.1, 0.1))
#
#    res = plt.boxplot(errors_by_time[:3], whis = 'range', showmeans = True)
#    plt.xticks([1,2,3], ["ml", "ens", "best"], fontsize = 12)
#
#    plt.xlabel("Ensemble selection approach", fontsize = 15)
#    plt.ylabel("Error", fontsize=15)
#    plt.show()
#    plt.close()

#    plt.figure(figsize=(10, 10))
#
#    plt.title("Validation set size={}".format(validate_count), fontsize = 15)
#
#    ml_line, = plt.plot(range(max_learn_count-1), ml_errors, label="Predicted")
#    ens_line, = plt.plot(range(max_learn_count-1), ens_errors, label="Ensemble")
#    best_lb, = plt.plot(range(max_learn_count-1), best_errors, label="Best")
#
#    plt.legend(handles=[ml_line, ens_line, best_lb], fontsize = 12)
#
#    plt.ylabel("Mean error", fontsize = 12)
#    plt.xlabel("Training set size", fontsize = 12)
#
#    plt.show()
#    plt.close()


#! /usr/bin/env python3

import tkinter as tk
from tkinter import filedialog

import urllib.request

import os

#class to act like argparse Namespace
class Namespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

def choose_data_files():
    # Ein Fenster erstellen
    fenster = tk.Tk()

    faelle = choose_a_file(fenster,"Wähle die Fälle.xls Datei aus",[("Excel Dateien","*.xlsx")])

    personen = choose_a_file(fenster,"Wähle die Personen.xls Datei aus",[("Excel Dateien","*.xlsx")])

    return faelle, personen

def choose_a_file(fenster,message,file_types):
    currdir = os.getcwd()
    tempdir = ''
    while not tempdir:
        tempdir = filedialog.askopenfilename(parent=fenster, initialdir=currdir, title=message, filetypes=file_types)
    return tempdir

def download_xlsx_parser(path):
    url = "https://github.com/dilshod/xlsx2csv/raw/master/xlsx2csv.py"
    # Download the file from `url` and save it locally under `file_name`:
    urllib.request.urlretrieve(url, path)

def xls_to_csv(xlsx_parser,xlsx_file,save_path):
    from xlsx2csv import Xlsx2csv as parser
    parser(xlsx_file, outputencoding="utf-8").convert(save_path,sheetid=2)

def remove_unnecessary_lines(file,skiplines):
    with open(file, 'r') as fin:
        data = fin.read().splitlines(True)
    with open(file, 'w') as fout:
        fout.writelines(data[skiplines:])

def start_analyze(faelle_csv,personen_csv,fachschaftenliste_md):
    import analyze as analyze
    args = Namespace(faelle_csv = faelle_csv, personen_csv=personen_csv,fachschaftenliste_md=fachschaftenliste_md)
    analyze.analyze(args)

def download_fachschaften_md(path):
    url = "https://raw.githubusercontent.com/HSZemi/vs-bonn/master/md/Ordnungen/FKGO.md"
    # Download the file from `url` and save it locally under `file_name`:
    urllib.request.urlretrieve(url, path)

    with open(path, 'r') as fin:
        data = fin.read()
        data = data.split('# Anlage Fachschaftenliste')[1]
    with open(path, 'w') as fout:
        fout.write(data)

if __name__ == "__main__":
    faelle_csv = 'faelle.csv'
    personen_csv = 'personen.csv'
    fachschaften_md = 'fachschaften_md.md'

    faelle,personen = choose_data_files()
    download_xlsx_parser("xlsx2csv.py")
    download_fachschaften_md(fachschaften_md)
    #convert fälle
    xls_to_csv("xlsx2csv.py",faelle,faelle_csv)

    #convert personen
    xls_to_csv("xlsx2csv.py",personen,personen_csv)

    remove_unnecessary_lines("faelle.csv",5)
    remove_unnecessary_lines("personen.csv",5)

    start_analyze(faelle_csv,personen_csv,fachschaften_md)


# uncompyle6 version 3.7.4
# Python bytecode 3.3 (3230)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Users/sklam/dev/llvmpy/dist/0.12.7/tools/intrgen.py
# Compiled at: 2014-08-01 13:34:49
# Size of source mod 2**32: 831 bytes
import sys

def gen(f, out=sys.stdout):
    intr = []
    maxw = 0
    flag = False
    for line in open(f):
        if line.startswith('#ifdef GET_INTRINSIC_ENUM_VALUES'):
            flag = True
        elif flag:
            if line.startswith('#endif'):
                break
            else:
                item = line.split()[0].replace(',', '')
                if len(item) > maxw:
                    maxw = len(item)
                intr.append(item)
                continue

    maxw = len('INTR_') + maxw
    idx = 1
    for i in intr:
        s = 'INTR_' + i.upper()
        out.write('%s = %d\n' % (s.ljust(maxw), idx))
        idx += 1

if __name__ == '__main__':
    gen(sys.argv[1])


from xai.brain.wordbase.nouns._communique import _COMMUNIQUE

#calss header
class _COMMUNIQUES(_COMMUNIQUE, ):
	def __init__(self,):
		_COMMUNIQUE.__init__(self)
		self.name = "COMMUNIQUES"
		self.specie = 'nouns'
		self.basic = "communique"
		self.jsondata = {}


import MeCab

from shlex import quote
from typing import Optional, List

from .word_storage import WordStorage
from .text_storage import TextStorage
from .neologd import normalize
from .word import Word

class MorphoAnalyzer(TextStorage):
    def __init__(self, storage: WordStorage, mecab_dicdir: Optional[str] = None, use_base: bool = False) -> None:
        node_format: str = '%m\\t%h\\n'
        if use_base:
            node_format = '%f[6]\\t%h\\n'
        args: List[str] = [
            '--node-format ' + node_format,
            '--unk-format ' + node_format,
            '--eos-format EOS\\t\\n',
        ]
        if mecab_dicdir is not None:
            args.append("-d")
            args.append(quote(mecab_dicdir))
        arg: str = ' '.join(args)
        self.tagger = MeCab.Tagger(arg)
        self.storage = storage
        self.tagger.parse('')

    def push(self, path: str, text: str) -> None:
        parsed = self.tagger.parse(normalize(text))
        for line in parsed.split('\n'):
            if line == '':
                continue
            base_format, feature_id = line.split('\t')
            if base_format == 'EOS':
                continue
            word = Word(word=base_format, feature_id=feature_id, weight=0)
            self.storage.push(path, word)


"""
uses iwlist regex parsing from https://github.com/iancoleman/python-iwlist

iwlist is part of the Linux iw suite
"""

from __future__ import annotations
import pprint
import re
import logging
import subprocess
import argparse
import datetime
import json
import time
from pathlib import Path

cellRe = re.compile(r"^Cell\s+(?P<cellnumber>.+)\s+-\s+Address:\s(?P<mac>.+)$")
dataRe = [
    re.compile(r"^ESSID:\"(?P<essid>.*)\"$"),
    re.compile(
        r"^Frequency:(?P<frequency>[\d.]+) (?P<frequency_units>.+) \(Channel (?P<channel>\d+)\)$"
    ),
    re.compile(
        r"^Quality=(?P<signal_quality>\d+)/(?P<signal_total>\d+)\s+Signal level=(?P<signal_level_dBm>.+) d.+$"
    ),
    re.compile(
        r"^Signal level=(?P<signal_quality>\d+)/(?P<signal_total>\d+).*$"
    ),
]

def scan(iface: str) -> str:
    """
    use command line to scan for Wifi networks
    """
    cmd = ["iwlist", iface, "scan"]
    ret = subprocess.run(cmd, text=True, stdout=subprocess.PIPE, timeout=15)

    return ret.stdout

def parse(raw: str) -> dict[str, dict[str, str]]:
    """
    parse iwlist scan output
    """

    aps: dict[str, dict[str, str]] = {}
    for line in raw.split("\n"):
        line = line.strip()

        cell = cellRe.search(line)
        if cell:
            bssid = cell.group(2)
            aps[bssid] = {}
            continue

        for dat in dataRe:
            r = dat.search(line)
            if r:
                aps[bssid].update(r.groupdict())

    return aps

if __name__ == "__main__":
    P = argparse.ArgumentParser(description="WiFi scanner using iw")
    P.add_argument("-N", default=10, type=int, help="number of scans")
    P.add_argument(
        "-i", "--interface", default="wlan0", help="WiFi interface to scan"
    )
    P.add_argument(
        "logpath", help="directory to write JSON output to", default="."
    )
    args = P.parse_args()

    FMT = "%Y-%m-%dT%H_%M_%S"

    print(f"Wifi scan: {args.N} loops")

    now = datetime.datetime.now().strftime(FMT)

    logpath = Path(args.logpath).expanduser()
    logpath.mkdir(parents=True, exist_ok=True)
    logfile = logpath / ("wifi_" + now + ".json")
    print("Logging data to", logfile)

    dat_all = {}
    jstr = ""

    for _ in range(args.N):
        now = datetime.datetime.now().strftime(FMT)

        raw = scan(iface=args.interface)

        aps = parse(raw)
        pprint.pprint(aps)

        if not aps:
            logging.error(f"no data recorded {now}")
            time.sleep(1)
            continue

        if logfile.is_file():
            jstr = logfile.read_text()
            dat_all = json.loads(jstr)

        dat_all[now] = aps

        jstr = json.dumps(dat_all) + "\n"
        # we add a line ending so the data isn't all on one line for all times
        logfile.write_text(jstr)

        # try not to overwhelm the iw command with too many scans
        time.sleep(1)


"""
  User: Liujianhan
  Time: 15:51
 """

class Solution:
    @staticmethod
    def is_valid(s):
        stack = []
        match = {'{': '}', '(': ')', '[': ']'}
        for i in s:
            if i in '({[':
                stack.append(i)
            else:
                if not stack:
                    return False
                top = stack.pop()
                if match[top] != i:
                    return False
        return len(stack) == 0

01-数据结构/字符串/020.有效的括号.py


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

"""
    yogosyu
    ~~~~~~~

    The Sphinx extension to provide yomigana to 'glossary'.

    :copyright: (c) 2011 by Suzumizaki-Kimitaka(about Additional works)
    :license: 2-clause BSD.
"""

"""The yogosyu extension, 'glossary' enhancement for Sphinx

This is the extension for Sphinx( http://sphinx.pocoo.org/ ).
To use this, read the usage of extensions with/for Sphinx.
And please make sure sort_order.py and any sort_order_XX.py
(both only you want use) can be imported from this extension.

You should also use the extension 'user_ordered_index_patch.py'
to make same sort order in 'genindex.html'.

This extension adds 'yogosyu' Directive.
The Directive bases on sphinx.domains.std.Glossary, and adds
Yomigana feature.

Yomigana is 'How to read kanzi (or so), written in Kana'.
Kanzi(kanji) is known CJK/CJKV Ideographs in Unicode Standard,
and Kana is known the set includes Hiragana and Katakana.

We should use Yomigana to sort Japanese words, instead of
using the code points of the Unicode.

In addtion, we should know the needs, each end-user may want
to use different sort order:
- Small form first or Normal form first(e.g. U+3041 and U+3042)
- How to order symbols and alphabets
- How to treat separating symbols
  - Simply ignore, go first or go last.
- Or native order in ANY language.

As shown last in above, 'sort order' feature can be required any
language, even Yomigana itself is required by only severals.

You can inherit SortOrderBase or SortOrderLegacyLike classes
provided with sort_order.py, and use as the generic extension.
When 'language' configure is XX in your conf.py and the file
named sort_order_XX.py exists, this module automatically
imports it.

If suitable extension not exist, this module sort the word
just Sphinx 1.0.7 or prior does by using SortOrderLegacy.

Kanzi, Hiragana, Katakana and Kana is all the words to talk
about Japanese, but ofcourse you can use this feature if you
have the needs.

Yomigana is given after 'yomimark' in the term, not some inline
markup or so, because there can be the case almost all entries
requires Yomigana, especially any *cultural* works.

'yomimark' is the option, so that it goes besides 'sorted'.

'yomimark' can be "single_char_or_whitespace_or_unicode".
the acceptable words are defined docutils.parsers.rst.\
directives.__init__.py like follows:
- one of the words: tab space
- any single unicode character except white-spaces
  - the character itself, or the code point represented by:
  - the decimal number
  - the hexadecimal number prefixed x, \\x, U+, u, or \\u
  - XML style like ☮

The every term is splited by yomimark. 1st is the term itself,
2nd is treated as Yomigana. Currently 3rd and follower is
simply ignored, but using this way, the 'mono ruby' may be
supported for the future. (see HTML ruby module for example.)

Not all the entry have to give yomigana. Such entries treat as
the name of entry and the yomigana of it are same string.

Yomigana itself is currently not shown in builded HTML files.
They're only used to sort.

Extension work is done by Suzumizaki-Kimitaka, May 2011.
"""

from docutils.parsers.rst import directives
from docutils.statemachine import ViewList
from docutils import nodes
from sphinx.util.compat import Directive
from sphinx import addnodes

_sort_order_obj = None # Place Holder, and internal only.

class Yogosyu(Directive):
    """Yomigana featured version of sphinx.domains.std.Glossary

    The option 'yomimark' is added.
    """

    # root base class Directive is defined docutils.parsers.rst.__init__.py.
    # option parser functions defined docutils.parsers.rst.directives.py.

    has_content = True
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = False
    option_spec = {
        'sorted': directives.flag,
        'yomimark': directives.single_char_or_whitespace_or_unicode
    }

    def run(self):
        """Inherit sphinx.util.compatDerective.run

        Base codes are copied from sphinx.domains.std.Glossary.
        """
        env = self.state.document.settings.env
        objects = env.domaindata['std']['objects']
        gloss_entries = env.temp_data.setdefault('gloss_entries', set())
        node = addnodes.glossary()
        node.document = self.state.document

        # This directive implements a custom format of the reST definition list
        # that allows multiple lines of terms before the definition.  This is
        # easy to parse since we know that the contents of the glossary *must
        # be* a definition list.

        # first, collect single entries
        entries = []
        in_definition = True
        was_empty = True
        messages = []
        for line, (source, lineno) in zip(self.content, self.content.items):
            # empty line -> add to last definition
            if not line:
                if in_definition and entries:
                    entries[-1][1].append('', source, lineno)
                was_empty = True
                continue
            # unindented line -> a term
            if line and not line[0].isspace():
                # first term of definition
                if in_definition:
                    if not was_empty:
                        messages.append(self.state.reporter.system_message(
                            2, 'glossary term must be preceded by empty line',
                            source=source, line=lineno))
                    entries.append(([(line, source, lineno)], ViewList()))
                    in_definition = False
                # second term and following
                else:
                    if was_empty:
                        messages.append(self.state.reporter.system_message(
                            2, 'glossary terms must not be separated by empty '
                            'lines', source=source, line=lineno))
                    entries[-1][0].append((line, source, lineno))
            else:
                if not in_definition:
                    # first line of definition, determines indentation
                    in_definition = True
                    indent_len = len(line) - len(line.lstrip())
                entries[-1][1].append(line[indent_len:], source, lineno)
            was_empty = False

        # now, parse all the entries into a big definition list
        items = []
        for terms, definition in entries:
            termtexts = []
            termnodes = []
            system_messages = []
            ids = []
            for line, source, lineno in terms:
                # split yomi from entry name
                yomi = line
                if 'yomimark' in self.options:
                    yomimark = self.options['yomimark']
                    line_and_yomi = line.split(yomimark)
                    line = line_and_yomi[0]
                    if len(line_and_yomi) > 1:
                        yomi = line_and_yomi[1]
                # parse the term with inline markup
                res = self.state.inline_text(line, lineno)
                system_messages.extend(res[1])

                # get a text-only representation of the term and register it
                # as a cross-reference target
                tmp = nodes.paragraph('', '', *res[0])
                termtext = tmp.astext()
                new_id = 'term-' + nodes.make_id(termtext)
                if new_id in gloss_entries:
                    new_id = 'term-' + str(len(gloss_entries))
                gloss_entries.add(new_id)
                ids.append(new_id)
                objects['term', termtext.lower()] = env.docname, (new_id, yomi)
                termtexts.append(termtext)
                # add an index entry too
                indexnode = addnodes.index()
                indexnode['entries'] = [('single', termtext, new_id, 'main')]
                termnodes.append(indexnode)
                termnodes.extend(res[0])
                termnodes.append(addnodes.termsep())
            # make a single "term" node with all the terms, separated by termsep
            # nodes (remove the dangling trailing separator)
            term = nodes.term('', '', *termnodes[:-1])
            term['ids'].extend(ids)
            term['names'].extend(ids)
            term += system_messages

            defnode = nodes.definition()
            self.state.nested_parse(definition, definition.items[0][1], defnode)

            items.append((termtexts,
                          nodes.definition_list_item('', term, defnode),
                          yomi))

        if 'sorted' in self.options:
            items.sort(key = self.get_sort_entry)

        dlist = nodes.definition_list()
        dlist['classes'].append('glossary')
        dlist.extend(item[1] for item in items)
        node += dlist
        return messages + [node]

    def get_sort_entry(self, entry):
        """Get the string to sort binded/instead of the given entry

        Called by run(), only when 'sorted' is given.
        """
        return _sort_order_obj.get_string_to_sort(entry[2])

# ========================================================
# Hacking functions
# ========================================================

def my_make_refnode(builder, fromdocname, todocname, targetid, child, title=None):
    """Shortcut to create a reference node

    This is hacking function to replace sphinx.util.nodes.make_refnode.
    """
    if isinstance(targetid, tuple):
        targetid = targetid[0]
    node = nodes.reference('', '', internal=True)
    if fromdocname == todocname:
        node['refid'] = targetid
    else:
        node['refuri'] = (builder.get_relative_uri(fromdocname, todocname)
                          + '#' + targetid)
    if title:
        node['reftitle'] = title
    node.append(child)
    return node

def my_std_domain_get_objects(self):
    """Collect and return iterators of objects

    This is hacking function to replace
    sphinx.domains.std.StandardDomain.get_objects.
    """
    for (prog, option), info in self.data['progoptions'].iteritems():
        yield (option, option, 'option', info[0], info[1], 1)
    for (type, name), info in self.data['objects'].iteritems():
        if isinstance(info[1], tuple):
            yield (name, name, type, info[0], info[1][0],
                   self.object_types[type].attrs['searchprio'])
        else:
            yield (name, name, type, info[0], info[1],
                   self.object_types[type].attrs['searchprio'])
    for name, info in self.data['labels'].iteritems():
        yield (name, info[2], 'label', info[0], info[1], -1)

# ========================================================
# Initializing functions
# ========================================================

def determine_sort_order(app):
    """Determine sort order used in this module

    Called by Sphinx, on the event 'bulider-inited'.
    """
    global _sort_order_obj
    import sort_order
    _sort_order_obj = sort_order.get_sort_order(app.config)
    if _sort_order_obj.__class__ == sort_order.SortOrderLegacy:
        print ('yogosyu.py: Using SortOrderLegacy.')
    return

def setup(app):
    """Extends Sphinx as we want

    Called from Sphinx to extend.
    @param app sphinx.application.Sphinx object to use add builder or so.
    """
    app.add_directive('yogosyu', Yogosyu)
    app.add_directive(u'用語集', Yogosyu) # 'yogosyu' written in original.
    app.connect('builder-inited', determine_sort_order)

    # make sure import sphinx.util.nodes first, because
    # domains.std refers make_refnode by using the form
    # 'from sphinx.util.nodes import make_refnode'
    import sphinx.util.nodes
    sphinx.util.nodes.make_refnode = my_make_refnode
    import sphinx.domains.std
    sphinx.domains.std.make_refnode = my_make_refnode
    sphinx.domains.std.StandardDomain.get_objects = my_std_domain_get_objects
    return


"""The asynchronous client is based on `Twisted <http://twistedmatrix.com/>`_, a very mature and powerful asynchronous programming framework. It supports destination specific message and error handlers (with default "poison pill" error handling), concurrent message processing, graceful shutdown, and connect and disconnect timeouts.

.. seealso:: `STOMP protocol specification <http://stomp.github.com/>`_, `Twisted API documentation <http://twistedmatrix.com/documents/current/api/>`_, `Apache ActiveMQ - Stomp <http://activemq.apache.org/stomp.html>`_

Examples
--------

.. automodule:: stompest.examples
    :members:

Producer
^^^^^^^^

.. literalinclude:: ../../stompest/examples/async/producer.py

Transformer
^^^^^^^^^^^

.. literalinclude:: ../../stompest/examples/async/transformer.py

Consumer
^^^^^^^^

.. literalinclude:: ../../stompest/examples/async/consumer.py

API
---
"""
import functools
import logging
import time

from twisted.internet import defer, task, reactor

from stompest.error import StompCancelledError, StompConnectionError, StompFrameError, StompProtocolError, \
    StompAlreadyRunningError
from stompest.protocol import StompSession, StompSpec
from stompest.util import checkattr, cloneFrame

from .protocol import StompProtocolCreator
from .util import InFlightOperations, exclusive, wait

LOG_CATEGORY = __name__

connected = checkattr('_protocol')

# TODO: is it ensured that the DISCONNECT frame is the last frame we send?

class Stomp(object):
    """An asynchronous STOMP client for the Twisted framework.

    :param config: A :class:`~.StompConfig` object.
    :param receiptTimeout: When a STOMP frame was sent to the broker and a **RECEIPT** frame was requested, this is the time (in seconds) to wait for the **RECEIPT** frame to arrive. If :obj:`None`, we will wait indefinitely.
    :param heartBeatThresholds: tolerance thresholds (relative to the negotiated heart-beat periods). The default :obj:`None` is equivalent to the content of the class atrribute :attr:`DEFAULT_HEART_BEAT_THRESHOLDS`. Example: ``{'client': 0.6, 'server' 2.5}`` means that the client will send a heart-beat if it had shown no activity for 60 % of the negotiated client heart-beat period and that the client will disconnect if the server has shown no activity for 250 % of the negotiated server heart-beat period.

    .. note :: All API methods which may request a **RECEIPT** frame from the broker -- which is indicated by the **receipt** parameter -- will wait for the **RECEIPT** response until this client's **receiptTimeout**. Here, "wait" is to be understood in the asynchronous sense that the method's :class:`twisted.internet.defer.Deferred` result will only call back then. If **receipt** is :obj:`None`, no such header is sent, and the callback will be triggered earlier.

    .. seealso :: :class:`~.StompConfig` for how to set configuration options, :class:`~.StompSession` for session state, :mod:`.protocol.commands` for all API options which are documented here.
    """
    _protocolCreatorFactory = StompProtocolCreator

    DEFAULT_ACK_MODE = 'client-individual'
    MESSAGE_FAILED_HEADER = 'message-failed'
    DEFAULT_HEART_BEAT_THRESHOLDS = {'client': 0.8, 'server': 2.0}

    def __init__(self, config, receiptTimeout=None, heartBeatThresholds=None):
        self._config = config
        self._receiptTimeout = receiptTimeout
        self._heartBeatThresholds = heartBeatThresholds or self.DEFAULT_HEART_BEAT_THRESHOLDS

        self._session = StompSession(self._config.version, self._config.check)
        self._protocol = None
        self._protocolCreator = self._protocolCreatorFactory(self._config.uri)

        self.log = logging.getLogger(LOG_CATEGORY)

        # wait for CONNECTED frame
        self._connecting = InFlightOperations('STOMP session negotiation')
        self._disconnecting = False

        # keep track of active handlers for graceful disconnect
        self._messages = InFlightOperations('Handler for message')
        self._receipts = InFlightOperations('Waiting for receipt')

        self._handlers = {
            'MESSAGE': self._onMessage,
            'CONNECTED': self._onConnected,
            'ERROR': self._onError,
            'RECEIPT': self._onReceipt,
        }
        self._subscriptions = {}

        self._heartBeats = {}

    @property
    def disconnected(self):
        """This :class:`twisted.internet.defer.Deferred` calls back when the connection to the broker was lost. It will err back when the connection loss was unexpected or caused by another error.
        """
        return self._disconnected

    @property
    def session(self):
        """The :class:`~.StompSession` associated to this client.
        """
        return self._session

    def sendFrame(self, frame):
        """Send a raw STOMP frame.

        .. note :: If we are not connected, this method, and all other API commands for sending STOMP frames except :meth:`~.async.client.Stomp.connect`, will raise a :class:`~.StompConnectionError`. Use this command only if you have to bypass the :class:`~.StompSession` logic and you know what you're doing!
        """
        self._protocol.send(frame)
        self.session.sent()

    #
    # STOMP commands
    #
    @exclusive
    @defer.inlineCallbacks
    def connect(self, headers=None, versions=None, host=None, heartBeats=None, connectTimeout=None, connectedTimeout=None):
        """connect(headers=None, versions=None, host=None, heartBeats=None, connectTimeout=None, connectedTimeout=None)

        Establish a connection to a STOMP broker. If the wire-level connect fails, attempt a failover according to the settings in the client's :class:`~.StompConfig` object. If there are active subscriptions in the :attr:`~.async.client.Stomp.session`, replay them when the STOMP connection is established. This method returns a :class:`twisted.internet.defer.Deferred` object which calls back with :obj:`self` when the STOMP connection has been established and all subscriptions (if any) were replayed. In case of an error, it will err back with the reason of the failure.

        :param versions: The STOMP protocol versions we wish to support. The default behavior (:obj:`None`) is the same as for the :func:`~.commands.connect` function of the commands API, but the highest supported version will be the one you specified in the :class:`~.StompConfig` object. The version which is valid for the connection about to be initiated will be stored in the :attr:`~.async.client.Stomp.session`.
        :param connectTimeout: This is the time (in seconds) to wait for the wire-level connection to be established. If :obj:`None`, we will wait indefinitely.
        :param connectedTimeout: This is the time (in seconds) to wait for the STOMP connection to be established (that is, the broker's **CONNECTED** frame to arrive). If :obj:`None`, we will wait indefinitely.

        .. note :: Only one connect attempt may be pending at a time. Any other attempt will result in a :class:`~.StompAlreadyRunningError`.

        .. seealso :: The :mod:`.protocol.failover` and :mod:`~.protocol.session` modules for the details of subscription replay and failover transport.
        """
        frame = self.session.connect(self._config.login, self._config.passcode, headers, versions, host, heartBeats)

        try:
            self._protocol
        except:
            pass
        else:
            raise StompConnectionError('Already connected')

        try:
            self._protocol = yield self._protocolCreator.connect(connectTimeout, self.session.version, self._onFrame, self._onConnectionLost)
        except Exception as e:
            self.log.error('Endpoint connect failed')
            raise

        self._disconnected = defer.Deferred()
        self._disconnectReason = None

        try:
            with self._connecting(None, self.log) as connected:
                self.sendFrame(frame)
                yield wait(connected, connectedTimeout, StompCancelledError('STOMP broker did not answer on time [timeout=%s]' % connectedTimeout))
        except Exception as e:
            self.log.error('Could not establish STOMP session. Disconnecting ...')
            yield self.disconnect(failure=e)

        self._replay()

        defer.returnValue(self)

    @connected
    def disconnect(self, receipt=None, failure=None, timeout=None):
        """disconnect(self, receipt=None, failure=None, timeout=None)

        Send a **DISCONNECT** frame and terminate the STOMP connection. This method returns a :class:`twisted.internet.defer.Deferred` object which calls back with :obj:`None` when the STOMP connection has been closed. In case of a failure, it will err back with the failure reason.

        :param failure: A disconnect reason (a :class:`Exception`) to err back. Example: ``versions=['1.0', '1.1']``
        :param timeout: This is the time (in seconds) to wait for a graceful disconnect, thas is, for pending message handlers to complete. If receipt is :obj:`None`, we will wait indefinitely.

        .. note :: The :attr:`~.async.client.Stomp.session`'s active subscriptions will be cleared if no failure has been passed to this method. This allows you to replay the subscriptions upon reconnect. If you do not wish to do so, you have to clear the subscriptions yourself by calling the :meth:`~.StompSession.close` method of the :attr:`~.async.client.Stomp.session`. Only one disconnect attempt may be pending at a time. Any other attempt will result in a :class:`~.StompAlreadyRunningError`. The result of any (user-requested or not) disconnect event is available via the :attr:`disconnected` property.
        """
        if self._disconnecting:
            raise StompAlreadyRunningError('Already disconnecting')
        self._disconnecting = True
        self._disconnect(receipt, failure, timeout)
        return self.disconnected

    @defer.inlineCallbacks
    def _disconnect(self, receipt, failure, timeout):
        if failure:
            self._disconnectReason = failure

        self.log.info('Disconnecting ...%s' % ('' if (not failure) else  ('[reason=%s]' % failure)))
        protocol = self._protocol
        try:
            # notify that we are ready to disconnect after outstanding messages are ack'ed
            if self._messages:
                self.log.info('Waiting for outstanding message handlers to finish ... [timeout=%s]' % timeout)
                try:
                    yield task.cooperate(iter([wait(handler, timeout, StompCancelledError('Going down to disconnect now')) for handler in self._messages.values()])).whenDone()
                except StompCancelledError as e:
                    self._disconnectReason = StompCancelledError('Handlers did not finish in time.')
                else:
                    self.log.info('All handlers complete. Resuming disconnect ...')

            if self.session.state == self.session.CONNECTED:
                frame = self.session.disconnect(receipt)
                try:
                    self.sendFrame(frame)
                except Exception as e:
                    self._disconnectReason = StompConnectionError('Could not send %s. [%s]' % (frame.info(), e))

                try:
                    yield self._waitForReceipt(receipt)
                except StompCancelledError:
                    self._disconnectReason = StompCancelledError('Receipt for disconnect command did not arrive on time.')

            protocol.loseConnection()

        except Exception as e:
            self._disconnectReason = e

    @connected
    @defer.inlineCallbacks
    def send(self, destination, body='', headers=None, receipt=None):
        """send(destination, body='', headers=None, receipt=None)

        Send a **SEND** frame.
        """
        self.sendFrame(self.session.send(destination, body, headers, receipt))
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def ack(self, frame, receipt=None):
        """ack(frame, receipt=None)

        Send an **ACK** frame for a received **MESSAGE** frame.
        """
        self.sendFrame(self.session.ack(frame, receipt))
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def nack(self, frame, receipt=None):
        """nack(frame, receipt=None)

        Send a **NACK** frame for a received **MESSAGE** frame.
        """
        self.sendFrame(self.session.nack(frame, receipt))
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def begin(self, transaction=None, receipt=None):
        """begin(transaction=None, receipt=None)

        Send a **BEGIN** frame to begin a STOMP transaction.
        """
        frame = self.session.begin(transaction, receipt)
        self.sendFrame(frame)
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def abort(self, transaction=None, receipt=None):
        """abort(transaction=None, receipt=None)

        Send an **ABORT** frame to abort a STOMP transaction.
        """
        frame = self.session.abort(transaction, receipt)
        self.sendFrame(frame)
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def commit(self, transaction=None, receipt=None):
        """commit(transaction=None, receipt=None)

        Send a **COMMIT** frame to commit a STOMP transaction.
        """
        frame = self.session.commit(transaction, receipt)
        self.sendFrame(frame)
        yield self._waitForReceipt(receipt)

    @connected
    @defer.inlineCallbacks
    def subscribe(self, destination, handler, headers=None, receipt=None, ack=True, errorDestination=None, onMessageFailed=None):
        """subscribe(destination, handler, headers=None, receipt=None, ack=True, errorDestination=None, onMessageFailed=None)

        Send a **SUBSCRIBE** frame to subscribe to a STOMP destination. This method returns a :class:`twisted.internet.defer.Deferred` object which will fire with a token when a possibly requested **RECEIPT** frame has arrived. The callback value is a token which is used internally to match incoming **MESSAGE** frames and must be kept if you wish to :meth:`~.async.client.Stomp.unsubscribe` later.

        :param handler: A callable :obj:`f(client, frame)` which accepts this client and the received :class:`~.StompFrame`.
        :param ack: Check this option if you wish the client to automatically ack MESSAGE frames when the were handled (successfully or not).
        :param errorDestination: If a frame was not handled successfully, forward a copy of the offending frame to this destination. Example: ``errorDestination='/queue/back-to-square-one'``
        :param onMessageFailed: You can specify a custom error handler which must be a callable with signature :obj:`f(self, failure, frame, errorDestination)`. Note that a non-trivial choice of this error handler overrides the default behavior (forward frame to error destination and ack it).

        .. note :: As opposed to the behavior of stompest 1.x, the client will not disconnect when a message could not be handled. Rather, a disconnect will only be triggered in a "panic" situation when also the error handler failed. The automatic disconnect was partly a substitute for the missing NACK command in STOMP 1.0. If you wish to automatically disconnect, you have to implement the **onMessageFailed** hook.
        """
        if not callable(handler):
            raise ValueError('Cannot subscribe (handler is missing): %s' % handler)
        frame, token = self.session.subscribe(destination, headers, receipt, {'handler': handler, 'errorDestination': errorDestination, 'onMessageFailed': onMessageFailed})
        ack = ack and (frame.headers.setdefault(StompSpec.ACK_HEADER, self.DEFAULT_ACK_MODE) in StompSpec.CLIENT_ACK_MODES)
        self._subscriptions[token] = {'destination': destination, 'handler': self._createHandler(handler), 'ack': ack, 'errorDestination': errorDestination, 'onMessageFailed': onMessageFailed}
        self.sendFrame(frame)
        yield self._waitForReceipt(receipt)
        defer.returnValue(token)

    @connected
    @defer.inlineCallbacks
    def unsubscribe(self, token, receipt=None):
        """unsubscribe(token, receipt=None)

        Send an **UNSUBSCRIBE** frame to terminate an existing subscription.

        :param token: The result of the :meth:`~.async.client.Stomp.subscribe` command which initiated the subscription in question.
        """
        frame = self.session.unsubscribe(token, receipt)
        try:
            self._subscriptions.pop(token)
        except:
            self.log.warning('Cannot unsubscribe (subscription id unknown): %s=%s' % token)
            raise
        self.sendFrame(frame)
        yield self._waitForReceipt(receipt)

    #
    # callbacks for received STOMP frames
    #
    def _onFrame(self, frame):
        self.session.received()
        if not frame:
            return
        try:
            handler = self._handlers[frame.command]
        except KeyError:
            raise StompFrameError('Unknown STOMP command: %s' % repr(frame))
        handler(frame)

    def _onConnected(self, frame):
        self.session.connected(frame)
        self.log.info('Connected to stomp broker [session=%s]' % self.session.id)
        self._connecting[None].callback(None)
        self._beats()

    def _onError(self, frame):
        if self._connecting:
            self._connecting[None].errback(StompProtocolError('While trying to connect, received %s' % frame.info()))
            return

        #Workaround for AMQ < 5.2
        if 'Unexpected ACK received for message-id' in frame.headers.get('message', ''):
            self.log.debug('AMQ brokers < 5.2 do not support client-individual mode')
        else:
            self.disconnect(failure=StompProtocolError('Received %s' % frame.info()))

    @defer.inlineCallbacks
    def _onMessage(self, frame):
        headers = frame.headers
        messageId = headers[StompSpec.MESSAGE_ID_HEADER]

        if self._disconnecting:
            self.log.info('[%s] Ignoring message (disconnecting)' % messageId)
            try:
                self.nack(frame)
            except StompProtocolError:
                pass
            defer.returnValue(None)

        try:
            token = self.session.message(frame)
            subscription = self._subscriptions[token]
        except:
            self.log.error('[%s] Ignoring message (no handler found): %s' % (messageId, frame.info()))
            defer.returnValue(None)

        with self._messages(messageId, self.log):
            try:
                yield subscription['handler'](self, frame)
                if subscription['ack']:
                    self.ack(frame)
            except Exception as e:
                try:
                    self._onMessageFailed(e, frame, subscription)
                except Exception as e:
                    if not self._disconnecting:
                        self.disconnect(failure=e)
                finally:
                    if subscription['ack']:
                        self.ack(frame)

    def _onReceipt(self, frame):
        receipt = self.session.receipt(frame)
        self._receipts[receipt].callback(None)

    #
    # hook for MESSAGE frame error handling
    #
    def _onMessageFailed(self, failure, frame, subscription):
        onMessageFailed = subscription['onMessageFailed'] or Stomp.sendToErrorDestination
        onMessageFailed(self, failure, frame, subscription['errorDestination'])

    def sendToErrorDestination(self, failure, frame, errorDestination):
        """sendToErrorDestination(failure, frame, errorDestination)

        This is the default error handler for failed **MESSAGE** handlers: forward the offending frame to the error destination (if given) and ack the frame. As opposed to earlier versions, It may be used as a building block for custom error handlers.

        .. seealso :: The **onMessageFailed** argument of the :meth:`~.async.client.Stomp.subscribe` method.
        """
        if not errorDestination:
            return
        errorFrame = cloneFrame(frame, persistent=True)
        errorFrame.headers.setdefault(self.MESSAGE_FAILED_HEADER, str(failure))
        self.send(errorDestination, errorFrame.body, errorFrame.headers)

    #
    # private properties
    #
    @property
    def _protocol(self):
        protocol = self.__protocol
        if not protocol:
            raise StompConnectionError('Not connected')
        return protocol

    @_protocol.setter
    def _protocol(self, protocol):
        self.__protocol = protocol

    @property
    def _disconnectReason(self):
        return self.__disconnectReason

    @_disconnectReason.setter
    def _disconnectReason(self, reason):
        if reason:
            self.log.error(str(reason))
            reason = self._disconnectReason or reason # existing reason wins
        self.__disconnectReason = reason

    #
    # private helpers
    #

    def _beat(self, which):
        try:
            self._heartBeats.pop(which).cancel()
        except:
            pass
        remaining = self._beatRemaining(which)
        if remaining < 0:
            return
        if not remaining:
            if which == 'client':
                self.sendFrame(self.session.beat())
                remaining = self._beatRemaining(which)
            else:
                self.disconnect(failure=StompConnectionError('Server heart-beat timeout'))
                return
        self._heartBeats[which] = reactor.callLater(remaining, self._beat, which) #@UndefinedVariable

    def _beatRemaining(self, which):
        heartBeat = {'client': self.session.clientHeartBeat, 'server': self.session.serverHeartBeat}[which]
        if not heartBeat:
            return -1
        last = {'client': self.session.lastSent, 'server': self.session.lastReceived}[which]
        elapsed = time.time() - last
        return max((self._heartBeatThresholds[which] * heartBeat / 1000.0) - elapsed, 0)

    def _beats(self):
        for which in ('client', 'server'):
            self._beat(which)

    def _createHandler(self, handler):
        @functools.wraps(handler)
        def _handler(_, result):
            return handler(self, result)
        return _handler

    def _onConnectionLost(self, reason):
        self._protocol = None
        self.log.info('Disconnected: %s' % reason.getErrorMessage())
        if not self._disconnecting:
            self._disconnectReason = StompConnectionError('Unexpected connection loss [%s]' % reason.getErrorMessage())
        self.session.close(flush=not self._disconnectReason)
        self._beats()
        for operations in (self._connecting, self._messages, self._receipts):
            for waiting in operations.values():
                if not waiting.called:
                    waiting.errback(StompCancelledError('In-flight operation cancelled (connection lost)'))
                    waiting.addErrback(lambda _: None)
        if self._disconnectReason:
            self.log.debug('Calling disconnected deferred errback: %s' % self._disconnectReason)
            self._disconnected.errback(self._disconnectReason)
        else:
            #self.log.debug('Calling disconnected deferred callback')
            self._disconnected.callback(None)
        self._disconnecting = False
        self._disconnectReason = None
        self._disconnected = None

    def _replay(self):
        for (destination, headers, receipt, context) in self.session.replay():
            self.log.info('Replaying subscription: %s' % headers)
            self.subscribe(destination, headers=headers, receipt=receipt, **context)

    @defer.inlineCallbacks
    def _waitForReceipt(self, receipt):
        if receipt is None:
            defer.returnValue(None)
        with self._receipts(receipt, self.log) as receiptArrived:
            timeout = self._receiptTimeout
            yield wait(receiptArrived, timeout, StompCancelledError('Receipt did not arrive on time: %s [timeout=%s]' % (receipt, timeout)))


# Lesson 6 writing .py files instead of jupyter notebooks

# IF RUNNING IN SPYDER SET BREAKPOINTS USING F12

import pandas as pd
import random
from sys import stdout
from time import sleep
import re

if __name__ == '__main__': # this line ensures we are in the main application

    raw = pd.read_pickle('WellGenData.pkl')
    df = raw.copy()
    rani = random.randint(0, len(df))
    #rani = 24020

    # Start your game here, replace enter some text with a question you want to ask the player
    game_over = False
    tries = 0
    while not game_over:
        tries += 1

        d = df.iloc[rani]
        api = d['API_NO14']
        wellcomp = d['WELL_COMP_NAME']
        wellcommon = d['WELL_COMMON_NAME']
        wellauto = d['WELL_AUTO_NAME']

        wellname = wellcomp

        input_wn = input('Try altering this wellname to fail up the program: \n {}, {}, {}, {}.\ninput: '.format(
                wellcomp, wellcommon, wellauto, api)) # allows player to enter some text or a number
        print('index: '+str(rani)+'\n')

        '''
        think = ['Thinking','.','.','.']
        for i in range(len(think)):
            print(think[i], sep=' ', end='', flush=True)
            sleep(1)
        print('\n')
        '''

        #Clean Master Reference
        df.fillna(value='EMPTY', inplace=True)
        df.apply(lambda x: x.astype(str).str.upper())
        subreplacements = {
            'SECTION': '',
            'SEC': '',
            ' ': ''
            }

        subreplacements2 = {
            '_RD.': '',
            '_R.': '',
            '_ST.': '',
            '-RD.': '',
            '-R.': '',
            '-ST.': '',
            ' RD.': '',
            ' R.': '',
            ' ST.': '',
            }

        replacements = {
           'WELL_COMP_NAME': subreplacements,
           'WELL_COMMON_NAME': subreplacements,
           'WELL_AUTO_NAME': subreplacements,
        }

        df.replace(replacements, regex=True, inplace=True)

        for k,v in subreplacements2.items():
            df['WELL_COMP_NAME']=df['WELL_COMP_NAME'].apply(lambda x: re.sub(k, v, x))
            df['WELL_COMMON_NAME']=df['WELL_COMMON_NAME'].apply(lambda x: re.sub(k, v, x))
            df['WELL_AUTO_NAME']=df['WELL_AUTO_NAME'].apply(lambda x: re.sub(k, v, x))

        df['WELL_COMP_NAME']=df['WELL_COMP_NAME'].apply(lambda x: re.sub('[^0-9a-zA-Z]+', '', x))
        df['WELL_COMMON_NAME']=df['WELL_COMMON_NAME'].apply(lambda x: re.sub('[^0-9a-zA-Z]+', '', x))
        df['WELL_AUTO_NAME']=df['WELL_AUTO_NAME'].apply(lambda x: re.sub('[^0-9a-zA-Z]+', '', x))

        # Check the input with an if statement
        if input_wn == '':
            print('Nothing given')
        else:

            temp_wn = input_wn.upper()
            for k,v in subreplacements.items():
                if k in temp_wn:
                    temp_wn = temp_wn.replace(k,v)

            for k,v in subreplacements2.items():
                temp_wn = re.sub(k,v, temp_wn)

            temp_wn = re.sub('[^0-9a-zA-Z]+', '', temp_wn)

            comp_check = df['WELL_COMP_NAME'][df['WELL_COMP_NAME'].str.contains(temp_wn)]
            common_check = df['WELL_COMMON_NAME'][df['WELL_COMMON_NAME'].str.contains(temp_wn)]
            auto_check = df['WELL_AUTO_NAME'][df['WELL_AUTO_NAME'].str.contains(temp_wn)]

            comp_check_raw = raw['WELL_COMP_NAME'][df['WELL_COMP_NAME'].str.contains(temp_wn)]
            common_check_raw = raw['WELL_COMMON_NAME'][df['WELL_COMMON_NAME'].str.contains(temp_wn)]
            auto_check_raw = raw['WELL_AUTO_NAME'][df['WELL_AUTO_NAME'].str.contains(temp_wn)]

            found = 0

            if not comp_check.empty and found<=0:
                for indx, val in comp_check.iteritems():
                    if len(val) == len(temp_wn):
                        print('Well Comp Name match found:')
                        print (indx, comp_check_raw[indx])
                        print('\nYou fail! Game Over')
                        found +=1
                    else:
                        pass

            if not common_check.empty and found<=0:
                for indx, val in common_check.iteritems():
                    if len(val) == len(temp_wn):
                        print('Well Common Name match found:')
                        print (indx, common_check_raw[indx])
                        print('\nYou fail! Game Over')
                        found +=1
                    else:
                        pass

            if not auto_check.empty and found<=0:
                for indx, val in auto_check.iteritems():
                    if len(val) == len(temp_wn):
                        print('Well Auto Name match found:')
                        print (indx, auto_check_raw[indx])
                        print('\nYou fail! Game Over')
                        found +=1
                    else:
                        pass

            if found<=0:
                print('No Match. You Win! Game Over')

            game_over = True

        if tries == 5:
            game_over = True
            print('You took too long, Game Over')

        # Your task: make a 2-3 step text game with the starting code up above and have your neighbor try playing it


N = int(input())
t = list(map(int,input().split()))
v = list(map(int,input().split()))

v.append(0)
interval = 0.5
total_t = sum(t)
now_t = 0
now_v = 0
T = [sum(t[:i]) for i in range(1,N+1)]
now_n = 0
d = 0
d_int = 0
while now_t < total_t:
	nxt_t, nxt_v = T[now_n], v[now_n + 1]
	delta_t, delta_v = nxt_t - now_t, nxt_v - now_v
	rest_t = [T[i]-now_t for i in range(N)]
	if now_v < v[now_n]:
		if delta_t + delta_v == 0:
			a = -1
		else:
			a = 1
	elif now_v == v[now_n]:
		if delta_t + delta_v == 0:
			a = -1
		else:
			a = 0
	for i in range(N):
		rest = rest_t[i]
		if rest > 0:
			d_v = now_v - v[i+1]
			if rest == d_v:
				a = -1

	d_int += 2 * int(now_v * 2) + a
	d += now_v * interval + 1/2 * a * interval * interval
	now_t += interval
	now_v += a * interval
	if now_t == T[now_n]:
		now_n += 1

print(d)


# Simply Good Scanner (SGS)
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import validators
import webbrowser
import pyperclip

# Global variables
data = []

def work(num):
    cap = cv2.VideoCapture(num, cv2.CAP_DSHOW)
    cap.set(3, 640)
    cap.set(4, 480)

    try:
        while True:
            success, img = cap.read()
            for barcode in decode(img):
                myData = barcode.data.decode('utf-8')
                pts = np.array([barcode.polygon], np.int32)
                pts = pts.reshape((-1, 1, 2))
                cv2.polylines(img, [pts], True, (0,255,0), 5)
                pts2 = barcode.rect
                cv2.putText(img, myData, (pts2[0], pts2[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2)
                data.append(myData)
            cv2.imshow('Result', img)
            cv2.waitKey(1)
    except KeyboardInterrupt as e:
        cap.release()
        cv2.destroyAllWindows()

def startFunction():
    value = clicked.get()
    if value == "Select Source":
        messagebox.showerror("Source Error", "Select a source before clicking start you fool")
    elif value == "External Camera":
        work(1)
    elif value == "Laptop Camera":
        work(0)

def openFunction():
    dataList = list(set(data))
    for d in dataList:
        if validators.url(d):
            webbrowser.open(d)

def copyFunction():
    dataList = list(set(data))
    pyperclip.copy('\n'.join(dataList))

def showFunction():
    dataList = list(set(data))
    messagebox.showinfo("Output", '\n'.join(dataList))

def getOption(s):
    clicked.set(s)

# -- Windows only configuration --
try:
    from ctypes import windll
    windll.shcore.SetProcessDpiAwareness(1)
except:
    pass
# -- End Windows only configuration --

root = tk.Tk()

# This is the section of code which creates the main window
root.geometry("600x550")
root.title("Simply Good Scanner")
root.resizable(0, 0)

# Title
ttk.Label(root, text='Simply Good Scanner',
          font=("Comic Sans MS", 30)).place(x=100, y=20)

# Source Label
ttk.Label(root, text='Source', font=("Comic Sans MS", 20)).place(x=80, y=110)

# Options

options = ["Select Source", "Laptop Camera", "External Camera"]
clicked = tk.StringVar(value=options[0])

# Create Dropdown menu
drop = tk.OptionMenu(root, clicked, *options, command=getOption)
drop.place(x=320, y=100)
drop.config(font=("Comic Sans MS", 20))
menu = root.nametowidget(drop.menuname)
menu.config(font=("Comic Sans MS", 14))

# start button
startBtn = tk.Button(root,
                     text='Start',
                     command=startFunction,
                     font=("Comic Sans MS", 20))
startBtn.place(x=250, y=200)

# Open in browser button
openBtn = tk.Button(root,
                    text='Open only urls',
                    command=openFunction,
                    font=("Comic Sans MS", 20))
openBtn.place(x=200, y=280)
# Copy to clipboard button
copyBtn = tk.Button(root,
                    text='Copy to clipboard',
                    command=copyFunction,
                    font=("Comic Sans MS", 20))
copyBtn.place(x=180, y=360)
# Show data button
showBtn = tk.Button(root,
                    text='Show Data',
                    command=showFunction,
                    font=("Comic Sans MS", 20))
showBtn.place(x=220, y=440)

root.mainloop()


"""empty message

Revision ID: 3546f9ff35de
Revises: 2ecc16058eb2
Create Date: 2015-06-08 13:55:15.973308

"""

# revision identifiers, used by Alembic.
revision = '3546f9ff35de'
down_revision = '2ecc16058eb2'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('task', sa.Column('description', sa.String(length=120), nullable=True))
    op.add_column('task', sa.Column('difficulty', sa.Integer(), nullable=True))
    op.add_column('task', sa.Column('done', sa.Boolean(), nullable=True))
    op.add_column('task', sa.Column('end_date', sa.Date(), nullable=True))
    op.add_column('task', sa.Column('priority', sa.Integer(), nullable=True))
    op.add_column('task', sa.Column('start_date', sa.Date(), nullable=True))
    op.add_column('task', sa.Column('title', sa.String(length=200), nullable=True))
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('task', 'title')
    op.drop_column('task', 'start_date')
    op.drop_column('task', 'priority')
    op.drop_column('task', 'end_date')
    op.drop_column('task', 'done')
    op.drop_column('task', 'difficulty')
    op.drop_column('task', 'description')
    ### end Alembic commands ###


import pandas as pd
import numpy as np
import math

data = pd.read_csv("C:/Users/DOWEN1/Desktop/ABETAreversal.csv")
data1 = data[["AnimalID", "Age", "Treatment GP", "Treat ComBO"]]
del data["AnimalID"], data["Age"], data["Treatment GP"], data["Treat ComBO"]
for i in range(49, 90):
	data[str(i)] = data[str(i)] + 2
print(data)
data = data.apply(np.log10, axis=0)
print(data)

data = pd.concat([data1, data], axis=1)
data.to_csv("C:/Users/DOWEN1/Desktop/ABETAreversalLOGTRANSFORMED.csv")


#!/usr/bin/env python
# pylint: disable=C0116
# This program is dedicated to the public domain under the CC0 license.

"""
Simple Bot to reply to Telegram messages.
First, a few handler functions are defined. Then, those functions are passed to
the Dispatcher and registered at their respective places.
Then, the bot is started and runs until we press Ctrl-C on the command line.
Usage:
Basic Echobot example, repeats messages.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""

import logging
import datetime
import sqlite3

from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments update and
# context.
def start(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    user = update.effective_user
    update.message.reply_markdown_v2(
        f'Hi {user.mention_markdown_v2()}\!',
        reply_markup=ForceReply(selective=True),
    )

def help_command(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('Este es el comado de ayuda')

def echo(update: Update, _: CallbackContext) -> None:
    """Echo the user message."""
    update.message.reply_text(update.message.text)

def pole(update: Update, _: CallbackContext) -> None:
    """Pole para AP"""
    weekno = datetime.datetime.today().weekday()
    now = datetime.datetime.now()
    hour = now.hour

    db = sqlite3.connect("data.db")
    cursor = db.cursor()

    #sacamos las fecha de consulta
    fecha_ant = "'" + str(now.year) + '-' + str(now.month) + '-' + str(now.day) + "'"
    fecha_act = datetime.datetime(now.year, now.month, (now.day+1))

    # if weekno < 7:
    if weekno < 5 and now.hour>=5:
        if(update.message.text.lower()=="pole"):
            user = update.effective_user
            #Validamos que no se haya hecho la pole
            datos = cursor.execute("SELECT * FROM poles WHERE pole='pole' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
            if len(datos.fetchall())==0:
                registros = ('pole',user.mention_markdown_v2(),3.5,now)
                datos = cursor.execute("INSERT INTO poles(pole,user,points,date) VALUES(?,?,?,?)",registros)
                db.commit()
                update.message.reply_markdown_v2(
                    f'Erda bien y tal, {user.mention_markdown_v2()}\ ha hecho la pole',
                )
        elif (update.message.text.lower()=="plata"):
            user = update.effective_user
            # Validamos que se haya hecho la pole
            datos = cursor.execute("SELECT * FROM poles WHERE pole='pole' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
            if len(datos.fetchall())>0:
                # Validamos que no se haya hecho una plata
                datos = cursor.execute("SELECT * FROM poles WHERE pole='plata' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
                if len(datos.fetchall())==0:
                    #Validamos que si este usuario ya hizo alguna position, no puede hacer plata
                    datos = cursor.execute("SELECT * FROM poles WHERE user='"+str(user.mention_markdown_v2())+"' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
                    if len(datos.fetchall())==0:
                        registros = ('plata',user.mention_markdown_v2(),2,now)
                        datos = cursor.execute("INSERT INTO poles(pole,user,points,date) VALUES(?,?,?,?)",registros)
                        db.commit()
                        update.message.reply_markdown_v2(
                            f'muy bien, {user.mention_markdown_v2()}\ ha hecho la plata',
                        )
        elif (update.message.text.lower()=="bronce"):
            user = update.effective_user
            # Validamos que se haya hecho la plata
            datos = cursor.execute("SELECT * FROM poles WHERE pole='plata' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
            if len(datos.fetchall())>0:
                # Validamos que no se haya hecho una plata
                datos = cursor.execute("SELECT * FROM poles WHERE pole='bronce' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
                if len(datos.fetchall())==0:
                    #Validamos que si este usuario ya hizo alguna position, no puede hacer plata
                    datos = cursor.execute("SELECT * FROM poles WHERE user='"+str(user.mention_markdown_v2())+"' AND date >=  '" +str(now.year) + "-" + str(now.month).zfill(2) + "-" + str(now.day).zfill(2) + "'")
                    if len(datos.fetchall())==0:
                        registros = ('bronce',user.mention_markdown_v2(),0.5,now)
                        datos = cursor.execute("INSERT INTO poles(pole,user,points,date) VALUES(?,?,?,?)",registros)
                        db.commit()
                        update.message.reply_markdown_v2(
                            f'Algo es algo, {user.mention_markdown_v2()}\ ha conseguido el bronce',
                        )
        elif (update.message.text.lower()=="fail"):
            user = update.effective_user
            registros = ('fail',user.mention_markdown_v2(),0,now)
            datos = cursor.execute("INSERT INTO poles(pole,user,points,date) VALUES(?,?,?,?)",registros)
            db.commit()
            update.message.reply_markdown_v2(
                f'Al menos lo intento, {user.mention_markdown_v2()}\ ha conseguido un Fail',
            )
    elif (update.message.text.lower()=="poleprueba" or update.message.text.lower()=="plataprueba" or update.message.text.lower()=="bronceprueba" or update.message.text.lower()=="fail"):
        user = update.effective_user
        update.message.reply_markdown_v2(
            f'Deja de molestarme {user.mention_markdown_v2()} que estas no son horas de estar haciendo {update.message.text.lower()}',
        )

def main() -> None:
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    updater = Updater("1730057456:AAHQ99r1lHoiEmKdl2NpVcza17hipO-lI_A")

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on different commands - answer in Telegram
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))

    # on non command i.e message - echo the message on Telegram
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, pole))
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()


from django.shortcuts import render
from django.core.mail import EmailMessage
from django.http import HttpResponse
from django.conf import settings
from django.template.loader import render_to_string

def home(request):
  return render(request, 'base/index.html')

def sendEmail(request):
  if request.method == 'POST':

    template = render_to_string('base/email_template.html', {
      'name': request.POST['name'],
      'email': request.POST['email'],
      'message': request.POST['message'],
    })

    email = EmailMessage(
      request.POST['subject'],
      template,
      settings.EMAIL_HOST_USER,
      ['yahao_yan@outlook.com']
    )

    email.send()

    return HttpResponse(status=204)


import RPi.GPIO as GPIO
import tweepy
import os
from time import sleep
from credentials import *
import sqlite3
from textblob import TextBlob
import time

GPIO.setmode(GPIO.BOARD)
servoPin=11
GPIO.setup(servoPin, GPIO.OUT)
pwm=GPIO.PWM(servoPin,50)
db = sqlite3.connect('database.db')
cursor = db.cursor()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
pwm.start(7)

for tweet in tweepy.Cursor(api.search, q="china").items():
    if (not tweet.retweeted) and ('RT @' not in tweet.text):
        try:
            print(tweet.user.screen_name)
            print((tweet.text).encode('utf-8').strip())
            user = tweet.user.screen_name
            tweet_text = tweet.text
            tweet_time = tweet.created_at
            blob = TextBlob(tweet.text)
            for sentence in blob.sentences:
                print(sentence.sentiment.polarity)
                cursor.execute('''INSERT INTO tweet(created_at, tweet_sentiment)
                        VALUES(?,?)''', (tweet_time, sentence.sentiment.polarity))
            db.commit()
            sleep(3)

            range_of_motion = (((sentence.sentiment.polarity + 1) * 180) / 2)
            DC=1./18.*(range_of_motion)+2
            pwm.ChangeDutyCycle(DC)

        except tweepy.TweepError as e:
            print(e.reason)
        except StopIteration:
            break

        sleep(3)

pwm.stop()
GPIO.cleanup()


from tkinter import *

root=Tk()
lab1 = Label(text="ПРОСНИСЬ И ПОЙ", bg = 'LightCyan')

button1 = Button(text="Оперетта", bg = 'Green')

lab1.grid()

button1.grid(sticky='n')
root.mainloop()
# добавьте в программу еще три кнопки и два виджета Label


file=open("text.txt","r")
num_words = 0
num_lines = 0
num_charc = 0
num_spaces = 0

for line in file:
    num_lines+=1

    word = 'Y'
    for letter in line:
        if (letter != ' ' and word == 'Y'):
            num_words += 1
            word = 'N'
        elif (letter == ' '):
            num_spaces += 1
            word = 'Y'
            for i in letter:
                if (i != " " and i != "\n"):
                    num_charc += 1
print("Number of words in text file: ", num_words)

# printing total line count
print("Number of lines in text file: ", num_lines)

# printing total character count
print('Number of characters in text file: ', num_charc)

# printing total space count
print('Number of spaces in text file: ', num_spaces)

Programs/File Handling/Get number of characters words spaces in file.py


t = int(input())
dir = [(-1, 0), (1, 0), (0, -1), (0, 1)]  # 상 하 좌 우

def dfs(r, c, is_cut, route):
    global max_route

    if route > max_route:
        max_route = route

    visited[r][c] = 1

    for d in dir:
        nr, nc = r + d[0], c + d[1]
        if not 0 <= nr < n or not 0 <= nc < n:
            continue

        if not visited[nr][nc]:
            if field[r][c] > field[nr][nc]:
                visited[nr][nc] = 1
                dfs(nr, nc, is_cut, route+1)
                visited[nr][nc] = 0

            else:
                if not is_cut:
                    for cut in range(1, k+1):
                        field[nr][nc] -= cut
                        is_cut = 1
                        if field[r][c] > field[nr][nc]:
                            visited[nr][nc] = 1
                            dfs(nr, nc, is_cut, route+1)
                            visited[nr][nc] = 0
                        is_cut = 0
                        field[nr][nc] += cut

def max_h_idx(h):
    res = []
    for r in range(n):
        for c in range(n):
            if field[r][c] == h:
                res.append((r, c))
    return res

for i in range(1, t + 1):
    n, k = map(int, input().split())
    field = []
    max_route = 0
    for _ in range(n):
        field.append(list(map(int, input().split())))

    max_h = max(map(max, field))
    for r, c in max_h_idx(max_h):
        visited = [[0] * n for _ in range(n)]
        dfs(r, c, 0, 1)
    print("#{} {}".format(i, max_route))


"""
给定一个二叉树，返回其按层次遍历的节点值。 （即逐层地，从左到右访问所有节点）。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果：

[
  [3],
  [9,20],
  [15,7]
]
"""
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        rt = []
        queue = [root]
        while queue:
            queue, rt_level = self.getNextLevel(queue, [], [])
            rt.append(rt_level) if rt_level else None
        return rt

    def getNextLevel(self, queue, next_queue, rt_level):

        while queue:
            node = queue.pop(0)
            if node:
                rt_level.append(node.val)
                next_queue.append(node.left)
                next_queue.append(node.right)

        return next_queue, rt_level


# -*- coding: utf-8 -*-
import logging

from flask import Flask

app = Flask(__name__)

@app.route('/')
def root():
    app.logger.info('info log')
    app.logger.warning('warning log')
    return 'hello'

if __name__ == '__main__':
    app.debug = True

    handler = logging.FileHandler('demo2.log', encoding='UTF-8')

    handler.setLevel(logging.DEBUG)

    logging_format = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'
    )

    handler.setFormatter(logging_format)

    app.logger.addHandler(handler)

    app.run()


import gym
import time
import numpy as np

from abp import HRAAdaptive
from abp.utils import clear_summary_path
from abp.explanations import PDX
from tensorboardX import SummaryWriter
from gym.envs.registration import register
from abp.openai.envs.four_towers_multi_unit_pysc2.FourTowerSequential import FourTowerSequentialMultiUnit

from absl import app
from absl import flags
from collections import namedtuple
import datetime
import time
import sys
import numpy as np
import pandas as pd
import csv
import json
import imutil

def run_task(evaluation_config, network_config, reinforce_config):
    import absl
    absl.flags.FLAGS(sys.argv[:1])
    env = FourTowerSequentialMultiUnit()

    # env = gym.make(evaluation_config.env)
    max_episode_steps = 100
    state = env.reset()
    # print(state)
    choices = [0,1,2,3]
    pdx_explanation = PDX()

    reward_types = ['damageToZealot', 'damageToZergling', 'damageToRoach', 'damageToStalker', 'damageToMarine', 'damageToHydralisk']

    agent = HRAAdaptive(name = "FourTowerSequential",
                        choices = choices,
                        reward_types = reward_types,
                        network_config = network_config,
                        reinforce_config = reinforce_config)

    training_summaries_path = evaluation_config.summaries_path + "/train"
    clear_summary_path(training_summaries_path)
    train_summary_writer = SummaryWriter(training_summaries_path)

    test_summaries_path = evaluation_config.summaries_path + "/test"
    clear_summary_path(test_summaries_path)
    test_summary_writer = SummaryWriter(test_summaries_path)

    totalDamageToZealot = 0
    totalDamageToZergling = 0
    totalDamageToRoach = 0
    totalDamageToStalker = 0
    totalDamageToMarine = 0
    totalDamageToHydralisk = 0

    # Training Episodes
    for episode in range(evaluation_config.training_episodes):
        state = env.reset()
        total_reward = 0
        done = False
        dead = False
        deciding = True
        running = True
        steps = 0
        rewards = []

        initial_state = np.array(state)

        while deciding:
            steps += 1
            action, q_values = agent.predict(state)
            state, reward, done, dead, info = env.step(action)

            while running:
                action = 4
                state, reward, done, dead, info = env.step(action)
                if done:
                    break

            if not dead:
                rewards = {'damageToZealot': env.decomposed_rewards[len(env.decomposed_rewards) - 1][0], 'damageToZergling': env.decomposed_rewards[len(env.decomposed_rewards) - 1][1], 'damageToRoach': env.decomposed_rewards[len(env.decomposed_rewards) - 1][2], 'damageToStalker': env.decomposed_rewards[len(env.decomposed_rewards) - 1][3], 'damageToMarine': env.decomposed_rewards[len(env.decomposed_rewards) - 1][4], 'damageToHydralisk': env.decomposed_rewards[len(env.decomposed_rewards) - 1][5]}

            else:
                rewards = {'damageToZealot': env.decomposed_rewards[len(env.decomposed_rewards) - 2][0], 'damageToZergling': env.decomposed_rewards[len(env.decomposed_rewards) - 2][1], 'damageToRoach': env.decomposed_rewards[len(env.decomposed_rewards) - 2][2], 'damageToStalker': env.decomposed_rewards[len(env.decomposed_rewards) - 2][3], 'damageToMarine': env.decomposed_rewards[len(env.decomposed_rewards) - 2][4], 'damageToHydralisk': env.decomposed_rewards[len(env.decomposed_rewards) - 2][5]}

            for reward_type in rewards.keys():
                agent.reward(reward_type, rewards[reward_type])

            total_reward += rewards['damageToZealot'] + rewards['damageToZergling'] + rewards['damageToRoach'] + rewards['damageToStalker'] + rewards['damageToMarine'] + rewards['damageToHydralisk']

            if dead:
                break

        totalDamageToZealot += rewards['damageToZealot']
        totalDamageToZergling += rewards['damageToZergling']
        totalDamageToRoach += rewards['damageToRoach']
        totalDamageToStalker += rewards['damageToStalker']
        totalDamageToMarine += rewards['damageToMarine']
        totalDamageToHydralisk += rewards['damageToHydralisk']

        print("Damage to Zealot: {}".format(totalDamageToZealot))
        print("Damage to Zergling: {}".format(totalDamageToZergling))
        print("Damage to Roach: {}".format(totalDamageToRoach))
        print("Damage to Stalker: {}".format(totalDamageToStalker))
        print("Damage to Marine: {}".format(totalDamageToMarine))
        print("Damage to Hydralisk: {}".format(totalDamageToHydralisk))

        agent.end_episode(state)
        test_summary_writer.add_scalar(tag="Train/Episode Reward", scalar_value=total_reward,
                                       global_step=episode + 1)
        train_summary_writer.add_scalar(tag="Train/Steps to collect all Fruits", scalar_value=steps + 1,
                                        global_step=episode + 1)

        print("EPISODE REWARD {}".format(total_reward))
        print("EPISODE {}".format(episode))

    agent.disable_learning()

    # Test Episodes
    for episode in range(evaluation_config.test_episodes):
        state = env.reset()
        total_reward = 0
        done = False
        steps = 0
        deciding = True
        running = True

        while deciding:
            steps += 1
            action, q_values = agent.predict(state)
            print(action)
            print(q_values)

            if evaluation_config.render:
                # env.render()
                pdx_explanation.render_all_pdx(action, 4, q_values, ['Top_Left', 'Top_Right', 'Bottom_Left', 'Bottom_Right'], ['damageToZealot', 'damageToZergling', 'damageToRoach', 'damageToStalker', 'damageToMarine', 'damageToHydralisk'])
                time.sleep(evaluation_config.sleep)
                # This renders an image of the game and saves to test.jpg
                # imutil.show(self.last_timestep.observation['rgb_screen'], filename="test.jpg")

            state, reward, done, dead, info = env.step(action)

            while running:
                action = 4
                state, reward, done, dead, info = env.step(action)
                if done:
                    break

            if dead:
                break

        agent.end_episode(state)

        test_summary_writer.add_scalar(tag="Test/Episode Reward", scalar_value=total_reward,
                                       global_step=episode + 1)
        test_summary_writer.add_scalar(tag="Test/Steps to collect all Fruits", scalar_value=steps + 1,
                                       global_step=episode + 1)


import paho.mqtt.publish as publish
import time
import serial

sensor_types={'38':'VOLTAGE','39':'CURRENT','25':'ENERGY','24':'POWER_COEFF','17':'POWER' }
nodes={'1':'CorridorNode'}
mqtt_host = 'localhost'
mqtt_data_prefix = '/data/nodes/'
serial_device_name='/dev/ttyMySensorsGateway'

while True:
    try:
        with serial.Serial(serial_device_name, 115200) as ser:
            while True:
                line=ser.readline().rstrip()
                if not ';' in line: continue
                print (line)
                data=line.split(';')
                if data[1]==255:
                    if data[4]=='11':
                        nodes[data[0]] = data[5]
                else:
                    print ("[%s] %s%s/%s : %s" %(time.ctime(),mqtt_data_prefix, nodes.get(data[0],data[0]),sensor_types.get(data[4],'MISC'),data[5]))
                    publish.single('%s%s/%s' % (mqtt_data_prefix, nodes.get(data[0],data[0]),sensor_types.get(data[4],'MISC')),data[5],1)
    except:
        print ('[%s] %s' % (time.ctime(),'Cos sie wywalilo'))
        time.sleep(5)


'''
Nombre de archivo:
  +programaestudiante.py
Descripción:
  +Archivo principal (main) para la ejecución del programa
'''

#librerías necesarias
import sys, os
from procesamientodatos import *

#llamado a las diferentes funciones para el procesamiento de los datos por etapas (stage#)
stage1 = cargar_datos(sys.argv)
stage2 = unir_datos(stage1, select=['fecha','nombre','provincia','nombre_ruta','kms'])
stage3 = agregar_datos(stage2, group=['provincia','nombre'], agg='kms')
stage4 = presentar_datos(stage3, top=5, part='provincia', order='sum(kms)', show=50)
stage5 = almacenar_datos(stage4, nombre='resultados.csv', show=50)
#


from django.http import HttpResponse
from django.shortcuts import render
from django.template import Template,Context
from django.shortcuts import render_to_response
from mysite.books.models import Book
def display_meta(request):
	meta = request.META.items()
	meta.sort()
	return render_to_response('meta.html',locals())

def search(request):
	errors = []
	if 'q' in request.GET:
		q = request.GET['q']
		if not q:
			errors.append('Enter a search term.')
		elif len(q)>20:
			errors.append('Please enter at most 20 characters.')
		else:
			books = Book.objects.filter(title__icontains=q)
			return render_to_response('search_results.html',{'books':books,'query':q})
	return render_to_response('search_form.html',{'errors':errors})


import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)

if (cap.isOpened() == False):
    print("Unable to read camera feed")

is_End = False

# pts1 = np.float32([[90,65],[0,230],[570,75],[640,240]])

# pts2 = np.float32([[10,10],[10,290],[290,10],[290,290]])

# M = cv2.getPerspectiveTransform(pts1, pts2)

pre_frame = cv2.imread('pre_frame.png')
pre_frame = cv2.cvtColor(pre_frame, cv2.COLOR_BGR2GRAY)
while(not is_End):
    is_Change = False

    while(not is_Change):
            frame_list = []

            is_Stable = False
            while(not is_Stable):
                ret, frame = cap.read()
                cv2.circle(frame, (90,65), 2, (255,0,0),-1)
                cv2.circle(frame, (0,230), 2, (255,0,0),-1)
                cv2.circle(frame, (570,75), 2, (255,0,0),-1)
                cv2.circle(frame, (640,240), 2, (255,0,0),-1)

                gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                # gray_frame = cv2.warpPerspective(gray_frame, M, (300,300))

                if ret == True:
                    cv2.imshow('omok',gray_frame)
                    # time.sleep(1)

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
                    if len(frame_list):
                        frame_list = np.append(frame_list,np.array([gray_frame]),axis=0)
                    else:
                        frame_list = np.array([gray_frame])

                    if len(frame_list) == 5 :
                        if np.max(np.std(frame_list,axis=0)) < 50:
                            is_Stable = True
                            # cv2.imwrite('pre_frame.png',frame_list[-1])
                            print('Stable!')
                        else:
                            print(np.max(np.std(frame_list,axis=0)))
                            frame_list = frame_list[1:]
                            print('Not Stable!')

                else:
                    break

            if np.mean(abs(pre_frame - frame_list[-1])) > 10:
                print(np.mean(abs(pre_frame - frame_list[-1])))
                is_Change = True
                check_frame = abs(pre_frame - frame_list[-1])
                cv2.imshow('check_frame',check_frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
                time.sleep(5)
                print('Change!')
            else:
                print('Not Change!')

    cv2.imwrite('pre_frame.png',frame_list[-1])
    is_End = True
    print('End!')

cap.release()
cv2.destroyAllWindows()