# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# # /ai4e_api_tools has been added to the PYTHONPATH, so we can reference those
# libraries directly.
import json
import math
import os
import time
from datetime import datetime
from random import shuffle

from ai4e_app_insights import AppInsights
from ai4e_app_insights_wrapper import AI4EAppInsights
from ai4e_service import AI4EWrapper
from azure.storage.blob import BlockBlobService
from flask import Flask, request, make_response, jsonify
from flask_restful import Api
from task_management.api_task import ApiTaskManager

import api_config
import orchestrator
from sas_blob_utils import SasBlob

print('Creating application')

api_prefix = os.getenv('API_PREFIX')
print('API prefix: ', api_prefix)
app = Flask(__name__)
api = Api(app)

# Log requests, traces and exceptions to the Application Insights service
appinsights = AppInsights(app)

# Use the AI4EAppInsights library to send log messages.
log = AI4EAppInsights()

# Use the internal-container AI for Earth Task Manager (not for production use!).
api_task_manager = ApiTaskManager(flask_api=api, resource_prefix=api_prefix)

# Use the AI4EWrapper to executes your functions within a logging trace.
# Also, helps support long-running/async functions.
ai4e_wrapper = AI4EWrapper(app)

# Instantiate blob storage service to the internal container to put intermediate results and files
storage_account_name = os.getenv('STORAGE_ACCOUNT_NAME')
storage_account_key = os.getenv('STORAGE_ACCOUNT_KEY')
internal_storage_service = BlockBlobService(account_name=storage_account_name, account_key=storage_account_key)
internal_container = api_config.INTERNAL_CONTAINER
internal_datastore = {
    'account_name': storage_account_name,
    'account_key': storage_account_key,
    'container_name': internal_container
}
print('Internal storage container {} in account {} is set up.'.format(internal_container, storage_account_name))

@app.route('/', methods=['GET'])
def health_check():
    return 'Health check OK'

def _stop_aync(request_id, error_message):
    api_task_manager.UpdateTaskStatus(request_id, 'failed, error: {}'.format(error_message))
    # plus any telemetry to be collected

def _abort(error_code, error_message):
    return make_response(jsonify({'error': error_message}), error_code)

@app.route(api_prefix + '/request_detections', methods=['POST'])
def request_detections():
    if not request.is_json:
        return _abort(415, 'Body needs to have a mimetype for JSON (e.g. application/json).')

    try:
        post_body = request.get_json()
    except Exception as e:
        return _abort(415, 'Error occurred while reading the POST request body: {}.'.format(str(e)))

    # required param
    input_container_sas = post_body.get('input_container_sas', None)
    if not input_container_sas:
        return _abort(400, 'input_container_sas with read and list access is a required field.')

    result = orchestrator.check_data_container_sas(input_container_sas)
    if result is not None:
        return _abort(result[0], result[1])

    # TODO check that the expiry date of input_container_sas is at least a month into the future

    # TODO check images_requested_json_sas is a blob not a container

    task_info = api_task_manager.AddTask('queued')
    request_id = str(task_info['uuid'])

    # wrap_async_endpoint executes the function in a new thread and wraps it within a logging trace
    ai4e_wrapper.wrap_async_endpoint(_request_detections, 'post:request_detections',
                                     request_id=request_id, post_body=post_body)

    return jsonify(request_id=request_id)

def _request_detections(**kwargs):
    try:
        body = kwargs.get('post_body')

        input_container_sas = body['input_container_sas']
        images_requested_json_sas = body.get('images_requested_json_sas', None)
        image_path_prefix = body.get('image_path_prefix', None)

        first_n = body.get('first_n', None)
        first_n = int(first_n) if first_n else None
        sample_n = body.get('sample_n', None)
        sample_n = int(sample_n) if sample_n else None

        request_id = kwargs['request_id']
        api_task_manager.UpdateTaskStatus(request_id, 'running.')

        if images_requested_json_sas is None:
            api_task_manager.UpdateTaskStatus(request_id, 'running - listing all images to process.')
            print('runserver.py, running - listing all images to process.')

            # list all images to process
            blob_prefix = None if image_path_prefix is None else image_path_prefix
            image_paths = SasBlob.list_blobs_in_container(api_config.MAX_NUMBER_IMAGES_ACCEPTED,
                                                          sas_uri=input_container_sas,
                                                          blob_prefix=blob_prefix, blob_suffix='.jpg')
        else:
            print('runserver.py, running - using provided list of images.')
            image_paths_text = SasBlob.download_blob_to_text(images_requested_json_sas)
            image_paths = json.loads(image_paths_text)
            print('runserver.py, length of image_paths provided by the user: {}'.format(len(image_paths)))

            image_paths = [i for i in image_paths if str(i).lower().endswith(api_config.ACCEPTED_IMAGE_FILE_ENDINGS)]
            print('runserver.py, length of image_paths provided by the user, after filtering to jpg: {}'.format(
                len(image_paths)))

            if image_path_prefix is not None:
                image_paths =[i for i in image_paths if str(i).startswith(image_path_prefix)]
                print('runserver.py, length of image_paths provided by the user, after filtering for image_path_prefix: {}'.format(
                    len(image_paths)))

            res = orchestrator.spot_check_blob_paths_exist(image_paths, input_container_sas)
            if res is not None:
                raise LookupError('path {} provided in list of images to process does not exist in the container pointed to by data_container_sas.'.format(res))

        # apply the first_n and sample_n filters
        if first_n is not None:
            assert first_n > 0, 'parameter first_n is 0.'
            image_paths = image_paths[:first_n]  # will not error if first_n > total number of images

        if sample_n is not None:
            assert sample_n > 0, 'parameter sample_n is 0.'
            if sample_n > len(image_paths):
                raise ValueError('parameter sample_n specifies more images than available (after filtering by other provided params).')

            # we sample by just shuffling the image paths and take the first sample_n images
            print('First path before shuffling:', image_paths[0])
            shuffle(image_paths)
            print('First path after shuffling:', image_paths[0])
            image_paths = image_paths[:sample_n]
            image_paths = sorted(image_paths)

        num_images = len(image_paths)
        print('runserver.py, num_images after applying all filters: {}'.format(num_images))
        if num_images < 1:
            api_task_manager.UpdateTaskStatus(request_id, 'completed - zero images found in container or in provided list of images after filtering with the provided parameters.')
            return
        if num_images > api_config.MAX_NUMBER_IMAGES_ACCEPTED:
            api_task_manager.UpdateTaskStatus(request_id,
                                              'failed - the number of images ({}) requested for processing exceeds the maximum accepted ({}) in one call.'.format(
                                                  num_images, api_config.MAX_NUMBER_IMAGES_ACCEPTED))
            return

        image_paths_string = json.dumps(image_paths, indent=2)
        internal_storage_service.create_blob_from_text(internal_container,
                                                       '{}/{}_images.json'.format(request_id, request_id),
                                                       image_paths_string)
        api_task_manager.UpdateTaskStatus(request_id, 'running - images listed; processing {} images.'.format(num_images))
        print('runserver.py, running - images listed; processing {} images.'.format(num_images))

        # set up connection to AML Compute and data stores
        # do this for each request since pipeline step is associated with the data stores
        aml_compute = orchestrator.AMLCompute(request_id, input_container_sas, internal_datastore)
        print('AMLCompute resource connected successfully.')

        num_images_per_job = api_config.NUM_IMAGES_PER_JOB
        num_jobs = math.ceil(num_images / num_images_per_job)

        list_jobs = {}
        for job_index in range(num_jobs):
            begin, end = job_index * num_images_per_job, (job_index + 1) * num_images_per_job
            job_id = 'request{}_jobindex{}_total{}'.format(request_id, job_index, num_jobs)
            list_jobs[job_id] = { 'begin': begin, 'end': end }

        list_jobs_submitted = aml_compute.submit_jobs(request_id, list_jobs, api_task_manager, num_images)
        api_task_manager.UpdateTaskStatus(request_id,
                                          'running - all {} images submitted to cluster for processing.'.format(num_images))

    except Exception as e:
        api_task_manager.UpdateTaskStatus(request_id,
                                          'failed - an error occurred while processing the request: {}'.format(str(e)))
        print('runserver.py, exception in _request_detections: {}'.format(str(e)))
        return  # do not initiate _monitor_detections_request

    try:
        aml_monitor = orchestrator.AMLMonitor(request_id, list_jobs_submitted)

        # start another thread to monitor the jobs and consolidate the results when they finish
        ai4e_wrapper.wrap_async_endpoint(_monitor_detections_request, 'post:_monitor_detections_request',
                                         request_id=request_id,
                                         aml_monitor=aml_monitor)
    except Exception as e:
        api_task_manager.UpdateTaskStatus(request_id,
            'failed - an error occurred when starting the status monitoring process. ' +
            'The images should be submitted for processing though - please contact us to retrieve your results. Error: {}'.format(str(e)))
        print('runserver.py, exception when starting orchestrator.AMLMonitor: ', str(e))

def _monitor_detections_request(**kwargs):
    try:
        request_id = kwargs['request_id']
        aml_monitor = kwargs['aml_monitor']

        max_num_checks = api_config.MAX_MONITOR_CYCLES
        num_checks = 0

        print('Monitoring thread with _monitor_detections_request started.')

        # time.sleep() blocks the current thread only
        while True:
            time.sleep(api_config.MONITOR_PERIOD_MINUTES * 60)

            print('runserver.py, _monitor_detections_request, woke up at {} for check number {}.'.format(
                datetime.now(), num_checks))

            # check the status of the jobs and end if all jobs finished and results aggregated
            all_jobs_finished, status_tally = aml_monitor.check_job_status()
            print('all jobs finished? {}'.format(all_jobs_finished))
            for status, count in status_tally.items():
                print('status {}, number of jobs = {}'.format(status, count))

            num_failed = status_tally['Failed']
            # need to periodically check the enumerations are what AML returns - not the same as in their doc
            num_finished = status_tally['Finished'] + num_failed

            # all jobs finished, aggregate the results and return the URLs to the output files
            if all_jobs_finished:
                api_task_manager.UpdateTaskStatus(request_id,
                                                  'almost completed - model inference finished, now aggregating results.')
                output_file_urls = aml_monitor.aggregate_results()
                output_file_urls_str = json.dumps(output_file_urls)
                api_task_manager.UpdateTaskStatus(request_id,
                          'completed - timestamp {}. Number of failed shards: {}. URLs to output files: {}'.format(
                              str(datetime.now()), num_failed, output_file_urls_str))
                break

            # not all jobs are finished, update the status with number of shards finished
            api_task_manager.UpdateTaskStatus(request_id,
                          'running - last status check at {}, {} out of {} shards finished processing, {} failed.'.format(
                              str(datetime.now()), num_finished, aml_monitor.get_total_jobs(), num_failed))

            # not all jobs are finished but the maximum number of checking cycle is reached, stop this thread
            num_checks += 1
            if num_checks >= max_num_checks:
                api_task_manager.UpdateTaskStatus(request_id,
                'request unfinished after {} x {} minutes; abandoning the monitoring thread. Please contact us to retrieve any results.'.format(
                                                      api_config.MAX_MONITOR_CYCLES,
                                                      api_config.MONITOR_PERIOD_MINUTES
                                                  ))
                print('runserver.py, _monitor_detections_request, ending!')

                break
    except Exception as e:
        api_task_manager.UpdateTaskStatus(request_id,
            'failed - an error occurred while monitoring the status of this request. ' +
            'The images should be processing though - please contact us to retrieve your results. Error: {}'.format(str(e)))
        print('runserver.py, exception in _monitor_detections_request(): ', str(e))

@app.route(api_prefix + '/model_version', methods=['GET'])
def model_version():
    # wrap_sync_endpoint wraps your function within a logging trace.
    return ai4e_wrapper.wrap_sync_endpoint(_model_version, 'get:model_version')

def _model_version():
    return api_config.MODEL_VERSION

if __name__ == '__main__':
    app.run()


import pandas as pd
import numpy as np
import os, sys
import matplotlib.pyplot as plt

from parse import *

sys.path.append("../utils/")
from utils import *

subset_noise = False
noise_level = '1-2en01'

subset_difficulty = True
diff_level = '1en01'

data_dir = '../../out/'
games = []
games += get_games(data_dir, 'experiment')
#games += ['tmp']

data = get_data(data_dir, games)
#data = data[data['n_players'] < 6]
#data = data[data['score'] > 0.7]

if subset_noise:
    data = data[data['noise'] == noise_level]
if subset_difficulty:
    data = data[data['difficulty'] == diff_level]

def get_shape(i):
    shapes = ['$a$',
              '$b$',
              '$c$',
              '$d$',
              '$e$',
              '$f$',
              '$g$',
              '$h$',
              '$i$',
              '$j$',
              '$k$',
              '$l$',
              '$m$',
              '$n$',
              '$o$',
              '$p$',
              '$q$',
              '$r$',
              '$s$',
              '$t$',
              '$u$',
              '$v$',
              '$w$',
              '$x$',
              '$y$',
              '$z$',
        (4,0,0), (3,0,0), (4,0,45), (0,3,0), (3,0,90),
              (3,0,180), (3,0,270),
              (5,0,0), (6,0,0), (6,0,90), (4,2,0), (4,3,0),
              (5,2,0), (5,3,0),
              "d", "*", "$a$"]
    return shapes[i % len(shapes)]

plt.rcParams.update(pd.tools.plotting.mpl_stylesheet)

fig, ax = plt.subplots()
ax.margins(0.05)

colors = ['#348ABD',  '#467821', '#E4C334', '#A60628']

counts = dict([(n,0) for n in set(data['n_players'])])

data['nn_players'] = data['n_players'] + np.random.randn(len(data))*0.1

i = 0
for noise in set(data['noise']):
    if not subset_noise or noise == noise_level:
        noise_sub = data[data['noise'] == noise]
        x = sorted(set(noise_sub['n_players']))
        y = []
        for n in x:
            y += [np.mean(noise_sub[noise_sub['n_players'] == n]['score'])]
        ax.plot(x, y, c = colors[i], lw = 10, alpha = 0.5)
    i += 1

i = 0
j = 0
for noise in set(data['noise']):
    if not subset_noise or noise == noise_level:
        noise_sub = data[data['noise'] == noise]
        for game in set(noise_sub['game']):
            sub = noise_sub[noise_sub['game'] == game]
            n = list(set(sub['n_players']))[0]
            ax.plot(sub['nn_players'], sub['score'], marker='o', linestyle='', ms = 20, c = colors[i])
            ax.plot(sub['nn_players'], sub['score'], marker=get_shape(counts[n]), linestyle='', ms = 10, c = 'black')
            counts[n] += 1
    i += 1

#legend = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), title = 'Background', numpoints=1)
#legend.get_title().set_fontsize('30')
#plt.setp(plt.gca().get_legend().get_texts(), fontsize='20')

plt.xlabel('Number of Players', fontsize=50)
plt.ylabel('Individual Score', fontsize=50)
ax.tick_params(axis='x', labelsize=30)
ax.tick_params(axis='y', labelsize=30)

fig = plt.gcf()
fig.set_size_inches(18.5,10.5)
if subset_difficulty:
    fig.savefig('../../plots/performance-' + diff_level + '.png',dpi=100)
elif subset_noise:
    fig.savefig('../../plots/performance-' + noise_level + '.png',dpi=100)
else:
    fig.savefig('../../plots/performance-all.png',dpi=100)

#plt.show()


class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        left = 0
        for right in range(1, len(nums)):
            if nums[left] != nums[right]:
                nums[left+1] = nums[right]
                left += 1
                right += 1
            else:
                right += 1
        return left + 1

Code/0026.Remove Duplicates from Sorted Array/Remove Duplicates from Sorted Array_2.py


# Usage: python music_collection_renamer.py folder
#

import sys
import os
from tinytag import TinyTag
from os import listdir
from os.path import isfile, join

folder = sys.argv[1]

if not os.path.exists(folder):
    print("Folder doesn't exist...")
    exit

files = [f for f in listdir(folder) if isfile(join(folder, f))]

# Usually music folders have one .nfo file
for file in reversed(files):
    if file.endswith(".nfo"):
        os.remove(os.path.join(folder, file))
        break

# Remove artist name from the filename
# This is only for CD album folders, not for folders with mixed artists
temp = None
replace_with = ""
for file in files:
    if file.endswith((".mp3",".flac", ".wav", ".ogg", ".mp4", ".m4a", ".wma", ".wave", ".opus")):
        tag = TinyTag.get(os.path.join(folder, file))
        artist = tag.artist

        if temp == None:
            if artist not in file:
                break

            if artist + " - " in file:
                temp = artist + " - "
            elif "-"+artist+"-":
                temp = "-"+artist+"-"
                replace_with = " - "

        os.rename(os.path.join(folder, file), os.path.join(folder, file.replace(temp, replace_with)))


# -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 21:14:29 2020

@author: incog
"""

import tensorflow as tf
import tensorflow.keras.backend as K

import tensorflow.keras as keras

from tensorflow.keras.models import Sequential,Model,load_model
from tensorflow.keras.layers import Input, Dense, Dropout, LSTM, Activation, Bidirectional, Flatten, AdditiveAttention
from tensorflow.keras import utils
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.utils import Sequence

from music21 import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import datetime
import pytz
from IPython.display import clear_output

import glob
import pickle

from train import get_notes
from extract_notes import get_notes_midi1,get_notes_midi2,get_notes_midi5,get_notes_event1

def get_streams(data_path='data',filetype='.krn',save=True,save_name='streams'):
    streams=[]
    file_list=glob.glob(data_path+'/**/*'+filetype,recursive=True)
    count=0
    for file in file_list:
        streams.append(converter.parse(file))
        print('Processed files:'+str(count))
        count+=1
    with open('streams/'+save_name, 'wb') as filepath:
        pickle.dump(streams, filepath)
    return streams

def grab_first_part(stream):
  try: # file has instrument parts
    s2 = instrument.partitionByInstrument(midi) #Change to only grab the piano???
    notes_to_parse = s2.parts[0].recurse()
    if len(s2.parts)!=1:
      print('has more parts!')
  except: # file has notes in a flat structure
      print('no parts!')
      notes_to_parse = midi.flat.notes
  return notes_to_parse

def grab_first_parts(streams):
    parts=[]
    for stream in streams:
        parts.append(grab_first_part(stream))
    return parts

def transpose_streamsC(streams):
    '''
    Function that transposes all major pieces to the key of C major and minor pieces to A minor.
    To be used with Music21 streams that contain key information.

    '''
    keys=[]
    tstreams=[]
    for s in streams:
      temp=s.flat
      k=temp.getElementsByClass(key.Key)
      keys.append(k[0])
      iM = interval.Interval(k[0].tonic, pitch.Pitch('C4'))
      im = interval.Interval(k[0].tonic, pitch.Pitch('A3'))
      if k[0].mode=='minor':
        sNew = s.transpose(im)
      else:
        sNew = s.transpose(iM)

      tstreams.append(sNew)
    return tstreams

def transpose_streams_step(streams,step):
    ttstreams=[]
    for s in tstreams:
        sNew=s.transpose(step)
        ttstreams.append(sNew)
    return ttstreams

def get_notes(encoding,data_dir='data',file_extension='.krn',resolution=8,streams=True):
    if not os.path.exists('notes'):
        os.mkdir('notes')
    path=data_dir
    if encoding==1:
        get_notes_midi1(path,resolution=resolution,streams=streams)
    elif encoding==2:
        get_notes_midi2(path,resolution=resolution,streams=streams)
    elif encoding==3:
        get_notes_midi5(path,resolution=resolution,streams=streams)
    elif encoding==4:
        get_notes_event1(path,resolution=resolution,streams=streams)

#%%
if __name__=='__main__':
    streams=get_streams('data')
    streams=transpose_streamsC(streams)
    with open('streams/streams_C', 'wb') as filepath:
        pickle.dump(streams, filepath)

#%%

for enc in [1,2,4]:
    for res in [4,8,16,24]:
        get_notes(enc,data_dir='C:/scripts/streams/streams_C',resolution=res,streams=True)

#%%

def transpose_notes_step(enc,notes,step=1):
    if enc not in [1,2,4]:
        print('Wrong encoding')
        return None
    tnotes=[]
    for song in notes:
        tsong=[]
        for n in song:
            if enc==1 or enc==2:
                if n<=(127-step):
                    tsong.append(n+step)
                else:
                    tsong.append(n)
            else:
                if n<=(255-step):
                    tsong.append(n+step)
                else:
                    tsong.append(n)
        tnotes.append(np.array(tsong))

    return np.array(tnotes)

notes_path='notes/tstep2'
notes_list=glob.glob(notes_path+'/*')
for n in notes_list:
    notes=pd.read_pickle(n)
    filename=os.path.basename(n)
    os.makedirs(notes_path+'/'+ filename+'_transposed',exist_ok=True)
    for step in range(12):
        tnotes=transpose_notes_step(4,notes,step)
        with open(notes_path+'/'+filename+'_transposed'+'/'+filename+'t'+str(step), 'wb') as filepath:
            pickle.dump(tnotes, filepath)

#%%

for res in [4,8,16,24]:
    get_notes(2,data_dir='data1',resolution=res,streams=False)

#%%

def get_pitch_count(stream):
    pc=0
    for n in stream:
        if isinstance(n, note.Note):
            pc+=1
    return pc

def get_note_count(piece):
    pc=0
    for n in piece:
        if n<128:
            pc+=1
    return pc

def check_notes_streams(streams,notes):
    bad=[]
    for i in range(len(streams)):
        temp=streams[i].parts[0].flat.notes
        if get_pitch_count(temp)!=get_note_count(notes[i]):
            print(i)
            bad.append(i)
    return bad


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

from yile.application import create_app
from yile.extensions import db

def main():
    app = create_app()
    with app.app_context():
        db.create_all()

    app.run(host='0.0.0.0', debug=True)

if __name__ == '__main__':
    main()


from numpy import empty
from pylab import plot,show,imshow,gray,savefig,xlabel,ylabel

N = 50    #Number of times to perform iteration
points = 1000 #Pixels along a side (size of matrix to be plotted)

def mandelbrot(N):
    xi=empty([points,points],float)
    for X in range(points):
        x = float((4*X-2*points))/points  #converting pixel coordinate to actual x,y values
        for Y in range(points):
            y = float((4*Y-2*points))/points
            z0=0.0
            for n in range(N):
                z0 = z0*z0 + complex(x,y)

            if abs(z0) < 2:
                xi[X,Y] = abs(z0)
            else:
                xi[X,Y] = 0
    return xi

imshow(mandelbrot(N).T,extent=[-2,2,-2,2])
xlabel("x")
ylabel("y")
savefig("mandelbrotplot.png")
show()


import os
import string

def offByOne(str1, str2):
    l1, l2 = len(str1), len(str2)
    if l1 != l2:
        return False
    cnt = 0
    for i in range(l1):
        if(str1[i] != str2[i]):
            cnt += 1
        if cnt > 1:
            return False
    return True

def offBySwap(str1, str2):
    l1, l2 = len(str1), len(str2)
    if l1 != l2 or str1 == str2:
        return False
    for i in range(l1-1):
        str3 = str2[:i]+str2[i+1]+str2[i]+str2[i+2:]
        if str1 == str3:
            return True
    return False

def offByExtra(str1, str2):
    l1, l2 = len(str1), len(str2)
    if abs(l1-l2) != 1:
        return False
    if l2 > l1:
        str1, str2 = str2, str1
        l = l2
    else:
        l = l1
    for i in range(l):
        str3 = str1[:i]+str1[i+1:]
        if str2 == str3:
            return True
    return False

def ListOfNeighbors(str, L):
    print(str, "的邻居：")
    sum = 1
    for x in L:
        if offByExtra(str, x) or offByOne(str, x) or offBySwap(str, x):
            if sum % 5 != 0:
                print(x, end=' ')
            else:
                print(x)
            sum += 1
    print("\n一共", sum - 1, "个")

# list1 = [["read", "rexd"], ["read", "xexd"],
#          ["read", "readx"], ["read", "eadx"],
#          ["a", "x"], ["a", "a"], ["a", "A"]]
# for x in list1:
#     print("str1 =", x[0], "str2 =", x[1], "   ", offByOne(x[0], x[1]))

# list2 = [["read", "raed"], ["read", "erad"],
#          ["reaxd", "read"], ["read", "erda"],
#          ["read", "erbx"], ["x", "Y"],
#          ["aaa", "aaa"]]
# for x in list2:
#     print("str1 =", x[0], "str2 =", x[1], "   ", offBySwap(x[0], x[1]))

# list3 = [["abcd", "abxcd"], ["abxcd", "abcd"],
#          ["abcda", "abcd"], ["abcd", "bcda"],
#          ["abcd", "abcdef"], ["abcd", "abcd"]]
# for x in list3:
#     print("str1 =", x[0], "str2 =", x[1], "   ", offByExtra(x[0], x[1]))

f = open("EnglishWords.txt", mode='r')
wordList = []
while True:
    line = f.readline()
    if not line:
        break
    wordList.append(line[:-1])

Str = "code"
ListOfNeighbors(Str, wordList)


from flask import Blueprint, request, make_response
from app.models.customer import Customer
from app.models.video import Video
from app.models.rentals import Rental
from flask.json import jsonify
from app import db

customers_bp = Blueprint("customers", __name__, url_prefix="/customers")
videos_bp = Blueprint("videos", __name__, url_prefix="/videos")
rentals_bp = Blueprint("rentals", __name__, url_prefix="/rentals")

@customers_bp.route("", methods=["GET"], strict_slashes=False)
def get_customers():
    movie_buffs = Customer.query.all()
    response_body = []
    for nerd in movie_buffs:
        response_body.append(nerd.customer_info())
    return jsonify(response_body), 200

@customers_bp.route("/<int:customer_id>", methods=["GET"], strict_slashes=False)
def get_customer(customer_id):
    customer = Customer.query.get_or_404(customer_id)
    return jsonify(customer.customer_info()), 200

@customers_bp.route("", methods=["POST"], strict_slashes=False)
def new_customer():
    request_body = request.get_json()
    if "name" in request_body and "postal_code" in request_body and "phone" in request_body:
        customer = Customer(**request_body)
        db.session.add(customer)
        db.session.commit()
        return jsonify(customer.customer_info()), 201
    else:
        return jsonify({"details": "Invalid data"}), 400

@customers_bp.route("/<int:customer_id>", methods=["PUT"], strict_slashes=False)
def update_customer(customer_id):
    request_body = request.get_json()
    customer = Customer.query.get_or_404(customer_id)
    if "name" in request_body and "postal_code" in request_body and "phone" in request_body:
        customer.name = request_body["name"]
        customer.postal_code = request_body["postal_code"]
        customer.phone = request_body["phone"]
        db.session.commit()
        return jsonify(customer.customer_info()), 200
    else:
        return jsonify({"details": "Invalid data"}), 400

@customers_bp.route("/<int:customer_id>", methods=["DELETE"], strict_slashes=False)
def delete_customer(customer_id):
    keep_your_money = Customer.query.get_or_404(customer_id)
    db.session.delete(keep_your_money)
    db.session.commit()
    return make_response({"id": keep_your_money.customer_id}, 200)

@videos_bp.route("", methods=["GET"], strict_slashes=False)
def get_videos():
    videos = Video.query.all()
    response_body = []
    for video in videos:
        response_body.append(video.video_info())
    return jsonify(response_body), 200

@videos_bp.route("/<int:video_id>", methods=["GET"], strict_slashes=False)
def get_video(video_id):
    video = Video.query.get_or_404(video_id)
    return jsonify(video.video_info()), 200

@videos_bp.route("", methods=["POST"], strict_slashes=False)
def post_video():
    request_body = request.get_json()
    if "title" in request_body and "release_date" in request_body and "total_inventory" in request_body:
        video = Video(**request_body)
        video.available_inventory = video.total_inventory
        db.session.add(video)
        db.session.commit()
        return jsonify(video.video_info()), 201
    return jsonify({"details": "Invalid data"}), 400

@videos_bp.route("/<int:video_id>", methods=["PUT"], strict_slashes=False)
def update_video(video_id):
    request_body = request.get_json()
    video = Video.query.get_or_404(video_id)
    if "title" in request_body and "release_date" in request_body and "total_inventory" in request_body:
        video.title = request_body["title"]
        video.release_date = request_body["release_date"]
        video.total_inventory = request_body["total_inventory"]
        video.available_inventory = request_body["total_inventory"]
        db.session.commit()
        return jsonify(video.video_info()), 200
    else:
        return jsonify({"details": "Invalid data"}), 400

@videos_bp.route("/<video_id>", methods=["DELETE"], strict_slashes=False)
def delete_video(video_id):
    vid = Video.query.get_or_404(video_id)
    db.session.delete(vid)
    db.session.commit()
    return make_response({"id": vid.video_id}, 200)

def is_int(value):
    try:
        return int(value)
    except ValueError:
        return None

@rentals_bp.route("/check-out", methods= ["POST"], strict_slashes=False)
def rent_video():
    request_body = request.get_json()
    if "customer_id" not in request_body or "video_id" not in request_body:
        return make_response({"details": "Invalid data"}, 400)
    checkem_out = request_body["customer_id"]
    check_video = request_body["video_id"]
    if not is_int(checkem_out) or not is_int(check_video):
        return make_response({"details": "Bad request"}, 400)
    customer = Customer.query.get(checkem_out)
    video = Video.query.get(check_video)
    if not video.has_available_inventory():
        return make_response({"details": "Inventory not available"}, 400)
    video.check_out()
    customer.videos_checked_out_count += 1
    this_rental = Rental(**request_body)
    db.session.add(this_rental)
    db.session.commit()
    return make_response(this_rental.rental_info(), 200)

@rentals_bp.route("/check-in", methods=["POST"], strict_slashes=False)
def return_video():
    request_body = request.get_json()
    if "customer_id" not in request_body or "video_id" not in request_body:
        return make_response({"details": "Invalid data"}, 400)
    checkin_customer = request_body["customer_id"]
    checkin_video = request_body["video_id"]
    if not is_int(checkin_customer) or not is_int(checkin_video):
        return make_response({"details": "Bad request"}, 400)
    this_rental = Rental.query.filter_by(customer_id = checkin_customer, video_id = checkin_video).first()
    customer = Customer.query.get(checkin_customer)
    video = Video.query.get(checkin_video)
    this_customer = Customer.query.get(checkin_customer)
    if this_customer == None:
        return make_response({"details": "Invalid Customer"}, 400)

    if this_customer.videos_checked_out_count > 0:
        video.check_in()
        this_customer.videos_checked_out_count -= 1
        db.session.commit()
        user_msg = this_rental.rental_info()
        del user_msg["due_date"]
        return make_response(user_msg, 200)
    return make_response({"details": "Rentals all returned"}, 400)

@customers_bp.route("/<int:customer_id>/rentals", methods=["GET"], strict_slashes=False)
def customer_rentals(customer_id):
    customer = Customer.query.get_or_404(customer_id)
    rentals_history = []
    for rental in customer.rentals:
        video = Video.query.get(rental.video_id)
        rentals_history.append({"release_date" : video.release_date,
                            "title" : video.title,
                            "due_date" : rental.due_date
                            })
    return jsonify(rentals_history), 200

@videos_bp.route("/<int:video_id>/rentals", methods=["GET"], strict_slashes=False)
def video_rentals(video_id):
    video = Video.query.get_or_404(video_id)
    rentals_history = []
    for rental in video.rentals:
        customer = Customer.query.get(rental.customer_id)
        rentals_history.append({"name" : customer.name,
                            "phone" : customer.phone,
                            "postal_code" : customer.postal_code,
                            "due_date" : rental.due_date
                            })
    return jsonify(rentals_history), 200


"""empty message

Revision ID: 6bae619ac9f6
Revises: 27b5a273a861
Create Date: 2020-11-26 17:27:31.801212

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '6bae619ac9f6'
down_revision = '27b5a273a861'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('order', sa.Column('tour_date', sa.Date(), nullable=True))
    op.create_index(op.f('ix_order_tour_date'), 'order', ['tour_date'], unique=False)
    op.drop_index('ix_order_add_date', table_name='order')
    op.drop_column('order', 'add_date')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('order', sa.Column('add_date', sa.DATE(), autoincrement=False, nullable=True))
    op.create_index('ix_order_add_date', 'order', ['add_date'], unique=False)
    op.drop_index(op.f('ix_order_tour_date'), table_name='order')
    op.drop_column('order', 'tour_date')
    # ### end Alembic commands ###


from ..mixin                         import CommandMixin, ProjectMixin
from instrument_server.command       import Base
from instrument_server.command.mixin import ParserMixin

class Name(ProjectMixin, CommandMixin, ParserMixin, Base):
    def __init__(self, devices, state, **settings):
        Base        .__init__(self, devices, state, **settings)
        ParserMixin .__init__(self, command='project_name?')
        CommandMixin.__init__(self)
        ProjectMixin.__init__(self)

    @property
    def help_str(self):
        return 'Query project name'

    def execute(self, received_command):
        args = self.args(received_command)
        self.raise_if_not_project()
        if not 'name' in self.project:
            raise self.command_error("project missing 'name'")
        return self.project.procedure['name']

IS_COMMAND_PLUGIN = True
plugin            = Name


from itertools import combinations
k,n = map(int, input().split(","))

def num(k,n):
    nums=[1,2,3,4,5,6,7,8,9]
    res=[]
    for i in range(len(nums)):
        res +=list(combinations(nums,i))
    res=[x for x in res if len(x) == k]
    a=[]
    for j in res:
        if sum(j) ==n :
            a.append(list(j))
    return a

print(num(k,n))


import pandas as pd
import numpy as np
import pickle
import joblib
import random

def load(file_name):
    with open(file_name, 'rb') as fp:
        obj = pickle.load(fp)
    return obj

# dictionary of lists

original_df = pd.read_csv("Grouped_data_With_Prev_Value_Column.csv")

dict = {'Start Date':["12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020","12/1/2020"],
        'Start Hour': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
        }

df = pd.DataFrame(dict)
df_Copy =pd.DataFrame(dict)

df['Start Date'] = pd.to_datetime(df['Start Date']).dt.date

holiday = pd.read_csv('C:/Users/A02290684/Desktop/Thesis/Prediction/US Bank holidays.csv')
holiday['Holiday'] = pd.to_datetime(holiday['Holiday']).dt.date

df = df.merge(holiday, left_on = 'Start Date',right_on = 'Holiday',how = 'left')
df['holiday_ind'] = np.where(df['Holiday'].isna(),0,1)

df = df[['Start Date','Start Hour','holiday_ind']]

# df.set_index(['Start Date'],inplace=True)
df["Start Date"]= pd.to_datetime(df["Start Date"])

df['Weekend'] = df['Start Date'].dt.dayofweek
df['Weekend'] = (df["Weekend"] > 4).astype(int)

df = df[['Start Date','Start Hour','holiday_ind','Weekend']]

vals = df['Start Hour'].values
lists =[]
for val in vals:
    # print(val)
    df2 = original_df[original_df['Start Hour'] == val]
    rows = np.random.choice(df2.index.values, 1)
    sampled_df = df2.loc[rows]
    a =sampled_df['Prev_value'].values[0]
    lists.append(a)

df['Prev_value']=lists
# df['Prev_value']=df['Prev_value'].replace(0,5)
# df['Prev_value']=df['Prev_value'].replace(0,9)

df.set_index(['Start Date'],inplace=True)

# model=pickle.load(open("EV_Predictor.sav", 'rb'))
model = joblib.load("EV_Predictor.sav")

# df.to_csv("Data_With_Holiday__And_Weekend_Index.csv")
dataset_2 = df.values
Xnew = dataset_2[:,0:]

ynew = model.predict(Xnew)

result_df =  df_Copy[["Start Date","Start Hour"]]
result_df["Predicted Values"] =ynew

'''Adding Visitors'''
number_of_people = [1, 1, 1, 1, 1, 3, 5, 8, 11, 9, 8, 9, 10, 10, 9, 10, 8, 9, 8, 6, 4, 2, 2, 1]
number_of_people = np.asarray(number_of_people)
result_df["Number of visitors"] = number_of_people

visitors = []
for x in range(0,len(number_of_people)):
    if(number_of_people[x]==1):
        test = np.random.choice(np.arange(0, 2), p=[0.8, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==2):
        test = np.random.choice(np.arange(0, 3), p=[0.7, 0.1, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==3):
        test = np.random.choice(np.arange(0, 4), p=[0.5, 0.1, 0.1, 0.3])
        visitors.append(test)
    elif(number_of_people[x]==4):
        test = np.random.choice(np.arange(0, 5), p=[0.2, 0.1, 0.2, 0.2, 0.3])
        visitors.append(test)
    elif(number_of_people[x]==5):
        test = np.random.choice(np.arange(0, 6), p=[0.05, 0.05, 0.05, 0.25, 0.25, 0.35])
        visitors.append(test)
    elif(number_of_people[x]==6):
        test = np.random.choice(np.arange(0, 7), p=[0.034, 0.033, 0.033, 0.1, 0.2, 0.3, 0.3])
        visitors.append(test)
    elif(number_of_people[x]==7):
        test = np.random.choice(np.arange(0, 8), p=[0.034, 0.033, 0.033, 0.1, 0.2, 0.2, 0.2, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==8):
        test = np.random.choice(np.arange(0, 9), p=[0.02, 0.02, 0.02, 0.04, 0.1, 0.2, 0.2, 0.2, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==9):
        test = np.random.choice(np.arange(0, 10), p=[0.02, 0.02, 0.02, 0.02, 0.05, 0.07, 0.2, 0.2, 0.2, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==10):
        test = np.random.choice(np.arange(0, 11), p=[0.02, 0.02, 0.02, 0.02, 0.02, 0.05, 0.05, 0.2, 0.2, 0.2, 0.2])
        visitors.append(test)
    elif(number_of_people[x]==11):
        test = np.random.choice(np.arange(0, 12), p=[0.02, 0.02, 0.02, 0.02, 0.02, 0.05, 0.05, 0.1, 0.1, 0.2, 0.2, 0.2])
        visitors.append(test)
result_df["Number of probable visitors"] = visitors

result_df.loc[result_df['Number of probable visitors'] == 0, 'Predicted Values'] = result_df['Predicted Values']*0
result_df.loc[result_df['Number of probable visitors'] == 1, 'Predicted Values'] = result_df['Predicted Values']*1.0
result_df.loc[(result_df['Number of probable visitors'] > 1 ) & (result_df['Number of probable visitors'] <= 4), 'Predicted Values'] = result_df['Predicted Values']*1.5
result_df.loc[(result_df['Number of probable visitors'] >= 5 ) & (result_df['Number of probable visitors'] <= 8), 'Predicted Values'] = result_df['Predicted Values']*2.0
result_df.loc[(result_df['Number of probable visitors'] >= 9 ) & (result_df['Number of probable visitors'] <= 11), 'Predicted Values'] = result_df['Predicted Values']*2.5
# result_df.loc[result_df['Number of probable visitors'] >10 , 'Predicted Values'] = result_df['Predicted Values']*2.3

result_df = result_df[['Start Date','Start Hour','Number of probable visitors','Predicted Values']]
print(result_df)

result_df = result_df[['Start Date','Start Hour','Number of probable visitors','Predicted Values']]
result_df.set_index(['Start Date'],inplace=True)
# result_df.to_csv("Prediction_Results.csv")

Prediction with Probabilistic visitors.py


from pcdet.datasets import DatasetTemplate

class PointcloudPreprocessing(DatasetTemplate):
    def __init__(self, dataset_cfg, class_names, training=True, root_path=None, logger=None):
        super().__init__(
            dataset_cfg=dataset_cfg, class_names=class_names, training=training, root_path=root_path, logger=logger
        )
        self.root_path = root_path
    def preprocess_pointcloud(self, pointcloud):
        input_dict = {
            'points': pointcloud,
            'frame_id': 0
        }
        data_dict = self.prepare_data(data_dict=input_dict)
        return data_dict


#!/usr/bin/env python3
#coding=utf-8

'''
name: Laz
email:
data: 2019-3
introduce: File_load Client
env: python3.5
module: 第三方模块
'''

# 调用模块
from socket import *
import os
import sys
import time

# 菜单选项
def menu():
	print('+---+------------------------------+')
	print('| 1 |查看服务端文件                +')
	print('+---+------------------------------+')
	print('| 2 |下载文件到本地                +')
	print('+---+------------------------------+')
	print('| 3 |上传文件到服务端              +')
	print('+---+------------------------------+')
	print('| 4 |退出                          +')
	print('+---+------------------------------+')

# 客户端
class FtpClient(object):
	def __init__(self,sockfd):
		self.sockfd = sockfd

	def read_file(self):
		self.sockfd.send(b'R')	# 发送请求
		data = self.sockfd.recv(1024).decode()
		if data == 'OK':
			data = self.sockfd.recv(4096).decode()
			files = data.split('#')
			for file in files:
				print(file)
			print('文件列表展示完毕\n')
		else:
			# 打印失败原因
			print(data)

	def load_file(self,filename,file_path):
		self.sockfd.send(('L %s'%filename).encode())
		data = self.sockfd.recv(1024).decode()
		if data == 'OK':
			try:
				f = open(file_path+filename,'wb')
				while True:
					msg = self.sockfd.recv(4096)
					if msg == b'##':
						break
					f.write(msg)
				f.close()
				print('%s文件下载完毕'%filename)
			except Exception as e:
				print('Error:',e)
		else:
			print(data)

	def upload_file(self,filename,file_path):
		try:
			f = open(file_path+filename,'rb')
		except:
			print('要上传的文件%s不存在'%filename)
			return
		self.sockfd.send(('U %s'%filename).encode())
		data = self.sockfd.recv(1024).decode()
		if data == 'OK':
			while True:
				msg = f.read(4096)
				if not msg:
					time.sleep(0.1)
					self.sockfd.send(b'##')
					break
				self.sockfd.send(msg)
			f.close()
			print('%s文件上传完毕'%filename)
		else:
			print(data)

	def server_exit(self):
		self.sockfd.send(b'Q')

# 网络连接
def main():
	if len(sys.argv) < 3:
		print('argv is error')
		return
	# 创建套接字
	HOST = sys.argv[1]
	PORT = int(sys.argv[2])
	ADDR = (HOST,PORT)	# 文件服务端地址

	sockfd = socket()
	sockfd.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
	try:
		sockfd.connect(ADDR)
	except:
		print('连接服务器失败')
		return

	ftp = FtpClient(sockfd)	# 生成功能类对象
	while True:
		menu()
		try:
			opt = input(">>选项:")
			if opt[0] == "1":
				ftp.read_file()		# 返回文件名称列表信息
			elif opt[0] == "2":
				filename = opt.split(' ')[1]
				file_path = opt.split(' ')[-1]
				ftp.load_file(filename,file_path)
			elif opt[0] == "3":
				filename = opt.split(' ')[1]
				file_path = opt.split(' ')[-1]
				ftp.upload_file(filename,file_path)
			elif opt[0] == "4":
				ftp.server_exit()
				sockfd.close()
				sys.exit('谢谢使用')
			else:
				raise ValueError
		except ValueError:
			print('输入有误,请输入正确命令')
			continue
		except KeyboardInterrupt:
			sockfd.close()
			sys.exit('谢谢使用')
		except Exception as e:
			print("Error:",e)

if __name__ == "__main__":
	main()


from django.conf.urls import url

from . import views
from .views import export as export_views

urlpatterns = [
    url(r'^$', views.ReportIndexView.as_view(), name='index'),
    url(r'^hour_meter$', views.HourmeterReportView.as_view(), name='hour_meter'),
    url(r'^biaya_per_alat$', views.BiayaabPerAlatReportView.as_view(), name='biaya_per_alat'),
    url(r'^biaya_per_tanggal$', views. BiayaabPerTanggalReportView.as_view(), name='biaya_per_tanggal'),
    url(r'^bbm_alat_berat$', views.BBMabReportView.as_view(), name='bbm_ab'),
    url(r'^transaksi_tangki_induk$', views.TransaksiTangkiIndukReportView.as_view(), name='transaksi_tangki_induk'),
    url(r'^transaksi_mobil_tangki$', views.TransaksiMobilTangkiReportView.as_view(), name='transaksi_mobil_tangki'),
    url(r'^hasil_produksi$', views.ProduksiReportView.as_view(), name='hasil_produksi'),
    url(r'^export_hour_meter$', export_views.HourMeterExportView.as_view(), name='export_hour_meter'),
    url(r'^export_biaya_per_alat$', export_views.BiayaabPerAlatExportView.as_view(), name='export_biaya_per_alat'),
    url(r'^export_biaya_per_tanggal$', export_views.BiayaabPerTanggalExportView.as_view(), name='export_biaya_per_tanggal'),
    url(r'^export_bbm_ab$', export_views.BBMabExportView.as_view(), name='export_bbm_ab'),
    url(r'^export_transaksi_tangki_induk', export_views.TransaksiTangkiIndukExportView.as_view(), name='export_transaksi_tangki_induk'),
    url(r'^export_transaksi_mobil_tangki', export_views.TransaksiMobilTangkiExportView.as_view(), name='export_transaksi_mobil_tangki'),
    url(r'^export_produksi', export_views.ProduksiExportView.as_view(), name='export_produksi'),
]


# -*- coding: utf-8 -*-
"""
Created on Sun Aug  6 08:06:06 2017

@author: Administrator
"""

import requests
import re

def getHTMLtext(url):
    try:
        r = requests.get(url, timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return " "

def parsePage(ilt,html):
    try:
        plt = re.findall(r'\"view_price\"\:\"[\d].*"',html)
        tlt = re.findall(r'\"raw_title\"\:\".*?\"',html)
        for i in range(len(plt)):
            price = eval(plt[i].split(":")[1])
            title = eval(tlt[i].split(":")[1])
            ilt.append([price,title])
    except:
            return " "

def printGoodList(ilt):
    tplt = "{:4}\t{:8}\t{:16}"
    print(tplt.format("序号","价格","商品名称"))
    count = 0
    for g in ilt:
        count += 1
       # print(tplt.format(count,g[0],g[1]))
        print(count,g[0],g[1])

def main():
    goods = '马裤'
    start_url = 'https://s.taobao.com/search?q=' + 'goods' + '&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170806'
    infoList = []

    url = start_url
    html = getHTMLtext(url)
    parsePage(infoList,html)

    printGoodList(infoList)

main()


f = open('input8')
data = ''
for i in f:
    data += str(i)
f.close()

width = 25
height = 6

layers = []
for i in range((len(data)-1)//(height * width)):
    templayer = ''
    for j in range(height):
        templayer += data[(i*width*height)+width*j:(i*width*height)+width+width*j]
    layers.append(templayer)

zeroes = []
for i in layers: # each layer
    num = 0
    for j in i: # each line
        for k in j: #each char
            if k == '0':
                num += 1
    zeroes.append(num)

minzeroes = layers[zeroes.index(min(zeroes))]

ones = 0
for i in minzeroes:
    if i == '1':
        ones += 1

twos = 0
for i in minzeroes:
    if i == '2':
        twos += 1

print(ones * twos)


import subprocess as sp
import time

user = open("res/username.txt", 'r')
username = user.read()

while True:
    command = input(f'{username}@Recovery:~$ ')
    if command == "status":
        time.sleep(1)
        if display_driver == True:
            print('Everything OK')
        else:
            print('No display driver')
            print('Else OK')
    if command == "intrecover":
        import intrecover
    if command == "clear":
        tmp = sp.call('clear', shell=True)
    if command == 'sysedit':
        import sysedit
    if command == 'ddtrue':
        display_driver = True
    else:
        if command == 'ddfalse':
            display_driver = False


import pytest

from jwst.pipeline.calwebb_spec2 import Spec2Pipeline
from jwst.pipeline.collect_pipeline_cfgs import collect_pipeline_cfgs
from jwst.tests.base_classes import BaseJWSTTest, raw_from_asn

@pytest.mark.bigdata
class TestSpec2Pipeline(BaseJWSTTest):
    input_loc = 'niriss'
    ref_loc = ['test_spec2pipeline', 'truth']
    test_dir = 'test_spec2pipeline'

    def test_nis_wfss_spec2(self):
        """
        Regression test of calwebb_spec2 pipeline performed on NIRISS WFSS data.
        """
        # Collect data
        asn_file = self.get_data(self.test_dir,
                                 'jw87600-a3001_20171109T145456_spec2_001_asn.json')
        for file in raw_from_asn(asn_file):
            self.get_data(self.test_dir, file)

        # Run the step
        collect_pipeline_cfgs('cfgs')
        Spec2Pipeline.call(asn_file, config_file='cfgs/calwebb_spec2.cfg', save_bsub=True)

        # Test results.
        outputs = [('jw87600017001_02101_00002_nis_cal.fits',
                    'jw87600017001_02101_00002_nis_cal_ref.fits'),
                   ('jw87600017001_02101_00002_nis_x1d.fits',
                    'jw87600017001_02101_00002_nis_x1d_ref.fits')]
        self.compare_outputs(outputs)


class Music:

    def __init__(self, wykonawca, utwor, album, rok):
        self.wykonawca = wykonawca
        self.utwor = utwor
        self.album = album
        self.rok = rok

    def __str__(self):
        return "Wykonawca: " + self.wykonawca + '\n'+ "Utwór: " + self.utwor + '\n' + "Album: " + self.album + '\n' + "Rok: " + self.rok

u1 = Music('Dawid Podsiadło', 'Nie ma fal', 'Małomiasteczkowy', '2018')
print(u1)


class Solution(object):
    def duplicateZeros(self, arr):
        """
        :type arr: List[int]
        :rtype: None Do not return anything, modify arr in-place instead.
        """
        duplicateZeros = 0
        length_ = len(arr) - 1

        for left in range(len(arr)):
            if left > length_ - duplicateZeros:
                break
            if arr[left] == 0:
                if left == length_ - duplicateZeros:
                    arr[length_] = 0
                    length_ -= 1
                    break
                duplicateZeros += 1

        lastIdx = length_ - duplicateZeros
        print(lastIdx)
        for i in range(lastIdx, -1, -1):
            print(i)
            if arr[i] == 0:
                arr[i + duplicateZeros] = 0
                duplicateZeros -= 1
                arr[i + duplicateZeros] = 0
            else:
                arr[i + duplicateZeros] = arr[i]


u"""# Flask server app"""
import os

import wikipedia as wikipediaapp
from flask import Flask, render_template, request, send_from_directory

# import webbrowser

os.environ["FLASK_ENV"] = "deployment"

app = Flask(__name__)

@app.route("/favicon.ico")
def favicon():
    u"""Adding favicon."""
    return send_from_directory(
        os.path.join(app.root_path, "static"),
        "favicon.ico",
        mimetype="static/favicon.ico",
    )

@app.route("/")
def index():
    """## Index page."""
    # return "
Bienvenue sur le site
"
    return render_template("/index.html")

@app.route("/wikipedia")
@app.route("/wikipedia/")
def wikipediaindex():
    u"""## index of wikipedia folder."""
    return render_template("/wikipedia/index.html")

@app.route("/wikipedia/page.html", methods=["GET", "POST"])
def wikipediapage():
    u"""## View wikipedia page."""
    if request.method == "POST":
        page = request.form["page"]
    else:
        page = request.args.get("page")
    page_py = wikipediaapp.wiki_wiki.page(page)
    # page = page_py.summary
    return render_template("/wikipedia/page.html", page=page, page_py=page_py)

port = 8080
if __name__ == "__main__":
    # webbrowser.open("http://localhost:"+str(port))
    app.run(debug=True, port=port)
    # app.run(port=port)


from ParseTree.parser import StanfordServerParser
from ParseTree.matcher import MatcherContext

#It is more likely dependency parser
parser = StanfordServerParser()
obj = MatcherContext()

# sents = [
#     'What Heatmeter contains?'
# ]
#
# rules = {
#     '(ROOT ( SBARQ ( WHNP/WDT:wh_t ) ( NP ( NN ) ( NN:np_t ) ) ) ': {
#         'np_t': {
#             '( NN ( NN:subj-o ) ( PP ( IN:subj_in-o ) ( NP:obj-o ) ) )': {},
#             '( NN:subj-o )': {},
#         },
#         'wh_t': {
#             '( WHNP:whnp ( WDT ) ( NN:prop-o ) )': {},
#             '( WHNP/WHADVP:qtype-o )': {},
#         }
#     },
#     '( SBARQ:subj-o ))': {},
# }

sents = [
    'What is the average, minimum and maximum values for sensor1?'
]

rules = {
    '( SBARQ ( WHNP/WHADVP:wh_t ) ( SQ ( VBZ ) ( NP:np_t ) ) )': {
        'np_t': {
            '( NP ( NP  ) ( PP ( IN ) ( NP (NN:obj-o ) ) ) )': {},
            '( NP:subj-o )': {},
        },
        'wh_t': {
            '( WHNP:whnp ( WDT ) ( NN:prop-o ) )': {},
            '( WHNP/WHADVP:qtype-o )': {},
        }
    },
    '( SBARQ:subj-o )': {},
}

# rules = {'( SBARQ ( WHNP/WHADVP:wh_t ) ( SQ ( VBZ ) ( NP:np_t ) ) )'}

keys = ['subj', 'subj_in', 'obj', 'prop', 'qtype']

for sent in sents:
    tree = parser.parse(sent)
    contexts = obj.match_rules(tree, rules, allMatchedContext=True)
    for context in contexts:
        print(", ".join(['%s:%s' % (k, context.get(k)) for k in keys]))


'''------===================Задача 7 (1/10)=====================------
Дано положительное двузначное число. Найдите число десятков в нем.
>*Вводится единственное число (гарантируется, что оно соответствует условию задачи).'''
# n // 10
try:
  n = int (input("Введите двухзначное число:"));
except ValueError:
  print("Ошибка ввода типа");
else:
  if (n > 9 and n < 100):
    print( n // 10 );
  else:
    print("Введите только двухзначное число!!!!!!!")
finally:
  print("Спасибо, до свидания.")


from config import Configure
from snowflake.connector.errors import ProgrammingError
from log import logger
from snowflake_connect import Snowflake_connect
import datetime
import threading
from os import listdir
from os.path import join, isdir

class Remove_old_staged_files:

    def __init__(self, config_file):
        self.config_file = config_file

    def remove_old_staged_files(self, database):
        cone = Configure(self)
        config_data = cone.config()
        conn = Snowflake_connect(self)
        connection = conn.connect()
        connection.cursor().execute("USE WAREHOUSE " + config_data["warehouse"])
        connection.cursor().execute("USE DATABASE " + database)
        connection.cursor().execute("USE SCHEMA " + config_data["schema"])
        connection.cursor().execute("USE ROLE " + config_data["role"])
        cs = connection.cursor()
        try:
            sql = "REMOVE @" + database + " pattern='.*.csv.gz';"
            cs.execute(sql)
            logger.info(sql + " executed")
        except ProgrammingError as db_ex:
            print(f"Programming error: {db_ex}")
            logger.error(f"Programming error: {db_ex}")
            raise
        finally:
            cs.close()
        connection.close()

    def delete_old_staged_files(self):
        con = Configure(self.config_file)
        config_datas = con.config()
        source = config_datas["temp"]
        thread_list = []
        print("remove old stage files:  ", datetime.datetime.now())
        logger.info("Deleting old staged files from database stages")
        for database in listdir(source):
            if isdir(join(source, database)):
                print(database)
                thread = threading.Thread(target=Remove_old_staged_files.remove_old_staged_files,
                                          args=(self.config_file, database))
                thread_list.append(thread)
        for thr in thread_list:
            thr.start()
        for thre in thread_list:
            thre.join()

# dat = Remove_old_staged_files("cred.json")
# dat.delete_old_staged_files()


#  =========================================================================  #
#   Copyright 2018 National Technology & Engineering Solutions of Sandia,
#   LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS,
#   the U.S. Government retains certain rights in this software.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#  =========================================================================  #

from .standard import Standard

class ConvertingRunner(Standard):
    """
    This class represents a standard model for running quantum circuits and adding in errors.
    """

    def __init__(self, seed=None, simulator=None, converter=None):
        """

        Args:
            seed:
        """

        super().__init__(seed, simulator)

        self.converter = converter

    def run_circuit(self, state, circuit, give_output=False, removed_locations=None, gate_dict=None):
        """
        Apply a ``QuantumCircuit`` directly to a state without output.

        Args:
            state:
            circuit:
            give_output:
            removed_locations:
            gate_dict:

        Returns:

        """

        # Note: The circuit could be a tick or a full circuit.

        if self.converter:
            new_circuit = self.converter.compile(circuit)
        else:
            new_circuit = circuit

        result = super().run_circuit(state, new_circuit, give_output=give_output, removed_locations=removed_locations,
                                     gate_dict=gate_dict)

        return result


import sys
import time
import re

start_secs = time.time()
print()

# read in input file
l=[]
my_file = open("inp.txt", "r")
lines = my_file.readlines()
for line in lines:
    s = line.strip()
    l.append(s)
print(len(l))
def calc(n,m,op):
    if op == '+':
        return n + m
    if op == '*':
        return n * m
    print('error in calc: ' + str(n) + ' , ' + str(m) + ' , ' + str(op))

def isOp(c):
    if c == '+' or c == '*':
        return True
    else:
        return False

def plain_equation(s):
    # no parends here
    #1+2*3+4*5+6
    sarr = re.split(' ',s)
    ans = 0
    stack = []
    while True:
        c = sarr[0]
        sarr = sarr[1:]
        if len(stack) == 0 or isOp(c):
            stack.append(c)
        else:
            num = int(c)
            op = stack.pop()
            num1 = int(stack.pop())
            ans = calc(num1,num,op)
            stack.append(ans)
        if len(stack) == 1 and len(sarr) == 0:
            break

    return stack[0]

#1 + 2 * 3 + 4 * 5 + 6
#1 + (2 * (3+1))

sum = 0
for s in l:
    stack = []
    ans = 0
    s = s.replace('(','( ').replace(')',' )')
    sarr =  re.split(' ',s)
    if not '(' in sarr:
        s4 = ''
        for c4 in sarr:
            s4 = s4 + ' ' + c4
        s4 = s4.strip()
        ans = plain_equation(s4)
        sum = sum + ans
        continue

    #2 * 3 + (4 * 5)
    # we have at least one pair of parends
    #print('processing... ' + s)
    while True:
       #print(stack)
        if len(stack) > 0 and len(sarr) == 0 and ''.join(stack).find('(') == -1:
            s3 = ''
            for c3 in stack:
                s3 = s3 + c3 + ' '
            s3 = s3.strip()
            ans = plain_equation(s3)
            sum = sum + ans
            break

        if len(sarr) > 0:
            c = sarr[0]
            sarr = sarr[1:]
        else:
            c = ''
        if c == '':
            c = stack.pop()

        if len(stack) == 0:
            stack.append(c)
        else:
            if c == ')':
                # pop until '('
                s2 = ''
                c2 = stack.pop()
                while c2 != '(':
                    s2 = c2 + ' ' + s2
                    c2 = stack.pop()
                s2 = s2.strip()
                ans = plain_equation(s2)
                stack.append(str(ans))
            else:
                stack.append(c)

        if len(stack) <= 1 and len(sarr) == 0:
            break

print('part 1: ' + str(sum))  # too low: 202553431416

print()
end_secs = time.time()
print(str(end_secs-start_secs))


import struct
from base import base

class bit_field (base):
    def __init__ (self, value, width, max_num=None, endian="<", format="binary", signed=False, full_range=False, fuzzable=True, name=None):
        """
        The bit field primitive represents a number of variable length and is used to define all other integer types.

        Used to sub-class out words, qwords, bytes, and any other binary blocks.

        :type  value:      Integer
        :param value:      Default integer value
        :type  width:      Integer
        :param width:      Width of bit fields
        :type  endian:     Character
        :param endian:     (Optional, def=LITTLE_ENDIAN) Endianess of the bit field (LITTLE_ENDIAN: <, BIG_ENDIAN: >)
        :type  format:     String
        :param format:     (Optional, def=binary) Output format, "binary" or "ascii"
        :type  signed:     Boolean
        :param signed:     (Optional, def=False) Make size signed vs. unsigned (applicable only with format="ascii")
        :type  full_range: Boolean
        :param full_range: (Optional, def=False) If enabled the field mutates through *all* possible values.
        :type  fuzzable:   Boolean
        :param fuzzable:   (Optional, def=True) Enable/disable fuzzing of this primitive
        :type  name:       String
        :param name:       (Optional, def=None) Specifying a name gives you direct access to a primitive
        """

        assert(type(value) is int or type(value) is long)
        assert(type(width) is int or type(value) is long)

        super(bit_field,self).__init__()
        self.value         = self.original_value = value
        self.width         = width
        self.max_num       = max_num
        self.endian        = endian
        self.format        = format
        self.signed        = signed
        self.full_range    = full_range
        self.fuzzable      = fuzzable
        self.name          = name

        self.rendered      = ""        # rendered value
        self.fuzz_complete = False     # flag if this primitive has been completely fuzzed
        self.fuzz_library  = []        # library of fuzz heuristics
        self.mutant_index  = 0         # current mutation number

        if self.max_num is None:
            self.max_num = self.to_decimal("1" * width)

        assert(type(self.max_num) is int or type(self.max_num) is long)

        # build the fuzz library.
        if self.full_range:
            # add all possible values.
            for i in xrange(0, self.max_num):
                self.fuzz_library.append(i)
        else:
            # try only "smart" values.
            self.add_integer_boundaries(0)
            self.add_integer_boundaries(self.max_num / 2)
            self.add_integer_boundaries(self.max_num / 3)
            self.add_integer_boundaries(self.max_num / 4)
            self.add_integer_boundaries(self.max_num / 8)
            self.add_integer_boundaries(self.max_num / 16)
            self.add_integer_boundaries(self.max_num / 32)
            self.add_integer_boundaries(self.max_num)

        # if the optional file '.fuzz_ints' is found, parse each line as a new entry for the fuzz library.
        try:
            fh = open(".fuzz_ints", "r")

            for fuzz_int in fh.readlines():
                # convert the line into an integer, continue on failure.
                try:
                    fuzz_int = long(fuzz_int, 16)
                except:
                    continue

                if fuzz_int <= self.max_num:
                    self.fuzz_library.append(fuzz_int)

            fh.close()
        except:
            pass

    def add_integer_boundaries (self, integer):
        """
        Add the supplied integer and border cases to the integer fuzz heuristics library.

        :type  integer: Int
        :param integer: Integer to append to fuzz heuristics
        """

        for i in xrange(-10, 10):
            case = integer + i

            # ensure the border case falls within the valid range for this field.
            if 0 <= case <= self.max_num:
                if case not in self.fuzz_library:
                    self.fuzz_library.append(case)

    def render (self):
        """
        Render the primitive.
        """

        #
        # binary formatting.
        #

        if self.format == "binary":
            bit_stream = ""
            rendered   = ""

            # pad the bit stream to the next byte boundary.
            if self.width % 8 == 0:
                bit_stream += self.to_binary()
            else:
                bit_stream  = "0" * (8 - (self.width % 8))
                bit_stream += self.to_binary()

            # convert the bit stream from a string of bits into raw bytes.
            for i in xrange(len(bit_stream) / 8):
                chunk = bit_stream[8*i:8*i+8]
                rendered += struct.pack("B", self.to_decimal(chunk))

            # if necessary, convert the endianess of the raw bytes.
            if self.endian == "<":
                rendered = list(rendered)
                rendered.reverse()
                rendered = "".join(rendered)

            self.rendered = rendered

        #
        # ascii formatting.
        #

        else:
            # if the sign flag is raised and we are dealing with a signed integer (first bit is 1).
            if self.signed and self.to_binary()[0] == "1":
                max_num = self.to_decimal("0" + "1" * (self.width - 1))
                # chop off the sign bit.
                val = self.value & max_num

                # account for the fact that the negative scale works backwards.
                val = max_num - val

                # toss in the negative sign.
                self.rendered = "%d" % ~val

            # unsigned integer or positive signed integer.
            else:
                self.rendered = "%d" % self.value

        return self.rendered

    def to_binary (self, number=None, bit_count=None):
        """
        Convert a number to a binary string.

        :type  number:    Integer
        :param number:    (Optional, def=self.value) Number to convert
        :type  bit_count: Integer
        :param bit_count: (Optional, def=self.width) Width of bit string

        :rtype:  String
        :returns: Bit string
        """

        if number is None:
            number = self.value

        if bit_count is None:
            bit_count = self.width

        return "".join(map(lambda x:str((number >> x) & 1), range(bit_count -1, -1, -1)))

    def to_decimal (self, binary):
        """
        Convert a binary string to a decimal number.

        :type  binary: String
        :param binary: Binary string

        :rtype:  Integer
        :returns: Converted bit string
        """

        return int(binary, 2)


"""
 Copyright (C) 2018-2020 Intel Corporation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""
import logging as log
from collections import deque

from extensions.middle.InsertLayoutPropagationTransposes import InsertLayoutPropagationTranspose, \
    mark_as_correct_data_layout
from extensions.middle.pass_separator import PostMiddleStart
from mo.graph.graph import Graph, Node
from mo.middle.replacement import MiddleReplacementPattern

class MarkSubGraphsWithCorrectLayout(MiddleReplacementPattern):
    """
    The transformation looks for the layout agnostic operations which does not have a layout (NCHW or NHWC) and makes
    necessary changes to infer the part of the topology in the original layout:
    1. Prevents from adding Transpose operations before and after "reinterp_shape" like operations which change rank of
    the input and output tensors of this layout agnostic op.
    2. Disable attributes permutation for all intermediate ops between these "reinterp_shape" nodes.

    For now the transformation is triggered for MatMul operation only getting input as 4D or 5D tensors.
    """
    enabled = True
    graph_condition = [lambda graph: graph.graph['layout'] == 'NHWC']
    op_conditions = [lambda n: n.soft_get('op') == 'MatMul' and
                               any([len(port.data.get_shape()) in (4, 5) for port in n.in_ports().values()]),
                     ]

    def run_after(self):
        return [PostMiddleStart]

    def run_before(self):
        return [InsertLayoutPropagationTranspose]

    @staticmethod
    def get_input_nodes(node: Node):
        return [src_port.get_source().node for src_port in node.in_ports().values()]

    @staticmethod
    def get_output_nodes(node: Node):
        result = []
        for out_port in node.out_ports().values():
            for dest_port in out_port.get_destinations():
                result.append(dest_port.node)
        return result

    def bfs(self, start_nodes: list, visited: set, condition: callable = None, forward: bool = True):
        """
        The function performs BFS starting from selected nodes in forward or backward direction adding nodes by an
        optional condition
        :param start_nodes: Nodes to start search from
        :param visited: set of already visited nodes where traversing should not happen
        :param condition: function getting a Node as input and returning whether the node should be included into the
        resukt or not. If the value is None then the node is added unconditionally.
        :param forward: boolean flag specifying the traverse direction
        :return: the list of Nodes visited
        """
        assert visited is not None, 'The "visited" set must be defined'
        assert start_nodes is not None and len(start_nodes) != 0, 'The list of start nodes must be specified'

        result = list()
        d = deque(start_nodes)
        while len(d) != 0:
            cur_node = d.popleft()
            result.append(cur_node)
            visited.add(cur_node)
            if forward:
                next_nodes = self.get_output_nodes(cur_node)
            else:
                next_nodes = self.get_input_nodes(cur_node)
            for next_node in next_nodes:
                if next_node not in visited and (condition is None or condition(next_node)):
                    d.append(next_node)
        return result

    def find_and_replace_pattern(self, graph: Graph):
        visited = set()
        marked_nodes = set()
        condition_forward = lambda n: not InsertLayoutPropagationTranspose.is_nhwc_to_nchw_transpose_needed(n)
        condition_backward = lambda n: not InsertLayoutPropagationTranspose.is_nchw_to_nhwc_transpose_needed(n)
        for node_condition in self.op_conditions:
            for node in graph.get_op_nodes():
                if node_condition(node):
                    log.debug('Detected node "{}" as a node which should be executed in the original layout'
                              ''.format(node.soft_get('name', node.id)))
                    forward_visited_nodes = self.bfs([node], visited, condition_forward, True)
                    backward_visited_nodes = self.bfs([node], visited, condition_backward, False)

                    # find "reinterp_shape" like ops which change rank of input to 4D or 5D from smaller dimensions
                    for back_node in backward_visited_nodes:
                        for input_node in self.get_input_nodes(back_node):
                            if input_node not in backward_visited_nodes and not condition_forward(input_node):
                                marked_nodes.add(input_node)

                    # find "reinterp_shape" like ops which change rank of input from 4D or 5D to smaller dimensions
                    for forward_node in forward_visited_nodes:
                        for output_node in self.get_output_nodes(forward_node):
                            if output_node not in forward_visited_nodes and not condition_backward(output_node):
                                marked_nodes.add(output_node)

                    marked_nodes.update(forward_visited_nodes + backward_visited_nodes)

        if len(marked_nodes):
            log.debug('The following nodes will be executed in the original layout: {}'
                      ''.format([n.soft_get('name', n.id) for n in marked_nodes]))

            # mark all matched nodes as in correct layout and disable attributes permutation for them
            for visited_node in marked_nodes:
                mark_as_correct_data_layout(visited_node)
                visited_node['nchw_layout'] = True


import sys

useConsole = True
useFolder = False

print >>sys.stderr, 'HOMEPATH=(%s)' % HOMEPATH

a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), 'boost.py'], pathex=['Z:\\python projects\\boost-process'])
pyz = PYZ(a.pure)
if (not useFolder):
	exe = EXE( pyz,
		a.scripts,
		a.binaries,
		name='boost.exe',
		debug=False,
		strip=False,
		upx=True,
		console=useConsole )
else:
	exe = EXE(pyz,
		a.scripts,
		exclude_binaries=1,
		name='buildboost/boost.exe',
		debug=False,
		strip=False,
		upx=True,
		console=useConsole )
	coll = COLLECT( exe,
		a.binaries,
		strip=False,
		upx=True,
		name='distboost')


#!/usr/bin/env python3

from tkinter import *

root = Tk()

t = Text(root, height=2, width=30)
t.pack()
t.insert(END, "Just a text Widget\nin tow lines")

root.mainloop()


from argparse import ArgumentParser
import paho.mqtt.client as mqtt
import json
import requests

from shutter import shutter

def main():
    parser = ArgumentParser()
    parser.add_argument('token')
    parser.add_argument('--topic', default='home/shutter')
    parser.add_argument('--cacert', default='mqtt.beebotte.com.pem')
    parser.add_argument('--slack', help='Slack webhook URL', default=None)

    args = parser.parse_args()

    TOKEN = args.token
    HOSTNAME = "mqtt.beebotte.com"
    PORT = 8883
    TOPIC = args.topic
    CACERT = args.cacert
    SLACK = args.slack

    def post_to_slack(message):
        if SLACK:
            requests.post(SLACK, headers={'content-type': 'application/json'}, data=json.dumps({'text': message}))

    def on_connect(client, userdata, flags, respons_code):
        print(f'status {respons_code}')
        client.subscribe(TOPIC)

    def on_message(client, userdata, msg):
        print(msg.topic + " " + str(msg.payload))
        data = json.loads(msg.payload.decode("utf-8"))["data"]
        print(data)
        if data == 'open':
            post_to_slack('[INFO] Open myshutter.')
            shutter.setup()
            shutter.cmd(shutter.PIN_UP)
            shutter.cleanup()
        elif data == 'close':
            post_to_slack('[INFO] Close myshutter.')
            shutter.setup()
            shutter.setup()
            shutter.cmd(shutter.PIN_DOWN)
            shutter.cleanup()
        elif data == 'stop':
            post_to_slack('[INFO] Stop myshutter.')
            shutter.setup()
            shutter.cmd(shutter.PIN_STOP)
            shutter.cleanup()

    client = mqtt.Client()
    client.username_pw_set("token:%s"%TOKEN)
    client.on_connect = on_connect
    client.on_message = on_message
    client.tls_set(CACERT)

    try:
        client.connect(HOSTNAME, port=PORT, keepalive=60)
        post_to_slack('[INFO] myshutter is launched.')
        client.loop_forever()
    except Exception as e:
        post_to_slack(f'[ERROR] myshtter is terminated. Error message: {e}.')

if __name__ == '__main__':
    main()


"""Send an sms with 'kdeconnect-cli --send-sms' """
import argparse
import re
import subprocess
import sys

def print_device_id():
    """
    User chooses a device ID
    return: the device ID
    """
    print('Searching for device ids...')
    device_out, stderr = subprocess.Popen(['kdeconnect-cli', '-l'],
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT).communicate()

    device_id = list(re.findall(r'(?::\s)([\w]+)', str(device_out)))
    print('Device IDs On System:')
    for _id in device_id:
        print(f'\t{_id}')

def sendsms(phone_num, sms, d_id=None, bomb=False):
    """
    Send an sms via the kdeconnect-cli --send-sms <sms> --destination <phone_number> --device <device_id> command
    ;param phone_num: The phone number to send the sms to
    ;param sms: The sms to be sent
    ;param bomb: A boolean that indicates whether or not to send one letter at a time
    return: Print statement stating whether or not the sms was sent
    """

    # get the device id
    if not d_id:
        device_out, _ = subprocess.Popen(['kdeconnect-cli', '-l'],
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT).communicate()

        # check if device id is correct
        error = str(device_out).startswith('b"error: No such object path')
        if error:
            return False

        d_id = list(re.findall(r'(?::\s)([\w]+)', str(device_out)))[0]
        if not d_id:
            return False

    # send sms
    if bomb:
        sms = list(sms)
        for letter in sms:
            output, _ = subprocess.Popen(['kdeconnect-cli',
                                          '--send-sms', letter,
                                          '--destination', phone_num,
                                          '--device', d_id],
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT).communicate()
            error = str(output).startswith('b"error: No such object path')
            if error:
                return False
    else:
        output, _ = subprocess.Popen(['kdeconnect-cli',
                                      '--send-sms', sms,
                                      '--destination', phone_num,
                                      '--device', d_id],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT).communicate()
        error = str(output).startswith('b"error: No such object path')
        if error:
            return False

    print(f'Message sent to {phone_num}')
    return True

def main():
    """
    Parse arguments and sendsms
    :return: None
    """
    parser = argparse.ArgumentParser(description='Send an SMS via KDE "kdeconnect-cli --send-sms"')
    parser.add_argument('phonenumber', metavar='p', type=str,
                        help='The phone number you would like to send an sms to.')
    parser.add_argument('message', metavar='m', type=str, nargs='+',
                        help='The message you would like to send.')
    parser.add_argument('-d', '--device_id',
                        help='Override the default device id, and provide your own.')
    parser.add_argument('-b', '--bomb', type=bool, default=False, const=True, nargs='?',
                        help='Send one letter at a time')

    args = parser.parse_args()
    args.message = ' '.join(args.message)

    if args.device_id:
        # if the user provides a device id
        print(f'Using Device ID: {args.device_id}')
        result = sendsms(args.phonenumber, args.message, args.device_id)
        if not result:
            print('Error: Please verify your Device ID', file=sys.stderr)
            print_device_id()
    else:
        sendsms(args.phonenumber, args.message, bomb=args.bomb)

if __name__ == '__main__':
    main()


import threading
import serial
from model import Account, FaceFeature, AssociationRecord, RecognitionRecord, db
from time import sleep, time
import json
import socket
import random
import string

class Device:
    port = ''
    baudrate = 0
    IP = ''

    s: serial.Serial = None
    message = []
    recv_callback = lambda x, y: (x, y)
    db = None
    app = None

    def __init__(self, port, baudrate=115200, IP=None):
        self.port = port
        self.baudrate = baudrate

    def init_app(self, app, db, port=None, baudrate=None, recv_callback=None):
        self.app = app
        self.db = db
        self.recv_callback = recv_callback

        if port:
            self.port = port
        if baudrate:
            self.baudrate = baudrate

    def start_loop(self):
        while True:
            try:
                self.s = serial.Serial(self.port, self.baudrate)
                break
            except Exception as e:
                print(e)
        thr = threading.Thread(target=self._loop)
        thr.start()
        return self.s.is_open

    def _loop(self):
        while True:

            obj = self._recv_message()
            self.recv_callback(obj)
            # self.message.append(obj)
            msg_type = obj.get('type')
            if msg_type == 'face_info':
                if obj.get("msg") == 'have face':
                    print(obj['info'].get("score"))
                    if obj['info'].get("score") > 90:
                        if self.db:
                            with self.app.app_context():
                                face_feature: FaceFeature = FaceFeature.query.get(obj['info'].get('uid'))
                                if face_feature:
                                    record: RecognitionRecord = RecognitionRecord(face_feature=face_feature)

                                    if face_feature.account is not None:
                                        self.recv_callback(f"为{face_feature.account.name}开门")
                                        s = socket.socket()
                                        s.connect(('10.18.52.130', 8080))
                                        s.send(b'b1e')
                                        s.close()
                                    self.db.session.add(record)
                                    self.db.session.commit()
                                else:
                                    face_feature: FaceFeature = FaceFeature(UID=obj['info'].get('uid'),
                                                                            feature=obj['info'].get("feature"))
                                    self.db.session.add(face_feature)
                                    self.db.session.commit()
                    elif obj['info'].get("score") <= 60:
                        if obj['info'].get('feature') == 'null':
                            # 未知的脸
                            self._set_cfg(auto_out_feature=1, out_feature=1)  # 设置为不识别只获取
                            # print("_set_cfg 1")
                            pass
                        else:
                            if self.db:
                                uid = hex(int(time() * 0xf))[2:].upper()
                                uid = uid + "".join(
                                    random.sample(string.ascii_uppercase + string.digits, 32 - len(uid))
                                )  # 防止2038问题
                                with self.app.app_context():
                                    face_feature: FaceFeature = self.save_feature(uid, obj['info'].get('feature'),
                                                                                commit=False)
                                    record: RecognitionRecord = RecognitionRecord(face_feature=face_feature)
                                    self.db.session.add(record)
                                    self.db.session.commit()
                            self._set_cfg(auto_out_feature=2, out_feature=1)  # 设置为识别
                            # print("_set_cfg 0")
            # 2^31 = 2*2^30 = 2*4^15 = 0.5 * 4^16 = 0.5 * 16^4
            print(obj)

    @staticmethod
    def _build_json(type: str, param: dict, version=1, encoding='utf-8') -> bytes:
        data = {
            'version': version,
            'type': type,
        }
        data.update(param)
        data = json.dumps(data) + "\r\n"
        return data.encode(encoding)

    @staticmethod
    def _parse_result(data: bytes, encoding='utf-8') -> dict:
        return json.loads(data, encoding=encoding)

    def _send_message(self, data: bytes) -> None:
        assert data.endswith(b'\r\n')
        assert data.count(b'\r\n') == 1

        while not self.s.writable():
            pass
        self.s.write(data)
        print('send', data)

    def _recv_message(self) -> dict:
        data = b''
        while True:
            data += self.s.read_all()
            if not data.endswith(b'\r\n'):
                sleep(0.02)
                continue
            else:
                obj = self._parse_result(data)
                return obj

    def save_feature(self, UID: str, feature: str, account_sj_ID=None, commit=True) -> FaceFeature:
        assert len(feature) == 264
        assert len(UID) == 32
        print('save_feature')
        face_feature = FaceFeature(UID=UID, feature=feature, account_sj_ID=account_sj_ID)
        self.db.session.add(face_feature)
        self._add_uer_by_fea(face_feature.UID, face_feature.feature)
        self._recv_message()
        if commit:
            self.db.session.commit()
        return face_feature

    def _add_uer_by_fea(self, UID: str, feature: str):
        data = self._build_json(
            'add_uer_by_fea',
            {'user': {
                'uid': UID,
                'fea': feature
            }}
        )
        self._send_message(data)

    def _set_cfg(
            self,
            uart_baud=115200,
            out_feature=0,
            open_delay=1,
            pkt_fix=0,
            auto_out_feature=0,
            out_interval_in_ms=500,
            fea_gate=70,
    ):
        data = self._build_json(
            'set_cfg',
            {'cfg': {
                'uart_baud': uart_baud,
                'out_feature': out_feature,
                'open_delay': open_delay,
                'pkt_fix': pkt_fix,
                'auto_out_feature': auto_out_feature,
                'out_interval_in_ms': out_interval_in_ms,
                'fea_gate': fea_gate,
            }}
        )
        self._send_message(data)

        def __del__(self):
            self.s.close()

device = Device('COM6', 115200)

if __name__ == "__main__":
    device = Device('COM6', 115200)
    device.start_loop()


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

import sys
import httplib2
from bs4 import BeautifulSoup

def get_article_text(http, url):
	response, content = http.request(url,
		headers={
			'Accept-Encoding': 'gzip,deflate'
		})
	text = ''
	soup = BeautifulSoup(content)
	for p_tag in soup.find('div', class_ = 'content').find_all('p'):
		if p_tag.get('class') or p_tag.find('script'):
			break
		line = p_tag.get_text().replace('www.daomubiji.com', '')
		span_tag = p_tag.find('span')
		if span_tag:
			text += line.replace(span_tag.get_text(), '') + '\n'
			break
		else:
			text += line + '\n'
	return text

if __name__ == '__main__':
	http = httplib2.Http('.cache')
	response, content = http.request('http://www.daomubiji.com/',
		headers={
			'Accept-Encoding': 'gzip,deflate'
		})
	index_soup = BeautifulSoup(content)
	for table_tag in index_soup.find_all('table'):
		chapter = table_tag.find('h2').get_text()
		for a_tag in table_tag.find_all('a'):
			article_url = a_tag['href']
			text = get_article_text(http, article_url)
			with open(chapter + '.txt', 'a', encoding = 'utf-8') as f:
				f.write(text)


from pathlib import Path
from subprocess import run

from pkg_resources import get_distribution

def test_dependencies_directed():
    """Checks whether there are cycles in the graph of dependencies

    This codebase defines a component as one directory down, so
    ``cascade.core`` is at the bottom and ``cascade.executor`` at
    the top of the dependency graph. If this test fails, then there
    is a cycle in that graph of dependencies among the subdirectories one
    level down. That can lead to very difficult-to-debug import problems
    later. Fix it now.
    """
    package_root = Path(get_distribution("cascade").location)
    # Run this as a subprocess because it loads and deletes modules, which
    # sincerely messes with pytest's ability to construct mock objects
    # in conftest.py.
    result = run(f"python -m cascade.testing_utilities.component_dependencies {package_root} cascade".split())
    # That command won't run at all if the library isn't unpacked into
    # source files, so ignore when it fails entirely.
    if result.returncode in {0, 3776}:
        assert result.returncode == 0, f"{result.stdout} {result.stderr}"

def test_is_namespace_package():
    """
    Cascade is a namespace package. The ``cascade_config`` package is in
    the same namespace. Namespace packages don't have an ``__init__.py``
    in the base directory. That's it. That's all you have to do is not
    put an ``__init__.py`` in the base directory, and you're good.
    """
    package_root = Path(get_distribution("cascade").location)
    print(package_root)
    assert not (package_root / "cascade" / "__init__.py").exists()